-
Notifications
You must be signed in to change notification settings - Fork 2
/
clickhouse_cluster.go
182 lines (148 loc) · 4.22 KB
/
clickhouse_cluster.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
package bristle
import (
"database/sql"
"errors"
"strings"
"sync"
_ "github.com/ClickHouse/clickhouse-go"
)
type ClickhouseCluster struct {
sync.Mutex
DSN string
tables map[FullTableName]*ClickhouseTable
pool map[*sql.DB]bool
poolSize int
config ClickhouseClusterConfig
}
func NewClickhouseCluster(config ClickhouseClusterConfig) *ClickhouseCluster {
poolSize := config.PoolSize
if poolSize == 0 {
poolSize = 8
}
return &ClickhouseCluster{
DSN: config.DSN,
config: config,
tables: make(map[FullTableName]*ClickhouseTable),
pool: make(map[*sql.DB]bool),
poolSize: poolSize,
}
}
type FullTableName string
func (t FullTableName) Parts() []string {
return strings.SplitN(string(t), ".", 2)
}
func (f FullTableName) Database() string {
return f.Parts()[0]
}
func (f FullTableName) Table() string {
return f.Parts()[1]
}
func ParseFullTableName(full string) FullTableName {
result := FullTableName(full)
parts := result.Parts()
if len(parts) != 2 {
panic("ParseFullTableName failed, wrong number of seperators: " + full)
}
return result
}
var ErrNoSuchTable = errors.New("no clickhouse table by that name")
func (c *ClickhouseCluster) GetTable(fullTableName FullTableName) (*ClickhouseTable, error) {
table := c.tables[fullTableName]
if table != nil {
return table, nil
}
conn, err := c.GetConn()
if err != nil {
return nil, err
}
defer c.ReleaseConn(conn)
columnRows, err := conn.Query(
`SELECT name, position, type, default_expression FROM system.columns WHERE database = ? AND table = ? ORDER BY position`,
fullTableName.Database(),
fullTableName.Table(),
)
if err != nil {
return nil, err
}
defer columnRows.Close()
columns := map[string]*ClickhouseColumn{}
for columnRows.Next() {
var column ClickhouseColumn
if err := columnRows.Scan(&column.Name, &column.Position, &column.Type, &column.Default); err != nil {
return nil, err
}
columns[column.Name] = &column
}
if len(columns) == 0 {
return nil, ErrNoSuchTable
}
config, ok := c.config.Tables[string(fullTableName)]
if !ok {
config = DefaultClickhouseTableConfig()
}
table, err = newClickhouseTable(c, fullTableName, columns, config)
if err != nil {
return nil, err
}
c.tables[fullTableName] = table
return table, nil
}
var ErrNoConn = errors.New("no connection in pool")
func (c *ClickhouseCluster) ReleaseConn(conn *sql.DB) {
c.Lock()
defer c.Unlock()
c.pool[conn] = false
}
// Returns a connection from the pool if one is available, otherwise an error
func (c *ClickhouseCluster) GetConn() (*sql.DB, error) {
// This function attempts to avoid keeping a lock while doing misc network
// activity, like opening a new connection or pinging an existing one. This
// results in some messy code, so its well documented.
c.Lock()
// We try to find an existing connection in the pool which is free
var selectedConn *sql.DB
for conn, isCheckedOut := range c.pool {
if !isCheckedOut {
selectedConn = conn
break
}
}
// Note at this point we have the lock acquired, so its expected that we should
// still have it acquired when exiting this block.
if selectedConn != nil {
// We mark the selected connection as checked out
c.pool[selectedConn] = true
// Release the lock so we can check the selected connection with a ping
c.Unlock()
// Ping on the connection and return if its successful
if err := selectedConn.Ping(); err == nil {
return selectedConn, nil
}
// Finalize the connection
selectedConn.Close()
// Reacquire the lock
c.Lock()
delete(c.pool, selectedConn)
// We want to keep the lock acquired now so we match the state at the
// entrypoint of this block.
}
// If we don't have room for a new connection return that we're full
if len(c.pool) >= c.poolSize {
// Release the lock as we exit
c.Unlock()
// TODO: eventually we may want to block here
return nil, ErrNoConn
}
// Release the lock so we can open a new connection
c.Unlock()
connect, err := sql.Open("clickhouse", c.DSN)
if err != nil {
return nil, err
}
// Ok we're almost done, we can reaqcuire the lock once more and insert the
// newly opened connection in the pool, finally returning it to the user.
c.Lock()
c.pool[selectedConn] = true
c.Unlock()
return connect, nil
}