-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtask.go
More file actions
226 lines (183 loc) · 6.22 KB
/
task.go
File metadata and controls
226 lines (183 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package checkconsensussyncstatus
import (
"context"
"fmt"
"time"
"github.com/ethpandaops/assertoor/pkg/clients"
"github.com/ethpandaops/assertoor/pkg/clients/consensus/rpc"
"github.com/ethpandaops/assertoor/pkg/types"
"github.com/ethpandaops/assertoor/pkg/vars"
"github.com/sirupsen/logrus"
)
var (
TaskName = "check_consensus_sync_status"
TaskDescriptor = &types.TaskDescriptor{
Name: TaskName,
Description: "Checks consensus clients for their sync status.",
Category: "consensus",
Config: DefaultConfig(),
Outputs: []types.TaskOutputDefinition{
{
Name: "goodClients",
Type: "array",
Description: "Array of clients that meet sync criteria.",
},
{
Name: "failedClients",
Type: "array",
Description: "Array of clients that do not meet sync criteria.",
},
},
NewTask: NewTask,
}
)
type Task struct {
ctx *types.TaskContext
options *types.TaskOptions
config Config
logger logrus.FieldLogger
firstHeight map[uint16]uint64
}
type ClientInfo struct {
Name string `json:"name"`
Optimistic bool `json:"optimistic"`
Synchronizing bool `json:"synchronizing"`
SyncHead uint64 `json:"syncHead"`
SyncDistance uint64 `json:"syncDistance"`
}
func NewTask(ctx *types.TaskContext, options *types.TaskOptions) (types.Task, error) {
return &Task{
ctx: ctx,
options: options,
logger: ctx.Logger.GetLogger(),
firstHeight: map[uint16]uint64{},
}, nil
}
func (t *Task) Config() interface{} {
return t.config
}
func (t *Task) Timeout() time.Duration {
return t.options.Timeout.Duration
}
func (t *Task) LoadConfig() error {
config := DefaultConfig()
// parse static config
if t.options.Config != nil {
if err := t.options.Config.Unmarshal(&config); err != nil {
return fmt.Errorf("error parsing task config for %v: %w", TaskName, err)
}
}
// load dynamic vars
err := t.ctx.Vars.ConsumeVars(&config, t.options.ConfigVars)
if err != nil {
return err
}
// validate config
if err := config.Validate(); err != nil {
return err
}
t.config = config
return nil
}
func (t *Task) Execute(ctx context.Context) error {
checkCount := 0
for {
checkCount++
if done := t.processCheck(ctx, checkCount); done {
return nil
}
select {
case <-time.After(t.config.PollInterval.Duration):
case <-ctx.Done():
return ctx.Err()
}
}
}
func (t *Task) processCheck(ctx context.Context, checkCount int) bool {
allResultsPass := true
goodClients := []*ClientInfo{}
failedClients := []*ClientInfo{}
failedClientNames := []string{}
for _, client := range t.ctx.Scheduler.GetServices().ClientPool().GetClientsByNamePatterns(t.config.ClientPattern, "") {
var checkResult bool
checkLogger := t.logger.WithField("client", client.Config.Name)
syncStatus, err := client.ConsensusClient.GetRPCClient().GetNodeSyncStatus(ctx)
if ctx.Err() != nil {
return false
}
if err != nil {
checkLogger.Warnf("error fetching sync status: %v", err)
checkResult = false
} else {
checkResult = t.processClientCheck(client, syncStatus, checkLogger)
}
if !checkResult {
allResultsPass = false
failedClients = append(failedClients, t.getClientInfo(client, syncStatus))
failedClientNames = append(failedClientNames, client.Config.Name)
} else {
goodClients = append(goodClients, t.getClientInfo(client, syncStatus))
}
}
t.logger.Infof("Check result: %v, Failed Clients: %v", allResultsPass, failedClientNames)
if goodClientsData, err := vars.GeneralizeData(goodClients); err == nil {
t.ctx.Outputs.SetVar("goodClients", goodClientsData)
} else {
t.logger.Warnf("Failed setting `goodClients` output: %v", err)
}
if failedClientsData, err := vars.GeneralizeData(failedClients); err == nil {
t.ctx.Outputs.SetVar("failedClients", failedClientsData)
} else {
t.logger.Warnf("Failed setting `failedClients` output: %v", err)
}
if allResultsPass {
t.ctx.SetResult(types.TaskResultSuccess)
t.ctx.ReportProgress(100, fmt.Sprintf("All clients synced: %d", len(goodClients)))
return !t.config.ContinueOnPass
}
t.ctx.SetResult(types.TaskResultNone)
t.ctx.ReportProgress(0, fmt.Sprintf("Waiting for sync... %d/%d (attempt %d)", len(goodClients), len(goodClients)+len(failedClients), checkCount))
return false
}
func (t *Task) processClientCheck(client *clients.PoolClient, syncStatus *rpc.SyncStatus, checkLogger logrus.FieldLogger) bool {
clientIdx := client.ExecutionClient.GetIndex()
if t.firstHeight[clientIdx] == 0 {
t.firstHeight[clientIdx] = syncStatus.HeadSlot
}
if syncStatus.IsSyncing != t.config.ExpectSyncing {
checkLogger.Debugf("check failed. check: ExpectSyncing, expected: %v, got: %v", t.config.ExpectSyncing, syncStatus.IsSyncing)
return false
}
if syncStatus.IsOptimistic != t.config.ExpectOptimistic {
checkLogger.Debugf("check failed. check: ExpectOptimistic, expected: %v, got: %v", t.config.ExpectOptimistic, syncStatus.IsOptimistic)
return false
}
syncPercent := syncStatus.Percent()
if syncPercent < t.config.ExpectMinPercent {
checkLogger.Debugf("check failed. check: ExpectMinPercent, expected: >= %v, got: %v", t.config.ExpectMinPercent, syncPercent)
return false
}
if syncPercent > t.config.ExpectMaxPercent {
checkLogger.Debugf("check failed. check: ExpectMaxPercent, expected: <= %v, got: %v", t.config.ExpectMaxPercent, syncPercent)
return false
}
if int64(syncStatus.HeadSlot) < int64(t.config.MinSlotHeight) { //nolint:gosec // G115: slot values won't exceed int64 max
checkLogger.Debugf("check failed. check: MinSlotHeight, expected: >= %v, got: %v", t.config.MinSlotHeight, syncStatus.HeadSlot)
return false
}
if t.config.WaitForChainProgression && syncStatus.HeadSlot <= t.firstHeight[clientIdx] {
checkLogger.Debugf("check failed. check: WaitForChainProgression, expected block height: >= %v, got: %v", t.firstHeight[clientIdx], syncStatus.HeadSlot)
return false
}
return true
}
func (t *Task) getClientInfo(client *clients.PoolClient, syncStatus *rpc.SyncStatus) *ClientInfo {
clientInfo := &ClientInfo{
Name: client.Config.Name,
Synchronizing: syncStatus.IsSyncing,
Optimistic: syncStatus.IsOptimistic,
SyncHead: syncStatus.HeadSlot,
SyncDistance: syncStatus.SyncDistance,
}
return clientInfo
}