-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote.ts
More file actions
405 lines (367 loc) · 12.4 KB
/
remote.ts
File metadata and controls
405 lines (367 loc) · 12.4 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import {
PgIdentifier,
type Postgres,
PostgresVersion,
Statistics,
StatisticsMode,
} from "@query-doctor/core";
import { type Connectable } from "../sync/connectable.ts";
import { DumpCommand, RestoreCommand } from "../sync/schema-link.ts";
import { ConnectionManager } from "../sync/connection-manager.ts";
import { ExtensionNotInstalledError } from "../sync/errors.ts";
import { type RecentQuery } from "../sql/recent-query.ts";
import { type Op } from "jsondiffpatch/formatters/jsonpatch";
import { type FullSchema } from "../sync/schema_differ.ts";
import { type RemoteSyncFullSchemaResponse } from "./remote.dto.ts";
import { QueryOptimizer } from "./query-optimizer.ts";
import { EventEmitter } from "node:events";
import { log } from "../log.ts";
import { QueryLoader } from "./query-loader.ts";
import { SchemaLoader } from "./schema-loader.ts";
type RemoteEvents = {
dumpLog: [line: string];
restoreLog: [line: string];
};
/**
* Represents a db for doing optimization work.
* We only maintain one instance of this class as we only do
* optimization against one physical postgres database.
* But potentially more logical databases in the future.
*
* `Remote` only concerns itself with the remote it's doing optimization
* against. It does not deal with the source in any way aside from running sync
*/
export class Remote extends EventEmitter<RemoteEvents> {
static readonly baseDbName = PgIdentifier.fromString("postgres");
static readonly optimizingDbName = PgIdentifier.fromString(
"optimizing_db",
);
/* Threshold that we determine is "too few rows" for Postgres to start using indexes
* and not defaulting to table scan.
*/
private static readonly STATS_ROWS_THRESHOLD = 5_000;
readonly optimizer: QueryOptimizer;
/**
* We have to juggle 2 different connections to the Remote
*
* 1 -> connection to `/postgres` where we manage other databases.
* this pool stays connected long-term. That's this variable
*
* 2 -> connections to {@link Remote.databaseName}. This connection pool is
* destroyed and re-created on each successful sync along with the db itself
*/
private baseDbURL: Connectable;
/** The URL of the optimizing db */
private readonly optimizingDbUDRL: Connectable;
private isPolling = false;
private queryLoader?: QueryLoader;
private schemaLoader?: SchemaLoader;
private pgStatStatementsStatus: PgStatStatementsStatus =
PgStatStatementsStatus.Unknown;
constructor(
/** This has to be a local url. Very bad things will happen if this is a remote URL */
targetURL: Connectable,
private readonly manager: ConnectionManager,
/** The manager for ONLY the source db connections */
private readonly sourceManager: ConnectionManager = ConnectionManager
.forRemoteDatabase(),
) {
super();
this.baseDbURL = targetURL.withDatabaseName(Remote.baseDbName);
this.optimizingDbUDRL = targetURL.withDatabaseName(Remote.optimizingDbName);
this.optimizer = new QueryOptimizer(manager, this.optimizingDbUDRL);
}
async syncFrom(
source: Connectable,
statsStrategy: StatisticsStrategy = { type: "pullFromSource" },
): Promise<
{
meta: { version?: string; inferredStatsStrategy?: InferredStatsStrategy };
schema: RemoteSyncFullSchemaResponse;
recentQueriesError?: {
type: "extension_not_installed";
extensionName: string;
};
}
> {
await this.resetDatabase();
// First batch: get schema and other info in parallel (needed for stats decision)
const [
restoreResult,
recentQueries,
fullSchema,
databaseInfo,
] = await Promise
.allSettled([
// This potentially creates a lot of connections to the source
this.pipeSchema(this.optimizingDbUDRL, source),
this.getRecentQueries(source),
this.getFullSchema(source),
this.getDatabaseInfo(source),
]);
if (fullSchema.status === "fulfilled") {
this.schemaLoader?.update(fullSchema.value);
}
// Second: resolve stats strategy using table list from schema
const tables = fullSchema.status === "fulfilled"
? fullSchema.value.tables
: [];
const statsResult = await this.resolveStatistics(
source,
statsStrategy,
tables,
);
const pg = this.manager.getOrCreateConnection(
this.optimizingDbUDRL,
);
let queries: RecentQuery[] = [];
let recentQueriesError: {
type: "extension_not_installed";
extensionName: string;
} | undefined;
if (recentQueries.status === "fulfilled") {
queries = recentQueries.value;
} else if (recentQueries.reason instanceof ExtensionNotInstalledError) {
recentQueriesError = {
type: "extension_not_installed",
extensionName: recentQueries.reason.extension,
};
}
await this.onSuccessfulSync(
pg,
source,
queries,
statsResult.mode,
);
return {
meta: {
version: databaseInfo.status === "fulfilled"
? databaseInfo.value.serverVersion
: undefined,
inferredStatsStrategy: statsResult.strategy,
},
schema: fullSchema.status === "fulfilled"
? { type: "ok", value: fullSchema.value }
: {
type: "error",
error: fullSchema.reason instanceof Error
? fullSchema.reason.message
: "Unknown error",
},
recentQueriesError,
};
}
async getStatus() {
const queries = this.optimizer.getQueries();
const disabledIndexes = this.optimizer.getDisabledIndexes();
const [diffs] = await Promise.allSettled([
this.schemaLoader?.poll().then(
(results) => results.diffs,
(error) => {
log.error("Failed to poll schema", "remote");
throw error;
},
) ??
[] as Op[], /* no panic in case schemaLoader has not loaded in yet */
this.pollQueriesOnce().catch((error) => {
log.error("Failed to poll queries", "remote");
throw error;
}),
]);
return {
queries,
diffs,
disabledIndexes,
pgStatStatementsNotInstalled:
this.pgStatStatementsStatus === PgStatStatementsStatus.NotInstalled,
};
}
/**
* Runs a single poll of pg_stat_statements if
* there isn't already an in-flight request
*/
private async pollQueriesOnce() {
if (this.queryLoader && !this.isPolling) {
try {
this.isPolling = true;
await this.queryLoader.poll();
} finally {
this.isPolling = false;
}
}
}
/**
* Drops and recreates the {@link Remote.optimizingDbName} db.
*
* TODO: allow juggling multiple databases in the future
*/
private async resetDatabase(): Promise<void> {
const databaseName = Remote.optimizingDbName;
log.info(`Resetting internal database: ${databaseName}`, "remote");
const baseDb = this.manager.getOrCreateConnection(this.baseDbURL);
// these cannot be run in the same `exec` block as that implicitly creates transactions
await baseDb.exec(
// drop database does not allow parameterization
`drop database if exists ${databaseName} with (force);`,
);
await baseDb.exec(`create database ${databaseName};`);
}
private async pipeSchema(
target: Connectable,
source: Connectable,
): Promise<void> {
const dump = DumpCommand.spawn(source, "native-postgres");
// is copying up events like this a good idea?
dump.on("dump", (data) => {
this.emit("dumpLog", data);
});
dump.on("restore", (data) => {
this.emit("restoreLog", data);
});
const restore = RestoreCommand.spawn(target);
const { dump: dumpResult, restore: restoreResult } = await dump.pipeTo(
restore,
);
if (!dumpResult.status.success) {
throw new Error(
`Dump failed with status ${dumpResult.status.code}`,
);
}
if (restoreResult && !restoreResult.status.success) {
throw new Error(
`Restore failed with status ${restoreResult.status.code}`,
);
}
}
private async resolveStatistics(
source: Connectable,
strategy: StatisticsStrategy,
tables: { schemaName: PgIdentifier; tableName: PgIdentifier }[],
): Promise<StatsResult> {
if (strategy.type === "static") {
// Static strategy doesn't go through inference
return { mode: strategy.stats, strategy: "fromSource" };
}
return this.decideStatsStrategy(source, tables);
}
private async decideStatsStrategy(
source: Connectable,
tables: { schemaName: PgIdentifier; tableName: PgIdentifier }[],
): Promise<StatsResult> {
const connector = this.sourceManager.getConnectorFor(source);
const totalRows = await connector.getTotalRowCount(tables);
if (totalRows < Remote.STATS_ROWS_THRESHOLD) {
log.info(
`Total rows (${totalRows}) below threshold, using default 10k stats`,
"remote",
);
return { mode: Statistics.defaultStatsMode, strategy: "10k" };
}
log.info(
`Total rows (${totalRows}) above threshold, pulling source stats`,
"remote",
);
return { mode: await this.dumpSourceStats(source), strategy: "fromSource" };
}
private async dumpSourceStats(source: Connectable): Promise<StatisticsMode> {
const pg = this.sourceManager.getOrCreateConnection(
source,
);
const stats = await Statistics.dumpStats(
pg,
PostgresVersion.parse("17"),
"full",
);
return { kind: "fromStatisticsExport", source: { kind: "inline" }, stats };
}
private async getRecentQueries(
source: Connectable,
): Promise<RecentQuery[]> {
const connector = this.sourceManager.getConnectorFor(source);
return await connector.getRecentQueries();
}
private getFullSchema(source: Connectable): Promise<FullSchema> {
const connector = this.sourceManager.getConnectorFor(source);
return connector.getSchema();
}
private getDatabaseInfo(source: Connectable) {
const connector = this.sourceManager.getConnectorFor(source);
return connector.getDatabaseInfo();
}
async resetPgStatStatements(source: Connectable): Promise<void> {
const connector = this.sourceManager.getConnectorFor(source);
await connector.resetPgStatStatements();
this.optimizer.restart({ clearQueries: true });
}
/**
* Process a successful sync and run any potential cleanup functions
*/
private async onSuccessfulSync(
postgres: Postgres,
source: Connectable,
recentQueries: RecentQuery[],
stats?: StatisticsMode,
): Promise<void> {
if (source.isSupabase()) {
// https://gist.github.com/Xetera/067c613580320468e8367d9d6c0e06ad
await postgres.exec("drop schema if exists extensions cascade");
}
this.startQueryLoader(source);
this.optimizer.start(recentQueries, stats);
}
private startQueryLoader(source: Connectable) {
if (this.queryLoader) {
this.queryLoader.stop();
}
this.queryLoader = new QueryLoader(this.sourceManager, source);
this.schemaLoader = new SchemaLoader(this.sourceManager, source);
this.queryLoader.on("pollError", (error) => {
log.error("Failed to poll queries", "remote");
});
this.queryLoader.on("poll", (queries) => {
this.optimizer.addQueries(queries).catch((error) => {
log.error(
`Failed to add ${queries.length} queries to optimizer`,
"remote",
);
});
this.pgStatStatementsStatus = PgStatStatementsStatus.Installed;
}).on("pgStatStatementsNotInstalled", () => {
this.pgStatStatementsStatus = PgStatStatementsStatus.NotInstalled;
});
this.queryLoader.on("exit", () => {
log.error("Query loader exited", "remote");
this.queryLoader = undefined;
});
this.queryLoader.start();
}
async cleanup(): Promise<void> {
await this.optimizer.finish;
this.optimizer.stop();
await Promise.all([
this.manager.closeAll(),
this.sourceManager.closeAll(),
]);
}
async [Symbol.asyncDispose](): Promise<void> {
await this.cleanup();
}
}
export type StatisticsStrategy = {
type: "pullFromSource";
} | {
type: "static";
stats: StatisticsMode;
};
export type InferredStatsStrategy = "10k" | "fromSource";
type StatsResult = {
mode: StatisticsMode;
strategy: InferredStatsStrategy;
};
const PgStatStatementsStatus = {
Installed: "Installed",
NotInstalled: "Not Installed",
Unknown: "Unknown",
} as const;
type PgStatStatementsStatus =
typeof PgStatStatementsStatus[keyof typeof PgStatStatementsStatus];