-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimistic_integration_test.go
More file actions
214 lines (185 loc) · 8.78 KB
/
Copy pathoptimistic_integration_test.go
File metadata and controls
214 lines (185 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package executor_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/block/pg-sprite/internal/testutil"
"github.com/block/pg-sprite/pkg/dbconn"
"github.com/block/pg-sprite/pkg/executor"
"github.com/block/pg-sprite/pkg/preflight"
"github.com/block/pg-sprite/pkg/statement"
)
// budget is generous enough that an instant catalog change always fits and
// tight enough that a blocked or rewriting attempt is cancelled quickly.
var budget = executor.Budget{LockTimeout: 500 * time.Millisecond, StatementTimeout: 2 * time.Second}
func newPool(t *testing.T) (*pgxpool.Pool, string) {
t.Helper()
pool, err := dbconn.NewPool(t.Context(), dbconn.Config{URL: testutil.StartPostgres(t)})
require.NoError(t, err)
t.Cleanup(pool.Close)
return pool, testutil.NewSchema(t, pool)
}
func mustPreflight(t *testing.T, pool *pgxpool.Pool, schema, table string) preflight.PreflightedTable {
t.Helper()
pt, err := preflight.CheckTable(t.Context(), pool, schema, table, 1<<30)
require.NoError(t, err)
return pt
}
func mustParse(t *testing.T, sql string) statement.Statement {
t.Helper()
st, err := statement.ParseOne(sql)
require.NoError(t, err)
return st
}
func columnType(t *testing.T, pool *pgxpool.Pool, schema, table, column string) string {
t.Helper()
var typ string
err := pool.QueryRow(t.Context(),
`SELECT data_type FROM information_schema.columns
WHERE table_schema = $1 AND table_name = $2 AND column_name = $3`,
schema, table, column).Scan(&typ)
require.NoError(t, err)
return typ
}
func TestExecuteNativeCommitsInstantChange(t *testing.T) {
pool, schema := newPool(t)
_, err := pool.Exec(t.Context(), fmt.Sprintf("CREATE TABLE %s.t (id int PRIMARY KEY)", schema))
require.NoError(t, err)
pt := mustPreflight(t, pool, schema, "t")
st := mustParse(t, fmt.Sprintf("ALTER TABLE %s.t ADD COLUMN age int NOT NULL DEFAULT 0", schema))
require.NoError(t, executor.ExecuteNative(t.Context(), pool, pt, st, budget, executor.DefaultRetryPolicy()))
assert.Equal(t, "integer", columnType(t, pool, schema, "t", "age"), "the committed change must be visible")
}
func TestExecuteNativeCancelsWhenLockBlocked(t *testing.T) {
pool, schema := newPool(t)
_, err := pool.Exec(t.Context(), fmt.Sprintf("CREATE TABLE %s.t (id int PRIMARY KEY)", schema))
require.NoError(t, err)
pt := mustPreflight(t, pool, schema, "t")
// A second session holds ACCESS EXCLUSIVE for the whole test, so the
// attempt can never be granted its lock.
blocker, err := pool.Begin(t.Context())
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, blocker.Rollback(context.WithoutCancel(t.Context())))
})
_, err = blocker.Exec(t.Context(), fmt.Sprintf("LOCK TABLE %s.t IN ACCESS EXCLUSIVE MODE", schema))
require.NoError(t, err)
st := mustParse(t, fmt.Sprintf("ALTER TABLE %s.t ADD COLUMN age int", schema))
err = executor.ExecuteNative(t.Context(), pool, pt, st, budget, executor.DefaultRetryPolicy())
var budgetErr *executor.BudgetError
require.ErrorAs(t, err, &budgetErr)
assert.Equal(t, executor.CauseLock, budgetErr.Cause)
assert.Equal(t, budget.LockTimeout, budgetErr.Budget)
assert.Equal(t, executor.DefaultRetryPolicy().MaxAttempts, budgetErr.Attempts,
"an actually blocked DDL must exhaust the bounded retry policy")
}
func TestExecuteNativeCancelsRewriteAndLeavesTableUnchanged(t *testing.T) {
pool, schema := newPool(t)
_, err := pool.Exec(t.Context(), fmt.Sprintf("CREATE TABLE %s.t (id int PRIMARY KEY, v text)", schema))
require.NoError(t, err)
// Enough rows that a full table rewrite cannot finish inside a
// millisecond-scale statement budget.
_, err = pool.Exec(t.Context(), fmt.Sprintf(
"INSERT INTO %s.t SELECT g, repeat('x', 100) FROM generate_series(1, 300000) g", schema))
require.NoError(t, err)
pt := mustPreflight(t, pool, schema, "t")
// int -> bigint forces a full table rewrite under ACCESS EXCLUSIVE.
st := mustParse(t, fmt.Sprintf("ALTER TABLE %s.t ALTER COLUMN id TYPE bigint", schema))
tight := executor.Budget{LockTimeout: budget.LockTimeout, StatementTimeout: 50 * time.Millisecond}
err = executor.ExecuteNative(t.Context(), pool, pt, st, tight, executor.DefaultRetryPolicy())
var budgetErr *executor.BudgetError
require.ErrorAs(t, err, &budgetErr)
assert.Equal(t, executor.CauseStatement, budgetErr.Cause)
// The cancelled attempt must leave schema and data untouched.
assert.Equal(t, "integer", columnType(t, pool, schema, "t", "id"))
var count int
require.NoError(t, pool.QueryRow(t.Context(),
fmt.Sprintf("SELECT count(*) FROM %s.t", schema)).Scan(&count))
assert.Equal(t, 300000, count)
}
func TestExecuteNativeSurfacesOperationalErrors(t *testing.T) {
pool, schema := newPool(t)
_, err := pool.Exec(t.Context(), fmt.Sprintf("CREATE TABLE %s.t (id int PRIMARY KEY)", schema))
require.NoError(t, err)
pt := mustPreflight(t, pool, schema, "t")
// Dropping a column that does not exist is a plain SQL error, not a
// budget overrun.
st := mustParse(t, fmt.Sprintf("ALTER TABLE %s.t DROP COLUMN nope", schema))
err = executor.ExecuteNative(t.Context(), pool, pt, st, budget, executor.DefaultRetryPolicy())
require.Error(t, err)
var budgetErr *executor.BudgetError
assert.NotErrorAs(t, err, &budgetErr)
}
// Sub-millisecond budgets are as unbounded as zero ones: they truncate to
// PostgreSQL's 0ms, which disables the corresponding limit entirely.
func TestExecuteNativeRejectsUnboundedBudgets(t *testing.T) {
pool, schema := newPool(t)
_, err := pool.Exec(t.Context(), fmt.Sprintf("CREATE TABLE %s.t (id int PRIMARY KEY)", schema))
require.NoError(t, err)
pt := mustPreflight(t, pool, schema, "t")
st := mustParse(t, fmt.Sprintf("ALTER TABLE %s.t ADD COLUMN age int", schema))
unbounded := map[string]executor.Budget{
"zero lock": {LockTimeout: 0, StatementTimeout: time.Second},
"zero statement": {LockTimeout: time.Second, StatementTimeout: 0},
"sub-millisecond lock": {LockTimeout: 500 * time.Microsecond, StatementTimeout: time.Second},
"sub-millisecond statement": {LockTimeout: time.Second, StatementTimeout: 999 * time.Microsecond},
}
for name, b := range unbounded {
t.Run(name, func(t *testing.T) {
require.Error(t, executor.ExecuteNative(t.Context(), pool, pt, st, b, executor.DefaultRetryPolicy()))
})
}
// The smallest representable budget is valid and does not serialize to 0:
// a 1ms lock timeout must still cancel a blocked attempt rather than
// disabling the limit.
blocker, err := pool.Begin(t.Context())
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, blocker.Rollback(context.WithoutCancel(t.Context())))
})
_, err = blocker.Exec(t.Context(), fmt.Sprintf("LOCK TABLE %s.t IN ACCESS EXCLUSIVE MODE", schema))
require.NoError(t, err)
err = executor.ExecuteNative(t.Context(), pool, pt, st, executor.Budget{LockTimeout: time.Millisecond, StatementTimeout: time.Second}, executor.DefaultRetryPolicy())
var budgetErr *executor.BudgetError
require.ErrorAs(t, err, &budgetErr)
assert.Equal(t, executor.CauseLock, budgetErr.Cause)
}
// INV: ST-7 — a preflight proof for one table can never execute a statement
// against another, and a statement without a table target never executes.
func TestExecuteNativeRefusesTargetMismatch(t *testing.T) {
pool, schema := newPool(t)
for _, ddl := range []string{
fmt.Sprintf("CREATE TABLE %s.t (id int PRIMARY KEY)", schema),
fmt.Sprintf("CREATE TABLE %s.victim (id int PRIMARY KEY)", schema),
} {
_, err := pool.Exec(t.Context(), ddl)
require.NoError(t, err)
}
pt := mustPreflight(t, pool, schema, "t")
t.Run("statement targets a different table", func(t *testing.T) {
st := mustParse(t, fmt.Sprintf("ALTER TABLE %s.victim ADD COLUMN a int", schema))
err := executor.ExecuteNative(t.Context(), pool, pt, st, budget, executor.DefaultRetryPolicy())
require.ErrorIs(t, err, executor.ErrInvariantViolation)
var n int
require.NoError(t, pool.QueryRow(t.Context(),
`SELECT count(*) FROM information_schema.columns
WHERE table_schema = $1 AND table_name = 'victim' AND column_name = 'a'`, schema).Scan(&n))
assert.Zero(t, n, "the refused statement must never reach the database")
})
t.Run("unqualified statement does not match a qualified proof", func(t *testing.T) {
// Fail-closed: the proof verified schema.t, the statement names a
// bare t that search_path could resolve elsewhere.
st := mustParse(t, "ALTER TABLE t ADD COLUMN a int")
err := executor.ExecuteNative(t.Context(), pool, pt, st, budget, executor.DefaultRetryPolicy())
require.ErrorIs(t, err, executor.ErrInvariantViolation)
})
t.Run("statement without a table target", func(t *testing.T) {
st := mustParse(t, "CREATE TABLE elsewhere (id int)")
err := executor.ExecuteNative(t.Context(), pool, pt, st, budget, executor.DefaultRetryPolicy())
require.ErrorIs(t, err, executor.ErrInvariantViolation)
})
}