Skip to content

Commit c0ee3e1

Browse files
authored
CNDB-15160: add counter to track num of skipped compaction aggregates due to insufficient disk space (#1962)
### What is the issue #15160 ### What does this PR fix and why was it fixed count num of skipped compaction aggregates due to insufficient disk space
1 parent caa2c03 commit c0ee3e1

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

src/java/org/apache/cassandra/db/compaction/AbstractCompactionStrategy.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ protected BackgroundCompactions getBackgroundCompactions()
317317
return backgroundCompactions;
318318
}
319319

320+
public long getSkippedAggregatesDueToDiskSpace()
321+
{
322+
return backgroundCompactions.getSkippedAggregatesDueToDiskSpace();
323+
}
324+
320325
public static Map<String, String> validateOptions(Map<String, String> options) throws ConfigurationException
321326
{
322327
return CompactionStrategyOptions.validateOptions(options);

src/java/org/apache/cassandra/db/compaction/BackgroundCompactions.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.TreeMap;
2525
import java.util.UUID;
2626
import java.util.concurrent.ConcurrentHashMap;
27+
import java.util.concurrent.atomic.AtomicLong;
2728

2829
import com.google.common.collect.ImmutableList;
2930
import org.slf4j.Logger;
@@ -73,6 +74,11 @@ public class BackgroundCompactions
7374
*/
7475
MovingAverage compactionRate = ExpMovingAverage.decayBy1000();
7576

77+
/**
78+
* Track num of skipped compaction aggregates due to insufficient disk space
79+
*/
80+
private final AtomicLong skippedAggregatesDueToDiskSpace = new AtomicLong(0);
81+
7682
BackgroundCompactions(CompactionRealm realm)
7783
{
7884
this.metadata = realm.metadata();
@@ -283,6 +289,16 @@ private void updateCompactionRate(CompactionProgress progress)
283289
}
284290
}
285291

292+
public void incrementSkippedAggregatesDueToDiskSpace()
293+
{
294+
skippedAggregatesDueToDiskSpace.incrementAndGet();
295+
}
296+
297+
public long getSkippedAggregatesDueToDiskSpace()
298+
{
299+
return skippedAggregatesDueToDiskSpace.get();
300+
}
301+
286302
public Collection<CompactionAggregate> getAggregates()
287303
{
288304
return aggregates;

src/java/org/apache/cassandra/db/compaction/UnifiedCompactionStrategy.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,10 @@ List<CompactionAggregate> getSelection(List<CompactionAggregate.UnifiedAggregate
10901090
++proposed;
10911091
long overheadSizeInBytes = pick.totalOverheadInBytes();
10921092
if (overheadSizeInBytes > spaceAvailable)
1093+
{
1094+
getBackgroundCompactions().incrementSkippedAggregatesDueToDiskSpace();
10931095
continue; // compaction is too large for current cycle
1096+
}
10941097

10951098
int currentLevel = levelOf(pick);
10961099
boolean isAdaptive = controller.isRecentAdaptive(pick);

test/unit/org/apache/cassandra/db/compaction/UnifiedCompactionStrategyGetSelectionTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ public void testGetSelection(List<CompactionAggregate.UnifiedAggregate> compacti
190190
when(strategy.getController()).thenReturn(controller);
191191
when(strategy.getShardingStats(any())).thenReturn(stats);
192192
when(strategy.getSelection(any(), anyInt(), any(), anyLong(), anyInt())).thenCallRealMethod();
193+
when(strategy.getBackgroundCompactions()).thenReturn(Mockito.mock(BackgroundCompactions.class));
193194

194195
int[] perLevel = new int[levelCount];
195196
int maxReservations = totalCount / levelCount;

test/unit/org/apache/cassandra/db/compaction/UnifiedCompactionStrategyTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.ArrayList;
2121
import java.util.Arrays;
2222
import java.util.Collection;
23+
import java.util.Collections;
2324
import java.util.Comparator;
2425
import java.util.HashMap;
2526
import java.util.HashSet;
@@ -2198,4 +2199,47 @@ public void testGetLevel()
21982199
assertEquals(0.25d, level.min, 0);
21992200
assertEquals(0.5d, level.max, 0);
22002201
}
2202+
2203+
@Test
2204+
public void testSkippedAggregatesOnInsufficientDiskSpace()
2205+
{
2206+
long overheadSizeInBytes = 1000L;
2207+
long insufficientSpaceAvailable = 500L;
2208+
2209+
BackgroundCompactions backgroundCompactions = Mockito.mock(BackgroundCompactions.class);
2210+
Controller controller = Mockito.mock(Controller.class, Mockito.withSettings().stubOnly());
2211+
2212+
when(controller.prioritize(anyList())).thenCallRealMethod();
2213+
when(controller.getReservedThreads()).thenReturn(0);
2214+
when(controller.getReservationsType()).thenReturn(Reservations.Type.PER_LEVEL);
2215+
when(controller.getOverheadSizeInBytes(any(), anyLong())).thenReturn(overheadSizeInBytes);
2216+
when(controller.isRecentAdaptive(any())).thenReturn(false);
2217+
when(controller.overlapInclusionMethod()).thenReturn(Overlaps.InclusionMethod.TRANSITIVE);
2218+
when(controller.parallelizeOutputShards()).thenReturn(false);
2219+
2220+
CompactionSSTable mockSSTable = Mockito.mock(CompactionSSTable.class);
2221+
CompactionPick pick = CompactionPick.create(UUID.randomUUID(),
2222+
0,
2223+
ImmutableList.of(mockSSTable),
2224+
Collections.emptySet(),
2225+
1,
2226+
overheadSizeInBytes,
2227+
overheadSizeInBytes,
2228+
overheadSizeInBytes);
2229+
2230+
CompactionAggregate.UnifiedAggregate aggregate = Mockito.mock(CompactionAggregate.UnifiedAggregate.class, Mockito.withSettings().stubOnly());
2231+
when(aggregate.getSelected()).thenReturn(pick);
2232+
when(aggregate.maxOverlap()).thenReturn(0);
2233+
2234+
UnifiedCompactionStrategy strategy = new UnifiedCompactionStrategy(strategyFactory, backgroundCompactions, controller);
2235+
List<CompactionAggregate.UnifiedAggregate> pending = Arrays.asList(aggregate);
2236+
int[] perLevel = new int[1];
2237+
2238+
List<CompactionAggregate> result = strategy.getSelection(pending, 1, perLevel, insufficientSpaceAvailable, 0);
2239+
assertEquals("No compactions should be selected when insufficient disk space", 0, result.size());
2240+
Mockito.verify(backgroundCompactions, Mockito.times(1)).incrementSkippedAggregatesDueToDiskSpace();
2241+
2242+
Mockito.when(backgroundCompactions.getSkippedAggregatesDueToDiskSpace()).thenReturn(1L);
2243+
assertThat(strategy.getSkippedAggregatesDueToDiskSpace()).isEqualTo(1);
2244+
}
22012245
}

0 commit comments

Comments
 (0)