X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/aee63d7cbb2f8e39b417baebc145889d6290315e..8c682e4ffcd8fdf9bcaf8f062f1d0a6517ea9e5d:/services/keepstore/handlers.go diff --git a/services/keepstore/handlers.go b/services/keepstore/handlers.go index 8095207697..bd1ca67bfc 100644 --- a/services/keepstore/handlers.go +++ b/services/keepstore/handlers.go @@ -59,10 +59,18 @@ func MakeRESTRouter() *mux.Router { `/index/{prefix:[0-9a-f]{0,32}}`, IndexHandler).Methods("GET", "HEAD") rest.HandleFunc(`/status.json`, StatusHandler).Methods("GET", "HEAD") - // The PullHandler processes "PUT /pull" commands from Data Manager. - // It parses the JSON list of pull requests in the request body, and - // delivers them to the pull list manager for replication. + // The PullHandler and TrashHandler process "PUT /pull" and "PUT + // /trash" requests from Data Manager. These requests instruct + // Keep to replicate or delete blocks; see + // https://arvados.org/projects/arvados/wiki/Keep_Design_Doc + // for more details. + // + // Each handler parses the JSON list of block management requests + // in the message body, and replaces any existing pull queue or + // trash queue with their contentes. + // rest.HandleFunc(`/pull`, PullHandler).Methods("PUT") + rest.HandleFunc(`/trash`, TrashHandler).Methods("PUT") // Any request which does not match any of these routes gets // 400 Bad Request. @@ -110,8 +118,6 @@ func FindKeepVolumes() []string { func GetBlockHandler(resp http.ResponseWriter, req *http.Request) { hash := mux.Vars(req)["hash"] - log.Printf("%s %s", req.Method, hash) - hints := mux.Vars(req)["hints"] // Parse the locator string and hints from the request. @@ -165,9 +171,6 @@ func GetBlockHandler(resp http.ResponseWriter, req *http.Request) { if err != nil { // This type assertion is safe because the only errors // GetBlock can return are DiskHashError or NotFoundError. - if err == NotFoundError { - log.Printf("%s: not found, giving up\n", hash) - } http.Error(resp, err.Error(), err.(*KeepError).HTTPCode) return } @@ -175,9 +178,6 @@ func GetBlockHandler(resp http.ResponseWriter, req *http.Request) { resp.Header().Set("X-Block-Size", fmt.Sprintf("%d", len(block))) _, err = resp.Write(block) - if err != nil { - log.Printf("GetBlockHandler: writing response: %s", err) - } return } @@ -189,8 +189,6 @@ func PutBlockHandler(resp http.ResponseWriter, req *http.Request) { hash := mux.Vars(req)["hash"] - log.Printf("%s %s", req.Method, hash) - // Read the block data to be stored. // If the request exceeds BLOCKSIZE bytes, issue a HTTP 500 error. // @@ -228,18 +226,14 @@ func PutBlockHandler(resp http.ResponseWriter, req *http.Request) { // A HandleFunc to address /index and /index/{prefix} requests. // func IndexHandler(resp http.ResponseWriter, req *http.Request) { - prefix := mux.Vars(req)["prefix"] - - // Only the data manager may issue /index requests, - // and only if enforce_permissions is enabled. - // All other requests return 403 Forbidden. - api_token := GetApiToken(req) - if !enforce_permissions || - api_token == "" || - data_manager_token != api_token { - http.Error(resp, PermissionError.Error(), PermissionError.HTTPCode) + // Reject unauthorized requests. + if !IsDataManagerToken(GetApiToken(req)) { + http.Error(resp, UnauthorizedError.Error(), UnauthorizedError.HTTPCode) return } + + prefix := mux.Vars(req)["prefix"] + var index string for _, vol := range KeepVM.Volumes() { index = index + vol.Index(prefix) @@ -351,7 +345,6 @@ func GetVolumeStatus(volume string) *VolumeStatus { // func DeleteHandler(resp http.ResponseWriter, req *http.Request) { hash := mux.Vars(req)["hash"] - log.Printf("%s %s", req.Method, hash) // Confirm that this user is an admin and has a token with unlimited scope. var tok = GetApiToken(req) @@ -443,10 +436,8 @@ type PullRequest struct { func PullHandler(resp http.ResponseWriter, req *http.Request) { // Reject unauthorized requests. - api_token := GetApiToken(req) - if !IsDataManagerToken(api_token) { + if !IsDataManagerToken(GetApiToken(req)) { http.Error(resp, UnauthorizedError.Error(), UnauthorizedError.HTTPCode) - log.Printf("%s %s: %s\n", req.Method, req.URL, UnauthorizedError.Error()) return } @@ -455,14 +446,12 @@ func PullHandler(resp http.ResponseWriter, req *http.Request) { r := json.NewDecoder(req.Body) if err := r.Decode(&pr); err != nil { http.Error(resp, BadRequestError.Error(), BadRequestError.HTTPCode) - log.Printf("%s %s: %s\n", req.Method, req.URL, err.Error()) return } // We have a properly formatted pull list sent from the data // manager. Report success and send the list to the pull list // manager for further handling. - log.Printf("%s %s: received %v\n", req.Method, req.URL, pr) resp.WriteHeader(http.StatusOK) resp.Write([]byte( fmt.Sprintf("Received %d pull requests\n", len(pr)))) @@ -472,10 +461,48 @@ func PullHandler(resp http.ResponseWriter, req *http.Request) { plist.PushBack(p) } - if pullmgr == nil { - pullmgr = NewBlockWorkList() + if pullq == nil { + pullq = NewWorkQueue() + } + pullq.ReplaceQueue(plist) +} + +type TrashRequest struct { + Locator string `json:"locator"` + BlockMtime int64 `json:"block_mtime"` +} + +func TrashHandler(resp http.ResponseWriter, req *http.Request) { + // Reject unauthorized requests. + if !IsDataManagerToken(GetApiToken(req)) { + http.Error(resp, UnauthorizedError.Error(), UnauthorizedError.HTTPCode) + return + } + + // Parse the request body. + var trash []TrashRequest + r := json.NewDecoder(req.Body) + if err := r.Decode(&trash); err != nil { + http.Error(resp, BadRequestError.Error(), BadRequestError.HTTPCode) + return + } + + // We have a properly formatted trash list sent from the data + // manager. Report success and send the list to the trash work + // queue for further handling. + resp.WriteHeader(http.StatusOK) + resp.Write([]byte( + fmt.Sprintf("Received %d trash requests\n", len(trash)))) + + tlist := list.New() + for _, t := range trash { + tlist.PushBack(t) + } + + if trashq == nil { + trashq = NewWorkQueue() } - pullmgr.ReplaceList(plist) + trashq.ReplaceQueue(tlist) } // ==============================