2015-09-25 19:01:46 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
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.
|
2015-10-07 15:58:17 +00:00
|
|
|
func (g *Graveyard) InsertTxn(tx *memdb.Txn, key string, idx uint64) error {
|
2015-10-12 07:42:09 +00:00
|
|
|
// Insert the tombstone.
|
2015-10-07 15:58:17 +00:00
|
|
|
stone := &Tombstone{Key: key, Index: idx}
|
|
|
|
if err := tx.Insert("tombstones", stone); err != nil {
|
2015-09-25 19:01:46 +00:00
|
|
|
return fmt.Errorf("failed inserting tombstone: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-10-07 15:58:17 +00:00
|
|
|
if err := tx.Insert("index", &IndexEntry{"tombstones", idx}); err != nil {
|
2015-09-25 19:01:46 +00:00
|
|
|
return fmt.Errorf("failed updating index: %s", err)
|
|
|
|
}
|
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.
|
2015-10-07 15:58:17 +00:00
|
|
|
func (g *Graveyard) GetMaxIndexTxn(tx *memdb.Txn, prefix string) (uint64, error) {
|
|
|
|
stones, err := tx.Get("tombstones", "id_prefix", prefix)
|
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.
|
2015-10-19 21:56:22 +00:00
|
|
|
func (g *Graveyard) DumpTxn(tx *memdb.Txn) (memdb.ResultIterator, error) {
|
|
|
|
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.
|
|
|
|
func (g *Graveyard) RestoreTxn(tx *memdb.Txn, stone *Tombstone) error {
|
2015-10-07 15:58:17 +00:00
|
|
|
if err := tx.Insert("tombstones", stone); err != nil {
|
2015-09-25 19:01:46 +00:00
|
|
|
return fmt.Errorf("failed inserting tombstone: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-10-07 15:58:17 +00:00
|
|
|
if err := indexUpdateMaxTxn(tx, stone.Index, "tombstones"); err != nil {
|
2015-09-25 19:01:46 +00:00
|
|
|
return fmt.Errorf("failed updating index: %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.
|
|
|
|
func (g *Graveyard) ReapTxn(tx *memdb.Txn, idx uint64) error {
|
|
|
|
// 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
|
|
|
|
}
|