Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pool events for workers #624

Merged
merged 13 commits into from
Sep 20, 2024
Merged

Conversation

metcoder95
Copy link
Member

@metcoder95 metcoder95 commented Jul 31, 2024

No description provided.

@metcoder95 metcoder95 changed the base branch from current to feat/worker-events July 31, 2024 09:31
@metcoder95 metcoder95 requested a review from ronag July 31, 2024 09:31
@metcoder95 metcoder95 mentioned this pull request Jul 31, 2024
10 tasks
Base automatically changed from feat/worker-events to feat/custom_balancer August 2, 2024 09:16
@@ -31,6 +33,8 @@ export class WorkerInfo extends AsynchronouslyCreatedResource {
lastSeenResponseCount : number = 0;
onMessage : ResponseCallback;
histogram: RecordableHistogram | null;
terminating = false;
destroyed = false;
Copy link

@jerome-benoit jerome-benoit Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the use cases needing to keep track of the worker lifecycle at termination or pool destroying?

The use cases regarding internal worker state signaling:

  • Is a worker ready?
  • Is a worker errored?

The use cases regarding pool state signaling:

  • Is the pool ready?
  • ...

I do not see a use case where workers lifecycle need to be signaled to the pool user: a worker pool is meant to guarantee that the submitted task will be executed on a worker with a minimum overhead per design (worker pre-instantiation and preparation, workers lifecycle housekeeping, ... like in an object cache factory design pattern) .

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use-cases I've in mind was mostly monitoring or possibly to update the load balancer metrics upon request, my fear here is to cause big overhead due to the constant creations of EventEmitter.

Although the API might be better to communicate this, I found enough by stating the current state of the worker, found it less intrusive and easier to keep track. As using getters for it, the state will be updated almost immediately, reflecting the state of the worker at a given point of time.

Maybe I need to add some more to it, but currently I could just think on terminating and destroyed; which can be potentially helpful as there will be race conditions where the balancer might want to distribute the workload to a given worker that its crashing at the same time (reducing the surface but maybe not mitigating it).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood now. Make sense. The implementation is debatable but is inline with the existing events emission one.

LB would need to track worker lifecycle and task(s) lifecycle on a worker.

@@ -2,6 +2,7 @@ import assert from 'node:assert';

export abstract class AsynchronouslyCreatedResource {
onreadyListeners : (() => void)[] | null = [];
ondestroyListeners : (() => void)[] | null = [];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not make AsynchronouslyCreatedResource extends EventEmitter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we emit events with the updated state of the worker?

Copy link

@jerome-benoit jerome-benoit Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing code seems to re-eimplement from scratch EventEmitter semantic. I do not know the rationale behind it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See what you mean; me neither, I imagine the idea was to implement a lightweight version of the EventEmitter; let me see if there's some potential performance overhead that was tried to be avoided, otherwise can switch to it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After replacing, and using an EventEmitter instead, the performance dropped at least 30%. Then it confirms it was mostly due to small use case and to reduce overhead

Copy link

This issue has been marked as stale because it has been opened 45 days without activity. Remove stale label or comment or this will be closed in 10 days.

@github-actions github-actions bot added the stale label Sep 15, 2024
@metcoder95 metcoder95 added wip PRs WIP and removed stale labels Sep 15, 2024
test/pool.ts Outdated Show resolved Hide resolved
@metcoder95 metcoder95 merged commit f329b81 into feat/custom_balancer Sep 20, 2024
1 of 10 checks passed
@metcoder95 metcoder95 deleted the feat/pool-events branch September 20, 2024 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wip PRs WIP
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants