2021-04-05 22:18:19 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { join } from 'path';
|
|
|
|
import { Worker } from 'worker_threads';
|
2021-09-16 21:54:06 +00:00
|
|
|
import { format } from 'util';
|
2022-03-29 17:06:42 +00:00
|
|
|
import { app } from 'electron';
|
2021-04-05 22:18:19 +00:00
|
|
|
|
2021-09-16 21:54:06 +00:00
|
|
|
import { strictAssert } from '../util/assert';
|
2021-09-08 20:39:14 +00:00
|
|
|
import { explodePromise } from '../util/explodePromise';
|
2021-09-16 21:54:06 +00:00
|
|
|
import type { LoggerType } from '../types/Logging';
|
2023-03-02 17:59:18 +00:00
|
|
|
import { parseSqliteError, SqliteErrorKind } from './errors';
|
2022-11-17 00:29:15 +00:00
|
|
|
import type DB from './Server';
|
2021-09-08 20:39:14 +00:00
|
|
|
|
2021-08-09 22:04:17 +00:00
|
|
|
const MIN_TRACE_DURATION = 40;
|
2021-04-14 19:43:11 +00:00
|
|
|
|
2021-09-16 21:54:06 +00:00
|
|
|
export type InitializeOptions = Readonly<{
|
|
|
|
configDir: string;
|
|
|
|
key: string;
|
|
|
|
logger: LoggerType;
|
|
|
|
}>;
|
2021-04-05 22:18:19 +00:00
|
|
|
|
2021-09-16 21:54:06 +00:00
|
|
|
export type WorkerRequest = Readonly<
|
2021-04-05 22:18:19 +00:00
|
|
|
| {
|
2021-09-16 21:54:06 +00:00
|
|
|
type: 'init';
|
|
|
|
options: Omit<InitializeOptions, 'logger'>;
|
2021-04-05 22:18:19 +00:00
|
|
|
}
|
|
|
|
| {
|
2021-09-16 21:54:06 +00:00
|
|
|
type: 'close';
|
2021-04-05 22:18:19 +00:00
|
|
|
}
|
2021-07-10 00:43:36 +00:00
|
|
|
| {
|
2021-09-16 21:54:06 +00:00
|
|
|
type: 'removeDB';
|
2021-07-10 00:43:36 +00:00
|
|
|
}
|
2021-04-05 22:18:19 +00:00
|
|
|
| {
|
2021-09-16 21:54:06 +00:00
|
|
|
type: 'sqlCall';
|
2022-11-17 00:29:15 +00:00
|
|
|
method: keyof typeof DB;
|
|
|
|
args: ReadonlyArray<unknown>;
|
2021-09-16 21:54:06 +00:00
|
|
|
}
|
|
|
|
>;
|
|
|
|
|
|
|
|
export type WrappedWorkerRequest = Readonly<{
|
|
|
|
seq: number;
|
|
|
|
request: WorkerRequest;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
export type WrappedWorkerLogEntry = Readonly<{
|
|
|
|
type: 'log';
|
|
|
|
level: 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
|
|
|
|
args: ReadonlyArray<unknown>;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
export type WrappedWorkerResponse =
|
|
|
|
| Readonly<{
|
|
|
|
type: 'response';
|
|
|
|
seq: number;
|
|
|
|
error: string | undefined;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
response: any;
|
|
|
|
}>
|
|
|
|
| WrappedWorkerLogEntry;
|
2021-04-05 22:18:19 +00:00
|
|
|
|
|
|
|
type PromisePair<T> = {
|
|
|
|
resolve: (response: T) => void;
|
|
|
|
reject: (error: Error) => void;
|
|
|
|
};
|
|
|
|
|
2023-03-02 17:59:18 +00:00
|
|
|
type KnownErrorResolverType = Readonly<{
|
|
|
|
kind: SqliteErrorKind;
|
|
|
|
resolve: (err: Error) => void;
|
|
|
|
}>;
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
export class MainSQL {
|
|
|
|
private readonly worker: Worker;
|
|
|
|
|
2021-04-08 23:27:41 +00:00
|
|
|
private isReady = false;
|
|
|
|
|
|
|
|
private onReady: Promise<void> | undefined;
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
private readonly onExit: Promise<void>;
|
|
|
|
|
2023-03-02 17:59:18 +00:00
|
|
|
// Promise resolve callbacks for corruption and readonly errors.
|
|
|
|
private errorResolvers = new Array<KnownErrorResolverType>();
|
2021-09-08 20:39:14 +00:00
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
private seq = 0;
|
|
|
|
|
2021-09-16 21:54:06 +00:00
|
|
|
private logger?: LoggerType;
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
private onResponse = new Map<number, PromisePair<any>>();
|
|
|
|
|
|
|
|
constructor() {
|
2022-03-29 17:06:42 +00:00
|
|
|
const scriptDir = join(app.getAppPath(), 'ts', 'sql', 'mainWorker.js');
|
|
|
|
this.worker = new Worker(scriptDir);
|
2021-04-05 22:18:19 +00:00
|
|
|
|
|
|
|
this.worker.on('message', (wrappedResponse: WrappedWorkerResponse) => {
|
2021-09-16 21:54:06 +00:00
|
|
|
if (wrappedResponse.type === 'log') {
|
|
|
|
const { level, args } = wrappedResponse;
|
|
|
|
strictAssert(this.logger !== undefined, 'Logger not initialized');
|
|
|
|
this.logger[level](`MainSQL: ${format(...args)}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
const { seq, error, response } = wrappedResponse;
|
|
|
|
|
|
|
|
const pair = this.onResponse.get(seq);
|
|
|
|
this.onResponse.delete(seq);
|
|
|
|
if (!pair) {
|
|
|
|
throw new Error(`Unexpected worker response with seq: ${seq}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
2021-09-08 20:39:14 +00:00
|
|
|
const errorObj = new Error(error);
|
2023-03-02 17:59:18 +00:00
|
|
|
this.onError(errorObj);
|
2021-09-08 20:39:14 +00:00
|
|
|
|
|
|
|
pair.reject(errorObj);
|
2021-04-05 22:18:19 +00:00
|
|
|
} else {
|
|
|
|
pair.resolve(response);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-03-02 17:59:18 +00:00
|
|
|
const { promise: onExit, resolve: resolveOnExit } = explodePromise<void>();
|
|
|
|
this.onExit = onExit;
|
|
|
|
this.worker.once('exit', resolveOnExit);
|
2021-04-05 22:18:19 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 21:54:06 +00:00
|
|
|
public async initialize({
|
|
|
|
configDir,
|
|
|
|
key,
|
|
|
|
logger,
|
|
|
|
}: InitializeOptions): Promise<void> {
|
2021-04-08 23:27:41 +00:00
|
|
|
if (this.isReady || this.onReady) {
|
|
|
|
throw new Error('Already initialized');
|
|
|
|
}
|
|
|
|
|
2021-09-16 21:54:06 +00:00
|
|
|
this.logger = logger;
|
|
|
|
|
|
|
|
this.onReady = this.send({
|
|
|
|
type: 'init',
|
|
|
|
options: { configDir, key },
|
|
|
|
});
|
2021-04-08 23:27:41 +00:00
|
|
|
|
|
|
|
await this.onReady;
|
|
|
|
|
|
|
|
this.onReady = undefined;
|
|
|
|
this.isReady = true;
|
2021-04-05 22:18:19 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 20:39:14 +00:00
|
|
|
public whenCorrupted(): Promise<Error> {
|
2023-03-02 17:59:18 +00:00
|
|
|
const { promise, resolve } = explodePromise<Error>();
|
|
|
|
this.errorResolvers.push({ kind: SqliteErrorKind.Corrupted, resolve });
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
public whenReadonly(): Promise<Error> {
|
|
|
|
const { promise, resolve } = explodePromise<Error>();
|
|
|
|
this.errorResolvers.push({ kind: SqliteErrorKind.Readonly, resolve });
|
|
|
|
return promise;
|
2021-09-08 20:39:14 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
public async close(): Promise<void> {
|
2021-04-08 23:27:41 +00:00
|
|
|
if (!this.isReady) {
|
|
|
|
throw new Error('Not initialized');
|
|
|
|
}
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
await this.send({ type: 'close' });
|
|
|
|
await this.onExit;
|
|
|
|
}
|
|
|
|
|
2021-07-10 00:43:36 +00:00
|
|
|
public async removeDB(): Promise<void> {
|
|
|
|
await this.send({ type: 'removeDB' });
|
|
|
|
}
|
|
|
|
|
2022-11-17 00:29:15 +00:00
|
|
|
public async sqlCall<Method extends keyof typeof DB>(
|
|
|
|
method: Method,
|
|
|
|
...args: Parameters<typeof DB[Method]>
|
|
|
|
): Promise<ReturnType<typeof DB[Method]>> {
|
2021-04-08 23:27:41 +00:00
|
|
|
if (this.onReady) {
|
|
|
|
await this.onReady;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.isReady) {
|
|
|
|
throw new Error('Not initialized');
|
|
|
|
}
|
|
|
|
|
2022-11-14 19:35:37 +00:00
|
|
|
type SqlCallResult = Readonly<{
|
2022-11-17 00:29:15 +00:00
|
|
|
result: ReturnType<typeof DB[Method]>;
|
2022-11-14 19:35:37 +00:00
|
|
|
duration: number;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
const { result, duration } = await this.send<SqlCallResult>({
|
2021-04-26 22:01:22 +00:00
|
|
|
type: 'sqlCall',
|
|
|
|
method,
|
|
|
|
args,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (duration > MIN_TRACE_DURATION) {
|
2021-09-16 21:54:06 +00:00
|
|
|
strictAssert(this.logger !== undefined, 'Logger not initialized');
|
|
|
|
this.logger.info(`MainSQL: slow query ${method} duration=${duration}ms`);
|
2021-04-26 22:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2021-04-05 22:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async send<Response>(request: WorkerRequest): Promise<Response> {
|
|
|
|
const { seq } = this;
|
|
|
|
this.seq += 1;
|
|
|
|
|
2023-03-02 17:59:18 +00:00
|
|
|
const { promise: result, resolve, reject } = explodePromise<Response>();
|
|
|
|
this.onResponse.set(seq, { resolve, reject });
|
2021-04-05 22:18:19 +00:00
|
|
|
|
|
|
|
const wrappedRequest: WrappedWorkerRequest = {
|
|
|
|
seq,
|
|
|
|
request,
|
|
|
|
};
|
|
|
|
this.worker.postMessage(wrappedRequest);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2023-03-02 17:59:18 +00:00
|
|
|
|
|
|
|
private onError(error: Error): void {
|
|
|
|
const errorKind = parseSqliteError(error);
|
|
|
|
if (errorKind === SqliteErrorKind.Unknown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const resolvers = new Array<(error: Error) => void>();
|
|
|
|
this.errorResolvers = this.errorResolvers.filter(entry => {
|
|
|
|
if (entry.kind === errorKind) {
|
|
|
|
resolvers.push(entry.resolve);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const resolve of resolvers) {
|
|
|
|
resolve(error);
|
|
|
|
}
|
|
|
|
}
|
2021-04-05 22:18:19 +00:00
|
|
|
}
|