@fluidframework/agent-scheduler 2.0.0-internal.3.0.2 → 2.0.0-internal.3.2.0
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/.eslintrc.js +8 -10
- package/README.md +1 -1
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js.map +1 -1
- package/dist/scheduler.d.ts.map +1 -1
- package/dist/scheduler.js +45 -13
- package/dist/scheduler.js.map +1 -1
- package/dist/taskSubscription.d.ts.map +1 -1
- package/dist/taskSubscription.js.map +1 -1
- package/lib/agent.d.ts.map +1 -1
- package/lib/agent.js.map +1 -1
- package/lib/scheduler.d.ts.map +1 -1
- package/lib/scheduler.js +45 -13
- package/lib/scheduler.js.map +1 -1
- package/lib/taskSubscription.d.ts.map +1 -1
- package/lib/taskSubscription.js.map +1 -1
- package/package.json +40 -37
- package/prettier.config.cjs +1 -1
- package/src/agent.ts +51 -48
- package/src/scheduler.ts +471 -421
- package/src/taskSubscription.ts +45 -45
- package/tsconfig.esnext.json +6 -6
- package/tsconfig.json +9 -13
package/src/scheduler.ts
CHANGED
|
@@ -4,450 +4,500 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { assert, TypedEventEmitter } from "@fluidframework/common-utils";
|
|
7
|
+
import { FluidObject, IFluidHandle, IRequest } from "@fluidframework/core-interfaces";
|
|
8
|
+
import { UsageError } from "@fluidframework/container-utils";
|
|
7
9
|
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from "@fluidframework/core-interfaces";
|
|
12
|
-
import {
|
|
13
|
-
FluidDataStoreRuntime,
|
|
14
|
-
FluidObjectHandle,
|
|
15
|
-
ISharedObjectRegistry,
|
|
10
|
+
FluidDataStoreRuntime,
|
|
11
|
+
FluidObjectHandle,
|
|
12
|
+
ISharedObjectRegistry,
|
|
16
13
|
} from "@fluidframework/datastore";
|
|
17
14
|
import { AttachState } from "@fluidframework/container-definitions";
|
|
18
15
|
import { ISharedMap, IValueChanged, SharedMap } from "@fluidframework/map";
|
|
19
16
|
import { ConsensusRegisterCollection } from "@fluidframework/register-collection";
|
|
20
17
|
import { IFluidDataStoreRuntime, IChannelFactory } from "@fluidframework/datastore-definitions";
|
|
21
18
|
import {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
IFluidDataStoreContext,
|
|
20
|
+
IFluidDataStoreFactory,
|
|
21
|
+
NamedFluidDataStoreRegistryEntry,
|
|
25
22
|
} from "@fluidframework/runtime-definitions";
|
|
26
23
|
import { v4 as uuid } from "uuid";
|
|
24
|
+
import { TelemetryDataTag } from "@fluidframework/telemetry-utils";
|
|
27
25
|
import { IAgentScheduler, IAgentSchedulerEvents } from "./agent";
|
|
28
26
|
|
|
29
27
|
// Note: making sure this ID is unique and does not collide with storage provided clientID
|
|
30
28
|
const UnattachedClientId = `${uuid()}_unattached`;
|
|
31
29
|
|
|
32
30
|
const mapWait = async <T = any>(map: ISharedMap, key: string): Promise<T> => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
31
|
+
const maybeValue = map.get<T>(key);
|
|
32
|
+
if (maybeValue !== undefined) {
|
|
33
|
+
return maybeValue;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return new Promise((resolve) => {
|
|
37
|
+
const handler = (changed: IValueChanged) => {
|
|
38
|
+
if (changed.key === key) {
|
|
39
|
+
map.off("valueChanged", handler);
|
|
40
|
+
const value = map.get<T>(changed.key);
|
|
41
|
+
if (value === undefined) {
|
|
42
|
+
throw new Error("Unexpected valueChanged result");
|
|
43
|
+
}
|
|
44
|
+
resolve(value);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
map.on("valueChanged", handler);
|
|
48
|
+
});
|
|
51
49
|
};
|
|
52
50
|
|
|
53
51
|
const schedulerId = "scheduler";
|
|
54
52
|
|
|
55
|
-
export class AgentScheduler
|
|
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
|
-
|
|
53
|
+
export class AgentScheduler
|
|
54
|
+
extends TypedEventEmitter<IAgentSchedulerEvents>
|
|
55
|
+
implements IAgentScheduler
|
|
56
|
+
{
|
|
57
|
+
public static async load(
|
|
58
|
+
runtime: IFluidDataStoreRuntime,
|
|
59
|
+
context: IFluidDataStoreContext,
|
|
60
|
+
existing: boolean,
|
|
61
|
+
) {
|
|
62
|
+
let root: ISharedMap;
|
|
63
|
+
let consensusRegisterCollection: ConsensusRegisterCollection<string | null>;
|
|
64
|
+
if (!existing) {
|
|
65
|
+
root = SharedMap.create(runtime, "root");
|
|
66
|
+
root.bindToContext();
|
|
67
|
+
consensusRegisterCollection = ConsensusRegisterCollection.create(runtime);
|
|
68
|
+
consensusRegisterCollection.bindToContext();
|
|
69
|
+
root.set(schedulerId, consensusRegisterCollection.handle);
|
|
70
|
+
} else {
|
|
71
|
+
root = (await runtime.getChannel("root")) as ISharedMap;
|
|
72
|
+
const handle = await mapWait<IFluidHandle<ConsensusRegisterCollection<string | null>>>(
|
|
73
|
+
root,
|
|
74
|
+
schedulerId,
|
|
75
|
+
);
|
|
76
|
+
assert(handle !== undefined, 0x116 /* "Missing handle on scheduler load" */);
|
|
77
|
+
consensusRegisterCollection = await handle.get();
|
|
78
|
+
}
|
|
79
|
+
const agentScheduler = new AgentScheduler(runtime, context, consensusRegisterCollection);
|
|
80
|
+
agentScheduler.initialize();
|
|
81
|
+
|
|
82
|
+
return agentScheduler;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public get IAgentScheduler() {
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
public get IFluidLoadable() {
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private get clientId(): string {
|
|
93
|
+
if (this.runtime.attachState === AttachState.Detached) {
|
|
94
|
+
return UnattachedClientId;
|
|
95
|
+
}
|
|
96
|
+
const clientId = this.runtime.clientId;
|
|
97
|
+
assert(!!clientId, 0x117 /* "Trying to get missing clientId!" */);
|
|
98
|
+
return clientId;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Set of tasks registered by this client.
|
|
102
|
+
// Has no relationship with lists below.
|
|
103
|
+
// The only requirement here - a task can be registered by a client only once.
|
|
104
|
+
// Other clients can pick these tasks.
|
|
105
|
+
private readonly registeredTasks = new Set<string>();
|
|
106
|
+
|
|
107
|
+
// List of all tasks client is capable of running (essentially expressed desire to run)
|
|
108
|
+
// Client will proactively attempt to pick them up these tasks if they are not assigned to other clients.
|
|
109
|
+
// This is a strict superset of tasks running in the client.
|
|
110
|
+
private readonly locallyRunnableTasks = new Map<string, () => Promise<void>>();
|
|
111
|
+
|
|
112
|
+
// Set of registered tasks client is currently running.
|
|
113
|
+
// It's subset of this.locallyRunnableTasks
|
|
114
|
+
private runningTasks = new Set<string>();
|
|
115
|
+
|
|
116
|
+
private readonly _handle: IFluidHandle<this>;
|
|
117
|
+
|
|
118
|
+
constructor(
|
|
119
|
+
private readonly runtime: IFluidDataStoreRuntime,
|
|
120
|
+
private readonly context: IFluidDataStoreContext,
|
|
121
|
+
private readonly consensusRegisterCollection: ConsensusRegisterCollection<string | null>,
|
|
122
|
+
) {
|
|
123
|
+
super();
|
|
124
|
+
this._handle = new FluidObjectHandle(this, "", this.runtime.objectsRoutingContext);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public get handle() {
|
|
128
|
+
return this._handle;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
public async register(...taskUrls: string[]): Promise<void> {
|
|
132
|
+
for (const taskUrl of taskUrls) {
|
|
133
|
+
if (this.registeredTasks.has(taskUrl)) {
|
|
134
|
+
throw new UsageError(`Task is already registered`, {
|
|
135
|
+
taskUrl: { tag: TelemetryDataTag.CodeArtifact, value: taskUrl },
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const unregisteredTasks: string[] = [];
|
|
140
|
+
for (const taskUrl of taskUrls) {
|
|
141
|
+
this.registeredTasks.add(taskUrl);
|
|
142
|
+
// Only register for a new task.
|
|
143
|
+
const currentClient = this.getTaskClientId(taskUrl);
|
|
144
|
+
if (currentClient === undefined) {
|
|
145
|
+
unregisteredTasks.push(taskUrl);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return this.registerCore(unregisteredTasks);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
public async pick(taskId: string, worker: () => Promise<void>): Promise<void> {
|
|
152
|
+
if (this.locallyRunnableTasks.has(taskId)) {
|
|
153
|
+
throw new UsageError(`Task is already attempted`, {
|
|
154
|
+
taskUrl: { tag: TelemetryDataTag.CodeArtifact, value: taskId },
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
this.locallyRunnableTasks.set(taskId, worker);
|
|
158
|
+
|
|
159
|
+
// We have a policy to disallow non-interactive clients from taking tasks. Callers of pick() can
|
|
160
|
+
// either perform this check proactively and call conditionally, or catch the error (in which case
|
|
161
|
+
// they can know they will not get the task).
|
|
162
|
+
assert(
|
|
163
|
+
this.context.deltaManager.clientDetails.capabilities.interactive,
|
|
164
|
+
0x118 /* "Bad client interactive check" */,
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
// Check the current status and express interest if it's a new one (undefined) or currently unpicked (null).
|
|
168
|
+
if (this.isActive()) {
|
|
169
|
+
const currentClient = this.getTaskClientId(taskId);
|
|
170
|
+
if (currentClient === undefined || currentClient === null) {
|
|
171
|
+
await this.writeCore(taskId, this.clientId);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
public async release(...taskUrls: string[]): Promise<void> {
|
|
177
|
+
const active = this.isActive();
|
|
178
|
+
for (const taskUrl of taskUrls) {
|
|
179
|
+
if (!this.locallyRunnableTasks.has(taskUrl)) {
|
|
180
|
+
throw new UsageError(`Task was never registered`, {
|
|
181
|
+
taskUrl: { tag: TelemetryDataTag.CodeArtifact, value: taskUrl },
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
if (!this.runningTasks.has(taskUrl)) {
|
|
185
|
+
// If we got disconnected (and are attached), tasks that we WERE picked for at the time of disconnect
|
|
186
|
+
// will still show us as holding the task according to getTaskClientId (the CRC is stale), but we
|
|
187
|
+
// should not try to release because our disconnect will already result in either someone else or
|
|
188
|
+
// ourselves clearing the task upon reconnect.
|
|
189
|
+
// This UsageError is to enforce that the caller should check AgentScheduler.pickedTasks before trying
|
|
190
|
+
// to release a task.
|
|
191
|
+
throw new UsageError(`Task is not currently picked`, {
|
|
192
|
+
taskUrl: { tag: TelemetryDataTag.CodeArtifact, value: taskUrl },
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
// We may only release tasks that we KNOW we hold (detached state or connected and own the CRC). If we're
|
|
196
|
+
// attached+disconnected then we'll lose the task automatically, and so may not release manually (someone
|
|
197
|
+
// else might hold it by the time we reconnect)
|
|
198
|
+
assert(active, 0x119 /* "This agent became inactive while releasing" */);
|
|
199
|
+
if (this.getTaskClientId(taskUrl) !== this.clientId) {
|
|
200
|
+
throw new UsageError(`Task was never picked`, {
|
|
201
|
+
taskUrl: { tag: TelemetryDataTag.CodeArtifact, value: taskUrl },
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return this.releaseCore([...taskUrls]);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
public pickedTasks(): string[] {
|
|
209
|
+
return Array.from(this.runningTasks.values());
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private async registerCore(taskUrls: string[]): Promise<void> {
|
|
213
|
+
if (taskUrls.length > 0) {
|
|
214
|
+
const registersP: Promise<void>[] = [];
|
|
215
|
+
for (const taskUrl of taskUrls) {
|
|
216
|
+
registersP.push(this.writeCore(taskUrl, null));
|
|
217
|
+
}
|
|
218
|
+
await Promise.all(registersP);
|
|
219
|
+
|
|
220
|
+
// The registers should have up to date results now. Check the status.
|
|
221
|
+
for (const taskUrl of taskUrls) {
|
|
222
|
+
const taskStatus = this.getTaskClientId(taskUrl);
|
|
223
|
+
|
|
224
|
+
// Task should be either registered (null) or picked up.
|
|
225
|
+
assert(taskStatus !== undefined, 0x11a /* `Unsuccessful registration` */);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private async releaseCore(taskUrls: string[]) {
|
|
231
|
+
if (taskUrls.length > 0) {
|
|
232
|
+
const releasesP: Promise<void>[] = [];
|
|
233
|
+
for (const taskUrl of taskUrls) {
|
|
234
|
+
// Remove from local map so that it can be picked later.
|
|
235
|
+
this.locallyRunnableTasks.delete(taskUrl);
|
|
236
|
+
releasesP.push(this.writeCore(taskUrl, null));
|
|
237
|
+
}
|
|
238
|
+
await Promise.all(releasesP);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
private async clearTasks(taskUrls: string[]) {
|
|
243
|
+
assert(this.isActive(), 0x11b /* "Trying to clear tasks on inactive agent" */);
|
|
244
|
+
const clearP: Promise<void>[] = [];
|
|
245
|
+
for (const taskUrl of taskUrls) {
|
|
246
|
+
clearP.push(this.writeCore(taskUrl, null));
|
|
247
|
+
}
|
|
248
|
+
await Promise.all(clearP);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private getTaskClientId(url: string): string | null | undefined {
|
|
252
|
+
return this.consensusRegisterCollection.read(url);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
private async writeCore(key: string, clientId: string | null): Promise<void> {
|
|
256
|
+
await this.consensusRegisterCollection.write(key, clientId);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private initialize() {
|
|
260
|
+
const quorum = this.runtime.getQuorum();
|
|
261
|
+
// A client left the quorum. Iterate and clear tasks held by that client.
|
|
262
|
+
// Ideally a leader should do this cleanup. But it's complicated when a leader itself leaves.
|
|
263
|
+
// Probably okay for now to have every client try to do this.
|
|
264
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
265
|
+
quorum.on("removeMember", async (clientId: string) => {
|
|
266
|
+
assert(
|
|
267
|
+
this.runtime.objectsRoutingContext.isAttached,
|
|
268
|
+
0x11c /* "Detached object routing context" */,
|
|
269
|
+
);
|
|
270
|
+
// Cleanup only if connected. If not, cleanup will happen in initializeCore() that runs on connection.
|
|
271
|
+
if (this.isActive()) {
|
|
272
|
+
const tasks: Promise<any>[] = [];
|
|
273
|
+
const leftTasks: string[] = [];
|
|
274
|
+
for (const taskUrl of this.consensusRegisterCollection.keys()) {
|
|
275
|
+
if (this.getTaskClientId(taskUrl) === clientId) {
|
|
276
|
+
if (this.locallyRunnableTasks.has(taskUrl)) {
|
|
277
|
+
tasks.push(this.writeCore(taskUrl, this.clientId));
|
|
278
|
+
} else {
|
|
279
|
+
leftTasks.push(taskUrl);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
tasks.push(this.clearTasks(leftTasks));
|
|
284
|
+
await Promise.all(tasks).catch((error) => {
|
|
285
|
+
this.sendErrorEvent("AgentScheduler_RemoveMemberError", error);
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
// Listeners for new/released tasks. All clients will try to grab at the same time.
|
|
291
|
+
// May be we want a randomized timer (Something like raft) to reduce chattiness?
|
|
292
|
+
this.consensusRegisterCollection.on(
|
|
293
|
+
"atomicChanged",
|
|
294
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
295
|
+
async (key: string, currentClient: string | null) => {
|
|
296
|
+
// Check if this client was chosen.
|
|
297
|
+
if (this.isActive() && currentClient === this.clientId) {
|
|
298
|
+
this.onNewTaskAssigned(key);
|
|
299
|
+
} else {
|
|
300
|
+
// The call below mutates the consensusRegisterCollection in
|
|
301
|
+
// its event handler, which is not safe.
|
|
302
|
+
// We need to force this to be part of a different batch of ops by
|
|
303
|
+
// scheduling a microtask in order to work around the current validations.
|
|
304
|
+
// This is not recommended and should be avoided.
|
|
305
|
+
await Promise.resolve().then(async () => {
|
|
306
|
+
await this.onTaskReassigned(key, currentClient);
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
if (this.isActive()) {
|
|
313
|
+
this.initializeCore();
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
this.runtime.on("connected", () => {
|
|
317
|
+
if (this.isActive()) {
|
|
318
|
+
this.initializeCore();
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
if (this.runtime.attachState === AttachState.Detached) {
|
|
323
|
+
this.runtime
|
|
324
|
+
.waitAttached()
|
|
325
|
+
.then(() => {
|
|
326
|
+
this.clearRunningTasks();
|
|
327
|
+
})
|
|
328
|
+
.catch((error) => {
|
|
329
|
+
this.sendErrorEvent("AgentScheduler_clearRunningTasks", error);
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
this.runtime.on("disconnected", () => {
|
|
334
|
+
if (this.runtime.attachState !== AttachState.Detached) {
|
|
335
|
+
this.clearRunningTasks();
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
private onNewTaskAssigned(key: string) {
|
|
341
|
+
assert(!this.runningTasks.has(key), 0x11d /* "task is already running" */);
|
|
342
|
+
this.runningTasks.add(key);
|
|
343
|
+
const worker = this.locallyRunnableTasks.get(key);
|
|
344
|
+
if (worker === undefined) {
|
|
345
|
+
this.sendErrorEvent("AgentScheduler_UnwantedChange", undefined, key);
|
|
346
|
+
} else {
|
|
347
|
+
this.emit("picked", key);
|
|
348
|
+
worker().catch((error) => {
|
|
349
|
+
this.sendErrorEvent("AgentScheduler_FailedWork", error, key);
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
private async onTaskReassigned(key: string, currentClient: string | null) {
|
|
355
|
+
if (this.runningTasks.has(key)) {
|
|
356
|
+
this.runningTasks.delete(key);
|
|
357
|
+
this.emit("released", key);
|
|
358
|
+
}
|
|
359
|
+
assert(currentClient !== undefined, 0x11e /* "client is undefined" */);
|
|
360
|
+
if (this.isActive()) {
|
|
361
|
+
// attempt to pick up task if we are connected.
|
|
362
|
+
// If not, initializeCore() will do it when connected
|
|
363
|
+
if (currentClient === null) {
|
|
364
|
+
if (this.locallyRunnableTasks.has(key)) {
|
|
365
|
+
await this.writeCore(key, this.clientId);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
// Check if the op came from dropped client
|
|
369
|
+
// This could happen when "old" ops are submitted on reconnection.
|
|
370
|
+
// They carry "old" ref seq number, but if write is not contested, it will get accepted
|
|
371
|
+
else if (this.runtime.getQuorum().getMember(currentClient) === undefined) {
|
|
372
|
+
await this.writeCore(key, null);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
private isActive() {
|
|
378
|
+
// Scheduler should be active in detached container.
|
|
379
|
+
if (this.runtime.attachState === AttachState.Detached) {
|
|
380
|
+
return true;
|
|
381
|
+
}
|
|
382
|
+
if (!this.runtime.connected) {
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Note: we are not checking for this.context.deltaManager.clientDetails.capabilities.interactive
|
|
387
|
+
// here. Instead we assert in pick() if a non-interactive client tries to pick.
|
|
388
|
+
|
|
389
|
+
return this.context.deltaManager.active;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
private initializeCore() {
|
|
393
|
+
// Nobody released the tasks held by last client in previous session.
|
|
394
|
+
// Check to see if this client needs to do this.
|
|
395
|
+
const clearCandidates: string[] = [];
|
|
396
|
+
const tasks: Promise<any>[] = [];
|
|
397
|
+
|
|
398
|
+
for (const [taskUrl] of this.locallyRunnableTasks) {
|
|
399
|
+
if (!this.getTaskClientId(taskUrl)) {
|
|
400
|
+
tasks.push(this.writeCore(taskUrl, this.clientId));
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
for (const taskUrl of this.consensusRegisterCollection.keys()) {
|
|
405
|
+
const currentClient = this.getTaskClientId(taskUrl);
|
|
406
|
+
if (currentClient && this.runtime.getQuorum().getMember(currentClient) === undefined) {
|
|
407
|
+
clearCandidates.push(taskUrl);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
tasks.push(this.clearTasks(clearCandidates));
|
|
412
|
+
|
|
413
|
+
Promise.all(tasks).catch((error) => {
|
|
414
|
+
this.sendErrorEvent("AgentScheduler_InitError", error);
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
private clearRunningTasks() {
|
|
419
|
+
const tasks = this.runningTasks;
|
|
420
|
+
this.runningTasks = new Set<string>();
|
|
421
|
+
|
|
422
|
+
if (this.isActive()) {
|
|
423
|
+
// Clear all tasks with UnattachedClientId (if was unattached) and reapply for tasks with new clientId
|
|
424
|
+
// If we are simply disconnected, then proper cleanup will be done on connection.
|
|
425
|
+
this.initializeCore();
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
for (const task of tasks) {
|
|
429
|
+
this.emit("lost", task);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
private sendErrorEvent(eventName: string, error: any, key?: string) {
|
|
434
|
+
this.runtime.logger.sendErrorEvent({ eventName, key }, error);
|
|
435
|
+
}
|
|
393
436
|
}
|
|
394
437
|
|
|
395
438
|
class AgentSchedulerRuntime extends FluidDataStoreRuntime {
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
439
|
+
constructor(
|
|
440
|
+
dataStoreContext: IFluidDataStoreContext,
|
|
441
|
+
sharedObjectRegistry: ISharedObjectRegistry,
|
|
442
|
+
existing: boolean,
|
|
443
|
+
) {
|
|
444
|
+
super(dataStoreContext, sharedObjectRegistry, existing, async () =>
|
|
445
|
+
AgentScheduler.load(this, dataStoreContext, existing),
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
public async request(request: IRequest) {
|
|
449
|
+
const response = await super.request(request);
|
|
450
|
+
if (response.status === 404) {
|
|
451
|
+
if (request.url === "" || request.url === "/") {
|
|
452
|
+
const agentScheduler = await this.entryPoint?.get();
|
|
453
|
+
assert(
|
|
454
|
+
agentScheduler !== undefined,
|
|
455
|
+
0x466 /* entryPoint for AgentSchedulerRuntime should have been initialized by now */,
|
|
456
|
+
);
|
|
457
|
+
|
|
458
|
+
return { status: 200, mimeType: "fluid/object", value: agentScheduler };
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
return response;
|
|
462
|
+
}
|
|
420
463
|
}
|
|
421
464
|
|
|
422
465
|
export class AgentSchedulerFactory implements IFluidDataStoreFactory {
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
466
|
+
public static readonly type = "_scheduler";
|
|
467
|
+
public readonly type = AgentSchedulerFactory.type;
|
|
468
|
+
|
|
469
|
+
public get IFluidDataStoreFactory() {
|
|
470
|
+
return this;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
public static get registryEntry(): NamedFluidDataStoreRegistryEntry {
|
|
474
|
+
return [this.type, Promise.resolve(new AgentSchedulerFactory())];
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
public static async createChildInstance(
|
|
478
|
+
parentContext: IFluidDataStoreContext,
|
|
479
|
+
): Promise<AgentScheduler> {
|
|
480
|
+
const packagePath = [...parentContext.packagePath, AgentSchedulerFactory.type];
|
|
481
|
+
const dataStore = await parentContext.containerRuntime.createDataStore(packagePath);
|
|
482
|
+
const entryPoint: FluidObject<IAgentScheduler> | undefined =
|
|
483
|
+
await dataStore.entryPoint?.get();
|
|
484
|
+
|
|
485
|
+
// AgentSchedulerRuntime always puts an AgentScheduler object in the data store's entryPoint, but double-check
|
|
486
|
+
// while we plumb entryPoints correctly everywhere, so we can be sure the cast below is fine.
|
|
487
|
+
assert(
|
|
488
|
+
entryPoint?.IAgentScheduler !== undefined,
|
|
489
|
+
0x467 /* The data store's entryPoint is not an AgentScheduler! */,
|
|
490
|
+
);
|
|
491
|
+
return entryPoint as unknown as AgentScheduler;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
public async instantiateDataStore(context: IFluidDataStoreContext, existing: boolean) {
|
|
495
|
+
const mapFactory = SharedMap.getFactory();
|
|
496
|
+
const consensusRegisterCollectionFactory = ConsensusRegisterCollection.getFactory();
|
|
497
|
+
const dataTypes = new Map<string, IChannelFactory>();
|
|
498
|
+
dataTypes.set(mapFactory.type, mapFactory);
|
|
499
|
+
dataTypes.set(consensusRegisterCollectionFactory.type, consensusRegisterCollectionFactory);
|
|
500
|
+
|
|
501
|
+
return new AgentSchedulerRuntime(context, dataTypes, existing);
|
|
502
|
+
}
|
|
453
503
|
}
|