|
1
|
/** |
|
2
|
* Helper functions to wait for index synchronization in a deterministic way |
|
3
|
* by directly checking the index_data table instead of using arbitrary timeouts. |
|
4
|
*/ |
|
5
|
|
|
6
|
export interface WaitForIndexOptions { |
|
7
|
timeout?: number |
|
8
|
pollInterval?: number |
|
9
|
isLink?: boolean |
|
10
|
} |
|
11
|
|
|
12
|
/** |
|
13
|
* Wait for specific entities to be indexed by checking the index_data table directly. |
|
14
|
* This is more reliable than using arbitrary timeouts. |
|
15
|
*/ |
|
16
|
export async function waitForIndexedEntities( |
|
17
|
dbConnection: any, |
|
18
|
entityName: string, |
|
19
|
entityIds: string[], |
|
20
|
options: WaitForIndexOptions = {} |
|
21
|
): Promise<void> { |
|
22
|
const { timeout = 30000, pollInterval = 250 } = options |
|
23
|
const startTime = Date.now() |
|
24
|
|
|
25
|
// Normalize the entity name to match partition table naming convention |
|
26
|
const normalizedName = !options.isLink |
|
27
|
? entityName.toLowerCase().replace(/[^a-z0-9_]/g, "_") |
|
28
|
: `link${entityName.toLowerCase()}` |
|
29
|
const partitionTableName = `cat_${normalizedName}` |
|
30
|
const normalizedEntityName = options.isLink ? `Link${entityName}` : entityName |
|
31
|
|
|
32
|
while (Date.now() - startTime < timeout) { |
|
33
|
try { |
|
34
|
// Query the index_data table to check if all entities are indexed |
|
35
|
const result = await dbConnection.raw( |
|
36
|
`SELECT id FROM index_data WHERE id = ANY(?) AND staled_at IS NULL`, |
|
37
|
[entityIds] |
|
38
|
) |
|
39
|
|
|
40
|
const indexedIds = result.rows |
|
41
|
? result.rows.map((row: any) => row.id) |
|
42
|
: result.map((row: any) => row.id) |
|
43
|
|
|
44
|
// Check if all expected entities are indexed in index_data |
|
45
|
const allIndexed = entityIds.every((id) => indexedIds.includes(id)) |
|
46
|
|
|
47
|
if (!allIndexed) { |
|
48
|
await new Promise((resolve) => setTimeout(resolve, pollInterval)) |
|
49
|
continue |
|
50
|
} |
|
51
|
|
|
52
|
// Also check if data is replicated to the partition table |
|
53
|
const partitionResult = await dbConnection.raw( |
|
54
|
`SELECT id FROM ${partitionTableName} WHERE id = ANY(?)`, |
|
55
|
[entityIds] |
|
56
|
) |
|
57
|
|
|
58
|
const partitionIds = partitionResult.rows |
|
59
|
? partitionResult.rows.map((row: any) => row.id) |
|
60
|
: partitionResult.map((row: any) => row.id) |
|
61
|
|
|
62
|
// Check if all expected entities are in the partition table |
|
63
|
const allInPartition = entityIds.every((id) => partitionIds.includes(id)) |
|
64
|
|
|
65
|
if (allInPartition) { |
|
66
|
return |
|
67
|
} |
|
68
|
} catch (error) { |
|
69
|
console.error(error) |
|
70
|
} |
|
71
|
|
|
72
|
await new Promise((resolve) => setTimeout(resolve, pollInterval)) |
|
73
|
} |
|
74
|
|
|
75
|
console.error( |
|
76
|
`Entities [${entityIds.join( |
|
77
|
", " |
|
78
|
)}] of type '${normalizedEntityName}' were not fully replicated to partition table within ${timeout}ms` |
|
79
|
) |
|
80
|
} |