forked from microsoft/FluidFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateAndLoadContainerUtils.ts
More file actions
376 lines (344 loc) · 11.4 KB
/
createAndLoadContainerUtils.ts
File metadata and controls
376 lines (344 loc) · 11.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
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import type {
IContainer,
ICodeDetailsLoader,
IFluidCodeDetails,
IContainerPolicies,
} from "@fluidframework/container-definitions/internal";
import { LoaderHeader } from "@fluidframework/container-definitions/internal";
import type {
FluidObject,
IConfigProviderBase,
IRequest,
ITelemetryBaseLogger,
} from "@fluidframework/core-interfaces";
import type { IClientDetails } from "@fluidframework/driver-definitions";
import type {
IDocumentServiceFactory,
IUrlResolver,
} from "@fluidframework/driver-definitions/internal";
import { DriverHeader } from "@fluidframework/driver-definitions/internal";
import {
GenericError,
normalizeError,
createChildMonitoringContext,
mixinMonitoringContext,
sessionStorageConfigProvider,
PerformanceEvent,
isFluidError,
} from "@fluidframework/telemetry-utils/internal";
import { v4 as uuid } from "uuid";
import { DebugLogger } from "./debugLogger.js";
import { createFrozenDocumentServiceFactory } from "./frozenServices.js";
import { Loader } from "./loader.js";
import { pkgVersion } from "./packageVersion.js";
import type { ProtocolHandlerBuilder } from "./protocol.js";
import type {
LoadSummarizerSummaryResult,
OnDemandSummaryResults,
SummarizeOnDemandResults,
} from "./summarizerResultTypes.js";
interface OnDemandSummarizeResultsPromises {
readonly summarySubmitted: Promise<SummarizeOnDemandResults["summarySubmitted"]>;
readonly summaryOpBroadcasted: Promise<SummarizeOnDemandResults["summaryOpBroadcasted"]>;
}
interface OnDemandSummarizeOptions {
readonly reason?: string;
readonly retryOnFailure?: boolean;
readonly fullTree?: boolean;
}
interface SummarizerLike {
readonly ISummarizer?: SummarizerLike;
summarizeOnDemand(options: OnDemandSummarizeOptions): OnDemandSummarizeResultsPromises;
}
/**
* Properties necessary for creating and loading a container.
* @legacy @beta
*/
export interface ICreateAndLoadContainerProps {
/**
* The url resolver used by the loader for resolving external urls
* into Fluid urls such that the container specified by the
* external url can be loaded.
*/
readonly urlResolver: IUrlResolver;
/**
* The document service factory take the Fluid url provided
* by the resolved url and constructs all the necessary services
* for communication with the container's server.
*/
readonly documentServiceFactory: IDocumentServiceFactory;
/**
* The code loader handles loading the necessary code
* for running a container once it is loaded.
*/
readonly codeLoader: ICodeDetailsLoader;
/**
* A property bag of options/policies used by various layers
* to control features
*/
readonly options?: IContainerPolicies | undefined;
/**
* Scope is provided to all container and is a set of shared
* services for container's to integrate with their host environment.
*/
readonly scope?: FluidObject | undefined;
/**
* The logger that all telemetry should be pushed to.
*/
readonly logger?: ITelemetryBaseLogger | undefined;
/**
* The configuration provider which may be used to control features.
*/
readonly configProvider?: IConfigProviderBase | undefined;
/**
* Optional property for allowing the container to use a custom
* protocol implementation for handling the quorum and/or the audience.
*/
readonly protocolHandlerBuilder?: ProtocolHandlerBuilder | undefined;
/**
* Disables the Container from reconnecting if false, allows reconnect otherwise.
*/
readonly allowReconnect?: boolean | undefined;
/**
* Client details provided in the override will be merged over the default client.
*/
readonly clientDetailsOverride?: IClientDetails | undefined;
}
/**
* Props used to load a container.
* @legacy @beta
*/
export interface ILoadExistingContainerProps extends ICreateAndLoadContainerProps {
/**
* The request to resolve the container.
*/
readonly request: IRequest;
/**
* Pending local state to be applied to the container.
*/
readonly pendingLocalState?: string | undefined;
}
/**
* Props used to load summarizer container.
* @legacy @alpha
*/
export type ILoadSummarizerContainerProps = Omit<
ILoadExistingContainerProps,
"pendingLocalState"
>;
/**
* Props used to create a detached container.
* @legacy @beta
*/
export interface ICreateDetachedContainerProps extends ICreateAndLoadContainerProps {
/**
* The code details for the container to be created.
*/
readonly codeDetails: IFluidCodeDetails;
}
/**
* Props used to rehydrate a detached container.
* @legacy @beta
*/
export interface IRehydrateDetachedContainerProps extends ICreateAndLoadContainerProps {
/**
* The serialized state returned by calling serialize on another container
*/
readonly serializedState: string;
}
/**
* Creates a new container using the specified code details but in an unattached state. While unattached, all
* updates will only be local until the user explicitly attaches the container to a service provider.
* @param createDetachedContainerProps - Services and properties necessary for creating detached container.
* @legacy @beta
*/
export async function createDetachedContainer(
createDetachedContainerProps: ICreateDetachedContainerProps,
): Promise<IContainer> {
const loader = new Loader(createDetachedContainerProps);
return loader.createDetachedContainer(createDetachedContainerProps.codeDetails, {
canReconnect: createDetachedContainerProps.allowReconnect,
clientDetailsOverride: createDetachedContainerProps.clientDetailsOverride,
});
}
/**
* Creates a new container using the specified snapshot but in an unattached state. While unattached, all
* updates will only be local until the user explicitly attaches the container to a service provider.
* @param rehydrateDetachedContainerProps - Services and properties necessary for rehydrating detached container from a previously serialized container's state.
* @legacy @beta
*/
export async function rehydrateDetachedContainer(
rehydrateDetachedContainerProps: IRehydrateDetachedContainerProps,
): Promise<IContainer> {
const loader = new Loader(rehydrateDetachedContainerProps);
return loader.rehydrateDetachedContainerFromSnapshot(
rehydrateDetachedContainerProps.serializedState,
{
canReconnect: rehydrateDetachedContainerProps.allowReconnect,
clientDetailsOverride: rehydrateDetachedContainerProps.clientDetailsOverride,
},
);
}
/**
* Loads a container with an existing snapshot from the service.
* @param loadExistingContainerProps - Services and properties necessary for loading an existing container.
* @legacy @beta
*/
export async function loadExistingContainer(
loadExistingContainerProps: ILoadExistingContainerProps,
): Promise<IContainer> {
const loader = new Loader(loadExistingContainerProps);
return loader.resolve(
loadExistingContainerProps.request,
loadExistingContainerProps.pendingLocalState,
);
}
/**
* Properties required to load a frozen container from pending state.
* @legacy @alpha
*/
export interface ILoadFrozenContainerFromPendingStateProps
extends ILoadExistingContainerProps {
/**
* Pending local state to be applied to the container.
*/
readonly pendingLocalState: string;
}
/**
* Loads a frozen container from pending local state.
* @param props - Properties required to load a frozen container from pending state.
* @legacy @alpha
*/
export async function loadFrozenContainerFromPendingState(
props: ILoadFrozenContainerFromPendingStateProps,
): Promise<IContainer> {
return loadExistingContainer({
...props,
documentServiceFactory: createFrozenDocumentServiceFactory(props.documentServiceFactory),
});
}
/**
* Loads a summarizer container with the required headers, triggers an on-demand summary, and then closes it.
* Returns success/failure and an optional error for host-side handling.
*
* @legacy @alpha
*/
export async function loadSummarizerContainerAndMakeSummary(
loadSummarizerContainerProps: ILoadSummarizerContainerProps,
): Promise<LoadSummarizerSummaryResult> {
const { logger, configProvider, request: originalRequest } = loadSummarizerContainerProps;
const telemetryProps = {
loaderId: uuid(),
loaderVersion: pkgVersion,
};
const subMc = mixinMonitoringContext(
DebugLogger.mixinDebugLogger("fluid:telemetry", logger, {
all: telemetryProps,
}),
sessionStorageConfigProvider.value,
configProvider,
);
const mc = createChildMonitoringContext({
logger: subMc.logger,
namespace: "SummarizerOnDemand",
});
return PerformanceEvent.timedExecAsync(
mc.logger,
{ eventName: "SummarizerOnDemandSummary" },
async (event) => {
const baseHeaders = originalRequest.headers;
const request = {
...originalRequest,
headers: {
...baseHeaders,
[LoaderHeader.cache]: false,
[LoaderHeader.clientDetails]: {
capabilities: { interactive: false },
type: "summarizer",
},
[DriverHeader.summarizingClient]: true,
[LoaderHeader.reconnect]: false,
},
};
const container = await loadExistingContainer({
...loadSummarizerContainerProps,
request,
});
let summarySubmitted: SummarizeOnDemandResults["summarySubmitted"];
let summaryOpBroadcasted: SummarizeOnDemandResults["summaryOpBroadcasted"];
try {
if (container.getEntryPoint === undefined) {
throw new GenericError("container.getEntryPoint() is undefined");
}
const fluidObject = (await container.getEntryPoint()) as FluidObject<SummarizerLike>;
const summarizer = fluidObject?.ISummarizer;
if (summarizer === undefined) {
throw new GenericError("Summarizer entry point not available");
}
// Host controlled feature gate for fullTree
// Default value will be false
const fullTreeGate =
mc.config.getBoolean("Fluid.Summarizer.FullTree.OnDemand") === true;
const summarizeResults: OnDemandSummarizeResultsPromises =
summarizer.summarizeOnDemand({
reason: "summaryOnRequest",
retryOnFailure: true,
fullTree: fullTreeGate,
});
[summarySubmitted, summaryOpBroadcasted] = await Promise.all([
summarizeResults.summarySubmitted,
summarizeResults.summaryOpBroadcasted,
]);
const summaryResults: OnDemandSummaryResults = {
summarySubmitted: summarySubmitted.success,
summaryInfo: summarySubmitted.success
? {
stage: summarySubmitted.data.stage,
handle: summaryOpBroadcasted.success
? summaryOpBroadcasted.data.summarizeOp.contents.handle
: undefined,
}
: {},
summaryOpBroadcasted: summaryOpBroadcasted.success,
};
if (summarySubmitted.success && summaryOpBroadcasted.success) {
event.end({
success: true,
summarySubmitted: true,
summaryOpBroadcasted: true,
});
return {
success: true,
summaryResults,
};
}
const failureError =
summarySubmitted.success === false
? summarySubmitted.error
: summaryOpBroadcasted.success === false
? summaryOpBroadcasted.error
: new GenericError("On demand summary failed");
event.end({
success: false,
summarySubmitted: summarySubmitted.success,
summaryOpBroadcasted: summaryOpBroadcasted.success,
});
return {
success: false,
error: failureError,
};
} catch (error) {
event.cancel({ success: false }, error);
const caughtError = isFluidError(error) ? error : normalizeError(error);
return { success: false, error: caughtError };
} finally {
container.dispose();
}
},
{ start: true, end: true, cancel: "generic" },
);
}