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
39 Items []keepDisk `json:"items"`
43 err := this.Arvados.Call("GET", "keep_services", "", "accessible", nil, &m)
46 if err := this.Arvados.List("keep_disks", nil, &m); err != nil {
51 listed := make(map[string]bool)
52 service_roots := make(map[string]string)
54 for _, element := range m.Items {
61 // Construct server URL
62 url := fmt.Sprintf("http%s://%s:%d", n, element.Hostname, element.Port)
67 service_roots[element.Uuid] = url
69 if element.SvcType == "proxy" {
70 this.Using_proxy = true
74 this.SetServiceRoots(service_roots)
79 type uploadStatus struct {
87 func (this KeepClient) uploadToKeepServer(host string, hash string, body io.ReadCloser,
88 upload_status chan<- uploadStatus, expectedLength int64, tag string) {
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, ""}
100 if expectedLength > 0 {
101 req.ContentLength = expectedLength
104 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.Arvados.ApiToken))
105 req.Header.Add("Content-Type", "application/octet-stream")
107 if this.Using_proxy {
108 req.Header.Add(X_Keep_Desired_Replicas, fmt.Sprint(this.Want_replicas))
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, ""}
121 if xr := resp.Header.Get(X_Keep_Replicas_Stored); xr != "" {
122 fmt.Sscanf(xr, "%d", &rep)
125 defer resp.Body.Close()
126 defer io.Copy(ioutil.Discard, resp.Body)
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}
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}
142 func (this KeepClient) putReplicas(
144 tr *streamer.AsyncStream,
145 expectedLength int64) (locator string, replicas int, err error) {
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]
151 // Calculate the ordering for uploading to servers
152 sv := NewRootSorter(this.ServiceRoots(), hash).GetSortedRoots()
154 // The next server to try contacting
157 // The number of active writers
160 // Used to communicate status from the upload goroutines
161 upload_status := make(chan uploadStatus)
162 defer close(upload_status)
164 // Desired number of replicas
165 remaining_replicas := this.Want_replicas
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)
177 return locator, (this.Want_replicas - remaining_replicas), InsufficientReplicasError
183 log.Printf("[%v] Replicas remaining to write: %v active uploads: %v",
184 tag, remaining_replicas, active)
186 // Now wait for something to happen.
187 status := <-upload_status
189 if status.statusCode == 200 {
191 remaining_replicas -= status.replicas_stored
192 locator = status.response
196 return locator, this.Want_replicas, nil