2013-12-11 01:00:48 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-12-18 23:03:25 +00:00
|
|
|
"github.com/armon/gomdb"
|
2013-12-19 20:03:57 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2014-03-05 22:34:29 +00:00
|
|
|
"io"
|
2013-12-18 23:03:25 +00:00
|
|
|
"io/ioutil"
|
2014-03-05 22:34:29 +00:00
|
|
|
"log"
|
2013-12-18 23:03:25 +00:00
|
|
|
"os"
|
2013-12-11 01:00:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-01-08 18:31:20 +00:00
|
|
|
dbNodes = "nodes"
|
|
|
|
dbServices = "services"
|
2014-01-08 19:21:29 +00:00
|
|
|
dbChecks = "checks"
|
2014-03-31 18:48:32 +00:00
|
|
|
dbKVS = "kvs"
|
|
|
|
dbMaxMapSize = 512 * 1024 * 1024 // 512MB maximum size
|
2013-12-11 01:00:48 +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. The current
|
2013-12-18 23:03:25 +00:00
|
|
|
// implementation uses the Lightning Memory-Mapped Database (MDB).
|
|
|
|
// This gives us Multi-Version Concurrency Control for "free"
|
2013-12-11 01:00:48 +00:00
|
|
|
type StateStore struct {
|
2014-03-05 22:34:29 +00:00
|
|
|
logger *log.Logger
|
2014-01-08 18:31:20 +00:00
|
|
|
path string
|
|
|
|
env *mdb.Env
|
|
|
|
nodeTable *MDBTable
|
|
|
|
serviceTable *MDBTable
|
2014-01-08 19:21:29 +00:00
|
|
|
checkTable *MDBTable
|
2014-03-31 18:48:32 +00:00
|
|
|
kvsTable *MDBTable
|
2014-01-08 18:31:20 +00:00
|
|
|
tables MDBTables
|
2014-02-05 02:37:38 +00:00
|
|
|
watch map[*MDBTable]*NotifyGroup
|
2014-02-05 18:37:28 +00:00
|
|
|
queryTables map[string]MDBTables
|
2013-12-11 01:00:48 +00:00
|
|
|
}
|
|
|
|
|
2013-12-18 23:09:38 +00:00
|
|
|
// StateSnapshot is used to provide a point-in-time snapshot
|
|
|
|
// It works by starting a readonly transaction against all tables.
|
|
|
|
type StateSnapshot struct {
|
2014-02-05 02:33:15 +00:00
|
|
|
store *StateStore
|
|
|
|
tx *MDBTxn
|
|
|
|
lastIndex uint64
|
2013-12-18 23:09:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close is used to abort the transaction and allow for cleanup
|
|
|
|
func (s *StateSnapshot) Close() error {
|
|
|
|
s.tx.Abort()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-11 01:00:48 +00:00
|
|
|
// NewStateStore is used to create a new state store
|
2014-03-05 22:34:29 +00:00
|
|
|
func NewStateStore(logOutput io.Writer) (*StateStore, error) {
|
2013-12-18 23:03:25 +00:00
|
|
|
// Create a new temp dir
|
|
|
|
path, err := ioutil.TempDir("", "consul")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-12-12 22:41:13 +00:00
|
|
|
|
2013-12-18 23:03:25 +00:00
|
|
|
// Open the env
|
|
|
|
env, err := mdb.NewEnv()
|
2013-12-11 01:00:48 +00:00
|
|
|
if err != nil {
|
2013-12-18 23:03:25 +00:00
|
|
|
return nil, err
|
2013-12-11 01:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s := &StateStore{
|
2014-03-05 22:34:29 +00:00
|
|
|
logger: log.New(logOutput, "", log.LstdFlags),
|
|
|
|
path: path,
|
|
|
|
env: env,
|
|
|
|
watch: make(map[*MDBTable]*NotifyGroup),
|
2013-12-11 01:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we can initialize
|
|
|
|
if err := s.initialize(); err != nil {
|
2013-12-18 23:03:25 +00:00
|
|
|
env.Close()
|
|
|
|
os.RemoveAll(path)
|
2013-12-11 01:00:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close is used to safely shutdown the state store
|
|
|
|
func (s *StateStore) Close() error {
|
2013-12-18 23:03:25 +00:00
|
|
|
s.env.Close()
|
|
|
|
os.RemoveAll(s.path)
|
|
|
|
return nil
|
2013-12-11 01:00:48 +00:00
|
|
|
}
|
|
|
|
|
2013-12-18 23:03:25 +00:00
|
|
|
// initialize is used to setup the store for use
|
2013-12-11 01:00:48 +00:00
|
|
|
func (s *StateStore) initialize() error {
|
2013-12-18 23:03:25 +00:00
|
|
|
// Setup the Env first
|
2014-01-08 00:58:16 +00:00
|
|
|
if err := s.env.SetMaxDBs(mdb.DBI(32)); err != nil {
|
2013-12-18 23:03:25 +00:00
|
|
|
return err
|
2013-12-11 01:00:48 +00:00
|
|
|
}
|
|
|
|
|
2014-01-01 01:43:05 +00:00
|
|
|
// Increase the maximum map size
|
|
|
|
if err := s.env.SetMapSize(dbMaxMapSize); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-18 23:03:25 +00:00
|
|
|
// Optimize our flags for speed over safety, since the Raft log + snapshots
|
|
|
|
// are durable. We treat this as an ephemeral in-memory DB, since we nuke
|
|
|
|
// the data anyways.
|
|
|
|
var flags uint = mdb.NOMETASYNC | mdb.NOSYNC | mdb.NOTLS
|
|
|
|
if err := s.env.Open(s.path, flags, 0755); err != nil {
|
|
|
|
return err
|
2013-12-11 01:00:48 +00:00
|
|
|
}
|
|
|
|
|
2014-01-08 19:21:29 +00:00
|
|
|
// Tables use a generic struct encoder
|
|
|
|
encoder := func(obj interface{}) []byte {
|
|
|
|
buf, err := structs.Encode(255, obj)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return buf[1:]
|
|
|
|
}
|
|
|
|
|
2014-01-08 18:31:20 +00:00
|
|
|
// Setup our tables
|
|
|
|
s.nodeTable = &MDBTable{
|
|
|
|
Name: dbNodes,
|
|
|
|
Indexes: map[string]*MDBIndex{
|
|
|
|
"id": &MDBIndex{
|
|
|
|
Unique: true,
|
|
|
|
Fields: []string{"Node"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Decoder: func(buf []byte) interface{} {
|
|
|
|
out := new(structs.Node)
|
|
|
|
if err := structs.Decode(buf, out); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
},
|
|
|
|
}
|
2013-12-11 22:03:09 +00:00
|
|
|
|
2014-01-08 18:31:20 +00:00
|
|
|
s.serviceTable = &MDBTable{
|
|
|
|
Name: dbServices,
|
|
|
|
Indexes: map[string]*MDBIndex{
|
|
|
|
"id": &MDBIndex{
|
|
|
|
Unique: true,
|
|
|
|
Fields: []string{"Node", "ServiceID"},
|
|
|
|
},
|
|
|
|
"service": &MDBIndex{
|
|
|
|
AllowBlank: true,
|
|
|
|
Fields: []string{"ServiceName", "ServiceTag"},
|
|
|
|
},
|
|
|
|
},
|
2014-01-08 19:21:29 +00:00
|
|
|
Decoder: func(buf []byte) interface{} {
|
|
|
|
out := new(structs.ServiceNode)
|
|
|
|
if err := structs.Decode(buf, out); err != nil {
|
2014-01-08 18:31:20 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2014-01-08 19:21:29 +00:00
|
|
|
return out
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
s.checkTable = &MDBTable{
|
|
|
|
Name: dbChecks,
|
|
|
|
Indexes: map[string]*MDBIndex{
|
|
|
|
"id": &MDBIndex{
|
|
|
|
Unique: true,
|
|
|
|
Fields: []string{"Node", "CheckID"},
|
|
|
|
},
|
|
|
|
"status": &MDBIndex{
|
|
|
|
Fields: []string{"Status"},
|
|
|
|
},
|
|
|
|
"service": &MDBIndex{
|
|
|
|
AllowBlank: true,
|
|
|
|
Fields: []string{"ServiceName"},
|
|
|
|
},
|
|
|
|
"node": &MDBIndex{
|
|
|
|
AllowBlank: true,
|
|
|
|
Fields: []string{"Node", "ServiceID"},
|
|
|
|
},
|
2014-01-08 18:31:20 +00:00
|
|
|
},
|
|
|
|
Decoder: func(buf []byte) interface{} {
|
2014-01-08 19:21:29 +00:00
|
|
|
out := new(structs.HealthCheck)
|
2014-01-08 18:31:20 +00:00
|
|
|
if err := structs.Decode(buf, out); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
},
|
|
|
|
}
|
2013-12-18 23:03:25 +00:00
|
|
|
|
2014-03-31 18:48:32 +00:00
|
|
|
s.kvsTable = &MDBTable{
|
|
|
|
Name: dbKVS,
|
|
|
|
Indexes: map[string]*MDBIndex{
|
|
|
|
"id": &MDBIndex{
|
|
|
|
Unique: true,
|
|
|
|
Fields: []string{"Key"},
|
|
|
|
},
|
2014-03-31 20:12:35 +00:00
|
|
|
"id_prefix": &MDBIndex{
|
|
|
|
Virtual: true,
|
|
|
|
RealIndex: "id",
|
|
|
|
Fields: []string{"Key"},
|
|
|
|
IdxFunc: DefaultIndexPrefixFunc,
|
|
|
|
},
|
2014-03-31 18:48:32 +00:00
|
|
|
},
|
|
|
|
Decoder: func(buf []byte) interface{} {
|
|
|
|
out := new(structs.DirEntry)
|
|
|
|
if err := structs.Decode(buf, out); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-01-08 18:31:20 +00:00
|
|
|
// Store the set of tables
|
2014-03-31 18:48:32 +00:00
|
|
|
s.tables = []*MDBTable{s.nodeTable, s.serviceTable, s.checkTable, s.kvsTable}
|
2014-01-08 19:21:29 +00:00
|
|
|
for _, table := range s.tables {
|
|
|
|
table.Env = s.env
|
|
|
|
table.Encoder = encoder
|
|
|
|
if err := table.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-05 02:37:38 +00:00
|
|
|
|
|
|
|
// Setup a notification group per table
|
|
|
|
s.watch[table] = &NotifyGroup{}
|
2014-01-08 19:21:29 +00:00
|
|
|
}
|
2014-02-05 18:37:28 +00:00
|
|
|
|
|
|
|
// Setup the query tables
|
|
|
|
s.queryTables = map[string]MDBTables{
|
2014-02-05 21:30:18 +00:00
|
|
|
"Nodes": MDBTables{s.nodeTable},
|
|
|
|
"Services": MDBTables{s.serviceTable},
|
|
|
|
"ServiceNodes": MDBTables{s.nodeTable, s.serviceTable},
|
|
|
|
"NodeServices": MDBTables{s.nodeTable, s.serviceTable},
|
|
|
|
"ChecksInState": MDBTables{s.checkTable},
|
|
|
|
"NodeChecks": MDBTables{s.checkTable},
|
|
|
|
"ServiceChecks": MDBTables{s.checkTable},
|
|
|
|
"CheckServiceNodes": MDBTables{s.nodeTable, s.serviceTable, s.checkTable},
|
2014-03-31 23:00:23 +00:00
|
|
|
"KVSGet": MDBTables{s.kvsTable},
|
|
|
|
"KVSList": MDBTables{s.kvsTable},
|
2014-02-05 18:37:28 +00:00
|
|
|
}
|
2014-01-08 18:31:20 +00:00
|
|
|
return nil
|
2013-12-11 22:03:09 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 18:37:28 +00:00
|
|
|
// Watch is used to subscribe a channel to a set of MDBTables
|
|
|
|
func (s *StateStore) Watch(tables MDBTables, notify chan struct{}) {
|
|
|
|
for _, t := range tables {
|
|
|
|
s.watch[t].Wait(notify)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryTables returns the Tables that are queried for a given query
|
|
|
|
func (s *StateStore) QueryTables(q string) MDBTables {
|
|
|
|
return s.queryTables[q]
|
|
|
|
}
|
|
|
|
|
2013-12-18 23:03:25 +00:00
|
|
|
// EnsureNode is used to ensure a given node exists, with the provided address
|
2014-02-05 02:33:15 +00:00
|
|
|
func (s *StateStore) EnsureNode(index uint64, node structs.Node) error {
|
|
|
|
// Start a new txn
|
|
|
|
tx, err := s.nodeTable.StartTxn(false, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
if err := s.nodeTable.InsertTxn(tx, node); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.nodeTable.SetLastIndexTxn(tx, index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-05 02:45:40 +00:00
|
|
|
defer s.watch[s.nodeTable].Notify()
|
2014-02-05 02:33:15 +00:00
|
|
|
return tx.Commit()
|
2013-12-11 22:03:09 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 22:27:27 +00:00
|
|
|
// GetNode returns all the address of the known and if it was found
|
2014-02-05 02:33:15 +00:00
|
|
|
func (s *StateStore) GetNode(name string) (uint64, bool, string) {
|
|
|
|
idx, res, err := s.nodeTable.Get("id", name)
|
2013-12-18 23:03:25 +00:00
|
|
|
if err != nil {
|
2014-03-05 23:02:28 +00:00
|
|
|
s.logger.Printf("[ERR] consul.state: Error during node lookup: %v", err)
|
|
|
|
return 0, false, ""
|
2013-12-18 23:03:25 +00:00
|
|
|
}
|
2014-01-08 18:31:20 +00:00
|
|
|
if len(res) == 0 {
|
2014-02-05 02:33:15 +00:00
|
|
|
return idx, false, ""
|
2013-12-11 22:27:27 +00:00
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
return idx, true, res[0].(*structs.Node).Address
|
2013-12-11 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 22:03:09 +00:00
|
|
|
// GetNodes returns all the known nodes, the slice alternates between
|
|
|
|
// the node name and address
|
2014-02-05 02:33:15 +00:00
|
|
|
func (s *StateStore) Nodes() (uint64, structs.Nodes) {
|
|
|
|
idx, res, err := s.nodeTable.Get("id")
|
2013-12-11 22:03:09 +00:00
|
|
|
if err != nil {
|
2014-03-05 23:02:28 +00:00
|
|
|
s.logger.Printf("[ERR] consul.state: Error getting nodes: %v", err)
|
2013-12-11 22:03:09 +00:00
|
|
|
}
|
2014-01-08 18:31:20 +00:00
|
|
|
results := make([]structs.Node, len(res))
|
|
|
|
for i, r := range res {
|
|
|
|
results[i] = *r.(*structs.Node)
|
2013-12-11 22:03:09 +00:00
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
return idx, results
|
2013-12-11 22:03:09 +00:00
|
|
|
}
|
2013-12-11 22:27:27 +00:00
|
|
|
|
|
|
|
// EnsureService is used to ensure a given node exposes a service
|
2014-02-05 02:33:15 +00:00
|
|
|
func (s *StateStore) EnsureService(index uint64, node string, ns *structs.NodeService) error {
|
2014-02-05 03:01:32 +00:00
|
|
|
tables := MDBTables{s.nodeTable, s.serviceTable}
|
|
|
|
tx, err := tables.StartTxn(false)
|
2014-01-08 19:35:27 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("Failed to start txn: %v", err))
|
|
|
|
}
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2014-01-08 18:31:20 +00:00
|
|
|
// Ensure the node exists
|
2014-02-05 02:33:15 +00:00
|
|
|
res, err := s.nodeTable.GetTxn(tx, "id", node)
|
2013-12-18 23:03:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-08 18:31:20 +00:00
|
|
|
if len(res) == 0 {
|
|
|
|
return fmt.Errorf("Missing node registration")
|
2013-12-18 23:03:25 +00:00
|
|
|
}
|
|
|
|
|
2014-01-08 18:31:20 +00:00
|
|
|
// Create the entry
|
|
|
|
entry := structs.ServiceNode{
|
2014-02-05 02:33:15 +00:00
|
|
|
Node: node,
|
|
|
|
ServiceID: ns.ID,
|
|
|
|
ServiceName: ns.Service,
|
|
|
|
ServiceTag: ns.Tag,
|
|
|
|
ServicePort: ns.Port,
|
2013-12-18 23:03:25 +00:00
|
|
|
}
|
|
|
|
|
2014-01-08 18:31:20 +00:00
|
|
|
// Ensure the service entry is set
|
2014-01-08 19:35:27 +00:00
|
|
|
if err := s.serviceTable.InsertTxn(tx, &entry); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := s.serviceTable.SetLastIndexTxn(tx, index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-05 02:45:40 +00:00
|
|
|
defer s.watch[s.serviceTable].Notify()
|
2014-01-08 19:35:27 +00:00
|
|
|
return tx.Commit()
|
2013-12-11 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NodeServices is used to return all the services of a given node
|
2014-02-05 02:33:15 +00:00
|
|
|
func (s *StateStore) NodeServices(name string) (uint64, *structs.NodeServices) {
|
2014-02-05 19:10:10 +00:00
|
|
|
tables := s.queryTables["NodeServices"]
|
2014-02-05 03:01:32 +00:00
|
|
|
tx, err := tables.StartTxn(true)
|
2013-12-18 23:03:25 +00:00
|
|
|
if err != nil {
|
2014-01-08 18:31:20 +00:00
|
|
|
panic(fmt.Errorf("Failed to start txn: %v", err))
|
2013-12-18 23:03:25 +00:00
|
|
|
}
|
|
|
|
defer tx.Abort()
|
2014-02-05 03:01:32 +00:00
|
|
|
return s.parseNodeServices(tables, tx, name)
|
2013-12-18 23:03:25 +00:00
|
|
|
}
|
|
|
|
|
2014-01-08 18:31:20 +00:00
|
|
|
// parseNodeServices is used to get the services belonging to a
|
|
|
|
// node, using a given txn
|
2014-02-05 03:01:32 +00:00
|
|
|
func (s *StateStore) parseNodeServices(tables MDBTables, tx *MDBTxn, name string) (uint64, *structs.NodeServices) {
|
2014-01-08 18:31:20 +00:00
|
|
|
ns := &structs.NodeServices{
|
|
|
|
Services: make(map[string]*structs.NodeService),
|
|
|
|
}
|
2013-12-12 22:41:13 +00:00
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
// Get the maximum index
|
2014-02-05 03:01:32 +00:00
|
|
|
index, err := tables.LastIndexTxn(tx)
|
2014-02-05 02:33:15 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("Failed to get last index: %v", err))
|
|
|
|
}
|
|
|
|
|
2014-01-08 18:31:20 +00:00
|
|
|
// Get the node first
|
|
|
|
res, err := s.nodeTable.GetTxn(tx, "id", name)
|
2013-12-11 22:27:27 +00:00
|
|
|
if err != nil {
|
2014-03-05 23:02:28 +00:00
|
|
|
s.logger.Printf("[ERR] consul.state: Failed to get node: %v", err)
|
2014-01-08 18:31:20 +00:00
|
|
|
}
|
|
|
|
if len(res) == 0 {
|
2014-03-05 23:02:28 +00:00
|
|
|
return index, nil
|
2013-12-11 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
2014-01-08 18:31:20 +00:00
|
|
|
// Set the address
|
|
|
|
node := res[0].(*structs.Node)
|
2014-01-08 22:43:36 +00:00
|
|
|
ns.Node = *node
|
2013-12-18 23:03:25 +00:00
|
|
|
|
2014-01-08 18:31:20 +00:00
|
|
|
// Get the services
|
|
|
|
res, err = s.serviceTable.GetTxn(tx, "id", name)
|
|
|
|
if err != nil {
|
2014-03-05 23:02:28 +00:00
|
|
|
s.logger.Printf("[ERR] consul.state: Failed to get node '%s' services: %v", name, err)
|
2014-01-08 18:31:20 +00:00
|
|
|
}
|
2013-12-18 23:03:25 +00:00
|
|
|
|
2014-01-08 18:31:20 +00:00
|
|
|
// Add each service
|
|
|
|
for _, r := range res {
|
|
|
|
service := r.(*structs.ServiceNode)
|
|
|
|
srv := &structs.NodeService{
|
|
|
|
ID: service.ServiceID,
|
|
|
|
Service: service.ServiceName,
|
|
|
|
Tag: service.ServiceTag,
|
|
|
|
Port: service.ServicePort,
|
2013-12-18 23:03:25 +00:00
|
|
|
}
|
2014-01-08 18:31:20 +00:00
|
|
|
ns.Services[srv.ID] = srv
|
2013-12-11 22:27:27 +00:00
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
return index, ns
|
2013-12-11 22:27:27 +00:00
|
|
|
}
|
2013-12-11 23:34:10 +00:00
|
|
|
|
|
|
|
// DeleteNodeService is used to delete a node service
|
2014-02-05 02:33:15 +00:00
|
|
|
func (s *StateStore) DeleteNodeService(index uint64, node, id string) error {
|
2014-02-05 03:01:32 +00:00
|
|
|
tables := MDBTables{s.serviceTable, s.checkTable}
|
|
|
|
tx, err := tables.StartTxn(false)
|
2014-01-08 19:35:27 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("Failed to start txn: %v", err))
|
|
|
|
}
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if n, err := s.serviceTable.DeleteTxn(tx, "id", node, id); err != nil {
|
2014-01-08 19:35:27 +00:00
|
|
|
return err
|
2014-02-05 02:33:15 +00:00
|
|
|
} else if n > 0 {
|
|
|
|
if err := s.serviceTable.SetLastIndexTxn(tx, index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-05 02:45:40 +00:00
|
|
|
defer s.watch[s.serviceTable].Notify()
|
2014-01-08 19:35:27 +00:00
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
if n, err := s.checkTable.DeleteTxn(tx, "node", node, id); err != nil {
|
2014-01-08 19:35:27 +00:00
|
|
|
return err
|
2014-02-05 02:33:15 +00:00
|
|
|
} else if n > 0 {
|
|
|
|
if err := s.checkTable.SetLastIndexTxn(tx, index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-05 02:45:40 +00:00
|
|
|
defer s.watch[s.checkTable].Notify()
|
2014-01-08 19:35:27 +00:00
|
|
|
}
|
|
|
|
return tx.Commit()
|
2013-12-11 23:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteNode is used to delete a node and all it's services
|
2014-02-05 02:33:15 +00:00
|
|
|
func (s *StateStore) DeleteNode(index uint64, node string) error {
|
2014-02-05 03:01:32 +00:00
|
|
|
tables := MDBTables{s.nodeTable, s.serviceTable, s.checkTable}
|
|
|
|
tx, err := tables.StartTxn(false)
|
2014-01-08 18:37:16 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("Failed to start txn: %v", err))
|
|
|
|
}
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
if n, err := s.serviceTable.DeleteTxn(tx, "id", node); err != nil {
|
2013-12-18 23:03:25 +00:00
|
|
|
return err
|
2014-02-05 02:33:15 +00:00
|
|
|
} else if n > 0 {
|
|
|
|
if err := s.serviceTable.SetLastIndexTxn(tx, index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-05 02:45:40 +00:00
|
|
|
defer s.watch[s.serviceTable].Notify()
|
2013-12-18 23:03:25 +00:00
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
if n, err := s.checkTable.DeleteTxn(tx, "id", node); err != nil {
|
2014-01-08 19:35:27 +00:00
|
|
|
return err
|
2014-02-05 02:33:15 +00:00
|
|
|
} else if n > 0 {
|
|
|
|
if err := s.checkTable.SetLastIndexTxn(tx, index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-05 02:45:40 +00:00
|
|
|
defer s.watch[s.checkTable].Notify()
|
2014-01-08 19:35:27 +00:00
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
if n, err := s.nodeTable.DeleteTxn(tx, "id", node); err != nil {
|
2014-01-08 18:31:20 +00:00
|
|
|
return err
|
2014-02-05 02:33:15 +00:00
|
|
|
} else if n > 0 {
|
|
|
|
if err := s.nodeTable.SetLastIndexTxn(tx, index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-05 02:45:40 +00:00
|
|
|
defer s.watch[s.nodeTable].Notify()
|
2013-12-18 23:03:25 +00:00
|
|
|
}
|
2014-01-08 18:37:16 +00:00
|
|
|
return tx.Commit()
|
2013-12-11 23:34:10 +00:00
|
|
|
}
|
2013-12-12 19:07:14 +00:00
|
|
|
|
|
|
|
// Services is used to return all the services with a list of associated tags
|
2014-02-05 02:33:15 +00:00
|
|
|
func (s *StateStore) Services() (uint64, map[string][]string) {
|
2014-01-08 18:31:20 +00:00
|
|
|
// TODO: Optimize to not table scan.. We can do a distinct
|
|
|
|
// type of query to avoid this
|
2014-03-05 23:02:28 +00:00
|
|
|
services := make(map[string][]string)
|
2014-02-05 02:33:15 +00:00
|
|
|
idx, res, err := s.serviceTable.Get("id")
|
2013-12-18 23:03:25 +00:00
|
|
|
if err != nil {
|
2014-03-05 23:02:28 +00:00
|
|
|
s.logger.Printf("[ERR] consul.state: Failed to get services: %v", err)
|
|
|
|
return idx, services
|
2013-12-18 23:03:25 +00:00
|
|
|
}
|
2014-01-08 18:31:20 +00:00
|
|
|
for _, r := range res {
|
|
|
|
srv := r.(*structs.ServiceNode)
|
|
|
|
|
|
|
|
tags := services[srv.ServiceName]
|
|
|
|
if !strContains(tags, srv.ServiceTag) {
|
|
|
|
tags = append(tags, srv.ServiceTag)
|
|
|
|
services[srv.ServiceName] = tags
|
2013-12-18 23:03:25 +00:00
|
|
|
}
|
2013-12-12 19:07:14 +00:00
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
return idx, services
|
2013-12-12 19:07:14 +00:00
|
|
|
}
|
2013-12-12 19:37:19 +00:00
|
|
|
|
|
|
|
// ServiceNodes returns the nodes associated with a given service
|
2014-02-05 02:33:15 +00:00
|
|
|
func (s *StateStore) ServiceNodes(service string) (uint64, structs.ServiceNodes) {
|
2014-02-05 19:10:10 +00:00
|
|
|
tables := s.queryTables["ServiceNodes"]
|
2014-02-05 03:01:32 +00:00
|
|
|
tx, err := tables.StartTxn(true)
|
2013-12-18 23:03:25 +00:00
|
|
|
if err != nil {
|
2014-01-08 18:31:20 +00:00
|
|
|
panic(fmt.Errorf("Failed to start txn: %v", err))
|
2013-12-18 23:03:25 +00:00
|
|
|
}
|
|
|
|
defer tx.Abort()
|
2014-01-08 18:31:20 +00:00
|
|
|
|
2014-02-05 03:01:32 +00:00
|
|
|
idx, err := tables.LastIndexTxn(tx)
|
2014-02-05 02:33:15 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("Failed to get last index: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := s.serviceTable.GetTxn(tx, "service", service)
|
2014-03-05 23:02:28 +00:00
|
|
|
return idx, s.parseServiceNodes(tx, s.nodeTable, res, err)
|
2013-12-12 19:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceTagNodes returns the nodes associated with a given service matching a tag
|
2014-02-05 02:33:15 +00:00
|
|
|
func (s *StateStore) ServiceTagNodes(service, tag string) (uint64, structs.ServiceNodes) {
|
2014-02-05 19:10:10 +00:00
|
|
|
tables := s.queryTables["ServiceNodes"]
|
2014-02-05 03:01:32 +00:00
|
|
|
tx, err := tables.StartTxn(true)
|
2013-12-12 19:37:19 +00:00
|
|
|
if err != nil {
|
2014-01-08 18:31:20 +00:00
|
|
|
panic(fmt.Errorf("Failed to start txn: %v", err))
|
2013-12-12 19:37:19 +00:00
|
|
|
}
|
2013-12-18 23:03:25 +00:00
|
|
|
defer tx.Abort()
|
2014-01-08 18:31:20 +00:00
|
|
|
|
2014-02-05 03:01:32 +00:00
|
|
|
idx, err := tables.LastIndexTxn(tx)
|
2014-02-05 02:33:15 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("Failed to get last index: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := s.serviceTable.GetTxn(tx, "service", service, tag)
|
2014-03-05 23:02:28 +00:00
|
|
|
return idx, s.parseServiceNodes(tx, s.nodeTable, res, err)
|
2013-12-12 19:37:19 +00:00
|
|
|
}
|
2013-12-12 23:14:08 +00:00
|
|
|
|
2013-12-18 23:03:25 +00:00
|
|
|
// parseServiceNodes parses results ServiceNodes and ServiceTagNodes
|
2014-03-05 23:02:28 +00:00
|
|
|
func (s *StateStore) parseServiceNodes(tx *MDBTxn, table *MDBTable, res []interface{}, err error) structs.ServiceNodes {
|
|
|
|
nodes := make(structs.ServiceNodes, len(res))
|
2013-12-12 23:14:08 +00:00
|
|
|
if err != nil {
|
2014-03-05 23:02:28 +00:00
|
|
|
s.logger.Printf("[ERR] consul.state: Failed to get service nodes: %v", err)
|
|
|
|
return nodes
|
2013-12-12 23:14:08 +00:00
|
|
|
}
|
|
|
|
|
2014-01-08 18:31:20 +00:00
|
|
|
for i, r := range res {
|
|
|
|
srv := r.(*structs.ServiceNode)
|
2013-12-12 23:14:08 +00:00
|
|
|
|
2014-01-08 18:31:20 +00:00
|
|
|
// Get the address of the node
|
|
|
|
nodeRes, err := table.GetTxn(tx, "id", srv.Node)
|
|
|
|
if err != nil || len(nodeRes) != 1 {
|
2014-03-05 23:02:28 +00:00
|
|
|
s.logger.Printf("[ERR] consul.state: Failed to join service node %#v with node: %v", *srv, err)
|
|
|
|
continue
|
2013-12-12 23:14:08 +00:00
|
|
|
}
|
2014-01-08 18:31:20 +00:00
|
|
|
srv.Address = nodeRes[0].(*structs.Node).Address
|
2013-12-12 23:14:08 +00:00
|
|
|
|
2014-01-08 18:31:20 +00:00
|
|
|
nodes[i] = *srv
|
2013-12-12 23:14:08 +00:00
|
|
|
}
|
2014-01-08 18:31:20 +00:00
|
|
|
|
2013-12-18 23:03:25 +00:00
|
|
|
return nodes
|
|
|
|
}
|
2013-12-12 23:14:08 +00:00
|
|
|
|
2014-01-08 19:35:27 +00:00
|
|
|
// EnsureCheck is used to create a check or updates it's state
|
2014-02-05 02:33:15 +00:00
|
|
|
func (s *StateStore) EnsureCheck(index uint64, check *structs.HealthCheck) error {
|
2014-01-08 20:15:01 +00:00
|
|
|
// Ensure we have a status
|
|
|
|
if check.Status == "" {
|
|
|
|
check.Status = structs.HealthUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the txn
|
2014-02-05 03:01:32 +00:00
|
|
|
tables := MDBTables{s.nodeTable, s.serviceTable, s.checkTable}
|
|
|
|
tx, err := tables.StartTxn(false)
|
2014-01-08 20:15:01 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("Failed to start txn: %v", err))
|
|
|
|
}
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Ensure the node exists
|
|
|
|
res, err := s.nodeTable.GetTxn(tx, "id", check.Node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(res) == 0 {
|
|
|
|
return fmt.Errorf("Missing node registration")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the service exists if specified
|
|
|
|
if check.ServiceID != "" {
|
|
|
|
res, err = s.serviceTable.GetTxn(tx, "id", check.Node, check.ServiceID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(res) == 0 {
|
|
|
|
return fmt.Errorf("Missing service registration")
|
|
|
|
}
|
|
|
|
// Ensure we set the correct service
|
|
|
|
srv := res[0].(*structs.ServiceNode)
|
|
|
|
check.ServiceName = srv.ServiceName
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the check is set
|
|
|
|
if err := s.checkTable.InsertTxn(tx, check); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
if err := s.checkTable.SetLastIndexTxn(tx, index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-05 02:45:40 +00:00
|
|
|
defer s.watch[s.checkTable].Notify()
|
2014-01-08 20:15:01 +00:00
|
|
|
return tx.Commit()
|
2014-01-08 19:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteNodeCheck is used to delete a node health check
|
2014-02-05 02:33:15 +00:00
|
|
|
func (s *StateStore) DeleteNodeCheck(index uint64, node, id string) error {
|
|
|
|
tx, err := s.checkTable.StartTxn(false, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
if n, err := s.checkTable.DeleteTxn(tx, "id", node, id); err != nil {
|
|
|
|
return err
|
|
|
|
} else if n > 0 {
|
|
|
|
if err := s.checkTable.SetLastIndexTxn(tx, index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-05 02:45:40 +00:00
|
|
|
defer s.watch[s.checkTable].Notify()
|
2014-02-05 02:33:15 +00:00
|
|
|
}
|
|
|
|
return tx.Commit()
|
2014-01-08 19:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NodeChecks is used to get all the checks for a node
|
2014-02-05 02:33:15 +00:00
|
|
|
func (s *StateStore) NodeChecks(node string) (uint64, structs.HealthChecks) {
|
2014-03-05 23:02:28 +00:00
|
|
|
return s.parseHealthChecks(s.checkTable.Get("id", node))
|
2014-01-08 19:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceChecks is used to get all the checks for a service
|
2014-02-05 02:33:15 +00:00
|
|
|
func (s *StateStore) ServiceChecks(service string) (uint64, structs.HealthChecks) {
|
2014-03-05 23:02:28 +00:00
|
|
|
return s.parseHealthChecks(s.checkTable.Get("service", service))
|
2014-01-08 19:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckInState is used to get all the checks for a service in a given state
|
2014-02-05 02:33:15 +00:00
|
|
|
func (s *StateStore) ChecksInState(state string) (uint64, structs.HealthChecks) {
|
2014-03-05 23:02:28 +00:00
|
|
|
return s.parseHealthChecks(s.checkTable.Get("status", state))
|
2014-01-08 20:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseHealthChecks is used to handle the resutls of a Get against
|
|
|
|
// the checkTable
|
2014-03-05 23:02:28 +00:00
|
|
|
func (s *StateStore) parseHealthChecks(idx uint64, res []interface{}, err error) (uint64, structs.HealthChecks) {
|
|
|
|
results := make([]*structs.HealthCheck, len(res))
|
2014-01-08 20:15:01 +00:00
|
|
|
if err != nil {
|
2014-03-05 23:02:28 +00:00
|
|
|
s.logger.Printf("[ERR] consul.state: Failed to get health checks: %v", err)
|
|
|
|
return idx, results
|
2014-01-08 20:15:01 +00:00
|
|
|
}
|
|
|
|
for i, r := range res {
|
|
|
|
results[i] = r.(*structs.HealthCheck)
|
|
|
|
}
|
2014-02-05 02:33:15 +00:00
|
|
|
return idx, results
|
2014-01-08 19:35:27 +00:00
|
|
|
}
|
|
|
|
|
2014-01-08 22:58:53 +00:00
|
|
|
// CheckServiceNodes returns the nodes associated with a given service, along
|
|
|
|
// with any associated check
|
2014-02-05 02:33:15 +00:00
|
|
|
func (s *StateStore) CheckServiceNodes(service string) (uint64, structs.CheckServiceNodes) {
|
2014-02-05 21:30:18 +00:00
|
|
|
tables := s.queryTables["CheckServiceNodes"]
|
2014-02-05 03:01:32 +00:00
|
|
|
tx, err := tables.StartTxn(true)
|
2014-01-08 22:58:53 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("Failed to start txn: %v", err))
|
|
|
|
}
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2014-02-05 03:01:32 +00:00
|
|
|
idx, err := tables.LastIndexTxn(tx)
|
2014-02-05 02:33:15 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("Failed to get last index: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := s.serviceTable.GetTxn(tx, "service", service)
|
|
|
|
return idx, s.parseCheckServiceNodes(tx, res, err)
|
2014-01-08 22:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckServiceNodes returns the nodes associated with a given service, along
|
|
|
|
// with any associated checks
|
2014-02-05 02:33:15 +00:00
|
|
|
func (s *StateStore) CheckServiceTagNodes(service, tag string) (uint64, structs.CheckServiceNodes) {
|
2014-02-05 21:30:18 +00:00
|
|
|
tables := s.queryTables["CheckServiceNodes"]
|
2014-02-05 03:01:32 +00:00
|
|
|
tx, err := tables.StartTxn(true)
|
2014-01-08 22:58:53 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("Failed to start txn: %v", err))
|
|
|
|
}
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2014-02-05 03:01:32 +00:00
|
|
|
idx, err := tables.LastIndexTxn(tx)
|
2014-02-05 02:33:15 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("Failed to get last index: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := s.serviceTable.GetTxn(tx, "service", service, tag)
|
|
|
|
return idx, s.parseCheckServiceNodes(tx, res, err)
|
2014-01-08 22:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseCheckServiceNodes parses results CheckServiceNodes and CheckServiceTagNodes
|
|
|
|
func (s *StateStore) parseCheckServiceNodes(tx *MDBTxn, res []interface{}, err error) structs.CheckServiceNodes {
|
2014-03-05 23:02:28 +00:00
|
|
|
nodes := make(structs.CheckServiceNodes, len(res))
|
2014-01-08 22:58:53 +00:00
|
|
|
if err != nil {
|
2014-03-05 23:02:28 +00:00
|
|
|
s.logger.Printf("[ERR] consul.state: Failed to get service nodes: %v", err)
|
|
|
|
return nodes
|
2014-01-08 22:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, r := range res {
|
|
|
|
srv := r.(*structs.ServiceNode)
|
|
|
|
|
|
|
|
// Get the node
|
|
|
|
nodeRes, err := s.nodeTable.GetTxn(tx, "id", srv.Node)
|
|
|
|
if err != nil || len(nodeRes) != 1 {
|
2014-03-05 23:02:28 +00:00
|
|
|
s.logger.Printf("[ERR] consul.state: Failed to join service node %#v with node: %v", *srv, err)
|
|
|
|
continue
|
2014-01-08 22:58:53 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 21:14:28 +00:00
|
|
|
// Get any associated checks of the service
|
2014-02-05 02:33:15 +00:00
|
|
|
res, err := s.checkTable.GetTxn(tx, "node", srv.Node, srv.ServiceID)
|
2014-03-05 23:02:28 +00:00
|
|
|
_, checks := s.parseHealthChecks(0, res, err)
|
2014-01-08 22:58:53 +00:00
|
|
|
|
2014-01-15 21:14:28 +00:00
|
|
|
// Get any checks of the node, not assciated with any service
|
2014-02-05 02:33:15 +00:00
|
|
|
res, err = s.checkTable.GetTxn(tx, "node", srv.Node, "")
|
2014-03-05 23:02:28 +00:00
|
|
|
_, nodeChecks := s.parseHealthChecks(0, res, err)
|
2014-01-15 21:14:28 +00:00
|
|
|
checks = append(checks, nodeChecks...)
|
|
|
|
|
2014-01-08 22:58:53 +00:00
|
|
|
// Setup the node
|
|
|
|
nodes[i].Node = *nodeRes[0].(*structs.Node)
|
|
|
|
nodes[i].Service = structs.NodeService{
|
|
|
|
ID: srv.ServiceID,
|
|
|
|
Service: srv.ServiceName,
|
|
|
|
Tag: srv.ServiceTag,
|
|
|
|
Port: srv.ServicePort,
|
|
|
|
}
|
|
|
|
nodes[i].Checks = checks
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2014-03-31 19:13:57 +00:00
|
|
|
// KVSSet is used to create or update a KV entry
|
|
|
|
func (s *StateStore) KVSSet(index uint64, d *structs.DirEntry) error {
|
|
|
|
// Start a new txn
|
|
|
|
tx, err := s.kvsTable.StartTxn(false, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Get the existing node
|
|
|
|
res, err := s.kvsTable.GetTxn(tx, "id", d.Key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the create and modify times
|
|
|
|
if len(res) == 0 {
|
|
|
|
d.CreateIndex = index
|
|
|
|
} else {
|
|
|
|
d.CreateIndex = res[0].(*structs.DirEntry).CreateIndex
|
|
|
|
}
|
|
|
|
d.ModifyIndex = index
|
|
|
|
|
|
|
|
if err := s.kvsTable.InsertTxn(tx, d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.kvsTable.SetLastIndexTxn(tx, index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.watch[s.kvsTable].Notify()
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// KVSGet is used to get a KV entry
|
|
|
|
func (s *StateStore) KVSGet(key string) (uint64, *structs.DirEntry, error) {
|
|
|
|
idx, res, err := s.kvsTable.Get("id", key)
|
|
|
|
var d *structs.DirEntry
|
|
|
|
if len(res) > 0 {
|
|
|
|
d = res[0].(*structs.DirEntry)
|
|
|
|
}
|
|
|
|
return idx, d, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// KVSList is used to list all KV entries with a prefix
|
2014-03-31 20:12:35 +00:00
|
|
|
func (s *StateStore) KVSList(prefix string) (uint64, structs.DirEntries, error) {
|
|
|
|
idx, res, err := s.kvsTable.Get("id_prefix", prefix)
|
|
|
|
ents := make(structs.DirEntries, len(res))
|
|
|
|
for idx, r := range res {
|
|
|
|
ents[idx] = r.(*structs.DirEntry)
|
|
|
|
}
|
|
|
|
return idx, ents, err
|
2014-03-31 19:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// KVSDelete is used to delete a KVS entry
|
2014-03-31 19:24:06 +00:00
|
|
|
func (s *StateStore) KVSDelete(index uint64, key string) error {
|
2014-03-31 20:20:08 +00:00
|
|
|
return s.kvsDeleteWithIndex(index, "id", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// KVSDeleteTree is used to delete all keys with a given prefix
|
|
|
|
func (s *StateStore) KVSDeleteTree(index uint64, prefix string) error {
|
|
|
|
return s.kvsDeleteWithIndex(index, "id_prefix", prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// kvsDeleteWithIndex does a delete with either the id or id_prefix
|
|
|
|
func (s *StateStore) kvsDeleteWithIndex(index uint64, tableIndex, val string) error {
|
2014-03-31 19:24:06 +00:00
|
|
|
// Start a new txn
|
|
|
|
tx, err := s.kvsTable.StartTxn(false, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2014-03-31 20:20:08 +00:00
|
|
|
num, err := s.kvsTable.DeleteTxn(tx, tableIndex, val)
|
2014-03-31 19:24:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if num > 0 {
|
|
|
|
if err := s.kvsTable.SetLastIndexTxn(tx, index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.watch[s.kvsTable].Notify()
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
2014-03-31 19:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// KVSCheckAndSet is used to perform an atomic check-and-set
|
2014-03-31 19:37:11 +00:00
|
|
|
func (s *StateStore) KVSCheckAndSet(index uint64, d *structs.DirEntry) (bool, error) {
|
|
|
|
// Start a new txn
|
|
|
|
tx, err := s.kvsTable.StartTxn(false, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Get the existing node
|
|
|
|
res, err := s.kvsTable.GetTxn(tx, "id", d.Key)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the existing node if any
|
|
|
|
var exist *structs.DirEntry
|
|
|
|
if len(res) > 0 {
|
|
|
|
exist = res[0].(*structs.DirEntry)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the ModifyIndex as the constraint. A modify of time of 0
|
|
|
|
// means we are doing a set-if-not-exists, while any other value
|
|
|
|
// means we expect that modify time.
|
|
|
|
if d.ModifyIndex == 0 && exist != nil {
|
|
|
|
return false, nil
|
|
|
|
} else if d.ModifyIndex > 0 && (exist == nil || exist.ModifyIndex != d.ModifyIndex) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the create and modify times
|
|
|
|
if exist == nil {
|
|
|
|
d.CreateIndex = index
|
|
|
|
} else {
|
|
|
|
d.CreateIndex = exist.CreateIndex
|
|
|
|
}
|
|
|
|
d.ModifyIndex = index
|
|
|
|
|
|
|
|
if err := s.kvsTable.InsertTxn(tx, d); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if err := s.kvsTable.SetLastIndexTxn(tx, index); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer s.watch[s.kvsTable].Notify()
|
|
|
|
return true, tx.Commit()
|
2014-03-31 19:13:57 +00:00
|
|
|
}
|
|
|
|
|
2013-12-18 23:03:25 +00:00
|
|
|
// Snapshot is used to create a point in time snapshot
|
2013-12-18 23:09:38 +00:00
|
|
|
func (s *StateStore) Snapshot() (*StateSnapshot, error) {
|
2014-01-08 18:31:20 +00:00
|
|
|
// Begin a new txn on all tables
|
|
|
|
tx, err := s.tables.StartTxn(true)
|
2013-12-18 23:09:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
// Determine the max index
|
|
|
|
index, err := s.tables.LastIndexTxn(tx)
|
|
|
|
if err != nil {
|
|
|
|
tx.Abort()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-12-18 23:09:38 +00:00
|
|
|
// Return the snapshot
|
|
|
|
snap := &StateSnapshot{
|
2014-02-05 02:33:15 +00:00
|
|
|
store: s,
|
|
|
|
tx: tx,
|
|
|
|
lastIndex: index,
|
2013-12-18 23:09:38 +00:00
|
|
|
}
|
|
|
|
return snap, nil
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
// LastIndex returns the last index that affects the snapshotted data
|
|
|
|
func (s *StateSnapshot) LastIndex() uint64 {
|
|
|
|
return s.lastIndex
|
|
|
|
}
|
|
|
|
|
2013-12-18 23:09:38 +00:00
|
|
|
// Nodes returns all the known nodes, the slice alternates between
|
|
|
|
// the node name and address
|
2014-01-08 18:31:20 +00:00
|
|
|
func (s *StateSnapshot) Nodes() structs.Nodes {
|
|
|
|
res, err := s.store.nodeTable.GetTxn(s.tx, "id")
|
2013-12-18 23:09:38 +00:00
|
|
|
if err != nil {
|
2014-03-05 23:02:28 +00:00
|
|
|
s.store.logger.Printf("[ERR] consul.state: Failed to get nodes: %v", err)
|
|
|
|
return nil
|
2013-12-18 23:09:38 +00:00
|
|
|
}
|
2014-01-08 18:31:20 +00:00
|
|
|
results := make([]structs.Node, len(res))
|
|
|
|
for i, r := range res {
|
|
|
|
results[i] = *r.(*structs.Node)
|
2013-12-18 23:09:38 +00:00
|
|
|
}
|
2014-01-08 18:31:20 +00:00
|
|
|
return results
|
2013-12-18 23:09:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NodeServices is used to return all the services of a given node
|
2014-01-03 01:29:39 +00:00
|
|
|
func (s *StateSnapshot) NodeServices(name string) *structs.NodeServices {
|
2014-02-05 03:01:32 +00:00
|
|
|
_, res := s.store.parseNodeServices(s.store.tables, s.tx, name)
|
2014-02-05 02:33:15 +00:00
|
|
|
return res
|
2013-12-24 21:25:09 +00:00
|
|
|
}
|
2014-01-08 20:19:11 +00:00
|
|
|
|
|
|
|
// NodeChecks is used to return all the checks of a given node
|
|
|
|
func (s *StateSnapshot) NodeChecks(node string) structs.HealthChecks {
|
2014-02-05 02:33:15 +00:00
|
|
|
res, err := s.store.checkTable.GetTxn(s.tx, "id", node)
|
2014-03-05 23:02:28 +00:00
|
|
|
_, checks := s.store.parseHealthChecks(s.lastIndex, res, err)
|
2014-02-05 02:33:15 +00:00
|
|
|
return checks
|
2014-01-08 20:19:11 +00:00
|
|
|
}
|
2014-03-31 20:31:31 +00:00
|
|
|
|
|
|
|
// KVSDump is used to list all KV entries
|
|
|
|
func (s *StateSnapshot) KVSDump() structs.DirEntries {
|
|
|
|
res, err := s.store.kvsTable.GetTxn(s.tx, "id")
|
|
|
|
if err != nil {
|
|
|
|
s.store.logger.Printf("[ERR] consul.state: Failed to get KVS entries: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ents := make(structs.DirEntries, len(res))
|
|
|
|
for idx, r := range res {
|
|
|
|
ents[idx] = r.(*structs.DirEntry)
|
|
|
|
}
|
|
|
|
return ents
|
|
|
|
}
|