7709: Merge branch 'master' into 7709-api-rails4
[arvados.git] / sdk / go / keepclient / support.go
index b12f512507a71d8b07c08da17d7db29f93228fd2..9adbb4878f40541eb13c0feed39bf22241f4c3f5 100644 (file)
@@ -7,9 +7,12 @@ import (
        "git.curoverse.com/arvados.git/sdk/go/streamer"
        "io"
        "io/ioutil"
+       "log"
        "math/rand"
        "net"
        "net/http"
+       "os"
+       "regexp"
        "strings"
        "time"
 )
@@ -19,6 +22,13 @@ import (
 // log.Printf to DebugPrintf.
 var DebugPrintf = func(string, ...interface{}) {}
 
+func init() {
+       var matchTrue = regexp.MustCompile("^(?i:1|yes|true)$")
+       if matchTrue.MatchString(os.Getenv("ARVADOS_DEBUG")) {
+               DebugPrintf = log.Printf
+       }
+}
+
 type keepService struct {
        Uuid     string `json:"uuid"`
        Hostname string `json:"service_host"`
@@ -184,13 +194,13 @@ func (this *KeepClient) putReplicas(
                }()
        }()
 
-       // Desired number of replicas
-       remaining_replicas := this.Want_replicas
+       replicasDone := 0
+       replicasTodo := this.Want_replicas
 
        replicasPerThread := this.replicasPerService
        if replicasPerThread < 1 {
                // unlimited or unknown
-               replicasPerThread = remaining_replicas
+               replicasPerThread = replicasTodo
        }
 
        retriesRemaining := 1 + this.Retries
@@ -200,8 +210,8 @@ func (this *KeepClient) putReplicas(
                retriesRemaining -= 1
                next_server = 0
                retryServers = []string{}
-               for remaining_replicas > 0 {
-                       for active*replicasPerThread < remaining_replicas {
+               for replicasTodo > 0 {
+                       for active*replicasPerThread < replicasTodo {
                                // Start some upload requests
                                if next_server < len(sv) {
                                        DebugPrintf("DEBUG: [%08x] Begin upload %s to %s", requestID, hash, sv[next_server])
@@ -210,14 +220,14 @@ func (this *KeepClient) putReplicas(
                                        active += 1
                                } else {
                                        if active == 0 && retriesRemaining == 0 {
-                                               return locator, (this.Want_replicas - remaining_replicas), InsufficientReplicasError
+                                               return locator, replicasDone, InsufficientReplicasError
                                        } else {
                                                break
                                        }
                                }
                        }
                        DebugPrintf("DEBUG: [%08x] Replicas remaining to write: %v active uploads: %v",
-                               requestID, remaining_replicas, active)
+                               requestID, replicasTodo, active)
 
                        // Now wait for something to happen.
                        if active > 0 {
@@ -226,7 +236,8 @@ func (this *KeepClient) putReplicas(
 
                                if status.statusCode == 200 {
                                        // good news!
-                                       remaining_replicas -= status.replicas_stored
+                                       replicasDone += status.replicas_stored
+                                       replicasTodo -= status.replicas_stored
                                        locator = status.response
                                } else if status.statusCode == 0 || status.statusCode == 408 || status.statusCode == 429 ||
                                        (status.statusCode >= 500 && status.statusCode != 503) {
@@ -242,5 +253,5 @@ func (this *KeepClient) putReplicas(
                sv = retryServers
        }
 
-       return locator, this.Want_replicas, nil
+       return locator, replicasDone, nil
 }