MCPcopy
hub / github.com/dgraph-io/dgraph / initAndStartNode

Method initAndStartNode

dgraph/cmd/zero/raft.go:632–711  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

630}
631
632func (n *node) initAndStartNode() error {
633 x.Check(n.initProposalKey(n.Id))
634 _, restart, err := n.PastLife()
635 x.Check(err)
636
637 switch {
638 case restart:
639 glog.Infoln("Restarting node for dgraphzero")
640 sp, err := n.Store.Snapshot()
641 x.Checkf(err, "Unable to get existing snapshot")
642 if !raft.IsEmptySnap(sp) {
643 // It is important that we pick up the conf state here.
644 n.SetConfState(&sp.Metadata.ConfState)
645
646 var zs pb.ZeroSnapshot
647 x.Check(proto.Unmarshal(sp.Data, &zs))
648 n.server.SetMembershipState(zs.State)
649 for _, id := range sp.Metadata.ConfState.Voters {
650 n.Connect(id, zs.State.Zeros[id].Addr)
651 }
652 }
653
654 n.SetRaft(raft.RestartNode(n.Cfg))
655 foundCID, err := n.checkForCIDInEntries()
656 if err != nil {
657 return err
658 }
659 if !foundCID {
660 go n.proposeNewCID()
661 }
662
663 case len(opts.peer) > 0:
664 p := conn.GetPools().Connect(opts.peer, opts.tlsClientConfig)
665 if p == nil {
666 return errors.Errorf("Unhealthy connection to %v", opts.peer)
667 }
668
669 timeout := 8 * time.Second
670 for {
671 c := pb.NewRaftClient(p.Get())
672 ctx, cancel := context.WithTimeout(n.ctx, timeout)
673 // JoinCluster can block indefinitely, raft ignores conf change proposal
674 // if it has pending configuration.
675 _, err := c.JoinCluster(ctx, n.RaftContext)
676 if err == nil {
677 cancel()
678 break
679 }
680 if x.ShouldCrash(err) {
681 cancel()
682 log.Fatalf("Error while joining cluster: %v", err)
683 }
684 glog.Errorf("Error while joining cluster: %v\n", err)
685 timeout *= 2
686 if timeout > 32*time.Second {
687 timeout = 32 * time.Second
688 }
689 time.Sleep(timeout) // This is useful because JoinCluster can exit immediately.

Callers 1

runFunction · 0.80

Calls 15

initProposalKeyMethod · 0.95
checkForCIDInEntriesMethod · 0.95
proposeNewCIDMethod · 0.95
JoinClusterMethod · 0.95
RunMethod · 0.95
CheckFunction · 0.92
CheckfFunction · 0.92
GetPoolsFunction · 0.92
NewRaftClientFunction · 0.92
ShouldCrashFunction · 0.92
PastLifeMethod · 0.80
SetConfStateMethod · 0.80

Tested by

no test coverage detected