From e383088483cbb9b98e3a720a9be0b4fae479bbe0 Mon Sep 17 00:00:00 2001 From: Haoran Meng Date: Fri, 31 May 2024 13:09:50 +0800 Subject: [PATCH] Move data source state manager to StateContext (#31439) * Move data source state manager to StateContext * Move data source state manager to StateContext * Move data source state manager to StateContext * Move data source state manager to StateContext * Move data source state manager to StateContext --- .../proxy/ProxyMetaDataInfoExporterTest.java | 2 + .../impl/proxy/ProxyStateExporterTest.java | 15 +--- .../PrometheusPluginLifecycleServiceTest.java | 2 +- ...iteSplittingStorageUnitStatusExecutor.java | 4 +- .../driver/state/DriverStateContextTest.java | 2 - .../mode/manager/ContextManager.java | 4 +- .../metadata/MetaDataContextsFactory.java | 8 +- .../mode/service/PersistServiceFacade.java | 4 + ...alifiedDataSourceStatePersistService.java} | 22 ++--- .../mode/state/StateContext.java | 82 +++++++++++++++++++ ...tus.java => QualifiedDataSourceState.java} | 4 +- .../YamlQualifiedDataSourceStatusSwapper.java | 10 +-- .../mode/manager/ContextManagerTest.java | 2 +- .../mode/state/StateContextTest.java | 6 +- .../cluster/ClusterContextManagerBuilder.java | 7 +- .../event/StorageNodeChangedEvent.java | 6 +- .../QualifiedDataSourceChangedWatcher.java | 10 +-- ... QualifiedDataSourceStateServiceTest.java} | 6 +- ...alifiedDataSourceStateSubscriberTest.java} | 2 +- .../StateChangedSubscriberTest.java | 4 +- .../StandaloneContextManagerBuilder.java | 2 +- .../ProxyDatabaseConnectionManagerTest.java | 2 +- .../connector/ProxySQLExecutorTest.java | 3 +- .../backend/context/ProxyContextTest.java | 2 +- .../DatabaseAdminQueryBackendHandlerTest.java | 3 +- .../ral/QueryableRALBackendHandlerTest.java | 2 +- .../SetDistVariableExecutorTest.java | 2 +- .../FrontDatabaseProtocolTypeFactoryTest.java | 2 +- .../frontend/state/impl/OKProxyStateTest.java | 2 +- .../PostgreSQLCommandExecuteEngineTest.java | 2 +- .../core/util/PipelineContextUtils.java | 3 +- 31 files changed, 155 insertions(+), 72 deletions(-) rename mode/core/src/main/java/org/apache/shardingsphere/mode/{storage/service/QualifiedDataSourceStatusService.java => service/persist/QualifiedDataSourceStatePersistService.java} (79%) rename mode/core/src/main/java/org/apache/shardingsphere/mode/storage/{QualifiedDataSourceStatus.java => QualifiedDataSourceState.java} (93%) rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/service/{QualifiedDataSourceStatusServiceTest.java => QualifiedDataSourceStateServiceTest.java} (87%) rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/subscriber/{QualifiedDataSourceStatusSubscriberTest.java => QualifiedDataSourceStateSubscriberTest.java} (97%) diff --git a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java index 7008ec312e07b..64ce8e8f02ac9 100644 --- a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java +++ b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java @@ -25,6 +25,7 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; @@ -76,6 +77,7 @@ void assertExportWithContextManager() { private ContextManager mockContextManager() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getResourceMetaData()).thenReturn(mock(ResourceMetaData.class)); when(database.getResourceMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("ds_0", mock(StorageUnit.class))); when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class); diff --git a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyStateExporterTest.java b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyStateExporterTest.java index f2fc0fcdaf097..0824357c6a933 100644 --- a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyStateExporterTest.java +++ b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyStateExporterTest.java @@ -22,20 +22,12 @@ import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType; import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration; import org.apache.shardingsphere.agent.plugin.metrics.core.fixture.collector.MetricsCollectorFixture; -import org.apache.shardingsphere.infra.config.mode.ModeConfiguration; -import org.apache.shardingsphere.infra.instance.ComputeNodeInstance; -import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext; -import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData; -import org.apache.shardingsphere.infra.lock.LockContext; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.state.instance.InstanceStateContext; -import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.standalone.workerid.generator.StandaloneWorkerIdGenerator; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; -import org.apache.shardingsphere.mode.spi.PersistRepository; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; @@ -82,9 +74,8 @@ void assertExportWithContextManager() { private ContextManager mockContextManager() { MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData()); - ComputeNodeInstanceContext computeNodeInstanceContext = new ComputeNodeInstanceContext( - new ComputeNodeInstance(mock(InstanceMetaData.class)), new StandaloneWorkerIdGenerator(), new ModeConfiguration("Standalone", null), - mock(LockContext.class), new EventBusContext()); - return new ContextManager(metaDataContexts, computeNodeInstanceContext, mock(PersistRepository.class)); + ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); + when(result.getMetaDataContexts()).thenReturn(metaDataContexts); + return result; } } diff --git a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleServiceTest.java b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleServiceTest.java index be934e121087a..a836f3380d5da 100644 --- a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleServiceTest.java +++ b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleServiceTest.java @@ -74,6 +74,6 @@ private ContextManager mockContextManager() { ComputeNodeInstanceContext computeNodeInstanceContext = new ComputeNodeInstanceContext( new ComputeNodeInstance(mock(InstanceMetaData.class)), new StandaloneWorkerIdGenerator(), new ModeConfiguration("Standalone", null), mock(LockContext.class), new EventBusContext()); - return new ContextManager(metaDataContexts, computeNodeInstanceContext, mock(PersistRepository.class)); + return new ContextManager(metaDataContexts, computeNodeInstanceContext, mock(PersistRepository.class), false); } } diff --git a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingStorageUnitStatusExecutor.java b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingStorageUnitStatusExecutor.java index f8dcce5ec9c13..0b52639548547 100644 --- a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingStorageUnitStatusExecutor.java +++ b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingStorageUnitStatusExecutor.java @@ -28,7 +28,6 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.state.datasource.DataSourceState; import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.storage.service.QualifiedDataSourceStatusService; import org.apache.shardingsphere.readwritesplitting.constant.ReadwriteSplittingDataSourceType; import org.apache.shardingsphere.readwritesplitting.distsql.statement.AlterReadwriteSplittingStorageUnitStatusStatement; import org.apache.shardingsphere.readwritesplitting.exception.ReadwriteSplittingRuleExceptionIdentifier; @@ -77,8 +76,7 @@ private void checkBeforeUpdate(final AlterReadwriteSplittingStorageUnitStatusSta private void updateStatus(final ContextManager contextManager, final AlterReadwriteSplittingStorageUnitStatusStatement sqlStatement) { DataSourceState status = sqlStatement.isEnable() ? DataSourceState.ENABLED : DataSourceState.DISABLED; - new QualifiedDataSourceStatusService(contextManager.getRepository()) - .changeStatus(database.getName(), sqlStatement.getRuleName(), sqlStatement.getStorageUnitName(), status); + contextManager.getPersistServiceFacade().getQualifiedDataSourceStatePersistService().updateState(database.getName(), sqlStatement.getRuleName(), sqlStatement.getStorageUnitName(), status); } @Override diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java index e164b600a18c5..0fef4b94fff94 100644 --- a/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java +++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java @@ -32,7 +32,6 @@ import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; -import org.apache.shardingsphere.mode.state.StateContext; import org.apache.shardingsphere.traffic.rule.TrafficRule; import org.apache.shardingsphere.transaction.rule.TransactionRule; import org.junit.jupiter.api.BeforeEach; @@ -70,7 +69,6 @@ void setUp() { mock(MetaDataPersistService.class), new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), globalRuleMetaData, new ConfigurationProperties(new Properties()))); when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts); when(contextManager.getComputeNodeInstanceContext().getInstance().getState()).thenReturn(new InstanceStateContext()); - when(contextManager.getStateContext()).thenReturn(new StateContext()); } private Map mockDatabases() { diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java index a6af4feacf171..88be4c316a322 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java @@ -75,14 +75,14 @@ public final class ContextManager implements AutoCloseable { private final PersistRepository repository; - public ContextManager(final MetaDataContexts metaDataContexts, final ComputeNodeInstanceContext computeNodeInstanceContext, final PersistRepository repository) { + public ContextManager(final MetaDataContexts metaDataContexts, final ComputeNodeInstanceContext computeNodeInstanceContext, final PersistRepository repository, final boolean force) { this.metaDataContexts = new AtomicReference<>(metaDataContexts); this.computeNodeInstanceContext = computeNodeInstanceContext; this.repository = repository; persistServiceFacade = new PersistServiceFacade(repository, computeNodeInstanceContext.getModeConfiguration(), this); + stateContext = new StateContext(this.metaDataContexts.get().getMetaData(), persistServiceFacade.getQualifiedDataSourceStatePersistService().loadStates(), force); metaDataContextManager = new MetaDataContextManager(this.metaDataContexts, computeNodeInstanceContext, persistServiceFacade); executorEngine = ExecutorEngine.createExecutorEngineWithSize(metaDataContexts.getMetaData().getProps().getValue(ConfigurationPropertyKey.KERNEL_EXECUTOR_SIZE)); - stateContext = new StateContext(); } /** diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java index a1217a7220248..aa0431f6e7244 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java @@ -50,7 +50,7 @@ import org.apache.shardingsphere.metadata.factory.InternalMetaDataFactory; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter; -import org.apache.shardingsphere.mode.storage.QualifiedDataSourceStatus; +import org.apache.shardingsphere.mode.storage.QualifiedDataSourceState; import javax.sql.DataSource; import java.sql.SQLException; @@ -95,7 +95,7 @@ public static MetaDataContexts create(final MetaDataPersistService persistServic * @throws SQLException SQL exception */ public static MetaDataContexts create(final MetaDataPersistService persistService, final ContextManagerBuilderParameter param, - final ComputeNodeInstanceContext computeNodeInstanceContext, final Map statusMap) throws SQLException { + final ComputeNodeInstanceContext computeNodeInstanceContext, final Map statusMap) throws SQLException { boolean isDatabaseMetaDataExisted = !persistService.getDatabaseMetaDataService().loadAllDatabaseNames().isEmpty(); Map effectiveDatabaseConfigs = isDatabaseMetaDataExisted ? createEffectiveDatabaseConfigurations(getDatabaseNames(computeNodeInstanceContext, param.getDatabaseConfigs(), persistService), param.getDatabaseConfigs(), persistService) @@ -155,7 +155,7 @@ private static void closeGeneratedDataSources(final String databaseName, final M } } - private static void checkDataSourceStates(final Map databaseConfigs, final Map statusMap, final boolean force) { + private static void checkDataSourceStates(final Map databaseConfigs, final Map statusMap, final boolean force) { Map storageDataSourceStates = getStorageDataSourceStates(statusMap); databaseConfigs.forEach((key, value) -> { if (!value.getStorageUnits().isEmpty()) { @@ -164,7 +164,7 @@ private static void checkDataSourceStates(final Map getStorageDataSourceStates(final Map statusMap) { + private static Map getStorageDataSourceStates(final Map statusMap) { Map result = new HashMap<>(statusMap.size(), 1F); statusMap.forEach((key, value) -> { List values = Splitter.on(".").splitToList(key); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/service/PersistServiceFacade.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/service/PersistServiceFacade.java index 3c63b6d4391f4..17ba7def41627 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/service/PersistServiceFacade.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/service/PersistServiceFacade.java @@ -26,6 +26,7 @@ import org.apache.shardingsphere.mode.service.persist.MetaDataManagerPersistService; import org.apache.shardingsphere.mode.service.persist.PersistServiceBuilder; import org.apache.shardingsphere.mode.service.persist.ProcessPersistService; +import org.apache.shardingsphere.mode.service.persist.QualifiedDataSourceStatePersistService; import org.apache.shardingsphere.mode.service.pojo.ShardingSphereSchemaDataAlteredPOJO; import org.apache.shardingsphere.mode.spi.PersistRepository; import org.apache.shardingsphere.mode.state.StatePersistService; @@ -46,10 +47,13 @@ public final class PersistServiceFacade { private final ProcessPersistService processPersistService; + private final QualifiedDataSourceStatePersistService qualifiedDataSourceStatePersistService; + public PersistServiceFacade(final PersistRepository repository, final ModeConfiguration modeConfiguration, final ContextManager contextManager) { metaDataPersistService = new MetaDataPersistService(repository); computeNodePersistService = new ComputeNodePersistService(repository); statePersistService = new StatePersistService(repository); + qualifiedDataSourceStatePersistService = new QualifiedDataSourceStatePersistService(repository); PersistServiceBuilder persistServiceBuilder = TypedSPILoader.getService(PersistServiceBuilder.class, modeConfiguration.getType()); metaDataManagerPersistService = persistServiceBuilder.buildMetaDataManagerPersistService(contextManager); processPersistService = persistServiceBuilder.buildProcessPersistService(repository); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/storage/service/QualifiedDataSourceStatusService.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/service/persist/QualifiedDataSourceStatePersistService.java similarity index 79% rename from mode/core/src/main/java/org/apache/shardingsphere/mode/storage/service/QualifiedDataSourceStatusService.java rename to mode/core/src/main/java/org/apache/shardingsphere/mode/service/persist/QualifiedDataSourceStatePersistService.java index d02e0d4e18b52..c87370660d8ca 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/storage/service/QualifiedDataSourceStatusService.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/service/persist/QualifiedDataSourceStatePersistService.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.storage.service; +package org.apache.shardingsphere.mode.service.persist; import com.google.common.base.Strings; import lombok.RequiredArgsConstructor; @@ -23,7 +23,7 @@ import org.apache.shardingsphere.infra.state.datasource.DataSourceState; import org.apache.shardingsphere.infra.util.yaml.YamlEngine; import org.apache.shardingsphere.mode.spi.PersistRepository; -import org.apache.shardingsphere.mode.storage.QualifiedDataSourceStatus; +import org.apache.shardingsphere.mode.storage.QualifiedDataSourceState; import org.apache.shardingsphere.mode.storage.node.QualifiedDataSourceNode; import org.apache.shardingsphere.mode.storage.yaml.YamlQualifiedDataSourceStatus; import org.apache.shardingsphere.mode.storage.yaml.YamlQualifiedDataSourceStatusSwapper; @@ -33,21 +33,21 @@ import java.util.Map; /** - * Qualified data source status service. + * Qualified data source state persist service. */ @RequiredArgsConstructor -public final class QualifiedDataSourceStatusService { +public final class QualifiedDataSourceStatePersistService { private final PersistRepository repository; /** - * Load qualified data source status. + * Load qualified data source states. * - * @return qualified data source status + * @return qualified data source states */ - public Map loadStatus() { + public Map loadStates() { Collection qualifiedDataSourceNodes = repository.getChildrenKeys(QualifiedDataSourceNode.getRootPath()); - Map result = new HashMap<>(qualifiedDataSourceNodes.size(), 1F); + Map result = new HashMap<>(qualifiedDataSourceNodes.size(), 1F); qualifiedDataSourceNodes.forEach(each -> { String yamlContent = repository.query(QualifiedDataSourceNode.getQualifiedDataSourceNodePath(new QualifiedDataSource(each))); if (!Strings.isNullOrEmpty(yamlContent)) { @@ -58,15 +58,15 @@ public Map loadStatus() { } /** - * Change qualified data source status. + * Update qualified data source state. * * @param databaseName database name * @param groupName group name * @param storageUnitName storage unit name * @param dataSourceState data source state */ - public void changeStatus(final String databaseName, final String groupName, final String storageUnitName, final DataSourceState dataSourceState) { - QualifiedDataSourceStatus status = new QualifiedDataSourceStatus(dataSourceState); + public void updateState(final String databaseName, final String groupName, final String storageUnitName, final DataSourceState dataSourceState) { + QualifiedDataSourceState status = new QualifiedDataSourceState(dataSourceState); repository.persist(QualifiedDataSourceNode.getQualifiedDataSourceNodePath( new QualifiedDataSource(databaseName, groupName, storageUnitName)), YamlEngine.marshal(new YamlQualifiedDataSourceStatusSwapper().swapToYamlConfiguration(status))); } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/state/StateContext.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/state/StateContext.java index 32b7c685c41f4..57f788f8a35f6 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/state/StateContext.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/state/StateContext.java @@ -17,16 +17,88 @@ package org.apache.shardingsphere.mode.state; +import com.google.common.base.Preconditions; +import com.google.common.base.Splitter; +import lombok.extern.slf4j.Slf4j; +import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDataSource; import org.apache.shardingsphere.infra.state.cluster.ClusterState; +import org.apache.shardingsphere.infra.state.datasource.DataSourceState; +import org.apache.shardingsphere.infra.state.datasource.exception.UnavailableDataSourceException; +import org.apache.shardingsphere.mode.storage.QualifiedDataSourceState; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicReference; /** * State context. */ +@Slf4j public final class StateContext { private final AtomicReference currentClusterState = new AtomicReference<>(ClusterState.OK); + private final Map dataSourceStates = new ConcurrentHashMap<>(); + + public StateContext(final ShardingSphereMetaData metaData, final Map qualifiedDataSourceStates, final boolean force) { + initDataSourceState(metaData, convert(qualifiedDataSourceStates), force); + } + + private void initDataSourceState(final ShardingSphereMetaData metaData, final Map storageDataSourceStates, final boolean force) { + metaData.getDatabases().forEach((key, value) -> { + if (value.getResourceMetaData() != null && !value.getResourceMetaData().getStorageUnits().isEmpty()) { + initDataSourceState(key, value.getResourceMetaData().getStorageUnits(), storageDataSourceStates, force); + } + }); + } + + private void initDataSourceState(final String databaseName, final Map storageUnits, final Map storageDataSourceStates, final boolean force) { + storageUnits.forEach((key, value) -> initDataSourceState(databaseName, storageDataSourceStates, key, value.getDataSource(), force)); + } + + private void initDataSourceState(final String databaseName, final Map storageDataSourceStates, final String actualDataSourceName, final DataSource dataSource, + final boolean force) { + DataSourceState storageState = storageDataSourceStates.get(getCacheKey(databaseName, actualDataSourceName)); + if (DataSourceState.DISABLED == storageState) { + dataSourceStates.put(getCacheKey(databaseName, actualDataSourceName), storageState); + } else { + checkState(databaseName, actualDataSourceName, dataSource, force); + } + } + + private static Map convert(final Map qualifiedDataSourceStates) { + Map result = new HashMap<>(qualifiedDataSourceStates.size(), 1F); + qualifiedDataSourceStates.forEach((key, value) -> { + List values = Splitter.on(".").splitToList(key); + Preconditions.checkArgument(3 == values.size(), "Illegal data source of storage node."); + String databaseName = values.get(0); + String dataSourceName = values.get(2); + result.put(databaseName + "." + dataSourceName, DataSourceState.valueOf(value.getStatus().name())); + }); + return result; + } + + private void checkState(final String databaseName, final String actualDataSourceName, final DataSource dataSource, final boolean force) { + try (Connection ignored = dataSource.getConnection()) { + dataSourceStates.put(getCacheKey(databaseName, actualDataSourceName), DataSourceState.ENABLED); + } catch (final SQLException ex) { + ShardingSpherePreconditions.checkState(force, () -> new UnavailableDataSourceException(actualDataSourceName, ex)); + log.error("Data source unavailable, ignored with the -f parameter.", ex); + } + } + + private String getCacheKey(final String databaseName, final String dataSourceName) { + return databaseName + "." + dataSourceName; + } + /** * Get current cluster state. * @@ -44,4 +116,14 @@ public ClusterState getCurrentClusterState() { public void switchCurrentClusterState(final ClusterState state) { currentClusterState.set(state); } + + /** + * Update data source state. + * + * @param qualifiedDataSource qualified data source + * @param dataSourceState data source state + */ + public void updateDataSourceState(final QualifiedDataSource qualifiedDataSource, final DataSourceState dataSourceState) { + dataSourceStates.put(getCacheKey(qualifiedDataSource.getDatabaseName(), qualifiedDataSource.getDataSourceName()), dataSourceState); + } } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/storage/QualifiedDataSourceStatus.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/storage/QualifiedDataSourceState.java similarity index 93% rename from mode/core/src/main/java/org/apache/shardingsphere/mode/storage/QualifiedDataSourceStatus.java rename to mode/core/src/main/java/org/apache/shardingsphere/mode/storage/QualifiedDataSourceState.java index 2c9a9d2abc6ee..c7401ce51599f 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/storage/QualifiedDataSourceStatus.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/storage/QualifiedDataSourceState.java @@ -22,11 +22,11 @@ import org.apache.shardingsphere.infra.state.datasource.DataSourceState; /** - * Qualified data source status. + * Qualified data source state. */ @RequiredArgsConstructor @Getter -public final class QualifiedDataSourceStatus { +public final class QualifiedDataSourceState { private final DataSourceState status; } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/storage/yaml/YamlQualifiedDataSourceStatusSwapper.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/storage/yaml/YamlQualifiedDataSourceStatusSwapper.java index 4db8d95033bee..f75545cdff8e7 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/storage/yaml/YamlQualifiedDataSourceStatusSwapper.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/storage/yaml/YamlQualifiedDataSourceStatusSwapper.java @@ -19,22 +19,22 @@ import org.apache.shardingsphere.infra.state.datasource.DataSourceState; import org.apache.shardingsphere.infra.util.yaml.swapper.YamlConfigurationSwapper; -import org.apache.shardingsphere.mode.storage.QualifiedDataSourceStatus; +import org.apache.shardingsphere.mode.storage.QualifiedDataSourceState; /** * YAML qualified data source status swapper. */ -public final class YamlQualifiedDataSourceStatusSwapper implements YamlConfigurationSwapper { +public final class YamlQualifiedDataSourceStatusSwapper implements YamlConfigurationSwapper { @Override - public YamlQualifiedDataSourceStatus swapToYamlConfiguration(final QualifiedDataSourceStatus data) { + public YamlQualifiedDataSourceStatus swapToYamlConfiguration(final QualifiedDataSourceState data) { YamlQualifiedDataSourceStatus result = new YamlQualifiedDataSourceStatus(); result.setStatus(data.getStatus().name()); return result; } @Override - public QualifiedDataSourceStatus swapToObject(final YamlQualifiedDataSourceStatus yamlConfig) { - return new QualifiedDataSourceStatus(DataSourceState.valueOf(yamlConfig.getStatus())); + public QualifiedDataSourceState swapToObject(final YamlQualifiedDataSourceStatus yamlConfig) { + return new QualifiedDataSourceState(DataSourceState.valueOf(yamlConfig.getStatus())); } } diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java index d689b9de3bb6f..b6803aa5d6543 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java @@ -89,7 +89,7 @@ void setUp() { when(metaDataContexts.getMetaData().getDatabases().values()).thenReturn(Collections.singleton(database)); ComputeNodeInstanceContext computeNodeInstanceContext = mock(ComputeNodeInstanceContext.class); when(computeNodeInstanceContext.getModeConfiguration()).thenReturn(mock(ModeConfiguration.class)); - contextManager = new ContextManager(metaDataContexts, computeNodeInstanceContext, mock(PersistRepository.class)); + contextManager = new ContextManager(metaDataContexts, computeNodeInstanceContext, mock(PersistRepository.class), false); } private ShardingSphereDatabase mockDatabase() { diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/state/StateContextTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/state/StateContextTest.java index ba42fc4314670..03602fabb33d5 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/state/StateContextTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/state/StateContextTest.java @@ -17,15 +17,19 @@ package org.apache.shardingsphere.mode.state; +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.state.cluster.ClusterState; import org.junit.jupiter.api.Test; +import java.util.HashMap; + import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.mock; class StateContextTest { - private final StateContext stateContext = new StateContext(); + private final StateContext stateContext = new StateContext(mock(ShardingSphereMetaData.class), new HashMap<>(), false); @Test void assertGetCurrentClusterState() { diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java index 9c17de92bf4f5..be9560ee83f68 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java @@ -43,8 +43,8 @@ import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration; import org.apache.shardingsphere.mode.repository.cluster.lock.holder.DistributedLockHolder; import org.apache.shardingsphere.mode.repository.cluster.lock.impl.props.DefaultLockTypedProperties; +import org.apache.shardingsphere.mode.service.persist.QualifiedDataSourceStatePersistService; import org.apache.shardingsphere.mode.state.StatePersistService; -import org.apache.shardingsphere.mode.storage.service.QualifiedDataSourceStatusService; import java.sql.SQLException; import java.util.Collections; @@ -64,8 +64,9 @@ public ContextManager build(final ContextManagerBuilderParameter param, final Ev ComputeNodeInstanceContext computeNodeInstanceContext = buildComputeNodeInstanceContext(modeConfig, param.getInstanceMetaData(), repository, eventBusContext); repository.init(config, computeNodeInstanceContext); MetaDataPersistService metaDataPersistService = new MetaDataPersistService(repository); - MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, param, computeNodeInstanceContext, new QualifiedDataSourceStatusService(repository).loadStatus()); - ContextManager result = new ContextManager(metaDataContexts, computeNodeInstanceContext, repository); + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, param, computeNodeInstanceContext, + new QualifiedDataSourceStatePersistService(repository).loadStates()); + ContextManager result = new ContextManager(metaDataContexts, computeNodeInstanceContext, repository, param.isForce()); createSubscribers(eventBusContext, repository); registerOnline(eventBusContext, computeNodeInstanceContext, repository, param, result); setClusterState(result); diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/event/StorageNodeChangedEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/event/StorageNodeChangedEvent.java index c045e408c53c6..73f8bc70ffc22 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/event/StorageNodeChangedEvent.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/event/StorageNodeChangedEvent.java @@ -19,9 +19,9 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.rule.event.GovernanceEvent; import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDataSource; -import org.apache.shardingsphere.mode.storage.QualifiedDataSourceStatus; +import org.apache.shardingsphere.infra.rule.event.GovernanceEvent; +import org.apache.shardingsphere.mode.storage.QualifiedDataSourceState; /** * Storage node changed event. @@ -32,5 +32,5 @@ public final class StorageNodeChangedEvent implements GovernanceEvent { private final QualifiedDataSource qualifiedDataSource; - private final QualifiedDataSourceStatus status; + private final QualifiedDataSourceState status; } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/watcher/QualifiedDataSourceChangedWatcher.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/watcher/QualifiedDataSourceChangedWatcher.java index f9e923f9317d4..05cd9c2198fed 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/watcher/QualifiedDataSourceChangedWatcher.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/watcher/QualifiedDataSourceChangedWatcher.java @@ -19,16 +19,16 @@ import com.google.common.base.Strings; import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDataSource; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; import org.apache.shardingsphere.infra.rule.event.GovernanceEvent; +import org.apache.shardingsphere.infra.util.yaml.YamlEngine; +import org.apache.shardingsphere.mode.event.DataChangedEvent; +import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher; import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.event.StorageNodeChangedEvent; +import org.apache.shardingsphere.mode.storage.QualifiedDataSourceState; import org.apache.shardingsphere.mode.storage.node.QualifiedDataSourceNode; import org.apache.shardingsphere.mode.storage.yaml.YamlQualifiedDataSourceStatus; import org.apache.shardingsphere.mode.storage.yaml.YamlQualifiedDataSourceStatusSwapper; -import org.apache.shardingsphere.mode.storage.QualifiedDataSourceStatus; -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; import java.util.Arrays; import java.util.Collection; @@ -57,7 +57,7 @@ public Optional createGovernanceEvent(final DataChangedEvent ev } Optional qualifiedDataSource = QualifiedDataSourceNode.extractQualifiedDataSource(event.getKey()); if (qualifiedDataSource.isPresent()) { - QualifiedDataSourceStatus status = new YamlQualifiedDataSourceStatusSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(), YamlQualifiedDataSourceStatus.class)); + QualifiedDataSourceState status = new YamlQualifiedDataSourceStatusSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(), YamlQualifiedDataSourceStatus.class)); return Optional.of(new StorageNodeChangedEvent(qualifiedDataSource.get(), status)); } return Optional.empty(); diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/service/QualifiedDataSourceStatusServiceTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/service/QualifiedDataSourceStateServiceTest.java similarity index 87% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/service/QualifiedDataSourceStatusServiceTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/service/QualifiedDataSourceStateServiceTest.java index e91e6771e40a4..cbda735fc1fb8 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/service/QualifiedDataSourceStatusServiceTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/service/QualifiedDataSourceStateServiceTest.java @@ -18,7 +18,7 @@ package org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.service; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; -import org.apache.shardingsphere.mode.storage.service.QualifiedDataSourceStatusService; +import org.apache.shardingsphere.mode.service.persist.QualifiedDataSourceStatePersistService; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; @@ -32,7 +32,7 @@ import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) -class QualifiedDataSourceStatusServiceTest { +class QualifiedDataSourceStateServiceTest { @Mock private ClusterPersistRepository repository; @@ -41,6 +41,6 @@ class QualifiedDataSourceStatusServiceTest { void assertLoadStatus() { List disabledDataSources = Arrays.asList("replica_query_db.readwrite_ds.replica_ds_0", "other_schema.other_ds.other_ds0"); when(repository.getChildrenKeys(anyString())).thenReturn(disabledDataSources); - assertDoesNotThrow(() -> new QualifiedDataSourceStatusService(repository).loadStatus()); + assertDoesNotThrow(() -> new QualifiedDataSourceStatePersistService(repository).loadStates()); } } diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/subscriber/QualifiedDataSourceStatusSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/subscriber/QualifiedDataSourceStateSubscriberTest.java similarity index 97% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/subscriber/QualifiedDataSourceStatusSubscriberTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/subscriber/QualifiedDataSourceStateSubscriberTest.java index c28beba4fab52..67a11e39287db 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/subscriber/QualifiedDataSourceStatusSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/subscriber/QualifiedDataSourceStateSubscriberTest.java @@ -29,7 +29,7 @@ import static org.mockito.Mockito.verify; @ExtendWith(MockitoExtension.class) -class QualifiedDataSourceStatusSubscriberTest { +class QualifiedDataSourceStateSubscriberTest { @Mock private ClusterPersistRepository repository; diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/StateChangedSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/StateChangedSubscriberTest.java index f708bbf6c4e7e..4bd4390602424 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/StateChangedSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/StateChangedSubscriberTest.java @@ -45,7 +45,7 @@ import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.event.StorageNodeChangedEvent; import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration; -import org.apache.shardingsphere.mode.storage.QualifiedDataSourceStatus; +import org.apache.shardingsphere.mode.storage.QualifiedDataSourceState; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -112,7 +112,7 @@ private Map createDatabases() { void assertRenewForDisableStateChanged() { StaticDataSourceRuleAttribute ruleAttribute = mock(StaticDataSourceRuleAttribute.class); when(database.getRuleMetaData().getAttributes(StaticDataSourceRuleAttribute.class)).thenReturn(Collections.singleton(ruleAttribute)); - StorageNodeChangedEvent event = new StorageNodeChangedEvent(new QualifiedDataSource("db.readwrite_ds.ds_0"), new QualifiedDataSourceStatus(DataSourceState.DISABLED)); + StorageNodeChangedEvent event = new StorageNodeChangedEvent(new QualifiedDataSource("db.readwrite_ds.ds_0"), new QualifiedDataSourceState(DataSourceState.DISABLED)); subscriber.renew(event); verify(ruleAttribute).updateStatus( argThat(qualifiedDataSource -> Objects.equals(event.getQualifiedDataSource(), qualifiedDataSource)), diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java index 5c2b3da39b068..627bd808294be 100644 --- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java +++ b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java @@ -49,7 +49,7 @@ public ContextManager build(final ContextManagerBuilderParameter param, final Ev MetaDataPersistService persistService = new MetaDataPersistService(repository); ComputeNodeInstanceContext computeNodeInstanceContext = buildComputeNodeInstanceContext(param, eventBusContext); MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(persistService, param, computeNodeInstanceContext); - ContextManager result = new ContextManager(metaDataContexts, computeNodeInstanceContext, repository); + ContextManager result = new ContextManager(metaDataContexts, computeNodeInstanceContext, repository, param.isForce()); new StandaloneEventSubscriberRegistry(result).register(); return result; } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxyDatabaseConnectionManagerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxyDatabaseConnectionManagerTest.java index 18d70ce4b9591..9843da08bc7c7 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxyDatabaseConnectionManagerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxyDatabaseConnectionManagerTest.java @@ -127,7 +127,7 @@ private ContextManager mockContextManager() { when(metaData.getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Collections.singletonList(transactionRule))); ComputeNodeInstanceContext computeNodeInstanceContext = mock(ComputeNodeInstanceContext.class); when(computeNodeInstanceContext.getModeConfiguration()).thenReturn(mock(ModeConfiguration.class)); - return new ContextManager(MetaDataContextsFactory.create(mock(MetaDataPersistService.class), metaData), computeNodeInstanceContext, mock(PersistRepository.class)); + return new ContextManager(MetaDataContextsFactory.create(mock(MetaDataPersistService.class), metaData), computeNodeInstanceContext, mock(PersistRepository.class), false); } @AfterEach diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutorTest.java index baf87a324d583..6034968f676f3 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutorTest.java @@ -112,7 +112,8 @@ void setUp() { when(metaData.getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Arrays.asList(mock(SQLFederationRule.class), transactionRule))); ComputeNodeInstanceContext computeNodeInstanceContext = mock(ComputeNodeInstanceContext.class); when(computeNodeInstanceContext.getModeConfiguration()).thenReturn(mock(ModeConfiguration.class)); - ContextManager contextManager = new ContextManager(MetaDataContextsFactory.create(mock(MetaDataPersistService.class), metaData), computeNodeInstanceContext, mock(PersistRepository.class)); + ContextManager contextManager = new ContextManager(MetaDataContextsFactory.create(mock(MetaDataPersistService.class), metaData), computeNodeInstanceContext, + mock(PersistRepository.class), false); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java index 2951a46c86e54..0192e9ea99b9e 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java @@ -70,7 +70,7 @@ void restorePreviousContextManager() { @Test void assertInit() { MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData()); - ProxyContext.init(new ContextManager(metaDataContexts, mock(ComputeNodeInstanceContext.class, RETURNS_DEEP_STUBS), mock(PersistRepository.class))); + ProxyContext.init(new ContextManager(metaDataContexts, mock(ComputeNodeInstanceContext.class, RETURNS_DEEP_STUBS), mock(PersistRepository.class), false)); assertThat(ProxyContext.getInstance().getContextManager().getStateContext(), is(ProxyContext.getInstance().getContextManager().getStateContext())); assertThat(ProxyContext.getInstance().getContextManager().getStateContext().getCurrentClusterState(), is(ClusterState.OK)); assertThat(ProxyContext.getInstance().getContextManager().getMetaDataContexts(), is(ProxyContext.getInstance().getContextManager().getMetaDataContexts())); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/DatabaseAdminQueryBackendHandlerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/DatabaseAdminQueryBackendHandlerTest.java index a2743076f67a2..6bf2b32e076e3 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/DatabaseAdminQueryBackendHandlerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/DatabaseAdminQueryBackendHandlerTest.java @@ -112,12 +112,13 @@ void assertGetRowData() throws SQLException { private ContextManager mockContextManager() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class); + when(database.getResourceMetaData()).thenReturn(mock(ResourceMetaData.class)); when(database.getProtocolType()).thenReturn(databaseType); when(ProxyContext.getInstance().getContextManager().getDatabase("foo_db")).thenReturn(database); ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); ComputeNodeInstanceContext computeNodeInstanceContext = mock(ComputeNodeInstanceContext.class); when(computeNodeInstanceContext.getModeConfiguration()).thenReturn(mock(ModeConfiguration.class)); - return new ContextManager(MetaDataContextsFactory.create(mock(MetaDataPersistService.class), metaData), computeNodeInstanceContext, mock(PersistRepository.class)); + return new ContextManager(MetaDataContextsFactory.create(mock(MetaDataPersistService.class), metaData), computeNodeInstanceContext, mock(PersistRepository.class), false); } } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/QueryableRALBackendHandlerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/QueryableRALBackendHandlerTest.java index b889c72c71345..3a79734a741f9 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/QueryableRALBackendHandlerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/QueryableRALBackendHandlerTest.java @@ -76,7 +76,7 @@ void assertExecuteWithUnknownDatabase() { when(connectionSession.getDatabaseName()).thenReturn("unknown"); ComputeNodeInstanceContext computeNodeInstanceContext = mock(ComputeNodeInstanceContext.class); when(computeNodeInstanceContext.getModeConfiguration()).thenReturn(mock(ModeConfiguration.class)); - ContextManager contextManager = new ContextManager(metaDataContexts, computeNodeInstanceContext, mock(PersistRepository.class)); + ContextManager contextManager = new ContextManager(metaDataContexts, computeNodeInstanceContext, mock(PersistRepository.class), false); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); assertThrows(UnknownDatabaseException.class, () -> new DistSQLQueryBackendHandler(mock(ExportDatabaseConfigurationStatement.class), connectionSession).execute()); } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutorTest.java index 37c543c9cd655..256ddc0fd794c 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutorTest.java @@ -111,7 +111,7 @@ private ContextManager mockContextManager() { ContextManager result = new ContextManager(MetaDataContextsFactory.create(metaDataPersistService, new ShardingSphereMetaData()), new ComputeNodeInstanceContext(new ComputeNodeInstance(mock(InstanceMetaData.class)), mock(WorkerIdGenerator.class), new ModeConfiguration("Standalone", null), mock(LockContext.class), new EventBusContext()), - mock(PersistRepository.class)); + mock(PersistRepository.class), false); return result; } } diff --git a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java index dda70c3a2289f..f2e43d6e39ab9 100644 --- a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java +++ b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java @@ -101,6 +101,6 @@ private ContextManager mockContextManager(final Map