1 /* Internal methods to support keepclient.go */
18 type keepDisk struct {
19 Hostname string `json:"service_host"`
20 Port int `json:"service_port"`
21 SSL bool `json:"service_ssl_flag"`
22 SvcType string `json:"service_type"`
25 func (this *KeepClient) DiscoverKeepServers() error {
26 if prx := os.Getenv("ARVADOS_KEEP_PROXY"); prx != "" {
27 this.SetServiceRoots([]string{prx})
28 this.Using_proxy = true
32 // Construct request of keep disk list
36 if req, err = http.NewRequest("GET", fmt.Sprintf("https://%s/arvados/v1/keep_services/accessible?format=json", this.ApiServer), nil); err != nil {
40 // Add api token header
41 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.ApiToken))
43 req.Header.Add("X-External-Client", "1")
47 var resp *http.Response
48 if resp, err = this.Client.Do(req); err != nil {
52 if resp.StatusCode != http.StatusOK {
53 // fall back on keep disks
54 if req, err = http.NewRequest("GET", fmt.Sprintf("https://%s/arvados/v1/keep_disks", this.ApiServer), nil); err != nil {
57 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.ApiToken))
58 if resp, err = this.Client.Do(req); err != nil {
61 if resp.StatusCode != http.StatusOK {
62 return errors.New(resp.Status)
67 Items []keepDisk `json:"items"`
71 dec := json.NewDecoder(resp.Body)
73 if err := dec.Decode(&m); err != nil {
77 listed := make(map[string]bool)
78 service_roots := make([]string, 0, len(m.Items))
80 for _, element := range m.Items {
87 // Construct server URL
88 url := fmt.Sprintf("http%s://%s:%d", n, element.Hostname, element.Port)
93 service_roots = append(service_roots, url)
95 if element.SvcType == "proxy" {
96 this.Using_proxy = true
100 this.SetServiceRoots(service_roots)
105 func (this KeepClient) shuffledServiceRoots(hash string) (pseq []string) {
106 // Build an ordering with which to query the Keep servers based on the
107 // contents of the hash. "hash" is a hex-encoded number at least 8
108 // digits (32 bits) long
110 // seed used to calculate the next keep server from 'pool' to be added
114 // Keep servers still to be added to the ordering
115 service_roots := this.ServiceRoots()
116 pool := make([]string, len(service_roots))
117 copy(pool, service_roots)
119 // output probe sequence
120 pseq = make([]string, 0, len(service_roots))
122 // iterate while there are servers left to be assigned
126 // ran out of digits in the seed
127 if len(pseq) < (len(hash) / 4) {
128 // the number of servers added to the probe
129 // sequence is less than the number of 4-digit
130 // slices in 'hash' so refill the seed with the
132 seed = hash[len(hash)-4:]
137 // Take the next 8 digits (32 bytes) and interpret as an integer,
138 // then modulus with the size of the remaining pool to get the next
140 probe, _ := strconv.ParseUint(seed[0:8], 16, 32)
141 probe %= uint64(len(pool))
143 // Append the selected server to the probe sequence and remove it
145 pseq = append(pseq, pool[probe])
146 pool = append(pool[:probe], pool[probe+1:]...)
148 // Remove the digits just used from the seed
154 type uploadStatus struct {
162 func (this KeepClient) uploadToKeepServer(host string, hash string, body io.ReadCloser,
163 upload_status chan<- uploadStatus, expectedLength int64) {
165 log.Printf("Uploading %s to %s", hash, host)
167 var req *http.Request
169 var url = fmt.Sprintf("%s/%s", host, hash)
170 if req, err = http.NewRequest("PUT", url, nil); err != nil {
171 upload_status <- uploadStatus{err, url, 0, 0, ""}
176 if expectedLength > 0 {
177 req.ContentLength = expectedLength
180 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.ApiToken))
181 req.Header.Add("Content-Type", "application/octet-stream")
183 if this.Using_proxy {
184 req.Header.Add(X_Keep_Desired_Replicas, fmt.Sprint(this.Want_replicas))
189 var resp *http.Response
190 if resp, err = this.Client.Do(req); err != nil {
191 upload_status <- uploadStatus{err, url, 0, 0, ""}
197 if xr := resp.Header.Get(X_Keep_Replicas_Stored); xr != "" {
198 fmt.Sscanf(xr, "%d", &rep)
201 respbody, err2 := ioutil.ReadAll(&io.LimitedReader{resp.Body, 4096})
202 if err2 != nil && err2 != io.EOF {
203 upload_status <- uploadStatus{err2, url, resp.StatusCode, rep, string(respbody)}
207 locator := strings.TrimSpace(string(respbody))
209 if resp.StatusCode == http.StatusOK {
210 upload_status <- uploadStatus{nil, url, resp.StatusCode, rep, locator}
212 upload_status <- uploadStatus{errors.New(resp.Status), url, resp.StatusCode, rep, locator}
216 func (this KeepClient) putReplicas(
218 tr *streamer.AsyncStream,
219 expectedLength int64) (locator string, replicas int, err error) {
221 // Calculate the ordering for uploading to servers
222 sv := this.shuffledServiceRoots(hash)
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
236 remaining_replicas := this.Want_replicas
238 for remaining_replicas > 0 {
239 for active < remaining_replicas {
240 // Start some upload requests
241 if next_server < len(sv) {
242 go this.uploadToKeepServer(sv[next_server], hash, tr.MakeStreamReader(), upload_status, expectedLength)
247 return locator, (this.Want_replicas - remaining_replicas), InsufficientReplicasError
254 // Now wait for something to happen.
255 status := <-upload_status
256 if status.statusCode == 200 {
258 remaining_replicas -= status.replicas_stored
259 locator = status.response
261 // writing to keep server failed for some reason
262 log.Printf("Keep server put to %v failed with '%v'",
263 status.url, status.err)
266 log.Printf("Upload to %v status code: %v remaining replicas: %v active: %v", status.url, status.statusCode, remaining_replicas, active)
269 return locator, this.Want_replicas, nil