()
| 630 | } |
| 631 | |
| 632 | func (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. |
no test coverage detected