forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcd.go
210 lines (180 loc) · 5.3 KB
/
etcd.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
package host
import (
"context"
"fmt"
"os"
"sort"
"strings"
"sync"
"text/tabwriter"
"time"
"github.com/coreos/etcd/clientv3"
"bytes"
"github.com/openshift/origin/pkg/cmd/server/etcd"
"github.com/openshift/origin/pkg/oc/admin/diagnostics/diagnostics/log"
"github.com/openshift/origin/pkg/oc/admin/diagnostics/diagnostics/types"
)
// EtcdWriteVolume is a Diagnostic to check the writes occurring against etcd
// and organize them by volume.
type EtcdWriteVolume struct {
MasterConfigLocation string
V3Client *clientv3.Client
durationSpec string
duration time.Duration
}
const (
EtcdWriteName = "EtcdWriteVolume"
DurationParam = "duration"
DurationDefault = "1m"
)
func (d *EtcdWriteVolume) Name() string {
return EtcdWriteName
}
func (d *EtcdWriteVolume) Description() string {
return fmt.Sprintf("Check the volume of writes against etcd over a time period and classify them by operation and key")
}
func (d *EtcdWriteVolume) Requirements() (client bool, host bool) {
return false, true
}
func (d *EtcdWriteVolume) AvailableParameters() []types.Parameter {
return []types.Parameter{
{DurationParam, "How long to perform the write test", &d.durationSpec, DurationDefault},
}
}
func (d *EtcdWriteVolume) Complete(logger *log.Logger) error {
v3Client, found, err := findEtcdClients(d.MasterConfigLocation, logger)
if err != nil || !found {
return err
}
d.V3Client = v3Client
// determine the duration to run the check from either the deprecated env var, the flag, or the default
s := os.Getenv("ETCD_WRITE_VOLUME_DURATION") // deprecated way
if d.durationSpec != "" {
s = d.durationSpec
}
if s == "" {
s = DurationDefault
}
duration, err := time.ParseDuration(s)
if err != nil {
return fmt.Errorf("EtcdWriteVolume duration '%s' could not be parsed: %v", s, err)
}
d.duration = duration
return nil
}
func (d *EtcdWriteVolume) CanRun() (bool, error) {
if d.V3Client == nil {
return false, fmt.Errorf("must have a V3 etcd client")
}
return true, nil
}
func (d *EtcdWriteVolume) Check() types.DiagnosticResult {
r := types.NewDiagnosticResult(EtcdWriteName)
ctx := context.Background()
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(d.duration))
defer cancel()
keyStats := &keyCounter{}
stats := &lockedKeyCounter{KeyCounter: keyStats}
ch := d.V3Client.Watch(ctx, "/", clientv3.WithKeysOnly(), clientv3.WithPrefix())
for resource := range ch {
for _, evt := range resource.Events {
if evt.Kv == nil {
continue
}
action := fmt.Sprintf("v3:%s", evt.Type)
stats.Inc(strings.Split(action+"/"+strings.TrimPrefix(string(evt.Kv.Key), "/"), "/"))
}
}
bins := keyStats.Bins("", "/")
sort.Sort(DescendingBins(bins))
buf := &bytes.Buffer{}
tw := tabwriter.NewWriter(buf, 0, 0, 1, ' ', 0)
fmt.Fprintf(tw, "/\t%6d\t100.0%%\n", keyStats.count)
for _, b := range bins {
fmt.Fprintf(tw, "%s\t%6d\t%5.1f%%\n", b.Name, b.Count, float64(b.Count)/float64(keyStats.count)*100)
}
tw.Flush()
r.Info("DEw2004", fmt.Sprintf("Measured %.1f writes/sec\n", float64(keyStats.count)/float64(d.duration/time.Second))+buf.String())
return r
}
// findEtcdClients finds and loads etcd clients
func findEtcdClients(configFile string, logger *log.Logger) (*clientv3.Client, bool, error) {
masterConfig, err := GetMasterConfig(configFile, logger)
if err != nil {
configErr := fmt.Errorf("Unreadable master config; skipping this diagnostic.")
logger.Error("DE2001", configErr.Error())
return nil, false, configErr
}
if len(masterConfig.EtcdClientInfo.URLs) == 0 {
configErr := fmt.Errorf("No etcdClientInfo.urls defined; can't contact etcd")
logger.Error("DE2002", configErr.Error())
return nil, false, configErr
}
config, err := etcd.MakeEtcdClientV3Config(masterConfig.EtcdClientInfo)
if err != nil {
configErr := fmt.Errorf("Unable to create an etcd v3 client config: %v", err)
logger.Error("DE2004", configErr.Error())
return nil, false, configErr
}
config.DialTimeout = 5 * time.Second
v3Client, err := clientv3.New(*config)
if err != nil {
configErr := fmt.Errorf("Unable to create an etcd v3 client: %v", err)
logger.Error("DE2005", configErr.Error())
return nil, false, configErr
}
return v3Client, true, nil
}
type KeyCounter interface {
Inc(key []string)
}
type lockedKeyCounter struct {
lock sync.Mutex
KeyCounter
}
func (c *lockedKeyCounter) Inc(key []string) {
c.lock.Lock()
defer c.lock.Unlock()
c.KeyCounter.Inc(key)
}
type keyCounter struct {
count int
children map[string]*keyCounter
}
func (b *keyCounter) Inc(key []string) {
b.count++
if len(key) == 0 {
return
}
if b.children == nil {
b.children = make(map[string]*keyCounter)
}
child, ok := b.children[key[0]]
if !ok {
child = &keyCounter{}
b.children[key[0]] = child
}
child.Inc(key[1:])
}
type Bin struct {
Name string
Count int
}
func (b *keyCounter) Bins(parent, separator string) []Bin {
var bins []Bin
for k, v := range b.children {
childKey := parent + separator + k
bins = append(bins, Bin{Name: childKey, Count: v.count})
bins = append(bins, v.Bins(childKey, separator)...)
}
return bins
}
type DescendingBins []Bin
func (m DescendingBins) Len() int { return len(m) }
func (m DescendingBins) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m DescendingBins) Less(i, j int) bool {
if m[i].Name < m[j].Name {
return true
}
return false
}