1 /* Internal methods to support keepclient.go */
8 "git.curoverse.com/arvados.git/sdk/go/streamer"
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"`
26 func Md5String(s string) string {
27 return fmt.Sprintf("%x", md5.Sum([]byte(s)))
30 // Set timeouts apply when connecting to keepproxy services (assumed to be over
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
37 // TCP and TLS connection settings
38 this.Client.Transport = &http.Transport{
40 // The maximum time to wait to set up
41 // the initial TCP connection.
42 Timeout: 30 * time.Second,
44 // The TCP keep alive heartbeat
46 KeepAlive: 120 * time.Second,
49 TLSHandshakeTimeout: 10 * time.Second,
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
62 // TCP and TLS connection timeouts
63 this.Client.Transport = &http.Transport{
65 // The maximum time to wait to set up
66 // the initial TCP connection.
67 Timeout: 2 * time.Second,
69 // The TCP keep alive heartbeat
71 KeepAlive: 180 * time.Second,
74 TLSHandshakeTimeout: 4 * time.Second,
79 func (this *KeepClient) DiscoverKeepServers() error {
81 Items []keepDisk `json:"items"`
85 err := this.Arvados.Call("GET", "keep_services", "", "accessible", nil, &m)
88 if err := this.Arvados.List("keep_disks", nil, &m); err != nil {
93 listed := make(map[string]bool)
94 localRoots := make(map[string]string)
95 gatewayRoots := make(map[string]string)
97 for _, service := range m.Items {
102 url := fmt.Sprintf("%s://%s:%d", scheme, service.Hostname, service.Port)
110 switch service.SvcType {
112 localRoots[service.Uuid] = url
114 localRoots[service.Uuid] = url
115 this.Using_proxy = true
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
125 if this.Using_proxy {
126 this.setClientSettingsProxy()
128 this.setClientSettingsStore()
131 this.SetServiceRoots(localRoots, gatewayRoots)
135 type uploadStatus struct {
143 func (this KeepClient) uploadToKeepServer(host string, hash string, body io.ReadCloser,
144 upload_status chan<- uploadStatus, expectedLength int64, requestId string) {
146 var req *http.Request
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, ""}
156 req.ContentLength = expectedLength
157 if expectedLength > 0 {
158 // http.Client.Do will close the body ReadCloser when it is
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
169 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.Arvados.ApiToken))
170 req.Header.Add("Content-Type", "application/octet-stream")
172 if this.Using_proxy {
173 req.Header.Add(X_Keep_Desired_Replicas, fmt.Sprint(this.Want_replicas))
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, ""}
184 if xr := resp.Header.Get(X_Keep_Replicas_Stored); xr != "" {
185 fmt.Sscanf(xr, "%d", &rep)
188 defer resp.Body.Close()
189 defer io.Copy(ioutil.Discard, resp.Body)
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}
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}
205 func (this KeepClient) putReplicas(
207 tr *streamer.AsyncStream,
208 expectedLength int64) (locator string, replicas int, err error) {
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]
214 // Calculate the ordering for uploading to servers
215 sv := NewRootSorter(this.LocalRoots(), hash).GetSortedRoots()
217 // The next server to try contacting
220 // The number of active writers
223 // Used to communicate status from the upload goroutines
224 upload_status := make(chan uploadStatus)
225 defer close(upload_status)
227 // Desired number of replicas
228 remaining_replicas := this.Want_replicas
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)
240 return locator, (this.Want_replicas - remaining_replicas), InsufficientReplicasError
246 log.Printf("[%v] Replicas remaining to write: %v active uploads: %v",
247 requestId, remaining_replicas, active)
249 // Now wait for something to happen.
250 status := <-upload_status
253 if status.statusCode == 200 {
255 remaining_replicas -= status.replicas_stored
256 locator = status.response
260 return locator, this.Want_replicas, nil