10467: Merge branch 'master' into 10467-client-disconnect
[arvados.git] / services / keepstore / pull_worker.go
1 package main
2
3 import (
4         "context"
5         "crypto/rand"
6         "fmt"
7         "git.curoverse.com/arvados.git/sdk/go/keepclient"
8         "io"
9         "io/ioutil"
10         "log"
11         "time"
12 )
13
14 // RunPullWorker is used by Keepstore to initiate pull worker channel goroutine.
15 //      The channel will process pull list.
16 //              For each (next) pull request:
17 //                      For each locator listed, execute Pull on the server(s) listed
18 //                      Skip the rest of the servers if no errors
19 //              Repeat
20 //
21 func RunPullWorker(pullq *WorkQueue, keepClient *keepclient.KeepClient) {
22         nextItem := pullq.NextItem
23         for item := range nextItem {
24                 pullRequest := item.(PullRequest)
25                 err := PullItemAndProcess(item.(PullRequest), GenerateRandomAPIToken(), keepClient)
26                 pullq.DoneItem <- struct{}{}
27                 if err == nil {
28                         log.Printf("Pull %s success", pullRequest)
29                 } else {
30                         log.Printf("Pull %s error: %s", pullRequest, err)
31                 }
32         }
33 }
34
35 // PullItemAndProcess pulls items from PullQueue and processes them.
36 //      For each Pull request:
37 //              Generate a random API token.
38 //              Generate a permission signature using this token, timestamp ~60 seconds in the future, and desired block hash.
39 //              Using this token & signature, retrieve the given block.
40 //              Write to storage
41 //
42 func PullItemAndProcess(pullRequest PullRequest, token string, keepClient *keepclient.KeepClient) (err error) {
43         keepClient.Arvados.ApiToken = token
44
45         serviceRoots := make(map[string]string)
46         for _, addr := range pullRequest.Servers {
47                 serviceRoots[addr] = addr
48         }
49         keepClient.SetServiceRoots(serviceRoots, nil, nil)
50
51         // Generate signature with a random token
52         expiresAt := time.Now().Add(60 * time.Second)
53         signedLocator := SignLocator(pullRequest.Locator, token, expiresAt)
54
55         reader, contentLen, _, err := GetContent(signedLocator, keepClient)
56         if err != nil {
57                 return
58         }
59         if reader == nil {
60                 return fmt.Errorf("No reader found for : %s", signedLocator)
61         }
62         defer reader.Close()
63
64         readContent, err := ioutil.ReadAll(reader)
65         if err != nil {
66                 return err
67         }
68
69         if (readContent == nil) || (int64(len(readContent)) != contentLen) {
70                 return fmt.Errorf("Content not found for: %s", signedLocator)
71         }
72
73         err = PutContent(readContent, pullRequest.Locator)
74         return
75 }
76
77 // Fetch the content for the given locator using keepclient.
78 var GetContent = func(signedLocator string, keepClient *keepclient.KeepClient) (
79         reader io.ReadCloser, contentLength int64, url string, err error) {
80         reader, blocklen, url, err := keepClient.Get(signedLocator)
81         return reader, blocklen, url, err
82 }
83
84 const alphaNumeric = "0123456789abcdefghijklmnopqrstuvwxyz"
85
86 // GenerateRandomAPIToken generates a random api token
87 func GenerateRandomAPIToken() string {
88         var bytes = make([]byte, 36)
89         rand.Read(bytes)
90         for i, b := range bytes {
91                 bytes[i] = alphaNumeric[b%byte(len(alphaNumeric))]
92         }
93         return (string(bytes))
94 }
95
96 // Put block
97 var PutContent = func(content []byte, locator string) (err error) {
98         _, err = PutBlock(context.Background(), content, locator)
99         return
100 }