Skip to content

Commit fc141c8

Browse files
authored
bucket notifications - add notif storage test to health (#8850)
Signed-off-by: Amit Prinz Setter <alphaprinz@gmail.com>
1 parent d9b73f6 commit fc141c8

File tree

7 files changed

+61
-9
lines changed

7 files changed

+61
-9
lines changed

config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ config.NOTIFICATION_LOG_NS = 'notification_logging';
723723
config.NOTIFICATION_LOG_DIR = process.env.NOTIFICATION_LOG_DIR;
724724
config.NOTIFICATION_BATCH = process.env.BATCH || 10;
725725
config.NOTIFICATION_REQ_PER_SPACE_CHECK = process.env.NOTIFICATION_REQ_PER_SPACE_CHECK || 0;
726-
config.NOTIFICATION_SPACE_CHECK_THRESHOLD = parseInt(process.env.NOTIFICATION_SPACE_CHECK_THRESHOLD, 10) || 0.1;
726+
config.NOTIFICATION_SPACE_CHECK_THRESHOLD = parseFloat(process.env.NOTIFICATION_SPACE_CHECK_THRESHOLD) || 0.2;
727727

728728
///////////////////////////
729729
// KEY ROTATOR //

docs/NooBaaNonContainerized/Health.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ The `health` command is used to analyze NooBaa health with customizable options.
5555

5656
```sh
5757
noobaa-cli diagnose health [--deployment_type][--https_port]
58-
[--all_account_details][--all_bucket_details][--config_root][--debug]
58+
[--all_account_details][--all_bucket_details][--all_connection_details][--notif_storage_threshold][--config_root][--debug]
5959
```
6060
### Flags -
6161

@@ -84,6 +84,11 @@ noobaa-cli diagnose health [--deployment_type][--https_port]
8484
- Default: false
8585
- Description: Indicates if health output should contain connection test result.
8686

87+
- `notif_storage_threshold`
88+
- Type: Boolean
89+
- Default: false
90+
- Description: Whether health ouput should check if notification storage FS is below threshold.
91+
8792
- `config_root`
8893
- Type: String
8994
- Description: Indicates the config directory (default config.NSFS_NC_DEFAULT_CONF_DIR). config_root flag should be used only for dev/tests envs.
@@ -269,6 +274,11 @@ Output:
269274
}
270275
]
271276
},
277+
"notif_storage_threshold_details": {
278+
"threshold": 0.2,
279+
"ratio": 0.9,
280+
"result": "above threshold"
281+
}
272282
"config_directory": {
273283
"phase": "CONFIG_DIR_UNLOCKED",
274284
"config_dir_version": "1.0.0",

src/endpoint/s3/s3_bucket_logging.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const http_utils = require('../../util/http_utils');
66
const dgram = require('node:dgram');
77
const { Buffer } = require('node:buffer');
88
const config = require('../../../config');
9-
const {compose_notification_req, check_notif_relevant, check_free_space} = require('../../util/notifications_util');
9+
const {compose_notification_req, check_notif_relevant, check_free_space_if_needed} = require('../../util/notifications_util');
1010

1111
async function send_bucket_op_logs(req, res) {
1212
if (req.params && req.params.bucket &&
@@ -44,7 +44,7 @@ async function send_bucket_op_logs(req, res) {
4444
buffer: JSON.stringify(notif)
4545
});
4646

47-
check_free_space(req);
47+
check_free_space_if_needed(req);
4848
}
4949
}
5050
}

src/manage_nsfs/health.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class NSFSHealth {
103103
this.all_account_details = options.all_account_details;
104104
this.all_bucket_details = options.all_bucket_details;
105105
this.all_connection_details = options.all_connection_details;
106+
this.notif_storage_threshold = options.notif_storage_threshold;
106107
this.config_fs = options.config_fs;
107108
}
108109

@@ -131,13 +132,15 @@ class NSFSHealth {
131132
let bucket_details;
132133
let account_details;
133134
let connection_details;
135+
let notif_storage_threshold_details;
134136
const endpoint_response_code = (endpoint_state && endpoint_state.response?.response_code) || 'UNKNOWN_ERROR';
135137
const health_check_params = { service_status, pid, endpoint_response_code, config_directory_status };
136138
const service_health = this._calc_health_status(health_check_params);
137139
const error_code = this.get_error_code(health_check_params);
138140
if (this.all_bucket_details) bucket_details = await this.get_bucket_status();
139141
if (this.all_account_details) account_details = await this.get_account_status();
140142
if (this.all_connection_details) connection_details = await this.get_connection_status();
143+
if (this.notif_storage_threshold) notif_storage_threshold_details = this.get_notif_storage_threshold_status();
141144
const health = {
142145
service_name: NOOBAA_SERVICE_NAME,
143146
status: service_health,
@@ -160,7 +163,8 @@ class NSFSHealth {
160163
valid_buckets: bucket_details === undefined ? undefined : bucket_details.valid_storages,
161164
error_type: health_errors_tyes.PERSISTENT,
162165
},
163-
connections_status: connection_details
166+
connections_status: connection_details,
167+
notif_storage_threshold_details
164168
}
165169
};
166170
if (!this.all_account_details) delete health.checks.accounts_status;
@@ -428,6 +432,20 @@ class NSFSHealth {
428432
};
429433
}
430434

435+
get_notif_storage_threshold_status() {
436+
const check = notifications_util.check_free_space();
437+
const res = {
438+
threshold: config.NOTIFICATION_SPACE_CHECK_THRESHOLD,
439+
ratio: check.ratio
440+
};
441+
if (check.below) {
442+
res.result = 'below threshold';
443+
} else {
444+
res.result = 'above threshold';
445+
}
446+
return res;
447+
}
448+
431449
/**
432450
* get_config_file_data_or_error_object return an object containing config_data or err_obj if error occurred
433451
* @param {string} type
@@ -594,9 +612,11 @@ async function get_health_status(argv, config_fs) {
594612
const all_account_details = get_boolean_or_string_value(argv.all_account_details);
595613
const all_bucket_details = get_boolean_or_string_value(argv.all_bucket_details);
596614
const all_connection_details = get_boolean_or_string_value(argv.all_connection_details);
615+
const notif_storage_threshold = get_boolean_or_string_value(argv.notif_storage_threshold);
597616

598617
if (deployment_type === 'nc') {
599-
const health = new NSFSHealth({ https_port, all_account_details, all_bucket_details, all_connection_details, config_fs });
618+
const health = new NSFSHealth({ https_port,
619+
all_account_details, all_bucket_details, all_connection_details, notif_storage_threshold, config_fs });
600620
const health_status = await health.nc_nsfs_health();
601621
write_stdout_response(ManageCLIResponse.HealthStatus, health_status);
602622
} else {

src/manage_nsfs/manage_nsfs_constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const VALID_OPTIONS_GLACIER = {
7575
};
7676

7777
const VALID_OPTIONS_DIAGNOSE = {
78-
'health': new Set([ 'https_port', 'deployment_type', 'all_account_details', 'all_bucket_details', 'all_connection_details', ...CLI_MUTUAL_OPTIONS]),
78+
'health': new Set([ 'https_port', 'deployment_type', 'all_account_details', 'all_bucket_details', 'all_connection_details', 'notif_storage_threshold', ...CLI_MUTUAL_OPTIONS]),
7979
'gather-logs': new Set([ CONFIG_ROOT_FLAG]),
8080
'metrics': new Set([CONFIG_ROOT_FLAG])
8181
};
@@ -147,6 +147,7 @@ const OPTION_TYPE = {
147147
all_account_details: 'boolean',
148148
all_bucket_details: 'boolean',
149149
all_connection_details: 'boolean',
150+
notif_storage_threshold: 'boolean',
150151
https_port: 'number',
151152
debug: 'number',
152153
// upgrade options

src/test/unit_tests/test_nc_health.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,16 @@ mocha.describe('nsfs nc health', function() {
233233
assert.strictEqual(health_status.checks.connections_status.invalid_storages[0].name, connection_from_file.name);
234234
});
235235

236+
mocha.it('health should test notification storage', async function() {
237+
Health.notif_storage_threshold = true;
238+
config.NOTIFICATION_LOG_DIR = TMP_PATH;
239+
const health_status = await Health.nc_nsfs_health();
240+
Health.notif_storage_limit = false;
241+
242+
assert.strictEqual(health_status.checks.notif_storage_threshold_details.result, 'above threshold');
243+
assert.strictEqual(health_status.checks.notif_storage_threshold_details.threshold, config.NOTIFICATION_SPACE_CHECK_THRESHOLD);
244+
});
245+
236246
mocha.it('NooBaa service is inactive', async function() {
237247
set_mock_functions(Health, {
238248
get_service_state: [{ service_status: 'inactive', pid: 0 }],

src/util/notifications_util.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ function get_notification_logger(locking, namespace, poll_interval) {
564564
}
565565

566566
//If space check is configures, create an event in case free space is below threshold.
567-
function check_free_space(req) {
567+
function check_free_space_if_needed(req) {
568568
if (!req.object_sdk.nsfs_config_root || !config.NOTIFICATION_REQ_PER_SPACE_CHECK) {
569569
//free space check is disabled. nothing to do.
570570
return;
@@ -577,13 +577,23 @@ function check_free_space(req) {
577577
req.notification_logger.writes_counter = 0;
578578
const fs_stat = fs.statfsSync(config.NOTIFICATION_LOG_DIR);
579579
//is the ratio of available blocks less than the configures threshold?
580-
if (fs_stat.bavail / fs_stat.blocks < config.NOTIFICATION_SPACE_CHECK_THRESHOLD) {
580+
if (check_free_space().below) {
581581
//yes. raise an event.
582582
new NoobaaEvent(NoobaaEvent.NOTIFICATION_LOW_SPACE).create_event(null, {fs_stat});
583583
}
584584
}
585585
}
586586

587+
function check_free_space() {
588+
const fs_stat = fs.statfsSync(config.NOTIFICATION_LOG_DIR);
589+
const ratio = fs_stat.bavail / fs_stat.blocks;
590+
//is the ratio of available blocks less than the configures threshold?
591+
return {
592+
below: ratio < config.NOTIFICATION_SPACE_CHECK_THRESHOLD,
593+
ratio
594+
};
595+
}
596+
587597
/**
588598
* add_connect_file Creates a new connection file from the given content.
589599
* If content has an auth field in it's request_options_object, it is encrypted.
@@ -649,4 +659,5 @@ exports.get_notification_logger = get_notification_logger;
649659
exports.add_connect_file = add_connect_file;
650660
exports.update_connect_file = update_connect_file;
651661
exports.check_free_space = check_free_space;
662+
exports.check_free_space_if_needed = check_free_space_if_needed;
652663
exports.OP_TO_EVENT = OP_TO_EVENT;

0 commit comments

Comments
 (0)