From a29b877930696f7b629086e380d50c229f23dd62 Mon Sep 17 00:00:00 2001 From: Nurlan Moldomurov Date: Tue, 10 Sep 2024 11:55:25 +0300 Subject: [PATCH 1/5] PMM-13348 Fix status presentation. --- admin/commands/list.go | 4 +++- admin/commands/status.go | 4 +++- agent/agents/process/process.go | 2 +- agent/agents/supervisor/supervisor.go | 20 ++++++++++---------- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/admin/commands/list.go b/admin/commands/list.go index 6f3609e0a8..ed268e49f0 100644 --- a/admin/commands/list.go +++ b/admin/commands/list.go @@ -65,7 +65,9 @@ func (a listResultAgent) NiceAgentStatus() string { if res == "" { res = "unknown" //nolint:goconst } - res = cases.Title(language.English).String(strings.ToLower(res)) + res = strings.ToLower(res) + res = cases.Title(language.English).String(res) + res = strings.ReplaceAll(res, "_", " ") if a.Disabled { res += " (disabled)" } diff --git a/admin/commands/status.go b/admin/commands/status.go index 9395cb1252..ec425f9f04 100644 --- a/admin/commands/status.go +++ b/admin/commands/status.go @@ -60,7 +60,9 @@ func (res *statusResult) HumanReadableAgentType(agentType string) string { } func (res *statusResult) NiceAgentStatus(status string) string { - return cases.Title(language.English).String(strings.ToLower(status)) + status = strings.ToLower(status) + status = cases.Title(language.English).String(status) + return strings.ReplaceAll(status, "_", " ") } func (res *statusResult) Result() {} diff --git a/agent/agents/process/process.go b/agent/agents/process/process.go index 631e18b606..97654d5f5b 100644 --- a/agent/agents/process/process.go +++ b/agent/agents/process/process.go @@ -212,7 +212,7 @@ func (p *Process) toFailing(err error) { p.l.Tracef("Process: failing") p.changes <- inventorypb.AgentStatus_INITIALIZATION_ERROR p.l.Infof("Process: exited: %s.", p.cmd.ProcessState) - go p.toDone() + close(p.changes) p.err = err p.initialized <- false } diff --git a/agent/agents/supervisor/supervisor.go b/agent/agents/supervisor/supervisor.go index c0b0a9d7f2..4e65e37b8e 100644 --- a/agent/agents/supervisor/supervisor.go +++ b/agent/agents/supervisor/supervisor.go @@ -490,6 +490,16 @@ func (s *Supervisor) startProcess(agentID string, agentProcess *agentpb.SetState close(done) }() + //nolint:forcetypeassert + s.agentProcesses[agentID] = &agentProcessInfo{ + cancel: cancel, + done: done, + requestedState: proto.Clone(agentProcess).(*agentpb.SetStateRequest_AgentProcess), + listenPort: port, + processExecPath: processParams.Path, + logStore: logStore, + } + t := time.NewTimer(start_Process_Waiting) defer t.Stop() select { @@ -500,16 +510,6 @@ func (s *Supervisor) startProcess(agentID string, agentProcess *agentpb.SetState } case <-t.C: } - - //nolint:forcetypeassert - s.agentProcesses[agentID] = &agentProcessInfo{ - cancel: cancel, - done: done, - requestedState: proto.Clone(agentProcess).(*agentpb.SetStateRequest_AgentProcess), - listenPort: port, - processExecPath: processParams.Path, - logStore: logStore, - } return nil } From 8a1d459aba935d349137b5fd40cdbf70b8c5b02e Mon Sep 17 00:00:00 2001 From: Nurlan Moldomurov Date: Fri, 13 Sep 2024 00:22:43 +0300 Subject: [PATCH 2/5] PMM-13348 fix testing. --- agent/agents/process/process_test.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/agent/agents/process/process_test.go b/agent/agents/process/process_test.go index 7ab30eae16..fde8dee634 100644 --- a/agent/agents/process/process_test.go +++ b/agent/agents/process/process_test.go @@ -80,22 +80,24 @@ func TestProcess(t *testing.T) { }) t.Run("FailedToStart", func(t *testing.T) { - ctx, _, l := setup(t) + ctx, cancel, l := setup(t) p := New(&Params{Path: "no_such_command"}, nil, l) go p.Run(ctx) - assertStates(t, p, inventorypb.AgentStatus_STARTING, inventorypb.AgentStatus_INITIALIZATION_ERROR, - inventorypb.AgentStatus_DONE, inventorypb.AgentStatus_AGENT_STATUS_INVALID) + assertStates(t, p, inventorypb.AgentStatus_STARTING, inventorypb.AgentStatus_INITIALIZATION_ERROR, inventorypb.AgentStatus_AGENT_STATUS_INVALID) + cancel() + assertStates(t, p, inventorypb.AgentStatus_AGENT_STATUS_INVALID) }) t.Run("ExitedEarly", func(t *testing.T) { sleep := strconv.FormatFloat(runningT.Seconds()-0.5, 'f', -1, 64) - ctx, _, l := setup(t) + ctx, cancel, l := setup(t) p := New(&Params{Path: "sleep", Args: []string{sleep}}, nil, l) go p.Run(ctx) - assertStates(t, p, inventorypb.AgentStatus_STARTING, inventorypb.AgentStatus_INITIALIZATION_ERROR, - inventorypb.AgentStatus_DONE, inventorypb.AgentStatus_AGENT_STATUS_INVALID) + assertStates(t, p, inventorypb.AgentStatus_STARTING, inventorypb.AgentStatus_INITIALIZATION_ERROR, inventorypb.AgentStatus_AGENT_STATUS_INVALID) + cancel() + assertStates(t, p, inventorypb.AgentStatus_AGENT_STATUS_INVALID) }) t.Run("Exited", func(t *testing.T) { From c6856e4301ca4e5f426ec428b6f58441180747ed Mon Sep 17 00:00:00 2001 From: Nurlan Moldomurov Date: Fri, 13 Sep 2024 13:25:50 +0300 Subject: [PATCH 3/5] PMM-13348 refactoring. --- admin/commands/list.go | 7 ++----- admin/commands/status.go | 8 ++------ admin/helpers/helpers.go | 9 +++++++++ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/admin/commands/list.go b/admin/commands/list.go index ed268e49f0..7965a8d5aa 100644 --- a/admin/commands/list.go +++ b/admin/commands/list.go @@ -25,10 +25,9 @@ import ( "github.com/AlekSi/pointer" "github.com/sirupsen/logrus" - "golang.org/x/text/cases" - "golang.org/x/text/language" "github.com/percona/pmm/admin/agentlocal" + "github.com/percona/pmm/admin/helpers" "github.com/percona/pmm/api/inventorypb/json/client" "github.com/percona/pmm/api/inventorypb/json/client/agents" "github.com/percona/pmm/api/inventorypb/json/client/services" @@ -65,9 +64,7 @@ func (a listResultAgent) NiceAgentStatus() string { if res == "" { res = "unknown" //nolint:goconst } - res = strings.ToLower(res) - res = cases.Title(language.English).String(res) - res = strings.ReplaceAll(res, "_", " ") + res = helpers.NiceAgentStatus(res) if a.Disabled { res += " (disabled)" } diff --git a/admin/commands/status.go b/admin/commands/status.go index ec425f9f04..e83b3226f3 100644 --- a/admin/commands/status.go +++ b/admin/commands/status.go @@ -17,14 +17,12 @@ package commands import ( "context" "net/url" - "strings" "time" "github.com/pkg/errors" - "golang.org/x/text/cases" - "golang.org/x/text/language" "github.com/percona/pmm/admin/agentlocal" + "github.com/percona/pmm/admin/helpers" "github.com/percona/pmm/api/inventorypb/types" "github.com/percona/pmm/version" ) @@ -60,9 +58,7 @@ func (res *statusResult) HumanReadableAgentType(agentType string) string { } func (res *statusResult) NiceAgentStatus(status string) string { - status = strings.ToLower(status) - status = cases.Title(language.English).String(status) - return strings.ReplaceAll(status, "_", " ") + return helpers.NiceAgentStatus(status) } func (res *statusResult) Result() {} diff --git a/admin/helpers/helpers.go b/admin/helpers/helpers.go index 4f90c21ff7..7583b8996a 100644 --- a/admin/helpers/helpers.go +++ b/admin/helpers/helpers.go @@ -17,6 +17,9 @@ package helpers import ( "fmt" + "golang.org/x/text/cases" + "golang.org/x/text/language" + "strings" "github.com/pkg/errors" @@ -92,3 +95,9 @@ func IsOnPmmServer() (bool, error) { return status.NodeID == "pmm-server", nil } + +func NiceAgentStatus(status string) string { + status = strings.ToLower(status) + status = cases.Title(language.English).String(status) + return strings.ReplaceAll(status, "_", " ") +} From ce02a0479e85920ab70f9be904e3c0ab7aeb5d2e Mon Sep 17 00:00:00 2001 From: Nurlan Moldomurov Date: Fri, 13 Sep 2024 13:43:52 +0300 Subject: [PATCH 4/5] PMM-13348 fix test and linters. --- admin/helpers/helpers.go | 4 ++-- agent/agents/supervisor/supervisor_test.go | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/admin/helpers/helpers.go b/admin/helpers/helpers.go index 7583b8996a..61ae79ad2a 100644 --- a/admin/helpers/helpers.go +++ b/admin/helpers/helpers.go @@ -17,11 +17,11 @@ package helpers import ( "fmt" - "golang.org/x/text/cases" - "golang.org/x/text/language" "strings" "github.com/pkg/errors" + "golang.org/x/text/cases" + "golang.org/x/text/language" "github.com/percona/pmm/admin/agentlocal" "github.com/percona/pmm/api/inventorypb/json/client/nodes" diff --git a/agent/agents/supervisor/supervisor_test.go b/agent/agents/supervisor/supervisor_test.go index 4df5188813..bca9201f43 100644 --- a/agent/agents/supervisor/supervisor_test.go +++ b/agent/agents/supervisor/supervisor_test.go @@ -274,7 +274,8 @@ func TestStartProcessFail(t *testing.T) { t.Run("Start", func(t *testing.T) { expectedList := []*agentlocalpb.AgentInfo{} - require.Equal(t, expectedList, s.AgentsList()) + list := s.AgentsList() + require.Equal(t, expectedList, list) s.SetState(&agentpb.SetStateRequest{ AgentProcesses: map[string]*agentpb.SetStateRequest_AgentProcess{ @@ -285,14 +286,13 @@ func TestStartProcessFail(t *testing.T) { assertChanges(t, s, &agentpb.StateChangedRequest{AgentId: "sleep1", Status: inventorypb.AgentStatus_STARTING, ListenPort: 65000, ProcessExecPath: "sleep"}, &agentpb.StateChangedRequest{AgentId: "sleep1", Status: inventorypb.AgentStatus_INITIALIZATION_ERROR, ListenPort: 65000, ProcessExecPath: "sleep"}, - &agentpb.StateChangedRequest{AgentId: "sleep1", Status: inventorypb.AgentStatus_DONE, ListenPort: 65000, ProcessExecPath: "sleep"}, &agentpb.StateChangedRequest{AgentId: "sleep1", Status: inventorypb.AgentStatus_STARTING, ListenPort: 65001, ProcessExecPath: "sleep"}, &agentpb.StateChangedRequest{AgentId: "sleep1", Status: inventorypb.AgentStatus_INITIALIZATION_ERROR, ListenPort: 65001, ProcessExecPath: "sleep"}, - &agentpb.StateChangedRequest{AgentId: "sleep1", Status: inventorypb.AgentStatus_DONE, ListenPort: 65001, ProcessExecPath: "sleep"}, &agentpb.StateChangedRequest{AgentId: "sleep1", Status: inventorypb.AgentStatus_STARTING, ListenPort: 65002, ProcessExecPath: "sleep"}, - &agentpb.StateChangedRequest{AgentId: "sleep1", Status: inventorypb.AgentStatus_INITIALIZATION_ERROR, ListenPort: 65002, ProcessExecPath: "sleep"}, - &agentpb.StateChangedRequest{AgentId: "sleep1", Status: inventorypb.AgentStatus_DONE, ListenPort: 65002, ProcessExecPath: "sleep"}) - expectedList = []*agentlocalpb.AgentInfo{} + &agentpb.StateChangedRequest{AgentId: "sleep1", Status: inventorypb.AgentStatus_INITIALIZATION_ERROR, ListenPort: 65002, ProcessExecPath: "sleep"}) + expectedList = []*agentlocalpb.AgentInfo{ + {AgentType: type_TEST_SLEEP, AgentId: "sleep1", Status: inventorypb.AgentStatus_INITIALIZATION_ERROR, ListenPort: 65002, ProcessExecPath: "sleep"}, + } require.Equal(t, expectedList, s.AgentsList()) }) } From d82bd154006f2d4145002423baa4bd0c41f685ac Mon Sep 17 00:00:00 2001 From: Nurlan Moldomurov Date: Fri, 13 Sep 2024 13:57:45 +0300 Subject: [PATCH 5/5] PMM-13348 Add comment. --- admin/helpers/helpers.go | 1 + 1 file changed, 1 insertion(+) diff --git a/admin/helpers/helpers.go b/admin/helpers/helpers.go index 61ae79ad2a..7a76eff2a4 100644 --- a/admin/helpers/helpers.go +++ b/admin/helpers/helpers.go @@ -96,6 +96,7 @@ func IsOnPmmServer() (bool, error) { return status.NodeID == "pmm-server", nil } +// NiceAgentStatus returns prettified agent status. func NiceAgentStatus(status string) string { status = strings.ToLower(status) status = cases.Title(language.English).String(status)