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

Generate events when nodes are approved/rejected #3772

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

rossjones
Copy link
Contributor

@rossjones rossjones commented Apr 10, 2024

When nodes are approved or rejected, or connected/disconnected, the system may want to respond to these events by creating new evaluations, or cancelling previous tasks.

This PR adds an event emitter which allows implementations of NodeEventHandler to receive events that are one of:

NodeEventApproved
NodeEventRejected
NodeEventDeleted
NodeEventConnected
NodeEventDisconnected

all events are delivered in a goroutine, and so it is the recipients responsibility to handle the event quickly, or to create a new goroutine and return immediately.

Currently this PR delivers events to an event listener in the manager package which currently logs that it has received and event. In future, we will want to modify this behaviour to create new evaluations, or stop current executions based on the node info that is delivered with the event, the job types currently deployed, and the type of event. To enable this, the NodeEventListener is provided with a reference to the evaluation broker and the jobstore.

Copy link

coderabbitai bot commented Apr 10, 2024

Important

Auto Review Skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository.

To trigger a single review, invoke the @coderabbitai review command.


Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@rossjones rossjones marked this pull request as ready for review April 10, 2024 16:21
@rossjones rossjones requested a review from a team April 10, 2024 16:33
case <-completedChan:
return nil
case <-ctx.Done():
return nil
Copy link
Member

Choose a reason for hiding this comment

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

Probably worth returning ctx.Err() here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in ff7828f

Comment on lines 109 to 126
for {
select {
case <-completedChan:
return nil
case <-ctx.Done():
return nil
case <-e.clock.After(waitDuration):
return fmt.Errorf("timed out waiting for %s event callbacks to complete", event.String())
}
}
Copy link
Member

Choose a reason for hiding this comment

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

we can remove this for loop since select will block until one of its cases can run.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in ff7828f, not sure why it was there in the first place, probably force of habit.

for _, hlr := range e.callbacks {
wg.Add(1)
go func(handler NodeEventHandler) {
handler.HandleNodeEvent(ctx, info, event)
Copy link
Member

Choose a reason for hiding this comment

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

I am thinking it's worth passing a sub-context with a timeout to these methods instead of the parent context. This way we ensure that all the handlers receive the cancellation signal and can stop their execution, and (try to) prevent goroutine leaks on timeout. What do you think?

Copy link
Contributor Author

@rossjones rossjones Apr 10, 2024

Choose a reason for hiding this comment

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

Good idea, will give it a try. The problem might be that the timeout will not be using the mock clock, and so is only really useful for cleaning up unfinished callbacks rather than being useful to replace the timeout on each callback.

Cancellable version in 1da8eb2

Copy link
Member

Choose a reason for hiding this comment

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

The problem might be that the timeout will not be using the mock clock, and so is only really useful for cleaning up unfinished callbacks rather than being useful to replace the timeout on each callback

oo yeah, good point. Change looks good here - thanks.

wg.Wait()
}()

waitDuration := 1 * time.Second
Copy link
Member

Choose a reason for hiding this comment

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

Can we make this a field on the NodeEventEmitter? It will be easier to change in the event we need to adjust this value later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in ff7828f

Comment on lines 10 to 12
type NodeEventHandler interface {
HandleNodeEvent(ctx context.Context, info models.NodeInfo, event NodeEvent)
}
Copy link
Member

Choose a reason for hiding this comment

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

I would find the code slightly more readable if we declared this interface where it's expected/used before the RegisterHandler method on the NodeEventEmitter instead of in a separate file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reasonble, done in ff7828f. I think I put it in an interfaces.go as there's been a bit of a pattern with the eval stuff that there's an interfaces.go and a types.go.

@@ -815,3 +816,10 @@ func (b *InMemoryBroker) registerMetrics() (metric.Registration, error) {
}, orchestrator.EvalBrokerReady, orchestrator.EvalBrokerInflight, orchestrator.EvalBrokerPending,
orchestrator.EvalBrokerWaiting, orchestrator.EvalBrokerCancelable)
}

func (b *InMemoryBroker) HandleNodeEvent(ctx context.Context, info models.NodeInfo, evt manager.NodeEvent) {
Copy link
Member

Choose a reason for hiding this comment

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

For readability add a compile time assertion to the top of this file:
var _ manager.NodeEventHandler = &InMemoryBroker{}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in ff7828f

@@ -69,7 +69,6 @@ func (t liveness) IsValid() bool {
type livenessContainer struct {
CONNECTED NodeState
DISCONNECTED NodeState
HEALTHY NodeState
Copy link
Member

Choose a reason for hiding this comment

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

CONNECTED now implies healthy I assume? or was this just never used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was never used and I think only worked because the default for ints is 0.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Correct "healthy" implied something that we don't know about the node. "Connected" is the right term (for now)

Comment on lines +43 to +50
// WithClock is an option that can be used to set the clock for the NodeEventEmitter. This is useful
// for testing purposes.
func WithClock(clock clock.Clock) NodeEventEmitterOption {
return func(emitter *NodeEventEmitter) {
emitter.clock = clock
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Not sure this is used anywhere, was it meant to be used in a test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was, and is with b9a6953

@rossjones rossjones requested a review from a team April 11, 2024 11:36
Comment on lines +87 to +89
go func() {
s.clock.Add(10 * time.Second)
}()
Copy link
Member

Choose a reason for hiding this comment

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

why does this need a go routine? I'd assume adding 10 seconds to the clock is non-blocking?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some weirdness with the mock clock where it won't work when it's inline. I've been trying to work out if I'm just holding it wrong but I suspect it's something to do with the work in EmitEvent happening in a goroutine and requiring a yield (but I'm not 100% sure)

@frrist
Copy link
Member

frrist commented Apr 11, 2024

@rossjones I have implemented a fix for the NodeInfo overwrite bug here: #3785
It was a very large mechanical change to make and sadly touched a lot of files. If it looks good to you, could we land your change (this PR) after it to avoid conflicts? I suspect it will be easier to resolved conflicts from your change here than the other way around. However if you spot any significant issues with #3785 go ahead and merge this and I'll sort the conflicts on my end.

@wdbaruni
Copy link
Collaborator

@frrist are you owning this PR now? what do we need to do to unblock it and merge it?

@frrist
Copy link
Member

frrist commented Apr 16, 2024

@wdbaruni yeah I can take this over.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants