2015-09-25 19:01:46 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-03-19 13:11:20 +00:00
|
|
|
|
2019-11-25 17:57:35 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2015-09-25 19:01:46 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
)
|
|
|
|
|
2015-10-07 15:58:17 +00:00
|
|
|
// Tombstone is the internal type used to track tombstones.
|
2015-09-25 19:01:46 +00:00
|
|
|
type Tombstone struct {
|
|
|
|
Key string
|
|
|
|
Index uint64
|
2019-11-25 17:57:35 +00:00
|
|
|
|
|
|
|
structs.EnterpriseMeta
|
2015-09-25 19:01:46 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 15:58:17 +00:00
|
|
|
// Graveyard manages a set of tombstones.
|
2015-09-25 19:01:46 +00:00
|
|
|
type Graveyard struct {
|
2015-10-12 07:42:09 +00:00
|
|
|
// GC is when we create tombstones to track their time-to-live.
|
|
|
|
// The GC is consumed upstream to manage clearing of tombstones.
|
|
|
|
gc *TombstoneGC
|
2015-09-25 19:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewGraveyard returns a new graveyard.
|
2015-10-12 07:42:09 +00:00
|
|
|
func NewGraveyard(gc *TombstoneGC) *Graveyard {
|
|
|
|
return &Graveyard{gc: gc}
|
2015-09-25 19:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InsertTxn adds a new tombstone.
|
2020-06-03 17:21:00 +00:00
|
|
|
func (g *Graveyard) InsertTxn(tx *txn, key string, idx uint64, entMeta *structs.EnterpriseMeta) error {
|
2019-11-25 17:57:35 +00:00
|
|
|
stone := &Tombstone{
|
|
|
|
Key: key,
|
|
|
|
Index: idx,
|
|
|
|
}
|
|
|
|
if entMeta != nil {
|
|
|
|
stone.EnterpriseMeta = *entMeta
|
2015-09-25 19:01:46 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:57:35 +00:00
|
|
|
// Insert the tombstone.
|
|
|
|
if err := g.insertTombstoneWithTxn(tx, "tombstones", stone, false); err != nil {
|
|
|
|
return fmt.Errorf("failed inserting tombstone: %s", err)
|
2015-09-25 19:01:46 +00:00
|
|
|
}
|
2015-10-12 07:42:09 +00:00
|
|
|
|
|
|
|
// If GC is configured, then we hint that this index requires reaping.
|
|
|
|
if g.gc != nil {
|
|
|
|
tx.Defer(func() { g.gc.Hint(idx) })
|
|
|
|
}
|
2015-09-25 19:01:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMaxIndexTxn returns the highest index tombstone whose key matches the
|
|
|
|
// given context, using a prefix match.
|
2020-06-03 17:21:00 +00:00
|
|
|
func (g *Graveyard) GetMaxIndexTxn(tx *txn, prefix string, entMeta *structs.EnterpriseMeta) (uint64, error) {
|
2019-11-25 17:57:35 +00:00
|
|
|
stones, err := getWithTxn(tx, "tombstones", "id_prefix", prefix, entMeta)
|
2015-09-25 19:01:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed querying tombstones: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var lindex uint64
|
|
|
|
for stone := stones.Next(); stone != nil; stone = stones.Next() {
|
2015-10-07 04:40:27 +00:00
|
|
|
s := stone.(*Tombstone)
|
|
|
|
if s.Index > lindex {
|
|
|
|
lindex = s.Index
|
2015-09-25 19:01:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return lindex, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DumpTxn returns all the tombstones.
|
2020-06-03 17:21:00 +00:00
|
|
|
func (g *Graveyard) DumpTxn(tx *txn) (memdb.ResultIterator, error) {
|
2015-10-19 21:56:22 +00:00
|
|
|
iter, err := tx.Get("tombstones", "id")
|
2015-09-25 19:01:46 +00:00
|
|
|
if err != nil {
|
2015-10-19 21:56:22 +00:00
|
|
|
return nil, err
|
2015-09-25 19:01:46 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 21:56:22 +00:00
|
|
|
return iter, nil
|
2015-09-25 19:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RestoreTxn is used when restoring from a snapshot. For general inserts, use
|
|
|
|
// InsertTxn.
|
2020-06-03 17:21:00 +00:00
|
|
|
func (g *Graveyard) RestoreTxn(tx *txn, stone *Tombstone) error {
|
2019-11-25 17:57:35 +00:00
|
|
|
if err := g.insertTombstoneWithTxn(tx, "tombstones", stone, true); err != nil {
|
2015-09-25 19:01:46 +00:00
|
|
|
return fmt.Errorf("failed inserting tombstone: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReapTxn cleans out all tombstones whose index values are less than or equal
|
|
|
|
// to the given idx. This prevents unbounded storage growth of the tombstones.
|
2020-06-03 17:21:00 +00:00
|
|
|
func (g *Graveyard) ReapTxn(tx *txn, idx uint64) error {
|
2015-09-25 19:01:46 +00:00
|
|
|
// This does a full table scan since we currently can't index on a
|
|
|
|
// numeric value. Since this is all in-memory and done infrequently
|
|
|
|
// this pretty reasonable.
|
2015-10-07 15:58:17 +00:00
|
|
|
stones, err := tx.Get("tombstones", "id")
|
2015-09-25 19:01:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed querying tombstones: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-10-13 06:27:23 +00:00
|
|
|
// Find eligible tombstones.
|
|
|
|
var objs []interface{}
|
2015-09-25 19:01:46 +00:00
|
|
|
for stone := stones.Next(); stone != nil; stone = stones.Next() {
|
|
|
|
if stone.(*Tombstone).Index <= idx {
|
2015-10-13 06:27:23 +00:00
|
|
|
objs = append(objs, stone)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the tombstones in a separate loop so we don't trash the
|
|
|
|
// iterator.
|
|
|
|
for _, obj := range objs {
|
|
|
|
if err := tx.Delete("tombstones", obj); err != nil {
|
|
|
|
return fmt.Errorf("failed deleting tombstone: %s", err)
|
2015-09-25 19:01:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|