@fluidframework/container-loader 2.0.0-dev.2.3.0.115467 → 2.0.0-dev.4.1.0.148229

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