2013-12-07 00:54:33 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2014-04-04 23:34:23 +00:00
|
|
|
"crypto/tls"
|
2013-12-11 22:04:44 +00:00
|
|
|
"fmt"
|
2013-12-11 22:57:40 +00:00
|
|
|
"io"
|
2013-12-07 00:54:33 +00:00
|
|
|
"net"
|
2014-02-05 23:29:52 +00:00
|
|
|
"strings"
|
2014-02-05 18:38:29 +00:00
|
|
|
"time"
|
2014-11-26 09:25:37 +00:00
|
|
|
|
|
|
|
"github.com/armon/go-metrics"
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/agent"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/structs"
|
2017-06-15 13:16:16 +00:00
|
|
|
"github.com/hashicorp/consul/agent/pool"
|
2016-01-29 19:42:34 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2017-01-13 19:17:38 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
2015-12-16 19:38:35 +00:00
|
|
|
"github.com/hashicorp/memberlist"
|
2015-10-13 23:43:52 +00:00
|
|
|
"github.com/hashicorp/net-rpc-msgpackrpc"
|
2014-11-26 09:25:37 +00:00
|
|
|
"github.com/hashicorp/yamux"
|
2013-12-07 00:54:33 +00:00
|
|
|
)
|
|
|
|
|
2014-02-05 18:38:29 +00:00
|
|
|
const (
|
2014-05-10 02:16:40 +00:00
|
|
|
// maxQueryTime is used to bound the limit of a blocking query
|
2014-02-05 18:38:29 +00:00
|
|
|
maxQueryTime = 600 * time.Second
|
2014-04-29 05:25:09 +00:00
|
|
|
|
2015-05-15 00:59:43 +00:00
|
|
|
// defaultQueryTime is the amount of time we block waiting for a change
|
|
|
|
// if no time is specified. Previously we would wait the maxQueryTime.
|
|
|
|
defaultQueryTime = 300 * time.Second
|
|
|
|
|
|
|
|
// jitterFraction is a the limit to the amount of jitter we apply
|
|
|
|
// to a user specified MaxQueryTime. We divide the specified time by
|
2016-07-10 17:24:06 +00:00
|
|
|
// the fraction. So 16 == 6.25% limit of jitter. This same fraction
|
|
|
|
// is applied to the RPCHoldTimeout
|
2015-05-15 00:59:43 +00:00
|
|
|
jitterFraction = 16
|
|
|
|
|
2014-04-29 05:25:09 +00:00
|
|
|
// Warn if the Raft command is larger than this.
|
2014-05-06 21:10:08 +00:00
|
|
|
// If it's over 1MB something is probably being abusive.
|
|
|
|
raftWarnSize = 1024 * 1024
|
2014-05-10 02:16:40 +00:00
|
|
|
|
|
|
|
// enqueueLimit caps how long we will wait to enqueue
|
|
|
|
// a new Raft command. Something is probably wrong if this
|
|
|
|
// value is ever reached. However, it prevents us from blocking
|
|
|
|
// the requesting goroutine forever.
|
|
|
|
enqueueLimit = 30 * time.Second
|
2014-02-05 18:38:29 +00:00
|
|
|
)
|
|
|
|
|
2013-12-07 00:54:33 +00:00
|
|
|
// listen is used to listen for incoming RPC connections
|
|
|
|
func (s *Server) listen() {
|
|
|
|
for {
|
|
|
|
// Accept a connection
|
2017-06-25 19:36:03 +00:00
|
|
|
conn, err := s.Listener.Accept()
|
2013-12-07 00:54:33 +00:00
|
|
|
if err != nil {
|
|
|
|
if s.shutdown {
|
|
|
|
return
|
|
|
|
}
|
2014-01-10 19:06:11 +00:00
|
|
|
s.logger.Printf("[ERR] consul.rpc: failed to accept RPC conn: %v", err)
|
2013-12-07 00:54:33 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-04-07 19:45:33 +00:00
|
|
|
go s.handleConn(conn, false)
|
2014-02-20 23:16:26 +00:00
|
|
|
metrics.IncrCounter([]string{"consul", "rpc", "accept_conn"}, 1)
|
2013-12-07 00:54:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 19:38:35 +00:00
|
|
|
// logConn is a wrapper around memberlist's LogConn so that we format references
|
|
|
|
// to "from" addresses in a consistent way. This is just a shorter name.
|
|
|
|
func logConn(conn net.Conn) string {
|
|
|
|
return memberlist.LogConn(conn)
|
|
|
|
}
|
|
|
|
|
2013-12-09 21:13:40 +00:00
|
|
|
// handleConn is used to determine if this is a Raft or
|
|
|
|
// Consul type RPC connection and invoke the correct handler
|
2014-04-07 19:45:33 +00:00
|
|
|
func (s *Server) handleConn(conn net.Conn, isTLS bool) {
|
2013-12-09 21:13:40 +00:00
|
|
|
// Read a single byte
|
|
|
|
buf := make([]byte, 1)
|
|
|
|
if _, err := conn.Read(buf); err != nil {
|
2014-05-23 23:28:55 +00:00
|
|
|
if err != io.EOF {
|
2015-12-16 19:38:35 +00:00
|
|
|
s.logger.Printf("[ERR] consul.rpc: failed to read byte: %v %s", err, logConn(conn))
|
2014-05-23 23:28:55 +00:00
|
|
|
}
|
2013-12-09 21:13:40 +00:00
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
2017-06-15 13:16:16 +00:00
|
|
|
typ := pool.RPCType(buf[0])
|
2013-12-09 21:13:40 +00:00
|
|
|
|
2014-04-04 23:43:00 +00:00
|
|
|
// Enforce TLS if VerifyIncoming is set
|
2017-06-15 13:16:16 +00:00
|
|
|
if s.config.VerifyIncoming && !isTLS && typ != pool.RPCTLS {
|
2015-12-16 19:38:35 +00:00
|
|
|
s.logger.Printf("[WARN] consul.rpc: Non-TLS connection attempted with VerifyIncoming set %s", logConn(conn))
|
2014-04-04 23:43:00 +00:00
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-12-09 21:13:40 +00:00
|
|
|
// Switch on the byte
|
2017-06-15 13:16:16 +00:00
|
|
|
switch typ {
|
|
|
|
case pool.RPCConsul:
|
2013-12-09 21:13:40 +00:00
|
|
|
s.handleConsulConn(conn)
|
|
|
|
|
2017-06-15 13:16:16 +00:00
|
|
|
case pool.RPCRaft:
|
2014-02-20 23:16:26 +00:00
|
|
|
metrics.IncrCounter([]string{"consul", "rpc", "raft_handoff"}, 1)
|
2013-12-09 21:13:40 +00:00
|
|
|
s.raftLayer.Handoff(conn)
|
|
|
|
|
2017-06-15 13:16:16 +00:00
|
|
|
case pool.RPCTLS:
|
2014-04-07 19:45:33 +00:00
|
|
|
if s.rpcTLS == nil {
|
2015-12-16 19:38:35 +00:00
|
|
|
s.logger.Printf("[WARN] consul.rpc: TLS connection attempted, server not configured for TLS %s", logConn(conn))
|
2014-04-07 19:45:33 +00:00
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
conn = tls.Server(conn, s.rpcTLS)
|
|
|
|
s.handleConn(conn, true)
|
|
|
|
|
2017-06-15 13:16:16 +00:00
|
|
|
case pool.RPCMultiplexV2:
|
2014-05-27 18:00:35 +00:00
|
|
|
s.handleMultiplexV2(conn)
|
|
|
|
|
2017-06-15 13:16:16 +00:00
|
|
|
case pool.RPCSnapshot:
|
2016-10-26 02:20:24 +00:00
|
|
|
s.handleSnapshotConn(conn)
|
|
|
|
|
2013-12-09 21:13:40 +00:00
|
|
|
default:
|
2017-06-15 13:16:16 +00:00
|
|
|
s.logger.Printf("[ERR] consul.rpc: unrecognized RPC byte: %v %s", typ, logConn(conn))
|
2013-12-09 21:13:40 +00:00
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 18:00:35 +00:00
|
|
|
// handleMultiplexV2 is used to multiplex a single incoming connection
|
|
|
|
// using the Yamux multiplexer
|
|
|
|
func (s *Server) handleMultiplexV2(conn net.Conn) {
|
|
|
|
defer conn.Close()
|
2014-05-28 23:32:10 +00:00
|
|
|
conf := yamux.DefaultConfig()
|
|
|
|
conf.LogOutput = s.config.LogOutput
|
|
|
|
server, _ := yamux.Server(conn, conf)
|
2014-05-27 18:00:35 +00:00
|
|
|
for {
|
|
|
|
sub, err := server.Accept()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
2015-12-16 19:38:35 +00:00
|
|
|
s.logger.Printf("[ERR] consul.rpc: multiplex conn accept failed: %v %s", err, logConn(conn))
|
2014-05-27 18:00:35 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go s.handleConsulConn(sub)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-09 21:13:40 +00:00
|
|
|
// handleConsulConn is used to service a single Consul RPC connection
|
|
|
|
func (s *Server) handleConsulConn(conn net.Conn) {
|
2014-05-15 00:34:24 +00:00
|
|
|
defer conn.Close()
|
2015-10-13 23:43:52 +00:00
|
|
|
rpcCodec := msgpackrpc.NewServerCodec(conn)
|
2014-11-26 09:25:37 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.shutdownCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2013-12-07 00:54:33 +00:00
|
|
|
if err := s.rpcServer.ServeRequest(rpcCodec); err != nil {
|
2014-05-28 23:28:06 +00:00
|
|
|
if err != io.EOF && !strings.Contains(err.Error(), "closed") {
|
2015-12-16 19:38:35 +00:00
|
|
|
s.logger.Printf("[ERR] consul.rpc: RPC error: %v %s", err, logConn(conn))
|
2015-05-08 00:25:12 +00:00
|
|
|
metrics.IncrCounter([]string{"consul", "rpc", "request_error"}, 1)
|
2013-12-11 22:57:40 +00:00
|
|
|
}
|
2013-12-07 00:54:33 +00:00
|
|
|
return
|
|
|
|
}
|
2015-05-08 00:25:12 +00:00
|
|
|
metrics.IncrCounter([]string{"consul", "rpc", "request"}, 1)
|
2013-12-07 00:54:33 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-11 22:04:44 +00:00
|
|
|
|
2016-10-26 02:20:24 +00:00
|
|
|
// handleSnapshotConn is used to dispatch snapshot saves and restores, which
|
|
|
|
// stream so don't use the normal RPC mechanism.
|
|
|
|
func (s *Server) handleSnapshotConn(conn net.Conn) {
|
|
|
|
go func() {
|
|
|
|
defer conn.Close()
|
|
|
|
if err := s.handleSnapshotRequest(conn); err != nil {
|
|
|
|
s.logger.Printf("[ERR] consul.rpc: Snapshot RPC error: %v %s", err, logConn(conn))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2013-12-11 22:04:44 +00:00
|
|
|
// forward is used to forward to a remote DC or to forward to the local leader
|
|
|
|
// Returns a bool of if forwarding was performed, as well as any error
|
2014-04-19 00:17:12 +00:00
|
|
|
func (s *Server) forward(method string, info structs.RPCInfo, args interface{}, reply interface{}) (bool, error) {
|
2016-07-10 17:24:06 +00:00
|
|
|
var firstCheck time.Time
|
|
|
|
|
2013-12-11 22:04:44 +00:00
|
|
|
// Handle DC forwarding
|
2014-04-19 00:17:12 +00:00
|
|
|
dc := info.RequestDatacenter()
|
2013-12-11 22:04:44 +00:00
|
|
|
if dc != s.config.Datacenter {
|
|
|
|
err := s.forwardDC(method, dc, args, reply)
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
2014-04-19 00:26:59 +00:00
|
|
|
// Check if we can allow a stale read
|
|
|
|
if info.IsRead() && info.AllowStaleRead() {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2016-07-10 17:24:06 +00:00
|
|
|
CHECK_LEADER:
|
|
|
|
// Find the leader
|
|
|
|
isLeader, remoteServer := s.getLeader()
|
|
|
|
|
|
|
|
// Handle the case we are the leader
|
|
|
|
if isLeader {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the case of a known leader
|
|
|
|
if remoteServer != nil {
|
|
|
|
err := s.forwardLeader(remoteServer, method, args, reply)
|
2013-12-11 22:04:44 +00:00
|
|
|
return true, err
|
|
|
|
}
|
2016-07-10 17:24:06 +00:00
|
|
|
|
|
|
|
// Gate the request until there is a leader
|
|
|
|
if firstCheck.IsZero() {
|
|
|
|
firstCheck = time.Now()
|
|
|
|
}
|
|
|
|
if time.Now().Sub(firstCheck) < s.config.RPCHoldTimeout {
|
|
|
|
jitter := lib.RandomStagger(s.config.RPCHoldTimeout / jitterFraction)
|
|
|
|
select {
|
|
|
|
case <-time.After(jitter):
|
|
|
|
goto CHECK_LEADER
|
|
|
|
case <-s.shutdownCh:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No leader found and hold time exceeded
|
|
|
|
return true, structs.ErrNoLeader
|
2013-12-11 22:04:44 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 02:20:24 +00:00
|
|
|
// getLeader returns if the current node is the leader, and if not then it
|
|
|
|
// returns the leader which is potentially nil if the cluster has not yet
|
|
|
|
// elected a leader.
|
2016-07-10 17:24:06 +00:00
|
|
|
func (s *Server) getLeader() (bool, *agent.Server) {
|
|
|
|
// Check if we are the leader
|
|
|
|
if s.IsLeader() {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2014-05-27 22:45:19 +00:00
|
|
|
// Get the leader
|
2013-12-11 22:04:44 +00:00
|
|
|
leader := s.raft.Leader()
|
2015-05-08 18:35:12 +00:00
|
|
|
if leader == "" {
|
2016-07-10 17:24:06 +00:00
|
|
|
return false, nil
|
2013-12-11 22:04:44 +00:00
|
|
|
}
|
2014-05-27 22:45:19 +00:00
|
|
|
|
|
|
|
// Lookup the server
|
|
|
|
s.localLock.RLock()
|
2015-05-08 18:35:12 +00:00
|
|
|
server := s.localConsuls[leader]
|
2014-05-27 22:45:19 +00:00
|
|
|
s.localLock.RUnlock()
|
|
|
|
|
2016-07-10 17:24:06 +00:00
|
|
|
// Server could be nil
|
|
|
|
return false, server
|
|
|
|
}
|
|
|
|
|
|
|
|
// forwardLeader is used to forward an RPC call to the leader, or fail if no leader
|
|
|
|
func (s *Server) forwardLeader(server *agent.Server, method string, args interface{}, reply interface{}) error {
|
2014-05-27 22:45:19 +00:00
|
|
|
// Handle a missing server
|
|
|
|
if server == nil {
|
|
|
|
return structs.ErrNoLeader
|
|
|
|
}
|
2017-05-10 21:25:48 +00:00
|
|
|
return s.connPool.RPC(s.config.Datacenter, server.Addr, server.Version, method, server.UseTLS, args, reply)
|
2013-12-11 22:04:44 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 02:20:24 +00:00
|
|
|
// forwardDC is used to forward an RPC call to a remote DC, or fail if no servers
|
|
|
|
func (s *Server) forwardDC(method, dc string, args interface{}, reply interface{}) error {
|
2017-03-14 01:54:34 +00:00
|
|
|
manager, server, ok := s.router.FindRoute(dc)
|
2016-10-26 02:20:24 +00:00
|
|
|
if !ok {
|
2017-03-14 01:54:34 +00:00
|
|
|
s.logger.Printf("[WARN] consul.rpc: RPC request for DC %q, no path found", dc)
|
2016-10-26 02:20:24 +00:00
|
|
|
return structs.ErrNoDCPath
|
|
|
|
}
|
2013-12-12 00:33:19 +00:00
|
|
|
|
2014-02-20 23:16:26 +00:00
|
|
|
metrics.IncrCounter([]string{"consul", "rpc", "cross-dc", dc}, 1)
|
2017-05-10 21:25:48 +00:00
|
|
|
if err := s.connPool.RPC(dc, server.Addr, server.Version, method, server.UseTLS, args, reply); err != nil {
|
2017-03-14 01:54:34 +00:00
|
|
|
manager.NotifyFailedServer(server)
|
|
|
|
s.logger.Printf("[ERR] consul: RPC failed to server %s in DC %q: %v", server.Addr, dc, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2013-12-11 22:04:44 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 06:09:00 +00:00
|
|
|
// globalRPC is used to forward an RPC request to one server in each datacenter.
|
|
|
|
// This will only error for RPC-related errors. Otherwise, application-level
|
2014-10-05 20:15:59 +00:00
|
|
|
// errors can be sent in the response objects.
|
2014-10-02 06:09:00 +00:00
|
|
|
func (s *Server) globalRPC(method string, args interface{},
|
|
|
|
reply structs.CompoundResponse) error {
|
|
|
|
|
|
|
|
errorCh := make(chan error)
|
|
|
|
respCh := make(chan interface{})
|
|
|
|
|
|
|
|
// Make a new request into each datacenter
|
2017-03-14 01:54:34 +00:00
|
|
|
dcs := s.router.GetDatacenters()
|
2016-06-20 20:50:59 +00:00
|
|
|
for _, dc := range dcs {
|
2014-10-08 20:28:59 +00:00
|
|
|
go func(dc string) {
|
2014-10-02 06:09:00 +00:00
|
|
|
rr := reply.New()
|
2014-10-08 20:28:59 +00:00
|
|
|
if err := s.forwardDC(method, dc, args, &rr); err != nil {
|
2014-10-02 06:09:00 +00:00
|
|
|
errorCh <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respCh <- rr
|
2014-10-08 20:28:59 +00:00
|
|
|
}(dc)
|
2014-10-02 06:09:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 01:54:34 +00:00
|
|
|
replies, total := 0, len(dcs)
|
2014-10-08 20:28:59 +00:00
|
|
|
for replies < total {
|
2014-10-02 06:09:00 +00:00
|
|
|
select {
|
|
|
|
case err := <-errorCh:
|
|
|
|
return err
|
|
|
|
case rr := <-respCh:
|
|
|
|
reply.Add(rr)
|
2014-10-06 22:14:30 +00:00
|
|
|
replies++
|
2014-10-02 06:09:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-11 22:04:44 +00:00
|
|
|
// raftApply is used to encode a message, run it through raft, and return
|
|
|
|
// the FSM response along with any errors
|
2013-12-19 20:03:57 +00:00
|
|
|
func (s *Server) raftApply(t structs.MessageType, msg interface{}) (interface{}, error) {
|
|
|
|
buf, err := structs.Encode(t, msg)
|
2013-12-11 22:04:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to encode request: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-29 05:25:09 +00:00
|
|
|
// Warn if the command is very large
|
|
|
|
if n := len(buf); n > raftWarnSize {
|
|
|
|
s.logger.Printf("[WARN] consul: Attempting to apply large raft entry (%d bytes)", n)
|
|
|
|
}
|
|
|
|
|
2014-05-10 02:16:40 +00:00
|
|
|
future := s.raft.Apply(buf, enqueueLimit)
|
2013-12-11 22:04:44 +00:00
|
|
|
if err := future.Error(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return future.Response(), nil
|
|
|
|
}
|
2014-02-05 18:38:29 +00:00
|
|
|
|
2017-01-13 19:17:38 +00:00
|
|
|
// queryFn is used to perform a query operation. If a re-query is needed, the
|
2017-01-25 17:45:25 +00:00
|
|
|
// passed-in watch set will be used to block for changes. The passed-in state
|
|
|
|
// store should be used (vs. calling fsm.State()) since the given state store
|
|
|
|
// will be correctly watched for changes if the state store is restored from
|
|
|
|
// a snapshot.
|
2017-04-21 00:46:29 +00:00
|
|
|
type queryFn func(memdb.WatchSet, *state.Store) error
|
2017-01-13 19:17:38 +00:00
|
|
|
|
|
|
|
// blockingQuery is used to process a potentially blocking query operation.
|
|
|
|
func (s *Server) blockingQuery(queryOpts *structs.QueryOptions, queryMeta *structs.QueryMeta,
|
|
|
|
fn queryFn) error {
|
|
|
|
var timeout *time.Timer
|
|
|
|
|
|
|
|
// Fast path right to the non-blocking query.
|
|
|
|
if queryOpts.MinQueryIndex == 0 {
|
|
|
|
goto RUN_QUERY
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restrict the max query time, and ensure there is always one.
|
|
|
|
if queryOpts.MaxQueryTime > maxQueryTime {
|
|
|
|
queryOpts.MaxQueryTime = maxQueryTime
|
|
|
|
} else if queryOpts.MaxQueryTime <= 0 {
|
|
|
|
queryOpts.MaxQueryTime = defaultQueryTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply a small amount of jitter to the request.
|
|
|
|
queryOpts.MaxQueryTime += lib.RandomStagger(queryOpts.MaxQueryTime / jitterFraction)
|
|
|
|
|
|
|
|
// Setup a query timeout.
|
|
|
|
timeout = time.NewTimer(queryOpts.MaxQueryTime)
|
|
|
|
defer timeout.Stop()
|
|
|
|
|
|
|
|
RUN_QUERY:
|
|
|
|
// Update the query metadata.
|
|
|
|
s.setQueryMeta(queryMeta)
|
|
|
|
|
|
|
|
// If the read must be consistent we verify that we are still the leader.
|
|
|
|
if queryOpts.RequireConsistent {
|
|
|
|
if err := s.consistentRead(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the query.
|
|
|
|
metrics.IncrCounter([]string{"consul", "rpc", "query"}, 1)
|
2017-01-19 19:12:29 +00:00
|
|
|
|
2017-01-25 01:23:48 +00:00
|
|
|
// Operate on a consistent set of state. This makes sure that the
|
|
|
|
// abandon channel goes with the state that the caller is using to
|
|
|
|
// build watches.
|
|
|
|
state := s.fsm.State()
|
|
|
|
|
2017-01-19 19:12:29 +00:00
|
|
|
// We can skip all watch tracking if this isn't a blocking query.
|
|
|
|
var ws memdb.WatchSet
|
|
|
|
if queryOpts.MinQueryIndex > 0 {
|
|
|
|
ws = memdb.NewWatchSet()
|
2017-01-24 18:38:03 +00:00
|
|
|
|
|
|
|
// This channel will be closed if a snapshot is restored and the
|
|
|
|
// whole state store is abandoned.
|
2017-01-25 01:23:48 +00:00
|
|
|
ws.Add(state.AbandonCh())
|
2017-01-19 19:12:29 +00:00
|
|
|
}
|
2017-01-13 19:17:38 +00:00
|
|
|
|
|
|
|
// Block up to the timeout if we didn't see anything fresh.
|
2017-01-25 01:23:48 +00:00
|
|
|
err := fn(ws, state)
|
2017-01-13 19:17:38 +00:00
|
|
|
if err == nil && queryMeta.Index > 0 && queryMeta.Index <= queryOpts.MinQueryIndex {
|
|
|
|
if expired := ws.Watch(timeout.C); !expired {
|
2017-01-26 03:00:32 +00:00
|
|
|
// If a restore may have woken us up then bail out from
|
|
|
|
// the query immediately. This is slightly race-ey since
|
|
|
|
// this might have been interrupted for other reasons,
|
|
|
|
// but it's OK to kick it back to the caller in either
|
|
|
|
// case.
|
|
|
|
select {
|
|
|
|
case <-state.AbandonCh():
|
|
|
|
default:
|
|
|
|
goto RUN_QUERY
|
|
|
|
}
|
2017-01-13 19:17:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-19 00:37:19 +00:00
|
|
|
// setQueryMeta is used to populate the QueryMeta data for an RPC call
|
|
|
|
func (s *Server) setQueryMeta(m *structs.QueryMeta) {
|
|
|
|
if s.IsLeader() {
|
|
|
|
m.LastContact = 0
|
|
|
|
m.KnownLeader = true
|
|
|
|
} else {
|
|
|
|
m.LastContact = time.Now().Sub(s.raft.LastContact())
|
2015-05-08 18:35:12 +00:00
|
|
|
m.KnownLeader = (s.raft.Leader() != "")
|
2014-04-19 00:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-19 00:49:01 +00:00
|
|
|
|
|
|
|
// consistentRead is used to ensure we do not perform a stale
|
|
|
|
// read. This is done by verifying leadership before the read.
|
|
|
|
func (s *Server) consistentRead() error {
|
|
|
|
defer metrics.MeasureSince([]string{"consul", "rpc", "consistentRead"}, time.Now())
|
|
|
|
future := s.raft.VerifyLeader()
|
2017-06-16 15:49:54 +00:00
|
|
|
if err := future.Error(); err != nil {
|
2017-06-16 03:41:30 +00:00
|
|
|
return err //fail fast if leader verification fails
|
|
|
|
}
|
|
|
|
// poll consistent read readiness, wait for up to RPCHoldTimeout milliseconds
|
|
|
|
if s.isReadyForConsistentReads() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
jitter := lib.RandomStagger(s.config.RPCHoldTimeout / jitterFraction)
|
2017-06-16 15:49:54 +00:00
|
|
|
deadline := time.Now().Add(s.config.RPCHoldTimeout)
|
2017-06-16 03:41:30 +00:00
|
|
|
|
2017-06-16 15:49:54 +00:00
|
|
|
for time.Now().Before(deadline) {
|
2017-06-16 03:41:30 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(jitter):
|
|
|
|
// Drop through and check before we loop again.
|
|
|
|
|
|
|
|
case <-s.shutdownCh:
|
|
|
|
return fmt.Errorf("shutdown waiting for leader")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.isReadyForConsistentReads() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return structs.ErrNotReadyForConsistentReads
|
2014-04-19 00:49:01 +00:00
|
|
|
}
|