This repository was archived by the owner on Oct 8, 2026. It is now read-only.
Repository navigation
feat: replace pooled Trino instances before voluntary node disruption #1243
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6711f74
feat: replace Trino pool instances before voluntary node disruption
benben 02b8704
fix: distinguish invalid Trino placements from scheduling waits
benben f086d39
test: update migration fixtures for trino node replacement
benben 4e31c64
fix: preserve proven node replacement through observation faults
benben 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
7 changes: 7 additions & 0 deletions
7
controlplane/configstore/migrations/000043_add_trino_node_replacement.sql
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,7 @@ | ||
| -- +goose Up | ||
| ALTER TABLE duckgres_trino_pool_instances | ||
| ADD COLUMN node_replacement_evidence JSONB; | ||
|
|
||
| -- +goose Down | ||
| ALTER TABLE duckgres_trino_pool_instances | ||
| DROP COLUMN node_replacement_evidence; |
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,33 @@ | ||
| package configstore | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "github.com/posthog/duckgres/controlplane/trinopool" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| // RecordTrinoPoolNodeReplacement preserves the first infrastructure replacement request. | ||
| // Retries and subsequent drift observations cannot rewrite the original node identity. | ||
| func (cs *ConfigStore) RecordTrinoPoolNodeReplacement(ctx context.Context, lease TrinoPoolLease, instanceID string, evidence trinopool.NodeReplacementEvidence) error { | ||
| if err := evidence.Validate(); err != nil { | ||
| return err | ||
| } | ||
| encoded, err := json.Marshal(evidence) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return cs.withPoolAuthority(ctx, lease, func(tx *gorm.DB, _ *TrinoPool) error { | ||
| var instance TrinoPoolInstance | ||
| if err := tx.Where("instance_id = ? AND pool_id = ?", instanceID, lease.PoolID).First(&instance).Error; err != nil { | ||
| return fmt.Errorf("read instance for node replacement: %w", err) | ||
| } | ||
| if instance.NodeReplacementEvidence != nil || trinopool.Phase(instance.Phase).Terminal() { | ||
| return nil | ||
| } | ||
| return tx.Model(&TrinoPoolInstance{}).Where("instance_id = ? AND pool_id = ? AND node_replacement_evidence IS NULL", instanceID, lease.PoolID). | ||
| Update("node_replacement_evidence", string(encoded)).Error | ||
| }) | ||
| } |
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,143 @@ | ||
| //go:build kubernetes | ||
|
|
||
| package controlplane | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "log/slog" | ||
| "time" | ||
|
|
||
| "github.com/posthog/duckgres/controlplane/configstore" | ||
| "github.com/posthog/duckgres/controlplane/trinogateway" | ||
| "github.com/posthog/duckgres/controlplane/trinopool" | ||
| ) | ||
|
|
||
| type trinoPoolNodeReplacementStore interface { | ||
| RecordTrinoPoolNodeReplacement(context.Context, configstore.TrinoPoolLease, string, trinopool.NodeReplacementEvidence) error | ||
| } | ||
|
|
||
| var errTrinoCandidateNodeReplacement = errors.New("candidate node requires replacement before admission") | ||
| var errTrinoCandidateNodeRetired = errors.New("candidate retired before admission because its node requires replacement") | ||
|
|
||
| func definitiveTrinoNodePlacement(err error) bool { | ||
| return errors.Is(err, errTrinoCandidateNodeReplacement) || errors.Is(err, errTrinoUnschedulableNode) || errors.Is(err, errTrinoNodePlacementInvalid) | ||
| } | ||
|
|
||
| // protectPoolNodes visits all live instances before any instance advances. | ||
| // A busy candidate cannot starve the protection of existing serving pods. | ||
| func (o *trinoPoolOperator) protectPoolNodes(ctx context.Context, instances []configstore.TrinoPoolInstance) error { | ||
| o.nodeProtectionErrors = make(map[string]error) | ||
| if o.nodeGuard == nil { | ||
| return nil | ||
| } | ||
| ctx, cancel := context.WithTimeout(ctx, 30*time.Second) | ||
| defer cancel() | ||
| o.nodeGuard.reset() | ||
| o.nodeGuard.epoch = o.lease.Epoch | ||
| store, ok := o.store.(trinoPoolNodeReplacementStore) | ||
| if !ok { | ||
| return errors.New("node disruption handling requires durable replacement storage") | ||
| } | ||
| var failures []error | ||
| start := int(o.nodeProtectionCursor % uint64(max(1, len(instances)))) | ||
| o.nodeProtectionCursor++ | ||
| for offset := range instances { | ||
| index := (start + offset) % len(instances) | ||
| instance := &instances[index] | ||
| switch trinopool.Phase(instance.Phase) { | ||
| case trinopool.PhasePending, trinopool.PhaseRetiring, trinopool.PhaseRetired, trinopool.PhaseFailureRetired, trinopool.PhaseFailedPreparing, trinopool.PhaseLost: | ||
| continue | ||
| } | ||
| evidence, err := o.nodeGuard.inspect(ctx, inventoryOf(*instance)) | ||
| if err == errTrinoUnschedulableNode && instance.NodeReplacementEvidence != nil { | ||
| err = nil | ||
| } | ||
| if len(evidence) > 0 { | ||
| // Exact positive evidence remains valid when another pod or node is unavailable. | ||
| recordErr := o.dropAuthority(store.RecordTrinoPoolNodeReplacement(ctx, o.lease, instance.InstanceID, evidence[0])) | ||
| if recordErr == nil && instance.NodeReplacementEvidence == nil { | ||
| encoded, _ := json.Marshal(evidence[0]) | ||
| value := string(encoded) | ||
| instance.NodeReplacementEvidence = &value | ||
| slog.Info("Trino pool requested voluntary node replacement.", "pool", o.config.PublicID, "instance", instance.InstanceID, "node_uid", evidence[0].NodeUID, "nodeclaim_uid", evidence[0].NodeClaimUID, "reason", evidence[0].Reason) | ||
| } | ||
| if recordErr != nil { | ||
| err = errors.Join(err, recordErr) | ||
| } | ||
| if err == nil { | ||
| for _, node := range evidence { | ||
| if err = o.nodeGuard.cordon(ctx, node, o.config.PoolID, instance.InstanceID); err != nil { | ||
| break | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if err == errTrinoPodProtectionIncomplete && (instance.Phase == string(trinopool.PhaseCreating) || instance.Phase == string(trinopool.PhasePreparing)) { | ||
| // Pending pods are normal convergence, not failed protection of serving work. | ||
| continue | ||
| } | ||
| if err != nil { | ||
|
benben marked this conversation as resolved.
|
||
| o.nodeProtectionErrors[instance.InstanceID] = err | ||
| failures = append(failures, fmt.Errorf("protect instance %s from node disruption: %w", instance.InstanceID, err)) | ||
| if o.fenced { | ||
| return errors.Join(failures...) | ||
| } | ||
| } | ||
| } | ||
| return errors.Join(failures...) | ||
| } | ||
|
|
||
| func (o *trinoPoolOperator) candidateNodeCheck(ctx context.Context, instance configstore.TrinoPoolInstance) error { | ||
| if o.nodeGuard == nil { | ||
| return nil | ||
| } | ||
| if instance.NodeReplacementEvidence != nil { | ||
| return errTrinoCandidateNodeReplacement | ||
| } | ||
| o.nodeGuard.reset() | ||
| defer o.nodeGuard.reset() | ||
| o.nodeGuard.namedClaims = true | ||
| o.nodeGuard.epoch = o.lease.Epoch | ||
| evidence, err := o.nodeGuard.inspect(ctx, inventoryOf(instance)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if len(evidence) != 0 || instance.NodeReplacementEvidence != nil { | ||
| return errTrinoCandidateNodeReplacement | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // nodeSafeAdmission resolves committed admission before inspecting new infrastructure facts. | ||
| // A failed fresh check cannot erase an admission whose response was lost. | ||
| func (o *trinoPoolOperator) nodeSafeAdmission(ctx context.Context, instance configstore.TrinoPoolInstance, request trinogateway.AdmitMemberRequest) (trinogateway.Member, error) { | ||
| if o.nodeGuard != nil { | ||
| current, err := o.gateway.GetMember(ctx, o.config.RoutingGroup, instance.InstanceID) | ||
| if err != nil { | ||
| return trinogateway.Member{}, err | ||
| } | ||
| if current.Incarnation != instance.GatewayIncarnation || current.PodUID != instance.CoordinatorPodUID || current.BootID != instance.CoordinatorBootID { | ||
| return trinogateway.Member{}, errors.New("admission read-back identity changed") | ||
| } | ||
| if current.Phase == "ACTIVE" { | ||
| return o.gateway.AdmitMember(ctx, o.config.RoutingGroup, instance.InstanceID, request) | ||
| } | ||
| if current.Phase == "RETIRING" || current.Phase == "RETIRED" { | ||
| return trinogateway.Member{}, errTrinoCandidateNodeRetired | ||
| } | ||
| if err := o.candidateNodeCheck(ctx, instance); err != nil { | ||
| if definitiveTrinoNodePlacement(err) { | ||
| _, retireErr := o.gateway.RetireMember(ctx, o.config.RoutingGroup, instance.InstanceID, trinogateway.MemberStepRequest{Step: o.step(instance.InstanceID, "retire"), ExpectedGeneration: current.Generation}) | ||
| if retireErr != nil { | ||
| return trinogateway.Member{}, retireErr | ||
| } | ||
| return trinogateway.Member{}, errTrinoCandidateNodeRetired | ||
| } | ||
| return trinogateway.Member{}, err | ||
| } | ||
| } | ||
| return o.gateway.AdmitMember(ctx, o.config.RoutingGroup, instance.InstanceID, request) | ||
| } | ||
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.