@hotmeshio/hotmesh 0.31.0 → 0.31.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/package.json
CHANGED
|
@@ -4,7 +4,32 @@ exports.getNotificationChannelName = exports.deploySchema = void 0;
|
|
|
4
4
|
const enums_1 = require("../../../../modules/enums");
|
|
5
5
|
const utils_1 = require("../../../../modules/utils");
|
|
6
6
|
const procedures_1 = require("./procedures");
|
|
7
|
+
// Deploys already run (or running) per client and appId. Every engine and
|
|
8
|
+
// worker init calls deploySchema, so a process booting many workers would
|
|
9
|
+
// otherwise repeat the full DDL pass once per worker.
|
|
10
|
+
const deployments = new WeakMap();
|
|
7
11
|
async function deploySchema(streamClient, appId, logger) {
|
|
12
|
+
let byApp = deployments.get(streamClient);
|
|
13
|
+
if (!byApp) {
|
|
14
|
+
byApp = new Map();
|
|
15
|
+
deployments.set(streamClient, byApp);
|
|
16
|
+
}
|
|
17
|
+
const existing = byApp.get(appId);
|
|
18
|
+
if (existing) {
|
|
19
|
+
return existing;
|
|
20
|
+
}
|
|
21
|
+
const deployment = runDeploySchema(streamClient, appId, logger);
|
|
22
|
+
byApp.set(appId, deployment);
|
|
23
|
+
//a failed deploy is retried by the next caller
|
|
24
|
+
deployment.catch(() => {
|
|
25
|
+
if (byApp.get(appId) === deployment) {
|
|
26
|
+
byApp.delete(appId);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
return deployment;
|
|
30
|
+
}
|
|
31
|
+
exports.deploySchema = deploySchema;
|
|
32
|
+
async function runDeploySchema(streamClient, appId, logger) {
|
|
8
33
|
const isPool = streamClient?.totalCount !== undefined &&
|
|
9
34
|
streamClient?.idleCount !== undefined;
|
|
10
35
|
const client = isPool ? await streamClient.connect() : streamClient;
|
|
@@ -69,7 +94,6 @@ async function deploySchema(streamClient, appId, logger) {
|
|
|
69
94
|
}
|
|
70
95
|
}
|
|
71
96
|
}
|
|
72
|
-
exports.deploySchema = deploySchema;
|
|
73
97
|
function getAdvisoryLockId(appId) {
|
|
74
98
|
return hashStringToInt(appId);
|
|
75
99
|
}
|
|
@@ -128,28 +152,24 @@ async function waitForTablesCreation(streamClient, lockId, schemaName, logger) {
|
|
|
128
152
|
throw new Error('Timeout waiting for stream table creation');
|
|
129
153
|
}
|
|
130
154
|
/**
|
|
131
|
-
* Ensure correct indexes exist on existing databases. Drops
|
|
132
|
-
*
|
|
155
|
+
* Ensure correct indexes exist on existing databases. Drops legacy
|
|
156
|
+
* indexes, rebuilds a live index only when its definition is stale,
|
|
133
157
|
* then creates the correct indexes via IF NOT EXISTS.
|
|
134
158
|
*/
|
|
135
159
|
async function ensureIndexes(client, schemaName) {
|
|
136
160
|
const engineTable = `${schemaName}.engine_streams`;
|
|
137
161
|
const workerTable = `${schemaName}.worker_streams`;
|
|
138
|
-
// Drop legacy indexes
|
|
139
|
-
//
|
|
140
|
-
//
|
|
141
|
-
// idx_*
|
|
142
|
-
//
|
|
143
|
-
//
|
|
162
|
+
// Drop legacy indexes: the PR #169 split indexes, plus redundant ones:
|
|
163
|
+
// idx_*_expired_at duplicates the partial idx_*_processed_volume for
|
|
164
|
+
// the retention purge, and idx_*_stream_name_expired_at duplicates the
|
|
165
|
+
// leading column and predicate of idx_*_message_fetch. Every index here
|
|
166
|
+
// is maintained on each message's INSERT plus two non-HOT UPDATEs
|
|
167
|
+
// (reserve, ack).
|
|
144
168
|
for (const idx of [
|
|
145
169
|
'idx_engine_streams_dequeue',
|
|
146
170
|
'idx_engine_streams_stale_reservations',
|
|
147
171
|
'idx_worker_streams_dequeue',
|
|
148
172
|
'idx_worker_streams_stale_reservations',
|
|
149
|
-
'idx_engine_streams_active_messages',
|
|
150
|
-
'idx_engine_streams_message_fetch',
|
|
151
|
-
'idx_worker_streams_active_messages',
|
|
152
|
-
'idx_worker_streams_message_fetch',
|
|
153
173
|
'idx_engine_streams_expired_at',
|
|
154
174
|
'idx_engine_stream_name_expired_at',
|
|
155
175
|
'idx_worker_streams_expired_at',
|
|
@@ -157,6 +177,24 @@ async function ensureIndexes(client, schemaName) {
|
|
|
157
177
|
]) {
|
|
158
178
|
await client.query(`DROP INDEX IF EXISTS ${schemaName}.${idx}`);
|
|
159
179
|
}
|
|
180
|
+
// Drop a live index only when its columns predate the current shape.
|
|
181
|
+
// Dropping a current index locks the stream tables and rebuilds from a
|
|
182
|
+
// full heap scan while every consumer is blocked.
|
|
183
|
+
const activeColumns = '(stream_name, priority DESC, visible_at, id)';
|
|
184
|
+
const fetchColumns = '(stream_name, priority DESC, id)';
|
|
185
|
+
const liveIndexColumns = {
|
|
186
|
+
idx_engine_streams_active_messages: activeColumns,
|
|
187
|
+
idx_engine_streams_message_fetch: fetchColumns,
|
|
188
|
+
idx_worker_streams_active_messages: activeColumns,
|
|
189
|
+
idx_worker_streams_message_fetch: fetchColumns,
|
|
190
|
+
};
|
|
191
|
+
const { rows: liveIndexes } = await client.query(`SELECT indexname, indexdef FROM pg_indexes
|
|
192
|
+
WHERE schemaname = lower($1) AND indexname = ANY($2::text[])`, [schemaName, Object.keys(liveIndexColumns)]);
|
|
193
|
+
for (const { indexname, indexdef } of liveIndexes) {
|
|
194
|
+
if (!indexdef.includes(liveIndexColumns[indexname])) {
|
|
195
|
+
await client.query(`DROP INDEX IF EXISTS ${schemaName}.${indexname}`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
160
198
|
// Recreate with priority-aware ordering for priority DESC, id ASC dequeue
|
|
161
199
|
await client.query(`
|
|
162
200
|
CREATE INDEX IF NOT EXISTS idx_engine_streams_active_messages
|