Merge branch '8784-dir-listings'
[arvados.git] / services / keepstore / pull_worker.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "context"
9         "crypto/rand"
10         "fmt"
11         "io"
12         "io/ioutil"
13         "time"
14
15         "git.curoverse.com/arvados.git/sdk/go/keepclient"
16
17         log "github.com/Sirupsen/logrus"
18 )
19
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{}{}
28                 if err == nil {
29                         log.Printf("Pull %s success", pr)
30                 } else {
31                         log.Printf("Pull %s error: %s", pr, err)
32                 }
33         }
34 }
35
36 // PullItemAndProcess executes a pull request by retrieving the
37 // specified block from one of the specified servers, and storing it
38 // on a local volume.
39 //
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
43 // would.
44 func PullItemAndProcess(pullRequest PullRequest, keepClient *keepclient.KeepClient) error {
45         var vol Volume
46         if uuid := pullRequest.MountUUID; uuid != "" {
47                 vol = KeepVM.Lookup(pullRequest.MountUUID, true)
48                 if vol == nil {
49                         return fmt.Errorf("pull req has nonexistent mount: %v", pullRequest)
50                 }
51         }
52
53         keepClient.Arvados.ApiToken = randomToken
54
55         serviceRoots := make(map[string]string)
56         for _, addr := range pullRequest.Servers {
57                 serviceRoots[addr] = addr
58         }
59         keepClient.SetServiceRoots(serviceRoots, nil, nil)
60
61         // Generate signature with a random token
62         expiresAt := time.Now().Add(60 * time.Second)
63         signedLocator := SignLocator(pullRequest.Locator, randomToken, expiresAt)
64
65         reader, contentLen, _, err := GetContent(signedLocator, keepClient)
66         if err != nil {
67                 return err
68         }
69         if reader == nil {
70                 return fmt.Errorf("No reader found for : %s", signedLocator)
71         }
72         defer reader.Close()
73
74         readContent, err := ioutil.ReadAll(reader)
75         if err != nil {
76                 return err
77         }
78
79         if (readContent == nil) || (int64(len(readContent)) != contentLen) {
80                 return fmt.Errorf("Content not found for: %s", signedLocator)
81         }
82
83         writePulledBlock(vol, readContent, pullRequest.Locator)
84         return nil
85 }
86
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)
90 }
91
92 var writePulledBlock = func(volume Volume, data []byte, locator string) {
93         var err error
94         if volume != nil {
95                 err = volume.Put(context.Background(), locator, data)
96         } else {
97                 _, err = PutBlock(context.Background(), data, locator)
98         }
99         if err != nil {
100                 log.Printf("error writing pulled block %q: %s", locator, err)
101         }
102 }
103
104 var randomToken = func() string {
105         const alphaNumeric = "0123456789abcdefghijklmnopqrstuvwxyz"
106         var bytes = make([]byte, 36)
107         rand.Read(bytes)
108         for i, b := range bytes {
109                 bytes[i] = alphaNumeric[b%byte(len(alphaNumeric))]
110         }
111         return (string(bytes))
112 }()