You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
package log
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/raft"
|
|
"github.com/hashicorp/raft-boltdb/v2"
|
|
"go.etcd.io/bbolt"
|
|
)
|
|
|
|
// Log is an object that can return information about the Raft log.
|
|
type Log struct {
|
|
*raftboltdb.BoltStore
|
|
}
|
|
|
|
// New returns an instantiated Log object.
|
|
func New(path string, noFreelistSync bool) (*Log, error) {
|
|
bs, err := raftboltdb.New(raftboltdb.Options{
|
|
BoltOptions: &bbolt.Options{
|
|
NoFreelistSync: noFreelistSync,
|
|
},
|
|
Path: path,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("new bbolt store: %s", err)
|
|
}
|
|
return &Log{bs}, nil
|
|
}
|
|
|
|
// Indexes returns the first and last indexes.
|
|
func (l *Log) Indexes() (uint64, uint64, error) {
|
|
fi, err := l.FirstIndex()
|
|
if err != nil {
|
|
return 0, 0, fmt.Errorf("failed to get first index: %s", err)
|
|
}
|
|
li, err := l.LastIndex()
|
|
if err != nil {
|
|
return 0, 0, fmt.Errorf("failed to get last index: %s", err)
|
|
}
|
|
return fi, li, nil
|
|
}
|
|
|
|
// LastCommandIndex returns the index of the last Command
|
|
// log entry written to the Raft log. Returns an index of
|
|
// zero if no such log exists.
|
|
func (l *Log) LastCommandIndex() (uint64, error) {
|
|
fi, li, err := l.Indexes()
|
|
if err != nil {
|
|
return 0, fmt.Errorf("get indexes: %s", err)
|
|
}
|
|
|
|
// Check for empty log.
|
|
if li == 0 {
|
|
return 0, nil
|
|
}
|
|
|
|
var rl raft.Log
|
|
for i := li; i >= fi; i-- {
|
|
if err := l.GetLog(i, &rl); err != nil {
|
|
return 0, fmt.Errorf("get log at index %d: %s", i, err)
|
|
}
|
|
if rl.Type == raft.LogCommand {
|
|
return i, nil
|
|
}
|
|
}
|
|
return 0, nil
|
|
}
|
|
|
|
// Stats returns stats about the BBoltDB database.
|
|
func (l *Log) Stats() bbolt.Stats {
|
|
return l.BoltStore.Stats()
|
|
}
|