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