@fluidframework/container-runtime 2.0.0-internal.2.4.0 → 2.0.0-internal.3.0.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/dist/containerRuntime.d.ts +45 -42
- package/dist/containerRuntime.d.ts.map +1 -1
- package/dist/containerRuntime.js +87 -38
- package/dist/containerRuntime.js.map +1 -1
- package/dist/dataStoreContext.d.ts +1 -0
- package/dist/dataStoreContext.d.ts.map +1 -1
- package/dist/dataStoreContext.js +7 -2
- package/dist/dataStoreContext.js.map +1 -1
- package/dist/opLifecycle/outbox.d.ts.map +1 -1
- package/dist/opLifecycle/outbox.js +0 -1
- package/dist/opLifecycle/outbox.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/pendingStateManager.d.ts +4 -13
- package/dist/pendingStateManager.d.ts.map +1 -1
- package/dist/pendingStateManager.js +130 -160
- package/dist/pendingStateManager.js.map +1 -1
- package/dist/summarizerClientElection.d.ts +1 -2
- package/dist/summarizerClientElection.d.ts.map +1 -1
- package/dist/summarizerClientElection.js +3 -30
- package/dist/summarizerClientElection.js.map +1 -1
- package/dist/summarizerTypes.d.ts +0 -4
- package/dist/summarizerTypes.d.ts.map +1 -1
- package/dist/summarizerTypes.js.map +1 -1
- package/lib/containerRuntime.d.ts +45 -42
- package/lib/containerRuntime.d.ts.map +1 -1
- package/lib/containerRuntime.js +87 -38
- package/lib/containerRuntime.js.map +1 -1
- package/lib/dataStoreContext.d.ts +1 -0
- package/lib/dataStoreContext.d.ts.map +1 -1
- package/lib/dataStoreContext.js +7 -2
- package/lib/dataStoreContext.js.map +1 -1
- package/lib/opLifecycle/outbox.d.ts.map +1 -1
- package/lib/opLifecycle/outbox.js +0 -1
- package/lib/opLifecycle/outbox.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/pendingStateManager.d.ts +4 -13
- package/lib/pendingStateManager.d.ts.map +1 -1
- package/lib/pendingStateManager.js +130 -160
- package/lib/pendingStateManager.js.map +1 -1
- package/lib/summarizerClientElection.d.ts +1 -2
- package/lib/summarizerClientElection.d.ts.map +1 -1
- package/lib/summarizerClientElection.js +3 -30
- package/lib/summarizerClientElection.js.map +1 -1
- package/lib/summarizerTypes.d.ts +0 -4
- package/lib/summarizerTypes.d.ts.map +1 -1
- package/lib/summarizerTypes.js.map +1 -1
- package/package.json +34 -16
- package/src/containerRuntime.ts +116 -84
- package/src/dataStoreContext.ts +12 -6
- package/src/opLifecycle/outbox.ts +0 -2
- package/src/packageVersion.ts +1 -1
- package/src/pendingStateManager.ts +146 -187
- package/src/summarizerClientElection.ts +1 -30
- package/src/summarizerTypes.ts +0 -4
|
@@ -23,38 +23,74 @@ const packageVersion_1 = require("./packageVersion");
|
|
|
23
23
|
*/
|
|
24
24
|
class PendingStateManager {
|
|
25
25
|
constructor(stateHandler, initialLocalState) {
|
|
26
|
-
var _a;
|
|
26
|
+
var _a, _b;
|
|
27
27
|
this.stateHandler = stateHandler;
|
|
28
|
-
this.
|
|
28
|
+
this.pendingMessages = new double_ended_queue_1.default();
|
|
29
|
+
this.initialMessages = new double_ended_queue_1.default();
|
|
29
30
|
this.disposeOnce = new common_utils_1.Lazy(() => {
|
|
30
|
-
this.
|
|
31
|
-
this.
|
|
31
|
+
this.initialMessages.clear();
|
|
32
|
+
this.pendingMessages.clear();
|
|
32
33
|
});
|
|
33
|
-
// Maintains the count of messages that are currently unacked.
|
|
34
|
-
this._pendingMessagesCount = 0;
|
|
35
34
|
// Indicates whether we are processing a batch.
|
|
36
35
|
this.isProcessingBatch = false;
|
|
37
36
|
this.dispose = () => this.disposeOnce.value;
|
|
38
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Convert old local state format to the new format
|
|
39
|
+
* The old format contained "flush" messages as the indicator of batch ends
|
|
40
|
+
* The new format instead uses batch metadata on the last message to indicate batch ends
|
|
41
|
+
* ! TODO: Remove this conversion in "2.0.0-internal.4.0.0" as rollback from future version will be new format
|
|
42
|
+
* AB#2496 tracks removal
|
|
43
|
+
*/
|
|
44
|
+
if (initialLocalState === null || initialLocalState === void 0 ? void 0 : initialLocalState.pendingStates) {
|
|
45
|
+
const pendingStates = initialLocalState === null || initialLocalState === void 0 ? void 0 : initialLocalState.pendingStates;
|
|
46
|
+
let currentlyBatching = false;
|
|
47
|
+
for (let i = 0; i < pendingStates.length; i++) {
|
|
48
|
+
const initialState = pendingStates[i];
|
|
49
|
+
// Skip over "flush" messages
|
|
50
|
+
if (initialState.type === "message") {
|
|
51
|
+
if ((_a = initialState.opMetadata) === null || _a === void 0 ? void 0 : _a.batch) {
|
|
52
|
+
currentlyBatching = true;
|
|
53
|
+
}
|
|
54
|
+
else if (((_b = initialState.opMetadata) === null || _b === void 0 ? void 0 : _b.batch) === false) {
|
|
55
|
+
currentlyBatching = false;
|
|
56
|
+
}
|
|
57
|
+
else if (
|
|
58
|
+
// End of batch if we are currently batching and this is last message or next message is flush
|
|
59
|
+
currentlyBatching
|
|
60
|
+
&& (i === pendingStates.length - 1 || pendingStates[i + 1].type === "flush")) {
|
|
61
|
+
currentlyBatching = false;
|
|
62
|
+
initialState.opMetadata = Object.assign(Object.assign({}, initialState.opMetadata), { batch: false });
|
|
63
|
+
}
|
|
64
|
+
this.initialMessages.push(initialState);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
39
68
|
}
|
|
40
69
|
get pendingMessagesCount() {
|
|
41
|
-
return this.
|
|
70
|
+
return this.pendingMessages.length;
|
|
42
71
|
}
|
|
43
72
|
/**
|
|
44
|
-
* Called to check if there are any pending messages in the pending
|
|
73
|
+
* Called to check if there are any pending messages in the pending message queue.
|
|
45
74
|
* @returns A boolean indicating whether there are messages or not.
|
|
46
75
|
*/
|
|
47
76
|
hasPendingMessages() {
|
|
48
|
-
return this.
|
|
77
|
+
return !this.pendingMessages.isEmpty() || !this.initialMessages.isEmpty();
|
|
49
78
|
}
|
|
50
79
|
getLocalState() {
|
|
51
|
-
(0, common_utils_1.assert)(this.
|
|
52
|
-
if (this.
|
|
80
|
+
(0, common_utils_1.assert)(this.initialMessages.isEmpty(), 0x2e9 /* "Must call getLocalState() after applying initial states" */);
|
|
81
|
+
if (!this.pendingMessages.isEmpty()) {
|
|
53
82
|
return {
|
|
54
|
-
pendingStates: this.
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
83
|
+
pendingStates: this.pendingMessages.toArray().reduce((arr, message) => {
|
|
84
|
+
var _a;
|
|
85
|
+
// delete localOpMetadata since it may not be serializable
|
|
86
|
+
// and will be regenerated by applyStashedOp()
|
|
87
|
+
arr.push(Object.assign(Object.assign({}, message), { localOpMetadata: undefined }));
|
|
88
|
+
// TODO: Remove in 2.0.0-internal.4.0.0 (AB#2496)
|
|
89
|
+
if (((_a = message.opMetadata) === null || _a === void 0 ? void 0 : _a.batch) === false) {
|
|
90
|
+
arr.push({ type: "flush" });
|
|
91
|
+
}
|
|
92
|
+
return arr;
|
|
93
|
+
}, new Array()),
|
|
58
94
|
};
|
|
59
95
|
}
|
|
60
96
|
}
|
|
@@ -77,20 +113,7 @@ class PendingStateManager {
|
|
|
77
113
|
localOpMetadata,
|
|
78
114
|
opMetadata,
|
|
79
115
|
};
|
|
80
|
-
this.
|
|
81
|
-
this._pendingMessagesCount++;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Called when flush() is called on the ContainerRuntime to manually flush messages.
|
|
85
|
-
*/
|
|
86
|
-
onFlush() {
|
|
87
|
-
// If the previous state is not a message, flush is a no-op.
|
|
88
|
-
const previousState = this.pendingStates.peekBack();
|
|
89
|
-
if ((previousState === null || previousState === void 0 ? void 0 : previousState.type) !== "message") {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
// An explicit flush is interesting and is tracked only if there are messages sent in TurnBased mode.
|
|
93
|
-
this.pendingStates.push({ type: "flush" });
|
|
116
|
+
this.pendingMessages.push(pendingMessage);
|
|
94
117
|
}
|
|
95
118
|
/**
|
|
96
119
|
* Applies stashed ops at their reference sequence number so they are ready to be ACKed or resubmitted
|
|
@@ -98,29 +121,23 @@ class PendingStateManager {
|
|
|
98
121
|
*/
|
|
99
122
|
async applyStashedOpsAt(seqNum) {
|
|
100
123
|
// apply stashed ops at sequence number
|
|
101
|
-
while (!this.
|
|
124
|
+
while (!this.initialMessages.isEmpty()) {
|
|
102
125
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
103
|
-
const
|
|
104
|
-
if (
|
|
105
|
-
if (
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
throw new Error("loaded from snapshot too recent to apply stashed ops");
|
|
111
|
-
}
|
|
126
|
+
const nextMessage = this.initialMessages.peekFront();
|
|
127
|
+
if (seqNum !== undefined) {
|
|
128
|
+
if (nextMessage.referenceSequenceNumber > seqNum) {
|
|
129
|
+
break; // nothing left to do at this sequence number
|
|
130
|
+
}
|
|
131
|
+
if (nextMessage.referenceSequenceNumber < seqNum) {
|
|
132
|
+
throw new Error("loaded from snapshot too recent to apply stashed ops");
|
|
112
133
|
}
|
|
113
|
-
// applyStashedOp will cause the DDS to behave as if it has sent the op but not actually send it
|
|
114
|
-
const localOpMetadata = await this.stateHandler.applyStashedOp(nextState.messageType, nextState.content);
|
|
115
|
-
nextState.localOpMetadata = localOpMetadata;
|
|
116
134
|
}
|
|
117
|
-
//
|
|
135
|
+
// applyStashedOp will cause the DDS to behave as if it has sent the op but not actually send it
|
|
136
|
+
const localOpMetadata = await this.stateHandler.applyStashedOp(nextMessage.messageType, nextMessage.content);
|
|
137
|
+
nextMessage.localOpMetadata = localOpMetadata;
|
|
138
|
+
// then we push onto pendingMessages which will cause PendingStateManager to resubmit when we connect
|
|
118
139
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
119
|
-
|
|
120
|
-
this.pendingStates.push(firstPendingState);
|
|
121
|
-
if (firstPendingState.type === "message") {
|
|
122
|
-
this._pendingMessagesCount++;
|
|
123
|
-
}
|
|
140
|
+
this.pendingMessages.push(this.initialMessages.shift());
|
|
124
141
|
}
|
|
125
142
|
}
|
|
126
143
|
/**
|
|
@@ -131,23 +148,21 @@ class PendingStateManager {
|
|
|
131
148
|
processPendingLocalMessage(message) {
|
|
132
149
|
// Pre-processing part - This may be the start of a batch.
|
|
133
150
|
this.maybeProcessBatchBegin(message);
|
|
134
|
-
// Get the next
|
|
135
|
-
const
|
|
136
|
-
(0, common_utils_1.assert)(
|
|
137
|
-
this.
|
|
151
|
+
// Get the next message from the pending queue. Verify a message exists.
|
|
152
|
+
const pendingMessage = this.pendingMessages.peekFront();
|
|
153
|
+
(0, common_utils_1.assert)(pendingMessage !== undefined, 0x169 /* "No pending message found for this remote message" */);
|
|
154
|
+
this.pendingMessages.shift();
|
|
138
155
|
// Processing part - Verify that there has been no data corruption.
|
|
139
156
|
// The clientSequenceNumber of the incoming message must match that of the pending message.
|
|
140
|
-
if (
|
|
157
|
+
if (pendingMessage.clientSequenceNumber !== message.clientSequenceNumber) {
|
|
141
158
|
// Close the container because this could indicate data corruption.
|
|
142
|
-
const error = container_utils_1.DataProcessingError.create("pending local message clientSequenceNumber mismatch", "unexpectedAckReceived", message, { expectedClientSequenceNumber:
|
|
159
|
+
const error = container_utils_1.DataProcessingError.create("pending local message clientSequenceNumber mismatch", "unexpectedAckReceived", message, { expectedClientSequenceNumber: pendingMessage.clientSequenceNumber });
|
|
143
160
|
this.stateHandler.close(error);
|
|
144
161
|
return;
|
|
145
162
|
}
|
|
146
|
-
this._pendingMessagesCount--;
|
|
147
|
-
(0, common_utils_1.assert)(this._pendingMessagesCount >= 0, 0x3d6 /* positive */);
|
|
148
163
|
// Post-processing part - If we are processing a batch then this could be the last message in the batch.
|
|
149
164
|
this.maybeProcessBatchEnd(message);
|
|
150
|
-
return
|
|
165
|
+
return pendingMessage.localOpMetadata;
|
|
151
166
|
}
|
|
152
167
|
/**
|
|
153
168
|
* This message could be the first message in batch. If so, set batch state marking the beginning of a batch.
|
|
@@ -155,20 +170,6 @@ class PendingStateManager {
|
|
|
155
170
|
*/
|
|
156
171
|
maybeProcessBatchBegin(message) {
|
|
157
172
|
var _a;
|
|
158
|
-
/**
|
|
159
|
-
* We are checking if the next message is the start of a batch. It can happen in the following scenarios:
|
|
160
|
-
*
|
|
161
|
-
* 1. The FlushMode was set to TurnBased before this message was sent.
|
|
162
|
-
*
|
|
163
|
-
* 2. The FlushMode was already TurnBased and a flush was called before this message was sent. This essentially
|
|
164
|
-
* means that the flush marked the end of a previous batch and beginning of a new batch.
|
|
165
|
-
*
|
|
166
|
-
* Keep reading pending states from the queue until we encounter a message. It's possible that the FlushMode was
|
|
167
|
-
* updated a bunch of times without sending any messages.
|
|
168
|
-
*/
|
|
169
|
-
while (this.peekNextPendingState().type !== "message") {
|
|
170
|
-
this.pendingStates.shift();
|
|
171
|
-
}
|
|
172
173
|
// This message is the first in a batch if the "batch" property on the metadata is set to true
|
|
173
174
|
if ((_a = message.metadata) === null || _a === void 0 ? void 0 : _a.batch) {
|
|
174
175
|
// We should not already be processing a batch and there should be no pending batch begin message.
|
|
@@ -187,48 +188,36 @@ class PendingStateManager {
|
|
|
187
188
|
if (!this.isProcessingBatch) {
|
|
188
189
|
return;
|
|
189
190
|
}
|
|
190
|
-
const nextPendingState = this.peekNextPendingState();
|
|
191
|
-
if (nextPendingState.type === "message") {
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
191
|
// There should be a pending batch begin message.
|
|
195
192
|
(0, common_utils_1.assert)(this.pendingBatchBeginMessage !== undefined, 0x16d /* "There is no pending batch begin message" */);
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
193
|
+
const batchEndMetadata = (_a = message.metadata) === null || _a === void 0 ? void 0 : _a.batch;
|
|
194
|
+
if (this.pendingMessages.isEmpty() || batchEndMetadata === false) {
|
|
195
|
+
// Get the batch begin metadata from the first message in the batch.
|
|
196
|
+
const batchBeginMetadata = (_b = this.pendingBatchBeginMessage.metadata) === null || _b === void 0 ? void 0 : _b.batch;
|
|
197
|
+
// There could be just a single message in the batch. If so, it should not have any batch metadata. If there
|
|
198
|
+
// are multiple messages in the batch, verify that we got the correct batch begin and end metadata.
|
|
199
|
+
if (this.pendingBatchBeginMessage === message) {
|
|
200
|
+
(0, common_utils_1.assert)(batchBeginMetadata === undefined, 0x16e /* "Batch with single message should not have batch metadata" */);
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
if (batchBeginMetadata !== true || batchEndMetadata !== false) {
|
|
204
|
+
this.stateHandler.close(container_utils_1.DataProcessingError.create("Pending batch inconsistency", // Formerly known as asserts 0x16f and 0x170
|
|
205
|
+
"processPendingLocalMessage", message, {
|
|
206
|
+
runtimeVersion: packageVersion_1.pkgVersion,
|
|
207
|
+
batchClientId: this.pendingBatchBeginMessage.clientId,
|
|
208
|
+
clientId: this.stateHandler.clientId(),
|
|
209
|
+
hasBatchStart: batchBeginMetadata === true,
|
|
210
|
+
hasBatchEnd: batchEndMetadata === false,
|
|
211
|
+
messageType: message.type,
|
|
212
|
+
batchStartSequenceNumber: this.pendingBatchBeginMessage.clientSequenceNumber,
|
|
213
|
+
pendingMessagesCount: this.pendingMessagesCount,
|
|
214
|
+
}));
|
|
215
|
+
}
|
|
219
216
|
}
|
|
217
|
+
// Clear the pending batch state now that we have processed the entire batch.
|
|
218
|
+
this.pendingBatchBeginMessage = undefined;
|
|
219
|
+
this.isProcessingBatch = false;
|
|
220
220
|
}
|
|
221
|
-
// Clear the pending batch state now that we have processed the entire batch.
|
|
222
|
-
this.pendingBatchBeginMessage = undefined;
|
|
223
|
-
this.isProcessingBatch = false;
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Returns the next pending state from the pending state queue.
|
|
227
|
-
*/
|
|
228
|
-
peekNextPendingState() {
|
|
229
|
-
const nextPendingState = this.pendingStates.peekFront();
|
|
230
|
-
(0, common_utils_1.assert)(!!nextPendingState, 0x171 /* "No pending state found for the remote message" */);
|
|
231
|
-
return nextPendingState;
|
|
232
221
|
}
|
|
233
222
|
/**
|
|
234
223
|
* Called when the Container's connection state changes. If the Container gets connected, it replays all the pending
|
|
@@ -240,63 +229,44 @@ class PendingStateManager {
|
|
|
240
229
|
// This assert suggests we are about to send same ops twice, which will result in data loss.
|
|
241
230
|
(0, common_utils_1.assert)(this.clientId !== this.stateHandler.clientId(), 0x173 /* "replayPendingStates called twice for same clientId!" */);
|
|
242
231
|
this.clientId = this.stateHandler.clientId();
|
|
243
|
-
(0, common_utils_1.assert)(this.
|
|
244
|
-
let
|
|
245
|
-
if (
|
|
232
|
+
(0, common_utils_1.assert)(this.initialMessages.isEmpty(), 0x174 /* "initial states should be empty before replaying pending" */);
|
|
233
|
+
let pendingMessagesCount = this.pendingMessages.length;
|
|
234
|
+
if (pendingMessagesCount === 0) {
|
|
246
235
|
return;
|
|
247
236
|
}
|
|
248
|
-
//
|
|
249
|
-
this._pendingMessagesCount = 0;
|
|
250
|
-
const messageBatchQueue = new double_ended_queue_1.default();
|
|
251
|
-
// Process exactly `pendingStatesCount` items in the queue as it represents the number of states that were
|
|
237
|
+
// Process exactly `pendingMessagesCount` items in the queue as it represents the number of messages that were
|
|
252
238
|
// pending when we connected. This is important because the `reSubmitFn` might add more items in the queue
|
|
253
239
|
// which must not be replayed.
|
|
254
|
-
while (
|
|
240
|
+
while (pendingMessagesCount > 0) {
|
|
255
241
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
this.stateHandler.reSubmit(
|
|
242
|
+
let pendingMessage = this.pendingMessages.shift();
|
|
243
|
+
pendingMessagesCount--;
|
|
244
|
+
(0, common_utils_1.assert)(((_a = pendingMessage.opMetadata) === null || _a === void 0 ? void 0 : _a.batch) !== false, 0x41b /* We cannot process batches in chunks */);
|
|
245
|
+
/**
|
|
246
|
+
* We want to ensure grouped messages get processed in a batch.
|
|
247
|
+
* Note: It is not possible for the PendingStateManager to receive a partially acked batch. It will
|
|
248
|
+
* either receive the whole batch ack or nothing at all.
|
|
249
|
+
*/
|
|
250
|
+
if ((_b = pendingMessage.opMetadata) === null || _b === void 0 ? void 0 : _b.batch) {
|
|
251
|
+
(0, common_utils_1.assert)(pendingMessagesCount > 0, 0x554 /* Last pending message cannot be a batch begin */);
|
|
252
|
+
this.stateHandler.orderSequentially(() => {
|
|
253
|
+
var _a, _b;
|
|
254
|
+
while (pendingMessagesCount >= 0) { // check is >= because batch end may be last pending message
|
|
255
|
+
this.stateHandler.reSubmit(pendingMessage.messageType, pendingMessage.content, pendingMessage.localOpMetadata, pendingMessage.opMetadata);
|
|
256
|
+
if (((_a = pendingMessage.opMetadata) === null || _a === void 0 ? void 0 : _a.batch) === false) {
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
(0, common_utils_1.assert)(pendingMessagesCount > 0, 0x555 /* No batch end found */);
|
|
260
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
261
|
+
pendingMessage = this.pendingMessages.shift();
|
|
262
|
+
pendingMessagesCount--;
|
|
263
|
+
(0, common_utils_1.assert)(((_b = pendingMessage.opMetadata) === null || _b === void 0 ? void 0 : _b.batch) !== true, 0x556 /* Batch start needs a corresponding batch end */);
|
|
270
264
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
* We can't rely on the "batch" property in the message metadata as it gets
|
|
276
|
-
* updated elsewhere and it is not the same object instance that gets updated.
|
|
277
|
-
*/
|
|
278
|
-
if (messageBatchQueue.length > 0) {
|
|
279
|
-
this.stateHandler.orderSequentially(() => {
|
|
280
|
-
while (messageBatchQueue.length > 0) {
|
|
281
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
282
|
-
const message = messageBatchQueue.dequeue();
|
|
283
|
-
this.stateHandler.reSubmit(message.messageType, message.content, message.localOpMetadata, message.opMetadata);
|
|
284
|
-
}
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
(0, common_utils_1.assert)(messageBatchQueue.length === 0, 0x41c /* cannot flush in the middle of a batch */);
|
|
288
|
-
this.stateHandler.flush();
|
|
289
|
-
break;
|
|
290
|
-
default:
|
|
291
|
-
break;
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
this.stateHandler.reSubmit(pendingMessage.messageType, pendingMessage.content, pendingMessage.localOpMetadata, pendingMessage.opMetadata);
|
|
292
269
|
}
|
|
293
|
-
pendingStatesCount--;
|
|
294
|
-
}
|
|
295
|
-
// There are some cases where ops are stashed but not flushed. We need to ensure they are resubmitted
|
|
296
|
-
while (messageBatchQueue.length > 0) {
|
|
297
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
298
|
-
const message = messageBatchQueue.dequeue();
|
|
299
|
-
this.stateHandler.reSubmit(message.messageType, message.content, message.localOpMetadata, message.opMetadata);
|
|
300
270
|
}
|
|
301
271
|
}
|
|
302
272
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pendingStateManager.js","sourceRoot":"","sources":["../src/pendingStateManager.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;AAGH,+DAA4D;AAE5D,qEAAsE;AAItE,4EAAuC;AAEvC,qDAA8C;AAmD9C;;;;;;;;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AY,CAAC,QAAQ,CACtB,YAAY,CAAC,WAAW,EACxB,YAAY,CAAC,OAAO,EACpB,YAAY,CAAC,eAAe,EAC5B,YAAY,CAAC,UAAU,CAAC,CAAC;qBAChC;oBACD,MAAM;gBACV,KAAK,OAAO;oBACR;;;;uBAIG;oBACH,IAAI,iBAAiB,CAAC,MAAM,GAAG,CAAC,EAAE;wBAC9B,IAAI,CAAC,YAAY,CAAC,iBAAiB,CAAC,GAAG,EAAE;4BACrC,OAAO,iBAAiB,CAAC,MAAM,GAAG,CAAC,EAAE;gCACjC,oEAAoE;gCACpE,MAAM,OAAO,GAAG,iBAAiB,CAAC,OAAO,EAAG,CAAC;gCAC7C,IAAI,CAAC,YAAY,CAAC,QAAQ,CACtB,OAAO,CAAC,WAAW,EACnB,OAAO,CAAC,OAAO,EACf,OAAO,CAAC,eAAe,EACvB,OAAO,CAAC,UAAU,CAAC,CAAC;6BAC3B;wBACL,CAAC,CAAC,CAAC;qBACN;oBACD,IAAA,qBAAM,EAAC,iBAAiB,CAAC,MAAM,KAAK,CAAC,EAAE,KAAK,CAAC,2CAA2C,CAAC,CAAC;oBAC1F,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,CAAC;oBAC1B,MAAM;gBACV;oBACI,MAAM;aACb;YACD,kBAAkB,EAAE,CAAC;SACxB;QAED,qGAAqG;QACrG,OAAO,iBAAiB,CAAC,MAAM,GAAG,CAAC,EAAE;YACjC,oEAAoE;YACpE,MAAM,OAAO,GAAG,iBAAiB,CAAC,OAAO,EAAG,CAAC;YAC7C,IAAI,CAAC,YAAY,CAAC,QAAQ,CACtB,OAAO,CAAC,WAAW,EACnB,OAAO,CAAC,OAAO,EACf,OAAO,CAAC,eAAe,EACvB,OAAO,CAAC,UAAU,CAAC,CAAC;SAC3B;IACL,CAAC;CACJ;AA/VD,kDA+VC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IDisposable } from \"@fluidframework/common-definitions\";\nimport { assert, Lazy } from \"@fluidframework/common-utils\";\nimport { ICriticalContainerError } from \"@fluidframework/container-definitions\";\nimport { DataProcessingError } from \"@fluidframework/container-utils\";\nimport {\n ISequencedDocumentMessage,\n} from \"@fluidframework/protocol-definitions\";\nimport Deque from \"double-ended-queue\";\nimport { ContainerMessageType } from \"./containerRuntime\";\nimport { pkgVersion } from \"./packageVersion\";\n\n/**\n * This represents a message that has been submitted and is added to the pending queue when `submit` is called on the\n * ContainerRuntime. This message has either not been ack'd by the server or has not been submitted to the server yet.\n */\nexport interface IPendingMessage {\n type: \"message\";\n messageType: ContainerMessageType;\n clientSequenceNumber: number;\n referenceSequenceNumber: number;\n content: any;\n localOpMetadata: unknown;\n opMetadata: Record<string, unknown> | undefined;\n}\n\n/**\n * This represents an explicit flush call and is added to the pending queue when flush is called on the ContainerRuntime\n * to flush pending messages.\n */\nexport interface IPendingFlush {\n type: \"flush\";\n}\n\nexport type IPendingState = IPendingMessage | IPendingFlush;\n\nexport interface IPendingLocalState {\n /**\n * list of pending states, including ops and batch information\n */\n pendingStates: IPendingState[];\n}\n\nexport interface IRuntimeStateHandler{\n connected(): boolean;\n clientId(): string | undefined;\n close(error?: ICriticalContainerError): void;\n applyStashedOp: (type: ContainerMessageType, content: ISequencedDocumentMessage) => Promise<unknown>;\n flush(): void;\n reSubmit(\n type: ContainerMessageType,\n content: any,\n localOpMetadata: unknown,\n opMetadata: Record<string, unknown> | undefined): void;\n rollback(\n type: ContainerMessageType,\n content: any,\n localOpMetadata: unknown): void;\n orderSequentially(callback: () => void): void;\n}\n\n/**\n * PendingStateManager is responsible for maintaining the messages that have not been sent or have not yet been\n * acknowledged by the server. It also maintains the batch information for both automatically and manually flushed\n * batches along with the messages.\n * When the Container reconnects, it replays the pending states, which includes manual flushing\n * of messages and triggering resubmission of unacked ops.\n *\n * It verifies that all the ops are acked, are received in the right order and batch information is correct.\n */\nexport class PendingStateManager implements IDisposable {\n private readonly pendingStates = new Deque<IPendingState>();\n private readonly initialStates: Deque<IPendingState>;\n private readonly disposeOnce = new Lazy<void>(() => {\n this.initialStates.clear();\n this.pendingStates.clear();\n });\n\n // Maintains the count of messages that are currently unacked.\n private _pendingMessagesCount: number = 0;\n public get pendingMessagesCount(): number {\n return this._pendingMessagesCount;\n }\n\n // Indicates whether we are processing a batch.\n private isProcessingBatch: boolean = false;\n\n // This stores the first message in the batch that we are processing. This is used to verify that we get\n // the correct batch metadata.\n private pendingBatchBeginMessage: ISequencedDocumentMessage | undefined;\n\n private clientId: string | undefined;\n\n /**\n * Called to check if there are any pending messages in the pending state queue.\n * @returns A boolean indicating whether there are messages or not.\n */\n public hasPendingMessages(): boolean {\n return this._pendingMessagesCount !== 0 || !this.initialStates.isEmpty();\n }\n\n public getLocalState(): IPendingLocalState | undefined {\n assert(this.initialStates.isEmpty(), 0x2e9 /* \"Must call getLocalState() after applying initial states\" */);\n if (this.hasPendingMessages()) {\n return {\n pendingStates: this.pendingStates.toArray().map(\n // delete localOpMetadata since it may not be serializable\n // and will be regenerated by applyStashedOp()\n (state) => state.type === \"message\" ? { ...state, localOpMetadata: undefined } : state),\n };\n }\n }\n\n constructor(\n private readonly stateHandler: IRuntimeStateHandler,\n initialLocalState: IPendingLocalState | undefined,\n ) {\n this.initialStates = new Deque<IPendingState>(initialLocalState?.pendingStates ?? []);\n }\n\n public get disposed() { return this.disposeOnce.evaluated; }\n public readonly dispose = () => this.disposeOnce.value;\n\n /**\n * Called when a message is submitted locally. Adds the message and the associated details to the pending state\n * queue.\n * @param type - The container message type.\n * @param clientSequenceNumber - The clientSequenceNumber associated with the message.\n * @param content - The message content.\n * @param localOpMetadata - The local metadata associated with the message.\n */\n public onSubmitMessage(\n type: ContainerMessageType,\n clientSequenceNumber: number,\n referenceSequenceNumber: number,\n content: any,\n localOpMetadata: unknown,\n opMetadata: Record<string, unknown> | undefined,\n ) {\n const pendingMessage: IPendingMessage = {\n type: \"message\",\n messageType: type,\n clientSequenceNumber,\n referenceSequenceNumber,\n content,\n localOpMetadata,\n opMetadata,\n };\n\n this.pendingStates.push(pendingMessage);\n\n this._pendingMessagesCount++;\n }\n\n /**\n * Called when flush() is called on the ContainerRuntime to manually flush messages.\n */\n public onFlush() {\n // If the previous state is not a message, flush is a no-op.\n const previousState = this.pendingStates.peekBack();\n if (previousState?.type !== \"message\") {\n return;\n }\n\n // An explicit flush is interesting and is tracked only if there are messages sent in TurnBased mode.\n this.pendingStates.push({ type: \"flush\" });\n }\n\n /**\n * Applies stashed ops at their reference sequence number so they are ready to be ACKed or resubmitted\n * @param seqNum - Sequence number at which to apply ops. Will apply all ops if seqNum is undefined.\n */\n public async applyStashedOpsAt(seqNum?: number) {\n // apply stashed ops at sequence number\n while (!this.initialStates.isEmpty()) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const nextState = this.initialStates.peekFront()!;\n if (nextState.type === \"message\") {\n if (seqNum !== undefined) {\n if (nextState.referenceSequenceNumber > seqNum) {\n break; // nothing left to do at this sequence number\n } else if (nextState.referenceSequenceNumber < seqNum) {\n throw new Error(\"loaded from snapshot too recent to apply stashed ops\");\n }\n }\n\n // applyStashedOp will cause the DDS to behave as if it has sent the op but not actually send it\n const localOpMetadata =\n await this.stateHandler.applyStashedOp(nextState.messageType, nextState.content);\n nextState.localOpMetadata = localOpMetadata;\n }\n\n // then we push onto pendingStates which will cause PendingStateManager to resubmit when we connect\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const firstPendingState = this.initialStates.shift()!;\n this.pendingStates.push(firstPendingState);\n if (firstPendingState.type === \"message\") {\n this._pendingMessagesCount++;\n }\n }\n }\n\n /**\n * Processes a local message once its ack'd by the server. It verifies that there was no data corruption and that\n * the batch information was preserved for batch messages.\n * @param message - The message that got ack'd and needs to be processed.\n */\n public processPendingLocalMessage(message: ISequencedDocumentMessage): unknown {\n // Pre-processing part - This may be the start of a batch.\n this.maybeProcessBatchBegin(message);\n\n // Get the next state from the pending queue and verify that it is of type \"message\".\n const pendingState = this.peekNextPendingState();\n assert(pendingState.type === \"message\", 0x169 /* \"No pending message found for this remote message\" */);\n this.pendingStates.shift();\n\n // Processing part - Verify that there has been no data corruption.\n // The clientSequenceNumber of the incoming message must match that of the pending message.\n if (pendingState.clientSequenceNumber !== message.clientSequenceNumber) {\n // Close the container because this could indicate data corruption.\n const error = DataProcessingError.create(\n \"pending local message clientSequenceNumber mismatch\",\n \"unexpectedAckReceived\",\n message,\n { expectedClientSequenceNumber: pendingState.clientSequenceNumber },\n );\n\n this.stateHandler.close(error);\n return;\n }\n\n this._pendingMessagesCount--;\n assert(this._pendingMessagesCount >= 0, 0x3d6 /* positive */);\n\n // Post-processing part - If we are processing a batch then this could be the last message in the batch.\n this.maybeProcessBatchEnd(message);\n\n return pendingState.localOpMetadata;\n }\n\n /**\n * This message could be the first message in batch. If so, set batch state marking the beginning of a batch.\n * @param message - The message that is being processed.\n */\n private maybeProcessBatchBegin(message: ISequencedDocumentMessage) {\n /**\n * We are checking if the next message is the start of a batch. It can happen in the following scenarios:\n *\n * 1. The FlushMode was set to TurnBased before this message was sent.\n *\n * 2. The FlushMode was already TurnBased and a flush was called before this message was sent. This essentially\n * means that the flush marked the end of a previous batch and beginning of a new batch.\n *\n * Keep reading pending states from the queue until we encounter a message. It's possible that the FlushMode was\n * updated a bunch of times without sending any messages.\n */\n while (this.peekNextPendingState().type !== \"message\") {\n this.pendingStates.shift();\n }\n\n // This message is the first in a batch if the \"batch\" property on the metadata is set to true\n if (message.metadata?.batch) {\n // We should not already be processing a batch and there should be no pending batch begin message.\n assert(!this.isProcessingBatch && this.pendingBatchBeginMessage === undefined,\n 0x16b /* \"The pending batch state indicates we are already processing a batch\" */);\n\n // Set the pending batch state indicating we have started processing a batch.\n this.pendingBatchBeginMessage = message;\n this.isProcessingBatch = true;\n }\n }\n\n /**\n * This message could be the last message in batch. If so, clear batch state since the batch is complete.\n * @param message - The message that is being processed.\n */\n private maybeProcessBatchEnd(message: ISequencedDocumentMessage) {\n if (!this.isProcessingBatch) {\n return;\n }\n\n const nextPendingState = this.peekNextPendingState();\n if (nextPendingState.type === \"message\") {\n return;\n }\n\n // There should be a pending batch begin message.\n assert(this.pendingBatchBeginMessage !== undefined, 0x16d /* \"There is no pending batch begin message\" */);\n\n // Get the batch begin metadata from the first message in the batch.\n const batchBeginMetadata = this.pendingBatchBeginMessage.metadata?.batch;\n\n // There could be just a single message in the batch. If so, it should not have any batch metadata. If there\n // are multiple messages in the batch, verify that we got the correct batch begin and end metadata.\n if (this.pendingBatchBeginMessage === message) {\n assert(batchBeginMetadata === undefined,\n 0x16e /* \"Batch with single message should not have batch metadata\" */);\n } else {\n // Get the batch metadata from the last message in the batch.\n const batchEndMetadata = message.metadata?.batch;\n if (batchBeginMetadata !== true || batchEndMetadata !== false) {\n this.stateHandler.close(DataProcessingError.create(\n \"Pending batch inconsistency\", // Formerly known as asserts 0x16f and 0x170\n \"processPendingLocalMessage\",\n message,\n {\n runtimeVersion: pkgVersion,\n batchClientId: this.pendingBatchBeginMessage.clientId,\n clientId: this.stateHandler.clientId(),\n hasBatchStart: batchBeginMetadata === true,\n hasBatchEnd: batchEndMetadata === false,\n messageType: message.type,\n batchStartSequenceNumber: this.pendingBatchBeginMessage.clientSequenceNumber,\n pendingMessagesCount: this.pendingMessagesCount,\n nextPendingState: nextPendingState.type,\n }));\n }\n }\n\n // Clear the pending batch state now that we have processed the entire batch.\n this.pendingBatchBeginMessage = undefined;\n this.isProcessingBatch = false;\n }\n\n /**\n * Returns the next pending state from the pending state queue.\n */\n private peekNextPendingState(): IPendingState {\n const nextPendingState = this.pendingStates.peekFront();\n assert(!!nextPendingState, 0x171 /* \"No pending state found for the remote message\" */);\n return nextPendingState;\n }\n\n /**\n * Called when the Container's connection state changes. If the Container gets connected, it replays all the pending\n * states in its queue. This includes triggering resubmission of unacked ops.\n */\n public replayPendingStates() {\n assert(this.stateHandler.connected(), 0x172 /* \"The connection state is not consistent with the runtime\" */);\n\n // This assert suggests we are about to send same ops twice, which will result in data loss.\n assert(this.clientId !== this.stateHandler.clientId(),\n 0x173 /* \"replayPendingStates called twice for same clientId!\" */);\n this.clientId = this.stateHandler.clientId();\n\n assert(this.initialStates.isEmpty(), 0x174 /* \"initial states should be empty before replaying pending\" */);\n\n let pendingStatesCount = this.pendingStates.length;\n if (pendingStatesCount === 0) {\n return;\n }\n\n // Reset the pending message count because all these messages will be removed from the queue.\n this._pendingMessagesCount = 0;\n\n const messageBatchQueue = new Deque<IPendingMessage>();\n\n // Process exactly `pendingStatesCount` items in the queue as it represents the number of states that were\n // pending when we connected. This is important because the `reSubmitFn` might add more items in the queue\n // which must not be replayed.\n while (pendingStatesCount > 0) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const pendingState = this.pendingStates.shift()!;\n switch (pendingState.type) {\n case \"message\":\n assert(pendingState.opMetadata?.batch !== false || messageBatchQueue.length > 0,\n 0x41b /* We cannot process batches in chunks */);\n /**\n * We want to ensure grouped messages get processed in a batch.\n * Note: It is not possible for the PendingStateManager to receive a partially acked batch. It will\n * either receive the whole batch ack or nothing at all.\n */\n if (messageBatchQueue.length > 0 || pendingState.opMetadata?.batch) {\n messageBatchQueue.enqueue(pendingState);\n } else {\n this.stateHandler.reSubmit(\n pendingState.messageType,\n pendingState.content,\n pendingState.localOpMetadata,\n pendingState.opMetadata);\n }\n break;\n case \"flush\":\n /**\n * A \"flush\" call can indicate the end of a batch.\n * We can't rely on the \"batch\" property in the message metadata as it gets\n * updated elsewhere and it is not the same object instance that gets updated.\n */\n if (messageBatchQueue.length > 0) {\n this.stateHandler.orderSequentially(() => {\n while (messageBatchQueue.length > 0) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const message = messageBatchQueue.dequeue()!;\n this.stateHandler.reSubmit(\n message.messageType,\n message.content,\n message.localOpMetadata,\n message.opMetadata);\n }\n });\n }\n assert(messageBatchQueue.length === 0, 0x41c /* cannot flush in the middle of a batch */);\n this.stateHandler.flush();\n break;\n default:\n break;\n }\n pendingStatesCount--;\n }\n\n // There are some cases where ops are stashed but not flushed. We need to ensure they are resubmitted\n while (messageBatchQueue.length > 0) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const message = messageBatchQueue.dequeue()!;\n this.stateHandler.reSubmit(\n message.messageType,\n message.content,\n message.localOpMetadata,\n message.opMetadata);\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"pendingStateManager.js","sourceRoot":"","sources":["../src/pendingStateManager.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;AAGH,+DAA4D;AAE5D,qEAAsE;AAItE,4EAAuC;AAEvC,qDAA8C;AAmD9C;;;;;;;;GAQG;AACH,MAAa,mBAAmB;IAgD5B,YACqB,YAAkC,EACnD,iBAAiD;;QADhC,iBAAY,GAAZ,YAAY,CAAsB;QAhDtC,oBAAe,GAAG,IAAI,4BAAK,EAAmB,CAAC;QAC/C,oBAAe,GAAG,IAAI,4BAAK,EAAmB,CAAC;QAC/C,gBAAW,GAAG,IAAI,mBAAI,CAAO,GAAG,EAAE;YAC/C,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;YAC7B,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;QACjC,CAAC,CAAC,CAAC;QAMH,+CAA+C;QACvC,sBAAiB,GAAY,KAAK,CAAC;QAyE3B,YAAO,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC;QAlCnD;;;;;;WAMG;QACH,IAAI,iBAAiB,aAAjB,iBAAiB,uBAAjB,iBAAiB,CAAE,aAAa,EAAE;YAClC,MAAM,aAAa,GAAG,iBAAiB,aAAjB,iBAAiB,uBAAjB,iBAAiB,CAAE,aAAa,CAAC;YACvD,IAAI,iBAAiB,GAAG,KAAK,CAAC;YAC9B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,aAAa,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;gBAC3C,MAAM,YAAY,GAAG,aAAa,CA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CAAC;YAEpG;;;;eAIG;YACH,IAAI,MAAA,cAAc,CAAC,UAAU,0CAAE,KAAK,EAAE;gBAClC,IAAA,qBAAM,EAAC,oBAAoB,GAAG,CAAC,EAAE,KAAK,CAAC,kDAAkD,CAAC,CAAC;gBAE3F,IAAI,CAAC,YAAY,CAAC,iBAAiB,CAAC,GAAG,EAAE;;oBACrC,OAAO,oBAAoB,IAAI,CAAC,EAAE,EAAE,4DAA4D;wBAC5F,IAAI,CAAC,YAAY,CAAC,QAAQ,CACtB,cAAc,CAAC,WAAW,EAC1B,cAAc,CAAC,OAAO,EACtB,cAAc,CAAC,eAAe,EAC9B,cAAc,CAAC,UAAU,CAAC,CAAC;wBAE/B,IAAI,CAAA,MAAA,cAAc,CAAC,UAAU,0CAAE,KAAK,MAAK,KAAK,EAAE;4BAC5C,MAAM;yBACT;wBACD,IAAA,qBAAM,EAAC,oBAAoB,GAAG,CAAC,EAAE,KAAK,CAAC,wBAAwB,CAAC,CAAC;wBAEjE,oEAAoE;wBACpE,cAAc,GAAG,IAAI,CAAC,eAAe,CAAC,KAAK,EAAG,CAAC;wBAC/C,oBAAoB,EAAE,CAAC;wBACvB,IAAA,qBAAM,EAAC,CAAA,MAAA,cAAc,CAAC,UAAU,0CAAE,KAAK,MAAK,IAAI,EAC5C,KAAK,CAAC,iDAAiD,CAAC,CAAC;qBAChE;gBACL,CAAC,CAAC,CAAC;aACN;iBAAM;gBACH,IAAI,CAAC,YAAY,CAAC,QAAQ,CACtB,cAAc,CAAC,WAAW,EAC1B,cAAc,CAAC,OAAO,EACtB,cAAc,CAAC,eAAe,EAC9B,cAAc,CAAC,UAAU,CAAC,CAAC;aAClC;SACJ;IACL,CAAC;CACJ;AAtTD,kDAsTC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IDisposable } from \"@fluidframework/common-definitions\";\nimport { assert, Lazy } from \"@fluidframework/common-utils\";\nimport { ICriticalContainerError } from \"@fluidframework/container-definitions\";\nimport { DataProcessingError } from \"@fluidframework/container-utils\";\nimport {\n ISequencedDocumentMessage,\n} from \"@fluidframework/protocol-definitions\";\nimport Deque from \"double-ended-queue\";\nimport { ContainerMessageType } from \"./containerRuntime\";\nimport { pkgVersion } from \"./packageVersion\";\n\n/**\n * This represents a message that has been submitted and is added to the pending queue when `submit` is called on the\n * ContainerRuntime. This message has either not been ack'd by the server or has not been submitted to the server yet.\n */\nexport interface IPendingMessage {\n type: \"message\";\n messageType: ContainerMessageType;\n clientSequenceNumber: number;\n referenceSequenceNumber: number;\n content: any;\n localOpMetadata: unknown;\n opMetadata: Record<string, unknown> | undefined;\n}\n\n/**\n * This represents an explicit flush call and is added to the pending queue when flush is called on the ContainerRuntime\n * to flush pending messages.\n * @deprecated Use batch metadata on IPendingMessage instead. To be removed in 2.0.0-internal.4.0.0 (AB#2496)\n */\nexport interface IPendingFlush {\n type: \"flush\";\n}\n\nexport type IPendingState = IPendingMessage | IPendingFlush;\n\nexport interface IPendingLocalState {\n /**\n * list of pending states, including ops and batch information\n */\n pendingStates: IPendingState[];\n}\n\nexport interface IRuntimeStateHandler {\n connected(): boolean;\n clientId(): string | undefined;\n close(error?: ICriticalContainerError): void;\n applyStashedOp: (type: ContainerMessageType, content: ISequencedDocumentMessage) => Promise<unknown>;\n reSubmit(\n type: ContainerMessageType,\n content: any,\n localOpMetadata: unknown,\n opMetadata: Record<string, unknown> | undefined): void;\n rollback(\n type: ContainerMessageType,\n content: any,\n localOpMetadata: unknown): void;\n orderSequentially(callback: () => void): void;\n}\n\n/**\n * PendingStateManager is responsible for maintaining the messages that have not been sent or have not yet been\n * acknowledged by the server. It also maintains the batch information for both automatically and manually flushed\n * batches along with the messages.\n * When the Container reconnects, it replays the pending states, which includes manual flushing\n * of messages and triggering resubmission of unacked ops.\n *\n * It verifies that all the ops are acked, are received in the right order and batch information is correct.\n */\nexport class PendingStateManager implements IDisposable {\n private readonly pendingMessages = new Deque<IPendingMessage>();\n private readonly initialMessages = new Deque<IPendingMessage>();\n private readonly disposeOnce = new Lazy<void>(() => {\n this.initialMessages.clear();\n this.pendingMessages.clear();\n });\n\n public get pendingMessagesCount(): number {\n return this.pendingMessages.length;\n }\n\n // Indicates whether we are processing a batch.\n private isProcessingBatch: boolean = false;\n\n // This stores the first message in the batch that we are processing. This is used to verify that we get\n // the correct batch metadata.\n private pendingBatchBeginMessage: ISequencedDocumentMessage | undefined;\n\n private clientId: string | undefined;\n\n /**\n * Called to check if there are any pending messages in the pending message queue.\n * @returns A boolean indicating whether there are messages or not.\n */\n public hasPendingMessages(): boolean {\n return !this.pendingMessages.isEmpty() || !this.initialMessages.isEmpty();\n }\n\n public getLocalState(): IPendingLocalState | undefined {\n assert(this.initialMessages.isEmpty(), 0x2e9 /* \"Must call getLocalState() after applying initial states\" */);\n if (!this.pendingMessages.isEmpty()) {\n return {\n pendingStates: this.pendingMessages.toArray().reduce((arr, message) => {\n // delete localOpMetadata since it may not be serializable\n // and will be regenerated by applyStashedOp()\n arr.push({ ...message, localOpMetadata: undefined });\n\n // TODO: Remove in 2.0.0-internal.4.0.0 (AB#2496)\n if (message.opMetadata?.batch === false) {\n arr.push({ type: \"flush\" });\n }\n return arr;\n }, new Array<IPendingState>()),\n };\n }\n }\n\n constructor(\n private readonly stateHandler: IRuntimeStateHandler,\n initialLocalState: IPendingLocalState | undefined,\n ) {\n /**\n * Convert old local state format to the new format\n * The old format contained \"flush\" messages as the indicator of batch ends\n * The new format instead uses batch metadata on the last message to indicate batch ends\n * ! TODO: Remove this conversion in \"2.0.0-internal.4.0.0\" as rollback from future version will be new format\n * AB#2496 tracks removal\n */\n if (initialLocalState?.pendingStates) {\n const pendingStates = initialLocalState?.pendingStates;\n let currentlyBatching = false;\n for (let i = 0; i < pendingStates.length; i++) {\n const initialState = pendingStates[i];\n\n // Skip over \"flush\" messages\n if (initialState.type === \"message\") {\n if (initialState.opMetadata?.batch) {\n currentlyBatching = true;\n } else if (initialState.opMetadata?.batch === false) {\n currentlyBatching = false;\n } else if (\n // End of batch if we are currently batching and this is last message or next message is flush\n currentlyBatching\n && (i === pendingStates.length - 1 || pendingStates[i + 1].type === \"flush\")\n ) {\n currentlyBatching = false;\n initialState.opMetadata = { ...initialState.opMetadata, batch: false };\n }\n this.initialMessages.push(initialState);\n }\n }\n }\n }\n\n public get disposed() { return this.disposeOnce.evaluated; }\n public readonly dispose = () => this.disposeOnce.value;\n\n /**\n * Called when a message is submitted locally. Adds the message and the associated details to the pending state\n * queue.\n * @param type - The container message type.\n * @param clientSequenceNumber - The clientSequenceNumber associated with the message.\n * @param content - The message content.\n * @param localOpMetadata - The local metadata associated with the message.\n */\n public onSubmitMessage(\n type: ContainerMessageType,\n clientSequenceNumber: number,\n referenceSequenceNumber: number,\n content: any,\n localOpMetadata: unknown,\n opMetadata: Record<string, unknown> | undefined,\n ) {\n const pendingMessage: IPendingMessage = {\n type: \"message\",\n messageType: type,\n clientSequenceNumber,\n referenceSequenceNumber,\n content,\n localOpMetadata,\n opMetadata,\n };\n\n this.pendingMessages.push(pendingMessage);\n }\n\n /**\n * Applies stashed ops at their reference sequence number so they are ready to be ACKed or resubmitted\n * @param seqNum - Sequence number at which to apply ops. Will apply all ops if seqNum is undefined.\n */\n public async applyStashedOpsAt(seqNum?: number) {\n // apply stashed ops at sequence number\n while (!this.initialMessages.isEmpty()) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const nextMessage = this.initialMessages.peekFront()!;\n if (seqNum !== undefined) {\n if (nextMessage.referenceSequenceNumber > seqNum) {\n break; // nothing left to do at this sequence number\n }\n if (nextMessage.referenceSequenceNumber < seqNum) {\n throw new Error(\"loaded from snapshot too recent to apply stashed ops\");\n }\n }\n\n // applyStashedOp will cause the DDS to behave as if it has sent the op but not actually send it\n const localOpMetadata =\n await this.stateHandler.applyStashedOp(nextMessage.messageType, nextMessage.content);\n nextMessage.localOpMetadata = localOpMetadata;\n\n // then we push onto pendingMessages which will cause PendingStateManager to resubmit when we connect\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n this.pendingMessages.push(this.initialMessages.shift()!);\n }\n }\n\n /**\n * Processes a local message once its ack'd by the server. It verifies that there was no data corruption and that\n * the batch information was preserved for batch messages.\n * @param message - The message that got ack'd and needs to be processed.\n */\n public processPendingLocalMessage(message: ISequencedDocumentMessage): unknown {\n // Pre-processing part - This may be the start of a batch.\n this.maybeProcessBatchBegin(message);\n\n // Get the next message from the pending queue. Verify a message exists.\n const pendingMessage = this.pendingMessages.peekFront();\n assert(pendingMessage !== undefined, 0x169 /* \"No pending message found for this remote message\" */);\n this.pendingMessages.shift();\n\n // Processing part - Verify that there has been no data corruption.\n // The clientSequenceNumber of the incoming message must match that of the pending message.\n if (pendingMessage.clientSequenceNumber !== message.clientSequenceNumber) {\n // Close the container because this could indicate data corruption.\n const error = DataProcessingError.create(\n \"pending local message clientSequenceNumber mismatch\",\n \"unexpectedAckReceived\",\n message,\n { expectedClientSequenceNumber: pendingMessage.clientSequenceNumber },\n );\n\n this.stateHandler.close(error);\n return;\n }\n\n // Post-processing part - If we are processing a batch then this could be the last message in the batch.\n this.maybeProcessBatchEnd(message);\n\n return pendingMessage.localOpMetadata;\n }\n\n /**\n * This message could be the first message in batch. If so, set batch state marking the beginning of a batch.\n * @param message - The message that is being processed.\n */\n private maybeProcessBatchBegin(message: ISequencedDocumentMessage) {\n // This message is the first in a batch if the \"batch\" property on the metadata is set to true\n if (message.metadata?.batch) {\n // We should not already be processing a batch and there should be no pending batch begin message.\n assert(!this.isProcessingBatch && this.pendingBatchBeginMessage === undefined,\n 0x16b /* \"The pending batch state indicates we are already processing a batch\" */);\n\n // Set the pending batch state indicating we have started processing a batch.\n this.pendingBatchBeginMessage = message;\n this.isProcessingBatch = true;\n }\n }\n\n /**\n * This message could be the last message in batch. If so, clear batch state since the batch is complete.\n * @param message - The message that is being processed.\n */\n private maybeProcessBatchEnd(message: ISequencedDocumentMessage) {\n if (!this.isProcessingBatch) {\n return;\n }\n\n // There should be a pending batch begin message.\n assert(this.pendingBatchBeginMessage !== undefined, 0x16d /* \"There is no pending batch begin message\" */);\n\n const batchEndMetadata = message.metadata?.batch;\n if (this.pendingMessages.isEmpty() || batchEndMetadata === false) {\n // Get the batch begin metadata from the first message in the batch.\n const batchBeginMetadata = this.pendingBatchBeginMessage.metadata?.batch;\n\n // There could be just a single message in the batch. If so, it should not have any batch metadata. If there\n // are multiple messages in the batch, verify that we got the correct batch begin and end metadata.\n if (this.pendingBatchBeginMessage === message) {\n assert(batchBeginMetadata === undefined,\n 0x16e /* \"Batch with single message should not have batch metadata\" */);\n } else {\n if (batchBeginMetadata !== true || batchEndMetadata !== false) {\n this.stateHandler.close(DataProcessingError.create(\n \"Pending batch inconsistency\", // Formerly known as asserts 0x16f and 0x170\n \"processPendingLocalMessage\",\n message,\n {\n runtimeVersion: pkgVersion,\n batchClientId: this.pendingBatchBeginMessage.clientId,\n clientId: this.stateHandler.clientId(),\n hasBatchStart: batchBeginMetadata === true,\n hasBatchEnd: batchEndMetadata === false,\n messageType: message.type,\n batchStartSequenceNumber: this.pendingBatchBeginMessage.clientSequenceNumber,\n pendingMessagesCount: this.pendingMessagesCount,\n }));\n }\n }\n\n // Clear the pending batch state now that we have processed the entire batch.\n this.pendingBatchBeginMessage = undefined;\n this.isProcessingBatch = false;\n }\n }\n\n /**\n * Called when the Container's connection state changes. If the Container gets connected, it replays all the pending\n * states in its queue. This includes triggering resubmission of unacked ops.\n */\n public replayPendingStates() {\n assert(this.stateHandler.connected(), 0x172 /* \"The connection state is not consistent with the runtime\" */);\n\n // This assert suggests we are about to send same ops twice, which will result in data loss.\n assert(this.clientId !== this.stateHandler.clientId(),\n 0x173 /* \"replayPendingStates called twice for same clientId!\" */);\n this.clientId = this.stateHandler.clientId();\n\n assert(this.initialMessages.isEmpty(), 0x174 /* \"initial states should be empty before replaying pending\" */);\n\n let pendingMessagesCount = this.pendingMessages.length;\n if (pendingMessagesCount === 0) {\n return;\n }\n\n // Process exactly `pendingMessagesCount` items in the queue as it represents the number of messages that were\n // pending when we connected. This is important because the `reSubmitFn` might add more items in the queue\n // which must not be replayed.\n while (pendingMessagesCount > 0) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n let pendingMessage = this.pendingMessages.shift()!;\n pendingMessagesCount--;\n assert(pendingMessage.opMetadata?.batch !== false, 0x41b /* We cannot process batches in chunks */);\n\n /**\n * We want to ensure grouped messages get processed in a batch.\n * Note: It is not possible for the PendingStateManager to receive a partially acked batch. It will\n * either receive the whole batch ack or nothing at all.\n */\n if (pendingMessage.opMetadata?.batch) {\n assert(pendingMessagesCount > 0, 0x554 /* Last pending message cannot be a batch begin */);\n\n this.stateHandler.orderSequentially(() => {\n while (pendingMessagesCount >= 0) { // check is >= because batch end may be last pending message\n this.stateHandler.reSubmit(\n pendingMessage.messageType,\n pendingMessage.content,\n pendingMessage.localOpMetadata,\n pendingMessage.opMetadata);\n\n if (pendingMessage.opMetadata?.batch === false) {\n break;\n }\n assert(pendingMessagesCount > 0, 0x555 /* No batch end found */);\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n pendingMessage = this.pendingMessages.shift()!;\n pendingMessagesCount--;\n assert(pendingMessage.opMetadata?.batch !== true,\n 0x556 /* Batch start needs a corresponding batch end */);\n }\n });\n } else {\n this.stateHandler.reSubmit(\n pendingMessage.messageType,\n pendingMessage.content,\n pendingMessage.localOpMetadata,\n pendingMessage.opMetadata);\n }\n }\n }\n}\n"]}
|
|
@@ -25,7 +25,6 @@ export declare class SummarizerClientElection extends TypedEventEmitter<ISummari
|
|
|
25
25
|
private readonly summaryCollection;
|
|
26
26
|
readonly clientElection: IOrderedClientElection;
|
|
27
27
|
private readonly maxOpsSinceLastSummary;
|
|
28
|
-
private readonly electionEnabled;
|
|
29
28
|
/**
|
|
30
29
|
* Used to calculate number of ops since last summary ack for the current elected client.
|
|
31
30
|
* This will be undefined if there is no elected summarizer, or no summary ack has been
|
|
@@ -41,7 +40,7 @@ export declare class SummarizerClientElection extends TypedEventEmitter<ISummari
|
|
|
41
40
|
private lastReportedSeq;
|
|
42
41
|
get electedClientId(): string | undefined;
|
|
43
42
|
get electedParentId(): string | undefined;
|
|
44
|
-
constructor(logger: ITelemetryLogger, summaryCollection: IEventProvider<ISummaryCollectionOpEvents>, clientElection: IOrderedClientElection, maxOpsSinceLastSummary: number
|
|
43
|
+
constructor(logger: ITelemetryLogger, summaryCollection: IEventProvider<ISummaryCollectionOpEvents>, clientElection: IOrderedClientElection, maxOpsSinceLastSummary: number);
|
|
45
44
|
serialize(): ISerializedElection;
|
|
46
45
|
static isClientEligible(client: ITrackedClient): boolean;
|
|
47
46
|
static readonly clientDetailsPermitElection: (details: IClientDetails) => boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"summarizerClientElection.d.ts","sourceRoot":"","sources":["../src/summarizerClientElection.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AAC9F,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,cAAc,EAAe,MAAM,sCAAsC,CAAC;AACnF,OAAO,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACtG,OAAO,EAAE,0BAA0B,EAAE,MAAM,qBAAqB,CAAC;AAEjE,eAAO,MAAM,oBAAoB,eAAe,CAAC;AAEjD,MAAM,WAAW,+BAAgC,SAAQ,MAAM;IAC3D,CAAC,KAAK,EAAE,0BAA0B,EAAE,OAAO,EAAE,MAAM,IAAI,GAAG,IAAI,CAAC;CAClE;AAED,MAAM,WAAW,yBAA0B,SAAQ,cAAc,CAAC,+BAA+B,CAAC;IAC9F,QAAQ,CAAC,eAAe,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7C,QAAQ,CAAC,eAAe,EAAE,MAAM,GAAG,SAAS,CAAC;CACh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;;;;;OAKG;IACH,OAAO,CAAC,0BAA0B,CAAqB;IACvD;;;;OAIG;IACH,OAAO,CAAC,eAAe,CAAK;IAE5B,IAAW,eAAe,uBAEzB;IACD,IAAW,eAAe,uBAEzB;gBAGoB,MAAM,EAAE,gBAAgB,EACxB,iBAAiB,EAAE,cAAc,CAAC,0BAA0B,CAAC,EAC9D,cAAc,EAAE,sBAAsB,EACrC,sBAAsB,EAAE,MAAM;IAwD5C,SAAS,IAAI,mBAAmB;WASzB,gBAAgB,CAAC,MAAM,EAAE,cAAc,GAAG,OAAO;IAS/D,gBAAuB,2BAA2B,YAAa,cAAc,KAAG,OAAO,CACT;CACjF"}
|
|
@@ -14,13 +14,12 @@ exports.summarizerClientType = "summarizer";
|
|
|
14
14
|
* for some configured number of ops.
|
|
15
15
|
*/
|
|
16
16
|
class SummarizerClientElection extends common_utils_1.TypedEventEmitter {
|
|
17
|
-
constructor(logger, summaryCollection, clientElection, maxOpsSinceLastSummary
|
|
17
|
+
constructor(logger, summaryCollection, clientElection, maxOpsSinceLastSummary) {
|
|
18
18
|
super();
|
|
19
19
|
this.logger = logger;
|
|
20
20
|
this.summaryCollection = summaryCollection;
|
|
21
21
|
this.clientElection = clientElection;
|
|
22
22
|
this.maxOpsSinceLastSummary = maxOpsSinceLastSummary;
|
|
23
|
-
this.electionEnabled = electionEnabled;
|
|
24
23
|
/**
|
|
25
24
|
* Used to prevent excess logging by recording the sequence number that we last reported at,
|
|
26
25
|
* and making sure we don't report another event to telemetry. If things work as intended,
|
|
@@ -30,7 +29,7 @@ class SummarizerClientElection extends common_utils_1.TypedEventEmitter {
|
|
|
30
29
|
// On every inbound op, if enough ops pass without seeing a summary ack (per elected client),
|
|
31
30
|
// elect a new client and log to telemetry.
|
|
32
31
|
this.summaryCollection.on("default", ({ sequenceNumber }) => {
|
|
33
|
-
var _a, _b
|
|
32
|
+
var _a, _b;
|
|
34
33
|
const electedClientId = this.electedClientId;
|
|
35
34
|
if (electedClientId === undefined) {
|
|
36
35
|
// Reset election if no elected client, but eligible clients are connected.
|
|
@@ -42,7 +41,7 @@ class SummarizerClientElection extends common_utils_1.TypedEventEmitter {
|
|
|
42
41
|
}
|
|
43
42
|
return;
|
|
44
43
|
}
|
|
45
|
-
|
|
44
|
+
const electionSequenceNumber = this.clientElection.electionSequenceNumber;
|
|
46
45
|
const opsWithoutSummary = sequenceNumber - ((_a = this.lastSummaryAckSeqForClient) !== null && _a !== void 0 ? _a : electionSequenceNumber);
|
|
47
46
|
if (opsWithoutSummary > this.maxOpsSinceLastSummary) {
|
|
48
47
|
// Log and elect a new summarizer client.
|
|
@@ -54,35 +53,9 @@ class SummarizerClientElection extends common_utils_1.TypedEventEmitter {
|
|
|
54
53
|
lastSummaryAckSeqForClient: this.lastSummaryAckSeqForClient,
|
|
55
54
|
electionSequenceNumber,
|
|
56
55
|
nextElectedClientId: (_b = this.clientElection.peekNextElectedClient()) === null || _b === void 0 ? void 0 : _b.clientId,
|
|
57
|
-
electionEnabled: this.electionEnabled,
|
|
58
56
|
});
|
|
59
57
|
this.lastReportedSeq = sequenceNumber;
|
|
60
58
|
}
|
|
61
|
-
if (this.electionEnabled) {
|
|
62
|
-
const previousParentId = this.electedParentId;
|
|
63
|
-
this.clientElection.incrementElectedClient(sequenceNumber);
|
|
64
|
-
// Verify that state incremented as expected. This should be reliable,
|
|
65
|
-
// since all of OrderedClientElection is synchronous.
|
|
66
|
-
electionSequenceNumber = this.clientElection.electionSequenceNumber;
|
|
67
|
-
if (sequenceNumber > ((_c = this.lastSummaryAckSeqForClient) !== null && _c !== void 0 ? _c : electionSequenceNumber)) {
|
|
68
|
-
if (opsSinceLastReport > this.maxOpsSinceLastSummary) {
|
|
69
|
-
this.logger.sendErrorEvent({
|
|
70
|
-
eventName: "UnexpectedElectionSequenceNumber",
|
|
71
|
-
// Expected to be undefined
|
|
72
|
-
lastSummaryAckSeqForClient: this.lastSummaryAckSeqForClient,
|
|
73
|
-
// Expected to be same as op sequenceNumber
|
|
74
|
-
electionSequenceNumber,
|
|
75
|
-
sequenceNumber,
|
|
76
|
-
previousClientId: electedClientId,
|
|
77
|
-
previousParentId,
|
|
78
|
-
electedParentId: this.electedParentId,
|
|
79
|
-
electedClientId: this.electedClientId,
|
|
80
|
-
opsSinceLastReport,
|
|
81
|
-
maxOpsSinceLastSummary,
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
59
|
}
|
|
87
60
|
});
|
|
88
61
|
// When a summary ack comes in, reset our op seq counter.
|