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 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 if this.Client.Timeout == 0 {
36 this.Client.Timeout = 10 * time.Minute
41 if this.Client.Timeout == 0 {
42 this.Client.Timeout = 15 * time.Second
46 Items []keepDisk `json:"items"`
50 err := this.Arvados.Call("GET", "keep_services", "", "accessible", nil, &m)
53 if err := this.Arvados.List("keep_disks", nil, &m); err != nil {
58 listed := make(map[string]bool)
59 service_roots := make(map[string]string)
61 for _, element := range m.Items {
68 // Construct server URL
69 url := fmt.Sprintf("http%s://%s:%d", n, element.Hostname, element.Port)
74 service_roots[element.Uuid] = url
76 if element.SvcType == "proxy" {
77 this.Using_proxy = true
81 this.SetServiceRoots(service_roots)
86 type uploadStatus struct {
94 func (this KeepClient) uploadToKeepServer(host string, hash string, body io.ReadCloser,
95 upload_status chan<- uploadStatus, expectedLength int64, requestId string) {
99 var url = fmt.Sprintf("%s/%s", host, hash)
100 if req, err = http.NewRequest("PUT", url, nil); err != nil {
101 log.Printf("[%v] Error creating request PUT %v error: %v", requestId, url, err.Error())
102 upload_status <- uploadStatus{err, url, 0, 0, ""}
107 req.ContentLength = expectedLength
108 if expectedLength > 0 {
109 // http.Client.Do will close the body ReadCloser when it is
113 // "For client requests, a value of 0 means unknown if Body is
114 // not nil." In this case we do want the body to be empty, so
115 // don't set req.Body. However, we still need to close the
120 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.Arvados.ApiToken))
121 req.Header.Add("Content-Type", "application/octet-stream")
123 if this.Using_proxy {
124 req.Header.Add(X_Keep_Desired_Replicas, fmt.Sprint(this.Want_replicas))
127 var resp *http.Response
128 if resp, err = this.Client.Do(req); err != nil {
129 log.Printf("[%v] Upload failed %v error: %v", requestId, url, err.Error())
130 upload_status <- uploadStatus{err, url, 0, 0, ""}
135 if xr := resp.Header.Get(X_Keep_Replicas_Stored); xr != "" {
136 fmt.Sscanf(xr, "%d", &rep)
139 defer resp.Body.Close()
140 defer io.Copy(ioutil.Discard, resp.Body)
142 respbody, err2 := ioutil.ReadAll(&io.LimitedReader{resp.Body, 4096})
143 response := strings.TrimSpace(string(respbody))
144 if err2 != nil && err2 != io.EOF {
145 log.Printf("[%v] Upload %v error: %v response: %v", requestId, url, err2.Error(), response)
146 upload_status <- uploadStatus{err2, url, resp.StatusCode, rep, response}
147 } else if resp.StatusCode == http.StatusOK {
148 log.Printf("[%v] Upload %v success", requestId, url)
149 upload_status <- uploadStatus{nil, url, resp.StatusCode, rep, response}
151 log.Printf("[%v] Upload %v error: %v response: %v", requestId, url, resp.StatusCode, response)
152 upload_status <- uploadStatus{errors.New(resp.Status), url, resp.StatusCode, rep, response}
156 func (this KeepClient) putReplicas(
158 tr *streamer.AsyncStream,
159 expectedLength int64) (locator string, replicas int, err error) {
161 // Take the hash of locator and timestamp in order to identify this
162 // specific transaction in log statements.
163 requestId := fmt.Sprintf("%x", md5.Sum([]byte(locator+time.Now().String())))[0:8]
165 // Calculate the ordering for uploading to servers
166 sv := NewRootSorter(this.ServiceRoots(), hash).GetSortedRoots()
168 // The next server to try contacting
171 // The number of active writers
174 // Used to communicate status from the upload goroutines
175 upload_status := make(chan uploadStatus)
176 defer close(upload_status)
178 // Desired number of replicas
179 remaining_replicas := this.Want_replicas
181 for remaining_replicas > 0 {
182 for active < remaining_replicas {
183 // Start some upload requests
184 if next_server < len(sv) {
185 log.Printf("[%v] Begin upload %s to %s", requestId, hash, sv[next_server])
186 go this.uploadToKeepServer(sv[next_server], hash, tr.MakeStreamReader(), upload_status, expectedLength, requestId)
191 return locator, (this.Want_replicas - remaining_replicas), InsufficientReplicasError
197 log.Printf("[%v] Replicas remaining to write: %v active uploads: %v",
198 requestId, remaining_replicas, active)
200 // Now wait for something to happen.
201 status := <-upload_status
204 if status.statusCode == 200 {
206 remaining_replicas -= status.replicas_stored
207 locator = status.response
211 return locator, this.Want_replicas, nil