Skip to content

Commit

Permalink
cluster: fix tiflash uptime (#2227)
Browse files Browse the repository at this point in the history
  • Loading branch information
nexustar committed Jul 13, 2023
1 parent 4fd37f0 commit b3b7040
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
37 changes: 3 additions & 34 deletions pkg/cluster/manager/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
"github.com/pingcap/tiup/pkg/set"
"github.com/pingcap/tiup/pkg/tui"
"github.com/pingcap/tiup/pkg/utils"
"go.uber.org/zap"
)

// DisplayOption represents option of display command
Expand Down Expand Up @@ -610,8 +609,9 @@ func (m *Manager) GetClusterTopology(dopt DisplayOption, opt operator.Options) (
e, found := ctxt.GetInner(ctx).GetExecutor(ins.GetManageHost())
if found {
var active string
var systemdSince time.Duration
nctx := checkpoint.NewContext(ctx)
active, memory, _ = operator.GetServiceStatus(nctx, e, ins.ServiceName())
active, memory, systemdSince, _ = operator.GetServiceStatus(nctx, e, ins.ServiceName())
if status == "-" {
if active == "active" {
status = "Up"
Expand All @@ -620,7 +620,7 @@ func (m *Manager) GetClusterTopology(dopt DisplayOption, opt operator.Options) (
}
}
if dopt.ShowUptime && since == "-" {
since = formatInstanceSince(parseSystemctlSince(active))
since = formatInstanceSince(systemdSince)
}
}
}
Expand Down Expand Up @@ -733,37 +733,6 @@ func formatInstanceSince(uptime time.Duration) string {
return strings.Join(parts, "")
}

// `systemctl status xxx.service` returns as below
// Active: active (running) since Sat 2021-03-27 10:51:11 CST; 41min ago
func parseSystemctlSince(str string) (dur time.Duration) {
// if service is not found or other error, don't need to parse it
if str == "" {
return 0
}
defer func() {
if dur == 0 {
zap.L().Warn("failed to parse systemctl since", zap.String("value", str))
}
}()
parts := strings.Split(str, ";")
if len(parts) != 2 {
return
}
parts = strings.Split(parts[0], " ")
if len(parts) < 3 {
return
}

dateStr := strings.Join(parts[len(parts)-3:], " ")

tm, err := time.Parse("2006-01-02 15:04:05 MST", dateStr)
if err != nil {
return
}

return time.Since(tm)
}

// SetSSHKeySet set ssh key set.
func SetSSHKeySet(ctx context.Context, privateKeyPath string, publicKeyPath string) error {
ctxt.GetInner(ctx).PrivateKeyPath = privateKeyPath
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/operation/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func CheckServices(ctx context.Context, e ctxt.Executor, host, service string, d
return result
}

active, _, err := GetServiceStatus(ctx, e, service+".service")
active, _, _, err := GetServiceStatus(ctx, e, service+".service")
if err != nil {
result.Err = err
}
Expand Down
36 changes: 35 additions & 1 deletion pkg/cluster/operation/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ package operator
import (
"context"
"strings"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/ctxt"
"github.com/pingcap/tiup/pkg/cluster/module"
"go.uber.org/zap"
)

// GetServiceStatus return the Acitive line of status.
Expand All @@ -34,7 +36,7 @@ import (
Mar 09 13:56:19 ip-172-16-5-70 systemd[1]: Started drainer-8249 service.
*/
func GetServiceStatus(ctx context.Context, e ctxt.Executor, name string) (active, memory string, err error) {
func GetServiceStatus(ctx context.Context, e ctxt.Executor, name string) (active, memory string, since time.Duration, err error) {
c := module.SystemdModuleConfig{
Unit: name,
Action: "status",
Expand All @@ -50,6 +52,7 @@ func GetServiceStatus(ctx context.Context, e ctxt.Executor, name string) (active
switch words[0] {
case "Active:":
active = words[1]
since = parseSystemctlSince(line)
case "Memory:":
memory = words[1]
}
Expand All @@ -60,3 +63,34 @@ func GetServiceStatus(ctx context.Context, e ctxt.Executor, name string) (active
}
return
}

// `systemctl status xxx.service` returns as below
// Active: active (running) since Sat 2021-03-27 10:51:11 CST; 41min ago
func parseSystemctlSince(str string) (dur time.Duration) {
// if service is not found or other error, don't need to parse it
if str == "" {
return 0
}
defer func() {
if dur == 0 {
zap.L().Warn("failed to parse systemctl since", zap.String("value", str))
}
}()
parts := strings.Split(str, ";")
if len(parts) != 2 {
return
}
parts = strings.Split(parts[0], " ")
if len(parts) < 3 {
return
}

dateStr := strings.Join(parts[len(parts)-3:], " ")

tm, err := time.Parse("2006-01-02 15:04:05 MST", dateStr)
if err != nil {
return
}

return time.Since(tm)
}

0 comments on commit b3b7040

Please sign in to comment.