32
32
import java .util .regex .Matcher ;
33
33
import java .util .regex .Pattern ;
34
34
35
+ import javax .annotation .Nullable ;
36
+
35
37
import com .google .common .annotations .VisibleForTesting ;
36
38
import com .google .common .base .Preconditions ;
37
39
import com .google .common .collect .Iterables ;
@@ -342,7 +344,7 @@ public synchronized Collection<AbstractCompactionTask> getNextBackgroundTasks(in
342
344
if (expirationTasks != null )
343
345
return expirationTasks ;
344
346
345
- return getNextBackgroundTasks (getNextCompactionAggregates (), gcBefore , this );
347
+ return getNextBackgroundTasks (getNextCompactionAggregates (), gcBefore , null );
346
348
}
347
349
348
350
/// Check for fully expired sstables and return a collection of expiration tasks if found.
@@ -403,20 +405,20 @@ public Set<CompactionSSTable> getFullyExpiredSSTables(int gcBefore)
403
405
/// Used by CNDB where compaction aggregates come from etcd rather than the strategy.
404
406
/// @return collection of `AbstractCompactionTask`, which could be either a `CompactionTask` or a `UnifiedCompactionTask`
405
407
public synchronized Collection <AbstractCompactionTask > getNextBackgroundTasks (Collection <CompactionAggregate > aggregates , int gcBefore ,
406
- CompactionObserver compositeCompactionObserver )
408
+ @ Nullable CompactionObserver additionalObserver )
407
409
{
408
410
controller .onStrategyBackgroundTaskRequest ();
409
- return createCompactionTasks (aggregates , gcBefore , compositeCompactionObserver );
411
+ return createCompactionTasks (aggregates , gcBefore , additionalObserver );
410
412
}
411
413
412
414
private Collection <AbstractCompactionTask > createCompactionTasks (Collection <CompactionAggregate > aggregates , int gcBefore ,
413
- CompactionObserver compositeCompactionObserver )
415
+ @ Nullable CompactionObserver additionalObserver )
414
416
{
415
417
Collection <AbstractCompactionTask > tasks = new ArrayList <>(aggregates .size ());
416
418
try
417
419
{
418
420
for (CompactionAggregate aggregate : aggregates )
419
- createAndAddTasks (gcBefore , (CompactionAggregate .UnifiedAggregate ) aggregate , tasks , compositeCompactionObserver );
421
+ createAndAddTasks (gcBefore , (CompactionAggregate .UnifiedAggregate ) aggregate , tasks , additionalObserver );
420
422
421
423
return tasks ;
422
424
}
@@ -428,7 +430,7 @@ private Collection<AbstractCompactionTask> createCompactionTasks(Collection<Comp
428
430
429
431
/// Create compaction tasks for the given aggregate and add them to the given tasks list.
430
432
public void createAndAddTasks (int gcBefore , CompactionAggregate .UnifiedAggregate aggregate ,
431
- Collection <? super UnifiedCompactionTask > tasks , CompactionObserver compositeCompactionObserver )
433
+ Collection <? super UnifiedCompactionTask > tasks , @ Nullable CompactionObserver additionalObserver )
432
434
{
433
435
CompactionPick selected = aggregate .getSelected ();
434
436
int parallelism = aggregate .getPermittedParallelism ();
@@ -442,7 +444,7 @@ public void createAndAddTasks(int gcBefore, CompactionAggregate.UnifiedAggregate
442
444
{
443
445
// This will ignore the range of the operation, which is fine.
444
446
backgroundCompactions .setSubmitted (this , transaction .opId (), aggregate );
445
- createAndAddTasks (gcBefore , transaction , aggregate .operationRange (), aggregate .keepOriginals (), getShardingStats (aggregate ), parallelism , tasks , compositeCompactionObserver );
447
+ createAndAddTasks (gcBefore , transaction , aggregate .operationRange (), aggregate .keepOriginals (), getShardingStats (aggregate ), parallelism , tasks , additionalObserver );
446
448
}
447
449
else
448
450
{
@@ -451,6 +453,12 @@ public void createAndAddTasks(int gcBefore, CompactionAggregate.UnifiedAggregate
451
453
}
452
454
}
453
455
456
+ /// Return the num of in-progress compactions tracked by UCS
457
+ public int getCompactionInProgress ()
458
+ {
459
+ return backgroundCompactions .getCompactionsInProgress ().size ();
460
+ }
461
+
454
462
private static RuntimeException rejectTasks (Iterable <? extends AbstractCompactionTask > tasks , Throwable error )
455
463
{
456
464
for (var task : tasks )
@@ -614,7 +622,7 @@ void createAndAddTasks(int gcBefore,
614
622
int parallelism ,
615
623
Collection <? super CompactionTask > tasks )
616
624
{
617
- createAndAddTasks (gcBefore , transaction , null , false , shardingStats , parallelism , tasks , this );
625
+ createAndAddTasks (gcBefore , transaction , null , false , shardingStats , parallelism , tasks , null );
618
626
}
619
627
620
628
@ VisibleForTesting
@@ -625,12 +633,12 @@ void createAndAddTasks(int gcBefore,
625
633
ShardingStats shardingStats ,
626
634
int parallelism ,
627
635
Collection <? super UnifiedCompactionTask > tasks ,
628
- CompactionObserver compositeCompactionObserver )
636
+ @ Nullable CompactionObserver additionalObserver )
629
637
{
630
638
if (controller .parallelizeOutputShards () && parallelism > 1 )
631
- tasks .addAll (createParallelCompactionTasks (transaction , operationRange , keepOriginals , shardingStats , gcBefore , parallelism , compositeCompactionObserver ));
639
+ tasks .addAll (createParallelCompactionTasks (transaction , operationRange , keepOriginals , shardingStats , gcBefore , parallelism , additionalObserver ));
632
640
else
633
- tasks .add (createCompactionTask (transaction , operationRange , keepOriginals , shardingStats , gcBefore ));
641
+ tasks .add (createCompactionTask (transaction , operationRange , keepOriginals , shardingStats , gcBefore , additionalObserver ));
634
642
}
635
643
636
644
/// Create the sstable writer used for flushing.
@@ -677,9 +685,13 @@ private UnifiedCompactionTask createCompactionTask(LifecycleTransaction transact
677
685
/// where we produce outputs but cannot delete the input sstables until all components of the operation are complete.
678
686
///
679
687
/// @return a sharded compaction task that in turn will create a sharded compaction writer.
680
- private UnifiedCompactionTask createCompactionTask (LifecycleTransaction transaction , Range <Token > operationRange , boolean keepOriginals , ShardingStats shardingStats , int gcBefore )
688
+ private UnifiedCompactionTask createCompactionTask (LifecycleTransaction transaction , Range <Token > operationRange , boolean keepOriginals , ShardingStats shardingStats , int gcBefore ,
689
+ @ Nullable CompactionObserver additionalObserver )
681
690
{
682
- return new UnifiedCompactionTask (realm , this , transaction , gcBefore , keepOriginals , getShardManager (), shardingStats , operationRange , transaction .originals (), null , null , null );
691
+ UnifiedCompactionTask task = new UnifiedCompactionTask (realm , this , transaction , gcBefore , keepOriginals , getShardManager (), shardingStats , operationRange , transaction .originals (), null , null , null );
692
+ if (additionalObserver != null )
693
+ task .addObserver (additionalObserver );
694
+ return task ;
683
695
}
684
696
685
697
@ Override
@@ -701,7 +713,7 @@ private Collection<UnifiedCompactionTask> createParallelCompactionTasks(Lifecycl
701
713
ShardingStats shardingStats ,
702
714
int gcBefore ,
703
715
int parallelism ,
704
- CompactionObserver compositeCompactionObserver )
716
+ @ Nullable CompactionObserver additionalObserver )
705
717
{
706
718
final int coveredShardCount = shardingStats .coveredShardCount ;
707
719
assert parallelism > 1 ;
@@ -710,7 +722,8 @@ private Collection<UnifiedCompactionTask> createParallelCompactionTasks(Lifecycl
710
722
ShardManager shardManager = getShardManager ();
711
723
CompositeLifecycleTransaction compositeTransaction = new CompositeLifecycleTransaction (transaction );
712
724
SharedCompactionProgress sharedProgress = new SharedCompactionProgress (transaction .opId (), transaction .opType (), TableOperation .Unit .BYTES );
713
- SharedCompactionObserver sharedObserver = new SharedCompactionObserver (compositeCompactionObserver );
725
+ SharedCompactionObserver sharedObserver = new SharedCompactionObserver (this , additionalObserver );
726
+
714
727
SharedTableOperation sharedOperation = new SharedTableOperation (sharedProgress );
715
728
List <UnifiedCompactionTask > tasks = shardManager .splitSSTablesInShardsLimited (
716
729
sstables ,
@@ -741,7 +754,7 @@ private Collection<UnifiedCompactionTask> createParallelCompactionTasks(Lifecycl
741
754
if (tasks .size () == 1 ) // if there's just one range, make it a non-ranged task (to apply early open etc.)
742
755
{
743
756
assert tasks .get (0 ).inputSSTables ().equals (sstables );
744
- return Collections .singletonList (createCompactionTask (transaction , operationRange , keepOriginals , shardingStats , gcBefore ));
757
+ return Collections .singletonList (createCompactionTask (transaction , operationRange , keepOriginals , shardingStats , gcBefore , additionalObserver ));
745
758
}
746
759
else
747
760
return tasks ;
0 commit comments