Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cluster name check in repair concurrency control #1320

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ static boolean okToRepairSegment(
return allHostsChecked || (allLocalDcHostsChecked && DatacenterAvailability.LOCAL == dcAvailability);
}

@VisibleForTesting
String getClusterName() {
return clusterName;
}

private void registerMetric(String metricName, Gauge<?> gauge) {
if (context.metricRegistry.getMetrics().containsKey(metricName)) {
context.metricRegistry.remove(metricName);
Expand All @@ -220,12 +225,7 @@ public void run() {
Thread.currentThread().setName(clusterName + ":" + repairRunId);
Map<UUID, RepairRunner> currentRunners = context.repairManager.repairRunners;
// We only want the repair runners that are in RUNNING state for the same cluster
List<UUID> repairRunIds
= new ArrayList<UUID>(currentRunners.entrySet().stream()
.filter(entry -> entry.getValue().isRunning())
.filter(entry -> entry.getValue().clusterName == clusterName)
.map(Entry::getKey)
.collect(Collectors.toList()));
List<UUID> repairRunIds = RepairRunner.getRunningRepairRunIds(currentRunners, clusterName);

try {
Optional<RepairRun> repairRun = repairRunDao.getRepairRun(repairRunId);
Expand Down Expand Up @@ -275,14 +275,26 @@ public void run() {
LOG.debug("run() exiting for repair run #{}", repairRunId);
}

@VisibleForTesting
static List<UUID> getRunningRepairRunIds(Map<UUID, RepairRunner> currentRunners, String currentClusterName) {
return new ArrayList<UUID>(currentRunners.entrySet().stream()
.filter(entry -> entry.getValue().isRunning())
.filter(entry -> entry.getValue().getClusterName().equals(currentClusterName))
.map(Entry::getKey)
.collect(Collectors.toList()));
}

@VisibleForTesting
boolean isAllowedToRun(List<UUID> runningRepairRunIds, UUID currentId) {
runningRepairRunIds.sort((id1, id2) -> Long.valueOf(UUIDs.unixTimestamp(id1)).compareTo(UUIDs.unixTimestamp(id2)));
for (int i = 0; i < context.config.getMaxParallelRepairs(); i++) {
LOG.debug("Repair run #{} is in the list of running repair runs in position {}", runningRepairRunIds.get(i), i);
if (runningRepairRunIds.get(i).equals(currentId)) {
LOG.debug("Repair run #{} is allowed to run", currentId);
return true;
}
}
LOG.debug("Repair run #{} is not allowed to run", currentId);
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ public final class RepairRunnerTest {
private Map<String, String> replicas = ImmutableMap.of(
"127.0.0.1", "dc1"
);
private Map<UUID, RepairRunner> currentRunners;

@Before
public void setUp() throws Exception {
SegmentRunner.SEGMENT_RUNNERS.clear();
currentRunners = new HashMap<>();
// Create 3 runners for cluster1 and 1 for cluster2
for (int i = 0; i < 3; i++) {
RepairRunner runner = mock(RepairRunner.class);
when(runner.getClusterName()).thenReturn("cluster1");
when(runner.isRunning()).thenReturn(true);
currentRunners.put(UUID.randomUUID(), runner);
}

RepairRunner runner = mock(RepairRunner.class);
when(runner.getClusterName()).thenReturn("cluster2");
when(runner.isRunning()).thenReturn(true);
currentRunners.put(UUID.randomUUID(), runner);
}

public static Map<List<String>, List<String>> threeNodeCluster() {
Map<List<String>, List<String>> map = new HashMap<List<String>, List<String>>();
Expand Down Expand Up @@ -189,11 +208,6 @@ private static Map<List<String>, List<String>> addRangeToMap(
return map;
}

@Before
public void setUp() throws Exception {
SegmentRunner.SEGMENT_RUNNERS.clear();
}

@After
public void tearDown() {
DateTimeUtils.setCurrentMillisSystem();
Expand Down Expand Up @@ -1893,4 +1907,22 @@ public void isNotAllowedToRunTest() throws ReaperException {

assertFalse(repairRunner.isAllowedToRun(runningRepairs, unallowedRun));
}

@Test
public void testGetRunningRepairRunIds() {
String currentClusterName = "cluster1";
List<UUID> actualIds = RepairRunner.getRunningRepairRunIds(currentRunners, currentClusterName);
assertEquals("Expected 3 running repair run IDs", 3, actualIds.size());

String otherCurrentClusterName = "cluster2";
actualIds = RepairRunner.getRunningRepairRunIds(currentRunners, otherCurrentClusterName);
assertEquals("Expected 1 running repair run IDs", 1, actualIds.size());
}

@Test
public void testGetRunningRepairRunIdsEmpty() {
String currentClusterName = "cluster3";
List<UUID> actualIds = RepairRunner.getRunningRepairRunIds(currentRunners, currentClusterName);
assertEquals("Expected no running repair run ID", 0, actualIds.size());
}
}