Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 61 additions & 8 deletions internal/api/serverless/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import (
"github.com/google/uuid"
)

const testEnvironment = "staging"
const (
testEnvironment = "staging"
testVolume = "model-weights"
)

// environmentCall is one client call, named by the generated operation it sends.
type environmentCall struct {
Expand All @@ -28,6 +31,56 @@ type environmentCall struct {
func environmentScopedCalls() []environmentCall {
id := uuid.MustParse("11111111-1111-1111-1111-111111111111")
return []environmentCall{
{
operation: "ListVolumes",
call: func(ctx context.Context, c *Client) error {
_, err := c.ListVolumes(ctx, ListVolumesParams{})
return err
},
},
{
operation: "CreateVolume",
call: func(ctx context.Context, c *Client) error {
_, err := c.CreateVolume(ctx, VolumeCreate{
Name: testVolume,
})
return err
},
},
{
operation: "GetVolume",
call: func(ctx context.Context, c *Client) error {
_, err := c.GetVolume(ctx, testVolume)
return err
},
},
{
operation: "UpdateVolume",
call: func(ctx context.Context, c *Client) error {
_, err := c.UpdateVolume(ctx, testVolume, VolumeUpdate{})
return err
},
},
{
operation: "DeleteVolume",
call: func(ctx context.Context, c *Client) error {
return c.DeleteVolume(ctx, testVolume, id, false)
},
},
{
operation: "GetVolumeReferences",
call: func(ctx context.Context, c *Client) error {
_, err := c.GetVolumeReferences(ctx, testVolume, GetVolumeReferencesParams{})
return err
},
},
{
operation: "ListVolumeFiles",
call: func(ctx context.Context, c *Client) error {
_, err := c.ListVolumeFiles(ctx, testVolume, ListVolumeFilesParams{})
return err
},
},
{
operation: "CreateApp",
call: func(ctx context.Context, c *Client) error {
Expand Down Expand Up @@ -362,6 +415,13 @@ func environmentScopedCalls() []environmentCall {

func orgScopedCalls() []environmentCall {
return []environmentCall{
{
operation: "GetVolumeSettings",
call: func(ctx context.Context, c *Client) error {
_, err := c.GetVolumeSettings(ctx)
return err
},
},
{
operation: "ListGpuTypes",
call: func(ctx context.Context, c *Client) error {
Expand Down Expand Up @@ -559,23 +619,16 @@ var unwrappedOperations = []string{
"CompleteVolumeUploadFile",
"CopySecret",
"CreateSecretBatch",
"CreateVolume",
"CreateVolumeUpload",
"DeclareVolumeUploadFiles",
"DeleteVolume",
"FavoriteApp",
"GetAppSummary",
"GetMetricSeries",
"GetVolume",
"GetVolumeReferences",
"GetVolumeUpload",
"ListVolumeFiles",
"ListVolumes",
"RenewVolumeUploadFileParts",
"StartDefaultAsyncTask",
"StartDefaultSyncTask",
"UnfavoriteApp",
"UpdateVolume",
}

// environmentOperations are the generated operations whose params carry an
Expand Down
29 changes: 29 additions & 0 deletions internal/api/serverless/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func problemToError(p *gen.ProblemDetails, statusCode int) error {
case p.Title != "":
msg = p.Title
}
// The limit belongs to the headline: it is the organization's, not
// one of the fields the errors lines point at.
if limit := formatVolumeLimit(p); limit != "" {
msg = msg + ": " + limit
}
if extra := formatProblemErrors(p.Errors); extra != "" {
msg = msg + "\n" + extra
}
Expand Down Expand Up @@ -77,6 +82,30 @@ func formatProblemErrors(errors *[]gen.ProblemError) string {
return strings.Join(lines, "\n")
}

// formatVolumeLimit names the Shared Volume limit a volume-limit-exceeded
// conflict reports, with its cap and the requested value, or "" when the
// problem names none.
func formatVolumeLimit(p *gen.ProblemDetails) string {
if p.Limit == nil || *p.Limit == "" {
return ""
}
out := "limit " + *p.Limit
if p.Cap != nil {
out += fmt.Sprintf(", cap %d", *p.Cap)
}
if p.Current != nil {
out += fmt.Sprintf(", current %d", *p.Current)
}
if p.Requested != nil {
label := "requested"
if p.RequestedIsIncrement != nil && *p.RequestedIsIncrement {
label = "requested increase"
}
out += fmt.Sprintf(", %s %d", label, *p.Requested)
}
return out
}

func formatShortfall(amount *gen.MoneyAmount) string {
if amount == nil || amount.Amount == "" {
return ""
Expand Down
39 changes: 39 additions & 0 deletions internal/api/serverless/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,42 @@ func TestProblemToError_ProblemTypes(t *testing.T) {
})
}
}

func TestProblemToError_VolumeLimitStaysOnTheHeadline(t *testing.T) {
detail := "maxStoredBytes exceeds its limit"
pointer := "/files/0/size"
limit := "maxStoredBytes"
limitCap, current, requested := int64(1099511627776), int64(1095000000000), int64(10270000000)
increment := true
p := &gen.ProblemDetails{
Type: "https://docs.runware.ai/serverless/errors#volume-limit-exceeded",
Title: "Conflict",
Status: http.StatusConflict,
Detail: &detail,
Errors: &[]gen.ProblemError{
{
Detail: detail,
Pointer: &pointer,
},
},
Limit: &limit,
Cap: &limitCap,
Current: &current,
Requested: &requested,
RequestedIsIncrement: &increment,
}

err := problemToError(p, http.StatusConflict)
re, ok := errors.AsType[*transport.RunwareError](err)
if !ok {
t.Fatalf("expected *transport.RunwareError, got %T: %v", err, err)
}
headline, rest, _ := strings.Cut(re.Message, "\n")
want := detail + ": limit maxStoredBytes, cap 1099511627776, current 1095000000000, requested increase 10270000000"
if headline != want {
t.Errorf("headline = %q, want %q", headline, want)
}
if rest != " "+pointer+": "+detail {
t.Errorf("errors lines = %q, want the pointer line alone", rest)
}
}
63 changes: 17 additions & 46 deletions internal/api/serverless/volume_uploads.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,61 +81,32 @@ type volumeFilePage struct {
}

// LatestFiles reads every page of the Volume's latest files, starting again
// when the list changes between two pages. When the attempts run out on a
// later page's 400, the error keeps that problem, since the 400 may be about
// something other than a stale cursor.
// when the list changes between two pages (see latestFiles).
func (v *VolumeUploads) LatestFiles(ctx context.Context, volume put.VolumeRef) (put.FileList, error) {
var last error
for range volumeFileListAttempts {
list, changed, err := v.readFiles(ctx, volume)
if !changed {
if err != nil {
return put.FileList{}, err
}
return list, nil
}
last = err
}
return put.FileList{}, errors.Join(errVolumeFilesKeptChanging, last)
}

// readFiles reads the pages once; changed reports that the list may have
// changed under it, with the later page's 400 problem as err when that is why.
func (v *VolumeUploads) readFiles(ctx context.Context, volume put.VolumeRef) (put.FileList, bool, error) {
var list put.FileList
seen := make(map[string]struct{})
cursor := ""
for first := true; ; first = false {
digest, files, err := latestFiles(ctx, func(ctx context.Context, cursor string) (volumeFilesPage[put.VolumeFile], error) {
query := url.Values{}
query.Set("limit", strconv.Itoa(volumeFilesPageSize))
if cursor != "" {
query.Set("cursor", cursor)
}
var page volumeFilePage
err := v.do(ctx, http.MethodGet, volumePath(volume.Name, "files"), volume.Environment, query, nil, http.StatusOK, &page)
if err != nil {
// A cursor issued before an upload completed answers 400: the
// list changed, so start again from the first page.
if p, ok := errors.AsType[*put.ProblemError](err); ok && !first && p.Status == http.StatusBadRequest {
return put.FileList{}, true, err
}
return put.FileList{}, false, err
}
if first {
list.ManifestSHA256 = page.ManifestSHA256
} else if page.ManifestSHA256 != list.ManifestSHA256 {
return put.FileList{}, true, nil
}
list.Files = append(list.Files, page.Data...)
if page.NextCursor == nil || *page.NextCursor == "" {
return list, false, nil
}
cursor = *page.NextCursor
if _, dup := seen[cursor]; dup {
return put.FileList{}, false, errRepeatedVolumeCursor
}
seen[cursor] = struct{}{}
return volumeFilesPage[put.VolumeFile]{
digest: page.ManifestSHA256,
data: page.Data,
next: page.NextCursor,
}, err
}, func(err error) bool {
p, ok := errors.AsType[*put.ProblemError](err)
return ok && p.Status == http.StatusBadRequest
})
if err != nil {
return put.FileList{}, err
}
return put.FileList{
ManifestSHA256: digest,
Files: files,
}, nil
}

// KeyPairStatement reads the organization's signed key-pair statement.
Expand Down
Loading
Loading