Add RPC for querying for Node connections

This commit is contained in:
Alex Dadgar
2018-01-12 16:52:24 -08:00
parent 96c354304d
commit 61eaa104b6
3 changed files with 68 additions and 0 deletions

View File

@@ -4,7 +4,10 @@ import (
"fmt"
"strconv"
"errors"
"github.com/hashicorp/consul/agent/consul/autopilot"
"github.com/hashicorp/nomad/nomad/structs"
)
@@ -126,3 +129,20 @@ func (s *Status) RaftStats(args struct{}, reply *autopilot.ServerStats) error {
return nil
}
// HasNodeConn returns whether the server has a connection to the requested
// Node.
func (s *Status) HasNodeConn(args *structs.NodeSpecificRequest, reply *structs.NodeConnQueryResponse) error {
// Validate the args
if args.NodeID == "" {
return errors.New("Must provide the NodeID")
}
state, ok := s.srv.getNodeConn(args.NodeID)
if ok {
reply.Connected = true
reply.Established = state.Established
}
return nil
}

View File

@@ -5,10 +5,12 @@ import (
"github.com/hashicorp/net-rpc-msgpackrpc"
"github.com/hashicorp/nomad/acl"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStatusVersion(t *testing.T) {
@@ -169,3 +171,37 @@ func TestStatusMembers_ACL(t *testing.T) {
assert.Len(out.Members, 1)
}
}
func TestStatus_HasClientConn(t *testing.T) {
t.Parallel()
s1 := TestServer(t, nil)
defer s1.Shutdown()
codec := rpcClient(t, s1)
require := require.New(t)
arg := &structs.NodeSpecificRequest{
QueryOptions: structs.QueryOptions{
Region: "global",
AllowStale: true,
},
}
// Try without setting a node id
var out structs.NodeConnQueryResponse
require.NotNil(msgpackrpc.CallWithCodec(codec, "Status.HasNodeConn", arg, &out))
// Set a bad node id
arg.NodeID = uuid.Generate()
var out2 structs.NodeConnQueryResponse
require.Nil(msgpackrpc.CallWithCodec(codec, "Status.HasNodeConn", arg, &out2))
require.False(out2.Connected)
// Create a connection on that node
s1.addNodeConn(&RPCContext{
NodeID: arg.NodeID,
})
var out3 structs.NodeConnQueryResponse
require.Nil(msgpackrpc.CallWithCodec(codec, "Status.HasNodeConn", arg, &out3))
require.True(out3.Connected)
require.NotZero(out3.Established)
}

View File

@@ -1037,6 +1037,18 @@ type DeploymentUpdateResponse struct {
WriteMeta
}
// NodeConnQueryResponse is used to respond to a query of whether a server has
// a connection to a specific Node
type NodeConnQueryResponse struct {
// Connected indicates whether a connection to the Client exists
Connected bool
// Established marks the time at which the connection was established
Established time.Time
QueryMeta
}
const (
NodeStatusInit = "initializing"
NodeStatusReady = "ready"