Repository navigation
fix(serverless): say why a deploy --wait rollout failed when a worker could not start #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
63995ea
fix(serverless): say why a deploy --wait rollout failed when a worker…
itsNotMyUsername 75b1655
fix(serverless): blame the newest troubled worker and forget one that…
itsNotMyUsername e73605c
Merge branch 'main' into cli-wait-preparation-reason
itsNotMyUsername f48f400
fix(serverless): name a failed start before a disk wait, and keep the…
itsNotMyUsername c28ef3b
fix(serverless): read every page of live workers in the deploy wait
itsNotMyUsername File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| package serverless | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| serverlessapi "github.com/runware/runware-cli/internal/api/serverless" | ||
| ) | ||
|
|
||
| const ( | ||
| workerStatusUnhealthy = "unhealthy" | ||
| workerStatusReady = "ready" | ||
| workerStatusBusy = "busy" | ||
| // reasonVolumeDiskShort is a worker waiting for disk for its Shared Volumes' | ||
| // files. It is `pulling` while the wait may still end, so it is trouble | ||
| // before the worker turns unhealthy. The published spec does not list | ||
| // statusReason values yet; this is the API's value for that wait. | ||
| reasonVolumeDiskShort = "volume-disk-short" | ||
|
itsNotMyUsername marked this conversation as resolved.
Resolved
|
||
| ) | ||
|
|
||
| // workerTrouble is a worker that cannot start, with the reason the API gives, | ||
| // such as a Shared Volume the worker failed to prepare (`fetch-failed`, | ||
| // `deadline-exceeded`) or an app container that keeps exiting. | ||
| type workerTrouble struct { | ||
| id, status, reason string | ||
| } | ||
|
|
||
| func (t workerTrouble) String() string { | ||
| if t.reason == "" { | ||
| return fmt.Sprintf("worker %s is %s", t.id, t.status) | ||
| } | ||
| return fmt.Sprintf("worker %s is %s: %s", t.id, t.status, t.reason) | ||
| } | ||
|
|
||
| // workerWatch remembers the latest worker a deploy wait saw in trouble. The | ||
| // reason only exists while the worker does: once a rollout gives up, the API | ||
| // stops its workers and their reason becomes the stop, so the wait has to read | ||
| // it on the way. | ||
| type workerWatch struct { | ||
| client *serverlessapi.Client | ||
| appID string | ||
| last *workerTrouble | ||
| } | ||
|
|
||
| // workersPageLimit is the most workers a list page holds, and workersMaxPages | ||
| // bounds how many pages one poll reads, so a poll stays a few requests. | ||
| const workersPageLimit serverlessapi.Limit = 100 | ||
|
|
||
| const workersMaxPages = 10 | ||
|
|
||
| // workersAllVersions is the versionId that lists the workers of every version. | ||
| const workersAllVersions = "all" | ||
|
|
||
| // observe reads the app's live workers and returns the trouble it found when it | ||
| // differs from the last one. An unhealthy worker is the cause before one still | ||
| // waiting for disk, since the rollout keeps waiting on the latter; among equals | ||
| // the newest wins, which during an update is one of the version rolling out. | ||
| // | ||
| // A cause is forgotten only when that same worker is seen serving again, so a | ||
| // worker that recovered is not blamed later. A worker that is stopping or gone | ||
| // keeps it: that is the API stopping the worker after the rollout failed, when | ||
| // its reason becomes the stop. An unreadable list changes nothing and never | ||
| // fails the wait. | ||
| func (w *workerWatch) observe(ctx context.Context) (workerTrouble, bool) { | ||
| workers, err := w.liveWorkers(ctx) | ||
| if err != nil { | ||
| return workerTrouble{}, false | ||
| } | ||
| var cause *serverlessapi.Worker | ||
| for i := range workers { | ||
| wk := &workers[i] | ||
| rank := troubleRank(wk) | ||
| if rank == 0 { | ||
| if w.last != nil && wk.Id.String() == w.last.id && isServing(wk) { | ||
| w.last = nil | ||
| } | ||
| continue | ||
| } | ||
| if cause == nil || rank > troubleRank(cause) || (rank == troubleRank(cause) && wk.CreatedAt.After(cause.CreatedAt)) { | ||
| cause = wk | ||
| } | ||
| } | ||
| if cause == nil { | ||
| return workerTrouble{}, false | ||
| } | ||
| t := workerTrouble{id: cause.Id.String(), status: string(cause.Status), reason: workerReason(cause)} | ||
| if w.last != nil && *w.last == t { | ||
| return t, false | ||
| } | ||
| w.last = &t | ||
| return t, true | ||
| } | ||
|
|
||
| // liveWorkers lists the app's live workers of every version, following the | ||
| // cursor so a worker in trouble behind a page of healthy ones is still seen. | ||
| func (w *workerWatch) liveWorkers(ctx context.Context) ([]serverlessapi.Worker, error) { | ||
| all := workersAllVersions | ||
| live := serverlessapi.WorkerStateFilterLive | ||
| limit := workersPageLimit | ||
| params := serverlessapi.ListWorkersParams{VersionId: &all, State: &live, Limit: &limit} | ||
| var workers []serverlessapi.Worker | ||
| for range workersMaxPages { | ||
| page, err := w.client.ListWorkers(ctx, w.appID, params) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| workers = append(workers, page.Data...) | ||
| if page.NextCursor == nil || *page.NextCursor == "" { | ||
| break | ||
| } | ||
| params.Cursor = page.NextCursor | ||
| } | ||
| return workers, nil | ||
| } | ||
|
|
||
| // troubleRank orders the trouble a worker is in: 2 for a failed start, 1 for a | ||
| // wait for disk, 0 for none. | ||
| func troubleRank(wk *serverlessapi.Worker) int { | ||
| switch { | ||
| case string(wk.Status) == workerStatusUnhealthy: | ||
| return 2 | ||
| case workerReason(wk) == reasonVolumeDiskShort: | ||
| return 1 | ||
| default: | ||
| return 0 | ||
| } | ||
| } | ||
|
|
||
| func isServing(wk *serverlessapi.Worker) bool { | ||
| return string(wk.Status) == workerStatusReady || string(wk.Status) == workerStatusBusy | ||
| } | ||
|
|
||
| func workerReason(wk *serverlessapi.Worker) string { | ||
| if wk.StatusReason == nil { | ||
| return "" | ||
| } | ||
| return *wk.StatusReason | ||
| } | ||
|
|
||
| // cause is the last trouble seen, or "" when the wait saw none. | ||
| func (w *workerWatch) cause() string { | ||
| if w == nil || w.last == nil { | ||
| return "" | ||
| } | ||
| return w.last.String() | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.