-package main
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+package keepstore
import (
"context"
- "crypto/rand"
"fmt"
"io"
"io/ioutil"
"time"
- "git.curoverse.com/arvados.git/sdk/go/keepclient"
-
- log "github.com/Sirupsen/logrus"
+ "git.arvados.org/arvados.git/sdk/go/keepclient"
)
-// RunPullWorker is used by Keepstore to initiate pull worker channel goroutine.
-// The channel will process pull list.
-// For each (next) pull request:
-// For each locator listed, execute Pull on the server(s) listed
-// Skip the rest of the servers if no errors
-// Repeat
-//
-func RunPullWorker(pullq *WorkQueue, keepClient *keepclient.KeepClient) {
- nextItem := pullq.NextItem
- for item := range nextItem {
- pullRequest := item.(PullRequest)
- err := PullItemAndProcess(item.(PullRequest), keepClient)
+// RunPullWorker receives PullRequests from pullq, invokes
+// PullItemAndProcess on each one. After each PR, it logs a message
+// indicating whether the pull was successful.
+func (h *handler) runPullWorker(pullq *WorkQueue) {
+ for item := range pullq.NextItem {
+ pr := item.(PullRequest)
+ err := h.pullItemAndProcess(pr)
pullq.DoneItem <- struct{}{}
if err == nil {
- log.Printf("Pull %s success", pullRequest)
+ h.Logger.Printf("Pull %s success", pr)
} else {
- log.Printf("Pull %s error: %s", pullRequest, err)
+ h.Logger.Printf("Pull %s error: %s", pr, err)
}
}
}
-// PullItemAndProcess pulls items from PullQueue and processes them.
-// For each Pull request:
-// Generate a random API token.
-// Generate a permission signature using this token, timestamp ~60 seconds in the future, and desired block hash.
-// Using this token & signature, retrieve the given block.
-// Write to storage
+// PullItemAndProcess executes a pull request by retrieving the
+// specified block from one of the specified servers, and storing it
+// on a local volume.
//
-func PullItemAndProcess(pullRequest PullRequest, keepClient *keepclient.KeepClient) error {
- var vol Volume
+// If the PR specifies a non-blank mount UUID, PullItemAndProcess will
+// only attempt to write the data to the corresponding
+// volume. Otherwise it writes to any local volume, as a PUT request
+// would.
+func (h *handler) pullItemAndProcess(pullRequest PullRequest) error {
+ var vol *VolumeMount
if uuid := pullRequest.MountUUID; uuid != "" {
- vol = KeepVM.Lookup(pullRequest.MountUUID, true)
+ vol = h.volmgr.Lookup(pullRequest.MountUUID, true)
if vol == nil {
return fmt.Errorf("pull req has nonexistent mount: %v", pullRequest)
}
}
- keepClient.Arvados.ApiToken = randomToken
-
+ // Make a private copy of keepClient so we can set
+ // ServiceRoots to the source servers specified in the pull
+ // request.
+ keepClient := h.keepClient.Clone()
serviceRoots := make(map[string]string)
for _, addr := range pullRequest.Servers {
serviceRoots[addr] = addr
}
keepClient.SetServiceRoots(serviceRoots, nil, nil)
- // Generate signature with a random token
- expiresAt := time.Now().Add(60 * time.Second)
- signedLocator := SignLocator(pullRequest.Locator, randomToken, expiresAt)
+ signedLocator := SignLocator(h.Cluster, pullRequest.Locator, keepClient.Arvados.ApiToken, time.Now().Add(time.Minute))
reader, contentLen, _, err := GetContent(signedLocator, keepClient)
if err != nil {
return fmt.Errorf("Content not found for: %s", signedLocator)
}
- writePulledBlock(vol, readContent, pullRequest.Locator)
- return nil
+ return writePulledBlock(h.volmgr, vol, readContent, pullRequest.Locator)
}
-// Fetch the content for the given locator using keepclient.
+// GetContent fetches the content for the given locator using keepclient.
var GetContent = func(signedLocator string, keepClient *keepclient.KeepClient) (io.ReadCloser, int64, string, error) {
return keepClient.Get(signedLocator)
}
-var writePulledBlock = func(volume Volume, data []byte, locator string) {
- var err error
+var writePulledBlock = func(volmgr *RRVolumeManager, volume Volume, data []byte, locator string) error {
if volume != nil {
- err = volume.Put(context.Background(), locator, data)
- } else {
- _, err = PutBlock(context.Background(), data, locator)
- }
- if err != nil {
- log.Printf("error writing pulled block %q: %s", locator, err)
+ return volume.Put(context.Background(), locator, data)
}
+ _, err := PutBlock(context.Background(), volmgr, data, locator, nil)
+ return err
}
-
-var randomToken = func() string {
- const alphaNumeric = "0123456789abcdefghijklmnopqrstuvwxyz"
- var bytes = make([]byte, 36)
- rand.Read(bytes)
- for i, b := range bytes {
- bytes[i] = alphaNumeric[b%byte(len(alphaNumeric))]
- }
- return (string(bytes))
-}()