Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions coordinator/store/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,10 +731,6 @@ func (s *PostgresStore) migrate(ctx context.Context) error {
total_prompt_tokens BIGINT NOT NULL DEFAULT 0,
total_completion_tokens BIGINT NOT NULL DEFAULT 0
)`,
`CREATE TABLE IF NOT EXISTS usage_totals_backfill_state (
id INTEGER PRIMARY KEY DEFAULT 1 CHECK (id = 1),
cutoff_id BIGINT NOT NULL
)`,

// Partial index for UsageLocationBuckets — only rows with a
// non-null request_location are ever queried.
Expand Down
11 changes: 11 additions & 0 deletions coordinator/store/postgres_usage_totals_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ func (s *PostgresStore) prepareUsageTotalsMigration(ctx context.Context) (usageT
if err := acquireUsageTotalsMigrationLock(ctx, tx); err != nil {
return usageTotalsMigrationPreparation{}, err
}
// CREATE TABLE IF NOT EXISTS is not safe when separate transactions race
// to create the same relation: PostgreSQL can reject one on its internal
// pg_type uniqueness constraint. Keep the checkpoint DDL behind the same
// advisory lock that serializes the migration state machine.
if _, err := tx.Exec(ctx, `
CREATE TABLE IF NOT EXISTS usage_totals_backfill_state (
id INTEGER PRIMARY KEY DEFAULT 1 CHECK (id = 1),
cutoff_id BIGINT NOT NULL
)`); err != nil {
return usageTotalsMigrationPreparation{}, fmt.Errorf("create backfill checkpoint table: %w", err)
}

var applied, counterExists bool
if err := tx.QueryRow(ctx,
Expand Down
65 changes: 61 additions & 4 deletions coordinator/store/postgres_usage_totals_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"os"
"sync"
"testing"
"time"
)
Expand All @@ -17,6 +18,9 @@ func TestMigrateNoUngatedUsageTotalsAggregation(t *testing.T) {
if bytes.Contains(src, []byte("INSERT INTO usage_totals")) {
t.Fatal("usage_totals aggregation must not run in the unconditional startup statement list")
}
if bytes.Contains(src, []byte("CREATE TABLE IF NOT EXISTS usage_totals_backfill_state")) {
t.Fatal("usage_totals checkpoint DDL must run under the migration advisory lock")
}
}

func TestUsageTotalsMigrationBackfillsOnceWithoutRescanningUsage(t *testing.T) {
Expand Down Expand Up @@ -180,6 +184,63 @@ func TestUsageTotalsMigrationSerializesConcurrentCoordinators(t *testing.T) {
assertUsageTotalsMigrationValues(t, first, 2, 40, 60)
}

func TestUsageTotalsMigrationSerializesCheckpointTableCreation(t *testing.T) {
databaseURL := newWithdrawableTestDatabase(t)
const contenders = 8
stores := make([]*PostgresStore, contenders)
for i := range stores {
stores[i] = newWithdrawableMigrationStore(t, databaseURL)
}
prepareUsageTotalsMigrationSchema(t, stores[0])
if _, err := stores[0].pool.Exec(context.Background(), `
INSERT INTO usage_totals (
id,
total_requests,
total_prompt_tokens,
total_completion_tokens
) VALUES (1, 7, 80, 90)`); err != nil {
t.Fatalf("seed existing counter: %v", err)
}

start := make(chan struct{})
results := make(chan usageTotalsMigrationResult, contenders)
errs := make(chan error, contenders)
var wg sync.WaitGroup
for _, store := range stores {
wg.Add(1)
go func(s *PostgresStore) {
defer wg.Done()
<-start
result, err := s.applyUsageTotalsMigration(context.Background())
results <- result
errs <- err
}(store)
}
close(start)
wg.Wait()
close(results)
close(errs)

for err := range errs {
if err != nil {
t.Fatalf("concurrent checkpoint table creation: %v", err)
}
}
counts := map[usageTotalsMigrationResult]int{}
for result := range results {
counts[result]++
}
if counts[usageTotalsMigrationPreserved] != 1 ||
counts[usageTotalsMigrationSkipped] != contenders-1 {
t.Fatalf(
"concurrent results = %v, want one preserved and %d already-applied",
counts,
contenders-1,
)
}
assertUsageTotalsMigrationValues(t, stores[0], 7, 80, 90)
}

func prepareUsageTotalsMigrationSchema(t *testing.T, s *PostgresStore) {
t.Helper()
for _, statement := range []string{
Expand All @@ -198,10 +259,6 @@ func prepareUsageTotalsMigrationSchema(t *testing.T, s *PostgresStore) {
total_prompt_tokens BIGINT NOT NULL DEFAULT 0,
total_completion_tokens BIGINT NOT NULL DEFAULT 0
)`,
`CREATE TABLE usage_totals_backfill_state (
id INTEGER PRIMARY KEY DEFAULT 1 CHECK (id = 1),
cutoff_id BIGINT NOT NULL
)`,
} {
if _, err := s.pool.Exec(context.Background(), statement); err != nil {
t.Fatalf("prepare usage totals migration schema: %v", err)
Expand Down
Loading