forked from scylladb/scylla-bench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workloads.go
306 lines (256 loc) · 8.04 KB
/
workloads.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
package main
import (
"log"
"math"
"math/rand"
"time"
)
func MinInt64(a int64, b int64) int64 {
if a < b {
return a
} else {
return b
}
}
const (
minToken int64 = -(1 << 63)
maxToken int64 = (1 << 63) - 1
)
// Bounds are inclusive
type TokenRange struct {
Start int64
End int64
}
type WorkloadGenerator interface {
NextTokenRange() TokenRange
NextPartitionKey() int64
NextClusteringKey() int64
IsPartitionDone() bool
IsDone() bool
Restart()
}
type SequentialVisitAll struct {
PartitionOffset int64
PartitionCount int64
ClusteringRowCount int64
NextPartition int64
NextClusteringRow int64
}
func NewSequentialVisitAll(partitionOffset int64, partitionCount int64, clusteringRowCount int64) *SequentialVisitAll {
return &SequentialVisitAll{partitionOffset, partitionOffset + partitionCount, clusteringRowCount, partitionOffset, 0}
}
func (sva *SequentialVisitAll) NextTokenRange() TokenRange {
panic("SequentialVisitAll does not support NextTokenRange()")
}
func (sva *SequentialVisitAll) NextPartitionKey() int64 {
if sva.NextClusteringRow < sva.ClusteringRowCount {
return sva.NextPartition
}
sva.NextClusteringRow = 0
sva.NextPartition++
pk := sva.NextPartition
return pk
}
func (sva *SequentialVisitAll) NextClusteringKey() int64 {
ck := sva.NextClusteringRow
sva.NextClusteringRow++
return ck
}
func (sva *SequentialVisitAll) IsDone() bool {
return sva.NextPartition >= sva.PartitionCount || (sva.NextPartition+1 == sva.PartitionCount && sva.NextClusteringRow >= sva.ClusteringRowCount)
}
func (sva *SequentialVisitAll) Restart() {
sva.NextClusteringRow = 0
sva.NextPartition = sva.PartitionOffset
}
func (sva *SequentialVisitAll) IsPartitionDone() bool {
return sva.NextClusteringRow == sva.ClusteringRowCount
}
type RandomUniform struct {
Generator *rand.Rand
PartitionCount int64
ClusteringRowCount int64
}
func NewRandomUniform(i int, partitionCount int64, clusteringRowCount int64) *RandomUniform {
generator := rand.New(rand.NewSource(int64(time.Now().Nanosecond() * (i + 1))))
return &RandomUniform{generator, int64(partitionCount), int64(clusteringRowCount)}
}
func (ru *RandomUniform) NextTokenRange() TokenRange {
panic("RandomUniform does not support NextTokenRange()")
}
func (ru *RandomUniform) NextPartitionKey() int64 {
return ru.Generator.Int63n(ru.PartitionCount)
}
func (ru *RandomUniform) NextClusteringKey() int64 {
return ru.Generator.Int63n(ru.ClusteringRowCount)
}
func (ru *RandomUniform) IsDone() bool {
return false
}
func (ru *RandomUniform) IsPartitionDone() bool {
return false
}
func (ru *RandomUniform) Restart() {
}
type TimeSeriesWrite struct {
PkStride int64
PkOffset int64
PkCount int64
PkPosition int64
PkGeneration int64
CkCount int64
CkPosition int64
StartTime time.Time
Period time.Duration
MoveToNextPartition bool
}
func NewTimeSeriesWriter(threadId int, threadCount int, pkCount int64, ckCount int64, startTime time.Time, rate int64) *TimeSeriesWrite {
period := time.Duration(int64(time.Second.Nanoseconds()) * (pkCount / int64(threadCount)) / rate)
pkStride := int64(threadCount)
pkOffset := int64(threadId)
return &TimeSeriesWrite{pkStride, pkOffset, pkCount, pkOffset - pkStride, 0,
ckCount, 0, startTime, period, false}
}
func (tsw *TimeSeriesWrite) NextTokenRange() TokenRange {
panic("TimeSeriesWrite does not support NextTokenRange()")
}
func (tsw *TimeSeriesWrite) NextPartitionKey() int64 {
tsw.PkPosition += tsw.PkStride
if tsw.PkPosition >= tsw.PkCount {
tsw.PkPosition = tsw.PkOffset
tsw.CkPosition++
if tsw.CkPosition >= tsw.CkCount {
tsw.PkGeneration++
tsw.CkPosition = 0
}
}
tsw.MoveToNextPartition = false
return tsw.PkPosition<<32 | tsw.PkGeneration
}
func (tsw *TimeSeriesWrite) NextClusteringKey() int64 {
tsw.MoveToNextPartition = true
position := tsw.CkPosition + tsw.PkGeneration*tsw.CkCount
return -(tsw.StartTime.UnixNano() + tsw.Period.Nanoseconds()*position)
}
func (*TimeSeriesWrite) IsDone() bool {
return false
}
func (tsw *TimeSeriesWrite) IsPartitionDone() bool {
return tsw.MoveToNextPartition
}
func (*TimeSeriesWrite) Restart() {
}
type TimeSeriesRead struct {
Generator *rand.Rand
HalfNormalDist bool
PkStride int64
PkOffset int64
PkCount int64
PkPosition int64
StartTimestamp int64
CkCount int64
CurrentGeneration int64
Period int64
}
func NewTimeSeriesReader(threadId int, threadCount int, pkCount int64, ckCount int64, writeRate int64, distribution string, startTime time.Time) *TimeSeriesRead {
var halfNormalDist bool
switch distribution {
case "uniform":
halfNormalDist = false
case "hnormal":
halfNormalDist = true
default:
log.Fatal("unknown distribution", distribution)
}
generator := rand.New(rand.NewSource(int64(time.Now().Nanosecond() * (threadId + 1))))
pkStride := int64(threadCount)
pkOffset := int64(threadId) % pkCount
period := time.Second.Nanoseconds() / writeRate
return &TimeSeriesRead{generator, halfNormalDist, pkStride, pkOffset, pkCount, pkOffset - pkStride,
startTime.UnixNano(), ckCount, 0, period}
}
func RandomInt64(generator *rand.Rand, halfNormalDist bool, maxValue int64) int64 {
if halfNormalDist {
value := 1. - math.Min(math.Abs(generator.NormFloat64()), 4.)/4.
return int64(float64(maxValue) * value)
} else {
return generator.Int63n(maxValue)
}
}
func (tsw *TimeSeriesRead) NextTokenRange() TokenRange {
panic("TimeSeriesRead does not support NextTokenRange()")
}
func (tsw *TimeSeriesRead) NextPartitionKey() int64 {
tsw.PkPosition += tsw.PkStride
if tsw.PkPosition >= tsw.PkCount {
tsw.PkPosition = tsw.PkOffset
}
maxGeneration := (time.Now().UnixNano()-tsw.StartTimestamp)/(tsw.Period*tsw.CkCount) + 1
tsw.CurrentGeneration = RandomInt64(tsw.Generator, tsw.HalfNormalDist, maxGeneration)
return tsw.PkPosition<<32 | tsw.CurrentGeneration
}
func (tsw *TimeSeriesRead) NextClusteringKey() int64 {
maxRange := (time.Now().UnixNano()-tsw.StartTimestamp)/tsw.Period - tsw.CurrentGeneration*tsw.CkCount + 1
maxRange = MinInt64(tsw.CkCount, maxRange)
timestampDelta := (tsw.CurrentGeneration*tsw.CkCount + RandomInt64(tsw.Generator, tsw.HalfNormalDist, maxRange)) * tsw.Period
return -(timestampDelta + tsw.StartTimestamp)
}
func (*TimeSeriesRead) IsDone() bool {
return false
}
func (tsw *TimeSeriesRead) IsPartitionDone() bool {
return false
}
func (tsw *TimeSeriesRead) Restart() {
}
type RangeScan struct {
TotalRangeCount int
RangeOffset int
RangeCount int
NextRange int
}
func NewRangeScan(totalRangeCount int, rangeOffset int, rangeCount int) *RangeScan {
return &RangeScan{totalRangeCount, rangeOffset, rangeOffset + rangeCount, rangeOffset}
}
func (rs* RangeScan) NextTokenRange() TokenRange {
// Special case, no range splitting
if rs.TotalRangeCount == 1 {
rs.NextRange++;
return TokenRange{minToken, maxToken}
}
// This is in fact -1 compared to the real number of tokens, which
// is 2**64. But this is fine, as the worst that can happen is that
// due to the inprecise calculation of tokensPerRange more tokens
// will be in the very last range than should be, which is
// tolerable.
const tokenCount uint64 = ^uint64(0)
// Due to the special handling of TotalRangeCount == 1 above, this
// is guaranteed to safely fit into an int64
tokensPerRange := int64(tokenCount / uint64(rs.TotalRangeCount))
currentRange := rs.NextRange
rs.NextRange++;
firstToken := minToken + int64(currentRange) * tokensPerRange
var lastToken int64
// Make sure the very last range streches all the way to maxToken.
if rs.NextRange == rs.TotalRangeCount {
lastToken = maxToken
} else {
lastToken = firstToken + tokensPerRange - 1
}
return TokenRange{firstToken, lastToken}
}
func (*RangeScan) NextPartitionKey() int64 {
return 0
}
func (*RangeScan) NextClusteringKey() int64 {
return 0
}
func (*RangeScan) IsPartitionDone() bool {
return false
}
func (rs *RangeScan) IsDone() bool {
return rs.NextRange >= rs.RangeCount
}
func (rs *RangeScan) Restart() {
rs.NextRange = rs.RangeOffset
}