Skip to content

Commit 8b64fae

Browse files
committed
internal code modifications to be compatible with openrpc spec:
- seperate the perf task getters to have a method for each task with a defined result type instead of interface{} - modify healthcheck task to have its result as slice of object containes the check name instead of map from name to error - modify public ip task to have its result as slice of reports with the ip included instead of map from the ip to report - update performance monitor stub - modify the diagnositcs response to be slice of modules status instead of map
1 parent 38a0b91 commit 8b64fae

File tree

8 files changed

+368
-85
lines changed

8 files changed

+368
-85
lines changed

pkg/diagnostics/diagnostics.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import (
88

99
"github.com/gomodule/redigo/redis"
1010
"github.com/threefoldtech/zbus"
11+
"github.com/threefoldtech/zos/pkg"
12+
"github.com/threefoldtech/zos/pkg/perf"
1113
"github.com/threefoldtech/zos/pkg/utils"
1214
)
1315

14-
const (
15-
callTimeout = 3 * time.Second
16-
testNetworkKey = "perf.healthcheck"
17-
)
16+
const callTimeout = 3 * time.Second
17+
18+
var testNetworkKey = perf.GeneratePerfKey(pkg.HealthCheckTaskName)
1819

1920
// Modules is all the registered modules on zbus
2021
var Modules = []string{
@@ -32,6 +33,8 @@ var Modules = []string{
3233

3334
// ModuleStatus represents the status of a module or shows if error
3435
type ModuleStatus struct {
36+
// Name is the name of the module
37+
Name string `json:"name"`
3538
// Status holds the status of the module
3639
Status zbus.Status `json:"status,omitempty"`
3740
// Err contains any error related to the module
@@ -43,7 +46,7 @@ type Diagnostics struct {
4346
// SystemStatusOk is the overall system status
4447
SystemStatusOk bool `json:"system_status_ok"`
4548
// ZosModules is a list of modules with their objects and workers
46-
ZosModules map[string]ModuleStatus `json:"modules"`
49+
ZosModules []ModuleStatus `json:"modules"`
4750
// Healthy is the state of the node health check
4851
Healthy bool `json:"healthy"`
4952
}
@@ -70,7 +73,7 @@ func NewDiagnosticsManager(
7073
func (m *DiagnosticsManager) GetSystemDiagnostics(ctx context.Context) (Diagnostics, error) {
7174
results := Diagnostics{
7275
SystemStatusOk: true,
73-
ZosModules: make(map[string]ModuleStatus),
76+
ZosModules: []ModuleStatus{},
7477
}
7578

7679
var wg sync.WaitGroup
@@ -86,7 +89,7 @@ func (m *DiagnosticsManager) GetSystemDiagnostics(ctx context.Context) (Diagnost
8689
mut.Lock()
8790
defer mut.Unlock()
8891

89-
results.ZosModules[module] = report
92+
results.ZosModules = append(results.ZosModules, report)
9093

9194
if report.Err != nil {
9295
hasError = true
@@ -109,6 +112,7 @@ func (m *DiagnosticsManager) getModuleStatus(ctx context.Context, module string)
109112

110113
status, err := m.zbusClient.Status(ctx, module)
111114
return ModuleStatus{
115+
Name: module,
112116
Status: status,
113117
Err: err,
114118
}

pkg/perf/cache.go

Lines changed: 107 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,25 @@ var (
1818
ErrResultNotFound = errors.New("result not found")
1919
)
2020

21-
// generateKey is helper method to add moduleName as prefix for the taskName
22-
func generateKey(taskName string) string {
21+
// GeneratePerfKey is helper method to add moduleName as prefix for the taskName
22+
func GeneratePerfKey(taskName string) string {
2323
return fmt.Sprintf("%s.%s", moduleName, taskName)
2424
}
2525

26+
// exists check if a key exists
27+
func (pm *PerformanceMonitor) exists(key string) (bool, error) {
28+
conn := pm.pool.Get()
29+
defer conn.Close()
30+
31+
ok, err := redis.Bool(conn.Do("EXISTS", GeneratePerfKey(key)))
32+
if err != nil {
33+
return false, errors.Wrapf(err, "error checking if key %s exists", GeneratePerfKey(key))
34+
}
35+
return ok, nil
36+
}
37+
2638
// setCache set result in redis
27-
func (pm *PerformanceMonitor) setCache(ctx context.Context, result pkg.TaskResult) error {
39+
func (pm *PerformanceMonitor) setCache(_ context.Context, result pkg.TaskResult) error {
2840
data, err := json.Marshal(result)
2941
if err != nil {
3042
return errors.Wrap(err, "failed to marshal data to JSON")
@@ -33,10 +45,99 @@ func (pm *PerformanceMonitor) setCache(ctx context.Context, result pkg.TaskResul
3345
conn := pm.pool.Get()
3446
defer conn.Close()
3547

36-
_, err = conn.Do("SET", generateKey(result.Name), data)
48+
_, err = conn.Do("SET", GeneratePerfKey(result.Name), data)
3749
return err
3850
}
3951

52+
func (pm *PerformanceMonitor) getTaskResult(conn redis.Conn, key string, result interface{}) error {
53+
data, err := conn.Do("GET", GeneratePerfKey(key))
54+
if err != nil {
55+
return errors.Wrap(err, "failed to get the result")
56+
}
57+
58+
if data == nil {
59+
return ErrResultNotFound
60+
}
61+
62+
err = json.Unmarshal(data.([]byte), result)
63+
if err != nil {
64+
return errors.Wrap(err, "failed to unmarshal data from json")
65+
}
66+
67+
return nil
68+
}
69+
70+
func (pm *PerformanceMonitor) GetIperfTaskResult() (pkg.IperfTaskResult, error) {
71+
conn := pm.pool.Get()
72+
defer conn.Close()
73+
74+
var res pkg.IperfTaskResult
75+
err := pm.getTaskResult(conn, pkg.IperfTaskName, &res)
76+
return res, err
77+
}
78+
79+
func (pm *PerformanceMonitor) GetHealthTaskResult() (pkg.HealthTaskResult, error) {
80+
conn := pm.pool.Get()
81+
defer conn.Close()
82+
83+
var res pkg.HealthTaskResult
84+
err := pm.getTaskResult(conn, pkg.HealthCheckTaskName, &res)
85+
return res, err
86+
}
87+
88+
func (pm *PerformanceMonitor) GetPublicIpTaskResult() (pkg.PublicIpTaskResult, error) {
89+
conn := pm.pool.Get()
90+
defer conn.Close()
91+
92+
var res pkg.PublicIpTaskResult
93+
err := pm.getTaskResult(conn, pkg.PublicIpTaskName, &res)
94+
return res, err
95+
}
96+
97+
func (pm *PerformanceMonitor) GetCpuBenchTaskResult() (pkg.CpuBenchTaskResult, error) {
98+
conn := pm.pool.Get()
99+
defer conn.Close()
100+
101+
var res pkg.CpuBenchTaskResult
102+
err := pm.getTaskResult(conn, pkg.CpuBenchmarkTaskName, &res)
103+
return res, err
104+
}
105+
106+
func (pm *PerformanceMonitor) GetAllTaskResult() (pkg.AllTaskResult, error) {
107+
conn := pm.pool.Get()
108+
defer conn.Close()
109+
110+
var results pkg.AllTaskResult
111+
112+
var cpu pkg.CpuBenchTaskResult
113+
if err := pm.getTaskResult(conn, pkg.CpuBenchmarkTaskName, &cpu); err != nil {
114+
return pkg.AllTaskResult{}, fmt.Errorf("failed to get health result: %w", err)
115+
}
116+
results.CpuBenchmark = cpu
117+
118+
var health pkg.HealthTaskResult
119+
if err := pm.getTaskResult(conn, pkg.HealthCheckTaskName, &health); err != nil {
120+
return pkg.AllTaskResult{}, fmt.Errorf("failed to get health result: %w", err)
121+
}
122+
results.HealthCheck = health
123+
124+
var iperf pkg.IperfTaskResult
125+
if err := pm.getTaskResult(conn, pkg.IperfTaskName, &iperf); err != nil {
126+
return pkg.AllTaskResult{}, fmt.Errorf("failed to get iperf result: %w", err)
127+
}
128+
results.Iperf = iperf
129+
130+
var pIp pkg.PublicIpTaskResult
131+
if err := pm.getTaskResult(conn, pkg.PublicIpTaskName, &pIp); err != nil {
132+
return pkg.AllTaskResult{}, fmt.Errorf("failed to get public ip result: %w", err)
133+
}
134+
results.PublicIp = pIp
135+
136+
return results, nil
137+
}
138+
139+
// DEPRECATED
140+
40141
// get directly gets result for some key
41142
func get(conn redis.Conn, key string) (pkg.TaskResult, error) {
42143
var res pkg.TaskResult
@@ -62,7 +163,7 @@ func get(conn redis.Conn, key string) (pkg.TaskResult, error) {
62163
func (pm *PerformanceMonitor) Get(taskName string) (pkg.TaskResult, error) {
63164
conn := pm.pool.Get()
64165
defer conn.Close()
65-
return get(conn, generateKey(taskName))
166+
return get(conn, GeneratePerfKey(taskName))
66167
}
67168

68169
// GetAll gets the results for all the tests with moduleName as prefix
@@ -76,7 +177,7 @@ func (pm *PerformanceMonitor) GetAll() ([]pkg.TaskResult, error) {
76177

77178
cursor := 0
78179
for {
79-
values, err := redis.Values(conn.Do("SCAN", cursor, "MATCH", generateKey("*")))
180+
values, err := redis.Values(conn.Do("SCAN", cursor, "MATCH", GeneratePerfKey("*")))
80181
if err != nil {
81182
return nil, err
82183
}
@@ -101,15 +202,3 @@ func (pm *PerformanceMonitor) GetAll() ([]pkg.TaskResult, error) {
101202
}
102203
return res, nil
103204
}
104-
105-
// exists check if a key exists
106-
func (pm *PerformanceMonitor) exists(key string) (bool, error) {
107-
conn := pm.pool.Get()
108-
defer conn.Close()
109-
110-
ok, err := redis.Bool(conn.Do("EXISTS", generateKey(key)))
111-
if err != nil {
112-
return false, errors.Wrapf(err, "error checking if key %s exists", generateKey(key))
113-
}
114-
return ok, nil
115-
}

pkg/perf/cpubench/cpubench_task.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os/exec"
88

9+
"github.com/threefoldtech/zos/pkg"
910
"github.com/threefoldtech/zos/pkg/perf"
1011
"github.com/threefoldtech/zos/pkg/stubs"
1112
)
@@ -30,7 +31,7 @@ func NewTask() perf.Task {
3031

3132
// ID returns task ID.
3233
func (c *CPUBenchmarkTask) ID() string {
33-
return "cpu-benchmark"
34+
return pkg.CpuBenchmarkTaskName
3435
}
3536

3637
// Cron returns task cron schedule.

pkg/perf/healthcheck/healthcheck.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import (
99

1010
"github.com/cenkalti/backoff"
1111
"github.com/rs/zerolog/log"
12+
"github.com/threefoldtech/zos/pkg"
1213
"github.com/threefoldtech/zos/pkg/app"
1314
"github.com/threefoldtech/zos/pkg/perf"
1415
"github.com/threefoldtech/zos/pkg/stubs"
1516
)
1617

1718
const (
18-
id = "healthcheck"
1919
schedule = "0 */15 * * * *"
2020
description = "health check task runs multiple checks to ensure the node is in a usable state and set flags for the power daemon to stop reporting uptime if it is not usable"
2121
)
@@ -41,7 +41,7 @@ var _ perf.Task = (*healthcheckTask)(nil)
4141

4242
// ID returns task ID.
4343
func (h *healthcheckTask) ID() string {
44-
return id
44+
return pkg.HealthCheckTaskName
4545
}
4646

4747
func (h *healthcheckTask) Jitter() uint32 {
@@ -61,7 +61,7 @@ func (h *healthcheckTask) Description() string {
6161
// Run executes the health checks.
6262
func (h *healthcheckTask) Run(ctx context.Context) (interface{}, error) {
6363
log.Debug().Msg("starting health check task")
64-
errs := make(map[string][]string)
64+
errs := []pkg.HealthReport{}
6565

6666
cl := perf.GetZbusClient(ctx)
6767
zui := stubs.NewZUIStub(cl)
@@ -79,9 +79,13 @@ func (h *healthcheckTask) Run(ctx context.Context) (interface{}, error) {
7979

8080
mut.Lock()
8181
defer mut.Unlock()
82-
errs[label] = errorsToStrings(errors)
82+
errsStr := errorsToStrings(errors)
83+
errs = append(errs, pkg.HealthReport{
84+
TestName: label,
85+
Errors: errsStr,
86+
})
8387

84-
if err := zui.PushErrors(ctx, label, errs[label]); err != nil {
88+
if err := zui.PushErrors(ctx, label, errsStr); err != nil {
8589
return err
8690
}
8791

pkg/perf/iperf/iperf_task.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/cenkalti/backoff"
1414
"github.com/pkg/errors"
1515
"github.com/rs/zerolog/log"
16+
"github.com/threefoldtech/zos/pkg"
1617
"github.com/threefoldtech/zos/pkg/environment"
1718
"github.com/threefoldtech/zos/pkg/network/iperf"
1819
"github.com/threefoldtech/zos/pkg/perf"
@@ -55,7 +56,7 @@ func NewTask() perf.Task {
5556

5657
// ID returns the ID of the tcp task
5758
func (t *IperfTest) ID() string {
58-
return "iperf"
59+
return pkg.IperfTaskName
5960
}
6061

6162
// Cron returns the schedule for the tcp task

0 commit comments

Comments
 (0)