14 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
15 "git.curoverse.com/arvados.git/sdk/go/config"
16 "git.curoverse.com/arvados.git/sdk/go/httpserver"
17 "git.curoverse.com/arvados.git/sdk/go/keepclient"
18 "github.com/coreos/go-systemd/daemon"
19 "github.com/ghodss/yaml"
22 // A Keep "block" is 64MB.
23 const BlockSize = 64 * 1024 * 1024
25 // A Keep volume must have at least MinFreeKilobytes available
26 // in order to permit writes.
27 const MinFreeKilobytes = BlockSize / 1024
29 // ProcMounts /proc/mounts
30 var ProcMounts = "/proc/mounts"
36 type KeepError struct {
42 BadRequestError = &KeepError{400, "Bad Request"}
43 UnauthorizedError = &KeepError{401, "Unauthorized"}
44 CollisionError = &KeepError{500, "Collision"}
45 RequestHashError = &KeepError{422, "Hash mismatch in request"}
46 PermissionError = &KeepError{403, "Forbidden"}
47 DiskHashError = &KeepError{500, "Hash mismatch in stored data"}
48 ExpiredError = &KeepError{401, "Expired permission signature"}
49 NotFoundError = &KeepError{404, "Not Found"}
50 GenericError = &KeepError{500, "Fail"}
51 FullError = &KeepError{503, "Full"}
52 SizeRequiredError = &KeepError{411, "Missing Content-Length"}
53 TooLongError = &KeepError{413, "Block is too large"}
54 MethodDisabledError = &KeepError{405, "Method disabled"}
55 ErrNotImplemented = &KeepError{500, "Unsupported configuration"}
56 ErrClientDisconnect = &KeepError{503, "Client disconnected"}
59 func (e *KeepError) Error() string {
63 // ========================
64 // Internal data structures
66 // These global variables are used by multiple parts of the
67 // program. They are good candidates for moving into their own
70 // The Keep VolumeManager maintains a list of available volumes.
71 // Initialized by the --volumes flag (or by FindKeepVolumes).
72 var KeepVM VolumeManager
74 // The pull list manager and trash queue are threadsafe queues which
75 // support atomic update operations. The PullHandler and TrashHandler
76 // store results from Data Manager /pull and /trash requests here.
78 // See the Keep and Data Manager design documents for more details:
79 // https://arvados.org/projects/arvados/wiki/Keep_Design_Doc
80 // https://arvados.org/projects/arvados/wiki/Data_Manager_Design_Doc
86 deprecated.beforeFlagParse(theConfig)
88 dumpConfig := flag.Bool("dump-config", false, "write current configuration to stdout and exit (useful for migrating from command line flags to config file)")
90 defaultConfigPath := "/etc/arvados/keepstore/keepstore.yml"
96 "YAML or JSON configuration file `path`")
100 deprecated.afterFlagParse(theConfig)
102 err := config.LoadFile(theConfig, configPath)
103 if err != nil && (!os.IsNotExist(err) || configPath != defaultConfigPath) {
108 y, err := yaml.Marshal(theConfig)
116 err = theConfig.Start()
118 if pidfile := theConfig.PIDFile; pidfile != "" {
119 f, err := os.OpenFile(pidfile, os.O_RDWR|os.O_CREATE, 0777)
121 log.Fatalf("open pidfile (%s): %s", pidfile, err)
124 err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
126 log.Fatalf("flock pidfile (%s): %s", pidfile, err)
128 defer os.Remove(pidfile)
131 log.Fatalf("truncate pidfile (%s): %s", pidfile, err)
133 _, err = fmt.Fprint(f, os.Getpid())
135 log.Fatalf("write pidfile (%s): %s", pidfile, err)
139 log.Fatalf("sync pidfile (%s): %s", pidfile, err)
143 log.Println("keepstore starting, pid", os.Getpid())
144 defer log.Println("keepstore exiting, pid", os.Getpid())
146 // Start a round-robin VolumeManager with the volumes we have found.
147 KeepVM = MakeRRVolumeManager(theConfig.Volumes)
149 // Middleware stack: logger, MaxRequests limiter, method handlers
150 http.Handle("/", &LoggingRESTRouter{
151 httpserver.NewRequestLimiter(theConfig.MaxRequests,
155 // Set up a TCP listener.
156 listener, err := net.Listen("tcp", theConfig.Listen)
161 // Initialize Pull queue and worker
162 keepClient := &keepclient.KeepClient{
163 Arvados: &arvadosclient.ArvadosClient{},
165 Client: &http.Client{},
168 // Initialize the pullq and worker
169 pullq = NewWorkQueue()
170 go RunPullWorker(pullq, keepClient)
172 // Initialize the trashq and worker
173 trashq = NewWorkQueue()
174 go RunTrashWorker(trashq)
176 // Start emptyTrash goroutine
177 doneEmptyingTrash := make(chan bool)
178 go emptyTrash(doneEmptyingTrash, theConfig.TrashCheckInterval.Duration())
180 // Shut down the server gracefully (by closing the listener)
181 // if SIGTERM is received.
182 term := make(chan os.Signal, 1)
183 go func(sig <-chan os.Signal) {
185 log.Println("caught signal:", s)
186 doneEmptyingTrash <- true
189 signal.Notify(term, syscall.SIGTERM)
190 signal.Notify(term, syscall.SIGINT)
192 if _, err := daemon.SdNotify("READY=1"); err != nil {
193 log.Printf("Error notifying init daemon: %v", err)
195 log.Println("listening at", listener.Addr())
196 srv := &http.Server{}
200 // Periodically (once per interval) invoke EmptyTrash on all volumes.
201 func emptyTrash(done <-chan bool, interval time.Duration) {
202 ticker := time.NewTicker(interval)
207 for _, v := range theConfig.Volumes {