Skip to content

Commit

Permalink
Await CN services before starting server (#3937)
Browse files Browse the repository at this point in the history
  • Loading branch information
theoilie committed Sep 26, 2022
1 parent 56340ff commit 13d48e5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 37 deletions.
13 changes: 3 additions & 10 deletions creator-node/src/index.ts
Expand Up @@ -175,20 +175,13 @@ const startAppForWorker = async () => {
}
})

const nodeMode = config.get('devMode') ? 'Dev Mode' : 'Production Mode'

await serviceRegistry.initServices()
const nodeMode = config.get('devMode') ? 'Dev Mode' : 'Production Mode'
logger.info(`Initialized services (Node running in ${nodeMode})`)
serviceRegistry.initServicesAsynchronously()
const appInfo = initializeApp(getPort(), serviceRegistry)
logger.info('Initialized app and server')

// Make the first worker wait for some services to be fully up before spinning up other workers
serviceRegistry.initServicesAsynchronously()
if (clusterUtils.isThisWorkerInit()) {
await serviceRegistry.initServicesThatRequireServer(appInfo.app)
} else {
serviceRegistry.initServicesThatRequireServer(appInfo.app)
}
await serviceRegistry.initServicesThatRequireServer(appInfo.app)

if (clusterUtils.isThisWorkerInit() && process.send) {
process.send({ cmd: 'initComplete' })
Expand Down
52 changes: 25 additions & 27 deletions creator-node/src/serviceRegistry.js
Expand Up @@ -92,6 +92,31 @@ class ServiceRegistry {

this.synchronousServicesInitialized = true

// Cannot progress without recovering spID from node's record on L1 ServiceProviderFactory contract
// Retries indefinitely
await this._recoverNodeL1Identity()

// Init StateMachineManager
this.stateMachineManager = new StateMachineManager()
const {
monitorStateQueue,
findSyncRequestsQueue,
findReplicaSetUpdatesQueue,
cNodeEndpointToSpIdMapQueue,
manualSyncQueue,
recurringSyncQueue,
updateReplicaSetQueue,
recoverOrphanedDataQueue
} = await this.stateMachineManager.init(this.libs, this.prometheusRegistry)
this.monitorStateQueue = monitorStateQueue
this.findSyncRequestsQueue = findSyncRequestsQueue
this.findReplicaSetUpdatesQueue = findReplicaSetUpdatesQueue
this.cNodeEndpointToSpIdMapQueue = cNodeEndpointToSpIdMapQueue
this.manualSyncQueue = manualSyncQueue
this.recurringSyncQueue = recurringSyncQueue
this.updateReplicaSetQueue = updateReplicaSetQueue
this.recoverOrphanedDataQueue = recoverOrphanedDataQueue

logInfoWithDuration(
{ logger: genericLogger, startTime: start },
'ServiceRegistry || Initialized synchronous services'
Expand Down Expand Up @@ -287,8 +312,6 @@ class ServiceRegistry {
/**
* Some services require the node server to be running in order to initialize. Run those here.
* Specifically:
* - recover node L1 identity (requires node health check from server to return success)
* - initialize SnapbackSM service (requires node L1 identity)
* - construct SyncQueue (requires node L1 identity)
* - register node on L2 URSM contract (requires node L1 identity)
* - construct & init SkippedCIDsRetryQueue (requires SyncQueue)
Expand All @@ -297,31 +320,6 @@ class ServiceRegistry {
async initServicesThatRequireServer(app) {
const start = getStartTime()

// Cannot progress without recovering spID from node's record on L1 ServiceProviderFactory contract
// Retries indefinitely
await this._recoverNodeL1Identity()

// Init StateMachineManager
this.stateMachineManager = new StateMachineManager()
const {
monitorStateQueue,
findSyncRequestsQueue,
findReplicaSetUpdatesQueue,
cNodeEndpointToSpIdMapQueue,
manualSyncQueue,
recurringSyncQueue,
updateReplicaSetQueue,
recoverOrphanedDataQueue
} = await this.stateMachineManager.init(this.libs, this.prometheusRegistry)
this.monitorStateQueue = monitorStateQueue
this.findSyncRequestsQueue = findSyncRequestsQueue
this.findReplicaSetUpdatesQueue = findReplicaSetUpdatesQueue
this.cNodeEndpointToSpIdMapQueue = cNodeEndpointToSpIdMapQueue
this.manualSyncQueue = manualSyncQueue
this.recurringSyncQueue = recurringSyncQueue
this.updateReplicaSetQueue = updateReplicaSetQueue
this.recoverOrphanedDataQueue = recoverOrphanedDataQueue

// SyncQueue construction (requires L1 identity)
// Note - passes in reference to instance of self (serviceRegistry), a very sub-optimal workaround
this.syncQueue = new SyncQueue(config, this.redis, this)
Expand Down

0 comments on commit 13d48e5

Please sign in to comment.