forked from influxdata/influxdb-comparisons
/
main.go
226 lines (191 loc) · 6.19 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
// query_benchmarker_cassandra speed tests Cassandra servers using request
// data from stdin.
//
// It reads encoded HLQuery objects from stdin, and makes concurrent requests
// to the provided Cassandra cluster. This program is a 'heavy client', i.e.
// it builds a client-side index of table metadata before beginning the
// benchmarking.
//
// TODO(rw): On my machine, this only decodes 700k/sec messages from stdin.
package main
import (
"encoding/gob"
"flag"
"fmt"
"github.com/gocql/gocql"
"github.com/influxdata/influxdb-comparisons/bulk_query"
"github.com/influxdata/influxdb-comparisons/util/report"
"io"
"log"
"strconv"
"sync"
"time"
)
const (
BlessedKeyspace string = "measurements"
)
type CassandraQueryBenchmarker struct {
daemonUrl string
aggrPlanLabel string
subQueryParallelism int
requestTimeout time.Duration
csiTimeout time.Duration
//util vars
queryPool sync.Pool
hlQueryChan chan *HLQuery
statPool sync.Pool
statChan chan *bulk_query.Stat
workersGroup sync.WaitGroup
statGroup sync.WaitGroup
aggrPlan int
reportTags [][2]string
reportHostname string
reportQueryStat bulk_query.StatGroup
session *gocql.Session
queryExecutor *HLQueryExecutor
scanFinished bool
}
// Helpers for choice-like flags:
var (
aggrPlanChoices = map[string]int{
"server": AggrPlanTypeWithServerAggregation,
"client": AggrPlanTypeWithoutServerAggregation,
}
)
var querier = &CassandraQueryBenchmarker{}
// Parse args:
func init() {
bulk_query.Benchmarker.Init()
querier.Init()
flag.Parse()
bulk_query.Benchmarker.Validate()
querier.Validate()
}
func (b *CassandraQueryBenchmarker) Init() {
flag.StringVar(&b.daemonUrl, "url", "localhost:9042", "Cassandra URL.")
flag.StringVar(&b.aggrPlanLabel, "aggregation-plan", "", "Aggregation plan (choices: server, client)")
flag.IntVar(&b.subQueryParallelism, "subquery-workers", 1, "Number of concurrent subqueries to make (because the client does a scatter+gather operation).")
flag.DurationVar(&b.requestTimeout, "request-timeout", 60*time.Second, "Maximum request timeout.")
flag.DurationVar(&b.csiTimeout, "client-side-index-timeout", 10*time.Second, "Maximum client-side index timeout (only used at initialization).")
}
func (b *CassandraQueryBenchmarker) Validate() {
if _, ok := aggrPlanChoices[b.aggrPlanLabel]; !ok {
log.Fatal("invalid aggregation plan")
}
b.aggrPlan = aggrPlanChoices[b.aggrPlanLabel]
}
func (b *CassandraQueryBenchmarker) Prepare() {
// Make pools to minimize heap usage:
b.queryPool = sync.Pool{
New: func() interface{} {
return &HLQuery{
HumanLabel: make([]byte, 0, 1024),
HumanDescription: make([]byte, 0, 1024),
MeasurementName: make([]byte, 0, 1024),
FieldName: make([]byte, 0, 1024),
AggregationType: make([]byte, 0, 1024),
TagsCondition: make([]byte, 0, 1024),
}
},
}
b.session = NewCassandraSession(b.daemonUrl, b.requestTimeout)
b.hlQueryChan = make(chan *HLQuery)
b.queryExecutor = NewHLQueryExecutor(b.session, bulk_query.Benchmarker.Debug())
}
func (b *CassandraQueryBenchmarker) GetProcessor() bulk_query.Processor {
return b
}
func (b *CassandraQueryBenchmarker) GetScanner() bulk_query.Scanner {
return b
}
func (b *CassandraQueryBenchmarker) CleanUp() {
close(b.hlQueryChan)
b.session.Close()
}
func (b *CassandraQueryBenchmarker) PrepareProcess(i int) {
}
func (b *CassandraQueryBenchmarker) IsScanFinished() bool {
return b.scanFinished
}
func (b *CassandraQueryBenchmarker) UpdateReport(params *report.QueryReportParams, reportTags [][2]string, extraVals []report.ExtraVal) (updatedTags [][2]string, updatedExtraVals []report.ExtraVal) {
updatedTags = append(reportTags, [2]string{"aggregation_plan", b.aggrPlanLabel})
updatedTags = append(updatedTags, [2]string{"subquery_workers", strconv.Itoa(b.subQueryParallelism)})
updatedTags = append(updatedTags, [2]string{"request_timeout", strconv.Itoa(int(b.requestTimeout.Seconds()))})
updatedTags = append(updatedTags, [2]string{"client_side_index_timeout", strconv.Itoa(int(b.csiTimeout.Seconds()))})
params.DBType = "Cassandra"
params.DestinationUrl = b.daemonUrl
updatedExtraVals = extraVals
return
}
func main() {
bulk_query.Benchmarker.RunBenchmark(querier)
}
// scan reads encoded Queries and places them onto the workqueue.
func (b *CassandraQueryBenchmarker) RunScan(r io.Reader, closeChan chan int) {
dec := gob.NewDecoder(r)
n := int64(0)
loop:
for {
if bulk_query.Benchmarker.Limit() >= 0 && n >= bulk_query.Benchmarker.Limit() {
break
}
q := b.queryPool.Get().(*HLQuery)
err := dec.Decode(q)
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
q.ID = n
q.ForceUTC()
b.hlQueryChan <- q
n++
select {
case <-closeChan:
fmt.Printf("Received finish request\n")
break loop
default:
}
}
b.scanFinished = true
}
// processQueries reads byte buffers from hlQueryChan and writes them to the
// target server, while tracking latency.
func (b *CassandraQueryBenchmarker) RunProcess(i int, workersGroup *sync.WaitGroup, statPool sync.Pool, statChan chan *bulk_query.Stat) {
opts := HLQueryExecutorDoOptions{
AggregationPlan: b.aggrPlan,
Debug: bulk_query.Benchmarker.Debug(),
PrettyPrintResponses: bulk_query.Benchmarker.PrettyPrintResponses(),
}
labels := map[string][][]byte{}
for q := range b.hlQueryChan {
qpLagMs, reqLagMs, err := b.queryExecutor.Do(q, opts)
// if needed, prepare stat labels:
if _, ok := labels[string(q.HumanLabel)]; !ok {
labels[string(q.HumanLabel)] = [][]byte{
q.HumanLabel,
[]byte(fmt.Sprintf("%s-qp", q.HumanLabel)),
[]byte(fmt.Sprintf("%s-req", q.HumanLabel)),
}
}
ls := labels[string(q.HumanLabel)]
// total lag stat:
stat := statPool.Get().(*bulk_query.Stat)
stat.InitWithActual(ls[0], qpLagMs+reqLagMs, true)
statChan <- stat
// qp lag stat:
stat = statPool.Get().(*bulk_query.Stat)
stat.InitWithActual(ls[1], qpLagMs, false)
statChan <- stat
// req lag stat:
stat = statPool.Get().(*bulk_query.Stat)
stat.InitWithActual(ls[2], reqLagMs, false)
statChan <- stat
b.queryPool.Put(q)
if err != nil {
log.Fatalf("Error during request: %s\n", err.Error())
}
}
workersGroup.Done()
}