18 // A Keep "block" is 64MB.
19 const BLOCKSIZE = 64 * 1024 * 1024
21 type KeepClient struct {
25 Service_roots []string
30 type KeepDisk struct {
31 Hostname string `json:"service_host"`
32 Port int `json:"service_port"`
33 SSL bool `json:"service_ssl_flag"`
36 func MakeKeepClient() (kc KeepClient, err error) {
37 tr := &http.Transport{
38 TLSClientConfig: &tls.Config{InsecureSkipVerify: kc.ApiInsecure},
42 ApiServer: os.Getenv("ARVADOS_API_HOST"),
43 ApiToken: os.Getenv("ARVADOS_API_TOKEN"),
44 ApiInsecure: (os.Getenv("ARVADOS_API_HOST_INSECURE") != ""),
46 Client: &http.Client{Transport: tr}}
48 err = (&kc).DiscoverKeepDisks()
53 func (this *KeepClient) DiscoverKeepDisks() error {
54 // Construct request of keep disk list
57 if req, err = http.NewRequest("GET", fmt.Sprintf("https://%s/arvados/v1/keep_disks", this.ApiServer), nil); err != nil {
61 // Add api token header
62 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.ApiToken))
65 var resp *http.Response
66 if resp, err = this.Client.Do(req); err != nil {
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 this.Service_roots = make([]string, 0, len(m.Items))
84 for _, element := range m.Items {
90 // Construct server URL
91 url := fmt.Sprintf("http%s://%s:%d", n, element.Hostname, element.Port)
96 this.Service_roots = append(this.Service_roots, url)
100 // Must be sorted for ShuffledServiceRoots() to produce consistent
102 sort.Strings(this.Service_roots)
107 func (this KeepClient) ShuffledServiceRoots(hash string) (pseq []string) {
108 // Build an ordering with which to query the Keep servers based on the
109 // contents of the hash. "hash" is a hex-encoded number at least 8
110 // digits (32 bits) long
112 // seed used to calculate the next keep server from 'pool' to be added
116 // Keep servers still to be added to the ordering
117 pool := make([]string, len(this.Service_roots))
118 copy(pool, this.Service_roots)
120 // output probe sequence
121 pseq = make([]string, 0, len(this.Service_roots))
123 // iterate while there are servers left to be assigned
127 // ran out of digits in the seed
128 if len(pseq) < (len(hash) / 4) {
129 // the number of servers added to the probe
130 // sequence is less than the number of 4-digit
131 // slices in 'hash' so refill the seed with the
133 seed = hash[len(hash)-4:]
138 // Take the next 8 digits (32 bytes) and interpret as an integer,
139 // then modulus with the size of the remaining pool to get the next
141 probe, _ := strconv.ParseUint(seed[0:8], 16, 32)
142 probe %= uint64(len(pool))
144 // Append the selected server to the probe sequence and remove it
146 pseq = append(pseq, pool[probe])
147 pool = append(pool[:probe], pool[probe+1:]...)
149 // Remove the digits just used from the seed
155 type ReaderSlice struct {
160 // Read repeatedly from the reader into the specified buffer, and report each
161 // read to channel 'c'. Completes when Reader 'r' reports on the error channel
162 // and closes channel 'c'.
163 func ReadIntoBuffer(buffer []byte, r io.Reader, slices chan<- ReaderSlice) {
166 // Initially use entire buffer as scratch space
172 // Read into the scratch space
175 // Ran out of scratch space, try reading one more byte
177 n, err = r.Read(b[:])
180 // Reader has more data but we have nowhere to
181 // put it, so we're stuffed
182 slices <- ReaderSlice{nil, io.ErrShortBuffer}
184 // Return some other error (hopefully EOF)
185 slices <- ReaderSlice{nil, err}
190 // End on error (includes EOF)
192 slices <- ReaderSlice{nil, err}
197 // Make a slice with the contents of the read
198 slices <- ReaderSlice{ptr[:n], nil}
200 // Adjust the scratch space slice
206 // A read request to the Transfer() function
207 type ReadRequest struct {
210 result chan<- ReadResult
213 // A read result from the Transfer() function
214 type ReadResult struct {
219 // Reads from the buffer managed by the Transfer()
220 type BufferReader struct {
222 requests chan<- ReadRequest
223 responses chan ReadResult
226 func MakeBufferReader(requests chan<- ReadRequest) BufferReader {
227 return BufferReader{new(int), requests, make(chan ReadResult)}
230 // Reads from the buffer managed by the Transfer()
231 func (this BufferReader) Read(p []byte) (n int, err error) {
232 this.requests <- ReadRequest{*this.offset, len(p), this.responses}
233 rr, valid := <-this.responses
235 *this.offset += len(rr.slice)
236 return copy(p, rr.slice), rr.err
238 return 0, io.ErrUnexpectedEOF
242 func (this BufferReader) WriteTo(dest io.Writer) (written int64, err error) {
243 // Record starting offset in order to correctly report the number of bytes sent
244 starting_offset := *this.offset
246 this.requests <- ReadRequest{*this.offset, 32 * 1024, this.responses}
247 rr, valid := <-this.responses
249 log.Printf("WriteTo slice %v %d %v", *this.offset, len(rr.slice), rr.err)
250 *this.offset += len(rr.slice)
252 if rr.err == io.EOF {
253 // EOF is not an error.
254 return int64(*this.offset - starting_offset), nil
256 return int64(*this.offset - starting_offset), rr.err
262 return int64(*this.offset), io.ErrUnexpectedEOF
267 // Close the responses channel
268 func (this BufferReader) Close() error {
269 close(this.responses)
273 // Handle a read request. Returns true if a response was sent, and false if
274 // the request should be queued.
275 func HandleReadRequest(req ReadRequest, body []byte, complete bool) bool {
276 log.Printf("HandleReadRequest %d %d %d", req.offset, req.maxsize, len(body))
277 if req.offset < len(body) {
279 if req.offset+req.maxsize < len(body) {
280 end = req.offset + req.maxsize
284 req.result <- ReadResult{body[req.offset:end], nil}
286 } else if complete && req.offset >= len(body) {
287 req.result <- ReadResult{nil, io.EOF}
294 // If 'source_reader' is not nil, reads data from 'source_reader' and stores it
295 // in the provided buffer. Otherwise, use the contents of 'buffer' as is.
296 // Accepts read requests on the buffer on the 'requests' channel. Completes
297 // when 'requests' channel is closed.
298 func Transfer(source_buffer []byte, source_reader io.Reader, requests <-chan ReadRequest, reader_error chan error) {
299 // currently buffered data
302 // for receiving slices from ReadIntoBuffer
303 var slices chan ReaderSlice = nil
305 // indicates whether the buffered data is complete
306 var complete bool = false
308 if source_reader != nil {
309 // 'body' is the buffer slice representing the body content read so far
310 body = source_buffer[:0]
312 // used to communicate slices of the buffer as they are
313 // ReadIntoBuffer will close 'slices' when it is done with it
314 slices = make(chan ReaderSlice)
317 go ReadIntoBuffer(source_buffer, source_reader, slices)
319 // use the whole buffer
320 body = source_buffer[:]
322 // buffer is complete
326 pending_requests := make([]ReadRequest, 0)
330 case req, valid := <-requests:
331 // Handle a buffer read request
333 if !HandleReadRequest(req, body, complete) {
334 pending_requests = append(pending_requests, req)
337 // closed 'requests' channel indicates we're done
341 case bk, valid := <-slices:
342 // Got a new slice from the reader
344 if bk.reader_error != nil {
345 reader_error <- bk.reader_error
346 if bk.reader_error == io.EOF {
347 // EOF indicates the reader is done
348 // sending, so our buffer is complete.
351 // some other reader error
357 // adjust body bounds now that another slice has been read
358 body = source_buffer[0 : len(body)+len(bk.slice)]
361 // handle pending reads
363 for n < len(pending_requests) {
364 if HandleReadRequest(pending_requests[n], body, complete) {
366 // move the element from the
367 // back of the slice to
368 // position 'n', then shorten
369 // the slice by one element
370 pending_requests[n] = pending_requests[len(pending_requests)-1]
371 pending_requests = pending_requests[0 : len(pending_requests)-1]
374 // Request wasn't handled, so keep it in the request slice
383 // reader channel closed without signaling EOF
384 reader_error <- io.ErrUnexpectedEOF
392 type UploadStatus struct {
398 func (this KeepClient) uploadToKeepServer(host string, hash string, body io.ReadCloser,
399 upload_status chan<- UploadStatus, expectedLength int64) {
401 log.Printf("Uploading to %s", host)
403 var req *http.Request
405 var url = fmt.Sprintf("%s/%s", host, hash)
406 if req, err = http.NewRequest("PUT", url, nil); err != nil {
407 upload_status <- UploadStatus{err, url, 0}
411 if expectedLength > 0 {
412 req.ContentLength = expectedLength
415 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.ApiToken))
416 req.Header.Add("Content-Type", "application/octet-stream")
419 var resp *http.Response
420 if resp, err = this.Client.Do(req); err != nil {
421 upload_status <- UploadStatus{err, url, 0}
425 if resp.StatusCode == http.StatusOK {
426 upload_status <- UploadStatus{nil, url, resp.StatusCode}
428 upload_status <- UploadStatus{errors.New(resp.Status), url, resp.StatusCode}
432 var InsufficientReplicasError = errors.New("Could not write sufficient replicas")
434 func (this KeepClient) putReplicas(
436 requests chan ReadRequest,
437 reader_status chan error,
438 expectedLength int64) (replicas int, err error) {
440 // Calculate the ordering for uploading to servers
441 sv := this.ShuffledServiceRoots(hash)
443 // The next server to try contacting
446 // The number of active writers
449 // Used to communicate status from the upload goroutines
450 upload_status := make(chan UploadStatus)
451 defer close(upload_status)
453 // Desired number of replicas
454 remaining_replicas := this.Want_replicas
456 for remaining_replicas > 0 {
457 for active < remaining_replicas {
458 // Start some upload requests
459 if next_server < len(sv) {
460 go this.uploadToKeepServer(sv[next_server], hash, MakeBufferReader(requests), upload_status, expectedLength)
464 return (this.Want_replicas - remaining_replicas), InsufficientReplicasError
468 // Now wait for something to happen.
470 case status := <-reader_status:
471 if status == io.EOF {
475 return (this.Want_replicas - remaining_replicas), status
477 case status := <-upload_status:
478 if status.StatusCode == 200 {
480 remaining_replicas -= 1
482 // writing to keep server failed for some reason
483 log.Printf("Keep server put to %v failed with '%v'",
484 status.Url, status.Err)
487 log.Printf("Upload status %v %v %v", status.StatusCode, remaining_replicas, active)
491 return (this.Want_replicas - remaining_replicas), nil
494 var OversizeBlockError = errors.New("Block too big")
496 func (this KeepClient) PutHR(hash string, r io.Reader, expectedLength int64) (replicas int, err error) {
498 // Buffer for reads from 'r'
500 if expectedLength > 0 {
501 if expectedLength > BLOCKSIZE {
502 return 0, OversizeBlockError
504 buffer = make([]byte, expectedLength)
506 buffer = make([]byte, BLOCKSIZE)
509 // Read requests on Transfer() buffer
510 requests := make(chan ReadRequest)
511 defer close(requests)
513 // Reporting reader error states
514 reader_status := make(chan error)
515 defer close(reader_status)
517 // Start the transfer goroutine
518 go Transfer(buffer, r, requests, reader_status)
520 return this.putReplicas(hash, requests, reader_status, expectedLength)
523 func (this KeepClient) PutHB(hash string, buffer []byte) (replicas int, err error) {
524 // Read requests on Transfer() buffer
525 requests := make(chan ReadRequest)
526 defer close(requests)
528 // Start the transfer goroutine
529 go Transfer(buffer, nil, requests, nil)
531 return this.putReplicas(hash, requests, nil, int64(len(buffer)))
534 func (this KeepClient) PutB(buffer []byte) (hash string, replicas int, err error) {
535 hash = fmt.Sprintf("%x", md5.Sum(buffer))
536 replicas, err = this.PutHB(hash, buffer)
537 return hash, replicas, err
540 func (this KeepClient) PutR(r io.Reader) (hash string, replicas int, err error) {
541 if buffer, err := ioutil.ReadAll(r); err != nil {
544 return this.PutB(buffer)
548 var BlockNotFound = errors.New("Block not found")
550 func (this KeepClient) Get(hash string) (reader io.ReadCloser,
551 contentLength int64, url string, err error) {
552 return this.AuthorizedGet(hash, "", "")
555 func (this KeepClient) AuthorizedGet(hash string,
557 timestamp string) (reader io.ReadCloser,
558 contentLength int64, url string, err error) {
560 // Calculate the ordering for asking servers
561 sv := this.ShuffledServiceRoots(hash)
563 for _, host := range sv {
564 var req *http.Request
568 url = fmt.Sprintf("%s/%s+A%s@%s", host, hash,
569 signature, timestamp)
571 url = fmt.Sprintf("%s/%s", host, hash)
573 if req, err = http.NewRequest("GET", url, nil); err != nil {
577 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.ApiToken))
579 var resp *http.Response
580 if resp, err = this.Client.Do(req); err != nil {
584 if resp.StatusCode == http.StatusOK {
585 return resp.Body, resp.ContentLength, url, nil
589 return nil, 0, "", BlockNotFound
592 func (this KeepClient) Ask(hash string) (contentLength int64, url string, err error) {
593 return this.AuthorizedAsk(hash, "", "")
596 func (this KeepClient) AuthorizedAsk(hash string, signature string,
597 timestamp string) (contentLength int64, url string, err error) {
598 // Calculate the ordering for asking servers
599 sv := this.ShuffledServiceRoots(hash)
601 for _, host := range sv {
602 var req *http.Request
605 url = fmt.Sprintf("%s/%s+A%s@%s", host, hash,
606 signature, timestamp)
608 url = fmt.Sprintf("%s/%s", host, hash)
611 if req, err = http.NewRequest("HEAD", url, nil); err != nil {
615 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", this.ApiToken))
617 var resp *http.Response
618 if resp, err = this.Client.Do(req); err != nil {
622 if resp.StatusCode == http.StatusOK {
623 return resp.ContentLength, url, nil
627 return 0, "", BlockNotFound