nomad: change to use MemDB instead of raw iradix

This commit is contained in:
Armon Dadgar
2015-07-03 14:46:30 -07:00
parent 2094e5efc5
commit 11210fd353
3 changed files with 24 additions and 18 deletions

View File

@@ -45,7 +45,7 @@ func NewFSM(logOutput io.Writer) (*nomadFSM, error) {
// Close is used to cleanup resources associated with the FSM
func (n *nomadFSM) Close() error {
return n.state.Close()
return nil
}
// State is used to return a handle to the current state
@@ -96,5 +96,5 @@ func (s *nomadSnapshot) Persist(sink raft.SnapshotSink) error {
}
func (s *nomadSnapshot) Release() {
s.state.Close()
return
}

8
nomad/schema.go Normal file
View File

@@ -0,0 +1,8 @@
package nomad
import "github.com/hashicorp/go-memdb"
// stateStoreSchema is used to return the schema for the state store
func stateStoreSchema() *memdb.DBSchema {
return nil
}

View File

@@ -1,10 +1,11 @@
package nomad
import (
"fmt"
"io"
"log"
"github.com/hashicorp/go-immutable-radix"
"github.com/hashicorp/go-memdb"
)
// The StateStore is responsible for maintaining all the Consul
@@ -14,7 +15,7 @@ import (
// to provide write availability in the face of reads.
type StateStore struct {
logger *log.Logger
root *iradix.Tree
db *memdb.MemDB
}
// StateSnapshot is used to provide a point-in-time snapshot
@@ -22,33 +23,30 @@ type StateSnapshot struct {
StateStore
}
// Close is used to abort the transaction and allow for cleanup
func (s *StateSnapshot) Close() error {
return nil
}
// NewStateStore is used to create a new state store
func NewStateStore(logOutput io.Writer) (*StateStore, error) {
// Create the MemDB
db, err := memdb.NewMemDB(stateStoreSchema())
if err != nil {
return nil, fmt.Errorf("state store setup failed: %v", err)
}
// Create the state store
s := &StateStore{
logger: log.New(logOutput, "", log.LstdFlags),
root: iradix.New(),
db: db,
}
return s, nil
}
// Close is used to safely shutdown the state store
func (s *StateStore) Close() error {
return nil
}
// Snapshot is used to create a point in time snapshot. Because
// we use an immutable radix tree, we just need to preserve the
// pointer to the root, and we are done.
// we use MemDB, we just need to snapshot the state of the underlying
// database.
func (s *StateStore) Snapshot() (*StateSnapshot, error) {
snap := &StateSnapshot{
StateStore: StateStore{
logger: s.logger,
root: s.root,
db: s.db.Snapshot(),
},
}
return snap, nil