6 "git.curoverse.com/arvados.git/sdk/go/keepclient"
13 // RunPullWorker is used by Keepstore to initiate pull worker channel goroutine.
14 // The channel will process pull list.
15 // For each (next) pull request:
16 // For each locator listed, execute Pull on the server(s) listed
17 // Skip the rest of the servers if no errors
20 func RunPullWorker(pullq *WorkQueue, keepClient *keepclient.KeepClient) {
21 nextItem := pullq.NextItem
22 for item := range nextItem {
23 pullRequest := item.(PullRequest)
24 err := PullItemAndProcess(item.(PullRequest), GenerateRandomAPIToken(), keepClient)
25 pullq.DoneItem <- struct{}{}
27 log.Printf("Pull %s success", pullRequest)
29 log.Printf("Pull %s error: %s", pullRequest, err)
34 // PullItemAndProcess pulls items from PullQueue and processes them.
35 // For each Pull request:
36 // Generate a random API token.
37 // Generate a permission signature using this token, timestamp ~60 seconds in the future, and desired block hash.
38 // Using this token & signature, retrieve the given block.
41 func PullItemAndProcess(pullRequest PullRequest, token string, keepClient *keepclient.KeepClient) (err error) {
42 keepClient.Arvados.ApiToken = token
44 serviceRoots := make(map[string]string)
45 for _, addr := range pullRequest.Servers {
46 serviceRoots[addr] = addr
48 keepClient.SetServiceRoots(serviceRoots, nil, nil)
50 // Generate signature with a random token
51 expiresAt := time.Now().Add(60 * time.Second)
52 signedLocator := SignLocator(pullRequest.Locator, token, expiresAt)
54 reader, contentLen, _, err := GetContent(signedLocator, keepClient)
59 return fmt.Errorf("No reader found for : %s", signedLocator)
63 readContent, err := ioutil.ReadAll(reader)
68 if (readContent == nil) || (int64(len(readContent)) != contentLen) {
69 return fmt.Errorf("Content not found for: %s", signedLocator)
72 err = PutContent(readContent, pullRequest.Locator)
76 // Fetch the content for the given locator using keepclient.
77 var GetContent = func(signedLocator string, keepClient *keepclient.KeepClient) (
78 reader io.ReadCloser, contentLength int64, url string, err error) {
79 reader, blocklen, url, err := keepClient.Get(signedLocator)
80 return reader, blocklen, url, err
83 const alphaNumeric = "0123456789abcdefghijklmnopqrstuvwxyz"
85 // GenerateRandomAPIToken generates a random api token
86 func GenerateRandomAPIToken() string {
87 var bytes = make([]byte, 36)
89 for i, b := range bytes {
90 bytes[i] = alphaNumeric[b%byte(len(alphaNumeric))]
92 return (string(bytes))
96 var PutContent = func(content []byte, locator string) (err error) {
97 _, err = PutBlock(content, locator)