From 0d6de257a2de6775770299b1445b5475aa17f14c Mon Sep 17 00:00:00 2001 From: Pierre Souchay Date: Tue, 14 Aug 2018 18:45:33 +0200 Subject: [PATCH] Display more information about check being not properly added when it fails (#4405) * Display more information about check being not properly added when it fails It follows an incident where we add lots of error messages: [WARN] consul.fsm: EnsureRegistration failed: failed inserting check: Missing service registration That seems related to Consul failing to restart on respective agents. Having Node information as well as service information would help diagnose the issue. * Renamed ensureCheckIfNodeMatches() as requested by @banks --- agent/consul/state/catalog.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/agent/consul/state/catalog.go b/agent/consul/state/catalog.go index 18175d22a5..e4685bc20b 100644 --- a/agent/consul/state/catalog.go +++ b/agent/consul/state/catalog.go @@ -267,6 +267,17 @@ func (s *Store) EnsureRegistration(idx uint64, req *structs.RegisterRequest) err return nil } +func (s *Store) ensureCheckIfNodeMatches(tx *memdb.Txn, idx uint64, node string, check *structs.HealthCheck) error { + if check.Node != node { + return fmt.Errorf("check node %q does not match node %q", + check.Node, node) + } + if err := s.ensureCheckTxn(tx, idx, check); err != nil { + return fmt.Errorf("failed inserting check: %s on node %q", err, check.Node) + } + return nil +} + // ensureRegistrationTxn is used to make sure a node, service, and check // registration is performed within a single transaction to avoid race // conditions on state updates. @@ -316,21 +327,13 @@ func (s *Store) ensureRegistrationTxn(tx *memdb.Txn, idx uint64, req *structs.Re // Add the checks, if any. if req.Check != nil { - if req.Check.Node != req.Node { - return fmt.Errorf("check node %q does not match node %q", - req.Check.Node, req.Node) - } - if err := s.ensureCheckTxn(tx, idx, req.Check); err != nil { - return fmt.Errorf("failed inserting check: %s", err) + if err := s.ensureCheckIfNodeMatches(tx, idx, req.Node, req.Check); err != nil { + return err } } for _, check := range req.Checks { - if check.Node != req.Node { - return fmt.Errorf("check node %q does not match node %q", - check.Node, req.Node) - } - if err := s.ensureCheckTxn(tx, idx, check); err != nil { - return fmt.Errorf("failed inserting check: %s", err) + if err := s.ensureCheckIfNodeMatches(tx, idx, req.Node, check); err != nil { + return err } }