fix(multiplayer): recover recorded allocations

This commit is contained in:
Josh Creek
2026-09-01 10:40:10 +01:00
parent 55c46f56ec
commit 25fdc2c2c8
6 changed files with 68 additions and 12 deletions
+11 -3
View File
@@ -13,6 +13,7 @@ import (
// lease a match before returning it and fence binding by allocation ID.
type MatchClaimSource interface {
ClaimAllocatingMatch(context.Context, time.Time) (domain.AllocationRequest, bool, error)
FindProviderAllocation(context.Context, domain.AllocationRequest) (domain.Allocation, bool, error)
BindAllocatedMatch(context.Context, domain.Allocation) error
}
@@ -37,11 +38,18 @@ func (w Worker) RunOnce(ctx context.Context) (bool, error) {
if err != nil || !found {
return found, err
}
result, err := w.Service.Allocate(ctx, request, AllocationLabels(request))
allocation, recorded, err := w.Claims.FindProviderAllocation(ctx, request)
if err != nil {
return true, fmt.Errorf("allocate claimed match %s: %w", request.MatchID, err)
return true, fmt.Errorf("recover allocation for match %s: %w", request.MatchID, err)
}
if err := w.Claims.BindAllocatedMatch(ctx, result.Allocation); err != nil {
if !recorded {
result, err := w.Service.Allocate(ctx, request, AllocationLabels(request))
if err != nil {
return true, fmt.Errorf("allocate claimed match %s: %w", request.MatchID, err)
}
allocation = result.Allocation
}
if err := w.Claims.BindAllocatedMatch(ctx, allocation); err != nil {
return true, fmt.Errorf("bind allocated match %s: %w", request.MatchID, err)
}
return true, nil
+23 -5
View File
@@ -12,11 +12,17 @@ import (
)
type matchClaimSpy struct {
request domain.AllocationRequest
found bool
err error
bound domain.Allocation
bindErr error
request domain.AllocationRequest
found bool
err error
recorded domain.Allocation
recordErr error
bound domain.Allocation
bindErr error
}
func (s *matchClaimSpy) FindProviderAllocation(_ context.Context, _ domain.AllocationRequest) (domain.Allocation, bool, error) {
return s.recorded, s.recorded.AllocationID != "", s.recordErr
}
func (s *matchClaimSpy) ClaimAllocatingMatch(_ context.Context, _ time.Time) (domain.AllocationRequest, bool, error) {
@@ -51,6 +57,18 @@ func TestWorkerRetainsClaimWhenProviderOutcomeIsAmbiguous(t *testing.T) {
}
}
func TestWorkerRecoversDurableProviderAllocationWithoutCallingProvider(t *testing.T) {
request := domain.AllocationRequest{AllocationID: "allocation-1", MatchID: "match-1", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet"}
recorded := domain.Allocation{AllocationID: request.AllocationID, MatchID: request.MatchID, ServerID: "server-1", Region: request.Region, Build: request.Build, Protocol: request.Protocol, Transport: request.Transport, State: domain.ServerAllocated}
claims := &matchClaimSpy{request: request, found: true, recorded: recorded}
provider := &providerSpy{}
worker := Worker{Claims: claims, Service: Service{Provider: provider, Durable: &durableSpy{}, Now: func() time.Time { return time.Unix(1_000, 0) }}, Now: func() time.Time { return time.Unix(1_000, 0) }}
processed, err := worker.RunOnce(context.Background())
if err != nil || !processed || provider.calls != 0 || claims.bound != recorded {
t.Fatalf("processed=%t err=%v provider=%d bound=%+v", processed, err, provider.calls, claims.bound)
}
}
func TestWorkerDoesNothingWhenNoDurableMatchIsAvailable(t *testing.T) {
claims := &matchClaimSpy{}
worker := Worker{Claims: claims, Now: func() time.Time { return time.Unix(1_000, 0) }}
+4
View File
@@ -20,6 +20,10 @@ func (s AllocatingMatchClaims) ClaimAllocatingMatch(ctx context.Context, now tim
return item.Request, found, err
}
func (s AllocatingMatchClaims) FindProviderAllocation(ctx context.Context, request domain.AllocationRequest) (domain.Allocation, bool, error) {
return FindProviderAllocation(ctx, s.DB, request)
}
func (s AllocatingMatchClaims) BindAllocatedMatch(ctx context.Context, allocation domain.Allocation) error {
return BindAllocatedMatch(ctx, s.DB, allocation)
}
+25
View File
@@ -1,6 +1,7 @@
package store
import (
"bytes"
"context"
"database/sql"
"fmt"
@@ -47,6 +48,30 @@ const ReleaseAllocatedMatchClaimSQL = `UPDATE matches
SET allocation_id = NULL, allocation_claimed_at = NULL
WHERE match_id = $1 AND state = 'ALLOCATING' AND allocation_id = $2 AND server_id IS NULL`
// FindProviderAllocation verifies whether a recovered lease has already
// crossed the durable provider boundary. A worker can then bind it without
// issuing a second external allocation request after a crash.
func FindProviderAllocation(ctx context.Context, db *sql.DB, request domain.AllocationRequest) (domain.Allocation, bool, error) {
if db == nil || request.AllocationID == "" || request.MatchID == "" || request.Region == "" || request.Build == "" || request.Protocol <= 0 || (request.Transport != "enet" && request.Transport != "steam_sdr") {
return domain.Allocation{}, false, fmt.Errorf("invalid provider allocation lookup")
}
var allocation domain.Allocation
var digest []byte
err := db.QueryRowContext(ctx, SelectAllocationSQL, request.AllocationID).Scan(&allocation.AllocationID, &allocation.MatchID, &allocation.ServerID, &allocation.Region, &allocation.Build, &allocation.Protocol, &allocation.Transport, &allocation.AllocatedAt, &digest)
if err == sql.ErrNoRows {
return domain.Allocation{}, false, nil
}
if err != nil {
return domain.Allocation{}, false, err
}
want := allocationRequestDigest(request)
if !bytes.Equal(digest, want[:]) || allocation.MatchID != request.MatchID || allocation.Region != request.Region || allocation.Build != request.Build || allocation.Protocol != request.Protocol || allocation.Transport != request.Transport {
return domain.Allocation{}, false, domain.ErrConflict
}
allocation.State = domain.ServerAllocated
return allocation, true, nil
}
// ClaimAllocatingMatch returns one durable provider work item. The fixed
// allocation ID is retained across a lease recovery, allowing every later
// reconciliation step to reject a different server for the same match.