2015-09-25 19:01:46 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-03-19 13:11:20 +00:00
|
|
|
|
2015-09-25 19:01:46 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
2021-03-08 22:41:10 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2015-09-25 19:01:46 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
acl.EnterpriseMeta
|
2015-09-25 19:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
func (t Tombstone) IDValue() string {
|
|
|
|
return t.Key
|
|
|
|
}
|
|
|
|
|
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.
|
2022-04-05 21:10:06 +00:00
|
|
|
func (g *Graveyard) InsertTxn(tx WriteTxn, key string, idx uint64, entMeta *acl.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
|
|
|
|
}
|
|
|
|
|
|
|
|
// DumpTxn returns all the tombstones.
|
2020-09-03 23:38:03 +00:00
|
|
|
func (g *Graveyard) DumpTxn(tx ReadTxn) (memdb.ResultIterator, error) {
|
2021-11-08 14:35:56 +00:00
|
|
|
return tx.Get(tableTombstones, indexID)
|
2015-09-25 19:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RestoreTxn is used when restoring from a snapshot. For general inserts, use
|
|
|
|
// InsertTxn.
|
2021-03-08 22:41:10 +00:00
|
|
|
func (g *Graveyard) RestoreTxn(tx WriteTxn, 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.
|
2021-03-08 22:41:10 +00:00
|
|
|
func (g *Graveyard) ReapTxn(tx WriteTxn, 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.
|
2021-11-08 14:35:56 +00:00
|
|
|
stones, err := tx.Get(tableTombstones, indexID)
|
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
|
|
|
|
}
|