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

CID never created if Zero stops early after first init (cherry pick to release/v20.07) #6209

Closed
wants to merge 1 commit into from
Closed
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
101 changes: 75 additions & 26 deletions dgraph/cmd/zero/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,73 @@ func (n *node) triggerLeaderChange() {
n.server.updateZeroLeader()
}

func (n *node) proposeNewCID() {
// Either this is a new cluster or can't find a CID in the entries. So, propose a new ID for the cluster.
// CID check is needed for the case when a leader assigns a CID to the new node and the new node is proposing a CID
for len(n.server.membershipState().Cid) == 0 {
id := uuid.New().String()
err := n.proposeAndWait(context.Background(), &pb.ZeroProposal{Cid: id})
if err == nil {
glog.Infof("CID set for cluster: %v", id)
break
}
if err == errInvalidProposal {
glog.Errorf("invalid proposal error while proposing cluster id")
return
}
glog.Errorf("While proposing CID: %v. Retrying...", err)
time.Sleep(3 * time.Second)
}

// Apply trial license only if not already licensed.
if n.server.license() == nil {
if err := n.proposeTrialLicense(); err != nil {
glog.Errorf("while proposing trial license to cluster: %v", err)
}
}
}

func (n *node) checkForCIDInEntries() (bool, error) {
first, err := n.Store.FirstIndex()
if err != nil {
return false, err
}
last, err := n.Store.LastIndex()
if err != nil {
return false, err
}

for batch := first; batch <= last; {
entries, err := n.Store.Entries(batch, last+1, 64<<20)
if err != nil {
return false, err
}

// Exit early from the loop if no entries were found.
if len(entries) == 0 {
break
}

// increment the iterator to the next batch
batch = entries[len(entries)-1].Index + 1

for _, entry := range entries {
if entry.Type != raftpb.EntryNormal {
continue
}
var proposal pb.ZeroProposal
err = proposal.Unmarshal(entry.Data)
if err != nil {
return false, err
}
if len(proposal.Cid) > 0 {
return true, err
}
}
}
return false, err
}

func (n *node) initAndStartNode() error {
_, restart, err := n.PastLife()
x.Check(err)
Expand All @@ -461,6 +528,13 @@ func (n *node) initAndStartNode() error {
}

n.SetRaft(raft.RestartNode(n.Cfg))
foundCID, err := n.checkForCIDInEntries()
if err != nil {
return err
}
if !foundCID {
go n.proposeNewCID()
}

case len(opts.peer) > 0:
p := conn.GetPools().Connect(opts.peer)
Expand Down Expand Up @@ -500,32 +574,7 @@ func (n *node) initAndStartNode() error {
x.Check(err)
peers := []raft.Peer{{ID: n.Id, Context: data}}
n.SetRaft(raft.StartNode(n.Cfg, peers))

go func() {
// This is a new cluster. So, propose a new ID for the cluster.
for {
id := uuid.New().String()
err := n.proposeAndWait(context.Background(), &pb.ZeroProposal{Cid: id})
if err == nil {
glog.Infof("CID set for cluster: %v", id)
x.WriteCidFile(id)
break
}
if err == errInvalidProposal {
glog.Errorf("invalid proposal error while proposing cluster id")
return
}
glog.Errorf("While proposing CID: %v. Retrying...", err)
time.Sleep(3 * time.Second)
}

// Apply trial license only if not already licensed.
if n.server.license() == nil {
if err := n.proposeTrialLicense(); err != nil {
glog.Errorf("while proposing trial license to cluster: %v", err)
}
}
}()
go n.proposeNewCID()
}

go n.Run()
Expand Down