Skip to content

Worker lifecycle issues with lib/worker.js and lib/cluster.js #803

Description

@jdmarshall

AI caught that we don't see a close event if a worker is terminated. Thinking about this code has revealed some more issues.

We cannot tell if a worker has gone away

Problem is that nodejs/node#59053 means that we never get the Worker object and therefore we cannot determine if the worker is dead, except by controlling the top level application workflows that are responsible for creating Workers in the first place, and that seems a bit mad especially given how many libraries there are that already do that.

And if the broadcast channel hasn't closed, then we don't know when one of our siblings has exited. If a worker terminates after it has been announced, then all subsequent GET_METRICS_REQ calls get an unfulfilled promise and you get no more metrics at all.

I believe we will have to do some sort of supervisor functionality in the main thread. But I'm not aware of a way to enumerate all of the workers that are currently running.

We don't remember historical values from workers and simply return no answer if any are missing

Every time a worker dies, counts get screwed up.

There may be some argument for keeping the last value recorded from a worker. Probably not gauges, but maybe everything else.

Because the Worker feature is going to be new to all users, we can ask the users to do a bit of additional bookkeeping to use this
functionality as table stakes for using this feature at all. We still have the luxury of adding that ask without listing it as a breaking change in the process. And it's a non-breaking change for Cluster if we ship it in v0.16 because dead workers has always been a fatal error there.

##Tasks

[ ] Document that reloading prom-client has undefined behavior in production mode.
[ ] Add debugging output (or console.warn?) to warn about existing listeners being found on initial call
[x] Simplify the promise logic in clusterMetrics() and workerMetrics() and store a promise for in-flight calls
[x] Implement dead workers support in the aggregator - aggregate all 'sum' type metrics to keep stats from backtracking
[x] Add a shutdown function for syncing data
[x] Add a shutdown event to worker.js, which flushes a 'swan song' last metric update to the aggregator to be retained in perpetuity
[x] Add a shutdown event to cluster.js, which flushes a 'swan song' last metric update to the aggregator to be retained in perpetuity
[x] Implement an ACK for the flush messages, because BroadcastChannel is complete Fire and Forget and process.exit() is likely to cause it to abort.
[x] Update the examples to match the new logic

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions