-
Notifications
You must be signed in to change notification settings - Fork 82
/
shardingcfg.go
366 lines (344 loc) · 10.9 KB
/
shardingcfg.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
// Copyright 2019 PayPal Inc.
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lib
import (
"context"
"database/sql"
"errors"
"fmt"
"os"
"sync/atomic"
"time"
"github.com/paypal/hera/cal"
"github.com/paypal/hera/utility/logger"
)
// ShardMapRecord is the mapping between a physical bin to a logical shard
type ShardMapRecord struct {
bin int
logical int
flags int
}
// ShardingCfg is an array of 1024 ShardMapRecord
type ShardingCfg struct {
records [1024]*ShardMapRecord
}
var gShardingCfg atomic.Value
// GetShardingCfg atomically get the sharding config
func GetShardingCfg() *ShardingCfg {
cfg := gShardingCfg.Load()
if cfg == nil {
return nil
}
return cfg.(*ShardingCfg)
}
// WLCfg keeps the whitelist configuration
// keys should be int64 or string
type WLCfg struct {
records map[interface{}]*ShardMapRecord
}
var gWLCfg atomic.Value
// GetWLCfg atomically get whitelist config
func GetWLCfg() *WLCfg {
cfg := gWLCfg.Load()
if cfg == nil {
return nil
}
return cfg.(*WLCfg)
}
/*
get the SQL used to read the shard map configuration
*/
func getSQL() string {
// TODO: add hostname in the comment
if len(GetConfig().ShardingPostfix) != 0 {
return fmt.Sprintf("SELECT scuttle_id, shard_id, read_status, write_status from %s_shard_map_%s where status = 'Y'", GetConfig().ManagementTablePrefix, GetConfig().ShardingPostfix)
}
//TODO: is this still needed?
slowf := fmt.Sprintf("slow.%d", os.Getpid())
f, err := os.OpenFile(slowf, os.O_RDONLY, 0)
if err == nil {
buf := make([]byte, 64)
n, err := f.Read(buf[:63])
if err == nil {
buf[n] = 0
sql := fmt.Sprintf("SELECT scuttle_id + usleep(%s) - %s, shard_id, read_status, write_status from %s_shard_map where status = 'Y'", string(buf), string(buf), GetConfig().ManagementTablePrefix)
if logger.GetLogger().V(logger.Warning) {
logger.GetLogger().Log(logger.Warning, "slow shard map query ", sql)
}
return sql
}
}
// if we get here, it means it can't get the slow query
return fmt.Sprintf("SELECT scuttle_id, shard_id, read_status, write_status from %s_shard_map where status = 'Y'", GetConfig().ManagementTablePrefix)
}
/*
load the physical to logical maping
*/
func loadMap(ctx context.Context, db *sql.DB) error {
if logger.GetLogger().V(logger.Verbose) {
logger.GetLogger().Log(logger.Verbose, "Begin loading shard map")
}
if logger.GetLogger().V(logger.Verbose) {
defer func() {
logger.GetLogger().Log(logger.Verbose, "Done loading shard map")
}()
}
conn, err := db.Conn(ctx)
if err != nil {
return fmt.Errorf("Error (conn) loading shard map: %s", err.Error())
}
defer conn.Close()
stmt, err := conn.PrepareContext(ctx, getSQL())
if err != nil {
return fmt.Errorf("Error (stmt) loading shard map: %s", err.Error())
}
rows, err := stmt.QueryContext(ctx)
if err != nil {
return fmt.Errorf("Error (query) loading shard map: %s", err.Error())
}
defer rows.Close()
buckets := GetConfig().MaxScuttleBuckets
shards := GetConfig().NumOfShards
var cfg ShardingCfg
for rows.Next() {
var rstatus, wstatus sql.NullString
var rec ShardMapRecord
err = rows.Scan(&(rec.bin), &(rec.logical), &rstatus, &wstatus)
if err != nil {
return fmt.Errorf("Error (rows) loading shard map: %s", err.Error())
}
if rstatus.Valid && rstatus.String[0] == 'N' {
rec.flags |= 0x0008
}
if wstatus.Valid && wstatus.String[0] == 'N' {
rec.flags |= 0x0002
}
if (rec.bin < 0) || (rec.bin >= buckets) || (cfg.records[rec.bin] != nil) {
if logger.GetLogger().V(logger.Alert) {
logger.GetLogger().Log(logger.Alert, "shard map cfg bin range or doubly set", rec.bin)
}
msg := fmt.Sprintf("bin=%d", rec.bin)
evt := cal.NewCalEvent(cal.EventTypeError, "SHARDMAP_BIN2X", cal.TransOK, msg)
evt.Completed()
continue
}
if (rec.logical < 0) || (rec.logical >= shards) {
if logger.GetLogger().V(logger.Alert) {
logger.GetLogger().Log(logger.Alert, "shard map bad logical for sbucket", rec.bin)
}
msg := fmt.Sprintf("sbucket=%d", rec.bin)
evt := cal.NewCalEvent(cal.EventTypeError, "SHARDMAP_BADLOGICAL", cal.TransOK, msg)
evt.Completed()
rec.flags |= ShardMapRecordFlagsBadLogical
rec.logical = -1
}
cfg.records[rec.bin] = &rec
}
old := GetShardingCfg()
same := (old != nil)
// only log once, the first bucket not configured
//check all are set
for i := 0; i < buckets; i++ {
if cfg.records[i] == nil {
if err == nil {
if logger.GetLogger().V(logger.Alert) {
logger.GetLogger().Log(logger.Alert, "shard map cfg bin not configured", i)
}
msg := fmt.Sprintf("sbucket=%d", i)
evt := cal.NewCalEvent(cal.EventTypeError, "SHARDMAP_BIN0", cal.TransOK, msg)
evt.Completed()
err = fmt.Errorf("Error loading shard map, scuttle %d not configured", i)
}
rec := ShardMapRecord{bin: i, flags: ShardMapRecordFlagsBadLogical, logical: -1}
cfg.records[i] = &rec
}
if same && ((old.records[i].logical != cfg.records[i].logical) || (old.records[i].flags != cfg.records[i].flags)) {
if logger.GetLogger().V(logger.Warning) {
logger.GetLogger().Log(logger.Warning, "shard map updated.", i, "is the first differing scuttle")
}
evt := cal.NewCalEvent(EvtTypeSharding, "shard_map_change", cal.TransOK, fmt.Sprintf("list=[%d,%d]", i, i))
evt.Completed()
same = false
}
}
if !same {
gShardingCfg.Store(&cfg)
if logger.GetLogger().V(logger.Verbose) {
logger.GetLogger().Log(logger.Verbose, "Shard map loaded:", GetConfig().MaxScuttleBuckets, "buckets")
}
}
return err
}
/**
get the SQL used to read the whitelist configuration
*/
func getWLSQL() string {
skCol := "shard_key"
if GetConfig().ShardKeyValueTypeIsString {
skCol = "shard_key_string"
}
if len(GetConfig().ShardingPostfix) != 0 {
return fmt.Sprintf("SELECT %s, shard_id, read_status, write_status FROM %s_whitelist_%s WHERE enable = 'Y'", skCol, GetConfig().ManagementTablePrefix, GetConfig().ShardingPostfix)
}
return fmt.Sprintf("SELECT %s, shard_id, read_status, write_status FROM %s_whitelist WHERE enable = 'Y'", skCol, GetConfig().ManagementTablePrefix)
}
/*
load the whitelist mapping
*/
func loadWhitelist(ctx context.Context, db *sql.DB) {
if logger.GetLogger().V(logger.Verbose) {
logger.GetLogger().Log(logger.Verbose, "Begin loading whitelist")
}
if logger.GetLogger().V(logger.Verbose) {
defer func() {
logger.GetLogger().Log(logger.Verbose, "Done loading whitelist")
}()
}
conn, err := db.Conn(ctx)
if err != nil {
logger.GetLogger().Log(logger.Alert, "Error (conn) loading whitelist:", err)
return
}
defer conn.Close()
stmt, err := conn.PrepareContext(ctx, getWLSQL())
if err != nil {
logger.GetLogger().Log(logger.Alert, "Error (stmt) loading whitelist:", err)
return
}
rows, err := stmt.QueryContext(ctx)
if err != nil {
logger.GetLogger().Log(logger.Alert, "Error (query) loading whitelist:", err)
return
}
defer rows.Close()
cfg := WLCfg{records: make(map[interface{}]*ShardMapRecord)}
for rows.Next() {
var shardKey uint64
var shardKeyStr string
var rstatus, wstatus sql.NullString
var rec ShardMapRecord
if GetConfig().ShardKeyValueTypeIsString {
err = rows.Scan(&shardKeyStr, &(rec.logical), &rstatus, &wstatus)
} else {
err = rows.Scan(&shardKey, &(rec.logical), &rstatus, &wstatus)
}
if err != nil {
logger.GetLogger().Log(logger.Alert, "Error (rows) loading whitelist:", err)
return
}
if rstatus.Valid && rstatus.String[0] == 'N' {
rec.flags |= 0x0008
}
if wstatus.Valid && wstatus.String[0] == 'N' {
rec.flags |= 0x0002
}
if GetConfig().ShardKeyValueTypeIsString {
cfg.records[shardKeyStr] = &rec
} else {
cfg.records[shardKey] = &rec
}
}
gWLCfg.Store(&cfg)
}
// initialize the golang's database/sql object used to read the database configuration. The connection is created using the loopdriver,
// a sql driver used internally for ease of programming: the config load routines use standard database/sql interface.
func openDb(shard int) (*sql.DB, error) {
db, err := sql.Open("heraloop", fmt.Sprintf("%d:0:0", shard))
if err != nil {
return nil, err
}
db.SetMaxIdleConns(0)
return db, nil
}
// InitShardingCfg initializes the sharding config. If sharding and using shard map is enabled InitShardingCfg runs a go-routine which loads shard map configuration periodically
func InitShardingCfg() error {
if GetConfig().UseShardMap {
ctx := context.Background()
var db *sql.DB
var err error
i := 0
for ; i < 60; i++ {
for shard := 0; shard < GetConfig().NumOfShards; shard++ {
if db != nil {
db.Close()
}
db, err = openDb(shard)
if err == nil {
err = loadMap(ctx, db)
if err == nil {
break
}
}
logger.GetLogger().Log(logger.Warning, "Error <", err, "> loading the shard map from shard", shard)
evt := cal.NewCalEvent(cal.EventTypeError, "no_shard_map", cal.TransOK, "Error loading shard map")
evt.Completed()
}
if err == nil {
break
}
logger.GetLogger().Log(logger.Warning, "Error loading the shard map, retry in one second")
time.Sleep(time.Second)
}
if i == 60 {
return errors.New("Failed to load shard map, no more retry")
}
if GetConfig().EnableWhitelistTest {
loadWhitelist(ctx, db)
}
go func() {
for {
reloadInterval := time.Second * time.Duration(GetConfig().ShardingCfgReloadInterval)
if reloadInterval < 100 * time.Millisecond {
reloadInterval = 100 * time.Millisecond
}
time.Sleep(reloadInterval)
for shard := 0; shard < GetConfig().NumOfShards; shard++ {
if db != nil {
db.Close()
}
db, err = openDb(shard)
if err == nil {
err = loadMap(ctx, db)
if err == nil {
if shard == 0 && GetConfig().EnableWhitelistTest {
loadWhitelist(ctx, db)
}
break
}
}
logger.GetLogger().Log(logger.Warning, "Error <", err, "> loading the shard map from shard", shard)
evt := cal.NewCalEvent(cal.EventTypeError, "no_shard_map", cal.TransOK, "Error loading shard map")
evt.Completed()
}
}
}()
} else {
var cfg ShardingCfg
max := GetConfig().MaxScuttleBuckets
for i := 0; i < max; i++ {
var rec ShardMapRecord
rec.bin = i
cfg.records[i] = &rec
}
gShardingCfg.Store(&cfg)
if GetConfig().EnableWhitelistTest {
cfg := WLCfg{records: make(map[interface{}]*ShardMapRecord)}
gWLCfg.Store(&cfg)
}
}
return nil
}