github.com/Night-mk/quorum@v21.1.0+incompatible/raft/wal.go (about)

     1  package raft
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/coreos/etcd/raft/raftpb"
     7  	"github.com/coreos/etcd/wal"
     8  	"github.com/coreos/etcd/wal/walpb"
     9  	"github.com/ethereum/go-ethereum/log"
    10  )
    11  
    12  func (pm *ProtocolManager) openWAL(maybeRaftSnapshot *raftpb.Snapshot) *wal.WAL {
    13  	if !wal.Exist(pm.waldir) {
    14  		if err := os.Mkdir(pm.waldir, 0750); err != nil {
    15  			fatalf("cannot create waldir: %s", err)
    16  		}
    17  
    18  		wal, err := wal.Create(pm.waldir, nil)
    19  		if err != nil {
    20  			fatalf("failed to create waldir: %s", err)
    21  		}
    22  		wal.Close()
    23  	}
    24  
    25  	walsnap := walpb.Snapshot{}
    26  
    27  	log.Info("loading WAL", "term", walsnap.Term, "index", walsnap.Index)
    28  
    29  	if maybeRaftSnapshot != nil {
    30  		walsnap.Index, walsnap.Term = maybeRaftSnapshot.Metadata.Index, maybeRaftSnapshot.Metadata.Term
    31  	}
    32  
    33  	wal, err := wal.Open(pm.waldir, walsnap)
    34  	if err != nil {
    35  		fatalf("error loading WAL: %s", err)
    36  	}
    37  
    38  	return wal
    39  }
    40  
    41  func (pm *ProtocolManager) replayWAL(maybeRaftSnapshot *raftpb.Snapshot) (*wal.WAL, []raftpb.Entry) {
    42  	log.Info("replaying WAL")
    43  	wal := pm.openWAL(maybeRaftSnapshot)
    44  
    45  	_, hardState, entries, err := wal.ReadAll()
    46  	if err != nil {
    47  		fatalf("failed to read WAL: %s", err)
    48  	}
    49  
    50  	pm.raftStorage.SetHardState(hardState)
    51  	pm.raftStorage.Append(entries)
    52  
    53  	return wal, entries
    54  }