Merge branch 'master' into 3198-writable-fuse
[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"
13         "net/http"
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 // Set timeouts apply when connecting to keepproxy services (assumed to be over
31 // the Internet).
32 func (this *KeepClient) setClientSettingsProxy() {
33         if this.Client.Timeout == 0 {
34                 // Maximum time to wait for a complete response
35                 this.Client.Timeout = 300 * time.Second
36
37                 // TCP and TLS connection settings
38                 this.Client.Transport = &http.Transport{
39                         Dial: (&net.Dialer{
40                                 // The maximum time to wait to set up
41                                 // the initial TCP connection.
42                                 Timeout: 30 * time.Second,
43
44                                 // The TCP keep alive heartbeat
45                                 // interval.
46                                 KeepAlive: 120 * time.Second,
47                         }).Dial,
48
49                         TLSHandshakeTimeout: 10 * time.Second,
50                 }
51         }
52
53 }
54
55 // Set timeouts apply when connecting to keepstore services directly (assumed
56 // to be on the local network).
57 func (this *KeepClient) setClientSettingsStore() {
58         if this.Client.Timeout == 0 {
59                 // Maximum time to wait for a complete response
60                 this.Client.Timeout = 20 * time.Second
61
62                 // TCP and TLS connection timeouts
63                 this.Client.Transport = &http.Transport{
64                         Dial: (&net.Dialer{
65                                 // The maximum time to wait to set up
66                                 // the initial TCP connection.
67                                 Timeout: 2 * time.Second,
68
69                                 // The TCP keep alive heartbeat
70                                 // interval.
71                                 KeepAlive: 180 * time.Second,
72                         }).Dial,
73
74                         TLSHandshakeTimeout: 4 * time.Second,
75                 }
76         }
77 }
78
79 func (this *KeepClient) DiscoverKeepServers() error {
80         type svcList struct {
81                 Items []keepDisk `json:"items"`
82         }
83         var m svcList
84
85         err := this.Arvados.Call("GET", "keep_services", "", "accessible", nil, &m)
86
87         if err != nil {
88                 if err := this.Arvados.List("keep_disks", nil, &m); err != nil {
89                         return err
90                 }
91         }
92
93         listed := make(map[string]bool)
94         localRoots := make(map[string]string)
95         gatewayRoots := make(map[string]string)
96
97         for _, service := range m.Items {
98                 scheme := "http"
99                 if service.SSL {
100                         scheme = "https"
101                 }
102                 url := fmt.Sprintf("%s://%s:%d", scheme, service.Hostname, service.Port)
103
104                 // Skip duplicates
105                 if listed[url] {
106                         continue
107                 }
108                 listed[url] = true
109
110                 switch service.SvcType {
111                 case "disk":
112                         localRoots[service.Uuid] = url
113                 case "proxy":
114                         localRoots[service.Uuid] = url
115                         this.Using_proxy = true
116                 }
117                 // Gateway services are only used when specified by
118                 // UUID, so there's nothing to gain by filtering them
119                 // by service type. Including all accessible services
120                 // (gateway and otherwise) merely accommodates more
121                 // service configurations.
122                 gatewayRoots[service.Uuid] = url
123         }
124
125         if this.Using_proxy {
126                 this.setClientSettingsProxy()
127         } else {
128                 this.setClientSettingsStore()
129         }
130
131         this.SetServiceRoots(localRoots, gatewayRoots)
132         return nil
133 }
134
135 type uploadStatus struct {
136         err             error
137         url             string
138         statusCode      int
139         replicas_stored int
140         response        string
141 }
142
143 func (this KeepClient) uploadToKeepServer(host string, hash string, body io.ReadCloser,
144         upload_status chan<- uploadStatus, expectedLength int64, requestId string) {
145
146         var req *http.Request
147         var err error
148         var url = fmt.Sprintf("%s/%s", host, hash)
149         if req, err = http.NewRequest("PUT", url, nil); err != nil {
150                 log.Printf("[%v] Error creating request PUT %v error: %v", requestId, url, err.Error())
151                 upload_status <- uploadStatus{err, url, 0, 0, ""}
152                 body.Close()
153                 return
154         }
155
156         req.ContentLength = expectedLength
157         if expectedLength > 0 {
158                 // http.Client.Do will close the body ReadCloser when it is
159                 // done with it.
160                 req.Body = body
161         } else {
162                 // "For client requests, a value of 0 means unknown if Body is
163                 // not nil."  In this case we do want the body to be empty, so
164                 // don't set req.Body.  However, we still need to close the
165                 // body ReadCloser.
166                 body.Close()
167         }
168
169         req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.Arvados.ApiToken))
170         req.Header.Add("Content-Type", "application/octet-stream")
171
172         if this.Using_proxy {
173                 req.Header.Add(X_Keep_Desired_Replicas, fmt.Sprint(this.Want_replicas))
174         }
175
176         var resp *http.Response
177         if resp, err = this.Client.Do(req); err != nil {
178                 log.Printf("[%v] Upload failed %v error: %v", requestId, url, err.Error())
179                 upload_status <- uploadStatus{err, url, 0, 0, ""}
180                 return
181         }
182
183         rep := 1
184         if xr := resp.Header.Get(X_Keep_Replicas_Stored); xr != "" {
185                 fmt.Sscanf(xr, "%d", &rep)
186         }
187
188         defer resp.Body.Close()
189         defer io.Copy(ioutil.Discard, resp.Body)
190
191         respbody, err2 := ioutil.ReadAll(&io.LimitedReader{resp.Body, 4096})
192         response := strings.TrimSpace(string(respbody))
193         if err2 != nil && err2 != io.EOF {
194                 log.Printf("[%v] Upload %v error: %v response: %v", requestId, url, err2.Error(), response)
195                 upload_status <- uploadStatus{err2, url, resp.StatusCode, rep, response}
196         } else if resp.StatusCode == http.StatusOK {
197                 log.Printf("[%v] Upload %v success", requestId, url)
198                 upload_status <- uploadStatus{nil, url, resp.StatusCode, rep, response}
199         } else {
200                 log.Printf("[%v] Upload %v error: %v response: %v", requestId, url, resp.StatusCode, response)
201                 upload_status <- uploadStatus{errors.New(resp.Status), url, resp.StatusCode, rep, response}
202         }
203 }
204
205 func (this KeepClient) putReplicas(
206         hash string,
207         tr *streamer.AsyncStream,
208         expectedLength int64) (locator string, replicas int, err error) {
209
210         // Take the hash of locator and timestamp in order to identify this
211         // specific transaction in log statements.
212         requestId := fmt.Sprintf("%x", md5.Sum([]byte(locator+time.Now().String())))[0:8]
213
214         // Calculate the ordering for uploading to servers
215         sv := NewRootSorter(this.LocalRoots(), hash).GetSortedRoots()
216
217         // The next server to try contacting
218         next_server := 0
219
220         // The number of active writers
221         active := 0
222
223         // Used to communicate status from the upload goroutines
224         upload_status := make(chan uploadStatus)
225         defer close(upload_status)
226
227         // Desired number of replicas
228         remaining_replicas := this.Want_replicas
229
230         for remaining_replicas > 0 {
231                 for active < remaining_replicas {
232                         // Start some upload requests
233                         if next_server < len(sv) {
234                                 log.Printf("[%v] Begin upload %s to %s", requestId, hash, sv[next_server])
235                                 go this.uploadToKeepServer(sv[next_server], hash, tr.MakeStreamReader(), upload_status, expectedLength, requestId)
236                                 next_server += 1
237                                 active += 1
238                         } else {
239                                 if active == 0 {
240                                         return locator, (this.Want_replicas - remaining_replicas), InsufficientReplicasError
241                                 } else {
242                                         break
243                                 }
244                         }
245                 }
246                 log.Printf("[%v] Replicas remaining to write: %v active uploads: %v",
247                         requestId, remaining_replicas, active)
248
249                 // Now wait for something to happen.
250                 status := <-upload_status
251                 active -= 1
252
253                 if status.statusCode == 200 {
254                         // good news!
255                         remaining_replicas -= status.replicas_stored
256                         locator = status.response
257                 }
258         }
259
260         return locator, this.Want_replicas, nil
261 }