)
func main() {
- var srcConfigFile, dstConfigFile, srcKeepServicesJSON, dstKeepServicesJSON, prefix string
- var replications int
- var srcBlobSigningKey string
+ err := doMain()
+ if err != nil {
+ log.Fatalf("%v", err)
+ }
+}
- flag.StringVar(
- &srcConfigFile,
- "src-config-file",
+func doMain() error {
+ flags := flag.NewFlagSet("keep-rsync", flag.ExitOnError)
+
+ srcConfigFile := flags.String(
+ "src",
"",
- "Source configuration filename with full path that contains "+
- "an ARVADOS_API_TOKEN which is a valid datamanager token recognized by the source keep servers, "+
- "ARVADOS_API_HOST, ARVADOS_API_HOST_INSECURE, ARVADOS_EXTERNAL_CLIENT and ARVADOS_BLOB_SIGNING_KEY.")
+ "Source configuration filename. May be either a pathname to a config file, or (for example) 'foo' as shorthand for $HOME/.config/arvados/foo.conf file. This file is expected to specify the values for ARVADOS_API_TOKEN, ARVADOS_API_HOST, ARVADOS_API_HOST_INSECURE, and ARVADOS_BLOB_SIGNING_KEY for the source.")
- flag.StringVar(
- &dstConfigFile,
- "dst-config-file",
+ dstConfigFile := flags.String(
+ "dst",
"",
- "Destination configuration filename with full path that contains "+
- "an ARVADOS_API_TOKEN which is a valid datamanager token recognized by the destination keep servers, "+
- "ARVADOS_API_HOST, ARVADOS_API_HOST_INSECURE, ARVADOS_EXTERNAL_CLIENT and ARVADOS_BLOB_SIGNING_KEY.")
+ "Destination configuration filename. May be either a pathname to a config file, or (for example) 'foo' as shorthand for $HOME/.config/arvados/foo.conf file. This file is expected to specify the values for ARVADOS_API_TOKEN, ARVADOS_API_HOST, and ARVADOS_API_HOST_INSECURE for the destination.")
- flag.StringVar(
- &srcKeepServicesJSON,
+ srcKeepServicesJSON := flags.String(
"src-keep-services-json",
"",
"An optional list of available source keepservices. "+
"If not provided, this list is obtained from api server configured in src-config-file.")
- flag.StringVar(
- &dstKeepServicesJSON,
+ dstKeepServicesJSON := flags.String(
"dst-keep-services-json",
"",
"An optional list of available destination keepservices. "+
"If not provided, this list is obtained from api server configured in dst-config-file.")
- flag.IntVar(
- &replications,
+ replications := flags.Int(
"replications",
0,
"Number of replications to write to the destination. If replications not specified, "+
"default replication level configured on destination server will be used.")
- flag.StringVar(
- &prefix,
+ prefix := flags.String(
"prefix",
"",
"Index prefix")
- flag.Parse()
+ // Parse args; omit the first arg which is the command name
+ flags.Parse(os.Args[1:])
- srcConfig, srcBlobSigningKey, err := loadConfig(srcConfigFile)
+ srcConfig, srcBlobSigningKey, err := loadConfig(*srcConfigFile)
if err != nil {
- log.Fatalf("Error loading src configuration from file: %s", err.Error())
+ return fmt.Errorf("Error loading src configuration from file: %s", err.Error())
}
- dstConfig, _, err := loadConfig(dstConfigFile)
+ dstConfig, _, err := loadConfig(*dstConfigFile)
if err != nil {
- log.Fatalf("Error loading dst configuration from file: %s", err.Error())
+ return fmt.Errorf("Error loading dst configuration from file: %s", err.Error())
}
// setup src and dst keepclients
- kcSrc, err := setupKeepClient(srcConfig, srcKeepServicesJSON, false, 0)
+ kcSrc, err := setupKeepClient(srcConfig, *srcKeepServicesJSON, false, 0)
if err != nil {
- log.Fatalf("Error configuring src keepclient: %s", err.Error())
+ return fmt.Errorf("Error configuring src keepclient: %s", err.Error())
}
- kcDst, err := setupKeepClient(dstConfig, dstKeepServicesJSON, true, replications)
+ kcDst, err := setupKeepClient(dstConfig, *dstKeepServicesJSON, true, *replications)
if err != nil {
- log.Fatalf("Error configuring dst keepclient: %s", err.Error())
+ return fmt.Errorf("Error configuring dst keepclient: %s", err.Error())
}
// Copy blocks not found in dst from src
- err = performKeepRsync(kcSrc, kcDst, srcBlobSigningKey, prefix)
+ err = performKeepRsync(kcSrc, kcDst, srcBlobSigningKey, *prefix)
if err != nil {
- log.Fatalf("Error while syncing data: %s", err.Error())
+ return fmt.Errorf("Error while syncing data: %s", err.Error())
}
+
+ return nil
}
type apiConfig struct {
// Read config from file
func readConfigFromFile(filename string) (config apiConfig, blobSigningKey string, err error) {
if !strings.Contains(filename, "/") {
- filename = os.Getenv("HOME") + "/.config/arvados/" + filename
+ filename = os.Getenv("HOME") + "/.config/arvados/" + filename + ".conf"
}
content, err := ioutil.ReadFile(filename)
toBeCopied := getMissingLocators(srcIndex, dstIndex)
// Copy each missing block to dst
+ log.Printf("Before keep-rsync, there are %d blocks in src and %d blocks in dst. Start copying %d blocks from src not found in dst.",
+ len(srcIndex), len(dstIndex), len(toBeCopied))
+
err = copyBlocksToDst(toBeCopied, kcSrc, kcDst, blobSigningKey)
return err
// Copy blocks from src to dst; only those that are missing in dst are copied
func copyBlocksToDst(toBeCopied []string, kcSrc, kcDst *keepclient.KeepClient, blobSigningKey string) error {
- done := 0
total := len(toBeCopied)
- for _, locator := range toBeCopied {
- log.Printf("Getting block %d of %d: %v", done+1, total, locator)
+ startedAt := time.Now()
+ for done, locator := range toBeCopied {
+ if done == 0 {
+ log.Printf("Copying data block %d of %d (%.2f%% done): %v", done+1, total,
+ float64(done)/float64(total)*100, locator)
+ } else {
+ timePerBlock := time.Since(startedAt) / time.Duration(done)
+ log.Printf("Copying data block %d of %d (%.2f%% done, %v est. time remaining): %v", done+1, total,
+ float64(done)/float64(total)*100, timePerBlock*time.Duration(total-done), locator)
+ }
getLocator := locator
expiresAt := time.Now().AddDate(0, 0, 1)
getLocator = keepclient.SignLocator(getLocator, kcSrc.Arvados.ApiToken, expiresAt, []byte(blobSigningKey))
}
- reader, _, _, err := kcSrc.Get(getLocator)
+ reader, len, _, err := kcSrc.Get(getLocator)
if err != nil {
return fmt.Errorf("Error getting block: %v %v", locator, err)
}
- data, err := ioutil.ReadAll(reader)
- if err != nil {
- return fmt.Errorf("Error reading block data: %v %v", locator, err)
- }
- log.Printf("Writing block%d of %d: %v", locator)
- _, _, err = kcDst.PutB(data)
+ _, _, err = kcDst.PutHR(getLocator[:32], reader, len)
if err != nil {
- return fmt.Errorf("Error putting block data: %v %v", locator, err)
+ return fmt.Errorf("Error copying data block: %v %v", locator, err)
}
-
- done++
- log.Printf("%.2f%% done", float64(done)/float64(total)*100)
}
log.Printf("Successfully copied to destination %d blocks.", total)