Skip to content

Commit 387cf82

Browse files
authored
fix(state): bound record export reconstruction (#1253)
* fix(state): bound record export reconstruction * test(state): cover record export metadata branches
1 parent 5cb6777 commit 387cf82

4 files changed

Lines changed: 584 additions & 9 deletions

File tree

‎dashboard/exact-review-direct-publication.ts‎

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -810,18 +810,40 @@ export class ExactReviewDirectPublicationStore {
810810
cursor: number;
811811
limit: number;
812812
maxBytes: number;
813+
maxSourceBytes: number;
814+
maxRecords: number;
813815
}): { records: RecordExportEntry[]; nextCursor: number | null; watermark: number } {
814816
const placeholders = options.sections.map(() => "?").join(", ");
815817
const rows = Array.from(
816818
this.storage.sql.exec(
817-
`SELECT repo_slug, section, record_id, digest, deleted, revision, store_revision,
818-
source, updated_at
819-
FROM ${EXACT_REVIEW_RECORD_EXPORT_INDEX_TABLE}
820-
WHERE repo_slug = ?
821-
AND section IN (${placeholders})
822-
AND store_revision > ?
823-
AND store_revision > ?
824-
ORDER BY store_revision
819+
`SELECT export.repo_slug, export.section, export.record_id, export.digest,
820+
export.deleted, export.revision, export.store_revision, export.source,
821+
export.updated_at,
822+
CASE
823+
WHEN export.deleted = 1 THEN '0'
824+
WHEN export.source = 'canonical' AND export.section <> 'commits'
825+
THEN CAST(canonical.byte_length AS TEXT)
826+
ELSE CAST(backfill.byte_length AS TEXT)
827+
END AS source_byte_length
828+
FROM ${EXACT_REVIEW_RECORD_EXPORT_INDEX_TABLE} export
829+
LEFT JOIN ${EXACT_REVIEW_CANONICAL_RECORD_TABLE} canonical
830+
ON export.deleted = 0
831+
AND export.source = 'canonical'
832+
AND export.section <> 'commits'
833+
AND canonical.repo_slug = export.repo_slug
834+
AND canonical.section = export.section
835+
AND canonical.item_id = CAST(export.record_id AS INTEGER)
836+
LEFT JOIN ${EXACT_REVIEW_RECORD_BACKFILL_TABLE} backfill
837+
ON export.deleted = 0
838+
AND (export.source = 'backfill' OR export.section = 'commits')
839+
AND backfill.repo_slug = export.repo_slug
840+
AND backfill.section = export.section
841+
AND backfill.record_id = export.record_id
842+
WHERE export.repo_slug = ?
843+
AND export.section IN (${placeholders})
844+
AND export.store_revision > ?
845+
AND export.store_revision > ?
846+
ORDER BY export.store_revision
825847
LIMIT ?`,
826848
options.repoSlug,
827849
...options.sections,
@@ -830,9 +852,20 @@ export class ExactReviewDirectPublicationStore {
830852
options.limit,
831853
),
832854
);
855+
const selectedRows: Record<string, unknown>[] = [];
856+
let sourceBytes = 0;
857+
for (const row of rows) {
858+
if (selectedRows.length >= options.maxRecords) break;
859+
const byteLength = recordExportSourceByteLength(row);
860+
if (selectedRows.length > 0 && byteLength > options.maxSourceBytes - sourceBytes) {
861+
break;
862+
}
863+
selectedRows.push(row);
864+
sourceBytes += byteLength;
865+
}
833866
const records: RecordExportEntry[] = [];
834867
let responseBytes = 0;
835-
for (const row of rows) {
868+
for (const row of selectedRows) {
836869
const entry = this.recordExportEntrySync(row);
837870
const entryBytes = new TextEncoder().encode(JSON.stringify(entry)).byteLength;
838871
if (records.length && responseBytes + entryBytes > options.maxBytes) break;
@@ -1304,6 +1337,22 @@ export class ExactReviewDirectPublicationStore {
13041337
}
13051338
}
13061339

1340+
function recordExportSourceByteLength(row: Record<string, unknown>) {
1341+
const value = row.source_byte_length;
1342+
const byteLength =
1343+
typeof value === "string" && /^(0|[1-9]\d*)$/.test(value) ? Number(value) : NaN;
1344+
if (
1345+
!Number.isSafeInteger(byteLength) ||
1346+
byteLength < 0 ||
1347+
(Number(row.deleted) === 1 && byteLength !== 0)
1348+
) {
1349+
throw new Error(
1350+
`invalid record export byte metadata: ${String(row.repo_slug)}/${String(row.section)}/${String(row.record_id)}`,
1351+
);
1352+
}
1353+
return byteLength;
1354+
}
1355+
13071356
function canonicalTupleOperations(
13081357
plan: CanonicalDirectPublicationPlan,
13091358
): CanonicalDirectPublicationOperation[] {

‎dashboard/exact-review-queue.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
} from "./exact-review-publication-retry.ts";
2525
import {
2626
CanonicalRecordTupleConflictError,
27+
EXACT_REVIEW_DIRECT_PUBLICATION_MAX_FILE_BYTES,
2728
EXACT_REVIEW_DIRECT_PUBLICATION_MAX_POST_BYTES,
2829
REVIEW_COVERAGE_INVENTORY_KEY,
2930
ExactReviewDirectPublicationStore,
@@ -698,6 +699,8 @@ const DEFAULT_STATE_WRITER_COORDINATOR_MAX_LEASE_AGE_MS = 30 * 60_000;
698699
const RECORD_EXPORT_DEFAULT_LIMIT = 100;
699700
const RECORD_EXPORT_MAX_LIMIT = 200;
700701
const RECORD_EXPORT_MAX_RESPONSE_BYTES = 4 * 1024 * 1024;
702+
const RECORD_EXPORT_MAX_SOURCE_BYTES = EXACT_REVIEW_DIRECT_PUBLICATION_MAX_FILE_BYTES;
703+
const RECORD_EXPORT_MAX_RECONSTRUCTION_RECORDS = 50;
701704
const RECORD_INGEST_MAX_RECORDS = 100;
702705
const RECORD_INGEST_MAX_REQUEST_BYTES = 4 * 1024 * 1024;
703706
const RECORD_INGEST_MAX_FILE_BYTES = 2 * 1024 * 1024;
@@ -2692,6 +2695,8 @@ export class ExactReviewQueue {
26922695
cursor,
26932696
limit,
26942697
maxBytes: RECORD_EXPORT_MAX_RESPONSE_BYTES,
2698+
maxSourceBytes: RECORD_EXPORT_MAX_SOURCE_BYTES,
2699+
maxRecords: RECORD_EXPORT_MAX_RECONSTRUCTION_RECORDS,
26952700
});
26962701
return json({
26972702
repoSlug,

‎test/dashboard-worker-harness.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ class MemorySqlStorage {
9494
private readonly database = new DatabaseSync(":memory:");
9595
private failure: { pattern: RegExp; error: Error } | undefined;
9696
private bindingLimit = Number.POSITIVE_INFINITY;
97+
private queryHistory: Array<{ query: string; bindings: unknown[] }> | null = null;
9798

9899
exec(query: string, ...bindings: unknown[]) {
100+
this.queryHistory?.push({ query, bindings });
99101
if (bindings.length > this.bindingLimit) {
100102
throw new Error(`test SQL binding limit exceeded: ${bindings.length}`);
101103
}
@@ -136,6 +138,17 @@ class MemorySqlStorage {
136138
this.bindingLimit = limit;
137139
}
138140

141+
queriesMatching(pattern: RegExp) {
142+
const flags = pattern.flags.replaceAll("g", "");
143+
return (this.queryHistory ?? []).filter(({ query }) =>
144+
new RegExp(pattern.source, flags).test(query),
145+
);
146+
}
147+
148+
resetQueryHistory() {
149+
this.queryHistory = [];
150+
}
151+
139152
hasNormalizedQueue() {
140153
const table = this.database
141154
.prepare(

0 commit comments

Comments
 (0)