Merge branch '2751-python-sdk-keep-proxy-support' closes #2751
[arvados.git] / sdk / go / src / arvados.org / keepclient / support.go
1 /* Internal methods to support keepclient.go */
2 package keepclient
3
4 import (
5         "arvados.org/streamer"
6         "encoding/json"
7         "errors"
8         "fmt"
9         "io"
10         "io/ioutil"
11         "log"
12         "net/http"
13         "os"
14         "strconv"
15         "strings"
16 )
17
18 type keepDisk struct {
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 }
24
25 func (this *KeepClient) DiscoverKeepServers() error {
26         if prx := os.Getenv("ARVADOS_KEEP_PROXY"); prx != "" {
27                 this.SetServiceRoots([]string{prx})
28                 this.Using_proxy = true
29                 return nil
30         }
31
32         // Construct request of keep disk list
33         var req *http.Request
34         var err error
35
36         if req, err = http.NewRequest("GET", fmt.Sprintf("https://%s/arvados/v1/keep_services/accessible?format=json", this.ApiServer), nil); err != nil {
37                 return err
38         }
39
40         // Add api token header
41         req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.ApiToken))
42         if this.External {
43                 req.Header.Add("X-External-Client", "1")
44         }
45
46         // Make the request
47         var resp *http.Response
48         if resp, err = this.Client.Do(req); err != nil {
49                 return err
50         }
51
52         if resp.StatusCode != http.StatusOK {
53                 // fall back on keep disks
54                 if req, err = http.NewRequest("GET", fmt.Sprintf("https://%s/arvados/v1/keep_disks", this.ApiServer), nil); err != nil {
55                         return err
56                 }
57                 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.ApiToken))
58                 if resp, err = this.Client.Do(req); err != nil {
59                         return err
60                 }
61                 if resp.StatusCode != http.StatusOK {
62                         return errors.New(resp.Status)
63                 }
64         }
65
66         type svcList struct {
67                 Items []keepDisk `json:"items"`
68         }
69
70         // Decode json reply
71         dec := json.NewDecoder(resp.Body)
72         var m svcList
73         if err := dec.Decode(&m); err != nil {
74                 return err
75         }
76
77         listed := make(map[string]bool)
78         service_roots := make([]string, 0, len(m.Items))
79
80         for _, element := range m.Items {
81                 n := ""
82
83                 if element.SSL {
84                         n = "s"
85                 }
86
87                 // Construct server URL
88                 url := fmt.Sprintf("http%s://%s:%d", n, element.Hostname, element.Port)
89
90                 // Skip duplicates
91                 if !listed[url] {
92                         listed[url] = true
93                         service_roots = append(service_roots, url)
94                 }
95                 if element.SvcType == "proxy" {
96                         this.Using_proxy = true
97                 }
98         }
99
100         this.SetServiceRoots(service_roots)
101
102         return nil
103 }
104
105 func (this KeepClient) shuffledServiceRoots(hash string) (pseq []string) {
106         // Build an ordering with which to query the Keep servers based on the
107         // contents of the hash.  "hash" is a hex-encoded number at least 8
108         // digits (32 bits) long
109
110         // seed used to calculate the next keep server from 'pool' to be added
111         // to 'pseq'
112         seed := hash
113
114         // Keep servers still to be added to the ordering
115         service_roots := this.ServiceRoots()
116         pool := make([]string, len(service_roots))
117         copy(pool, service_roots)
118
119         // output probe sequence
120         pseq = make([]string, 0, len(service_roots))
121
122         // iterate while there are servers left to be assigned
123         for len(pool) > 0 {
124
125                 if len(seed) < 8 {
126                         // ran out of digits in the seed
127                         if len(pseq) < (len(hash) / 4) {
128                                 // the number of servers added to the probe
129                                 // sequence is less than the number of 4-digit
130                                 // slices in 'hash' so refill the seed with the
131                                 // last 4 digits.
132                                 seed = hash[len(hash)-4:]
133                         }
134                         seed += hash
135                 }
136
137                 // Take the next 8 digits (32 bytes) and interpret as an integer,
138                 // then modulus with the size of the remaining pool to get the next
139                 // selected server.
140                 probe, _ := strconv.ParseUint(seed[0:8], 16, 32)
141                 probe %= uint64(len(pool))
142
143                 // Append the selected server to the probe sequence and remove it
144                 // from the pool.
145                 pseq = append(pseq, pool[probe])
146                 pool = append(pool[:probe], pool[probe+1:]...)
147
148                 // Remove the digits just used from the seed
149                 seed = seed[8:]
150         }
151         return pseq
152 }
153
154 type uploadStatus struct {
155         err             error
156         url             string
157         statusCode      int
158         replicas_stored int
159         response        string
160 }
161
162 func (this KeepClient) uploadToKeepServer(host string, hash string, body io.ReadCloser,
163         upload_status chan<- uploadStatus, expectedLength int64) {
164
165         log.Printf("Uploading %s to %s", hash, host)
166
167         var req *http.Request
168         var err error
169         var url = fmt.Sprintf("%s/%s", host, hash)
170         if req, err = http.NewRequest("PUT", url, nil); err != nil {
171                 upload_status <- uploadStatus{err, url, 0, 0, ""}
172                 body.Close()
173                 return
174         }
175
176         if expectedLength > 0 {
177                 req.ContentLength = expectedLength
178         }
179
180         req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.ApiToken))
181         req.Header.Add("Content-Type", "application/octet-stream")
182
183         if this.Using_proxy {
184                 req.Header.Add(X_Keep_Desired_Replicas, fmt.Sprint(this.Want_replicas))
185         }
186
187         req.Body = body
188
189         var resp *http.Response
190         if resp, err = this.Client.Do(req); err != nil {
191                 upload_status <- uploadStatus{err, url, 0, 0, ""}
192                 body.Close()
193                 return
194         }
195
196         rep := 1
197         if xr := resp.Header.Get(X_Keep_Replicas_Stored); xr != "" {
198                 fmt.Sscanf(xr, "%d", &rep)
199         }
200
201         respbody, err2 := ioutil.ReadAll(&io.LimitedReader{resp.Body, 4096})
202         if err2 != nil && err2 != io.EOF {
203                 upload_status <- uploadStatus{err2, url, resp.StatusCode, rep, string(respbody)}
204                 return
205         }
206
207         locator := strings.TrimSpace(string(respbody))
208
209         if resp.StatusCode == http.StatusOK {
210                 upload_status <- uploadStatus{nil, url, resp.StatusCode, rep, locator}
211         } else {
212                 upload_status <- uploadStatus{errors.New(resp.Status), url, resp.StatusCode, rep, locator}
213         }
214 }
215
216 func (this KeepClient) putReplicas(
217         hash string,
218         tr *streamer.AsyncStream,
219         expectedLength int64) (locator string, replicas int, err error) {
220
221         // Calculate the ordering for uploading to servers
222         sv := this.shuffledServiceRoots(hash)
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
236         remaining_replicas := this.Want_replicas
237
238         for remaining_replicas > 0 {
239                 for active < remaining_replicas {
240                         // Start some upload requests
241                         if next_server < len(sv) {
242                                 go this.uploadToKeepServer(sv[next_server], hash, tr.MakeStreamReader(), upload_status, expectedLength)
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
254                 // Now wait for something to happen.
255                 status := <-upload_status
256                 if status.statusCode == 200 {
257                         // good news!
258                         remaining_replicas -= status.replicas_stored
259                         locator = status.response
260                 } else {
261                         // writing to keep server failed for some reason
262                         log.Printf("Keep server put to %v failed with '%v'",
263                                 status.url, status.err)
264                 }
265                 active -= 1
266                 log.Printf("Upload to %v status code: %v remaining replicas: %v active: %v", status.url, status.statusCode, remaining_replicas, active)
267         }
268
269         return locator, this.Want_replicas, nil
270 }