-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
440 lines (412 loc) · 10.7 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package main
import (
"context"
"crypto/tls"
"database/sql"
"fmt"
"math/rand"
"strings"
"sync/atomic"
"time"
"github.com/ClickHouse/clickhouse-go/v2"
chBuffer "github.com/kokizzu/ch-timed-buffer"
"github.com/kokizzu/gotro/D/Ch"
"github.com/kokizzu/gotro/L"
"github.com/kokizzu/gotro/S"
"golang.org/x/sync/errgroup"
)
const debug = true
type ClickhouseConf struct {
User string
Pass string
DB string
Host string
Port int
UseSsl bool
}
func (c ClickhouseConf) Connect() (a *Ch.Adapter, err error) {
hostPort := fmt.Sprintf("%s:%d", c.Host, c.Port)
conf := &clickhouse.Options{
Addr: []string{hostPort},
Auth: clickhouse.Auth{
Database: c.DB,
Username: c.User,
Password: c.Pass,
},
Settings: clickhouse.Settings{
`max_execution_time`: 60,
`allow_experimental_lightweight_delete`: 1,
`async_insert`: 1,
//`wait_for_async_insert`:1,
},
DialTimeout: 5 * time.Second,
Compression: &clickhouse.Compression{
Method: clickhouse.CompressionLZ4,
},
//Debug: true,
}
if c.UseSsl {
conf.TLS = &tls.Config{
InsecureSkipVerify: true,
}
}
connectFunc := func() *sql.DB {
conn := clickhouse.OpenDB(conf)
conn.SetMaxIdleConns(5)
conn.SetMaxOpenConns(200)
conn.SetConnMaxLifetime(time.Hour)
return conn
}
conn := connectFunc()
err = conn.Ping()
if isError(err) {
return nil, err
}
a = &Ch.Adapter{
DB: conn,
Reconnect: connectFunc,
}
return a, nil
}
func main() {
const createTable = `
CREATE TABLE IF NOT EXISTS ver3(
root LowCardinality(String) CODEC(LZ4HC),
bucket LowCardinality(String) CODEC(LZ4HC),
key String CODEC(LZ4HC),
version_id String CODEC(LZ4HC),
ver DateTime64 CODEC(LZ4HC)
) engine=ReplacingMergeTree(ver)
ORDER BY (root, bucket, key, version_id)
`
cc := ClickhouseConf{
DB: "default",
Host: "localhost",
Port: 9000,
}
ch, err := cc.Connect()
L.PanicIf(err, `cc.Connect`)
// create table
_, err = ch.Exec(createTable)
L.PanicIf(err, `table creation`)
// truncate table
const truncateTable = `TRUNCATE TABLE ver3`
_, err = ch.Exec(truncateTable)
L.PanicIf(err, `table truncation`)
ctx := context.Background()
eg, ctx := errgroup.WithContext(ctx)
const maxDelete = 1_000_000 // when changing this, must also change insertTotal
deleteQueue := make(chan [2]string, maxDelete)
const root = `root1`
const bucket = `bucket1`
// do parallel insert
const insertThread = 8 // assume 8 instance inserting altogether
const insertTotal = 20_000_000 // when changing this, must also change maxDelete
const randomInsertEvery = 2000
const insertUseAsync = false
deleteChance := float32(maxDelete) / insertTotal
insertErr := uint64(0)
insertThreadDone := uint32(0)
insertDone := uint64(0)
insertDur := uint64(0)
// testing creation of random pattern
insertPatterns := []func() string{ // frequency
func() string {
return `Veeam/Archive/Backups/` + HEX32(0) + `/` + HEX32(1) + `/blocks/` + HEX32(0) + `/` + INT3() + `.` + HEX32(1) + `.` + HEX32(2) + `.blk`
},
func() string {
return `Veeam/Archive/Backups/` + HEX32(0) + `/` + HEX32(1) + `/checkpoints/checkpoint.` + INT2()
},
func() string {
return `Veeam/Archive/Backups/` + HEX32(0) + `/` + HEX32(1) + `/objs/` + STR() + `.` + INT1()
},
func() string {
return `Veeam/Archive/Backups/KeySetParts/key-` + HEX32(0) + `-` + HEX32(1) + `.rep`
},
func() string {
return `Veeam/Archive/Backups/KeySetParts/key-` + HEX32(0) + `.dat`
},
}
for z := 0; z < insertThread; z++ {
thread := z
eg.Go(func() error {
defer fmt.Println(`insert thread done`, thread)
var timedBuffer *chBuffer.TimedBuffer
if !insertUseAsync {
const insertEvery = 40_000
timedBuffer = chBuffer.NewTimedBuffer(ch.DB, insertEvery, 1*time.Second, func(tx *sql.Tx) *sql.Stmt {
const insertQuery = `
INSERT INTO ver3 VALUES(?, ?, ?, ?, ?)
`
stmt, err := tx.Prepare(insertQuery)
L.IsError(err, `failed to tx.Prepare: `+insertQuery)
return stmt
})
}
for z := 0; z < insertTotal/insertThread; z++ {
atomic.AddUint64(&insertDur, track(func() {
var key string
if z%randomInsertEvery == 0 {
key = insertPatterns[rand.Int()%len(insertPatterns)]()
} else {
key = insertPatterns[0]()
}
verId := S.RandomPassword(24)
if insertUseAsync {
// slow and high cpu usage, better use ch-timed-buffer, wait_for_async_insert=0 even worse
const insertQuery = `
INSERT INTO ver3 SETTINGS async_insert=1, wait_for_async_insert=1 VALUES (?, ?, ?, ?, ?)
`
_, err := ch.Exec(insertQuery, root, bucket, key, verId, time.Now().Format(`2006-01-02 15:04:05.000000`))
if isError(err) {
atomic.AddUint64(&insertErr, 1)
return
}
} else {
// fast insert but slow to do other queries, also high cpu usage
if !timedBuffer.Insert([]any{root, bucket, key, verId, time.Now().Format(`2006-01-02 15:04:05.000000`)}) {
atomic.AddUint64(&insertErr, 1)
return
}
}
atomic.AddUint64(&insertDone, 1)
if rand.Float32() < deleteChance {
deleteQueue <- [2]string{key, verId}
}
}))
}
if atomic.AddUint32(&insertThreadDone, 1) == insertThread {
close(deleteQueue)
}
if !insertUseAsync {
timedBuffer.Close()
}
return nil
})
}
// do parallel deletion
const deleteThread = 4
const stopAnywaySec = 10
exitAfterSec := stopAnywaySec
deleteErr := uint64(0)
deleteDone := uint64(0)
deleteDur := uint64(0)
for z := 0; z < deleteThread; z++ {
thread := z
eg.Go(func() error {
defer fmt.Println(`delete thread done`, thread)
for key := range deleteQueue {
atomic.AddUint64(&deleteDur, track(func() {
const deleteQuery = `
DELETE FROM ver3 WHERE root=? AND bucket=? AND key=? AND version_id=?
`
_, err := ch.Exec(deleteQuery, root, bucket, key[0], key[1])
if isError(err) {
atomic.AddUint64(&deleteErr, 1)
}
atomic.AddUint64(&deleteDone, 1)
}))
if exitAfterSec <= 0 {
return nil
}
}
return nil
})
}
// do LIST query
_ = []func() string{ // frequency
func() string {
return `Veeam/Archive/Backups/` + HEX32(0) + `/` + HEX32(1) + `/blocks/` + HEX32e(0) + `/` + INT3() + `/` + HEX32e(1)
},
func() string {
return `Veeam/Archive/Backups/` + HEX32(0) + `/` + HEX32(1) + `/blocks/` + HEX32e(0) + `/` + INT3()
},
func() string {
return `Veeam/Archive/Backups/` + HEX32(0) + `/` + HEX32(1) + `/blocks/` + HEX32e(0)
},
func() string {
return `Veeam/Archive/Backups/` + HEX32(0) + `/` + HEX32(1) + `/blocks`
},
func() string {
return `Veeam/Archive/Backups/` + HEX32(0) + `/` + HEX32(1) + `/checkpoints`
},
func() string {
return `Veeam/Archive/Backups/` + HEX32(0) + `/` + HEX32(1)
},
func() string {
return `Veeam/Archive/Backups/` + HEX32(0)
},
func() string {
return `Veeam/Archive/Backups/` + HEX32(0) + `/` + HEX32(1) + `/objs`
},
func() string {
return `Veeam/Archive/Backups/` + HEX32(0) + `/` + HEX32(1)
},
func() string {
return `Veeam/Archive/Backups/` + HEX32(0)
},
func() string {
return `Veeam/Archive/Backups/KeySetParts`
},
func() string {
return `Veeam/Archive/Backups`
},
func() string {
return `Veeam/Archive`
},
func() string {
return `Veeam`
},
func() string {
return ``
},
}
const listingTotal = 100_000
const listingThread = 4
const maxRandomPattern = 15
listingErr := uint64(0)
listingDone := uint64(0)
listingRows := uint64(0)
listingDur := uint64(0)
listingHighestRows := uint64(0)
for z := 0; z < listingThread; z++ {
thread := z
eg.Go(func() error {
defer fmt.Println(`LIST thread done`, thread)
time.Sleep(time.Second)
patterns := [maxRandomPattern]string{`Veeam`}
for z := 0; z < listingTotal/listingThread; z++ {
atomic.AddUint64(&listingDur, track(func() {
pattern := patterns[rand.Int()%maxRandomPattern]
parts := len(strings.Split(pattern, `/`)) + 1
const listingQuery = `
SELECT arrayStringConcat(splitByChar('/', key, ?),'/'), MAX(version_id)
FROM ver3
WHERE root=?
AND bucket=?
AND key LIKE ?
GROUP BY 1
ORDER BY 1 ASC
LIMIT 1001
`
rows, err := ch.Query(listingQuery,
parts, root, bucket, pattern+`%`)
if isError(err) {
atomic.AddUint64(&listingErr, 1)
return
}
atomic.AddUint64(&listingDone, 1)
var str1, str2 string
var total = uint64(0)
for rows.Next() {
err := rows.Scan(&str1, &str2)
if isError(err) {
atomic.AddUint64(&listingErr, 1)
return
}
total++
if total < maxRandomPattern && !S.Contains(str1, `.`) { // most likely a directory
patterns[(z+int(total))%maxRandomPattern] = str1
}
}
atomic.AddUint64(&listingRows, total)
if listingHighestRows < total {
listingHighestRows = total
}
if exitAfterSec <= 0 {
return
}
}))
}
return nil
})
}
// show statistics
const microSec2sec = 1000_000
eg.Go(func() error {
defer fmt.Println(`stats thread done`)
ticker := time.NewTicker(time.Second)
sec := 1
for {
select {
case <-ticker.C:
fmt.Printf("INS %d (%.1f/s), DEL %d (%.1f/s), LIST %d (%.1f/s), ROWS %d (high=%d), ERR: %d/%d/%d, %d sec\n",
insertDone,
float64(insertDone)/(float64(insertDur)/microSec2sec),
deleteDone,
float64(deleteDone)/(float64(deleteDur)/microSec2sec),
listingDone,
float64(listingDone)/(float64(listingDur)/microSec2sec),
listingRows,
listingHighestRows,
insertErr,
deleteErr,
listingErr,
sec,
)
if insertThreadDone >= insertThread && len(deleteQueue) == 0 {
exitAfterSec--
if exitAfterSec < 0 {
return nil
}
}
sec++
}
}
})
L.IsError(eg.Wait(), `eg.Wait`)
}
func isError(err error) bool {
if debug {
if err != nil {
panic(err)
}
}
return err != nil
}
func STR() string {
return S.RandomPassword(40)
}
func INT1() string {
return fmt.Sprint(rand.Int() % 256)
}
func INT2() string {
return fmt.Sprint(rand.Int() % (256 * 256))
}
func INT3() string {
r := rand.Float32()
if r < 0.33 {
return `100000000`
}
if r < 0.66 {
return `200000000`
}
return `300000000`
}
var existingKeys [3][]string
func HEX32(pos int) string {
slice := existingKeys[pos]
// 33% of returning existing key
if pos < 2 && len(slice) > 100 && rand.Float32() < 0.66 {
key := slice[rand.Int()%len(slice)]
return key
}
key := S.RandomPassword(32)
if pos < 2 {
existingKeys[pos] = append(existingKeys[pos], key) // ignore race condition
}
return key
}
func HEX32e(pos int) string {
slice := existingKeys[pos]
if pos < 2 && len(slice) > 0 {
key := slice[rand.Int()%len(slice)]
return key
}
return S.RandomPassword(32)
}
func track(f func()) uint64 {
start := time.Now()
f()
return uint64(time.Since(start).Microseconds())
}