/
ShardFactory.ts
99 lines (89 loc) · 2.96 KB
/
ShardFactory.ts
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { ChunkFactory } from "./ChunkFactory";
import { ShardCursor } from "./models/ChangeFeedCursor";
import { Shard } from "./Shard";
import { ContainerClient, CommonOptions } from "@azure/storage-blob";
import { Chunk } from "./Chunk";
import { AbortSignalLike } from "@azure/core-http";
import { SpanStatusCode } from "@azure/core-tracing";
import { createSpan } from "./utils/tracing";
/**
* Options to configure {@link ShardFactory.create} operation.
*/
export interface CreateShardOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*/
abortSignal?: AbortSignalLike;
}
export class ShardFactory {
private readonly chunkFactory: ChunkFactory;
constructor(chunkFactory: ChunkFactory) {
this.chunkFactory = chunkFactory;
}
public async create(
containerClient: ContainerClient,
shardPath: string,
shardCursor?: ShardCursor,
options: CreateShardOptions = {}
): Promise<Shard> {
const { span, updatedOptions } = createSpan("ShardFactory-create", options);
try {
const chunks: string[] = [];
const blockOffset: number = shardCursor?.BlockOffset || 0;
const eventIndex: number = shardCursor?.EventIndex || 0;
for await (const blobItem of containerClient.listBlobsFlat({
prefix: shardPath,
abortSignal: options.abortSignal,
tracingOptions: updatedOptions.tracingOptions,
})) {
chunks.push(blobItem.name);
}
const currentChunkPath = shardCursor?.CurrentChunkPath;
let chunkIndex = -1;
let currentChunk: Chunk | undefined = undefined;
// Chunks can be empty right after hour flips.
if (chunks.length !== 0) {
// Fast forward to current Chunk
if (currentChunkPath) {
for (let i = 0; i < chunks.length; i++) {
if (chunks[i] === currentChunkPath) {
chunkIndex = i;
break;
}
}
if (chunkIndex === -1) {
throw new Error(`Chunk ${currentChunkPath} not found.`);
}
} else {
chunkIndex = 0;
}
// Fast forward to current Chunk.
if (chunkIndex > 0) {
chunks.splice(0, chunkIndex);
}
currentChunk = await this.chunkFactory.create(
containerClient,
chunks.shift()!,
blockOffset,
eventIndex,
{
abortSignal: options.abortSignal,
tracingOptions: updatedOptions.tracingOptions,
}
);
}
return new Shard(containerClient, this.chunkFactory, chunks, currentChunk, shardPath);
} catch (e: any) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message,
});
throw e;
} finally {
span.end();
}
}
}