@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.
- package/.eslintrc.js +18 -21
- package/.mocharc.js +2 -2
- package/README.md +45 -43
- package/api-extractor.json +2 -2
- package/closeAndGetPendingLocalState.md +51 -0
- package/dist/audience.d.ts.map +1 -1
- package/dist/audience.js.map +1 -1
- package/dist/catchUpMonitor.d.ts.map +1 -1
- package/dist/catchUpMonitor.js.map +1 -1
- package/dist/collabWindowTracker.d.ts.map +1 -1
- package/dist/collabWindowTracker.js.map +1 -1
- package/dist/connectionManager.d.ts +2 -2
- package/dist/connectionManager.d.ts.map +1 -1
- package/dist/connectionManager.js +51 -24
- package/dist/connectionManager.js.map +1 -1
- package/dist/connectionState.d.ts.map +1 -1
- package/dist/connectionState.js.map +1 -1
- package/dist/connectionStateHandler.d.ts.map +1 -1
- package/dist/connectionStateHandler.js +35 -16
- package/dist/connectionStateHandler.js.map +1 -1
- package/dist/container.d.ts +1 -10
- package/dist/container.d.ts.map +1 -1
- package/dist/container.js +89 -44
- package/dist/container.js.map +1 -1
- package/dist/containerContext.d.ts.map +1 -1
- package/dist/containerContext.js +6 -2
- package/dist/containerContext.js.map +1 -1
- package/dist/containerStorageAdapter.d.ts.map +1 -1
- package/dist/containerStorageAdapter.js +2 -4
- package/dist/containerStorageAdapter.js.map +1 -1
- package/dist/contracts.d.ts.map +1 -1
- package/dist/contracts.js.map +1 -1
- package/dist/deltaManager.d.ts +3 -3
- package/dist/deltaManager.d.ts.map +1 -1
- package/dist/deltaManager.js +56 -27
- package/dist/deltaManager.js.map +1 -1
- package/dist/deltaManagerProxy.d.ts.map +1 -1
- package/dist/deltaManagerProxy.js.map +1 -1
- package/dist/deltaQueue.d.ts.map +1 -1
- package/dist/deltaQueue.js +4 -2
- package/dist/deltaQueue.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/loader.d.ts +3 -3
- package/dist/loader.d.ts.map +1 -1
- package/dist/loader.js +18 -15
- package/dist/loader.js.map +1 -1
- package/dist/packageVersion.d.ts +1 -1
- package/dist/packageVersion.js +1 -1
- package/dist/packageVersion.js.map +1 -1
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +2 -1
- package/dist/protocol.js.map +1 -1
- package/dist/protocolTreeDocumentStorageService.d.ts.map +1 -1
- package/dist/protocolTreeDocumentStorageService.js.map +1 -1
- package/dist/quorum.d.ts.map +1 -1
- package/dist/quorum.js.map +1 -1
- package/dist/retriableDocumentStorageService.d.ts.map +1 -1
- package/dist/retriableDocumentStorageService.js +6 -2
- package/dist/retriableDocumentStorageService.js.map +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +6 -4
- package/dist/utils.js.map +1 -1
- package/lib/audience.d.ts.map +1 -1
- package/lib/audience.js.map +1 -1
- package/lib/catchUpMonitor.d.ts.map +1 -1
- package/lib/catchUpMonitor.js.map +1 -1
- package/lib/collabWindowTracker.d.ts.map +1 -1
- package/lib/collabWindowTracker.js.map +1 -1
- package/lib/connectionManager.d.ts +2 -2
- package/lib/connectionManager.d.ts.map +1 -1
- package/lib/connectionManager.js +53 -26
- package/lib/connectionManager.js.map +1 -1
- package/lib/connectionState.d.ts.map +1 -1
- package/lib/connectionState.js.map +1 -1
- package/lib/connectionStateHandler.d.ts.map +1 -1
- package/lib/connectionStateHandler.js +35 -16
- package/lib/connectionStateHandler.js.map +1 -1
- package/lib/container.d.ts +1 -10
- package/lib/container.d.ts.map +1 -1
- package/lib/container.js +93 -48
- package/lib/container.js.map +1 -1
- package/lib/containerContext.d.ts.map +1 -1
- package/lib/containerContext.js +6 -2
- package/lib/containerContext.js.map +1 -1
- package/lib/containerStorageAdapter.d.ts.map +1 -1
- package/lib/containerStorageAdapter.js +2 -4
- package/lib/containerStorageAdapter.js.map +1 -1
- package/lib/contracts.d.ts.map +1 -1
- package/lib/contracts.js.map +1 -1
- package/lib/deltaManager.d.ts +3 -3
- package/lib/deltaManager.d.ts.map +1 -1
- package/lib/deltaManager.js +58 -29
- package/lib/deltaManager.js.map +1 -1
- package/lib/deltaManagerProxy.d.ts.map +1 -1
- package/lib/deltaManagerProxy.js.map +1 -1
- package/lib/deltaQueue.d.ts.map +1 -1
- package/lib/deltaQueue.js +4 -2
- package/lib/deltaQueue.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js.map +1 -1
- package/lib/loader.d.ts +3 -3
- package/lib/loader.d.ts.map +1 -1
- package/lib/loader.js +18 -15
- package/lib/loader.js.map +1 -1
- package/lib/packageVersion.d.ts +1 -1
- package/lib/packageVersion.js +1 -1
- package/lib/packageVersion.js.map +1 -1
- package/lib/protocol.d.ts.map +1 -1
- package/lib/protocol.js +2 -1
- package/lib/protocol.js.map +1 -1
- package/lib/protocolTreeDocumentStorageService.d.ts.map +1 -1
- package/lib/protocolTreeDocumentStorageService.js.map +1 -1
- package/lib/quorum.d.ts.map +1 -1
- package/lib/quorum.js.map +1 -1
- package/lib/retriableDocumentStorageService.d.ts.map +1 -1
- package/lib/retriableDocumentStorageService.js +6 -2
- package/lib/retriableDocumentStorageService.js.map +1 -1
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +6 -4
- package/lib/utils.js.map +1 -1
- package/package.json +115 -114
- package/prettier.config.cjs +1 -1
- package/src/audience.ts +51 -46
- package/src/catchUpMonitor.ts +39 -37
- package/src/collabWindowTracker.ts +75 -70
- package/src/connectionManager.ts +1006 -944
- package/src/connectionState.ts +19 -19
- package/src/connectionStateHandler.ts +544 -465
- package/src/container.ts +2056 -1909
- package/src/containerContext.ts +350 -340
- package/src/containerStorageAdapter.ts +163 -153
- package/src/contracts.ts +155 -153
- package/src/deltaManager.ts +1069 -992
- package/src/deltaManagerProxy.ts +143 -137
- package/src/deltaQueue.ts +155 -151
- package/src/index.ts +14 -17
- package/src/loader.ts +428 -430
- package/src/packageVersion.ts +1 -1
- package/src/protocol.ts +93 -87
- package/src/protocolTreeDocumentStorageService.ts +30 -33
- package/src/quorum.ts +34 -34
- package/src/retriableDocumentStorageService.ts +118 -102
- package/src/utils.ts +89 -82
- package/tsconfig.esnext.json +6 -6
- package/tsconfig.json +8 -12
package/src/connectionManager.ts
CHANGED
|
@@ -5,59 +5,52 @@
|
|
|
5
5
|
|
|
6
6
|
import { default as AbortController } from "abort-controller";
|
|
7
7
|
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
IDisposable,
|
|
9
|
+
ITelemetryLogger,
|
|
10
|
+
ITelemetryProperties,
|
|
11
11
|
} from "@fluidframework/common-definitions";
|
|
12
12
|
import { assert, performance, TypedEventEmitter } from "@fluidframework/common-utils";
|
|
13
13
|
import {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
IDeltaQueue,
|
|
15
|
+
ReadOnlyInfo,
|
|
16
|
+
IConnectionDetails,
|
|
17
|
+
ICriticalContainerError,
|
|
18
18
|
} from "@fluidframework/container-definitions";
|
|
19
19
|
import { GenericError, UsageError } from "@fluidframework/container-utils";
|
|
20
20
|
import {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
IAnyDriverError,
|
|
22
|
+
IDocumentService,
|
|
23
|
+
IDocumentDeltaConnection,
|
|
24
|
+
IDocumentDeltaConnectionEvents,
|
|
25
25
|
} from "@fluidframework/driver-definitions";
|
|
26
26
|
import {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
27
|
+
canRetryOnError,
|
|
28
|
+
createWriteError,
|
|
29
|
+
createGenericNetworkError,
|
|
30
|
+
getRetryDelayFromError,
|
|
31
|
+
waitForConnectedState,
|
|
32
|
+
DeltaStreamConnectionForbiddenError,
|
|
33
|
+
logNetworkFailure,
|
|
34
|
+
isRuntimeMessage,
|
|
35
35
|
} from "@fluidframework/driver-utils";
|
|
36
36
|
import {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
37
|
+
ConnectionMode,
|
|
38
|
+
IClient,
|
|
39
|
+
IClientConfiguration,
|
|
40
|
+
IClientDetails,
|
|
41
|
+
IDocumentMessage,
|
|
42
|
+
INack,
|
|
43
|
+
INackContent,
|
|
44
|
+
ISequencedDocumentMessage,
|
|
45
|
+
ISignalClient,
|
|
46
|
+
ISignalMessage,
|
|
47
|
+
ITokenClaims,
|
|
48
|
+
MessageType,
|
|
49
|
+
ScopeType,
|
|
50
|
+
ISequencedDocumentSystemMessage,
|
|
51
51
|
} from "@fluidframework/protocol-definitions";
|
|
52
|
-
import {
|
|
53
|
-
|
|
54
|
-
normalizeError,
|
|
55
|
-
} from "@fluidframework/telemetry-utils";
|
|
56
|
-
import {
|
|
57
|
-
ReconnectMode,
|
|
58
|
-
IConnectionManager,
|
|
59
|
-
IConnectionManagerFactoryArgs,
|
|
60
|
-
} from "./contracts";
|
|
52
|
+
import { TelemetryLogger, normalizeError } from "@fluidframework/telemetry-utils";
|
|
53
|
+
import { ReconnectMode, IConnectionManager, IConnectionManagerFactoryArgs } from "./contracts";
|
|
61
54
|
import { DeltaQueue } from "./deltaQueue";
|
|
62
55
|
import { SignalType } from "./protocol";
|
|
63
56
|
|
|
@@ -68,13 +61,15 @@ const DefaultChunkSize = 16 * 1024;
|
|
|
68
61
|
const fatalConnectErrorProp = { fatalConnectError: true };
|
|
69
62
|
|
|
70
63
|
function getNackReconnectInfo(nackContent: INackContent) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
64
|
+
const message = `Nack (${nackContent.type}): ${nackContent.message}`;
|
|
65
|
+
const canRetry = nackContent.code !== 403;
|
|
66
|
+
const retryAfterMs =
|
|
67
|
+
nackContent.retryAfter !== undefined ? nackContent.retryAfter * 1000 : undefined;
|
|
68
|
+
return createGenericNetworkError(
|
|
69
|
+
message,
|
|
70
|
+
{ canRetry, retryAfterMs },
|
|
71
|
+
{ statusCode: nackContent.code, driverVersion: undefined },
|
|
72
|
+
);
|
|
78
73
|
}
|
|
79
74
|
|
|
80
75
|
/**
|
|
@@ -82,66 +77,77 @@ function getNackReconnectInfo(nackContent: INackContent) {
|
|
|
82
77
|
* or receiving ops. Used in storage-only mode.
|
|
83
78
|
*/
|
|
84
79
|
const clientNoDeltaStream: IClient = {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
80
|
+
mode: "read",
|
|
81
|
+
details: { capabilities: { interactive: true } },
|
|
82
|
+
permission: [],
|
|
83
|
+
user: { id: "storage-only client" }, // we need some "fake" ID here.
|
|
84
|
+
scopes: [],
|
|
90
85
|
};
|
|
91
86
|
const clientIdNoDeltaStream: string = "storage-only client";
|
|
92
87
|
|
|
93
88
|
class NoDeltaStream
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
89
|
+
extends TypedEventEmitter<IDocumentDeltaConnectionEvents>
|
|
90
|
+
implements IDocumentDeltaConnection, IDisposable
|
|
91
|
+
{
|
|
92
|
+
clientId = clientIdNoDeltaStream;
|
|
93
|
+
claims: ITokenClaims = {
|
|
94
|
+
scopes: [ScopeType.DocRead],
|
|
95
|
+
} as any;
|
|
96
|
+
mode: ConnectionMode = "read";
|
|
97
|
+
existing: boolean = true;
|
|
98
|
+
maxMessageSize: number = 0;
|
|
99
|
+
version: string = "";
|
|
100
|
+
initialMessages: ISequencedDocumentMessage[] = [];
|
|
101
|
+
initialSignals: ISignalMessage[] = [];
|
|
102
|
+
initialClients: ISignalClient[] = [
|
|
103
|
+
{ client: clientNoDeltaStream, clientId: clientIdNoDeltaStream },
|
|
104
|
+
];
|
|
105
|
+
serviceConfiguration: IClientConfiguration = {
|
|
106
|
+
maxMessageSize: 0,
|
|
107
|
+
blockSize: 0,
|
|
108
|
+
};
|
|
109
|
+
checkpointSequenceNumber?: number | undefined = undefined;
|
|
110
|
+
submit(messages: IDocumentMessage[]): void {
|
|
111
|
+
this.emit(
|
|
112
|
+
"nack",
|
|
113
|
+
this.clientId,
|
|
114
|
+
messages.map((operation) => {
|
|
115
|
+
return {
|
|
116
|
+
operation,
|
|
117
|
+
content: { message: "Cannot submit with storage-only connection", code: 403 },
|
|
118
|
+
};
|
|
119
|
+
}),
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
submitSignal(message: any): void {
|
|
123
|
+
this.emit("nack", this.clientId, {
|
|
124
|
+
operation: message,
|
|
125
|
+
content: { message: "Cannot submit signal with storage-only connection", code: 403 },
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private _disposed = false;
|
|
130
|
+
public get disposed() {
|
|
131
|
+
return this._disposed;
|
|
132
|
+
}
|
|
133
|
+
public dispose() {
|
|
134
|
+
this._disposed = true;
|
|
135
|
+
}
|
|
130
136
|
}
|
|
131
137
|
|
|
132
138
|
/**
|
|
133
139
|
* Interface to track the current in-progress connection attempt.
|
|
134
140
|
*/
|
|
135
141
|
interface IPendingConnection {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
142
|
+
/**
|
|
143
|
+
* Used to cancel an in-progress connection attempt.
|
|
144
|
+
*/
|
|
145
|
+
abort(): void;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Desired ConnectionMode of this in-progress connection attempt.
|
|
149
|
+
*/
|
|
150
|
+
connectionMode: ConnectionMode;
|
|
145
151
|
}
|
|
146
152
|
|
|
147
153
|
/**
|
|
@@ -150,849 +156,905 @@ interface IPendingConnection {
|
|
|
150
156
|
* Exposes various controls to influence this process, including manual reconnects, forced read-only mode, etc.
|
|
151
157
|
*/
|
|
152
158
|
export class ConnectionManager implements IConnectionManager {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
159
|
+
/** Connection mode used when reconnecting on error or disconnect. */
|
|
160
|
+
private readonly defaultReconnectionMode: ConnectionMode;
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Tracks the current in-progress connection attempt. Undefined if there is none.
|
|
164
|
+
* Note: Once the connection attempt fires and the code becomes asynchronous, its possible that a new connection
|
|
165
|
+
* attempt was fired and this.pendingConnection was overwritten to reflect the new attempt.
|
|
166
|
+
*/
|
|
167
|
+
private pendingConnection: IPendingConnection | undefined;
|
|
168
|
+
private connection: IDocumentDeltaConnection | undefined;
|
|
169
|
+
|
|
170
|
+
/** file ACL - whether user has only read-only access to a file */
|
|
171
|
+
private _readonlyPermissions: boolean | undefined;
|
|
172
|
+
|
|
173
|
+
/** tracks host requiring read-only mode. */
|
|
174
|
+
private _forceReadonly = false;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Controls whether the DeltaManager will automatically reconnect to the delta stream after receiving a disconnect.
|
|
178
|
+
*/
|
|
179
|
+
private _reconnectMode: ReconnectMode;
|
|
180
|
+
|
|
181
|
+
/** True if there is pending (async) reconnection from "read" to "write" */
|
|
182
|
+
private pendingReconnect = false;
|
|
183
|
+
|
|
184
|
+
private clientSequenceNumber = 0;
|
|
185
|
+
private clientSequenceNumberObserved = 0;
|
|
186
|
+
/** Counts the number of non-runtime ops sent by the client which may not be acked. */
|
|
187
|
+
private localOpsToIgnore = 0;
|
|
188
|
+
|
|
189
|
+
/** track clientId used last time when we sent any ops */
|
|
190
|
+
private lastSubmittedClientId: string | undefined;
|
|
191
|
+
|
|
192
|
+
private connectFirstConnection = true;
|
|
193
|
+
|
|
194
|
+
private _connectionVerboseProps: Record<string, string | number> = {};
|
|
195
|
+
|
|
196
|
+
private _connectionProps: ITelemetryProperties = {};
|
|
197
|
+
|
|
198
|
+
private _disposed = false;
|
|
199
|
+
|
|
200
|
+
private readonly _outbound: DeltaQueue<IDocumentMessage[]>;
|
|
201
|
+
|
|
202
|
+
public get connectionVerboseProps() {
|
|
203
|
+
return this._connectionVerboseProps;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
public readonly clientDetails: IClientDetails;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* The current connection mode, initially read.
|
|
210
|
+
*/
|
|
211
|
+
public get connectionMode(): ConnectionMode {
|
|
212
|
+
return this.connection?.mode ?? "read";
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
public get connected() {
|
|
216
|
+
return this.connection !== undefined;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
public get clientId() {
|
|
220
|
+
return this.connection?.clientId;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Automatic reconnecting enabled or disabled.
|
|
224
|
+
* If set to Never, then reconnecting will never be allowed.
|
|
225
|
+
*/
|
|
226
|
+
public get reconnectMode(): ReconnectMode {
|
|
227
|
+
return this._reconnectMode;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
public get maxMessageSize(): number {
|
|
231
|
+
return this.connection?.serviceConfiguration?.maxMessageSize ?? DefaultChunkSize;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
public get version(): string {
|
|
235
|
+
if (this.connection === undefined) {
|
|
236
|
+
throw new Error("Cannot check version without a connection");
|
|
237
|
+
}
|
|
238
|
+
return this.connection.version;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
public get serviceConfiguration(): IClientConfiguration | undefined {
|
|
242
|
+
return this.connection?.serviceConfiguration;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
public get scopes(): string[] | undefined {
|
|
246
|
+
return this.connection?.claims.scopes;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
public get outbound(): IDeltaQueue<IDocumentMessage[]> {
|
|
250
|
+
return this._outbound;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Returns set of props that can be logged in telemetry that provide some insights / statistics
|
|
255
|
+
* about current or last connection (if there is no connection at the moment)
|
|
256
|
+
*/
|
|
257
|
+
public get connectionProps(): ITelemetryProperties {
|
|
258
|
+
return this.connection !== undefined
|
|
259
|
+
? this._connectionProps
|
|
260
|
+
: {
|
|
261
|
+
...this._connectionProps,
|
|
262
|
+
// Report how many ops this client sent in last disconnected session
|
|
263
|
+
sentOps: this.clientSequenceNumber,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
public shouldJoinWrite(): boolean {
|
|
268
|
+
// We don't have to wait for ack for topmost NoOps. So subtract those.
|
|
269
|
+
return (
|
|
270
|
+
this.clientSequenceNumberObserved < this.clientSequenceNumber - this.localOpsToIgnore
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Tells if container is in read-only mode.
|
|
276
|
+
* Data stores should listen for "readonly" notifications and disallow user
|
|
277
|
+
* making changes to data stores.
|
|
278
|
+
* Readonly state can be because of no storage write permission,
|
|
279
|
+
* or due to host forcing readonly mode for container.
|
|
280
|
+
* It is undefined if we have not yet established websocket connection
|
|
281
|
+
* and do not know if user has write access to a file.
|
|
282
|
+
*/
|
|
283
|
+
private get readonly(): boolean | undefined {
|
|
284
|
+
if (this._forceReadonly) {
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
return this._readonlyPermissions;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
public get readOnlyInfo(): ReadOnlyInfo {
|
|
291
|
+
const storageOnly =
|
|
292
|
+
this.connection !== undefined && this.connection instanceof NoDeltaStream;
|
|
293
|
+
if (storageOnly || this._forceReadonly || this._readonlyPermissions === true) {
|
|
294
|
+
return {
|
|
295
|
+
readonly: true,
|
|
296
|
+
forced: this._forceReadonly,
|
|
297
|
+
permissions: this._readonlyPermissions,
|
|
298
|
+
storageOnly,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return { readonly: this._readonlyPermissions };
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private static detailsFromConnection(connection: IDocumentDeltaConnection): IConnectionDetails {
|
|
306
|
+
return {
|
|
307
|
+
claims: connection.claims,
|
|
308
|
+
clientId: connection.clientId,
|
|
309
|
+
existing: connection.existing,
|
|
310
|
+
checkpointSequenceNumber: connection.checkpointSequenceNumber,
|
|
311
|
+
get initialClients() {
|
|
312
|
+
return connection.initialClients;
|
|
313
|
+
},
|
|
314
|
+
mode: connection.mode,
|
|
315
|
+
serviceConfiguration: connection.serviceConfiguration,
|
|
316
|
+
version: connection.version,
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
constructor(
|
|
321
|
+
private readonly serviceProvider: () => IDocumentService | undefined,
|
|
322
|
+
private client: IClient,
|
|
323
|
+
reconnectAllowed: boolean,
|
|
324
|
+
private readonly logger: ITelemetryLogger,
|
|
325
|
+
private readonly props: IConnectionManagerFactoryArgs,
|
|
326
|
+
) {
|
|
327
|
+
this.clientDetails = this.client.details;
|
|
328
|
+
this.defaultReconnectionMode = this.client.mode;
|
|
329
|
+
this._reconnectMode = reconnectAllowed ? ReconnectMode.Enabled : ReconnectMode.Never;
|
|
330
|
+
|
|
331
|
+
// Outbound message queue. The outbound queue is represented as a queue of an array of ops. Ops contained
|
|
332
|
+
// within an array *must* fit within the maxMessageSize and are guaranteed to be ordered sequentially.
|
|
333
|
+
this._outbound = new DeltaQueue<IDocumentMessage[]>((messages) => {
|
|
334
|
+
if (this.connection === undefined) {
|
|
335
|
+
throw new Error("Attempted to submit an outbound message without connection");
|
|
336
|
+
}
|
|
337
|
+
this.connection.submit(messages);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
this._outbound.on("error", (error) => {
|
|
341
|
+
this.props.closeHandler(normalizeError(error));
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
public dispose(error?: ICriticalContainerError, switchToReadonly: boolean = true) {
|
|
346
|
+
if (this._disposed) {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
this._disposed = true;
|
|
350
|
+
|
|
351
|
+
this.pendingConnection = undefined;
|
|
352
|
+
|
|
353
|
+
// Ensure that things like triggerConnect() will short circuit
|
|
354
|
+
this._reconnectMode = ReconnectMode.Never;
|
|
355
|
+
|
|
356
|
+
this._outbound.clear();
|
|
357
|
+
|
|
358
|
+
const disconnectReason = "Closing DeltaManager";
|
|
359
|
+
|
|
360
|
+
// This raises "disconnect" event if we have active connection.
|
|
361
|
+
this.disconnectFromDeltaStream(disconnectReason);
|
|
362
|
+
|
|
363
|
+
if (switchToReadonly) {
|
|
364
|
+
// Notify everyone we are in read-only state.
|
|
365
|
+
// Useful for data stores in case we hit some critical error,
|
|
366
|
+
// to switch to a mode where user edits are not accepted
|
|
367
|
+
this.set_readonlyPermissions(true);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Enables or disables automatic reconnecting.
|
|
373
|
+
* Will throw an error if reconnectMode set to Never.
|
|
374
|
+
*/
|
|
375
|
+
public setAutoReconnect(mode: ReconnectMode): void {
|
|
376
|
+
assert(
|
|
377
|
+
mode !== ReconnectMode.Never && this._reconnectMode !== ReconnectMode.Never,
|
|
378
|
+
0x278 /* "API is not supported for non-connecting or closed container" */,
|
|
379
|
+
);
|
|
380
|
+
|
|
381
|
+
this._reconnectMode = mode;
|
|
382
|
+
|
|
383
|
+
if (mode !== ReconnectMode.Enabled) {
|
|
384
|
+
// immediately disconnect - do not rely on service eventually dropping connection.
|
|
385
|
+
this.disconnectFromDeltaStream("setAutoReconnect");
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Sends signal to runtime (and data stores) to be read-only.
|
|
391
|
+
* Hosts may have read only views, indicating to data stores that no edits are allowed.
|
|
392
|
+
* This is independent from this._readonlyPermissions (permissions) and this.connectionMode
|
|
393
|
+
* (server can return "write" mode even when asked for "read")
|
|
394
|
+
* Leveraging same "readonly" event as runtime & data stores should behave the same in such case
|
|
395
|
+
* as in read-only permissions.
|
|
396
|
+
* But this.active can be used by some DDSes to figure out if ops can be sent
|
|
397
|
+
* (for example, read-only view still participates in code proposals / upgrades decisions)
|
|
398
|
+
*
|
|
399
|
+
* Forcing Readonly does not prevent DDS from generating ops. It is up to user code to honour
|
|
400
|
+
* the readonly flag. If ops are generated, they will accumulate locally and not be sent. If
|
|
401
|
+
* there are pending in the outbound queue, it will stop sending until force readonly is
|
|
402
|
+
* cleared.
|
|
403
|
+
*
|
|
404
|
+
* @param readonly - set or clear force readonly.
|
|
405
|
+
*/
|
|
406
|
+
public forceReadonly(readonly: boolean) {
|
|
407
|
+
if (readonly !== this._forceReadonly) {
|
|
408
|
+
this.logger.sendTelemetryEvent({
|
|
409
|
+
eventName: "ForceReadOnly",
|
|
410
|
+
value: readonly,
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
const oldValue = this.readonly;
|
|
414
|
+
this._forceReadonly = readonly;
|
|
415
|
+
|
|
416
|
+
if (oldValue !== this.readonly) {
|
|
417
|
+
if (this._reconnectMode === ReconnectMode.Never) {
|
|
418
|
+
throw new UsageError("API is not supported for non-connecting or closed container");
|
|
419
|
+
}
|
|
420
|
+
let reconnect = false;
|
|
421
|
+
if (this.readonly === true) {
|
|
422
|
+
// If we switch to readonly while connected, we should disconnect first
|
|
423
|
+
// See comment in the "readonly" event handler to deltaManager set up by
|
|
424
|
+
// the ContainerRuntime constructor
|
|
425
|
+
|
|
426
|
+
if (this.shouldJoinWrite()) {
|
|
427
|
+
// If we have pending changes, then we will never send them - it smells like
|
|
428
|
+
// host logic error.
|
|
429
|
+
this.logger.sendErrorEvent({ eventName: "ForceReadonlyPendingChanged" });
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
reconnect = this.disconnectFromDeltaStream("Force readonly");
|
|
433
|
+
}
|
|
434
|
+
this.props.readonlyChangeHandler(this.readonly);
|
|
435
|
+
if (reconnect) {
|
|
436
|
+
// reconnect if we disconnected from before.
|
|
437
|
+
this.triggerConnect("read");
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
private set_readonlyPermissions(readonly: boolean) {
|
|
443
|
+
const oldValue = this.readonly;
|
|
444
|
+
this._readonlyPermissions = readonly;
|
|
445
|
+
if (oldValue !== this.readonly) {
|
|
446
|
+
this.props.readonlyChangeHandler(this.readonly);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
public connect(connectionMode?: ConnectionMode) {
|
|
451
|
+
this.connectCore(connectionMode).catch((error) => {
|
|
452
|
+
const normalizedError = normalizeError(error, { props: fatalConnectErrorProp });
|
|
453
|
+
this.props.closeHandler(normalizedError);
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
private async connectCore(connectionMode?: ConnectionMode): Promise<void> {
|
|
458
|
+
assert(!this._disposed, 0x26a /* "not closed" */);
|
|
459
|
+
|
|
460
|
+
if (this.connection !== undefined) {
|
|
461
|
+
return; // Connection attempt already completed successfully
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
let pendingConnectionMode;
|
|
465
|
+
if (this.pendingConnection !== undefined) {
|
|
466
|
+
pendingConnectionMode = this.pendingConnection.connectionMode;
|
|
467
|
+
this.cancelConnection(); // Throw out in-progress connection attempt in favor of new attempt
|
|
468
|
+
assert(
|
|
469
|
+
this.pendingConnection === undefined,
|
|
470
|
+
0x344 /* this.pendingConnection should be undefined */,
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
// If there is no specified ConnectionMode, try the previous mode, if there is no previous mode use default
|
|
474
|
+
let requestedMode = connectionMode ?? pendingConnectionMode ?? this.defaultReconnectionMode;
|
|
475
|
+
|
|
476
|
+
// if we have any non-acked ops from last connection, reconnect as "write".
|
|
477
|
+
// without that we would connect in view-only mode, which will result in immediate
|
|
478
|
+
// firing of "connected" event from Container and switch of current clientId (as tracked
|
|
479
|
+
// by all DDSes). This will make it impossible to figure out if ops actually made it through,
|
|
480
|
+
// so DDSes will immediately resubmit all pending ops, and some of them will be duplicates, corrupting document
|
|
481
|
+
if (this.shouldJoinWrite()) {
|
|
482
|
+
requestedMode = "write";
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const docService = this.serviceProvider();
|
|
486
|
+
assert(docService !== undefined, 0x2a7 /* "Container is not attached" */);
|
|
487
|
+
|
|
488
|
+
let connection: IDocumentDeltaConnection | undefined;
|
|
489
|
+
|
|
490
|
+
if (docService.policies?.storageOnly === true) {
|
|
491
|
+
connection = new NoDeltaStream();
|
|
492
|
+
this.setupNewSuccessfulConnection(connection, "read");
|
|
493
|
+
assert(this.pendingConnection === undefined, 0x2b3 /* "logic error" */);
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
let delayMs = InitialReconnectDelayInMs;
|
|
498
|
+
let connectRepeatCount = 0;
|
|
499
|
+
const connectStartTime = performance.now();
|
|
500
|
+
let lastError: any;
|
|
501
|
+
|
|
502
|
+
const abortController = new AbortController();
|
|
503
|
+
const abortSignal = abortController.signal;
|
|
504
|
+
this.pendingConnection = {
|
|
505
|
+
abort: () => {
|
|
506
|
+
abortController.abort();
|
|
507
|
+
},
|
|
508
|
+
connectionMode: requestedMode,
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
// This loop will keep trying to connect until successful, with a delay between each iteration.
|
|
512
|
+
while (connection === undefined) {
|
|
513
|
+
if (this._disposed) {
|
|
514
|
+
throw new Error("Attempting to connect a closed DeltaManager");
|
|
515
|
+
}
|
|
516
|
+
if (abortSignal.aborted === true) {
|
|
517
|
+
this.logger.sendTelemetryEvent({
|
|
518
|
+
eventName: "ConnectionAttemptCancelled",
|
|
519
|
+
attempts: connectRepeatCount,
|
|
520
|
+
duration: TelemetryLogger.formatTick(performance.now() - connectStartTime),
|
|
521
|
+
connectionEstablished: false,
|
|
522
|
+
});
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
connectRepeatCount++;
|
|
526
|
+
|
|
527
|
+
try {
|
|
528
|
+
this.client.mode = requestedMode;
|
|
529
|
+
connection = await docService.connectToDeltaStream({
|
|
530
|
+
...this.client,
|
|
531
|
+
mode: requestedMode,
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
if (connection.disposed) {
|
|
535
|
+
// Nobody observed this connection, so drop it on the floor and retry.
|
|
536
|
+
this.logger.sendTelemetryEvent({ eventName: "ReceivedClosedConnection" });
|
|
537
|
+
connection = undefined;
|
|
538
|
+
}
|
|
539
|
+
} catch (origError: any) {
|
|
540
|
+
if (
|
|
541
|
+
typeof origError === "object" &&
|
|
542
|
+
origError !== null &&
|
|
543
|
+
origError?.errorType === DeltaStreamConnectionForbiddenError.errorType
|
|
544
|
+
) {
|
|
545
|
+
connection = new NoDeltaStream();
|
|
546
|
+
requestedMode = "read";
|
|
547
|
+
break;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// Socket.io error when we connect to wrong socket, or hit some multiplexing bug
|
|
551
|
+
if (!canRetryOnError(origError)) {
|
|
552
|
+
const error = normalizeError(origError, { props: fatalConnectErrorProp });
|
|
553
|
+
this.props.closeHandler(error);
|
|
554
|
+
throw error;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// Since the error is retryable this will not log to the error table
|
|
558
|
+
logNetworkFailure(
|
|
559
|
+
this.logger,
|
|
560
|
+
{
|
|
561
|
+
attempts: connectRepeatCount,
|
|
562
|
+
delay: delayMs, // milliseconds
|
|
563
|
+
eventName: "DeltaConnectionFailureToConnect",
|
|
564
|
+
duration: TelemetryLogger.formatTick(performance.now() - connectStartTime),
|
|
565
|
+
},
|
|
566
|
+
origError,
|
|
567
|
+
);
|
|
568
|
+
|
|
569
|
+
lastError = origError;
|
|
570
|
+
|
|
571
|
+
const retryDelayFromError = getRetryDelayFromError(origError);
|
|
572
|
+
delayMs = retryDelayFromError ?? Math.min(delayMs * 2, MaxReconnectDelayInMs);
|
|
573
|
+
|
|
574
|
+
if (retryDelayFromError !== undefined) {
|
|
575
|
+
this.props.reconnectionDelayHandler(retryDelayFromError, origError);
|
|
576
|
+
}
|
|
577
|
+
await waitForConnectedState(delayMs);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// If we retried more than once, log an event about how long it took (this will not log to error table)
|
|
582
|
+
if (connectRepeatCount > 1) {
|
|
583
|
+
logNetworkFailure(
|
|
584
|
+
this.logger,
|
|
585
|
+
{
|
|
586
|
+
eventName: "MultipleDeltaConnectionFailures",
|
|
587
|
+
attempts: connectRepeatCount,
|
|
588
|
+
duration: TelemetryLogger.formatTick(performance.now() - connectStartTime),
|
|
589
|
+
},
|
|
590
|
+
lastError,
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// Check for abort signal after while loop as well
|
|
595
|
+
if (abortSignal.aborted === true) {
|
|
596
|
+
connection.dispose();
|
|
597
|
+
this.logger.sendTelemetryEvent({
|
|
598
|
+
eventName: "ConnectionAttemptCancelled",
|
|
599
|
+
attempts: connectRepeatCount,
|
|
600
|
+
duration: TelemetryLogger.formatTick(performance.now() - connectStartTime),
|
|
601
|
+
connectionEstablished: true,
|
|
602
|
+
});
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
this.setupNewSuccessfulConnection(connection, requestedMode);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Start the connection. Any error should result in container being closed.
|
|
611
|
+
* And report the error if it escapes for any reason.
|
|
612
|
+
* @param args - The connection arguments
|
|
613
|
+
*/
|
|
614
|
+
private triggerConnect(connectionMode: ConnectionMode) {
|
|
615
|
+
// reconnect() has async await of waitForConnectedState(), and that causes potential race conditions
|
|
616
|
+
// where we might already have a connection. If it were to happen, it's possible that we will connect
|
|
617
|
+
// with different mode to `connectionMode`. Glancing through the caller chains, it looks like code should be
|
|
618
|
+
// fine (if needed, reconnect flow will get triggered again). Places where new mode matters should encode it
|
|
619
|
+
// directly in connectCore - see this.shouldJoinWrite() test as an example.
|
|
620
|
+
// assert(this.connection === undefined, 0x239 /* "called only in disconnected state" */);
|
|
621
|
+
|
|
622
|
+
if (this.reconnectMode !== ReconnectMode.Enabled) {
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
this.connect(connectionMode);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Disconnect the current connection.
|
|
630
|
+
* @param reason - Text description of disconnect reason to emit with disconnect event
|
|
631
|
+
* @returns A boolean that indicates if there was an existing connection (or pending connection) to disconnect
|
|
632
|
+
*/
|
|
633
|
+
private disconnectFromDeltaStream(reason: string): boolean {
|
|
634
|
+
this.pendingReconnect = false;
|
|
635
|
+
|
|
636
|
+
if (this.connection === undefined) {
|
|
637
|
+
if (this.pendingConnection !== undefined) {
|
|
638
|
+
this.cancelConnection();
|
|
639
|
+
return true;
|
|
640
|
+
}
|
|
641
|
+
return false;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
assert(
|
|
645
|
+
this.pendingConnection === undefined,
|
|
646
|
+
0x27b /* "reentrancy may result in incorrect behavior" */,
|
|
647
|
+
);
|
|
648
|
+
|
|
649
|
+
const connection = this.connection;
|
|
650
|
+
// Avoid any re-entrancy - clear object reference
|
|
651
|
+
this.connection = undefined;
|
|
652
|
+
|
|
653
|
+
// Remove listeners first so we don't try to retrigger this flow accidentally through reconnectOnError
|
|
654
|
+
connection.off("op", this.opHandler);
|
|
655
|
+
connection.off("signal", this.props.signalHandler);
|
|
656
|
+
connection.off("nack", this.nackHandler);
|
|
657
|
+
connection.off("disconnect", this.disconnectHandlerInternal);
|
|
658
|
+
connection.off("error", this.errorHandler);
|
|
659
|
+
connection.off("pong", this.props.pongHandler);
|
|
660
|
+
|
|
661
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
662
|
+
this._outbound.pause();
|
|
663
|
+
this._outbound.clear();
|
|
664
|
+
this.props.disconnectHandler(reason);
|
|
665
|
+
|
|
666
|
+
connection.dispose();
|
|
667
|
+
|
|
668
|
+
this._connectionVerboseProps = {};
|
|
669
|
+
|
|
670
|
+
return true;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
/**
|
|
674
|
+
* Cancel in-progress connection attempt.
|
|
675
|
+
*/
|
|
676
|
+
private cancelConnection() {
|
|
677
|
+
assert(
|
|
678
|
+
this.pendingConnection !== undefined,
|
|
679
|
+
0x345 /* this.pendingConnection is undefined when trying to cancel */,
|
|
680
|
+
);
|
|
681
|
+
this.pendingConnection.abort();
|
|
682
|
+
this.pendingConnection = undefined;
|
|
683
|
+
this.logger.sendTelemetryEvent({ eventName: "ConnectionCancelReceived" });
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* Once we've successfully gotten a connection, we need to set up state, attach event listeners, and process
|
|
688
|
+
* initial messages.
|
|
689
|
+
* @param connection - The newly established connection
|
|
690
|
+
*/
|
|
691
|
+
private setupNewSuccessfulConnection(
|
|
692
|
+
connection: IDocumentDeltaConnection,
|
|
693
|
+
requestedMode: ConnectionMode,
|
|
694
|
+
) {
|
|
695
|
+
// Old connection should have been cleaned up before establishing a new one
|
|
696
|
+
assert(
|
|
697
|
+
this.connection === undefined,
|
|
698
|
+
0x0e6 /* "old connection exists on new connection setup" */,
|
|
699
|
+
);
|
|
700
|
+
assert(
|
|
701
|
+
!connection.disposed,
|
|
702
|
+
0x28a /* "can't be disposed - Callers need to ensure that!" */,
|
|
703
|
+
);
|
|
704
|
+
|
|
705
|
+
this.pendingConnection = undefined;
|
|
706
|
+
|
|
707
|
+
this.connection = connection;
|
|
708
|
+
|
|
709
|
+
// Does information in scopes & mode matches?
|
|
710
|
+
// If we asked for "write" and got "read", then file is read-only
|
|
711
|
+
// But if we ask read, server can still give us write.
|
|
712
|
+
const readonly = !connection.claims.scopes.includes(ScopeType.DocWrite);
|
|
713
|
+
|
|
714
|
+
if (connection.mode !== requestedMode) {
|
|
715
|
+
this.logger.sendTelemetryEvent({
|
|
716
|
+
eventName: "ConnectionModeMismatch",
|
|
717
|
+
requestedMode,
|
|
718
|
+
mode: connection.mode,
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
// This connection mode validation logic is moving to the driver layer in 0.44. These two asserts can be
|
|
722
|
+
// removed after those packages have released and become ubiquitous.
|
|
723
|
+
assert(
|
|
724
|
+
requestedMode === "read" || readonly === (this.connectionMode === "read"),
|
|
725
|
+
0x0e7 /* "claims/connectionMode mismatch" */,
|
|
726
|
+
);
|
|
727
|
+
assert(
|
|
728
|
+
!readonly || this.connectionMode === "read",
|
|
729
|
+
0x0e8 /* "readonly perf with write connection" */,
|
|
730
|
+
);
|
|
731
|
+
|
|
732
|
+
this.set_readonlyPermissions(readonly);
|
|
733
|
+
|
|
734
|
+
if (this._disposed) {
|
|
735
|
+
// Raise proper events, Log telemetry event and close connection.
|
|
736
|
+
this.disconnectFromDeltaStream("ConnectionManager already closed");
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
this._outbound.resume();
|
|
741
|
+
|
|
742
|
+
connection.on("op", this.opHandler);
|
|
743
|
+
connection.on("signal", this.props.signalHandler);
|
|
744
|
+
connection.on("nack", this.nackHandler);
|
|
745
|
+
connection.on("disconnect", this.disconnectHandlerInternal);
|
|
746
|
+
connection.on("error", this.errorHandler);
|
|
747
|
+
connection.on("pong", this.props.pongHandler);
|
|
748
|
+
|
|
749
|
+
// Initial messages are always sorted. However, due to early op handler installed by drivers and appending those
|
|
750
|
+
// ops to initialMessages, resulting set is no longer sorted, which would result in client hitting storage to
|
|
751
|
+
// fill in gap. We will recover by cancelling this request once we process remaining ops, but it's a waste that
|
|
752
|
+
// we could avoid
|
|
753
|
+
const initialMessages = connection.initialMessages.sort(
|
|
754
|
+
(a, b) => a.sequenceNumber - b.sequenceNumber,
|
|
755
|
+
);
|
|
756
|
+
|
|
757
|
+
// Some storages may provide checkpointSequenceNumber to identify how far client is behind.
|
|
758
|
+
let checkpointSequenceNumber = connection.checkpointSequenceNumber;
|
|
759
|
+
|
|
760
|
+
this._connectionVerboseProps = {
|
|
761
|
+
clientId: connection.clientId,
|
|
762
|
+
mode: connection.mode,
|
|
763
|
+
};
|
|
764
|
+
|
|
765
|
+
// reset connection props
|
|
766
|
+
this._connectionProps = {};
|
|
767
|
+
|
|
768
|
+
if (connection.relayServiceAgent !== undefined) {
|
|
769
|
+
this._connectionVerboseProps.relayServiceAgent = connection.relayServiceAgent;
|
|
770
|
+
this._connectionProps.relayServiceAgent = connection.relayServiceAgent;
|
|
771
|
+
}
|
|
772
|
+
this._connectionProps.socketDocumentId = connection.claims.documentId;
|
|
773
|
+
this._connectionProps.connectionMode = connection.mode;
|
|
774
|
+
|
|
775
|
+
let last = -1;
|
|
776
|
+
if (initialMessages.length !== 0) {
|
|
777
|
+
this._connectionVerboseProps.connectionInitialOpsFrom =
|
|
778
|
+
initialMessages[0].sequenceNumber;
|
|
779
|
+
last = initialMessages[initialMessages.length - 1].sequenceNumber;
|
|
780
|
+
this._connectionVerboseProps.connectionInitialOpsTo = last + 1;
|
|
781
|
+
// Update knowledge of how far we are behind, before raising "connect" event
|
|
782
|
+
// This is duplication of what incomingOpHandler() does, but we have to raise event before we get there,
|
|
783
|
+
// so duplicating update logic here as well.
|
|
784
|
+
if (checkpointSequenceNumber === undefined || checkpointSequenceNumber < last) {
|
|
785
|
+
checkpointSequenceNumber = last;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
this.props.incomingOpHandler(
|
|
790
|
+
initialMessages,
|
|
791
|
+
this.connectFirstConnection ? "InitialOps" : "ReconnectOps",
|
|
792
|
+
);
|
|
793
|
+
|
|
794
|
+
const details = ConnectionManager.detailsFromConnection(connection);
|
|
795
|
+
details.checkpointSequenceNumber = checkpointSequenceNumber;
|
|
796
|
+
this.props.connectHandler(details);
|
|
797
|
+
|
|
798
|
+
this.connectFirstConnection = false;
|
|
799
|
+
|
|
800
|
+
// Synthesize clear & join signals out of initialClients state.
|
|
801
|
+
// This allows us to have single way to process signals, and makes it simpler to initialize
|
|
802
|
+
// protocol in Container.
|
|
803
|
+
const clearSignal: ISignalMessage = {
|
|
804
|
+
clientId: null, // system message
|
|
805
|
+
content: JSON.stringify({
|
|
806
|
+
type: SignalType.Clear,
|
|
807
|
+
}),
|
|
808
|
+
};
|
|
809
|
+
this.props.signalHandler(clearSignal);
|
|
810
|
+
|
|
811
|
+
for (const priorClient of connection.initialClients ?? []) {
|
|
812
|
+
const joinSignal: ISignalMessage = {
|
|
813
|
+
clientId: null, // system signal
|
|
814
|
+
content: JSON.stringify({
|
|
815
|
+
type: SignalType.ClientJoin,
|
|
816
|
+
content: priorClient, // ISignalClient
|
|
817
|
+
}),
|
|
818
|
+
};
|
|
819
|
+
this.props.signalHandler(joinSignal);
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
// Unfortunately, there is no defined order between initialSignals (including join & leave signals)
|
|
823
|
+
// and connection.initialClients. In practice, connection.initialSignals quite often contains join signal
|
|
824
|
+
// for "self" and connection.initialClients does not contain "self", so we have to process them after
|
|
825
|
+
// "clear" signal above.
|
|
826
|
+
if (connection.initialSignals !== undefined) {
|
|
827
|
+
for (const signal of connection.initialSignals) {
|
|
828
|
+
this.props.signalHandler(signal);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
/**
|
|
834
|
+
* Disconnect the current connection and reconnect. Closes the container if it fails.
|
|
835
|
+
* @param connection - The connection that wants to reconnect - no-op if it's different from this.connection
|
|
836
|
+
* @param requestedMode - Read or write
|
|
837
|
+
* @param error - Error reconnect information including whether or not to reconnect
|
|
838
|
+
* @returns A promise that resolves when the connection is reestablished or we stop trying
|
|
839
|
+
*/
|
|
840
|
+
private reconnectOnError(requestedMode: ConnectionMode, error: IAnyDriverError) {
|
|
841
|
+
this.reconnect(requestedMode, error.message, error).catch(this.props.closeHandler);
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
/**
|
|
845
|
+
* Disconnect the current connection and reconnect.
|
|
846
|
+
* @param connection - The connection that wants to reconnect - no-op if it's different from this.connection
|
|
847
|
+
* @param requestedMode - Read or write
|
|
848
|
+
* @param error - Error reconnect information including whether or not to reconnect
|
|
849
|
+
* @returns A promise that resolves when the connection is reestablished or we stop trying
|
|
850
|
+
*/
|
|
851
|
+
private async reconnect(
|
|
852
|
+
requestedMode: ConnectionMode,
|
|
853
|
+
disconnectMessage: string,
|
|
854
|
+
error?: IAnyDriverError,
|
|
855
|
+
) {
|
|
856
|
+
// We quite often get protocol errors before / after observing nack/disconnect
|
|
857
|
+
// we do not want to run through same sequence twice.
|
|
858
|
+
// If we're already disconnected/disconnecting it's not appropriate to call this again.
|
|
859
|
+
assert(this.connection !== undefined, 0x0eb /* "Missing connection for reconnect" */);
|
|
860
|
+
|
|
861
|
+
this.disconnectFromDeltaStream(disconnectMessage);
|
|
862
|
+
|
|
863
|
+
// We will always trigger reconnect, even if canRetry is false.
|
|
864
|
+
// Any truly fatal error state will result in container close upon attempted reconnect,
|
|
865
|
+
// which is a preferable to closing abruptly when a live connection fails.
|
|
866
|
+
if (error !== undefined && !error.canRetry) {
|
|
867
|
+
this.logger.sendTelemetryEvent(
|
|
868
|
+
{
|
|
869
|
+
eventName: "reconnectingDespiteFatalError",
|
|
870
|
+
reconnectMode: this.reconnectMode,
|
|
871
|
+
},
|
|
872
|
+
error,
|
|
873
|
+
);
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
if (this.reconnectMode === ReconnectMode.Never) {
|
|
877
|
+
// Do not raise container error if we are closing just because we lost connection.
|
|
878
|
+
// Those errors (like IdleDisconnect) would show up in telemetry dashboards and
|
|
879
|
+
// are very misleading, as first initial reaction - some logic is broken.
|
|
880
|
+
this.props.closeHandler();
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
// If closed then we can't reconnect
|
|
884
|
+
if (this._disposed || this.reconnectMode !== ReconnectMode.Enabled) {
|
|
885
|
+
return;
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
const delayMs = getRetryDelayFromError(error);
|
|
889
|
+
if (error !== undefined && delayMs !== undefined) {
|
|
890
|
+
this.props.reconnectionDelayHandler(delayMs, error);
|
|
891
|
+
await waitForConnectedState(delayMs);
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
this.triggerConnect(requestedMode);
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
public prepareMessageToSend(
|
|
898
|
+
message: Omit<IDocumentMessage, "clientSequenceNumber">,
|
|
899
|
+
): IDocumentMessage | undefined {
|
|
900
|
+
if (this.readonly === true) {
|
|
901
|
+
assert(
|
|
902
|
+
this.readOnlyInfo.readonly === true,
|
|
903
|
+
0x1f0 /* "Unexpected mismatch in readonly" */,
|
|
904
|
+
);
|
|
905
|
+
const error = new GenericError("deltaManagerReadonlySubmit", undefined /* error */, {
|
|
906
|
+
readonly: this.readOnlyInfo.readonly,
|
|
907
|
+
forcedReadonly: this.readOnlyInfo.forced,
|
|
908
|
+
readonlyPermissions: this.readOnlyInfo.permissions,
|
|
909
|
+
storageOnly: this.readOnlyInfo.storageOnly,
|
|
910
|
+
});
|
|
911
|
+
this.props.closeHandler(error);
|
|
912
|
+
return undefined;
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
// reset clientSequenceNumber if we are using new clientId.
|
|
916
|
+
// we keep info about old connection as long as possible to be able to account for all non-acked ops
|
|
917
|
+
// that we pick up on next connection.
|
|
918
|
+
assert(!!this.connection, 0x0e4 /* "Lost old connection!" */);
|
|
919
|
+
if (this.lastSubmittedClientId !== this.connection?.clientId) {
|
|
920
|
+
this.lastSubmittedClientId = this.connection?.clientId;
|
|
921
|
+
this.clientSequenceNumber = 0;
|
|
922
|
+
this.clientSequenceNumberObserved = 0;
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
if (!isRuntimeMessage(message)) {
|
|
926
|
+
this.localOpsToIgnore++;
|
|
927
|
+
} else {
|
|
928
|
+
this.localOpsToIgnore = 0;
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
return {
|
|
932
|
+
...message,
|
|
933
|
+
clientSequenceNumber: ++this.clientSequenceNumber,
|
|
934
|
+
};
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
public submitSignal(content: any) {
|
|
938
|
+
if (this.connection !== undefined) {
|
|
939
|
+
this.connection.submitSignal(content);
|
|
940
|
+
} else {
|
|
941
|
+
this.logger.sendErrorEvent({ eventName: "submitSignalDisconnected" });
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
public sendMessages(messages: IDocumentMessage[]) {
|
|
946
|
+
assert(this.connected, 0x2b4 /* "not connected on sending ops!" */);
|
|
947
|
+
// If connection is "read" or implicit "read" (got leave op for "write" connection),
|
|
948
|
+
// then op can't make it through - we will get a nack if op is sent.
|
|
949
|
+
// We can short-circuit this process.
|
|
950
|
+
// Note that we also want nacks to be rare and be treated as catastrophic failures.
|
|
951
|
+
// Be careful with reentrancy though - disconnected event should not be be raised in the
|
|
952
|
+
// middle of the current workflow, but rather on clean stack!
|
|
953
|
+
if (this.connectionMode === "read") {
|
|
954
|
+
if (!this.pendingReconnect) {
|
|
955
|
+
this.pendingReconnect = true;
|
|
956
|
+
Promise.resolve()
|
|
957
|
+
.then(async () => {
|
|
958
|
+
if (this.pendingReconnect) {
|
|
959
|
+
// still valid?
|
|
960
|
+
await this.reconnect(
|
|
961
|
+
"write", // connectionMode
|
|
962
|
+
"Switch to write", // message
|
|
963
|
+
);
|
|
964
|
+
}
|
|
965
|
+
})
|
|
966
|
+
.catch(() => {});
|
|
967
|
+
}
|
|
968
|
+
return;
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
assert(!this.pendingReconnect, 0x2b5 /* "logic error" */);
|
|
972
|
+
|
|
973
|
+
this._outbound.push(messages);
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
public beforeProcessingIncomingOp(message: ISequencedDocumentMessage) {
|
|
977
|
+
// if we have connection, and message is local, then we better treat is as local!
|
|
978
|
+
assert(
|
|
979
|
+
this.clientId !== message.clientId || this.lastSubmittedClientId === message.clientId,
|
|
980
|
+
0x0ee /* "Not accounting local messages correctly" */,
|
|
981
|
+
);
|
|
982
|
+
|
|
983
|
+
if (
|
|
984
|
+
this.lastSubmittedClientId !== undefined &&
|
|
985
|
+
this.lastSubmittedClientId === message.clientId
|
|
986
|
+
) {
|
|
987
|
+
const clientSequenceNumber = message.clientSequenceNumber;
|
|
988
|
+
|
|
989
|
+
assert(
|
|
990
|
+
this.clientSequenceNumberObserved < clientSequenceNumber,
|
|
991
|
+
0x0ef /* "client seq# not growing" */,
|
|
992
|
+
);
|
|
993
|
+
assert(
|
|
994
|
+
clientSequenceNumber <= this.clientSequenceNumber,
|
|
995
|
+
0x0f0 /* "Incoming local client seq# > generated by this client" */,
|
|
996
|
+
);
|
|
997
|
+
|
|
998
|
+
this.clientSequenceNumberObserved = clientSequenceNumber;
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
if (message.type === MessageType.ClientLeave) {
|
|
1002
|
+
const systemLeaveMessage = message as ISequencedDocumentSystemMessage;
|
|
1003
|
+
const clientId = JSON.parse(systemLeaveMessage.data) as string;
|
|
1004
|
+
if (clientId === this.clientId) {
|
|
1005
|
+
// We have been kicked out from quorum
|
|
1006
|
+
this.logger.sendPerformanceEvent({ eventName: "ReadConnectionTransition" });
|
|
1007
|
+
|
|
1008
|
+
// Please see #8483 for more details on why maintaining connection further as is would not work.
|
|
1009
|
+
// Short story - connection properties are immutable, and many processes (consensus DDSes, summarizer)
|
|
1010
|
+
// assume that connection stays "write" connection until disconnect, and act accordingly, which may
|
|
1011
|
+
// not work well with de-facto "read" connection we are in after receiving own leave op on timeout.
|
|
1012
|
+
// Clients need to be able to transition to "read" state after some time of inactivity!
|
|
1013
|
+
// Note - this may close container!
|
|
1014
|
+
this.reconnect(
|
|
1015
|
+
"read", // connectionMode
|
|
1016
|
+
"Switch to read", // message
|
|
1017
|
+
).catch((error) => {
|
|
1018
|
+
this.logger.sendErrorEvent({ eventName: "SwitchToReadConnection" }, error);
|
|
1019
|
+
});
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
private readonly opHandler = (documentId: string, messagesArg: ISequencedDocumentMessage[]) => {
|
|
1025
|
+
const messages = Array.isArray(messagesArg) ? messagesArg : [messagesArg];
|
|
1026
|
+
this.props.incomingOpHandler(messages, "opHandler");
|
|
1027
|
+
};
|
|
1028
|
+
|
|
1029
|
+
// Always connect in write mode after getting nacked.
|
|
1030
|
+
private readonly nackHandler = (documentId: string, messages: INack[]) => {
|
|
1031
|
+
const message = messages[0];
|
|
1032
|
+
if (this._readonlyPermissions === true) {
|
|
1033
|
+
this.props.closeHandler(
|
|
1034
|
+
createWriteError("writeOnReadOnlyDocument", { driverVersion: undefined }),
|
|
1035
|
+
);
|
|
1036
|
+
return;
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
const reconnectInfo = getNackReconnectInfo(message.content);
|
|
1040
|
+
|
|
1041
|
+
// If the nack indicates we cannot retry, then close the container outright
|
|
1042
|
+
if (!reconnectInfo.canRetry) {
|
|
1043
|
+
this.props.closeHandler(reconnectInfo);
|
|
1044
|
+
return;
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
this.reconnectOnError("write", reconnectInfo);
|
|
1048
|
+
};
|
|
1049
|
+
|
|
1050
|
+
// Connection mode is always read on disconnect/error unless the system mode was write.
|
|
1051
|
+
private readonly disconnectHandlerInternal = (disconnectReason: IAnyDriverError) => {
|
|
1052
|
+
// Note: we might get multiple disconnect calls on same socket, as early disconnect notification
|
|
1053
|
+
// ("server_disconnect", ODSP-specific) is mapped to "disconnect"
|
|
1054
|
+
this.reconnectOnError(this.defaultReconnectionMode, disconnectReason);
|
|
1055
|
+
};
|
|
1056
|
+
|
|
1057
|
+
private readonly errorHandler = (error: IAnyDriverError) => {
|
|
1058
|
+
this.reconnectOnError(this.defaultReconnectionMode, error);
|
|
1059
|
+
};
|
|
998
1060
|
}
|