@@ -426,9 +426,6 @@ func (s *levelsController) runCompactor(id int, lc *z.Closer) {
426
426
return
427
427
}
428
428
429
- ticker := time .NewTicker (50 * time .Millisecond )
430
- defer ticker .Stop ()
431
-
432
429
moveL0toFront := func (prios []compactionPriority ) []compactionPriority {
433
430
idx := - 1
434
431
for i , p := range prios {
@@ -474,10 +471,21 @@ func (s *levelsController) runCompactor(id int, lc *z.Closer) {
474
471
return false
475
472
}
476
473
474
+ ticker := time .NewTicker (50 * time .Millisecond )
475
+ defer ticker .Stop ()
476
+ var backOff int
477
477
for {
478
478
select {
479
479
// Can add a done channel or other stuff.
480
480
case <- ticker .C :
481
+ if z .NumAllocBytes () > 16 << 30 {
482
+ // Back off. We're already using a lot of memory.
483
+ backOff ++
484
+ if backOff % 1000 == 0 {
485
+ s .kv .opt .Infof ("Compaction backed off %d times\n " , backOff )
486
+ }
487
+ break
488
+ }
481
489
runOnce ()
482
490
case <- lc .HasBeenClosed ():
483
491
return
@@ -982,8 +990,16 @@ type compactDef struct {
982
990
func (s * levelsController ) addSplits (cd * compactDef ) {
983
991
cd .splits = cd .splits [:0 ]
984
992
985
- // Pick one every 3 tables.
986
- const N = 3
993
+ // Let's say we have 10 tables in cd.bot and min width = 3. Then, we'll pick
994
+ // 0, 1, 2 (pick), 3, 4, 5 (pick), 6, 7, 8 (pick), 9 (pick, because last table).
995
+ // This gives us 4 picks for 10 tables.
996
+ // In an edge case, 142 tables in bottom led to 48 splits. That's too many splits, because it
997
+ // then uses up a lot of memory for table builder.
998
+ // We should keep it so we have at max 5 splits.
999
+ width := int (math .Ceil (float64 (len (cd .bot ) / 5.0 )))
1000
+ if width < 3 {
1001
+ width = 3
1002
+ }
987
1003
skr := cd .thisRange
988
1004
skr .extend (cd .nextRange )
989
1005
@@ -1000,7 +1016,7 @@ func (s *levelsController) addSplits(cd *compactDef) {
1000
1016
addRange ([]byte {})
1001
1017
return
1002
1018
}
1003
- if i % N == N - 1 {
1019
+ if i % width == width - 1 {
1004
1020
// Right should always have ts=maxUint64 otherwise we'll lose keys
1005
1021
// in subcompaction. Consider the following.
1006
1022
// Top table is [A1...C3(deleted)]
0 commit comments