forked from microsoft/FluidFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummaryReader.ts
More file actions
313 lines (291 loc) · 9.81 KB
/
summaryReader.ts
File metadata and controls
313 lines (291 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import { bufferToString, toUtf8 } from "@fluidframework/common-utils";
import type {
IDocumentAttributes,
ISequencedDocumentMessage,
} from "@fluidframework/protocol-definitions";
import {
convertWholeFlatSummaryToSnapshotTreeAndBlobs,
type IGitManager,
type IWholeFlatSummary,
LatestSummaryId,
} from "@fluidframework/server-services-client";
import {
type IDeliState,
requestWithRetry,
shouldRetryNetworkError,
} from "@fluidframework/server-services-core";
import {
CommonProperties,
getLumberBaseProperties,
LumberEventName,
Lumberjack,
} from "@fluidframework/server-services-telemetry";
import type { ILatestSummaryState, ISummaryReader } from "./interfaces";
/**
* Git specific implementation of ISummaryReader
* @internal
*/
export class SummaryReader implements ISummaryReader {
private readonly lumberProperties: Record<string, any>;
constructor(
private readonly tenantId: string,
private readonly documentId: string,
private readonly summaryStorage: IGitManager,
private readonly enableWholeSummaryUpload: boolean,
private readonly isEphemeralContainer: boolean | undefined,
private readonly maxRetriesOnError: number = 6,
) {
this.lumberProperties = {
...getLumberBaseProperties(this.documentId, this.tenantId),
[CommonProperties.isEphemeralContainer]: this.isEphemeralContainer,
};
}
/**
* Reads the most recent version of summary for a document. In case the storage is having trouble processing the
* request, returns a set of defaults with fromSummary flag set to false.
*/
public async readLastSummary(): Promise<ILatestSummaryState> {
const summaryReaderMetric = Lumberjack.newLumberMetric(LumberEventName.SummaryReader);
summaryReaderMetric.setProperties(this.lumberProperties);
if (this.enableWholeSummaryUpload) {
try {
let wholeFlatSummary: IWholeFlatSummary | undefined;
try {
// Attempt to fetch the latest summary by "latest" ID
wholeFlatSummary = await requestWithRetry(
async () => this.summaryStorage.getSummary(LatestSummaryId),
"readWholeSummary_getSummary",
this.lumberProperties,
shouldRetryNetworkError,
this.maxRetriesOnError,
);
} catch (error: any) {
Lumberjack.warning(
`Error fetching summary with ID ${LatestSummaryId}. Will try with explicit ref.`,
this.lumberProperties,
error,
);
wholeFlatSummary = undefined;
}
if (!wholeFlatSummary) {
// If fetching by "latest" ID fails, attempt to fetch the latest summary by explicit ref
const existingRef = await requestWithRetry(
async () => this.summaryStorage.getRef(encodeURIComponent(this.documentId)),
"readWholeSummary_getRef",
this.lumberProperties,
shouldRetryNetworkError,
this.maxRetriesOnError,
);
if (!existingRef) {
throw new Error("Could not find a ref for the document.");
}
wholeFlatSummary = await requestWithRetry(
async () => this.summaryStorage.getSummary(existingRef.object.sha),
"readWholeSummary_getSummary",
this.lumberProperties,
shouldRetryNetworkError,
this.maxRetriesOnError,
);
}
const normalizedSummary =
convertWholeFlatSummaryToSnapshotTreeAndBlobs(wholeFlatSummary);
// Obtain IDs of specific fields from the downloaded summary
const attributesBlobId =
normalizedSummary.snapshotTree.trees[".protocol"]?.blobs?.attributes;
const scribeBlobId =
normalizedSummary.snapshotTree.trees[".serviceProtocol"]?.blobs?.scribe;
const deliBlobId =
normalizedSummary.snapshotTree.trees[".serviceProtocol"]?.blobs?.deli;
const opsBlobId = normalizedSummary.snapshotTree.trees[".logTail"]?.blobs?.logTail;
// Parse specific fields from the downloaded summary
const attributesContent = attributesBlobId
? normalizedSummary.blobs.get(attributesBlobId)
: undefined;
const scribeContent = scribeBlobId
? normalizedSummary.blobs.get(scribeBlobId)
: undefined;
const deliContent = deliBlobId
? normalizedSummary.blobs.get(deliBlobId)
: undefined;
const opsContent = opsBlobId ? normalizedSummary.blobs.get(opsBlobId) : undefined;
const attributes = attributesContent
? (JSON.parse(bufferToString(attributesContent, "utf8")) as IDocumentAttributes)
: this.getDefaultAttributes();
const scribe = scribeContent
? bufferToString(scribeContent, "utf8")
: this.getDefaultScribe();
const deli = deliContent
? (JSON.parse(bufferToString(deliContent, "utf8")) as IDeliState)
: this.getDefaultDeli();
const messages = opsContent
? (JSON.parse(
bufferToString(opsContent, "utf8"),
) as ISequencedDocumentMessage[])
: this.getDefaultMesages();
summaryReaderMetric.setProperties({
[CommonProperties.minLogtailSequenceNumber]: Math.min(
...messages.map((message) => message.sequenceNumber),
),
[CommonProperties.maxLogtailSequenceNumber]: Math.max(
...messages.map((message) => message.sequenceNumber),
),
[CommonProperties.lastSummarySequenceNumber]: deli.sequenceNumber,
[CommonProperties.clientCount]: deli.clients?.length,
});
summaryReaderMetric.success(`Successfully read whole summary`);
return {
protocolHead: attributes.sequenceNumber,
scribe,
messages,
fromSummary: true,
};
} catch (error: any) {
summaryReaderMetric.error(
`Returning default summary due to error when reading whole summary`,
error,
);
return this.getDefaultSummaryState();
}
} else {
try {
const existingRef = await requestWithRetry(
async () => this.summaryStorage.getRef(encodeURIComponent(this.documentId)),
"readSummary_getRef",
this.lumberProperties,
shouldRetryNetworkError,
this.maxRetriesOnError,
);
if (!existingRef) {
throw new Error("Could not find a ref for the document.");
}
const [attributesContent, scribeContent, deliContent, opsContent] =
await Promise.all([
requestWithRetry(
async () =>
this.summaryStorage.getContent(
existingRef.object.sha,
".protocol/attributes",
),
"readSummary_getProtocolAttributesContent",
this.lumberProperties,
shouldRetryNetworkError,
this.maxRetriesOnError,
).catch(() => undefined),
requestWithRetry(
async () =>
this.summaryStorage.getContent(
existingRef.object.sha,
".serviceProtocol/scribe",
),
"readSummary_getServiceProtocolScribeContent",
this.lumberProperties,
shouldRetryNetworkError,
this.maxRetriesOnError,
).catch(() => undefined),
requestWithRetry(
async () =>
this.summaryStorage.getContent(
existingRef.object.sha,
".serviceProtocol/deli",
),
"readSummary_getServiceProtocolDeliContent",
this.lumberProperties,
shouldRetryNetworkError,
this.maxRetriesOnError,
).catch(() => undefined),
requestWithRetry(
async () =>
this.summaryStorage.getContent(
existingRef.object.sha,
".logTail/logTail",
),
"readSummary_getLogTailContent",
this.lumberProperties,
shouldRetryNetworkError,
this.maxRetriesOnError,
).catch(() => undefined),
]);
const attributes = attributesContent
? (JSON.parse(
toUtf8(attributesContent.content, attributesContent.encoding),
) as IDocumentAttributes)
: this.getDefaultAttributes();
const scribe = scribeContent
? toUtf8(scribeContent.content, scribeContent.encoding)
: this.getDefaultScribe();
const deli = deliContent
? (JSON.parse(toUtf8(deliContent.content, deliContent.encoding)) as IDeliState)
: this.getDefaultDeli();
const messages = opsContent
? (JSON.parse(
toUtf8(opsContent.content, opsContent.encoding),
) as ISequencedDocumentMessage[])
: this.getDefaultMesages();
summaryReaderMetric.setProperties({
[CommonProperties.minLogtailSequenceNumber]: Math.min(
...messages.map((message) => message.sequenceNumber),
),
[CommonProperties.maxLogtailSequenceNumber]: Math.max(
...messages.map((message) => message.sequenceNumber),
),
[CommonProperties.lastSummarySequenceNumber]: deli.sequenceNumber,
[CommonProperties.clientCount]: deli.clients?.length,
});
summaryReaderMetric.success(`Successfully read summary`);
return {
protocolHead: attributes.sequenceNumber,
scribe,
messages,
fromSummary: true,
};
} catch (error: any) {
summaryReaderMetric.error(
`Returning default summary due to error when reading summary`,
error,
);
return this.getDefaultSummaryState();
}
}
}
private getDefaultSummaryState(): ILatestSummaryState {
return {
protocolHead: 0,
scribe: "",
messages: [],
fromSummary: false,
};
}
private getDefaultAttributes(): IDocumentAttributes {
Lumberjack.info("Using default attributes when reading summary.", this.lumberProperties);
return {
sequenceNumber: 0,
minimumSequenceNumber: 0,
};
}
private getDefaultScribe(): string {
Lumberjack.info("Using default scribe when reading summary.", this.lumberProperties);
return "";
}
private getDefaultDeli(): IDeliState {
Lumberjack.info("Using default deli state when reading summary.", this.lumberProperties);
return {
clients: undefined,
durableSequenceNumber: 0,
logOffset: 0,
sequenceNumber: 0,
signalClientConnectionNumber: 0,
expHash1: "",
lastSentMSN: undefined,
nackMessages: undefined,
checkpointTimestamp: undefined,
};
}
private getDefaultMesages(): ISequencedDocumentMessage[] {
Lumberjack.info("Using default messages when reading summary.", this.lumberProperties);
return [];
}
}