4869: Correctly handle zero-length blocks in Keep client/Keep proxy. Remove
[arvados.git] / sdk / go / keepclient / support.go
1 /* Internal methods to support keepclient.go */
2 package keepclient
3
4 import (
5         "crypto/md5"
6         "errors"
7         "fmt"
8         "git.curoverse.com/arvados.git/sdk/go/streamer"
9         "io"
10         "io/ioutil"
11         "log"
12         "net/http"
13         "os"
14         "strings"
15         "time"
16 )
17
18 type keepDisk struct {
19         Uuid     string `json:"uuid"`
20         Hostname string `json:"service_host"`
21         Port     int    `json:"service_port"`
22         SSL      bool   `json:"service_ssl_flag"`
23         SvcType  string `json:"service_type"`
24 }
25
26 func Md5String(s string) string {
27         return fmt.Sprintf("%x", md5.Sum([]byte(s)))
28 }
29
30 func (this *KeepClient) DiscoverKeepServers() error {
31         if prx := os.Getenv("ARVADOS_KEEP_PROXY"); prx != "" {
32                 sr := map[string]string{"proxy": prx}
33                 this.SetServiceRoots(sr)
34                 this.Using_proxy = true
35                 if this.Client.Timeout == 0 {
36                         this.Client.Timeout = 10 * time.Minute
37                 }
38                 return nil
39         }
40
41         if this.Client.Timeout == 0 {
42                 this.Client.Timeout = 15 * time.Second
43         }
44
45         type svcList struct {
46                 Items []keepDisk `json:"items"`
47         }
48         var m svcList
49
50         err := this.Arvados.Call("GET", "keep_services", "", "accessible", nil, &m)
51
52         if err != nil {
53                 if err := this.Arvados.List("keep_disks", nil, &m); err != nil {
54                         return err
55                 }
56         }
57
58         listed := make(map[string]bool)
59         service_roots := make(map[string]string)
60
61         for _, element := range m.Items {
62                 n := ""
63
64                 if element.SSL {
65                         n = "s"
66                 }
67
68                 // Construct server URL
69                 url := fmt.Sprintf("http%s://%s:%d", n, element.Hostname, element.Port)
70
71                 // Skip duplicates
72                 if !listed[url] {
73                         listed[url] = true
74                         service_roots[element.Uuid] = url
75                 }
76                 if element.SvcType == "proxy" {
77                         this.Using_proxy = true
78                 }
79         }
80
81         this.SetServiceRoots(service_roots)
82
83         return nil
84 }
85
86 type uploadStatus struct {
87         err             error
88         url             string
89         statusCode      int
90         replicas_stored int
91         response        string
92 }
93
94 func (this KeepClient) uploadToKeepServer(host string, hash string, body io.ReadCloser,
95         upload_status chan<- uploadStatus, expectedLength int64, requestId string) {
96
97         var req *http.Request
98         var err error
99         var url = fmt.Sprintf("%s/%s", host, hash)
100         if req, err = http.NewRequest("PUT", url, nil); err != nil {
101                 log.Printf("[%v] Error creating request PUT %v error: %v", requestId, url, err.Error())
102                 upload_status <- uploadStatus{err, url, 0, 0, ""}
103                 body.Close()
104                 return
105         }
106
107         if expectedLength > -1 {
108                 req.ContentLength = expectedLength
109         }
110         if expectedLength == 0 {
111                 defer body.Close()
112         }
113
114         req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.Arvados.ApiToken))
115         req.Header.Add("Content-Type", "application/octet-stream")
116
117         if this.Using_proxy {
118                 req.Header.Add(X_Keep_Desired_Replicas, fmt.Sprint(this.Want_replicas))
119         }
120
121         req.Body = body
122
123         var resp *http.Response
124         if resp, err = this.Client.Do(req); err != nil {
125                 log.Printf("[%v] Upload failed %v error: %v", requestId, url, err.Error())
126                 upload_status <- uploadStatus{err, url, 0, 0, ""}
127                 return
128         }
129
130         rep := 1
131         if xr := resp.Header.Get(X_Keep_Replicas_Stored); xr != "" {
132                 fmt.Sscanf(xr, "%d", &rep)
133         }
134
135         defer resp.Body.Close()
136         defer io.Copy(ioutil.Discard, resp.Body)
137
138         respbody, err2 := ioutil.ReadAll(&io.LimitedReader{resp.Body, 4096})
139         response := strings.TrimSpace(string(respbody))
140         if err2 != nil && err2 != io.EOF {
141                 log.Printf("[%v] Upload %v error: %v response: %v", requestId, url, err2.Error(), response)
142                 upload_status <- uploadStatus{err2, url, resp.StatusCode, rep, response}
143         } else if resp.StatusCode == http.StatusOK {
144                 log.Printf("[%v] Upload %v success", requestId, url)
145                 upload_status <- uploadStatus{nil, url, resp.StatusCode, rep, response}
146         } else {
147                 log.Printf("[%v] Upload %v error: %v response: %v", requestId, url, resp.StatusCode, response)
148                 upload_status <- uploadStatus{errors.New(resp.Status), url, resp.StatusCode, rep, response}
149         }
150 }
151
152 func (this KeepClient) putReplicas(
153         hash string,
154         tr *streamer.AsyncStream,
155         expectedLength int64) (locator string, replicas int, err error) {
156
157         // Take the hash of locator and timestamp in order to identify this
158         // specific transaction in log statements.
159         requestId := fmt.Sprintf("%x", md5.Sum([]byte(locator+time.Now().String())))[0:8]
160
161         // Calculate the ordering for uploading to servers
162         sv := NewRootSorter(this.ServiceRoots(), hash).GetSortedRoots()
163
164         // The next server to try contacting
165         next_server := 0
166
167         // The number of active writers
168         active := 0
169
170         // Used to communicate status from the upload goroutines
171         upload_status := make(chan uploadStatus)
172         defer close(upload_status)
173
174         // Desired number of replicas
175         remaining_replicas := this.Want_replicas
176
177         for remaining_replicas > 0 {
178                 for active < remaining_replicas {
179                         // Start some upload requests
180                         if next_server < len(sv) {
181                                 log.Printf("[%v] Begin upload %s to %s", requestId, hash, sv[next_server])
182                                 go this.uploadToKeepServer(sv[next_server], hash, tr.MakeStreamReader(), upload_status, expectedLength, requestId)
183                                 next_server += 1
184                                 active += 1
185                         } else {
186                                 if active == 0 {
187                                         return locator, (this.Want_replicas - remaining_replicas), InsufficientReplicasError
188                                 } else {
189                                         break
190                                 }
191                         }
192                 }
193                 log.Printf("[%v] Replicas remaining to write: %v active uploads: %v",
194                         requestId, remaining_replicas, active)
195
196                 // Now wait for something to happen.
197                 status := <-upload_status
198                 active -= 1
199
200                 if status.statusCode == 200 {
201                         // good news!
202                         remaining_replicas -= status.replicas_stored
203                         locator = status.response
204                 }
205         }
206
207         return locator, this.Want_replicas, nil
208 }