github.com/baptiste-b-pegasys/quorum/v22@v22.4.2/raft/persistence.go (about)

     1  package raft
     2  
     3  import (
     4  	"encoding/binary"
     5  
     6  	"github.com/ethereum/go-ethereum/log"
     7  
     8  	"github.com/syndtr/goleveldb/leveldb"
     9  	"github.com/syndtr/goleveldb/leveldb/errors"
    10  	"github.com/syndtr/goleveldb/leveldb/opt"
    11  )
    12  
    13  var (
    14  	noFsync = &opt.WriteOptions{
    15  		NoWriteMerge: false,
    16  		Sync:         false,
    17  	}
    18  )
    19  
    20  func openQuorumRaftDb(path string) (db *leveldb.DB, err error) {
    21  	// Open the db and recover any potential corruptions
    22  	db, err = leveldb.OpenFile(path, &opt.Options{
    23  		OpenFilesCacheCapacity: -1, // -1 means 0??
    24  		BlockCacheCapacity:     -1,
    25  	})
    26  	if _, corrupted := err.(*errors.ErrCorrupted); corrupted {
    27  		db, err = leveldb.RecoverFile(path, nil)
    28  	}
    29  	return
    30  }
    31  
    32  func (pm *ProtocolManager) loadAppliedIndex() uint64 {
    33  	dat, err := pm.quorumRaftDb.Get(appliedDbKey, nil)
    34  	var lastAppliedIndex uint64
    35  	if err == errors.ErrNotFound {
    36  		lastAppliedIndex = 0
    37  	} else if err != nil {
    38  		fatalf("loadAppliedIndex error: %s", err)
    39  	} else {
    40  		lastAppliedIndex = binary.LittleEndian.Uint64(dat)
    41  	}
    42  
    43  	pm.mu.Lock()
    44  	pm.appliedIndex = lastAppliedIndex
    45  	pm.mu.Unlock()
    46  
    47  	log.Info("loaded the latest applied index", "lastAppliedIndex", lastAppliedIndex)
    48  
    49  	return lastAppliedIndex
    50  }
    51  
    52  func (pm *ProtocolManager) writeAppliedIndex(index uint64) {
    53  	log.Info("persisted the latest applied index", "index", index)
    54  	buf := make([]byte, 8)
    55  	binary.LittleEndian.PutUint64(buf, index)
    56  	pm.quorumRaftDb.Put(appliedDbKey, buf, noFsync)
    57  }