2020-08-27 14:18:53 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/go-memdb"
|
2020-08-31 17:19:28 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/stream"
|
2020-09-23 14:52:04 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2020-08-27 14:18:53 +00:00
|
|
|
// ReadTxn is implemented by memdb.Txn to perform read operations.
|
|
|
|
type ReadTxn interface {
|
|
|
|
Get(table, index string, args ...interface{}) (memdb.ResultIterator, error)
|
|
|
|
First(table, index string, args ...interface{}) (interface{}, error)
|
|
|
|
FirstWatch(table, index string, args ...interface{}) (<-chan struct{}, interface{}, error)
|
|
|
|
Abort()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Changes wraps a memdb.Changes to include the index at which these changes
|
|
|
|
// were made.
|
|
|
|
type Changes struct {
|
|
|
|
// Index is the latest index at the time these changes were committed.
|
|
|
|
Index uint64
|
|
|
|
Changes memdb.Changes
|
2020-09-23 14:52:04 +00:00
|
|
|
MsgType structs.MessageType
|
2020-08-27 14:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// changeTrackerDB is a thin wrapper around memdb.DB which enables TrackChanges on
|
|
|
|
// all write transactions. When the transaction is committed the changes are
|
2020-10-08 18:27:52 +00:00
|
|
|
// sent to the EventBroker which will create and emit change events.
|
2020-08-27 14:18:53 +00:00
|
|
|
type changeTrackerDB struct {
|
2020-10-08 18:27:52 +00:00
|
|
|
memdb *memdb.MemDB
|
|
|
|
publisher *stream.EventBroker
|
2020-11-05 21:06:52 +00:00
|
|
|
processChanges changeProcessor
|
2020-08-27 14:18:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 16:21:03 +00:00
|
|
|
func NewChangeTrackerDB(db *memdb.MemDB, publisher *stream.EventBroker, changesFn changeProcessor) *changeTrackerDB {
|
2020-09-04 13:53:36 +00:00
|
|
|
return &changeTrackerDB{
|
2020-10-08 18:27:52 +00:00
|
|
|
memdb: db,
|
2020-08-31 17:19:28 +00:00
|
|
|
publisher: publisher,
|
2020-09-04 13:53:36 +00:00
|
|
|
processChanges: changesFn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:06:52 +00:00
|
|
|
type changeProcessor func(ReadTxn, Changes) *structs.Events
|
2020-09-04 13:53:36 +00:00
|
|
|
|
2020-11-05 21:06:52 +00:00
|
|
|
func noOpProcessChanges(ReadTxn, Changes) *structs.Events { return nil }
|
2020-08-28 12:36:33 +00:00
|
|
|
|
2020-08-27 14:18:53 +00:00
|
|
|
// ReadTxn returns a read-only transaction which behaves exactly the same as
|
|
|
|
// memdb.Txn
|
|
|
|
//
|
|
|
|
// TODO: this could return a regular memdb.Txn if all the state functions accepted
|
|
|
|
// the ReadTxn interface
|
|
|
|
func (c *changeTrackerDB) ReadTxn() *txn {
|
2020-10-08 18:27:52 +00:00
|
|
|
return &txn{Txn: c.memdb.Txn(false)}
|
2020-08-27 14:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTxn returns a wrapped memdb.Txn suitable for writes to the state store.
|
|
|
|
// 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
|
|
|
|
// data directly into the DB. These cases may use WriteTxnRestore.
|
|
|
|
func (c *changeTrackerDB) WriteTxn(idx uint64) *txn {
|
|
|
|
t := &txn{
|
2020-10-08 18:27:52 +00:00
|
|
|
Txn: c.memdb.Txn(true),
|
2020-08-27 14:18:53 +00:00
|
|
|
Index: idx,
|
|
|
|
publish: c.publish,
|
2020-10-19 13:30:15 +00:00
|
|
|
msgType: structs.IgnoreUnknownTypeFlag, // The zero value of structs.MessageType is noderegistration.
|
2020-08-27 14:18:53 +00:00
|
|
|
}
|
|
|
|
t.Txn.TrackChanges()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2020-10-02 20:13:49 +00:00
|
|
|
func (c *changeTrackerDB) WriteTxnMsgT(msgType structs.MessageType, idx uint64) *txn {
|
2020-09-23 14:52:04 +00:00
|
|
|
t := &txn{
|
2020-10-22 16:21:03 +00:00
|
|
|
msgType: msgType,
|
|
|
|
Txn: c.memdb.Txn(true),
|
|
|
|
Index: idx,
|
|
|
|
publish: c.publish,
|
2020-09-23 14:52:04 +00:00
|
|
|
}
|
|
|
|
t.Txn.TrackChanges()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2020-10-06 20:21:58 +00:00
|
|
|
func (c *changeTrackerDB) publish(changes Changes) (*structs.Events, error) {
|
2020-10-08 18:27:52 +00:00
|
|
|
readOnlyTx := c.memdb.Txn(false)
|
2020-08-27 14:18:53 +00:00
|
|
|
defer readOnlyTx.Abort()
|
|
|
|
|
2020-11-05 21:06:52 +00:00
|
|
|
events := c.processChanges(readOnlyTx, changes)
|
2020-10-06 20:21:58 +00:00
|
|
|
if events != nil {
|
|
|
|
c.publisher.Publish(events)
|
|
|
|
}
|
2020-11-05 21:06:52 +00:00
|
|
|
|
2020-10-04 19:12:35 +00:00
|
|
|
return events, nil
|
2020-08-27 14:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTxnRestore returns a wrapped RW transaction that does NOT have change
|
|
|
|
// tracking enabled. This should only be used in Restore where we need to
|
|
|
|
// 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
|
|
|
|
// at one index - the effect is to write many values that were previously
|
|
|
|
// written across many indexes.
|
|
|
|
func (c *changeTrackerDB) WriteTxnRestore() *txn {
|
|
|
|
return &txn{
|
2020-10-08 18:27:52 +00:00
|
|
|
Txn: c.memdb.Txn(true),
|
2020-08-27 14:18:53 +00:00
|
|
|
Index: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
// txn wraps a memdb.Txn to capture changes and send them to the EventBroker.
|
2020-08-27 14:18:53 +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.
|
|
|
|
type txn struct {
|
2020-10-02 20:13:49 +00:00
|
|
|
// msgType is used to inform event sourcing which type of event to create
|
|
|
|
msgType structs.MessageType
|
2020-09-23 14:52:04 +00:00
|
|
|
|
2020-08-27 14:18:53 +00:00
|
|
|
*memdb.Txn
|
|
|
|
// Index in raft where the write is occurring. The value is zero for a
|
|
|
|
// read-only, or WriteTxnRestore transaction.
|
|
|
|
// Index is stored so that it may be passed along to any subscribers as part
|
|
|
|
// of a change event.
|
|
|
|
Index uint64
|
2020-10-06 20:21:58 +00:00
|
|
|
publish func(changes Changes) (*structs.Events, error)
|
2020-08-27 14:18:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
// Commit first pushes changes to EventBroker, then calls Commit on the
|
2020-08-27 14:18:53 +00:00
|
|
|
// underlying transaction.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
func (tx *txn) Commit() error {
|
|
|
|
// publish may be nil if this is a read-only or WriteTxnRestore transaction.
|
|
|
|
// In those cases changes should also be empty, and there will be nothing
|
|
|
|
// to publish.
|
|
|
|
if tx.publish != nil {
|
|
|
|
changes := Changes{
|
|
|
|
Index: tx.Index,
|
|
|
|
Changes: tx.Txn.Changes(),
|
2020-09-23 14:52:04 +00:00
|
|
|
MsgType: tx.MsgType(),
|
2020-08-27 14:18:53 +00:00
|
|
|
}
|
2020-10-22 16:21:03 +00:00
|
|
|
_, err := tx.publish(changes)
|
2020-10-04 19:12:35 +00:00
|
|
|
if err != nil {
|
2020-08-27 14:18:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Txn.Commit()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-23 14:52:04 +00:00
|
|
|
// MsgType returns a MessageType from the txn's context.
|
|
|
|
// If the context is empty or the value isn't set IgnoreUnknownTypeFlag will
|
|
|
|
// be returned to signal that the MsgType is unknown.
|
|
|
|
func (tx *txn) MsgType() structs.MessageType {
|
2020-10-02 20:13:49 +00:00
|
|
|
return tx.msgType
|
2020-09-23 14:52:04 +00:00
|
|
|
}
|