@fluidframework/container-loader 2.0.0-internal.2.1.1 → 2.0.0-internal.2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/.eslintrc.js +1 -1
  2. package/README.md +21 -11
  3. package/dist/audience.d.ts.map +1 -1
  4. package/dist/audience.js +6 -1
  5. package/dist/audience.js.map +1 -1
  6. package/dist/collabWindowTracker.js +5 -4
  7. package/dist/collabWindowTracker.js.map +1 -1
  8. package/dist/connectionManager.d.ts.map +1 -1
  9. package/dist/connectionManager.js +13 -2
  10. package/dist/connectionManager.js.map +1 -1
  11. package/dist/connectionStateHandler.d.ts +20 -11
  12. package/dist/connectionStateHandler.d.ts.map +1 -1
  13. package/dist/connectionStateHandler.js +65 -36
  14. package/dist/connectionStateHandler.js.map +1 -1
  15. package/dist/container.d.ts +8 -0
  16. package/dist/container.d.ts.map +1 -1
  17. package/dist/container.js +23 -7
  18. package/dist/container.js.map +1 -1
  19. package/dist/containerContext.d.ts.map +1 -1
  20. package/dist/containerContext.js +5 -1
  21. package/dist/containerContext.js.map +1 -1
  22. package/dist/contracts.d.ts +8 -0
  23. package/dist/contracts.d.ts.map +1 -1
  24. package/dist/contracts.js.map +1 -1
  25. package/dist/deltaManager.d.ts +1 -1
  26. package/dist/deltaManager.d.ts.map +1 -1
  27. package/dist/deltaManager.js +4 -3
  28. package/dist/deltaManager.js.map +1 -1
  29. package/dist/packageVersion.d.ts +1 -1
  30. package/dist/packageVersion.js +1 -1
  31. package/dist/packageVersion.js.map +1 -1
  32. package/lib/audience.d.ts.map +1 -1
  33. package/lib/audience.js +6 -1
  34. package/lib/audience.js.map +1 -1
  35. package/lib/collabWindowTracker.js +5 -4
  36. package/lib/collabWindowTracker.js.map +1 -1
  37. package/lib/connectionManager.d.ts.map +1 -1
  38. package/lib/connectionManager.js +13 -2
  39. package/lib/connectionManager.js.map +1 -1
  40. package/lib/connectionStateHandler.d.ts +20 -11
  41. package/lib/connectionStateHandler.d.ts.map +1 -1
  42. package/lib/connectionStateHandler.js +65 -36
  43. package/lib/connectionStateHandler.js.map +1 -1
  44. package/lib/container.d.ts +8 -0
  45. package/lib/container.d.ts.map +1 -1
  46. package/lib/container.js +22 -7
  47. package/lib/container.js.map +1 -1
  48. package/lib/containerContext.d.ts.map +1 -1
  49. package/lib/containerContext.js +5 -1
  50. package/lib/containerContext.js.map +1 -1
  51. package/lib/contracts.d.ts +8 -0
  52. package/lib/contracts.d.ts.map +1 -1
  53. package/lib/contracts.js.map +1 -1
  54. package/lib/deltaManager.d.ts +1 -1
  55. package/lib/deltaManager.d.ts.map +1 -1
  56. package/lib/deltaManager.js +4 -3
  57. package/lib/deltaManager.js.map +1 -1
  58. package/lib/packageVersion.d.ts +1 -1
  59. package/lib/packageVersion.js +1 -1
  60. package/lib/packageVersion.js.map +1 -1
  61. package/package.json +22 -15
  62. package/prettier.config.cjs +8 -0
  63. package/src/audience.ts +6 -1
  64. package/src/collabWindowTracker.ts +5 -5
  65. package/src/connectionManager.ts +15 -3
  66. package/src/connectionStateHandler.ts +87 -39
  67. package/src/container.ts +26 -6
  68. package/src/containerContext.ts +8 -2
  69. package/src/contracts.ts +8 -0
  70. package/src/deltaManager.ts +4 -3
  71. package/src/packageVersion.ts +1 -1
@@ -6,16 +6,23 @@ import { assert, Timer } from "@fluidframework/common-utils";
6
6
  import { PerformanceEvent, loggerToMonitoringContext } from "@fluidframework/telemetry-utils";
7
7
  import { ConnectionState } from "./connectionState";
8
8
  import { CatchUpMonitor } from "./catchUpMonitor";
9
+ // Based on recent data, it looks like majority of cases where we get stuck are due to really slow or
10
+ // timing out ops fetches. So attempt recovery infrequently. Also fetch uses 30 second timeout, so
11
+ // if retrying fixes the problem, we should not see these events.
9
12
  const JoinOpTimeoutMs = 45000;
13
+ // Timeout waiting for "self" join signal, before giving up
14
+ const JoinSignalTimeoutMs = 5000;
10
15
  export function createConnectionStateHandler(inputs, deltaManager, clientId) {
11
16
  const mc = loggerToMonitoringContext(inputs.logger);
12
- return createConnectionStateHandlerCore(mc.config.getBoolean("Fluid.Container.CatchUpBeforeDeclaringConnected") === true, inputs, deltaManager, clientId);
17
+ return createConnectionStateHandlerCore(mc.config.getBoolean("Fluid.Container.CatchUpBeforeDeclaringConnected") === true, // connectedRaisedWhenCaughtUp
18
+ mc.config.getBoolean("Fluid.Container.DisableJoinSignalWait") !== true, // readClientsWaitForJoinSignal
19
+ inputs, deltaManager, clientId);
13
20
  }
14
- export function createConnectionStateHandlerCore(wait, inputs, deltaManager, clientId) {
15
- if (!wait) {
16
- return new ConnectionStateHandler(inputs, clientId);
21
+ export function createConnectionStateHandlerCore(connectedRaisedWhenCaughtUp, readClientsWaitForJoinSignal, inputs, deltaManager, clientId) {
22
+ if (!connectedRaisedWhenCaughtUp) {
23
+ return new ConnectionStateHandler(inputs, readClientsWaitForJoinSignal, clientId);
17
24
  }
18
- return new ConnectionStateCatchup(inputs, (handler) => new ConnectionStateHandler(handler, clientId), deltaManager);
25
+ return new ConnectionStateCatchup(inputs, (handler) => new ConnectionStateHandler(handler, readClientsWaitForJoinSignal, clientId), deltaManager);
19
26
  }
20
27
  /**
21
28
  * Class that can be used as a base class for building IConnectionStateHandler adapters / pipeline.
@@ -35,8 +42,8 @@ class ConnectionStateHandlerPassThrough {
35
42
  dispose() { return this.pimpl.dispose(); }
36
43
  initProtocol(protocol) { return this.pimpl.initProtocol(protocol); }
37
44
  receivedDisconnectEvent(reason) { return this.pimpl.receivedDisconnectEvent(reason); }
38
- receivedConnectEvent(connectionMode, details) {
39
- return this.pimpl.receivedConnectEvent(connectionMode, details);
45
+ receivedConnectEvent(details) {
46
+ return this.pimpl.receivedConnectEvent(details);
40
47
  }
41
48
  /**
42
49
  * IConnectionStateHandlerInputs
@@ -123,17 +130,18 @@ class ConnectionStateCatchup extends ConnectionStateHandlerPassThrough {
123
130
  *
124
131
  * For (a) we give up waiting after some time (same timeout as server uses), and go ahead and transition to Connected.
125
132
  *
126
- * For (b) we log telemetry if it takes too long, but still only transition to Connected when the Join op is processed
127
- * and we are added to the Quorum.
133
+ * For (b) we log telemetry if it takes too long, but still only transition to Connected when the Join op/signal is
134
+ * processed.
128
135
  *
129
136
  * For (c) this is optional behavior, controlled by the parameters of receivedConnectEvent
130
137
  */
131
138
  class ConnectionStateHandler {
132
- constructor(handler, _clientId) {
139
+ constructor(handler, readClientsWaitForJoinSignal, clientIdFromPausedSession) {
133
140
  var _a;
134
141
  this.handler = handler;
135
- this._clientId = _clientId;
142
+ this.readClientsWaitForJoinSignal = readClientsWaitForJoinSignal;
136
143
  this._connectionState = ConnectionState.Disconnected;
144
+ this._clientId = clientIdFromPausedSession;
137
145
  this.prevClientLeftTimer = new Timer(
138
146
  // Default is 5 min for which we are going to wait for its own "leave" message. This is same as
139
147
  // the max time on server after which leave op is sent.
@@ -141,10 +149,8 @@ class ConnectionStateHandler {
141
149
  assert(this.connectionState !== ConnectionState.Connected, 0x2ac /* "Connected when timeout waiting for leave from previous session fired!" */);
142
150
  this.applyForConnectedState("timeout");
143
151
  });
144
- // Based on recent data, it looks like majority of cases where we get stuck are due to really slow or
145
- // timing out ops fetches. So attempt recovery infrequently. Also fetch uses 30 second timeout, so
146
- // if retrying fixes the problem, we should not see these events.
147
- this.joinOpTimer = new Timer(JoinOpTimeoutMs, () => {
152
+ this.joinOpTimer = new Timer(0, // default value is not used - startJoinOpTimer() explicitly provides timeout
153
+ () => {
148
154
  // I've observed timer firing within couple ms from disconnect event, looks like
149
155
  // queued timer callback is not cancelled if timer is cancelled while callback sits in the queue.
150
156
  if (this.connectionState !== ConnectionState.CatchingUp) {
@@ -170,7 +176,8 @@ class ConnectionStateHandler {
170
176
  }
171
177
  startJoinOpTimer() {
172
178
  assert(!this.joinOpTimer.hasTimer, 0x234 /* "has joinOpTimer" */);
173
- this.joinOpTimer.start();
179
+ assert(this.connection !== undefined, 0x4b3 /* have connection */);
180
+ this.joinOpTimer.start(this.connection.mode === "write" ? JoinOpTimeoutMs : JoinSignalTimeoutMs);
174
181
  }
175
182
  stopJoinOpTimer() {
176
183
  assert(this.joinOpTimer.hasTimer, 0x235 /* "no joinOpTimer" */);
@@ -197,8 +204,8 @@ class ConnectionStateHandler {
197
204
  if (this.joinOpTimer.hasTimer) {
198
205
  this.stopJoinOpTimer();
199
206
  }
200
- else {
201
- // timer has already fired, meaning it took too long to get join on.
207
+ else if (this.shouldWaitForJoinSignal()) {
208
+ // timer has already fired, meaning it took too long to get join op/signal.
202
209
  // Record how long it actually took to recover.
203
210
  this.handler.logConnectionIssue("ReceivedJoinOp");
204
211
  }
@@ -230,9 +237,11 @@ class ConnectionStateHandler {
230
237
  }
231
238
  else {
232
239
  // Adding this event temporarily so that we can get help debugging if something goes wrong.
240
+ // We may not see any ops due to being disconnected all that time - that's not an error!
241
+ const error = source === "timeout" && this.connectionState !== ConnectionState.Disconnected;
233
242
  this.handler.logger.sendTelemetryEvent({
234
243
  eventName: "connectedStateRejected",
235
- category: source === "timeout" ? "error" : "generic",
244
+ category: error ? "error" : "generic",
236
245
  details: JSON.stringify({
237
246
  source,
238
247
  pendingClientId: this.pendingClientId,
@@ -251,23 +260,29 @@ class ConnectionStateHandler {
251
260
  }
252
261
  }
253
262
  receivedDisconnectEvent(reason) {
263
+ this.connection = undefined;
254
264
  this.setConnectionState(ConnectionState.Disconnected, reason);
255
265
  }
266
+ shouldWaitForJoinSignal() {
267
+ assert(this.connection !== undefined, 0x4b4 /* all callers call here with active connection */);
268
+ return this.connection.mode === "write" || this.readClientsWaitForJoinSignal;
269
+ }
256
270
  /**
257
271
  * The "connect" event indicates the connection to the Relay Service is live.
258
272
  * However, some additional conditions must be met before we can fully transition to
259
273
  * "Connected" state. This function handles that interim period, known as "Connecting" state.
260
- * @param connectionMode - Read or Write connection
261
274
  * @param details - Connection details returned from the Relay Service
262
275
  * @param deltaManager - DeltaManager to be used for delaying Connected transition until caught up.
263
276
  * If it's undefined, then don't delay and transition to Connected as soon as Leave/Join op are accounted for
264
277
  */
265
- receivedConnectEvent(connectionMode, details) {
278
+ receivedConnectEvent(details) {
279
+ this.connection = details;
266
280
  const oldState = this._connectionState;
267
281
  this._connectionState = ConnectionState.CatchingUp;
268
- const writeConnection = connectionMode === "write";
269
282
  // The following checks are wrong. They are only valid if user has write access to a file.
270
283
  // If user lost such access mid-session, user will not be able to get "write" connection.
284
+ //
285
+ // const writeConnection = details.mode === "write";
271
286
  // assert(!this.handler.shouldClientJoinWrite() || writeConnection,
272
287
  // 0x30a /* shouldClientJoinWrite should imply this is a writeConnection */);
273
288
  // assert(!this.waitingForLeaveOp || writeConnection,
@@ -281,15 +296,14 @@ class ConnectionStateHandler {
281
296
  this._pendingClientId = details.clientId;
282
297
  // IMPORTANT: Report telemetry after we set _pendingClientId, but before transitioning to Connected state
283
298
  this.handler.connectionStateChanged(ConnectionState.CatchingUp, oldState);
284
- // For write connections, this pending clientId could be in the quorum already (i.e. join op already processed).
285
- // We are fetching ops from storage in parallel to connecting to Relay Service,
286
- // and given async processes, it's possible that we have already processed our own join message before
287
- // connection was fully established.
288
- // If protocol is not initialized yet, we expect it will process the join op after it's initialized.
289
- const waitingForJoinOp = writeConnection && !this.hasMember(this._pendingClientId);
290
- if (waitingForJoinOp) {
291
- // Previous client left, and we are waiting for our own join op. When it is processed we'll join the quorum
292
- // and attempt to transition to Connected state via receivedAddMemberEvent.
299
+ // Check if we need to wait for join op/signal, and if we need to wait for leave op from previous connection.
300
+ // Pending clientId could have joined already (i.e. join op/signal already processed):
301
+ // We are fetching ops from storage in parallel to connecting to Relay Service,
302
+ // and given async processes, it's possible that we have already processed our own join message before
303
+ // connection was fully established.
304
+ if (!this.hasMember(this._pendingClientId) && this.shouldWaitForJoinSignal()) {
305
+ // We are waiting for our own join op / signal. When it is processed
306
+ // we'll attempt to transition to Connected state via receivedAddMemberEvent() flow.
293
307
  this.startJoinOpTimer();
294
308
  }
295
309
  else if (!this.waitingForLeaveOp) {
@@ -309,6 +323,9 @@ class ConnectionStateHandler {
309
323
  }
310
324
  const oldState = this._connectionState;
311
325
  this._connectionState = value;
326
+ // This is the only place in code that deals with quorum. The rest works with audience
327
+ // The code below ensures that we do not send ops until we know that old "write" client's disconnect
328
+ // produced (and sequenced) leave op
312
329
  let client;
313
330
  if (this._clientId !== undefined) {
314
331
  client = (_b = (_a = this.protocol) === null || _a === void 0 ? void 0 : _a.quorum) === null || _b === void 0 ? void 0 : _b.getMember(this._clientId);
@@ -357,20 +374,32 @@ class ConnectionStateHandler {
357
374
  // Old design was checking only quorum for "write" clients.
358
375
  // Latest change checks audience for all types of connections.
359
376
  get membership() {
360
- var _a;
361
- return (_a = this.protocol) === null || _a === void 0 ? void 0 : _a.quorum;
377
+ var _a, _b;
378
+ // We could always use audience here, and in practice it will probably be correct.
379
+ // (including case when this.readClientsWaitForJoinSignal === false).
380
+ // But only if it's superset of quorum, i.e. when filtered to "write" clients, they are always identical!
381
+ // It's safer to assume that we have bugs and engaging kill-bit switch should bring us back to well-known
382
+ // and tested state!
383
+ return this.readClientsWaitForJoinSignal ? (_a = this.protocol) === null || _a === void 0 ? void 0 : _a.audience : (_b = this.protocol) === null || _b === void 0 ? void 0 : _b.quorum;
362
384
  }
363
385
  initProtocol(protocol) {
364
386
  var _a, _b;
365
387
  this.protocol = protocol;
366
- (_a = this.membership) === null || _a === void 0 ? void 0 : _a.on("addMember", (clientId) => {
388
+ (_a = this.membership) === null || _a === void 0 ? void 0 : _a.on("addMember", (clientId, details) => {
389
+ assert(details.mode === "read" || protocol.quorum.getMember(clientId) !== undefined, 0x4b5 /* Audience is subset of quorum */);
367
390
  this.receivedAddMemberEvent(clientId);
368
391
  });
369
392
  (_b = this.membership) === null || _b === void 0 ? void 0 : _b.on("removeMember", (clientId) => {
393
+ assert(protocol.quorum.getMember(clientId) === undefined, 0x4b6 /* Audience is subset of quorum */);
370
394
  this.receivedRemoveMemberEvent(clientId);
371
395
  });
372
- // Very unlikely race condition, but theoretically can happen - our new connection is already
373
- // summarized and we are loading from such summary.
396
+ /* There is a tiny tiny race possible, where these events happen in this order:
397
+ 1. A connection is established (no "cached" mode is used, so it happens in parallel / faster than other steps)
398
+ 2. Some other client produces a summary
399
+ 3. We get "lucky" and load from that summary as our initial snapshot
400
+ 4. ConnectionStateHandler.initProtocol is called, "self" is already in the quorum.
401
+ We could avoid this sequence (and delete test case for it) if we move connection lower in Container.load()
402
+ */
374
403
  if (this.hasMember(this.pendingClientId)) {
375
404
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
376
405
  this.receivedAddMemberEvent(this.pendingClientId);
@@ -1 +1 @@
1
- {"version":3,"file":"connectionStateHandler.js","sourceRoot":"","sources":["../src/connectionStateHandler.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,8BAA8B,CAAC;AAI7D,OAAO,EAAE,gBAAgB,EAAE,yBAAyB,EAAE,MAAM,iCAAiC,CAAC;AAC9F,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAmB,MAAM,kBAAkB,CAAC;AAGnE,MAAM,eAAe,GAAG,KAAK,CAAC;AA8B9B,MAAM,UAAU,4BAA4B,CACxC,MAAqC,EACrC,YAAqC,EACrC,QAAiB;IAEjB,MAAM,EAAE,GAAG,yBAAyB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACpD,OAAO,gCAAgC,CACnC,EAAE,CAAC,MAAM,CAAC,UAAU,CAAC,iDAAiD,CAAC,KAAK,IAAI,EAChF,MAAM,EACN,YAAY,EACZ,QAAQ,CACX,CAAC;AACN,CAAC;AAED,MAAM,UAAU,gCAAgC,CAC5C,IAAa,EACb,MAAqC,EACrC,YAAqC,EACrC,QAAiB;IAEjB,IAAI,CAAC,IAAI,EAAE;QACP,OAAO,IAAI,sBAAsB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;KACvD;IACD,OAAO,IAAI,sBAAsB,CAC7B,MAAM,EACN,CAAC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,SAAS,IAAI,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE;YAC9D,IAAI,CAAC,mBAAmB,CAAC,OAAO,EAAE,CAAC;SACtC;IACL,CAAC;IAES,SAAS,CAAC,QAAiB;;QACjC,OAAO,CAAA,MAAA,IAAI,CAAC,UAAU,0CAAE,SAAS,CAAC,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,EAAE,CAAC,MAAK,SAAS,CAAC;IACpE,CAAC;CACJ","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { ITelemetryLogger, ITelemetryProperties } from \"@fluidframework/common-definitions\";\nimport { assert, Timer } from \"@fluidframework/common-utils\";\nimport { IConnectionDetails, IDeltaManager } from \"@fluidframework/container-definitions\";\nimport { ILocalSequencedClient } from \"@fluidframework/protocol-base\";\nimport { ConnectionMode } from \"@fluidframework/protocol-definitions\";\nimport { PerformanceEvent, loggerToMonitoringContext } from \"@fluidframework/telemetry-utils\";\nimport { ConnectionState } from \"./connectionState\";\nimport { CatchUpMonitor, ICatchUpMonitor } from \"./catchUpMonitor\";\nimport { IProtocolHandler } from \"./protocol\";\n\nconst JoinOpTimeoutMs = 45000;\n\n/** Constructor parameter type for passing in dependencies needed by the ConnectionStateHandler */\nexport interface IConnectionStateHandlerInputs {\n logger: ITelemetryLogger;\n /** Log to telemetry any change in state, included to Connecting */\n connectionStateChanged:\n (value: ConnectionState, oldState: ConnectionState, reason?: string | undefined) => void;\n /** Whether to expect the client to join in write mode on next connection */\n shouldClientJoinWrite: () => boolean;\n /** (Optional) How long should we wait on our previous client's Leave op before transitioning to Connected again */\n maxClientLeaveWaitTime: number | undefined;\n /** Log an issue encountered while in the Connecting state. details will be logged as a JSON string */\n logConnectionIssue: (eventName: string, details?: ITelemetryProperties) => void;\n}\n\n/**\n * interface that connection state handler implements\n */\nexport interface IConnectionStateHandler {\n readonly connectionState: ConnectionState;\n readonly pendingClientId: string | undefined;\n\n containerSaved(): void;\n dispose(): void;\n initProtocol(protocol: IProtocolHandler): void;\n receivedConnectEvent(connectionMode: ConnectionMode, details: IConnectionDetails): void;\n receivedDisconnectEvent(reason: string): void;\n}\n\nexport function createConnectionStateHandler(\n inputs: IConnectionStateHandlerInputs,\n deltaManager: IDeltaManager<any, any>,\n clientId?: string,\n) {\n const mc = loggerToMonitoringContext(inputs.logger);\n return createConnectionStateHandlerCore(\n mc.config.getBoolean(\"Fluid.Container.CatchUpBeforeDeclaringConnected\") === true,\n inputs,\n deltaManager,\n clientId,\n );\n}\n\nexport function createConnectionStateHandlerCore(\n wait: boolean,\n inputs: IConnectionStateHandlerInputs,\n deltaManager: IDeltaManager<any, any>,\n clientId?: string,\n) {\n if (!wait) {\n return new ConnectionStateHandler(inputs, clientId);\n }\n return new ConnectionStateCatchup(\n inputs,\n (handler: IConnectionStateHandlerInputs) => new ConnectionStateHandler(handler, clientId),\n deltaManager);\n}\n\n/**\n * Class that can be used as a base class for building IConnectionStateHandler adapters / pipeline.\n * It implements both ends of communication interfaces and passes data back and forward\n */\nclass ConnectionStateHandlerPassThrough implements IConnectionStateHandler, IConnectionStateHandlerInputs {\n protected readonly pimpl: IConnectionStateHandler;\n\n constructor(\n protected readonly inputs: IConnectionStateHandlerInputs,\n pimplFactory: (handler: IConnectionStateHandlerInputs) => IConnectionStateHandler,\n ) {\n this.pimpl = pimplFactory(this);\n }\n\n /**\n * IConnectionStateHandler\n */\n public get connectionState() { return this.pimpl.connectionState; }\n public get pendingClientId() { return this.pimpl.pendingClientId; }\n\n public containerSaved() { return this.pimpl.containerSaved(); }\n public dispose() { return this.pimpl.dispose(); }\n public initProtocol(protocol: IProtocolHandler) { return this.pimpl.initProtocol(protocol); }\n public receivedDisconnectEvent(reason: string) { return this.pimpl.receivedDisconnectEvent(reason); }\n\n public receivedConnectEvent(connectionMode: ConnectionMode, details: IConnectionDetails) {\n return this.pimpl.receivedConnectEvent(connectionMode, details);\n }\n\n /**\n * IConnectionStateHandlerInputs\n */\n\n public get logger() { return this.inputs.logger; }\n public connectionStateChanged(\n value: ConnectionState,\n oldState: ConnectionState,\n reason?: string | undefined,\n ) {\n return this.inputs.connectionStateChanged(value, oldState, reason);\n }\n public shouldClientJoinWrite() { return this.inputs.shouldClientJoinWrite(); }\n public get maxClientLeaveWaitTime() { return this.inputs.maxClientLeaveWaitTime; }\n public logConnectionIssue(eventName: string, details?: ITelemetryProperties) {\n return this.inputs.logConnectionIssue(eventName, details);\n }\n}\n\n/**\n * Implementation of IConnectionStateHandler pass-through adapter that waits for specific sequence number\n * before raising connected event\n */\nclass ConnectionStateCatchup extends ConnectionStateHandlerPassThrough {\n private catchUpMonitor: ICatchUpMonitor | undefined;\n\n constructor(\n inputs: IConnectionStateHandlerInputs,\n pimplFactory: (handler: IConnectionStateHandlerInputs) => IConnectionStateHandler,\n private readonly deltaManager: IDeltaManager<any, any>,\n ) {\n super(inputs, pimplFactory);\n this._connectionState = this.pimpl.connectionState;\n }\n\n private _connectionState: ConnectionState;\n public get connectionState() {\n return this._connectionState;\n }\n\n public connectionStateChanged(value: ConnectionState, oldState: ConnectionState, reason?: string | undefined) {\n switch (value) {\n case ConnectionState.Connected:\n assert(this._connectionState === ConnectionState.CatchingUp, 0x3e1 /* connectivity transitions */);\n // Create catch-up monitor here (not earlier), as we might get more exact info by now about how far\n // client is behind through join signal. This is only true if base layer uses signals (i.e. audience,\n // not quorum, including for \"rea\" connections) to make decisions about moving to \"connected\" state.\n // In addition to that, in its current form, doing this in ConnectionState.CatchingUp is dangerous as\n // we might get callback right away, and it will screw up state transition (as code outside of switch\n // statement will overwrite current state).\n assert(this.catchUpMonitor === undefined, 0x3eb /* catchUpMonitor should be gone */);\n this.catchUpMonitor = new CatchUpMonitor(this.deltaManager, this.transitionToConnectedState);\n return;\n case ConnectionState.Disconnected:\n this.catchUpMonitor?.dispose();\n this.catchUpMonitor = undefined;\n break;\n case ConnectionState.CatchingUp:\n assert(this._connectionState === ConnectionState.Disconnected, 0x3e3 /* connectivity transitions */);\n break;\n default:\n }\n this._connectionState = value;\n this.inputs.connectionStateChanged(value, oldState, reason);\n }\n\n private readonly transitionToConnectedState = () => {\n // Defensive measure, we should always be in Connecting state when this is called.\n const state = this.pimpl.connectionState;\n assert(state === ConnectionState.Connected, 0x3e5 /* invariant broken */);\n assert(this._connectionState === ConnectionState.CatchingUp, 0x3e6 /* invariant broken */);\n this._connectionState = ConnectionState.Connected;\n this.inputs.connectionStateChanged(ConnectionState.Connected, ConnectionState.CatchingUp, \"caught up\");\n };\n}\n\n/**\n * In the lifetime of a container, the connection will likely disconnect and reconnect periodically.\n * This class ensures that any ops sent by this container instance on previous connection are either\n * sequenced or blocked by the server before emitting the new \"connected\" event and allowing runtime to resubmit ops.\n *\n * Each connection is assigned a clientId by the service, and the connection is book-ended by a Join and a Leave op\n * generated by the service. Due to the distributed nature of the Relay Service, in the case of reconnect we cannot\n * make any assumptions about ordering of operations between the old and new connections - i.e. new Join op could\n * be sequenced before old Leave op (and some acks from pending ops that were in flight when we disconnected).\n *\n * The job of this class is to encapsulate the transition period during reconnect, which is identified by\n * ConnectionState.CatchingUp. Specifically, before moving to Connected state with the new clientId, it ensures that:\n *\n * a. We process the Leave op for the previous clientId. This allows us to properly handle any acks from in-flight ops\n * that got sequenced with the old clientId (we'll recognize them as local ops). After the Leave op, any other\n * pending ops can safely be submitted with the new clientId without fear of duplication in the sequenced op stream.\n *\n * b. We process the Join op for the new clientId (identified when the underlying connection was first established),\n * indicating the service is ready to sequence ops sent with the new clientId.\n *\n * c. We process all ops known at the time the underlying connection was established (so we are \"caught up\")\n *\n * For (a) we give up waiting after some time (same timeout as server uses), and go ahead and transition to Connected.\n *\n * For (b) we log telemetry if it takes too long, but still only transition to Connected when the Join op is processed\n * and we are added to the Quorum.\n *\n * For (c) this is optional behavior, controlled by the parameters of receivedConnectEvent\n */\nclass ConnectionStateHandler implements IConnectionStateHandler {\n private _connectionState = ConnectionState.Disconnected;\n private _pendingClientId: string | undefined;\n private readonly prevClientLeftTimer: Timer;\n private readonly joinOpTimer: Timer;\n private protocol?: IProtocolHandler;\n\n private waitEvent: PerformanceEvent | undefined;\n\n public get connectionState(): ConnectionState {\n return this._connectionState;\n }\n\n private get clientId(): string | undefined {\n return this._clientId;\n }\n\n public get pendingClientId(): string | undefined {\n return this._pendingClientId;\n }\n\n constructor(\n private readonly handler: IConnectionStateHandlerInputs,\n private _clientId?: string,\n ) {\n this.prevClientLeftTimer = new Timer(\n // Default is 5 min for which we are going to wait for its own \"leave\" message. This is same as\n // the max time on server after which leave op is sent.\n this.handler.maxClientLeaveWaitTime ?? 300000,\n () => {\n assert(this.connectionState !== ConnectionState.Connected,\n 0x2ac /* \"Connected when timeout waiting for leave from previous session fired!\" */);\n this.applyForConnectedState(\"timeout\");\n },\n );\n\n // Based on recent data, it looks like majority of cases where we get stuck are due to really slow or\n // timing out ops fetches. So attempt recovery infrequently. Also fetch uses 30 second timeout, so\n // if retrying fixes the problem, we should not see these events.\n this.joinOpTimer = new Timer(\n JoinOpTimeoutMs,\n () => {\n // I've observed timer firing within couple ms from disconnect event, looks like\n // queued timer callback is not cancelled if timer is cancelled while callback sits in the queue.\n if (this.connectionState !== ConnectionState.CatchingUp) {\n return;\n }\n const details = {\n protocolInitialized: this.protocol !== undefined,\n pendingClientId: this.pendingClientId,\n clientJoined: this.hasMember(this.pendingClientId),\n waitingForLeaveOp: this.waitingForLeaveOp,\n };\n this.handler.logConnectionIssue(\"NoJoinOp\", details);\n },\n );\n }\n\n private startJoinOpTimer() {\n assert(!this.joinOpTimer.hasTimer, 0x234 /* \"has joinOpTimer\" */);\n this.joinOpTimer.start();\n }\n\n private stopJoinOpTimer() {\n assert(this.joinOpTimer.hasTimer, 0x235 /* \"no joinOpTimer\" */);\n this.joinOpTimer.clear();\n }\n\n private get waitingForLeaveOp() {\n return this.prevClientLeftTimer.hasTimer;\n }\n\n public dispose() {\n assert(!this.joinOpTimer.hasTimer, 0x2a5 /* \"join timer\" */);\n this.prevClientLeftTimer.clear();\n }\n\n public containerSaved() {\n // If we were waiting for moving to Connected state, then only apply for state change. Since the container\n // is now saved and we don't have any ops to roundtrip, we can clear the timer and apply for connected state.\n if (this.waitingForLeaveOp) {\n this.prevClientLeftTimer.clear();\n this.applyForConnectedState(\"containerSaved\");\n }\n }\n\n private receivedAddMemberEvent(clientId: string) {\n // This is the only one that requires the pending client ID\n if (clientId === this.pendingClientId) {\n if (this.joinOpTimer.hasTimer) {\n this.stopJoinOpTimer();\n } else {\n // timer has already fired, meaning it took too long to get join on.\n // Record how long it actually took to recover.\n this.handler.logConnectionIssue(\"ReceivedJoinOp\");\n }\n // Start the event in case we are waiting for leave or timeout.\n if (this.waitingForLeaveOp) {\n this.waitEvent = PerformanceEvent.start(this.handler.logger, {\n eventName: \"WaitBeforeClientLeave\",\n details: JSON.stringify({\n waitOnClientId: this._clientId,\n hadOutstandingOps: this.handler.shouldClientJoinWrite(),\n }),\n });\n }\n this.applyForConnectedState(\"addMemberEvent\");\n }\n }\n\n private applyForConnectedState(source: \"removeMemberEvent\" | \"addMemberEvent\" | \"timeout\" | \"containerSaved\") {\n assert(this.protocol !== undefined, 0x236 /* \"In all cases it should be already installed\" */);\n\n assert(!this.waitingForLeaveOp || this.hasMember(this.clientId),\n 0x2e2 /* \"Must only wait for leave message when clientId in quorum\" */);\n\n // Move to connected state only if we are in Connecting state, we have seen our join op\n // and there is no timer running which means we are not waiting for previous client to leave\n // or timeout has occurred while doing so.\n if (this.pendingClientId !== this.clientId\n && this.hasMember(this.pendingClientId)\n && !this.waitingForLeaveOp\n ) {\n this.waitEvent?.end({ source });\n this.setConnectionState(ConnectionState.Connected);\n } else {\n // Adding this event temporarily so that we can get help debugging if something goes wrong.\n this.handler.logger.sendTelemetryEvent({\n eventName: \"connectedStateRejected\",\n category: source === \"timeout\" ? \"error\" : \"generic\",\n details: JSON.stringify({\n source,\n pendingClientId: this.pendingClientId,\n clientId: this.clientId,\n waitingForLeaveOp: this.waitingForLeaveOp,\n clientJoined: this.hasMember(this.pendingClientId),\n }),\n });\n }\n }\n\n private receivedRemoveMemberEvent(clientId: string) {\n // If the client which has left was us, then finish the timer.\n if (this.clientId === clientId) {\n this.prevClientLeftTimer.clear();\n this.applyForConnectedState(\"removeMemberEvent\");\n }\n }\n\n public receivedDisconnectEvent(reason: string) {\n this.setConnectionState(ConnectionState.Disconnected, reason);\n }\n\n /**\n * The \"connect\" event indicates the connection to the Relay Service is live.\n * However, some additional conditions must be met before we can fully transition to\n * \"Connected\" state. This function handles that interim period, known as \"Connecting\" state.\n * @param connectionMode - Read or Write connection\n * @param details - Connection details returned from the Relay Service\n * @param deltaManager - DeltaManager to be used for delaying Connected transition until caught up.\n * If it's undefined, then don't delay and transition to Connected as soon as Leave/Join op are accounted for\n */\n public receivedConnectEvent(\n connectionMode: ConnectionMode,\n details: IConnectionDetails,\n ) {\n const oldState = this._connectionState;\n this._connectionState = ConnectionState.CatchingUp;\n\n const writeConnection = connectionMode === \"write\";\n\n // The following checks are wrong. They are only valid if user has write access to a file.\n // If user lost such access mid-session, user will not be able to get \"write\" connection.\n // assert(!this.handler.shouldClientJoinWrite() || writeConnection,\n // 0x30a /* shouldClientJoinWrite should imply this is a writeConnection */);\n // assert(!this.waitingForLeaveOp || writeConnection,\n // 0x2a6 /* \"waitingForLeaveOp should imply writeConnection (we need to be ready to flush pending ops)\" */);\n\n // Stash the clientID to detect when transitioning from connecting (socket.io channel open) to connected\n // (have received the join message for the client ID)\n // This is especially important in the reconnect case. It's possible there could be outstanding\n // ops sent by this client, so we should keep the old client id until we see our own client's\n // join message. after we see the join message for our new connection with our new client id,\n // we know there can no longer be outstanding ops that we sent with the previous client id.\n this._pendingClientId = details.clientId;\n\n // IMPORTANT: Report telemetry after we set _pendingClientId, but before transitioning to Connected state\n this.handler.connectionStateChanged(ConnectionState.CatchingUp, oldState);\n\n // For write connections, this pending clientId could be in the quorum already (i.e. join op already processed).\n // We are fetching ops from storage in parallel to connecting to Relay Service,\n // and given async processes, it's possible that we have already processed our own join message before\n // connection was fully established.\n // If protocol is not initialized yet, we expect it will process the join op after it's initialized.\n const waitingForJoinOp = writeConnection && !this.hasMember(this._pendingClientId);\n\n if (waitingForJoinOp) {\n // Previous client left, and we are waiting for our own join op. When it is processed we'll join the quorum\n // and attempt to transition to Connected state via receivedAddMemberEvent.\n this.startJoinOpTimer();\n } else if (!this.waitingForLeaveOp) {\n // We're not waiting for Join or Leave op (if read-only connection those don't even apply),\n // go ahead and declare the state to be Connected!\n // If we are waiting for Leave op still, do nothing for now, we will transition to Connected later.\n this.setConnectionState(ConnectionState.Connected);\n }\n // else - We are waiting for Leave op still, do nothing for now, we will transition to Connected later\n }\n\n private setConnectionState(value: ConnectionState.Disconnected, reason: string): void;\n private setConnectionState(value: ConnectionState.Connected): void;\n private setConnectionState(value: ConnectionState.Disconnected | ConnectionState.Connected, reason?: string): void {\n if (this.connectionState === value) {\n // Already in the desired state - exit early\n this.handler.logger.sendErrorEvent({ eventName: \"setConnectionStateSame\", value });\n return;\n }\n\n const oldState = this._connectionState;\n this._connectionState = value;\n let client: ILocalSequencedClient | undefined;\n if (this._clientId !== undefined) {\n client = this.protocol?.quorum?.getMember(this._clientId);\n }\n if (value === ConnectionState.Connected) {\n assert(oldState === ConnectionState.CatchingUp,\n 0x1d8 /* \"Should only transition from Connecting state\" */);\n // Mark our old client should have left in the quorum if it's still there\n if (client !== undefined) {\n client.shouldHaveLeft = true;\n }\n this._clientId = this.pendingClientId;\n } else if (value === ConnectionState.Disconnected) {\n // Clear pending state immediately to prepare for reconnect\n this._pendingClientId = undefined;\n\n if (this.joinOpTimer.hasTimer) {\n this.stopJoinOpTimer();\n }\n\n // Only wait for \"leave\" message if the connected client exists in the quorum and had some non-acked ops\n // Also check if the timer is not already running as\n // we could receive \"Disconnected\" event multiple times without getting connected and in that case we\n // don't want to reset the timer as we still want to wait on original client which started this timer.\n if (client !== undefined\n && this.handler.shouldClientJoinWrite()\n && !this.waitingForLeaveOp // same as !this.prevClientLeftTimer.hasTimer\n ) {\n this.prevClientLeftTimer.restart();\n } else {\n // Adding this event temporarily so that we can get help debugging if something goes wrong.\n this.handler.logger.sendTelemetryEvent({\n eventName: \"noWaitOnDisconnected\",\n details: JSON.stringify({\n clientId: this._clientId,\n inQuorum: client !== undefined,\n waitingForLeaveOp: this.waitingForLeaveOp,\n hadOutstandingOps: this.handler.shouldClientJoinWrite(),\n }),\n });\n }\n }\n\n // Report transition before we propagate event across layers\n this.handler.connectionStateChanged(this._connectionState, oldState, reason);\n }\n\n // Helper method to switch between quorum and audience.\n // Old design was checking only quorum for \"write\" clients.\n // Latest change checks audience for all types of connections.\n protected get membership() {\n return this.protocol?.quorum;\n }\n\n public initProtocol(protocol: IProtocolHandler) {\n this.protocol = protocol;\n\n this.membership?.on(\"addMember\", (clientId) => {\n this.receivedAddMemberEvent(clientId);\n });\n\n this.membership?.on(\"removeMember\", (clientId) => {\n this.receivedRemoveMemberEvent(clientId);\n });\n\n // Very unlikely race condition, but theoretically can happen - our new connection is already\n // summarized and we are loading from such summary.\n if (this.hasMember(this.pendingClientId)) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n this.receivedAddMemberEvent(this.pendingClientId!);\n }\n\n // if we have a clientId from a previous container we need to wait for its leave message\n if (this.clientId !== undefined && this.hasMember(this.clientId)) {\n this.prevClientLeftTimer.restart();\n }\n }\n\n protected hasMember(clientId?: string) {\n return this.membership?.getMember(clientId ?? \"\") !== undefined;\n }\n}\n"]}
1
+ {"version":3,"file":"connectionStateHandler.js","sourceRoot":"","sources":["../src/connectionStateHandler.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,8BAA8B,CAAC;AAI7D,OAAO,EAAE,gBAAgB,EAAE,yBAAyB,EAAE,MAAM,iCAAiC,CAAC;AAC9F,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,cAA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C,QAAQ,CAAC,KAAK,SAAS,EAC5F,KAAK,CAAC,kCAAkC,CAAC,CAAC;YAC9C,IAAI,CAAC,sBAAsB,CAAC,QAAQ,CAAC,CAAC;QAC1C,CAAC,CAAC,CAAC;QAEH,MAAA,IAAI,CAAC,UAAU,0CAAE,EAAE,CAAC,cAAc,EAAE,CAAC,QAAQ,EAAE,EAAE;YAC7C,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,KAAK,SAAS,EAAE,KAAK,CAAC,kCAAkC,CAAC,CAAC;YACpG,IAAI,CAAC,yBAAyB,CAAC,QAAQ,CAAC,CAAC;QAC7C,CAAC,CAAC,CAAC;QAEH;;;;;;UAME;QACF,IAAI,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,eAAe,CAAC,EAAE;YACtC,oEAAoE;YACpE,IAAI,CAAC,sBAAsB,CAAC,IAAI,CAAC,eAAgB,CAAC,CAAC;SACtD;QAED,wFAAwF;QACxF,IAAI,IAAI,CAAC,QAAQ,KAAK,SAAS,IAAI,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE;YAC9D,IAAI,CAAC,mBAAmB,CAAC,OAAO,EAAE,CAAC;SACtC;IACL,CAAC;IAES,SAAS,CAAC,QAAiB;;QACjC,OAAO,CAAA,MAAA,IAAI,CAAC,UAAU,0CAAE,SAAS,CAAC,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,EAAE,CAAC,MAAK,SAAS,CAAC;IACpE,CAAC;CACJ","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { ITelemetryLogger, ITelemetryProperties } from \"@fluidframework/common-definitions\";\nimport { assert, Timer } from \"@fluidframework/common-utils\";\nimport { IConnectionDetails, IDeltaManager } from \"@fluidframework/container-definitions\";\nimport { ILocalSequencedClient } from \"@fluidframework/protocol-base\";\nimport { ISequencedClient, IClient } from \"@fluidframework/protocol-definitions\";\nimport { PerformanceEvent, loggerToMonitoringContext } from \"@fluidframework/telemetry-utils\";\nimport { ConnectionState } from \"./connectionState\";\nimport { CatchUpMonitor, ICatchUpMonitor } from \"./catchUpMonitor\";\nimport { IProtocolHandler } from \"./protocol\";\n\n// Based on recent data, it looks like majority of cases where we get stuck are due to really slow or\n// timing out ops fetches. So attempt recovery infrequently. Also fetch uses 30 second timeout, so\n// if retrying fixes the problem, we should not see these events.\nconst JoinOpTimeoutMs = 45000;\n\n// Timeout waiting for \"self\" join signal, before giving up\nconst JoinSignalTimeoutMs = 5000;\n\n/** Constructor parameter type for passing in dependencies needed by the ConnectionStateHandler */\nexport interface IConnectionStateHandlerInputs {\n logger: ITelemetryLogger;\n /** Log to telemetry any change in state, included to Connecting */\n connectionStateChanged:\n (value: ConnectionState, oldState: ConnectionState, reason?: string | undefined) => void;\n /** Whether to expect the client to join in write mode on next connection */\n shouldClientJoinWrite: () => boolean;\n /** (Optional) How long should we wait on our previous client's Leave op before transitioning to Connected again */\n maxClientLeaveWaitTime: number | undefined;\n /** Log an issue encountered while in the Connecting state. details will be logged as a JSON string */\n logConnectionIssue: (eventName: string, details?: ITelemetryProperties) => void;\n}\n\n/**\n * interface that connection state handler implements\n */\nexport interface IConnectionStateHandler {\n readonly connectionState: ConnectionState;\n readonly pendingClientId: string | undefined;\n\n containerSaved(): void;\n dispose(): void;\n initProtocol(protocol: IProtocolHandler): void;\n receivedConnectEvent(details: IConnectionDetails): void;\n receivedDisconnectEvent(reason: string): void;\n}\n\nexport function createConnectionStateHandler(\n inputs: IConnectionStateHandlerInputs,\n deltaManager: IDeltaManager<any, any>,\n clientId?: string,\n) {\n const mc = loggerToMonitoringContext(inputs.logger);\n return createConnectionStateHandlerCore(\n mc.config.getBoolean(\"Fluid.Container.CatchUpBeforeDeclaringConnected\") === true, // connectedRaisedWhenCaughtUp\n mc.config.getBoolean(\"Fluid.Container.DisableJoinSignalWait\") !== true, // readClientsWaitForJoinSignal\n inputs,\n deltaManager,\n clientId,\n );\n}\n\nexport function createConnectionStateHandlerCore(\n connectedRaisedWhenCaughtUp: boolean,\n readClientsWaitForJoinSignal: boolean,\n inputs: IConnectionStateHandlerInputs,\n deltaManager: IDeltaManager<any, any>,\n clientId?: string,\n) {\n if (!connectedRaisedWhenCaughtUp) {\n return new ConnectionStateHandler(inputs, readClientsWaitForJoinSignal, clientId);\n }\n return new ConnectionStateCatchup(\n inputs,\n (handler: IConnectionStateHandlerInputs) => new ConnectionStateHandler(\n handler,\n readClientsWaitForJoinSignal,\n clientId),\n deltaManager);\n}\n\n/**\n * Helper internal interface to abstract away Audience & Quorum\n */\ninterface IMembership {\n on(\n eventName: \"addMember\" | \"removeMember\",\n listener: (clientId: string, details: IClient | ISequencedClient) => void);\n getMember(clientId: string): undefined | unknown;\n}\n\n/**\n * Class that can be used as a base class for building IConnectionStateHandler adapters / pipeline.\n * It implements both ends of communication interfaces and passes data back and forward\n */\nclass ConnectionStateHandlerPassThrough implements IConnectionStateHandler, IConnectionStateHandlerInputs {\n protected readonly pimpl: IConnectionStateHandler;\n\n constructor(\n protected readonly inputs: IConnectionStateHandlerInputs,\n pimplFactory: (handler: IConnectionStateHandlerInputs) => IConnectionStateHandler,\n ) {\n this.pimpl = pimplFactory(this);\n }\n\n /**\n * IConnectionStateHandler\n */\n public get connectionState() { return this.pimpl.connectionState; }\n public get pendingClientId() { return this.pimpl.pendingClientId; }\n\n public containerSaved() { return this.pimpl.containerSaved(); }\n public dispose() { return this.pimpl.dispose(); }\n public initProtocol(protocol: IProtocolHandler) { return this.pimpl.initProtocol(protocol); }\n public receivedDisconnectEvent(reason: string) { return this.pimpl.receivedDisconnectEvent(reason); }\n\n public receivedConnectEvent(details: IConnectionDetails) {\n return this.pimpl.receivedConnectEvent(details);\n }\n\n /**\n * IConnectionStateHandlerInputs\n */\n\n public get logger() { return this.inputs.logger; }\n public connectionStateChanged(\n value: ConnectionState,\n oldState: ConnectionState,\n reason?: string | undefined,\n ) {\n return this.inputs.connectionStateChanged(value, oldState, reason);\n }\n public shouldClientJoinWrite() { return this.inputs.shouldClientJoinWrite(); }\n public get maxClientLeaveWaitTime() { return this.inputs.maxClientLeaveWaitTime; }\n public logConnectionIssue(eventName: string, details?: ITelemetryProperties) {\n return this.inputs.logConnectionIssue(eventName, details);\n }\n}\n\n/**\n * Implementation of IConnectionStateHandler pass-through adapter that waits for specific sequence number\n * before raising connected event\n */\nclass ConnectionStateCatchup extends ConnectionStateHandlerPassThrough {\n private catchUpMonitor: ICatchUpMonitor | undefined;\n\n constructor(\n inputs: IConnectionStateHandlerInputs,\n pimplFactory: (handler: IConnectionStateHandlerInputs) => IConnectionStateHandler,\n private readonly deltaManager: IDeltaManager<any, any>,\n ) {\n super(inputs, pimplFactory);\n this._connectionState = this.pimpl.connectionState;\n }\n\n private _connectionState: ConnectionState;\n public get connectionState() {\n return this._connectionState;\n }\n\n public connectionStateChanged(value: ConnectionState, oldState: ConnectionState, reason?: string | undefined) {\n switch (value) {\n case ConnectionState.Connected:\n assert(this._connectionState === ConnectionState.CatchingUp, 0x3e1 /* connectivity transitions */);\n // Create catch-up monitor here (not earlier), as we might get more exact info by now about how far\n // client is behind through join signal. This is only true if base layer uses signals (i.e. audience,\n // not quorum, including for \"rea\" connections) to make decisions about moving to \"connected\" state.\n // In addition to that, in its current form, doing this in ConnectionState.CatchingUp is dangerous as\n // we might get callback right away, and it will screw up state transition (as code outside of switch\n // statement will overwrite current state).\n assert(this.catchUpMonitor === undefined, 0x3eb /* catchUpMonitor should be gone */);\n this.catchUpMonitor = new CatchUpMonitor(this.deltaManager, this.transitionToConnectedState);\n return;\n case ConnectionState.Disconnected:\n this.catchUpMonitor?.dispose();\n this.catchUpMonitor = undefined;\n break;\n case ConnectionState.CatchingUp:\n assert(this._connectionState === ConnectionState.Disconnected, 0x3e3 /* connectivity transitions */);\n break;\n default:\n }\n this._connectionState = value;\n this.inputs.connectionStateChanged(value, oldState, reason);\n }\n\n private readonly transitionToConnectedState = () => {\n // Defensive measure, we should always be in Connecting state when this is called.\n const state = this.pimpl.connectionState;\n assert(state === ConnectionState.Connected, 0x3e5 /* invariant broken */);\n assert(this._connectionState === ConnectionState.CatchingUp, 0x3e6 /* invariant broken */);\n this._connectionState = ConnectionState.Connected;\n this.inputs.connectionStateChanged(ConnectionState.Connected, ConnectionState.CatchingUp, \"caught up\");\n };\n}\n\n/**\n * In the lifetime of a container, the connection will likely disconnect and reconnect periodically.\n * This class ensures that any ops sent by this container instance on previous connection are either\n * sequenced or blocked by the server before emitting the new \"connected\" event and allowing runtime to resubmit ops.\n *\n * Each connection is assigned a clientId by the service, and the connection is book-ended by a Join and a Leave op\n * generated by the service. Due to the distributed nature of the Relay Service, in the case of reconnect we cannot\n * make any assumptions about ordering of operations between the old and new connections - i.e. new Join op could\n * be sequenced before old Leave op (and some acks from pending ops that were in flight when we disconnected).\n *\n * The job of this class is to encapsulate the transition period during reconnect, which is identified by\n * ConnectionState.CatchingUp. Specifically, before moving to Connected state with the new clientId, it ensures that:\n *\n * a. We process the Leave op for the previous clientId. This allows us to properly handle any acks from in-flight ops\n * that got sequenced with the old clientId (we'll recognize them as local ops). After the Leave op, any other\n * pending ops can safely be submitted with the new clientId without fear of duplication in the sequenced op stream.\n *\n * b. We process the Join op for the new clientId (identified when the underlying connection was first established),\n * indicating the service is ready to sequence ops sent with the new clientId.\n *\n * c. We process all ops known at the time the underlying connection was established (so we are \"caught up\")\n *\n * For (a) we give up waiting after some time (same timeout as server uses), and go ahead and transition to Connected.\n *\n * For (b) we log telemetry if it takes too long, but still only transition to Connected when the Join op/signal is\n * processed.\n *\n * For (c) this is optional behavior, controlled by the parameters of receivedConnectEvent\n */\nclass ConnectionStateHandler implements IConnectionStateHandler {\n private _connectionState = ConnectionState.Disconnected;\n private _pendingClientId: string | undefined;\n private readonly prevClientLeftTimer: Timer;\n private readonly joinOpTimer: Timer;\n private protocol?: IProtocolHandler;\n private connection?: IConnectionDetails;\n private _clientId?: string;\n\n private waitEvent: PerformanceEvent | undefined;\n\n public get connectionState(): ConnectionState {\n return this._connectionState;\n }\n\n private get clientId(): string | undefined {\n return this._clientId;\n }\n\n public get pendingClientId(): string | undefined {\n return this._pendingClientId;\n }\n\n constructor(\n private readonly handler: IConnectionStateHandlerInputs,\n private readonly readClientsWaitForJoinSignal: boolean,\n clientIdFromPausedSession?: string,\n ) {\n this._clientId = clientIdFromPausedSession;\n this.prevClientLeftTimer = new Timer(\n // Default is 5 min for which we are going to wait for its own \"leave\" message. This is same as\n // the max time on server after which leave op is sent.\n this.handler.maxClientLeaveWaitTime ?? 300000,\n () => {\n assert(this.connectionState !== ConnectionState.Connected,\n 0x2ac /* \"Connected when timeout waiting for leave from previous session fired!\" */);\n this.applyForConnectedState(\"timeout\");\n },\n );\n\n this.joinOpTimer = new Timer(\n 0, // default value is not used - startJoinOpTimer() explicitly provides timeout\n () => {\n // I've observed timer firing within couple ms from disconnect event, looks like\n // queued timer callback is not cancelled if timer is cancelled while callback sits in the queue.\n if (this.connectionState !== ConnectionState.CatchingUp) {\n return;\n }\n const details = {\n protocolInitialized: this.protocol !== undefined,\n pendingClientId: this.pendingClientId,\n clientJoined: this.hasMember(this.pendingClientId),\n waitingForLeaveOp: this.waitingForLeaveOp,\n };\n this.handler.logConnectionIssue(\"NoJoinOp\", details);\n },\n );\n }\n\n private startJoinOpTimer() {\n assert(!this.joinOpTimer.hasTimer, 0x234 /* \"has joinOpTimer\" */);\n assert(this.connection !== undefined, 0x4b3 /* have connection */);\n this.joinOpTimer.start(\n this.connection.mode === \"write\" ? JoinOpTimeoutMs : JoinSignalTimeoutMs,\n );\n }\n\n private stopJoinOpTimer() {\n assert(this.joinOpTimer.hasTimer, 0x235 /* \"no joinOpTimer\" */);\n this.joinOpTimer.clear();\n }\n\n private get waitingForLeaveOp() {\n return this.prevClientLeftTimer.hasTimer;\n }\n\n public dispose() {\n assert(!this.joinOpTimer.hasTimer, 0x2a5 /* \"join timer\" */);\n this.prevClientLeftTimer.clear();\n }\n\n public containerSaved() {\n // If we were waiting for moving to Connected state, then only apply for state change. Since the container\n // is now saved and we don't have any ops to roundtrip, we can clear the timer and apply for connected state.\n if (this.waitingForLeaveOp) {\n this.prevClientLeftTimer.clear();\n this.applyForConnectedState(\"containerSaved\");\n }\n }\n\n private receivedAddMemberEvent(clientId: string) {\n // This is the only one that requires the pending client ID\n if (clientId === this.pendingClientId) {\n if (this.joinOpTimer.hasTimer) {\n this.stopJoinOpTimer();\n } else if (this.shouldWaitForJoinSignal()) {\n // timer has already fired, meaning it took too long to get join op/signal.\n // Record how long it actually took to recover.\n this.handler.logConnectionIssue(\"ReceivedJoinOp\");\n }\n // Start the event in case we are waiting for leave or timeout.\n if (this.waitingForLeaveOp) {\n this.waitEvent = PerformanceEvent.start(this.handler.logger, {\n eventName: \"WaitBeforeClientLeave\",\n details: JSON.stringify({\n waitOnClientId: this._clientId,\n hadOutstandingOps: this.handler.shouldClientJoinWrite(),\n }),\n });\n }\n this.applyForConnectedState(\"addMemberEvent\");\n }\n }\n\n private applyForConnectedState(source: \"removeMemberEvent\" | \"addMemberEvent\" | \"timeout\" | \"containerSaved\") {\n assert(this.protocol !== undefined, 0x236 /* \"In all cases it should be already installed\" */);\n\n assert(!this.waitingForLeaveOp || this.hasMember(this.clientId),\n 0x2e2 /* \"Must only wait for leave message when clientId in quorum\" */);\n\n // Move to connected state only if we are in Connecting state, we have seen our join op\n // and there is no timer running which means we are not waiting for previous client to leave\n // or timeout has occurred while doing so.\n if (this.pendingClientId !== this.clientId\n && this.hasMember(this.pendingClientId)\n && !this.waitingForLeaveOp\n ) {\n this.waitEvent?.end({ source });\n this.setConnectionState(ConnectionState.Connected);\n } else {\n // Adding this event temporarily so that we can get help debugging if something goes wrong.\n // We may not see any ops due to being disconnected all that time - that's not an error!\n const error = source === \"timeout\" && this.connectionState !== ConnectionState.Disconnected;\n this.handler.logger.sendTelemetryEvent({\n eventName: \"connectedStateRejected\",\n category: error ? \"error\" : \"generic\",\n details: JSON.stringify({\n source,\n pendingClientId: this.pendingClientId,\n clientId: this.clientId,\n waitingForLeaveOp: this.waitingForLeaveOp,\n clientJoined: this.hasMember(this.pendingClientId),\n }),\n });\n }\n }\n\n private receivedRemoveMemberEvent(clientId: string) {\n // If the client which has left was us, then finish the timer.\n if (this.clientId === clientId) {\n this.prevClientLeftTimer.clear();\n this.applyForConnectedState(\"removeMemberEvent\");\n }\n }\n\n public receivedDisconnectEvent(reason: string) {\n this.connection = undefined;\n this.setConnectionState(ConnectionState.Disconnected, reason);\n }\n\n private shouldWaitForJoinSignal() {\n assert(this.connection !== undefined, 0x4b4 /* all callers call here with active connection */);\n return this.connection.mode === \"write\" || this.readClientsWaitForJoinSignal;\n }\n\n /**\n * The \"connect\" event indicates the connection to the Relay Service is live.\n * However, some additional conditions must be met before we can fully transition to\n * \"Connected\" state. This function handles that interim period, known as \"Connecting\" state.\n * @param details - Connection details returned from the Relay Service\n * @param deltaManager - DeltaManager to be used for delaying Connected transition until caught up.\n * If it's undefined, then don't delay and transition to Connected as soon as Leave/Join op are accounted for\n */\n public receivedConnectEvent(\n details: IConnectionDetails,\n ) {\n this.connection = details;\n\n const oldState = this._connectionState;\n this._connectionState = ConnectionState.CatchingUp;\n\n // The following checks are wrong. They are only valid if user has write access to a file.\n // If user lost such access mid-session, user will not be able to get \"write\" connection.\n //\n // const writeConnection = details.mode === \"write\";\n // assert(!this.handler.shouldClientJoinWrite() || writeConnection,\n // 0x30a /* shouldClientJoinWrite should imply this is a writeConnection */);\n // assert(!this.waitingForLeaveOp || writeConnection,\n // 0x2a6 /* \"waitingForLeaveOp should imply writeConnection (we need to be ready to flush pending ops)\" */);\n\n // Stash the clientID to detect when transitioning from connecting (socket.io channel open) to connected\n // (have received the join message for the client ID)\n // This is especially important in the reconnect case. It's possible there could be outstanding\n // ops sent by this client, so we should keep the old client id until we see our own client's\n // join message. after we see the join message for our new connection with our new client id,\n // we know there can no longer be outstanding ops that we sent with the previous client id.\n this._pendingClientId = details.clientId;\n\n // IMPORTANT: Report telemetry after we set _pendingClientId, but before transitioning to Connected state\n this.handler.connectionStateChanged(ConnectionState.CatchingUp, oldState);\n\n // Check if we need to wait for join op/signal, and if we need to wait for leave op from previous connection.\n // Pending clientId could have joined already (i.e. join op/signal already processed):\n // We are fetching ops from storage in parallel to connecting to Relay Service,\n // and given async processes, it's possible that we have already processed our own join message before\n // connection was fully established.\n if (!this.hasMember(this._pendingClientId) && this.shouldWaitForJoinSignal()) {\n // We are waiting for our own join op / signal. When it is processed\n // we'll attempt to transition to Connected state via receivedAddMemberEvent() flow.\n this.startJoinOpTimer();\n } else if (!this.waitingForLeaveOp) {\n // We're not waiting for Join or Leave op (if read-only connection those don't even apply),\n // go ahead and declare the state to be Connected!\n // If we are waiting for Leave op still, do nothing for now, we will transition to Connected later.\n this.setConnectionState(ConnectionState.Connected);\n }\n // else - We are waiting for Leave op still, do nothing for now, we will transition to Connected later\n }\n\n private setConnectionState(value: ConnectionState.Disconnected, reason: string): void;\n private setConnectionState(value: ConnectionState.Connected): void;\n private setConnectionState(value: ConnectionState.Disconnected | ConnectionState.Connected, reason?: string): void {\n if (this.connectionState === value) {\n // Already in the desired state - exit early\n this.handler.logger.sendErrorEvent({ eventName: \"setConnectionStateSame\", value });\n return;\n }\n\n const oldState = this._connectionState;\n this._connectionState = value;\n\n // This is the only place in code that deals with quorum. The rest works with audience\n // The code below ensures that we do not send ops until we know that old \"write\" client's disconnect\n // produced (and sequenced) leave op\n let client: ILocalSequencedClient | undefined;\n if (this._clientId !== undefined) {\n client = this.protocol?.quorum?.getMember(this._clientId);\n }\n if (value === ConnectionState.Connected) {\n assert(oldState === ConnectionState.CatchingUp,\n 0x1d8 /* \"Should only transition from Connecting state\" */);\n // Mark our old client should have left in the quorum if it's still there\n if (client !== undefined) {\n client.shouldHaveLeft = true;\n }\n this._clientId = this.pendingClientId;\n } else if (value === ConnectionState.Disconnected) {\n // Clear pending state immediately to prepare for reconnect\n this._pendingClientId = undefined;\n\n if (this.joinOpTimer.hasTimer) {\n this.stopJoinOpTimer();\n }\n\n // Only wait for \"leave\" message if the connected client exists in the quorum and had some non-acked ops\n // Also check if the timer is not already running as\n // we could receive \"Disconnected\" event multiple times without getting connected and in that case we\n // don't want to reset the timer as we still want to wait on original client which started this timer.\n if (client !== undefined\n && this.handler.shouldClientJoinWrite()\n && !this.waitingForLeaveOp // same as !this.prevClientLeftTimer.hasTimer\n ) {\n this.prevClientLeftTimer.restart();\n } else {\n // Adding this event temporarily so that we can get help debugging if something goes wrong.\n this.handler.logger.sendTelemetryEvent({\n eventName: \"noWaitOnDisconnected\",\n details: JSON.stringify({\n clientId: this._clientId,\n inQuorum: client !== undefined,\n waitingForLeaveOp: this.waitingForLeaveOp,\n hadOutstandingOps: this.handler.shouldClientJoinWrite(),\n }),\n });\n }\n }\n\n // Report transition before we propagate event across layers\n this.handler.connectionStateChanged(this._connectionState, oldState, reason);\n }\n\n // Helper method to switch between quorum and audience.\n // Old design was checking only quorum for \"write\" clients.\n // Latest change checks audience for all types of connections.\n protected get membership(): IMembership | undefined {\n // We could always use audience here, and in practice it will probably be correct.\n // (including case when this.readClientsWaitForJoinSignal === false).\n // But only if it's superset of quorum, i.e. when filtered to \"write\" clients, they are always identical!\n // It's safer to assume that we have bugs and engaging kill-bit switch should bring us back to well-known\n // and tested state!\n return this.readClientsWaitForJoinSignal ? this.protocol?.audience : this.protocol?.quorum;\n }\n\n public initProtocol(protocol: IProtocolHandler) {\n this.protocol = protocol;\n\n this.membership?.on(\"addMember\", (clientId, details) => {\n assert((details as IClient).mode === \"read\" || protocol.quorum.getMember(clientId) !== undefined,\n 0x4b5 /* Audience is subset of quorum */);\n this.receivedAddMemberEvent(clientId);\n });\n\n this.membership?.on(\"removeMember\", (clientId) => {\n assert(protocol.quorum.getMember(clientId) === undefined, 0x4b6 /* Audience is subset of quorum */);\n this.receivedRemoveMemberEvent(clientId);\n });\n\n /* There is a tiny tiny race possible, where these events happen in this order:\n 1. A connection is established (no \"cached\" mode is used, so it happens in parallel / faster than other steps)\n 2. Some other client produces a summary\n 3. We get \"lucky\" and load from that summary as our initial snapshot\n 4. ConnectionStateHandler.initProtocol is called, \"self\" is already in the quorum.\n We could avoid this sequence (and delete test case for it) if we move connection lower in Container.load()\n */\n if (this.hasMember(this.pendingClientId)) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n this.receivedAddMemberEvent(this.pendingClientId!);\n }\n\n // if we have a clientId from a previous container we need to wait for its leave message\n if (this.clientId !== undefined && this.hasMember(this.clientId)) {\n this.prevClientLeftTimer.restart();\n }\n }\n\n protected hasMember(clientId?: string) {\n return this.membership?.getMember(clientId ?? \"\") !== undefined;\n }\n}\n"]}
@@ -2,6 +2,7 @@
2
2
  * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
3
  * Licensed under the MIT License.
4
4
  */
5
+ import { ITelemetryLogger, ITelemetryProperties } from "@fluidframework/common-definitions";
5
6
  import { IRequest, IResponse, IFluidRouter } from "@fluidframework/core-interfaces";
6
7
  import { IAudience, IContainer, IContainerEvents, IDeltaManager, ICriticalContainerError, AttachState, ReadOnlyInfo, IContainerLoadMode, IFluidCodeDetails } from "@fluidframework/container-definitions";
7
8
  import { IDocumentStorageService, IFluidResolvedUrl, IResolvedUrl } from "@fluidframework/driver-definitions";
@@ -58,6 +59,13 @@ export interface IContainerConfig {
58
59
  * @throws an error beginning with `"Container closed"` if the container is closed before it catches up.
59
60
  */
60
61
  export declare function waitContainerToCatchUp(container: IContainer): Promise<boolean>;
62
+ /**
63
+ * Helper function to report to telemetry cases where operation takes longer than expected (1s)
64
+ * @param logger - logger to use
65
+ * @param eventName - event name
66
+ * @param action - functor to call and measure
67
+ */
68
+ export declare function ReportIfTooLong(logger: ITelemetryLogger, eventName: string, action: () => Promise<ITelemetryProperties>): Promise<void>;
61
69
  /**
62
70
  * State saved by a container at close time, to be used to load a new instance
63
71
  * of the container to the same state
@@ -1 +1 @@
1
- {"version":3,"file":"container.d.ts","sourceRoot":"","sources":["../src/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C,uBAAuB;IAiD/B,OAAO,CAAC,sBAAsB;IA2B9B,OAAO,CAAC,wBAAwB;IAQhC,OAAO,KAAK,MAAM,GAkBjB;IAED;;;;;OAKG;IACH,OAAO,CAAC,gBAAgB;IAKxB,OAAO,CAAC,kBAAkB;YAsDZ,2BAA2B;IAgBzC,OAAO,CAAC,iCAAiC;IAsDzC,OAAO,CAAC,wBAAwB;IAgChC,OAAO,CAAC,sBAAsB;IAkB9B,+DAA+D;IAC/D,OAAO,CAAC,WAAW;IAcnB,OAAO,CAAC,oBAAoB;IAa5B,OAAO,CAAC,aAAa;IAerB,OAAO,CAAC,oBAAoB;IAmC5B,OAAO,CAAC,YAAY;IAIpB,OAAO,CAAC,aAAa;IAUrB;;;;OAIG;YACW,iBAAiB;YAiBjB,0BAA0B;YAgB1B,kBAAkB;IAkChC,OAAO,CAAC,yBAAyB;IAQjC;;;;;OAKG;IACH,OAAO,CAAC,wBAAwB;CAWnC"}
package/lib/container.js CHANGED
@@ -111,10 +111,10 @@ const getCodeProposal =
111
111
  * @param eventName - event name
112
112
  * @param action - functor to call and measure
113
113
  */
114
- async function ReportIfTooLong(logger, eventName, action) {
114
+ export async function ReportIfTooLong(logger, eventName, action) {
115
115
  const event = PerformanceEvent.start(logger, { eventName });
116
116
  const props = await action();
117
- if (event.duration > 1000) {
117
+ if (event.duration > 200) {
118
118
  event.end(props);
119
119
  }
120
120
  }
@@ -202,9 +202,23 @@ export class Container extends EventEmitterWithErrorHandling {
202
202
  shouldClientJoinWrite: () => this._deltaManager.connectionManager.shouldJoinWrite(),
203
203
  maxClientLeaveWaitTime: this.loader.services.options.maxClientLeaveWaitTime,
204
204
  logConnectionIssue: (eventName, details) => {
205
+ const mode = this.connectionMode;
205
206
  // We get here when socket does not receive any ops on "write" connection, including
206
207
  // its own join op. Attempt recovery option.
207
- this._deltaManager.logConnectionIssue(Object.assign({ eventName, duration: performance.now() - this.connectionTransitionTimes[ConnectionState.CatchingUp] }, (details === undefined ? {} : { details: JSON.stringify(details) })));
208
+ this._deltaManager.logConnectionIssue(Object.assign({ eventName,
209
+ mode, duration: performance.now() - this.connectionTransitionTimes[ConnectionState.CatchingUp] }, (details === undefined ? {} : { details: JSON.stringify(details) })));
210
+ // If this is "write" connection, it took too long to receive join op. But in most cases that's due
211
+ // to very slow op fetches and we will eventually get there.
212
+ // For "read" connections, we get here due to self join signal not arriving on time. We will need to
213
+ // better understand when and why it may happen.
214
+ // For now, attempt to recover by reconnecting. In future, maybe we can query relay service for
215
+ // current state of audience.
216
+ // Other possible recovery path - move to connected state (i.e. ConnectionStateHandler.joinOpTimer
217
+ // to call this.applyForConnectedState("addMemberEvent") for "read" connections)
218
+ if (mode === "read") {
219
+ this.disconnect();
220
+ this.connect();
221
+ }
208
222
  },
209
223
  }, this.deltaManager, this._clientId);
210
224
  this.on(savedContainerEvent, () => {
@@ -1025,7 +1039,8 @@ export class Container extends EventEmitterWithErrorHandling {
1025
1039
  // eslint-disable-next-line @typescript-eslint/no-floating-promises
1026
1040
  deltaManager.inboundSignal.pause();
1027
1041
  deltaManager.on("connect", (details, _opsBehind) => {
1028
- this.connectionStateHandler.receivedConnectEvent(this.connectionMode, details);
1042
+ assert(this.connectionMode === details.mode, 0x4b7 /* mismatch */);
1043
+ this.connectionStateHandler.receivedConnectEvent(details);
1029
1044
  });
1030
1045
  deltaManager.on("disconnect", (reason) => {
1031
1046
  var _a;
@@ -1138,7 +1153,7 @@ export class Container extends EventEmitterWithErrorHandling {
1138
1153
  let clientSequenceNumber = -1;
1139
1154
  for (const message of batch) {
1140
1155
  clientSequenceNumber = this.submitMessage(MessageType.Operation, message.contents, true, // batch
1141
- message.metadata);
1156
+ message.metadata, message.compression);
1142
1157
  }
1143
1158
  this._deltaManager.flush();
1144
1159
  return clientSequenceNumber;
@@ -1155,7 +1170,7 @@ export class Container extends EventEmitterWithErrorHandling {
1155
1170
  this.options.summarizeProtocolTree === true;
1156
1171
  return this.submitMessage(MessageType.Summarize, JSON.stringify(summary), false /* batch */);
1157
1172
  }
1158
- submitMessage(type, contents, batch, metadata) {
1173
+ submitMessage(type, contents, batch, metadata, compression) {
1159
1174
  var _a;
1160
1175
  if (this.connectionState !== ConnectionState.Connected) {
1161
1176
  this.mc.logger.sendErrorEvent({ eventName: "SubmitMessageWithNoConnection", type });
@@ -1163,7 +1178,7 @@ export class Container extends EventEmitterWithErrorHandling {
1163
1178
  }
1164
1179
  this.messageCountAfterDisconnection += 1;
1165
1180
  (_a = this.collabWindowTracker) === null || _a === void 0 ? void 0 : _a.stopSequenceNumberUpdate();
1166
- return this._deltaManager.submit(type, contents, batch, metadata);
1181
+ return this._deltaManager.submit(type, contents, batch, metadata, compression);
1167
1182
  }
1168
1183
  processRemoteMessage(message) {
1169
1184
  const local = this.clientId === message.clientId;