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

Hare msg validation #572

Merged
merged 14 commits into from Feb 25, 2019
Merged

Hare msg validation #572

merged 14 commits into from Feb 25, 2019

Conversation

gavraz
Copy link
Contributor

@gavraz gavraz commented Feb 19, 2019

  • Extracted validation
  • Instance id is now uint32
  • Broker validates instance id
  • Fixed & added unit tests

hare/haretypes.go Outdated Show resolved Hide resolved
hare/haretypes.go Outdated Show resolved Hide resolved
hare/algorithm.go Outdated Show resolved Hide resolved
Copy link
Contributor

@antonlerner antonlerner left a comment

Choose a reason for hiding this comment

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

Please refrain from using explicit uint32 types, only cast to primitive type when serializing

hare/broker.go Outdated
}
broker.pending[instanceId.Id()] = append(broker.pending[instanceId.Id()], mOut)
broker.pending[instanceId.Id()] = append(broker.pending[instanceId.Id()], hareMsg)
Copy link
Contributor

Choose a reason for hiding this comment

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

are there any concurrent writes to pending[] map? should it be locked if so? I'd recommend running your unit tests with --race before you push

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am protecting it with the lock. Let me know if you see a scenario in which I do not. Didn't see issues with -race regarding this test.

Copy link
Contributor

Choose a reason for hiding this comment

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

As I wrote in the comment above, if you'll move Register into this select you could lose the mutex. @antonlerner what do you think? It is a different scenario than what you'd benchmarked, Register is called less often than this loop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure it is a good idea because I will eventually want to remove the inboxer interface and return the inbox. That will complicate the register call. We can probably solve it by passing a function on the channel but it sounds too complicated.

Copy link
Contributor

Choose a reason for hiding this comment

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

you can still make this change and return the channel. Just create the channel and pass it to the dispatcher via the register channel. You can make this change as well as removing the inboxer as part of this PR.

@gavraz gavraz requested a review from zalmen February 20, 2019 10:04
hare/broker.go Outdated Show resolved Hide resolved
hare/broker.go Outdated Show resolved Hide resolved
continue
}

instanceId := NewBytes32(hareMsg.Message.InstanceId)
// message validation
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider extracting the code below to a method (until // validation passed), it will improve the readability

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I considered it before and decided that using continue is preferred on using the return value.

Copy link
Contributor

Choose a reason for hiding this comment

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

Your comment has nothing to do with what I wrote... the dispatcher method is very long for no reason. Please break into smaller methods

hare/broker.go Outdated
outbox map[uint32]chan *pb.HareMessage
pending map[uint32][]*pb.HareMessage
mutex sync.RWMutex
maxReg uint32
Copy link
Contributor

Choose a reason for hiding this comment

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

consider renaming to lastReg, "max" is a bit confusing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But it is the max and not the last. using the last will add the assumption that we never start a proc id' before id if id'>id. While this is correct for now I prefer not to add this assumption, nor see a reason to do that.

Copy link
Contributor

@zalmen zalmen Feb 24, 2019

Choose a reason for hiding this comment

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

Of course you'll never start proc id' before id, if id'>id. The assumptions should be as strict as possible and should be loosened only when needed. In which case would you allow executing process out of order? Do you have tests for such cases?

hare/broker.go Outdated
continue
}

if hareMsg.Message.InstanceId > broker.maxReg+1 { // intended for future unregistered instance
Copy link
Contributor

Choose a reason for hiding this comment

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

you can't read from maxReg without locking broker.mutex. I would suggest that you'll move Register and Unregister into this select loop and lose the mutex

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, you should also check that the InstanceId belongs to a "live" execution (i.e. that if someone tries to send you a message on a Hare instance that was already completed that message will be considered invalid)

Copy link
Contributor

Choose a reason for hiding this comment

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

btw, currently it seems that no one calls Unregister - open a bug (if it's a small fix then add it as part of this PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed 1 & 2.
Opened a bug for 3.

hare/broker.go Outdated
}
broker.pending[instanceId.Id()] = append(broker.pending[instanceId.Id()], mOut)
broker.pending[instanceId.Id()] = append(broker.pending[instanceId.Id()], hareMsg)
Copy link
Contributor

Choose a reason for hiding this comment

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

As I wrote in the comment above, if you'll move Register into this select you could lose the mutex. @antonlerner what do you think? It is a different scenario than what you'd benchmarked, Register is called less often than this loop

hare/algorithm.go Show resolved Hide resolved
hare/broker.go Outdated Show resolved Hide resolved
@gavraz gavraz dismissed antonlerner’s stale review February 25, 2019 12:24

Fixed & reviewed by Yuval

@gavraz gavraz merged commit 664c6bf into develop Feb 25, 2019
beckmani pushed a commit that referenced this pull request Mar 6, 2019
* Extracted validation
* Instance id is now uint32 like layer id
* Broker validates instance id
* Replaced uint32 with InstanceId and id types
* Event loop broker (no mutex)
@y0sher y0sher deleted the hare_msg_validation branch September 10, 2019 09:30
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