1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.arvados.org/arvados.git/sdk/go/keepclient"
17 // RunPullWorker receives PullRequests from pullq, invokes
18 // PullItemAndProcess on each one. After each PR, it logs a message
19 // indicating whether the pull was successful.
20 func (h *handler) runPullWorker(pullq *WorkQueue) {
21 for item := range pullq.NextItem {
22 pr := item.(PullRequest)
23 err := h.pullItemAndProcess(pr)
24 pullq.DoneItem <- struct{}{}
26 h.Logger.Printf("Pull %s success", pr)
28 h.Logger.Printf("Pull %s error: %s", pr, err)
33 // PullItemAndProcess executes a pull request by retrieving the
34 // specified block from one of the specified servers, and storing it
37 // If the PR specifies a non-blank mount UUID, PullItemAndProcess will
38 // only attempt to write the data to the corresponding
39 // volume. Otherwise it writes to any local volume, as a PUT request
41 func (h *handler) pullItemAndProcess(pullRequest PullRequest) error {
43 if uuid := pullRequest.MountUUID; uuid != "" {
44 vol = h.volmgr.Lookup(pullRequest.MountUUID, true)
46 return fmt.Errorf("pull req has nonexistent mount: %v", pullRequest)
50 // Make a private copy of keepClient so we can set
51 // ServiceRoots to the source servers specified in the pull
53 keepClient := *h.keepClient
54 serviceRoots := make(map[string]string)
55 for _, addr := range pullRequest.Servers {
56 serviceRoots[addr] = addr
58 keepClient.SetServiceRoots(serviceRoots, nil, nil)
60 signedLocator := SignLocator(h.Cluster, pullRequest.Locator, keepClient.Arvados.ApiToken, time.Now().Add(time.Minute))
62 reader, contentLen, _, err := GetContent(signedLocator, &keepClient)
67 return fmt.Errorf("No reader found for : %s", signedLocator)
71 readContent, err := ioutil.ReadAll(reader)
76 if (readContent == nil) || (int64(len(readContent)) != contentLen) {
77 return fmt.Errorf("Content not found for: %s", signedLocator)
80 return writePulledBlock(h.volmgr, vol, readContent, pullRequest.Locator)
83 // GetContent fetches the content for the given locator using keepclient.
84 var GetContent = func(signedLocator string, keepClient *keepclient.KeepClient) (io.ReadCloser, int64, string, error) {
85 return keepClient.Get(signedLocator)
88 var writePulledBlock = func(volmgr *RRVolumeManager, volume Volume, data []byte, locator string) error {
90 return volume.Put(context.Background(), locator, data)
92 _, err := PutBlock(context.Background(), volmgr, data, locator, nil)