Skip to content

Commit

Permalink
[ha-agent] Reset on empty RC updates
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreYang committed Dec 19, 2024
1 parent 79af72b commit 1bd744a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
13 changes: 13 additions & 0 deletions comp/haagent/impl/haagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func (h *haAgentImpl) SetLeader(leaderAgentHostname string) {
}
}

func (h *haAgentImpl) resetAgentState() {
h.state.Store(string(haagent.Unknown))
}

// ShouldRunIntegration return true if the agent integrations should to run.
// When ha-agent is disabled, the agent behave as standalone agent (non HA) and will always run all integrations.
func (h *haAgentImpl) ShouldRunIntegration(integrationName string) bool {
Expand All @@ -78,6 +82,15 @@ func (h *haAgentImpl) ShouldRunIntegration(integrationName string) bool {
func (h *haAgentImpl) onHaAgentUpdate(updates map[string]state.RawConfig, applyStateCallback func(string, state.ApplyStatus)) {
h.log.Debugf("Updates received: count=%d", len(updates))

// New updates arrived, but if the list of updates is empty,
// it means we don't have any updates applying to this agent anymore.
// In this case, reset HA Agent setting to default states.
if len(updates) == 0 {
h.log.Warn("Empty update received. Resetting Agent State to Unknown.")
h.resetAgentState()
return
}

for configPath, rawConfig := range updates {
h.log.Debugf("Received config %s: %s", configPath, string(rawConfig.Config))
haAgentMsg := haAgentConfig{}
Expand Down
36 changes: 32 additions & 4 deletions comp/haagent/impl/haagent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,36 @@ func Test_RCListener(t *testing.T) {
}

func Test_haAgentImpl_onHaAgentUpdate(t *testing.T) {

tests := []struct {
name string
initialState haagent.State
updates map[string]state.RawConfig
expectedApplyID string
expectedApplyStatus state.ApplyStatus
expectedAgentState haagent.State
}{
{
name: "successful update",
name: "successful update with leader matching current agent",
initialState: haagent.Unknown,
updates: map[string]state.RawConfig{
testConfigID: {Config: []byte(`{"group":"testGroup01","leader":"ha-agent1"}`)},
testConfigID: {Config: []byte(`{"group":"testGroup01","leader":"my-agent-hostname"}`)},
},
expectedApplyID: testConfigID,
expectedApplyStatus: state.ApplyStatus{
State: state.ApplyStateAcknowledged,
},
expectedAgentState: haagent.Active,
},
{
name: "successful update with leader NOT matching current agent",
updates: map[string]state.RawConfig{
testConfigID: {Config: []byte(`{"group":"testGroup01","leader":"another-agent-hostname"}`)},
},
expectedApplyID: testConfigID,
expectedApplyStatus: state.ApplyStatus{
State: state.ApplyStateAcknowledged,
},
expectedAgentState: haagent.Standby,
},
{
name: "invalid payload",
Expand All @@ -133,17 +147,26 @@ func Test_haAgentImpl_onHaAgentUpdate(t *testing.T) {
State: state.ApplyStateError,
Error: "error unmarshalling payload",
},
expectedAgentState: haagent.Unknown,
},
{
name: "invalid group",
updates: map[string]state.RawConfig{
testConfigID: {Config: []byte(`{"group":"invalidGroup","leader":"ha-agent1"}`)},
testConfigID: {Config: []byte(`{"group":"invalidGroup","leader":"another-agent-hostname"}`)},
},
expectedApplyID: testConfigID,
expectedApplyStatus: state.ApplyStatus{
State: state.ApplyStateError,
Error: "group does not match",
},
expectedAgentState: haagent.Unknown,
},
{
name: "empty update",
updates: map[string]state.RawConfig{},
//expectedApplyID: testConfigID,
//expectedApplyStatus: nil,
expectedAgentState: haagent.Unknown,
},
}
for _, tt := range tests {
Expand All @@ -160,6 +183,10 @@ func Test_haAgentImpl_onHaAgentUpdate(t *testing.T) {

h := newHaAgentImpl(logmock.New(t), newHaAgentConfigs(agentConfigComponent))

if tt.initialState != "" {
h.state.Store(string(tt.initialState))
}

var applyID string
var applyStatus state.ApplyStatus
applyFunc := func(id string, status state.ApplyStatus) {
Expand All @@ -169,6 +196,7 @@ func Test_haAgentImpl_onHaAgentUpdate(t *testing.T) {
h.onHaAgentUpdate(tt.updates, applyFunc)
assert.Equal(t, tt.expectedApplyID, applyID)
assert.Equal(t, tt.expectedApplyStatus, applyStatus)
assert.Equal(t, tt.expectedAgentState, h.GetState())
})
}
}
Expand Down

0 comments on commit 1bd744a

Please sign in to comment.