1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
17 "git.curoverse.com/arvados.git/sdk/go/config"
18 "git.curoverse.com/arvados.git/sdk/go/keepclient"
19 "github.com/coreos/go-systemd/daemon"
24 // A Keep "block" is 64MB.
25 const BlockSize = 64 * 1024 * 1024
27 // A Keep volume must have at least MinFreeKilobytes available
28 // in order to permit writes.
29 const MinFreeKilobytes = BlockSize / 1024
31 // ProcMounts /proc/mounts
32 var ProcMounts = "/proc/mounts"
38 type KeepError struct {
44 BadRequestError = &KeepError{400, "Bad Request"}
45 UnauthorizedError = &KeepError{401, "Unauthorized"}
46 CollisionError = &KeepError{500, "Collision"}
47 RequestHashError = &KeepError{422, "Hash mismatch in request"}
48 PermissionError = &KeepError{403, "Forbidden"}
49 DiskHashError = &KeepError{500, "Hash mismatch in stored data"}
50 ExpiredError = &KeepError{401, "Expired permission signature"}
51 NotFoundError = &KeepError{404, "Not Found"}
52 GenericError = &KeepError{500, "Fail"}
53 FullError = &KeepError{503, "Full"}
54 SizeRequiredError = &KeepError{411, "Missing Content-Length"}
55 TooLongError = &KeepError{413, "Block is too large"}
56 MethodDisabledError = &KeepError{405, "Method disabled"}
57 ErrNotImplemented = &KeepError{500, "Unsupported configuration"}
58 ErrClientDisconnect = &KeepError{503, "Client disconnected"}
61 func (e *KeepError) Error() string {
65 // ========================
66 // Internal data structures
68 // These global variables are used by multiple parts of the
69 // program. They are good candidates for moving into their own
72 // The Keep VolumeManager maintains a list of available volumes.
73 // Initialized by the --volumes flag (or by FindKeepVolumes).
74 var KeepVM VolumeManager
76 // The pull list manager and trash queue are threadsafe queues which
77 // support atomic update operations. The PullHandler and TrashHandler
78 // store results from Data Manager /pull and /trash requests here.
80 // See the Keep and Data Manager design documents for more details:
81 // https://arvados.org/projects/arvados/wiki/Keep_Design_Doc
82 // https://arvados.org/projects/arvados/wiki/Data_Manager_Design_Doc
88 deprecated.beforeFlagParse(theConfig)
90 dumpConfig := flag.Bool("dump-config", false, "write current configuration to stdout and exit (useful for migrating from command line flags to config file)")
91 getVersion := flag.Bool("version", false, "Print version information and exit.")
93 defaultConfigPath := "/etc/arvados/keepstore/keepstore.yml"
99 "YAML or JSON configuration file `path`")
103 // Print version information if requested
105 fmt.Printf("keepstore %s\n", version)
109 deprecated.afterFlagParse(theConfig)
111 err := config.LoadFile(theConfig, configPath)
112 if err != nil && (!os.IsNotExist(err) || configPath != defaultConfigPath) {
117 log.Fatal(config.DumpAndExit(theConfig))
120 log.Printf("keepstore %s started", version)
122 err = theConfig.Start()
127 if pidfile := theConfig.PIDFile; pidfile != "" {
128 f, err := os.OpenFile(pidfile, os.O_RDWR|os.O_CREATE, 0777)
130 log.Fatalf("open pidfile (%s): %s", pidfile, err)
133 err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
135 log.Fatalf("flock pidfile (%s): %s", pidfile, err)
137 defer os.Remove(pidfile)
140 log.Fatalf("truncate pidfile (%s): %s", pidfile, err)
142 _, err = fmt.Fprint(f, os.Getpid())
144 log.Fatalf("write pidfile (%s): %s", pidfile, err)
148 log.Fatalf("sync pidfile (%s): %s", pidfile, err)
152 log.Println("keepstore starting, pid", os.Getpid())
153 defer log.Println("keepstore exiting, pid", os.Getpid())
155 // Start a round-robin VolumeManager with the volumes we have found.
156 KeepVM = MakeRRVolumeManager(theConfig.Volumes)
158 // Middleware/handler stack
159 router := MakeRESTRouter()
161 // Set up a TCP listener.
162 listener, err := net.Listen("tcp", theConfig.Listen)
167 // Initialize keepclient for pull workers
168 keepClient := &keepclient.KeepClient{
169 Arvados: &arvadosclient.ArvadosClient{},
173 // Initialize the pullq and workers
174 pullq = NewWorkQueue()
175 for i := 0; i < 1 || i < theConfig.PullWorkers; i++ {
176 go RunPullWorker(pullq, keepClient)
179 // Initialize the trashq and workers
180 trashq = NewWorkQueue()
181 for i := 0; i < 1 || i < theConfig.TrashWorkers; i++ {
182 go RunTrashWorker(trashq)
185 // Start emptyTrash goroutine
186 doneEmptyingTrash := make(chan bool)
187 go emptyTrash(doneEmptyingTrash, theConfig.TrashCheckInterval.Duration())
189 // Shut down the server gracefully (by closing the listener)
190 // if SIGTERM is received.
191 term := make(chan os.Signal, 1)
192 go func(sig <-chan os.Signal) {
194 log.Println("caught signal:", s)
195 doneEmptyingTrash <- true
198 signal.Notify(term, syscall.SIGTERM)
199 signal.Notify(term, syscall.SIGINT)
201 if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
202 log.Printf("Error notifying init daemon: %v", err)
204 log.Println("listening at", listener.Addr())
210 // Periodically (once per interval) invoke EmptyTrash on all volumes.
211 func emptyTrash(done <-chan bool, interval time.Duration) {
212 ticker := time.NewTicker(interval)
217 for _, v := range theConfig.Volumes {