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