1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.curoverse.com/arvados.git/sdk/go/keepclient"
17 log "github.com/Sirupsen/logrus"
20 // RunPullWorker receives PullRequests from pullq, invokes
21 // PullItemAndProcess on each one. After each PR, it logs a message
22 // indicating whether the pull was successful.
23 func RunPullWorker(pullq *WorkQueue, keepClient *keepclient.KeepClient) {
24 for item := range pullq.NextItem {
25 pr := item.(PullRequest)
26 err := PullItemAndProcess(pr, keepClient)
27 pullq.DoneItem <- struct{}{}
29 log.Printf("Pull %s success", pr)
31 log.Printf("Pull %s error: %s", pr, err)
36 // PullItemAndProcess executes a pull request by retrieving the
37 // specified block from one of the specified servers, and storing it
40 // If the PR specifies a non-blank mount UUID, PullItemAndProcess will
41 // only attempt to write the data to the corresponding
42 // volume. Otherwise it writes to any local volume, as a PUT request
44 func PullItemAndProcess(pullRequest PullRequest, keepClient *keepclient.KeepClient) error {
46 if uuid := pullRequest.MountUUID; uuid != "" {
47 vol = KeepVM.Lookup(pullRequest.MountUUID, true)
49 return fmt.Errorf("pull req has nonexistent mount: %v", pullRequest)
53 keepClient.Arvados.ApiToken = randomToken
55 serviceRoots := make(map[string]string)
56 for _, addr := range pullRequest.Servers {
57 serviceRoots[addr] = addr
59 keepClient.SetServiceRoots(serviceRoots, nil, nil)
61 // Generate signature with a random token
62 expiresAt := time.Now().Add(60 * time.Second)
63 signedLocator := SignLocator(pullRequest.Locator, randomToken, expiresAt)
65 reader, contentLen, _, err := GetContent(signedLocator, keepClient)
70 return fmt.Errorf("No reader found for : %s", signedLocator)
74 readContent, err := ioutil.ReadAll(reader)
79 if (readContent == nil) || (int64(len(readContent)) != contentLen) {
80 return fmt.Errorf("Content not found for: %s", signedLocator)
83 writePulledBlock(vol, readContent, pullRequest.Locator)
87 // Fetch the content for the given locator using keepclient.
88 var GetContent = func(signedLocator string, keepClient *keepclient.KeepClient) (io.ReadCloser, int64, string, error) {
89 return keepClient.Get(signedLocator)
92 var writePulledBlock = func(volume Volume, data []byte, locator string) {
95 err = volume.Put(context.Background(), locator, data)
97 _, err = PutBlock(context.Background(), data, locator)
100 log.Printf("error writing pulled block %q: %s", locator, err)
104 var randomToken = func() string {
105 const alphaNumeric = "0123456789abcdefghijklmnopqrstuvwxyz"
106 var bytes = make([]byte, 36)
108 for i, b := range bytes {
109 bytes[i] = alphaNumeric[b%byte(len(alphaNumeric))]
111 return (string(bytes))