Skip to content

Commit 3609d94

Browse files
OTLegendOTLegend
authored andcommitted
[feature] Add metrics for in memory and disk cache size before cleanup
1 parent c1a8210 commit 3609d94

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/commands/cleaners/operation-id-cleaner-command.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ class OperationIdCleanerCommand extends Command {
2424
* @param command
2525
*/
2626
async execute() {
27+
const memoryBytes = this.operationIdService.getOperationIdMemoryCacheSizeBytes();
28+
const fileBytes = await this.operationIdService.getOperationIdFileCacheSizeBytes();
29+
const bytesInMegabyte = 1024 * 1024;
30+
this.logger.debug(
31+
`Operation cache footprint before cleanup: memory=${(
32+
memoryBytes / bytesInMegabyte
33+
).toFixed(2)}MB, files=${(fileBytes / bytesInMegabyte).toFixed(2)}MB`,
34+
);
35+
2736
this.logger.debug('Starting command for removal of expired cache files');
2837
const timeToBeDeleted = Date.now() - OPERATION_ID_COMMAND_CLEANUP_TIME_MILLS;
2938
await this.repositoryModuleManager.removeOperationIdRecord(timeToBeDeleted, [

src/service/operation-id-service.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,30 @@ class OperationIdService {
150150
delete this.memoryCachedHandlersData[operationId];
151151
}
152152

153+
getOperationIdMemoryCacheSizeBytes() {
154+
let total = 0;
155+
for (const operationId in this.memoryCachedHandlersData) {
156+
const { data } = this.memoryCachedHandlersData[operationId];
157+
total += Buffer.from(JSON.stringify(data)).byteLength;
158+
}
159+
return total;
160+
}
161+
162+
async getOperationIdFileCacheSizeBytes() {
163+
const cacheFolderPath = this.fileService.getOperationIdCachePath();
164+
const cacheFolderExists = await this.fileService.pathExists(cacheFolderPath);
165+
if (!cacheFolderExists) return 0;
166+
167+
const fileList = await this.fileService.readDirectory(cacheFolderPath);
168+
let total = 0;
169+
for (const fileName of fileList) {
170+
// eslint-disable-next-line no-await-in-loop
171+
const stats = await this.fileService.stat(path.join(cacheFolderPath, fileName));
172+
total += stats.size;
173+
}
174+
return total;
175+
}
176+
153177
async removeExpiredOperationIdMemoryCache(expiredTimeout) {
154178
const now = Date.now();
155179
let deleted = 0;

0 commit comments

Comments
 (0)