2013-12-19 20:03:57 +00:00
|
|
|
package structs
|
2013-12-11 22:04:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"github.com/ugorji/go/codec"
|
2014-02-05 18:21:31 +00:00
|
|
|
"time"
|
2013-12-11 22:04:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2013-12-19 23:08:55 +00:00
|
|
|
ErrNoLeader = fmt.Errorf("No cluster leader")
|
|
|
|
ErrNoDCPath = fmt.Errorf("No path to datacenter")
|
|
|
|
ErrNoServers = fmt.Errorf("No known Consul servers")
|
2013-12-11 22:04:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MessageType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
RegisterRequestType MessageType = iota
|
|
|
|
DeregisterRequestType
|
2014-03-31 20:41:43 +00:00
|
|
|
KVSRequestType
|
2013-12-11 22:04:44 +00:00
|
|
|
)
|
|
|
|
|
2014-01-08 19:21:29 +00:00
|
|
|
const (
|
|
|
|
HealthUnknown = "unknown"
|
|
|
|
HealthPassing = "passing"
|
|
|
|
HealthWarning = "warning"
|
|
|
|
HealthCritical = "critical"
|
|
|
|
)
|
|
|
|
|
2014-04-19 00:14:00 +00:00
|
|
|
// RPCInfo is used to describe common information about query
|
|
|
|
type RPCInfo interface {
|
|
|
|
RequestDatacenter() string
|
|
|
|
IsRead() bool
|
|
|
|
AllowStaleRead() bool
|
|
|
|
}
|
|
|
|
|
2014-04-21 18:31:15 +00:00
|
|
|
// QueryOptions is used to specify various flags for read queries
|
|
|
|
type QueryOptions struct {
|
|
|
|
// If set, wait until query exceeds given index. Must be provided
|
|
|
|
// with MaxQueryTime.
|
2014-02-05 18:21:31 +00:00
|
|
|
MinQueryIndex uint64
|
|
|
|
|
2014-04-21 18:31:15 +00:00
|
|
|
// Provided with MinQueryIndex to wait for change.
|
2014-02-05 18:21:31 +00:00
|
|
|
MaxQueryTime time.Duration
|
|
|
|
|
2014-04-18 23:46:51 +00:00
|
|
|
// If set, any follower can service the request. Results
|
|
|
|
// may be arbitrarily stale.
|
|
|
|
AllowStale bool
|
|
|
|
|
|
|
|
// If set, the leader must verify leadership prior to
|
|
|
|
// servicing the request. Prevents a stale read.
|
|
|
|
RequireConsistent bool
|
|
|
|
}
|
|
|
|
|
2014-04-19 00:14:00 +00:00
|
|
|
// QueryOption only applies to reads, so always true
|
|
|
|
func (q QueryOptions) IsRead() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q QueryOptions) AllowStaleRead() bool {
|
|
|
|
return q.AllowStale
|
|
|
|
}
|
|
|
|
|
|
|
|
type WriteRequest struct{}
|
|
|
|
|
|
|
|
// WriteRequest only applies to writes, always false
|
|
|
|
func (w WriteRequest) IsRead() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w WriteRequest) AllowStaleRead() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-04-18 23:46:51 +00:00
|
|
|
// QueryMeta allows a query response to include potentially
|
|
|
|
// useful metadata about a query
|
|
|
|
type QueryMeta struct {
|
2014-04-21 18:13:36 +00:00
|
|
|
// This is the index associated with the read
|
|
|
|
Index uint64
|
|
|
|
|
2014-04-18 23:46:51 +00:00
|
|
|
// If AllowStale is used, this is time elapsed since
|
|
|
|
// last contact between the follower and leader. This
|
|
|
|
// can be used to gauge staleness.
|
|
|
|
LastContact time.Duration
|
|
|
|
|
|
|
|
// Used to indicate if there is a known leader node
|
|
|
|
KnownLeader bool
|
|
|
|
}
|
|
|
|
|
2013-12-11 22:04:44 +00:00
|
|
|
// 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.
|
|
|
|
type RegisterRequest struct {
|
2014-01-08 21:39:40 +00:00
|
|
|
Datacenter string
|
|
|
|
Node string
|
|
|
|
Address string
|
|
|
|
Service *NodeService
|
|
|
|
Check *HealthCheck
|
2014-04-19 00:14:00 +00:00
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RegisterRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
2013-12-11 22:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeregisterRequest is used for the Catalog.Deregister endpoint
|
|
|
|
// to deregister a node as providing a service. If no service is
|
|
|
|
// provided the entire node is deregistered.
|
|
|
|
type DeregisterRequest struct {
|
2014-01-06 22:18:38 +00:00
|
|
|
Datacenter string
|
|
|
|
Node string
|
|
|
|
ServiceID string
|
2014-01-08 21:39:40 +00:00
|
|
|
CheckID string
|
2014-04-19 00:14:00 +00:00
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DeregisterRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
2013-12-11 22:04:44 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 18:21:31 +00:00
|
|
|
// DCSpecificRequest is used to query about a specific DC
|
|
|
|
type DCSpecificRequest struct {
|
|
|
|
Datacenter string
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryOptions
|
2014-02-05 18:21:31 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 00:14:00 +00:00
|
|
|
func (r *DCSpecificRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2014-01-08 22:43:36 +00:00
|
|
|
// ServiceSpecificRequest is used to query about a specific node
|
|
|
|
type ServiceSpecificRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
ServiceName string
|
|
|
|
ServiceTag string
|
|
|
|
TagFilter bool // Controls tag filtering
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryOptions
|
2014-01-08 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 00:14:00 +00:00
|
|
|
func (r *ServiceSpecificRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2014-01-08 22:43:36 +00:00
|
|
|
// NodeSpecificRequest is used to request the information about a single node
|
|
|
|
type NodeSpecificRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
Node string
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryOptions
|
2014-01-08 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 00:14:00 +00:00
|
|
|
func (r *NodeSpecificRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2014-01-08 22:43:36 +00:00
|
|
|
// ChecksInStateRequest is used to query for nodes in a state
|
|
|
|
type ChecksInStateRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
State string
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryOptions
|
2014-01-08 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 00:14:00 +00:00
|
|
|
func (r *ChecksInStateRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2013-12-12 18:48:36 +00:00
|
|
|
// Used to return information about a node
|
|
|
|
type Node struct {
|
|
|
|
Node string
|
|
|
|
Address string
|
|
|
|
}
|
|
|
|
type Nodes []Node
|
|
|
|
|
2013-12-12 19:07:14 +00:00
|
|
|
// Used to return information about a provided services.
|
|
|
|
// Maps service name to available tags
|
|
|
|
type Services map[string][]string
|
|
|
|
|
2013-12-12 19:37:19 +00:00
|
|
|
// ServiceNode represents a node that is part of a service
|
|
|
|
type ServiceNode struct {
|
|
|
|
Node string
|
|
|
|
Address string
|
2014-01-06 22:18:38 +00:00
|
|
|
ServiceID string
|
2014-01-08 18:29:29 +00:00
|
|
|
ServiceName string
|
2014-04-03 18:23:55 +00:00
|
|
|
ServiceTags []string
|
2013-12-12 19:37:19 +00:00
|
|
|
ServicePort int
|
|
|
|
}
|
|
|
|
type ServiceNodes []ServiceNode
|
|
|
|
|
2013-12-12 19:46:25 +00:00
|
|
|
// NodeService is a service provided by a node
|
|
|
|
type NodeService struct {
|
2014-01-06 22:18:38 +00:00
|
|
|
ID string
|
|
|
|
Service string
|
2014-04-03 18:23:55 +00:00
|
|
|
Tags []string
|
2014-01-06 22:18:38 +00:00
|
|
|
Port int
|
2013-12-12 19:46:25 +00:00
|
|
|
}
|
2014-01-03 01:29:39 +00:00
|
|
|
type NodeServices struct {
|
2014-01-08 22:43:36 +00:00
|
|
|
Node Node
|
2014-01-08 18:29:29 +00:00
|
|
|
Services map[string]*NodeService
|
2014-01-03 01:29:39 +00:00
|
|
|
}
|
2013-12-12 19:46:25 +00:00
|
|
|
|
2014-01-08 19:21:29 +00:00
|
|
|
// 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
|
2014-04-21 23:20:22 +00:00
|
|
|
Output string // Holds output of script runs
|
2014-01-08 19:21:29 +00:00
|
|
|
ServiceID string // optional associated service
|
|
|
|
ServiceName string // optional service name
|
|
|
|
}
|
2014-01-08 19:35:27 +00:00
|
|
|
type HealthChecks []*HealthCheck
|
2014-01-08 19:21:29 +00:00
|
|
|
|
2014-01-08 22:58:53 +00:00
|
|
|
// CheckServiceNode is used to provide the node, it's service
|
2014-01-08 22:43:36 +00:00
|
|
|
// definition, as well as a HealthCheck that is associated
|
2014-01-08 22:58:53 +00:00
|
|
|
type CheckServiceNode struct {
|
2014-01-08 22:43:36 +00:00
|
|
|
Node Node
|
|
|
|
Service NodeService
|
2014-01-08 22:58:53 +00:00
|
|
|
Checks HealthChecks
|
2014-01-08 21:52:09 +00:00
|
|
|
}
|
2014-01-08 22:58:53 +00:00
|
|
|
type CheckServiceNodes []CheckServiceNode
|
2014-01-08 21:52:09 +00:00
|
|
|
|
2014-04-27 19:49:20 +00:00
|
|
|
// NodeInfo is used to dump all associated information about
|
|
|
|
// a node. This is currently used for the UI only, as it is
|
|
|
|
// rather expensive to generate.
|
|
|
|
type NodeInfo struct {
|
|
|
|
Node string
|
|
|
|
Address string
|
|
|
|
Services []*NodeService
|
|
|
|
Checks []*HealthCheck
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeDump is used to dump all the nodes with all their
|
|
|
|
// associated data. This is currently used for the UI only,
|
|
|
|
// as it is rather expensive to generate.
|
|
|
|
type NodeDump []*NodeInfo
|
|
|
|
|
2014-02-05 18:44:28 +00:00
|
|
|
type IndexedNodes struct {
|
|
|
|
Nodes Nodes
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryMeta
|
2014-02-05 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type IndexedServices struct {
|
2014-02-05 22:27:24 +00:00
|
|
|
Services Services
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryMeta
|
2014-02-05 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type IndexedServiceNodes struct {
|
|
|
|
ServiceNodes ServiceNodes
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryMeta
|
2014-02-05 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type IndexedNodeServices struct {
|
|
|
|
NodeServices *NodeServices
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryMeta
|
2014-02-05 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type IndexedHealthChecks struct {
|
|
|
|
HealthChecks HealthChecks
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryMeta
|
2014-02-05 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 21:30:18 +00:00
|
|
|
type IndexedCheckServiceNodes struct {
|
|
|
|
Nodes CheckServiceNodes
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryMeta
|
2014-02-05 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 19:56:06 +00:00
|
|
|
type IndexedNodeDump struct {
|
|
|
|
Dump NodeDump
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2014-03-31 18:47:10 +00:00
|
|
|
// DirEntry is used to represent a directory entry. This is
|
|
|
|
// used for values in our Key-Value store.
|
|
|
|
type DirEntry struct {
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
|
|
|
Key string
|
|
|
|
Flags uint64
|
|
|
|
Value []byte
|
|
|
|
}
|
|
|
|
type DirEntries []*DirEntry
|
|
|
|
|
|
|
|
type KVSOp string
|
|
|
|
|
|
|
|
const (
|
2014-03-31 19:13:40 +00:00
|
|
|
KVSSet KVSOp = "set"
|
|
|
|
KVSDelete = "delete"
|
|
|
|
KVSDeleteTree = "delete-tree"
|
|
|
|
KVSCAS = "cas" // Check-and-set
|
2014-03-31 18:47:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// KVSRequest is used to operate on the Key-Value store
|
|
|
|
type KVSRequest struct {
|
2014-03-31 21:13:03 +00:00
|
|
|
Datacenter string
|
|
|
|
Op KVSOp // Which operation are we performing
|
|
|
|
DirEnt DirEntry // Which directory entry
|
2014-04-19 00:14:00 +00:00
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *KVSRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
2014-03-31 18:47:10 +00:00
|
|
|
}
|
|
|
|
|
2014-03-31 23:00:23 +00:00
|
|
|
// KeyRequest is used to request a key, or key prefix
|
|
|
|
type KeyRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
Key string
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryOptions
|
2014-03-31 23:00:23 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 00:14:00 +00:00
|
|
|
func (r *KeyRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2014-04-28 23:33:54 +00:00
|
|
|
// KeyListRequest is used to list keys
|
|
|
|
type KeyListRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
Prefix string
|
|
|
|
Seperator string
|
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *KeyListRequest) RequestDatacenter() string {
|
|
|
|
return r.Datacenter
|
|
|
|
}
|
|
|
|
|
2014-03-31 23:00:23 +00:00
|
|
|
type IndexedDirEntries struct {
|
|
|
|
Entries DirEntries
|
2014-04-18 23:46:51 +00:00
|
|
|
QueryMeta
|
2014-03-31 23:00:23 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 23:33:54 +00:00
|
|
|
type IndexedKeyList struct {
|
|
|
|
Keys []string
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2014-05-08 22:01:02 +00:00
|
|
|
// Session is used to represent an open session in the KV store.
|
|
|
|
// This issued to associate node checks with acquired locks.
|
|
|
|
type Session struct {
|
2014-05-08 23:54:04 +00:00
|
|
|
ID string
|
|
|
|
Node string
|
|
|
|
Checks []string
|
|
|
|
CreateIndex uint64
|
2014-05-08 22:01:02 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 22:04:44 +00:00
|
|
|
// Decode is used to decode a MsgPack encoded object
|
|
|
|
func Decode(buf []byte, out interface{}) error {
|
|
|
|
var handle codec.MsgpackHandle
|
|
|
|
return codec.NewDecoder(bytes.NewReader(buf), &handle).Decode(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode is used to encode a MsgPack object with type prefix
|
|
|
|
func Encode(t MessageType, msg interface{}) ([]byte, error) {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
buf.WriteByte(uint8(t))
|
|
|
|
|
|
|
|
handle := codec.MsgpackHandle{}
|
|
|
|
encoder := codec.NewEncoder(buf, &handle)
|
|
|
|
err := encoder.Encode(msg)
|
|
|
|
return buf.Bytes(), err
|
|
|
|
}
|