From c8d0d09087279531d8c7923be203ff24fb56e332 Mon Sep 17 00:00:00 2001 From: James Phillips Date: Wed, 7 Oct 2015 08:58:17 -0700 Subject: [PATCH] De-generalizes graveyard since that ended up as a YAGNI (only useful for KV). --- consul/state/graveyard.go | 32 +++++++++++++++----------------- consul/state/schema.go | 10 +++++----- consul/state/state_store.go | 2 +- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/consul/state/graveyard.go b/consul/state/graveyard.go index 487691f8d2..f5d12ba5e6 100644 --- a/consul/state/graveyard.go +++ b/consul/state/graveyard.go @@ -6,31 +6,29 @@ import ( "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 { Key string Index uint64 } -// Graveyard manages a set of tombstones for a table. This is just used for -// KVS right now but we've broken it out for other table types later. +// Graveyard manages a set of tombstones. type Graveyard struct { - Table string } // NewGraveyard returns a new graveyard. -func NewGraveyard(table string) *Graveyard { - return &Graveyard{Table: "tombstones_" + table} +func NewGraveyard() *Graveyard { + return &Graveyard{} } // InsertTxn adds a new tombstone. -func (g *Graveyard) InsertTxn(tx *memdb.Txn, context string, idx uint64) error { - stone := &Tombstone{Key: context, Index: idx} - if err := tx.Insert(g.Table, stone); err != nil { +func (g *Graveyard) InsertTxn(tx *memdb.Txn, key string, idx uint64) error { + stone := &Tombstone{Key: key, Index: idx} + if err := tx.Insert("tombstones", stone); err != nil { 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 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 // given context, using a prefix match. -func (g *Graveyard) GetMaxIndexTxn(tx *memdb.Txn, context string) (uint64, error) { - stones, err := tx.Get(g.Table, "id_prefix", context) +func (g *Graveyard) GetMaxIndexTxn(tx *memdb.Txn, prefix string) (uint64, error) { + stones, err := tx.Get("tombstones", "id_prefix", prefix) if err != nil { 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. 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 { 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 // InsertTxn. 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) } - 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 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 // numeric value. Since this is all in-memory and done infrequently // this pretty reasonable. - stones, err := tx.Get(g.Table, "id") + stones, err := tx.Get("tombstones", "id") if err != nil { return fmt.Errorf("failed querying tombstones: %s", err) } for stone := stones.Next(); stone != nil; stone = stones.Next() { 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) } } diff --git a/consul/state/schema.go b/consul/state/schema.go index cd8d32b68d..4efbe1b1af 100644 --- a/consul/state/schema.go +++ b/consul/state/schema.go @@ -25,7 +25,7 @@ func stateStoreSchema() *memdb.DBSchema { servicesTableSchema, checksTableSchema, kvsTableSchema, - func() *memdb.TableSchema { return tombstonesTableSchema("kvs") }, + tombstonesTableSchema, sessionsTableSchema, sessionChecksTableSchema, aclsTableSchema, @@ -230,11 +230,11 @@ func kvsTableSchema() *memdb.TableSchema { } // tombstonesTableSchema returns a new table schema used for -// storing tombstones during the given table's delete operations -// to prevent the index from sliding backwards. -func tombstonesTableSchema(table string) *memdb.TableSchema { +// storing tombstones during KV delete operations to prevent +// the index from sliding backwards. +func tombstonesTableSchema() *memdb.TableSchema { return &memdb.TableSchema{ - Name: "tombstones_" + table, + Name: "tombstones", Indexes: map[string]*memdb.IndexSchema{ "id": &memdb.IndexSchema{ Name: "id", diff --git a/consul/state/state_store.go b/consul/state/state_store.go index 095737a589..d8f87eddda 100644 --- a/consul/state/state_store.go +++ b/consul/state/state_store.go @@ -102,7 +102,7 @@ func NewStateStore(logOutput io.Writer) (*StateStore, error) { db: db, tableWatches: tableWatches, kvsWatch: NewPrefixWatch(), - kvsGraveyard: NewGraveyard("kvs"), + kvsGraveyard: NewGraveyard(), lockDelay: NewDelay(), } return s, nil