-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriptionSetupRecovery.test.mjs
More file actions
293 lines (264 loc) · 10.1 KB
/
Copy pathsubscriptionSetupRecovery.test.mjs
File metadata and controls
293 lines (264 loc) · 10.1 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// harper-pro#642 end-to-end regression: a stuck sender-side setup gate must not leave the connection
// ping-alive forever; the receiver's watchdog must reconnect from the durable cursor and converge.
import { suite, test, before, after } from 'node:test';
import assert from 'node:assert/strict';
import { setTimeout as delay } from 'node:timers/promises';
import { join } from 'node:path';
import { startHarper, teardownHarper, getNextAvailableLoopbackAddress } from '@harperfast/integration-testing';
import { sendOperation, readLog } from './clusterShared.mjs';
process.env.HARPER_INTEGRATION_TEST_INSTALL_SCRIPT = join(import.meta.dirname, '..', '..', 'dist', 'bin', 'harper.js');
const DB = 'data';
const TABLE = 'setup_recovery';
const SETUP_TIMEOUT_MS = 3000;
const RECOVERY_TIMEOUT_MS = 30000;
function optionsFor(node, env, databases = [DB, 'system']) {
return {
config: {
analytics: { aggregatePeriod: -1 },
logging: { colors: false, stdStreams: true, console: true, level: 'warn' },
threads: { count: 1 },
replication: {
securePort: node.hostname + ':9933',
databases,
pingInterval: 1000,
pingTimeout: 3000,
},
},
env,
};
}
async function hasRecord(node, id) {
const result = await sendOperation(node, {
operation: 'search_by_id',
database: DB,
table: TABLE,
ids: [id],
get_attributes: ['id'],
}).catch(() => null);
return Array.isArray(result) && result.some((record) => record?.id === id);
}
async function waitForRecord(node, id, timeoutMs = RECOVERY_TIMEOUT_MS) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
if (await hasRecord(node, id)) return true;
await delay(250);
}
return false;
}
async function waitForLog(node, pattern, timeoutMs = RECOVERY_TIMEOUT_MS) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const log = await readLog(node);
if (pattern.test(log)) return log;
await delay(250);
}
return '';
}
function countSetupWatchdogWarnings(log, database = DB) {
return log
.split('\n')
.filter((line) => line.includes('Subscription-setup watchdog:') && line.includes(`(db: "${database}")`)).length;
}
async function socketConnected(node, database) {
const status = await sendOperation(node, { operation: 'cluster_status' });
return status.connections.some((connection) =>
connection.database_sockets?.some((socket) => socket.database === database && socket.connected === true)
);
}
async function waitForSocket(node, database, timeoutMs = RECOVERY_TIMEOUT_MS) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
if (await socketConnected(node, database).catch(() => false)) return true;
await delay(250);
}
return false;
}
async function waitForRole(node, role, timeoutMs = RECOVERY_TIMEOUT_MS) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const roles = await sendOperation(node, { operation: 'list_roles' }).catch(() => null);
if (Array.isArray(roles) && roles.some((entry) => entry?.role === role)) return true;
await delay(250);
}
return false;
}
suite('subscription setup recovery', { timeout: 120000 }, (ctx) => {
before(async () => {
const sourceCtx = { name: ctx.name, harper: { hostname: await getNextAvailableLoopbackAddress() } };
const receiverCtx = { name: ctx.name, harper: { hostname: await getNextAvailableLoopbackAddress() } };
await Promise.all([
startHarper(sourceCtx, optionsFor(sourceCtx.harper, { HARPER_TEST_SUBSCRIPTION_SETUP_STALL_ONCE_DB: DB })),
startHarper(
receiverCtx,
optionsFor(receiverCtx.harper, { HARPER_TEST_SUBSCRIPTION_SETUP_TIMEOUT_MS: String(SETUP_TIMEOUT_MS) })
),
]);
ctx.source = sourceCtx.harper;
ctx.receiver = receiverCtx.harper;
await Promise.all(
[ctx.source, ctx.receiver].map((node) =>
sendOperation(node, {
operation: 'create_table',
database: DB,
table: TABLE,
primary_key: 'id',
attributes: [{ name: 'id', type: 'ID' }],
})
)
);
});
after(async () => {
await Promise.all([ctx.source, ctx.receiver].filter(Boolean).map((node) => teardownHarper({ harper: node })));
});
test('a ping-alive setup hang reconnects and converges without a restart', async () => {
await sendOperation(ctx.receiver, {
operation: 'add_node',
rejectUnauthorized: false,
hostname: ctx.source.hostname,
authorization: ctx.receiver.admin,
});
const sourceStallLog = await waitForLog(ctx.source, /\[test\] stalling subscription setup before DB_SCHEMA/);
assert.match(sourceStallLog, /\[test\] stalling subscription setup before DB_SCHEMA/);
const recoveryLog = await waitForLog(ctx.receiver, /Subscription-setup watchdog:.*\(db: "data"\)/);
assert.match(
recoveryLog,
/Subscription-setup watchdog:.*\(db: "data"\)/,
'the receiver data watchdog must drive recovery'
);
const first = `after-setup-watchdog-${Date.now()}`;
await sendOperation(ctx.source, {
operation: 'insert',
database: DB,
table: TABLE,
records: [{ id: first }],
});
assert.equal(
await waitForRecord(ctx.receiver, first),
true,
'a record written after the setup hang must arrive over the recovered subscription'
);
assert.equal(await socketConnected(ctx.receiver, DB), true, 'the recovered data socket must be connected');
const warningsBeforeIdle = countSetupWatchdogWarnings(await readLog(ctx.receiver));
assert.ok(warningsBeforeIdle >= 1, 'at least one setup-watchdog recovery should have occurred');
await delay(SETUP_TIMEOUT_MS * 3);
const second = `after-idle-${Date.now()}`;
await sendOperation(ctx.source, {
operation: 'insert',
database: DB,
table: TABLE,
records: [{ id: second }],
});
assert.equal(await waitForRecord(ctx.receiver, second), true, 'healthy idle must not rearm setup recovery');
assert.equal(
countSetupWatchdogWarnings(await readLog(ctx.receiver)),
warningsBeforeIdle,
'healthy idle must not cause setup-watchdog reconnect churn'
);
});
});
suite('sender subscription setup recovery', { timeout: 120000 }, (ctx) => {
before(async () => {
const sourceCtx = { name: ctx.name, harper: { hostname: await getNextAvailableLoopbackAddress() } };
const receiverCtx = { name: ctx.name, harper: { hostname: await getNextAvailableLoopbackAddress() } };
await Promise.all([
startHarper(
sourceCtx,
optionsFor(sourceCtx.harper, {
HARPER_TEST_SUBSCRIPTION_SETUP_STALL_ONCE_DB: DB,
HARPER_TEST_SEND_SUBSCRIPTION_RESOLVE_TIMEOUT_MS: '2000',
})
),
startHarper(receiverCtx, optionsFor(receiverCtx.harper, { HARPER_TEST_SUBSCRIPTION_SETUP_TIMEOUT_MS: '20000' })),
]);
ctx.source = sourceCtx.harper;
ctx.receiver = receiverCtx.harper;
await Promise.all(
[ctx.source, ctx.receiver].map((node) =>
sendOperation(node, {
operation: 'create_table',
database: DB,
table: TABLE,
primary_key: 'id',
attributes: [{ name: 'id', type: 'ID' }],
})
)
);
});
after(async () => {
await Promise.all([ctx.source, ctx.receiver].filter(Boolean).map((node) => teardownHarper({ harper: node })));
});
test('the bounded sender gate closes first and the replacement subscription converges', async () => {
await sendOperation(ctx.receiver, {
operation: 'add_node',
rejectUnauthorized: false,
hostname: ctx.source.hostname,
authorization: ctx.receiver.admin,
});
const timeoutLog = await waitForLog(ctx.source, /Timed out waiting for authorization subscription setup/);
assert.match(timeoutLog, /Timed out waiting for authorization subscription setup/);
assert.doesNotMatch(
await readLog(ctx.receiver),
/Subscription-setup watchdog:.*\(db: "data"\)/,
'the longer receiver backstop must not race the sender gate timeout'
);
const id = `after-sender-timeout-${Date.now()}`;
await sendOperation(ctx.source, {
operation: 'insert',
database: DB,
table: TABLE,
records: [{ id }],
});
assert.equal(await waitForRecord(ctx.receiver, id), true, 'the sender-timeout retry must converge');
assert.doesNotMatch(
await readLog(ctx.receiver),
/Subscription-setup watchdog:.*\(db: "data"\)/,
'the receiver data watchdog must remain quiet after sender-driven convergence'
);
});
});
suite('system subscription setup recovery', { timeout: 120000 }, (ctx) => {
before(async () => {
const sourceCtx = { name: ctx.name, harper: { hostname: await getNextAvailableLoopbackAddress() } };
const receiverCtx = { name: ctx.name, harper: { hostname: await getNextAvailableLoopbackAddress() } };
await Promise.all([
startHarper(
sourceCtx,
optionsFor(sourceCtx.harper, { HARPER_TEST_SUBSCRIPTION_SETUP_STALL_ONCE_DB: 'system' }, ['system'])
),
startHarper(
receiverCtx,
optionsFor(receiverCtx.harper, { HARPER_TEST_SUBSCRIPTION_SETUP_TIMEOUT_MS: '3000' }, ['system'])
),
]);
ctx.source = sourceCtx.harper;
ctx.receiver = receiverCtx.harper;
});
after(async () => {
await Promise.all([ctx.source, ctx.receiver].filter(Boolean).map((node) => teardownHarper({ harper: node })));
});
test('an unsolicited handshake schema cannot acknowledge the stalled system request', async () => {
await sendOperation(ctx.receiver, {
operation: 'add_node',
rejectUnauthorized: false,
hostname: ctx.source.hostname,
authorization: ctx.receiver.admin,
});
assert.match(
await waitForLog(ctx.source, /\[test\] stalling subscription setup before DB_SCHEMA for db "system"/),
/\[test\] stalling subscription setup before DB_SCHEMA for db "system"/
);
assert.match(
await waitForLog(ctx.receiver, /Subscription-setup watchdog:.*\(db: "system"\)/),
/Subscription-setup watchdog:.*\(db: "system"\)/,
'the unsolicited handshake schema must not retire the correlated system request'
);
assert.equal(await waitForSocket(ctx.receiver, 'system'), true, 'the replacement system socket must connect');
const role = `after-system-setup-watchdog-${Date.now()}`;
await sendOperation(ctx.source, { operation: 'add_role', role, permission: { super_user: false } });
assert.equal(await waitForRole(ctx.receiver, role), true, 'system-table replication must converge after recovery');
assert.ok(
countSetupWatchdogWarnings(await readLog(ctx.receiver), 'system') >= 1,
'the correlated system request should trigger recovery'
);
});
});