upload.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package server
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "log"
  8. "net/http"
  9. "net/url"
  10. "os"
  11. "strconv"
  12. "github.com/jmorganca/ollama/api"
  13. )
  14. func startUpload(ctx context.Context, mp ModelPath, layer *Layer, regOpts *RegistryOptions) (*url.URL, error) {
  15. requestURL := mp.BaseURL()
  16. requestURL = requestURL.JoinPath("v2", mp.GetNamespaceRepository(), "blobs/uploads/")
  17. if layer.From != "" {
  18. values := requestURL.Query()
  19. values.Add("mount", layer.Digest)
  20. values.Add("from", layer.From)
  21. requestURL.RawQuery = values.Encode()
  22. }
  23. resp, err := makeRequestWithRetry(ctx, "POST", requestURL, nil, nil, regOpts)
  24. if err != nil {
  25. log.Printf("couldn't start upload: %v", err)
  26. return nil, err
  27. }
  28. defer resp.Body.Close()
  29. // Extract UUID location from header
  30. location := resp.Header.Get("Location")
  31. if location == "" {
  32. return nil, fmt.Errorf("location header is missing in response")
  33. }
  34. return url.Parse(location)
  35. }
  36. func uploadBlobChunked(ctx context.Context, requestURL *url.URL, layer *Layer, regOpts *RegistryOptions, fn func(api.ProgressResponse)) error {
  37. // TODO allow resumability
  38. // TODO allow canceling uploads via DELETE
  39. fp, err := GetBlobsPath(layer.Digest)
  40. if err != nil {
  41. return err
  42. }
  43. f, err := os.Open(fp)
  44. if err != nil {
  45. return err
  46. }
  47. defer f.Close()
  48. // 95MiB chunk size
  49. chunkSize := 95 * 1024 * 1024
  50. pw := ProgressWriter{
  51. status: fmt.Sprintf("uploading %s", layer.Digest),
  52. digest: layer.Digest,
  53. total: layer.Size,
  54. fn: fn,
  55. }
  56. for offset := int64(0); offset < int64(layer.Size); {
  57. chunk := int64(layer.Size) - offset
  58. if chunk > int64(chunkSize) {
  59. chunk = int64(chunkSize)
  60. }
  61. resp, err := uploadBlobChunk(ctx, http.MethodPatch, requestURL, f, offset, chunk, regOpts, &pw)
  62. if err != nil {
  63. fn(api.ProgressResponse{
  64. Status: fmt.Sprintf("error uploading chunk: %v", err),
  65. Digest: layer.Digest,
  66. Total: layer.Size,
  67. Completed: int(offset),
  68. })
  69. return err
  70. }
  71. offset += chunk
  72. location := resp.Header.Get("Docker-Upload-Location")
  73. if location == "" {
  74. location = resp.Header.Get("Location")
  75. }
  76. requestURL, err = url.Parse(location)
  77. if err != nil {
  78. return err
  79. }
  80. }
  81. values := requestURL.Query()
  82. values.Add("digest", layer.Digest)
  83. requestURL.RawQuery = values.Encode()
  84. headers := make(http.Header)
  85. headers.Set("Content-Type", "application/octet-stream")
  86. headers.Set("Content-Length", "0")
  87. // finish the upload
  88. resp, err := makeRequest(ctx, "PUT", requestURL, headers, nil, regOpts)
  89. if err != nil {
  90. log.Printf("couldn't finish upload: %v", err)
  91. return err
  92. }
  93. defer resp.Body.Close()
  94. if resp.StatusCode >= http.StatusBadRequest {
  95. body, _ := io.ReadAll(resp.Body)
  96. return fmt.Errorf("on finish upload registry responded with code %d: %v", resp.StatusCode, string(body))
  97. }
  98. return nil
  99. }
  100. func uploadBlobChunk(ctx context.Context, method string, requestURL *url.URL, r io.ReaderAt, offset, limit int64, opts *RegistryOptions, pw *ProgressWriter) (*http.Response, error) {
  101. sectionReader := io.NewSectionReader(r, int64(offset), limit)
  102. headers := make(http.Header)
  103. headers.Set("Content-Type", "application/octet-stream")
  104. headers.Set("Content-Length", strconv.Itoa(int(limit)))
  105. headers.Set("X-Redirect-Uploads", "1")
  106. if method == http.MethodPatch {
  107. headers.Set("Content-Range", fmt.Sprintf("%d-%d", offset, offset+sectionReader.Size()-1))
  108. }
  109. for try := 0; try < MaxRetries; try++ {
  110. resp, err := makeRequest(ctx, method, requestURL, headers, io.TeeReader(sectionReader, pw), opts)
  111. if err != nil && !errors.Is(err, io.EOF) {
  112. return nil, err
  113. }
  114. defer resp.Body.Close()
  115. switch {
  116. case resp.StatusCode == http.StatusTemporaryRedirect:
  117. location, err := resp.Location()
  118. if err != nil {
  119. return nil, err
  120. }
  121. pw.completed = int(offset)
  122. if _, err := uploadBlobChunk(ctx, http.MethodPut, location, r, offset, limit, nil, pw); err != nil {
  123. // retry
  124. log.Printf("retrying redirected upload: %v", err)
  125. continue
  126. }
  127. return resp, nil
  128. case resp.StatusCode == http.StatusUnauthorized:
  129. auth := resp.Header.Get("www-authenticate")
  130. authRedir := ParseAuthRedirectString(auth)
  131. token, err := getAuthToken(ctx, authRedir)
  132. if err != nil {
  133. return nil, err
  134. }
  135. opts.Token = token
  136. pw.completed = int(offset)
  137. sectionReader = io.NewSectionReader(r, offset, limit)
  138. continue
  139. case resp.StatusCode >= http.StatusBadRequest:
  140. body, _ := io.ReadAll(resp.Body)
  141. return nil, fmt.Errorf("on upload registry responded with code %d: %s", resp.StatusCode, body)
  142. }
  143. return resp, nil
  144. }
  145. return nil, fmt.Errorf("max retries exceeded")
  146. }
  147. type ProgressWriter struct {
  148. status string
  149. digest string
  150. bucket int
  151. completed int
  152. total int
  153. fn func(api.ProgressResponse)
  154. }
  155. func (pw *ProgressWriter) Write(b []byte) (int, error) {
  156. n := len(b)
  157. pw.bucket += n
  158. pw.completed += n
  159. // throttle status updates to not spam the client
  160. if pw.bucket >= 1024*1024 || pw.completed >= pw.total {
  161. pw.fn(api.ProgressResponse{
  162. Status: pw.status,
  163. Digest: pw.digest,
  164. Total: pw.total,
  165. Completed: pw.completed,
  166. })
  167. pw.bucket = 0
  168. }
  169. return n, nil
  170. }