-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Reconcile registry model states on auth changes #2121
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -233,6 +233,81 @@ func (m *Manager) RefreshSchedulerEntry(authID string) { | |
| m.scheduler.upsertAuth(snapshot) | ||
| } | ||
|
|
||
| // ReconcileRegistryModelStates clears stale per-model runtime failures for | ||
| // models that are currently registered for the auth in the global model registry. | ||
| // | ||
| // This keeps the scheduler and the global registry aligned after model | ||
| // re-registration. Without this reconciliation, a model can reappear in | ||
| // /v1/models after registry refresh while the scheduler still blocks it because | ||
| // auth.ModelStates retained an older failure such as not_found or quota. | ||
| func (m *Manager) ReconcileRegistryModelStates(ctx context.Context, authID string) { | ||
| if m == nil || authID == "" { | ||
| return | ||
| } | ||
|
|
||
| supportedModels := registry.GetGlobalRegistry().GetModelsForClient(authID) | ||
| if len(supportedModels) == 0 { | ||
| return | ||
| } | ||
|
|
||
| supported := make(map[string]struct{}, len(supportedModels)) | ||
| for _, model := range supportedModels { | ||
| if model == nil { | ||
| continue | ||
| } | ||
| modelKey := canonicalModelKey(model.ID) | ||
| if modelKey == "" { | ||
| continue | ||
| } | ||
| supported[modelKey] = struct{}{} | ||
| } | ||
| if len(supported) == 0 { | ||
| return | ||
| } | ||
|
|
||
| var snapshot *Auth | ||
| now := time.Now() | ||
|
|
||
| m.mu.Lock() | ||
| auth, ok := m.auths[authID] | ||
| if ok && auth != nil && len(auth.ModelStates) > 0 { | ||
| changed := false | ||
| for modelKey, state := range auth.ModelStates { | ||
| if state == nil { | ||
| continue | ||
| } | ||
| baseModel := canonicalModelKey(modelKey) | ||
| if baseModel == "" { | ||
| baseModel = strings.TrimSpace(modelKey) | ||
| } | ||
| if _, supportedModel := supported[baseModel]; !supportedModel { | ||
| continue | ||
| } | ||
| if modelStateIsClean(state) { | ||
| continue | ||
| } | ||
| resetModelState(state, now) | ||
| changed = true | ||
| } | ||
| if changed { | ||
| updateAggregatedAvailability(auth, now) | ||
| if !hasModelError(auth, now) { | ||
| auth.LastError = nil | ||
| auth.StatusMessage = "" | ||
| auth.Status = StatusActive | ||
| } | ||
|
Comment on lines
+295
to
+299
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| auth.UpdatedAt = now | ||
| _ = m.persist(ctx, auth) | ||
|
||
| snapshot = auth.Clone() | ||
| } | ||
| } | ||
| m.mu.Unlock() | ||
|
|
||
| if m.scheduler != nil && snapshot != nil { | ||
| m.scheduler.upsertAuth(snapshot) | ||
| } | ||
| } | ||
|
|
||
| func (m *Manager) SetSelector(selector Selector) { | ||
| if m == nil { | ||
| return | ||
|
|
@@ -1735,6 +1810,22 @@ func resetModelState(state *ModelState, now time.Time) { | |
| state.UpdatedAt = now | ||
| } | ||
|
|
||
| func modelStateIsClean(state *ModelState) bool { | ||
| if state == nil { | ||
| return true | ||
| } | ||
| if state.Status != StatusActive { | ||
| return false | ||
| } | ||
| if state.Unavailable || state.StatusMessage != "" || !state.NextRetryAfter.IsZero() || state.LastError != nil { | ||
| return false | ||
| } | ||
| if state.Quota.Exceeded || state.Quota.Reason != "" || !state.Quota.NextRecoverAt.IsZero() || state.Quota.BackoffLevel != 0 { | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func updateAggregatedAvailability(auth *Auth, now time.Time) { | ||
| if auth == nil || len(auth.ModelStates) == 0 { | ||
| return | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic to determine
baseModelhas a potentially confusing fallback. IfcanonicalModelKey(modelKey)returns an empty string, it falls back tostrings.TrimSpace(modelKey). Thesupportedmap is keyed by canonical model keys. IfcanonicalModelKeyfails (returns empty), it's likely that the model key is invalid or not in a form that can be canonicalized. Falling back to the raw (but trimmed)modelKeywill likely result in a lookup miss in thesupportedmap anyway if the key contains suffixes or other non-canonical parts. This makes the code harder to reason about. It would be clearer to skip reconciliation for model keys that cannot be canonicalized.