-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathquery-server-client.ts
More file actions
327 lines (292 loc) · 10.4 KB
/
query-server-client.ts
File metadata and controls
327 lines (292 loc) · 10.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
import { ensureFile } from "fs-extra";
import type { DisposeHandler } from "../common/disposable-object";
import { DisposableObject } from "../common/disposable-object";
import type { CancellationToken } from "vscode";
import type { RequestType } from "vscode-jsonrpc/node";
import { createMessageConnection } from "vscode-jsonrpc/node";
import type { CodeQLCliServer } from "../codeql-cli/cli";
import { shouldDebugQueryServer, spawnServer } from "../codeql-cli/cli";
import type { QueryServerConfig } from "../config";
import type { BaseLogger, Logger } from "../common/logging";
import { showAndLogErrorMessage } from "../common/logging";
import type { ProgressReporter } from "../common/logging/vscode";
import { extLogger } from "../common/logging/vscode";
import type { ProgressMessage, WithProgressId } from "./messages";
import { progress } from "./messages";
import type { ProgressCallback } from "../common/vscode/progress";
import { withProgress } from "../common/vscode/progress";
import { ServerProcess } from "./server-process";
import type { App } from "../common/app";
type ServerOpts = {
logger: Logger;
contextStoragePath: string;
};
type WithProgressReporting = (
task: (
progress: ProgressReporter,
token: CancellationToken,
) => Thenable<void>,
) => Thenable<void>;
const MAX_UNEXPECTED_TERMINATIONS = 5;
/**
* Client that manages a query server process.
* The server process is started upon initialization and tracked during its lifetime.
* The server process is disposed when the client is disposed, or if the client asks
* to restart it (which disposes the existing process and starts a new one).
*/
export class QueryServerClient extends DisposableObject {
serverProcess?: ServerProcess;
progressCallbacks: {
[key: number]: ((res: ProgressMessage) => void) | undefined;
};
nextCallback: number;
nextProgress: number;
unexpectedTerminationCount = 0;
withProgressReporting: WithProgressReporting;
private readonly queryServerStartListeners = [] as Array<
(progress: ProgressCallback) => void
>;
// Can't use standard vscode EventEmitter here since they do not cause the calling
// function to fail if one of the event handlers fail. This is something that
// we need here.
readonly onDidStartQueryServer = (
e: (progress: ProgressCallback) => void,
) => {
this.queryServerStartListeners.push(e);
};
public activeQueryLogger: BaseLogger;
constructor(
app: App,
readonly config: QueryServerConfig,
readonly cliServer: CodeQLCliServer,
readonly opts: ServerOpts,
withProgressReporting: WithProgressReporting,
readonly warmOverlayBaseCache: boolean = false,
) {
super();
// Since no query is active when we initialize, just point the "active query logger" to the
// default logger.
this.activeQueryLogger = this.logger;
// When the query server configuration changes, restart the query server.
if (config.onDidChangeConfiguration !== undefined) {
this.push(
config.onDidChangeConfiguration(() =>
app.commands.execute("codeQL.restartQueryServerOnConfigChange"),
),
);
}
this.withProgressReporting = withProgressReporting;
this.nextCallback = 0;
this.nextProgress = 0;
this.progressCallbacks = {};
}
get logger(): Logger {
return this.opts.logger;
}
/**
* Whether this query server supports the 'evaluation/runQueries' method for running multiple
* queries at once.
*/
async supportsRunQueriesMethod(): Promise<boolean> {
return await this.cliServer.cliConstraints.supportsQueryServerRunQueries();
}
/** Stops the query server by disposing of the current server process. */
private stopQueryServer(): void {
if (this.serverProcess !== undefined) {
this.disposeAndStopTracking(this.serverProcess);
} else {
void this.logger.log("No server process to be stopped.");
}
}
/**
* Restarts the query server by disposing of the current server process and then starting a new one.
* This resets the unexpected termination count. As hopefully it is an indication that the user has fixed the
* issue.
*/
async restartQueryServer(progress: ProgressCallback): Promise<void> {
// Reset the unexpected termination count when we restart the query server manually
// or due to config change
this.unexpectedTerminationCount = 0;
await this.restartQueryServerInternal(progress);
}
/**
* Try and restart the query server if it has unexpectedly terminated.
*/
private restartQueryServerOnFailure() {
if (this.unexpectedTerminationCount < MAX_UNEXPECTED_TERMINATIONS) {
void withProgress(
async (progress) => this.restartQueryServerInternal(progress),
{
title: "Restarting CodeQL query server due to unexpected termination",
},
);
} else {
void showAndLogErrorMessage(
extLogger,
"The CodeQL query server has unexpectedly terminated too many times. Please check the logs for errors. You can manually restart the query server using the command 'CodeQL: Restart query server'.",
);
// Make sure we dispose anyway to reject all pending requests.
this.serverProcess?.dispose();
}
this.unexpectedTerminationCount++;
}
/**
* Restarts the query server by disposing of the current server process and then starting a new one.
* This does not reset the unexpected termination count.
*/
private async restartQueryServerInternal(
progress: ProgressCallback,
): Promise<void> {
this.stopQueryServer();
await this.startQueryServer();
// Ensure we await all responses from event handlers so that
// errors can be properly reported to the user.
await Promise.all(
this.queryServerStartListeners.map((handler) => handler(progress)),
);
}
showLog(): void {
this.logger.show();
}
/** Starts a new query server process, sending progress messages to the status bar. */
async startQueryServer(): Promise<void> {
// Use an arrow function to preserve the value of `this`.
return this.withProgressReporting((progress, _) =>
this.startQueryServerImpl(progress),
);
}
/** Starts a new query server process, sending progress messages to the given reporter. */
private async startQueryServerImpl(
progressReporter: ProgressReporter,
): Promise<void> {
void this.logger.log("Starting query server.");
const ramArgs = await this.cliServer.resolveRam(
this.config.queryMemoryMb,
progressReporter,
);
const args = ["--threads", this.config.numThreads.toString()].concat(
ramArgs,
);
if (this.config.cacheSize > 0) {
args.push("--max-disk-cache");
args.push(this.config.cacheSize.toString());
}
const structuredLogFile = `${this.opts.contextStoragePath}/structured-evaluator-log.json`;
await ensureFile(structuredLogFile);
args.push("--evaluator-log");
args.push(structuredLogFile);
if (this.config.debug) {
args.push("--debug", "--tuple-counting");
}
if (shouldDebugQueryServer()) {
args.push(
"-J=-agentlib:jdwp=transport=dt_socket,address=localhost:9010,server=y,suspend=y,quiet=y",
);
}
if (this.warmOverlayBaseCache) {
args.push(
"--no-evaluate-as-overlay",
"--cache-at-frontier",
"--warm-cache-only",
);
}
const queryServerSuffix = this.warmOverlayBaseCache
? " for warming overlay-base cache"
: "";
const child = spawnServer(
this.config.codeQlPath,
`CodeQL query server${queryServerSuffix}`,
["execute", "query-server2"],
args,
this.logger,
(data) =>
this.activeQueryLogger.log(data.toString(), {
trailingNewline: false,
}),
undefined, // no listener for stdout
progressReporter,
);
progressReporter.report({
message: `Connecting to CodeQL query server${queryServerSuffix}`,
});
const connection = createMessageConnection(child.stdout, child.stdin);
connection.onNotification(progress, (res) => {
const callback = this.progressCallbacks[res.id];
if (callback) {
callback(res);
}
});
this.serverProcess = new ServerProcess(
child,
connection,
`Query Server 2${queryServerSuffix}`,
this.logger,
);
// Ensure the server process is disposed together with this client.
this.track(this.serverProcess);
connection.listen();
progressReporter.report({
message: `Connected to CodeQL query server${queryServerSuffix} v2`,
});
this.nextCallback = 0;
this.nextProgress = 0;
this.progressCallbacks = {};
// 'exit' may or may not fire after 'error' event, so we want to guard against restarting the
// query server twice if both events fire.
let wasExitOrErrorHandled = false;
child.on("error", (err: Error) => {
if (!wasExitOrErrorHandled) {
void this.logger.log(
`Query server${queryServerSuffix} terminated with error: ${err}.`,
);
this.restartQueryServerOnFailure();
wasExitOrErrorHandled = true;
}
});
child.on("exit", (code: number, signal: string) => {
if (!wasExitOrErrorHandled) {
if (code !== null) {
void this.logger.log(
`Query server${queryServerSuffix} terminated with exit code: ${code}.`,
);
}
if (signal !== null) {
void this.logger.log(
`Query server${queryServerSuffix} terminated due to receipt of signal: ${signal}.`,
);
}
this.restartQueryServerOnFailure();
wasExitOrErrorHandled = true;
}
});
}
get serverProcessPid(): number {
return this.serverProcess!.child.pid || 0;
}
async sendRequest<P, R, E>(
type: RequestType<WithProgressId<P>, R, E>,
parameter: P,
token?: CancellationToken,
progress?: (res: ProgressMessage) => void,
): Promise<R> {
const id = this.nextProgress++;
this.progressCallbacks[id] = progress;
try {
if (this.serverProcess === undefined) {
throw new Error("No query server process found.");
}
return await this.serverProcess.connection.sendRequest(
type,
{ body: parameter, progressId: id },
token,
);
} finally {
delete this.progressCallbacks[id];
}
}
public dispose(disposeHandler?: DisposeHandler): void {
this.progressCallbacks = {};
this.stopQueryServer();
super.dispose(disposeHandler);
}
}