Skip to content
This repository was archived by the owner on Feb 1, 2021. It is now read-only.

Refactor and move event handling code into the cluster package#2752

Merged
nishanttotla merged 7 commits intodocker-archive:masterfrom
nishanttotla:events-refactor-watchdog
Jul 21, 2017
Merged

Refactor and move event handling code into the cluster package#2752
nishanttotla merged 7 commits intodocker-archive:masterfrom
nishanttotla:events-refactor-watchdog

Conversation

@nishanttotla
Copy link
Copy Markdown
Contributor

@nishanttotla nishanttotla commented Jun 29, 2017

In this PR

  • Remove watchQueue as a cluster object, and package it inside the APIEventHandler
  • Move events code to the cluster package
  • Refactor functions and structs with more descriptive names
  • Remove redundant code

Addresses #2749
Follows up #2747

@nishanttotla nishanttotla added this to the 1.3.0 milestone Jun 29, 2017
@nishanttotla nishanttotla force-pushed the events-refactor-watchdog branch 2 times, most recently from dbfd98b to eb42260 Compare June 30, 2017 00:30
@nishanttotla nishanttotla mentioned this pull request Jun 30, 2017
3 tasks
@nishanttotla nishanttotla force-pushed the events-refactor-watchdog branch 2 times, most recently from 9249e16 to eeafb8d Compare June 30, 2017 17:22
@nishanttotla nishanttotla changed the title [WIP] Major events refactoring Major events refactoring Jun 30, 2017
@nishanttotla nishanttotla changed the title Major events refactoring Refactor and move event handling code into the cluster package Jun 30, 2017
@nishanttotla nishanttotla force-pushed the events-refactor-watchdog branch 3 times, most recently from 3edec95 to 839ce5f Compare July 5, 2017 16:38
@nishanttotla
Copy link
Copy Markdown
Contributor Author

Ping @allencloud can you take a look?

@allencloud
Copy link
Copy Markdown
Contributor

OK, I will today. @nishanttotla

@nishanttotla nishanttotla force-pushed the events-refactor-watchdog branch 4 times, most recently from a3c9c7d to 2342a32 Compare July 15, 2017 01:12
Signed-off-by: Nishant Totla <nishanttotla@gmail.com>
Signed-off-by: Nishant Totla <nishanttotla@gmail.com>
This is to eliminate some confusion around how events are handled in
Swarm.

Signed-off-by: Nishant Totla <nishanttotla@gmail.com>
function

Signed-off-by: Nishant Totla <nishanttotla@gmail.com>
Signed-off-by: Nishant Totla <nishanttotla@gmail.com>
@nishanttotla nishanttotla force-pushed the events-refactor-watchdog branch from 2342a32 to 9f062b6 Compare July 18, 2017 06:13
Copy link
Copy Markdown
Contributor

@alexmavr alexmavr left a comment

Choose a reason for hiding this comment

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

Implementation looks good, just a couple comments on code structure. I ran a couple smoke-tests locally and this PR does not seem to be causing any behavioral changes on first glance.


// EventHandler is exported
type EventHandler interface {
Handle(*Event) error
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks like at this point all implementations of this method always return nil instead of an error. How about making this not return anything?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Seems like you're right. I can't recall why it was this way in the past, because generally event related errors don't cause things to fail, but perhaps in the future we might want to log those errors. What do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Personally, I like to avoid returning an error when one is never expected to occur, but I don't have a strong preference here.

// CloseWatchQueues unregisters all API event handlers (the ones with
// watch queues) and closes the respective queues. This should be
// called when the manager shuts down
func (eh *ClusterEventHandlers) CloseWatchQueues() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This could also be structured as a generic Cleanup method. Then, we can switch on the type of the handler below and perform any sort of type-specific cleanup (such as closing the watch queue for APIEventHandlers).

This is probably a vain effort though, as the other handlers don't require any cleanup, and I don't think we expect any new types of event handlers with the current architecture

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree that this effort isn't very useful at this time, and we will not have more types of event handlers. Besides, it won't be hard to change it in the future if necessary.

}

// HandleAll callbacks for the events
func (eh *ClusterEventHandlers) HandleAll(e *Event) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not name this Handle so that the ClusterEventHandlers also satisfies the EventHandler interface?

Copy link
Copy Markdown
Contributor Author

@nishanttotla nishanttotla Jul 21, 2017

Choose a reason for hiding this comment

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

I called it HandleAll for more clarity about what it does, and it didn't seem like we got anything by making it satisfy the interface.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The benefit to the rename is that we don't have to implement a Cluster.Handle method at all, if we embed Cluster.ClusterEventHandlers

@@ -169,31 +167,31 @@ func NewCluster(scheduler *scheduler.Scheduler, TLSConfig *tls.Config, master st

// Handle callbacks for the events
func (c *Cluster) Handle(e *cluster.Event) error {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If the Cluster struct were to embed a clusterEventHandlers, and the HandleAll method were renamed to Handle, this method wrapper could be removed entirely, as well as the Register/Unregister methods below.

This comment also applies for the swarm cluster object as well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This sounds like a reasonable thing to do. I believe in addition to renaming HandleAll, we'll also need to change the interface to not have Handle return the error, as you said above.

// CloseWatchQueues unregisters all API event handlers (the ones with
// watch queues) and closes the respective queues. This should be
// called when the manager shuts down
func (c *Cluster) CloseWatchQueues() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This method looks like a no-op, is there any reason why it can't be removed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh, this is a mistake I think. It was supposed to call the CloseWatchQueues function of ClusterEventHandlers.

Signed-off-by: Nishant Totla <nishanttotla@gmail.com>
@nishanttotla nishanttotla force-pushed the events-refactor-watchdog branch 2 times, most recently from 9d05bfa to 4fcf4a1 Compare July 21, 2017 20:36
@nishanttotla
Copy link
Copy Markdown
Contributor Author

@alexmavr Addressed your comments, and embedded ClusterEventsHandler inside the Cluster struct.

I've kept the error returned by the handler as of now, because I want to investigate if it makes sense to error out in some places (this I plan to do as a follow-up)

@nishanttotla nishanttotla modified the milestones: 1.2.9, 1.3.0 Jul 21, 2017
Copy link
Copy Markdown
Contributor

@alexmavr alexmavr left a comment

Choose a reason for hiding this comment

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

LGTM. Tests still pass with the new changes

refuseTimeout: defaultRefuseTimeout,
}

cluster.InitClusterEventHandlers()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That method change wasn't really required, you can still initialize an embedded struct, such as:

cluster := &Cluster{
   ClusterEventHandlers: cluster.NewClusterEventHandlers(),
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, I had changed it earlier for some reason, but forgot to fix it later. I'll change it.

…side the Cluster object

Signed-off-by: Nishant Totla <nishanttotla@gmail.com>
@nishanttotla nishanttotla force-pushed the events-refactor-watchdog branch from 4fcf4a1 to 995e078 Compare July 21, 2017 23:03
@nishanttotla nishanttotla merged commit d28fd0d into docker-archive:master Jul 21, 2017
@nishanttotla nishanttotla deleted the events-refactor-watchdog branch July 21, 2017 23:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants