5824: Turn off debug printfs unless enabled by calling program.
authorTom Clegg <tom@curoverse.com>
Tue, 3 Nov 2015 19:06:52 +0000 (14:06 -0500)
committerTom Clegg <tom@curoverse.com>
Wed, 4 Nov 2015 04:05:37 +0000 (23:05 -0500)
sdk/go/keepclient/keepclient_test.go
sdk/go/keepclient/support.go
services/keepproxy/keepproxy.go

index df4638619f488bc86dd3626cf2579a60fda62bed..4615ebc92ef2f8b59bc70ac1d8320833faee8a41 100644 (file)
@@ -143,10 +143,9 @@ func (s *StandaloneSuite) TestUploadToStubKeepServer(c *C) {
                make(chan string)}
 
        UploadToStubHelper(c, st,
-               func(kc *KeepClient, url string, reader io.ReadCloser,
-                       writer io.WriteCloser, upload_status chan uploadStatus) {
+               func(kc *KeepClient, url string, reader io.ReadCloser, writer io.WriteCloser, upload_status chan uploadStatus) {
 
-                       go kc.uploadToKeepServer(url, st.expectPath, reader, upload_status, int64(len("foo")), "TestUploadToStubKeepServer")
+                       go kc.uploadToKeepServer(url, st.expectPath, reader, upload_status, int64(len("foo")), 0)
 
                        writer.Write([]byte("foo"))
                        writer.Close()
@@ -178,7 +177,7 @@ func (s *StandaloneSuite) TestUploadToStubKeepServerBufferReader(c *C) {
 
                        br1 := tr.MakeStreamReader()
 
-                       go kc.uploadToKeepServer(url, st.expectPath, br1, upload_status, 3, "TestUploadToStubKeepServerBufferReader")
+                       go kc.uploadToKeepServer(url, st.expectPath, br1, upload_status, 3, 0)
 
                        writer.Write([]byte("foo"))
                        writer.Close()
@@ -238,7 +237,7 @@ func (s *StandaloneSuite) TestFailedUploadToStubKeepServer(c *C) {
                func(kc *KeepClient, url string, reader io.ReadCloser,
                        writer io.WriteCloser, upload_status chan uploadStatus) {
 
-                       go kc.uploadToKeepServer(url, hash, reader, upload_status, 3, "TestFailedUploadToStubKeepServer")
+                       go kc.uploadToKeepServer(url, hash, reader, upload_status, 3, 0)
 
                        writer.Write([]byte("foo"))
                        writer.Close()
index 4a210243defcd0bd33ea130dd17c96b2c151534f..8414afab1eace8b00125ca88f4f37279b95fc558 100644 (file)
@@ -9,12 +9,18 @@ import (
        "io"
        "io/ioutil"
        "log"
+       "math/rand"
        "net"
        "net/http"
        "strings"
        "time"
 )
 
+// Function used to emit debug messages. The easiest way to enable
+// keepclient debug messages in your application is to assign
+// log.Printf to DebugPrintf.
+var DebugPrintf = func(string, ...interface{}) {}
+
 type keepService struct {
        Uuid     string `json:"uuid"`
        Hostname string `json:"service_host"`
@@ -170,13 +176,13 @@ type uploadStatus struct {
 }
 
 func (this *KeepClient) uploadToKeepServer(host string, hash string, body io.ReadCloser,
-       upload_status chan<- uploadStatus, expectedLength int64, requestId string) {
+       upload_status chan<- uploadStatus, expectedLength int64, requestID int32) {
 
        var req *http.Request
        var err error
        var url = fmt.Sprintf("%s/%s", host, hash)
        if req, err = http.NewRequest("PUT", url, nil); err != nil {
-               log.Printf("[%v] Error creating request PUT %v error: %v", requestId, url, err.Error())
+               log.Printf("[%08x] Error creating request PUT %v error: %v", requestID, url, err.Error())
                upload_status <- uploadStatus{err, url, 0, 0, ""}
                body.Close()
                return
@@ -201,7 +207,7 @@ func (this *KeepClient) uploadToKeepServer(host string, hash string, body io.Rea
 
        var resp *http.Response
        if resp, err = this.Client.Do(req); err != nil {
-               log.Printf("[%v] Upload failed %v error: %v", requestId, url, err.Error())
+               log.Printf("[%08x] Upload failed %v error: %v", requestID, url, err.Error())
                upload_status <- uploadStatus{err, url, 0, 0, ""}
                return
        }
@@ -217,13 +223,13 @@ func (this *KeepClient) uploadToKeepServer(host string, hash string, body io.Rea
        respbody, err2 := ioutil.ReadAll(&io.LimitedReader{R: resp.Body, N: 4096})
        response := strings.TrimSpace(string(respbody))
        if err2 != nil && err2 != io.EOF {
-               log.Printf("[%v] Upload %v error: %v response: %v", requestId, url, err2.Error(), response)
+               log.Printf("[%08x] Upload %v error: %v response: %v", requestID, url, err2.Error(), response)
                upload_status <- uploadStatus{err2, url, resp.StatusCode, rep, response}
        } else if resp.StatusCode == http.StatusOK {
-               log.Printf("[%v] Upload %v success", requestId, url)
+               log.Printf("[%08x] Upload %v success", requestID, url)
                upload_status <- uploadStatus{nil, url, resp.StatusCode, rep, response}
        } else {
-               log.Printf("[%v] Upload %v error: %v response: %v", requestId, url, resp.StatusCode, response)
+               log.Printf("[%08x] Upload %v error: %v response: %v", requestID, url, resp.StatusCode, response)
                upload_status <- uploadStatus{errors.New(resp.Status), url, resp.StatusCode, rep, response}
        }
 }
@@ -233,9 +239,9 @@ 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.
-       requestId := fmt.Sprintf("%x", md5.Sum([]byte(hash+time.Now().String())))[0:8]
+       // Generate an arbitrary ID to identify this specific
+       // transaction in debug logs.
+       requestID := rand.Int31()
 
        // Calculate the ordering for uploading to servers
        sv := NewRootSorter(this.WritableLocalRoots(), hash).GetSortedRoots()
@@ -280,8 +286,8 @@ func (this *KeepClient) putReplicas(
                        for active*replicasPerThread < remaining_replicas {
                                // Start some upload requests
                                if next_server < len(sv) {
-                                       log.Printf("[%v] Begin upload %s to %s", requestId, hash, sv[next_server])
-                                       go this.uploadToKeepServer(sv[next_server], hash, tr.MakeStreamReader(), upload_status, expectedLength, requestId)
+                                       log.Printf("[%08x] Begin upload %s to %s", requestID, hash, sv[next_server])
+                                       go this.uploadToKeepServer(sv[next_server], hash, tr.MakeStreamReader(), upload_status, expectedLength, requestID)
                                        next_server += 1
                                        active += 1
                                } else {
@@ -292,8 +298,8 @@ func (this *KeepClient) putReplicas(
                                        }
                                }
                        }
-                       log.Printf("[%v] Replicas remaining to write: %v active uploads: %v",
-                               requestId, remaining_replicas, active)
+                       log.Printf("[%08x] Replicas remaining to write: %v active uploads: %v",
+                               requestID, remaining_replicas, active)
 
                        // Now wait for something to happen.
                        if active > 0 {
index 1a1189658d7ba1473aa33036cce60276932cb9b8..79ed51eb0e00f57eb38d4a31251a87c2bc5e866c 100644 (file)
@@ -84,6 +84,9 @@ func main() {
                log.Fatalf("Error setting up arvados client %s", err.Error())
        }
 
+       if os.Getenv("ARVADOS_DEBUG") != "" {
+               keepclient.DebugPrintf = log.Printf
+       }
        kc, err := keepclient.MakeKeepClient(&arv)
        if err != nil {
                log.Fatalf("Error setting up keep client %s", err.Error())