| 1 |
package raftboltdb |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
|
| 6 |
"github.com/boltdb/bolt" |
| 7 |
"github.com/hashicorp/raft" |
| 8 |
) |
| 9 |
|
| 10 |
const ( |
| 11 |
// Permissions to use on the db file. This is only used if the |
| 12 |
// database file does not exist and needs to be created. |
| 13 |
dbFileMode = 0600 |
| 14 |
) |
| 15 |
|
| 16 |
var ( |
| 17 |
// Bucket names we perform transactions in |
| 18 |
dbLogs = []byte("logs") |
| 19 |
dbConf = []byte("conf") |
| 20 |
|
| 21 |
// An error indicating a given key does not exist |
| 22 |
ErrKeyNotFound = errors.New("not found") |
| 23 |
) |
| 24 |
|
| 25 |
// BoltStore provides access to BoltDB for Raft to store and retrieve |
| 26 |
// log entries. It also provides key/value storage, and can be used as |
| 27 |
// a LogStore and StableStore. |
| 28 |
type BoltStore struct { |
| 29 |
// conn is the underlying handle to the db. |
| 30 |
conn *bolt.DB |
| 31 |
|
| 32 |
// The path to the Bolt database file |
| 33 |
path string |
| 34 |
} |
| 35 |
|
| 36 |
// Options contains all the configuraiton used to open the BoltDB |
| 37 |
type Options struct { |
| 38 |
// Path is the file path to the BoltDB to use |
| 39 |
Path string |
| 40 |
|
| 41 |
// BoltOptions contains any specific BoltDB options you might |
| 42 |
// want to specify [e.g. open timeout] |
| 43 |
BoltOptions *bolt.Options |
| 44 |
|
| 45 |
// NoSync causes the database to skip fsync calls after each |
| 46 |
// write to the log. This is unsafe, so it should be used |
| 47 |
// with caution. |
| 48 |
NoSync bool |
| 49 |
} |
| 50 |
|
| 51 |
// readOnly returns true if the contained bolt options say to open |
| 52 |
// the DB in readOnly mode [this can be useful to tools that want |
| 53 |
// to examine the log] |
| 54 |
func (o *Options) readOnly() bool { |
| 55 |
return o != nil && o.BoltOptions != nil && o.BoltOptions.ReadOnly |
| 56 |
} |
| 57 |
|
| 58 |
// NewBoltStore takes a file path and returns a connected Raft backend. |
| 59 |
func NewBoltStore(path string) (*BoltStore, error) { |
| 60 |
return New(Options{Path: path}) |
| 61 |
} |
| 62 |
|
| 63 |
// New uses the supplied options to open the BoltDB and prepare it for use as a raft backend. |
| 64 |
func New(options Options) (*BoltStore, error) { |
| 65 |
// Try to connect |
| 66 |
handle, err := bolt.Open(options.Path, dbFileMode, options.BoltOptions) |
| 67 |
if err != nil { |
| 68 |
return nil, err |
| 69 |
} |
| 70 |
handle.NoSync = options.NoSync |
| 71 |
|
| 72 |
// Create the new store |
| 73 |
store := &BoltStore{ |
| 74 |
conn: handle, |
| 75 |
path: options.Path, |
| 76 |
} |
| 77 |
|
| 78 |
// If the store was opened read-only, don't try and create buckets |
| 79 |
if !options.readOnly() { |
| 80 |
// Set up our buckets |
| 81 |
if err := store.initialize(); err != nil { |
| 82 |
store.Close() |
| 83 |
return nil, err |
| 84 |
} |
| 85 |
} |
| 86 |
return store, nil |
| 87 |
} |
| 88 |
|
| 89 |
// initialize is used to set up all of the buckets. |
| 90 |
func (b *BoltStore) initialize() error { |
| 91 |
tx, err := b.conn.Begin(true) |
| 92 |
if err != nil { |
| 93 |
return err |
| 94 |
} |
| 95 |
defer tx.Rollback() |
| 96 |
|
| 97 |
// Create all the buckets |
| 98 |
if _, err := tx.CreateBucketIfNotExists(dbLogs); err != nil { |
| 99 |
return err |
| 100 |
} |
| 101 |
if _, err := tx.CreateBucketIfNotExists(dbConf); err != nil { |
| 102 |
return err |
| 103 |
} |
| 104 |
|
| 105 |
return tx.Commit() |
| 106 |
} |
| 107 |
|
| 108 |
// Close is used to gracefully close the DB connection. |
| 109 |
func (b *BoltStore) Close() error { |
| 110 |
return b.conn.Close() |
| 111 |
} |
| 112 |
|
| 113 |
// FirstIndex returns the first known index from the Raft log. |
| 114 |
func (b *BoltStore) FirstIndex() (uint64, error) { |
| 115 |
tx, err := b.conn.Begin(false) |
| 116 |
if err != nil { |
| 117 |
return 0, err |
| 118 |
} |
| 119 |
defer tx.Rollback() |
| 120 |
|
| 121 |
curs := tx.Bucket(dbLogs).Cursor() |
| 122 |
if first, _ := curs.First(); first == nil { |
| 123 |
return 0, nil |
| 124 |
} else { |
| 125 |
return bytesToUint64(first), nil |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
// LastIndex returns the last known index from the Raft log. |
| 130 |
func (b *BoltStore) LastIndex() (uint64, error) { |
| 131 |
tx, err := b.conn.Begin(false) |
| 132 |
if err != nil { |
| 133 |
return 0, err |
| 134 |
} |
| 135 |
defer tx.Rollback() |
| 136 |
|
| 137 |
curs := tx.Bucket(dbLogs).Cursor() |
| 138 |
if last, _ := curs.Last(); last == nil { |
| 139 |
return 0, nil |
| 140 |
} else { |
| 141 |
return bytesToUint64(last), nil |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// GetLog is used to retrieve a log from BoltDB at a given index. |
| 146 |
func (b *BoltStore) GetLog(idx uint64, log *raft.Log) error { |
| 147 |
tx, err := b.conn.Begin(false) |
| 148 |
if err != nil { |
| 149 |
return err |
| 150 |
} |
| 151 |
defer tx.Rollback() |
| 152 |
|
| 153 |
bucket := tx.Bucket(dbLogs) |
| 154 |
val := bucket.Get(uint64ToBytes(idx)) |
| 155 |
|
| 156 |
if val == nil { |
| 157 |
return raft.ErrLogNotFound |
| 158 |
} |
| 159 |
return decodeMsgPack(val, log) |
| 160 |
} |
| 161 |
|
| 162 |
// StoreLog is used to store a single raft log |
| 163 |
func (b *BoltStore) StoreLog(log *raft.Log) error { |
| 164 |
return b.StoreLogs([]*raft.Log{log}) |
| 165 |
} |
| 166 |
|
| 167 |
// StoreLogs is used to store a set of raft logs |
| 168 |
func (b *BoltStore) StoreLogs(logs []*raft.Log) error { |
| 169 |
tx, err := b.conn.Begin(true) |
| 170 |
if err != nil { |
| 171 |
return err |
| 172 |
} |
| 173 |
defer tx.Rollback() |
| 174 |
|
| 175 |
for _, log := range logs { |
| 176 |
key := uint64ToBytes(log.Index) |
| 177 |
val, err := encodeMsgPack(log) |
| 178 |
if err != nil { |
| 179 |
return err |
| 180 |
} |
| 181 |
bucket := tx.Bucket(dbLogs) |
| 182 |
if err := bucket.Put(key, val.Bytes()); err != nil { |
| 183 |
return err |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return tx.Commit() |
| 188 |
} |
| 189 |
|
| 190 |
// DeleteRange is used to delete logs within a given range inclusively. |
| 191 |
func (b *BoltStore) DeleteRange(min, max uint64) error { |
| 192 |
minKey := uint64ToBytes(min) |
| 193 |
|
| 194 |
tx, err := b.conn.Begin(true) |
| 195 |
if err != nil { |
| 196 |
return err |
| 197 |
} |
| 198 |
defer tx.Rollback() |
| 199 |
|
| 200 |
curs := tx.Bucket(dbLogs).Cursor() |
| 201 |
for k, _ := curs.Seek(minKey); k != nil; k, _ = curs.Next() { |
| 202 |
// Handle out-of-range log index |
| 203 |
if bytesToUint64(k) > max { |
| 204 |
break |
| 205 |
} |
| 206 |
|
| 207 |
// Delete in-range log index |
| 208 |
if err := curs.Delete(); err != nil { |
| 209 |
return err |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
return tx.Commit() |
| 214 |
} |
| 215 |
|
| 216 |
// Set is used to set a key/value set outside of the raft log |
| 217 |
func (b *BoltStore) Set(k, v []byte) error { |
| 218 |
tx, err := b.conn.Begin(true) |
| 219 |
if err != nil { |
| 220 |
return err |
| 221 |
} |
| 222 |
defer tx.Rollback() |
| 223 |
|
| 224 |
bucket := tx.Bucket(dbConf) |
| 225 |
if err := bucket.Put(k, v); err != nil { |
| 226 |
return err |
| 227 |
} |
| 228 |
|
| 229 |
return tx.Commit() |
| 230 |
} |
| 231 |
|
| 232 |
// Get is used to retrieve a value from the k/v store by key |
| 233 |
func (b *BoltStore) Get(k []byte) ([]byte, error) { |
| 234 |
tx, err := b.conn.Begin(false) |
| 235 |
if err != nil { |
| 236 |
return nil, err |
| 237 |
} |
| 238 |
defer tx.Rollback() |
| 239 |
|
| 240 |
bucket := tx.Bucket(dbConf) |
| 241 |
val := bucket.Get(k) |
| 242 |
|
| 243 |
if val == nil { |
| 244 |
return nil, ErrKeyNotFound |
| 245 |
} |
| 246 |
return append([]byte(nil), val...), nil |
| 247 |
} |
| 248 |
|
| 249 |
// SetUint64 is like Set, but handles uint64 values |
| 250 |
func (b *BoltStore) SetUint64(key []byte, val uint64) error { |
| 251 |
return b.Set(key, uint64ToBytes(val)) |
| 252 |
} |
| 253 |
|
| 254 |
// GetUint64 is like Get, but handles uint64 values |
| 255 |
func (b *BoltStore) GetUint64(key []byte) (uint64, error) { |
| 256 |
val, err := b.Get(key) |
| 257 |
if err != nil { |
| 258 |
return 0, err |
| 259 |
} |
| 260 |
return bytesToUint64(val), nil |
| 261 |
} |
| 262 |
|
| 263 |
// Sync performs an fsync on the database file handle. This is not necessary |
| 264 |
// under normal operation unless NoSync is enabled, in which this forces the |
| 265 |
// database file to sync against the disk. |
| 266 |
func (b *BoltStore) Sync() error { |
| 267 |
return b.conn.Sync() |
| 268 |
} |