@fluidframework/container-loader 0.53.0 → 0.54.2

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 (57) hide show
  1. package/dist/connectionManager.d.ts +153 -0
  2. package/dist/connectionManager.d.ts.map +1 -0
  3. package/dist/connectionManager.js +664 -0
  4. package/dist/connectionManager.js.map +1 -0
  5. package/dist/container.d.ts +2 -1
  6. package/dist/container.d.ts.map +1 -1
  7. package/dist/container.js +25 -28
  8. package/dist/container.js.map +1 -1
  9. package/dist/contracts.d.ts +112 -0
  10. package/dist/contracts.d.ts.map +1 -0
  11. package/dist/contracts.js +14 -0
  12. package/dist/contracts.js.map +1 -0
  13. package/dist/deltaManager.d.ts +26 -135
  14. package/dist/deltaManager.d.ts.map +1 -1
  15. package/dist/deltaManager.js +142 -767
  16. package/dist/deltaManager.js.map +1 -1
  17. package/dist/loader.d.ts +9 -4
  18. package/dist/loader.d.ts.map +1 -1
  19. package/dist/loader.js +5 -4
  20. package/dist/loader.js.map +1 -1
  21. package/dist/packageVersion.d.ts +1 -1
  22. package/dist/packageVersion.js +1 -1
  23. package/dist/packageVersion.js.map +1 -1
  24. package/dist/protocolTreeDocumentStorageService.d.ts +2 -2
  25. package/dist/protocolTreeDocumentStorageService.d.ts.map +1 -1
  26. package/lib/connectionManager.d.ts +153 -0
  27. package/lib/connectionManager.d.ts.map +1 -0
  28. package/lib/connectionManager.js +660 -0
  29. package/lib/connectionManager.js.map +1 -0
  30. package/lib/container.d.ts +2 -1
  31. package/lib/container.d.ts.map +1 -1
  32. package/lib/container.js +25 -28
  33. package/lib/container.js.map +1 -1
  34. package/lib/contracts.d.ts +112 -0
  35. package/lib/contracts.d.ts.map +1 -0
  36. package/lib/contracts.js +11 -0
  37. package/lib/contracts.js.map +1 -0
  38. package/lib/deltaManager.d.ts +26 -135
  39. package/lib/deltaManager.d.ts.map +1 -1
  40. package/lib/deltaManager.js +146 -771
  41. package/lib/deltaManager.js.map +1 -1
  42. package/lib/loader.d.ts +9 -4
  43. package/lib/loader.d.ts.map +1 -1
  44. package/lib/loader.js +6 -5
  45. package/lib/loader.js.map +1 -1
  46. package/lib/packageVersion.d.ts +1 -1
  47. package/lib/packageVersion.js +1 -1
  48. package/lib/packageVersion.js.map +1 -1
  49. package/lib/protocolTreeDocumentStorageService.d.ts +2 -2
  50. package/lib/protocolTreeDocumentStorageService.d.ts.map +1 -1
  51. package/package.json +8 -8
  52. package/src/connectionManager.ts +892 -0
  53. package/src/container.ts +39 -39
  54. package/src/contracts.ts +156 -0
  55. package/src/deltaManager.ts +181 -978
  56. package/src/loader.ts +31 -9
  57. package/src/packageVersion.ts +1 -1
@@ -7,7 +7,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
7
7
  return (mod && mod.__esModule) ? mod : { "default": mod };
8
8
  };
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.DeltaManager = exports.ReconnectMode = void 0;
10
+ exports.DeltaManager = void 0;
11
11
  const abort_controller_1 = __importDefault(require("abort-controller"));
12
12
  const uuid_1 = require("uuid");
13
13
  const common_utils_1 = require("@fluidframework/common-utils");
@@ -18,79 +18,16 @@ const protocol_definitions_1 = require("@fluidframework/protocol-definitions");
18
18
  const driver_utils_1 = require("@fluidframework/driver-utils");
19
19
  const container_utils_1 = require("@fluidframework/container-utils");
20
20
  const deltaQueue_1 = require("./deltaQueue");
21
- const MaxReconnectDelayInMs = 8000;
22
- const InitialReconnectDelayInMs = 1000;
23
- const DefaultChunkSize = 16 * 1024;
24
- function getNackReconnectInfo(nackContent) {
25
- const message = `Nack (${nackContent.type}): ${nackContent.message}`;
26
- const canRetry = nackContent.code !== 403;
27
- const retryAfterMs = nackContent.retryAfter !== undefined ? nackContent.retryAfter * 1000 : undefined;
28
- return driver_utils_1.createGenericNetworkError(`nack [${nackContent.code}]`, message, canRetry, retryAfterMs, { statusCode: nackContent.code });
29
- }
30
- const createReconnectError = (fluidErrorCode, err) => telemetry_utils_1.wrapError(err, (errorMessage) => new driver_utils_1.GenericNetworkError(fluidErrorCode, errorMessage, true /* canRetry */));
31
- const fatalConnectErrorProp = { fatalConnectError: true };
32
- var ReconnectMode;
33
- (function (ReconnectMode) {
34
- ReconnectMode["Never"] = "Never";
35
- ReconnectMode["Disabled"] = "Disabled";
36
- ReconnectMode["Enabled"] = "Enabled";
37
- })(ReconnectMode = exports.ReconnectMode || (exports.ReconnectMode = {}));
38
- /**
39
- * Implementation of IDocumentDeltaConnection that does not support submitting
40
- * or receiving ops. Used in storage-only mode.
41
- */
42
- class NoDeltaStream extends common_utils_1.TypedEventEmitter {
43
- constructor() {
44
- super(...arguments);
45
- this.clientId = "storage-only client";
46
- this.claims = {
47
- scopes: [protocol_definitions_1.ScopeType.DocRead],
48
- };
49
- this.mode = "read";
50
- this.existing = true;
51
- this.maxMessageSize = 0;
52
- this.version = "";
53
- this.initialMessages = [];
54
- this.initialSignals = [];
55
- this.initialClients = [];
56
- this.serviceConfiguration = {
57
- maxMessageSize: 0,
58
- blockSize: 0,
59
- summary: undefined,
60
- };
61
- this.checkpointSequenceNumber = undefined;
62
- this._disposed = false;
63
- }
64
- submit(messages) {
65
- this.emit("nack", this.clientId, messages.map((operation) => {
66
- return {
67
- operation,
68
- content: { message: "Cannot submit with storage-only connection", code: 403 },
69
- };
70
- }));
71
- }
72
- submitSignal(message) {
73
- this.emit("nack", this.clientId, {
74
- operation: message,
75
- content: { message: "Cannot submit signal with storage-only connection", code: 403 },
76
- });
77
- }
78
- get disposed() { return this._disposed; }
79
- dispose() { this._disposed = true; }
80
- }
81
21
  /**
82
22
  * Manages the flow of both inbound and outbound messages. This class ensures that shared objects receive delta
83
23
  * messages in order regardless of possible network conditions or timings causing out of order delivery.
84
24
  */
85
25
  class DeltaManager extends common_utils_1.TypedEventEmitter {
86
- constructor(serviceProvider, client, logger, reconnectAllowed, _active) {
26
+ constructor(serviceProvider, logger, _active, createConnectionManager) {
87
27
  super();
88
28
  this.serviceProvider = serviceProvider;
89
- this.client = client;
90
29
  this.logger = logger;
91
30
  this._active = _active;
92
- // tracks host requiring read-only mode.
93
- this._forceReadonly = false;
94
31
  this.pending = [];
95
32
  // The minimum sequence number and last sequence number received from the server
96
33
  this.minSequenceNumber = 0;
@@ -107,82 +44,30 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
107
44
  this.baseTerm = 0;
108
45
  // The sequence number we initially loaded from
109
46
  this.initSequenceNumber = 0;
110
- this.clientSequenceNumber = 0;
111
- this.clientSequenceNumberObserved = 0;
112
- // Counts the number of noops sent by the client which may not be acked.
113
- this.trailingNoopCount = 0;
114
47
  this.closed = false;
115
- this.deltaStreamDelayId = uuid_1.v4();
116
- this.deltaStorageDelayId = uuid_1.v4();
117
- this.messageBuffer = [];
118
- this.connectFirstConnection = true;
119
48
  this.throttlingIdSet = new Set();
120
49
  this.timeTillThrottling = 0;
121
- this.connectionStateProps = {};
122
- // True if current connection has checkpoint information
123
- // I.e. we know how far behind the client was at the time of establishing connection
124
- this._hasCheckpointSequenceNumber = false;
125
50
  this.closeAbortController = new abort_controller_1.default();
126
- // True if there is pending (async) reconnection from "read" to "write"
127
- this.pendingReconnect = false;
128
- // downgrade "write" connection to "read"
129
- this.downgradedConnection = false;
130
- this.opHandler = (documentId, messagesArg) => {
131
- const messages = Array.isArray(messagesArg) ? messagesArg : [messagesArg];
132
- this.enqueueMessages(messages, "opHandler");
133
- };
134
- this.signalHandler = (message) => {
135
- this._inboundSignal.push(message);
136
- };
137
- // Always connect in write mode after getting nacked.
138
- this.nackHandler = (documentId, messages) => {
139
- const message = messages[0];
140
- // TODO: we should remove this check when service updates?
141
- // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
142
- if (this._readonlyPermissions) {
143
- this.close(driver_utils_1.createWriteError("writeOnReadOnlyDocument"));
144
- }
145
- // check message.content for Back-compat with old service.
146
- const reconnectInfo = message.content !== undefined
147
- ? getNackReconnectInfo(message.content) :
148
- driver_utils_1.createGenericNetworkError("nackReasonUnknown", undefined, true);
149
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
150
- this.reconnectOnError("write", reconnectInfo);
151
- };
152
- // Connection mode is always read on disconnect/error unless the system mode was write.
153
- this.disconnectHandler = (disconnectReason) => {
154
- // Note: we might get multiple disconnect calls on same socket, as early disconnect notification
155
- // ("server_disconnect", ODSP-specific) is mapped to "disconnect"
156
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
157
- this.reconnectOnError(this.defaultReconnectionMode, createReconnectError("dmDocumentDeltaConnectionDisconnected", disconnectReason));
158
- };
159
- this.errorHandler = (error) => {
160
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
161
- this.reconnectOnError(this.defaultReconnectionMode, createReconnectError("dmDocumentDeltaConnectionError", error));
162
- };
163
- this.pongHandler = (latency) => {
164
- this.emit("pong", latency);
51
+ this.deltaStorageDelayId = uuid_1.v4();
52
+ this.deltaStreamDelayId = uuid_1.v4();
53
+ this.messageBuffer = [];
54
+ const props = {
55
+ incomingOpHandler: (messages, reason) => this.enqueueMessages(messages, reason),
56
+ signalHandler: (message) => this._inboundSignal.push(message),
57
+ reconnectionDelayHandler: (delayMs, error) => this.emitDelayInfo(this.deltaStreamDelayId, delayMs, error),
58
+ closeHandler: (error) => this.close(error),
59
+ disconnectHandler: (reason) => this.disconnectHandler(reason),
60
+ connectHandler: (connection) => this.connectHandler(connection),
61
+ pongHandler: (latency) => this.emit("pong", latency),
62
+ readonlyChangeHandler: (readonly) => telemetry_utils_1.safeRaiseEvent(this, this.logger, "readonly", readonly),
165
63
  };
166
- this.clientDetails = this.client.details;
167
- this.defaultReconnectionMode = this.client.mode;
168
- this._reconnectMode = reconnectAllowed ? ReconnectMode.Enabled : ReconnectMode.Never;
64
+ this.connectionManager = createConnectionManager(props);
169
65
  this._inbound = new deltaQueue_1.DeltaQueue((op) => {
170
66
  this.processInboundMessage(op);
171
67
  });
172
68
  this._inbound.on("error", (error) => {
173
69
  this.close(container_utils_1.CreateProcessingError(error, "deltaManagerInboundErrorHandler", this.lastMessage));
174
70
  });
175
- // Outbound message queue. The outbound queue is represented as a queue of an array of ops. Ops contained
176
- // within an array *must* fit within the maxMessageSize and are guaranteed to be ordered sequentially.
177
- this._outbound = new deltaQueue_1.DeltaQueue((messages) => {
178
- if (this.connection === undefined) {
179
- throw new Error("Attempted to submit an outbound message without connection");
180
- }
181
- this.connection.submit(messages);
182
- });
183
- this._outbound.on("error", (error) => {
184
- this.close(telemetry_utils_1.normalizeError(error));
185
- });
186
71
  // Inbound signal queue
187
72
  this._inboundSignal = new deltaQueue_1.DeltaQueue((message) => {
188
73
  if (this.handler === undefined) {
@@ -203,21 +88,9 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
203
88
  get active() { return this._active(); }
204
89
  get disposed() { return this.closed; }
205
90
  get IDeltaSender() { return this; }
206
- /**
207
- * Tells if current connection has checkpoint information.
208
- * I.e. we know how far behind the client was at the time of establishing connection
209
- */
210
- get hasCheckpointSequenceNumber() {
211
- // Valid to be called only if we have active connection.
212
- common_utils_1.assert(this.connection !== undefined, 0x0df /* "Missing active connection" */);
213
- return this._hasCheckpointSequenceNumber;
214
- }
215
91
  get inbound() {
216
92
  return this._inbound;
217
93
  }
218
- get outbound() {
219
- return this._outbound;
220
- }
221
94
  get inboundSignal() {
222
95
  return this._inboundSignal;
223
96
  }
@@ -239,39 +112,22 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
239
112
  get minimumSequenceNumber() {
240
113
  return this.minSequenceNumber;
241
114
  }
242
- get maxMessageSize() {
243
- var _a, _b, _c;
244
- return (_c = (_b = (_a = this.connection) === null || _a === void 0 ? void 0 : _a.serviceConfiguration) === null || _b === void 0 ? void 0 : _b.maxMessageSize) !== null && _c !== void 0 ? _c : DefaultChunkSize;
245
- }
246
- get version() {
247
- if (this.connection === undefined) {
248
- throw new Error("Cannot check version without a connection");
249
- }
250
- return this.connection.version;
251
- }
252
- get serviceConfiguration() {
253
- var _a;
254
- return (_a = this.connection) === null || _a === void 0 ? void 0 : _a.serviceConfiguration;
255
- }
256
- get scopes() {
257
- var _a;
258
- return (_a = this.connection) === null || _a === void 0 ? void 0 : _a.claims.scopes;
259
- }
260
- get socketDocumentId() {
261
- var _a;
262
- return (_a = this.connection) === null || _a === void 0 ? void 0 : _a.claims.documentId;
263
- }
264
115
  /**
265
- * The current connection mode, initially read.
116
+ * Tells if current connection has checkpoint information.
117
+ * I.e. we know how far behind the client was at the time of establishing connection
266
118
  */
267
- get connectionMode() {
268
- var _a;
269
- common_utils_1.assert(!this.downgradedConnection || ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.mode) === "write", 0x277 /* "Did we forget to reset downgradedConnection on new connection?" */);
270
- if (this.connection === undefined) {
271
- return "read";
272
- }
273
- return this.connection.mode;
274
- }
119
+ get hasCheckpointSequenceNumber() {
120
+ // Valid to be called only if we have active connection.
121
+ common_utils_1.assert(this.connectionManager.connected, 0x0df /* "Missing active connection" */);
122
+ return this._checkpointSequenceNumber !== undefined;
123
+ }
124
+ // Forwarding connection manager properties / IDeltaManager implementation
125
+ get maxMessageSize() { return this.connectionManager.maxMessageSize; }
126
+ get version() { return this.connectionManager.version; }
127
+ get serviceConfiguration() { return this.connectionManager.serviceConfiguration; }
128
+ get outbound() { return this.connectionManager.outbound; }
129
+ get readOnlyInfo() { return this.connectionManager.readOnlyInfo; }
130
+ get clientDetails() { return this.connectionManager.clientDetails; }
275
131
  /**
276
132
  * Tells if container is in read-only mode.
277
133
  * Data stores should listen for "readonly" notifications and disallow user
@@ -283,109 +139,50 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
283
139
  * @deprecated - use readOnlyInfo
284
140
  */
285
141
  get readonly() {
286
- if (this._forceReadonly) {
287
- return true;
288
- }
289
- return this._readonlyPermissions;
290
- }
291
- get readOnlyInfo() {
292
- const storageOnly = this.connection !== undefined && this.connection instanceof NoDeltaStream;
293
- if (storageOnly || this._forceReadonly || this._readonlyPermissions === true) {
294
- return {
295
- readonly: true,
296
- forced: this._forceReadonly,
297
- permissions: this._readonlyPermissions,
298
- storageOnly,
299
- };
300
- }
301
- return { readonly: this._readonlyPermissions };
302
- }
303
- /**
304
- * Automatic reconnecting enabled or disabled.
305
- * If set to Never, then reconnecting will never be allowed.
306
- */
307
- get reconnectMode() {
308
- return this._reconnectMode;
142
+ return this.readOnlyInfo.readonly;
309
143
  }
310
- shouldJoinWrite() {
311
- // We don't have to wait for ack for topmost NoOps. So subtract those.
312
- return this.clientSequenceNumberObserved < (this.clientSequenceNumber - this.trailingNoopCount);
313
- }
314
- /**
315
- * Returns set of props that can be logged in telemetry that provide some insights / statistics
316
- * about current or last connection (if there is no connection at the moment)
317
- */
318
- connectionProps() {
319
- const common = {
320
- sequenceNumber: this.lastSequenceNumber,
144
+ submit(type, contents, batch = false, metadata) {
145
+ // Start adding trace for the op.
146
+ const traces = [
147
+ {
148
+ action: "start",
149
+ service: "client",
150
+ timestamp: Date.now(),
151
+ }
152
+ ];
153
+ const messagePartial = {
154
+ contents: JSON.stringify(contents),
155
+ metadata,
156
+ referenceSequenceNumber: this.lastProcessedSequenceNumber,
157
+ traces,
158
+ type,
321
159
  };
322
- if (this.connection !== undefined) {
323
- return Object.assign(Object.assign({}, common), { connectionMode: this.connectionMode, relayServiceAgent: this.connection.relayServiceAgent });
160
+ if (!batch) {
161
+ this.flush();
324
162
  }
325
- else {
326
- return Object.assign(Object.assign({}, common), {
327
- // Report how many ops this client sent in last disconnected session
328
- sentOps: this.clientSequenceNumber });
163
+ const message = this.connectionManager.prepareMessageToSend(messagePartial);
164
+ if (message === undefined) {
165
+ return -1;
329
166
  }
330
- }
331
- /**
332
- * Enables or disables automatic reconnecting.
333
- * Will throw an error if reconnectMode set to Never.
334
- */
335
- setAutoReconnect(mode) {
336
- common_utils_1.assert(mode !== ReconnectMode.Never && this._reconnectMode !== ReconnectMode.Never, 0x278 /* "API is not supported for non-connecting or closed container" */);
337
- this._reconnectMode = mode;
338
- if (mode !== ReconnectMode.Enabled) {
339
- // immediately disconnect - do not rely on service eventually dropping connection.
340
- this.disconnectFromDeltaStream("setAutoReconnect");
167
+ this.messageBuffer.push(message);
168
+ if (!batch) {
169
+ this.flush();
341
170
  }
171
+ this.emit("submitOp", message);
172
+ return message.clientSequenceNumber;
342
173
  }
343
- /**
344
- * Sends signal to runtime (and data stores) to be read-only.
345
- * Hosts may have read only views, indicating to data stores that no edits are allowed.
346
- * This is independent from this._readonlyPermissions (permissions) and this.connectionMode
347
- * (server can return "write" mode even when asked for "read")
348
- * Leveraging same "readonly" event as runtime & data stores should behave the same in such case
349
- * as in read-only permissions.
350
- * But this.active can be used by some DDSes to figure out if ops can be sent
351
- * (for example, read-only view still participates in code proposals / upgrades decisions)
352
- *
353
- * Forcing Readonly does not prevent DDS from generating ops. It is up to user code to honour
354
- * the readonly flag. If ops are generated, they will accumulate locally and not be sent. If
355
- * there are pending in the outbound queue, it will stop sending until force readonly is
356
- * cleared.
357
- *
358
- * @param readonly - set or clear force readonly.
359
- */
360
- forceReadonly(readonly) {
361
- if (readonly !== this._forceReadonly) {
362
- this.logger.sendTelemetryEvent({
363
- eventName: "ForceReadOnly",
364
- value: readonly,
365
- });
366
- }
367
- const oldValue = this.readOnlyInfo.readonly;
368
- this._forceReadonly = readonly;
369
- if (oldValue !== this.readOnlyInfo.readonly) {
370
- common_utils_1.assert(this._reconnectMode !== ReconnectMode.Never, 0x279 /* "API is not supported for non-connecting or closed container" */);
371
- let reconnect = false;
372
- if (this.readOnlyInfo.readonly === true) {
373
- // If we switch to readonly while connected, we should disconnect first
374
- // See comment in the "readonly" event handler to deltaManager set up by
375
- // the ContainerRuntime constructor
376
- if (this.shouldJoinWrite()) {
377
- // If we have pending changes, then we will never send them - it smells like
378
- // host logic error.
379
- this.logger.sendErrorEvent({ eventName: "ForceReadonlyPendingChanged" });
380
- }
381
- reconnect = this.disconnectFromDeltaStream("Force readonly");
382
- }
383
- telemetry_utils_1.safeRaiseEvent(this, this.logger, "readonly", this.readOnlyInfo.readonly);
384
- if (reconnect) {
385
- // reconnect if we disconnected from before.
386
- this.triggerConnect({ reason: "forceReadonly", mode: "read", fetchOpsFromStorage: false });
387
- }
174
+ submitSignal(content) { return this.connectionManager.submitSignal(content); }
175
+ flush() {
176
+ if (this.messageBuffer.length === 0) {
177
+ return;
388
178
  }
179
+ // The prepareFlush event allows listeners to append metadata to the batch prior to submission.
180
+ this.emit("prepareSend", this.messageBuffer);
181
+ this.connectionManager.sendMessages(this.messageBuffer);
182
+ this.messageBuffer = [];
183
+ }
184
+ get connectionProps() {
185
+ return Object.assign({ sequenceNumber: this.lastSequenceNumber }, this.connectionManager.connectionProps);
389
186
  }
390
187
  /**
391
188
  * Log error event with a bunch of internal to DeltaManager information about state of op processing
@@ -395,20 +192,46 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
395
192
  */
396
193
  logConnectionIssue(event) {
397
194
  var _a;
398
- common_utils_1.assert(this.connection !== undefined, 0x238 /* "called only in connected state" */);
195
+ common_utils_1.assert(this.connectionManager.connected, 0x238 /* "called only in connected state" */);
399
196
  const pendingSorted = this.pending.sort((a, b) => a.sequenceNumber - b.sequenceNumber);
400
197
  this.logger.sendErrorEvent(Object.assign(Object.assign(Object.assign(Object.assign({}, event), {
401
198
  // This directly tells us if fetching ops is in flight, and thus likely the reason of
402
199
  // stalled op processing
403
200
  fetchReason: this.fetchReason,
404
201
  // A bunch of useful sequence numbers to understand if we are holding some ops from processing
405
- lastQueuedSequenceNumber: this.lastQueuedSequenceNumber, lastProcessedSequenceNumber: this.lastProcessedSequenceNumber, lastObserved: this.lastObservedSeqNumber }), this.connectionStateProps), { pendingOps: this.pending.length, pendingFirst: (_a = pendingSorted[0]) === null || _a === void 0 ? void 0 : _a.sequenceNumber, haveHandler: this.handler !== undefined, inboundLength: this.inbound.length, inboundPaused: this.inbound.paused }));
202
+ lastQueuedSequenceNumber: this.lastQueuedSequenceNumber, lastProcessedSequenceNumber: this.lastProcessedSequenceNumber, lastObserved: this.lastObservedSeqNumber }), this.connectionManager.connectionVerboseProps), { pendingOps: this.pending.length, pendingFirst: (_a = pendingSorted[0]) === null || _a === void 0 ? void 0 : _a.sequenceNumber, haveHandler: this.handler !== undefined, inboundLength: this.inbound.length, inboundPaused: this.inbound.paused }));
406
203
  }
407
- set_readonlyPermissions(readonly) {
408
- const oldValue = this.readOnlyInfo.readonly;
409
- this._readonlyPermissions = readonly;
410
- if (oldValue !== this.readOnlyInfo.readonly) {
411
- telemetry_utils_1.safeRaiseEvent(this, this.logger, "readonly", this.readOnlyInfo.readonly);
204
+ connectHandler(connection) {
205
+ this.refreshDelayInfo(this.deltaStreamDelayId);
206
+ const props = this.connectionManager.connectionVerboseProps;
207
+ props.connectionLastQueuedSequenceNumber = this.lastQueuedSequenceNumber;
208
+ props.connectionLastObservedSeqNumber = this.lastObservedSeqNumber;
209
+ const checkpointSequenceNumber = connection.checkpointSequenceNumber;
210
+ this._checkpointSequenceNumber = checkpointSequenceNumber;
211
+ if (checkpointSequenceNumber !== undefined) {
212
+ this.updateLatestKnownOpSeqNumber(checkpointSequenceNumber);
213
+ }
214
+ // We cancel all ops on lost of connectivity, and rely on DDSes to resubmit them.
215
+ // Semantics are not well defined for batches (and they are broken right now on disconnects anyway),
216
+ // but it's safe to assume (until better design is put into place) that batches should not exist
217
+ // across multiple connections. Right now we assume runtime will not submit any ops in disconnected
218
+ // state. As requirements change, so should these checks.
219
+ common_utils_1.assert(this.messageBuffer.length === 0, 0x0e9 /* "messageBuffer is not empty on new connection" */);
220
+ this.emit("connect", connection, checkpointSequenceNumber !== undefined ?
221
+ this.lastObservedSeqNumber - this.lastSequenceNumber : undefined);
222
+ // If we got some initial ops, then we know the gap and call above fetched ops to fill it.
223
+ // Same is true for "write" mode even if we have no ops - we will get "join" own op very very soon.
224
+ // However if we are connecting as view-only, then there is no good signal to realize if client is behind.
225
+ // Thus we have to hit storage to see if any ops are there.
226
+ if (checkpointSequenceNumber !== undefined) {
227
+ // We know how far we are behind (roughly). If it's non-zero gap, fetch ops right away.
228
+ if (checkpointSequenceNumber > this.lastQueuedSequenceNumber) {
229
+ this.fetchMissingDeltas("AfterConnection");
230
+ }
231
+ // we do not know the gap, and we will not learn about it if socket is quite - have to ask.
232
+ }
233
+ else if (connection.mode === "read") {
234
+ this.fetchMissingDeltas("AfterReadConnection");
412
235
  }
413
236
  }
414
237
  dispose() {
@@ -449,65 +272,16 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
449
272
  // (which in most cases will happen when we are done processing cached ops)
450
273
  if (cacheOnly) {
451
274
  // fire and forget
452
- this.fetchMissingDeltas("DocumentOpen", this.lastQueuedSequenceNumber);
275
+ this.fetchMissingDeltas("DocumentOpen");
453
276
  }
454
277
  }
455
278
  // Ensure there is no need to call this.processPendingOps() at the end of boot sequence
456
279
  common_utils_1.assert(this.fetchReason !== undefined || this.pending.length === 0, 0x269 /* "pending ops are not dropped" */);
457
280
  }
458
- static detailsFromConnection(connection) {
459
- return {
460
- claims: connection.claims,
461
- clientId: connection.clientId,
462
- existing: connection.existing,
463
- checkpointSequenceNumber: connection.checkpointSequenceNumber,
464
- get initialClients() { return connection.initialClients; },
465
- mode: connection.mode,
466
- serviceConfiguration: connection.serviceConfiguration,
467
- version: connection.version,
468
- };
469
- }
470
- async connect(args) {
471
- const connection = await this.connectCore(args);
472
- return DeltaManager.detailsFromConnection(connection);
473
- }
474
- /**
475
- * Start the connection. Any error should result in container being close.
476
- * And report the error if it excape for any reason.
477
- * @param args - The connection arguments
478
- */
479
- triggerConnect(args) {
480
- common_utils_1.assert(this.connection === undefined, 0x239 /* "called only in disconnected state" */);
481
- if (this.reconnectMode !== ReconnectMode.Enabled) {
482
- return;
483
- }
484
- this.connectCore(args).catch((err) => {
485
- // Errors are raised as "error" event and close container.
486
- // Have a catch-all case in case we missed something
487
- if (!this.closed) {
488
- this.logger.sendErrorEvent({ eventName: "ConnectException" }, err);
489
- }
490
- });
491
- }
492
- async connectCore(args) {
493
- var _a, _b, _c;
494
- common_utils_1.assert(!this.closed, 0x26a /* "not closed" */);
495
- if (this.connection !== undefined) {
496
- return this.connection;
497
- }
498
- if (this.connectionP !== undefined) {
499
- return this.connectionP;
500
- }
281
+ connect(args) {
282
+ var _a;
501
283
  const fetchOpsFromStorage = (_a = args.fetchOpsFromStorage) !== null && _a !== void 0 ? _a : true;
502
- let requestedMode = (_b = args.mode) !== null && _b !== void 0 ? _b : this.defaultReconnectionMode;
503
- // if we have any non-acked ops from last connection, reconnect as "write".
504
- // without that we would connect in view-only mode, which will result in immediate
505
- // firing of "connected" event from Container and switch of current clientId (as tracked
506
- // by all DDSes). This will make it impossible to figure out if ops actually made it through,
507
- // so DDSes will immediately resubmit all pending ops, and some of them will be duplicates, corrupting document
508
- if (this.shouldJoinWrite()) {
509
- requestedMode = "write";
510
- }
284
+ telemetry_utils_1.logIfFalse(this.handler !== undefined || !fetchOpsFromStorage, this.logger, "CantFetchWithoutBaseline"); // can't fetch if no baseline
511
285
  // Note: There is race condition here.
512
286
  // We want to issue request to storage as soon as possible, to
513
287
  // reduce latency of becoming current, thus this code here.
@@ -517,213 +291,11 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
517
291
  // own "join" message and realize any gap client has in ops.
518
292
  // But for view-only connection, we have no such signal, and with no traffic
519
293
  // on the wire, we might be always behind.
520
- // See comment at the end of setupNewSuccessfulConnection()
521
- telemetry_utils_1.logIfFalse(this.handler !== undefined || !fetchOpsFromStorage, this.logger, "CantFetchWithoutBaseline"); // can't fetch if no baseline
294
+ // See comment at the end of "connect" handler
522
295
  if (fetchOpsFromStorage) {
523
- this.fetchMissingDeltas(args.reason, this.lastQueuedSequenceNumber);
524
- }
525
- const docService = this.serviceProvider();
526
- common_utils_1.assert(docService !== undefined, 0x2a7 /* "Container is not attached" */);
527
- if (((_c = docService.policies) === null || _c === void 0 ? void 0 : _c.storageOnly) === true) {
528
- const connection = new NoDeltaStream();
529
- this.connectionP = Promise.resolve(connection); // to keep setupNewSuccessfulConnection happy
530
- this.setupNewSuccessfulConnection(connection, "read");
531
- return connection;
532
- }
533
- // The promise returned from connectCore will settle with a resolved connection or reject with error
534
- const connectCore = async () => {
535
- let connection;
536
- let delayMs = InitialReconnectDelayInMs;
537
- let connectRepeatCount = 0;
538
- const connectStartTime = common_utils_1.performance.now();
539
- let lastError;
540
- // This loop will keep trying to connect until successful, with a delay between each iteration.
541
- while (connection === undefined) {
542
- if (this.closed) {
543
- throw new Error("Attempting to connect a closed DeltaManager");
544
- }
545
- connectRepeatCount++;
546
- try {
547
- this.client.mode = requestedMode;
548
- connection = await docService.connectToDeltaStream(this.client);
549
- if (connection.disposed) {
550
- // Nobody observed this connection, so drop it on the floor and retry.
551
- this.logger.sendTelemetryEvent({ eventName: "ReceivedClosedConnection" });
552
- connection = undefined;
553
- }
554
- }
555
- catch (origError) {
556
- if (typeof origError === "object" && origError !== null &&
557
- (origError === null || origError === void 0 ? void 0 : origError.errorType) === driver_utils_1.DeltaStreamConnectionForbiddenError.errorType) {
558
- connection = new NoDeltaStream();
559
- requestedMode = "read";
560
- break;
561
- }
562
- // Socket.io error when we connect to wrong socket, or hit some multiplexing bug
563
- if (!driver_utils_1.canRetryOnError(origError)) {
564
- const error = telemetry_utils_1.normalizeError(origError, { props: fatalConnectErrorProp });
565
- this.close(error);
566
- throw error;
567
- }
568
- // Log error once - we get too many errors in logs when we are offline,
569
- // and unfortunately there is no reliable way to detect that.
570
- if (connectRepeatCount === 1) {
571
- driver_utils_1.logNetworkFailure(this.logger, {
572
- delay: delayMs,
573
- eventName: "DeltaConnectionFailureToConnect",
574
- duration: telemetry_utils_1.TelemetryLogger.formatTick(common_utils_1.performance.now() - connectStartTime),
575
- }, origError);
576
- }
577
- lastError = origError;
578
- const retryDelayFromError = driver_utils_1.getRetryDelayFromError(origError);
579
- delayMs = retryDelayFromError !== null && retryDelayFromError !== void 0 ? retryDelayFromError : Math.min(delayMs * 2, MaxReconnectDelayInMs);
580
- if (retryDelayFromError !== undefined) {
581
- this.emitDelayInfo(this.deltaStreamDelayId, retryDelayFromError, origError);
582
- }
583
- await driver_utils_1.waitForConnectedState(delayMs);
584
- }
585
- }
586
- // If we retried more than once, log an event about how long it took
587
- if (connectRepeatCount > 1) {
588
- this.logger.sendTelemetryEvent({
589
- eventName: "MultipleDeltaConnectionFailures",
590
- attempts: connectRepeatCount,
591
- duration: telemetry_utils_1.TelemetryLogger.formatTick(common_utils_1.performance.now() - connectStartTime),
592
- }, lastError);
593
- }
594
- this.setupNewSuccessfulConnection(connection, requestedMode);
595
- return connection;
596
- };
597
- // This promise settles as soon as we know the outcome of the connection attempt
598
- // Set it upfront, such that if connection is established (NoDeltaConnection) or rejected (bug in
599
- // connectToDeltaStream() implementation - throwing exception vs. returning rejected promise) in
600
- // synchronous way, we have this.connectionP setup for all the code to assert correctness of the flow.
601
- const deferred = new common_utils_1.Deferred();
602
- this.connectionP = deferred.promise;
603
- // Regardless of how the connection attempt concludes, we'll clear the promise and remove the listener
604
- // Reject the connection promise if the DeltaManager gets closed during connection
605
- const cleanupAndReject = (error) => {
606
- this.connectionP = undefined;
607
- this.removeListener("closed", cleanupAndReject);
608
- // This error came from some logic error in this file. Fail-fast to learn and fix the issue faster
609
- const normalizedError = telemetry_utils_1.normalizeError(error, { props: fatalConnectErrorProp });
610
- this.close(normalizedError);
611
- deferred.reject(normalizedError);
612
- };
613
- this.on("closed", cleanupAndReject);
614
- // Attempt the connection
615
- connectCore().then((connection) => {
616
- this.removeListener("closed", cleanupAndReject);
617
- deferred.resolve(connection);
618
- }).catch(cleanupAndReject);
619
- return this.connectionP;
620
- }
621
- flush() {
622
- if (this.messageBuffer.length === 0) {
623
- return;
624
- }
625
- // The prepareFlush event allows listeners to append metadata to the batch prior to submission.
626
- this.emit("prepareSend", this.messageBuffer);
627
- this._outbound.push(this.messageBuffer);
628
- this.messageBuffer = [];
629
- }
630
- /**
631
- * Submits the given delta returning the client sequence number for the message. Contents is the actual
632
- * contents of the message. appData is optional metadata that can be attached to the op by the app.
633
- *
634
- * If batch is set to true then the submit will be batched - and as a result guaranteed to be ordered sequentially
635
- * in the global sequencing space. The batch will be flushed either when flush is called or when a non-batched
636
- * op is submitted.
637
- */
638
- submit(type, contents, batch = false, metadata) {
639
- // TODO need to fail if gets too large
640
- // const serializedContent = JSON.stringify(this.messageBuffer);
641
- // const maxOpSize = this.context.deltaManager.maxMessageSize;
642
- var _a, _b;
643
- if (this.readOnlyInfo.readonly === true) {
644
- common_utils_1.assert(this.readOnlyInfo.readonly === true, 0x1f0 /* "Unexpected mismatch in readonly" */);
645
- const error = new container_utils_1.GenericError("deltaManagerReadonlySubmit", undefined /* error */, {
646
- readonly: this.readOnlyInfo.readonly,
647
- forcedReadonly: this.readOnlyInfo.forced,
648
- readonlyPermissions: this.readOnlyInfo.permissions,
649
- storageOnly: this.readOnlyInfo.storageOnly,
650
- });
651
- this.close(error);
652
- return -1;
653
- }
654
- // reset clientSequenceNumber if we are using new clientId.
655
- // we keep info about old connection as long as possible to be able to account for all non-acked ops
656
- // that we pick up on next connection.
657
- common_utils_1.assert(!!this.connection, 0x0e4 /* "Lost old connection!" */);
658
- if (this.lastSubmittedClientId !== ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.clientId)) {
659
- this.lastSubmittedClientId = (_b = this.connection) === null || _b === void 0 ? void 0 : _b.clientId;
660
- this.clientSequenceNumber = 0;
661
- this.clientSequenceNumberObserved = 0;
662
- }
663
- // If connection is "read" or implicit "read" (got leave op for "write" connection),
664
- // then op can't make it through - we will get a nack if op is sent.
665
- // We can short-circuit this process.
666
- // Note that we also want nacks to be rare and be treated as catastrophic failures.
667
- // Be careful with reentrancy though - disconnected event should not be be raised in the
668
- // middle of the current workflow, but rather on clean stack!
669
- if (this.connectionMode === "read" || this.downgradedConnection) {
670
- if (!this.pendingReconnect) {
671
- this.pendingReconnect = true;
672
- Promise.resolve().then(async () => {
673
- if (this.pendingReconnect) { // still valid?
674
- return this.reconnectOnErrorCore("write", // connectionMode
675
- "Switch to write");
676
- }
677
- })
678
- .catch(() => { });
679
- }
680
- // Can return -1 here, but no other path does it (other than error path in Container),
681
- // so it's better not to introduce new states.
682
- return ++this.clientSequenceNumber;
683
- }
684
- const service = this.clientDetails.type === undefined || this.clientDetails.type === ""
685
- ? "unknown"
686
- : this.clientDetails.type;
687
- // Start adding trace for the op.
688
- const traces = [
689
- {
690
- action: "start",
691
- service,
692
- timestamp: Date.now(),
693
- }
694
- ];
695
- const message = {
696
- clientSequenceNumber: ++this.clientSequenceNumber,
697
- contents: JSON.stringify(contents),
698
- metadata,
699
- referenceSequenceNumber: this.lastProcessedSequenceNumber,
700
- traces,
701
- type,
702
- };
703
- if (type === protocol_definitions_1.MessageType.NoOp) {
704
- this.trailingNoopCount++;
705
- }
706
- else {
707
- this.trailingNoopCount = 0;
708
- }
709
- this.emit("submitOp", message);
710
- if (!batch) {
711
- this.flush();
712
- this.messageBuffer.push(message);
713
- this.flush();
714
- }
715
- else {
716
- this.messageBuffer.push(message);
717
- }
718
- return message.clientSequenceNumber;
719
- }
720
- submitSignal(content) {
721
- if (this.connection !== undefined) {
722
- this.connection.submitSignal(content);
723
- }
724
- else {
725
- this.logger.sendErrorEvent({ eventName: "submitSignalDisconnected" });
296
+ this.fetchMissingDeltas(args.reason);
726
297
  }
298
+ this.connectionManager.connect(args.mode);
727
299
  }
728
300
  async getDeltas(from, // inclusive
729
301
  to, // exclusive
@@ -735,9 +307,6 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
735
307
  if (this.deltaStorage === undefined) {
736
308
  this.deltaStorage = await docService.connectToDeltaStorage();
737
309
  }
738
- common_utils_1.assert(this.closeAbortController.signal.onabort === null, 0x1e8 /* "reentrancy" */);
739
- const controller = new abort_controller_1.default();
740
- this.closeAbortController.signal.onabort = () => controller.abort();
741
310
  let cancelFetch;
742
311
  if (to !== undefined) {
743
312
  const lastExpectedOp = to - 1; // make it inclusive!
@@ -745,7 +314,7 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
745
314
  // received through delta stream. Validate that before moving forward.
746
315
  if (this.lastQueuedSequenceNumber >= lastExpectedOp) {
747
316
  this.logger.sendPerformanceEvent(Object.assign({ reason: this.fetchReason, eventName: "ExtraStorageCall", early: true, from,
748
- to }, this.connectionStateProps));
317
+ to }, this.connectionManager.connectionVerboseProps));
749
318
  return;
750
319
  }
751
320
  // Be prepared for the case where webSocket would receive the ops that we are trying to fill through
@@ -762,6 +331,7 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
762
331
  // That said, if we have socket connection, make sure we got ops up to checkpointSequenceNumber!
763
332
  cancelFetch = (op) => op.sequenceNumber >= this.lastObservedSeqNumber;
764
333
  }
334
+ const controller = new abort_controller_1.default();
765
335
  let opsFromFetch = false;
766
336
  const opListener = (op) => {
767
337
  common_utils_1.assert(op.sequenceNumber === this.lastQueuedSequenceNumber, 0x23a /* "seq#'s" */);
@@ -773,8 +343,10 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
773
343
  this._inbound.off("push", opListener);
774
344
  }
775
345
  };
776
- this._inbound.on("push", opListener);
777
346
  try {
347
+ this._inbound.on("push", opListener);
348
+ common_utils_1.assert(this.closeAbortController.signal.onabort === null, 0x1e8 /* "reentrancy" */);
349
+ this.closeAbortController.signal.onabort = () => controller.abort();
778
350
  const stream = this.deltaStorage.fetchMessages(from, // inclusive
779
351
  to, // exclusive
780
352
  controller.signal, cacheOnly, this.fetchReason);
@@ -794,9 +366,9 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
794
366
  }
795
367
  }
796
368
  finally {
797
- common_utils_1.assert(!opsFromFetch, 0x289 /* "logic error" */);
798
369
  this.closeAbortController.signal.onabort = null;
799
370
  this._inbound.off("push", opListener);
371
+ common_utils_1.assert(!opsFromFetch, 0x289 /* "logic error" */);
800
372
  }
801
373
  }
802
374
  /**
@@ -807,16 +379,9 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
807
379
  return;
808
380
  }
809
381
  this.closed = true;
810
- // Ensure that things like triggerConnect() will short circuit
811
- this._reconnectMode = ReconnectMode.Never;
382
+ this.connectionManager.dispose(error);
812
383
  this.closeAbortController.abort();
813
- const disconnectReason = error !== undefined
814
- ? `Closing DeltaManager (${error.message})`
815
- : "Closing DeltaManager";
816
- // This raises "disconnect" event if we have active connection.
817
- this.disconnectFromDeltaStream(disconnectReason);
818
384
  this._inbound.clear();
819
- this._outbound.clear();
820
385
  this._inboundSignal.clear();
821
386
  // eslint-disable-next-line @typescript-eslint/no-floating-promises
822
387
  this._inbound.pause();
@@ -824,10 +389,6 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
824
389
  this._inboundSignal.pause();
825
390
  // Drop pending messages - this will ensure catchUp() does not go into infinite loop
826
391
  this.pending = [];
827
- // Notify everyone we are in read-only state.
828
- // Useful for data stores in case we hit some critical error,
829
- // to switch to a mode where user edits are not accepted
830
- this.set_readonlyPermissions(true);
831
392
  // This needs to be the last thing we do (before removing listeners), as it causes
832
393
  // Container to dispose context and break ability of data stores / runtime to "hear"
833
394
  // from delta manager, including notification (above) about readonly state.
@@ -840,6 +401,20 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
840
401
  this.timeTillThrottling = 0;
841
402
  }
842
403
  }
404
+ disconnectHandler(reason) {
405
+ if (this.messageBuffer.length > 0) {
406
+ // Behavior is not well defined here RE batches across connections / disconnect.
407
+ // DeltaManager overall policy - drop all ops on disconnection and rely on
408
+ // container runtime to deal with resubmitting any ops that did not make it through.
409
+ // So drop them, but also raise error event to look into details.
410
+ this.logger.sendErrorEvent({
411
+ eventName: "OpenBatchOnDisconnect",
412
+ length: this.messageBuffer.length,
413
+ });
414
+ this.messageBuffer.length = 0;
415
+ }
416
+ this.emit("disconnect", reason);
417
+ }
843
418
  /**
844
419
  * Emit info about a delay in service communication on account of throttling.
845
420
  * @param id - Id of the connection that is delayed
@@ -855,183 +430,6 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
855
430
  this.emit("throttled", throttlingWarning);
856
431
  }
857
432
  }
858
- /**
859
- * Once we've successfully gotten a connection, we need to set up state, attach event listeners, and process
860
- * initial messages.
861
- * @param connection - The newly established connection
862
- */
863
- setupNewSuccessfulConnection(connection, requestedMode) {
864
- // Old connection should have been cleaned up before establishing a new one
865
- common_utils_1.assert(this.connection === undefined, 0x0e6 /* "old connection exists on new connection setup" */);
866
- common_utils_1.assert(this.connectionP !== undefined || this.closed, 0x27f /* "reentrancy may result in incorrect behavior" */);
867
- common_utils_1.assert(!connection.disposed, 0x28a /* "can't be disposed - Callers need to ensure that!" */);
868
- this.connectionP = undefined;
869
- this.connection = connection;
870
- // Does information in scopes & mode matches?
871
- // If we asked for "write" and got "read", then file is read-only
872
- // But if we ask read, server can still give us write.
873
- const readonly = !connection.claims.scopes.includes(protocol_definitions_1.ScopeType.DocWrite);
874
- // This connection mode validation logic is moving to the driver layer in 0.44. These two asserts can be
875
- // removed after those packages have released and become ubiquitous.
876
- common_utils_1.assert(requestedMode === "read" || readonly === (this.connectionMode === "read"), 0x0e7 /* "claims/connectionMode mismatch" */);
877
- common_utils_1.assert(!readonly || this.connectionMode === "read", 0x0e8 /* "readonly perf with write connection" */);
878
- this.set_readonlyPermissions(readonly);
879
- this.refreshDelayInfo(this.deltaStreamDelayId);
880
- if (this.closed) {
881
- // Raise proper events, Log telemetry event and close connection.
882
- this.disconnectFromDeltaStream("DeltaManager already closed");
883
- return;
884
- }
885
- // We cancel all ops on lost of connectivity, and rely on DDSes to resubmit them.
886
- // Semantics are not well defined for batches (and they are broken right now on disconnects anyway),
887
- // but it's safe to assume (until better design is put into place) that batches should not exist
888
- // across multiple connections. Right now we assume runtime will not submit any ops in disconnected
889
- // state. As requirements change, so should these checks.
890
- common_utils_1.assert(this.messageBuffer.length === 0, 0x0e9 /* "messageBuffer is not empty on new connection" */);
891
- this._outbound.resume();
892
- connection.on("op", this.opHandler);
893
- connection.on("signal", this.signalHandler);
894
- connection.on("nack", this.nackHandler);
895
- connection.on("disconnect", this.disconnectHandler);
896
- connection.on("error", this.errorHandler);
897
- connection.on("pong", this.pongHandler);
898
- // Initial messages are always sorted. However, due to early op handler installed by drivers and appending those
899
- // ops to initialMessages, resulting set is no longer sorted, which would result in client hitting storage to
900
- // fill in gap. We will recover by cancelling this request once we process remaining ops, but it's a waste that
901
- // we could avoid
902
- const initialMessages = connection.initialMessages.sort((a, b) => a.sequenceNumber - b.sequenceNumber);
903
- this.connectionStateProps = {
904
- connectionLastQueuedSequenceNumber: this.lastQueuedSequenceNumber,
905
- connectionLastObservedSeqNumber: this.lastObservedSeqNumber,
906
- clientId: connection.clientId,
907
- mode: connection.mode,
908
- };
909
- if (connection.relayServiceAgent !== undefined) {
910
- this.connectionStateProps.relayServiceAgent = connection.relayServiceAgent;
911
- }
912
- this._hasCheckpointSequenceNumber = false;
913
- // Some storages may provide checkpointSequenceNumber to identify how far client is behind.
914
- const checkpointSequenceNumber = connection.checkpointSequenceNumber;
915
- if (checkpointSequenceNumber !== undefined) {
916
- this._hasCheckpointSequenceNumber = true;
917
- this.updateLatestKnownOpSeqNumber(checkpointSequenceNumber);
918
- }
919
- // Update knowledge of how far we are behind, before raising "connect" event
920
- // This is duplication of what enqueueMessages() does, but we have to raise event before we get there,
921
- // so duplicating update logic here as well.
922
- const last = initialMessages.length > 0 ? initialMessages[initialMessages.length - 1].sequenceNumber : -1;
923
- if (initialMessages.length > 0) {
924
- this._hasCheckpointSequenceNumber = true;
925
- this.updateLatestKnownOpSeqNumber(last);
926
- }
927
- // Notify of the connection
928
- // WARNING: This has to happen before processInitialMessages() call below.
929
- // If not, we may not update Container.pendingClientId in time before seeing our own join session op.
930
- this.emit("connect", DeltaManager.detailsFromConnection(connection), this._hasCheckpointSequenceNumber ? this.lastObservedSeqNumber - this.lastSequenceNumber : undefined);
931
- this.enqueueMessages(initialMessages, this.connectFirstConnection ? "InitialOps" : "ReconnectOps");
932
- if (connection.initialSignals !== undefined) {
933
- for (const signal of connection.initialSignals) {
934
- this._inboundSignal.push(signal);
935
- }
936
- }
937
- // If we got some initial ops, then we know the gap and call above fetched ops to fill it.
938
- // Same is true for "write" mode even if we have no ops - we will get self "join" ops very very soon.
939
- // However if we are connecting as view-only, then there is no good signal to realize if client is behind.
940
- // Thus we have to hit storage to see if any ops are there.
941
- if (initialMessages.length === 0) {
942
- if (checkpointSequenceNumber !== undefined) {
943
- // We know how far we are behind (roughly). If it's non-zero gap, fetch ops right away.
944
- if (checkpointSequenceNumber > this.lastQueuedSequenceNumber) {
945
- this.fetchMissingDeltas("AfterConnection", this.lastQueuedSequenceNumber);
946
- }
947
- // we do not know the gap, and we will not learn about it if socket is quite - have to ask.
948
- }
949
- else if (connection.mode === "read") {
950
- this.fetchMissingDeltas("AfterReadConnection", this.lastQueuedSequenceNumber);
951
- }
952
- }
953
- else {
954
- this.connectionStateProps.connectionInitialOpsFrom = initialMessages[0].sequenceNumber;
955
- this.connectionStateProps.connectionInitialOpsTo = last + 1;
956
- }
957
- this.connectFirstConnection = false;
958
- }
959
- /**
960
- * Disconnect the current connection.
961
- * @param reason - Text description of disconnect reason to emit with disconnect event
962
- */
963
- disconnectFromDeltaStream(reason) {
964
- this.pendingReconnect = false;
965
- this.downgradedConnection = false;
966
- if (this.connection === undefined) {
967
- return false;
968
- }
969
- common_utils_1.assert(this.connectionP === undefined, 0x27b /* "reentrancy may result in incorrect behavior" */);
970
- const connection = this.connection;
971
- // Avoid any re-entrancy - clear object reference
972
- this.connection = undefined;
973
- // Remove listeners first so we don't try to retrigger this flow accidentally through reconnectOnError
974
- connection.off("op", this.opHandler);
975
- connection.off("signal", this.signalHandler);
976
- connection.off("nack", this.nackHandler);
977
- connection.off("disconnect", this.disconnectHandler);
978
- connection.off("error", this.errorHandler);
979
- connection.off("pong", this.pongHandler);
980
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
981
- this._outbound.pause();
982
- this._outbound.clear();
983
- this.emit("disconnect", reason);
984
- connection.dispose();
985
- this.connectionStateProps = {};
986
- return true;
987
- }
988
- /**
989
- * Disconnect the current connection and reconnect.
990
- * @param connection - The connection that wants to reconnect - no-op if it's different from this.connection
991
- * @param requestedMode - Read or write
992
- * @param error - Error reconnect information including whether or not to reconnect
993
- * @returns A promise that resolves when the connection is reestablished or we stop trying
994
- */
995
- async reconnectOnError(requestedMode, error) {
996
- return this.reconnectOnErrorCore(requestedMode, error.message, error);
997
- }
998
- /**
999
- * Disconnect the current connection and reconnect.
1000
- * @param connection - The connection that wants to reconnect - no-op if it's different from this.connection
1001
- * @param requestedMode - Read or write
1002
- * @param error - Error reconnect information including whether or not to reconnect
1003
- * @returns A promise that resolves when the connection is reestablished or we stop trying
1004
- */
1005
- async reconnectOnErrorCore(requestedMode, disconnectMessage, error) {
1006
- // We quite often get protocol errors before / after observing nack/disconnect
1007
- // we do not want to run through same sequence twice.
1008
- // If we're already disconnected/disconnecting it's not appropriate to call this again.
1009
- common_utils_1.assert(this.connection !== undefined, 0x0eb /* "Missing connection for reconnect" */);
1010
- this.disconnectFromDeltaStream(disconnectMessage);
1011
- const canRetry = error !== undefined ? driver_utils_1.canRetryOnError(error) : true;
1012
- // If reconnection is not an option, close the DeltaManager
1013
- if (!canRetry) {
1014
- this.close(telemetry_utils_1.normalizeError(error, { props: fatalConnectErrorProp }));
1015
- }
1016
- else if (this.reconnectMode === ReconnectMode.Never) {
1017
- // Do not raise container error if we are closing just because we lost connection.
1018
- // Those errors (like IdleDisconnect) would show up in telemetry dashboards and
1019
- // are very misleading, as first initial reaction - some logic is broken.
1020
- this.close();
1021
- }
1022
- // If closed then we can't reconnect
1023
- if (this.closed) {
1024
- return;
1025
- }
1026
- if (this.reconnectMode === ReconnectMode.Enabled) {
1027
- const delayMs = error !== undefined ? driver_utils_1.getRetryDelayFromError(error) : undefined;
1028
- if (delayMs !== undefined) {
1029
- this.emitDelayInfo(this.deltaStreamDelayId, delayMs, error);
1030
- await driver_utils_1.waitForConnectedState(delayMs);
1031
- }
1032
- this.triggerConnect({ reason: "reconnect", mode: requestedMode, fetchOpsFromStorage: false });
1033
- }
1034
- }
1035
433
  // returns parts of message (in string format) that should never change for a given message.
1036
434
  // Used for message comparison. It attempts to avoid comparing fields that potentially may differ.
1037
435
  // for example, it's not clear if serverMetadata or timestamp property is a property of message or server state.
@@ -1043,7 +441,7 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
1043
441
  return `${m.clientId}-${m.type}-${m.minimumSequenceNumber}-${m.referenceSequenceNumber}-${m.timestamp}`;
1044
442
  }
1045
443
  enqueueMessages(messages, reason, allowGaps = false) {
1046
- var _a, _b, _c;
444
+ var _a, _b;
1047
445
  if (this.handler === undefined) {
1048
446
  // We did not setup handler yet.
1049
447
  // This happens when we connect to web socket faster than we get attributes for container
@@ -1103,7 +501,7 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
1103
501
  // correctly take into account pending ops.
1104
502
  if (eventName !== undefined) {
1105
503
  this.logger.sendPerformanceEvent(Object.assign({ eventName,
1106
- reason, previousReason: this.prevEnqueueMessagesReason, from, to: last + 1, length: messages.length, fetchReason: this.fetchReason, duplicate: duplicate > 0 ? duplicate : undefined, initialGap: initialGap !== 0 ? initialGap : undefined, gap: gap > 0 ? gap : undefined, firstMissing, dmInitialSeqNumber: this.initialSequenceNumber }, this.connectionStateProps));
504
+ reason, previousReason: this.prevEnqueueMessagesReason, from, to: last + 1, length: messages.length, fetchReason: this.fetchReason, duplicate: duplicate > 0 ? duplicate : undefined, initialGap: initialGap !== 0 ? initialGap : undefined, gap: gap > 0 ? gap : undefined, firstMissing, dmInitialSeqNumber: this.initialSequenceNumber }, this.connectionManager.connectionVerboseProps));
1107
505
  }
1108
506
  }
1109
507
  this.updateLatestKnownOpSeqNumber(messages[messages.length - 1].sequenceNumber);
@@ -1119,7 +517,7 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
1119
517
  const message2 = this.comparableMessagePayload(message);
1120
518
  if (message1 !== message2) {
1121
519
  const error = new driver_utils_1.NonRetryableError("twoMessagesWithSameSeqNumAndDifferentPayload", undefined, driver_definitions_1.DriverErrorType.fileOverwrittenInStorage, {
1122
- clientId: (_c = this.connection) === null || _c === void 0 ? void 0 : _c.clientId,
520
+ clientId: this.connectionManager.clientId,
1123
521
  sequenceNumber: message.sequenceNumber,
1124
522
  message1,
1125
523
  message2,
@@ -1130,7 +528,7 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
1130
528
  }
1131
529
  else if (message.sequenceNumber !== this.lastQueuedSequenceNumber + 1) {
1132
530
  this.pending.push(message);
1133
- this.fetchMissingDeltas(reason, this.lastQueuedSequenceNumber, message.sequenceNumber);
531
+ this.fetchMissingDeltas(reason, message.sequenceNumber);
1134
532
  }
1135
533
  else {
1136
534
  this.lastQueuedSequenceNumber = message.sequenceNumber;
@@ -1144,56 +542,34 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
1144
542
  this.prevEnqueueMessagesReason = this.pending.length > 0 ? "unknown" : reason;
1145
543
  }
1146
544
  processInboundMessage(message) {
1147
- var _a, _b, _c;
1148
545
  const startTime = Date.now();
1149
546
  this.lastProcessedMessage = message;
1150
547
  // All non-system messages are coming from some client, and should have clientId
1151
548
  // System messages may have no clientId (but some do, like propose, noop, summarize)
1152
549
  common_utils_1.assert(message.clientId !== undefined
1153
550
  || protocol_base_1.isSystemMessage(message), 0x0ed /* "non-system message have to have clientId" */);
1154
- // if we have connection, and message is local, then we better treat is as local!
1155
- common_utils_1.assert(this.connection === undefined
1156
- || this.connection.clientId !== message.clientId
1157
- || this.lastSubmittedClientId === message.clientId, 0x0ee /* "Not accounting local messages correctly" */);
1158
- if (this.lastSubmittedClientId !== undefined && this.lastSubmittedClientId === message.clientId) {
1159
- const clientSequenceNumber = message.clientSequenceNumber;
1160
- common_utils_1.assert(this.clientSequenceNumberObserved < clientSequenceNumber, 0x0ef /* "client seq# not growing" */);
1161
- common_utils_1.assert(clientSequenceNumber <= this.clientSequenceNumber, 0x0f0 /* "Incoming local client seq# > generated by this client" */);
1162
- this.clientSequenceNumberObserved = clientSequenceNumber;
1163
- }
1164
551
  // TODO Remove after SPO picks up the latest build.
1165
552
  if (typeof message.contents === "string"
1166
553
  && message.contents !== ""
1167
554
  && message.type !== protocol_definitions_1.MessageType.ClientLeave) {
1168
555
  message.contents = JSON.parse(message.contents);
1169
556
  }
1170
- if (message.type === protocol_definitions_1.MessageType.ClientLeave) {
1171
- const systemLeaveMessage = message;
1172
- const clientId = JSON.parse(systemLeaveMessage.data);
1173
- if (clientId === ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.clientId)) {
1174
- // We have been kicked out from quorum
1175
- this.logger.sendPerformanceEvent({ eventName: "ReadConnectionTransition" });
1176
- this.downgradedConnection = true;
1177
- }
1178
- }
557
+ this.connectionManager.beforeProcessingIncomingOp(message);
1179
558
  // Add final ack trace.
1180
559
  if (message.traces !== undefined && message.traces.length > 0) {
1181
- const service = this.clientDetails.type === undefined || this.clientDetails.type === ""
1182
- ? "unknown"
1183
- : this.clientDetails.type;
1184
560
  message.traces.push({
1185
561
  action: "end",
1186
- service,
562
+ service: "client",
1187
563
  timestamp: Date.now(),
1188
564
  });
1189
565
  }
1190
566
  // Watch the minimum sequence number and be ready to update as needed
1191
567
  if (this.minSequenceNumber > message.minimumSequenceNumber) {
1192
- throw new container_utils_1.DataCorruptionError("msnMovesBackwards", Object.assign(Object.assign({}, extractLogSafeMessageProperties(message)), { clientId: (_b = this.connection) === null || _b === void 0 ? void 0 : _b.clientId }));
568
+ throw new container_utils_1.DataCorruptionError("msnMovesBackwards", Object.assign(Object.assign({}, extractLogSafeMessageProperties(message)), { clientId: this.connectionManager.clientId }));
1193
569
  }
1194
570
  this.minSequenceNumber = message.minimumSequenceNumber;
1195
571
  if (message.sequenceNumber !== this.lastProcessedSequenceNumber + 1) {
1196
- throw new container_utils_1.DataCorruptionError("nonSequentialSequenceNumber", Object.assign(Object.assign({}, extractLogSafeMessageProperties(message)), { clientId: (_c = this.connection) === null || _c === void 0 ? void 0 : _c.clientId }));
572
+ throw new container_utils_1.DataCorruptionError("nonSequentialSequenceNumber", Object.assign(Object.assign({}, extractLogSafeMessageProperties(message)), { clientId: this.connectionManager.clientId }));
1197
573
  }
1198
574
  this.lastProcessedSequenceNumber = message.sequenceNumber;
1199
575
  // a bunch of code assumes that this is true
@@ -1215,15 +591,15 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
1215
591
  /**
1216
592
  * Retrieves the missing deltas between the given sequence numbers
1217
593
  */
1218
- fetchMissingDeltas(reasonArg, lastKnowOp, to) {
1219
- this.fetchMissingDeltasCore(reasonArg, false /* cacheOnly */, lastKnowOp, to).catch((error) => {
594
+ fetchMissingDeltas(reasonArg, to) {
595
+ this.fetchMissingDeltasCore(reasonArg, false /* cacheOnly */, to).catch((error) => {
1220
596
  this.logger.sendErrorEvent({ eventName: "fetchMissingDeltasException" }, error);
1221
597
  });
1222
598
  }
1223
599
  /**
1224
600
  * Retrieves the missing deltas between the given sequence numbers
1225
601
  */
1226
- async fetchMissingDeltasCore(reason, cacheOnly, lastKnowOp, to) {
602
+ async fetchMissingDeltasCore(reason, cacheOnly, to) {
1227
603
  var _a;
1228
604
  // Exit out early if we're already fetching deltas
1229
605
  if (this.fetchReason !== undefined) {
@@ -1235,12 +611,11 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
1235
611
  }
1236
612
  if (this.handler === undefined) {
1237
613
  // We do not poses yet any information
1238
- common_utils_1.assert(lastKnowOp === 0, 0x26b /* "initial state" */);
614
+ common_utils_1.assert(this.lastQueuedSequenceNumber === 0, 0x26b /* "initial state" */);
1239
615
  return;
1240
616
  }
1241
617
  try {
1242
- common_utils_1.assert(lastKnowOp === this.lastQueuedSequenceNumber, 0x0f1 /* "from arg" */);
1243
- let from = lastKnowOp + 1;
618
+ let from = this.lastQueuedSequenceNumber + 1;
1244
619
  const n = (_a = this.previouslyProcessedMessage) === null || _a === void 0 ? void 0 : _a.sequenceNumber;
1245
620
  if (n !== undefined) {
1246
621
  // If we already processed at least one op, then we have this.previouslyProcessedMessage populated
@@ -1248,7 +623,7 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
1248
623
  // Knowing about this mechanism, we could ask for op we already observed to increase validation.
1249
624
  // This is especially useful when coming out of offline mode or loading from
1250
625
  // very old cached (by client / driver) snapshot.
1251
- common_utils_1.assert(n === lastKnowOp, 0x0f2 /* "previouslyProcessedMessage" */);
626
+ common_utils_1.assert(n === this.lastQueuedSequenceNumber, 0x0f2 /* "previouslyProcessedMessage" */);
1252
627
  common_utils_1.assert(from > 1, 0x0f3 /* "not positive" */);
1253
628
  from--;
1254
629
  }
@@ -1296,7 +671,7 @@ class DeltaManager extends common_utils_1.TypedEventEmitter {
1296
671
  // (the other 50%), and thus these errors below should be looked at even if code below results in
1297
672
  // recovery.
1298
673
  if (this.lastQueuedSequenceNumber < this.lastObservedSeqNumber) {
1299
- this.fetchMissingDeltas("OpsBehind", this.lastQueuedSequenceNumber);
674
+ this.fetchMissingDeltas("OpsBehind");
1300
675
  }
1301
676
  }
1302
677
  }