diff --git a/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/CreateXClusterConfig.java b/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/CreateXClusterConfig.java index 509e0a074911..c3e9c16311db 100644 --- a/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/CreateXClusterConfig.java +++ b/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/CreateXClusterConfig.java @@ -521,9 +521,10 @@ protected void addSubtasksForTablesNeedBootstrap( if (xClusterConfig.getType() == ConfigType.Db) { if (!xClusterConfig.getDbIds().contains(namespaceId)) { xClusterConfig.addNamespaces(Set.of(namespaceId)); + xClusterConfig.updateStatusForNamespace( + namespaceId, XClusterNamespaceConfig.Status.Updating); } - xClusterConfig.updateStatusForNamespace( - namespaceId, XClusterNamespaceConfig.Status.Updating); + if (!isReplicationConfigCreated) { createCreateOutboundReplicationGroupTask( xClusterConfig, Collections.singleton(namespaceId)); diff --git a/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/EditXClusterConfig.java b/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/EditXClusterConfig.java index 559dbc6b9097..99a40244c341 100644 --- a/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/EditXClusterConfig.java +++ b/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/EditXClusterConfig.java @@ -112,7 +112,8 @@ public void run() { addSubtasksToAddDatabasesToXClusterConfig(xClusterConfig, databaseIdsToAdd); } if (!databaseIdsToRemove.isEmpty()) { - addSubtasksToRemoveDatabasesFromXClusterConfig(xClusterConfig, databaseIdsToRemove); + addSubtasksToRemoveDatabasesFromXClusterConfig( + xClusterConfig, databaseIdsToRemove, false /* keepEntry */); } } else { @@ -461,11 +462,12 @@ protected void addSubtasksToAddDatabasesToXClusterConfig( } protected void addSubtasksToRemoveDatabasesFromXClusterConfig( - XClusterConfig xClusterConfig, Set databases) { + XClusterConfig xClusterConfig, Set databases, boolean keepEntry) { for (String dbId : databases) { createXClusterRemoveNamespaceFromTargetUniverseTask(xClusterConfig, dbId); - createXClusterRemoveNamespaceFromOutboundReplicationGroupTask(xClusterConfig, dbId); + createXClusterRemoveNamespaceFromOutboundReplicationGroupTask( + xClusterConfig, dbId, keepEntry); } if (xClusterConfig.isUsedForDr()) { diff --git a/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/RestartXClusterConfig.java b/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/RestartXClusterConfig.java index 20ba304f8d22..a92de2553601 100644 --- a/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/RestartXClusterConfig.java +++ b/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/RestartXClusterConfig.java @@ -12,11 +12,14 @@ import com.yugabyte.yw.models.XClusterConfig; import com.yugabyte.yw.models.XClusterConfig.ConfigType; import com.yugabyte.yw.models.XClusterConfig.XClusterConfigStatusType; +import com.yugabyte.yw.models.XClusterNamespaceConfig; import com.yugabyte.yw.models.XClusterTableConfig; +import java.util.Collections; import java.util.List; import java.util.Set; import javax.inject.Inject; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; import org.yb.master.MasterDdlOuterClass; @Slf4j @@ -61,12 +64,14 @@ public void run() { false /* deletePitrConfigs */)); } + boolean isDBScopedReplication = xClusterConfig.getType() == ConfigType.Db; + List tableInfoList = taskParams().getTableInfoList(); Set tableIds = getTableIds(tableInfoList); boolean isRestartWholeConfig; - if (xClusterConfig.getType() != ConfigType.Db) { + if (!isDBScopedReplication) { // Set table type for old xCluster configs. xClusterConfig.updateTableType(tableInfoList); @@ -90,9 +95,14 @@ public void run() { taskParams().getDbs().size() == xClusterConfig.getNamespaces().size(); } + if (!CollectionUtils.isEmpty(taskParams().getDbs())) { + xClusterConfig.updateStatusForNamespaces( + taskParams().getDbs(), XClusterNamespaceConfig.Status.Updating); + } + log.info("isRestartWholeConfig is {}", isRestartWholeConfig); if (isRestartWholeConfig) { - if (xClusterConfig.getType() != ConfigType.Db) { + if (!isDBScopedReplication) { createXClusterConfigSetStatusForTablesTask( xClusterConfig, getTableIds(tableInfoList), XClusterTableConfig.Status.Updating); } @@ -117,7 +127,7 @@ public void run() { createXClusterConfigSetStatusTask( xClusterConfig, XClusterConfig.XClusterConfigStatusType.Updating); - if (xClusterConfig.getType() == ConfigType.Db) { + if (isDBScopedReplication) { addSubtasksToCreateXClusterConfig( xClusterConfig, taskParams().getDbs(), @@ -149,17 +159,27 @@ public void run() { .setSubTaskGroupType(UserTaskDetails.SubTaskGroupType.ConfigureUniverse); } } else { - createXClusterConfigSetStatusForTablesTask( - xClusterConfig, tableIds, XClusterTableConfig.Status.Updating); + if (!isDBScopedReplication) { + createXClusterConfigSetStatusForTablesTask( + xClusterConfig, tableIds, XClusterTableConfig.Status.Updating); + + createRemoveTableFromXClusterConfigSubtasks( + xClusterConfig, tableIds, true /* keepEntry */); + + createXClusterConfigSetStatusForTablesTask( + xClusterConfig, tableIds, XClusterTableConfig.Status.Updating); - createRemoveTableFromXClusterConfigSubtasks( - xClusterConfig, tableIds, true /* keepEntry */); + addSubtasksToAddTablesToXClusterConfig( + xClusterConfig, tableInfoList, taskParams().getMainTableIndexTablesMap(), tableIds); + } else { + createXClusterConfigSetStatusTask( + xClusterConfig, XClusterConfig.XClusterConfigStatusType.Updating); - createXClusterConfigSetStatusForTablesTask( - xClusterConfig, tableIds, XClusterTableConfig.Status.Updating); + addSubtasksToRemoveDatabasesFromXClusterConfig( + xClusterConfig, taskParams().getDbs(), true /* keepEntry */); - addSubtasksToAddTablesToXClusterConfig( - xClusterConfig, tableInfoList, taskParams().getMainTableIndexTablesMap(), tableIds); + addSubtasksToAddDatabasesToXClusterConfig(xClusterConfig, taskParams().getDbs()); + } } createXClusterConfigSetStatusTask( @@ -180,22 +200,42 @@ public void run() { } catch (Exception e) { log.error("{} hit error : {}", getName(), e.getMessage()); - // Set XClusterConfig status to Running if at least one table is running. - Set tablesInRunningStatus = - xClusterConfig.getTableIdsInStatus( - xClusterConfig.getTableIds(), XClusterTableConfig.Status.Running); - if (tablesInRunningStatus.isEmpty()) { - xClusterConfig.updateStatus(XClusterConfigStatusType.Failed); + if (xClusterConfig.getType().equals(XClusterConfig.ConfigType.Db)) { + // Set XClusterConfig status to Running if at least one namespace is running. + Set namespacesInRunningStatus = + xClusterConfig.getNamespaceIdsInStatus( + xClusterConfig.getDbIds(), + Collections.singleton(XClusterNamespaceConfig.Status.Running)); + if (namespacesInRunningStatus.isEmpty()) { + xClusterConfig.updateStatus(XClusterConfigStatusType.Failed); + } else { + xClusterConfig.updateStatus(XClusterConfigStatusType.Running); + } + // Set namespaces in updating status to failed. + Set namespacesInUpdatingStatus = + xClusterConfig.getNamespaceIdsInStatus( + taskParams().getDbs(), X_CLUSTER_NAMESPACE_CONFIG_PENDING_STATUS_LIST); + xClusterConfig.updateStatusForNamespaces( + namespacesInUpdatingStatus, XClusterNamespaceConfig.Status.Failed); } else { - xClusterConfig.updateStatus(XClusterConfigStatusType.Running); + + // Set XClusterConfig status to Running if at least one table is running. + Set tablesInRunningStatus = + xClusterConfig.getTableIdsInStatus( + xClusterConfig.getTableIds(), XClusterTableConfig.Status.Running); + if (tablesInRunningStatus.isEmpty()) { + xClusterConfig.updateStatus(XClusterConfigStatusType.Failed); + } else { + xClusterConfig.updateStatus(XClusterConfigStatusType.Running); + } + // Set tables in updating status to failed. + Set tablesInUpdatingStatus = + xClusterConfig.getTableIdsInStatus( + getTableIds(taskParams().getTableInfoList()), + X_CLUSTER_TABLE_CONFIG_PENDING_STATUS_LIST); + xClusterConfig.updateStatusForTables( + tablesInUpdatingStatus, XClusterTableConfig.Status.Failed); } - // Set tables in updating status to failed. - Set tablesInUpdatingStatus = - xClusterConfig.getTableIdsInStatus( - getTableIds(taskParams().getTableInfoList()), - X_CLUSTER_TABLE_CONFIG_PENDING_STATUS_LIST); - xClusterConfig.updateStatusForTables( - tablesInUpdatingStatus, XClusterTableConfig.Status.Failed); // Set backup and restore status to failed and alter load balanced. boolean isLoadBalancerAltered = false; for (Restore restore : restoreList) { diff --git a/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/XClusterConfigTaskBase.java b/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/XClusterConfigTaskBase.java index c6bb2c8025a2..a7c75589d492 100644 --- a/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/XClusterConfigTaskBase.java +++ b/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/XClusterConfigTaskBase.java @@ -3045,16 +3045,18 @@ protected SubTaskGroup createXClusterAddNamespaceToOutboundReplicationGroupTask( * * @param xClusterConfig config used * @param dbId db id on the source universe that are being added to checkpoint. + * @param keepEntry whether to keep the entry in the YBA DB. * @return The created subtask group */ protected SubTaskGroup createXClusterRemoveNamespaceFromOutboundReplicationGroupTask( - XClusterConfig xClusterConfig, String dbId) { + XClusterConfig xClusterConfig, String dbId, boolean keepEntry) { SubTaskGroup subTaskGroup = createSubTaskGroup("XClusterRemoveNamespaceFromOutboundReplication"); XClusterRemoveNamespaceFromOutboundReplicationGroup.Params taskParams = new XClusterRemoveNamespaceFromOutboundReplicationGroup.Params(); taskParams.xClusterConfig = xClusterConfig; taskParams.dbToRemove = dbId; + taskParams.keepEntry = keepEntry; XClusterRemoveNamespaceFromOutboundReplicationGroup task = createTask(XClusterRemoveNamespaceFromOutboundReplicationGroup.class); task.initialize(taskParams); diff --git a/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/subtasks/xcluster/XClusterRemoveNamespaceFromOutboundReplicationGroup.java b/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/subtasks/xcluster/XClusterRemoveNamespaceFromOutboundReplicationGroup.java index bbd8119418ac..41228d17e8af 100644 --- a/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/subtasks/xcluster/XClusterRemoveNamespaceFromOutboundReplicationGroup.java +++ b/managed/src/main/java/com/yugabyte/yw/commissioner/tasks/subtasks/xcluster/XClusterRemoveNamespaceFromOutboundReplicationGroup.java @@ -33,6 +33,18 @@ public static class Params extends XClusterConfigTaskParams { // The parent xCluster config must be stored in xClusterConfig field. // The db to be removed from the xcluster replication must be stored in the dbToRemove field. public String dbToRemove; + + public boolean keepEntry; + } + + @Override + public String getName() { + return String.format( + "%s(xClusterConfig=%s,dbToRemove=%s,keepEntry=%s)", + this.getClass().getSimpleName(), + taskParams().getXClusterConfig().getUuid(), + taskParams().dbToRemove, + taskParams().keepEntry); } @Override @@ -71,7 +83,12 @@ public void run() { "XClusterRemoveNamespaceFromOutboundReplicationGroup rpc failed with error: %s", createResponse.errorMessage())); } - xClusterConfig.removeNamespaces(Set.of(dbId)); + if (!taskParams().keepEntry) { + log.info( + "Removing db id: {} from xClusterConfig Object {}", dbId, xClusterConfig.getUuid()); + xClusterConfig.removeNamespaces(Set.of(dbId)); + } + log.debug( "Removing source db id: {} from xClusterConfig {} completed", taskParams().getDbToRemove(), diff --git a/managed/src/test/java/com/yugabyte/yw/commissioner/tasks/local/DRDbScopedLocalTest.java b/managed/src/test/java/com/yugabyte/yw/commissioner/tasks/local/DRDbScopedLocalTest.java index e51cb5cac220..d60899817e18 100644 --- a/managed/src/test/java/com/yugabyte/yw/commissioner/tasks/local/DRDbScopedLocalTest.java +++ b/managed/src/test/java/com/yugabyte/yw/commissioner/tasks/local/DRDbScopedLocalTest.java @@ -636,7 +636,7 @@ public void testDbScopedSwitchover() throws InterruptedException { deleteDrConfig(drConfigUUID, newSourceUniverse, newTargetUniverse); } - // @Test + @Test public void testDbScopedRestart() throws InterruptedException { CreateDRMetadata createData = defaultDbDRCreate(); UUID drConfigUUID = createData.drConfigUUID;