2021-11-08 14:35:56 +00:00
|
|
|
//go:build !consulent
|
2019-11-25 17:57:35 +00:00
|
|
|
// +build !consulent
|
|
|
|
|
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2021-11-08 14:35:56 +00:00
|
|
|
"bytes"
|
2019-11-25 17:57:35 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-memdb"
|
2021-02-05 22:53:08 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-11-25 17:57:35 +00:00
|
|
|
)
|
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
func kvsIndexer() indexerSingleWithPrefix {
|
|
|
|
return indexerSingleWithPrefix{
|
2021-11-08 21:20:50 +00:00
|
|
|
readIndex: readIndex(indexFromIDValue),
|
|
|
|
writeIndex: writeIndex(indexFromIDValue),
|
|
|
|
prefixIndex: prefixIndex(prefixIndexForIDValue),
|
2019-11-25 17:57:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 21:20:50 +00:00
|
|
|
func prefixIndexForIDValue(arg interface{}) ([]byte, error) {
|
|
|
|
switch v := arg.(type) {
|
|
|
|
// DeletePrefix always uses a string, pass it along unmodified
|
|
|
|
case string:
|
|
|
|
return []byte(v), nil
|
|
|
|
case structs.EnterpriseMeta:
|
|
|
|
return nil, nil
|
|
|
|
case singleValueID:
|
|
|
|
var b indexBuilder
|
|
|
|
if v.IDValue() != "" {
|
|
|
|
// Omit null terminator, because we want to prefix match keys
|
|
|
|
b.String(v.IDValue())
|
|
|
|
}
|
|
|
|
prefix := bytes.Trim(b.Bytes(), "\x00")
|
|
|
|
return prefix, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for singleValueID prefix index", arg)
|
|
|
|
}
|
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
func prefixIndexForKVEntry(arg interface{}) ([]byte, error) {
|
|
|
|
var b indexBuilder
|
|
|
|
switch v := arg.(type) {
|
|
|
|
// DeletePrefix always uses a string, pass it along unmodified
|
|
|
|
case string:
|
|
|
|
return []byte(v), nil
|
|
|
|
case structs.EnterpriseMeta:
|
|
|
|
return nil, nil
|
|
|
|
case singleValueID:
|
|
|
|
// Omit null terminator, because we want to prefix match keys
|
|
|
|
(*bytes.Buffer)(&b).WriteString(v.IDValue())
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for singleValueID prefix index", arg)
|
|
|
|
}
|
|
|
|
|
2020-11-11 16:21:42 +00:00
|
|
|
func insertKVTxn(tx WriteTxn, entry *structs.DirEntry, updateMax bool, _ bool) error {
|
2021-11-08 14:35:56 +00:00
|
|
|
if err := tx.Insert(tableKVs, entry); err != nil {
|
2019-11-25 17:57:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if updateMax {
|
2021-11-08 14:35:56 +00:00
|
|
|
if err := indexUpdateMaxTxn(tx, entry.ModifyIndex, tableKVs); err != nil {
|
2019-11-25 17:57:35 +00:00
|
|
|
return fmt.Errorf("failed updating kvs index: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-08 14:35:56 +00:00
|
|
|
if err := tx.Insert(tableIndex, &IndexEntry{tableKVs, entry.ModifyIndex}); err != nil {
|
2019-11-25 17:57:35 +00:00
|
|
|
return fmt.Errorf("failed updating kvs index: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
func kvsListEntriesTxn(tx ReadTxn, ws memdb.WatchSet, prefix string, entMeta structs.EnterpriseMeta) (uint64, structs.DirEntries, error) {
|
2019-11-25 17:57:35 +00:00
|
|
|
var ents structs.DirEntries
|
|
|
|
var lindex uint64
|
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
entries, err := tx.Get(tableKVs, indexID+"_prefix", prefix)
|
2019-11-25 17:57:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed kvs lookup: %s", err)
|
|
|
|
}
|
|
|
|
ws.Add(entries.WatchCh())
|
|
|
|
|
|
|
|
// Gather all of the keys found
|
|
|
|
for entry := entries.Next(); entry != nil; entry = entries.Next() {
|
|
|
|
e := entry.(*structs.DirEntry)
|
|
|
|
ents = append(ents, e)
|
|
|
|
if e.ModifyIndex > lindex {
|
|
|
|
lindex = e.ModifyIndex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lindex, ents, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// kvsDeleteTreeTxn is the inner method that does a recursive delete inside an
|
|
|
|
// existing transaction.
|
2020-09-03 23:38:03 +00:00
|
|
|
func (s *Store) kvsDeleteTreeTxn(tx WriteTxn, idx uint64, prefix string, entMeta *structs.EnterpriseMeta) error {
|
2019-11-25 17:57:35 +00:00
|
|
|
// For prefix deletes, only insert one tombstone and delete the entire subtree
|
2021-11-08 14:35:56 +00:00
|
|
|
deleted, err := tx.DeletePrefix(tableKVs, indexID+"_prefix", prefix)
|
2019-11-25 17:57:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed recursive deleting kvs entry: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if deleted {
|
|
|
|
if prefix != "" { // don't insert a tombstone if the entire tree is deleted, all watchers on keys will see the max_index of the tree
|
|
|
|
if err := s.kvsGraveyard.InsertTxn(tx, prefix, idx, entMeta); err != nil {
|
|
|
|
return fmt.Errorf("failed adding to graveyard: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 22:53:08 +00:00
|
|
|
if err := tx.Insert(tableIndex, &IndexEntry{"kvs", idx}); err != nil {
|
2019-11-25 17:57:35 +00:00
|
|
|
return fmt.Errorf("failed updating index: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
func kvsMaxIndex(tx ReadTxn, entMeta structs.EnterpriseMeta) uint64 {
|
2019-11-25 17:57:35 +00:00
|
|
|
return maxIndexTxn(tx, "kvs", "tombstones")
|
|
|
|
}
|
|
|
|
|
2020-09-03 23:38:03 +00:00
|
|
|
func kvsDeleteWithEntry(tx WriteTxn, entry *structs.DirEntry, idx uint64) error {
|
2019-11-25 17:57:35 +00:00
|
|
|
// Delete the entry and update the index.
|
2021-11-08 14:35:56 +00:00
|
|
|
if err := tx.Delete(tableKVs, entry); err != nil {
|
2019-11-25 17:57:35 +00:00
|
|
|
return fmt.Errorf("failed deleting kvs entry: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
if err := tx.Insert(tableIndex, &IndexEntry{tableKVs, idx}); err != nil {
|
2019-11-25 17:57:35 +00:00
|
|
|
return fmt.Errorf("failed updating kvs index: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-08 14:35:56 +00:00
|
|
|
|
|
|
|
func partitionedIndexEntryName(entry string, _ string) string {
|
|
|
|
return entry
|
|
|
|
}
|