nomad: expose the raft index on write

This commit is contained in:
Armon Dadgar 2015-07-06 14:34:32 -06:00
parent a04d125760
commit c897e7c69a
3 changed files with 18 additions and 5 deletions

View File

@ -40,10 +40,17 @@ func (c *Client) Register(args *structs.RegisterRequest, reply *structs.GenericR
}
// Commit this update via Raft
_, err := c.srv.raftApply(structs.RegisterRequestType, args)
_, index, err := c.srv.raftApply(structs.RegisterRequestType, args)
if err != nil {
c.srv.logger.Printf("[ERR] nomad.client: Register failed: %v", err)
return err
}
// Set the reply index
reply.Index = index
return nil
}
func (c *Client) Deregister(args *structs.DeregisterRequest, reply *structs.GenericResponse) error {
return nil
}

View File

@ -26,6 +26,9 @@ func TestClientEndpoint_Register(t *testing.T) {
if err := msgpackrpc.CallWithCodec(codec, "Client.Register", req, &resp); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Index == 0 {
t.Fatalf("bad index: %d", resp.Index)
}
// Check for the node in the FSM
state := s1.fsm.State()
@ -36,4 +39,7 @@ func TestClientEndpoint_Register(t *testing.T) {
if out == nil {
t.Fatalf("expected node")
}
if out.CreateIndex != resp.Index {
t.Fatalf("index mis-match")
}
}

View File

@ -236,10 +236,10 @@ func (s *Server) forwardRegion(region, method string, args interface{}, reply in
// raftApply is used to encode a message, run it through raft, and return
// the FSM response along with any errors
func (s *Server) raftApply(t structs.MessageType, msg interface{}) (interface{}, error) {
func (s *Server) raftApply(t structs.MessageType, msg interface{}) (interface{}, uint64, error) {
buf, err := structs.Encode(t, msg)
if err != nil {
return nil, fmt.Errorf("Failed to encode request: %v", err)
return nil, 0, fmt.Errorf("Failed to encode request: %v", err)
}
// Warn if the command is very large
@ -249,8 +249,8 @@ func (s *Server) raftApply(t structs.MessageType, msg interface{}) (interface{},
future := s.raft.Apply(buf, enqueueLimit)
if err := future.Error(); err != nil {
return nil, err
return nil, 0, err
}
return future.Response(), nil
return future.Response(), future.Index(), nil
}