consul: Log state store errors

pull/19/head
Armon Dadgar 2014-02-03 15:21:56 -08:00
parent 1a5158212c
commit 231bbb1bd5
3 changed files with 40 additions and 19 deletions

View File

@ -14,7 +14,8 @@ import (
// along with Raft to provide strong consistency. We implement // along with Raft to provide strong consistency. We implement
// this outside the Server to avoid exposing this outside the package. // this outside the Server to avoid exposing this outside the package.
type consulFSM struct { type consulFSM struct {
state *StateStore logger *log.Logger
state *StateStore
} }
// consulSnapshot is used to provide a snapshot of the current // consulSnapshot is used to provide a snapshot of the current
@ -25,14 +26,15 @@ type consulSnapshot struct {
} }
// NewFSM is used to construct a new FSM with a blank state // NewFSM is used to construct a new FSM with a blank state
func NewFSM() (*consulFSM, error) { func NewFSM(logOutput io.Writer) (*consulFSM, error) {
state, err := NewStateStore() state, err := NewStateStore()
if err != nil { if err != nil {
return nil, err return nil, err
} }
fsm := &consulFSM{ fsm := &consulFSM{
state: state, logger: log.New(logOutput, "", log.LstdFlags),
state: state,
} }
return fsm, nil return fsm, nil
} }
@ -65,17 +67,26 @@ func (c *consulFSM) decodeRegister(buf []byte) interface{} {
func (c *consulFSM) applyRegister(req *structs.RegisterRequest) interface{} { func (c *consulFSM) applyRegister(req *structs.RegisterRequest) interface{} {
// Ensure the node // Ensure the node
node := structs.Node{req.Node, req.Address} node := structs.Node{req.Node, req.Address}
c.state.EnsureNode(node) if err := c.state.EnsureNode(node); err != nil {
c.logger.Printf("[INFO] consul.fsm: EnsureNode failed: %v", err)
return err
}
// Ensure the service if provided // Ensure the service if provided
if req.Service != nil { if req.Service != nil {
c.state.EnsureService(req.Node, req.Service.ID, req.Service.Service, if err := c.state.EnsureService(req.Node, req.Service.ID, req.Service.Service,
req.Service.Tag, req.Service.Port) req.Service.Tag, req.Service.Port); err != nil {
c.logger.Printf("[INFO] consul.fsm: EnsureService failed: %v", err)
return err
}
} }
// Ensure the check if provided // Ensure the check if provided
if req.Check != nil { if req.Check != nil {
c.state.EnsureCheck(req.Check) if err := c.state.EnsureCheck(req.Check); err != nil {
c.logger.Printf("[INFO] consul.fsm: EnsureCheck failed: %v", err)
return err
}
} }
return nil return nil
@ -89,18 +100,27 @@ func (c *consulFSM) applyDeregister(buf []byte) interface{} {
// Either remove the service entry or the whole node // Either remove the service entry or the whole node
if req.ServiceID != "" { if req.ServiceID != "" {
c.state.DeleteNodeService(req.Node, req.ServiceID) if err := c.state.DeleteNodeService(req.Node, req.ServiceID); err != nil {
c.logger.Printf("[INFO] consul.fsm: DeleteNodeService failed: %v", err)
return err
}
} else if req.CheckID != "" { } else if req.CheckID != "" {
c.state.DeleteNodeCheck(req.Node, req.CheckID) if err := c.state.DeleteNodeCheck(req.Node, req.CheckID); err != nil {
c.logger.Printf("[INFO] consul.fsm: DeleteNodeCheck failed: %v", err)
return err
}
} else { } else {
c.state.DeleteNode(req.Node) if err := c.state.DeleteNode(req.Node); err != nil {
c.logger.Printf("[INFO] consul.fsm: DeleteNode failed: %v", err)
return err
}
} }
return nil return nil
} }
func (c *consulFSM) Snapshot() (raft.FSMSnapshot, error) { func (c *consulFSM) Snapshot() (raft.FSMSnapshot, error) {
defer func(start time.Time) { defer func(start time.Time) {
log.Printf("[INFO] consul: FSM snapshot created in %v", time.Now().Sub(start)) c.logger.Printf("[INFO] consul.fsm: snapshot created in %v", time.Now().Sub(start))
}(time.Now()) }(time.Now())
// Create a new snapshot // Create a new snapshot

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/consul/structs"
"github.com/hashicorp/raft" "github.com/hashicorp/raft"
"os"
"testing" "testing"
) )
@ -35,7 +36,7 @@ func makeLog(buf []byte) *raft.Log {
} }
func TestFSM_RegisterNode(t *testing.T) { func TestFSM_RegisterNode(t *testing.T) {
fsm, err := NewFSM() fsm, err := NewFSM(os.Stderr)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -68,7 +69,7 @@ func TestFSM_RegisterNode(t *testing.T) {
} }
func TestFSM_RegisterNode_Service(t *testing.T) { func TestFSM_RegisterNode_Service(t *testing.T) {
fsm, err := NewFSM() fsm, err := NewFSM(os.Stderr)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -120,7 +121,7 @@ func TestFSM_RegisterNode_Service(t *testing.T) {
} }
func TestFSM_DeregisterService(t *testing.T) { func TestFSM_DeregisterService(t *testing.T) {
fsm, err := NewFSM() fsm, err := NewFSM(os.Stderr)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -174,7 +175,7 @@ func TestFSM_DeregisterService(t *testing.T) {
} }
func TestFSM_DeregisterCheck(t *testing.T) { func TestFSM_DeregisterCheck(t *testing.T) {
fsm, err := NewFSM() fsm, err := NewFSM(os.Stderr)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -228,7 +229,7 @@ func TestFSM_DeregisterCheck(t *testing.T) {
} }
func TestFSM_DeregisterNode(t *testing.T) { func TestFSM_DeregisterNode(t *testing.T) {
fsm, err := NewFSM() fsm, err := NewFSM(os.Stderr)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -294,7 +295,7 @@ func TestFSM_DeregisterNode(t *testing.T) {
} }
func TestFSM_SnapshotRestore(t *testing.T) { func TestFSM_SnapshotRestore(t *testing.T) {
fsm, err := NewFSM() fsm, err := NewFSM(os.Stderr)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -329,7 +330,7 @@ func TestFSM_SnapshotRestore(t *testing.T) {
} }
// Try to restore on a new FSM // Try to restore on a new FSM
fsm2, err := NewFSM() fsm2, err := NewFSM(os.Stderr)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }

View File

@ -195,7 +195,7 @@ func (s *Server) setupRaft() error {
// Create the FSM // Create the FSM
var err error var err error
s.fsm, err = NewFSM() s.fsm, err = NewFSM(s.config.LogOutput)
if err != nil { if err != nil {
return err return err
} }