8936: update blob-signing-ttl related documentation.
[arvados.git] / services / keepstore / keepstore.go
1 package main
2
3 import (
4         "bytes"
5         "flag"
6         "fmt"
7         "git.curoverse.com/arvados.git/sdk/go/keepclient"
8         "io/ioutil"
9         "log"
10         "net"
11         "net/http"
12         "os"
13         "os/signal"
14         "strings"
15         "syscall"
16         "time"
17 )
18
19 // ======================
20 // Configuration settings
21 //
22 // TODO(twp): make all of these configurable via command line flags
23 // and/or configuration file settings.
24
25 // Default TCP address on which to listen for requests.
26 // Initialized by the --listen flag.
27 const DefaultAddr = ":25107"
28
29 // A Keep "block" is 64MB.
30 const BlockSize = 64 * 1024 * 1024
31
32 // A Keep volume must have at least MinFreeKilobytes available
33 // in order to permit writes.
34 const MinFreeKilobytes = BlockSize / 1024
35
36 // ProcMounts /proc/mounts
37 var ProcMounts = "/proc/mounts"
38
39 // enforcePermissions controls whether permission signatures
40 // should be enforced (affecting GET and DELETE requests).
41 // Initialized by the -enforce-permissions flag.
42 var enforcePermissions bool
43
44 // blobSignatureTTL is the time duration for which new permission
45 // signatures (returned by PUT requests) will be valid.
46 // Initialized by the -permission-ttl flag.
47 var blobSignatureTTL time.Duration
48
49 // dataManagerToken represents the API token used by the
50 // Data Manager, and is required on certain privileged operations.
51 // Initialized by the -data-manager-token-file flag.
52 var dataManagerToken string
53
54 // neverDelete can be used to prevent the DELETE handler from
55 // actually deleting anything.
56 var neverDelete = true
57
58 // trashLifetime is the time duration after a block is trashed
59 // during which it can be recovered using an /untrash request
60 // Use 10s or 10m or 10h to set as 10 seconds or minutes or hours respectively.
61 var trashLifetime time.Duration
62
63 // trashCheckInterval is the time duration at which the emptyTrash goroutine
64 // will check and delete expired trashed blocks. Default is one day.
65 // Use 10s or 10m or 10h to set as 10 seconds or minutes or hours respectively.
66 var trashCheckInterval time.Duration
67
68 var maxBuffers = 128
69 var bufs *bufferPool
70
71 // KeepError types.
72 //
73 type KeepError struct {
74         HTTPCode int
75         ErrMsg   string
76 }
77
78 var (
79         BadRequestError     = &KeepError{400, "Bad Request"}
80         UnauthorizedError   = &KeepError{401, "Unauthorized"}
81         CollisionError      = &KeepError{500, "Collision"}
82         RequestHashError    = &KeepError{422, "Hash mismatch in request"}
83         PermissionError     = &KeepError{403, "Forbidden"}
84         DiskHashError       = &KeepError{500, "Hash mismatch in stored data"}
85         ExpiredError        = &KeepError{401, "Expired permission signature"}
86         NotFoundError       = &KeepError{404, "Not Found"}
87         GenericError        = &KeepError{500, "Fail"}
88         FullError           = &KeepError{503, "Full"}
89         SizeRequiredError   = &KeepError{411, "Missing Content-Length"}
90         TooLongError        = &KeepError{413, "Block is too large"}
91         MethodDisabledError = &KeepError{405, "Method disabled"}
92         ErrNotImplemented   = &KeepError{500, "Unsupported configuration"}
93 )
94
95 func (e *KeepError) Error() string {
96         return e.ErrMsg
97 }
98
99 // ========================
100 // Internal data structures
101 //
102 // These global variables are used by multiple parts of the
103 // program. They are good candidates for moving into their own
104 // packages.
105
106 // The Keep VolumeManager maintains a list of available volumes.
107 // Initialized by the --volumes flag (or by FindKeepVolumes).
108 var KeepVM VolumeManager
109
110 // The pull list manager and trash queue are threadsafe queues which
111 // support atomic update operations. The PullHandler and TrashHandler
112 // store results from Data Manager /pull and /trash requests here.
113 //
114 // See the Keep and Data Manager design documents for more details:
115 // https://arvados.org/projects/arvados/wiki/Keep_Design_Doc
116 // https://arvados.org/projects/arvados/wiki/Data_Manager_Design_Doc
117 //
118 var pullq *WorkQueue
119 var trashq *WorkQueue
120
121 type volumeSet []Volume
122
123 var (
124         flagSerializeIO bool
125         flagReadonly    bool
126         volumes         volumeSet
127 )
128
129 func (vs *volumeSet) String() string {
130         return fmt.Sprintf("%+v", (*vs)[:])
131 }
132
133 // TODO(twp): continue moving as much code as possible out of main
134 // so it can be effectively tested. Esp. handling and postprocessing
135 // of command line flags (identifying Keep volumes and initializing
136 // permission arguments).
137
138 func main() {
139         log.Println("keepstore starting, pid", os.Getpid())
140         defer log.Println("keepstore exiting, pid", os.Getpid())
141
142         var (
143                 dataManagerTokenFile string
144                 listen               string
145                 blobSigningKeyFile   string
146                 permissionTTLSec     int
147                 pidfile              string
148         )
149         flag.StringVar(
150                 &dataManagerTokenFile,
151                 "data-manager-token-file",
152                 "",
153                 "File with the API token used by the Data Manager. All DELETE "+
154                         "requests or GET /index requests must carry this token.")
155         flag.BoolVar(
156                 &enforcePermissions,
157                 "enforce-permissions",
158                 false,
159                 "Enforce permission signatures on requests.")
160         flag.StringVar(
161                 &listen,
162                 "listen",
163                 DefaultAddr,
164                 "Listening address, in the form \"host:port\". e.g., 10.0.1.24:8000. Omit the host part to listen on all interfaces.")
165         flag.BoolVar(
166                 &neverDelete,
167                 "never-delete",
168                 true,
169                 "If true, nothing will be deleted. "+
170                         "Warning: the relevant features in keepstore and data manager have not been extensively tested. "+
171                         "You should leave this option alone unless you can afford to lose data.")
172         flag.StringVar(
173                 &blobSigningKeyFile,
174                 "permission-key-file",
175                 "",
176                 "Synonym for -blob-signing-key-file.")
177         flag.StringVar(
178                 &blobSigningKeyFile,
179                 "blob-signing-key-file",
180                 "",
181                 "File containing the secret key for generating and verifying "+
182                         "blob permission signatures.")
183         flag.IntVar(
184                 &permissionTTLSec,
185                 "permission-ttl",
186                 0,
187                 "Synonym for -blob-signature-ttl.")
188         flag.IntVar(
189                 &permissionTTLSec,
190                 "blob-signature-ttl",
191                 int(time.Duration(2*7*24*time.Hour).Seconds()),
192                 "Lifetime of blob permission signatures. This will become a part of the signing key, "+
193                         "and will cause clients to retry or fail if changed while they are in progress. "+
194                         "See services/api/config/application.default.yml.")
195         flag.BoolVar(
196                 &flagSerializeIO,
197                 "serialize",
198                 false,
199                 "Serialize read and write operations on the following volumes.")
200         flag.BoolVar(
201                 &flagReadonly,
202                 "readonly",
203                 false,
204                 "Do not write, delete, or touch anything on the following volumes.")
205         flag.StringVar(
206                 &pidfile,
207                 "pid",
208                 "",
209                 "Path to write pid file during startup. This file is kept open and locked with LOCK_EX until keepstore exits, so `fuser -k pidfile` is one way to shut down. Exit immediately if there is an error opening, locking, or writing the pid file.")
210         flag.IntVar(
211                 &maxBuffers,
212                 "max-buffers",
213                 maxBuffers,
214                 fmt.Sprintf("Maximum RAM to use for data buffers, given in multiples of block size (%d MiB). When this limit is reached, HTTP requests requiring buffers (like GET and PUT) will wait for buffer space to be released.", BlockSize>>20))
215         flag.DurationVar(
216                 &trashLifetime,
217                 "trash-lifetime",
218                 0*time.Second,
219                 "Time duration after a block is trashed during which it can be recovered using an /untrash request")
220         flag.DurationVar(
221                 &trashCheckInterval,
222                 "trash-check-interval",
223                 24*time.Hour,
224                 "Time duration at which the emptyTrash goroutine will check and delete expired trashed blocks. Default is one day.")
225
226         flag.Parse()
227
228         if maxBuffers < 0 {
229                 log.Fatal("-max-buffers must be greater than zero.")
230         }
231         bufs = newBufferPool(maxBuffers, BlockSize)
232
233         if pidfile != "" {
234                 f, err := os.OpenFile(pidfile, os.O_RDWR|os.O_CREATE, 0777)
235                 if err != nil {
236                         log.Fatalf("open pidfile (%s): %s", pidfile, err)
237                 }
238                 err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
239                 if err != nil {
240                         log.Fatalf("flock pidfile (%s): %s", pidfile, err)
241                 }
242                 err = f.Truncate(0)
243                 if err != nil {
244                         log.Fatalf("truncate pidfile (%s): %s", pidfile, err)
245                 }
246                 _, err = fmt.Fprint(f, os.Getpid())
247                 if err != nil {
248                         log.Fatalf("write pidfile (%s): %s", pidfile, err)
249                 }
250                 err = f.Sync()
251                 if err != nil {
252                         log.Fatalf("sync pidfile (%s): %s", pidfile, err)
253                 }
254                 defer f.Close()
255                 defer os.Remove(pidfile)
256         }
257
258         if len(volumes) == 0 {
259                 if (&unixVolumeAdder{&volumes}).Discover() == 0 {
260                         log.Fatal("No volumes found.")
261                 }
262         }
263
264         for _, v := range volumes {
265                 log.Printf("Using volume %v (writable=%v)", v, v.Writable())
266         }
267
268         // Initialize data manager token and permission key.
269         // If these tokens are specified but cannot be read,
270         // raise a fatal error.
271         if dataManagerTokenFile != "" {
272                 if buf, err := ioutil.ReadFile(dataManagerTokenFile); err == nil {
273                         dataManagerToken = strings.TrimSpace(string(buf))
274                 } else {
275                         log.Fatalf("reading data manager token: %s\n", err)
276                 }
277         }
278
279         if neverDelete != true {
280                 log.Print("never-delete is not set. Warning: the relevant features in keepstore and data manager have not " +
281                         "been extensively tested. You should leave this option alone unless you can afford to lose data.")
282         }
283
284         if blobSigningKeyFile != "" {
285                 if buf, err := ioutil.ReadFile(blobSigningKeyFile); err == nil {
286                         PermissionSecret = bytes.TrimSpace(buf)
287                 } else {
288                         log.Fatalf("reading permission key: %s\n", err)
289                 }
290         }
291
292         blobSignatureTTL = time.Duration(permissionTTLSec) * time.Second
293
294         if PermissionSecret == nil {
295                 if enforcePermissions {
296                         log.Fatal("-enforce-permissions requires a permission key")
297                 } else {
298                         log.Println("Running without a PermissionSecret. Block locators " +
299                                 "returned by this server will not be signed, and will be rejected " +
300                                 "by a server that enforces permissions.")
301                         log.Println("To fix this, use the -blob-signing-key-file flag " +
302                                 "to specify the file containing the permission key.")
303                 }
304         }
305
306         // Start a round-robin VolumeManager with the volumes we have found.
307         KeepVM = MakeRRVolumeManager(volumes)
308
309         // Tell the built-in HTTP server to direct all requests to the REST router.
310         loggingRouter := MakeLoggingRESTRouter()
311         http.HandleFunc("/", func(resp http.ResponseWriter, req *http.Request) {
312                 loggingRouter.ServeHTTP(resp, req)
313         })
314
315         // Set up a TCP listener.
316         listener, err := net.Listen("tcp", listen)
317         if err != nil {
318                 log.Fatal(err)
319         }
320
321         // Initialize Pull queue and worker
322         keepClient := &keepclient.KeepClient{
323                 Arvados:       nil,
324                 Want_replicas: 1,
325                 Client:        &http.Client{},
326         }
327
328         // Initialize the pullq and worker
329         pullq = NewWorkQueue()
330         go RunPullWorker(pullq, keepClient)
331
332         // Initialize the trashq and worker
333         trashq = NewWorkQueue()
334         go RunTrashWorker(trashq)
335
336         // Start emptyTrash goroutine
337         doneEmptyingTrash := make(chan bool)
338         go emptyTrash(doneEmptyingTrash, trashCheckInterval)
339
340         // Shut down the server gracefully (by closing the listener)
341         // if SIGTERM is received.
342         term := make(chan os.Signal, 1)
343         go func(sig <-chan os.Signal) {
344                 s := <-sig
345                 log.Println("caught signal:", s)
346                 doneEmptyingTrash <- true
347                 listener.Close()
348         }(term)
349         signal.Notify(term, syscall.SIGTERM)
350         signal.Notify(term, syscall.SIGINT)
351
352         log.Println("listening at", listen)
353         srv := &http.Server{Addr: listen}
354         srv.Serve(listener)
355 }
356
357 // At every trashCheckInterval tick, invoke EmptyTrash on all volumes.
358 func emptyTrash(doneEmptyingTrash chan bool, trashCheckInterval time.Duration) {
359         ticker := time.NewTicker(trashCheckInterval)
360
361         for {
362                 select {
363                 case <-ticker.C:
364                         for _, v := range volumes {
365                                 if v.Writable() {
366                                         v.EmptyTrash()
367                                 }
368                         }
369                 case <-doneEmptyingTrash:
370                         ticker.Stop()
371                         return
372                 }
373         }
374 }