Merge branch '11537-keepproxy-loop'
[arvados.git] / sdk / go / keepclient / discover.go
index 650c2b5f61c40732734de03b1f5852fe0e4fc414..f3e39606980b79b71ddb62ec7d61d90f9b6d0056 100644 (file)
@@ -4,6 +4,7 @@ import (
        "encoding/json"
        "fmt"
        "log"
+       "net/http"
        "os"
        "os/signal"
        "reflect"
@@ -12,16 +13,34 @@ import (
        "time"
 )
 
-// DiscoverKeepServers gets list of available keep services from api server
+// DiscoverKeepServers gets list of available keep services from the
+// API server.
+//
+// If a list of services is provided in the arvadosclient (e.g., from
+// an environment variable or local config), that list is used
+// instead.
 func (this *KeepClient) DiscoverKeepServers() error {
-       var list svcList
+       if this.Arvados.KeepServiceURIs != nil {
+               this.foundNonDiskSvc = true
+               this.replicasPerService = 0
+               if c, ok := this.Client.(*http.Client); ok {
+                       this.setClientSettingsNonDisk(c)
+               }
+               roots := make(map[string]string)
+               for i, uri := range this.Arvados.KeepServiceURIs {
+                       roots[fmt.Sprintf("00000-bi6l4-%015d", i)] = uri
+               }
+               this.SetServiceRoots(roots, roots, roots)
+               return nil
+       }
 
-       // Get keep services from api server
+       // ArvadosClient did not provide a services list. Ask API
+       // server for a list of accessible services.
+       var list svcList
        err := this.Arvados.Call("GET", "keep_services", "", "accessible", nil, &list)
        if err != nil {
                return err
        }
-
        return this.loadKeepServers(list)
 }
 
@@ -57,14 +76,14 @@ func (kc *KeepClient) RefreshServices(interval, errInterval time.Duration) {
                timer.Reset(interval)
 
                if err := kc.DiscoverKeepServers(); err != nil {
-                       log.Println("Error retrieving services list: %v (retrying in %v)", err, errInterval)
+                       log.Printf("WARNING: Error retrieving services list: %v (retrying in %v)", err, errInterval)
                        timer.Reset(errInterval)
                        continue
                }
                newRoots := []map[string]string{kc.LocalRoots(), kc.GatewayRoots()}
 
                if !reflect.DeepEqual(previousRoots, newRoots) {
-                       log.Printf("Updated services list: locals %v gateways %v", newRoots[0], newRoots[1])
+                       DebugPrintf("DEBUG: Updated services list: locals %v gateways %v", newRoots[0], newRoots[1])
                        previousRoots = newRoots
                }
 
@@ -84,7 +103,6 @@ func (this *KeepClient) loadKeepServers(list svcList) error {
 
        // replicasPerService is 1 for disks; unknown or unlimited otherwise
        this.replicasPerService = 1
-       this.Using_proxy = false
 
        for _, service := range list.Items {
                scheme := "http"
@@ -100,10 +118,6 @@ func (this *KeepClient) loadKeepServers(list svcList) error {
                listed[url] = true
 
                localRoots[service.Uuid] = url
-               if service.SvcType == "proxy" {
-                       this.Using_proxy = true
-               }
-
                if service.ReadOnly == false {
                        writableLocalRoots[service.Uuid] = url
                        if service.SvcType != "disk" {
@@ -111,6 +125,10 @@ func (this *KeepClient) loadKeepServers(list svcList) error {
                        }
                }
 
+               if service.SvcType != "disk" {
+                       this.foundNonDiskSvc = true
+               }
+
                // Gateway services are only used when specified by
                // UUID, so there's nothing to gain by filtering them
                // by service type. Including all accessible services
@@ -119,10 +137,12 @@ func (this *KeepClient) loadKeepServers(list svcList) error {
                gatewayRoots[service.Uuid] = url
        }
 
-       if this.Using_proxy {
-               this.setClientSettingsProxy()
-       } else {
-               this.setClientSettingsDisk()
+       if client, ok := this.Client.(*http.Client); ok {
+               if this.foundNonDiskSvc {
+                       this.setClientSettingsNonDisk(client)
+               } else {
+                       this.setClientSettingsDisk(client)
+               }
        }
 
        this.SetServiceRoots(localRoots, writableLocalRoots, gatewayRoots)