Merge branch 'master' into 7167-keep-rsync
authorradhika <radhika@curoverse.com>
Wed, 7 Oct 2015 17:52:43 +0000 (13:52 -0400)
committerradhika <radhika@curoverse.com>
Wed, 7 Oct 2015 17:52:43 +0000 (13:52 -0400)
sdk/go/arvadosclient/arvadosclient.go
sdk/go/arvadostest/run_servers.go
sdk/go/keepclient/keepclient.go
sdk/go/keepclient/support.go
sdk/python/tests/run_test_server.py
tools/keep-rsync/.gitignore [new file with mode: 0644]
tools/keep-rsync/keep-rsync.go [new file with mode: 0644]
tools/keep-rsync/keep-rsync_test.go [new file with mode: 0644]

index 1cce0a7fc92d24e21fa694add86c75c63952eb46..cc99efdcf4b5deb327113780fdb8bf355163becf 100644 (file)
@@ -78,21 +78,42 @@ type ArvadosClient struct {
        DiscoveryDoc Dict
 }
 
-// Create a new ArvadosClient, initialized with standard Arvados environment
-// variables ARVADOS_API_HOST, ARVADOS_API_TOKEN, and (optionally)
-// ARVADOS_API_HOST_INSECURE.
+// APIConfig struct consists of:
+//    APIToken        string
+//    APIHost         string
+//    APIHostInsecure bool
+//    ExternalClient  bool
+type APIConfig struct {
+       APIToken        string
+       APIHost         string
+       APIHostInsecure bool
+       ExternalClient  bool
+}
+
+// Create a new ArvadosClient, initialized with standard Arvados environment variables
+// ARVADOS_API_HOST, ARVADOS_API_TOKEN, ARVADOS_API_HOST_INSECURE, ARVADOS_EXTERNAL_CLIENT.
 func MakeArvadosClient() (ac ArvadosClient, err error) {
+       var config APIConfig
+       config.APIToken = os.Getenv("ARVADOS_API_TOKEN")
+       config.APIHost = os.Getenv("ARVADOS_API_HOST")
+
        var matchTrue = regexp.MustCompile("^(?i:1|yes|true)$")
-       insecure := matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE"))
-       external := matchTrue.MatchString(os.Getenv("ARVADOS_EXTERNAL_CLIENT"))
 
+       config.APIHostInsecure = matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE"))
+       config.ExternalClient = matchTrue.MatchString(os.Getenv("ARVADOS_EXTERNAL_CLIENT"))
+
+       return MakeArvadosClientWithConfig(config)
+}
+
+// Create a new ArvadosClient, using the given input parameters.
+func MakeArvadosClientWithConfig(config APIConfig) (ac ArvadosClient, err error) {
        ac = ArvadosClient{
-               ApiServer:   os.Getenv("ARVADOS_API_HOST"),
-               ApiToken:    os.Getenv("ARVADOS_API_TOKEN"),
-               ApiInsecure: insecure,
+               ApiServer:   config.APIHost,
+               ApiToken:    config.APIToken,
+               ApiInsecure: config.APIHostInsecure,
                Client: &http.Client{Transport: &http.Transport{
-                       TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure}}},
-               External: external}
+                       TLSClientConfig: &tls.Config{InsecureSkipVerify: config.APIHostInsecure}}},
+               External: config.ExternalClient}
 
        if ac.ApiServer == "" {
                return ac, MissingArvadosApiHost
index cad16917dba286504f6693cac3a3fbd4d05a741e..a1751361642de4a663f50612475a2ede82fc6f82 100644 (file)
@@ -99,11 +99,18 @@ func StopAPI() {
 }
 
 func StartKeep() {
+       StartKeepAdditional(false)
+}
+
+func StartKeepAdditional(keepExisting bool) {
        cwd, _ := os.Getwd()
        defer os.Chdir(cwd)
        chdirToPythonTests()
 
        cmd := exec.Command("python", "run_test_server.py", "start_keep")
+       if keepExisting {
+               cmd = exec.Command("python", "run_test_server.py", "start_keep", "--keep_existing", "true")
+       }
        stderr, err := cmd.StderrPipe()
        if err != nil {
                log.Fatalf("Setting up stderr pipe: %s", err)
index 53dfb2b5384302b0f113ec608c016760a2ca77ce..05fad6e10e33f1daa3c93d8c3dab2a6ad07b3b78 100644 (file)
@@ -54,9 +54,20 @@ type KeepClient struct {
        replicasPerService int
 }
 
-// Create a new KeepClient.  This will contact the API server to discover Keep
-// servers.
+// MakeKeepClient creates a new KeepClient by contacting the API server to discover Keep servers.
 func MakeKeepClient(arv *arvadosclient.ArvadosClient) (*KeepClient, error) {
+       kc := initKeepClient(arv)
+       return kc, kc.DiscoverKeepServers()
+}
+
+// MakeKeepClientFromJSON creates a new KeepClient using the given json to load keep servers.
+func MakeKeepClientFromJSON(arv *arvadosclient.ArvadosClient, svcJSON string) (*KeepClient, error) {
+       kc := initKeepClient(arv)
+       return kc, kc.DiscoverKeepServersFromJSON(svcJSON)
+}
+
+// Make a new KeepClient struct.
+func initKeepClient(arv *arvadosclient.ArvadosClient) *KeepClient {
        var matchTrue = regexp.MustCompile("^(?i:1|yes|true)$")
        insecure := matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE"))
        kc := &KeepClient{
@@ -66,7 +77,7 @@ func MakeKeepClient(arv *arvadosclient.ArvadosClient) (*KeepClient, error) {
                Client: &http.Client{Transport: &http.Transport{
                        TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure}}},
        }
-       return kc, kc.DiscoverKeepServers()
+       return kc
 }
 
 // Put a block given the block hash, a reader, and the number of bytes
index 63800b1e08577edec185cab13bc6c97780b349b1..8be178036ad0cce1a447391cf0d3a4ec8e8a625d 100644 (file)
@@ -2,6 +2,7 @@ package keepclient
 
 import (
        "crypto/md5"
+       "encoding/json"
        "errors"
        "fmt"
        "git.curoverse.com/arvados.git/sdk/go/streamer"
@@ -76,19 +77,38 @@ func (this *KeepClient) setClientSettingsDisk() {
        }
 }
 
+type svcList struct {
+       Items []keepService `json:"items"`
+}
+
 // DiscoverKeepServers gets list of available keep services from api server
 func (this *KeepClient) DiscoverKeepServers() error {
-       type svcList struct {
-               Items []keepService `json:"items"`
-       }
-       var m svcList
+       var list svcList
 
        // Get keep services from api server
-       err := this.Arvados.Call("GET", "keep_services", "", "accessible", nil, &m)
+       err := this.Arvados.Call("GET", "keep_services", "", "accessible", nil, &list)
        if err != nil {
                return err
        }
 
+       return this.loadKeepServers(list)
+}
+
+// DiscoverKeepServersFromJSON gets list of available keep services from given JSON
+func (this *KeepClient) DiscoverKeepServersFromJSON(services string) error {
+       var list svcList
+
+       // Load keep services from given json
+       dec := json.NewDecoder(strings.NewReader(services))
+       if err := dec.Decode(&list); err != nil {
+               return err
+       }
+
+       return this.loadKeepServers(list)
+}
+
+// loadKeepServers
+func (this *KeepClient) loadKeepServers(list svcList) error {
        listed := make(map[string]bool)
        localRoots := make(map[string]string)
        gatewayRoots := make(map[string]string)
@@ -98,7 +118,7 @@ func (this *KeepClient) DiscoverKeepServers() error {
        this.replicasPerService = 1
        this.Using_proxy = false
 
-       for _, service := range m.Items {
+       for _, service := range list.Items {
                scheme := "http"
                if service.SSL {
                        scheme = "https"
index 5d0c42ad2109e2d605f5ab45fea5bd64fc26b1e8..fba9bb69295a9cdf4dec4999aa2c8d4ee40b56bb 100644 (file)
@@ -43,6 +43,7 @@ if not os.path.exists(TEST_TMPDIR):
 
 my_api_host = None
 _cached_config = {}
+keep_existing = None
 
 def find_server_pid(PID_PATH, wait=10):
     now = time.time()
@@ -324,7 +325,8 @@ def _start_keep(n, keep_args):
     return port
 
 def run_keep(blob_signing_key=None, enforce_permissions=False):
-    stop_keep()
+    if keep_existing is None:
+      stop_keep()
 
     keep_args = {}
     if not blob_signing_key:
@@ -344,12 +346,18 @@ def run_keep(blob_signing_key=None, enforce_permissions=False):
         host=os.environ['ARVADOS_API_HOST'],
         token=os.environ['ARVADOS_API_TOKEN'],
         insecure=True)
+
     for d in api.keep_services().list().execute()['items']:
         api.keep_services().delete(uuid=d['uuid']).execute()
     for d in api.keep_disks().list().execute()['items']:
         api.keep_disks().delete(uuid=d['uuid']).execute()
 
-    for d in range(0, 2):
+    start_index = 0
+    end_index = 2
+    if keep_existing is not None:
+        start_index = 2
+        end_index = 3
+    for d in range(start_index, end_index):
         port = _start_keep(d, keep_args)
         svc = api.keep_services().create(body={'keep_service': {
             'uuid': 'zzzzz-bi6l4-keepdisk{:07d}'.format(d),
@@ -374,6 +382,8 @@ def _stop_keep(n):
 def stop_keep():
     _stop_keep(0)
     _stop_keep(1)
+    # We may have created an additional keep servers when keep_existing is used
+    _stop_keep(2)
 
 def run_keep_proxy():
     if 'ARVADOS_TEST_PROXY_SERVICES' in os.environ:
@@ -595,8 +605,11 @@ if __name__ == "__main__":
     parser = argparse.ArgumentParser()
     parser.add_argument('action', type=str, help="one of {}".format(actions))
     parser.add_argument('--auth', type=str, metavar='FIXTURE_NAME', help='Print authorization info for given api_client_authorizations fixture')
+    parser.add_argument('--keep_existing', type=str, help="Used to add additional keep servers, without terminating existing servers")
     args = parser.parse_args()
 
+    keep_existing = args.keep_existing
+
     if args.action not in actions:
         print("Unrecognized action '{}'. Actions are: {}.".format(args.action, actions), file=sys.stderr)
         sys.exit(1)
diff --git a/tools/keep-rsync/.gitignore b/tools/keep-rsync/.gitignore
new file mode 100644 (file)
index 0000000..5ee7f3b
--- /dev/null
@@ -0,0 +1 @@
+keep-rsync
diff --git a/tools/keep-rsync/keep-rsync.go b/tools/keep-rsync/keep-rsync.go
new file mode 100644 (file)
index 0000000..72f4356
--- /dev/null
@@ -0,0 +1,299 @@
+package main
+
+import (
+       "bytes"
+       "flag"
+       "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
+       "git.curoverse.com/arvados.git/sdk/go/keepclient"
+       "io/ioutil"
+       "log"
+       "regexp"
+       "strings"
+)
+
+// keep-rsync arguments
+var (
+       srcConfig           arvadosclient.APIConfig
+       dstConfig           arvadosclient.APIConfig
+       srcKeepServicesJSON string
+       dstKeepServicesJSON string
+       replications        int
+       prefix              string
+)
+
+func main() {
+       var srcConfigFile string
+       var dstConfigFile string
+
+       flag.StringVar(
+               &srcConfigFile,
+               "src-config-file",
+               "",
+               "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, and ARVADOS_BLOB_SIGNING_KEY.")
+
+       flag.StringVar(
+               &dstConfigFile,
+               "dst-config-file",
+               "",
+               "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, and ARVADOS_BLOB_SIGNING_KEY.")
+
+       flag.StringVar(
+               &srcKeepServicesJSON,
+               "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,
+               "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",
+               3,
+               "Number of replications to write to the destination.")
+
+       flag.StringVar(
+               &prefix,
+               "prefix",
+               "",
+               "Index prefix")
+
+       flag.Parse()
+
+       var err error
+
+       // Load config
+       if srcConfigFile == "" {
+               log.Fatal("-src-config-file must be specified.")
+       }
+       srcConfig, err = readConfigFromFile(srcConfigFile)
+       if err != nil {
+               log.Fatal("Error reading source configuration: %s", err.Error())
+       }
+
+       if dstConfigFile == "" {
+               log.Fatal("-dst-config-file must be specified.")
+       }
+       dstConfig, err = readConfigFromFile(dstConfigFile)
+       if err != nil {
+               log.Fatal("Error reading destination configuration: %s", err.Error())
+       }
+
+       // Initialize keep-rsync
+       err = initializeKeepRsync()
+       if err != nil {
+               log.Fatal("Error configurating keep-rsync: %s", err.Error())
+       }
+
+       // Copy blocks not found in dst from src
+       performKeepRsync()
+}
+
+var matchTrue = regexp.MustCompile("^(?i:1|yes|true)$")
+
+// Reads config from file
+func readConfigFromFile(filename string) (arvadosclient.APIConfig, error) {
+       var config arvadosclient.APIConfig
+
+       content, err := ioutil.ReadFile(filename)
+       if err != nil {
+               return config, err
+       }
+
+       lines := strings.Split(string(content), "\n")
+       for _, line := range lines {
+               if line == "" {
+                       continue
+               }
+               kv := strings.Split(line, "=")
+
+               switch kv[0] {
+               case "ARVADOS_API_TOKEN":
+                       config.APIToken = kv[1]
+               case "ARVADOS_API_HOST":
+                       config.APIHost = kv[1]
+               case "ARVADOS_API_HOST_INSECURE":
+                       config.APIHostInsecure = matchTrue.MatchString(kv[1])
+               case "ARVADOS_EXTERNAL_CLIENT":
+                       config.ExternalClient = matchTrue.MatchString(kv[1])
+               }
+       }
+       return config, nil
+}
+
+// keep-rsync source and destination clients
+var (
+       arvSrc arvadosclient.ArvadosClient
+       arvDst arvadosclient.ArvadosClient
+       kcSrc  *keepclient.KeepClient
+       kcDst  *keepclient.KeepClient
+)
+
+// Initializes keep-rsync using the config provided
+func initializeKeepRsync() (err error) {
+       // arvSrc from srcConfig
+       arvSrc, err = arvadosclient.MakeArvadosClientWithConfig(srcConfig)
+       if err != nil {
+               return
+       }
+
+       // arvDst from dstConfig
+       arvDst, err = arvadosclient.MakeArvadosClientWithConfig(dstConfig)
+       if err != nil {
+               return
+       }
+
+       // if srcKeepServicesJSON is provided, use it to load services; else, use DiscoverKeepServers
+       if srcKeepServicesJSON == "" {
+               kcSrc, err = keepclient.MakeKeepClient(&arvSrc)
+               if err != nil {
+                       return
+               }
+       } else {
+               kcSrc, err = keepclient.MakeKeepClientFromJSON(&arvSrc, srcKeepServicesJSON)
+               if err != nil {
+                       return
+               }
+       }
+
+       // if dstKeepServicesJSON is provided, use it to load services; else, use DiscoverKeepServers
+       if dstKeepServicesJSON == "" {
+               kcDst, err = keepclient.MakeKeepClient(&arvDst)
+               if err != nil {
+                       return
+               }
+       } else {
+               kcDst, err = keepclient.MakeKeepClientFromJSON(&arvDst, dstKeepServicesJSON)
+               if err != nil {
+                       return
+               }
+       }
+       kcDst.Want_replicas = replications
+
+       return
+}
+
+// Get unique block locators from src and dst
+// Copy any blocks missing in dst
+func performKeepRsync() error {
+       // Get unique locators from src
+       srcIndex, err := getUniqueLocators(kcSrc, prefix)
+       if err != nil {
+               return err
+       }
+
+       // Get unique locators from dst
+       dstIndex, err := getUniqueLocators(kcDst, prefix)
+       if err != nil {
+               return err
+       }
+
+       // Get list of locators found in src, but missing in dst
+       toBeCopied := getMissingLocators(srcIndex, dstIndex)
+
+       // Copy each missing block to dst
+       copyBlocksToDst(toBeCopied)
+
+       return nil
+}
+
+// Get list of unique locators from the specified cluster
+func getUniqueLocators(kc *keepclient.KeepClient, prefix string) (map[string]bool, error) {
+       var indexBytes []byte
+
+       for uuid := range kc.LocalRoots() {
+               reader, err := kc.GetIndex(uuid, prefix)
+               if err != nil {
+                       return nil, err
+               }
+
+               var readBytes []byte
+               readBytes, err = ioutil.ReadAll(reader)
+               if err != nil {
+                       return nil, err
+               }
+
+               indexBytes = append(indexBytes, readBytes...)
+       }
+
+       // Got index; Now dedup it
+       locators := bytes.Split(indexBytes, []byte("\n"))
+
+       uniqueLocators := map[string]bool{}
+       for _, loc := range locators {
+               if len(loc) == 0 {
+                       continue
+               }
+
+               locator := string(bytes.Split(loc, []byte(" "))[0])
+               if _, ok := uniqueLocators[locator]; !ok {
+                       uniqueLocators[locator] = true
+               }
+       }
+       return uniqueLocators, nil
+}
+
+// Get list of locators that are in src but not in dst
+func getMissingLocators(srcLocators map[string]bool, dstLocators map[string]bool) []string {
+       var missingLocators []string
+       for locator := range srcLocators {
+               if _, ok := dstLocators[locator]; !ok {
+                       missingLocators = append(missingLocators, locator)
+               }
+       }
+       return missingLocators
+}
+
+// Copy blocks from src to dst; only those that are missing in dst are copied
+func copyBlocksToDst(toBeCopied []string) {
+       done := 0
+       total := len(toBeCopied)
+       var failed []string
+
+       for _, locator := range toBeCopied {
+               log.Printf("Getting block %d of %d", done+1, total)
+
+               log.Printf("Getting block: %v", locator)
+
+               reader, _, _, err := kcSrc.Get(locator)
+               if err != nil {
+                       log.Printf("Error getting block: %q %v", locator, err)
+                       failed = append(failed, locator)
+                       continue
+               }
+               data, err := ioutil.ReadAll(reader)
+               if err != nil {
+                       log.Printf("Error reading block data: %q %v", locator, err)
+                       failed = append(failed, locator)
+                       continue
+               }
+
+               log.Printf("Copying block: %q", locator)
+               _, rep, err := kcDst.PutB(data)
+               if err != nil {
+                       log.Printf("Error putting block data: %q %v", locator, err)
+                       failed = append(failed, locator)
+                       continue
+               }
+               if rep != replications {
+                       log.Printf("Failed to put enough number of replicas. Wanted: %d; Put: %d", replications, rep)
+                       failed = append(failed, locator)
+                       continue
+               }
+
+               done++
+               log.Printf("%.2f%% done", float64(done)/float64(total)*100)
+       }
+
+       log.Printf("Successfully copied to destination %d and failed %d out of a total of %d", done, len(failed), total)
+       log.Printf("Failed blocks %v", failed)
+}
diff --git a/tools/keep-rsync/keep-rsync_test.go b/tools/keep-rsync/keep-rsync_test.go
new file mode 100644 (file)
index 0000000..4e1cb2b
--- /dev/null
@@ -0,0 +1,252 @@
+package main
+
+import (
+       "crypto/md5"
+       "fmt"
+       "io/ioutil"
+       "os"
+       "regexp"
+       "testing"
+
+       "git.curoverse.com/arvados.git/sdk/go/arvadostest"
+       "git.curoverse.com/arvados.git/sdk/go/keepclient"
+
+       . "gopkg.in/check.v1"
+)
+
+// Gocheck boilerplate
+func Test(t *testing.T) {
+       TestingT(t)
+}
+
+// Gocheck boilerplate
+var _ = Suite(&ServerRequiredSuite{})
+
+// Tests that require the Keep server running
+type ServerRequiredSuite struct{}
+
+func (s *ServerRequiredSuite) SetUpSuite(c *C) {
+}
+
+func (s *ServerRequiredSuite) SetUpTest(c *C) {
+       arvadostest.ResetEnv()
+       srcKeepServicesJSON = ""
+       dstKeepServicesJSON = ""
+}
+
+func (s *ServerRequiredSuite) TearDownSuite(c *C) {
+       arvadostest.StopKeep()
+       arvadostest.StopAPI()
+}
+
+// Testing keep-rsync needs two sets of keep services: src and dst.
+// The test setup hence tweaks keep-rsync initialzation to achieve this.
+// First invoke initializeKeepRsync and then invoke StartKeepAdditional
+// to create the keep servers to be used as destination.
+func setupRsync(c *C) {
+       // srcConfig
+       srcConfig.APIHost = os.Getenv("ARVADOS_API_HOST")
+       srcConfig.APIToken = os.Getenv("ARVADOS_API_TOKEN")
+       srcConfig.APIHostInsecure = matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE"))
+
+       // dstConfig
+       dstConfig.APIHost = os.Getenv("ARVADOS_API_HOST")
+       dstConfig.APIToken = os.Getenv("ARVADOS_API_TOKEN")
+       dstConfig.APIHostInsecure = matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE"))
+
+       replications = 1
+
+       // Start API and Keep servers
+       arvadostest.StartAPI()
+       arvadostest.StartKeep()
+
+       // initialize keep-rsync
+       err := initializeKeepRsync()
+       c.Assert(err, Equals, nil)
+
+       // Create two more keep servers to be used as destination
+       arvadostest.StartKeepAdditional(true)
+
+       // load kcDst
+       kcDst, err = keepclient.MakeKeepClient(&arvDst)
+       c.Assert(err, Equals, nil)
+       kcDst.Want_replicas = 1
+}
+
+// Test readConfigFromFile method
+func (s *ServerRequiredSuite) TestReadConfigFromFile(c *C) {
+       // Setup a test config file
+       file, err := ioutil.TempFile(os.TempDir(), "config")
+       c.Assert(err, Equals, nil)
+       defer os.Remove(file.Name())
+
+       fileContent := "ARVADOS_API_HOST=testhost\n"
+       fileContent += "ARVADOS_API_TOKEN=testtoken\n"
+       fileContent += "ARVADOS_API_HOST_INSECURE=true"
+
+       _, err = file.Write([]byte(fileContent))
+
+       // Invoke readConfigFromFile method with this test filename
+       config, err := readConfigFromFile(file.Name())
+       c.Assert(err, Equals, nil)
+       c.Assert(config.APIHost, Equals, "testhost")
+       c.Assert(config.APIToken, Equals, "testtoken")
+       c.Assert(config.APIHostInsecure, Equals, true)
+       c.Assert(config.ExternalClient, Equals, false)
+}
+
+// Test keep-rsync initialization, with src and dst keep servers.
+// Do a Put and Get in src, both of which should succeed.
+// Do a Put and Get in dst, both of which should succeed.
+// Do a Get in dst for the src hash, which should raise block not found error.
+// Do a Get in src for the dst hash, which should raise block not found error.
+func (s *ServerRequiredSuite) TestRsyncPutInOne_GetFromOtherShouldFail(c *C) {
+       setupRsync(c)
+
+       // Put a block in src using kcSrc and Get it
+       srcData := []byte("test-data1")
+       locatorInSrc := fmt.Sprintf("%x", md5.Sum(srcData))
+
+       hash, rep, err := kcSrc.PutB(srcData)
+       c.Check(hash, Matches, fmt.Sprintf(`^%s\+10(\+.+)?$`, locatorInSrc))
+       c.Check(rep, Equals, 2)
+       c.Check(err, Equals, nil)
+
+       reader, blocklen, _, err := kcSrc.Get(locatorInSrc)
+       c.Assert(err, Equals, nil)
+       c.Check(blocklen, Equals, int64(10))
+       all, err := ioutil.ReadAll(reader)
+       c.Check(all, DeepEquals, srcData)
+
+       // Put a different block in src using kcSrc and Get it
+       dstData := []byte("test-data2")
+       locatorInDst := fmt.Sprintf("%x", md5.Sum(dstData))
+
+       hash, rep, err = kcDst.PutB(dstData)
+       c.Check(hash, Matches, fmt.Sprintf(`^%s\+10(\+.+)?$`, locatorInDst))
+       c.Check(rep, Equals, 1)
+       c.Check(err, Equals, nil)
+
+       reader, blocklen, _, err = kcDst.Get(locatorInDst)
+       c.Assert(err, Equals, nil)
+       c.Check(blocklen, Equals, int64(10))
+       all, err = ioutil.ReadAll(reader)
+       c.Check(all, DeepEquals, dstData)
+
+       // Get srcLocator using kcDst should fail with NotFound error
+       _, _, _, err = kcDst.Get(locatorInSrc)
+       c.Assert(err.Error(), Equals, "Block not found")
+
+       // Get dstLocator using kcSrc should fail with NotFound error
+       _, _, _, err = kcSrc.Get(locatorInDst)
+       c.Assert(err.Error(), Equals, "Block not found")
+}
+
+// Test keep-rsync initialization, with srcKeepServicesJSON
+func (s *ServerRequiredSuite) TestRsyncInitializeWithKeepServicesJSON(c *C) {
+       srcKeepServicesJSON = "{ \"kind\":\"arvados#keepServiceList\", \"etag\":\"\", \"self_link\":\"\", \"offset\":null, \"limit\":null, \"items\":[ { \"href\":\"/keep_services/zzzzz-bi6l4-123456789012340\", \"kind\":\"arvados#keepService\", \"etag\":\"641234567890enhj7hzx432e5\", \"uuid\":\"zzzzz-bi6l4-123456789012340\", \"owner_uuid\":\"zzzzz-tpzed-123456789012345\", \"service_host\":\"keep0.zzzzz.arvadosapi.com\", \"service_port\":25107, \"service_ssl_flag\":false, \"service_type\":\"disk\", \"read_only\":false }, { \"href\":\"/keep_services/zzzzz-bi6l4-123456789012341\", \"kind\":\"arvados#keepService\", \"etag\":\"641234567890enhj7hzx432e5\", \"uuid\":\"zzzzz-bi6l4-123456789012341\", \"owner_uuid\":\"zzzzz-tpzed-123456789012345\", \"service_host\":\"keep0.zzzzz.arvadosapi.com\", \"service_port\":25108, \"service_ssl_flag\":false, \"service_type\":\"disk\", \"read_only\":false } ], \"items_available\":2 }"
+
+       setupRsync(c)
+
+       localRoots := kcSrc.LocalRoots()
+       c.Check(localRoots != nil, Equals, true)
+
+       foundIt := false
+       for k := range localRoots {
+               if k == "zzzzz-bi6l4-123456789012340" {
+                       foundIt = true
+               }
+       }
+       c.Check(foundIt, Equals, true)
+
+       foundIt = false
+       for k := range localRoots {
+               if k == "zzzzz-bi6l4-123456789012341" {
+                       foundIt = true
+               }
+       }
+       c.Check(foundIt, Equals, true)
+}
+
+// Put 5 blocks in src. Put 2 of those blocks in dst
+// Hence there are 3 additional blocks in src
+// Also, put 2 extra blocks in dts; they are hence only in dst
+// Run rsync and verify that those 7 blocks are now available in dst
+func (s *ServerRequiredSuite) TestKeepRsync(c *C) {
+       setupRsync(c)
+
+       // Put a few blocks in src using kcSrc
+       var srcLocators []string
+       for i := 0; i < 5; i++ {
+               data := []byte(fmt.Sprintf("test-data-%d", i))
+               hash := fmt.Sprintf("%x", md5.Sum(data))
+
+               hash2, rep, err := kcSrc.PutB(data)
+               c.Check(hash2, Matches, fmt.Sprintf(`^%s\+11(\+.+)?$`, hash))
+               c.Check(rep, Equals, 2)
+               c.Check(err, Equals, nil)
+
+               reader, blocklen, _, err := kcSrc.Get(hash)
+               c.Assert(err, Equals, nil)
+               c.Check(blocklen, Equals, int64(11))
+               all, err := ioutil.ReadAll(reader)
+               c.Check(all, DeepEquals, data)
+
+               srcLocators = append(srcLocators, fmt.Sprintf("%s+%d", hash, blocklen))
+       }
+
+       // Put just two of those blocks in dst using kcDst
+       var dstLocators []string
+       for i := 0; i < 2; i++ {
+               data := []byte(fmt.Sprintf("test-data-%d", i))
+               hash := fmt.Sprintf("%x", md5.Sum(data))
+
+               hash2, rep, err := kcDst.PutB(data)
+               c.Check(hash2, Matches, fmt.Sprintf(`^%s\+11(\+.+)?$`, hash))
+               c.Check(rep, Equals, 1)
+               c.Check(err, Equals, nil)
+
+               reader, blocklen, _, err := kcDst.Get(hash)
+               c.Assert(err, Equals, nil)
+               c.Check(blocklen, Equals, int64(11))
+               all, err := ioutil.ReadAll(reader)
+               c.Check(all, DeepEquals, data)
+
+               dstLocators = append(dstLocators, fmt.Sprintf("%s+%d", hash, blocklen))
+       }
+
+       // Put two more blocks in dst; they are not in src at all
+       var extraDstLocators []string
+       for i := 0; i < 2; i++ {
+               data := []byte(fmt.Sprintf("other-data-%d", i))
+               hash := fmt.Sprintf("%x", md5.Sum(data))
+
+               hash2, rep, err := kcDst.PutB(data)
+               c.Check(hash2, Matches, fmt.Sprintf(`^%s\+12(\+.+)?$`, hash))
+               c.Check(rep, Equals, 1)
+               c.Check(err, Equals, nil)
+
+               reader, blocklen, _, err := kcDst.Get(hash)
+               c.Assert(err, Equals, nil)
+               c.Check(blocklen, Equals, int64(12))
+               all, err := ioutil.ReadAll(reader)
+               c.Check(all, DeepEquals, data)
+
+               extraDstLocators = append(extraDstLocators, fmt.Sprintf("%s+%d", hash, blocklen))
+       }
+
+       err := performKeepRsync()
+       c.Check(err, Equals, nil)
+
+       // Now GetIndex from dst and verify that all 5 from src and the 2 extra blocks are found
+       dstIndex, err := getUniqueLocators(kcDst, "")
+       c.Check(err, Equals, nil)
+       for _, locator := range srcLocators {
+               _, ok := dstIndex[locator]
+               c.Assert(ok, Equals, true)
+       }
+       for _, locator := range extraDstLocators {
+               _, ok := dstIndex[locator]
+               c.Assert(ok, Equals, true)
+       }
+}