2023-08-22 15:50:21 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/jmorganca/ollama/api"
|
|
|
|
)
|
|
|
|
|
2023-09-19 21:22:54 +00:00
|
|
|
const (
|
2023-09-28 17:00:34 +00:00
|
|
|
redirectChunkSize int64 = 1024 * 1024 * 1024
|
|
|
|
regularChunkSize int64 = 95 * 1024 * 1024
|
2023-09-19 21:22:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func startUpload(ctx context.Context, mp ModelPath, layer *Layer, regOpts *RegistryOptions) (*url.URL, int64, error) {
|
2023-08-22 15:50:21 +00:00
|
|
|
requestURL := mp.BaseURL()
|
|
|
|
requestURL = requestURL.JoinPath("v2", mp.GetNamespaceRepository(), "blobs/uploads/")
|
|
|
|
if layer.From != "" {
|
|
|
|
values := requestURL.Query()
|
|
|
|
values.Add("mount", layer.Digest)
|
|
|
|
values.Add("from", layer.From)
|
|
|
|
requestURL.RawQuery = values.Encode()
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := makeRequestWithRetry(ctx, "POST", requestURL, nil, nil, regOpts)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("couldn't start upload: %v", err)
|
2023-09-19 21:22:54 +00:00
|
|
|
return nil, 0, err
|
2023-08-22 15:50:21 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2023-09-19 21:22:54 +00:00
|
|
|
location := resp.Header.Get("Docker-Upload-Location")
|
|
|
|
chunkSize := redirectChunkSize
|
2023-08-22 15:50:21 +00:00
|
|
|
if location == "" {
|
2023-09-19 21:22:54 +00:00
|
|
|
location = resp.Header.Get("Location")
|
|
|
|
chunkSize = regularChunkSize
|
|
|
|
}
|
|
|
|
|
|
|
|
locationURL, err := url.Parse(location)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
2023-08-22 15:50:21 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 17:00:34 +00:00
|
|
|
return locationURL, chunkSize, nil
|
2023-08-22 15:50:21 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 21:22:54 +00:00
|
|
|
func uploadBlob(ctx context.Context, requestURL *url.URL, layer *Layer, chunkSize int64, regOpts *RegistryOptions, fn func(api.ProgressResponse)) error {
|
2023-08-22 15:50:21 +00:00
|
|
|
// TODO allow resumability
|
|
|
|
// TODO allow canceling uploads via DELETE
|
|
|
|
|
|
|
|
fp, err := GetBlobsPath(layer.Digest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(fp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2023-09-14 16:54:05 +00:00
|
|
|
pw := ProgressWriter{
|
|
|
|
status: fmt.Sprintf("uploading %s", layer.Digest),
|
|
|
|
digest: layer.Digest,
|
|
|
|
total: layer.Size,
|
|
|
|
fn: fn,
|
|
|
|
}
|
2023-08-22 15:50:21 +00:00
|
|
|
|
2023-09-28 17:00:34 +00:00
|
|
|
for offset := int64(0); offset < layer.Size; {
|
|
|
|
chunk := layer.Size - offset
|
|
|
|
if chunk > chunkSize {
|
|
|
|
chunk = chunkSize
|
2023-08-22 15:50:21 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 22:42:50 +00:00
|
|
|
resp, err := uploadBlobChunk(ctx, http.MethodPatch, requestURL, f, offset, chunk, regOpts, &pw)
|
2023-09-14 20:30:28 +00:00
|
|
|
if err != nil {
|
|
|
|
fn(api.ProgressResponse{
|
2023-09-14 22:42:50 +00:00
|
|
|
Status: fmt.Sprintf("error uploading chunk: %v", err),
|
2023-09-14 20:30:28 +00:00
|
|
|
Digest: layer.Digest,
|
|
|
|
Total: layer.Size,
|
2023-09-28 17:00:34 +00:00
|
|
|
Completed: offset,
|
2023-09-14 20:30:28 +00:00
|
|
|
})
|
2023-09-15 22:59:30 +00:00
|
|
|
|
|
|
|
return err
|
2023-08-22 15:50:21 +00:00
|
|
|
}
|
2023-09-14 16:54:05 +00:00
|
|
|
|
2023-09-14 20:30:28 +00:00
|
|
|
offset += chunk
|
2023-09-14 22:42:50 +00:00
|
|
|
location := resp.Header.Get("Docker-Upload-Location")
|
|
|
|
if location == "" {
|
|
|
|
location = resp.Header.Get("Location")
|
|
|
|
}
|
|
|
|
|
|
|
|
requestURL, err = url.Parse(location)
|
2023-09-14 20:30:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-09-14 16:54:05 +00:00
|
|
|
}
|
2023-08-22 15:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
values := requestURL.Query()
|
|
|
|
values.Add("digest", layer.Digest)
|
|
|
|
requestURL.RawQuery = values.Encode()
|
|
|
|
|
|
|
|
headers := make(http.Header)
|
|
|
|
headers.Set("Content-Type", "application/octet-stream")
|
|
|
|
headers.Set("Content-Length", "0")
|
|
|
|
|
|
|
|
// finish the upload
|
|
|
|
resp, err := makeRequest(ctx, "PUT", requestURL, headers, nil, regOpts)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("couldn't finish upload: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2023-08-27 04:55:21 +00:00
|
|
|
if resp.StatusCode >= http.StatusBadRequest {
|
2023-08-22 15:50:21 +00:00
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
return fmt.Errorf("on finish upload registry responded with code %d: %v", resp.StatusCode, string(body))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-14 16:54:05 +00:00
|
|
|
|
2023-09-14 22:42:50 +00:00
|
|
|
func uploadBlobChunk(ctx context.Context, method string, requestURL *url.URL, r io.ReaderAt, offset, limit int64, opts *RegistryOptions, pw *ProgressWriter) (*http.Response, error) {
|
2023-09-28 17:00:34 +00:00
|
|
|
sectionReader := io.NewSectionReader(r, offset, limit)
|
2023-09-14 20:30:28 +00:00
|
|
|
|
|
|
|
headers := make(http.Header)
|
|
|
|
headers.Set("Content-Type", "application/octet-stream")
|
|
|
|
headers.Set("Content-Length", strconv.Itoa(int(limit)))
|
2023-09-14 22:42:50 +00:00
|
|
|
headers.Set("X-Redirect-Uploads", "1")
|
|
|
|
|
|
|
|
if method == http.MethodPatch {
|
|
|
|
headers.Set("Content-Range", fmt.Sprintf("%d-%d", offset, offset+sectionReader.Size()-1))
|
|
|
|
}
|
2023-09-14 20:30:28 +00:00
|
|
|
|
|
|
|
for try := 0; try < MaxRetries; try++ {
|
2023-09-14 22:42:50 +00:00
|
|
|
resp, err := makeRequest(ctx, method, requestURL, headers, io.TeeReader(sectionReader, pw), opts)
|
2023-09-14 20:30:28 +00:00
|
|
|
if err != nil && !errors.Is(err, io.EOF) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
switch {
|
2023-09-14 22:42:50 +00:00
|
|
|
case resp.StatusCode == http.StatusTemporaryRedirect:
|
|
|
|
location, err := resp.Location()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-28 17:00:34 +00:00
|
|
|
pw.completed = offset
|
2023-09-14 22:42:50 +00:00
|
|
|
if _, err := uploadBlobChunk(ctx, http.MethodPut, location, r, offset, limit, nil, pw); err != nil {
|
|
|
|
// retry
|
|
|
|
log.Printf("retrying redirected upload: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2023-09-14 20:30:28 +00:00
|
|
|
case resp.StatusCode == http.StatusUnauthorized:
|
|
|
|
auth := resp.Header.Get("www-authenticate")
|
|
|
|
authRedir := ParseAuthRedirectString(auth)
|
|
|
|
token, err := getAuthToken(ctx, authRedir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.Token = token
|
|
|
|
|
2023-09-28 17:00:34 +00:00
|
|
|
pw.completed = offset
|
2023-09-14 20:30:28 +00:00
|
|
|
sectionReader = io.NewSectionReader(r, offset, limit)
|
|
|
|
continue
|
|
|
|
case resp.StatusCode >= http.StatusBadRequest:
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
return nil, fmt.Errorf("on upload registry responded with code %d: %s", resp.StatusCode, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("max retries exceeded")
|
|
|
|
}
|
|
|
|
|
2023-09-14 16:54:05 +00:00
|
|
|
type ProgressWriter struct {
|
|
|
|
status string
|
|
|
|
digest string
|
2023-09-28 17:00:34 +00:00
|
|
|
bucket int64
|
|
|
|
completed int64
|
|
|
|
total int64
|
2023-09-14 16:54:05 +00:00
|
|
|
fn func(api.ProgressResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pw *ProgressWriter) Write(b []byte) (int, error) {
|
|
|
|
n := len(b)
|
2023-09-28 17:00:34 +00:00
|
|
|
pw.bucket += int64(n)
|
2023-09-14 16:54:05 +00:00
|
|
|
|
|
|
|
// throttle status updates to not spam the client
|
2023-09-28 17:00:34 +00:00
|
|
|
if pw.bucket >= 1024*1024 || pw.completed+pw.bucket >= pw.total {
|
|
|
|
pw.completed += pw.bucket
|
2023-09-14 16:54:05 +00:00
|
|
|
pw.fn(api.ProgressResponse{
|
|
|
|
Status: pw.status,
|
|
|
|
Digest: pw.digest,
|
|
|
|
Total: pw.total,
|
|
|
|
Completed: pw.completed,
|
|
|
|
})
|
|
|
|
|
|
|
|
pw.bucket = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|