-
Notifications
You must be signed in to change notification settings - Fork 9
/
agent.go
226 lines (194 loc) · 5.29 KB
/
agent.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
package main
import (
"fmt"
"sync"
"time"
"go.uber.org/zap"
"go.etcd.io/etcd/api/v3/etcdserverpb"
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
"go.etcd.io/etcd/client/pkg/v3/logutil"
clientv3 "go.etcd.io/etcd/client/v3"
)
func memberList(gcfg globalConfig) (*clientv3.MemberListResponse, error) {
cfgSpec := clientConfigWithoutEndpoints(gcfg)
eps, err := endpointsFromCmd(gcfg)
if err != nil {
return nil, err
}
cfgSpec.Endpoints = eps
c, err := createClient(cfgSpec)
if err != nil {
return nil, err
}
ctx, cancel := commandCtx(gcfg.commandTimeout)
defer func() {
c.Close()
cancel()
}()
members, err := c.MemberList(ctx)
if err != nil {
return nil, err
}
return members, nil
}
type epHealth struct {
Ep string `json:"endpoint"`
Health bool `json:"health"`
Took string `json:"took"`
Error string `json:"error,omitempty"`
}
func (eh epHealth) String() string {
return fmt.Sprintf("endpoint: %s, health: %t, took: %s, error: %s", eh.Ep, eh.Health, eh.Took, eh.Error)
}
func clusterHealth(gcfg globalConfig) ([]epHealth, error) {
eps, err := endpointsFromCluster(gcfg)
if err != nil {
return nil, err
}
lg, err := logutil.CreateDefaultZapLogger(zap.InfoLevel)
if err != nil {
return nil, err
}
cfgSpec := clientConfigWithoutEndpoints(gcfg)
var cfgs []*clientv3.Config
for _, ep := range eps {
cfg, err := clientv3.NewClientConfig(&clientv3.ConfigSpec{
Endpoints: []string{ep},
DialTimeout: cfgSpec.DialTimeout,
KeepAliveTime: cfgSpec.KeepAliveTime,
KeepAliveTimeout: cfgSpec.KeepAliveTimeout,
Secure: cfgSpec.Secure,
Auth: cfgSpec.Auth,
}, lg)
if err != nil {
return nil, err
}
cfgs = append(cfgs, cfg)
}
healthCh := make(chan epHealth, len(eps))
var wg sync.WaitGroup
for _, cfg := range cfgs {
wg.Add(1)
go func(cfg *clientv3.Config) {
defer wg.Done()
ep := cfg.Endpoints[0]
cfg.Logger = lg.Named("client")
cli, err := clientv3.New(*cfg)
if err != nil {
healthCh <- epHealth{Ep: ep, Health: false, Error: err.Error()}
return
}
startTs := time.Now()
// get a random key. As long as we can get the response
// without an error, the endpoint is health.
ctx, cancel := commandCtx(gcfg.commandTimeout)
_, err = cli.Get(ctx, "health")
eh := epHealth{Ep: ep, Health: false, Took: time.Since(startTs).String()}
if err == nil || err == rpctypes.ErrPermissionDenied {
eh.Health = true
} else {
eh.Error = err.Error()
}
if eh.Health {
resp, err := cli.AlarmList(ctx)
if err == nil && len(resp.Alarms) > 0 {
eh.Health = false
eh.Error = "Active Alarm(s): "
for _, v := range resp.Alarms {
switch v.Alarm {
case etcdserverpb.AlarmType_NOSPACE:
// We ignore AlarmType_NOSPACE, and we need to
// continue to perform defragmentation.
eh.Health = true
eh.Error = eh.Error + "NOSPACE "
case etcdserverpb.AlarmType_CORRUPT:
eh.Error = eh.Error + "CORRUPT "
default:
eh.Error = eh.Error + "UNKNOWN "
}
}
} else if err != nil {
eh.Health = false
eh.Error = "Unable to fetch the alarm list"
}
}
cancel()
healthCh <- eh
}(cfg)
}
wg.Wait()
close(healthCh)
var healthList []epHealth
for h := range healthCh {
healthList = append(healthList, h)
}
return healthList, nil
}
type epStatus struct {
Ep string `json:"Endpoint"`
Resp *clientv3.StatusResponse `json:"Status"`
}
func (es epStatus) String() string {
return fmt.Sprintf("endpoint: %s, dbSize: %d, dbSizeInUse: %d, memberId: %x, leader: %x, revision: %d, term: %d, index: %d",
es.Ep, es.Resp.DbSize, es.Resp.DbSizeInUse, es.Resp.Header.MemberId, es.Resp.Leader, es.Resp.Header.Revision, es.Resp.RaftTerm, es.Resp.RaftIndex)
}
func membersStatus(gcfg globalConfig) ([]epStatus, error) {
eps, err := endpoints(gcfg)
if err != nil {
return nil, err
}
var statusList []epStatus
for _, ep := range eps {
status, err := memberStatus(gcfg, ep)
if err != nil {
return nil, fmt.Errorf("failed to get member(%q) status: %w", ep, err)
}
statusList = append(statusList, status)
}
return statusList, nil
}
func memberStatus(gcfg globalConfig, ep string) (epStatus, error) {
cfgSpec := clientConfigWithoutEndpoints(gcfg)
cfgSpec.Endpoints = []string{ep}
c, err := createClient(cfgSpec)
if err != nil {
return epStatus{}, fmt.Errorf("failed to createClient: %w", err)
}
ctx, cancel := commandCtx(gcfg.commandTimeout)
defer func() {
c.Close()
cancel()
}()
resp, err := c.Status(ctx, ep)
return epStatus{Ep: ep, Resp: resp}, err
}
func compact(gcfg globalConfig, rev int64, ep string) error {
cfgSpec := clientConfigWithoutEndpoints(gcfg)
cfgSpec.Endpoints = []string{ep}
c, err := createClient(cfgSpec)
if err != nil {
return err
}
ctx, cancel := commandCtx(gcfg.commandTimeout)
defer func() {
c.Close()
cancel()
}()
_, err = c.Compact(ctx, rev, []clientv3.CompactOption{clientv3.WithCompactPhysical()}...)
return err
}
func defragment(gcfg globalConfig, ep string) error {
cfgSpec := clientConfigWithoutEndpoints(gcfg)
cfgSpec.Endpoints = []string{ep}
c, err := createClient(cfgSpec)
if err != nil {
return err
}
ctx, cancel := commandCtx(gcfg.commandTimeout)
defer func() {
c.Close()
cancel()
}()
_, err = c.Defragment(ctx, ep)
return err
}