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 if expectedLength > -1 {
108 req.ContentLength = expectedLength
110 if expectedLength == 0 {
114 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.Arvados.ApiToken))
115 req.Header.Add("Content-Type", "application/octet-stream")
117 if this.Using_proxy {
118 req.Header.Add(X_Keep_Desired_Replicas, fmt.Sprint(this.Want_replicas))
123 var resp *http.Response
124 if resp, err = this.Client.Do(req); err != nil {
125 log.Printf("[%v] Upload failed %v error: %v", requestId, url, err.Error())
126 upload_status <- uploadStatus{err, url, 0, 0, ""}
131 if xr := resp.Header.Get(X_Keep_Replicas_Stored); xr != "" {
132 fmt.Sscanf(xr, "%d", &rep)
135 defer resp.Body.Close()
136 defer io.Copy(ioutil.Discard, resp.Body)
138 respbody, err2 := ioutil.ReadAll(&io.LimitedReader{resp.Body, 4096})
139 response := strings.TrimSpace(string(respbody))
140 if err2 != nil && err2 != io.EOF {
141 log.Printf("[%v] Upload %v error: %v response: %v", requestId, url, err2.Error(), response)
142 upload_status <- uploadStatus{err2, url, resp.StatusCode, rep, response}
143 } else if resp.StatusCode == http.StatusOK {
144 log.Printf("[%v] Upload %v success", requestId, url)
145 upload_status <- uploadStatus{nil, url, resp.StatusCode, rep, response}
147 log.Printf("[%v] Upload %v error: %v response: %v", requestId, url, resp.StatusCode, response)
148 upload_status <- uploadStatus{errors.New(resp.Status), url, resp.StatusCode, rep, response}
152 func (this KeepClient) putReplicas(
154 tr *streamer.AsyncStream,
155 expectedLength int64) (locator string, replicas int, err error) {
157 // Take the hash of locator and timestamp in order to identify this
158 // specific transaction in log statements.
159 requestId := fmt.Sprintf("%x", md5.Sum([]byte(locator+time.Now().String())))[0:8]
161 // Calculate the ordering for uploading to servers
162 sv := NewRootSorter(this.ServiceRoots(), hash).GetSortedRoots()
164 // The next server to try contacting
167 // The number of active writers
170 // Used to communicate status from the upload goroutines
171 upload_status := make(chan uploadStatus)
172 defer close(upload_status)
174 // Desired number of replicas
175 remaining_replicas := this.Want_replicas
177 for remaining_replicas > 0 {
178 for active < remaining_replicas {
179 // Start some upload requests
180 if next_server < len(sv) {
181 log.Printf("[%v] Begin upload %s to %s", requestId, hash, sv[next_server])
182 go this.uploadToKeepServer(sv[next_server], hash, tr.MakeStreamReader(), upload_status, expectedLength, requestId)
187 return locator, (this.Want_replicas - remaining_replicas), InsufficientReplicasError
193 log.Printf("[%v] Replicas remaining to write: %v active uploads: %v",
194 requestId, remaining_replicas, active)
196 // Now wait for something to happen.
197 status := <-upload_status
200 if status.statusCode == 200 {
202 remaining_replicas -= status.replicas_stored
203 locator = status.response
207 return locator, this.Want_replicas, nil