mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-14 07:12:03 +00:00
feat: reconcile Agones allocations durably
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
// Package allocator coordinates provider allocation with durable control-plane
|
||||
// state. It does not expose an endpoint until both boundaries succeed.
|
||||
package allocator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/cosmic-clash/cosmic-clash/server/agones"
|
||||
"github.com/cosmic-clash/cosmic-clash/server/domain"
|
||||
)
|
||||
|
||||
type Provider interface {
|
||||
Allocate(context.Context, domain.AllocationRequest, map[string]string, time.Time) (agones.AllocatedServer, error)
|
||||
}
|
||||
|
||||
type Durable interface {
|
||||
RecordProviderAllocation(context.Context, domain.Allocation, time.Time) (domain.Allocation, error)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
Provider Provider
|
||||
Durable Durable
|
||||
Now func() time.Time
|
||||
}
|
||||
|
||||
func (s Service) Allocate(ctx context.Context, request domain.AllocationRequest, labels map[string]string) (agones.AllocatedServer, error) {
|
||||
if s.Provider == nil || s.Durable == nil || s.Now == nil {
|
||||
return agones.AllocatedServer{}, errNotConfigured
|
||||
}
|
||||
now := s.Now()
|
||||
result, err := s.Provider.Allocate(ctx, request, labels, now)
|
||||
if err != nil {
|
||||
return agones.AllocatedServer{}, err
|
||||
}
|
||||
if _, err := s.Durable.RecordProviderAllocation(ctx, result.Allocation, now); err != nil {
|
||||
return agones.AllocatedServer{}, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
var errNotConfigured = &configurationError{}
|
||||
|
||||
type configurationError struct{}
|
||||
|
||||
func (*configurationError) Error() string { return "allocator service is not configured" }
|
||||
@@ -0,0 +1,54 @@
|
||||
package allocator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cosmic-clash/cosmic-clash/server/agones"
|
||||
"github.com/cosmic-clash/cosmic-clash/server/domain"
|
||||
)
|
||||
|
||||
type providerSpy struct {
|
||||
calls int
|
||||
result agones.AllocatedServer
|
||||
err error
|
||||
}
|
||||
|
||||
func (p *providerSpy) Allocate(_ context.Context, _ domain.AllocationRequest, _ map[string]string, _ time.Time) (agones.AllocatedServer, error) {
|
||||
p.calls++
|
||||
return p.result, p.err
|
||||
}
|
||||
|
||||
type durableSpy struct {
|
||||
calls int
|
||||
allocation domain.Allocation
|
||||
err error
|
||||
}
|
||||
|
||||
func (d *durableSpy) RecordProviderAllocation(_ context.Context, allocation domain.Allocation, _ time.Time) (domain.Allocation, error) {
|
||||
d.calls++
|
||||
d.allocation = allocation
|
||||
return allocation, d.err
|
||||
}
|
||||
|
||||
func TestServiceDurablyRecordsProviderAllocationBeforeReturning(t *testing.T) {
|
||||
provider := &providerSpy{result: agones.AllocatedServer{Allocation: domain.Allocation{AllocationID: "a", MatchID: "m", ServerID: "gs", State: domain.ServerAllocated}, Endpoint: "127.0.0.1:7777"}}
|
||||
durable := &durableSpy{}
|
||||
service := Service{Provider: provider, Durable: durable, Now: func() time.Time { return time.Unix(1000, 0) }}
|
||||
result, err := service.Allocate(context.Background(), domain.AllocationRequest{AllocationID: "a", MatchID: "m", Region: "EU", Build: "b", Protocol: 1, Transport: "enet"}, map[string]string{"region": "EU"})
|
||||
if err != nil || result.Endpoint == "" || durable.calls != 1 || durable.allocation.ServerID != "gs" {
|
||||
t.Fatalf("result=%+v err=%v durable=%+v", result, err, durable)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceDoesNotReturnProviderResultAfterDurableFailure(t *testing.T) {
|
||||
provider := &providerSpy{result: agones.AllocatedServer{Allocation: domain.Allocation{AllocationID: "a", State: domain.ServerAllocated}, Endpoint: "127.0.0.1:7777"}}
|
||||
durable := &durableSpy{err: errors.New("database unavailable")}
|
||||
service := Service{Provider: provider, Durable: durable, Now: func() time.Time { return time.Unix(1000, 0) }}
|
||||
result, err := service.Allocate(context.Background(), domain.AllocationRequest{AllocationID: "a", MatchID: "m", Region: "EU", Build: "b", Protocol: 1, Transport: "enet"}, map[string]string{"region": "EU"})
|
||||
if err == nil || result.Endpoint != "" || durable.calls != 1 {
|
||||
t.Fatalf("result=%+v err=%v calls=%d", result, err, durable.calls)
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,14 @@ const SelectAllocationSQL = `SELECT allocation_id, match_id, server_id, region,
|
||||
protocol_version, transport, allocated_at, request_digest
|
||||
FROM allocations WHERE allocation_id = $1`
|
||||
|
||||
const ProviderServerClaimSQL = `UPDATE game_servers SET state = 'ALLOCATED', updated_at = $6
|
||||
WHERE server_id = $1 AND state = 'READY' AND region = $2 AND build = $3
|
||||
AND protocol_version = $4 AND transport = $5
|
||||
RETURNING server_id`
|
||||
|
||||
const ServerAllocationConflictSQL = `SELECT allocation_id FROM allocations
|
||||
WHERE server_id = $1 FOR UPDATE`
|
||||
|
||||
func RegisterReadyServer(ctx context.Context, db *sql.DB, server domain.ReadyServer, now time.Time) error {
|
||||
if db == nil || server.ServerID == "" || (server.Region != "EU" && server.Region != "NA") || server.Build == "" || server.Protocol <= 0 || (server.Transport != "enet" && server.Transport != "steam_sdr") || server.State != domain.ServerReady || now.IsZero() {
|
||||
return fmt.Errorf("invalid ready server registration")
|
||||
@@ -46,7 +54,7 @@ func RegisterReadyServer(ctx context.Context, db *sql.DB, server domain.ReadySer
|
||||
}
|
||||
|
||||
func ClaimAllocation(ctx context.Context, db *sql.DB, request domain.AllocationRequest, now time.Time) (domain.Allocation, error) {
|
||||
if db == nil || request.AllocationID == "" || request.MatchID == "" || (request.Region != "EU" && request.Region != "NA") || request.Build == "" || request.Protocol <= 0 || (request.Transport != "enet" && request.Transport != "steam_sdr") || now.IsZero() {
|
||||
if !validAllocationInput(db, request, now) {
|
||||
return domain.Allocation{}, domain.ErrAllocationInput
|
||||
}
|
||||
digest := allocationRequestDigest(request)
|
||||
@@ -80,6 +88,57 @@ func ClaimAllocation(ctx context.Context, db *sql.DB, request domain.AllocationR
|
||||
return allocation, err
|
||||
}
|
||||
|
||||
// RecordProviderAllocation reconciles a provider-side Agones claim with the
|
||||
// durable registry. It is deliberately separate from ClaimAllocation because
|
||||
// Agones has already selected the server; no client-facing assignment may use
|
||||
// the result until this exact tuple is durably recorded.
|
||||
func RecordProviderAllocation(ctx context.Context, db *sql.DB, allocation domain.Allocation, now time.Time) (domain.Allocation, error) {
|
||||
request := domain.AllocationRequest{AllocationID: allocation.AllocationID, MatchID: allocation.MatchID, Region: allocation.Region, Build: allocation.Build, Protocol: allocation.Protocol, Transport: allocation.Transport}
|
||||
if !validAllocationInput(db, request, now) || allocation.State != domain.ServerAllocated || allocation.ServerID == "" {
|
||||
return domain.Allocation{}, domain.ErrAllocationInput
|
||||
}
|
||||
digest := allocationRequestDigest(request)
|
||||
var recorded domain.Allocation
|
||||
err := RunSerializable(ctx, db, DefaultSerializableAttempts, func(ctx context.Context, tx *sql.Tx) error {
|
||||
var prior domain.Allocation
|
||||
var priorDigest []byte
|
||||
err := tx.QueryRowContext(ctx, SelectAllocationSQL, allocation.AllocationID).Scan(&prior.AllocationID, &prior.MatchID, &prior.ServerID, &prior.Region, &prior.Build, &prior.Protocol, &prior.Transport, &prior.AllocatedAt, &priorDigest)
|
||||
if err == nil {
|
||||
if !bytes.Equal(priorDigest, digest[:]) || prior.ServerID != allocation.ServerID {
|
||||
return domain.ErrConflict
|
||||
}
|
||||
recorded = prior
|
||||
recorded.State = domain.ServerAllocated
|
||||
return nil
|
||||
}
|
||||
if err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
var existing string
|
||||
if err := tx.QueryRowContext(ctx, ServerAllocationConflictSQL, allocation.ServerID).Scan(&existing); err == nil {
|
||||
return domain.ErrConflict
|
||||
} else if err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
var serverID string
|
||||
if err := tx.QueryRowContext(ctx, ProviderServerClaimSQL, allocation.ServerID, allocation.Region, allocation.Build, allocation.Protocol, allocation.Transport, now).Scan(&serverID); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return domain.ErrNoCapacity
|
||||
}
|
||||
return err
|
||||
}
|
||||
recorded = allocation
|
||||
recorded.AllocatedAt = now
|
||||
_, err = tx.ExecContext(ctx, InsertAllocationSQL, allocation.AllocationID, allocation.MatchID, serverID, allocation.Region, allocation.Build, allocation.Protocol, allocation.Transport, digest[:], now)
|
||||
return err
|
||||
})
|
||||
return recorded, err
|
||||
}
|
||||
|
||||
func validAllocationInput(db *sql.DB, request domain.AllocationRequest, now time.Time) bool {
|
||||
return db != nil && request.AllocationID != "" && request.MatchID != "" && (request.Region == "EU" || request.Region == "NA") && request.Build != "" && request.Protocol > 0 && (request.Transport == "enet" || request.Transport == "steam_sdr") && !now.IsZero()
|
||||
}
|
||||
|
||||
func allocationRequestDigest(request domain.AllocationRequest) [32]byte {
|
||||
return sha256.Sum256([]byte(fmt.Sprintf("%s\x00%s\x00%s\x00%s\x00%d\x00%s", request.AllocationID, request.MatchID, request.Region, request.Build, request.Protocol, request.Transport)))
|
||||
}
|
||||
|
||||
@@ -137,6 +137,10 @@ type queueTicketRecord struct {
|
||||
|
||||
type PostgresQueue struct{ DB *sql.DB }
|
||||
|
||||
func (q PostgresQueue) RecordProviderAllocation(ctx context.Context, allocation domain.Allocation, now time.Time) (domain.Allocation, error) {
|
||||
return RecordProviderAllocation(ctx, q.DB, allocation, now)
|
||||
}
|
||||
|
||||
const QueueProbeRecordSQL = `UPDATE queue_tickets
|
||||
SET predicted_rtt = jsonb_set(COALESCE(predicted_rtt, '{}'::jsonb), ARRAY[$2], to_jsonb($3::double precision), true)
|
||||
WHERE player_id = $1 AND state IN ('QUEUED', 'PROPOSED') AND expires_at > $4`
|
||||
|
||||
Reference in New Issue
Block a user