-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathrelay.go
More file actions
245 lines (207 loc) · 5.63 KB
/
relay.go
File metadata and controls
245 lines (207 loc) · 5.63 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package cmd
import (
"fmt"
"net/http"
"time"
"xbase"
"github.com/spf13/cobra"
)
func NewRelayCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "relay",
Short: "show/enable/disable relay worker",
}
cmd.AddCommand(NewRelayStatusCommand())
cmd.AddCommand(NewRelayInfosCommand())
cmd.AddCommand(NewRelayStartCommand())
cmd.AddCommand(NewRelayStopCommand())
cmd.AddCommand(NewRelayParallelTypeCommand())
cmd.AddCommand(NewRelayResetCommand())
cmd.AddCommand(NewRelayResetToNowCommand())
cmd.AddCommand(NewRelayMaxWorkersCommand())
cmd.AddCommand(NewRelayNowCommand())
return cmd
}
func NewRelayStatusCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Short: "show relay status",
Run: relayStatusCommand,
}
return cmd
}
func relayStatusCommand(cmd *cobra.Command, args []string) {
relayUrl := "http://127.0.0.1:8080/v1/relay/status"
resp, err := xbase.HTTPGet(relayUrl)
if err != nil {
log.Panicf("error:%+v", err)
}
fmt.Printf(resp)
}
func NewRelayInfosCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "infos",
Short: "show relay all infos",
Run: relayInfosCommand,
}
return cmd
}
func relayInfosCommand(cmd *cobra.Command, args []string) {
relayUrl := "http://127.0.0.1:8080/v1/relay/infos"
resp, err := xbase.HTTPGet(relayUrl)
if err != nil {
log.Panicf("error:%+v", err)
}
fmt.Printf(resp)
}
func setRelay(url string) {
resp, cleanup, err := xbase.HTTPPut(url, nil)
defer cleanup()
if err != nil {
log.Panicf("error:%+v", err)
}
if resp == nil || resp.StatusCode != http.StatusOK {
log.Panicf("radoncli.set.relay.url[%s].response.error:%+s", url, xbase.HTTPReadBody(resp))
}
}
func NewRelayStartCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: "start the relay worker",
Run: relayStartCommand,
}
return cmd
}
func relayStartCommand(cmd *cobra.Command, args []string) {
relayUrl := "http://127.0.0.1:8080/v1/relay/start"
setRelay(relayUrl)
}
func NewRelayStopCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "stop",
Short: "stop the relay worker",
Run: relayStopCommand,
}
return cmd
}
func relayStopCommand(cmd *cobra.Command, args []string) {
relayUrl := "http://127.0.0.1:8080/v1/relay/stop"
setRelay(relayUrl)
}
func setParallelType(url string, t int32) {
type request struct {
Type int32 `json:"type"`
}
req := &request{
Type: t,
}
resp, cleanup, err := xbase.HTTPPut(url, &req)
defer cleanup()
if err != nil {
log.Panicf("error:%+v", err)
}
if resp == nil || resp.StatusCode != http.StatusOK {
log.Panicf("radoncli.set.parallel.type.to.[%v].url[%s].response.error:%+s", t, url, xbase.HTTPReadBody(resp))
}
}
func NewRelayParallelTypeCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "paralleltype",
Short: "parallel type, 0:turn off parallel relay, 1:same events type can parallel(default), 2:all events type can parallel",
Run: relayParallelTypeCommand,
}
cmd.Flags().IntVar(&localFlags.parallelType, "type", 1, "")
return cmd
}
func relayParallelTypeCommand(cmd *cobra.Command, args []string) {
url := "http://127.0.0.1:8080/v1/relay/paralleltype"
setParallelType(url, int32(localFlags.parallelType))
}
func relayResetGTID(gtid int64) {
if gtid < 1514254947594569594 {
log.Panicf("gtid[%v].less.than[1514254947594569594].should.be.UTC().UnixNano()", gtid)
}
relayUrl := "http://127.0.0.1:8080/v1/relay/reset"
type request struct {
GTID int64 `json:"gtid"`
}
req := &request{
GTID: gtid,
}
resp, cleanup, err := xbase.HTTPPost(relayUrl, &req)
defer cleanup()
if err != nil {
log.Panicf("error:%+v", err)
}
if resp == nil || resp.StatusCode != http.StatusOK {
log.Panicf("radoncli.set.relay.to.[%v].url[%s].response.error:%+s", req, relayUrl, xbase.HTTPReadBody(resp))
}
log.Info("reset.relay.gtid.to[%v]", gtid)
}
func NewRelayResetCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "reset",
Short: "reset the relay worker GTID",
Run: relayResetCommand,
}
cmd.Flags().Int64Var(&localFlags.gtid, "gtid", 0, "--gtid=[timestamp(UTC().UnixNano())]")
return cmd
}
func relayResetCommand(cmd *cobra.Command, args []string) {
relayResetGTID(localFlags.gtid)
}
func NewRelayResetToNowCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "resettonow",
Short: "reset the relay worker GTID to time.NOW().UTC().UnixNano()",
Run: relayResetToNowCommand,
}
return cmd
}
func relayResetToNowCommand(cmd *cobra.Command, args []string) {
relayResetGTID(time.Now().UTC().UnixNano())
}
func NewRelayMaxWorkersCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "workers",
Short: "Set the max relay parallel workers",
Run: relayMaxWorkersCommand,
}
cmd.Flags().IntVar(&localFlags.maxWorkers, "max", 0, "--max=[1, 1024]")
return cmd
}
func relayMaxWorkersCommand(cmd *cobra.Command, args []string) {
relayUrl := "http://127.0.0.1:8080/v1/relay/workers"
type request struct {
Workers int `json:"workers"`
}
req := &request{
Workers: localFlags.maxWorkers,
}
resp, cleanup, err := xbase.HTTPPost(relayUrl, &req)
defer cleanup()
if err != nil {
log.Panicf("error:%+v", err)
}
if resp == nil || resp.StatusCode != http.StatusOK {
log.Panicf("radoncli.relay.set.max.parallel.worker.to.[%v].url[%s].response.error:%+s", req, relayUrl, xbase.HTTPReadBody(resp))
}
}
func NewRelayNowCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "now",
Short: "returns the Now().UTC().UnixNano()",
Run: relayNowCommand,
}
return cmd
}
func relayNowCommand(cmd *cobra.Command, args []string) {
log.Info("Now().UTC().UnixNano():%v", time.Now().UTC().UnixNano())
}