De-generalizes graveyard since that ended up as a YAGNI (only useful for KV).

This commit is contained in:
James Phillips 2015-10-07 08:58:17 -07:00
parent 537fd67464
commit c8d0d09087
3 changed files with 21 additions and 23 deletions

View File

@ -6,31 +6,29 @@ import (
"github.com/hashicorp/go-memdb" "github.com/hashicorp/go-memdb"
) )
// tombstone is the internal type used to track tombstones. // Tombstone is the internal type used to track tombstones.
type Tombstone struct { type Tombstone struct {
Key string Key string
Index uint64 Index uint64
} }
// Graveyard manages a set of tombstones for a table. This is just used for // Graveyard manages a set of tombstones.
// KVS right now but we've broken it out for other table types later.
type Graveyard struct { type Graveyard struct {
Table string
} }
// NewGraveyard returns a new graveyard. // NewGraveyard returns a new graveyard.
func NewGraveyard(table string) *Graveyard { func NewGraveyard() *Graveyard {
return &Graveyard{Table: "tombstones_" + table} return &Graveyard{}
} }
// InsertTxn adds a new tombstone. // InsertTxn adds a new tombstone.
func (g *Graveyard) InsertTxn(tx *memdb.Txn, context string, idx uint64) error { func (g *Graveyard) InsertTxn(tx *memdb.Txn, key string, idx uint64) error {
stone := &Tombstone{Key: context, Index: idx} stone := &Tombstone{Key: key, Index: idx}
if err := tx.Insert(g.Table, stone); err != nil { if err := tx.Insert("tombstones", stone); err != nil {
return fmt.Errorf("failed inserting tombstone: %s", err) return fmt.Errorf("failed inserting tombstone: %s", err)
} }
if err := tx.Insert("index", &IndexEntry{g.Table, idx}); err != nil { if err := tx.Insert("index", &IndexEntry{"tombstones", idx}); err != nil {
return fmt.Errorf("failed updating index: %s", err) return fmt.Errorf("failed updating index: %s", err)
} }
return nil return nil
@ -38,8 +36,8 @@ func (g *Graveyard) InsertTxn(tx *memdb.Txn, context string, idx uint64) error {
// GetMaxIndexTxn returns the highest index tombstone whose key matches the // GetMaxIndexTxn returns the highest index tombstone whose key matches the
// given context, using a prefix match. // given context, using a prefix match.
func (g *Graveyard) GetMaxIndexTxn(tx *memdb.Txn, context string) (uint64, error) { func (g *Graveyard) GetMaxIndexTxn(tx *memdb.Txn, prefix string) (uint64, error) {
stones, err := tx.Get(g.Table, "id_prefix", context) stones, err := tx.Get("tombstones", "id_prefix", prefix)
if err != nil { if err != nil {
return 0, fmt.Errorf("failed querying tombstones: %s", err) return 0, fmt.Errorf("failed querying tombstones: %s", err)
} }
@ -56,7 +54,7 @@ func (g *Graveyard) GetMaxIndexTxn(tx *memdb.Txn, context string) (uint64, error
// DumpTxn returns all the tombstones. // DumpTxn returns all the tombstones.
func (g *Graveyard) DumpTxn(tx *memdb.Txn) ([]*Tombstone, error) { func (g *Graveyard) DumpTxn(tx *memdb.Txn) ([]*Tombstone, error) {
stones, err := tx.Get(g.Table, "id") stones, err := tx.Get("tombstones", "id")
if err != nil { if err != nil {
return nil, fmt.Errorf("failed querying tombstones: %s", err) return nil, fmt.Errorf("failed querying tombstones: %s", err)
} }
@ -71,11 +69,11 @@ func (g *Graveyard) DumpTxn(tx *memdb.Txn) ([]*Tombstone, error) {
// RestoreTxn is used when restoring from a snapshot. For general inserts, use // RestoreTxn is used when restoring from a snapshot. For general inserts, use
// InsertTxn. // InsertTxn.
func (g *Graveyard) RestoreTxn(tx *memdb.Txn, stone *Tombstone) error { func (g *Graveyard) RestoreTxn(tx *memdb.Txn, stone *Tombstone) error {
if err := tx.Insert(g.Table, stone); err != nil { if err := tx.Insert("tombstones", stone); err != nil {
return fmt.Errorf("failed inserting tombstone: %s", err) return fmt.Errorf("failed inserting tombstone: %s", err)
} }
if err := indexUpdateMaxTxn(tx, stone.Index, g.Table); err != nil { if err := indexUpdateMaxTxn(tx, stone.Index, "tombstones"); err != nil {
return fmt.Errorf("failed updating index: %s", err) return fmt.Errorf("failed updating index: %s", err)
} }
return nil return nil
@ -87,14 +85,14 @@ func (g *Graveyard) ReapTxn(tx *memdb.Txn, idx uint64) error {
// This does a full table scan since we currently can't index on a // 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 // numeric value. Since this is all in-memory and done infrequently
// this pretty reasonable. // this pretty reasonable.
stones, err := tx.Get(g.Table, "id") stones, err := tx.Get("tombstones", "id")
if err != nil { if err != nil {
return fmt.Errorf("failed querying tombstones: %s", err) return fmt.Errorf("failed querying tombstones: %s", err)
} }
for stone := stones.Next(); stone != nil; stone = stones.Next() { for stone := stones.Next(); stone != nil; stone = stones.Next() {
if stone.(*Tombstone).Index <= idx { if stone.(*Tombstone).Index <= idx {
if err := tx.Delete(g.Table, stone); err != nil { if err := tx.Delete("tombstones", stone); err != nil {
return fmt.Errorf("failed deleting tombstone: %s", err) return fmt.Errorf("failed deleting tombstone: %s", err)
} }
} }

View File

@ -25,7 +25,7 @@ func stateStoreSchema() *memdb.DBSchema {
servicesTableSchema, servicesTableSchema,
checksTableSchema, checksTableSchema,
kvsTableSchema, kvsTableSchema,
func() *memdb.TableSchema { return tombstonesTableSchema("kvs") }, tombstonesTableSchema,
sessionsTableSchema, sessionsTableSchema,
sessionChecksTableSchema, sessionChecksTableSchema,
aclsTableSchema, aclsTableSchema,
@ -230,11 +230,11 @@ func kvsTableSchema() *memdb.TableSchema {
} }
// tombstonesTableSchema returns a new table schema used for // tombstonesTableSchema returns a new table schema used for
// storing tombstones during the given table's delete operations // storing tombstones during KV delete operations to prevent
// to prevent the index from sliding backwards. // the index from sliding backwards.
func tombstonesTableSchema(table string) *memdb.TableSchema { func tombstonesTableSchema() *memdb.TableSchema {
return &memdb.TableSchema{ return &memdb.TableSchema{
Name: "tombstones_" + table, Name: "tombstones",
Indexes: map[string]*memdb.IndexSchema{ Indexes: map[string]*memdb.IndexSchema{
"id": &memdb.IndexSchema{ "id": &memdb.IndexSchema{
Name: "id", Name: "id",

View File

@ -102,7 +102,7 @@ func NewStateStore(logOutput io.Writer) (*StateStore, error) {
db: db, db: db,
tableWatches: tableWatches, tableWatches: tableWatches,
kvsWatch: NewPrefixWatch(), kvsWatch: NewPrefixWatch(),
kvsGraveyard: NewGraveyard("kvs"), kvsGraveyard: NewGraveyard(),
lockDelay: NewDelay(), lockDelay: NewDelay(),
} }
return s, nil return s, nil