@fluidframework/container-loader 2.0.0-dev.2.2.0.111723 → 2.0.0-dev.3.1.0.125672

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.
Files changed (159) hide show
  1. package/.eslintrc.js +18 -21
  2. package/.mocharc.js +2 -2
  3. package/README.md +58 -40
  4. package/api-extractor.json +2 -2
  5. package/dist/audience.d.ts +0 -1
  6. package/dist/audience.d.ts.map +1 -1
  7. package/dist/audience.js +6 -1
  8. package/dist/audience.js.map +1 -1
  9. package/dist/catchUpMonitor.d.ts.map +1 -1
  10. package/dist/catchUpMonitor.js.map +1 -1
  11. package/dist/collabWindowTracker.d.ts.map +1 -1
  12. package/dist/collabWindowTracker.js +5 -4
  13. package/dist/collabWindowTracker.js.map +1 -1
  14. package/dist/connectionManager.d.ts +5 -5
  15. package/dist/connectionManager.d.ts.map +1 -1
  16. package/dist/connectionManager.js +66 -32
  17. package/dist/connectionManager.js.map +1 -1
  18. package/dist/connectionState.d.ts.map +1 -1
  19. package/dist/connectionState.js.map +1 -1
  20. package/dist/connectionStateHandler.d.ts +3 -3
  21. package/dist/connectionStateHandler.d.ts.map +1 -1
  22. package/dist/connectionStateHandler.js +46 -24
  23. package/dist/connectionStateHandler.js.map +1 -1
  24. package/dist/container.d.ts +27 -0
  25. package/dist/container.d.ts.map +1 -1
  26. package/dist/container.js +191 -57
  27. package/dist/container.js.map +1 -1
  28. package/dist/containerContext.d.ts +3 -2
  29. package/dist/containerContext.d.ts.map +1 -1
  30. package/dist/containerContext.js +11 -6
  31. package/dist/containerContext.js.map +1 -1
  32. package/dist/containerStorageAdapter.d.ts.map +1 -1
  33. package/dist/containerStorageAdapter.js +2 -4
  34. package/dist/containerStorageAdapter.js.map +1 -1
  35. package/dist/contracts.d.ts +9 -1
  36. package/dist/contracts.d.ts.map +1 -1
  37. package/dist/contracts.js.map +1 -1
  38. package/dist/deltaManager.d.ts +18 -6
  39. package/dist/deltaManager.d.ts.map +1 -1
  40. package/dist/deltaManager.js +110 -37
  41. package/dist/deltaManager.js.map +1 -1
  42. package/dist/deltaManagerProxy.d.ts.map +1 -1
  43. package/dist/deltaManagerProxy.js.map +1 -1
  44. package/dist/deltaQueue.d.ts.map +1 -1
  45. package/dist/deltaQueue.js +4 -2
  46. package/dist/deltaQueue.js.map +1 -1
  47. package/dist/index.d.ts +1 -1
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js.map +1 -1
  50. package/dist/loader.d.ts +3 -3
  51. package/dist/loader.d.ts.map +1 -1
  52. package/dist/loader.js +16 -8
  53. package/dist/loader.js.map +1 -1
  54. package/dist/packageVersion.d.ts +1 -1
  55. package/dist/packageVersion.js +1 -1
  56. package/dist/packageVersion.js.map +1 -1
  57. package/dist/protocol.d.ts.map +1 -1
  58. package/dist/protocol.js +2 -1
  59. package/dist/protocol.js.map +1 -1
  60. package/dist/protocolTreeDocumentStorageService.d.ts.map +1 -1
  61. package/dist/protocolTreeDocumentStorageService.js.map +1 -1
  62. package/dist/quorum.d.ts.map +1 -1
  63. package/dist/quorum.js.map +1 -1
  64. package/dist/retriableDocumentStorageService.d.ts.map +1 -1
  65. package/dist/retriableDocumentStorageService.js +6 -2
  66. package/dist/retriableDocumentStorageService.js.map +1 -1
  67. package/dist/utils.d.ts.map +1 -1
  68. package/dist/utils.js +6 -4
  69. package/dist/utils.js.map +1 -1
  70. package/lib/audience.d.ts +0 -1
  71. package/lib/audience.d.ts.map +1 -1
  72. package/lib/audience.js +6 -1
  73. package/lib/audience.js.map +1 -1
  74. package/lib/catchUpMonitor.d.ts.map +1 -1
  75. package/lib/catchUpMonitor.js.map +1 -1
  76. package/lib/collabWindowTracker.d.ts.map +1 -1
  77. package/lib/collabWindowTracker.js +5 -4
  78. package/lib/collabWindowTracker.js.map +1 -1
  79. package/lib/connectionManager.d.ts +5 -5
  80. package/lib/connectionManager.d.ts.map +1 -1
  81. package/lib/connectionManager.js +68 -34
  82. package/lib/connectionManager.js.map +1 -1
  83. package/lib/connectionState.d.ts.map +1 -1
  84. package/lib/connectionState.js.map +1 -1
  85. package/lib/connectionStateHandler.d.ts +3 -3
  86. package/lib/connectionStateHandler.d.ts.map +1 -1
  87. package/lib/connectionStateHandler.js +46 -24
  88. package/lib/connectionStateHandler.js.map +1 -1
  89. package/lib/container.d.ts +27 -0
  90. package/lib/container.d.ts.map +1 -1
  91. package/lib/container.js +194 -61
  92. package/lib/container.js.map +1 -1
  93. package/lib/containerContext.d.ts +3 -2
  94. package/lib/containerContext.d.ts.map +1 -1
  95. package/lib/containerContext.js +11 -6
  96. package/lib/containerContext.js.map +1 -1
  97. package/lib/containerStorageAdapter.d.ts.map +1 -1
  98. package/lib/containerStorageAdapter.js +2 -4
  99. package/lib/containerStorageAdapter.js.map +1 -1
  100. package/lib/contracts.d.ts +9 -1
  101. package/lib/contracts.d.ts.map +1 -1
  102. package/lib/contracts.js.map +1 -1
  103. package/lib/deltaManager.d.ts +18 -6
  104. package/lib/deltaManager.d.ts.map +1 -1
  105. package/lib/deltaManager.js +112 -39
  106. package/lib/deltaManager.js.map +1 -1
  107. package/lib/deltaManagerProxy.d.ts.map +1 -1
  108. package/lib/deltaManagerProxy.js.map +1 -1
  109. package/lib/deltaQueue.d.ts.map +1 -1
  110. package/lib/deltaQueue.js +4 -2
  111. package/lib/deltaQueue.js.map +1 -1
  112. package/lib/index.d.ts +1 -1
  113. package/lib/index.d.ts.map +1 -1
  114. package/lib/index.js.map +1 -1
  115. package/lib/loader.d.ts +3 -3
  116. package/lib/loader.d.ts.map +1 -1
  117. package/lib/loader.js +16 -8
  118. package/lib/loader.js.map +1 -1
  119. package/lib/packageVersion.d.ts +1 -1
  120. package/lib/packageVersion.js +1 -1
  121. package/lib/packageVersion.js.map +1 -1
  122. package/lib/protocol.d.ts.map +1 -1
  123. package/lib/protocol.js +2 -1
  124. package/lib/protocol.js.map +1 -1
  125. package/lib/protocolTreeDocumentStorageService.d.ts.map +1 -1
  126. package/lib/protocolTreeDocumentStorageService.js.map +1 -1
  127. package/lib/quorum.d.ts.map +1 -1
  128. package/lib/quorum.js.map +1 -1
  129. package/lib/retriableDocumentStorageService.d.ts.map +1 -1
  130. package/lib/retriableDocumentStorageService.js +6 -2
  131. package/lib/retriableDocumentStorageService.js.map +1 -1
  132. package/lib/utils.d.ts.map +1 -1
  133. package/lib/utils.js +6 -4
  134. package/lib/utils.js.map +1 -1
  135. package/package.json +22 -19
  136. package/prettier.config.cjs +1 -1
  137. package/src/audience.ts +52 -42
  138. package/src/catchUpMonitor.ts +39 -37
  139. package/src/collabWindowTracker.ts +75 -70
  140. package/src/connectionManager.ts +1009 -938
  141. package/src/connectionState.ts +19 -19
  142. package/src/connectionStateHandler.ts +544 -462
  143. package/src/container.ts +2040 -1785
  144. package/src/containerContext.ts +352 -337
  145. package/src/containerStorageAdapter.ts +163 -153
  146. package/src/contracts.ts +155 -145
  147. package/src/deltaManager.ts +1069 -945
  148. package/src/deltaManagerProxy.ts +143 -137
  149. package/src/deltaQueue.ts +155 -151
  150. package/src/index.ts +14 -17
  151. package/src/loader.ts +427 -422
  152. package/src/packageVersion.ts +1 -1
  153. package/src/protocol.ts +93 -87
  154. package/src/protocolTreeDocumentStorageService.ts +30 -33
  155. package/src/quorum.ts +34 -34
  156. package/src/retriableDocumentStorageService.ts +118 -102
  157. package/src/utils.ts +89 -82
  158. package/tsconfig.esnext.json +6 -6
  159. package/tsconfig.json +8 -12
package/src/container.ts CHANGED
@@ -7,83 +7,78 @@
7
7
  import merge from "lodash/merge";
8
8
  import { v4 as uuid } from "uuid";
9
9
  import {
10
- ITelemetryLogger, ITelemetryProperties,
10
+ ITelemetryLogger,
11
+ ITelemetryProperties,
12
+ TelemetryEventCategory,
11
13
  } from "@fluidframework/common-definitions";
12
14
  import { assert, performance, unreachableCase } from "@fluidframework/common-utils";
15
+ import { IRequest, IResponse, IFluidRouter } from "@fluidframework/core-interfaces";
13
16
  import {
14
- IRequest,
15
- IResponse,
16
- IFluidRouter,
17
- } from "@fluidframework/core-interfaces";
18
- import {
19
- IAudience,
20
- IConnectionDetails,
21
- IContainer,
22
- IContainerEvents,
23
- IDeltaManager,
24
- ICriticalContainerError,
25
- ContainerWarning,
26
- AttachState,
27
- IThrottlingWarning,
28
- ReadOnlyInfo,
29
- IContainerLoadMode,
30
- IFluidCodeDetails,
31
- isFluidCodeDetails,
32
- IBatchMessage,
17
+ IAudience,
18
+ IConnectionDetails,
19
+ IContainer,
20
+ IContainerEvents,
21
+ IDeltaManager,
22
+ ICriticalContainerError,
23
+ ContainerWarning,
24
+ AttachState,
25
+ IThrottlingWarning,
26
+ ReadOnlyInfo,
27
+ IContainerLoadMode,
28
+ IFluidCodeDetails,
29
+ isFluidCodeDetails,
30
+ IBatchMessage,
33
31
  } from "@fluidframework/container-definitions";
32
+ import { GenericError, UsageError } from "@fluidframework/container-utils";
34
33
  import {
35
- GenericError,
36
- UsageError,
37
- } from "@fluidframework/container-utils";
38
- import {
39
- IDocumentService,
40
- IDocumentStorageService,
41
- IFluidResolvedUrl,
42
- IResolvedUrl,
34
+ IDocumentService,
35
+ IDocumentStorageService,
36
+ IFluidResolvedUrl,
37
+ IResolvedUrl,
43
38
  } from "@fluidframework/driver-definitions";
44
39
  import {
45
- readAndParse,
46
- OnlineStatus,
47
- isOnline,
48
- ensureFluidResolvedUrl,
49
- combineAppAndProtocolSummary,
50
- runWithRetry,
51
- isFluidResolvedUrl,
40
+ readAndParse,
41
+ OnlineStatus,
42
+ isOnline,
43
+ ensureFluidResolvedUrl,
44
+ combineAppAndProtocolSummary,
45
+ runWithRetry,
46
+ isFluidResolvedUrl,
52
47
  } from "@fluidframework/driver-utils";
53
48
  import { IQuorumSnapshot } from "@fluidframework/protocol-base";
54
49
  import {
55
- IClient,
56
- IClientConfiguration,
57
- IClientDetails,
58
- ICommittedProposal,
59
- IDocumentAttributes,
60
- IDocumentMessage,
61
- IProtocolState,
62
- IQuorumClients,
63
- IQuorumProposals,
64
- ISequencedClient,
65
- ISequencedDocumentMessage,
66
- ISequencedProposal,
67
- ISignalMessage,
68
- ISnapshotTree,
69
- ISummaryContent,
70
- ISummaryTree,
71
- IVersion,
72
- MessageType,
73
- SummaryType,
50
+ IClient,
51
+ IClientConfiguration,
52
+ IClientDetails,
53
+ ICommittedProposal,
54
+ IDocumentAttributes,
55
+ IDocumentMessage,
56
+ IProtocolState,
57
+ IQuorumClients,
58
+ IQuorumProposals,
59
+ ISequencedClient,
60
+ ISequencedDocumentMessage,
61
+ ISequencedProposal,
62
+ ISignalMessage,
63
+ ISnapshotTree,
64
+ ISummaryContent,
65
+ ISummaryTree,
66
+ IVersion,
67
+ MessageType,
68
+ SummaryType,
74
69
  } from "@fluidframework/protocol-definitions";
75
70
  import {
76
- ChildLogger,
77
- EventEmitterWithErrorHandling,
78
- PerformanceEvent,
79
- raiseConnectedEvent,
80
- TelemetryLogger,
81
- connectedEventName,
82
- disconnectedEventName,
83
- normalizeError,
84
- MonitoringContext,
85
- loggerToMonitoringContext,
86
- wrapError,
71
+ ChildLogger,
72
+ EventEmitterWithErrorHandling,
73
+ PerformanceEvent,
74
+ raiseConnectedEvent,
75
+ TelemetryLogger,
76
+ connectedEventName,
77
+ disconnectedEventName,
78
+ normalizeError,
79
+ MonitoringContext,
80
+ loggerToMonitoringContext,
81
+ wrapError,
87
82
  } from "@fluidframework/telemetry-utils";
88
83
  import { Audience } from "./audience";
89
84
  import { ContainerContext } from "./containerContext";
@@ -93,20 +88,17 @@ import { DeltaManagerProxy } from "./deltaManagerProxy";
93
88
  import { ILoaderOptions, Loader, RelativeLoader } from "./loader";
94
89
  import { pkgVersion } from "./packageVersion";
95
90
  import { ContainerStorageAdapter } from "./containerStorageAdapter";
96
- import {
97
- IConnectionStateHandler,
98
- createConnectionStateHandler,
99
- } from "./connectionStateHandler";
91
+ import { IConnectionStateHandler, createConnectionStateHandler } from "./connectionStateHandler";
100
92
  import { getProtocolSnapshotTree, getSnapshotTreeFromSerializedContainer } from "./utils";
101
- import { initQuorumValuesFromCodeDetails, getCodeDetailsFromQuorumValues, QuorumProxy } from "./quorum";
93
+ import {
94
+ initQuorumValuesFromCodeDetails,
95
+ getCodeDetailsFromQuorumValues,
96
+ QuorumProxy,
97
+ } from "./quorum";
102
98
  import { CollabWindowTracker } from "./collabWindowTracker";
103
99
  import { ConnectionManager } from "./connectionManager";
104
100
  import { ConnectionState } from "./connectionState";
105
- import {
106
- IProtocolHandler,
107
- ProtocolHandler,
108
- ProtocolHandlerBuilder,
109
- } from "./protocol";
101
+ import { IProtocolHandler, ProtocolHandler, ProtocolHandlerBuilder } from "./protocol";
110
102
 
111
103
  const detachedContainerRefSeqNumber = 0;
112
104
 
@@ -114,36 +106,36 @@ const dirtyContainerEvent = "dirty";
114
106
  const savedContainerEvent = "saved";
115
107
 
116
108
  export interface IContainerLoadOptions {
117
- /**
118
- * Disables the Container from reconnecting if false, allows reconnect otherwise.
119
- */
120
- canReconnect?: boolean;
121
- /**
122
- * Client details provided in the override will be merged over the default client.
123
- */
124
- clientDetailsOverride?: IClientDetails;
125
- resolvedUrl: IFluidResolvedUrl;
126
- /**
127
- * Control which snapshot version to load from. See IParsedUrl for detailed information.
128
- */
129
- version: string | undefined;
130
- /**
131
- * Loads the Container in paused state if true, unpaused otherwise.
132
- */
133
- loadMode?: IContainerLoadMode;
109
+ /**
110
+ * Disables the Container from reconnecting if false, allows reconnect otherwise.
111
+ */
112
+ canReconnect?: boolean;
113
+ /**
114
+ * Client details provided in the override will be merged over the default client.
115
+ */
116
+ clientDetailsOverride?: IClientDetails;
117
+ resolvedUrl: IFluidResolvedUrl;
118
+ /**
119
+ * Control which snapshot version to load from. See IParsedUrl for detailed information.
120
+ */
121
+ version: string | undefined;
122
+ /**
123
+ * Loads the Container in paused state if true, unpaused otherwise.
124
+ */
125
+ loadMode?: IContainerLoadMode;
134
126
  }
135
127
 
136
128
  export interface IContainerConfig {
137
- resolvedUrl?: IFluidResolvedUrl;
138
- canReconnect?: boolean;
139
- /**
140
- * Client details provided in the override will be merged over the default client.
141
- */
142
- clientDetailsOverride?: IClientDetails;
143
- /**
144
- * Serialized state from a previous instance of this container
145
- */
146
- serializedContainerState?: IPendingContainerState;
129
+ resolvedUrl?: IFluidResolvedUrl;
130
+ canReconnect?: boolean;
131
+ /**
132
+ * Client details provided in the override will be merged over the default client.
133
+ */
134
+ clientDetailsOverride?: IClientDetails;
135
+ /**
136
+ * Serialized state from a previous instance of this container
137
+ */
138
+ serializedContainerState?: IPendingContainerState;
147
139
  }
148
140
 
149
141
  /**
@@ -163,94 +155,101 @@ export interface IContainerConfig {
163
155
  * @throws an error beginning with `"Container closed"` if the container is closed before it catches up.
164
156
  */
165
157
  export async function waitContainerToCatchUp(container: IContainer) {
166
- // Make sure we stop waiting if container is closed.
167
- if (container.closed) {
168
- throw new UsageError("waitContainerToCatchUp: Container closed");
169
- }
170
-
171
- return new Promise<boolean>((resolve, reject) => {
172
- const deltaManager = container.deltaManager;
173
-
174
- const closedCallback = (err?: ICriticalContainerError | undefined) => {
175
- container.off("closed", closedCallback);
176
- const baseMessage = "Container closed while waiting to catch up";
177
- reject(
178
- err !== undefined
179
- ? wrapError(err, (innerMessage) => new GenericError(`${baseMessage}: ${innerMessage}`))
180
- : new GenericError(baseMessage),
181
- );
182
- };
183
- container.on("closed", closedCallback);
184
-
185
- // Depending on config, transition to "connected" state may include the guarantee
186
- // that all known ops have been processed. If so, we may introduce additional wait here.
187
- // Waiting for "connected" state in either case gets us at least to our own Join op
188
- // which is a reasonable approximation of "caught up"
189
- const waitForOps = () => {
190
- assert(container.connectionState === ConnectionState.CatchingUp
191
- || container.connectionState === ConnectionState.Connected,
192
- 0x0cd /* "Container disconnected while waiting for ops!" */);
193
- const hasCheckpointSequenceNumber = deltaManager.hasCheckpointSequenceNumber;
194
-
195
- const connectionOpSeqNumber = deltaManager.lastKnownSeqNumber;
196
- assert(deltaManager.lastSequenceNumber <= connectionOpSeqNumber,
197
- 0x266 /* "lastKnownSeqNumber should never be below last processed sequence number" */);
198
- if (deltaManager.lastSequenceNumber === connectionOpSeqNumber) {
199
- container.off("closed", closedCallback);
200
- resolve(hasCheckpointSequenceNumber);
201
- return;
202
- }
203
- const callbackOps = (message: ISequencedDocumentMessage) => {
204
- if (connectionOpSeqNumber <= message.sequenceNumber) {
205
- container.off("closed", closedCallback);
206
- resolve(hasCheckpointSequenceNumber);
207
- deltaManager.off("op", callbackOps);
208
- }
209
- };
210
- deltaManager.on("op", callbackOps);
211
- };
212
-
213
- // We can leverage DeltaManager's "connect" event here and test for ConnectionState.Disconnected
214
- // But that works only if service provides us checkPointSequenceNumber
215
- // Our internal testing is based on R11S that does not, but almost all tests connect as "write" and
216
- // use this function to catch up, so leveraging our own join op as a fence/barrier
217
- if (container.connectionState === ConnectionState.Connected) {
218
- waitForOps();
219
- return;
220
- }
221
-
222
- const callback = () => {
223
- container.off(connectedEventName, callback);
224
- waitForOps();
225
- };
226
- container.on(connectedEventName, callback);
227
-
228
- if (container.connectionState === ConnectionState.Disconnected) {
229
- container.connect();
230
- }
231
- });
158
+ // Make sure we stop waiting if container is closed.
159
+ if (container.closed) {
160
+ throw new UsageError("waitContainerToCatchUp: Container closed");
161
+ }
162
+
163
+ return new Promise<boolean>((resolve, reject) => {
164
+ const deltaManager = container.deltaManager;
165
+
166
+ const closedCallback = (err?: ICriticalContainerError | undefined) => {
167
+ container.off("closed", closedCallback);
168
+ const baseMessage = "Container closed while waiting to catch up";
169
+ reject(
170
+ err !== undefined
171
+ ? wrapError(
172
+ err,
173
+ (innerMessage) => new GenericError(`${baseMessage}: ${innerMessage}`),
174
+ )
175
+ : new GenericError(baseMessage),
176
+ );
177
+ };
178
+ container.on("closed", closedCallback);
179
+
180
+ // Depending on config, transition to "connected" state may include the guarantee
181
+ // that all known ops have been processed. If so, we may introduce additional wait here.
182
+ // Waiting for "connected" state in either case gets us at least to our own Join op
183
+ // which is a reasonable approximation of "caught up"
184
+ const waitForOps = () => {
185
+ assert(
186
+ container.connectionState === ConnectionState.CatchingUp ||
187
+ container.connectionState === ConnectionState.Connected,
188
+ 0x0cd /* "Container disconnected while waiting for ops!" */,
189
+ );
190
+ const hasCheckpointSequenceNumber = deltaManager.hasCheckpointSequenceNumber;
191
+
192
+ const connectionOpSeqNumber = deltaManager.lastKnownSeqNumber;
193
+ assert(
194
+ deltaManager.lastSequenceNumber <= connectionOpSeqNumber,
195
+ 0x266 /* "lastKnownSeqNumber should never be below last processed sequence number" */,
196
+ );
197
+ if (deltaManager.lastSequenceNumber === connectionOpSeqNumber) {
198
+ container.off("closed", closedCallback);
199
+ resolve(hasCheckpointSequenceNumber);
200
+ return;
201
+ }
202
+ const callbackOps = (message: ISequencedDocumentMessage) => {
203
+ if (connectionOpSeqNumber <= message.sequenceNumber) {
204
+ container.off("closed", closedCallback);
205
+ resolve(hasCheckpointSequenceNumber);
206
+ deltaManager.off("op", callbackOps);
207
+ }
208
+ };
209
+ deltaManager.on("op", callbackOps);
210
+ };
211
+
212
+ // We can leverage DeltaManager's "connect" event here and test for ConnectionState.Disconnected
213
+ // But that works only if service provides us checkPointSequenceNumber
214
+ // Our internal testing is based on R11S that does not, but almost all tests connect as "write" and
215
+ // use this function to catch up, so leveraging our own join op as a fence/barrier
216
+ if (container.connectionState === ConnectionState.Connected) {
217
+ waitForOps();
218
+ return;
219
+ }
220
+
221
+ const callback = () => {
222
+ container.off(connectedEventName, callback);
223
+ waitForOps();
224
+ };
225
+ container.on(connectedEventName, callback);
226
+
227
+ if (container.connectionState === ConnectionState.Disconnected) {
228
+ container.connect();
229
+ }
230
+ });
232
231
  }
233
232
 
234
233
  const getCodeProposal =
235
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
236
- (quorum: IQuorumProposals) => quorum.get("code") ?? quorum.get("code2");
234
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
235
+ (quorum: IQuorumProposals) => quorum.get("code") ?? quorum.get("code2");
237
236
 
238
237
  /**
239
- * Helper function to report to telemetry cases where operation takes longer than expected (1s)
238
+ * Helper function to report to telemetry cases where operation takes longer than expected (200ms)
240
239
  * @param logger - logger to use
241
240
  * @param eventName - event name
242
241
  * @param action - functor to call and measure
243
242
  */
244
- async function ReportIfTooLong(
245
- logger: ITelemetryLogger,
246
- eventName: string,
247
- action: () => Promise<ITelemetryProperties>,
243
+ export async function ReportIfTooLong(
244
+ logger: ITelemetryLogger,
245
+ eventName: string,
246
+ action: () => Promise<ITelemetryProperties>,
248
247
  ) {
249
- const event = PerformanceEvent.start(logger, { eventName });
250
- const props = await action();
251
- if (event.duration > 1000) {
252
- event.end(props);
253
- }
248
+ const event = PerformanceEvent.start(logger, { eventName });
249
+ const props = await action();
250
+ if (event.duration > 200) {
251
+ event.end(props);
252
+ }
254
253
  }
255
254
 
256
255
  /**
@@ -258,1613 +257,1869 @@ async function ReportIfTooLong(
258
257
  * of the container to the same state
259
258
  */
260
259
  export interface IPendingContainerState {
261
- pendingRuntimeState: unknown;
262
- url: string;
263
- protocol: IProtocolState;
264
- term: number;
265
- clientId?: string;
260
+ pendingRuntimeState: unknown;
261
+ url: string;
262
+ protocol: IProtocolState;
263
+ term: number;
264
+ clientId?: string;
266
265
  }
267
266
 
268
267
  const summarizerClientType = "summarizer";
269
268
 
270
- export class Container extends EventEmitterWithErrorHandling<IContainerEvents> implements IContainer {
271
- public static version = "^0.1.0";
272
-
273
- /**
274
- * Load an existing container.
275
- */
276
- public static async load(
277
- loader: Loader,
278
- loadOptions: IContainerLoadOptions,
279
- pendingLocalState?: IPendingContainerState,
280
- protocolHandlerBuilder?: ProtocolHandlerBuilder,
281
- ): Promise<Container> {
282
- const container = new Container(
283
- loader,
284
- {
285
- clientDetailsOverride: loadOptions.clientDetailsOverride,
286
- resolvedUrl: loadOptions.resolvedUrl,
287
- canReconnect: loadOptions.canReconnect,
288
- serializedContainerState: pendingLocalState,
289
- },
290
- protocolHandlerBuilder);
291
-
292
- return PerformanceEvent.timedExecAsync(
293
- container.mc.logger,
294
- { eventName: "Load" },
295
- async (event) => new Promise<Container>((resolve, reject) => {
296
- const version = loadOptions.version;
297
-
298
- const defaultMode: IContainerLoadMode = { opsBeforeReturn: "cached" };
299
- // if we have pendingLocalState, anything we cached is not useful and we shouldn't wait for connection
300
- // to return container, so ignore this value and use undefined for opsBeforeReturn
301
- const mode: IContainerLoadMode = pendingLocalState
302
- ? { ...(loadOptions.loadMode ?? defaultMode), opsBeforeReturn: undefined }
303
- : loadOptions.loadMode ?? defaultMode;
304
-
305
- const onClosed = (err?: ICriticalContainerError) => {
306
- // pre-0.58 error message: containerClosedWithoutErrorDuringLoad
307
- reject(err ?? new GenericError("Container closed without error during load"));
308
- };
309
- container.on("closed", onClosed);
310
-
311
- container.load(version, mode, pendingLocalState)
312
- .finally(() => {
313
- container.removeListener("closed", onClosed);
314
- })
315
- .then((props) => {
316
- event.end({ ...props, ...loadOptions.loadMode });
317
- resolve(container);
318
- },
319
- (error) => {
320
- const err = normalizeError(error);
321
- // Depending where error happens, we can be attempting to connect to web socket
322
- // and continuously retrying (consider offline mode)
323
- // Host has no container to close, so it's prudent to do it here
324
- container.close(err);
325
- onClosed(err);
326
- });
327
- }),
328
- { start: true, end: true, cancel: "generic" },
329
- );
330
- }
331
-
332
- /**
333
- * Create a new container in a detached state.
334
- */
335
- public static async createDetached(
336
- loader: Loader,
337
- codeDetails: IFluidCodeDetails,
338
- protocolHandlerBuilder?: ProtocolHandlerBuilder,
339
- ): Promise<Container> {
340
- const container = new Container(
341
- loader,
342
- {},
343
- protocolHandlerBuilder);
344
-
345
- return PerformanceEvent.timedExecAsync(
346
- container.mc.logger,
347
- { eventName: "CreateDetached" },
348
- async (_event) => {
349
- await container.createDetached(codeDetails);
350
- return container;
351
- },
352
- { start: true, end: true, cancel: "generic" });
353
- }
354
-
355
- /**
356
- * Create a new container in a detached state that is initialized with a
357
- * snapshot from a previous detached container.
358
- */
359
- public static async rehydrateDetachedFromSnapshot(
360
- loader: Loader,
361
- snapshot: string,
362
- protocolHandlerBuilder?: ProtocolHandlerBuilder,
363
- ): Promise<Container> {
364
- const container = new Container(
365
- loader,
366
- {},
367
- protocolHandlerBuilder);
368
-
369
- return PerformanceEvent.timedExecAsync(
370
- container.mc.logger,
371
- { eventName: "RehydrateDetachedFromSnapshot" },
372
- async (_event) => {
373
- const deserializedSummary = JSON.parse(snapshot) as ISummaryTree;
374
- await container.rehydrateDetachedFromSnapshot(deserializedSummary);
375
- return container;
376
- },
377
- { start: true, end: true, cancel: "generic" });
378
- }
379
-
380
- public subLogger: TelemetryLogger;
381
-
382
- // Tells if container can reconnect on losing fist connection
383
- // If false, container gets closed on loss of connection.
384
- private readonly _canReconnect: boolean = true;
385
-
386
- private readonly mc: MonitoringContext;
387
-
388
- private _lifecycleState: "loading" | "loaded" | "closing" | "closed" = "loading";
389
-
390
- private setLoaded() {
391
- // It's conceivable the container could be closed when this is called
392
- // Only transition states if currently loading
393
- if (this._lifecycleState === "loading") {
394
- // Propagate current connection state through the system.
395
- this.propagateConnectionState(true /* initial transition */);
396
- this._lifecycleState = "loaded";
397
- }
398
- }
399
-
400
- public get closed(): boolean {
401
- return (this._lifecycleState === "closing" || this._lifecycleState === "closed");
402
- }
403
-
404
- private _attachState = AttachState.Detached;
405
-
406
- private readonly storageService: ContainerStorageAdapter;
407
- public get storage(): IDocumentStorageService {
408
- return this.storageService;
409
- }
410
-
411
- private readonly clientDetailsOverride: IClientDetails | undefined;
412
- private readonly _deltaManager: DeltaManager<ConnectionManager>;
413
- private service: IDocumentService | undefined;
414
-
415
- private _context: ContainerContext | undefined;
416
- private get context() {
417
- if (this._context === undefined) {
418
- throw new GenericError("Attempted to access context before it was defined");
419
- }
420
- return this._context;
421
- }
422
- private _protocolHandler: IProtocolHandler | undefined;
423
- private get protocolHandler() {
424
- if (this._protocolHandler === undefined) {
425
- throw new Error("Attempted to access protocolHandler before it was defined");
426
- }
427
- return this._protocolHandler;
428
- }
429
-
430
- /** During initialization we pause the inbound queues. We track this state to ensure we only call resume once */
431
- private inboundQueuePausedFromInit = true;
432
- private firstConnection = true;
433
- private readonly connectionTransitionTimes: number[] = [];
434
- private messageCountAfterDisconnection: number = 0;
435
- private _loadedFromVersion: IVersion | undefined;
436
- private _resolvedUrl: IFluidResolvedUrl | undefined;
437
- private attachStarted = false;
438
- private _dirtyContainer = false;
439
-
440
- private lastVisible: number | undefined;
441
- private readonly visibilityEventHandler: (() => void) | undefined;
442
- private readonly connectionStateHandler: IConnectionStateHandler;
443
-
444
- private setAutoReconnectTime = performance.now();
445
-
446
- private collabWindowTracker: CollabWindowTracker | undefined;
447
-
448
- private get connectionMode() { return this._deltaManager.connectionManager.connectionMode; }
449
-
450
- public get IFluidRouter(): IFluidRouter { return this; }
451
-
452
- public get resolvedUrl(): IResolvedUrl | undefined {
453
- return this._resolvedUrl;
454
- }
455
-
456
- public get loadedFromVersion(): IVersion | undefined {
457
- return this._loadedFromVersion;
458
- }
459
-
460
- public get readOnlyInfo(): ReadOnlyInfo {
461
- return this._deltaManager.readOnlyInfo;
462
- }
463
-
464
- public get closeSignal(): AbortSignal {
465
- return this._deltaManager.closeAbortController.signal;
466
- }
467
-
468
- /**
469
- * Tracks host requiring read-only mode.
470
- */
471
- public forceReadonly(readonly: boolean) {
472
- this._deltaManager.connectionManager.forceReadonly(readonly);
473
- }
474
-
475
- public get deltaManager(): IDeltaManager<ISequencedDocumentMessage, IDocumentMessage> {
476
- return this._deltaManager;
477
- }
478
-
479
- public get connectionState(): ConnectionState {
480
- return this.connectionStateHandler.connectionState;
481
- }
482
-
483
- public get connected(): boolean {
484
- return this.connectionStateHandler.connectionState === ConnectionState.Connected;
485
- }
486
-
487
- /**
488
- * Service configuration details. If running in offline mode will be undefined otherwise will contain service
489
- * configuration details returned as part of the initial connection.
490
- */
491
- public get serviceConfiguration(): IClientConfiguration | undefined {
492
- return this._deltaManager.serviceConfiguration;
493
- }
494
-
495
- private _clientId: string | undefined;
496
-
497
- /**
498
- * The server provided id of the client.
499
- * Set once this.connected is true, otherwise undefined
500
- */
501
- public get clientId(): string | undefined {
502
- return this._clientId;
503
- }
504
-
505
- /**
506
- * The server provided claims of the client.
507
- * Set once this.connected is true, otherwise undefined
508
- */
509
- public get scopes(): string[] | undefined {
510
- return this._deltaManager.connectionManager.scopes;
511
- }
512
-
513
- public get clientDetails(): IClientDetails {
514
- return this._deltaManager.clientDetails;
515
- }
516
-
517
- /**
518
- * Get the code details that are currently specified for the container.
519
- * @returns The current code details if any are specified, undefined if none are specified.
520
- */
521
- public getSpecifiedCodeDetails(): IFluidCodeDetails | undefined {
522
- return this.getCodeDetailsFromQuorum();
523
- }
524
-
525
- /**
526
- * Get the code details that were used to load the container.
527
- * @returns The code details that were used to load the container if it is loaded, undefined if it is not yet
528
- * loaded.
529
- */
530
- public getLoadedCodeDetails(): IFluidCodeDetails | undefined {
531
- return this._context?.codeDetails;
532
- }
533
-
534
- /**
535
- * Retrieves the audience associated with the document
536
- */
537
- public get audience(): IAudience {
538
- return this.protocolHandler.audience;
539
- }
540
-
541
- /**
542
- * Returns true if container is dirty.
543
- * Which means data loss if container is closed at that same moment
544
- * Most likely that happens when there is no network connection to Relay Service
545
- */
546
- public get isDirty() {
547
- return this._dirtyContainer;
548
- }
549
-
550
- private get serviceFactory() { return this.loader.services.documentServiceFactory; }
551
- private get urlResolver() { return this.loader.services.urlResolver; }
552
- public readonly options: ILoaderOptions;
553
- private get scope() { return this.loader.services.scope; }
554
- private get codeLoader() { return this.loader.services.codeLoader; }
555
-
556
- constructor(
557
- private readonly loader: Loader,
558
- config: IContainerConfig,
559
- private readonly protocolHandlerBuilder?: ProtocolHandlerBuilder,
560
- ) {
561
- super((name, error) => {
562
- this.mc.logger.sendErrorEvent(
563
- {
564
- eventName: "ContainerEventHandlerException",
565
- name: typeof name === "string" ? name : undefined,
566
- },
567
- error);
568
- });
569
-
570
- this.clientDetailsOverride = config.clientDetailsOverride;
571
- this._resolvedUrl = config.resolvedUrl;
572
- if (config.canReconnect !== undefined) {
573
- this._canReconnect = config.canReconnect;
574
- }
575
-
576
- // Create logger for data stores to use
577
- const type = this.client.details.type;
578
- const interactive = this.client.details.capabilities.interactive;
579
- const clientType =
580
- `${interactive ? "interactive" : "noninteractive"}${type !== undefined && type !== "" ? `/${type}` : ""}`;
581
- // Need to use the property getter for docId because for detached flow we don't have the docId initially.
582
- // We assign the id later so property getter is used.
583
- this.subLogger = ChildLogger.create(
584
- loader.services.subLogger,
585
- undefined,
586
- {
587
- all: {
588
- clientType, // Differentiating summarizer container from main container
589
- containerId: uuid(),
590
- docId: () => this._resolvedUrl?.id ?? undefined,
591
- containerAttachState: () => this._attachState,
592
- containerLifecycleState: () => this._lifecycleState,
593
- containerConnectionState: () => ConnectionState[this.connectionState],
594
- serializedContainer: config.serializedContainerState !== undefined,
595
- },
596
- // we need to be judicious with our logging here to avoid generating too much data
597
- // all data logged here should be broadly applicable, and not specific to a
598
- // specific error or class of errors
599
- error: {
600
- // load information to associate errors with the specific load point
601
- dmInitialSeqNumber: () => this._deltaManager?.initialSequenceNumber,
602
- dmLastProcessedSeqNumber: () => this._deltaManager?.lastSequenceNumber,
603
- dmLastKnownSeqNumber: () => this._deltaManager?.lastKnownSeqNumber,
604
- containerLoadedFromVersionId: () => this.loadedFromVersion?.id,
605
- containerLoadedFromVersionDate: () => this.loadedFromVersion?.date,
606
- // message information to associate errors with the specific execution state
607
- // dmLastMsqSeqNumber: if present, same as dmLastProcessedSeqNumber
608
- dmLastMsqSeqNumber: () => this.deltaManager?.lastMessage?.sequenceNumber,
609
- dmLastMsqSeqTimestamp: () => this.deltaManager?.lastMessage?.timestamp,
610
- dmLastMsqSeqClientId: () => this.deltaManager?.lastMessage?.clientId,
611
- connectionStateDuration:
612
- () => performance.now() - this.connectionTransitionTimes[this.connectionState],
613
- },
614
- });
615
-
616
- // Prefix all events in this file with container-loader
617
- this.mc = loggerToMonitoringContext(ChildLogger.create(this.subLogger, "Container"));
618
-
619
- const summarizeProtocolTree =
620
- this.mc.config.getBoolean("Fluid.Container.summarizeProtocolTree2")
621
- ?? this.loader.services.options.summarizeProtocolTree;
622
-
623
- this.options = {
624
- ... this.loader.services.options,
625
- summarizeProtocolTree,
626
- };
627
-
628
- this._deltaManager = this.createDeltaManager();
629
-
630
- this._clientId = config.serializedContainerState?.clientId;
631
- this.connectionStateHandler = createConnectionStateHandler(
632
- {
633
- logger: this.mc.logger,
634
- connectionStateChanged: (value, oldState, reason) => {
635
- if (value === ConnectionState.Connected) {
636
- this._clientId = this.connectionStateHandler.pendingClientId;
637
- }
638
- this.logConnectionStateChangeTelemetry(value, oldState, reason);
639
- if (this._lifecycleState === "loaded") {
640
- this.propagateConnectionState(
641
- false /* initial transition */,
642
- value === ConnectionState.Disconnected ? reason : undefined /* disconnectedReason */,
643
- );
644
- }
645
- },
646
- shouldClientJoinWrite: () => this._deltaManager.connectionManager.shouldJoinWrite(),
647
- maxClientLeaveWaitTime: this.loader.services.options.maxClientLeaveWaitTime,
648
- logConnectionIssue: (eventName: string, details?: ITelemetryProperties) => {
649
- const mode = this.connectionMode;
650
- // We get here when socket does not receive any ops on "write" connection, including
651
- // its own join op. Attempt recovery option.
652
- this._deltaManager.logConnectionIssue({
653
- eventName,
654
- mode,
655
- duration: performance.now() - this.connectionTransitionTimes[ConnectionState.CatchingUp],
656
- ...(details === undefined ? {} : { details: JSON.stringify(details) }),
657
- });
658
-
659
- // If this is "write" connection, it took too long to receive join op. But in most cases that's due
660
- // to very slow op fetches and we will eventually get there.
661
- // For "read" connections, we get here due to self join signal not arriving on time. We will need to
662
- // better understand when and why it may happen.
663
- // For now, attempt to recover by reconnecting. In future, maybe we can query relay service for
664
- // current state of audience.
665
- // Other possible recovery path - move to connected state (i.e. ConnectionStateHandler.joinOpTimer
666
- // to call this.applyForConnectedState("addMemberEvent") for "read" connections)
667
- if (mode === "read") {
668
- this.disconnect();
669
- this.connect();
670
- }
671
- },
672
- },
673
- this.deltaManager,
674
- this._clientId,
675
- );
676
-
677
- this.on(savedContainerEvent, () => {
678
- this.connectionStateHandler.containerSaved();
679
- });
680
-
681
- this.storageService = new ContainerStorageAdapter(
682
- this.loader.services.detachedBlobStorage,
683
- this.mc.logger,
684
- this.options.summarizeProtocolTree === true
685
- ? () => this.captureProtocolSummary()
686
- : undefined,
687
- );
688
-
689
- const isDomAvailable = typeof document === "object" &&
690
- document !== null &&
691
- typeof document.addEventListener === "function" &&
692
- document.addEventListener !== null;
693
- // keep track of last time page was visible for telemetry
694
- if (isDomAvailable) {
695
- this.lastVisible = document.hidden ? performance.now() : undefined;
696
- this.visibilityEventHandler = () => {
697
- if (document.hidden) {
698
- this.lastVisible = performance.now();
699
- } else {
700
- // settimeout so this will hopefully fire after disconnect event if being hidden caused it
701
- setTimeout(() => { this.lastVisible = undefined; }, 0);
702
- }
703
- };
704
- document.addEventListener("visibilitychange", this.visibilityEventHandler);
705
- }
706
-
707
- // We observed that most users of platform do not check Container.connected event on load, causing bugs.
708
- // As such, we are raising events when new listener pops up.
709
- // Note that we can raise both "disconnected" & "connect" events at the same time,
710
- // if we are in connecting stage.
711
- this.on("newListener", (event: string, listener: (...args: any[]) => void) => {
712
- // Fire events on the end of JS turn, giving a chance for caller to be in consistent state.
713
- Promise.resolve().then(() => {
714
- switch (event) {
715
- case dirtyContainerEvent:
716
- if (this._dirtyContainer) {
717
- listener();
718
- }
719
- break;
720
- case savedContainerEvent:
721
- if (!this._dirtyContainer) {
722
- listener();
723
- }
724
- break;
725
- case connectedEventName:
726
- if (this.connected) {
727
- listener(this.clientId);
728
- }
729
- break;
730
- case disconnectedEventName:
731
- if (!this.connected) {
732
- listener();
733
- }
734
- break;
735
- default:
736
- }
737
- }).catch((error) => {
738
- this.mc.logger.sendErrorEvent({ eventName: "RaiseConnectedEventError" }, error);
739
- });
740
- });
741
- }
742
-
743
- /**
744
- * Retrieves the quorum associated with the document
745
- */
746
- public getQuorum(): IQuorumClients {
747
- return this.protocolHandler.quorum;
748
- }
749
-
750
- public close(error?: ICriticalContainerError) {
751
- // 1. Ensure that close sequence is exactly the same no matter if it's initiated by host or by DeltaManager
752
- // 2. We need to ensure that we deliver disconnect event to runtime properly. See connectionStateChanged
753
- // handler. We only deliver events if container fully loaded. Transitioning from "loading" ->
754
- // "closing" will lose that info (can also solve by tracking extra state).
755
- this._deltaManager.close(error);
756
- assert(this.connectionState === ConnectionState.Disconnected,
757
- 0x0cf /* "disconnect event was not raised!" */);
758
-
759
- assert(this._lifecycleState === "closed", 0x314 /* Container properly closed */);
760
- }
761
-
762
- private closeCore(error?: ICriticalContainerError) {
763
- assert(!this.closed, 0x315 /* re-entrancy */);
764
-
765
- try {
766
- // Ensure that we raise all key events even if one of these throws
767
- try {
768
- // Raise event first, to ensure we capture _lifecycleState before transition.
769
- // This gives us a chance to know what errors happened on open vs. on fully loaded container.
770
- this.mc.logger.sendTelemetryEvent(
771
- {
772
- eventName: "ContainerClose",
773
- category: error === undefined ? "generic" : "error",
774
- },
775
- error,
776
- );
777
-
778
- this._lifecycleState = "closing";
779
-
780
- this._protocolHandler?.close();
781
-
782
- this.connectionStateHandler.dispose();
783
-
784
- this._context?.dispose(error !== undefined ? new Error(error.message) : undefined);
785
-
786
- this.storageService.dispose();
787
-
788
- // Notify storage about critical errors. They may be due to disconnect between client & server knowledge
789
- // about file, like file being overwritten in storage, but client having stale local cache.
790
- // Driver need to ensure all caches are cleared on critical errors
791
- this.service?.dispose(error);
792
- } catch (exception) {
793
- this.mc.logger.sendErrorEvent({ eventName: "ContainerCloseException" }, exception);
794
- }
795
-
796
- this.emit("closed", error);
797
-
798
- this.removeAllListeners();
799
- if (this.visibilityEventHandler !== undefined) {
800
- document.removeEventListener("visibilitychange", this.visibilityEventHandler);
801
- }
802
- } finally {
803
- this._lifecycleState = "closed";
804
- }
805
- }
806
-
807
- public closeAndGetPendingLocalState(): string {
808
- // runtime matches pending ops to successful ones by clientId and client seq num, so we need to close the
809
- // container at the same time we get pending state, otherwise this container could reconnect and resubmit with
810
- // a new clientId and a future container using stale pending state without the new clientId would resubmit them
811
- assert(this.attachState === AttachState.Attached, 0x0d1 /* "Container should be attached before close" */);
812
- assert(this.resolvedUrl !== undefined && this.resolvedUrl.type === "fluid",
813
- 0x0d2 /* "resolved url should be valid Fluid url" */);
814
- assert(!!this._protocolHandler, 0x2e3 /* "Must have a valid protocol handler instance" */);
815
- assert(this._protocolHandler.attributes.term !== undefined,
816
- 0x37e /* Must have a valid protocol handler instance */);
817
- const pendingState: IPendingContainerState = {
818
- pendingRuntimeState: this.context.getPendingLocalState(),
819
- url: this.resolvedUrl.url,
820
- protocol: this.protocolHandler.getProtocolState(),
821
- term: this._protocolHandler.attributes.term,
822
- clientId: this.clientId,
823
- };
824
-
825
- this.mc.logger.sendTelemetryEvent({ eventName: "CloseAndGetPendingLocalState" });
826
-
827
- this.close();
828
-
829
- return JSON.stringify(pendingState);
830
- }
831
-
832
- public get attachState(): AttachState {
833
- return this._attachState;
834
- }
835
-
836
- public serialize(): string {
837
- assert(this.attachState === AttachState.Detached, 0x0d3 /* "Should only be called in detached container" */);
838
-
839
- const appSummary: ISummaryTree = this.context.createSummary();
840
- const protocolSummary = this.captureProtocolSummary();
841
- const combinedSummary = combineAppAndProtocolSummary(appSummary, protocolSummary);
842
-
843
- if (this.loader.services.detachedBlobStorage && this.loader.services.detachedBlobStorage.size > 0) {
844
- combinedSummary.tree[".hasAttachmentBlobs"] = { type: SummaryType.Blob, content: "true" };
845
- }
846
- return JSON.stringify(combinedSummary);
847
- }
848
-
849
- public async attach(request: IRequest): Promise<void> {
850
- await PerformanceEvent.timedExecAsync(this.mc.logger, { eventName: "Attach" }, async () => {
851
- if (this._lifecycleState !== "loaded") {
852
- // pre-0.58 error message: containerNotValidForAttach
853
- throw new UsageError(`The Container is not in a valid state for attach [${this._lifecycleState}]`);
854
- }
855
-
856
- // If container is already attached or attach is in progress, throw an error.
857
- assert(this._attachState === AttachState.Detached && !this.attachStarted,
858
- 0x205 /* "attach() called more than once" */);
859
- this.attachStarted = true;
860
-
861
- // If attachment blobs were uploaded in detached state we will go through a different attach flow
862
- const hasAttachmentBlobs = this.loader.services.detachedBlobStorage !== undefined
863
- && this.loader.services.detachedBlobStorage.size > 0;
864
-
865
- try {
866
- assert(this.deltaManager.inbound.length === 0,
867
- 0x0d6 /* "Inbound queue should be empty when attaching" */);
868
-
869
- let summary: ISummaryTree;
870
- if (!hasAttachmentBlobs) {
871
- // Get the document state post attach - possibly can just call attach but we need to change the
872
- // semantics around what the attach means as far as async code goes.
873
- const appSummary: ISummaryTree = this.context.createSummary();
874
- const protocolSummary = this.captureProtocolSummary();
875
- summary = combineAppAndProtocolSummary(appSummary, protocolSummary);
876
-
877
- // Set the state as attaching as we are starting the process of attaching container.
878
- // This should be fired after taking the summary because it is the place where we are
879
- // starting to attach the container to storage.
880
- // Also, this should only be fired in detached container.
881
- this._attachState = AttachState.Attaching;
882
- this.context.notifyAttaching(getSnapshotTreeFromSerializedContainer(summary));
883
- }
884
-
885
- // Actually go and create the resolved document
886
- const createNewResolvedUrl = await this.urlResolver.resolve(request);
887
- ensureFluidResolvedUrl(createNewResolvedUrl);
888
- if (this.service === undefined) {
889
- assert(this.client.details.type !== summarizerClientType,
890
- 0x2c4 /* "client should not be summarizer before container is created" */);
891
- this.service = await runWithRetry(
892
- async () => this.serviceFactory.createContainer(
893
- summary,
894
- createNewResolvedUrl,
895
- this.subLogger,
896
- false, // clientIsSummarizer
897
- ),
898
- "containerAttach",
899
- this.mc.logger,
900
- {
901
- cancel: this.closeSignal,
902
- }, // progress
903
- );
904
- }
905
- const resolvedUrl = this.service.resolvedUrl;
906
- ensureFluidResolvedUrl(resolvedUrl);
907
- this._resolvedUrl = resolvedUrl;
908
- await this.storageService.connectToService(this.service);
909
-
910
- if (hasAttachmentBlobs) {
911
- // upload blobs to storage
912
- assert(!!this.loader.services.detachedBlobStorage, 0x24e /* "assertion for type narrowing" */);
913
-
914
- // build a table mapping IDs assigned locally to IDs assigned by storage and pass it to runtime to
915
- // support blob handles that only know about the local IDs
916
- const redirectTable = new Map<string, string>();
917
- // if new blobs are added while uploading, upload them too
918
- while (redirectTable.size < this.loader.services.detachedBlobStorage.size) {
919
- const newIds = this.loader.services.detachedBlobStorage.getBlobIds().filter(
920
- (id) => !redirectTable.has(id));
921
- for (const id of newIds) {
922
- const blob = await this.loader.services.detachedBlobStorage.readBlob(id);
923
- const response = await this.storageService.createBlob(blob);
924
- redirectTable.set(id, response.id);
925
- }
926
- }
927
-
928
- // take summary and upload
929
- const appSummary: ISummaryTree = this.context.createSummary(redirectTable);
930
- const protocolSummary = this.captureProtocolSummary();
931
- summary = combineAppAndProtocolSummary(appSummary, protocolSummary);
932
-
933
- this._attachState = AttachState.Attaching;
934
- this.context.notifyAttaching(getSnapshotTreeFromSerializedContainer(summary));
935
-
936
- await this.storageService.uploadSummaryWithContext(summary, {
937
- referenceSequenceNumber: 0,
938
- ackHandle: undefined,
939
- proposalHandle: undefined,
940
- });
941
- }
942
-
943
- this._attachState = AttachState.Attached;
944
- this.emit("attached");
945
-
946
- if (!this.closed) {
947
- this.resumeInternal({ fetchOpsFromStorage: false, reason: "createDetached" });
948
- }
949
- } catch (error) {
950
- // add resolved URL on error object so that host has the ability to find this document and delete it
951
- const newError = normalizeError(error);
952
- const resolvedUrl = this.resolvedUrl;
953
- if (isFluidResolvedUrl(resolvedUrl)) {
954
- newError.addTelemetryProperties({ resolvedUrl: resolvedUrl.url });
955
- }
956
- this.close(newError);
957
- throw newError;
958
- }
959
- },
960
- { start: true, end: true, cancel: "generic" });
961
- }
962
-
963
- public async request(path: IRequest): Promise<IResponse> {
964
- return PerformanceEvent.timedExecAsync(
965
- this.mc.logger,
966
- { eventName: "Request" },
967
- async () => this.context.request(path),
968
- { end: true, cancel: "error" },
969
- );
970
- }
971
-
972
- private setAutoReconnectInternal(mode: ReconnectMode) {
973
- const currentMode = this._deltaManager.connectionManager.reconnectMode;
974
-
975
- if (currentMode === mode) {
976
- return;
977
- }
978
-
979
- const now = performance.now();
980
- const duration = now - this.setAutoReconnectTime;
981
- this.setAutoReconnectTime = now;
982
-
983
- this.mc.logger.sendTelemetryEvent({
984
- eventName: mode === ReconnectMode.Enabled ? "AutoReconnectEnabled" : "AutoReconnectDisabled",
985
- connectionMode: this.connectionMode,
986
- connectionState: ConnectionState[this.connectionState],
987
- duration,
988
- });
989
-
990
- this._deltaManager.connectionManager.setAutoReconnect(mode);
991
- }
992
-
993
- public connect() {
994
- if (this.closed) {
995
- throw new UsageError(`The Container is closed and cannot be connected`);
996
- } else if (this._attachState !== AttachState.Attached) {
997
- throw new UsageError(`The Container is not attached and cannot be connected`);
998
- } else if (!this.connected) {
999
- // Note: no need to fetch ops as we do it preemptively as part of DeltaManager.attachOpHandler().
1000
- // If there is gap, we will learn about it once connected, but the gap should be small (if any),
1001
- // assuming that connect() is called quickly after initial container boot.
1002
- this.connectInternal({ reason: "DocumentConnect", fetchOpsFromStorage: false });
1003
- }
1004
- }
1005
-
1006
- private connectInternal(args: IConnectionArgs) {
1007
- assert(!this.closed, 0x2c5 /* "Attempting to connect() a closed Container" */);
1008
- assert(this._attachState === AttachState.Attached,
1009
- 0x2c6 /* "Attempting to connect() a container that is not attached" */);
1010
-
1011
- // Resume processing ops and connect to delta stream
1012
- this.resumeInternal(args);
1013
-
1014
- // Set Auto Reconnect Mode
1015
- const mode = ReconnectMode.Enabled;
1016
- this.setAutoReconnectInternal(mode);
1017
- }
1018
-
1019
- public disconnect() {
1020
- if (this.closed) {
1021
- throw new UsageError(`The Container is closed and cannot be disconnected`);
1022
- } else {
1023
- this.disconnectInternal();
1024
- }
1025
- }
1026
-
1027
- private disconnectInternal() {
1028
- assert(!this.closed, 0x2c7 /* "Attempting to disconnect() a closed Container" */);
1029
-
1030
- // Set Auto Reconnect Mode
1031
- const mode = ReconnectMode.Disabled;
1032
- this.setAutoReconnectInternal(mode);
1033
- }
1034
-
1035
- private resumeInternal(args: IConnectionArgs) {
1036
- assert(!this.closed, 0x0d9 /* "Attempting to connect() a closed DeltaManager" */);
1037
-
1038
- // Resume processing ops
1039
- if (this.inboundQueuePausedFromInit) {
1040
- this.inboundQueuePausedFromInit = false;
1041
- this._deltaManager.inbound.resume();
1042
- this._deltaManager.inboundSignal.resume();
1043
- }
1044
-
1045
- // Ensure connection to web socket
1046
- this.connectToDeltaStream(args);
1047
- }
1048
-
1049
- public async getAbsoluteUrl(relativeUrl: string): Promise<string | undefined> {
1050
- if (this.resolvedUrl === undefined) {
1051
- return undefined;
1052
- }
1053
-
1054
- return this.urlResolver.getAbsoluteUrl(
1055
- this.resolvedUrl,
1056
- relativeUrl,
1057
- getPackageName(this._context?.codeDetails));
1058
- }
1059
-
1060
- public async proposeCodeDetails(codeDetails: IFluidCodeDetails) {
1061
- if (!isFluidCodeDetails(codeDetails)) {
1062
- throw new Error("Provided codeDetails are not IFluidCodeDetails");
1063
- }
1064
-
1065
- if (this.codeLoader.IFluidCodeDetailsComparer) {
1066
- const comparison = await this.codeLoader.IFluidCodeDetailsComparer.compare(
1067
- codeDetails,
1068
- this.getCodeDetailsFromQuorum());
1069
- if (comparison !== undefined && comparison <= 0) {
1070
- throw new Error("Proposed code details should be greater than the current");
1071
- }
1072
- }
1073
-
1074
- return this.protocolHandler.quorum.propose("code", codeDetails)
1075
- .then(() => true)
1076
- .catch(() => false);
1077
- }
1078
-
1079
- private async processCodeProposal(): Promise<void> {
1080
- const codeDetails = this.getCodeDetailsFromQuorum();
1081
-
1082
- await Promise.all([
1083
- this.deltaManager.inbound.pause(),
1084
- this.deltaManager.inboundSignal.pause()]);
1085
-
1086
- if ((await this.context.satisfies(codeDetails) === true)) {
1087
- this.deltaManager.inbound.resume();
1088
- this.deltaManager.inboundSignal.resume();
1089
- return;
1090
- }
1091
-
1092
- // pre-0.58 error message: existingContextDoesNotSatisfyIncomingProposal
1093
- this.close(new GenericError("Existing context does not satisfy incoming proposal"));
1094
- }
1095
-
1096
- private async getVersion(version: string | null): Promise<IVersion | undefined> {
1097
- const versions = await this.storageService.getVersions(version, 1);
1098
- return versions[0];
1099
- }
1100
-
1101
- private recordConnectStartTime() {
1102
- if (this.connectionTransitionTimes[ConnectionState.Disconnected] === undefined) {
1103
- this.connectionTransitionTimes[ConnectionState.Disconnected] = performance.now();
1104
- }
1105
- }
1106
-
1107
- private connectToDeltaStream(args: IConnectionArgs) {
1108
- this.recordConnectStartTime();
1109
-
1110
- // All agents need "write" access, including summarizer.
1111
- if (!this._canReconnect || !this.client.details.capabilities.interactive) {
1112
- args.mode = "write";
1113
- }
1114
-
1115
- this._deltaManager.connect(args);
1116
- }
1117
-
1118
- /**
1119
- * Load container.
1120
- *
1121
- * @param specifiedVersion - Version SHA to load snapshot. If not specified, will fetch the latest snapshot.
1122
- */
1123
- private async load(
1124
- specifiedVersion: string | undefined,
1125
- loadMode: IContainerLoadMode,
1126
- pendingLocalState?: IPendingContainerState,
1127
- ) {
1128
- if (this._resolvedUrl === undefined) {
1129
- throw new Error("Attempting to load without a resolved url");
1130
- }
1131
- this.service = await this.serviceFactory.createDocumentService(
1132
- this._resolvedUrl,
1133
- this.subLogger,
1134
- this.client.details.type === summarizerClientType,
1135
- );
1136
-
1137
- // Ideally we always connect as "read" by default.
1138
- // Currently that works with SPO & r11s, because we get "write" connection when connecting to non-existing file.
1139
- // We should not rely on it by (one of them will address the issue, but we need to address both)
1140
- // 1) switching create new flow to one where we create file by posting snapshot
1141
- // 2) Fixing quorum workflows (have retry logic)
1142
- // That all said, "read" does not work with memorylicious workflows (that opens two simultaneous
1143
- // connections to same file) in two ways:
1144
- // A) creation flow breaks (as one of the clients "sees" file as existing, and hits #2 above)
1145
- // B) Once file is created, transition from view-only connection to write does not work - some bugs to be fixed.
1146
- const connectionArgs: IConnectionArgs = { reason: "DocumentOpen", mode: "write", fetchOpsFromStorage: false };
1147
-
1148
- // Start websocket connection as soon as possible. Note that there is no op handler attached yet, but the
1149
- // DeltaManager is resilient to this and will wait to start processing ops until after it is attached.
1150
- if (loadMode.deltaConnection === undefined) {
1151
- this.connectToDeltaStream(connectionArgs);
1152
- }
1153
-
1154
- if (!pendingLocalState) {
1155
- await this.storageService.connectToService(this.service);
1156
- } else {
1157
- // if we have pendingLocalState we can load without storage; don't wait for connection
1158
- this.storageService.connectToService(this.service).catch((error) => this.close(error));
1159
- }
1160
-
1161
- this._attachState = AttachState.Attached;
1162
-
1163
- // Fetch specified snapshot.
1164
- const { snapshot, versionId } = pendingLocalState === undefined
1165
- ? await this.fetchSnapshotTree(specifiedVersion)
1166
- : { snapshot: undefined, versionId: undefined };
1167
- assert(snapshot !== undefined || pendingLocalState !== undefined, 0x237 /* "Snapshot should exist" */);
1168
-
1169
- const attributes: IDocumentAttributes = pendingLocalState === undefined
1170
- ? await this.getDocumentAttributes(this.storageService, snapshot)
1171
- : {
1172
- sequenceNumber: pendingLocalState.protocol.sequenceNumber,
1173
- minimumSequenceNumber: pendingLocalState.protocol.minimumSequenceNumber,
1174
- term: pendingLocalState.term,
1175
- };
1176
-
1177
- let opsBeforeReturnP: Promise<void> | undefined;
1178
-
1179
- // Attach op handlers to finish initialization and be able to start processing ops
1180
- // Kick off any ops fetching if required.
1181
- switch (loadMode.opsBeforeReturn) {
1182
- case undefined:
1183
- // Start prefetch, but not set opsBeforeReturnP - boot is not blocked by it!
1184
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
1185
- this.attachDeltaManagerOpHandler(attributes, loadMode.deltaConnection !== "none" ? "all" : "none");
1186
- break;
1187
- case "cached":
1188
- opsBeforeReturnP = this.attachDeltaManagerOpHandler(attributes, "cached");
1189
- break;
1190
- case "all":
1191
- opsBeforeReturnP = this.attachDeltaManagerOpHandler(attributes, "all");
1192
- break;
1193
- default:
1194
- unreachableCase(loadMode.opsBeforeReturn);
1195
- }
1196
-
1197
- // ...load in the existing quorum
1198
- // Initialize the protocol handler
1199
- if (pendingLocalState === undefined) {
1200
- await this.initializeProtocolStateFromSnapshot(
1201
- attributes,
1202
- this.storageService,
1203
- snapshot);
1204
- } else {
1205
- this.initializeProtocolState(
1206
- attributes,
1207
- {
1208
- members: pendingLocalState.protocol.members,
1209
- proposals: pendingLocalState.protocol.proposals,
1210
- values: pendingLocalState.protocol.values,
1211
- }, // pending IQuorumSnapshot
1212
- );
1213
- }
1214
-
1215
- const codeDetails = this.getCodeDetailsFromQuorum();
1216
- await this.instantiateContext(
1217
- true, // existing
1218
- codeDetails,
1219
- snapshot,
1220
- pendingLocalState?.pendingRuntimeState,
1221
- );
1222
-
1223
- // We might have hit some failure that did not manifest itself in exception in this flow,
1224
- // do not start op processing in such case - static version of Container.load() will handle it correctly.
1225
- if (!this.closed) {
1226
- if (opsBeforeReturnP !== undefined) {
1227
- this._deltaManager.inbound.resume();
1228
-
1229
- await ReportIfTooLong(
1230
- this.mc.logger,
1231
- "WaitOps",
1232
- async () => { await opsBeforeReturnP; return {}; });
1233
- await ReportIfTooLong(
1234
- this.mc.logger,
1235
- "WaitOpProcessing",
1236
- async () => this._deltaManager.inbound.waitTillProcessingDone());
1237
-
1238
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
1239
- this._deltaManager.inbound.pause();
1240
- }
1241
-
1242
- switch (loadMode.deltaConnection) {
1243
- case undefined:
1244
- case "delayed":
1245
- assert(this.inboundQueuePausedFromInit, 0x346 /* inboundQueuePausedFromInit should be true */);
1246
- this.inboundQueuePausedFromInit = false;
1247
- this._deltaManager.inbound.resume();
1248
- this._deltaManager.inboundSignal.resume();
1249
- break;
1250
- case "none":
1251
- break;
1252
- default:
1253
- unreachableCase(loadMode.deltaConnection);
1254
- }
1255
- }
1256
-
1257
- // Safety net: static version of Container.load() should have learned about it through "closed" handler.
1258
- // But if that did not happen for some reason, fail load for sure.
1259
- // Otherwise we can get into situations where container is closed and does not try to connect to ordering
1260
- // service, but caller does not know that (callers do expect container to be not closed on successful path
1261
- // and listen only on "closed" event)
1262
- if (this.closed) {
1263
- throw new Error("Container was closed while load()");
1264
- }
1265
-
1266
- // Internal context is fully loaded at this point
1267
- this.setLoaded();
1268
-
1269
- return {
1270
- sequenceNumber: attributes.sequenceNumber,
1271
- version: versionId,
1272
- dmLastProcessedSeqNumber: this._deltaManager.lastSequenceNumber,
1273
- dmLastKnownSeqNumber: this._deltaManager.lastKnownSeqNumber,
1274
- };
1275
- }
1276
-
1277
- private async createDetached(source: IFluidCodeDetails) {
1278
- const attributes: IDocumentAttributes = {
1279
- sequenceNumber: detachedContainerRefSeqNumber,
1280
- term: 1,
1281
- minimumSequenceNumber: 0,
1282
- };
1283
-
1284
- await this.attachDeltaManagerOpHandler(attributes);
1285
-
1286
- // Need to just seed the source data in the code quorum. Quorum itself is empty
1287
- const qValues = initQuorumValuesFromCodeDetails(source);
1288
- this.initializeProtocolState(
1289
- attributes,
1290
- {
1291
- members: [],
1292
- proposals: [],
1293
- values: qValues,
1294
- }, // IQuorumSnapShot
1295
- );
1296
-
1297
- // The load context - given we seeded the quorum - will be great
1298
- await this.instantiateContextDetached(
1299
- false, // existing
1300
- );
1301
-
1302
- this.setLoaded();
1303
- }
1304
-
1305
- private async rehydrateDetachedFromSnapshot(detachedContainerSnapshot: ISummaryTree) {
1306
- if (detachedContainerSnapshot.tree[".hasAttachmentBlobs"] !== undefined) {
1307
- assert(!!this.loader.services.detachedBlobStorage && this.loader.services.detachedBlobStorage.size > 0,
1308
- 0x250 /* "serialized container with attachment blobs must be rehydrated with detached blob storage" */);
1309
- delete detachedContainerSnapshot.tree[".hasAttachmentBlobs"];
1310
- }
1311
-
1312
- const snapshotTree = getSnapshotTreeFromSerializedContainer(detachedContainerSnapshot);
1313
- this.storageService.loadSnapshotForRehydratingContainer(snapshotTree);
1314
- const attributes = await this.getDocumentAttributes(this.storageService, snapshotTree);
1315
-
1316
- await this.attachDeltaManagerOpHandler(attributes);
1317
-
1318
- // Initialize the protocol handler
1319
- const baseTree = getProtocolSnapshotTree(snapshotTree);
1320
- const qValues = await readAndParse<[string, ICommittedProposal][]>(
1321
- this.storageService,
1322
- baseTree.blobs.quorumValues,
1323
- );
1324
- const codeDetails = getCodeDetailsFromQuorumValues(qValues);
1325
- this.initializeProtocolState(
1326
- attributes,
1327
- {
1328
- members: [],
1329
- proposals: [],
1330
- values: codeDetails !== undefined ? initQuorumValuesFromCodeDetails(codeDetails) : [],
1331
- }, // IQuorumSnapShot
1332
- );
1333
-
1334
- await this.instantiateContextDetached(
1335
- true, // existing
1336
- snapshotTree,
1337
- );
1338
-
1339
- this.setLoaded();
1340
- }
1341
-
1342
- private async getDocumentAttributes(
1343
- storage: IDocumentStorageService,
1344
- tree: ISnapshotTree | undefined,
1345
- ): Promise<IDocumentAttributes> {
1346
- if (tree === undefined) {
1347
- return {
1348
- minimumSequenceNumber: 0,
1349
- sequenceNumber: 0,
1350
- term: 1,
1351
- };
1352
- }
1353
-
1354
- // Backward compatibility: old docs would have ".attributes" instead of "attributes"
1355
- const attributesHash = ".protocol" in tree.trees
1356
- ? tree.trees[".protocol"].blobs.attributes
1357
- : tree.blobs[".attributes"];
1358
-
1359
- const attributes = await readAndParse<IDocumentAttributes>(storage, attributesHash);
1360
-
1361
- // Backward compatibility for older summaries with no term
1362
- if (attributes.term === undefined) {
1363
- attributes.term = 1;
1364
- }
1365
-
1366
- return attributes;
1367
- }
1368
-
1369
- private async initializeProtocolStateFromSnapshot(
1370
- attributes: IDocumentAttributes,
1371
- storage: IDocumentStorageService,
1372
- snapshot: ISnapshotTree | undefined,
1373
- ): Promise<void> {
1374
- const quorumSnapshot: IQuorumSnapshot = {
1375
- members: [],
1376
- proposals: [],
1377
- values: [],
1378
- };
1379
-
1380
- if (snapshot !== undefined) {
1381
- const baseTree = getProtocolSnapshotTree(snapshot);
1382
- [quorumSnapshot.members, quorumSnapshot.proposals, quorumSnapshot.values] = await Promise.all([
1383
- readAndParse<[string, ISequencedClient][]>(storage, baseTree.blobs.quorumMembers),
1384
- readAndParse<[number, ISequencedProposal, string[]][]>(storage, baseTree.blobs.quorumProposals),
1385
- readAndParse<[string, ICommittedProposal][]>(storage, baseTree.blobs.quorumValues),
1386
- ]);
1387
- }
1388
-
1389
- this.initializeProtocolState(attributes, quorumSnapshot);
1390
- }
1391
-
1392
- private initializeProtocolState(
1393
- attributes: IDocumentAttributes,
1394
- quorumSnapshot: IQuorumSnapshot,
1395
- ): void {
1396
- const protocolHandlerBuilder =
1397
- this.protocolHandlerBuilder ?? ((...args) => new ProtocolHandler(...args, new Audience()));
1398
- const protocol = protocolHandlerBuilder(
1399
- attributes,
1400
- quorumSnapshot,
1401
- (key, value) => this.submitMessage(MessageType.Propose, JSON.stringify({ key, value })),
1402
- );
1403
-
1404
- const protocolLogger = ChildLogger.create(this.subLogger, "ProtocolHandler");
1405
-
1406
- protocol.quorum.on("error", (error) => {
1407
- protocolLogger.sendErrorEvent(error);
1408
- });
1409
-
1410
- // Track membership changes and update connection state accordingly
1411
- this.connectionStateHandler.initProtocol(protocol);
1412
-
1413
- protocol.quorum.on("addProposal", (proposal: ISequencedProposal) => {
1414
- if (proposal.key === "code" || proposal.key === "code2") {
1415
- this.emit("codeDetailsProposed", proposal.value, proposal);
1416
- }
1417
- });
1418
-
1419
- protocol.quorum.on(
1420
- "approveProposal",
1421
- (sequenceNumber, key, value) => {
1422
- if (key === "code" || key === "code2") {
1423
- if (!isFluidCodeDetails(value)) {
1424
- this.mc.logger.sendErrorEvent({
1425
- eventName: "CodeProposalNotIFluidCodeDetails",
1426
- });
1427
- }
1428
- this.processCodeProposal().catch((error) => {
1429
- this.close(normalizeError(error));
1430
- throw error;
1431
- });
1432
- }
1433
- });
1434
- // we need to make sure this member get set in a synchronous context,
1435
- // or other things can happen after the object that will be set is created, but not yet set
1436
- // this was breaking this._initialClients handling
1437
- //
1438
- this._protocolHandler = protocol;
1439
- }
1440
-
1441
- private captureProtocolSummary(): ISummaryTree {
1442
- const quorumSnapshot = this.protocolHandler.snapshot();
1443
- const summary: ISummaryTree = {
1444
- tree: {
1445
- attributes: {
1446
- content: JSON.stringify(this.protocolHandler.attributes),
1447
- type: SummaryType.Blob,
1448
- },
1449
- quorumMembers: {
1450
- content: JSON.stringify(quorumSnapshot.members),
1451
- type: SummaryType.Blob,
1452
- },
1453
- quorumProposals: {
1454
- content: JSON.stringify(quorumSnapshot.proposals),
1455
- type: SummaryType.Blob,
1456
- },
1457
- quorumValues: {
1458
- content: JSON.stringify(quorumSnapshot.values),
1459
- type: SummaryType.Blob,
1460
- },
1461
- },
1462
- type: SummaryType.Tree,
1463
- };
1464
-
1465
- return summary;
1466
- }
1467
-
1468
- private getCodeDetailsFromQuorum(): IFluidCodeDetails {
1469
- const quorum = this.protocolHandler.quorum;
1470
-
1471
- const pkg = getCodeProposal(quorum);
1472
-
1473
- return pkg as IFluidCodeDetails;
1474
- }
1475
-
1476
- private get client(): IClient {
1477
- const client: IClient = this.options?.client !== undefined
1478
- ? (this.options.client as IClient)
1479
- : {
1480
- details: {
1481
- capabilities: { interactive: true },
1482
- },
1483
- mode: "read", // default reconnection mode on lost connection / connection error
1484
- permission: [],
1485
- scopes: [],
1486
- user: { id: "" },
1487
- };
1488
-
1489
- if (this.clientDetailsOverride !== undefined) {
1490
- merge(client.details, this.clientDetailsOverride);
1491
- }
1492
- client.details.environment = [client.details.environment, ` loaderVersion:${pkgVersion}`].join(";");
1493
- return client;
1494
- }
1495
-
1496
- /**
1497
- * Returns true if connection is active, i.e. it's "write" connection and
1498
- * container runtime was notified about this connection (i.e. we are up-to-date and could send ops).
1499
- * This happens after client received its own joinOp and thus is in the quorum.
1500
- * If it's not true, runtime is not in position to send ops.
1501
- */
1502
- private activeConnection() {
1503
- return this.connectionState === ConnectionState.Connected &&
1504
- this.connectionMode === "write";
1505
- }
1506
-
1507
- private createDeltaManager() {
1508
- const serviceProvider = () => this.service;
1509
- const deltaManager = new DeltaManager<ConnectionManager>(
1510
- serviceProvider,
1511
- ChildLogger.create(this.subLogger, "DeltaManager"),
1512
- () => this.activeConnection(),
1513
- (props: IConnectionManagerFactoryArgs) => new ConnectionManager(
1514
- serviceProvider,
1515
- this.client,
1516
- this._canReconnect,
1517
- ChildLogger.create(this.subLogger, "ConnectionManager"),
1518
- props),
1519
- );
1520
-
1521
- // Disable inbound queues as Container is not ready to accept any ops until we are fully loaded!
1522
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
1523
- deltaManager.inbound.pause();
1524
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
1525
- deltaManager.inboundSignal.pause();
1526
-
1527
- deltaManager.on("connect", (details: IConnectionDetails, _opsBehind?: number) => {
1528
- assert(this.connectionMode === details.mode, "mismatch");
1529
- this.connectionStateHandler.receivedConnectEvent(
1530
- details,
1531
- );
1532
- });
1533
-
1534
- deltaManager.on("disconnect", (reason: string) => {
1535
- this.collabWindowTracker?.stopSequenceNumberUpdate();
1536
- this.connectionStateHandler.receivedDisconnectEvent(reason);
1537
- });
1538
-
1539
- deltaManager.on("throttled", (warning: IThrottlingWarning) => {
1540
- const warn = warning as ContainerWarning;
1541
- // Some "warning" events come from outside the container and are logged
1542
- // elsewhere (e.g. summarizing container). We shouldn't log these here.
1543
- if (warn.logged !== true) {
1544
- this.mc.logger.sendTelemetryEvent({ eventName: "ContainerWarning" }, warn);
1545
- }
1546
- this.emit("warning", warn);
1547
- });
1548
-
1549
- deltaManager.on("readonly", (readonly) => {
1550
- this.setContextConnectedState(this.connectionState === ConnectionState.Connected, readonly);
1551
- this.emit("readonly", readonly);
1552
- });
1553
-
1554
- deltaManager.on("closed", (error?: ICriticalContainerError) => {
1555
- this.closeCore(error);
1556
- });
1557
-
1558
- return deltaManager;
1559
- }
1560
-
1561
- private async attachDeltaManagerOpHandler(
1562
- attributes: IDocumentAttributes,
1563
- prefetchType?: "cached" | "all" | "none") {
1564
- return this._deltaManager.attachOpHandler(
1565
- attributes.minimumSequenceNumber,
1566
- attributes.sequenceNumber,
1567
- attributes.term ?? 1,
1568
- {
1569
- process: (message) => this.processRemoteMessage(message),
1570
- processSignal: (message) => {
1571
- this.processSignal(message);
1572
- },
1573
- },
1574
- prefetchType);
1575
- }
1576
-
1577
- private logConnectionStateChangeTelemetry(
1578
- value: ConnectionState,
1579
- oldState: ConnectionState,
1580
- reason?: string,
1581
- ) {
1582
- // Log actual event
1583
- const time = performance.now();
1584
- this.connectionTransitionTimes[value] = time;
1585
- const duration = time - this.connectionTransitionTimes[oldState];
1586
-
1587
- let durationFromDisconnected: number | undefined;
1588
- let connectionInitiationReason: string | undefined;
1589
- let autoReconnect: ReconnectMode | undefined;
1590
- let checkpointSequenceNumber: number | undefined;
1591
- let opsBehind: number | undefined;
1592
- if (value === ConnectionState.Disconnected) {
1593
- autoReconnect = this._deltaManager.connectionManager.reconnectMode;
1594
- } else {
1595
- if (value === ConnectionState.Connected) {
1596
- durationFromDisconnected = time - this.connectionTransitionTimes[ConnectionState.Disconnected];
1597
- durationFromDisconnected = TelemetryLogger.formatTick(durationFromDisconnected);
1598
- } else {
1599
- // This info is of most interest on establishing connection only.
1600
- checkpointSequenceNumber = this.deltaManager.lastKnownSeqNumber;
1601
- if (this.deltaManager.hasCheckpointSequenceNumber) {
1602
- opsBehind = checkpointSequenceNumber - this.deltaManager.lastSequenceNumber;
1603
- }
1604
- }
1605
- connectionInitiationReason = this.firstConnection ? "InitialConnect" : "AutoReconnect";
1606
- }
1607
-
1608
- this.mc.logger.sendPerformanceEvent({
1609
- eventName: `ConnectionStateChange_${ConnectionState[value]}`,
1610
- from: ConnectionState[oldState],
1611
- duration,
1612
- durationFromDisconnected,
1613
- reason,
1614
- connectionInitiationReason,
1615
- pendingClientId: this.connectionStateHandler.pendingClientId,
1616
- clientId: this.clientId,
1617
- autoReconnect,
1618
- opsBehind,
1619
- online: OnlineStatus[isOnline()],
1620
- lastVisible: this.lastVisible !== undefined ? performance.now() - this.lastVisible : undefined,
1621
- checkpointSequenceNumber,
1622
- quorumSize: this._protocolHandler?.quorum.getMembers().size,
1623
- ...this._deltaManager.connectionProps,
1624
- });
1625
-
1626
- if (value === ConnectionState.Connected) {
1627
- this.firstConnection = false;
1628
- }
1629
- }
1630
-
1631
- private propagateConnectionState(initialTransition: boolean, disconnectedReason?: string) {
1632
- // When container loaded, we want to propagate initial connection state.
1633
- // After that, we communicate only transitions to Connected & Disconnected states, skipping all other states.
1634
- // This can be changed in the future, for example we likely should add "CatchingUp" event on Container.
1635
- if (!initialTransition &&
1636
- this.connectionState !== ConnectionState.Connected &&
1637
- this.connectionState !== ConnectionState.Disconnected) {
1638
- return;
1639
- }
1640
- const state = this.connectionState === ConnectionState.Connected;
1641
-
1642
- const logOpsOnReconnect: boolean =
1643
- this.connectionState === ConnectionState.Connected &&
1644
- !this.firstConnection &&
1645
- this.connectionMode === "write";
1646
- if (logOpsOnReconnect) {
1647
- this.messageCountAfterDisconnection = 0;
1648
- }
1649
-
1650
- // Both protocol and context should not be undefined if we got so far.
1651
-
1652
- this.setContextConnectedState(state, this._deltaManager.connectionManager.readOnlyInfo.readonly ?? false);
1653
- this.protocolHandler.setConnectionState(state, this.clientId);
1654
- raiseConnectedEvent(this.mc.logger, this, state, this.clientId, disconnectedReason);
1655
-
1656
- if (logOpsOnReconnect) {
1657
- this.mc.logger.sendTelemetryEvent(
1658
- { eventName: "OpsSentOnReconnect", count: this.messageCountAfterDisconnection });
1659
- }
1660
- }
1661
-
1662
- // back-compat: ADO #1385: Remove in the future, summary op should come through submitSummaryMessage()
1663
- private submitContainerMessage(type: MessageType, contents: any, batch?: boolean, metadata?: any): number {
1664
- switch (type) {
1665
- case MessageType.Operation:
1666
- return this.submitMessage(
1667
- type,
1668
- JSON.stringify(contents),
1669
- batch,
1670
- metadata);
1671
- case MessageType.Summarize:
1672
- return this.submitSummaryMessage(contents as unknown as ISummaryContent);
1673
- default:
1674
- this.close(new GenericError("invalidContainerSubmitOpType",
1675
- undefined /* error */,
1676
- { messageType: type }));
1677
- return -1;
1678
- }
1679
- }
1680
-
1681
- /** @returns clientSequenceNumber of last message in a batch */
1682
- private submitBatch(batch: IBatchMessage[]): number {
1683
- let clientSequenceNumber = -1;
1684
- for (const message of batch) {
1685
- clientSequenceNumber = this.submitMessage(
1686
- MessageType.Operation,
1687
- message.contents,
1688
- true, // batch
1689
- message.metadata,
1690
- message.compression);
1691
- }
1692
- this._deltaManager.flush();
1693
- return clientSequenceNumber;
1694
- }
1695
-
1696
- private submitSummaryMessage(summary: ISummaryContent) {
1697
- // github #6451: this is only needed for staging so the server
1698
- // know when the protocol tree is included
1699
- // this can be removed once all clients send
1700
- // protocol tree by default
1701
- if (summary.details === undefined) {
1702
- summary.details = {};
1703
- }
1704
- summary.details.includesProtocolTree =
1705
- this.options.summarizeProtocolTree === true;
1706
- return this.submitMessage(MessageType.Summarize, JSON.stringify(summary), false /* batch */);
1707
- }
1708
-
1709
- private submitMessage(type: MessageType,
1710
- contents?: string,
1711
- batch?: boolean,
1712
- metadata?: any,
1713
- compression?: string): number {
1714
- if (this.connectionState !== ConnectionState.Connected) {
1715
- this.mc.logger.sendErrorEvent({ eventName: "SubmitMessageWithNoConnection", type });
1716
- return -1;
1717
- }
1718
-
1719
- this.messageCountAfterDisconnection += 1;
1720
- this.collabWindowTracker?.stopSequenceNumberUpdate();
1721
- return this._deltaManager.submit(type, contents, batch, metadata, compression);
1722
- }
1723
-
1724
- private processRemoteMessage(message: ISequencedDocumentMessage) {
1725
- const local = this.clientId === message.clientId;
1726
-
1727
- // Allow the protocol handler to process the message
1728
- const result = this.protocolHandler.processMessage(message, local);
1729
-
1730
- // Forward messages to the loaded runtime for processing
1731
- this.context.process(message, local);
1732
-
1733
- // Inactive (not in quorum or not writers) clients don't take part in the minimum sequence number calculation.
1734
- if (this.activeConnection()) {
1735
- if (this.collabWindowTracker === undefined) {
1736
- // Note that config from first connection will be used for this container's lifetime.
1737
- // That means that if relay service changes settings, such changes will impact only newly booted
1738
- // clients.
1739
- // All existing will continue to use settings they got earlier.
1740
- assert(
1741
- this.serviceConfiguration !== undefined,
1742
- 0x2e4 /* "there should be service config for active connection" */);
1743
- this.collabWindowTracker = new CollabWindowTracker(
1744
- (type) => {
1745
- assert(this.activeConnection(),
1746
- 0x241 /* "disconnect should result in stopSequenceNumberUpdate() call" */);
1747
- this.submitMessage(type);
1748
- },
1749
- this.serviceConfiguration.noopTimeFrequency,
1750
- this.serviceConfiguration.noopCountFrequency,
1751
- );
1752
- }
1753
- this.collabWindowTracker.scheduleSequenceNumberUpdate(message, result.immediateNoOp === true);
1754
- }
1755
-
1756
- this.emit("op", message);
1757
- }
1758
-
1759
- private submitSignal(message: any) {
1760
- this._deltaManager.submitSignal(JSON.stringify(message));
1761
- }
1762
-
1763
- private processSignal(message: ISignalMessage) {
1764
- // No clientId indicates a system signal message.
1765
- if (message.clientId === null) {
1766
- this.protocolHandler.processSignal(message);
1767
- } else {
1768
- const local = this.clientId === message.clientId;
1769
- this.context.processSignal(message, local);
1770
- }
1771
- }
1772
-
1773
- /**
1774
- * Get the most recent snapshot, or a specific version.
1775
- * @param specifiedVersion - The specific version of the snapshot to retrieve
1776
- * @returns The snapshot requested, or the latest snapshot if no version was specified, plus version ID
1777
- */
1778
- private async fetchSnapshotTree(specifiedVersion: string | undefined):
1779
- Promise<{ snapshot?: ISnapshotTree; versionId?: string; }> {
1780
- const version = await this.getVersion(specifiedVersion ?? null);
1781
-
1782
- if (version === undefined && specifiedVersion !== undefined) {
1783
- // We should have a defined version to load from if specified version requested
1784
- this.mc.logger.sendErrorEvent({ eventName: "NoVersionFoundWhenSpecified", id: specifiedVersion });
1785
- }
1786
- this._loadedFromVersion = version;
1787
- const snapshot = await this.storageService.getSnapshotTree(version) ?? undefined;
1788
-
1789
- if (snapshot === undefined && version !== undefined) {
1790
- this.mc.logger.sendErrorEvent({ eventName: "getSnapshotTreeFailed", id: version.id });
1791
- }
1792
- return { snapshot, versionId: version?.id };
1793
- }
1794
-
1795
- private async instantiateContextDetached(
1796
- existing: boolean,
1797
- snapshot?: ISnapshotTree,
1798
- ) {
1799
- const codeDetails = this.getCodeDetailsFromQuorum();
1800
- if (codeDetails === undefined) {
1801
- throw new Error("pkg should be provided in create flow!!");
1802
- }
1803
-
1804
- await this.instantiateContext(
1805
- existing,
1806
- codeDetails,
1807
- snapshot,
1808
- );
1809
- }
1810
-
1811
- private async instantiateContext(
1812
- existing: boolean,
1813
- codeDetails: IFluidCodeDetails,
1814
- snapshot?: ISnapshotTree,
1815
- pendingLocalState?: unknown,
1816
- ) {
1817
- assert(this._context?.disposed !== false, 0x0dd /* "Existing context not disposed" */);
1818
-
1819
- // The relative loader will proxy requests to '/' to the loader itself assuming no non-cache flags
1820
- // are set. Global requests will still go directly to the loader
1821
- const loader = new RelativeLoader(this, this.loader);
1822
- this._context = await ContainerContext.createOrLoad(
1823
- this,
1824
- this.scope,
1825
- this.codeLoader,
1826
- codeDetails,
1827
- snapshot,
1828
- new DeltaManagerProxy(this._deltaManager),
1829
- new QuorumProxy(this.protocolHandler.quorum),
1830
- loader,
1831
- (type, contents, batch, metadata) => this.submitContainerMessage(type, contents, batch, metadata),
1832
- (summaryOp: ISummaryContent) => this.submitSummaryMessage(summaryOp),
1833
- (batch: IBatchMessage[]) => this.submitBatch(batch),
1834
- (message) => this.submitSignal(message),
1835
- (error?: ICriticalContainerError) => this.close(error),
1836
- Container.version,
1837
- (dirty: boolean) => this.updateDirtyContainerState(dirty),
1838
- existing,
1839
- pendingLocalState,
1840
- );
1841
-
1842
- this.emit("contextChanged", codeDetails);
1843
- }
1844
-
1845
- private updateDirtyContainerState(dirty: boolean) {
1846
- if (this._dirtyContainer === dirty) {
1847
- return;
1848
- }
1849
- this._dirtyContainer = dirty;
1850
- this.emit(dirty ? dirtyContainerEvent : savedContainerEvent);
1851
- }
1852
-
1853
- /**
1854
- * Set the connected state of the ContainerContext
1855
- * This controls the "connected" state of the ContainerRuntime as well
1856
- * @param state - Is the container currently connected?
1857
- * @param readonly - Is the container in readonly mode?
1858
- */
1859
- private setContextConnectedState(state: boolean, readonly: boolean): void {
1860
- if (this._context?.disposed === false) {
1861
- /**
1862
- * We want to lie to the ContainerRuntime when we are in readonly mode to prevent issues with pending
1863
- * ops getting through to the DeltaManager.
1864
- * The ContainerRuntime's "connected" state simply means it is ok to send ops
1865
- * See https://dev.azure.com/fluidframework/internal/_workitems/edit/1246
1866
- */
1867
- this.context.setConnectionState(state && !readonly, this.clientId);
1868
- }
1869
- }
269
+ export class Container
270
+ extends EventEmitterWithErrorHandling<IContainerEvents>
271
+ implements IContainer
272
+ {
273
+ public static version = "^0.1.0";
274
+
275
+ /**
276
+ * Load an existing container.
277
+ */
278
+ public static async load(
279
+ loader: Loader,
280
+ loadOptions: IContainerLoadOptions,
281
+ pendingLocalState?: IPendingContainerState,
282
+ protocolHandlerBuilder?: ProtocolHandlerBuilder,
283
+ ): Promise<Container> {
284
+ const container = new Container(
285
+ loader,
286
+ {
287
+ clientDetailsOverride: loadOptions.clientDetailsOverride,
288
+ resolvedUrl: loadOptions.resolvedUrl,
289
+ canReconnect: loadOptions.canReconnect,
290
+ serializedContainerState: pendingLocalState,
291
+ },
292
+ protocolHandlerBuilder,
293
+ );
294
+
295
+ return PerformanceEvent.timedExecAsync(
296
+ container.mc.logger,
297
+ { eventName: "Load" },
298
+ async (event) =>
299
+ new Promise<Container>((resolve, reject) => {
300
+ const version = loadOptions.version;
301
+
302
+ const defaultMode: IContainerLoadMode = { opsBeforeReturn: "cached" };
303
+ // if we have pendingLocalState, anything we cached is not useful and we shouldn't wait for connection
304
+ // to return container, so ignore this value and use undefined for opsBeforeReturn
305
+ const mode: IContainerLoadMode = pendingLocalState
306
+ ? { ...(loadOptions.loadMode ?? defaultMode), opsBeforeReturn: undefined }
307
+ : loadOptions.loadMode ?? defaultMode;
308
+
309
+ const onClosed = (err?: ICriticalContainerError) => {
310
+ // pre-0.58 error message: containerClosedWithoutErrorDuringLoad
311
+ reject(
312
+ err ?? new GenericError("Container closed without error during load"),
313
+ );
314
+ };
315
+ container.on("closed", onClosed);
316
+
317
+ container
318
+ .load(version, mode, pendingLocalState)
319
+ .finally(() => {
320
+ container.removeListener("closed", onClosed);
321
+ })
322
+ .then(
323
+ (props) => {
324
+ event.end({ ...props, ...loadOptions.loadMode });
325
+ resolve(container);
326
+ },
327
+ (error) => {
328
+ const err = normalizeError(error);
329
+ // Depending where error happens, we can be attempting to connect to web socket
330
+ // and continuously retrying (consider offline mode)
331
+ // Host has no container to close, so it's prudent to do it here
332
+ container.close(err);
333
+ onClosed(err);
334
+ },
335
+ );
336
+ }),
337
+ { start: true, end: true, cancel: "generic" },
338
+ );
339
+ }
340
+
341
+ /**
342
+ * Create a new container in a detached state.
343
+ */
344
+ public static async createDetached(
345
+ loader: Loader,
346
+ codeDetails: IFluidCodeDetails,
347
+ protocolHandlerBuilder?: ProtocolHandlerBuilder,
348
+ ): Promise<Container> {
349
+ const container = new Container(loader, {}, protocolHandlerBuilder);
350
+
351
+ return PerformanceEvent.timedExecAsync(
352
+ container.mc.logger,
353
+ { eventName: "CreateDetached" },
354
+ async (_event) => {
355
+ await container.createDetached(codeDetails);
356
+ return container;
357
+ },
358
+ { start: true, end: true, cancel: "generic" },
359
+ );
360
+ }
361
+
362
+ /**
363
+ * Create a new container in a detached state that is initialized with a
364
+ * snapshot from a previous detached container.
365
+ */
366
+ public static async rehydrateDetachedFromSnapshot(
367
+ loader: Loader,
368
+ snapshot: string,
369
+ protocolHandlerBuilder?: ProtocolHandlerBuilder,
370
+ ): Promise<Container> {
371
+ const container = new Container(loader, {}, protocolHandlerBuilder);
372
+
373
+ return PerformanceEvent.timedExecAsync(
374
+ container.mc.logger,
375
+ { eventName: "RehydrateDetachedFromSnapshot" },
376
+ async (_event) => {
377
+ const deserializedSummary = JSON.parse(snapshot) as ISummaryTree;
378
+ await container.rehydrateDetachedFromSnapshot(deserializedSummary);
379
+ return container;
380
+ },
381
+ { start: true, end: true, cancel: "generic" },
382
+ );
383
+ }
384
+
385
+ public subLogger: TelemetryLogger;
386
+
387
+ // Tells if container can reconnect on losing fist connection
388
+ // If false, container gets closed on loss of connection.
389
+ private readonly _canReconnect: boolean = true;
390
+
391
+ private readonly mc: MonitoringContext;
392
+
393
+ /**
394
+ * Lifecycle state of the container, used mainly to prevent re-entrancy and telemetry
395
+ *
396
+ * States are allowed to progress to further states:
397
+ * "loading" - "loaded" - "closing" - "disposing" - "closed" - "disposed"
398
+ *
399
+ * For example, moving from "closed" to "disposing" is not allowed since it is an earlier state.
400
+ *
401
+ * loading: Container has been created, but is not yet in normal/loaded state
402
+ * loaded: Container is in normal/loaded state
403
+ * closing: Container has started closing process (for re-entrancy prevention)
404
+ * disposing: Container has started disposing process (for re-entrancy prevention)
405
+ * closed: Container has closed
406
+ * disposed: Container has been disposed
407
+ */
408
+ private _lifecycleState:
409
+ | "loading"
410
+ | "loaded"
411
+ | "closing"
412
+ | "disposing"
413
+ | "closed"
414
+ | "disposed" = "loading";
415
+
416
+ private setLoaded() {
417
+ // It's conceivable the container could be closed when this is called
418
+ // Only transition states if currently loading
419
+ if (this._lifecycleState === "loading") {
420
+ // Propagate current connection state through the system.
421
+ this.propagateConnectionState(true /* initial transition */);
422
+ this._lifecycleState = "loaded";
423
+ }
424
+ }
425
+
426
+ public get closed(): boolean {
427
+ return (
428
+ this._lifecycleState === "closing" ||
429
+ this._lifecycleState === "closed" ||
430
+ this._lifecycleState === "disposing" ||
431
+ this._lifecycleState === "disposed"
432
+ );
433
+ }
434
+
435
+ private _attachState = AttachState.Detached;
436
+
437
+ private readonly storageService: ContainerStorageAdapter;
438
+ public get storage(): IDocumentStorageService {
439
+ return this.storageService;
440
+ }
441
+
442
+ private readonly clientDetailsOverride: IClientDetails | undefined;
443
+ private readonly _deltaManager: DeltaManager<ConnectionManager>;
444
+ private service: IDocumentService | undefined;
445
+
446
+ private _context: ContainerContext | undefined;
447
+ private get context() {
448
+ if (this._context === undefined) {
449
+ throw new GenericError("Attempted to access context before it was defined");
450
+ }
451
+ return this._context;
452
+ }
453
+ private _protocolHandler: IProtocolHandler | undefined;
454
+ private get protocolHandler() {
455
+ if (this._protocolHandler === undefined) {
456
+ throw new Error("Attempted to access protocolHandler before it was defined");
457
+ }
458
+ return this._protocolHandler;
459
+ }
460
+
461
+ /** During initialization we pause the inbound queues. We track this state to ensure we only call resume once */
462
+ private inboundQueuePausedFromInit = true;
463
+ private firstConnection = true;
464
+ private readonly connectionTransitionTimes: number[] = [];
465
+ private messageCountAfterDisconnection: number = 0;
466
+ private _loadedFromVersion: IVersion | undefined;
467
+ private _resolvedUrl: IFluidResolvedUrl | undefined;
468
+ private attachStarted = false;
469
+ private _dirtyContainer = false;
470
+
471
+ private lastVisible: number | undefined;
472
+ private readonly visibilityEventHandler: (() => void) | undefined;
473
+ private readonly connectionStateHandler: IConnectionStateHandler;
474
+
475
+ private setAutoReconnectTime = performance.now();
476
+
477
+ private collabWindowTracker: CollabWindowTracker | undefined;
478
+
479
+ private get connectionMode() {
480
+ return this._deltaManager.connectionManager.connectionMode;
481
+ }
482
+
483
+ public get IFluidRouter(): IFluidRouter {
484
+ return this;
485
+ }
486
+
487
+ public get resolvedUrl(): IResolvedUrl | undefined {
488
+ return this._resolvedUrl;
489
+ }
490
+
491
+ public get loadedFromVersion(): IVersion | undefined {
492
+ return this._loadedFromVersion;
493
+ }
494
+
495
+ public get readOnlyInfo(): ReadOnlyInfo {
496
+ return this._deltaManager.readOnlyInfo;
497
+ }
498
+
499
+ public get closeSignal(): AbortSignal {
500
+ return this._deltaManager.closeAbortController.signal;
501
+ }
502
+
503
+ /**
504
+ * Tracks host requiring read-only mode.
505
+ */
506
+ public forceReadonly(readonly: boolean) {
507
+ this._deltaManager.connectionManager.forceReadonly(readonly);
508
+ }
509
+
510
+ public get deltaManager(): IDeltaManager<ISequencedDocumentMessage, IDocumentMessage> {
511
+ return this._deltaManager;
512
+ }
513
+
514
+ public get connectionState(): ConnectionState {
515
+ return this.connectionStateHandler.connectionState;
516
+ }
517
+
518
+ public get connected(): boolean {
519
+ return this.connectionStateHandler.connectionState === ConnectionState.Connected;
520
+ }
521
+
522
+ /**
523
+ * Service configuration details. If running in offline mode will be undefined otherwise will contain service
524
+ * configuration details returned as part of the initial connection.
525
+ */
526
+ public get serviceConfiguration(): IClientConfiguration | undefined {
527
+ return this._deltaManager.serviceConfiguration;
528
+ }
529
+
530
+ private _clientId: string | undefined;
531
+
532
+ /**
533
+ * The server provided id of the client.
534
+ * Set once this.connected is true, otherwise undefined
535
+ */
536
+ public get clientId(): string | undefined {
537
+ return this._clientId;
538
+ }
539
+
540
+ /**
541
+ * The server provided claims of the client.
542
+ * Set once this.connected is true, otherwise undefined
543
+ */
544
+ public get scopes(): string[] | undefined {
545
+ return this._deltaManager.connectionManager.scopes;
546
+ }
547
+
548
+ public get clientDetails(): IClientDetails {
549
+ return this._deltaManager.clientDetails;
550
+ }
551
+
552
+ /**
553
+ * Get the code details that are currently specified for the container.
554
+ * @returns The current code details if any are specified, undefined if none are specified.
555
+ */
556
+ public getSpecifiedCodeDetails(): IFluidCodeDetails | undefined {
557
+ return this.getCodeDetailsFromQuorum();
558
+ }
559
+
560
+ /**
561
+ * Get the code details that were used to load the container.
562
+ * @returns The code details that were used to load the container if it is loaded, undefined if it is not yet
563
+ * loaded.
564
+ */
565
+ public getLoadedCodeDetails(): IFluidCodeDetails | undefined {
566
+ return this._context?.codeDetails;
567
+ }
568
+
569
+ /**
570
+ * Retrieves the audience associated with the document
571
+ */
572
+ public get audience(): IAudience {
573
+ return this.protocolHandler.audience;
574
+ }
575
+
576
+ /**
577
+ * Returns true if container is dirty.
578
+ * Which means data loss if container is closed at that same moment
579
+ * Most likely that happens when there is no network connection to Relay Service
580
+ */
581
+ public get isDirty() {
582
+ return this._dirtyContainer;
583
+ }
584
+
585
+ private get serviceFactory() {
586
+ return this.loader.services.documentServiceFactory;
587
+ }
588
+ private get urlResolver() {
589
+ return this.loader.services.urlResolver;
590
+ }
591
+ public readonly options: ILoaderOptions;
592
+ private get scope() {
593
+ return this.loader.services.scope;
594
+ }
595
+ private get codeLoader() {
596
+ return this.loader.services.codeLoader;
597
+ }
598
+
599
+ constructor(
600
+ private readonly loader: Loader,
601
+ config: IContainerConfig,
602
+ private readonly protocolHandlerBuilder?: ProtocolHandlerBuilder,
603
+ ) {
604
+ super((name, error) => {
605
+ this.mc.logger.sendErrorEvent(
606
+ {
607
+ eventName: "ContainerEventHandlerException",
608
+ name: typeof name === "string" ? name : undefined,
609
+ },
610
+ error,
611
+ );
612
+ });
613
+
614
+ this.clientDetailsOverride = config.clientDetailsOverride;
615
+ this._resolvedUrl = config.resolvedUrl;
616
+ if (config.canReconnect !== undefined) {
617
+ this._canReconnect = config.canReconnect;
618
+ }
619
+
620
+ // Create logger for data stores to use
621
+ const type = this.client.details.type;
622
+ const interactive = this.client.details.capabilities.interactive;
623
+ const clientType = `${interactive ? "interactive" : "noninteractive"}${
624
+ type !== undefined && type !== "" ? `/${type}` : ""
625
+ }`;
626
+ // Need to use the property getter for docId because for detached flow we don't have the docId initially.
627
+ // We assign the id later so property getter is used.
628
+ this.subLogger = ChildLogger.create(loader.services.subLogger, undefined, {
629
+ all: {
630
+ clientType, // Differentiating summarizer container from main container
631
+ containerId: uuid(),
632
+ docId: () => this._resolvedUrl?.id ?? undefined,
633
+ containerAttachState: () => this._attachState,
634
+ containerLifecycleState: () => this._lifecycleState,
635
+ containerConnectionState: () => ConnectionState[this.connectionState],
636
+ serializedContainer: config.serializedContainerState !== undefined,
637
+ },
638
+ // we need to be judicious with our logging here to avoid generating too much data
639
+ // all data logged here should be broadly applicable, and not specific to a
640
+ // specific error or class of errors
641
+ error: {
642
+ // load information to associate errors with the specific load point
643
+ dmInitialSeqNumber: () => this._deltaManager?.initialSequenceNumber,
644
+ dmLastProcessedSeqNumber: () => this._deltaManager?.lastSequenceNumber,
645
+ dmLastKnownSeqNumber: () => this._deltaManager?.lastKnownSeqNumber,
646
+ containerLoadedFromVersionId: () => this.loadedFromVersion?.id,
647
+ containerLoadedFromVersionDate: () => this.loadedFromVersion?.date,
648
+ // message information to associate errors with the specific execution state
649
+ // dmLastMsqSeqNumber: if present, same as dmLastProcessedSeqNumber
650
+ dmLastMsqSeqNumber: () => this.deltaManager?.lastMessage?.sequenceNumber,
651
+ dmLastMsqSeqTimestamp: () => this.deltaManager?.lastMessage?.timestamp,
652
+ dmLastMsqSeqClientId: () => this.deltaManager?.lastMessage?.clientId,
653
+ connectionStateDuration: () =>
654
+ performance.now() - this.connectionTransitionTimes[this.connectionState],
655
+ },
656
+ });
657
+
658
+ // Prefix all events in this file with container-loader
659
+ this.mc = loggerToMonitoringContext(ChildLogger.create(this.subLogger, "Container"));
660
+
661
+ const summarizeProtocolTree =
662
+ this.mc.config.getBoolean("Fluid.Container.summarizeProtocolTree2") ??
663
+ this.loader.services.options.summarizeProtocolTree;
664
+
665
+ this.options = {
666
+ ...this.loader.services.options,
667
+ summarizeProtocolTree,
668
+ };
669
+
670
+ this._deltaManager = this.createDeltaManager();
671
+
672
+ this._clientId = config.serializedContainerState?.clientId;
673
+ this.connectionStateHandler = createConnectionStateHandler(
674
+ {
675
+ logger: this.mc.logger,
676
+ connectionStateChanged: (value, oldState, reason) => {
677
+ if (value === ConnectionState.Connected) {
678
+ this._clientId = this.connectionStateHandler.pendingClientId;
679
+ }
680
+ this.logConnectionStateChangeTelemetry(value, oldState, reason);
681
+ if (this._lifecycleState === "loaded") {
682
+ this.propagateConnectionState(
683
+ false /* initial transition */,
684
+ value === ConnectionState.Disconnected
685
+ ? reason
686
+ : undefined /* disconnectedReason */,
687
+ );
688
+ }
689
+ },
690
+ shouldClientJoinWrite: () => this._deltaManager.connectionManager.shouldJoinWrite(),
691
+ maxClientLeaveWaitTime: this.loader.services.options.maxClientLeaveWaitTime,
692
+ logConnectionIssue: (
693
+ eventName: string,
694
+ category: TelemetryEventCategory,
695
+ details?: ITelemetryProperties,
696
+ ) => {
697
+ const mode = this.connectionMode;
698
+ // We get here when socket does not receive any ops on "write" connection, including
699
+ // its own join op.
700
+ // Report issues only if we already loaded container - op processing is paused while container is loading,
701
+ // so we always time-out processing of join op in cases where fetching snapshot takes a minute.
702
+ // It's not a problem with op processing itself - such issues should be tracked as part of boot perf monitoring instead.
703
+ this._deltaManager.logConnectionIssue({
704
+ eventName,
705
+ mode,
706
+ category: this._lifecycleState === "loading" ? "generic" : category,
707
+ duration:
708
+ performance.now() -
709
+ this.connectionTransitionTimes[ConnectionState.CatchingUp],
710
+ ...(details === undefined ? {} : { details: JSON.stringify(details) }),
711
+ });
712
+
713
+ // If this is "write" connection, it took too long to receive join op. But in most cases that's due
714
+ // to very slow op fetches and we will eventually get there.
715
+ // For "read" connections, we get here due to self join signal not arriving on time. We will need to
716
+ // better understand when and why it may happen.
717
+ // For now, attempt to recover by reconnecting. In future, maybe we can query relay service for
718
+ // current state of audience.
719
+ // Other possible recovery path - move to connected state (i.e. ConnectionStateHandler.joinOpTimer
720
+ // to call this.applyForConnectedState("addMemberEvent") for "read" connections)
721
+ if (mode === "read") {
722
+ this.disconnect();
723
+ this.connect();
724
+ }
725
+ },
726
+ },
727
+ this.deltaManager,
728
+ this._clientId,
729
+ );
730
+
731
+ this.on(savedContainerEvent, () => {
732
+ this.connectionStateHandler.containerSaved();
733
+ });
734
+
735
+ this.storageService = new ContainerStorageAdapter(
736
+ this.loader.services.detachedBlobStorage,
737
+ this.mc.logger,
738
+ this.options.summarizeProtocolTree === true
739
+ ? () => this.captureProtocolSummary()
740
+ : undefined,
741
+ );
742
+
743
+ const isDomAvailable =
744
+ typeof document === "object" &&
745
+ document !== null &&
746
+ typeof document.addEventListener === "function" &&
747
+ document.addEventListener !== null;
748
+ // keep track of last time page was visible for telemetry
749
+ if (isDomAvailable) {
750
+ this.lastVisible = document.hidden ? performance.now() : undefined;
751
+ this.visibilityEventHandler = () => {
752
+ if (document.hidden) {
753
+ this.lastVisible = performance.now();
754
+ } else {
755
+ // settimeout so this will hopefully fire after disconnect event if being hidden caused it
756
+ setTimeout(() => {
757
+ this.lastVisible = undefined;
758
+ }, 0);
759
+ }
760
+ };
761
+ document.addEventListener("visibilitychange", this.visibilityEventHandler);
762
+ }
763
+
764
+ // We observed that most users of platform do not check Container.connected event on load, causing bugs.
765
+ // As such, we are raising events when new listener pops up.
766
+ // Note that we can raise both "disconnected" & "connect" events at the same time,
767
+ // if we are in connecting stage.
768
+ this.on("newListener", (event: string, listener: (...args: any[]) => void) => {
769
+ // Fire events on the end of JS turn, giving a chance for caller to be in consistent state.
770
+ Promise.resolve()
771
+ .then(() => {
772
+ switch (event) {
773
+ case dirtyContainerEvent:
774
+ if (this._dirtyContainer) {
775
+ listener();
776
+ }
777
+ break;
778
+ case savedContainerEvent:
779
+ if (!this._dirtyContainer) {
780
+ listener();
781
+ }
782
+ break;
783
+ case connectedEventName:
784
+ if (this.connected) {
785
+ listener(this.clientId);
786
+ }
787
+ break;
788
+ case disconnectedEventName:
789
+ if (!this.connected) {
790
+ listener();
791
+ }
792
+ break;
793
+ default:
794
+ }
795
+ })
796
+ .catch((error) => {
797
+ this.mc.logger.sendErrorEvent({ eventName: "RaiseConnectedEventError" }, error);
798
+ });
799
+ });
800
+ }
801
+
802
+ /**
803
+ * Retrieves the quorum associated with the document
804
+ */
805
+ public getQuorum(): IQuorumClients {
806
+ return this.protocolHandler.quorum;
807
+ }
808
+
809
+ public dispose?(error?: ICriticalContainerError) {
810
+ this._deltaManager.close(error, true /* doDispose */);
811
+ this.verifyClosed();
812
+ }
813
+
814
+ public close(error?: ICriticalContainerError) {
815
+ // 1. Ensure that close sequence is exactly the same no matter if it's initiated by host or by DeltaManager
816
+ // 2. We need to ensure that we deliver disconnect event to runtime properly. See connectionStateChanged
817
+ // handler. We only deliver events if container fully loaded. Transitioning from "loading" ->
818
+ // "closing" will lose that info (can also solve by tracking extra state).
819
+ this._deltaManager.close(error);
820
+ this.verifyClosed();
821
+ }
822
+
823
+ private verifyClosed(): void {
824
+ assert(
825
+ this.connectionState === ConnectionState.Disconnected,
826
+ 0x0cf /* "disconnect event was not raised!" */,
827
+ );
828
+
829
+ assert(
830
+ this._lifecycleState === "closed" || this._lifecycleState === "disposed",
831
+ 0x314 /* Container properly closed */,
832
+ );
833
+ }
834
+
835
+ private closeCore(error?: ICriticalContainerError) {
836
+ assert(!this.closed, 0x315 /* re-entrancy */);
837
+
838
+ try {
839
+ // Ensure that we raise all key events even if one of these throws
840
+ try {
841
+ // Raise event first, to ensure we capture _lifecycleState before transition.
842
+ // This gives us a chance to know what errors happened on open vs. on fully loaded container.
843
+ this.mc.logger.sendTelemetryEvent(
844
+ {
845
+ eventName: "ContainerClose",
846
+ category: error === undefined ? "generic" : "error",
847
+ },
848
+ error,
849
+ );
850
+
851
+ this._lifecycleState = "closing";
852
+
853
+ this._protocolHandler?.close();
854
+
855
+ this.connectionStateHandler.dispose();
856
+
857
+ this._context?.dispose(error !== undefined ? new Error(error.message) : undefined);
858
+
859
+ this.storageService.dispose();
860
+
861
+ // Notify storage about critical errors. They may be due to disconnect between client & server knowledge
862
+ // about file, like file being overwritten in storage, but client having stale local cache.
863
+ // Driver need to ensure all caches are cleared on critical errors
864
+ this.service?.dispose(error);
865
+ } catch (exception) {
866
+ this.mc.logger.sendErrorEvent({ eventName: "ContainerCloseException" }, exception);
867
+ }
868
+
869
+ this.emit("closed", error);
870
+
871
+ if (this.visibilityEventHandler !== undefined) {
872
+ document.removeEventListener("visibilitychange", this.visibilityEventHandler);
873
+ }
874
+ } finally {
875
+ this._lifecycleState = "closed";
876
+ }
877
+ }
878
+
879
+ private _disposed = false;
880
+ private disposeCore(error?: ICriticalContainerError) {
881
+ assert(!this._disposed, 0x54c /* Container already disposed */);
882
+ this._disposed = true;
883
+
884
+ try {
885
+ // Ensure that we raise all key events even if one of these throws
886
+ try {
887
+ // Raise event first, to ensure we capture _lifecycleState before transition.
888
+ // This gives us a chance to know what errors happened on open vs. on fully loaded container.
889
+ this.mc.logger.sendTelemetryEvent(
890
+ {
891
+ eventName: "ContainerDispose",
892
+ category: error === undefined ? "generic" : "error",
893
+ },
894
+ error,
895
+ );
896
+
897
+ // ! Progressing from "closed" to "disposing" is not allowed
898
+ if (this._lifecycleState !== "closed") {
899
+ this._lifecycleState = "disposing";
900
+ }
901
+
902
+ this._protocolHandler?.close();
903
+
904
+ this.connectionStateHandler.dispose();
905
+
906
+ this._context?.dispose(error !== undefined ? new Error(error.message) : undefined);
907
+
908
+ this.storageService.dispose();
909
+
910
+ // Notify storage about critical errors. They may be due to disconnect between client & server knowledge
911
+ // about file, like file being overwritten in storage, but client having stale local cache.
912
+ // Driver need to ensure all caches are cleared on critical errors
913
+ this.service?.dispose(error);
914
+ } catch (exception) {
915
+ this.mc.logger.sendErrorEvent(
916
+ { eventName: "ContainerDisposeException" },
917
+ exception,
918
+ );
919
+ }
920
+
921
+ this.emit("disposed", error);
922
+
923
+ this.removeAllListeners();
924
+ if (this.visibilityEventHandler !== undefined) {
925
+ document.removeEventListener("visibilitychange", this.visibilityEventHandler);
926
+ }
927
+ } finally {
928
+ this._lifecycleState = "disposed";
929
+ }
930
+ }
931
+
932
+ public closeAndGetPendingLocalState(): string {
933
+ // runtime matches pending ops to successful ones by clientId and client seq num, so we need to close the
934
+ // container at the same time we get pending state, otherwise this container could reconnect and resubmit with
935
+ // a new clientId and a future container using stale pending state without the new clientId would resubmit them
936
+ assert(
937
+ this.attachState === AttachState.Attached,
938
+ 0x0d1 /* "Container should be attached before close" */,
939
+ );
940
+ assert(
941
+ this.resolvedUrl !== undefined && this.resolvedUrl.type === "fluid",
942
+ 0x0d2 /* "resolved url should be valid Fluid url" */,
943
+ );
944
+ assert(!!this._protocolHandler, 0x2e3 /* "Must have a valid protocol handler instance" */);
945
+ assert(
946
+ this._protocolHandler.attributes.term !== undefined,
947
+ 0x37e /* Must have a valid protocol handler instance */,
948
+ );
949
+ const pendingState: IPendingContainerState = {
950
+ pendingRuntimeState: this.context.getPendingLocalState(),
951
+ url: this.resolvedUrl.url,
952
+ protocol: this.protocolHandler.getProtocolState(),
953
+ term: this._protocolHandler.attributes.term,
954
+ clientId: this.clientId,
955
+ };
956
+
957
+ this.mc.logger.sendTelemetryEvent({ eventName: "CloseAndGetPendingLocalState" });
958
+
959
+ // Only close here as method name suggests
960
+ this.close();
961
+
962
+ return JSON.stringify(pendingState);
963
+ }
964
+
965
+ public get attachState(): AttachState {
966
+ return this._attachState;
967
+ }
968
+
969
+ public serialize(): string {
970
+ assert(
971
+ this.attachState === AttachState.Detached,
972
+ 0x0d3 /* "Should only be called in detached container" */,
973
+ );
974
+
975
+ const appSummary: ISummaryTree = this.context.createSummary();
976
+ const protocolSummary = this.captureProtocolSummary();
977
+ const combinedSummary = combineAppAndProtocolSummary(appSummary, protocolSummary);
978
+
979
+ if (
980
+ this.loader.services.detachedBlobStorage &&
981
+ this.loader.services.detachedBlobStorage.size > 0
982
+ ) {
983
+ combinedSummary.tree[".hasAttachmentBlobs"] = {
984
+ type: SummaryType.Blob,
985
+ content: "true",
986
+ };
987
+ }
988
+ return JSON.stringify(combinedSummary);
989
+ }
990
+
991
+ public async attach(request: IRequest): Promise<void> {
992
+ await PerformanceEvent.timedExecAsync(
993
+ this.mc.logger,
994
+ { eventName: "Attach" },
995
+ async () => {
996
+ if (this._lifecycleState !== "loaded") {
997
+ // pre-0.58 error message: containerNotValidForAttach
998
+ throw new UsageError(
999
+ `The Container is not in a valid state for attach [${this._lifecycleState}]`,
1000
+ );
1001
+ }
1002
+
1003
+ // If container is already attached or attach is in progress, throw an error.
1004
+ assert(
1005
+ this._attachState === AttachState.Detached && !this.attachStarted,
1006
+ 0x205 /* "attach() called more than once" */,
1007
+ );
1008
+ this.attachStarted = true;
1009
+
1010
+ // If attachment blobs were uploaded in detached state we will go through a different attach flow
1011
+ const hasAttachmentBlobs =
1012
+ this.loader.services.detachedBlobStorage !== undefined &&
1013
+ this.loader.services.detachedBlobStorage.size > 0;
1014
+
1015
+ try {
1016
+ assert(
1017
+ this.deltaManager.inbound.length === 0,
1018
+ 0x0d6 /* "Inbound queue should be empty when attaching" */,
1019
+ );
1020
+
1021
+ let summary: ISummaryTree;
1022
+ if (!hasAttachmentBlobs) {
1023
+ // Get the document state post attach - possibly can just call attach but we need to change the
1024
+ // semantics around what the attach means as far as async code goes.
1025
+ const appSummary: ISummaryTree = this.context.createSummary();
1026
+ const protocolSummary = this.captureProtocolSummary();
1027
+ summary = combineAppAndProtocolSummary(appSummary, protocolSummary);
1028
+
1029
+ // Set the state as attaching as we are starting the process of attaching container.
1030
+ // This should be fired after taking the summary because it is the place where we are
1031
+ // starting to attach the container to storage.
1032
+ // Also, this should only be fired in detached container.
1033
+ this._attachState = AttachState.Attaching;
1034
+ this.context.notifyAttaching(
1035
+ getSnapshotTreeFromSerializedContainer(summary),
1036
+ );
1037
+ }
1038
+
1039
+ // Actually go and create the resolved document
1040
+ const createNewResolvedUrl = await this.urlResolver.resolve(request);
1041
+ ensureFluidResolvedUrl(createNewResolvedUrl);
1042
+ if (this.service === undefined) {
1043
+ assert(
1044
+ this.client.details.type !== summarizerClientType,
1045
+ 0x2c4 /* "client should not be summarizer before container is created" */,
1046
+ );
1047
+ this.service = await runWithRetry(
1048
+ async () =>
1049
+ this.serviceFactory.createContainer(
1050
+ summary,
1051
+ createNewResolvedUrl,
1052
+ this.subLogger,
1053
+ false, // clientIsSummarizer
1054
+ ),
1055
+ "containerAttach",
1056
+ this.mc.logger,
1057
+ {
1058
+ cancel: this.closeSignal,
1059
+ }, // progress
1060
+ );
1061
+ }
1062
+ const resolvedUrl = this.service.resolvedUrl;
1063
+ ensureFluidResolvedUrl(resolvedUrl);
1064
+ this._resolvedUrl = resolvedUrl;
1065
+ await this.storageService.connectToService(this.service);
1066
+
1067
+ if (hasAttachmentBlobs) {
1068
+ // upload blobs to storage
1069
+ assert(
1070
+ !!this.loader.services.detachedBlobStorage,
1071
+ 0x24e /* "assertion for type narrowing" */,
1072
+ );
1073
+
1074
+ // build a table mapping IDs assigned locally to IDs assigned by storage and pass it to runtime to
1075
+ // support blob handles that only know about the local IDs
1076
+ const redirectTable = new Map<string, string>();
1077
+ // if new blobs are added while uploading, upload them too
1078
+ while (redirectTable.size < this.loader.services.detachedBlobStorage.size) {
1079
+ const newIds = this.loader.services.detachedBlobStorage
1080
+ .getBlobIds()
1081
+ .filter((id) => !redirectTable.has(id));
1082
+ for (const id of newIds) {
1083
+ const blob =
1084
+ await this.loader.services.detachedBlobStorage.readBlob(id);
1085
+ const response = await this.storageService.createBlob(blob);
1086
+ redirectTable.set(id, response.id);
1087
+ }
1088
+ }
1089
+
1090
+ // take summary and upload
1091
+ const appSummary: ISummaryTree = this.context.createSummary(redirectTable);
1092
+ const protocolSummary = this.captureProtocolSummary();
1093
+ summary = combineAppAndProtocolSummary(appSummary, protocolSummary);
1094
+
1095
+ this._attachState = AttachState.Attaching;
1096
+ this.context.notifyAttaching(
1097
+ getSnapshotTreeFromSerializedContainer(summary),
1098
+ );
1099
+
1100
+ await this.storageService.uploadSummaryWithContext(summary, {
1101
+ referenceSequenceNumber: 0,
1102
+ ackHandle: undefined,
1103
+ proposalHandle: undefined,
1104
+ });
1105
+ }
1106
+
1107
+ this._attachState = AttachState.Attached;
1108
+ this.emit("attached");
1109
+
1110
+ if (!this.closed) {
1111
+ this.resumeInternal({
1112
+ fetchOpsFromStorage: false,
1113
+ reason: "createDetached",
1114
+ });
1115
+ }
1116
+ } catch (error) {
1117
+ // add resolved URL on error object so that host has the ability to find this document and delete it
1118
+ const newError = normalizeError(error);
1119
+ const resolvedUrl = this.resolvedUrl;
1120
+ if (isFluidResolvedUrl(resolvedUrl)) {
1121
+ newError.addTelemetryProperties({ resolvedUrl: resolvedUrl.url });
1122
+ }
1123
+ this.close(newError);
1124
+ this.dispose?.(newError);
1125
+ throw newError;
1126
+ }
1127
+ },
1128
+ { start: true, end: true, cancel: "generic" },
1129
+ );
1130
+ }
1131
+
1132
+ public async request(path: IRequest): Promise<IResponse> {
1133
+ return PerformanceEvent.timedExecAsync(
1134
+ this.mc.logger,
1135
+ { eventName: "Request" },
1136
+ async () => this.context.request(path),
1137
+ { end: true, cancel: "error" },
1138
+ );
1139
+ }
1140
+
1141
+ private setAutoReconnectInternal(mode: ReconnectMode) {
1142
+ const currentMode = this._deltaManager.connectionManager.reconnectMode;
1143
+
1144
+ if (currentMode === mode) {
1145
+ return;
1146
+ }
1147
+
1148
+ const now = performance.now();
1149
+ const duration = now - this.setAutoReconnectTime;
1150
+ this.setAutoReconnectTime = now;
1151
+
1152
+ this.mc.logger.sendTelemetryEvent({
1153
+ eventName:
1154
+ mode === ReconnectMode.Enabled ? "AutoReconnectEnabled" : "AutoReconnectDisabled",
1155
+ connectionMode: this.connectionMode,
1156
+ connectionState: ConnectionState[this.connectionState],
1157
+ duration,
1158
+ });
1159
+
1160
+ this._deltaManager.connectionManager.setAutoReconnect(mode);
1161
+ }
1162
+
1163
+ public connect() {
1164
+ if (this.closed) {
1165
+ throw new UsageError(`The Container is closed and cannot be connected`);
1166
+ } else if (this._attachState !== AttachState.Attached) {
1167
+ throw new UsageError(`The Container is not attached and cannot be connected`);
1168
+ } else if (!this.connected) {
1169
+ // Note: no need to fetch ops as we do it preemptively as part of DeltaManager.attachOpHandler().
1170
+ // If there is gap, we will learn about it once connected, but the gap should be small (if any),
1171
+ // assuming that connect() is called quickly after initial container boot.
1172
+ this.connectInternal({ reason: "DocumentConnect", fetchOpsFromStorage: false });
1173
+ }
1174
+ }
1175
+
1176
+ private connectInternal(args: IConnectionArgs) {
1177
+ assert(!this.closed, 0x2c5 /* "Attempting to connect() a closed Container" */);
1178
+ assert(
1179
+ this._attachState === AttachState.Attached,
1180
+ 0x2c6 /* "Attempting to connect() a container that is not attached" */,
1181
+ );
1182
+
1183
+ // Resume processing ops and connect to delta stream
1184
+ this.resumeInternal(args);
1185
+
1186
+ // Set Auto Reconnect Mode
1187
+ const mode = ReconnectMode.Enabled;
1188
+ this.setAutoReconnectInternal(mode);
1189
+ }
1190
+
1191
+ public disconnect() {
1192
+ if (this.closed) {
1193
+ throw new UsageError(`The Container is closed and cannot be disconnected`);
1194
+ } else {
1195
+ this.disconnectInternal();
1196
+ }
1197
+ }
1198
+
1199
+ private disconnectInternal() {
1200
+ assert(!this.closed, 0x2c7 /* "Attempting to disconnect() a closed Container" */);
1201
+
1202
+ // Set Auto Reconnect Mode
1203
+ const mode = ReconnectMode.Disabled;
1204
+ this.setAutoReconnectInternal(mode);
1205
+ }
1206
+
1207
+ private resumeInternal(args: IConnectionArgs) {
1208
+ assert(!this.closed, 0x0d9 /* "Attempting to connect() a closed DeltaManager" */);
1209
+
1210
+ // Resume processing ops
1211
+ if (this.inboundQueuePausedFromInit) {
1212
+ this.inboundQueuePausedFromInit = false;
1213
+ this._deltaManager.inbound.resume();
1214
+ this._deltaManager.inboundSignal.resume();
1215
+ }
1216
+
1217
+ // Ensure connection to web socket
1218
+ this.connectToDeltaStream(args);
1219
+ }
1220
+
1221
+ public async getAbsoluteUrl(relativeUrl: string): Promise<string | undefined> {
1222
+ if (this.resolvedUrl === undefined) {
1223
+ return undefined;
1224
+ }
1225
+
1226
+ return this.urlResolver.getAbsoluteUrl(
1227
+ this.resolvedUrl,
1228
+ relativeUrl,
1229
+ getPackageName(this._context?.codeDetails),
1230
+ );
1231
+ }
1232
+
1233
+ public async proposeCodeDetails(codeDetails: IFluidCodeDetails) {
1234
+ if (!isFluidCodeDetails(codeDetails)) {
1235
+ throw new Error("Provided codeDetails are not IFluidCodeDetails");
1236
+ }
1237
+
1238
+ if (this.codeLoader.IFluidCodeDetailsComparer) {
1239
+ const comparison = await this.codeLoader.IFluidCodeDetailsComparer.compare(
1240
+ codeDetails,
1241
+ this.getCodeDetailsFromQuorum(),
1242
+ );
1243
+ if (comparison !== undefined && comparison <= 0) {
1244
+ throw new Error("Proposed code details should be greater than the current");
1245
+ }
1246
+ }
1247
+
1248
+ return this.protocolHandler.quorum
1249
+ .propose("code", codeDetails)
1250
+ .then(() => true)
1251
+ .catch(() => false);
1252
+ }
1253
+
1254
+ private async processCodeProposal(): Promise<void> {
1255
+ const codeDetails = this.getCodeDetailsFromQuorum();
1256
+
1257
+ await Promise.all([
1258
+ this.deltaManager.inbound.pause(),
1259
+ this.deltaManager.inboundSignal.pause(),
1260
+ ]);
1261
+
1262
+ if ((await this.context.satisfies(codeDetails)) === true) {
1263
+ this.deltaManager.inbound.resume();
1264
+ this.deltaManager.inboundSignal.resume();
1265
+ return;
1266
+ }
1267
+
1268
+ // pre-0.58 error message: existingContextDoesNotSatisfyIncomingProposal
1269
+ const error = new GenericError("Existing context does not satisfy incoming proposal");
1270
+ this.close(error);
1271
+ this.dispose?.(error);
1272
+ }
1273
+
1274
+ private async getVersion(version: string | null): Promise<IVersion | undefined> {
1275
+ const versions = await this.storageService.getVersions(version, 1);
1276
+ return versions[0];
1277
+ }
1278
+
1279
+ private recordConnectStartTime() {
1280
+ if (this.connectionTransitionTimes[ConnectionState.Disconnected] === undefined) {
1281
+ this.connectionTransitionTimes[ConnectionState.Disconnected] = performance.now();
1282
+ }
1283
+ }
1284
+
1285
+ private connectToDeltaStream(args: IConnectionArgs) {
1286
+ this.recordConnectStartTime();
1287
+
1288
+ // All agents need "write" access, including summarizer.
1289
+ if (!this._canReconnect || !this.client.details.capabilities.interactive) {
1290
+ args.mode = "write";
1291
+ }
1292
+
1293
+ this._deltaManager.connect(args);
1294
+ }
1295
+
1296
+ /**
1297
+ * Load container.
1298
+ *
1299
+ * @param specifiedVersion - Version SHA to load snapshot. If not specified, will fetch the latest snapshot.
1300
+ */
1301
+ private async load(
1302
+ specifiedVersion: string | undefined,
1303
+ loadMode: IContainerLoadMode,
1304
+ pendingLocalState?: IPendingContainerState,
1305
+ ) {
1306
+ if (this._resolvedUrl === undefined) {
1307
+ throw new Error("Attempting to load without a resolved url");
1308
+ }
1309
+ this.service = await this.serviceFactory.createDocumentService(
1310
+ this._resolvedUrl,
1311
+ this.subLogger,
1312
+ this.client.details.type === summarizerClientType,
1313
+ );
1314
+
1315
+ // Ideally we always connect as "read" by default.
1316
+ // Currently that works with SPO & r11s, because we get "write" connection when connecting to non-existing file.
1317
+ // We should not rely on it by (one of them will address the issue, but we need to address both)
1318
+ // 1) switching create new flow to one where we create file by posting snapshot
1319
+ // 2) Fixing quorum workflows (have retry logic)
1320
+ // That all said, "read" does not work with memorylicious workflows (that opens two simultaneous
1321
+ // connections to same file) in two ways:
1322
+ // A) creation flow breaks (as one of the clients "sees" file as existing, and hits #2 above)
1323
+ // B) Once file is created, transition from view-only connection to write does not work - some bugs to be fixed.
1324
+ const connectionArgs: IConnectionArgs = {
1325
+ reason: "DocumentOpen",
1326
+ mode: "write",
1327
+ fetchOpsFromStorage: false,
1328
+ };
1329
+
1330
+ // Start websocket connection as soon as possible. Note that there is no op handler attached yet, but the
1331
+ // DeltaManager is resilient to this and will wait to start processing ops until after it is attached.
1332
+ if (loadMode.deltaConnection === undefined) {
1333
+ this.connectToDeltaStream(connectionArgs);
1334
+ }
1335
+
1336
+ if (!pendingLocalState) {
1337
+ await this.storageService.connectToService(this.service);
1338
+ } else {
1339
+ // if we have pendingLocalState we can load without storage; don't wait for connection
1340
+ this.storageService.connectToService(this.service).catch((error) => {
1341
+ this.close(error);
1342
+ this.dispose?.(error);
1343
+ });
1344
+ }
1345
+
1346
+ this._attachState = AttachState.Attached;
1347
+
1348
+ // Fetch specified snapshot.
1349
+ const { snapshot, versionId } =
1350
+ pendingLocalState === undefined
1351
+ ? await this.fetchSnapshotTree(specifiedVersion)
1352
+ : { snapshot: undefined, versionId: undefined };
1353
+ assert(
1354
+ snapshot !== undefined || pendingLocalState !== undefined,
1355
+ 0x237 /* "Snapshot should exist" */,
1356
+ );
1357
+
1358
+ const attributes: IDocumentAttributes =
1359
+ pendingLocalState === undefined
1360
+ ? await this.getDocumentAttributes(this.storageService, snapshot)
1361
+ : {
1362
+ sequenceNumber: pendingLocalState.protocol.sequenceNumber,
1363
+ minimumSequenceNumber: pendingLocalState.protocol.minimumSequenceNumber,
1364
+ term: pendingLocalState.term,
1365
+ };
1366
+
1367
+ let opsBeforeReturnP: Promise<void> | undefined;
1368
+
1369
+ // Attach op handlers to finish initialization and be able to start processing ops
1370
+ // Kick off any ops fetching if required.
1371
+ switch (loadMode.opsBeforeReturn) {
1372
+ case undefined:
1373
+ // Start prefetch, but not set opsBeforeReturnP - boot is not blocked by it!
1374
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
1375
+ this.attachDeltaManagerOpHandler(
1376
+ attributes,
1377
+ loadMode.deltaConnection !== "none" ? "all" : "none",
1378
+ );
1379
+ break;
1380
+ case "cached":
1381
+ opsBeforeReturnP = this.attachDeltaManagerOpHandler(attributes, "cached");
1382
+ break;
1383
+ case "all":
1384
+ opsBeforeReturnP = this.attachDeltaManagerOpHandler(attributes, "all");
1385
+ break;
1386
+ default:
1387
+ unreachableCase(loadMode.opsBeforeReturn);
1388
+ }
1389
+
1390
+ // ...load in the existing quorum
1391
+ // Initialize the protocol handler
1392
+ if (pendingLocalState === undefined) {
1393
+ await this.initializeProtocolStateFromSnapshot(
1394
+ attributes,
1395
+ this.storageService,
1396
+ snapshot,
1397
+ );
1398
+ } else {
1399
+ this.initializeProtocolState(
1400
+ attributes,
1401
+ {
1402
+ members: pendingLocalState.protocol.members,
1403
+ proposals: pendingLocalState.protocol.proposals,
1404
+ values: pendingLocalState.protocol.values,
1405
+ }, // pending IQuorumSnapshot
1406
+ );
1407
+ }
1408
+
1409
+ const codeDetails = this.getCodeDetailsFromQuorum();
1410
+ await this.instantiateContext(
1411
+ true, // existing
1412
+ codeDetails,
1413
+ snapshot,
1414
+ pendingLocalState?.pendingRuntimeState,
1415
+ );
1416
+
1417
+ // We might have hit some failure that did not manifest itself in exception in this flow,
1418
+ // do not start op processing in such case - static version of Container.load() will handle it correctly.
1419
+ if (!this.closed) {
1420
+ if (opsBeforeReturnP !== undefined) {
1421
+ this._deltaManager.inbound.resume();
1422
+
1423
+ await PerformanceEvent.timedExecAsync(
1424
+ this.mc.logger,
1425
+ { eventName: "WaitOps" },
1426
+ async () => opsBeforeReturnP,
1427
+ );
1428
+ await PerformanceEvent.timedExecAsync(
1429
+ this.mc.logger,
1430
+ { eventName: "WaitOpProcessing" },
1431
+ async () => this._deltaManager.inbound.waitTillProcessingDone(),
1432
+ );
1433
+
1434
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
1435
+ this._deltaManager.inbound.pause();
1436
+ }
1437
+
1438
+ switch (loadMode.deltaConnection) {
1439
+ case undefined:
1440
+ case "delayed":
1441
+ assert(
1442
+ this.inboundQueuePausedFromInit,
1443
+ 0x346 /* inboundQueuePausedFromInit should be true */,
1444
+ );
1445
+ this.inboundQueuePausedFromInit = false;
1446
+ this._deltaManager.inbound.resume();
1447
+ this._deltaManager.inboundSignal.resume();
1448
+ break;
1449
+ case "none":
1450
+ break;
1451
+ default:
1452
+ unreachableCase(loadMode.deltaConnection);
1453
+ }
1454
+ }
1455
+
1456
+ // Safety net: static version of Container.load() should have learned about it through "closed" handler.
1457
+ // But if that did not happen for some reason, fail load for sure.
1458
+ // Otherwise we can get into situations where container is closed and does not try to connect to ordering
1459
+ // service, but caller does not know that (callers do expect container to be not closed on successful path
1460
+ // and listen only on "closed" event)
1461
+ if (this.closed) {
1462
+ throw new Error("Container was closed while load()");
1463
+ }
1464
+
1465
+ // Internal context is fully loaded at this point
1466
+ this.setLoaded();
1467
+
1468
+ return {
1469
+ sequenceNumber: attributes.sequenceNumber,
1470
+ version: versionId,
1471
+ dmLastProcessedSeqNumber: this._deltaManager.lastSequenceNumber,
1472
+ dmLastKnownSeqNumber: this._deltaManager.lastKnownSeqNumber,
1473
+ };
1474
+ }
1475
+
1476
+ private async createDetached(source: IFluidCodeDetails) {
1477
+ const attributes: IDocumentAttributes = {
1478
+ sequenceNumber: detachedContainerRefSeqNumber,
1479
+ term: 1,
1480
+ minimumSequenceNumber: 0,
1481
+ };
1482
+
1483
+ await this.attachDeltaManagerOpHandler(attributes);
1484
+
1485
+ // Need to just seed the source data in the code quorum. Quorum itself is empty
1486
+ const qValues = initQuorumValuesFromCodeDetails(source);
1487
+ this.initializeProtocolState(
1488
+ attributes,
1489
+ {
1490
+ members: [],
1491
+ proposals: [],
1492
+ values: qValues,
1493
+ }, // IQuorumSnapShot
1494
+ );
1495
+
1496
+ // The load context - given we seeded the quorum - will be great
1497
+ await this.instantiateContextDetached(
1498
+ false, // existing
1499
+ );
1500
+
1501
+ this.setLoaded();
1502
+ }
1503
+
1504
+ private async rehydrateDetachedFromSnapshot(detachedContainerSnapshot: ISummaryTree) {
1505
+ if (detachedContainerSnapshot.tree[".hasAttachmentBlobs"] !== undefined) {
1506
+ assert(
1507
+ !!this.loader.services.detachedBlobStorage &&
1508
+ this.loader.services.detachedBlobStorage.size > 0,
1509
+ 0x250 /* "serialized container with attachment blobs must be rehydrated with detached blob storage" */,
1510
+ );
1511
+ delete detachedContainerSnapshot.tree[".hasAttachmentBlobs"];
1512
+ }
1513
+
1514
+ const snapshotTree = getSnapshotTreeFromSerializedContainer(detachedContainerSnapshot);
1515
+ this.storageService.loadSnapshotForRehydratingContainer(snapshotTree);
1516
+ const attributes = await this.getDocumentAttributes(this.storageService, snapshotTree);
1517
+
1518
+ await this.attachDeltaManagerOpHandler(attributes);
1519
+
1520
+ // Initialize the protocol handler
1521
+ const baseTree = getProtocolSnapshotTree(snapshotTree);
1522
+ const qValues = await readAndParse<[string, ICommittedProposal][]>(
1523
+ this.storageService,
1524
+ baseTree.blobs.quorumValues,
1525
+ );
1526
+ const codeDetails = getCodeDetailsFromQuorumValues(qValues);
1527
+ this.initializeProtocolState(
1528
+ attributes,
1529
+ {
1530
+ members: [],
1531
+ proposals: [],
1532
+ values:
1533
+ codeDetails !== undefined ? initQuorumValuesFromCodeDetails(codeDetails) : [],
1534
+ }, // IQuorumSnapShot
1535
+ );
1536
+
1537
+ await this.instantiateContextDetached(
1538
+ true, // existing
1539
+ snapshotTree,
1540
+ );
1541
+
1542
+ this.setLoaded();
1543
+ }
1544
+
1545
+ private async getDocumentAttributes(
1546
+ storage: IDocumentStorageService,
1547
+ tree: ISnapshotTree | undefined,
1548
+ ): Promise<IDocumentAttributes> {
1549
+ if (tree === undefined) {
1550
+ return {
1551
+ minimumSequenceNumber: 0,
1552
+ sequenceNumber: 0,
1553
+ term: 1,
1554
+ };
1555
+ }
1556
+
1557
+ // Backward compatibility: old docs would have ".attributes" instead of "attributes"
1558
+ const attributesHash =
1559
+ ".protocol" in tree.trees
1560
+ ? tree.trees[".protocol"].blobs.attributes
1561
+ : tree.blobs[".attributes"];
1562
+
1563
+ const attributes = await readAndParse<IDocumentAttributes>(storage, attributesHash);
1564
+
1565
+ // Backward compatibility for older summaries with no term
1566
+ if (attributes.term === undefined) {
1567
+ attributes.term = 1;
1568
+ }
1569
+
1570
+ return attributes;
1571
+ }
1572
+
1573
+ private async initializeProtocolStateFromSnapshot(
1574
+ attributes: IDocumentAttributes,
1575
+ storage: IDocumentStorageService,
1576
+ snapshot: ISnapshotTree | undefined,
1577
+ ): Promise<void> {
1578
+ const quorumSnapshot: IQuorumSnapshot = {
1579
+ members: [],
1580
+ proposals: [],
1581
+ values: [],
1582
+ };
1583
+
1584
+ if (snapshot !== undefined) {
1585
+ const baseTree = getProtocolSnapshotTree(snapshot);
1586
+ [quorumSnapshot.members, quorumSnapshot.proposals, quorumSnapshot.values] =
1587
+ await Promise.all([
1588
+ readAndParse<[string, ISequencedClient][]>(
1589
+ storage,
1590
+ baseTree.blobs.quorumMembers,
1591
+ ),
1592
+ readAndParse<[number, ISequencedProposal, string[]][]>(
1593
+ storage,
1594
+ baseTree.blobs.quorumProposals,
1595
+ ),
1596
+ readAndParse<[string, ICommittedProposal][]>(
1597
+ storage,
1598
+ baseTree.blobs.quorumValues,
1599
+ ),
1600
+ ]);
1601
+ }
1602
+
1603
+ this.initializeProtocolState(attributes, quorumSnapshot);
1604
+ }
1605
+
1606
+ private initializeProtocolState(
1607
+ attributes: IDocumentAttributes,
1608
+ quorumSnapshot: IQuorumSnapshot,
1609
+ ): void {
1610
+ const protocolHandlerBuilder =
1611
+ this.protocolHandlerBuilder ??
1612
+ ((...args) => new ProtocolHandler(...args, new Audience()));
1613
+ const protocol = protocolHandlerBuilder(attributes, quorumSnapshot, (key, value) =>
1614
+ this.submitMessage(MessageType.Propose, JSON.stringify({ key, value })),
1615
+ );
1616
+
1617
+ const protocolLogger = ChildLogger.create(this.subLogger, "ProtocolHandler");
1618
+
1619
+ protocol.quorum.on("error", (error) => {
1620
+ protocolLogger.sendErrorEvent(error);
1621
+ });
1622
+
1623
+ // Track membership changes and update connection state accordingly
1624
+ this.connectionStateHandler.initProtocol(protocol);
1625
+
1626
+ protocol.quorum.on("addProposal", (proposal: ISequencedProposal) => {
1627
+ if (proposal.key === "code" || proposal.key === "code2") {
1628
+ this.emit("codeDetailsProposed", proposal.value, proposal);
1629
+ }
1630
+ });
1631
+
1632
+ protocol.quorum.on("approveProposal", (sequenceNumber, key, value) => {
1633
+ if (key === "code" || key === "code2") {
1634
+ if (!isFluidCodeDetails(value)) {
1635
+ this.mc.logger.sendErrorEvent({
1636
+ eventName: "CodeProposalNotIFluidCodeDetails",
1637
+ });
1638
+ }
1639
+ this.processCodeProposal().catch((error) => {
1640
+ const normalizedError = normalizeError(error);
1641
+ this.close(normalizedError);
1642
+ this.dispose?.(normalizedError);
1643
+ throw error;
1644
+ });
1645
+ }
1646
+ });
1647
+ // we need to make sure this member get set in a synchronous context,
1648
+ // or other things can happen after the object that will be set is created, but not yet set
1649
+ // this was breaking this._initialClients handling
1650
+ //
1651
+ this._protocolHandler = protocol;
1652
+ }
1653
+
1654
+ private captureProtocolSummary(): ISummaryTree {
1655
+ const quorumSnapshot = this.protocolHandler.snapshot();
1656
+ const summary: ISummaryTree = {
1657
+ tree: {
1658
+ attributes: {
1659
+ content: JSON.stringify(this.protocolHandler.attributes),
1660
+ type: SummaryType.Blob,
1661
+ },
1662
+ quorumMembers: {
1663
+ content: JSON.stringify(quorumSnapshot.members),
1664
+ type: SummaryType.Blob,
1665
+ },
1666
+ quorumProposals: {
1667
+ content: JSON.stringify(quorumSnapshot.proposals),
1668
+ type: SummaryType.Blob,
1669
+ },
1670
+ quorumValues: {
1671
+ content: JSON.stringify(quorumSnapshot.values),
1672
+ type: SummaryType.Blob,
1673
+ },
1674
+ },
1675
+ type: SummaryType.Tree,
1676
+ };
1677
+
1678
+ return summary;
1679
+ }
1680
+
1681
+ private getCodeDetailsFromQuorum(): IFluidCodeDetails {
1682
+ const quorum = this.protocolHandler.quorum;
1683
+
1684
+ const pkg = getCodeProposal(quorum);
1685
+
1686
+ return pkg as IFluidCodeDetails;
1687
+ }
1688
+
1689
+ private get client(): IClient {
1690
+ const client: IClient =
1691
+ this.options?.client !== undefined
1692
+ ? (this.options.client as IClient)
1693
+ : {
1694
+ details: {
1695
+ capabilities: { interactive: true },
1696
+ },
1697
+ mode: "read", // default reconnection mode on lost connection / connection error
1698
+ permission: [],
1699
+ scopes: [],
1700
+ user: { id: "" },
1701
+ };
1702
+
1703
+ if (this.clientDetailsOverride !== undefined) {
1704
+ merge(client.details, this.clientDetailsOverride);
1705
+ }
1706
+ client.details.environment = [
1707
+ client.details.environment,
1708
+ ` loaderVersion:${pkgVersion}`,
1709
+ ].join(";");
1710
+ return client;
1711
+ }
1712
+
1713
+ /**
1714
+ * Returns true if connection is active, i.e. it's "write" connection and
1715
+ * container runtime was notified about this connection (i.e. we are up-to-date and could send ops).
1716
+ * This happens after client received its own joinOp and thus is in the quorum.
1717
+ * If it's not true, runtime is not in position to send ops.
1718
+ */
1719
+ private activeConnection() {
1720
+ return (
1721
+ this.connectionState === ConnectionState.Connected && this.connectionMode === "write"
1722
+ );
1723
+ }
1724
+
1725
+ private createDeltaManager() {
1726
+ const serviceProvider = () => this.service;
1727
+ const deltaManager = new DeltaManager<ConnectionManager>(
1728
+ serviceProvider,
1729
+ ChildLogger.create(this.subLogger, "DeltaManager"),
1730
+ () => this.activeConnection(),
1731
+ (props: IConnectionManagerFactoryArgs) =>
1732
+ new ConnectionManager(
1733
+ serviceProvider,
1734
+ this.client,
1735
+ this._canReconnect,
1736
+ ChildLogger.create(this.subLogger, "ConnectionManager"),
1737
+ props,
1738
+ ),
1739
+ );
1740
+
1741
+ // Disable inbound queues as Container is not ready to accept any ops until we are fully loaded!
1742
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
1743
+ deltaManager.inbound.pause();
1744
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
1745
+ deltaManager.inboundSignal.pause();
1746
+
1747
+ deltaManager.on("connect", (details: IConnectionDetails, _opsBehind?: number) => {
1748
+ assert(this.connectionMode === details.mode, 0x4b7 /* mismatch */);
1749
+ this.connectionStateHandler.receivedConnectEvent(details);
1750
+ });
1751
+
1752
+ deltaManager.on("disconnect", (reason: string) => {
1753
+ this.collabWindowTracker?.stopSequenceNumberUpdate();
1754
+ if (!this.closed) {
1755
+ this.connectionStateHandler.receivedDisconnectEvent(reason);
1756
+ }
1757
+ });
1758
+
1759
+ deltaManager.on("throttled", (warning: IThrottlingWarning) => {
1760
+ const warn = warning as ContainerWarning;
1761
+ // Some "warning" events come from outside the container and are logged
1762
+ // elsewhere (e.g. summarizing container). We shouldn't log these here.
1763
+ if (warn.logged !== true) {
1764
+ this.mc.logger.sendTelemetryEvent({ eventName: "ContainerWarning" }, warn);
1765
+ }
1766
+ this.emit("warning", warn);
1767
+ });
1768
+
1769
+ deltaManager.on("readonly", (readonly) => {
1770
+ this.setContextConnectedState(
1771
+ this.connectionState === ConnectionState.Connected,
1772
+ readonly,
1773
+ );
1774
+ this.emit("readonly", readonly);
1775
+ });
1776
+
1777
+ deltaManager.on("closed", (error?: ICriticalContainerError) => {
1778
+ this.closeCore(error);
1779
+ });
1780
+
1781
+ deltaManager.on("disposed", (error?: ICriticalContainerError) => {
1782
+ this.disposeCore(error);
1783
+ });
1784
+
1785
+ return deltaManager;
1786
+ }
1787
+
1788
+ private async attachDeltaManagerOpHandler(
1789
+ attributes: IDocumentAttributes,
1790
+ prefetchType?: "cached" | "all" | "none",
1791
+ ) {
1792
+ return this._deltaManager.attachOpHandler(
1793
+ attributes.minimumSequenceNumber,
1794
+ attributes.sequenceNumber,
1795
+ attributes.term ?? 1,
1796
+ {
1797
+ process: (message) => this.processRemoteMessage(message),
1798
+ processSignal: (message) => {
1799
+ this.processSignal(message);
1800
+ },
1801
+ },
1802
+ prefetchType,
1803
+ );
1804
+ }
1805
+
1806
+ private logConnectionStateChangeTelemetry(
1807
+ value: ConnectionState,
1808
+ oldState: ConnectionState,
1809
+ reason?: string,
1810
+ ) {
1811
+ // Log actual event
1812
+ const time = performance.now();
1813
+ this.connectionTransitionTimes[value] = time;
1814
+ const duration = time - this.connectionTransitionTimes[oldState];
1815
+
1816
+ let durationFromDisconnected: number | undefined;
1817
+ let connectionInitiationReason: string | undefined;
1818
+ let autoReconnect: ReconnectMode | undefined;
1819
+ let checkpointSequenceNumber: number | undefined;
1820
+ let opsBehind: number | undefined;
1821
+ if (value === ConnectionState.Disconnected) {
1822
+ autoReconnect = this._deltaManager.connectionManager.reconnectMode;
1823
+ } else {
1824
+ if (value === ConnectionState.Connected) {
1825
+ durationFromDisconnected =
1826
+ time - this.connectionTransitionTimes[ConnectionState.Disconnected];
1827
+ durationFromDisconnected = TelemetryLogger.formatTick(durationFromDisconnected);
1828
+ } else {
1829
+ // This info is of most interest on establishing connection only.
1830
+ checkpointSequenceNumber = this.deltaManager.lastKnownSeqNumber;
1831
+ if (this.deltaManager.hasCheckpointSequenceNumber) {
1832
+ opsBehind = checkpointSequenceNumber - this.deltaManager.lastSequenceNumber;
1833
+ }
1834
+ }
1835
+ connectionInitiationReason = this.firstConnection ? "InitialConnect" : "AutoReconnect";
1836
+ }
1837
+
1838
+ this.mc.logger.sendPerformanceEvent({
1839
+ eventName: `ConnectionStateChange_${ConnectionState[value]}`,
1840
+ from: ConnectionState[oldState],
1841
+ duration,
1842
+ durationFromDisconnected,
1843
+ reason,
1844
+ connectionInitiationReason,
1845
+ pendingClientId: this.connectionStateHandler.pendingClientId,
1846
+ clientId: this.clientId,
1847
+ autoReconnect,
1848
+ opsBehind,
1849
+ online: OnlineStatus[isOnline()],
1850
+ lastVisible:
1851
+ this.lastVisible !== undefined ? performance.now() - this.lastVisible : undefined,
1852
+ checkpointSequenceNumber,
1853
+ quorumSize: this._protocolHandler?.quorum.getMembers().size,
1854
+ ...this._deltaManager.connectionProps,
1855
+ });
1856
+
1857
+ if (value === ConnectionState.Connected) {
1858
+ this.firstConnection = false;
1859
+ }
1860
+ }
1861
+
1862
+ private propagateConnectionState(initialTransition: boolean, disconnectedReason?: string) {
1863
+ // When container loaded, we want to propagate initial connection state.
1864
+ // After that, we communicate only transitions to Connected & Disconnected states, skipping all other states.
1865
+ // This can be changed in the future, for example we likely should add "CatchingUp" event on Container.
1866
+ if (
1867
+ !initialTransition &&
1868
+ this.connectionState !== ConnectionState.Connected &&
1869
+ this.connectionState !== ConnectionState.Disconnected
1870
+ ) {
1871
+ return;
1872
+ }
1873
+ const state = this.connectionState === ConnectionState.Connected;
1874
+
1875
+ const logOpsOnReconnect: boolean =
1876
+ this.connectionState === ConnectionState.Connected &&
1877
+ !this.firstConnection &&
1878
+ this.connectionMode === "write";
1879
+ if (logOpsOnReconnect) {
1880
+ this.messageCountAfterDisconnection = 0;
1881
+ }
1882
+
1883
+ // Both protocol and context should not be undefined if we got so far.
1884
+
1885
+ this.setContextConnectedState(
1886
+ state,
1887
+ this._deltaManager.connectionManager.readOnlyInfo.readonly ?? false,
1888
+ );
1889
+ this.protocolHandler.setConnectionState(state, this.clientId);
1890
+ raiseConnectedEvent(this.mc.logger, this, state, this.clientId, disconnectedReason);
1891
+
1892
+ if (logOpsOnReconnect) {
1893
+ this.mc.logger.sendTelemetryEvent({
1894
+ eventName: "OpsSentOnReconnect",
1895
+ count: this.messageCountAfterDisconnection,
1896
+ });
1897
+ }
1898
+ }
1899
+
1900
+ // back-compat: ADO #1385: Remove in the future, summary op should come through submitSummaryMessage()
1901
+ private submitContainerMessage(
1902
+ type: MessageType,
1903
+ contents: any,
1904
+ batch?: boolean,
1905
+ metadata?: any,
1906
+ ): number {
1907
+ switch (type) {
1908
+ case MessageType.Operation:
1909
+ return this.submitMessage(type, JSON.stringify(contents), batch, metadata);
1910
+ case MessageType.Summarize:
1911
+ return this.submitSummaryMessage(contents as unknown as ISummaryContent);
1912
+ default: {
1913
+ const newError = new GenericError(
1914
+ "invalidContainerSubmitOpType",
1915
+ undefined /* error */,
1916
+ { messageType: type },
1917
+ );
1918
+ this.close(newError);
1919
+ this.dispose?.(newError);
1920
+ return -1;
1921
+ }
1922
+ }
1923
+ }
1924
+
1925
+ /** @returns clientSequenceNumber of last message in a batch */
1926
+ private submitBatch(batch: IBatchMessage[]): number {
1927
+ let clientSequenceNumber = -1;
1928
+ for (const message of batch) {
1929
+ clientSequenceNumber = this.submitMessage(
1930
+ MessageType.Operation,
1931
+ message.contents,
1932
+ true, // batch
1933
+ message.metadata,
1934
+ message.compression,
1935
+ );
1936
+ }
1937
+ this._deltaManager.flush();
1938
+ return clientSequenceNumber;
1939
+ }
1940
+
1941
+ private submitSummaryMessage(summary: ISummaryContent) {
1942
+ // github #6451: this is only needed for staging so the server
1943
+ // know when the protocol tree is included
1944
+ // this can be removed once all clients send
1945
+ // protocol tree by default
1946
+ if (summary.details === undefined) {
1947
+ summary.details = {};
1948
+ }
1949
+ summary.details.includesProtocolTree = this.options.summarizeProtocolTree === true;
1950
+ return this.submitMessage(
1951
+ MessageType.Summarize,
1952
+ JSON.stringify(summary),
1953
+ false /* batch */,
1954
+ );
1955
+ }
1956
+
1957
+ private submitMessage(
1958
+ type: MessageType,
1959
+ contents?: string,
1960
+ batch?: boolean,
1961
+ metadata?: any,
1962
+ compression?: string,
1963
+ ): number {
1964
+ if (this.connectionState !== ConnectionState.Connected) {
1965
+ this.mc.logger.sendErrorEvent({ eventName: "SubmitMessageWithNoConnection", type });
1966
+ return -1;
1967
+ }
1968
+
1969
+ this.messageCountAfterDisconnection += 1;
1970
+ this.collabWindowTracker?.stopSequenceNumberUpdate();
1971
+ return this._deltaManager.submit(type, contents, batch, metadata, compression);
1972
+ }
1973
+
1974
+ private processRemoteMessage(message: ISequencedDocumentMessage) {
1975
+ const local = this.clientId === message.clientId;
1976
+
1977
+ // Allow the protocol handler to process the message
1978
+ const result = this.protocolHandler.processMessage(message, local);
1979
+
1980
+ // Forward messages to the loaded runtime for processing
1981
+ this.context.process(message, local);
1982
+
1983
+ // Inactive (not in quorum or not writers) clients don't take part in the minimum sequence number calculation.
1984
+ if (this.activeConnection()) {
1985
+ if (this.collabWindowTracker === undefined) {
1986
+ // Note that config from first connection will be used for this container's lifetime.
1987
+ // That means that if relay service changes settings, such changes will impact only newly booted
1988
+ // clients.
1989
+ // All existing will continue to use settings they got earlier.
1990
+ assert(
1991
+ this.serviceConfiguration !== undefined,
1992
+ 0x2e4 /* "there should be service config for active connection" */,
1993
+ );
1994
+ this.collabWindowTracker = new CollabWindowTracker(
1995
+ (type) => {
1996
+ assert(
1997
+ this.activeConnection(),
1998
+ 0x241 /* "disconnect should result in stopSequenceNumberUpdate() call" */,
1999
+ );
2000
+ this.submitMessage(type);
2001
+ },
2002
+ this.serviceConfiguration.noopTimeFrequency,
2003
+ this.serviceConfiguration.noopCountFrequency,
2004
+ );
2005
+ }
2006
+ this.collabWindowTracker.scheduleSequenceNumberUpdate(
2007
+ message,
2008
+ result.immediateNoOp === true,
2009
+ );
2010
+ }
2011
+
2012
+ this.emit("op", message);
2013
+ }
2014
+
2015
+ private submitSignal(message: any) {
2016
+ this._deltaManager.submitSignal(JSON.stringify(message));
2017
+ }
2018
+
2019
+ private processSignal(message: ISignalMessage) {
2020
+ // No clientId indicates a system signal message.
2021
+ if (message.clientId === null) {
2022
+ this.protocolHandler.processSignal(message);
2023
+ } else {
2024
+ const local = this.clientId === message.clientId;
2025
+ this.context.processSignal(message, local);
2026
+ }
2027
+ }
2028
+
2029
+ /**
2030
+ * Get the most recent snapshot, or a specific version.
2031
+ * @param specifiedVersion - The specific version of the snapshot to retrieve
2032
+ * @returns The snapshot requested, or the latest snapshot if no version was specified, plus version ID
2033
+ */
2034
+ private async fetchSnapshotTree(
2035
+ specifiedVersion: string | undefined,
2036
+ ): Promise<{ snapshot?: ISnapshotTree; versionId?: string }> {
2037
+ const version = await this.getVersion(specifiedVersion ?? null);
2038
+
2039
+ if (version === undefined && specifiedVersion !== undefined) {
2040
+ // We should have a defined version to load from if specified version requested
2041
+ this.mc.logger.sendErrorEvent({
2042
+ eventName: "NoVersionFoundWhenSpecified",
2043
+ id: specifiedVersion,
2044
+ });
2045
+ }
2046
+ this._loadedFromVersion = version;
2047
+ const snapshot = (await this.storageService.getSnapshotTree(version)) ?? undefined;
2048
+
2049
+ if (snapshot === undefined && version !== undefined) {
2050
+ this.mc.logger.sendErrorEvent({ eventName: "getSnapshotTreeFailed", id: version.id });
2051
+ }
2052
+ return { snapshot, versionId: version?.id };
2053
+ }
2054
+
2055
+ private async instantiateContextDetached(existing: boolean, snapshot?: ISnapshotTree) {
2056
+ const codeDetails = this.getCodeDetailsFromQuorum();
2057
+ if (codeDetails === undefined) {
2058
+ throw new Error("pkg should be provided in create flow!!");
2059
+ }
2060
+
2061
+ await this.instantiateContext(existing, codeDetails, snapshot);
2062
+ }
2063
+
2064
+ private async instantiateContext(
2065
+ existing: boolean,
2066
+ codeDetails: IFluidCodeDetails,
2067
+ snapshot?: ISnapshotTree,
2068
+ pendingLocalState?: unknown,
2069
+ ) {
2070
+ assert(this._context?.disposed !== false, 0x0dd /* "Existing context not disposed" */);
2071
+
2072
+ // The relative loader will proxy requests to '/' to the loader itself assuming no non-cache flags
2073
+ // are set. Global requests will still go directly to the loader
2074
+ const loader = new RelativeLoader(this, this.loader);
2075
+ this._context = await ContainerContext.createOrLoad(
2076
+ this,
2077
+ this.scope,
2078
+ this.codeLoader,
2079
+ codeDetails,
2080
+ snapshot,
2081
+ new DeltaManagerProxy(this._deltaManager),
2082
+ new QuorumProxy(this.protocolHandler.quorum),
2083
+ loader,
2084
+ (type, contents, batch, metadata) =>
2085
+ this.submitContainerMessage(type, contents, batch, metadata),
2086
+ (summaryOp: ISummaryContent) => this.submitSummaryMessage(summaryOp),
2087
+ (batch: IBatchMessage[]) => this.submitBatch(batch),
2088
+ (message) => this.submitSignal(message),
2089
+ (error?: ICriticalContainerError) => this.dispose?.(error),
2090
+ (error?: ICriticalContainerError) => this.close(error),
2091
+ Container.version,
2092
+ (dirty: boolean) => this.updateDirtyContainerState(dirty),
2093
+ existing,
2094
+ pendingLocalState,
2095
+ );
2096
+
2097
+ this.emit("contextChanged", codeDetails);
2098
+ }
2099
+
2100
+ private updateDirtyContainerState(dirty: boolean) {
2101
+ if (this._dirtyContainer === dirty) {
2102
+ return;
2103
+ }
2104
+ this._dirtyContainer = dirty;
2105
+ this.emit(dirty ? dirtyContainerEvent : savedContainerEvent);
2106
+ }
2107
+
2108
+ /**
2109
+ * Set the connected state of the ContainerContext
2110
+ * This controls the "connected" state of the ContainerRuntime as well
2111
+ * @param state - Is the container currently connected?
2112
+ * @param readonly - Is the container in readonly mode?
2113
+ */
2114
+ private setContextConnectedState(state: boolean, readonly: boolean): void {
2115
+ if (this._context?.disposed === false) {
2116
+ /**
2117
+ * We want to lie to the ContainerRuntime when we are in readonly mode to prevent issues with pending
2118
+ * ops getting through to the DeltaManager.
2119
+ * The ContainerRuntime's "connected" state simply means it is ok to send ops
2120
+ * See https://dev.azure.com/fluidframework/internal/_workitems/edit/1246
2121
+ */
2122
+ this.context.setConnectionState(state && !readonly, this.clientId);
2123
+ }
2124
+ }
1870
2125
  }