Skip to content

Commit a648670

Browse files
authored
dedupe: don't add same WACZ file to per-crawl WACZ index (#1146)
- Only add WACZ file to `c:{crawlid}:wacz` index when setting a new WACZ filename, not on every crawl start up - Add it even if not using external redis, to keep logic simpler - Fixes #1145
1 parent c9e027e commit a648670

2 files changed

Lines changed: 37 additions & 26 deletions

File tree

src/crawler.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,10 +1929,7 @@ self.__bx_behaviors.selectMainBehavior();
19291929
this.params.generateWACZ &&
19301930
(this.storage || this.isExternalDedupeStore)
19311931
) {
1932-
const filename = await this.crawlState.setWACZFilename();
1933-
if (this.isExternalDedupeStore) {
1934-
await this.crawlState.addSourceWACZForDedupe(filename);
1935-
}
1932+
await this.crawlState.setWACZFilename();
19361933
}
19371934
if (this.isExternalDedupeStore) {
19381935
await this.crawlState.addCrawlForDedupe();

src/util/state.ts

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Redis, Result, Callback, type ChainableCommander } from "ioredis";
22
import { v4 as uuidv4 } from "uuid";
33

4-
import { logger } from "./logger.js";
4+
import { formatErr, logger } from "./logger.js";
55

66
import {
77
MAX_DEPTH,
@@ -299,11 +299,21 @@ export class RedisDedupeIndex {
299299
index,
300300
);
301301
if (!waczdata) {
302+
logger.warn(
303+
"Invalid WACZ index, WARC-Refers-To-Container will not be set",
304+
{ crawlId, index },
305+
"state",
306+
);
302307
return "";
303308
}
304309
const { filename } = JSON.parse(waczdata);
305310
return filename;
306-
} catch (_) {
311+
} catch (e) {
312+
logger.warn(
313+
"Error getting WACZ index, WARC-Refers-To-Container will not be set",
314+
{ crawlId, index, ...formatErr(e) },
315+
"state",
316+
);
307317
return "";
308318
}
309319
}
@@ -854,8 +864,6 @@ export class RedisCrawlState extends RedisDedupeIndex {
854864

855865
sitemapDoneKey: string;
856866

857-
waczFilename: string | null = null;
858-
859867
includedCrawls: Set<string> = new Set<string>();
860868

861869
rateLimitTTL: number;
@@ -1265,44 +1273,50 @@ return inx;
12651273
}
12661274

12671275
async setWACZFilename(): Promise<string> {
1268-
const filename = process.env.STORE_FILENAME || "@ts-@id.wacz";
1269-
this.waczFilename = interpolateFilename(filename, this.crawlId);
1270-
if (
1271-
!(await this.redis.hsetnx(
1272-
`${this.crawlId}:nextWacz`,
1273-
this.uid,
1274-
this.waczFilename,
1275-
))
1276-
) {
1277-
this.waczFilename = await this.redis.hget(
1278-
`${this.crawlId}:nextWacz`,
1279-
this.uid,
1276+
const filenameTemplate = process.env.STORE_FILENAME || "@ts-@id.wacz";
1277+
const crawlId = this.crawlId;
1278+
const filename = interpolateFilename(filenameTemplate, crawlId);
1279+
if (!(await this.redis.hsetnx(`${crawlId}:nextWacz`, this.uid, filename))) {
1280+
this.dedupeCurrFilename =
1281+
(await this.redis.hget(`${crawlId}:nextWacz`, this.uid)) || "";
1282+
1283+
this.dedupeKeyIndex = Number(
1284+
(await this.redis.hget(`${crawlId}:nextWaczIndex`, this.uid)) || 0,
12801285
);
1286+
12811287
logger.debug(
12821288
"Keeping WACZ Filename",
1283-
{ filename: this.waczFilename },
1289+
{ filename, index: this.dedupeKeyIndex },
12841290
"state",
12851291
);
12861292
} else {
1293+
await this.addSourceWACZForDedupe(filename);
1294+
1295+
await this.redis.hset(
1296+
`${crawlId}:nextWaczIndex`,
1297+
this.uid,
1298+
this.dedupeKeyIndex,
1299+
);
1300+
12871301
logger.debug(
12881302
"Using New WACZ Filename",
1289-
{ filename: this.waczFilename },
1303+
{ filename, index: this.dedupeKeyIndex },
12901304
"state",
12911305
);
12921306
}
1293-
return this.waczFilename!;
1307+
return filename;
12941308
}
12951309

12961310
async getWACZFilename(): Promise<string> {
1297-
if (!this.waczFilename) {
1311+
if (!this.dedupeCurrFilename) {
12981312
return await this.setWACZFilename();
12991313
}
1300-
return this.waczFilename;
1314+
return this.dedupeCurrFilename;
13011315
}
13021316

13031317
async clearWACZFilename(): Promise<void> {
13041318
await this.redis.hdel(`${this.crawlId}:nextWacz`, this.uid);
1305-
this.waczFilename = null;
1319+
this.dedupeCurrFilename = "";
13061320

13071321
await this.redis.del(`${this.uid}:duperef`);
13081322
}

0 commit comments

Comments
 (0)