From 266c6736bf3a619adc37dee8fe4419e56c62b195 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Wed, 8 Jan 2014 11:21:29 -0800 Subject: [PATCH] Adding HealthCheck and an associated table --- consul/state_store.go | 68 ++++++++++++++++++++++++++------------- consul/structs/structs.go | 18 +++++++++++ 2 files changed, 64 insertions(+), 22 deletions(-) diff --git a/consul/state_store.go b/consul/state_store.go index 1b22063725..f5b19e44c0 100644 --- a/consul/state_store.go +++ b/consul/state_store.go @@ -11,6 +11,7 @@ import ( const ( dbNodes = "nodes" dbServices = "services" + dbChecks = "checks" dbMaxMapSize = 1024 * 1024 * 1024 // 1GB maximum size ) @@ -26,6 +27,7 @@ type StateStore struct { env *mdb.Env nodeTable *MDBTable serviceTable *MDBTable + checkTable *MDBTable tables MDBTables } @@ -97,9 +99,17 @@ func (s *StateStore) initialize() error { return err } + // 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:] + } + // Setup our tables s.nodeTable = &MDBTable{ - Env: s.env, Name: dbNodes, Indexes: map[string]*MDBIndex{ "id": &MDBIndex{ @@ -107,13 +117,6 @@ func (s *StateStore) initialize() error { Fields: []string{"Node"}, }, }, - Encoder: func(obj interface{}) []byte { - buf, err := structs.Encode(255, obj) - if err != nil { - panic(err) - } - return buf[1:] - }, Decoder: func(buf []byte) interface{} { out := new(structs.Node) if err := structs.Decode(buf, out); err != nil { @@ -122,12 +125,8 @@ func (s *StateStore) initialize() error { return out }, } - if err := s.nodeTable.Init(); err != nil { - return err - } s.serviceTable = &MDBTable{ - Env: s.env, Name: dbServices, Indexes: map[string]*MDBIndex{ "id": &MDBIndex{ @@ -139,13 +138,6 @@ func (s *StateStore) initialize() error { Fields: []string{"ServiceName", "ServiceTag"}, }, }, - Encoder: func(obj interface{}) []byte { - buf, err := structs.Encode(255, obj) - if err != nil { - panic(err) - } - return buf[1:] - }, Decoder: func(buf []byte) interface{} { out := new(structs.ServiceNode) if err := structs.Decode(buf, out); err != nil { @@ -154,12 +146,44 @@ func (s *StateStore) initialize() error { return out }, } - if err := s.serviceTable.Init(); err != nil { - return err + + 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"}, + }, + }, + Decoder: func(buf []byte) interface{} { + out := new(structs.HealthCheck) + if err := structs.Decode(buf, out); err != nil { + panic(err) + } + return out + }, } // Store the set of tables - s.tables = []*MDBTable{s.nodeTable, s.serviceTable} + s.tables = []*MDBTable{s.nodeTable, s.serviceTable, s.checkTable} + for _, table := range s.tables { + table.Env = s.env + table.Encoder = encoder + if err := table.Init(); err != nil { + return err + } + } return nil } diff --git a/consul/structs/structs.go b/consul/structs/structs.go index 3db95ab20e..2c4bdde6a8 100644 --- a/consul/structs/structs.go +++ b/consul/structs/structs.go @@ -19,6 +19,13 @@ const ( DeregisterRequestType ) +const ( + HealthUnknown = "unknown" + HealthPassing = "passing" + HealthWarning = "warning" + HealthCritical = "critical" +) + // RegisterRequest is used for the Catalog.Register endpoint // to register a node as providing a service. If no service // is provided, the node is registered. @@ -89,6 +96,17 @@ type NodeServices struct { Services map[string]*NodeService } +// HealthCheck represents a single check on a given node +type HealthCheck struct { + Node string + CheckID string // Unique per-node ID + Name string // Check name + Status string // The current check status + Notes string // Additional notes with the status + ServiceID string // optional associated service + ServiceName string // optional service name +} + // Decode is used to decode a MsgPack encoded object func Decode(buf []byte, out interface{}) error { var handle codec.MsgpackHandle