2015-06-01 15:49:10 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2015-07-03 21:46:30 +00:00
|
|
|
"fmt"
|
2015-06-01 15:49:10 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
2015-06-03 09:21:59 +00:00
|
|
|
|
2015-07-03 21:46:30 +00:00
|
|
|
"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
|
2015-07-03 21:46:30 +00:00
|
|
|
db *memdb.MemDB
|
2015-06-01 15:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StateSnapshot is used to provide a point-in-time snapshot
|
|
|
|
type StateSnapshot struct {
|
2015-06-03 09:26:49 +00:00
|
|
|
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) {
|
2015-07-03 21:46:30 +00:00
|
|
|
// 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),
|
2015-07-03 21:46:30 +00:00
|
|
|
db: db,
|
2015-06-01 15:49:10 +00:00
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2015-06-03 09:21:59 +00:00
|
|
|
// Snapshot is used to create a point in time snapshot. Because
|
2015-07-03 21:46:30 +00:00
|
|
|
// 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) {
|
2015-06-03 09:21:59 +00:00
|
|
|
snap := &StateSnapshot{
|
2015-06-03 09:26:49 +00:00
|
|
|
StateStore: StateStore{
|
2015-06-03 09:21:59 +00:00
|
|
|
logger: s.logger,
|
2015-07-03 21:46:30 +00:00
|
|
|
db: s.db.Snapshot(),
|
2015-06-03 09:21:59 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return snap, nil
|
2015-06-01 15:49:10 +00:00
|
|
|
}
|