@fluidframework/container-loader 2.0.0-internal.3.0.5 → 2.0.0-internal.3.1.1

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 (148) hide show
  1. package/.eslintrc.js +18 -21
  2. package/.mocharc.js +2 -2
  3. package/README.md +45 -43
  4. package/api-extractor.json +2 -2
  5. package/closeAndGetPendingLocalState.md +51 -0
  6. package/dist/audience.d.ts.map +1 -1
  7. package/dist/audience.js.map +1 -1
  8. package/dist/catchUpMonitor.d.ts.map +1 -1
  9. package/dist/catchUpMonitor.js.map +1 -1
  10. package/dist/collabWindowTracker.d.ts.map +1 -1
  11. package/dist/collabWindowTracker.js.map +1 -1
  12. package/dist/connectionManager.d.ts +2 -2
  13. package/dist/connectionManager.d.ts.map +1 -1
  14. package/dist/connectionManager.js +51 -24
  15. package/dist/connectionManager.js.map +1 -1
  16. package/dist/connectionState.d.ts.map +1 -1
  17. package/dist/connectionState.js.map +1 -1
  18. package/dist/connectionStateHandler.d.ts.map +1 -1
  19. package/dist/connectionStateHandler.js +35 -16
  20. package/dist/connectionStateHandler.js.map +1 -1
  21. package/dist/container.d.ts +1 -10
  22. package/dist/container.d.ts.map +1 -1
  23. package/dist/container.js +89 -44
  24. package/dist/container.js.map +1 -1
  25. package/dist/containerContext.d.ts.map +1 -1
  26. package/dist/containerContext.js +6 -2
  27. package/dist/containerContext.js.map +1 -1
  28. package/dist/containerStorageAdapter.d.ts.map +1 -1
  29. package/dist/containerStorageAdapter.js +2 -4
  30. package/dist/containerStorageAdapter.js.map +1 -1
  31. package/dist/contracts.d.ts.map +1 -1
  32. package/dist/contracts.js.map +1 -1
  33. package/dist/deltaManager.d.ts +3 -3
  34. package/dist/deltaManager.d.ts.map +1 -1
  35. package/dist/deltaManager.js +56 -27
  36. package/dist/deltaManager.js.map +1 -1
  37. package/dist/deltaManagerProxy.d.ts.map +1 -1
  38. package/dist/deltaManagerProxy.js.map +1 -1
  39. package/dist/deltaQueue.d.ts.map +1 -1
  40. package/dist/deltaQueue.js +4 -2
  41. package/dist/deltaQueue.js.map +1 -1
  42. package/dist/index.d.ts +1 -1
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js.map +1 -1
  45. package/dist/loader.d.ts +3 -3
  46. package/dist/loader.d.ts.map +1 -1
  47. package/dist/loader.js +18 -15
  48. package/dist/loader.js.map +1 -1
  49. package/dist/packageVersion.d.ts +1 -1
  50. package/dist/packageVersion.js +1 -1
  51. package/dist/packageVersion.js.map +1 -1
  52. package/dist/protocol.d.ts.map +1 -1
  53. package/dist/protocol.js +2 -1
  54. package/dist/protocol.js.map +1 -1
  55. package/dist/protocolTreeDocumentStorageService.d.ts.map +1 -1
  56. package/dist/protocolTreeDocumentStorageService.js.map +1 -1
  57. package/dist/quorum.d.ts.map +1 -1
  58. package/dist/quorum.js.map +1 -1
  59. package/dist/retriableDocumentStorageService.d.ts.map +1 -1
  60. package/dist/retriableDocumentStorageService.js +6 -2
  61. package/dist/retriableDocumentStorageService.js.map +1 -1
  62. package/dist/utils.d.ts.map +1 -1
  63. package/dist/utils.js +6 -4
  64. package/dist/utils.js.map +1 -1
  65. package/lib/audience.d.ts.map +1 -1
  66. package/lib/audience.js.map +1 -1
  67. package/lib/catchUpMonitor.d.ts.map +1 -1
  68. package/lib/catchUpMonitor.js.map +1 -1
  69. package/lib/collabWindowTracker.d.ts.map +1 -1
  70. package/lib/collabWindowTracker.js.map +1 -1
  71. package/lib/connectionManager.d.ts +2 -2
  72. package/lib/connectionManager.d.ts.map +1 -1
  73. package/lib/connectionManager.js +53 -26
  74. package/lib/connectionManager.js.map +1 -1
  75. package/lib/connectionState.d.ts.map +1 -1
  76. package/lib/connectionState.js.map +1 -1
  77. package/lib/connectionStateHandler.d.ts.map +1 -1
  78. package/lib/connectionStateHandler.js +35 -16
  79. package/lib/connectionStateHandler.js.map +1 -1
  80. package/lib/container.d.ts +1 -10
  81. package/lib/container.d.ts.map +1 -1
  82. package/lib/container.js +93 -48
  83. package/lib/container.js.map +1 -1
  84. package/lib/containerContext.d.ts.map +1 -1
  85. package/lib/containerContext.js +6 -2
  86. package/lib/containerContext.js.map +1 -1
  87. package/lib/containerStorageAdapter.d.ts.map +1 -1
  88. package/lib/containerStorageAdapter.js +2 -4
  89. package/lib/containerStorageAdapter.js.map +1 -1
  90. package/lib/contracts.d.ts.map +1 -1
  91. package/lib/contracts.js.map +1 -1
  92. package/lib/deltaManager.d.ts +3 -3
  93. package/lib/deltaManager.d.ts.map +1 -1
  94. package/lib/deltaManager.js +58 -29
  95. package/lib/deltaManager.js.map +1 -1
  96. package/lib/deltaManagerProxy.d.ts.map +1 -1
  97. package/lib/deltaManagerProxy.js.map +1 -1
  98. package/lib/deltaQueue.d.ts.map +1 -1
  99. package/lib/deltaQueue.js +4 -2
  100. package/lib/deltaQueue.js.map +1 -1
  101. package/lib/index.d.ts +1 -1
  102. package/lib/index.d.ts.map +1 -1
  103. package/lib/index.js.map +1 -1
  104. package/lib/loader.d.ts +3 -3
  105. package/lib/loader.d.ts.map +1 -1
  106. package/lib/loader.js +18 -15
  107. package/lib/loader.js.map +1 -1
  108. package/lib/packageVersion.d.ts +1 -1
  109. package/lib/packageVersion.js +1 -1
  110. package/lib/packageVersion.js.map +1 -1
  111. package/lib/protocol.d.ts.map +1 -1
  112. package/lib/protocol.js +2 -1
  113. package/lib/protocol.js.map +1 -1
  114. package/lib/protocolTreeDocumentStorageService.d.ts.map +1 -1
  115. package/lib/protocolTreeDocumentStorageService.js.map +1 -1
  116. package/lib/quorum.d.ts.map +1 -1
  117. package/lib/quorum.js.map +1 -1
  118. package/lib/retriableDocumentStorageService.d.ts.map +1 -1
  119. package/lib/retriableDocumentStorageService.js +6 -2
  120. package/lib/retriableDocumentStorageService.js.map +1 -1
  121. package/lib/utils.d.ts.map +1 -1
  122. package/lib/utils.js +6 -4
  123. package/lib/utils.js.map +1 -1
  124. package/package.json +115 -114
  125. package/prettier.config.cjs +1 -1
  126. package/src/audience.ts +51 -46
  127. package/src/catchUpMonitor.ts +39 -37
  128. package/src/collabWindowTracker.ts +75 -70
  129. package/src/connectionManager.ts +1006 -944
  130. package/src/connectionState.ts +19 -19
  131. package/src/connectionStateHandler.ts +544 -465
  132. package/src/container.ts +2056 -1909
  133. package/src/containerContext.ts +350 -340
  134. package/src/containerStorageAdapter.ts +163 -153
  135. package/src/contracts.ts +155 -153
  136. package/src/deltaManager.ts +1069 -992
  137. package/src/deltaManagerProxy.ts +143 -137
  138. package/src/deltaQueue.ts +155 -151
  139. package/src/index.ts +14 -17
  140. package/src/loader.ts +428 -430
  141. package/src/packageVersion.ts +1 -1
  142. package/src/protocol.ts +93 -87
  143. package/src/protocolTreeDocumentStorageService.ts +30 -33
  144. package/src/quorum.ts +34 -34
  145. package/src/retriableDocumentStorageService.ts +118 -102
  146. package/src/utils.ts +89 -82
  147. package/tsconfig.esnext.json +6 -6
  148. package/tsconfig.json +8 -12
@@ -6,59 +6,48 @@
6
6
  import { default as AbortController } from "abort-controller";
7
7
  import { v4 as uuid } from "uuid";
8
8
  import {
9
- ITelemetryLogger,
10
- IEventProvider,
11
- ITelemetryProperties,
12
- ITelemetryErrorEvent,
9
+ ITelemetryLogger,
10
+ IEventProvider,
11
+ ITelemetryProperties,
12
+ ITelemetryErrorEvent,
13
13
  } from "@fluidframework/common-definitions";
14
14
  import {
15
- IDeltaHandlerStrategy,
16
- IDeltaManager,
17
- IDeltaManagerEvents,
18
- IDeltaQueue,
19
- ICriticalContainerError,
20
- IThrottlingWarning,
21
- IConnectionDetails,
15
+ IDeltaHandlerStrategy,
16
+ IDeltaManager,
17
+ IDeltaManagerEvents,
18
+ IDeltaQueue,
19
+ ICriticalContainerError,
20
+ IThrottlingWarning,
21
+ IConnectionDetails,
22
22
  } from "@fluidframework/container-definitions";
23
23
  import { assert, TypedEventEmitter } from "@fluidframework/common-utils";
24
+ import { normalizeError, logIfFalse, safeRaiseEvent } from "@fluidframework/telemetry-utils";
24
25
  import {
25
- normalizeError,
26
- logIfFalse,
27
- safeRaiseEvent,
28
- } from "@fluidframework/telemetry-utils";
29
- import {
30
- IDocumentDeltaStorageService,
31
- IDocumentService,
32
- DriverErrorType,
26
+ IDocumentDeltaStorageService,
27
+ IDocumentService,
28
+ DriverErrorType,
33
29
  } from "@fluidframework/driver-definitions";
34
30
  import {
35
- IDocumentMessage,
36
- ISequencedDocumentMessage,
37
- ISignalMessage,
38
- MessageType,
39
- ConnectionMode,
31
+ IDocumentMessage,
32
+ ISequencedDocumentMessage,
33
+ ISignalMessage,
34
+ MessageType,
35
+ ConnectionMode,
40
36
  } from "@fluidframework/protocol-definitions";
37
+ import { NonRetryableError, isRuntimeMessage, MessageType2 } from "@fluidframework/driver-utils";
41
38
  import {
42
- NonRetryableError,
43
- isRuntimeMessage,
44
- MessageType2,
45
- } from "@fluidframework/driver-utils";
46
- import {
47
- ThrottlingWarning,
48
- DataCorruptionError,
49
- extractSafePropertiesFromMessage,
50
- DataProcessingError,
39
+ ThrottlingWarning,
40
+ DataCorruptionError,
41
+ extractSafePropertiesFromMessage,
42
+ DataProcessingError,
51
43
  } from "@fluidframework/container-utils";
52
44
  import { DeltaQueue } from "./deltaQueue";
53
- import {
54
- IConnectionManagerFactoryArgs,
55
- IConnectionManager,
56
- } from "./contracts";
45
+ import { IConnectionManagerFactoryArgs, IConnectionManager } from "./contracts";
57
46
 
58
47
  export interface IConnectionArgs {
59
- mode?: ConnectionMode;
60
- fetchOpsFromStorage?: boolean;
61
- reason: string;
48
+ mode?: ConnectionMode;
49
+ fetchOpsFromStorage?: boolean;
50
+ reason: string;
62
51
  }
63
52
 
64
53
  /**
@@ -66,27 +55,27 @@ export interface IConnectionArgs {
66
55
  * but not exposed on the public interface IDeltaManager
67
56
  */
68
57
  export interface IDeltaManagerInternalEvents extends IDeltaManagerEvents {
69
- (event: "throttled", listener: (error: IThrottlingWarning) => void);
70
- (event: "closed" | "disposed", listener: (error?: ICriticalContainerError) => void);
58
+ (event: "throttled", listener: (error: IThrottlingWarning) => void);
59
+ (event: "closed" | "disposed", listener: (error?: ICriticalContainerError) => void);
71
60
  }
72
61
 
73
62
  /**
74
63
  * Determines if message was sent by client, not service
75
64
  */
76
65
  function isClientMessage(message: ISequencedDocumentMessage | IDocumentMessage): boolean {
77
- if (isRuntimeMessage(message)) {
78
- return true;
79
- }
80
- switch (message.type) {
81
- case MessageType.Propose:
82
- case MessageType.Reject:
83
- case MessageType.NoOp:
84
- case MessageType2.Accept:
85
- case MessageType.Summarize:
86
- return true;
87
- default:
88
- return false;
89
- }
66
+ if (isRuntimeMessage(message)) {
67
+ return true;
68
+ }
69
+ switch (message.type) {
70
+ case MessageType.Propose:
71
+ case MessageType.Reject:
72
+ case MessageType.NoOp:
73
+ case MessageType2.Accept:
74
+ case MessageType.Summarize:
75
+ return true;
76
+ default:
77
+ return false;
78
+ }
90
79
  }
91
80
 
92
81
  /**
@@ -94,943 +83,1031 @@ function isClientMessage(message: ISequencedDocumentMessage | IDocumentMessage):
94
83
  * messages in order regardless of possible network conditions or timings causing out of order delivery.
95
84
  */
96
85
  export class DeltaManager<TConnectionManager extends IConnectionManager>
97
- extends TypedEventEmitter<IDeltaManagerInternalEvents>
98
- implements
99
- IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>,
100
- IEventProvider<IDeltaManagerInternalEvents> {
101
- public readonly connectionManager: TConnectionManager;
102
-
103
- public get active(): boolean { return this._active(); }
104
-
105
- public get disposed() { return this._closed; }
86
+ extends TypedEventEmitter<IDeltaManagerInternalEvents>
87
+ implements
88
+ IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>,
89
+ IEventProvider<IDeltaManagerInternalEvents>
90
+ {
91
+ public readonly connectionManager: TConnectionManager;
106
92
 
107
- public get IDeltaSender() { return this; }
108
-
109
- private pending: ISequencedDocumentMessage[] = [];
110
- private fetchReason: string | undefined;
93
+ public get active(): boolean {
94
+ return this._active();
95
+ }
111
96
 
112
- // A boolean used to assert that ops are not being sent while processing another op.
113
- private currentlyProcessingOps: boolean = false;
114
-
115
- // The minimum sequence number and last sequence number received from the server
116
- private minSequenceNumber: number = 0;
117
-
118
- // There are three numbers we track
119
- // * lastQueuedSequenceNumber is the last queued sequence number. If there are gaps in seq numbers, then this number
120
- // is not updated until we cover that gap, so it increases each time by 1.
121
- // * lastObservedSeqNumber is an estimation of last known sequence number for container in storage. It's initially
122
- // populated at web socket connection time (if storage provides that info) and is updated once ops shows up.
123
- // It's never less than lastQueuedSequenceNumber
124
- // * lastProcessedSequenceNumber - last processed sequence number
125
- private lastQueuedSequenceNumber: number = 0;
126
- private lastObservedSeqNumber: number = 0;
127
- private lastProcessedSequenceNumber: number = 0;
128
- private lastProcessedMessage: ISequencedDocumentMessage | undefined;
129
- private baseTerm: number = 0;
130
-
131
- /** count number of noops sent by the client which may not be acked */
132
- private noOpCount: number = 0;
133
- /** Track clientSequenceNumber of the last op */
134
- private lastClientSequenceNumber: number = 0;
135
-
136
- /**
137
- * Track down the ops size.
138
- */
139
- private opsSize: number = 0;
140
- private prevEnqueueMessagesReason: string | undefined;
141
- private previouslyProcessedMessage: ISequencedDocumentMessage | undefined;
142
-
143
- // The sequence number we initially loaded from
144
- // In case of reading from a snapshot or pending state, its value will be equal to
145
- // the last message that got serialized.
146
- private initSequenceNumber: number = 0;
147
-
148
- private readonly _inbound: DeltaQueue<ISequencedDocumentMessage>;
149
- private readonly _inboundSignal: DeltaQueue<ISignalMessage>;
150
-
151
- private _closed = false;
152
- private _disposed = false;
153
-
154
- private handler: IDeltaHandlerStrategy | undefined;
155
- private deltaStorage: IDocumentDeltaStorageService | undefined;
156
-
157
- private readonly throttlingIdSet = new Set<string>();
158
- private timeTillThrottling: number = 0;
159
-
160
- public readonly closeAbortController = new AbortController();
161
-
162
- private readonly deltaStorageDelayId = uuid();
163
- private readonly deltaStreamDelayId = uuid();
164
-
165
- private messageBuffer: IDocumentMessage[] = [];
166
-
167
- private _checkpointSequenceNumber: number | undefined;
168
-
169
- public get inbound(): IDeltaQueue<ISequencedDocumentMessage> {
170
- return this._inbound;
171
- }
172
-
173
- public get inboundSignal(): IDeltaQueue<ISignalMessage> {
174
- return this._inboundSignal;
175
- }
176
-
177
- public get initialSequenceNumber(): number {
178
- return this.initSequenceNumber;
179
- }
180
-
181
- public get lastSequenceNumber(): number {
182
- return this.lastProcessedSequenceNumber;
183
- }
184
-
185
- public get lastMessage() {
186
- return this.lastProcessedMessage;
187
- }
188
-
189
- public get lastKnownSeqNumber() {
190
- return this.lastObservedSeqNumber;
191
- }
192
-
193
- public get referenceTerm(): number {
194
- return this.baseTerm;
195
- }
196
-
197
- public get minimumSequenceNumber(): number {
198
- return this.minSequenceNumber;
199
- }
200
-
201
- /**
202
- * Tells if current connection has checkpoint information.
203
- * I.e. we know how far behind the client was at the time of establishing connection
204
- */
205
- public get hasCheckpointSequenceNumber() {
206
- // Valid to be called only if we have active connection.
207
- assert(this.connectionManager.connected, 0x0df /* "Missing active connection" */);
208
- return this._checkpointSequenceNumber !== undefined;
209
- }
210
-
211
- // Forwarding connection manager properties / IDeltaManager implementation
212
- public get maxMessageSize(): number { return this.connectionManager.maxMessageSize; }
213
- public get version() { return this.connectionManager.version; }
214
- public get serviceConfiguration() { return this.connectionManager.serviceConfiguration; }
215
- public get outbound() { return this.connectionManager.outbound; }
216
- public get readOnlyInfo() { return this.connectionManager.readOnlyInfo; }
217
- public get clientDetails() { return this.connectionManager.clientDetails; }
218
-
219
- public submit(type: MessageType, contents?: string, batch = false, metadata?: any, compression?: string) {
220
- const messagePartial: Omit<IDocumentMessage, "clientSequenceNumber"> = {
221
- contents,
222
- metadata,
223
- referenceSequenceNumber: this.lastProcessedSequenceNumber,
224
- type,
225
- compression,
226
- };
227
-
228
- if (!batch) {
229
- this.flush();
230
- }
231
- const message = this.connectionManager.prepareMessageToSend(messagePartial);
232
- if (message === undefined) {
233
- return -1;
234
- }
235
-
236
- assert(isClientMessage(message), 0x419 /* client sends non-client message */);
237
-
238
- if (contents !== undefined) {
239
- this.opsSize += contents.length;
240
- }
241
-
242
- this.messageBuffer.push(message);
243
-
244
- if (message.type === MessageType.NoOp){
245
- this.noOpCount++;
246
- }
247
-
248
- this.emit("submitOp", message);
249
-
250
- if (!batch) {
251
- this.flush();
252
- }
253
- return message.clientSequenceNumber;
254
- }
255
-
256
- public submitSignal(content: any) { return this.connectionManager.submitSignal(content); }
257
-
258
- public flush() {
259
- const batch = this.messageBuffer;
260
- if (batch.length === 0) {
261
- return;
262
- }
263
-
264
- this.messageBuffer = [];
265
-
266
- // The prepareFlush event allows listeners to append metadata to the batch prior to submission.
267
- this.emit("prepareSend", batch);
268
-
269
- if (batch.length === 1) {
270
- assert(batch[0].metadata?.batch === undefined, 0x3c9 /* no batch markup on single message */);
271
- } else {
272
- assert(batch[0].metadata?.batch === true, 0x3ca /* no start batch markup */);
273
- assert(batch[batch.length - 1].metadata?.batch === false, 0x3cb /* no end batch markup */);
274
- }
275
-
276
- this.connectionManager.sendMessages(batch);
277
-
278
- assert(this.messageBuffer.length === 0, 0x3cc /* reentrancy */);
279
- }
280
-
281
- public get connectionProps(): ITelemetryProperties {
282
- return {
283
- sequenceNumber: this.lastSequenceNumber,
284
- opsSize: this.opsSize > 0 ? this.opsSize : undefined,
285
- ...this.connectionManager.connectionProps,
286
- };
287
- }
288
-
289
- /**
290
- * Log error event with a bunch of internal to DeltaManager information about state of op processing
291
- * Used to diagnose connectivity issues related to op processing (i.e. cases where for some reason
292
- * we stop processing ops that results in no processing join op and thus moving to connected state)
293
- * @param event - Event to log.
294
- */
295
- public logConnectionIssue(event: ITelemetryErrorEvent) {
296
- assert(this.connectionManager.connected, 0x238 /* "called only in connected state" */);
297
-
298
- const pendingSorted = this.pending.sort((a, b) => a.sequenceNumber - b.sequenceNumber);
299
- this.logger.sendErrorEvent({
300
- ...event,
301
- // This directly tells us if fetching ops is in flight, and thus likely the reason of
302
- // stalled op processing
303
- fetchReason: this.fetchReason,
304
- // A bunch of useful sequence numbers to understand if we are holding some ops from processing
305
- lastQueuedSequenceNumber: this.lastQueuedSequenceNumber, // last sequential op
306
- lastProcessedSequenceNumber: this.lastProcessedSequenceNumber, // same as above, but after processing
307
- lastObserved: this.lastObservedSeqNumber, // last sequence we ever saw; may have gaps with above.
308
- // connection info
309
- ...this.connectionManager.connectionVerboseProps,
310
- pendingOps: this.pending.length, // Do we have any pending ops?
311
- pendingFirst: pendingSorted[0]?.sequenceNumber, // is the first pending op the one that we are missing?
312
- haveHandler: this.handler !== undefined, // do we have handler installed?
313
- inboundLength: this.inbound.length,
314
- inboundPaused: this.inbound.paused,
315
- });
316
- }
317
-
318
- constructor(
319
- private readonly serviceProvider: () => IDocumentService | undefined,
320
- private readonly logger: ITelemetryLogger,
321
- private readonly _active: () => boolean,
322
- createConnectionManager: (props: IConnectionManagerFactoryArgs) => TConnectionManager,
323
- ) {
324
- super();
325
- const props: IConnectionManagerFactoryArgs = {
326
- incomingOpHandler: (messages: ISequencedDocumentMessage[], reason: string) => {
327
- try {
328
- this.enqueueMessages(messages, reason);
329
- } catch (error) {
330
- this.logger.sendErrorEvent({ eventName: "EnqueueMessages_Exception" }, error);
331
- this.close(normalizeError(error));
332
- }
333
- },
334
- signalHandler: (message: ISignalMessage) => this._inboundSignal.push(message),
335
- reconnectionDelayHandler: (delayMs: number, error: unknown) =>
336
- this.emitDelayInfo(this.deltaStreamDelayId, delayMs, error),
337
- closeHandler: (error: any) => this.close(error),
338
- disconnectHandler: (reason: string) => this.disconnectHandler(reason),
339
- connectHandler: (connection: IConnectionDetails) => this.connectHandler(connection),
340
- pongHandler: (latency: number) => this.emit("pong", latency),
341
- readonlyChangeHandler: (readonly?: boolean) => safeRaiseEvent(this, this.logger, "readonly", readonly),
342
- };
343
-
344
- this.connectionManager = createConnectionManager(props);
345
- this._inbound = new DeltaQueue<ISequencedDocumentMessage>(
346
- (op) => {
347
- this.processInboundMessage(op);
348
- });
349
-
350
- this._inbound.on("error", (error) => {
351
- this.close(
352
- DataProcessingError.wrapIfUnrecognized(error, "deltaManagerInboundErrorHandler", this.lastMessage));
353
- });
354
-
355
- // Inbound signal queue
356
- this._inboundSignal = new DeltaQueue<ISignalMessage>((message) => {
357
- if (this.handler === undefined) {
358
- throw new Error("Attempted to process an inbound signal without a handler attached");
359
- }
360
- this.handler.processSignal({
361
- clientId: message.clientId,
362
- content: JSON.parse(message.content as string),
363
- });
364
- });
365
-
366
- this._inboundSignal.on("error", (error) => {
367
- this.close(normalizeError(error));
368
- });
369
-
370
- // Initially, all queues are created paused.
371
- // - outbound is flipped back and forth in setupNewSuccessfulConnection / disconnectFromDeltaStream
372
- // - inbound & inboundSignal are resumed in attachOpHandler() when we have handler setup
373
- }
374
-
375
- private connectHandler(connection: IConnectionDetails) {
376
- this.refreshDelayInfo(this.deltaStreamDelayId);
377
-
378
- const props = this.connectionManager.connectionVerboseProps;
379
- props.connectionLastQueuedSequenceNumber = this.lastQueuedSequenceNumber;
380
- props.connectionLastObservedSeqNumber = this.lastObservedSeqNumber;
381
-
382
- const checkpointSequenceNumber = connection.checkpointSequenceNumber;
383
- this._checkpointSequenceNumber = checkpointSequenceNumber;
384
- if (checkpointSequenceNumber !== undefined) {
385
- this.updateLatestKnownOpSeqNumber(checkpointSequenceNumber);
386
- }
387
-
388
- // We cancel all ops on lost of connectivity, and rely on DDSes to resubmit them.
389
- // Semantics are not well defined for batches (and they are broken right now on disconnects anyway),
390
- // but it's safe to assume (until better design is put into place) that batches should not exist
391
- // across multiple connections. Right now we assume runtime will not submit any ops in disconnected
392
- // state. As requirements change, so should these checks.
393
- assert(this.messageBuffer.length === 0, 0x0e9 /* "messageBuffer is not empty on new connection" */);
394
-
395
- this.opsSize = 0;
396
- this.noOpCount = 0;
397
-
398
- this.emit(
399
- "connect",
400
- connection,
401
- checkpointSequenceNumber !== undefined ?
402
- this.lastObservedSeqNumber - this.lastSequenceNumber : undefined);
403
-
404
- // If we got some initial ops, then we know the gap and call above fetched ops to fill it.
405
- // Same is true for "write" mode even if we have no ops - we will get "join" own op very very soon.
406
- // However if we are connecting as view-only, then there is no good signal to realize if client is behind.
407
- // Thus we have to hit storage to see if any ops are there.
408
- if (checkpointSequenceNumber !== undefined) {
409
- // We know how far we are behind (roughly). If it's non-zero gap, fetch ops right away.
410
- if (checkpointSequenceNumber > this.lastQueuedSequenceNumber) {
411
- this.fetchMissingDeltas("AfterConnection");
412
- }
413
- // we do not know the gap, and we will not learn about it if socket is quite - have to ask.
414
- } else if (connection.mode === "read") {
415
- this.fetchMissingDeltas("AfterReadConnection");
416
- }
417
- }
418
-
419
- public dispose() {
420
- throw new Error("Not implemented.");
421
- }
422
-
423
- /**
424
- * Sets the sequence number from which inbound messages should be returned
425
- */
426
- public async attachOpHandler(
427
- minSequenceNumber: number,
428
- sequenceNumber: number,
429
- term: number,
430
- handler: IDeltaHandlerStrategy,
431
- prefetchType: "cached" | "all" | "none" = "none",
432
- ) {
433
- this.initSequenceNumber = sequenceNumber;
434
- this.lastProcessedSequenceNumber = sequenceNumber;
435
- this.baseTerm = term;
436
- this.minSequenceNumber = minSequenceNumber;
437
- this.lastQueuedSequenceNumber = sequenceNumber;
438
- this.lastObservedSeqNumber = sequenceNumber;
439
-
440
- // We will use same check in other places to make sure all the seq number above are set properly.
441
- assert(this.handler === undefined, 0x0e2 /* "DeltaManager already has attached op handler!" */);
442
- this.handler = handler;
443
- // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
444
- assert(!!(this.handler as any), 0x0e3 /* "Newly set op handler is null/undefined!" */);
445
-
446
- // There should be no pending fetch!
447
- // This API is called right after attachOpHandler by Container.load().
448
- // We might have connection already and it might have called fetchMissingDeltas() from
449
- // setupNewSuccessfulConnection. But it should do nothing, because there is no way to fetch ops before
450
- // we know snapshot sequence number that is set in attachOpHandler. So all such calls should be noop.
451
- assert(this.fetchReason === undefined, 0x268 /* "There can't be pending fetch that early in boot sequence!" */);
452
-
453
- if (this._closed) {
454
- return;
455
- }
456
-
457
- this._inbound.resume();
458
- this._inboundSignal.resume();
459
-
460
- if (prefetchType !== "none") {
461
- const cacheOnly = prefetchType === "cached";
462
- await this.fetchMissingDeltasCore(`DocumentOpen_${prefetchType}`, cacheOnly);
463
-
464
- // Keep going with fetching ops from storage once we have all cached ops in.
465
- // But do not block load and make this request async / not blocking this api.
466
- // Ops processing will start once cached ops are in and and will stop when queue is empty
467
- // (which in most cases will happen when we are done processing cached ops)
468
- if (cacheOnly) {
469
- // fire and forget
470
- this.fetchMissingDeltas("PostDocumentOpen");
471
- }
472
- }
473
-
474
- // Ensure there is no need to call this.processPendingOps() at the end of boot sequence
475
- assert(this.fetchReason !== undefined || this.pending.length === 0, 0x269 /* "pending ops are not dropped" */);
476
- }
477
-
478
- public connect(args: IConnectionArgs) {
479
- const fetchOpsFromStorage = args.fetchOpsFromStorage ?? true;
480
- logIfFalse(
481
- this.handler !== undefined || !fetchOpsFromStorage,
482
- this.logger,
483
- "CantFetchWithoutBaseline"); // can't fetch if no baseline
484
-
485
- // Note: There is race condition here.
486
- // We want to issue request to storage as soon as possible, to
487
- // reduce latency of becoming current, thus this code here.
488
- // But there is no ordering between fetching OPs and connection to delta stream
489
- // As result, we might be behind by the time we connect to delta stream
490
- // In case of r/w connection, that's not an issue, because we will hear our
491
- // own "join" message and realize any gap client has in ops.
492
- // But for view-only connection, we have no such signal, and with no traffic
493
- // on the wire, we might be always behind.
494
- // See comment at the end of "connect" handler
495
- if (fetchOpsFromStorage) {
496
- this.fetchMissingDeltas(args.reason);
497
- }
498
-
499
- this.connectionManager.connect(args.mode);
500
- }
501
-
502
- private async getDeltas(
503
- from: number, // inclusive
504
- to: number | undefined, // exclusive
505
- fetchReason: string,
506
- callback: (messages: ISequencedDocumentMessage[]) => void,
507
- cacheOnly: boolean) {
508
- const docService = this.serviceProvider();
509
- if (docService === undefined) {
510
- throw new Error("Delta manager is not attached");
511
- }
512
-
513
- if (this.deltaStorage === undefined) {
514
- this.deltaStorage = await docService.connectToDeltaStorage();
515
- }
516
-
517
- let cancelFetch: (op: ISequencedDocumentMessage) => boolean;
518
-
519
- if (to !== undefined) {
520
- const lastExpectedOp = to - 1; // make it inclusive!
521
-
522
- // It is possible that due to asynchrony (including await above), required ops were already
523
- // received through delta stream. Validate that before moving forward.
524
- if (this.lastQueuedSequenceNumber >= lastExpectedOp) {
525
- this.logger.sendPerformanceEvent({
526
- reason: fetchReason,
527
- eventName: "ExtraStorageCall",
528
- early: true,
529
- from,
530
- to,
531
- ...this.connectionManager.connectionVerboseProps,
532
- });
533
- return;
534
- }
535
-
536
- // Be prepared for the case where webSocket would receive the ops that we are trying to fill through
537
- // storage. Ideally it should never happen (i.e. ops on socket are always ordered, and thus once we
538
- // detected gap, this gap can't be filled in later on through websocket).
539
- // And in practice that does look like the case. The place where this code gets hit is if we lost
540
- // connection and reconnected (likely to another box), and new socket's initial ops contains these ops.
541
- cancelFetch = (op: ISequencedDocumentMessage) => op.sequenceNumber >= lastExpectedOp;
542
- } else {
543
- // Unbound requests are made to proactively fetch ops, but also get up to date in cases where socket
544
- // is silent (and connection is "read", thus we might not have any data on how far client is behind).
545
- // Once we have any op coming in from socket, we can cancel it as it's not needed any more.
546
- // That said, if we have socket connection, make sure we got ops up to checkpointSequenceNumber!
547
- cancelFetch = (op: ISequencedDocumentMessage) => op.sequenceNumber >= this.lastObservedSeqNumber;
548
- }
549
-
550
- const controller = new AbortController();
551
- let opsFromFetch = false;
552
-
553
- const opListener = (op: ISequencedDocumentMessage) => {
554
- assert(op.sequenceNumber === this.lastQueuedSequenceNumber, 0x23a /* "seq#'s" */);
555
- // Ops that are coming from this request should not cancel itself.
556
- // This is useless for known ranges (to is defined) as it means request is over either way.
557
- // And it will cancel unbound request too early, not allowing us to learn where the end of the file is.
558
- if (!opsFromFetch && cancelFetch(op)) {
559
- controller.abort();
560
- this._inbound.off("push", opListener);
561
- }
562
- };
563
-
564
- try {
565
- this._inbound.on("push", opListener);
566
- assert(this.closeAbortController.signal.onabort === null, 0x1e8 /* "reentrancy" */);
567
- this.closeAbortController.signal.onabort = () => controller.abort();
568
-
569
- const stream = this.deltaStorage.fetchMessages(
570
- from, // inclusive
571
- to, // exclusive
572
- controller.signal,
573
- cacheOnly,
574
- fetchReason);
575
-
576
- // eslint-disable-next-line no-constant-condition
577
- while (true) {
578
- const result = await stream.read();
579
- if (result.done) {
580
- break;
581
- }
582
- try {
583
- opsFromFetch = true;
584
- callback(result.value);
585
- } finally {
586
- opsFromFetch = false;
587
- }
588
- }
589
- } finally {
590
- this.closeAbortController.signal.onabort = null;
591
- this._inbound.off("push", opListener);
592
- assert(!opsFromFetch, 0x289 /* "logic error" */);
593
- }
594
- }
595
-
596
- /**
597
- * Closes the connection and clears inbound & outbound queues.
598
- *
599
- * @param doDispose - should the DeltaManager treat this close call as a dispose?
600
- * Differences between close and dispose:
601
- * - dispose will emit "disposed" event while close emits "closed"
602
- * - dispose will remove all listeners
603
- * - dispose can be called after closure, but not vis versa
604
- */
605
- public close(error?: ICriticalContainerError, doDispose?: boolean): void {
606
- if (this._closed) {
607
- if (doDispose === true) {
608
- this.disposeInternal(error);
609
- }
610
- return;
611
- }
612
- this._closed = true;
613
-
614
- this.connectionManager.dispose(error, doDispose !== true);
615
-
616
- this.closeAbortController.abort();
617
-
618
- this._inbound.clear();
619
- this._inboundSignal.clear();
620
-
621
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
622
- this._inbound.pause();
623
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
624
- this._inboundSignal.pause();
625
-
626
- // Drop pending messages - this will ensure catchUp() does not go into infinite loop
627
- this.pending = [];
628
-
629
- if (doDispose === true) {
630
- this.disposeInternal(error);
631
- } else {
632
- this.emit("closed", error);
633
- this.disposeInternal(error); // ! TODO: remove this call when Container close no longer disposes
634
- }
635
- }
636
-
637
- private disposeInternal(error?: ICriticalContainerError): void {
638
- if (this._disposed) {
639
- return;
640
- }
641
- this._disposed = true;
642
-
643
- // This needs to be the last thing we do (before removing listeners), as it causes
644
- // Container to dispose context and break ability of data stores / runtime to "hear"
645
- // from delta manager, including notification (above) about readonly state.
646
- this.emit("disposed", error);
647
- this.removeAllListeners();
648
- }
649
-
650
- public refreshDelayInfo(id: string) {
651
- this.throttlingIdSet.delete(id);
652
- if (this.throttlingIdSet.size === 0) {
653
- this.timeTillThrottling = 0;
654
- }
655
- }
656
-
657
- private disconnectHandler(reason: string) {
658
- this.messageBuffer.length = 0;
659
- this.emit("disconnect", reason);
660
- }
661
-
662
- /**
663
- * Emit info about a delay in service communication on account of throttling.
664
- * @param id - Id of the connection that is delayed
665
- * @param delayMs - Duration of the delay
666
- * @param error - error object indicating the throttling
667
- */
668
- public emitDelayInfo(id: string, delayMs: number, error: unknown) {
669
- const timeNow = Date.now();
670
- this.throttlingIdSet.add(id);
671
- if (delayMs > 0 && (timeNow + delayMs > this.timeTillThrottling)) {
672
- this.timeTillThrottling = timeNow + delayMs;
673
-
674
- const throttlingWarning: IThrottlingWarning = ThrottlingWarning.wrap(
675
- error,
676
- delayMs / 1000 /* retryAfterSeconds */,
677
- this.logger,
678
- );
679
- this.emit("throttled", throttlingWarning);
680
- }
681
- }
682
-
683
- // returns parts of message (in string format) that should never change for a given message.
684
- // Used for message comparison. It attempts to avoid comparing fields that potentially may differ.
685
- // for example, it's not clear if serverMetadata or timestamp property is a property of message or server state.
686
- // We only extract the most obvious fields that are sufficient (with high probability) to detect sequence number
687
- // reuse.
688
- // Also payload goes to telemetry, so no PII, including content!!
689
- // Note: It's possible for a duplicate op to be broadcasted and have everything the same except the timestamp.
690
- private comparableMessagePayload(m: ISequencedDocumentMessage) {
691
- return `${m.clientId}-${m.type}-${m.minimumSequenceNumber}-${m.referenceSequenceNumber}-${m.timestamp}`;
692
- }
693
-
694
- private enqueueMessages(
695
- messages: ISequencedDocumentMessage[],
696
- reason: string,
697
- allowGaps = false,
698
- ): void {
699
- if (this.handler === undefined) {
700
- // We did not setup handler yet.
701
- // This happens when we connect to web socket faster than we get attributes for container
702
- // and thus faster than attachOpHandler() is called
703
- // this.lastProcessedSequenceNumber is still zero, so we can't rely on this.fetchMissingDeltas()
704
- // to do the right thing.
705
- this.pending = this.pending.concat(messages);
706
- return;
707
- }
708
-
709
- // Pending ops should never just hang around for nothing.
710
- // This invariant will stay true through this function execution,
711
- // so there is no need to process pending ops here.
712
- // It's responsibility of
713
- // - attachOpHandler()
714
- // - fetchMissingDeltas() after it's done with querying storage
715
- assert(this.pending.length === 0 || this.fetchReason !== undefined, 0x1e9 /* "Pending ops" */);
716
-
717
- if (messages.length === 0) {
718
- return;
719
- }
720
-
721
- const from = messages[0].sequenceNumber;
722
- const last = messages[messages.length - 1].sequenceNumber;
723
-
724
- // Report stats about missing and duplicate ops
725
- // This helps better understand why we fetch ops from storage, and thus may delay
726
- // getting current / sending ops
727
- // It's possible that this batch is already too late - do not bother
728
- if (last > this.lastQueuedSequenceNumber) {
729
- let prev = from - 1;
730
- const initialGap = prev - this.lastQueuedSequenceNumber;
731
- let firstMissing: number | undefined;
732
- let duplicate = 0;
733
- let gap = 0;
734
-
735
- // Count all gaps and duplicates
736
- for (const message of messages) {
737
- if (message.sequenceNumber === prev) {
738
- duplicate++;
739
- } else if (message.sequenceNumber !== prev + 1) {
740
- gap++;
741
- if (firstMissing === undefined) {
742
- firstMissing = prev + 1;
743
- }
744
- }
745
- prev = message.sequenceNumber;
746
- }
747
-
748
- let eventName: string | undefined;
749
-
750
- // Report if we found some issues
751
- if (duplicate !== 0 || gap !== 0 && !allowGaps || initialGap > 0 && this.fetchReason === undefined) {
752
- eventName = "enqueueMessages";
753
- // Also report if we are fetching ops, and same range comes in, thus making this fetch obsolete.
754
- } else if (this.fetchReason !== undefined && this.fetchReason !== reason &&
755
- (from <= this.lastQueuedSequenceNumber + 1 && last > this.lastQueuedSequenceNumber)) {
756
- eventName = "enqueueMessagesExtraFetch";
757
- }
758
-
759
- // Report if there is something to report
760
- // Do not report when pending fetch is in progress, as such reporting will not
761
- // correctly take into account pending ops.
762
- if (eventName !== undefined) {
763
- this.logger.sendPerformanceEvent({
764
- eventName,
765
- reason,
766
- previousReason: this.prevEnqueueMessagesReason,
767
- from,
768
- to: last + 1, // exclusive, being consistent with the other telemetry / APIs
769
- length: messages.length,
770
- fetchReason: this.fetchReason,
771
- duplicate: duplicate > 0 ? duplicate : undefined,
772
- initialGap: initialGap !== 0 ? initialGap : undefined,
773
- gap: gap > 0 ? gap : undefined,
774
- firstMissing,
775
- dmInitialSeqNumber: this.initialSequenceNumber,
776
- ...this.connectionManager.connectionVerboseProps,
777
- });
778
- }
779
- }
780
-
781
- this.updateLatestKnownOpSeqNumber(messages[messages.length - 1].sequenceNumber);
782
-
783
- const n = this.previouslyProcessedMessage?.sequenceNumber;
784
- assert(n === undefined || n === this.lastQueuedSequenceNumber,
785
- 0x0ec /* "Unexpected value for previously processed message's sequence number" */);
786
-
787
- for (const message of messages) {
788
- // Check that the messages are arriving in the expected order
789
- if (message.sequenceNumber <= this.lastQueuedSequenceNumber) {
790
- // Validate that we do not have data loss, i.e. sequencing is reset and started again
791
- // with numbers that this client already observed before.
792
- if (this.previouslyProcessedMessage?.sequenceNumber === message.sequenceNumber) {
793
- const message1 = this.comparableMessagePayload(this.previouslyProcessedMessage);
794
- const message2 = this.comparableMessagePayload(message);
795
- if (message1 !== message2) {
796
- const error = new NonRetryableError(
797
- // This looks like a data corruption but the culprit was that the file was overwritten
798
- // in storage. See PR #5882.
799
- // Likely to be an issue with Fluid Services. Content does not match previous client
800
- // knowledge about this file. If the file is overwritten for any reason, this error can be
801
- // hit. One example is that some clients could be submitting ops to two different service
802
- // instances such that the same sequence number is reused for two different ops.
803
- // pre-0.58 error message: twoMessagesWithSameSeqNumAndDifferentPayload
804
- "Found two messages with the same sequenceNumber but different payloads. Likely to be a "
805
- + "service issue",
806
- DriverErrorType.fileOverwrittenInStorage,
807
- {
808
- clientId: this.connectionManager.clientId,
809
- sequenceNumber: message.sequenceNumber,
810
- message1,
811
- message2,
812
- driverVersion: undefined,
813
- },
814
- );
815
- this.close(error);
816
- }
817
- }
818
- } else if (message.sequenceNumber !== this.lastQueuedSequenceNumber + 1) {
819
- this.pending.push(message);
820
- this.fetchMissingDeltas(reason, message.sequenceNumber);
821
- } else {
822
- this.lastQueuedSequenceNumber = message.sequenceNumber;
823
- this.previouslyProcessedMessage = message;
824
- this._inbound.push(message);
825
- }
826
- }
827
-
828
- // When / if we report a gap in ops in the future, we want telemetry to correctly reflect source
829
- // of prior ops. But if we have some out of order ops (this.pending), then reporting current reason
830
- // becomes not accurate, as the gap existed before current batch, so we should just report "unknown".
831
- this.prevEnqueueMessagesReason = this.pending.length > 0 ? "unknown" : reason;
832
- }
833
-
834
- private processInboundMessage(message: ISequencedDocumentMessage): void {
835
- const startTime = Date.now();
836
- assert(!this.currentlyProcessingOps, 0x3af /* Already processing ops. */);
837
- this.currentlyProcessingOps = true;
838
- this.lastProcessedMessage = message;
839
-
840
- const isString = typeof message.clientId === "string";
841
- assert(message.clientId === null || isString, 0x41a /* undefined or string */);
842
- // All client messages are coming from some client, and should have clientId,
843
- // and non-client message should not have clientId. But, there are two exceptions:
844
- // 1. (Legacy) We can see message.type === "attach" or "chunkedOp" for legacy files before RTM
845
- // 2. Non-immediate noops (contents: null) can be sent by service without clientId
846
- if (!isString && isClientMessage(message) && message.type !== MessageType.NoOp) {
847
- throw new DataCorruptionError("Mismatch in clientId",
848
- { ...extractSafePropertiesFromMessage(message), messageType: message.type });
849
- }
850
-
851
- // TODO Remove after SPO picks up the latest build.
852
- if (
853
- typeof message.contents === "string"
854
- && message.contents !== ""
855
- && message.type !== MessageType.ClientLeave
856
- ) {
857
- message.contents = JSON.parse(message.contents);
858
- }
859
-
860
- // Validate client sequence number has no gap. Decrement the noOpCount by gap
861
- // If the count ends up negative, that means we have a real gap and throw error
862
- if (this.connectionManager.clientId !== undefined && this.connectionManager.clientId === message.clientId) {
863
- if (message.type === MessageType.NoOp){
864
- this.noOpCount--;
865
- }
866
- const clientSeqNumGap = message.clientSequenceNumber - this.lastClientSequenceNumber - 1;
867
- this.noOpCount -= clientSeqNumGap;
868
- if (this.noOpCount < 0) {
869
- throw new Error(`gap in client sequence number: ${clientSeqNumGap}`);
870
- }
871
- this.lastClientSequenceNumber = message.clientSequenceNumber;
872
- }
873
-
874
- this.connectionManager.beforeProcessingIncomingOp(message);
875
-
876
- // Watch the minimum sequence number and be ready to update as needed
877
- if (this.minSequenceNumber > message.minimumSequenceNumber) {
878
- // pre-0.58 error message: msnMovesBackwards
879
- throw new DataCorruptionError("Found a lower minimumSequenceNumber (msn) than previously recorded", {
880
- ...extractSafePropertiesFromMessage(message),
881
- clientId: this.connectionManager.clientId,
882
- });
883
- }
884
-
885
- // Client ops: MSN has to be lower than sequence #, as client can continue to send ops with same
886
- // reference sequence number as this op.
887
- // System ops (when no clients are connected) are the only ops where equation is possible.
888
- const diff = message.sequenceNumber - message.minimumSequenceNumber;
889
- if (diff < 0 || diff === 0 && message.clientId !== null) {
890
- throw new DataCorruptionError("MSN has to be lower than sequence #",
891
- extractSafePropertiesFromMessage(message));
892
- }
893
-
894
- this.minSequenceNumber = message.minimumSequenceNumber;
895
-
896
- if (message.sequenceNumber !== this.lastProcessedSequenceNumber + 1) {
897
- // pre-0.58 error message: nonSequentialSequenceNumber
898
- throw new DataCorruptionError("Found a non-Sequential sequenceNumber", {
899
- ...extractSafePropertiesFromMessage(message),
900
- clientId: this.connectionManager.clientId,
901
- });
902
- }
903
- this.lastProcessedSequenceNumber = message.sequenceNumber;
904
-
905
- // a bunch of code assumes that this is true
906
- assert(this.lastProcessedSequenceNumber <= this.lastObservedSeqNumber,
907
- 0x267 /* "lastObservedSeqNumber should be updated first" */);
908
-
909
- // Back-compat for older server with no term
910
- if (message.term === undefined) {
911
- message.term = 1;
912
- }
913
- this.baseTerm = message.term;
914
-
915
- if (this.handler === undefined) {
916
- throw new Error("Attempted to process an inbound message without a handler attached");
917
- }
918
- this.handler.process(message);
919
- this.currentlyProcessingOps = false;
920
- const endTime = Date.now();
921
-
922
- // Should be last, after changing this.lastProcessedSequenceNumber above, as many callers
923
- // test this.lastProcessedSequenceNumber instead of using op.sequenceNumber itself.
924
- this.emit("op", message, endTime - startTime);
925
- }
926
-
927
- /**
928
- * Retrieves the missing deltas between the given sequence numbers
929
- */
930
- private fetchMissingDeltas(reasonArg: string, to?: number) {
931
- this.fetchMissingDeltasCore(reasonArg, false /* cacheOnly */, to).catch((error) => {
932
- this.logger.sendErrorEvent({ eventName: "fetchMissingDeltasException" }, error);
933
- });
934
- }
935
-
936
- /**
937
- * Retrieves the missing deltas between the given sequence numbers
938
- */
939
- private async fetchMissingDeltasCore(
940
- reason: string,
941
- cacheOnly: boolean,
942
- to?: number) {
943
- // Exit out early if we're already fetching deltas
944
- if (this.fetchReason !== undefined) {
945
- return;
946
- }
947
-
948
- if (this._closed) {
949
- this.logger.sendTelemetryEvent({ eventName: "fetchMissingDeltasClosedConnection", reason });
950
- return;
951
- }
952
-
953
- if (this.handler === undefined) {
954
- // We do not poses yet any information
955
- assert(this.lastQueuedSequenceNumber === 0, 0x26b /* "initial state" */);
956
- return;
957
- }
958
-
959
- try {
960
- let from = this.lastQueuedSequenceNumber + 1;
961
-
962
- const n = this.previouslyProcessedMessage?.sequenceNumber;
963
- if (n !== undefined) {
964
- // If we already processed at least one op, then we have this.previouslyProcessedMessage populated
965
- // and can use it to validate that we are operating on same file, i.e. it was not overwritten.
966
- // Knowing about this mechanism, we could ask for op we already observed to increase validation.
967
- // This is especially useful when coming out of offline mode or loading from
968
- // very old cached (by client / driver) snapshot.
969
- assert(n === this.lastQueuedSequenceNumber, 0x0f2 /* "previouslyProcessedMessage" */);
970
- assert(from > 1, 0x0f3 /* "not positive" */);
971
- from--;
972
- }
973
-
974
- const fetchReason = `${reason}_fetch`;
975
- this.fetchReason = fetchReason;
976
-
977
- await this.getDeltas(
978
- from,
979
- to,
980
- fetchReason,
981
- (messages) => {
982
- this.refreshDelayInfo(this.deltaStorageDelayId);
983
- this.enqueueMessages(messages, fetchReason);
984
- },
985
- cacheOnly);
986
- } catch (error) {
987
- this.logger.sendErrorEvent({ eventName: "GetDeltas_Exception" }, error);
988
- this.close(normalizeError(error));
989
- } finally {
990
- this.refreshDelayInfo(this.deltaStorageDelayId);
991
- this.fetchReason = undefined;
992
- this.processPendingOps(reason);
993
- }
994
- }
995
-
996
- /**
997
- * Sorts pending ops and attempts to apply them
998
- */
999
- private processPendingOps(reason?: string): void {
1000
- if (this._closed) {
1001
- return;
1002
- }
1003
-
1004
- assert(this.handler !== undefined, 0x26c /* "handler should be installed" */);
1005
-
1006
- const pendingSorted = this.pending.sort((a, b) => a.sequenceNumber - b.sequenceNumber);
1007
- this.pending = [];
1008
- // Given that we do not track where these ops came from any more, it's not very
1009
- // actionably to report gaps in this range.
1010
- this.enqueueMessages(pendingSorted, `${reason}_pending`, true /* allowGaps */);
1011
-
1012
- // Re-entrancy is ignored by fetchMissingDeltas, execution will come here when it's over
1013
- if (this.fetchReason === undefined) {
1014
- // See issue #7312 for more details
1015
- // We observe cases where client gets into situation where it is not aware of missing ops
1016
- // (i.e. client being behind), and as such, does not attempt to fetch them.
1017
- // In some cases client may not have enough signal (example - "read" connection that is silent -
1018
- // there is no easy way for client to realize it's behind, see a bit of commentary / logic at the
1019
- // end of setupNewSuccessfulConnection). In other cases it should be able to learn that info ("write"
1020
- // connection, learn by receiving its own join op), but data suggest it does not happen.
1021
- // In 50% of these cases we do know we are behind through checkpointSequenceNumber on connection object
1022
- // and thus can leverage that to trigger recovery. But this is not going to solve all the problems
1023
- // (the other 50%), and thus these errors below should be looked at even if code below results in
1024
- // recovery.
1025
- if (this.lastQueuedSequenceNumber < this.lastObservedSeqNumber) {
1026
- this.fetchMissingDeltas("OpsBehind");
1027
- }
1028
- }
1029
- }
1030
-
1031
- private updateLatestKnownOpSeqNumber(seq: number) {
1032
- if (this.lastObservedSeqNumber < seq) {
1033
- this.lastObservedSeqNumber = seq;
1034
- }
1035
- }
97
+ public get disposed() {
98
+ return this._closed;
99
+ }
100
+
101
+ public get IDeltaSender() {
102
+ return this;
103
+ }
104
+
105
+ private pending: ISequencedDocumentMessage[] = [];
106
+ private fetchReason: string | undefined;
107
+
108
+ // A boolean used to assert that ops are not being sent while processing another op.
109
+ private currentlyProcessingOps: boolean = false;
110
+
111
+ // The minimum sequence number and last sequence number received from the server
112
+ private minSequenceNumber: number = 0;
113
+
114
+ // There are three numbers we track
115
+ // * lastQueuedSequenceNumber is the last queued sequence number. If there are gaps in seq numbers, then this number
116
+ // is not updated until we cover that gap, so it increases each time by 1.
117
+ // * lastObservedSeqNumber is an estimation of last known sequence number for container in storage. It's initially
118
+ // populated at web socket connection time (if storage provides that info) and is updated once ops shows up.
119
+ // It's never less than lastQueuedSequenceNumber
120
+ // * lastProcessedSequenceNumber - last processed sequence number
121
+ private lastQueuedSequenceNumber: number = 0;
122
+ private lastObservedSeqNumber: number = 0;
123
+ private lastProcessedSequenceNumber: number = 0;
124
+ private lastProcessedMessage: ISequencedDocumentMessage | undefined;
125
+ private baseTerm: number = 0;
126
+
127
+ /** count number of noops sent by the client which may not be acked */
128
+ private noOpCount: number = 0;
129
+ /** Track clientSequenceNumber of the last op */
130
+ private lastClientSequenceNumber: number = 0;
131
+
132
+ /**
133
+ * Track down the ops size.
134
+ */
135
+ private opsSize: number = 0;
136
+ private prevEnqueueMessagesReason: string | undefined;
137
+ private previouslyProcessedMessage: ISequencedDocumentMessage | undefined;
138
+
139
+ // The sequence number we initially loaded from
140
+ // In case of reading from a snapshot or pending state, its value will be equal to
141
+ // the last message that got serialized.
142
+ private initSequenceNumber: number = 0;
143
+
144
+ private readonly _inbound: DeltaQueue<ISequencedDocumentMessage>;
145
+ private readonly _inboundSignal: DeltaQueue<ISignalMessage>;
146
+
147
+ private _closed = false;
148
+ private _disposed = false;
149
+
150
+ private handler: IDeltaHandlerStrategy | undefined;
151
+ private deltaStorage: IDocumentDeltaStorageService | undefined;
152
+
153
+ private readonly throttlingIdSet = new Set<string>();
154
+ private timeTillThrottling: number = 0;
155
+
156
+ public readonly closeAbortController = new AbortController();
157
+
158
+ private readonly deltaStorageDelayId = uuid();
159
+ private readonly deltaStreamDelayId = uuid();
160
+
161
+ private messageBuffer: IDocumentMessage[] = [];
162
+
163
+ private _checkpointSequenceNumber: number | undefined;
164
+
165
+ public get inbound(): IDeltaQueue<ISequencedDocumentMessage> {
166
+ return this._inbound;
167
+ }
168
+
169
+ public get inboundSignal(): IDeltaQueue<ISignalMessage> {
170
+ return this._inboundSignal;
171
+ }
172
+
173
+ public get initialSequenceNumber(): number {
174
+ return this.initSequenceNumber;
175
+ }
176
+
177
+ public get lastSequenceNumber(): number {
178
+ return this.lastProcessedSequenceNumber;
179
+ }
180
+
181
+ public get lastMessage() {
182
+ return this.lastProcessedMessage;
183
+ }
184
+
185
+ public get lastKnownSeqNumber() {
186
+ return this.lastObservedSeqNumber;
187
+ }
188
+
189
+ public get referenceTerm(): number {
190
+ return this.baseTerm;
191
+ }
192
+
193
+ public get minimumSequenceNumber(): number {
194
+ return this.minSequenceNumber;
195
+ }
196
+
197
+ /**
198
+ * Tells if current connection has checkpoint information.
199
+ * I.e. we know how far behind the client was at the time of establishing connection
200
+ */
201
+ public get hasCheckpointSequenceNumber() {
202
+ // Valid to be called only if we have active connection.
203
+ assert(this.connectionManager.connected, 0x0df /* "Missing active connection" */);
204
+ return this._checkpointSequenceNumber !== undefined;
205
+ }
206
+
207
+ // Forwarding connection manager properties / IDeltaManager implementation
208
+ public get maxMessageSize(): number {
209
+ return this.connectionManager.maxMessageSize;
210
+ }
211
+ public get version() {
212
+ return this.connectionManager.version;
213
+ }
214
+ public get serviceConfiguration() {
215
+ return this.connectionManager.serviceConfiguration;
216
+ }
217
+ public get outbound() {
218
+ return this.connectionManager.outbound;
219
+ }
220
+ public get readOnlyInfo() {
221
+ return this.connectionManager.readOnlyInfo;
222
+ }
223
+ public get clientDetails() {
224
+ return this.connectionManager.clientDetails;
225
+ }
226
+
227
+ public submit(
228
+ type: MessageType,
229
+ contents?: string,
230
+ batch = false,
231
+ metadata?: any,
232
+ compression?: string,
233
+ ) {
234
+ const messagePartial: Omit<IDocumentMessage, "clientSequenceNumber"> = {
235
+ contents,
236
+ metadata,
237
+ referenceSequenceNumber: this.lastProcessedSequenceNumber,
238
+ type,
239
+ compression,
240
+ };
241
+
242
+ if (!batch) {
243
+ this.flush();
244
+ }
245
+ const message = this.connectionManager.prepareMessageToSend(messagePartial);
246
+ if (message === undefined) {
247
+ return -1;
248
+ }
249
+
250
+ assert(isClientMessage(message), 0x419 /* client sends non-client message */);
251
+
252
+ if (contents !== undefined) {
253
+ this.opsSize += contents.length;
254
+ }
255
+
256
+ this.messageBuffer.push(message);
257
+
258
+ if (message.type === MessageType.NoOp) {
259
+ this.noOpCount++;
260
+ }
261
+
262
+ this.emit("submitOp", message);
263
+
264
+ if (!batch) {
265
+ this.flush();
266
+ }
267
+ return message.clientSequenceNumber;
268
+ }
269
+
270
+ public submitSignal(content: any) {
271
+ return this.connectionManager.submitSignal(content);
272
+ }
273
+
274
+ public flush() {
275
+ const batch = this.messageBuffer;
276
+ if (batch.length === 0) {
277
+ return;
278
+ }
279
+
280
+ this.messageBuffer = [];
281
+
282
+ // The prepareFlush event allows listeners to append metadata to the batch prior to submission.
283
+ this.emit("prepareSend", batch);
284
+
285
+ if (batch.length === 1) {
286
+ assert(
287
+ batch[0].metadata?.batch === undefined,
288
+ 0x3c9 /* no batch markup on single message */,
289
+ );
290
+ } else {
291
+ assert(batch[0].metadata?.batch === true, 0x3ca /* no start batch markup */);
292
+ assert(
293
+ batch[batch.length - 1].metadata?.batch === false,
294
+ 0x3cb /* no end batch markup */,
295
+ );
296
+ }
297
+
298
+ this.connectionManager.sendMessages(batch);
299
+
300
+ assert(this.messageBuffer.length === 0, 0x3cc /* reentrancy */);
301
+ }
302
+
303
+ public get connectionProps(): ITelemetryProperties {
304
+ return {
305
+ sequenceNumber: this.lastSequenceNumber,
306
+ opsSize: this.opsSize > 0 ? this.opsSize : undefined,
307
+ ...this.connectionManager.connectionProps,
308
+ };
309
+ }
310
+
311
+ /**
312
+ * Log error event with a bunch of internal to DeltaManager information about state of op processing
313
+ * Used to diagnose connectivity issues related to op processing (i.e. cases where for some reason
314
+ * we stop processing ops that results in no processing join op and thus moving to connected state)
315
+ * @param event - Event to log.
316
+ */
317
+ public logConnectionIssue(event: ITelemetryErrorEvent) {
318
+ assert(this.connectionManager.connected, 0x238 /* "called only in connected state" */);
319
+
320
+ const pendingSorted = this.pending.sort((a, b) => a.sequenceNumber - b.sequenceNumber);
321
+ this.logger.sendErrorEvent({
322
+ ...event,
323
+ // This directly tells us if fetching ops is in flight, and thus likely the reason of
324
+ // stalled op processing
325
+ fetchReason: this.fetchReason,
326
+ // A bunch of useful sequence numbers to understand if we are holding some ops from processing
327
+ lastQueuedSequenceNumber: this.lastQueuedSequenceNumber, // last sequential op
328
+ lastProcessedSequenceNumber: this.lastProcessedSequenceNumber, // same as above, but after processing
329
+ lastObserved: this.lastObservedSeqNumber, // last sequence we ever saw; may have gaps with above.
330
+ // connection info
331
+ ...this.connectionManager.connectionVerboseProps,
332
+ pendingOps: this.pending.length, // Do we have any pending ops?
333
+ pendingFirst: pendingSorted[0]?.sequenceNumber, // is the first pending op the one that we are missing?
334
+ haveHandler: this.handler !== undefined, // do we have handler installed?
335
+ inboundLength: this.inbound.length,
336
+ inboundPaused: this.inbound.paused,
337
+ });
338
+ }
339
+
340
+ constructor(
341
+ private readonly serviceProvider: () => IDocumentService | undefined,
342
+ private readonly logger: ITelemetryLogger,
343
+ private readonly _active: () => boolean,
344
+ createConnectionManager: (props: IConnectionManagerFactoryArgs) => TConnectionManager,
345
+ ) {
346
+ super();
347
+ const props: IConnectionManagerFactoryArgs = {
348
+ incomingOpHandler: (messages: ISequencedDocumentMessage[], reason: string) => {
349
+ try {
350
+ this.enqueueMessages(messages, reason);
351
+ } catch (error) {
352
+ this.logger.sendErrorEvent({ eventName: "EnqueueMessages_Exception" }, error);
353
+ this.close(normalizeError(error));
354
+ }
355
+ },
356
+ signalHandler: (message: ISignalMessage) => this._inboundSignal.push(message),
357
+ reconnectionDelayHandler: (delayMs: number, error: unknown) =>
358
+ this.emitDelayInfo(this.deltaStreamDelayId, delayMs, error),
359
+ closeHandler: (error: any) => this.close(error),
360
+ disconnectHandler: (reason: string) => this.disconnectHandler(reason),
361
+ connectHandler: (connection: IConnectionDetails) => this.connectHandler(connection),
362
+ pongHandler: (latency: number) => this.emit("pong", latency),
363
+ readonlyChangeHandler: (readonly?: boolean) =>
364
+ safeRaiseEvent(this, this.logger, "readonly", readonly),
365
+ };
366
+
367
+ this.connectionManager = createConnectionManager(props);
368
+ this._inbound = new DeltaQueue<ISequencedDocumentMessage>((op) => {
369
+ this.processInboundMessage(op);
370
+ });
371
+
372
+ this._inbound.on("error", (error) => {
373
+ this.close(
374
+ DataProcessingError.wrapIfUnrecognized(
375
+ error,
376
+ "deltaManagerInboundErrorHandler",
377
+ this.lastMessage,
378
+ ),
379
+ );
380
+ });
381
+
382
+ // Inbound signal queue
383
+ this._inboundSignal = new DeltaQueue<ISignalMessage>((message) => {
384
+ if (this.handler === undefined) {
385
+ throw new Error(
386
+ "Attempted to process an inbound signal without a handler attached",
387
+ );
388
+ }
389
+ this.handler.processSignal({
390
+ clientId: message.clientId,
391
+ content: JSON.parse(message.content as string),
392
+ });
393
+ });
394
+
395
+ this._inboundSignal.on("error", (error) => {
396
+ this.close(normalizeError(error));
397
+ });
398
+
399
+ // Initially, all queues are created paused.
400
+ // - outbound is flipped back and forth in setupNewSuccessfulConnection / disconnectFromDeltaStream
401
+ // - inbound & inboundSignal are resumed in attachOpHandler() when we have handler setup
402
+ }
403
+
404
+ private connectHandler(connection: IConnectionDetails) {
405
+ this.refreshDelayInfo(this.deltaStreamDelayId);
406
+
407
+ const props = this.connectionManager.connectionVerboseProps;
408
+ props.connectionLastQueuedSequenceNumber = this.lastQueuedSequenceNumber;
409
+ props.connectionLastObservedSeqNumber = this.lastObservedSeqNumber;
410
+
411
+ const checkpointSequenceNumber = connection.checkpointSequenceNumber;
412
+ this._checkpointSequenceNumber = checkpointSequenceNumber;
413
+ if (checkpointSequenceNumber !== undefined) {
414
+ this.updateLatestKnownOpSeqNumber(checkpointSequenceNumber);
415
+ }
416
+
417
+ // We cancel all ops on lost of connectivity, and rely on DDSes to resubmit them.
418
+ // Semantics are not well defined for batches (and they are broken right now on disconnects anyway),
419
+ // but it's safe to assume (until better design is put into place) that batches should not exist
420
+ // across multiple connections. Right now we assume runtime will not submit any ops in disconnected
421
+ // state. As requirements change, so should these checks.
422
+ assert(
423
+ this.messageBuffer.length === 0,
424
+ 0x0e9 /* "messageBuffer is not empty on new connection" */,
425
+ );
426
+
427
+ this.opsSize = 0;
428
+ this.noOpCount = 0;
429
+
430
+ this.emit(
431
+ "connect",
432
+ connection,
433
+ checkpointSequenceNumber !== undefined
434
+ ? this.lastObservedSeqNumber - this.lastSequenceNumber
435
+ : undefined,
436
+ );
437
+
438
+ // If we got some initial ops, then we know the gap and call above fetched ops to fill it.
439
+ // Same is true for "write" mode even if we have no ops - we will get "join" own op very very soon.
440
+ // However if we are connecting as view-only, then there is no good signal to realize if client is behind.
441
+ // Thus we have to hit storage to see if any ops are there.
442
+ if (checkpointSequenceNumber !== undefined) {
443
+ // We know how far we are behind (roughly). If it's non-zero gap, fetch ops right away.
444
+ if (checkpointSequenceNumber > this.lastQueuedSequenceNumber) {
445
+ this.fetchMissingDeltas("AfterConnection");
446
+ }
447
+ // we do not know the gap, and we will not learn about it if socket is quite - have to ask.
448
+ } else if (connection.mode === "read") {
449
+ this.fetchMissingDeltas("AfterReadConnection");
450
+ }
451
+ }
452
+
453
+ public dispose() {
454
+ throw new Error("Not implemented.");
455
+ }
456
+
457
+ /**
458
+ * Sets the sequence number from which inbound messages should be returned
459
+ */
460
+ public async attachOpHandler(
461
+ minSequenceNumber: number,
462
+ sequenceNumber: number,
463
+ term: number,
464
+ handler: IDeltaHandlerStrategy,
465
+ prefetchType: "cached" | "all" | "none" = "none",
466
+ ) {
467
+ this.initSequenceNumber = sequenceNumber;
468
+ this.lastProcessedSequenceNumber = sequenceNumber;
469
+ this.baseTerm = term;
470
+ this.minSequenceNumber = minSequenceNumber;
471
+ this.lastQueuedSequenceNumber = sequenceNumber;
472
+ this.lastObservedSeqNumber = sequenceNumber;
473
+
474
+ // We will use same check in other places to make sure all the seq number above are set properly.
475
+ assert(
476
+ this.handler === undefined,
477
+ 0x0e2 /* "DeltaManager already has attached op handler!" */,
478
+ );
479
+ this.handler = handler;
480
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
481
+ assert(!!(this.handler as any), 0x0e3 /* "Newly set op handler is null/undefined!" */);
482
+
483
+ // There should be no pending fetch!
484
+ // This API is called right after attachOpHandler by Container.load().
485
+ // We might have connection already and it might have called fetchMissingDeltas() from
486
+ // setupNewSuccessfulConnection. But it should do nothing, because there is no way to fetch ops before
487
+ // we know snapshot sequence number that is set in attachOpHandler. So all such calls should be noop.
488
+ assert(
489
+ this.fetchReason === undefined,
490
+ 0x268 /* "There can't be pending fetch that early in boot sequence!" */,
491
+ );
492
+
493
+ if (this._closed) {
494
+ return;
495
+ }
496
+
497
+ this._inbound.resume();
498
+ this._inboundSignal.resume();
499
+
500
+ if (prefetchType !== "none") {
501
+ const cacheOnly = prefetchType === "cached";
502
+ await this.fetchMissingDeltasCore(`DocumentOpen_${prefetchType}`, cacheOnly);
503
+
504
+ // Keep going with fetching ops from storage once we have all cached ops in.
505
+ // But do not block load and make this request async / not blocking this api.
506
+ // Ops processing will start once cached ops are in and and will stop when queue is empty
507
+ // (which in most cases will happen when we are done processing cached ops)
508
+ if (cacheOnly) {
509
+ // fire and forget
510
+ this.fetchMissingDeltas("PostDocumentOpen");
511
+ }
512
+ }
513
+
514
+ // Ensure there is no need to call this.processPendingOps() at the end of boot sequence
515
+ assert(
516
+ this.fetchReason !== undefined || this.pending.length === 0,
517
+ 0x269 /* "pending ops are not dropped" */,
518
+ );
519
+ }
520
+
521
+ public connect(args: IConnectionArgs) {
522
+ const fetchOpsFromStorage = args.fetchOpsFromStorage ?? true;
523
+ logIfFalse(
524
+ this.handler !== undefined || !fetchOpsFromStorage,
525
+ this.logger,
526
+ "CantFetchWithoutBaseline",
527
+ ); // can't fetch if no baseline
528
+
529
+ // Note: There is race condition here.
530
+ // We want to issue request to storage as soon as possible, to
531
+ // reduce latency of becoming current, thus this code here.
532
+ // But there is no ordering between fetching OPs and connection to delta stream
533
+ // As result, we might be behind by the time we connect to delta stream
534
+ // In case of r/w connection, that's not an issue, because we will hear our
535
+ // own "join" message and realize any gap client has in ops.
536
+ // But for view-only connection, we have no such signal, and with no traffic
537
+ // on the wire, we might be always behind.
538
+ // See comment at the end of "connect" handler
539
+ if (fetchOpsFromStorage) {
540
+ this.fetchMissingDeltas(args.reason);
541
+ }
542
+
543
+ this.connectionManager.connect(args.mode);
544
+ }
545
+
546
+ private async getDeltas(
547
+ from: number, // inclusive
548
+ to: number | undefined, // exclusive
549
+ fetchReason: string,
550
+ callback: (messages: ISequencedDocumentMessage[]) => void,
551
+ cacheOnly: boolean,
552
+ ) {
553
+ const docService = this.serviceProvider();
554
+ if (docService === undefined) {
555
+ throw new Error("Delta manager is not attached");
556
+ }
557
+
558
+ if (this.deltaStorage === undefined) {
559
+ this.deltaStorage = await docService.connectToDeltaStorage();
560
+ }
561
+
562
+ let cancelFetch: (op: ISequencedDocumentMessage) => boolean;
563
+
564
+ if (to !== undefined) {
565
+ const lastExpectedOp = to - 1; // make it inclusive!
566
+
567
+ // It is possible that due to asynchrony (including await above), required ops were already
568
+ // received through delta stream. Validate that before moving forward.
569
+ if (this.lastQueuedSequenceNumber >= lastExpectedOp) {
570
+ this.logger.sendPerformanceEvent({
571
+ reason: fetchReason,
572
+ eventName: "ExtraStorageCall",
573
+ early: true,
574
+ from,
575
+ to,
576
+ ...this.connectionManager.connectionVerboseProps,
577
+ });
578
+ return;
579
+ }
580
+
581
+ // Be prepared for the case where webSocket would receive the ops that we are trying to fill through
582
+ // storage. Ideally it should never happen (i.e. ops on socket are always ordered, and thus once we
583
+ // detected gap, this gap can't be filled in later on through websocket).
584
+ // And in practice that does look like the case. The place where this code gets hit is if we lost
585
+ // connection and reconnected (likely to another box), and new socket's initial ops contains these ops.
586
+ cancelFetch = (op: ISequencedDocumentMessage) => op.sequenceNumber >= lastExpectedOp;
587
+ } else {
588
+ // Unbound requests are made to proactively fetch ops, but also get up to date in cases where socket
589
+ // is silent (and connection is "read", thus we might not have any data on how far client is behind).
590
+ // Once we have any op coming in from socket, we can cancel it as it's not needed any more.
591
+ // That said, if we have socket connection, make sure we got ops up to checkpointSequenceNumber!
592
+ cancelFetch = (op: ISequencedDocumentMessage) =>
593
+ op.sequenceNumber >= this.lastObservedSeqNumber;
594
+ }
595
+
596
+ const controller = new AbortController();
597
+ let opsFromFetch = false;
598
+
599
+ const opListener = (op: ISequencedDocumentMessage) => {
600
+ assert(op.sequenceNumber === this.lastQueuedSequenceNumber, 0x23a /* "seq#'s" */);
601
+ // Ops that are coming from this request should not cancel itself.
602
+ // This is useless for known ranges (to is defined) as it means request is over either way.
603
+ // And it will cancel unbound request too early, not allowing us to learn where the end of the file is.
604
+ if (!opsFromFetch && cancelFetch(op)) {
605
+ controller.abort();
606
+ this._inbound.off("push", opListener);
607
+ }
608
+ };
609
+
610
+ try {
611
+ this._inbound.on("push", opListener);
612
+ assert(this.closeAbortController.signal.onabort === null, 0x1e8 /* "reentrancy" */);
613
+ this.closeAbortController.signal.onabort = () => controller.abort();
614
+
615
+ const stream = this.deltaStorage.fetchMessages(
616
+ from, // inclusive
617
+ to, // exclusive
618
+ controller.signal,
619
+ cacheOnly,
620
+ fetchReason,
621
+ );
622
+
623
+ // eslint-disable-next-line no-constant-condition
624
+ while (true) {
625
+ const result = await stream.read();
626
+ if (result.done) {
627
+ break;
628
+ }
629
+ try {
630
+ opsFromFetch = true;
631
+ callback(result.value);
632
+ } finally {
633
+ opsFromFetch = false;
634
+ }
635
+ }
636
+ } finally {
637
+ this.closeAbortController.signal.onabort = null;
638
+ this._inbound.off("push", opListener);
639
+ assert(!opsFromFetch, 0x289 /* "logic error" */);
640
+ }
641
+ }
642
+
643
+ /**
644
+ * Closes the connection and clears inbound & outbound queues.
645
+ *
646
+ * @param doDispose - should the DeltaManager treat this close call as a dispose?
647
+ * Differences between close and dispose:
648
+ * - dispose will emit "disposed" event while close emits "closed"
649
+ * - dispose will remove all listeners
650
+ * - dispose can be called after closure, but not vis versa
651
+ */
652
+ public close(error?: ICriticalContainerError, doDispose?: boolean): void {
653
+ if (this._closed) {
654
+ if (doDispose === true) {
655
+ this.disposeInternal(error);
656
+ }
657
+ return;
658
+ }
659
+ this._closed = true;
660
+
661
+ this.connectionManager.dispose(error, doDispose !== true);
662
+
663
+ this.closeAbortController.abort();
664
+
665
+ this._inbound.clear();
666
+ this._inboundSignal.clear();
667
+
668
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
669
+ this._inbound.pause();
670
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
671
+ this._inboundSignal.pause();
672
+
673
+ // Drop pending messages - this will ensure catchUp() does not go into infinite loop
674
+ this.pending = [];
675
+
676
+ if (doDispose === true) {
677
+ this.disposeInternal(error);
678
+ } else {
679
+ this.emit("closed", error);
680
+ this.disposeInternal(error); // ! TODO: remove this call when Container close no longer disposes
681
+ }
682
+ }
683
+
684
+ private disposeInternal(error?: ICriticalContainerError): void {
685
+ if (this._disposed) {
686
+ return;
687
+ }
688
+ this._disposed = true;
689
+
690
+ // This needs to be the last thing we do (before removing listeners), as it causes
691
+ // Container to dispose context and break ability of data stores / runtime to "hear"
692
+ // from delta manager, including notification (above) about readonly state.
693
+ this.emit("disposed", error);
694
+ this.removeAllListeners();
695
+ }
696
+
697
+ public refreshDelayInfo(id: string) {
698
+ this.throttlingIdSet.delete(id);
699
+ if (this.throttlingIdSet.size === 0) {
700
+ this.timeTillThrottling = 0;
701
+ }
702
+ }
703
+
704
+ private disconnectHandler(reason: string) {
705
+ this.messageBuffer.length = 0;
706
+ this.emit("disconnect", reason);
707
+ }
708
+
709
+ /**
710
+ * Emit info about a delay in service communication on account of throttling.
711
+ * @param id - Id of the connection that is delayed
712
+ * @param delayMs - Duration of the delay
713
+ * @param error - error object indicating the throttling
714
+ */
715
+ public emitDelayInfo(id: string, delayMs: number, error: unknown) {
716
+ const timeNow = Date.now();
717
+ this.throttlingIdSet.add(id);
718
+ if (delayMs > 0 && timeNow + delayMs > this.timeTillThrottling) {
719
+ this.timeTillThrottling = timeNow + delayMs;
720
+
721
+ const throttlingWarning: IThrottlingWarning = ThrottlingWarning.wrap(
722
+ error,
723
+ delayMs / 1000 /* retryAfterSeconds */,
724
+ this.logger,
725
+ );
726
+ this.emit("throttled", throttlingWarning);
727
+ }
728
+ }
729
+
730
+ // returns parts of message (in string format) that should never change for a given message.
731
+ // Used for message comparison. It attempts to avoid comparing fields that potentially may differ.
732
+ // for example, it's not clear if serverMetadata or timestamp property is a property of message or server state.
733
+ // We only extract the most obvious fields that are sufficient (with high probability) to detect sequence number
734
+ // reuse.
735
+ // Also payload goes to telemetry, so no PII, including content!!
736
+ // Note: It's possible for a duplicate op to be broadcasted and have everything the same except the timestamp.
737
+ private comparableMessagePayload(m: ISequencedDocumentMessage) {
738
+ return `${m.clientId}-${m.type}-${m.minimumSequenceNumber}-${m.referenceSequenceNumber}-${m.timestamp}`;
739
+ }
740
+
741
+ private enqueueMessages(
742
+ messages: ISequencedDocumentMessage[],
743
+ reason: string,
744
+ allowGaps = false,
745
+ ): void {
746
+ if (this.handler === undefined) {
747
+ // We did not setup handler yet.
748
+ // This happens when we connect to web socket faster than we get attributes for container
749
+ // and thus faster than attachOpHandler() is called
750
+ // this.lastProcessedSequenceNumber is still zero, so we can't rely on this.fetchMissingDeltas()
751
+ // to do the right thing.
752
+ this.pending = this.pending.concat(messages);
753
+ return;
754
+ }
755
+
756
+ // Pending ops should never just hang around for nothing.
757
+ // This invariant will stay true through this function execution,
758
+ // so there is no need to process pending ops here.
759
+ // It's responsibility of
760
+ // - attachOpHandler()
761
+ // - fetchMissingDeltas() after it's done with querying storage
762
+ assert(
763
+ this.pending.length === 0 || this.fetchReason !== undefined,
764
+ 0x1e9 /* "Pending ops" */,
765
+ );
766
+
767
+ if (messages.length === 0) {
768
+ return;
769
+ }
770
+
771
+ const from = messages[0].sequenceNumber;
772
+ const last = messages[messages.length - 1].sequenceNumber;
773
+
774
+ // Report stats about missing and duplicate ops
775
+ // This helps better understand why we fetch ops from storage, and thus may delay
776
+ // getting current / sending ops
777
+ // It's possible that this batch is already too late - do not bother
778
+ if (last > this.lastQueuedSequenceNumber) {
779
+ let prev = from - 1;
780
+ const initialGap = prev - this.lastQueuedSequenceNumber;
781
+ let firstMissing: number | undefined;
782
+ let duplicate = 0;
783
+ let gap = 0;
784
+
785
+ // Count all gaps and duplicates
786
+ for (const message of messages) {
787
+ if (message.sequenceNumber === prev) {
788
+ duplicate++;
789
+ } else if (message.sequenceNumber !== prev + 1) {
790
+ gap++;
791
+ if (firstMissing === undefined) {
792
+ firstMissing = prev + 1;
793
+ }
794
+ }
795
+ prev = message.sequenceNumber;
796
+ }
797
+
798
+ let eventName: string | undefined;
799
+
800
+ // Report if we found some issues
801
+ if (
802
+ duplicate !== 0 ||
803
+ (gap !== 0 && !allowGaps) ||
804
+ (initialGap > 0 && this.fetchReason === undefined)
805
+ ) {
806
+ eventName = "enqueueMessages";
807
+ // Also report if we are fetching ops, and same range comes in, thus making this fetch obsolete.
808
+ } else if (
809
+ this.fetchReason !== undefined &&
810
+ this.fetchReason !== reason &&
811
+ from <= this.lastQueuedSequenceNumber + 1 &&
812
+ last > this.lastQueuedSequenceNumber
813
+ ) {
814
+ eventName = "enqueueMessagesExtraFetch";
815
+ }
816
+
817
+ // Report if there is something to report
818
+ // Do not report when pending fetch is in progress, as such reporting will not
819
+ // correctly take into account pending ops.
820
+ if (eventName !== undefined) {
821
+ this.logger.sendPerformanceEvent({
822
+ eventName,
823
+ reason,
824
+ previousReason: this.prevEnqueueMessagesReason,
825
+ from,
826
+ to: last + 1, // exclusive, being consistent with the other telemetry / APIs
827
+ length: messages.length,
828
+ fetchReason: this.fetchReason,
829
+ duplicate: duplicate > 0 ? duplicate : undefined,
830
+ initialGap: initialGap !== 0 ? initialGap : undefined,
831
+ gap: gap > 0 ? gap : undefined,
832
+ firstMissing,
833
+ dmInitialSeqNumber: this.initialSequenceNumber,
834
+ ...this.connectionManager.connectionVerboseProps,
835
+ });
836
+ }
837
+ }
838
+
839
+ this.updateLatestKnownOpSeqNumber(messages[messages.length - 1].sequenceNumber);
840
+
841
+ const n = this.previouslyProcessedMessage?.sequenceNumber;
842
+ assert(
843
+ n === undefined || n === this.lastQueuedSequenceNumber,
844
+ 0x0ec /* "Unexpected value for previously processed message's sequence number" */,
845
+ );
846
+
847
+ for (const message of messages) {
848
+ // Check that the messages are arriving in the expected order
849
+ if (message.sequenceNumber <= this.lastQueuedSequenceNumber) {
850
+ // Validate that we do not have data loss, i.e. sequencing is reset and started again
851
+ // with numbers that this client already observed before.
852
+ if (this.previouslyProcessedMessage?.sequenceNumber === message.sequenceNumber) {
853
+ const message1 = this.comparableMessagePayload(this.previouslyProcessedMessage);
854
+ const message2 = this.comparableMessagePayload(message);
855
+ if (message1 !== message2) {
856
+ const error = new NonRetryableError(
857
+ // This looks like a data corruption but the culprit was that the file was overwritten
858
+ // in storage. See PR #5882.
859
+ // Likely to be an issue with Fluid Services. Content does not match previous client
860
+ // knowledge about this file. If the file is overwritten for any reason, this error can be
861
+ // hit. One example is that some clients could be submitting ops to two different service
862
+ // instances such that the same sequence number is reused for two different ops.
863
+ // pre-0.58 error message: twoMessagesWithSameSeqNumAndDifferentPayload
864
+ "Found two messages with the same sequenceNumber but different payloads. Likely to be a " +
865
+ "service issue",
866
+ DriverErrorType.fileOverwrittenInStorage,
867
+ {
868
+ clientId: this.connectionManager.clientId,
869
+ sequenceNumber: message.sequenceNumber,
870
+ message1,
871
+ message2,
872
+ driverVersion: undefined,
873
+ },
874
+ );
875
+ this.close(error);
876
+ }
877
+ }
878
+ } else if (message.sequenceNumber !== this.lastQueuedSequenceNumber + 1) {
879
+ this.pending.push(message);
880
+ this.fetchMissingDeltas(reason, message.sequenceNumber);
881
+ } else {
882
+ this.lastQueuedSequenceNumber = message.sequenceNumber;
883
+ this.previouslyProcessedMessage = message;
884
+ this._inbound.push(message);
885
+ }
886
+ }
887
+
888
+ // When / if we report a gap in ops in the future, we want telemetry to correctly reflect source
889
+ // of prior ops. But if we have some out of order ops (this.pending), then reporting current reason
890
+ // becomes not accurate, as the gap existed before current batch, so we should just report "unknown".
891
+ this.prevEnqueueMessagesReason = this.pending.length > 0 ? "unknown" : reason;
892
+ }
893
+
894
+ private processInboundMessage(message: ISequencedDocumentMessage): void {
895
+ const startTime = Date.now();
896
+ assert(!this.currentlyProcessingOps, 0x3af /* Already processing ops. */);
897
+ this.currentlyProcessingOps = true;
898
+ this.lastProcessedMessage = message;
899
+
900
+ const isString = typeof message.clientId === "string";
901
+ assert(message.clientId === null || isString, 0x41a /* undefined or string */);
902
+ // All client messages are coming from some client, and should have clientId,
903
+ // and non-client message should not have clientId. But, there are two exceptions:
904
+ // 1. (Legacy) We can see message.type === "attach" or "chunkedOp" for legacy files before RTM
905
+ // 2. Non-immediate noops (contents: null) can be sent by service without clientId
906
+ if (!isString && isClientMessage(message) && message.type !== MessageType.NoOp) {
907
+ throw new DataCorruptionError("Mismatch in clientId", {
908
+ ...extractSafePropertiesFromMessage(message),
909
+ messageType: message.type,
910
+ });
911
+ }
912
+
913
+ // TODO Remove after SPO picks up the latest build.
914
+ if (
915
+ typeof message.contents === "string" &&
916
+ message.contents !== "" &&
917
+ message.type !== MessageType.ClientLeave
918
+ ) {
919
+ message.contents = JSON.parse(message.contents);
920
+ }
921
+
922
+ // Validate client sequence number has no gap. Decrement the noOpCount by gap
923
+ // If the count ends up negative, that means we have a real gap and throw error
924
+ if (
925
+ this.connectionManager.clientId !== undefined &&
926
+ this.connectionManager.clientId === message.clientId
927
+ ) {
928
+ if (message.type === MessageType.NoOp) {
929
+ this.noOpCount--;
930
+ }
931
+ const clientSeqNumGap =
932
+ message.clientSequenceNumber - this.lastClientSequenceNumber - 1;
933
+ this.noOpCount -= clientSeqNumGap;
934
+ if (this.noOpCount < 0) {
935
+ throw new Error(`gap in client sequence number: ${clientSeqNumGap}`);
936
+ }
937
+ this.lastClientSequenceNumber = message.clientSequenceNumber;
938
+ }
939
+
940
+ this.connectionManager.beforeProcessingIncomingOp(message);
941
+
942
+ // Watch the minimum sequence number and be ready to update as needed
943
+ if (this.minSequenceNumber > message.minimumSequenceNumber) {
944
+ // pre-0.58 error message: msnMovesBackwards
945
+ throw new DataCorruptionError(
946
+ "Found a lower minimumSequenceNumber (msn) than previously recorded",
947
+ {
948
+ ...extractSafePropertiesFromMessage(message),
949
+ clientId: this.connectionManager.clientId,
950
+ },
951
+ );
952
+ }
953
+
954
+ // Client ops: MSN has to be lower than sequence #, as client can continue to send ops with same
955
+ // reference sequence number as this op.
956
+ // System ops (when no clients are connected) are the only ops where equation is possible.
957
+ const diff = message.sequenceNumber - message.minimumSequenceNumber;
958
+ if (diff < 0 || (diff === 0 && message.clientId !== null)) {
959
+ throw new DataCorruptionError(
960
+ "MSN has to be lower than sequence #",
961
+ extractSafePropertiesFromMessage(message),
962
+ );
963
+ }
964
+
965
+ this.minSequenceNumber = message.minimumSequenceNumber;
966
+
967
+ if (message.sequenceNumber !== this.lastProcessedSequenceNumber + 1) {
968
+ // pre-0.58 error message: nonSequentialSequenceNumber
969
+ throw new DataCorruptionError("Found a non-Sequential sequenceNumber", {
970
+ ...extractSafePropertiesFromMessage(message),
971
+ clientId: this.connectionManager.clientId,
972
+ });
973
+ }
974
+ this.lastProcessedSequenceNumber = message.sequenceNumber;
975
+
976
+ // a bunch of code assumes that this is true
977
+ assert(
978
+ this.lastProcessedSequenceNumber <= this.lastObservedSeqNumber,
979
+ 0x267 /* "lastObservedSeqNumber should be updated first" */,
980
+ );
981
+
982
+ // Back-compat for older server with no term
983
+ if (message.term === undefined) {
984
+ message.term = 1;
985
+ }
986
+ this.baseTerm = message.term;
987
+
988
+ if (this.handler === undefined) {
989
+ throw new Error("Attempted to process an inbound message without a handler attached");
990
+ }
991
+ this.handler.process(message);
992
+ this.currentlyProcessingOps = false;
993
+ const endTime = Date.now();
994
+
995
+ // Should be last, after changing this.lastProcessedSequenceNumber above, as many callers
996
+ // test this.lastProcessedSequenceNumber instead of using op.sequenceNumber itself.
997
+ this.emit("op", message, endTime - startTime);
998
+ }
999
+
1000
+ /**
1001
+ * Retrieves the missing deltas between the given sequence numbers
1002
+ */
1003
+ private fetchMissingDeltas(reasonArg: string, to?: number) {
1004
+ this.fetchMissingDeltasCore(reasonArg, false /* cacheOnly */, to).catch((error) => {
1005
+ this.logger.sendErrorEvent({ eventName: "fetchMissingDeltasException" }, error);
1006
+ });
1007
+ }
1008
+
1009
+ /**
1010
+ * Retrieves the missing deltas between the given sequence numbers
1011
+ */
1012
+ private async fetchMissingDeltasCore(reason: string, cacheOnly: boolean, to?: number) {
1013
+ // Exit out early if we're already fetching deltas
1014
+ if (this.fetchReason !== undefined) {
1015
+ return;
1016
+ }
1017
+
1018
+ if (this._closed) {
1019
+ this.logger.sendTelemetryEvent({
1020
+ eventName: "fetchMissingDeltasClosedConnection",
1021
+ reason,
1022
+ });
1023
+ return;
1024
+ }
1025
+
1026
+ if (this.handler === undefined) {
1027
+ // We do not poses yet any information
1028
+ assert(this.lastQueuedSequenceNumber === 0, 0x26b /* "initial state" */);
1029
+ return;
1030
+ }
1031
+
1032
+ try {
1033
+ let from = this.lastQueuedSequenceNumber + 1;
1034
+
1035
+ const n = this.previouslyProcessedMessage?.sequenceNumber;
1036
+ if (n !== undefined) {
1037
+ // If we already processed at least one op, then we have this.previouslyProcessedMessage populated
1038
+ // and can use it to validate that we are operating on same file, i.e. it was not overwritten.
1039
+ // Knowing about this mechanism, we could ask for op we already observed to increase validation.
1040
+ // This is especially useful when coming out of offline mode or loading from
1041
+ // very old cached (by client / driver) snapshot.
1042
+ assert(
1043
+ n === this.lastQueuedSequenceNumber,
1044
+ 0x0f2 /* "previouslyProcessedMessage" */,
1045
+ );
1046
+ assert(from > 1, 0x0f3 /* "not positive" */);
1047
+ from--;
1048
+ }
1049
+
1050
+ const fetchReason = `${reason}_fetch`;
1051
+ this.fetchReason = fetchReason;
1052
+
1053
+ await this.getDeltas(
1054
+ from,
1055
+ to,
1056
+ fetchReason,
1057
+ (messages) => {
1058
+ this.refreshDelayInfo(this.deltaStorageDelayId);
1059
+ this.enqueueMessages(messages, fetchReason);
1060
+ },
1061
+ cacheOnly,
1062
+ );
1063
+ } catch (error) {
1064
+ this.logger.sendErrorEvent({ eventName: "GetDeltas_Exception" }, error);
1065
+ this.close(normalizeError(error));
1066
+ } finally {
1067
+ this.refreshDelayInfo(this.deltaStorageDelayId);
1068
+ this.fetchReason = undefined;
1069
+ this.processPendingOps(reason);
1070
+ }
1071
+ }
1072
+
1073
+ /**
1074
+ * Sorts pending ops and attempts to apply them
1075
+ */
1076
+ private processPendingOps(reason?: string): void {
1077
+ if (this._closed) {
1078
+ return;
1079
+ }
1080
+
1081
+ assert(this.handler !== undefined, 0x26c /* "handler should be installed" */);
1082
+
1083
+ const pendingSorted = this.pending.sort((a, b) => a.sequenceNumber - b.sequenceNumber);
1084
+ this.pending = [];
1085
+ // Given that we do not track where these ops came from any more, it's not very
1086
+ // actionably to report gaps in this range.
1087
+ this.enqueueMessages(pendingSorted, `${reason}_pending`, true /* allowGaps */);
1088
+
1089
+ // Re-entrancy is ignored by fetchMissingDeltas, execution will come here when it's over
1090
+ if (this.fetchReason === undefined) {
1091
+ // See issue #7312 for more details
1092
+ // We observe cases where client gets into situation where it is not aware of missing ops
1093
+ // (i.e. client being behind), and as such, does not attempt to fetch them.
1094
+ // In some cases client may not have enough signal (example - "read" connection that is silent -
1095
+ // there is no easy way for client to realize it's behind, see a bit of commentary / logic at the
1096
+ // end of setupNewSuccessfulConnection). In other cases it should be able to learn that info ("write"
1097
+ // connection, learn by receiving its own join op), but data suggest it does not happen.
1098
+ // In 50% of these cases we do know we are behind through checkpointSequenceNumber on connection object
1099
+ // and thus can leverage that to trigger recovery. But this is not going to solve all the problems
1100
+ // (the other 50%), and thus these errors below should be looked at even if code below results in
1101
+ // recovery.
1102
+ if (this.lastQueuedSequenceNumber < this.lastObservedSeqNumber) {
1103
+ this.fetchMissingDeltas("OpsBehind");
1104
+ }
1105
+ }
1106
+ }
1107
+
1108
+ private updateLatestKnownOpSeqNumber(seq: number) {
1109
+ if (this.lastObservedSeqNumber < seq) {
1110
+ this.lastObservedSeqNumber = seq;
1111
+ }
1112
+ }
1036
1113
  }