open-nomad/nomad/state_store.go

54 lines
1.3 KiB
Go
Raw Normal View History

2015-06-01 15:49:10 +00:00
package nomad
import (
"fmt"
2015-06-01 15:49:10 +00:00
"io"
"log"
"github.com/hashicorp/go-memdb"
2015-06-01 15:49:10 +00:00
)
// The StateStore is responsible for maintaining all the Consul
// state. It is manipulated by the FSM which maintains consistency
// through the use of Raft. The goals of the StateStore are to provide
// high concurrency for read operations without blocking writes, and
// to provide write availability in the face of reads.
type StateStore struct {
logger *log.Logger
db *memdb.MemDB
2015-06-01 15:49:10 +00:00
}
// StateSnapshot is used to provide a point-in-time snapshot
type StateSnapshot struct {
StateStore
2015-06-01 15:49:10 +00:00
}
// 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
2015-06-01 15:49:10 +00:00
s := &StateStore{
logger: log.New(logOutput, "", log.LstdFlags),
db: db,
2015-06-01 15:49:10 +00:00
}
return s, nil
}
// Snapshot is used to create a point in time snapshot. Because
// we use MemDB, we just need to snapshot the state of the underlying
// database.
2015-06-01 15:49:10 +00:00
func (s *StateStore) Snapshot() (*StateSnapshot, error) {
snap := &StateSnapshot{
StateStore: StateStore{
logger: s.logger,
db: s.db.Snapshot(),
},
}
return snap, nil
2015-06-01 15:49:10 +00:00
}