From fb8429b0739249cdb608f4bd0dc89508a098e8d0 Mon Sep 17 00:00:00 2001 From: Martin Martinez Rivera Date: Thu, 4 Jun 2020 14:51:18 -0700 Subject: [PATCH 1/3] Use new field nextRaftId to give unique IDs to new nodes. --- dgraph/cmd/zero/zero.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/dgraph/cmd/zero/zero.go b/dgraph/cmd/zero/zero.go index 0e5cd5cad60..640794147d9 100644 --- a/dgraph/cmd/zero/zero.go +++ b/dgraph/cmd/zero/zero.go @@ -56,6 +56,7 @@ type Server struct { NumReplicas int state *pb.MembershipState + nextRaftId uint64 nextLeaseId uint64 nextTxnTs uint64 @@ -83,6 +84,7 @@ func (s *Server) Init() { Groups: make(map[uint32]*pb.Group), Zeros: make(map[uint64]*pb.Member), } + s.nextRaftId = 1 s.nextLeaseId = 1 s.nextTxnTs = 1 s.nextGroup = 1 @@ -217,7 +219,10 @@ func (s *Server) hasLeader(gid uint32) bool { func (s *Server) SetMembershipState(state *pb.MembershipState) { s.Lock() defer s.Unlock() + s.state = state + s.nextRaftId = x.Max(s.nextRaftId, s.state.MaxRaftId + 1) + if state.Zeros == nil { state.Zeros = make(map[uint64]*pb.Member) } @@ -491,7 +496,8 @@ func (s *Server) Connect(ctx context.Context, } } if m.Id == 0 { - m.Id = s.state.MaxRaftId + 1 + m.Id = s.nextRaftId + s.nextRaftId += 1 proposal.MaxRaftId = m.Id } From b35d2455b2b5350f762669996b5e72c5aeeaee2a Mon Sep 17 00:00:00 2001 From: Martin Martinez Rivera Date: Thu, 4 Jun 2020 14:59:08 -0700 Subject: [PATCH 2/3] Update nextRaftId when updating the maxRaftId. --- dgraph/cmd/zero/raft.go | 1 + dgraph/cmd/zero/zero.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/dgraph/cmd/zero/raft.go b/dgraph/cmd/zero/raft.go index 86b0aea302d..395c80189ed 100644 --- a/dgraph/cmd/zero/raft.go +++ b/dgraph/cmd/zero/raft.go @@ -324,6 +324,7 @@ func (n *node) applyProposal(e raftpb.Entry) (string, error) { return p.Key, errInvalidProposal } state.MaxRaftId = p.MaxRaftId + n.server.nextRaftId = x.Max(n.server.nextRaftId, p.MaxRaftId+1) } if p.SnapshotTs != nil { for gid, ts := range p.SnapshotTs { diff --git a/dgraph/cmd/zero/zero.go b/dgraph/cmd/zero/zero.go index 640794147d9..5ece856192c 100644 --- a/dgraph/cmd/zero/zero.go +++ b/dgraph/cmd/zero/zero.go @@ -221,7 +221,7 @@ func (s *Server) SetMembershipState(state *pb.MembershipState) { defer s.Unlock() s.state = state - s.nextRaftId = x.Max(s.nextRaftId, s.state.MaxRaftId + 1) + s.nextRaftId = x.Max(s.nextRaftId, s.state.MaxRaftId+1) if state.Zeros == nil { state.Zeros = make(map[uint64]*pb.Member) From 235fbe86be08d891a46aad527a04c04783ba1823 Mon Sep 17 00:00:00 2001 From: Martin Martinez Rivera Date: Fri, 5 Jun 2020 11:07:15 -0700 Subject: [PATCH 3/3] Add comment. --- dgraph/cmd/zero/zero.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dgraph/cmd/zero/zero.go b/dgraph/cmd/zero/zero.go index 5ece856192c..d1da1cefde4 100644 --- a/dgraph/cmd/zero/zero.go +++ b/dgraph/cmd/zero/zero.go @@ -496,6 +496,9 @@ func (s *Server) Connect(ctx context.Context, } } if m.Id == 0 { + // In certain situations, the proposal can be sent and return with an error. + // However, Dgraph will keep retrying the proposal. To avoid assigning duplicating + // IDs, the couter is incremented every time a proposal is created. m.Id = s.nextRaftId s.nextRaftId += 1 proposal.MaxRaftId = m.Id