2020-03-19 13:11:20 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
)
|
|
|
|
|
2020-06-03 16:59:10 +00:00
|
|
|
// changeTrackerDB is a thin wrapper around memdb.DB which enables TrackChanges on
|
2020-06-02 20:05:08 +00:00
|
|
|
// all write transactions. When the transaction is committed the changes are
|
|
|
|
// sent to the eventPublisher which will create and emit change events.
|
2020-06-03 16:59:10 +00:00
|
|
|
type changeTrackerDB struct {
|
2020-06-02 20:05:08 +00:00
|
|
|
db *memdb.MemDB
|
2020-06-03 16:59:10 +00:00
|
|
|
// TODO(streaming): add publisher
|
2020-03-19 13:11:20 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 20:05:08 +00:00
|
|
|
// Txn exists to maintain backwards compatibility with memdb.DB.Txn. Preexisting
|
|
|
|
// code may use it to create a read-only transaction, but it will panic if called
|
|
|
|
// with write=true.
|
|
|
|
//
|
|
|
|
// Deprecated: use either ReadTxn, or WriteTxn.
|
2020-06-03 17:21:00 +00:00
|
|
|
func (db *changeTrackerDB) Txn(write bool) *txn {
|
2020-03-19 13:11:20 +00:00
|
|
|
if write {
|
|
|
|
panic("don't use db.Txn(true), use db.WriteTxn(idx uin64)")
|
|
|
|
}
|
2020-06-02 20:05:08 +00:00
|
|
|
return db.ReadTxn()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadTxn returns a read-only transaction which behaves exactly the same as
|
|
|
|
// memdb.Txn
|
2020-06-03 17:21:00 +00:00
|
|
|
func (db *changeTrackerDB) ReadTxn() *txn {
|
|
|
|
return &txn{Txn: db.db.Txn(false)}
|
2020-03-19 13:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTxn returns a wrapped memdb.Txn suitable for writes to the state store.
|
2020-06-02 20:05:08 +00:00
|
|
|
// It will track changes and publish events for the changes when Commit
|
|
|
|
// is called.
|
|
|
|
//
|
|
|
|
// The idx argument must be the index of the current Raft operation. Almost
|
|
|
|
// all mutations to state should happen as part of a raft apply so the index of
|
|
|
|
// the log being applied can be passed to WriteTxn.
|
|
|
|
// The exceptional cases are transactions that are executed on an empty
|
|
|
|
// memdb.DB as part of Restore, and those executed by tests where we insert
|
2020-03-19 13:11:20 +00:00
|
|
|
// data directly into the DB. These cases may use WriteTxnRestore.
|
2020-06-03 17:21:00 +00:00
|
|
|
func (db *changeTrackerDB) WriteTxn(idx uint64) *txn {
|
|
|
|
t := &txn{
|
2020-06-02 20:05:08 +00:00
|
|
|
Txn: db.db.Txn(true),
|
2020-03-19 13:11:20 +00:00
|
|
|
Index: idx,
|
|
|
|
}
|
|
|
|
t.Txn.TrackChanges()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTxnRestore returns a wrapped RW transaction that does NOT have change
|
|
|
|
// tracking enabled. This should only be used in Restore where we need to
|
2020-06-02 20:05:08 +00:00
|
|
|
// replace the entire contents of the Store without a need to track the changes.
|
|
|
|
// WriteTxnRestore uses a zero index since the whole restore doesn't really occur
|
2020-03-19 13:11:20 +00:00
|
|
|
// at one index - the effect is to write many values that were previously
|
|
|
|
// written across many indexes.
|
2020-06-03 17:21:00 +00:00
|
|
|
func (db *changeTrackerDB) WriteTxnRestore() *txn {
|
|
|
|
t := &txn{
|
2020-06-02 20:05:08 +00:00
|
|
|
Txn: db.db.Txn(true),
|
2020-03-19 13:11:20 +00:00
|
|
|
Index: 0,
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2020-06-03 17:21:00 +00:00
|
|
|
// txn wraps a memdb.Txn to capture changes and send them to the EventPublisher.
|
2020-06-02 20:05:08 +00:00
|
|
|
//
|
|
|
|
// This can not be done with txn.Defer because the callback passed to Defer is
|
|
|
|
// invoked after commit completes, and because the callback can not return an
|
|
|
|
// error. Any errors from the callback would be lost, which would result in a
|
|
|
|
// missing change event, even though the state store had changed.
|
2020-06-03 17:21:00 +00:00
|
|
|
type txn struct {
|
2020-06-02 20:05:08 +00:00
|
|
|
// Index in raft where the write is occurring. The value is zero for a
|
|
|
|
// read-only transaction, and for a WriteTxnRestore transaction.
|
|
|
|
// Index is stored so that it may be passed along to any subscribers as part
|
|
|
|
// of a change event.
|
2020-03-19 13:11:20 +00:00
|
|
|
Index uint64
|
|
|
|
*memdb.Txn
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:05:08 +00:00
|
|
|
// Commit first pushes changes to EventPublisher, then calls Commit on the
|
|
|
|
// underlying transaction.
|
2020-03-19 13:11:20 +00:00
|
|
|
//
|
2020-06-02 20:05:08 +00:00
|
|
|
// Note that this function, unlike memdb.Txn, returns an error which must be checked
|
|
|
|
// by the caller. A non-nil error indicates that a commit failed and was not
|
|
|
|
// applied.
|
2020-06-03 17:21:00 +00:00
|
|
|
func (tx *txn) Commit() error {
|
2020-06-02 20:05:08 +00:00
|
|
|
// changes may be empty if this is a read-only or WriteTxnRestore transaction.
|
2020-06-03 17:21:00 +00:00
|
|
|
// TODO(streaming): publish changes: changes := tx.Txn.Changes()
|
2020-03-19 13:11:20 +00:00
|
|
|
|
|
|
|
tx.Txn.Commit()
|
|
|
|
return nil
|
|
|
|
}
|