1 /* Internal methods to support keepclient.go */
17 type keepDisk struct {
18 Hostname string `json:"service_host"`
19 Port int `json:"service_port"`
20 SSL bool `json:"service_ssl_flag"`
21 SvcType string `json:"service_type"`
24 func (this *KeepClient) discoverKeepServers() error {
25 if prx := os.Getenv("ARVADOS_KEEP_PROXY"); prx != "" {
26 this.Service_roots = make([]string, 1)
27 this.Service_roots[0] = 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", this.ApiServer), nil); err != nil {
40 // Add api token header
41 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.ApiToken))
44 var resp *http.Response
45 if resp, err = this.Client.Do(req); err != nil {
49 if resp.StatusCode != 200 {
50 // fall back on keep disks
51 if req, err = http.NewRequest("GET", fmt.Sprintf("https://%s/arvados/v1/keep_disks", this.ApiServer), nil); err != nil {
54 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.ApiToken))
55 if resp, err = this.Client.Do(req); err != nil {
61 Items []keepDisk `json:"items"`
65 dec := json.NewDecoder(resp.Body)
67 if err := dec.Decode(&m); err != nil {
71 listed := make(map[string]bool)
72 this.Service_roots = make([]string, 0, len(m.Items))
74 for _, element := range m.Items {
81 // Construct server URL
82 url := fmt.Sprintf("http%s://%s:%d", n, element.Hostname, element.Port)
87 this.Service_roots = append(this.Service_roots, url)
89 if element.SvcType == "proxy" {
90 this.Using_proxy = true
94 // Must be sorted for ShuffledServiceRoots() to produce consistent
96 sort.Strings(this.Service_roots)
101 func (this KeepClient) shuffledServiceRoots(hash string) (pseq []string) {
102 // Build an ordering with which to query the Keep servers based on the
103 // contents of the hash. "hash" is a hex-encoded number at least 8
104 // digits (32 bits) long
106 // seed used to calculate the next keep server from 'pool' to be added
110 // Keep servers still to be added to the ordering
111 pool := make([]string, len(this.Service_roots))
112 copy(pool, this.Service_roots)
114 // output probe sequence
115 pseq = make([]string, 0, len(this.Service_roots))
117 // iterate while there are servers left to be assigned
121 // ran out of digits in the seed
122 if len(pseq) < (len(hash) / 4) {
123 // the number of servers added to the probe
124 // sequence is less than the number of 4-digit
125 // slices in 'hash' so refill the seed with the
127 seed = hash[len(hash)-4:]
132 // Take the next 8 digits (32 bytes) and interpret as an integer,
133 // then modulus with the size of the remaining pool to get the next
135 probe, _ := strconv.ParseUint(seed[0:8], 16, 32)
136 probe %= uint64(len(pool))
138 // Append the selected server to the probe sequence and remove it
140 pseq = append(pseq, pool[probe])
141 pool = append(pool[:probe], pool[probe+1:]...)
143 // Remove the digits just used from the seed
149 type uploadStatus struct {
156 func (this KeepClient) uploadToKeepServer(host string, hash string, body io.ReadCloser,
157 upload_status chan<- uploadStatus, expectedLength int64) {
159 log.Printf("Uploading to %s", host)
161 var req *http.Request
163 var url = fmt.Sprintf("%s/%s", host, hash)
164 if req, err = http.NewRequest("PUT", url, nil); err != nil {
165 upload_status <- uploadStatus{err, url, 0, 0}
170 if expectedLength > 0 {
171 req.ContentLength = expectedLength
174 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.ApiToken))
175 req.Header.Add("Content-Type", "application/octet-stream")
177 if this.Using_proxy {
178 req.Header.Add("X-Keep-Desired-Replicas", fmt.Sprint(this.Want_replicas))
183 var resp *http.Response
184 if resp, err = this.Client.Do(req); err != nil {
185 upload_status <- uploadStatus{err, url, 0, 0}
190 if xr := resp.Header.Get("X-Keep-Replicas-Stored"); xr != "" {
191 fmt.Sscanf(xr, "%d", &rep)
194 if resp.StatusCode == http.StatusOK {
195 upload_status <- uploadStatus{nil, url, resp.StatusCode, rep}
197 upload_status <- uploadStatus{errors.New(resp.Status), url, resp.StatusCode, rep}
201 func (this KeepClient) putReplicas(
203 tr *streamer.AsyncStream,
204 expectedLength int64) (replicas int, err error) {
206 // Calculate the ordering for uploading to servers
207 sv := this.shuffledServiceRoots(hash)
209 // The next server to try contacting
212 // The number of active writers
215 // Used to communicate status from the upload goroutines
216 upload_status := make(chan uploadStatus)
217 defer close(upload_status)
219 // Desired number of replicas
221 remaining_replicas := this.Want_replicas
223 for remaining_replicas > 0 {
224 for active < remaining_replicas {
225 // Start some upload requests
226 if next_server < len(sv) {
227 go this.uploadToKeepServer(sv[next_server], hash, tr.MakeStreamReader(), upload_status, expectedLength)
233 return (this.Want_replicas - remaining_replicas), InsufficientReplicasError
240 // Now wait for something to happen.
241 status := <-upload_status
242 if status.statusCode == 200 {
244 remaining_replicas -= status.replicas_stored
246 // writing to keep server failed for some reason
247 log.Printf("Keep server put to %v failed with '%v'",
248 status.url, status.err)
251 log.Printf("Upload status %v %v %v", status.statusCode, remaining_replicas, active)
254 return this.Want_replicas, nil