-
Notifications
You must be signed in to change notification settings - Fork 3
/
batch.go
369 lines (330 loc) · 7.3 KB
/
batch.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
package batchinsert
import (
"container/list"
"database/sql"
"errors"
"net/url"
"strconv"
"sync"
"time"
_ "github.com/kshvakov/clickhouse"
)
type ConnectionOpenStrategy string
const OpenStrategyRandom ConnectionOpenStrategy = "random"
const OpenStrategyInOrder ConnectionOpenStrategy = "in_order"
var ErrClosed = errors.New("batch insert is closed")
type options struct {
//
host string
// Auth credentials
userName, password string
// Select the current default database
database string
// Timeout in second
readTimeout, writeTimeOut int
// Disable/enable the Nagle Algorithm for tcp socket (default is 'true' - disable)
noDelay bool
// Comma separated list of single address host for load-balancing
altHosts string
// Random/in_order (default random)
connectionOpenStrategy ConnectionOpenStrategy
// Maximum rows in block (default is 1000000).
// If the rows are larger then the data will be split into several blocks to send them
// to the server
blockSize int
// Maximum amount of pre-allocated byte chunks used in queries (default is 100).
// Decrease this if you experience memory problems at the expense of more GC pressure
// and vice versa.
poolSize int
// Enable debug output
debug bool
//
logger Logger
//
maxBatchSize int
//
flushPeriod time.Duration
}
var defaultOptions = options{
host: "127.0.0.1:9000",
noDelay: true,
connectionOpenStrategy: OpenStrategyRandom,
blockSize: 1000000,
poolSize: 100,
logger: nopLogger{},
maxBatchSize: 10000,
flushPeriod: 1 * time.Second,
}
type Option func(opt *options)
func WithHost(host string) Option {
return func(opt *options) {
opt.host = host
}
}
// Auth credentials
func WithUserInfo(userName, password string) Option {
return func(opt *options) {
opt.userName, opt.password = userName, password
}
}
// Select the current default database
func WithDatabase(db string) Option {
return func(opt *options) {
opt.database = db
}
}
// Timeout in second
func WithReadTimeout(readTimeout int) Option {
return func(opt *options) {
opt.readTimeout = readTimeout
}
}
// Timeout in second
func WithWriteTimeOut(writeTimeOut int) Option {
return func(opt *options) {
opt.writeTimeOut = writeTimeOut
}
}
// Disable/enable the Nagle Algorithm for tcp socket (default is 'true' - disable)
func WithNoDelay(noDelay bool) Option {
return func(opt *options) {
opt.noDelay = noDelay
}
}
// Comma separated list of single address host for load-balancing
func WithAltHosts(altHosts string) Option {
return func(opt *options) {
opt.altHosts = altHosts
}
}
// Random/in_order (default random)
func WithConnectionOpenStrategy(connectionOpenStrategy ConnectionOpenStrategy) Option {
return func(opt *options) {
opt.connectionOpenStrategy = connectionOpenStrategy
}
}
// Maximum rows in block (default is 1000000).
// If the rows are larger then the data will be split into several blocks to send them
// to the server
func WithBlockSize(blockSize int) Option {
return func(opt *options) {
opt.blockSize = blockSize
}
}
// Maximum amount of pre-allocated byte chunks used in queries (default is 100).
// Decrease this if you experience memory problems at the expense of more GC pressure
// and vice versa.
func WithPoolSize(poolSize int) Option {
return func(opt *options) {
opt.poolSize = poolSize
}
}
// Enable debug output
func WithDebug(debug bool) Option {
return func(opt *options) {
opt.debug = debug
}
}
//
func WithLogger(logger Logger) Option {
return func(opt *options) {
opt.logger = logger
}
}
//
func WithMaxBatchSize(maxBatchSize int) Option {
return func(opt *options) {
opt.maxBatchSize = maxBatchSize
}
}
//
func WithFlushPeriod(flushPeriod time.Duration) Option {
return func(opt *options) {
opt.flushPeriod = flushPeriod
}
}
func GetUrl(opts options) string {
u := new(url.URL)
u.Scheme = "tcp"
u.Host = opts.host
v := url.Values{}
if opts.userName != "" {
v.Set("username", opts.userName)
}
if opts.password != "" {
v.Set("password", opts.password)
}
if opts.database != "" {
v.Set("database", opts.database)
}
if opts.readTimeout > 0 {
v.Set("read_timeout", strconv.Itoa(opts.readTimeout))
}
if opts.writeTimeOut > 0 {
v.Set("write_timeout", strconv.Itoa(opts.writeTimeOut))
}
if !opts.noDelay {
v.Set("no_delay", "false")
}
if opts.altHosts != "" {
v.Set("alt_hosts", opts.altHosts)
}
if opts.connectionOpenStrategy != OpenStrategyRandom {
v.Set("connection_open_strategy", string(opts.connectionOpenStrategy))
}
if opts.blockSize != 1000000 {
v.Set("block_size", strconv.Itoa(opts.blockSize))
}
if opts.poolSize != 100 {
v.Set("pool_size", strconv.Itoa(opts.poolSize))
}
if opts.debug {
v.Set("debug", "true")
}
u.RawQuery = v.Encode()
return u.String()
}
//
type BatchInsert struct {
opts options
mu sync.Mutex
wg sync.WaitGroup
db *sql.DB
cache *list.List
kick chan struct{}
exit chan struct{}
insertSql string
exited bool
}
//
func New(insertSql string, opts ...Option) (*BatchInsert, error) {
//
options := defaultOptions
for _, opt := range opts {
opt(&options)
}
//
url := GetUrl(options)
options.logger.Log("open url", url)
//
db, err := sql.Open("clickhouse", url)
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
_ = db.Close()
return nil, err
}
//
b := &BatchInsert{
opts: options,
db: db,
cache: list.New(),
kick: make(chan struct{}),
exit: make(chan struct{}),
insertSql: insertSql,
}
b.wg.Add(1)
go func() {
b.flusher()
b.wg.Done()
}()
return b, nil
}
//
func (b *BatchInsert) Insert(values ...interface{}) (err error) {
b.mu.Lock()
if b.exited {
b.mu.Unlock()
return ErrClosed
}
b.cache.PushBack(values)
if b.cache.Len() >= b.opts.maxBatchSize {
select {
case b.kick <- struct{}{}:
default:
}
}
b.mu.Unlock()
return
}
func (b *BatchInsert) Close() error {
b.mu.Lock()
if b.exited {
b.mu.Unlock()
return nil
}
b.exited = true
close(b.exit)
b.mu.Unlock()
b.wg.Wait()
return b.db.Close()
}
//
func (b *BatchInsert) flusher() {
ticker := time.NewTicker(b.opts.flushPeriod)
defer ticker.Stop()
exited := false
for {
select {
case <-ticker.C:
case <-b.kick:
case <-b.exit:
exited = true
}
b.mu.Lock()
l := b.cache
b.cache = list.New()
b.mu.Unlock()
startTime := time.Now()
if l.Len() > 0 {
err := batchInsert(b.db, b.insertSql, l)
if err != nil {
b.opts.logger.Log("flush error", err)
} else {
b.opts.logger.Log("flushed", l.Len(), "cost", time.Since(startTime))
}
}
if exited {
close(b.kick)
b.opts.logger.Log("flusher", "stop")
return
}
}
}
//
func batchInsert(db *sql.DB, insertSql string, values *list.List) (err error) {
var tx *sql.Tx
tx, err = db.Begin()
if err != nil {
return err
}
defer func() {
if err != nil {
_ = tx.Rollback()
}
}()
stmt, err := tx.Prepare(insertSql)
if err != nil {
return err
}
defer stmt.Close()
for e := values.Front(); e != nil; e = e.Next() {
_, err = stmt.Exec(e.Value.([]interface{})...)
if err != nil {
return err
}
}
return tx.Commit()
}
func (b *BatchInsert) Len() int {
b.mu.Lock()
defer b.mu.Unlock()
return b.cache.Len()
}
func (b *BatchInsert) DB() *sql.DB {
b.mu.Lock()
defer b.mu.Unlock()
return b.db
}