From 42d7def171cd6cf07a86fe61cbfc80b3a0a0b631 Mon Sep 17 00:00:00 2001 From: Sameer Kumar Subudhi Date: Wed, 10 Jan 2024 12:38:09 +0530 Subject: [PATCH] :bug: Kickstart event subscription if the eventSubscriptionClient is renewed --- services/blockchain-connector/config.js | 2 +- .../blockchain-connector/shared/sdk/client.js | 18 +++++++++++++----- .../blockchain-connector/shared/sdk/events.js | 5 +++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/services/blockchain-connector/config.js b/services/blockchain-connector/config.js index cc725ebbbd..ad8864446f 100644 --- a/services/blockchain-connector/config.js +++ b/services/blockchain-connector/config.js @@ -127,7 +127,7 @@ config.apiClient = { // Every n milliseconds, verify if client connection is alive config.clientConnVerifyInterval = - Number(process.env.CLIENT_CONNECTION_VERIFY_INTERVAL) || 60 * 1000; // in millisecs + Number(process.env.CLIENT_CONNECTION_VERIFY_INTERVAL) || 30 * 1000; // in millisecs // Backdoor config to restart the connector if the stall issue pops up - disabled by default const exitDelay = Number(process.env.CONNECTOR_EXIT_DELAY_IN_HOURS); // in hours diff --git a/services/blockchain-connector/shared/sdk/client.js b/services/blockchain-connector/shared/sdk/client.js index 480755100e..61e6e3be1c 100644 --- a/services/blockchain-connector/shared/sdk/client.js +++ b/services/blockchain-connector/shared/sdk/client.js @@ -56,15 +56,15 @@ const clientInstantiationStats = { }; let requestCount = 0; +const checkIsClientAlive = client => client && client._channel && client._channel.isAlive; + const getApiClientStats = () => ({ ...clientInstantiationStats, - currentPoolSize: clientPool.length, + activePoolSize: clientPool.filter(client => checkIsClientAlive(client)).length, expectedPoolSize: MAX_CLIENT_POOL_SIZE, numEndpointInvocations: requestCount, }); -const checkIsClientAlive = client => client && client._channel && client._channel.isAlive; - const pingListener = apiClient => { if (!isObject(apiClient)) { logger.warn(`apiClient is ${JSON.stringify(apiClient)}. Cannot register a pingListener.`); @@ -125,7 +125,13 @@ const initClientPool = async poolSize => { // Set the intervals only at application init if (clientPool.length === 0) { setInterval(() => { - logger.info(`API client instantiation stats: ${JSON.stringify(getApiClientStats())}`); + const stats = getApiClientStats(); + logger.info(`API client instantiation stats: ${JSON.stringify(stats)}`); + if (stats.activePoolSize < stats.expectedPoolSize) { + logger.warn( + 'activePoolSize should catch up with the expectedPoolSize, once the node is under less stress.', + ); + } }, 5 * 60 * 1000); setInterval(() => { @@ -133,12 +139,14 @@ const initClientPool = async poolSize => { if (isObject(apiClient)) return; // Re-instantiate when null - clientPool[index] = await instantiateNewClient() + const newApiClient = await instantiateNewClient() .then(client => { client.poolIndex = index; return client; }) .catch(() => null); + clientPool[index] = newApiClient; + if (newApiClient) Signals.get('newApiClient').dispatch(newApiClient.poolIndex); }); }, WS_SERVER_PING_INTERVAL); } diff --git a/services/blockchain-connector/shared/sdk/events.js b/services/blockchain-connector/shared/sdk/events.js index 6b7982f9ff..6e8b025b6a 100644 --- a/services/blockchain-connector/shared/sdk/events.js +++ b/services/blockchain-connector/shared/sdk/events.js @@ -160,6 +160,9 @@ const ensureAPIClientLiveness = () => { logger.debug( `Dispatched 'resetApiClient' signal for the event subscription API client ${apiClient.poolIndex}.`, ); + } else { + logger.debug('Triggered subscribeToAllRegisteredEvents from ensureAPIClientLiveness.'); + await subscribeToAllRegisteredEvents(); } } } catch (_) { @@ -174,11 +177,13 @@ const ensureAPIClientLiveness = () => { }; const nodeIsSyncedListener = () => { + logger.debug('Node is now synced with the network.'); isNodeSynced = true; ensureAPIClientLiveness(); }; const genesisBlockDownloadedListener = () => { + logger.debug('Genesis block is now downloaded.'); isGenesisBlockDownloaded = true; ensureAPIClientLiveness(); };