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