Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 07664d4

Browse files
authored
Merge pull request ClickHouse#255 from clobucks/feature/time-random-open-connection-strategy
Add connOpenTimeRandom strategy to distribute connections equally across shards.
2 parents a4601ff + 96c1243 commit 07664d4

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ Golang SQL database driver for [Yandex ClickHouse](https://clickhouse.yandex/)
1818
* no_delay - disable/enable the Nagle Algorithm for tcp socket (default is 'true' - disable)
1919
* alt_hosts - comma separated list of single address host for load-balancing
2020
* connection_open_strategy - random/in_order (default random).
21-
* random - choose random server from set
22-
* in_order - first live server is choosen in specified order
21+
* random - choose random server from set
22+
* in_order - first live server is choosen in specified order
23+
* time_random - choose random(based on current time) server from set. This option differs from `random` in that randomness is based on current time rather than on amount of previous connections.
2324
* block_size - 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. If one block was sent to the server, the data will be persisted on the server disk, we can't rollback the transaction. So always keep in mind that the batch size no larger than the block_size if you want atomic batch insert.
2425
* pool_size - maximum amount of preallocated 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.
2526
* debug - enable debug output (boolean value)

bootstrap.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ func open(dsn string) (*clickhouse, error) {
152152
connOpenStrategy = connOpenRandom
153153
case "in_order":
154154
connOpenStrategy = connOpenInOrder
155+
case "time_random":
156+
connOpenStrategy = connOpenTimeRandom
155157
}
156158

157159
settings, err := makeQuerySettings(query)

connect.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ func (s openStrategy) String() string {
1717
switch s {
1818
case connOpenInOrder:
1919
return "in_order"
20+
case connOpenTimeRandom:
21+
return "time_random"
2022
}
2123
return "random"
2224
}
2325

2426
const (
2527
connOpenRandom openStrategy = iota + 1
2628
connOpenInOrder
29+
connOpenTimeRandom
2730
)
2831

2932
type connOptions struct {
@@ -55,13 +58,21 @@ func dial(options connOptions) (*connect, error) {
5558
}
5659
tlsConfig.InsecureSkipVerify = options.skipVerify
5760
}
61+
checkedHosts := make(map[int]struct{}, len(options.hosts))
5862
for i := range options.hosts {
5963
var num int
6064
switch options.openStrategy {
6165
case connOpenInOrder:
6266
num = i
6367
case connOpenRandom:
6468
num = (ident + i) % len(options.hosts)
69+
case connOpenTimeRandom:
70+
// select host based on milliseconds
71+
num = int((time.Now().UnixNano()/1000)%1000) % len(options.hosts)
72+
for _, ok := checkedHosts[num]; ok; _, ok = checkedHosts[num] {
73+
num = int(time.Now().UnixNano()) % len(options.hosts)
74+
}
75+
checkedHosts[num] = struct{}{}
6576
}
6677
switch {
6778
case options.secure:

0 commit comments

Comments
 (0)