4869: Improve logging
[arvados.git] / sdk / go / keepclient / support.go
index ce15ce91adbab7926c0eca76ece43b52ab051bb2..26338ca571ecc8494287988fa8c8b397efbbca93 100644 (file)
@@ -2,28 +2,35 @@
 package keepclient
 
 import (
-       "git.curoverse.com/arvados.git/sdk/go/streamer"
+       "crypto/md5"
        "errors"
        "fmt"
+       "git.curoverse.com/arvados.git/sdk/go/streamer"
        "io"
        "io/ioutil"
        "log"
        "net/http"
        "os"
-       "strconv"
        "strings"
+       "time"
 )
 
 type keepDisk struct {
+       Uuid     string `json:"uuid"`
        Hostname string `json:"service_host"`
        Port     int    `json:"service_port"`
        SSL      bool   `json:"service_ssl_flag"`
        SvcType  string `json:"service_type"`
 }
 
+func Md5String(s string) string {
+       return fmt.Sprintf("%x", md5.Sum([]byte(s)))
+}
+
 func (this *KeepClient) DiscoverKeepServers() error {
        if prx := os.Getenv("ARVADOS_KEEP_PROXY"); prx != "" {
-               this.SetServiceRoots([]string{prx})
+               sr := map[string]string{"proxy": prx}
+               this.SetServiceRoots(sr)
                this.Using_proxy = true
                return nil
        }
@@ -42,7 +49,7 @@ func (this *KeepClient) DiscoverKeepServers() error {
        }
 
        listed := make(map[string]bool)
-       service_roots := make([]string, 0, len(m.Items))
+       service_roots := make(map[string]string)
 
        for _, element := range m.Items {
                n := ""
@@ -57,7 +64,7 @@ func (this *KeepClient) DiscoverKeepServers() error {
                // Skip duplicates
                if !listed[url] {
                        listed[url] = true
-                       service_roots = append(service_roots, url)
+                       service_roots[element.Uuid] = url
                }
                if element.SvcType == "proxy" {
                        this.Using_proxy = true
@@ -69,55 +76,6 @@ func (this *KeepClient) DiscoverKeepServers() error {
        return nil
 }
 
-func (this KeepClient) shuffledServiceRoots(hash string) (pseq []string) {
-       // Build an ordering with which to query the Keep servers based on the
-       // contents of the hash.  "hash" is a hex-encoded number at least 8
-       // digits (32 bits) long
-
-       // seed used to calculate the next keep server from 'pool' to be added
-       // to 'pseq'
-       seed := hash
-
-       // Keep servers still to be added to the ordering
-       service_roots := this.ServiceRoots()
-       pool := make([]string, len(service_roots))
-       copy(pool, service_roots)
-
-       // output probe sequence
-       pseq = make([]string, 0, len(service_roots))
-
-       // iterate while there are servers left to be assigned
-       for len(pool) > 0 {
-
-               if len(seed) < 8 {
-                       // ran out of digits in the seed
-                       if len(pseq) < (len(hash) / 4) {
-                               // the number of servers added to the probe
-                               // sequence is less than the number of 4-digit
-                               // slices in 'hash' so refill the seed with the
-                               // last 4 digits.
-                               seed = hash[len(hash)-4:]
-                       }
-                       seed += hash
-               }
-
-               // Take the next 8 digits (32 bytes) and interpret as an integer,
-               // then modulus with the size of the remaining pool to get the next
-               // selected server.
-               probe, _ := strconv.ParseUint(seed[0:8], 16, 32)
-               probe %= uint64(len(pool))
-
-               // Append the selected server to the probe sequence and remove it
-               // from the pool.
-               pseq = append(pseq, pool[probe])
-               pool = append(pool[:probe], pool[probe+1:]...)
-
-               // Remove the digits just used from the seed
-               seed = seed[8:]
-       }
-       return pseq
-}
-
 type uploadStatus struct {
        err             error
        url             string
@@ -127,9 +85,9 @@ type uploadStatus struct {
 }
 
 func (this KeepClient) uploadToKeepServer(host string, hash string, body io.ReadCloser,
-       upload_status chan<- uploadStatus, expectedLength int64) {
+       upload_status chan<- uploadStatus, expectedLength int64, tag string) {
 
-       log.Printf("Uploading %s to %s", hash, host)
+       log.Printf("[%v] Begin upload %s to %s", tag, hash, host)
 
        var req *http.Request
        var err error
@@ -179,7 +137,7 @@ func (this KeepClient) uploadToKeepServer(host string, hash string, body io.Read
        if resp.StatusCode == http.StatusOK {
                upload_status <- uploadStatus{nil, url, resp.StatusCode, rep, locator}
        } else {
-               upload_status <- uploadStatus{errors.New(resp.Status), url, resp.StatusCode, rep, locator}
+               upload_status <- uploadStatus{errors.New(resp.Status), url, resp.StatusCode, rep, string(respbody)}
        }
 }
 
@@ -188,8 +146,12 @@ func (this KeepClient) putReplicas(
        tr *streamer.AsyncStream,
        expectedLength int64) (locator string, replicas int, err error) {
 
+       // Take the hash of locator and timestamp in order to identify this
+       // specific transaction in log statements.
+       tag := fmt.Sprintf("%x", md5.Sum([]byte(locator+time.Now().String())))[0:8]
+
        // Calculate the ordering for uploading to servers
-       sv := this.shuffledServiceRoots(hash)
+       sv := NewRootSorter(this.ServiceRoots(), hash).GetSortedRoots()
 
        // The next server to try contacting
        next_server := 0
@@ -202,14 +164,13 @@ func (this KeepClient) putReplicas(
        defer close(upload_status)
 
        // Desired number of replicas
-
        remaining_replicas := this.Want_replicas
 
        for remaining_replicas > 0 {
                for active < remaining_replicas {
                        // Start some upload requests
                        if next_server < len(sv) {
-                               go this.uploadToKeepServer(sv[next_server], hash, tr.MakeStreamReader(), upload_status, expectedLength)
+                               go this.uploadToKeepServer(sv[next_server], hash, tr.MakeStreamReader(), upload_status, expectedLength, tag)
                                next_server += 1
                                active += 1
                        } else {
@@ -223,17 +184,19 @@ func (this KeepClient) putReplicas(
 
                // Now wait for something to happen.
                status := <-upload_status
+               log.Printf("[%v] Upload to %v status code: %v remaining replicas: %v active: %v",
+                       tag, status.url, status.statusCode, remaining_replicas, active)
                if status.statusCode == 200 {
                        // good news!
                        remaining_replicas -= status.replicas_stored
                        locator = status.response
                } else {
                        // writing to keep server failed for some reason
-                       log.Printf("Keep server put to %v failed with '%v'",
-                               status.url, status.err)
+                       log.Printf("[%v] Upload to %v failed with error '%v', response '%v'",
+                               tag, status.url, status.statusCode, status.err, status.response)
                }
                active -= 1
-               log.Printf("Upload to %v status code: %v remaining replicas: %v active: %v", status.url, status.statusCode, remaining_replicas, active)
+
        }
 
        return locator, this.Want_replicas, nil