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

[FIXED] Ensure state changes are posted in order they occur #12

Merged
merged 1 commit into from
Sep 15, 2016

Conversation

kozlovic
Copy link
Member

Both state changes and errors were posted through the use of a go
routine. This did not guarantee that the changes would arrive in
the state change handler in the same order they were produced.

Resolves #11

@coveralls
Copy link

Coverage Status

Coverage increased (+0.2%) to 90.09% when pulling dd88364 on fix_state_change into 92e724b on master.

@@ -36,6 +36,10 @@ type Node struct {
// Async handler
handler Handler

// Used to chain go routines posting events to async handler.
nextSCCh chan bool
Copy link
Member

Choose a reason for hiding this comment

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

maybe nextStateChg

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure. I was trying to keep it small. It standed for next State Change Channel

Copy link
Member

Choose a reason for hiding this comment

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

Took me awhile to figure that out.. ;) That is why I suggested something more descriptive but still short. Maybe just stateChg?

@@ -514,6 +520,20 @@ func (n *Node) switchToCandidate() {
n.switchState(CANDIDATE)
}

// Execute `f` in a separate go routine, but ensures that functions
// are executed in order.
func (n *Node) serializeGoRoutine(nextCh *(chan bool), f func()) {
Copy link
Member

Choose a reason for hiding this comment

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

So this can only handle two channels at most correct? Might want to document if that is the case.

Copy link
Member

Choose a reason for hiding this comment

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

Should we make it generic where their is a list of channels you can select from?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, because the node creates a nextChannel every time, and pass it as a closure to the function.

Copy link
Member

Choose a reason for hiding this comment

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

So we have N Go routines they are just chained together correct? Feels like we should be able to do with 1 Go routine correct?

Copy link
Member Author

@kozlovic kozlovic Sep 14, 2016

Choose a reason for hiding this comment

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

The goal is to not block the node when posting a state change (or error). The default StateChange handler is channel based, and unbuffered (making it buffered would not be a solution anyway). If the user is not dequeuing, the node could be blocked.
Options are for the node to have a single go routine but use an array or list of events that are added and notify the central go routine doing the dispatching.
I feel that in this case, the proposed solution is elegant and simpler. I don't think we can expect thousands of changes a second ;-). But we do want changes to be added to the handler in order. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I hear you, just concerned that we have N Go routines that are lock stepped instead of one that is selecting from multiple channels. But your solution may be better.

Copy link
Member Author

Choose a reason for hiding this comment

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

Let me experiment. I will let you know when ready for review, ok?

Copy link
Member

Choose a reason for hiding this comment

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

Yes sounds good.

@kozlovic
Copy link
Member Author

There are 2 commits, 1 for a fix to first implementation (and rename variables). The second is using a single go-routine. I will let you decide which one you prefer.

@derekcollison
Copy link
Member

ok so I merged the other, but should I merge this one?

Which one is this?

@kozlovic
Copy link
Member Author

This one is for the state change ordering issue. Originally go routines would be started and chained together. You were concerned about the number. The current code shows how we could do with a single go routine (and array of functions).

@derekcollison
Copy link
Member

Which do you prefer?

@kozlovic
Copy link
Member Author

I personally prefer the first one. Simpler and I believe perfectly fine for the use case (don't think there would be too many events).

@kozlovic
Copy link
Member Author

kozlovic commented Sep 15, 2016

Regardless, choose and let me know. I will then need to rebase master and do some cleanup (squash/force commits).

@derekcollison
Copy link
Member

Let's go with your suggestion.. thx..

Both state changes and errors were posted through the use of a go
routine. This did not guarantee that the changes would arrive in
the state change handler in the same order they were produced.

Resolves #11
@coveralls
Copy link

Coverage Status

Coverage increased (+0.2%) to 89.936% when pulling 03e72c9 on fix_state_change into 6b77f48 on master.

@kozlovic
Copy link
Member Author

Ok, good to go here.

@derekcollison derekcollison merged commit 26a3418 into master Sep 15, 2016
@derekcollison derekcollison deleted the fix_state_change branch September 15, 2016 00:51
kozlovic added a commit that referenced this pull request Sep 16, 2016
This is another approach for solving issue #11 that was addressed
with PR #12. The advantage of this approach over #12 is that there
will be atmost 1 go-routine per type of event (StateChange and error).

Added a test that demonstrates that not receiving from state change
and error channels do not block the Node.

Also fixed a flapping test.

Resolves #11
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

3 participants