@@ -43,6 +43,7 @@ const { SemaphoreMonitor } = require('../server/bg_services/semaphore_monitor');
43
43
const prom_reporting = require ( '../server/analytic_services/prometheus_reporting' ) ;
44
44
const { PersistentLogger } = require ( '../util/persistent_logger' ) ;
45
45
const { get_notification_logger } = require ( '../util/notifications_util' ) ;
46
+ const { is_nc_environment } = require ( '../nc/nc_utils' ) ;
46
47
const NoobaaEvent = require ( '../manage_nsfs/manage_nsfs_events_utils' ) . NoobaaEvent ;
47
48
const cluster = /** @type {import('node:cluster').Cluster } */ (
48
49
/** @type {unknown } */ ( require ( 'node:cluster' ) )
@@ -57,7 +58,8 @@ const SERVICES_TYPES_ENUM = Object.freeze({
57
58
S3 : 'S3' ,
58
59
STS : 'STS' ,
59
60
IAM : 'IAM' ,
60
- METRICS : 'METRICS'
61
+ METRICS : 'METRICS' ,
62
+ FORK_HEALTH : 'FORK_HEALTH' ,
61
63
} ) ;
62
64
63
65
const new_umask = process . env . NOOBAA_ENDPOINT_UMASK || 0o000 ;
@@ -117,11 +119,11 @@ async function main(options = {}) {
117
119
const https_metrics_port = options . https_metrics_port || config . EP_METRICS_SERVER_SSL_PORT ;
118
120
/**
119
121
* Please notice that we can run the main in 2 states:
120
- * 1. Only the primary process runs the main (fork is 0 or undefined) - everything that
122
+ * 1. Only the primary process runs the main (fork is 0 or undefined) - everything that
121
123
* is implemented here would be run by this process.
122
- * 2. A primary process with multiple forks (IMPORTANT) - if there is implementation that
123
- * in only relevant to the primary process it should be implemented in
124
- * fork_utils.start_workers because the primary process returns after start_workers
124
+ * 2. A primary process with multiple forks (IMPORTANT) - if there is implementation that
125
+ * in only relevant to the primary process it should be implemented in
126
+ * fork_utils.start_workers because the primary process returns after start_workers
125
127
* and the forks will continue executing the code lines in this function
126
128
* */
127
129
const is_workers_started_from_primary = await fork_utils . start_workers ( http_metrics_port , https_metrics_port ,
@@ -202,14 +204,29 @@ async function main(options = {}) {
202
204
{ ...options , https_port : https_port_s3 , http_port : http_port_s3 , virtual_hosts, bucket_logger, notification_logger } ) ;
203
205
await start_endpoint_server_and_cert ( SERVICES_TYPES_ENUM . STS , init_request_sdk , { https_port : https_port_sts , virtual_hosts } ) ;
204
206
await start_endpoint_server_and_cert ( SERVICES_TYPES_ENUM . IAM , init_request_sdk , { https_port : https_port_iam } ) ;
205
-
207
+ const is_nc = is_nc_environment ( ) ;
208
+ // fork health server currently runs only on non containerized enviorment
209
+ if ( is_nc ) {
210
+ // current process is the primary and only fork. start the fork server directly with the base port
211
+ if ( cluster . isPrimary ) {
212
+ await fork_message_request_handler ( {
213
+ nsfs_config_root : options . nsfs_config_root ,
214
+ health_port : config . ENDPOINT_FORK_PORT_BASE
215
+ } ) ;
216
+ // current process is a worker so we listen to get the port from the primary process.
217
+ } else {
218
+ process . on ( 'message' , fork_message_request_handler ) ;
219
+ //send a message to the primary process that we are ready to receive messages
220
+ process . send ( { ready_to_start_fork_server : true } ) ;
221
+ }
222
+ }
206
223
207
224
// START METRICS SERVER
208
225
if ( ( http_metrics_port > 0 || https_metrics_port > 0 ) && cluster . isPrimary ) {
209
226
await prom_reporting . start_server ( http_metrics_port , https_metrics_port , false , options . nsfs_config_root ) ;
210
227
}
211
228
212
- // TODO: currently NC NSFS deployments don't have internal_rpc_client nor db,
229
+ // TODO: currently NC NSFS deployments don't have internal_rpc_client nor db,
213
230
// there for namespace monitor won't be registered
214
231
if ( internal_rpc_client && config . NAMESPACE_MONITOR_ENABLED ) {
215
232
endpoint_stats_collector . instance ( ) . set_rpc_client ( internal_rpc_client ) ;
@@ -289,8 +306,6 @@ function create_endpoint_handler(server_type, init_request_sdk, { virtual_hosts,
289
306
return blob_rest_handler ( req , res ) ;
290
307
} else if ( req . url . startsWith ( '/total_fork_count' ) ) {
291
308
return fork_count_handler ( req , res ) ;
292
- } else if ( req . url . startsWith ( '/endpoint_fork_id' ) ) {
293
- return endpoint_fork_id_handler ( req , res ) ;
294
309
} else if ( req . url . startsWith ( '/_/' ) ) {
295
310
// internals non S3 requests
296
311
const api = req . url . slice ( '/_/' . length ) ;
@@ -531,8 +546,38 @@ function unavailable_handler(req, res) {
531
546
res . end ( reply ) ;
532
547
}
533
548
549
+ /**
550
+ * handler for the inidivdual fork server. used to handle requests the get the worker id
551
+ * currently used to check if fork is alive by the health script
552
+ * @param {EndpointRequest } req
553
+ * @param {import('http').ServerResponse } res
554
+ */
555
+ function fork_main_handler ( req , res ) {
556
+ endpoint_utils . set_noobaa_server_header ( res ) ;
557
+ endpoint_utils . prepare_rest_request ( req ) ;
558
+ if ( req . url . startsWith ( '/endpoint_fork_id' ) ) {
559
+ return endpoint_fork_id_handler ( req , res ) ;
560
+ } else {
561
+ return internal_api_error ( req , res , `Unknown API call ${ req . url } ` ) ;
562
+ }
563
+ }
564
+
565
+ /**
566
+ * fork_message_request_handler is used to handle messages from the primary process.
567
+ * the primary process sends a message with the designated port to start the fork server.
568
+ * @param {Object } msg
569
+ */
570
+ async function fork_message_request_handler ( msg ) {
571
+ await http_utils . start_https_server ( msg . health_port ,
572
+ SERVICES_TYPES_ENUM . FORK_HEALTH ,
573
+ fork_main_handler ,
574
+ msg . nsfs_config_root
575
+ ) ;
576
+ }
577
+
534
578
exports . main = main ;
535
579
exports . create_endpoint_handler = create_endpoint_handler ;
536
580
exports . create_init_request_sdk = create_init_request_sdk ;
581
+ exports . endpoint_fork_id_handler = endpoint_fork_id_handler ;
537
582
538
583
if ( require . main === module ) main ( ) ;
0 commit comments