1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
21 "git.arvados.org/arvados.git/sdk/go/arvados"
22 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
23 "git.arvados.org/arvados.git/sdk/go/asyncbuf"
26 // DebugPrintf emits debug messages. The easiest way to enable
27 // keepclient debug messages in your application is to assign
28 // log.Printf to DebugPrintf.
29 var DebugPrintf = func(string, ...interface{}) {}
32 if arvadosclient.StringBool(os.Getenv("ARVADOS_DEBUG")) {
33 DebugPrintf = log.Printf
37 type keepService struct {
38 Uuid string `json:"uuid"`
39 Hostname string `json:"service_host"`
40 Port int `json:"service_port"`
41 SSL bool `json:"service_ssl_flag"`
42 SvcType string `json:"service_type"`
43 ReadOnly bool `json:"read_only"`
46 // Md5String returns md5 hash for the bytes in the given string
47 func Md5String(s string) string {
48 return fmt.Sprintf("%x", md5.Sum([]byte(s)))
52 Items []keepService `json:"items"`
55 type uploadStatus struct {
60 classesStored map[string]int
64 func (kc *KeepClient) uploadToKeepServer(host string, hash string, classesTodo []string, body io.Reader,
65 uploadStatusChan chan<- uploadStatus, expectedLength int, reqid string) {
69 var url = fmt.Sprintf("%s/%s", host, hash)
70 if req, err = http.NewRequest("PUT", url, nil); err != nil {
71 DebugPrintf("DEBUG: [%s] Error creating request PUT %v error: %v", reqid, url, err.Error())
72 uploadStatusChan <- uploadStatus{err, url, 0, 0, nil, ""}
76 req.ContentLength = int64(expectedLength)
77 if expectedLength > 0 {
78 req.Body = ioutil.NopCloser(body)
80 // "For client requests, a value of 0 means unknown if
81 // Body is not nil." In this case we do want the body
82 // to be empty, so don't set req.Body.
85 req.Header.Add("X-Request-Id", reqid)
86 req.Header.Add("Authorization", "OAuth2 "+kc.Arvados.ApiToken)
87 req.Header.Add("Content-Type", "application/octet-stream")
88 req.Header.Add(XKeepDesiredReplicas, fmt.Sprint(kc.Want_replicas))
89 if len(classesTodo) > 0 {
90 req.Header.Add(XKeepStorageClasses, strings.Join(classesTodo, ", "))
93 var resp *http.Response
94 if resp, err = kc.httpClient().Do(req); err != nil {
95 DebugPrintf("DEBUG: [%s] Upload failed %v error: %v", reqid, url, err.Error())
96 uploadStatusChan <- uploadStatus{err, url, 0, 0, nil, err.Error()}
101 if xr := resp.Header.Get(XKeepReplicasStored); xr != "" {
102 fmt.Sscanf(xr, "%d", &rep)
104 scc := resp.Header.Get(XKeepStorageClassesConfirmed)
105 classesStored, err := parseStorageClassesConfirmedHeader(scc)
107 DebugPrintf("DEBUG: [%s] Ignoring invalid %s header %q: %s", reqid, XKeepStorageClassesConfirmed, scc, err)
110 defer resp.Body.Close()
111 defer io.Copy(ioutil.Discard, resp.Body)
113 respbody, err2 := ioutil.ReadAll(&io.LimitedReader{R: resp.Body, N: 4096})
114 response := strings.TrimSpace(string(respbody))
115 if err2 != nil && err2 != io.EOF {
116 DebugPrintf("DEBUG: [%s] Upload %v error: %v response: %v", reqid, url, err2.Error(), response)
117 uploadStatusChan <- uploadStatus{err2, url, resp.StatusCode, rep, classesStored, response}
118 } else if resp.StatusCode == http.StatusOK {
119 DebugPrintf("DEBUG: [%s] Upload %v success", reqid, url)
120 uploadStatusChan <- uploadStatus{nil, url, resp.StatusCode, rep, classesStored, response}
122 if resp.StatusCode >= 300 && response == "" {
123 response = resp.Status
125 DebugPrintf("DEBUG: [%s] Upload %v error: %v response: %v", reqid, url, resp.StatusCode, response)
126 uploadStatusChan <- uploadStatus{errors.New(resp.Status), url, resp.StatusCode, rep, classesStored, response}
130 func (kc *KeepClient) BlockWrite(ctx context.Context, req arvados.BlockWriteOptions) (arvados.BlockWriteResponse, error) {
131 var resp arvados.BlockWriteResponse
132 var getReader func() io.Reader
133 if req.Data == nil && req.Reader == nil {
134 return resp, errors.New("invalid BlockWriteOptions: Data and Reader are both nil")
136 if req.DataSize < 0 {
137 return resp, fmt.Errorf("invalid BlockWriteOptions: negative DataSize %d", req.DataSize)
139 if req.DataSize > BLOCKSIZE || len(req.Data) > BLOCKSIZE {
140 return resp, ErrOversizeBlock
143 if req.DataSize > len(req.Data) {
144 return resp, errors.New("invalid BlockWriteOptions: DataSize > len(Data)")
146 if req.DataSize == 0 {
147 req.DataSize = len(req.Data)
149 getReader = func() io.Reader { return bytes.NewReader(req.Data[:req.DataSize]) }
151 buf := asyncbuf.NewBuffer(make([]byte, 0, req.DataSize))
153 _, err := io.Copy(buf, HashCheckingReader{req.Reader, md5.New(), req.Hash})
154 buf.CloseWithError(err)
156 getReader = buf.NewReader
160 _, err := io.Copy(m, getReader())
164 req.Hash = fmt.Sprintf("%x", m.Sum(nil))
166 if req.StorageClasses == nil {
167 if len(kc.StorageClasses) > 0 {
168 req.StorageClasses = kc.StorageClasses
170 req.StorageClasses = kc.DefaultStorageClasses
173 if req.Replicas == 0 {
174 req.Replicas = kc.Want_replicas
176 if req.RequestID == "" {
177 req.RequestID = kc.getRequestID()
179 if req.Attempts == 0 {
180 req.Attempts = 1 + kc.Retries
183 // Calculate the ordering for uploading to servers
184 sv := NewRootSorter(kc.WritableLocalRoots(), req.Hash).GetSortedRoots()
186 // The next server to try contacting
189 // The number of active writers
192 // Used to communicate status from the upload goroutines
193 uploadStatusChan := make(chan uploadStatus)
195 // Wait for any abandoned uploads (e.g., we started
196 // two uploads and the first replied with replicas=2)
197 // to finish before closing the status channel.
202 close(uploadStatusChan)
206 replicasTodo := map[string]int{}
207 for _, c := range req.StorageClasses {
208 replicasTodo[c] = req.Replicas
211 replicasPerThread := kc.replicasPerService
212 if replicasPerThread < 1 {
213 // unlimited or unknown
214 replicasPerThread = req.Replicas
217 retriesRemaining := req.Attempts
218 var retryServers []string
220 lastError := make(map[string]string)
221 trackingClasses := len(replicasTodo) > 0
223 for retriesRemaining > 0 {
226 retryServers = []string{}
228 var classesTodo []string
229 var maxConcurrency int
230 for sc, r := range replicasTodo {
231 classesTodo = append(classesTodo, sc)
232 if maxConcurrency == 0 || maxConcurrency > r {
233 // Having more than r
235 // would overreplicate
240 if !trackingClasses {
241 maxConcurrency = req.Replicas - resp.Replicas
243 if maxConcurrency < 1 {
244 // If there are no non-zero entries in
245 // replicasTodo, we're done.
248 for active*replicasPerThread < maxConcurrency {
249 // Start some upload requests
250 if nextServer < len(sv) {
251 DebugPrintf("DEBUG: [%s] Begin upload %s to %s", req.RequestID, req.Hash, sv[nextServer])
252 go kc.uploadToKeepServer(sv[nextServer], req.Hash, classesTodo, getReader(), uploadStatusChan, req.DataSize, req.RequestID)
256 if active == 0 && retriesRemaining == 0 {
257 msg := "Could not write sufficient replicas: "
258 for _, resp := range lastError {
261 msg = msg[:len(msg)-2]
262 return resp, InsufficientReplicasError{error: errors.New(msg)}
268 DebugPrintf("DEBUG: [%s] Replicas remaining to write: %v active uploads: %v", req.RequestID, replicasTodo, active)
273 // Wait for something to happen.
274 status := <-uploadStatusChan
277 if status.statusCode == http.StatusOK {
278 delete(lastError, status.url)
279 resp.Replicas += status.replicasStored
280 if len(status.classesStored) == 0 {
281 // Server doesn't report
282 // storage classes. Give up
283 // trying to track which ones
284 // are satisfied; just rely on
286 trackingClasses = false
288 for className, replicas := range status.classesStored {
289 if replicasTodo[className] > replicas {
290 replicasTodo[className] -= replicas
292 delete(replicasTodo, className)
295 resp.Locator = status.response
297 msg := fmt.Sprintf("[%d] %s", status.statusCode, status.response)
301 lastError[status.url] = msg
304 if status.statusCode == 0 || status.statusCode == 408 || status.statusCode == 429 ||
305 (status.statusCode >= 500 && status.statusCode != 503) {
306 // Timeout, too many requests, or other server side failure
307 // Do not retry when status code is 503, which means the keep server is full
308 retryServers = append(retryServers, status.url[0:strings.LastIndex(status.url, "/")])
318 func parseStorageClassesConfirmedHeader(hdr string) (map[string]int, error) {
322 classesStored := map[string]int{}
323 for _, cr := range strings.Split(hdr, ",") {
324 cr = strings.TrimSpace(cr)
328 fields := strings.SplitN(cr, "=", 2)
329 if len(fields) != 2 {
330 return nil, fmt.Errorf("expected exactly one '=' char in entry %q", cr)
332 className := fields[0]
334 return nil, fmt.Errorf("empty class name in entry %q", cr)
336 replicas, err := strconv.Atoi(fields[1])
337 if err != nil || replicas < 1 {
338 return nil, fmt.Errorf("invalid replica count %q", fields[1])
340 classesStored[className] = replicas
342 return classesStored, nil