func startNode(id uint64, peers []raft.Peer, iface iface) *node { st := raft.NewMemoryStorage() c := &raft.Config{ ID: id, ElectionTick: 10, HeartbeatTick: 1, Storage: st, MaxSizePerMsg: 1024 * 1024, MaxInflightMsgs: 256, } rn := raft.StartNode(c, peers) n := &node{ Node: rn, id: id, storage: st, iface: iface, pausec: make(chan bool), } n.start() return n }
// OpenStorage creates or reloads the disk-backed storage in path. func OpenStorage(node uint64, dir string, stateMachine StateMachine) ( raftStorage *etcdraft.MemoryStorage, diskStorage DiskStorage, lastSnapIdx, lastEntIdx uint64, exists bool, err error) { // TODO(soheil): maybe store and return a custom metadata. glog.V(2).Infof("openning raft storage on %s", dir) sp := path.Join(dir, "snap") wp := path.Join(dir, "wal") exists = exist(sp) && exist(wp) && wal.Exist(wp) s := snap.New(sp) raftStorage = etcdraft.NewMemoryStorage() var w *wal.WAL if !exists { mustMkdir(sp) mustMkdir(wp) w, err = createWAL(node, wp) diskStorage = &storage{w, s} return } ss, err := s.Load() if err != nil && err != snap.ErrNoSnapshot { return } if ss != nil { if err = stateMachine.Restore(ss.Data); err != nil { err = fmt.Errorf("raft: cannot restore statemachine from snapshot: %v", err) return } if err = raftStorage.ApplySnapshot(*ss); err != nil { err = fmt.Errorf("raft: cannot apply snapshot: %v", err) return } lastSnapIdx = ss.Metadata.Index glog.Infof("raft: recovered statemachine from snapshot at index %d", lastSnapIdx) } var st raftpb.HardState var ents []raftpb.Entry w, st, ents, err = readWAL(node, wp, ss) if err != nil { return } raftStorage.SetHardState(st) raftStorage.Append(ents) if len(ents) != 0 { lastEntIdx = ents[len(ents)-1].Index } else if ss != nil { lastEntIdx = ss.Metadata.Index } else { lastEntIdx = 0 } diskStorage = &storage{w, s} return }