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

Add size limit for debug folder #128

Merged
merged 3 commits into from
Apr 3, 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
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;
}
Loading