Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache for sqlite method calls to prevent duplicate writes #207

Merged
merged 4 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 44 additions & 7 deletions src/database/standalone-sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import * as R from 'ramda';
import sql from 'sql-bricks';
import * as winston from 'winston';
import CircuitBreaker from 'opossum';
import NodeCache from 'node-cache';

// TODO enable eslint
/* eslint-disable */
Expand Down Expand Up @@ -405,6 +406,8 @@ export class StandaloneSqliteDatabaseWorker {
private bundleFormatIds: { [filter: string]: number } = {};
private filterIds: { [filter: string]: number } = {};

private insertDataHashCache: NodeCache;

// Transactions
resetBundlesToHeightFn: Sqlite.Transaction;
resetCoreToHeightFn: Sqlite.Transaction;
Expand Down Expand Up @@ -761,6 +764,12 @@ export class StandaloneSqliteDatabaseWorker {
});
},
);

this.insertDataHashCache = new NodeCache({
stdTTL: 60 * 7, // 7 minutes
checkperiod: 60, // 1 minute
useClones: false,
});
}

getMaxHeight() {
Expand Down Expand Up @@ -1132,13 +1141,7 @@ export class StandaloneSqliteDatabaseWorker {
cachedAt?: number;
}) {
const hashBuffer = fromB64Url(hash);
this.stmts.data.insertDataHash.run({
hash: hashBuffer,
data_size: dataSize,
original_source_content_type: contentType,
indexed_at: currentUnixTimestamp(),
cached_at: cachedAt,
});

this.stmts.data.insertDataId.run({
id: fromB64Url(id),
contiguous_data_hash: hashBuffer,
Expand All @@ -1151,6 +1154,19 @@ export class StandaloneSqliteDatabaseWorker {
indexed_at: currentUnixTimestamp(),
});
}

if (this.insertDataHashCache.get(hash)) {
djwhitt marked this conversation as resolved.
Show resolved Hide resolved
return;
}
this.insertDataHashCache.set(hash, true);

this.stmts.data.insertDataHash.run({
hash: hashBuffer,
data_size: dataSize,
original_source_content_type: contentType,
indexed_at: currentUnixTimestamp(),
cached_at: cachedAt,
});
}

getGqlNewTransactionTags(txId: Buffer) {
Expand Down Expand Up @@ -2432,6 +2448,8 @@ export class StandaloneSqliteDatabase
Awaited<ReturnType<StandaloneSqliteDatabase['getTransactionAttributes']>>
>;

private saveDataContentAttributesCache: NodeCache;

constructor({
log,
coreDbPath,
Expand Down Expand Up @@ -2505,6 +2523,16 @@ export class StandaloneSqliteDatabase
this.getTransactionAttributesCircuitBreaker,
]);

//
// Initialize method caches
//

this.saveDataContentAttributesCache = new NodeCache({
stdTTL: 60 * 7, // 7 minutes
checkperiod: 60, // 1 minute
useClones: false,
});

//
// Initialize workers
//
Expand Down Expand Up @@ -2780,6 +2808,15 @@ export class StandaloneSqliteDatabase
dataSize: number;
contentType?: string;
}) {
if (this.saveDataContentAttributesCache.get(id)) {
metrics.saveMethodsDuplicateCounter.inc({
method: 'saveDataContentAttributes',
});
return Promise.resolve();
}

this.saveDataContentAttributesCache.set(id, true);

return this.queueWrite('data', 'saveDataContentAttributes', [
{
id,
Expand Down
6 changes: 6 additions & 0 deletions src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ export const methodDurationSummary = new promClient.Summary({
labelNames: ['worker', 'role', 'method'],
});

export const saveMethodsDuplicateCounter = new promClient.Counter({
name: 'save_methods_duplicate_total',
help: 'Count of duplicate calls to save methods',
labelNames: ['method'],
});

//
// Block importer metrics
//
Expand Down