forked from longbridge/gorm-sharding
-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathsharding.go
568 lines (502 loc) · 15.3 KB
/
sharding.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
package sharding
import (
"errors"
"fmt"
"hash/crc32"
"strconv"
"strings"
"sync"
"github.com/bwmarrin/snowflake"
"github.com/longbridgeapp/sqlparser"
"golang.org/x/exp/slices"
"gorm.io/gorm"
)
var (
ErrMissingShardingKey = errors.New("sharding key or id required, and use operator =")
ErrInvalidID = errors.New("invalid id format")
ErrInsertDiffSuffix = errors.New("can not insert different suffix table in one query ")
)
var (
ShardingIgnoreStoreKey = "sharding_ignore"
)
type Sharding struct {
*gorm.DB
ConnPool *ConnPool
configs map[string]Config
querys sync.Map
snowflakeNodes []*snowflake.Node
_config Config
_tables []any
mutex sync.RWMutex
}
// Config specifies the configuration for sharding.
type Config struct {
// When DoubleWrite enabled, data will double write to both main table and sharding table.
DoubleWrite bool
// ShardingKey specifies the table column you want to used for sharding the table rows.
// For example, for a product order table, you may want to split the rows by `user_id`.
ShardingKey string
// NumberOfShards specifies how many tables you want to sharding.
NumberOfShards uint
// tableFormat specifies the sharding table suffix format.
tableFormat string
// ShardingAlgorithm specifies a function to generate the sharding
// table's suffix by the column value.
// For example, this function implements a mod sharding algorithm.
//
// func(value any) (suffix string, err error) {
// if uid, ok := value.(int64);ok {
// return fmt.Sprintf("_%02d", user_id % 64), nil
// }
// return "", errors.New("invalid user_id")
// }
ShardingAlgorithm func(columnValue any) (suffix string, err error)
// ShardingSuffixs specifies a function to generate all table's suffix.
// Used to support Migrator and generate PrimaryKey.
// For example, this function get a mod all sharding suffixs.
//
// func () (suffixs []string) {
// numberOfShards := 5
// for i := 0; i < numberOfShards; i++ {
// suffixs = append(suffixs, fmt.Sprintf("_%02d", i%numberOfShards))
// }
// return
// }
ShardingSuffixs func() (suffixs []string)
// ShardingAlgorithmByPrimaryKey specifies a function to generate the sharding
// table's suffix by the primary key. Used when no sharding key specified.
// For example, this function use the Snowflake library to generate the suffix.
//
// func(id int64) (suffix string) {
// return fmt.Sprintf("_%02d", snowflake.ParseInt64(id).Node())
// }
ShardingAlgorithmByPrimaryKey func(id int64) (suffix string)
// PrimaryKeyGenerator specifies the primary key generate algorithm.
// Used only when insert and the record does not contains an id field.
// Options are PKSnowflake, PKPGSequence and PKCustom.
// When use PKCustom, you should also specify PrimaryKeyGeneratorFn.
PrimaryKeyGenerator int
// PrimaryKeyGeneratorFn specifies a function to generate the primary key.
// When use auto-increment like generator, the tableIdx argument could ignored.
// For example, this function use the Snowflake library to generate the primary key.
// If you don't want to auto-fill the `id` or use a primary key that isn't called `id`, just return 0.
//
// func(tableIdx int64) int64 {
// return nodes[tableIdx].Generate().Int64()
// }
PrimaryKeyGeneratorFn func(tableIdx int64) int64
}
func Register(config Config, tables ...any) *Sharding {
return &Sharding{
_config: config,
_tables: tables,
}
}
func (s *Sharding) compile() error {
if s.configs == nil {
s.configs = make(map[string]Config)
}
for _, table := range s._tables {
if t, ok := table.(string); ok {
s.configs[t] = s._config
} else {
stmt := &gorm.Statement{DB: s.DB}
if err := stmt.Parse(table); err == nil {
s.configs[stmt.Table] = s._config
} else {
return err
}
}
}
for t, c := range s.configs {
if c.NumberOfShards > 1024 && c.PrimaryKeyGenerator == PKSnowflake {
panic("Snowflake NumberOfShards should less than 1024")
}
if c.PrimaryKeyGenerator == PKSnowflake {
c.PrimaryKeyGeneratorFn = s.genSnowflakeKey
} else if c.PrimaryKeyGenerator == PKPGSequence {
// Execute SQL to CREATE SEQUENCE for this table if not exist
err := s.createPostgreSQLSequenceKeyIfNotExist(t)
if err != nil {
return err
}
c.PrimaryKeyGeneratorFn = func(index int64) int64 {
return s.genPostgreSQLSequenceKey(t, index)
}
} else if c.PrimaryKeyGenerator == PKMySQLSequence {
err := s.createMySQLSequenceKeyIfNotExist(t)
if err != nil {
return err
}
c.PrimaryKeyGeneratorFn = func(index int64) int64 {
return s.genMySQLSequenceKey(t, index)
}
} else if c.PrimaryKeyGenerator == PKCustom {
if c.PrimaryKeyGeneratorFn == nil {
return errors.New("PrimaryKeyGeneratorFn is required when use PKCustom")
}
} else {
return errors.New("PrimaryKeyGenerator can only be one of PKSnowflake, PKPGSequence, PKMySQLSequence and PKCustom")
}
if c.ShardingAlgorithm == nil {
if c.NumberOfShards == 0 {
return errors.New("specify NumberOfShards or ShardingAlgorithm")
}
if c.NumberOfShards < 10 {
c.tableFormat = "_%01d"
} else if c.NumberOfShards < 100 {
c.tableFormat = "_%02d"
} else if c.NumberOfShards < 1000 {
c.tableFormat = "_%03d"
} else if c.NumberOfShards < 10000 {
c.tableFormat = "_%04d"
}
c.ShardingAlgorithm = func(value any) (suffix string, err error) {
id := 0
switch value := value.(type) {
case int:
id = value
case int64:
id = int(value)
case string:
id, err = strconv.Atoi(value)
if err != nil {
id = int(crc32.ChecksumIEEE([]byte(value)))
}
default:
return "", fmt.Errorf("default algorithm only support integer and string column," +
"if you use other type, specify you own ShardingAlgorithm")
}
return fmt.Sprintf(c.tableFormat, id%int(c.NumberOfShards)), nil
}
}
if c.ShardingSuffixs == nil {
c.ShardingSuffixs = func() (suffixs []string) {
for i := 0; i < int(c.NumberOfShards); i++ {
suffix, err := c.ShardingAlgorithm(i)
if err != nil {
return nil
}
suffixs = append(suffixs, suffix)
}
return
}
}
if c.ShardingAlgorithmByPrimaryKey == nil {
if c.PrimaryKeyGenerator == PKSnowflake {
c.ShardingAlgorithmByPrimaryKey = func(id int64) (suffix string) {
return fmt.Sprintf(c.tableFormat, snowflake.ParseInt64(id).Node())
}
}
}
s.configs[t] = c
}
return nil
}
// Name plugin name for Gorm plugin interface
func (s *Sharding) Name() string {
return "gorm:sharding"
}
// LastQuery get last SQL query
func (s *Sharding) LastQuery() string {
if query, ok := s.querys.Load("last_query"); ok {
return query.(string)
}
return ""
}
// Initialize implement for Gorm plugin interface
func (s *Sharding) Initialize(db *gorm.DB) error {
db.Dialector = NewShardingDialector(db.Dialector, s)
s.DB = db
s.registerCallbacks(db)
for t, c := range s.configs {
if c.PrimaryKeyGenerator == PKPGSequence {
err := s.DB.Exec("CREATE SEQUENCE IF NOT EXISTS " + pgSeqName(t)).Error
if err != nil {
return fmt.Errorf("init postgresql sequence error, %w", err)
}
}
if c.PrimaryKeyGenerator == PKMySQLSequence {
err := s.DB.Exec("CREATE TABLE IF NOT EXISTS " + mySQLSeqName(t) + " (id INT NOT NULL)").Error
if err != nil {
return fmt.Errorf("init mysql create sequence error, %w", err)
}
err = s.DB.Exec("INSERT INTO " + mySQLSeqName(t) + " VALUES (0)").Error
if err != nil {
return fmt.Errorf("init mysql insert sequence error, %w", err)
}
}
}
s.snowflakeNodes = make([]*snowflake.Node, 1024)
for i := int64(0); i < 1024; i++ {
n, err := snowflake.NewNode(i)
if err != nil {
return fmt.Errorf("init snowflake node error, %w", err)
}
s.snowflakeNodes[i] = n
}
return s.compile()
}
func (s *Sharding) registerCallbacks(db *gorm.DB) {
s.Callback().Create().Before("*").Register("gorm:sharding", s.switchConn)
s.Callback().Query().Before("*").Register("gorm:sharding", s.switchConn)
s.Callback().Update().Before("*").Register("gorm:sharding", s.switchConn)
s.Callback().Delete().Before("*").Register("gorm:sharding", s.switchConn)
s.Callback().Row().Before("*").Register("gorm:sharding", s.switchConn)
s.Callback().Raw().Before("*").Register("gorm:sharding", s.switchConn)
}
func (s *Sharding) switchConn(db *gorm.DB) {
// Support ignore sharding in some case, like:
// When DoubleWrite is enabled, we need to query database schema
// information by table name during the migration.
if _, ok := db.Get(ShardingIgnoreStoreKey); !ok {
s.mutex.Lock()
if db.Statement.ConnPool != nil {
s.ConnPool = &ConnPool{ConnPool: db.Statement.ConnPool, sharding: s}
db.Statement.ConnPool = s.ConnPool
}
s.mutex.Unlock()
}
}
// resolve split the old query to full table query and sharding table query
func (s *Sharding) resolve(query string, args ...any) (ftQuery, stQuery, tableName string, err error) {
ftQuery = query
stQuery = query
if len(s.configs) == 0 {
return
}
expr, err := sqlparser.NewParser(strings.NewReader(query)).ParseStatement()
if err != nil {
return ftQuery, stQuery, tableName, nil
}
var table *sqlparser.TableName
var condition sqlparser.Expr
var isInsert bool
var insertNames []*sqlparser.Ident
var insertExpressions []*sqlparser.Exprs
var insertStmt *sqlparser.InsertStatement
switch stmt := expr.(type) {
case *sqlparser.SelectStatement:
tbl, ok := stmt.FromItems.(*sqlparser.TableName)
if !ok {
return
}
if stmt.Hint != nil && stmt.Hint.Value == "nosharding" {
return
}
table = tbl
condition = stmt.Condition
case *sqlparser.InsertStatement:
table = stmt.TableName
isInsert = true
insertNames = stmt.ColumnNames
insertExpressions = stmt.Expressions
insertStmt = stmt
case *sqlparser.UpdateStatement:
condition = stmt.Condition
table = stmt.TableName
case *sqlparser.DeleteStatement:
condition = stmt.Condition
table = stmt.TableName
default:
return ftQuery, stQuery, "", sqlparser.ErrNotImplemented
}
tableName = table.Name.Name
r, ok := s.configs[tableName]
if !ok {
return
}
var suffix string
if isInsert {
var newTable *sqlparser.TableName
for _, insertExpression := range insertExpressions {
var value any
var id int64
var keyFind bool
columnNames := insertNames
insertValues := insertExpression.Exprs
value, id, keyFind, err = s.insertValue(r.ShardingKey, insertNames, insertValues, args...)
if err != nil {
return
}
var subSuffix string
subSuffix, err = getSuffix(value, id, keyFind, r)
if err != nil {
return
}
if suffix != "" && suffix != subSuffix {
err = ErrInsertDiffSuffix
return
}
suffix = subSuffix
newTable = &sqlparser.TableName{Name: &sqlparser.Ident{Name: tableName + suffix}}
fillID := true
if isInsert {
for _, name := range insertNames {
if name.Name == "id" {
fillID = false
break
}
}
suffixWord := strings.Replace(suffix, "_", "", 1)
tblIdx, err := strconv.Atoi(suffixWord)
if err != nil {
tblIdx = slices.Index(r.ShardingSuffixs(), suffix)
if tblIdx == -1 {
return ftQuery, stQuery, tableName, errors.New("table suffix '" + suffix + "' is not in ShardingSuffixs. In order to generate the primary key, ShardingSuffixs should include all table suffixes")
}
//return ftQuery, stQuery, tableName, err
}
id := r.PrimaryKeyGeneratorFn(int64(tblIdx))
if id == 0 {
fillID = false
}
if fillID {
columnNames = append(insertNames, &sqlparser.Ident{Name: "id"})
insertValues = append(insertValues, &sqlparser.NumberLit{Value: strconv.FormatInt(id, 10)})
}
}
if fillID {
insertStmt.ColumnNames = columnNames
insertExpression.Exprs = insertValues
}
}
ftQuery = insertStmt.String()
insertStmt.TableName = newTable
stQuery = insertStmt.String()
} else {
var value any
var id int64
var keyFind bool
value, id, keyFind, err = s.nonInsertValue(r.ShardingKey, condition, args...)
if err != nil {
return
}
suffix, err = getSuffix(value, id, keyFind, r)
if err != nil {
return
}
newTable := &sqlparser.TableName{Name: &sqlparser.Ident{Name: tableName + suffix}}
switch stmt := expr.(type) {
case *sqlparser.SelectStatement:
ftQuery = stmt.String()
stmt.FromItems = newTable
stmt.OrderBy = replaceOrderByTableName(stmt.OrderBy, tableName, newTable.Name.Name)
stQuery = stmt.String()
case *sqlparser.UpdateStatement:
ftQuery = stmt.String()
stmt.TableName = newTable
stQuery = stmt.String()
case *sqlparser.DeleteStatement:
ftQuery = stmt.String()
stmt.TableName = newTable
stQuery = stmt.String()
}
}
return
}
func getSuffix(value any, id int64, keyFind bool, r Config) (suffix string, err error) {
if keyFind {
suffix, err = r.ShardingAlgorithm(value)
if err != nil {
return
}
} else {
if r.ShardingAlgorithmByPrimaryKey == nil {
err = fmt.Errorf("there is not sharding key and ShardingAlgorithmByPrimaryKey is not configured")
return
}
suffix = r.ShardingAlgorithmByPrimaryKey(id)
}
return
}
func (s *Sharding) insertValue(key string, names []*sqlparser.Ident, exprs []sqlparser.Expr, args ...any) (value any, id int64, keyFind bool, err error) {
if len(names) != len(exprs) {
return nil, 0, keyFind, errors.New("column names and expressions mismatch")
}
for i, name := range names {
if name.Name == key {
switch expr := exprs[i].(type) {
case *sqlparser.BindExpr:
value = args[expr.Pos]
case *sqlparser.StringLit:
value = expr.Value
case *sqlparser.NumberLit:
value = expr.Value
default:
return nil, 0, keyFind, sqlparser.ErrNotImplemented
}
keyFind = true
break
}
}
if !keyFind {
return nil, 0, keyFind, ErrMissingShardingKey
}
return
}
func (s *Sharding) nonInsertValue(key string, condition sqlparser.Expr, args ...any) (value any, id int64, keyFind bool, err error) {
err = sqlparser.Walk(sqlparser.VisitFunc(func(node sqlparser.Node) error {
if n, ok := node.(*sqlparser.BinaryExpr); ok {
x, ok := n.X.(*sqlparser.Ident)
if !ok {
if q, ok2 := n.X.(*sqlparser.QualifiedRef); ok2 {
x = q.Column
ok = true
}
}
if ok {
if x.Name == key && n.Op == sqlparser.EQ {
keyFind = true
switch expr := n.Y.(type) {
case *sqlparser.BindExpr:
value = args[expr.Pos]
case *sqlparser.StringLit:
value = expr.Value
case *sqlparser.NumberLit:
value = expr.Value
default:
return sqlparser.ErrNotImplemented
}
return nil
} else if x.Name == "id" && n.Op == sqlparser.EQ {
switch expr := n.Y.(type) {
case *sqlparser.BindExpr:
v := args[expr.Pos]
var ok bool
if id, ok = v.(int64); !ok {
return fmt.Errorf("ID should be int64 type")
}
case *sqlparser.NumberLit:
id, err = strconv.ParseInt(expr.Value, 10, 64)
if err != nil {
return err
}
default:
return ErrInvalidID
}
return nil
}
}
}
return nil
}), condition)
if err != nil {
return
}
if !keyFind && id == 0 {
return nil, 0, keyFind, ErrMissingShardingKey
}
return
}
func replaceOrderByTableName(orderBy []*sqlparser.OrderingTerm, oldName, newName string) []*sqlparser.OrderingTerm {
for i, term := range orderBy {
if x, ok := term.X.(*sqlparser.QualifiedRef); ok {
if x.Table.Name == oldName {
x.Table.Name = newName
orderBy[i].X = x
}
}
}
return orderBy
}