forked from solana-labs/solana-ping-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workers.go
308 lines (285 loc) · 10.9 KB
/
workers.go
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"sort"
"strings"
"time"
"github.com/blocto/solana-go-sdk/client"
"github.com/blocto/solana-go-sdk/types"
)
type PingType string
const DefaultAlertThredHold = 20
const DualModeNoFeeTriggerName = "no-fee-dualmode"
const (
DataPointReport PingType = "report"
DataPoint1Min PingType = "datapoint1min"
)
func launchWorkers(c ClustersToRun) {
// Run Ping Service
runCluster := func(clusterConf ClusterConfig) {
if !clusterConf.PingServiceEnabled {
log.Println("==> go pingDataWorker", clusterConf.Cluster, " PingServiceEnabled ", clusterConf.PingServiceEnabled)
} else {
for i := 0; i < clusterConf.PingConfig.NumWorkers; i++ {
log.Println("==> go pingDataWorker", clusterConf.Cluster, " n:", clusterConf.PingConfig.NumWorkers, "i:", i)
go pingDataWorker(clusterConf, i)
time.Sleep(2 * time.Second)
}
}
if clusterConf.Report.Enabled {
go reportWorker(clusterConf)
}
}
// Single Cluster or all Cluster
switch c {
case RunMainnetBeta:
runCluster(config.Mainnet)
case RunTestnet:
runCluster(config.Testnet)
case RunDevnet:
runCluster(config.Devnet)
case RunAllClusters:
runCluster(config.Mainnet)
runCluster(config.Testnet)
runCluster(config.Devnet)
default:
panic(ErrInvalidCluster)
}
// Run Retension Service
if config.Retension.Enabled {
time.Sleep(2 * time.Second)
go retensionServiceWorker()
}
}
func pingDataWorker(cConf ClusterConfig, workerNum int) {
log.Println(">> Solana DataPoint1MinWorker for ", cConf.Cluster, " worker:", workerNum, " start!")
defer log.Println(">> Solana DataPoint1MinWorker for ", cConf.Cluster, " worker:", workerNum, " end!")
var failover RPCFailover
var c *client.Client
var acct types.Account
switch cConf.Cluster {
case MainnetBeta:
failover = mainnetFailover
clusterAcct, err := getConfigKeyPair(config.ClusterCLIConfig.ConfigMain)
if err != nil {
log.Panic("getConfigKeyPair Error")
}
acct = clusterAcct
case Testnet:
failover = testnetFailover
clusterAcct, err := getConfigKeyPair(config.ClusterCLIConfig.ConfigTestnet)
if err != nil {
log.Panic("Testnet getConfigKeyPair Error")
}
acct = clusterAcct
case Devnet:
failover = devnetFailover
clusterAcct, err := getConfigKeyPair(config.ClusterCLIConfig.ConfigDevnet)
if err != nil {
log.Panic("Devnet getConfigKeyPair Error")
}
acct = clusterAcct
default:
panic(ErrInvalidCluster)
}
pingWithFee := true
for {
c = failover.GoNext(c, cConf, workerNum)
result, err := Ping(c, DataPoint1Min, acct, cConf, pingWithFee)
extraTimeStart := time.Now().UTC().Unix()
if cConf.PingConfig.ComputeFeeDualMode {
if !pingWithFee {
result.ComputeUnitPrice = 0
result.RequestComputeUnits = 0
}
}
addRecord(result)
if influxdb != nil && influxdb.Client != nil {
influxdb.SendDatapointAsync(influxdb.PrepareInfluxdbData(result))
}
failover.GetEndpoint().RetryResult(err)
extraTimeStop := time.Now().UTC().Unix()
waitTime := cConf.ClusterPing.PingConfig.MinPerPingTime - (result.TakeTime / 1000) - (extraTimeStop - extraTimeStart)
if waitTime > 0 {
time.Sleep(time.Duration(waitTime) * time.Second)
}
if cConf.PingConfig.ComputeFeeDualMode {
pingWithFee = !pingWithFee
}
}
}
func retensionServiceWorker() {
log.Println(">> Retension Service Worker start!")
defer log.Println(">> Retension Service Worker end!")
for {
now := time.Now().UTC().Unix()
if config.Retension.KeepHours < 6 {
config.Retension.KeepHours = 6
}
timeB4 := now - (config.Retension.KeepHours * 60 * 60)
deleteTimeBefore(timeB4)
if config.Retension.UpdateIntervalSec < 300 {
config.Retension.UpdateIntervalSec = 300
}
time.Sleep(time.Duration(config.Retension.UpdateIntervalSec) * time.Second)
}
}
func getConfigKeyPair(c SolanaCLIConfig) (types.Account, error) {
body, err := ioutil.ReadFile(c.KeypairPath)
if err != nil {
return types.Account{}, ErrKeyPairFile
}
key := []byte{}
err = json.Unmarshal(body, &key)
if err != nil {
return types.Account{}, err
}
acct, err := types.AccountFromBytes(key)
if err != nil {
return types.Account{}, err
}
return acct, nil
}
func reportWorker(cConf ClusterConfig) {
log.Println(">> Report Worker for ", cConf.Cluster, " start!")
defer log.Println(">> Report Worker for ", cConf.Cluster, " end!")
var lastReporTime int64
trigger := NewAlertTrigger(cConf)
var triggerNoFee AlertTrigger // TriggerNoFee is used only when ComputeFeeDualMode is on
if cConf.PingConfig.ComputeUnitPrice > 0 && cConf.PingConfig.ComputeFeeDualMode {
triggerNoFee = NewAlertTriggerByParams(DualModeNoFeeTriggerName, cConf.Report.LevelFilePath+".nofee", cConf.LossThreshold)
}
for {
now := time.Now().UTC().Unix()
if lastReporTime == 0 { // server restart will cause lasterReportTime zero
lastReporTime = now - int64(cConf.Report.Interval)
log.Println("reconstruct lastReport time=", lastReporTime, "time now=", time.Now().UTC().Unix())
}
sendReportAlert := func(slackReportEnabled bool, slackAlertEnabled bool,
discordReportEnabled bool, discordAlertEnabled bool,
groupStatistic *GroupsAllStatistic, globalStatistic GlobalStatistic,
toSendAlert bool, alertTrigger AlertTrigger, messageMemo string) {
var accessToken string
switch cConf.Cluster {
case MainnetBeta:
accessToken = mainnetFailover.GetEndpoint().AccessToken
case Testnet:
accessToken = testnetFailover.GetEndpoint().AccessToken
case Devnet:
accessToken = devnetFailover.GetEndpoint().AccessToken
default:
panic(fmt.Sprintf("%s:%s", "no such cluster", cConf.Cluster))
}
if slackReportEnabled {
slackReportSend(cConf, groupStatistic, &globalStatistic, []string{accessToken}, messageMemo)
}
if slackAlertEnabled && toSendAlert {
slackAlertSend(cConf, &globalStatistic, groupStatistic.GlobalErrorStatistic,
alertTrigger.ThresholdLevels[alertTrigger.ThresholdIndex], []string{accessToken}, messageMemo)
}
if discordReportEnabled {
discordReportSend(cConf, groupStatistic, &globalStatistic, []string{accessToken}, messageMemo)
}
if discordAlertEnabled && toSendAlert {
discordAlertSend(cConf, &globalStatistic, groupStatistic.GlobalErrorStatistic,
alertTrigger.ThresholdLevels[alertTrigger.ThresholdIndex], []string{accessToken}, messageMemo)
}
}
getDataFromComputeFee := AllData
if cConf.PingConfig.ComputeUnitPrice > 0 && cConf.PingConfig.RequestUnits > 0 {
getDataFromComputeFee = HasComputeUnitPrice
}
data := getAfter(cConf.Cluster, DataPoint1Min, lastReporTime, getDataFromComputeFee, 0)
if len(data) <= 0 { // No Data
log.Println(cConf.Cluster, " getAfter return empty")
time.Sleep(30 * time.Second)
continue
}
groupsStat, globalStat := getGlobalStatistis(cConf, data, lastReporTime, now)
trigger.Update(globalStat.Loss)
// ShouldAlertSend execute once only. TODO: make shouldAlertSend a function which does not modify any value
alertSend := trigger.ShouldAlertSend()
messageMemo := ""
if cConf.PingConfig.ComputeUnitPrice > 0 {
// count txs by fee
m := map[uint64]uint64{}
for _, v := range data {
m[v.ComputeUnitPrice]++
}
// sort keys for pretty output
keys := make([]uint64, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
// prepare output
outputs := make([]string, 0, len(keys))
for _, computeUnitPrice := range keys {
txCount := m[computeUnitPrice]
outputs = append(outputs, fmt.Sprintf("%vtx@%vml", txCount, computeUnitPrice))
}
messageMemo = fmt.Sprintf("with-fee (max: 10^8 ml), ({count}tx@{compute_unit_price}ml: %v)", strings.Join(outputs, ","))
} else {
messageMemo = "no-fee"
}
sendReportAlert(cConf.Report.Slack.Report.Enabled, cConf.Report.Slack.Alert.Enabled,
cConf.Report.Discord.Report.Enabled, cConf.Report.Discord.Alert.Enabled,
groupsStat, globalStat, alertSend, trigger, messageMemo)
// ComputeFeeDualMode for no-fee alert
if cConf.PingConfig.ComputeUnitPrice > 0 && cConf.PingConfig.RequestUnits > 0 && cConf.PingConfig.ComputeFeeDualMode {
dataNoFee := getAfter(cConf.Cluster, DataPoint1Min, lastReporTime, NoComputeUnitPrice, 0)
if len(data) <= 0 { // No Data
log.Println(cConf.Cluster, "ComputeFeeDualMode noComputeUnitPrice getAfter return empty")
} else {
groupsStatNoFee, globalStatNoFee := getGlobalStatistis(cConf, dataNoFee, lastReporTime, now)
triggerNoFee.Update(globalStatNoFee.Loss)
alertSendNoFee := triggerNoFee.ShouldAlertSend()
sendReportAlert(cConf.Report.Slack.Report.Enabled, cConf.Report.Slack.Alert.Enabled,
cConf.Report.Discord.Report.Enabled, cConf.Report.Discord.Alert.Enabled,
groupsStatNoFee, globalStatNoFee, alertSendNoFee, triggerNoFee, "no-fee (dual-mode)")
}
}
lastReporTime = now
time.Sleep(time.Duration(cConf.Report.Interval) * time.Second)
}
}
func slackReportSend(cConf ClusterConfig, groupsStat *GroupsAllStatistic, globalStat *GlobalStatistic, hideKeywords []string, memo string) {
payload := SlackPayload{}
payload.ReportPayload(cConf.Cluster, groupsStat, *globalStat, hideKeywords, memo)
err := SlackSend(cConf.Report.Slack.Report.Webhook, &payload)
if err != nil {
log.Println("slackReportSend Error:", err)
}
}
func slackAlertSend(conf ClusterConfig, globalStat *GlobalStatistic, globalErrorStatistic map[string]int, threadhold float64, hideKeywords []string, messageMemo string) {
payload := SlackPayload{}
payload.AlertPayload(conf, globalStat, globalErrorStatistic, threadhold, hideKeywords, messageMemo)
err := SlackSend(conf.Report.Slack.Alert.Webhook, &payload)
if err != nil {
log.Println("slackAlertSend Error:", err)
}
}
func discordReportSend(cConf ClusterConfig, groupsStat *GroupsAllStatistic, globalStat *GlobalStatistic, hideKeywords []string, messageMemo string) {
payload := DiscordPayload{BotAvatarURL: cConf.Report.Discord.BotAvatarURL, BotName: cConf.Report.Discord.BotName}
payload.ReportPayload(cConf.Cluster, groupsStat, *globalStat, hideKeywords, messageMemo)
err := DiscordSend(cConf.Report.Discord.Report.Webhook, &payload)
if err != nil {
log.Println("discordReportSend Error:", err)
}
}
func discordAlertSend(cConf ClusterConfig, globalStat *GlobalStatistic, globalErrorStatistic map[string]int, threadhold float64, hideKeywords []string, messageMemo string) {
payload := DiscordPayload{BotAvatarURL: cConf.Report.Discord.BotAvatarURL, BotName: cConf.Report.Discord.BotName}
payload.AlertPayload(cConf, globalStat, globalErrorStatistic, threadhold, hideKeywords, messageMemo)
err := DiscordSend(cConf.Report.Discord.Alert.Webhook, &payload)
if err != nil {
log.Println("discordAlertSend Error:", err)
}
}
func getGlobalStatistis(cConf ClusterConfig, resutls []PingResult, lastReportTime int64, currentTime int64) (*GroupsAllStatistic, GlobalStatistic) {
groups := grouping1Min(resutls, lastReportTime, currentTime)
groupsStat := statisticCompute(cConf, groups)
return groupsStat, groupsStat.GetGroupsAllStatistic(false) // get raw data
}