-
Notifications
You must be signed in to change notification settings - Fork 335
Expand file tree
/
Copy pathproc_pool.ts
More file actions
164 lines (150 loc) · 4.25 KB
/
Copy pathproc_pool.ts
File metadata and controls
164 lines (150 loc) · 4.25 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
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { MultiMutex, Mutex } from '@livekit/mutex';
import type { RunningJobInfo } from '../job.js';
import { Queue } from '../utils.js';
import type { InferenceExecutor } from './inference_executor.js';
import type { JobExecutor } from './job_executor.js';
import { JobProcExecutor } from './job_proc_executor.js';
export class ProcPool {
agent: string;
initializeTimeout: number;
closeTimeout: number;
executors: JobExecutor[] = [];
tasks: Promise<void>[] = [];
started = false;
closed = false;
controller = new AbortController();
initMutex = new Mutex();
procMutex?: MultiMutex;
// Keep each lock token paired with its warmed process so MultiMutex slots are always released correctly.
warmedProcQueue = new Queue<{ proc: JobExecutor; unlock: () => void }>();
inferenceExecutor?: InferenceExecutor;
memoryWarnMB: number;
memoryLimitMB: number;
constructor(
agent: string,
numIdleProcesses: number,
initializeTimeout: number,
closeTimeout: number,
inferenceExecutor: InferenceExecutor | undefined,
memoryWarnMB: number,
memoryLimitMB: number,
) {
this.agent = agent;
if (numIdleProcesses > 0) {
this.procMutex = new MultiMutex(numIdleProcesses);
}
this.initializeTimeout = initializeTimeout;
this.closeTimeout = closeTimeout;
this.inferenceExecutor = inferenceExecutor;
this.memoryWarnMB = memoryWarnMB;
this.memoryLimitMB = memoryLimitMB;
}
get processes(): JobExecutor[] {
return this.executors;
}
getByJobId(id: string): JobExecutor | null {
return this.executors.find((x) => x.runningJob && x.runningJob.job.id === id) || null;
}
async launchJob(info: RunningJobInfo) {
let proc: JobExecutor;
if (this.procMutex) {
const entry = await this.warmedProcQueue.get();
proc = entry.proc;
// Release exactly the slot that produced this warmed process.
entry.unlock();
} else {
proc = new JobProcExecutor(
this.agent,
this.inferenceExecutor,
this.initializeTimeout,
this.closeTimeout,
this.memoryWarnMB,
this.memoryLimitMB,
2500,
60000,
500,
);
this.executors.push(proc);
await proc.start();
await proc.initialize();
}
await proc.launchJob(info);
}
async procWatchTask(procUnlock: () => void) {
const proc = new JobProcExecutor(
this.agent,
this.inferenceExecutor,
this.initializeTimeout,
this.closeTimeout,
this.memoryWarnMB,
this.memoryLimitMB,
2500,
60000,
500,
);
try {
this.executors.push(proc);
const unlock = await this.initMutex.lock();
if (this.closed) {
return;
}
await proc.start();
try {
await proc.initialize();
await this.warmedProcQueue.put({ proc, unlock: procUnlock });
} catch {
// Initialization failed before enqueue, so release the acquired slot immediately.
procUnlock();
}
unlock();
await proc.join();
} finally {
const procIndex = this.executors.indexOf(proc);
if (procIndex !== -1) {
this.executors.splice(procIndex, 1);
} else {
throw new Error(`proc ${proc} not found in executors`);
}
}
}
start() {
if (this.started) {
return;
}
this.started = true;
this.run(this.controller.signal);
}
async run(signal: AbortSignal) {
if (this.procMutex) {
while (!signal.aborted) {
const procUnlock = await this.procMutex.lock();
const task = this.procWatchTask(procUnlock);
this.tasks.push(task);
task.finally(() => {
const taskIndex = this.tasks.indexOf(task);
if (taskIndex !== -1) {
this.tasks.splice(taskIndex, 1);
} else {
throw new Error(`task ${task} not found in tasks`);
}
});
}
}
}
async close() {
if (!this.started) {
return;
}
this.closed = true;
this.controller.abort();
this.warmedProcQueue.items.forEach((e) => {
e.unlock();
e.proc.close();
});
this.executors.forEach((e) => e.close());
await Promise.allSettled(this.tasks);
}
}