-
Notifications
You must be signed in to change notification settings - Fork 25
/
cluster.go
339 lines (293 loc) · 7.2 KB
/
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
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
package sharding
import (
"fmt"
"strconv"
"sync"
"github.com/go-pg/pg/v10"
)
type shardInfo struct {
id int
shard *pg.DB
dbInd int
}
// Cluster maps many (up to 2048) logical database shards implemented
// using PostgreSQL schemas to far fewer physical PostgreSQL servers.
type Cluster struct {
gen *IDGen
dbs []*pg.DB
servers []*pg.DB // unique dbs
shards []shardInfo
shardList []*pg.DB
}
// NewClusterWithGen returns new PostgreSQL cluster consisting of physical
// dbs and running nshards logical shards.
func NewClusterWithGen(dbs []*pg.DB, nshards int, gen *IDGen) *Cluster {
if gen == nil {
gen = DefaultIDGen
}
if len(dbs) == 0 {
panic("at least one db is required")
}
if nshards == 0 {
panic("at least one shard is required")
}
if len(dbs) > gen.NumShards() || nshards > gen.NumShards() {
panic(fmt.Sprintf("too many shards"))
}
if nshards < len(dbs) {
panic("number of shards must be greater or equal number of dbs")
}
if nshards%len(dbs) != 0 {
panic("number of shards must be divideable by number of dbs")
}
cl := &Cluster{
gen: gen,
dbs: dbs,
shards: make([]shardInfo, nshards),
shardList: make([]*pg.DB, nshards),
}
cl.init()
return cl
}
func NewCluster(dbs []*pg.DB, nshards int) *Cluster {
return NewClusterWithGen(dbs, nshards, nil)
}
func (cl *Cluster) init() {
dbSet := make(map[*pg.DB]struct{})
for _, db := range cl.dbs {
if _, ok := dbSet[db]; ok {
continue
}
dbSet[db] = struct{}{}
cl.servers = append(cl.servers, db)
}
for i := 0; i < len(cl.shards); i++ {
dbInd := i % len(cl.dbs)
shard := cl.newShard(cl.dbs[dbInd], int64(i))
cl.shards[i] = shardInfo{
id: i,
shard: shard,
dbInd: dbInd,
}
cl.shardList[i] = shard
}
}
func (cl *Cluster) IDGen() *IDGen {
return cl.gen
}
func (cl *Cluster) newShard(db *pg.DB, id int64) *pg.DB {
name := "shard" + strconv.FormatInt(id, 10)
return db.
WithParam("shard_id", id).
WithParam("shard", pg.Safe(name)).
WithParam("epoch", cl.gen.epoch).
WithParam("SHARD_ID", id).
WithParam("SHARD", pg.Safe(name)).
WithParam("EPOCH", cl.gen.epoch)
}
func (cl *Cluster) Close() error {
var firstErr error
for _, db := range cl.servers {
if err := db.Close(); err != nil && firstErr == nil {
firstErr = err
}
}
return firstErr
}
// DBs returns list of database servers in the cluster.
func (cl *Cluster) DBs() []*pg.DB {
return cl.dbs
}
// DB returns db id and db for the number.
func (cl *Cluster) DB(number int64) (int, *pg.DB) {
idx := uint64(number)
idx %= uint64(len(cl.shards))
dbInd := cl.shards[idx].dbInd
return dbInd, cl.dbs[dbInd]
}
// Shards returns list of shards running in the db. If db is nil all
// shards are returned.
func (cl *Cluster) Shards(db *pg.DB) []*pg.DB {
if db == nil {
return cl.shardList
}
var shards []*pg.DB
for i := range cl.shards {
shard := &cl.shards[i]
if cl.dbs[shard.dbInd] == db {
shards = append(shards, shard.shard)
}
}
return shards
}
// Shard maps the number to the corresponding shard in the cluster.
func (cl *Cluster) Shard(number int64) *pg.DB {
idx := uint64(number) % uint64(len(cl.shards))
return cl.shards[idx].shard
}
// SplitShard uses SplitID to extract shard id from the id and then
// returns corresponding Shard in the cluster.
func (cl *Cluster) SplitShard(id int64) *pg.DB {
_, shardID, _ := cl.gen.SplitID(id)
return cl.Shard(shardID)
}
// ForEachDB concurrently calls the fn on each database in the cluster.
func (cl *Cluster) ForEachDB(fn func(db *pg.DB) error) error {
errCh := make(chan error, 1)
var wg sync.WaitGroup
wg.Add(len(cl.servers))
for _, db := range cl.servers {
go func(db *pg.DB) {
defer wg.Done()
if err := fn(db); err != nil {
select {
case errCh <- err:
default:
}
}
}(db)
}
wg.Wait()
select {
case err := <-errCh:
return err
default:
return nil
}
}
// ForEachShard concurrently calls the fn on each shard in the cluster.
// It is the same as ForEachNShards(1, fn).
func (cl *Cluster) ForEachShard(fn func(shard *pg.DB) error) error {
return cl.ForEachDB(func(db *pg.DB) error {
var firstErr error
for i := range cl.shards {
shard := cl.shards[i].shard
if shard.Options() != db.Options() {
continue
}
if err := fn(shard); err != nil && firstErr == nil {
firstErr = err
}
}
return firstErr
})
}
// ForEachNShards concurrently calls the fn on each N shards in the cluster.
func (cl *Cluster) ForEachNShards(n int, fn func(shard *pg.DB) error) error {
return cl.ForEachDB(func(db *pg.DB) error {
var wg sync.WaitGroup
errCh := make(chan error, 1)
limit := make(chan struct{}, n)
for i := range cl.shards {
shard := cl.shards[i].shard
if shard.Options() != db.Options() {
continue
}
limit <- struct{}{}
wg.Add(1)
go func(shard *pg.DB) {
defer func() {
<-limit
wg.Done()
}()
if err := fn(shard); err != nil {
select {
case errCh <- err:
default:
}
}
}(shard)
}
wg.Wait()
select {
case err := <-errCh:
return err
default:
return nil
}
})
}
// SubCluster is a subset of the cluster.
type SubCluster struct {
cl *Cluster
shards []*shardInfo
}
// SubCluster returns a subset of the cluster of the given size.
func (cl *Cluster) SubCluster(number int64, size int) *SubCluster {
if size > len(cl.shards) {
size = len(cl.shards)
}
step := len(cl.shards) / size
clusterId := int(uint64(number)%uint64(step)) * size
shards := make([]*shardInfo, size)
for i := 0; i < size; i++ {
shards[i] = &cl.shards[clusterId+i]
}
return &SubCluster{
cl: cl,
shards: shards,
}
}
// SplitShard uses SplitID to extract shard id from the id and then
// returns corresponding Shard in the subcluster.
func (cl *SubCluster) SplitShard(id int64) *pg.DB {
_, shardID, _ := cl.cl.gen.SplitID(id)
return cl.Shard(shardID)
}
// Shard maps the number to the corresponding shard in the subscluster.
func (cl *SubCluster) Shard(number int64) *pg.DB {
idx := uint64(number) % uint64(len(cl.shards))
return cl.shards[idx].shard
}
// ForEachShard concurrently calls the fn on each shard in the subcluster.
// It is the same as ForEachNShards(1, fn).
func (cl *SubCluster) ForEachShard(fn func(shard *pg.DB) error) error {
return cl.cl.ForEachDB(func(db *pg.DB) error {
var firstErr error
for i := range cl.shards {
shard := cl.shards[i].shard
if shard.Options() != db.Options() {
continue
}
if err := fn(shard); err != nil && firstErr == nil {
firstErr = err
}
}
return firstErr
})
}
// ForEachNShards concurrently calls the fn on each N shards in the subcluster.
func (cl *SubCluster) ForEachNShards(n int, fn func(shard *pg.DB) error) error {
return cl.cl.ForEachDB(func(db *pg.DB) error {
var wg sync.WaitGroup
errCh := make(chan error, 1)
limit := make(chan struct{}, n)
for i := range cl.shards {
shard := cl.shards[i].shard
if shard.Options() != db.Options() {
continue
}
limit <- struct{}{}
wg.Add(1)
go func(shard *pg.DB) {
defer func() {
<-limit
wg.Done()
}()
if err := fn(shard); err != nil {
select {
case errCh <- err:
default:
}
}
}(shard)
}
wg.Wait()
select {
case err := <-errCh:
return err
default:
return nil
}
})
}