Skip to content

Commit

Permalink
Add size limit for debug folder (#128)
Browse files Browse the repository at this point in the history
* chore: started implementation of debug-max-size

* chore: added debug size limit check

* chore: add kysor support for debug-max-size
  • Loading branch information
troykessler committed Apr 3, 2024
1 parent 38ea7c7 commit 2d3e8dc
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
7 changes: 7 additions & 0 deletions common/protocol/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class Validator {
protected requestBackoff!: number;
protected cache!: string;
protected debug!: boolean;
protected debugMaxSize!: number;
protected metrics!: boolean;
protected metricsPort!: number;
protected home!: string;
Expand Down Expand Up @@ -254,6 +255,11 @@ export class Validator {
"jsonfile"
)
.option("--debug", "Run the validator node in debug mode")
.option(
"--debug-max-size <number>",
"The maximum size of the debug folder in bytes. Use zero to disable [default = 10737418240 (10GB)]",
"10737418240"
)
.option(
"--metrics",
"Start a prometheus metrics server on http://localhost:8080/metrics"
Expand Down Expand Up @@ -301,6 +307,7 @@ export class Validator {
this.requestBackoff = parseInt(options.requestBackoff);
this.cache = options.cache;
this.debug = options.debug;
this.debugMaxSize = parseInt(options.debugMaxSize);
this.metrics = options.metrics;
this.metricsPort = parseInt(options.metricsPort);
this.home = options.home;
Expand Down
14 changes: 13 additions & 1 deletion common/protocol/src/methods/helpers/archiveDebugBundle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync, mkdirSync, createWriteStream, readFileSync } from "fs";
import path from "path";
import { DataItem, standardizeError, Validator } from "../..";
import { DataItem, dirSize, standardizeError, Validator } from "../..";
import JSZip from "jszip";
import { VoteType } from "@kyvejs/types/client/kyve/bundles/v1beta1/tx";

Expand Down Expand Up @@ -31,6 +31,18 @@ export function archiveDebugBundle(
mkdirSync(path.join(this.home, `debug`), { recursive: true });
}

// if size of debug folder exceeds debug max limit we don't store an archive
if (this.debugMaxSize > 0) {
const debugDirSize = dirSize(path.join(this.home, `debug`));

if (debugDirSize >= this.debugMaxSize) {
this.logger.warn(
`Skipped saving debug information, debug dir exceeded max size of ${this.debugMaxSize} with ${debugDirSize} bytes`
);
return;
}
}

const zip = new JSZip();

// save metadata which includes vote reasons and args
Expand Down
29 changes: 29 additions & 0 deletions common/protocol/src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { BigNumber } from "bignumber.js";
import crypto from "crypto";
import { statSync, readdirSync } from "fs";
import { join } from "path";

import { DataItem } from "..";

Expand Down Expand Up @@ -172,3 +174,30 @@ export async function callWithBackoffStrategy<T>(
})().catch((err) => reject(err));
});
}

/**
* Get recursively the size of a dir including all children
*
* @method dirSize
* @param {onErrorRetryerType} dir path of the dir
* @return {number} returns the size in bytes
*/
export const dirSize = (dir: string): number => {
const files = readdirSync(dir, { withFileTypes: true });

const paths = files.map((file) => {
const path = join(dir, file.name);

if (file.isDirectory()) return dirSize(path);

if (file.isFile()) {
const { size } = statSync(path);

return size;
}

return 0;
});

return paths.flat(Infinity).reduce((i, size) => i + size, 0);
};
6 changes: 6 additions & 0 deletions tools/kysor/src/commands/valaccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ valaccounts
"Specify the port of the metrics server. Only considered if '--metrics' is set [default = 8080]",
"8080"
)
.option(
"--debug-max-size <number>",
"Specify the max size in bytes for the debug folder. [default = 10737418240 (10GB)]",
"10737418240"
)
.option("--recover", "Create a valaccount by importing an existing mnemonic")
.action(async (options) => {
try {
Expand Down Expand Up @@ -133,6 +138,7 @@ valaccounts
cache: options.cache,
metrics: options.metrics,
metricsPort: options.metricsPort,
debugMaxSize: options.debugMaxSize,
};

fs.writeFileSync(
Expand Down
5 changes: 5 additions & 0 deletions tools/kysor/src/kysor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,11 @@ export const run = async (options: any) => {
args.push(`${valaccount.metricsPort}`);
}

if (valaccount.debugMaxSize) {
args.push("--debug-max-size");
args.push(`${valaccount.debugMaxSize}`);
}

logger.info("Starting process ...");

console.log("\n");
Expand Down
1 change: 1 addition & 0 deletions tools/kysor/src/types/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export interface IValaccountConfig {
cache: string;
metrics: boolean;
metricsPort: string;
debugMaxSize: number;
}

0 comments on commit 2d3e8dc

Please sign in to comment.