Skip to content

Commit

Permalink
[PLAT-16026] fix hasActiveXClusterConfig condition
Browse files Browse the repository at this point in the history
Summary: change the condition to account for possible inconsistent DR config metadata not having an active XCluster config (e.g. after a failed switchover)

Test Plan: populate some fake data (a dr_config and an xcluster_config with secondary='t'), issue a list customer config request.

Reviewers: cwang, daniel, spothuraju

Reviewed By: cwang, daniel, spothuraju

Subscribers: yugaware

Differential Revision: https://phorge.dev.yugabyte.com/D39773
  • Loading branch information
artem-mindrov committed Nov 6, 2024
1 parent 50c512e commit 49fe314
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
15 changes: 3 additions & 12 deletions managed/src/main/java/com/yugabyte/yw/models/DrConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,7 @@ public XClusterConfig getActiveXClusterConfig() {
"DrConfig %s(%s) does not have any corresponding xCluster config",
this.name, this.uuid));
}
if (xClusterConfigs.size() == 1) {
return xClusterConfigs.get(0);
}

return xClusterConfigs.stream()
.filter(xClusterConfig -> !xClusterConfig.isSecondary())
.findFirst()
Expand All @@ -185,15 +183,8 @@ public boolean hasActiveXClusterConfig() {
if (xClusterConfigs.isEmpty()) {
return false;
}
if (xClusterConfigs.size() == 1) {
return true;
}
for (XClusterConfig xClusterConfig : xClusterConfigs) {
if (!xClusterConfig.isSecondary()) {
return true;
}
}
return false;

return xClusterConfigs.stream().anyMatch(config -> !config.isSecondary());
}

@JsonIgnore
Expand Down
26 changes: 14 additions & 12 deletions managed/src/test/java/com/yugabyte/yw/models/DrConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

import static com.yugabyte.yw.common.ModelFactory.createUniverse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.fasterxml.jackson.databind.JsonNode;
import com.yugabyte.yw.common.FakeDBApplication;
Expand Down Expand Up @@ -138,8 +139,8 @@ public void testDrConfigNoXClusterConfigAttached() {
new PitrParams(),
sourceDbId1);
drConfig.setXClusterConfigs(new ArrayList<>());
assertEquals(false, drConfig.hasActiveXClusterConfig());
assertThrows(IllegalStateException.class, () -> drConfig.getActiveXClusterConfig());
assertFalse(drConfig.hasActiveXClusterConfig());
assertThrows(IllegalStateException.class, drConfig::getActiveXClusterConfig);
}

@Test
Expand All @@ -153,18 +154,19 @@ public void testDrConfigHasExactlyOneXClusterConfig() {
backupRequestParams,
new PitrParams(),
sourceDbId1);
assertEquals(true, drConfig.hasActiveXClusterConfig());
try {
drConfig.getActiveXClusterConfig();
} catch (Exception e) {
fail();
}
assertTrue(drConfig.hasActiveXClusterConfig());
XClusterConfig xClusterConfig = drConfig.getActiveXClusterConfig();
assertNotNull(xClusterConfig);

xClusterConfig.setSecondary(true);
xClusterConfig.update();
assertFalse(drConfig.hasActiveXClusterConfig());
assertThrows(IllegalStateException.class, drConfig::getActiveXClusterConfig);
}

@Test
public void testDrConfigHasNoActiveXClusterConfig() {
Set<String> sourceDbId1 = Set.of("db1");
Set<String> sourceDbId2 = Set.of("db2");
DrConfig drConfig =
DrConfig.create(
"replication1",
Expand All @@ -182,7 +184,7 @@ public void testDrConfigHasNoActiveXClusterConfig() {
xClusterConfig.update();
}

assertEquals(false, drConfig.hasActiveXClusterConfig());
assertThrows(IllegalStateException.class, () -> drConfig.getActiveXClusterConfig());
assertFalse(drConfig.hasActiveXClusterConfig());
assertThrows(IllegalStateException.class, drConfig::getActiveXClusterConfig);
}
}

0 comments on commit 49fe314

Please sign in to comment.