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)
66 // 'defer' is a stack, so it will drain the Body before closing it.
67 defer resp.Body.Close()
68 defer io.Copy(ioutil.Discard, resp.Body)
71 Items []keepDisk `json:"items"`
75 dec := json.NewDecoder(resp.Body)
77 if err := dec.Decode(&m); err != nil {
81 listed := make(map[string]bool)
82 service_roots := make([]string, 0, len(m.Items))
84 for _, element := range m.Items {
91 // Construct server URL
92 url := fmt.Sprintf("http%s://%s:%d", n, element.Hostname, element.Port)
97 service_roots = append(service_roots, url)
99 if element.SvcType == "proxy" {
100 this.Using_proxy = true
104 this.SetServiceRoots(service_roots)
109 func (this KeepClient) shuffledServiceRoots(hash string) (pseq []string) {
110 // Build an ordering with which to query the Keep servers based on the
111 // contents of the hash. "hash" is a hex-encoded number at least 8
112 // digits (32 bits) long
114 // seed used to calculate the next keep server from 'pool' to be added
118 // Keep servers still to be added to the ordering
119 service_roots := this.ServiceRoots()
120 pool := make([]string, len(service_roots))
121 copy(pool, service_roots)
123 // output probe sequence
124 pseq = make([]string, 0, len(service_roots))
126 // iterate while there are servers left to be assigned
130 // ran out of digits in the seed
131 if len(pseq) < (len(hash) / 4) {
132 // the number of servers added to the probe
133 // sequence is less than the number of 4-digit
134 // slices in 'hash' so refill the seed with the
136 seed = hash[len(hash)-4:]
141 // Take the next 8 digits (32 bytes) and interpret as an integer,
142 // then modulus with the size of the remaining pool to get the next
144 probe, _ := strconv.ParseUint(seed[0:8], 16, 32)
145 probe %= uint64(len(pool))
147 // Append the selected server to the probe sequence and remove it
149 pseq = append(pseq, pool[probe])
150 pool = append(pool[:probe], pool[probe+1:]...)
152 // Remove the digits just used from the seed
158 type uploadStatus struct {
166 func (this KeepClient) uploadToKeepServer(host string, hash string, body io.ReadCloser,
167 upload_status chan<- uploadStatus, expectedLength int64) {
169 log.Printf("Uploading %s to %s", hash, host)
171 var req *http.Request
173 var url = fmt.Sprintf("%s/%s", host, hash)
174 if req, err = http.NewRequest("PUT", url, nil); err != nil {
175 upload_status <- uploadStatus{err, url, 0, 0, ""}
180 if expectedLength > 0 {
181 req.ContentLength = expectedLength
184 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.ApiToken))
185 req.Header.Add("Content-Type", "application/octet-stream")
187 if this.Using_proxy {
188 req.Header.Add(X_Keep_Desired_Replicas, fmt.Sprint(this.Want_replicas))
193 var resp *http.Response
194 if resp, err = this.Client.Do(req); err != nil {
195 upload_status <- uploadStatus{err, url, 0, 0, ""}
201 if xr := resp.Header.Get(X_Keep_Replicas_Stored); xr != "" {
202 fmt.Sscanf(xr, "%d", &rep)
205 defer resp.Body.Close()
206 defer io.Copy(ioutil.Discard, resp.Body)
208 respbody, err2 := ioutil.ReadAll(&io.LimitedReader{resp.Body, 4096})
209 if err2 != nil && err2 != io.EOF {
210 upload_status <- uploadStatus{err2, url, resp.StatusCode, rep, string(respbody)}
214 locator := strings.TrimSpace(string(respbody))
216 if resp.StatusCode == http.StatusOK {
217 upload_status <- uploadStatus{nil, url, resp.StatusCode, rep, locator}
219 upload_status <- uploadStatus{errors.New(resp.Status), url, resp.StatusCode, rep, locator}
223 func (this KeepClient) putReplicas(
225 tr *streamer.AsyncStream,
226 expectedLength int64) (locator string, replicas int, err error) {
228 // Calculate the ordering for uploading to servers
229 sv := this.shuffledServiceRoots(hash)
231 // The next server to try contacting
234 // The number of active writers
237 // Used to communicate status from the upload goroutines
238 upload_status := make(chan uploadStatus)
239 defer close(upload_status)
241 // Desired number of replicas
243 remaining_replicas := this.Want_replicas
245 for remaining_replicas > 0 {
246 for active < remaining_replicas {
247 // Start some upload requests
248 if next_server < len(sv) {
249 go this.uploadToKeepServer(sv[next_server], hash, tr.MakeStreamReader(), upload_status, expectedLength)
254 return locator, (this.Want_replicas - remaining_replicas), InsufficientReplicasError
261 // Now wait for something to happen.
262 status := <-upload_status
263 if status.statusCode == 200 {
265 remaining_replicas -= status.replicas_stored
266 locator = status.response
268 // writing to keep server failed for some reason
269 log.Printf("Keep server put to %v failed with '%v'",
270 status.url, status.err)
273 log.Printf("Upload to %v status code: %v remaining replicas: %v active: %v", status.url, status.statusCode, remaining_replicas, active)
276 return locator, this.Want_replicas, nil