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

raft: Fix race when proposal is cancelled and triggered at the same time #1846

Merged
merged 1 commit into from
Jan 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions manager/state/raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1467,11 +1467,6 @@ func (n *Node) handleAddressChange(ctx context.Context, member *membership.Membe
return nil
}

type applyResult struct {
resp proto.Message
err error
}

// processInternalRaftRequest sends a message to nodes participating
// in the raft to apply a log entry and then waits for it to be applied
// on the server. It will block until the update is performed, there is
Expand Down Expand Up @@ -1520,13 +1515,18 @@ func (n *Node) processInternalRaftRequest(ctx context.Context, r *api.InternalRa

select {
case x := <-ch:
res := x.(*applyResult)
return res.resp, res.err
return x.(proto.Message), nil
case <-waitCtx.Done():
n.wait.cancel(r.ID)
if !n.wait.cancel(r.ID) {
// wait already triggered
return (<-ch).(proto.Message), nil
}
return nil, ErrLostLeadership
case <-ctx.Done():
n.wait.cancel(r.ID)
if !n.wait.cancel(r.ID) {
// wait already triggered
return (<-ch).(proto.Message), nil
}
return nil, ctx.Err()
}
}
Expand Down Expand Up @@ -1588,7 +1588,7 @@ func (n *Node) processEntry(ctx context.Context, entry raftpb.Entry) error {
return nil
}

if !n.wait.trigger(r.ID, &applyResult{resp: r, err: nil}) {
if !n.wait.trigger(r.ID, r) {
// There was no wait on this ID, meaning we don't have a
// transaction in progress that would be committed to the
// memory store by the "trigger" call. Either a different node
Expand Down
3 changes: 2 additions & 1 deletion manager/state/raft/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ func (w *wait) trigger(id uint64, x interface{}) bool {
return false
}

func (w *wait) cancel(id uint64) {
func (w *wait) cancel(id uint64) bool {
w.l.Lock()
waitItem, ok := w.m[id]
delete(w.m, id)
w.l.Unlock()
if ok && waitItem.cancel != nil {
waitItem.cancel()
}
return ok
}

func (w *wait) cancelAll() {
Expand Down