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