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"`
24 ReadOnly bool `json:"read_only"`
27 func Md5String(s string) string {
28 return fmt.Sprintf("%x", md5.Sum([]byte(s)))
31 // Set timeouts apply when connecting to keepproxy services (assumed to be over
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
38 // TCP and TLS connection settings
39 this.Client.Transport = &http.Transport{
41 // The maximum time to wait to set up
42 // the initial TCP connection.
43 Timeout: 30 * time.Second,
45 // The TCP keep alive heartbeat
47 KeepAlive: 120 * time.Second,
50 TLSHandshakeTimeout: 10 * time.Second,
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
63 // TCP and TLS connection timeouts
64 this.Client.Transport = &http.Transport{
66 // The maximum time to wait to set up
67 // the initial TCP connection.
68 Timeout: 2 * time.Second,
70 // The TCP keep alive heartbeat
72 KeepAlive: 180 * time.Second,
75 TLSHandshakeTimeout: 4 * time.Second,
80 func (this *KeepClient) DiscoverKeepServers() error {
82 Items []keepDisk `json:"items"`
86 err := this.Arvados.Call("GET", "keep_services", "", "accessible", nil, &m)
89 if err := this.Arvados.List("keep_disks", nil, &m); err != nil {
94 listed := make(map[string]bool)
95 localRoots := make(map[string]string)
96 gatewayRoots := make(map[string]string)
97 writableLocalRoots := make(map[string]string)
99 for _, service := range m.Items {
104 url := fmt.Sprintf("%s://%s:%d", scheme, service.Hostname, service.Port)
112 switch service.SvcType {
114 localRoots[service.Uuid] = url
116 localRoots[service.Uuid] = url
117 this.Using_proxy = true
120 if service.ReadOnly == false {
121 writableLocalRoots[service.Uuid] = url
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
132 if this.Using_proxy {
133 this.setClientSettingsProxy()
135 this.setClientSettingsStore()
138 this.SetServiceRoots(localRoots, writableLocalRoots, gatewayRoots)
142 type uploadStatus struct {
150 func (this KeepClient) uploadToKeepServer(host string, hash string, body io.ReadCloser,
151 upload_status chan<- uploadStatus, expectedLength int64, requestId string) {
153 var req *http.Request
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, ""}
163 req.ContentLength = expectedLength
164 if expectedLength > 0 {
165 // http.Client.Do will close the body ReadCloser when it is
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
176 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.Arvados.ApiToken))
177 req.Header.Add("Content-Type", "application/octet-stream")
179 if this.Using_proxy {
180 req.Header.Add(X_Keep_Desired_Replicas, fmt.Sprint(this.Want_replicas))
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, ""}
191 if xr := resp.Header.Get(X_Keep_Replicas_Stored); xr != "" {
192 fmt.Sscanf(xr, "%d", &rep)
195 defer resp.Body.Close()
196 defer io.Copy(ioutil.Discard, resp.Body)
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}
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}
212 func (this KeepClient) putReplicas(
214 tr *streamer.AsyncStream,
215 expectedLength int64) (locator string, replicas int, err error) {
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]
221 // Calculate the ordering for uploading to servers
222 sv := NewRootSorter(this.WritableLocalRoots(), hash).GetSortedRoots()
224 // The next server to try contacting
227 // The number of active writers
230 // Used to communicate status from the upload goroutines
231 upload_status := make(chan uploadStatus)
232 defer close(upload_status)
234 // Desired number of replicas
235 remaining_replicas := this.Want_replicas
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)
247 return locator, (this.Want_replicas - remaining_replicas), InsufficientReplicasError
253 log.Printf("[%v] Replicas remaining to write: %v active uploads: %v",
254 requestId, remaining_replicas, active)
256 // Now wait for something to happen.
257 status := <-upload_status
260 if status.statusCode == 200 {
262 remaining_replicas -= status.replicas_stored
263 locator = status.response
267 return locator, this.Want_replicas, nil