@@ -98,11 +98,7 @@ func (local *local) SplitAndScatterRegionByRanges(
98
98
if len (ranges ) == 0 {
99
99
return nil
100
100
}
101
-
102
- db , err := local .g .GetDB ()
103
- if err != nil {
104
- return errors .Trace (err )
105
- }
101
+ var err error
106
102
107
103
minKey := codec .EncodeBytes ([]byte {}, ranges [0 ].start )
108
104
maxKey := codec .EncodeBytes ([]byte {}, ranges [len (ranges )- 1 ].end )
@@ -111,13 +107,16 @@ func (local *local) SplitAndScatterRegionByRanges(
111
107
var retryKeys [][]byte
112
108
waitTime := splitRegionBaseBackOffTime
113
109
skippedKeys := 0
110
+ var skipped_scatter bool
111
+ skipped_scatter = false
114
112
for i := 0 ; i < splitRetryTimes ; i ++ {
115
113
log .FromContext (ctx ).Info ("split and scatter region" ,
116
114
logutil .Key ("minKey" , minKey ),
117
115
logutil .Key ("maxKey" , maxKey ),
118
116
zap .Int ("retry" , i ),
119
117
)
120
118
err = nil
119
+ // wait time
121
120
if i > 0 {
122
121
select {
123
122
case <- time .After (waitTime ):
@@ -130,6 +129,7 @@ func (local *local) SplitAndScatterRegionByRanges(
130
129
}
131
130
}
132
131
var regions []* split.RegionInfo
132
+ //scan all regions
133
133
regions , err = split .PaginateScanRegion (ctx , local .splitCli , minKey , maxKey , 128 )
134
134
log .FromContext (ctx ).Info ("paginate scan regions" , zap .Int ("count" , len (regions )),
135
135
logutil .Key ("start" , minKey ), logutil .Key ("end" , maxKey ))
@@ -147,6 +147,25 @@ func (local *local) SplitAndScatterRegionByRanges(
147
147
break
148
148
}
149
149
150
+ if ! skipped_scatter {
151
+ log .FromContext (ctx ).Info ("scattering initial scan region" , zap .Int ("regions" , len (regions )))
152
+ for _ , region := range regions {
153
+ local .ScatterRegions (ctx , region )
154
+ }
155
+ scatterCount , err := local .waitForScatterRegions (ctx , regions )
156
+ if scatterCount == len (regions ) {
157
+ log .FromContext (ctx ).Info ("waiting for scattering initial regions done" ,
158
+ zap .Int ("regions" , len (regions )))
159
+ } else {
160
+ log .FromContext (ctx ).Warn ("waiting for scattering initial regions timeout" ,
161
+ zap .Int ("skipped_keys" , skippedKeys ),
162
+ zap .Int ("scatterCount" , scatterCount ),
163
+ zap .Int ("regions" , len (regions )),
164
+ zap .Error (err ))
165
+ }
166
+ skipped_scatter = true
167
+ }
168
+
150
169
needSplitRanges := make ([]Range , 0 , len (ranges ))
151
170
startKey := make ([]byte , 0 )
152
171
endKey := make ([]byte , 0 )
@@ -172,16 +191,6 @@ func (local *local) SplitAndScatterRegionByRanges(
172
191
return nil
173
192
}
174
193
175
- var tableRegionStats map [uint64 ]int64
176
- if tableInfo != nil {
177
- tableRegionStats , err = fetchTableRegionSizeStats (ctx , db , tableInfo .ID )
178
- if err != nil {
179
- log .FromContext (ctx ).Warn ("fetch table region size statistics failed" ,
180
- zap .String ("table" , tableInfo .Name ), zap .Error (err ))
181
- tableRegionStats , err = make (map [uint64 ]int64 ), nil
182
- }
183
- }
184
-
185
194
regionMap := make (map [uint64 ]* split.RegionInfo )
186
195
for _ , region := range regions {
187
196
regionMap [region .Region .GetId ()] = region
@@ -291,15 +300,6 @@ func (local *local) SplitAndScatterRegionByRanges(
291
300
}
292
301
sendLoop:
293
302
for regionID , keys := range splitKeyMap {
294
- // if region not in tableRegionStats, that means this region is newly split, so
295
- // we can skip split it again.
296
- regionSize , ok := tableRegionStats [regionID ]
297
- if ! ok {
298
- log .FromContext (ctx ).Warn ("region stats not found" , zap .Uint64 ("region" , regionID ))
299
- }
300
- if len (keys ) == 1 && regionSize < regionSplitSize {
301
- skippedKeys ++
302
- }
303
303
select {
304
304
case ch <- & splitInfo {region : regionMap [regionID ], keys : keys }:
305
305
case <- ctx .Done ():
@@ -335,10 +335,9 @@ func (local *local) SplitAndScatterRegionByRanges(
335
335
scatterCount , err := local .waitForScatterRegions (ctx , scatterRegions )
336
336
if scatterCount == len (scatterRegions ) {
337
337
log .FromContext (ctx ).Info ("waiting for scattering regions done" ,
338
- zap .Int ("skipped_keys" , skippedKeys ),
339
338
zap .Int ("regions" , len (scatterRegions )), zap .Duration ("take" , time .Since (startTime )))
340
339
} else {
341
- log .FromContext (ctx ).Info ("waiting for scattering regions timeout" ,
340
+ log .FromContext (ctx ).Warn ("waiting for scattering regions timeout" ,
342
341
zap .Int ("skipped_keys" , skippedKeys ),
343
342
zap .Int ("scatterCount" , scatterCount ),
344
343
zap .Int ("regions" , len (scatterRegions )),
@@ -380,6 +379,28 @@ func fetchTableRegionSizeStats(ctx context.Context, db *sql.DB, tableID int64) (
380
379
return stats , errors .Trace (err )
381
380
}
382
381
382
+ func (local * local ) ScatterRegions (ctx context.Context , regionInfo * split.RegionInfo ) error {
383
+ var err error
384
+ waitTime := time .Second
385
+ for i := 0 ; i < 10 ; i ++ {
386
+ myArray := []* split.RegionInfo {regionInfo }
387
+ if err = local .splitCli .ScatterRegions (ctx , myArray ); err != nil {
388
+ select {
389
+ case <- time .After (waitTime ):
390
+ case <- ctx .Done ():
391
+ log .FromContext (ctx ).Warn ("scatter region failed" , zap .Error (ctx .Err ()), zap .Int ("retry" , i ))
392
+ return ctx .Err ()
393
+ }
394
+ } else {
395
+ break
396
+ }
397
+ }
398
+ if err != nil {
399
+ log .FromContext (ctx ).Warn ("scatter region failed" , zap .Error (err ))
400
+ }
401
+ return err
402
+ }
403
+
383
404
func (local * local ) BatchSplitRegions (
384
405
ctx context.Context ,
385
406
region * split.RegionInfo ,
@@ -400,7 +421,7 @@ func (local *local) BatchSplitRegions(
400
421
for _ , region := range scatterRegions {
401
422
// Wait for a while until the regions successfully splits.
402
423
local .waitForSplit (ctx , region .Region .Id )
403
- if err = local .splitCli . ScatterRegion (ctx , region ); err != nil {
424
+ if err = local .ScatterRegions (ctx , region ); err != nil {
404
425
failedErr = err
405
426
retryRegions = append (retryRegions , region )
406
427
}
@@ -410,7 +431,7 @@ func (local *local) BatchSplitRegions(
410
431
}
411
432
// the scatter operation likely fails because region replicate not finish yet
412
433
// pack them to one log to avoid printing a lot warn logs.
413
- log .FromContext (ctx ).Warn ("scatter region failed" , zap .Int ("regionCount" , len (newRegions )),
434
+ log .FromContext (ctx ).Warn ("scatter region failed" , zap .Int ("regionCount" , len (scatterRegions )),
414
435
zap .Int ("failedCount" , len (retryRegions )), zap .Error (failedErr ), zap .Int ("retry" , i ))
415
436
scatterRegions = retryRegions
416
437
retryRegions = make ([]* split.RegionInfo , 0 )
@@ -509,7 +530,7 @@ func (local *local) checkRegionScatteredOrReScatter(ctx context.Context, regionI
509
530
default :
510
531
log .FromContext (ctx ).Debug ("scatter-region operator status is abnormal, will scatter region again" ,
511
532
logutil .Region (regionInfo .Region ), zap .Stringer ("status" , resp .GetStatus ()))
512
- return false , local .splitCli . ScatterRegion (ctx , regionInfo )
533
+ return false , local .ScatterRegions (ctx , regionInfo )
513
534
}
514
535
}
515
536
@@ -618,18 +639,10 @@ type storeWriteLimiter struct {
618
639
}
619
640
620
641
func newStoreWriteLimiter (limit int ) * storeWriteLimiter {
621
- var burst int
622
- // Allow burst of at most 20% of the limit.
623
- if limit <= math .MaxInt - limit / 5 {
624
- burst = limit + limit / 5
625
- } else {
626
- // If overflowed, set burst to math.MaxInt.
627
- burst = math .MaxInt
628
- }
629
642
return & storeWriteLimiter {
630
643
limiters : make (map [uint64 ]* rate.Limiter ),
631
644
limit : limit ,
632
- burst : burst ,
645
+ burst : limit ,
633
646
}
634
647
}
635
648
0 commit comments