7 "git.curoverse.com/arvados.git/sdk/go/keepclient"
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
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{}{}
28 log.Printf("Pull %s success", pullRequest)
30 log.Printf("Pull %s error: %s", pullRequest, err)
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.
42 func PullItemAndProcess(pullRequest PullRequest, token string, keepClient *keepclient.KeepClient) (err error) {
43 keepClient.Arvados.ApiToken = token
45 serviceRoots := make(map[string]string)
46 for _, addr := range pullRequest.Servers {
47 serviceRoots[addr] = addr
49 keepClient.SetServiceRoots(serviceRoots, nil, nil)
51 // Generate signature with a random token
52 expiresAt := time.Now().Add(60 * time.Second)
53 signedLocator := SignLocator(pullRequest.Locator, token, expiresAt)
55 reader, contentLen, _, err := GetContent(signedLocator, keepClient)
60 return errors.New(fmt.Sprintf("No reader found for : %s", signedLocator))
64 readContent, err := ioutil.ReadAll(reader)
69 if (readContent == nil) || (int64(len(readContent)) != contentLen) {
70 return errors.New(fmt.Sprintf("Content not found for: %s", signedLocator))
73 err = PutContent(readContent, pullRequest.Locator)
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
84 const alphaNumeric = "0123456789abcdefghijklmnopqrstuvwxyz"
86 // GenerateRandomAPIToken generates a random api token
87 func GenerateRandomAPIToken() string {
88 var bytes = make([]byte, 36)
90 for i, b := range bytes {
91 bytes[i] = alphaNumeric[b%byte(len(alphaNumeric))]
93 return (string(bytes))
97 var PutContent = func(content []byte, locator string) (err error) {
98 _, err = PutBlock(content, locator)