X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/da13bb400f87fdd4157146e2d0b171b730fa3208..8a27fe370239ecb8e50d53f46b45ed61203a35ca:/services/keepstore/pull_worker.go diff --git a/services/keepstore/pull_worker.go b/services/keepstore/pull_worker.go index e42b6e4b89..abe3dc3857 100644 --- a/services/keepstore/pull_worker.go +++ b/services/keepstore/pull_worker.go @@ -1,60 +1,67 @@ -package main +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + +package keepstore import ( "context" - "crypto/rand" "fmt" - "git.curoverse.com/arvados.git/sdk/go/keepclient" "io" "io/ioutil" - "log" "time" + + "git.arvados.org/arvados.git/sdk/go/keepclient" ) -// RunPullWorker is used by Keepstore to initiate pull worker channel goroutine. -// The channel will process pull list. -// For each (next) pull request: -// For each locator listed, execute Pull on the server(s) listed -// Skip the rest of the servers if no errors -// Repeat -// -func RunPullWorker(pullq *WorkQueue, keepClient *keepclient.KeepClient) { - nextItem := pullq.NextItem - for item := range nextItem { - pullRequest := item.(PullRequest) - err := PullItemAndProcess(item.(PullRequest), GenerateRandomAPIToken(), keepClient) +// RunPullWorker receives PullRequests from pullq, invokes +// PullItemAndProcess on each one. After each PR, it logs a message +// indicating whether the pull was successful. +func (h *handler) runPullWorker(pullq *WorkQueue) { + for item := range pullq.NextItem { + pr := item.(PullRequest) + err := h.pullItemAndProcess(pr) pullq.DoneItem <- struct{}{} if err == nil { - log.Printf("Pull %s success", pullRequest) + h.Logger.Printf("Pull %s success", pr) } else { - log.Printf("Pull %s error: %s", pullRequest, err) + h.Logger.Printf("Pull %s error: %s", pr, err) } } } -// PullItemAndProcess pulls items from PullQueue and processes them. -// For each Pull request: -// Generate a random API token. -// Generate a permission signature using this token, timestamp ~60 seconds in the future, and desired block hash. -// Using this token & signature, retrieve the given block. -// Write to storage +// PullItemAndProcess executes a pull request by retrieving the +// specified block from one of the specified servers, and storing it +// on a local volume. // -func PullItemAndProcess(pullRequest PullRequest, token string, keepClient *keepclient.KeepClient) (err error) { - keepClient.Arvados.ApiToken = token +// If the PR specifies a non-blank mount UUID, PullItemAndProcess will +// only attempt to write the data to the corresponding +// volume. Otherwise it writes to any local volume, as a PUT request +// would. +func (h *handler) pullItemAndProcess(pullRequest PullRequest) error { + var vol *VolumeMount + if uuid := pullRequest.MountUUID; uuid != "" { + vol = h.volmgr.Lookup(pullRequest.MountUUID, true) + if vol == nil { + return fmt.Errorf("pull req has nonexistent mount: %v", pullRequest) + } + } + // Make a private copy of keepClient so we can set + // ServiceRoots to the source servers specified in the pull + // request. + keepClient := *h.keepClient serviceRoots := make(map[string]string) for _, addr := range pullRequest.Servers { serviceRoots[addr] = addr } keepClient.SetServiceRoots(serviceRoots, nil, nil) - // Generate signature with a random token - expiresAt := time.Now().Add(60 * time.Second) - signedLocator := SignLocator(pullRequest.Locator, token, expiresAt) + signedLocator := SignLocator(h.Cluster, pullRequest.Locator, keepClient.Arvados.ApiToken, time.Now().Add(time.Minute)) - reader, contentLen, _, err := GetContent(signedLocator, keepClient) + reader, contentLen, _, err := GetContent(signedLocator, &keepClient) if err != nil { - return + return err } if reader == nil { return fmt.Errorf("No reader found for : %s", signedLocator) @@ -70,31 +77,18 @@ func PullItemAndProcess(pullRequest PullRequest, token string, keepClient *keepc return fmt.Errorf("Content not found for: %s", signedLocator) } - err = PutContent(readContent, pullRequest.Locator) - return + return writePulledBlock(h.volmgr, vol, readContent, pullRequest.Locator) } -// Fetch the content for the given locator using keepclient. -var GetContent = func(signedLocator string, keepClient *keepclient.KeepClient) ( - reader io.ReadCloser, contentLength int64, url string, err error) { - reader, blocklen, url, err := keepClient.Get(signedLocator) - return reader, blocklen, url, err +// GetContent fetches the content for the given locator using keepclient. +var GetContent = func(signedLocator string, keepClient *keepclient.KeepClient) (io.ReadCloser, int64, string, error) { + return keepClient.Get(signedLocator) } -const alphaNumeric = "0123456789abcdefghijklmnopqrstuvwxyz" - -// GenerateRandomAPIToken generates a random api token -func GenerateRandomAPIToken() string { - var bytes = make([]byte, 36) - rand.Read(bytes) - for i, b := range bytes { - bytes[i] = alphaNumeric[b%byte(len(alphaNumeric))] +var writePulledBlock = func(volmgr *RRVolumeManager, volume Volume, data []byte, locator string) error { + if volume != nil { + return volume.Put(context.Background(), locator, data) } - return (string(bytes)) -} - -// Put block -var PutContent = func(content []byte, locator string) (err error) { - _, err = PutBlock(context.TODO(), content, locator) - return + _, err := PutBlock(context.Background(), volmgr, data, locator, nil) + return err }