2016-05-02 23:21:04 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-memdb"
|
2021-01-30 00:24:41 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2021-01-30 00:24:41 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2016-05-02 23:21:04 +00:00
|
|
|
)
|
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
const (
|
|
|
|
tableKVs = "kvs"
|
|
|
|
tableTombstones = "tombstones"
|
|
|
|
|
|
|
|
indexSession = "session"
|
|
|
|
)
|
|
|
|
|
|
|
|
// kvsTableSchema returns a new table schema used for storing structs.DirEntry
|
2017-11-29 01:03:34 +00:00
|
|
|
func kvsTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
2021-11-08 14:35:56 +00:00
|
|
|
Name: tableKVs,
|
2017-11-29 01:03:34 +00:00
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
2021-11-08 14:35:56 +00:00
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
2017-11-29 01:03:34 +00:00
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
2019-11-25 17:57:35 +00:00
|
|
|
Indexer: kvsIndexer(),
|
2017-11-29 01:03:34 +00:00
|
|
|
},
|
2021-11-08 14:35:56 +00:00
|
|
|
indexSession: {
|
|
|
|
Name: indexSession,
|
2017-11-29 01:03:34 +00:00
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &memdb.UUIDFieldIndex{
|
|
|
|
Field: "Session",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 21:20:50 +00:00
|
|
|
// indexFromIDValue creates an index key from any struct that implements singleValueID
|
|
|
|
func indexFromIDValue(raw interface{}) ([]byte, error) {
|
2021-11-08 14:35:56 +00:00
|
|
|
e, ok := raw.(singleValueID)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T, does not implement singleValueID", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
v := e.IDValue()
|
|
|
|
if v == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(v)
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2017-11-29 01:03:34 +00:00
|
|
|
// tombstonesTableSchema returns a new table schema used for storing tombstones
|
|
|
|
// during KV delete operations to prevent the index from sliding backwards.
|
|
|
|
func tombstonesTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
2021-11-08 14:35:56 +00:00
|
|
|
Name: tableTombstones,
|
2017-11-29 01:03:34 +00:00
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
2021-11-08 14:35:56 +00:00
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
2017-11-29 01:03:34 +00:00
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
2019-11-25 17:57:35 +00:00
|
|
|
Indexer: kvsIndexer(),
|
2017-11-29 01:03:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 23:21:04 +00:00
|
|
|
// KVs is used to pull the full list of KVS entries for use during snapshots.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Snapshot) KVs() (memdb.ResultIterator, error) {
|
2021-11-08 14:35:56 +00:00
|
|
|
return s.tx.Get(tableKVs, indexID+"_prefix")
|
2016-05-02 23:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tombstones is used to pull all the tombstones from the graveyard.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Snapshot) Tombstones() (memdb.ResultIterator, error) {
|
2016-05-02 23:21:04 +00:00
|
|
|
return s.store.kvsGraveyard.DumpTxn(s.tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// KVS is used when restoring from a snapshot. Use KVSSet for general inserts.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Restore) KVS(entry *structs.DirEntry) error {
|
2020-11-11 16:21:42 +00:00
|
|
|
if err := insertKVTxn(s.tx, entry, true, true); err != nil {
|
2016-05-02 23:21:04 +00:00
|
|
|
return fmt.Errorf("failed inserting kvs entry: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tombstone is used when restoring from a snapshot. For general inserts, use
|
|
|
|
// Graveyard.InsertTxn.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Restore) Tombstone(stone *Tombstone) error {
|
2016-05-02 23:21:04 +00:00
|
|
|
if err := s.store.kvsGraveyard.RestoreTxn(s.tx, stone); err != nil {
|
|
|
|
return fmt.Errorf("failed restoring tombstone: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReapTombstones is used to delete all the tombstones with an index
|
|
|
|
// less than or equal to the given index. This is used to prevent
|
|
|
|
// unbounded storage growth of the tombstones.
|
2020-03-19 13:11:20 +00:00
|
|
|
func (s *Store) ReapTombstones(idx uint64, index uint64) error {
|
|
|
|
tx := s.db.WriteTxn(idx)
|
2016-05-02 23:21:04 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
if err := s.kvsGraveyard.ReapTxn(tx, index); err != nil {
|
|
|
|
return fmt.Errorf("failed to reap kvs tombstones: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2016-05-02 23:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// KVSSet is used to store a key/value pair.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Store) KVSSet(idx uint64, entry *structs.DirEntry) error {
|
2021-11-08 14:35:56 +00:00
|
|
|
entry.EnterpriseMeta.Normalize()
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2016-05-02 23:21:04 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Perform the actual set.
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := kvsSetTxn(tx, idx, entry, false); err != nil {
|
2016-05-02 23:21:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2016-05-02 23:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// kvsSetTxn is used to insert or update a key/value pair in the state
|
|
|
|
// store. It is the inner method used and handles only the actual storage.
|
|
|
|
// If updateSession is true, then the incoming entry will set the new
|
|
|
|
// session (should be validated before calling this). Otherwise, we will keep
|
|
|
|
// whatever the existing session is.
|
2020-09-03 23:38:03 +00:00
|
|
|
func kvsSetTxn(tx WriteTxn, idx uint64, entry *structs.DirEntry, updateSession bool) error {
|
2021-11-08 14:35:56 +00:00
|
|
|
existingNode, err := tx.First(tableKVs, indexID, entry)
|
2016-05-02 23:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed kvs lookup: %s", err)
|
|
|
|
}
|
2019-06-18 13:06:29 +00:00
|
|
|
existing, _ := existingNode.(*structs.DirEntry)
|
2016-05-02 23:21:04 +00:00
|
|
|
|
2020-05-14 19:25:04 +00:00
|
|
|
// Set the CreateIndex.
|
2016-05-02 23:21:04 +00:00
|
|
|
if existing != nil {
|
2019-06-18 13:06:29 +00:00
|
|
|
entry.CreateIndex = existing.CreateIndex
|
2016-05-02 23:21:04 +00:00
|
|
|
} else {
|
|
|
|
entry.CreateIndex = idx
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preserve the existing session unless told otherwise. The "existing"
|
|
|
|
// session for a new entry is "no session".
|
|
|
|
if !updateSession {
|
|
|
|
if existing != nil {
|
2019-06-18 13:06:29 +00:00
|
|
|
entry.Session = existing.Session
|
2016-05-02 23:21:04 +00:00
|
|
|
} else {
|
|
|
|
entry.Session = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 19:25:04 +00:00
|
|
|
// Set the ModifyIndex.
|
2019-06-18 13:06:29 +00:00
|
|
|
if existing != nil && existing.Equal(entry) {
|
2020-05-14 19:25:04 +00:00
|
|
|
// Skip further writing in the state store if the entry is not actually
|
|
|
|
// changed. Nevertheless, the input's ModifyIndex should be reset
|
|
|
|
// since the TXN API returns a copy in the response.
|
|
|
|
entry.ModifyIndex = existing.ModifyIndex
|
2019-06-18 13:06:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-05-14 19:25:04 +00:00
|
|
|
entry.ModifyIndex = idx
|
2019-06-18 13:06:29 +00:00
|
|
|
|
2016-05-02 23:21:04 +00:00
|
|
|
// Store the kv pair in the state store and update the index.
|
2020-11-11 16:21:42 +00:00
|
|
|
if err := insertKVTxn(tx, entry, false, false); err != nil {
|
2016-05-02 23:21:04 +00:00
|
|
|
return fmt.Errorf("failed inserting kvs entry: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// KVSGet is used to retrieve a key/value pair from the state store.
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) KVSGet(ws memdb.WatchSet, key string, entMeta *acl.EnterpriseMeta) (uint64, *structs.DirEntry, error) {
|
2016-05-02 23:21:04 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
// TODO: accept non-pointer entMeta
|
|
|
|
if entMeta == nil {
|
|
|
|
entMeta = structs.DefaultEnterpriseMetaInDefaultPartition()
|
|
|
|
}
|
|
|
|
|
|
|
|
return kvsGetTxn(tx, ws, key, *entMeta)
|
2016-05-04 21:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// kvsGetTxn is the inner method that gets a KVS entry inside an existing
|
|
|
|
// transaction.
|
2020-09-03 23:38:03 +00:00
|
|
|
func kvsGetTxn(tx ReadTxn,
|
2022-04-05 21:10:06 +00:00
|
|
|
ws memdb.WatchSet, key string, entMeta acl.EnterpriseMeta) (uint64, *structs.DirEntry, error) {
|
2019-11-25 17:57:35 +00:00
|
|
|
|
2016-05-02 23:21:04 +00:00
|
|
|
// Get the table index.
|
2019-11-25 17:57:35 +00:00
|
|
|
idx := kvsMaxIndex(tx, entMeta)
|
2016-05-02 23:21:04 +00:00
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
watchCh, entry, err := tx.FirstWatch(tableKVs, indexID, Query{Value: key, EnterpriseMeta: entMeta})
|
2016-05-02 23:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed kvs lookup: %s", err)
|
|
|
|
}
|
2017-01-24 19:20:51 +00:00
|
|
|
ws.Add(watchCh)
|
2016-05-02 23:21:04 +00:00
|
|
|
if entry != nil {
|
|
|
|
return idx, entry.(*structs.DirEntry), nil
|
|
|
|
}
|
|
|
|
return idx, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// KVSList is used to list out all keys under a given prefix. If the
|
|
|
|
// prefix is left empty, all keys in the KVS will be returned. The returned
|
|
|
|
// is the max index of the returned kvs entries or applicable tombstones, or
|
|
|
|
// else it's the full table indexes for kvs and tombstones.
|
2019-11-25 17:57:35 +00:00
|
|
|
func (s *Store) KVSList(ws memdb.WatchSet,
|
2022-04-05 21:10:06 +00:00
|
|
|
prefix string, entMeta *acl.EnterpriseMeta) (uint64, structs.DirEntries, error) {
|
2019-11-25 17:57:35 +00:00
|
|
|
|
2016-05-02 23:21:04 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
// TODO: accept non-pointer entMeta
|
|
|
|
if entMeta == nil {
|
|
|
|
entMeta = structs.DefaultEnterpriseMetaInDefaultPartition()
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.kvsListTxn(tx, ws, prefix, *entMeta)
|
2016-05-13 23:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// kvsListTxn is the inner method that gets a list of KVS entries matching a
|
|
|
|
// prefix.
|
2020-09-03 23:38:03 +00:00
|
|
|
func (s *Store) kvsListTxn(tx ReadTxn,
|
2022-04-05 21:10:06 +00:00
|
|
|
ws memdb.WatchSet, prefix string, entMeta acl.EnterpriseMeta) (uint64, structs.DirEntries, error) {
|
2016-05-02 23:21:04 +00:00
|
|
|
|
|
|
|
// Get the table indexes.
|
2019-11-25 17:57:35 +00:00
|
|
|
idx := kvsMaxIndex(tx, entMeta)
|
2016-05-02 23:21:04 +00:00
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
lindex, entries, err := kvsListEntriesTxn(tx, ws, prefix, entMeta)
|
2016-05-02 23:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed kvs lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the highest index in the graveyard. If the prefix is empty
|
|
|
|
// then just use the full table indexes since we are listing everything.
|
|
|
|
if prefix != "" {
|
2021-11-08 14:35:56 +00:00
|
|
|
gindex, err := s.kvsGraveyard.GetMaxIndexTxn(tx, prefix, &entMeta)
|
2016-05-02 23:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed graveyard lookup: %s", err)
|
|
|
|
}
|
|
|
|
if gindex > lindex {
|
|
|
|
lindex = gindex
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lindex = idx
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the sub index if it was set and there are entries, otherwise use
|
|
|
|
// the full table index from above.
|
|
|
|
if lindex != 0 {
|
|
|
|
idx = lindex
|
|
|
|
}
|
2019-11-25 17:57:35 +00:00
|
|
|
return idx, entries, nil
|
2016-05-02 23:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// KVSDelete is used to perform a shallow delete on a single key in the
|
|
|
|
// the state store.
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) KVSDelete(idx uint64, key string, entMeta *acl.EnterpriseMeta) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2016-05-02 23:21:04 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Perform the actual delete
|
2019-11-25 17:57:35 +00:00
|
|
|
if err := s.kvsDeleteTxn(tx, idx, key, entMeta); err != nil {
|
2016-05-02 23:21:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2016-05-02 23:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// kvsDeleteTxn is the inner method used to perform the actual deletion
|
|
|
|
// of a key/value pair within an existing transaction.
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) kvsDeleteTxn(tx WriteTxn, idx uint64, key string, entMeta *acl.EnterpriseMeta) error {
|
2021-11-08 14:35:56 +00:00
|
|
|
|
|
|
|
if entMeta == nil {
|
|
|
|
entMeta = structs.DefaultEnterpriseMetaInDefaultPartition()
|
|
|
|
}
|
|
|
|
|
2016-05-02 23:21:04 +00:00
|
|
|
// Look up the entry in the state store.
|
2021-11-08 14:35:56 +00:00
|
|
|
entry, err := tx.First(tableKVs, indexID, Query{Value: key, EnterpriseMeta: *entMeta})
|
2016-05-02 23:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed kvs lookup: %s", err)
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a tombstone.
|
2019-11-25 17:57:35 +00:00
|
|
|
if err := s.kvsGraveyard.InsertTxn(tx, key, idx, entMeta); err != nil {
|
2016-05-02 23:21:04 +00:00
|
|
|
return fmt.Errorf("failed adding to graveyard: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
return kvsDeleteWithEntry(tx, entry.(*structs.DirEntry), idx)
|
2016-05-02 23:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// KVSDeleteCAS is used to try doing a KV delete operation with a given
|
|
|
|
// raft index. If the CAS index specified is not equal to the last
|
|
|
|
// observed index for the given key, then the call is a noop, otherwise
|
|
|
|
// a normal KV delete is invoked.
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) KVSDeleteCAS(idx, cidx uint64, key string, entMeta *acl.EnterpriseMeta) (bool, error) {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2016-05-02 23:21:04 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2019-11-25 17:57:35 +00:00
|
|
|
set, err := s.kvsDeleteCASTxn(tx, idx, cidx, key, entMeta)
|
2016-05-04 21:20:11 +00:00
|
|
|
if !set || err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
err = tx.Commit()
|
|
|
|
return err == nil, err
|
2016-05-04 21:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// kvsDeleteCASTxn is the inner method that does a CAS delete within an existing
|
|
|
|
// transaction.
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) kvsDeleteCASTxn(tx WriteTxn, idx, cidx uint64, key string, entMeta *acl.EnterpriseMeta) (bool, error) {
|
2021-11-08 14:35:56 +00:00
|
|
|
if entMeta == nil {
|
|
|
|
entMeta = structs.DefaultEnterpriseMetaInDefaultPartition()
|
|
|
|
}
|
|
|
|
entry, err := tx.First(tableKVs, indexID, Query{Value: key, EnterpriseMeta: *entMeta})
|
2016-05-02 23:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed kvs lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the existing index does not match the provided CAS
|
|
|
|
// index arg, then we shouldn't update anything and can safely
|
|
|
|
// return early here.
|
|
|
|
e, ok := entry.(*structs.DirEntry)
|
|
|
|
if !ok || e.ModifyIndex != cidx {
|
|
|
|
return entry == nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the actual deletion if the above passed.
|
2019-11-25 17:57:35 +00:00
|
|
|
if err := s.kvsDeleteTxn(tx, idx, key, entMeta); err != nil {
|
2016-05-02 23:21:04 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// KVSSetCAS is used to do a check-and-set operation on a KV entry. The
|
|
|
|
// ModifyIndex in the provided entry is used to determine if we should
|
|
|
|
// write the entry to the state store or bail. Returns a bool indicating
|
|
|
|
// if a write happened and any error.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Store) KVSSetCAS(idx uint64, entry *structs.DirEntry) (bool, error) {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2016-05-02 23:21:04 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
set, err := kvsSetCASTxn(tx, idx, entry)
|
2016-05-04 21:20:11 +00:00
|
|
|
if !set || err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
err = tx.Commit()
|
|
|
|
return err == nil, err
|
2016-05-04 21:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// kvsSetCASTxn is the inner method used to do a CAS inside an existing
|
|
|
|
// transaction.
|
2020-09-03 23:38:03 +00:00
|
|
|
func kvsSetCASTxn(tx WriteTxn, idx uint64, entry *structs.DirEntry) (bool, error) {
|
2021-11-08 14:35:56 +00:00
|
|
|
existing, err := tx.First(tableKVs, indexID, entry)
|
2016-05-02 23:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed kvs lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the we should do the set. A ModifyIndex of 0 means that
|
|
|
|
// we are doing a set-if-not-exists.
|
|
|
|
if entry.ModifyIndex == 0 && existing != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if entry.ModifyIndex != 0 && existing == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
e, ok := existing.(*structs.DirEntry)
|
|
|
|
if ok && entry.ModifyIndex != 0 && entry.ModifyIndex != e.ModifyIndex {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we made it this far, we should perform the set.
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := kvsSetTxn(tx, idx, entry, false); err != nil {
|
2016-05-02 23:21:04 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// KVSDeleteTree is used to do a recursive delete on a key prefix
|
|
|
|
// in the state store. If any keys are modified, the last index is
|
|
|
|
// set, otherwise this is a no-op.
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) KVSDeleteTree(idx uint64, prefix string, entMeta *acl.EnterpriseMeta) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2016-05-02 23:21:04 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2019-11-25 17:57:35 +00:00
|
|
|
if err := s.kvsDeleteTreeTxn(tx, idx, prefix, entMeta); err != nil {
|
2016-05-04 21:20:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2016-05-04 21:20:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 23:21:04 +00:00
|
|
|
// KVSLockDelay returns the expiration time for any lock delay associated with
|
|
|
|
// the given key.
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) KVSLockDelay(key string, entMeta *acl.EnterpriseMeta) time.Time {
|
2019-11-25 17:57:35 +00:00
|
|
|
return s.lockDelay.GetExpiration(key, entMeta)
|
2016-05-02 23:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// KVSLock is similar to KVSSet but only performs the set if the lock can be
|
|
|
|
// acquired.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Store) KVSLock(idx uint64, entry *structs.DirEntry) (bool, error) {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2016-05-02 23:21:04 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
locked, err := kvsLockTxn(tx, idx, entry)
|
2016-05-04 21:20:11 +00:00
|
|
|
if !locked || err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
err = tx.Commit()
|
|
|
|
return err == nil, err
|
2016-05-04 21:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// kvsLockTxn is the inner method that does a lock inside an existing
|
|
|
|
// transaction.
|
2020-09-03 23:38:03 +00:00
|
|
|
func kvsLockTxn(tx WriteTxn, idx uint64, entry *structs.DirEntry) (bool, error) {
|
2016-05-02 23:21:04 +00:00
|
|
|
// Verify that a session is present.
|
|
|
|
if entry.Session == "" {
|
|
|
|
return false, fmt.Errorf("missing session")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the session exists.
|
2021-11-08 21:20:50 +00:00
|
|
|
sess, err := tx.First(tableSessions, indexID, Query{Value: entry.Session, EnterpriseMeta: entry.EnterpriseMeta})
|
2016-05-02 23:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed session lookup: %s", err)
|
|
|
|
}
|
|
|
|
if sess == nil {
|
|
|
|
return false, fmt.Errorf("invalid session %#v", entry.Session)
|
|
|
|
}
|
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
existing, err := tx.First(tableKVs, indexID, entry)
|
2016-05-02 23:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed kvs lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the entry, using the existing entry if present.
|
|
|
|
if existing != nil {
|
|
|
|
e := existing.(*structs.DirEntry)
|
|
|
|
if e.Session == entry.Session {
|
|
|
|
// We already hold this lock, good to go.
|
|
|
|
entry.CreateIndex = e.CreateIndex
|
|
|
|
entry.LockIndex = e.LockIndex
|
|
|
|
} else if e.Session != "" {
|
|
|
|
// Bail out, someone else holds this lock.
|
|
|
|
return false, nil
|
|
|
|
} else {
|
|
|
|
// Set up a new lock with this session.
|
|
|
|
entry.CreateIndex = e.CreateIndex
|
|
|
|
entry.LockIndex = e.LockIndex + 1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
entry.CreateIndex = idx
|
|
|
|
entry.LockIndex = 1
|
|
|
|
}
|
|
|
|
entry.ModifyIndex = idx
|
|
|
|
|
|
|
|
// If we made it this far, we should perform the set.
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := kvsSetTxn(tx, idx, entry, true); err != nil {
|
2016-05-02 23:21:04 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// KVSUnlock is similar to KVSSet but only performs the set if the lock can be
|
|
|
|
// unlocked (the key must already exist and be locked).
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Store) KVSUnlock(idx uint64, entry *structs.DirEntry) (bool, error) {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2016-05-02 23:21:04 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
unlocked, err := kvsUnlockTxn(tx, idx, entry)
|
2016-05-04 21:20:11 +00:00
|
|
|
if !unlocked || err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
err = tx.Commit()
|
|
|
|
return err == nil, err
|
2016-05-04 21:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// kvsUnlockTxn is the inner method that does an unlock inside an existing
|
|
|
|
// transaction.
|
2020-09-03 23:38:03 +00:00
|
|
|
func kvsUnlockTxn(tx WriteTxn, idx uint64, entry *structs.DirEntry) (bool, error) {
|
2016-05-02 23:21:04 +00:00
|
|
|
// Verify that a session is present.
|
|
|
|
if entry.Session == "" {
|
|
|
|
return false, fmt.Errorf("missing session")
|
|
|
|
}
|
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
existing, err := tx.First(tableKVs, indexID, entry)
|
2016-05-02 23:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed kvs lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bail if there's no existing key.
|
|
|
|
if existing == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the given session is the lock holder.
|
|
|
|
e := existing.(*structs.DirEntry)
|
|
|
|
if e.Session != entry.Session {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the lock and update the entry.
|
|
|
|
entry.Session = ""
|
|
|
|
entry.LockIndex = e.LockIndex
|
|
|
|
entry.CreateIndex = e.CreateIndex
|
|
|
|
entry.ModifyIndex = idx
|
|
|
|
|
|
|
|
// If we made it this far, we should perform the set.
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := kvsSetTxn(tx, idx, entry, true); err != nil {
|
2016-05-02 23:21:04 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
2016-05-05 22:46:59 +00:00
|
|
|
|
|
|
|
// kvsCheckSessionTxn checks to see if the given session matches the current
|
|
|
|
// entry for a key.
|
2020-09-03 23:38:03 +00:00
|
|
|
func kvsCheckSessionTxn(tx WriteTxn,
|
2022-04-05 21:10:06 +00:00
|
|
|
key string, session string, entMeta *acl.EnterpriseMeta) (*structs.DirEntry, error) {
|
2019-11-25 17:57:35 +00:00
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
if entMeta == nil {
|
|
|
|
entMeta = structs.DefaultEnterpriseMetaInDefaultPartition()
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, err := tx.First(tableKVs, indexID, Query{Value: key, EnterpriseMeta: *entMeta})
|
2016-05-05 22:46:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed kvs lookup: %s", err)
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, fmt.Errorf("failed to check session, key %q doesn't exist", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
e := entry.(*structs.DirEntry)
|
|
|
|
if e.Session != session {
|
|
|
|
return nil, fmt.Errorf("failed session check for key %q, current session %q != %q", key, e.Session, session)
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// kvsCheckIndexTxn checks to see if the given modify index matches the current
|
|
|
|
// entry for a key.
|
2020-09-03 23:38:03 +00:00
|
|
|
func kvsCheckIndexTxn(tx WriteTxn,
|
2022-04-05 21:10:06 +00:00
|
|
|
key string, cidx uint64, entMeta acl.EnterpriseMeta) (*structs.DirEntry, error) {
|
2019-11-25 17:57:35 +00:00
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
entry, err := tx.First(tableKVs, indexID, Query{Value: key, EnterpriseMeta: entMeta})
|
2016-05-05 22:46:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed kvs lookup: %s", err)
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, fmt.Errorf("failed to check index, key %q doesn't exist", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
e := entry.(*structs.DirEntry)
|
|
|
|
if e.ModifyIndex != cidx {
|
|
|
|
return nil, fmt.Errorf("failed index check for key %q, current modify index %d != %d", key, e.ModifyIndex, cidx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|