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"
18 // RunPullWorker receives PullRequests from pullq, invokes
19 // PullItemAndProcess on each one. After each PR, it logs a message
20 // indicating whether the pull was successful.
21 func RunPullWorker(pullq *WorkQueue, keepClient *keepclient.KeepClient) {
22 for item := range pullq.NextItem {
23 pr := item.(PullRequest)
24 err := PullItemAndProcess(pr, keepClient)
25 pullq.DoneItem <- struct{}{}
27 log.Printf("Pull %s success", pr)
29 log.Printf("Pull %s error: %s", pr, err)
34 // PullItemAndProcess executes a pull request by retrieving the
35 // specified block from one of the specified servers, and storing it
38 // If the PR specifies a non-blank mount UUID, PullItemAndProcess will
39 // only attempt to write the data to the corresponding
40 // volume. Otherwise it writes to any local volume, as a PUT request
42 func PullItemAndProcess(pullRequest PullRequest, keepClient *keepclient.KeepClient) error {
44 if uuid := pullRequest.MountUUID; uuid != "" {
45 vol = KeepVM.Lookup(pullRequest.MountUUID, true)
47 return fmt.Errorf("pull req has nonexistent mount: %v", pullRequest)
51 keepClient.Arvados.ApiToken = randomToken
53 serviceRoots := make(map[string]string)
54 for _, addr := range pullRequest.Servers {
55 serviceRoots[addr] = addr
57 keepClient.SetServiceRoots(serviceRoots, nil, nil)
59 // Generate signature with a random token
60 expiresAt := time.Now().Add(60 * time.Second)
61 signedLocator := SignLocator(pullRequest.Locator, randomToken, expiresAt)
63 reader, contentLen, _, err := GetContent(signedLocator, keepClient)
68 return fmt.Errorf("No reader found for : %s", signedLocator)
72 readContent, err := ioutil.ReadAll(reader)
77 if (readContent == nil) || (int64(len(readContent)) != contentLen) {
78 return fmt.Errorf("Content not found for: %s", signedLocator)
81 writePulledBlock(vol, readContent, pullRequest.Locator)
85 // Fetch the content for the given locator using keepclient.
86 var GetContent = func(signedLocator string, keepClient *keepclient.KeepClient) (io.ReadCloser, int64, string, error) {
87 return keepClient.Get(signedLocator)
90 var writePulledBlock = func(volume Volume, data []byte, locator string) {
93 err = volume.Put(context.Background(), locator, data)
95 _, err = PutBlock(context.Background(), data, locator)
98 log.Printf("error writing pulled block %q: %s", locator, err)
102 var randomToken = func() string {
103 const alphaNumeric = "0123456789abcdefghijklmnopqrstuvwxyz"
104 var bytes = make([]byte, 36)
106 for i, b := range bytes {
107 bytes[i] = alphaNumeric[b%byte(len(alphaNumeric))]
109 return (string(bytes))