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

Refactor Cache Initialization and Logging in ValidatorsProcessor #67

Merged
merged 1 commit into from
Nov 29, 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
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Explorer API - Debug",
"type": "node-terminal",
"request": "launch",
"skipFiles": ["<node_internals>/**"],
"command": "npm run start:dev",
"cwd": "${workspaceFolder}/api",
"sourceMaps": true
}
]
}
50 changes: 31 additions & 19 deletions api/src/ol/validators/validators.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,38 @@ export class ValidatorsProcessor extends WorkerHost {
}

public async onModuleInit() {
// Note: this order is important
await this.updateValidatorsHandlersCache();
await this.updateVfnStatusCache();
await this.updateValidatorsCache();
await this.updateValidatorsVouchesCache();

// Clear all jobs in the queue
this.validatorsQueue.drain(true);

await this.validatorsQueue.add('updateValidatorsHandlersCache', undefined, {
repeat: {
every: 12 * 60 * 60 * 1000, // 12 hours
},
});
this.updateValidatorsHandlersCache();

await this.validatorsQueue.add('updateValidatorsCache', undefined, {
repeat: {
every: 30 * 1000, // 30 seconds
},
});
this.updateValidatorsCache();

await this.validatorsQueue.add('updateVfnStatusCache', undefined, {
repeat: {
every: 5 * 60 * 1000, // 5 minutes
},
});
this.updateVfnStatusCache();

await this.validatorsQueue.add('updateValidatorsVouchesCache', undefined, {
repeat: {
every: 60 * 1000, // 60 seconds
},
});
this.updateValidatorsVouchesCache();

this.logger.log('ValidatorsProcessor initialized');
}
Expand All @@ -75,43 +80,50 @@ export class ValidatorsProcessor extends WorkerHost {
}
}

private async updateValidatorsHandlersCache() {
const start = Date.now();
try {
const validatorsHandlers = await this.validatorsService.loadValidatorHandles();
const obj = Object.fromEntries(validatorsHandlers);
await redisClient.set(VALIDATORS_HANDLERS_CACHE_KEY, JSON.stringify(obj));
const duration = Date.now() - start;
this.logger.log(`Validators handlers cache updated in ${duration}ms`);
} catch (error) {
this.logger.error('Error updating validators handlers cache', error);
}
}

private async updateVfnStatusCache() {
const start = Date.now();
try {
const vfnStatus = await this.validatorsService.queryValidatorsVfnStatus();
await redisClient.set(VALIDATORS_VFN_STATUS_CACHE_KEY, JSON.stringify(vfnStatus));
this.logger.log('VFN status cache updated');
const duration = Date.now() - start;
this.logger.log(`VFN status cache updated in ${duration}ms`);
} catch (error) {
this.logger.error('Error updating VFN status cache', error);
}
}

private async updateValidatorsCache() {
const start = Date.now();
try {
const validators = await this.validatorsService.queryValidators();
await redisClient.set(VALIDATORS_CACHE_KEY, JSON.stringify(validators));
this.logger.log('Validators cache updated');
const duration = Date.now() - start;
this.logger.log(`Validators cache updated in ${duration}ms`);
} catch (error) {
this.logger.error('Error updating validators cache', error);
}
}

private async updateValidatorsHandlersCache() {
try {
const validatorsHandlers = await this.validatorsService.loadValidatorHandles();
await redisClient.set(
VALIDATORS_HANDLERS_CACHE_KEY,
JSON.stringify(JSON.stringify(Array.from(validatorsHandlers.entries()))),
);
this.logger.log('Validators handlers cache updated');
} catch (error) {
this.logger.error('Error updating validators handlers cache', error);
}
}

private async updateValidatorsVouchesCache() {
const start = Date.now();
try {
const validatorsVouches = await this.validatorsService.queryValidatorsVouches();
await redisClient.set(VALIDATORS_VOUCHES_CACHE_KEY, JSON.stringify(validatorsVouches));
const duration = Date.now() - start;
this.logger.log(`Validators vouches cache updated in ${duration}ms`);
} catch (error) {
this.logger.error('Error updating validators vouches cache', error);
}
Expand Down
22 changes: 6 additions & 16 deletions api/src/ol/validators/validators.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,9 @@ export class ValidatorsService {
public async getValidatorsHandlers(): Promise<Map<string, string>> {
if (this.cacheEnabled) {
const cacheHandlersString = await this.getFromCache<string>(VALIDATORS_HANDLERS_CACHE_KEY);
if (cacheHandlersString) {
try {
const map = new Map<string, string>();
const entries: [string, string][] = JSON.parse(cacheHandlersString);
entries.forEach((entry) => {
map.set(entry[0], entry[1]);
});
return map;
} catch (parseError) {
this.logger.error('Error parsing validators handlers cache', parseError);
}
}
return cacheHandlersString
? new Map<string, string>(Object.entries(cacheHandlersString))
: new Map();
}

let handlers = new Map<string, string>();
Expand All @@ -97,10 +88,9 @@ export class ValidatorsService {
} catch (error) {
this.logger.error('Error loading validators handlers', error);
} finally {
await this.setCache(
VALIDATORS_HANDLERS_CACHE_KEY,
JSON.stringify(Array.from(handlers.entries())),
);
const obj = Object.fromEntries(handlers);
await redisClient.set(VALIDATORS_HANDLERS_CACHE_KEY, JSON.stringify(obj));
this.logger.log('Validators handlers cache updated');
}

return handlers;
Expand Down