-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
main.go
331 lines (290 loc) · 8.3 KB
/
main.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Peter Mattis ([email protected])
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"math"
"math/rand"
"os"
"os/signal"
"sync"
"sync/atomic"
"syscall"
"time"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/cmd/internal/localcluster"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
)
var workers = flag.Int("w", 1, "number of workers; the i'th worker talks to node i%numNodes")
var numNodes = flag.Int("n", 4, "number of nodes")
var duration = flag.Duration("duration", math.MaxInt64, "how long to run the simulation for")
var blockSize = flag.Int("b", 1000, "block size")
var numLocalities = flag.Int("l", 0, "number of localities")
func newRand() *rand.Rand {
return rand.New(rand.NewSource(timeutil.Now().UnixNano()))
}
// allocSim is allows investigation of allocation/rebalancing heuristics. A
// pool of workers generates block_writer-style load where the i'th worker
// talks to node i%numNodes. Every second a monitor goroutine outputs status
// such as the per-node replica and leaseholder counts.
//
// TODO(peter): Allow configuration zone-config constraints.
type allocSim struct {
*localcluster.Cluster
stats struct {
ops uint64
errors uint64
}
ranges struct {
syncutil.Mutex
count int
replicas []int
leases []int
}
localities []roachpb.Locality
}
func newAllocSim(c *localcluster.Cluster) *allocSim {
return &allocSim{
Cluster: c,
}
}
func (a *allocSim) run(workers int) {
a.setup()
for i := 0; i < workers; i++ {
go a.worker(i, workers)
}
go a.rangeStats(time.Second)
a.monitor(time.Second)
}
func (a *allocSim) setup() {
db := a.DB[0]
if _, err := db.Exec("CREATE DATABASE IF NOT EXISTS allocsim"); err != nil {
log.Fatal(context.Background(), err)
}
blocks := `
CREATE TABLE IF NOT EXISTS blocks (
id INT NOT NULL,
num INT NOT NULL,
data BYTES NOT NULL,
PRIMARY KEY (id, num)
)
`
if _, err := db.Exec(blocks); err != nil {
log.Fatal(context.Background(), err)
}
}
func (a *allocSim) maybeLogError(err error) {
if localcluster.IsUnavailableError(err) {
return
}
log.Error(context.Background(), err)
atomic.AddUint64(&a.stats.errors, 1)
}
func (a *allocSim) worker(i, workers int) {
const insert = `INSERT INTO allocsim.blocks (id, num, data) VALUES ($1, $2, repeat('a', $3))`
r := newRand()
db := a.DB[i%len(a.DB)]
for num := i; true; num += workers {
if _, err := db.Exec(insert, r.Int63(), num, *blockSize); err != nil {
a.maybeLogError(err)
} else {
atomic.AddUint64(&a.stats.ops, 1)
}
}
}
func (a *allocSim) rangeInfo() (total int, replicas []int, leases []int) {
replicas = make([]int, len(a.Nodes))
leases = make([]int, len(a.Nodes))
// Retrieve the metrics for each node and extract the replica and leaseholder
// counts.
var wg sync.WaitGroup
wg.Add(len(a.Status))
for i := range a.Status {
go func(i int) {
defer wg.Done()
resp, err := a.Status[i].Metrics(context.Background(), &serverpb.MetricsRequest{
NodeId: fmt.Sprintf("%d", i+1),
})
if err != nil {
log.Fatal(context.Background(), err)
}
var metrics map[string]interface{}
if err := json.Unmarshal(resp.Data, &metrics); err != nil {
log.Fatal(context.Background(), err)
}
stores := metrics["stores"].(map[string]interface{})
for _, v := range stores {
storeMetrics := v.(map[string]interface{})
if v, ok := storeMetrics["replicas"]; ok {
replicas[i] += int(v.(float64))
}
if v, ok := storeMetrics["replicas.leaseholders"]; ok {
leases[i] += int(v.(float64))
}
}
}(i)
}
wg.Wait()
for _, v := range replicas {
total += v
}
return total, replicas, leases
}
func (a *allocSim) rangeStats(d time.Duration) {
for {
count, replicas, leases := a.rangeInfo()
a.ranges.Lock()
a.ranges.count = count
a.ranges.replicas = replicas
a.ranges.leases = leases
a.ranges.Unlock()
time.Sleep(d)
}
}
const padding = "__________"
func formatHeader(header string, numberNodes int, localities []roachpb.Locality) string {
var buf bytes.Buffer
_, _ = buf.WriteString(header)
for i := 1; i <= numberNodes; i++ {
var loc string
if localities != nil {
loc = fmt.Sprintf(":%s", localities[i-1])
}
node := fmt.Sprintf("%d%s", i, loc)
fmt.Fprintf(&buf, "%s%s", padding[:len(padding)-len(node)], node)
}
return buf.String()
}
func (a *allocSim) monitor(d time.Duration) {
formatNodes := func(replicas, leases []int) string {
var buf bytes.Buffer
for i := range replicas {
alive := a.Nodes[i].Alive()
if !alive {
_, _ = buf.WriteString("\033[0;31;49m")
}
fmt.Fprintf(&buf, "%*s", len(padding), fmt.Sprintf("%d/%d", replicas[i], leases[i]))
if !alive {
_, _ = buf.WriteString("\033[0m")
}
}
return buf.String()
}
start := timeutil.Now()
lastTime := start
var numReplicas int
var lastOps uint64
for ticks := 0; true; ticks++ {
time.Sleep(d)
now := timeutil.Now()
elapsed := now.Sub(lastTime).Seconds()
ops := atomic.LoadUint64(&a.stats.ops)
a.ranges.Lock()
ranges := a.ranges.count
replicas := a.ranges.replicas
leases := a.ranges.leases
a.ranges.Unlock()
if ticks%20 == 0 || numReplicas != len(replicas) {
numReplicas = len(replicas)
fmt.Println(formatHeader("_elapsed__ops/sec___errors_replicas", numReplicas, a.localities))
}
fmt.Printf("%8s %8.1f %8d %8d%s\n",
time.Duration(now.Sub(start).Seconds()+0.5)*time.Second,
float64(ops-lastOps)/elapsed, atomic.LoadUint64(&a.stats.errors),
ranges, formatNodes(replicas, leases))
lastTime = now
lastOps = ops
}
}
func (a *allocSim) finalStatus() {
a.ranges.Lock()
defer a.ranges.Unlock()
// TODO(bram): With the addition of localities, these stats will have to be
// updated.
fmt.Println(formatHeader("___stats___________________________", len(a.ranges.replicas), a.localities))
genStats := func(name string, counts []int) {
var total float64
for _, count := range counts {
total += float64(count)
}
mean := total / float64(len(counts))
var buf bytes.Buffer
fmt.Fprintf(&buf, "%8s (total%% / diff%%) ", name)
for _, count := range counts {
var percent, fromMean float64
if total != 0 {
percent = float64(count) / total * 100
fromMean = math.Abs((float64(count) - mean) / total * 100)
}
fmt.Fprintf(&buf, " %9.9s", fmt.Sprintf("%.0f/%.0f", percent, fromMean))
}
fmt.Println(buf.String())
}
genStats("replicas", a.ranges.replicas)
genStats("leases", a.ranges.leases)
}
func main() {
flag.Parse()
c := localcluster.New(*numNodes)
defer c.Close()
log.SetExitFunc(func(code int) {
c.Close()
os.Exit(code)
})
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
a := newAllocSim(c)
var localities [][]string
if *numLocalities > 0 {
a.localities = make([]roachpb.Locality, len(c.Nodes), len(c.Nodes))
// Add some localities to the cluster. The
for i := range c.Nodes {
locality := roachpb.Locality{
Tiers: []roachpb.Tier{
{
Key: "l",
Value: fmt.Sprintf("%d", i%*numLocalities),
},
},
}
localities = append(localities, []string{fmt.Sprintf("--locality=%s", locality)})
a.localities[i] = locality
}
}
go func() {
var exitStatus int
select {
case s := <-signalCh:
log.Infof(context.Background(), "signal received: %v", s)
exitStatus = 1
case <-time.After(*duration):
log.Infof(context.Background(), "finished run of: %s", *duration)
}
c.Close()
a.finalStatus()
os.Exit(exitStatus)
}()
c.Start("allocsim", *workers, []string{}, flag.Args(), localities)
c.UpdateZoneConfig(1, 1<<20)
a.run(*workers)
}