From 86d9951c6038775e387074f7c8298cd73d2a88bb Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Tue, 30 Jan 2024 21:30:12 +0800 Subject: [PATCH] Refactor DatabaseRuleDefinitionExecutor.checkBeforeUpdate (#29912) --- .../CreateBroadcastTableRuleExecutor.java | 44 ++++++------ .../DropBroadcastTableRuleExecutor.java | 17 +++-- .../CreateBroadcastTableRuleExecutorTest.java | 22 ++++-- .../DropBroadcastTableRuleExecutorTest.java | 6 +- .../update/AlterEncryptRuleExecutor.java | 20 +++--- .../update/CreateEncryptRuleExecutor.java | 25 ++++--- .../update/DropEncryptRuleExecutor.java | 20 +++--- .../update/AlterEncryptRuleExecutorTest.java | 17 ++++- .../update/CreateEncryptRuleExecutorTest.java | 25 +++++-- .../update/DropEncryptRuleExecutorTest.java | 14 +++- .../handler/update/AlterMaskRuleExecutor.java | 19 ++--- .../update/CreateMaskRuleExecutor.java | 27 +++---- .../handler/update/DropMaskRuleExecutor.java | 19 ++--- .../update/AlterMaskRuleExecutorTest.java | 18 +++-- .../update/CreateMaskRuleExecutorTest.java | 23 ++++-- .../update/DropMaskRuleExecutorTest.java | 17 +++-- .../AlterReadwriteSplittingRuleExecutor.java | 13 ++-- .../CreateReadwriteSplittingRuleExecutor.java | 13 ++-- .../DropReadwriteSplittingRuleExecutor.java | 16 +++-- ...terReadwriteSplittingRuleExecutorTest.java | 38 +++++++--- ...ateReadwriteSplittingRuleExecutorTest.java | 36 +++++++--- ...ropReadwriteSplittingRuleExecutorTest.java | 16 +++-- .../AlterDefaultShadowAlgorithmExecutor.java | 17 +++-- .../update/AlterShadowRuleExecutor.java | 17 +++-- .../CreateDefaultShadowAlgorithmExecutor.java | 60 ++++++++-------- .../update/CreateShadowRuleExecutor.java | 19 ++--- .../DropDefaultShadowAlgorithmExecutor.java | 17 +++-- .../update/DropShadowAlgorithmExecutor.java | 31 ++++---- .../update/DropShadowRuleExecutor.java | 23 +++--- ...terDefaultShadowAlgorithmExecutorTest.java | 18 +++-- .../update/AlterShadowRuleExecutorTest.java | 36 ++++++++-- ...ateDefaultShadowAlgorithmExecutorTest.java | 19 +++-- .../update/CreateShadowRuleExecutorTest.java | 37 +++++++--- ...ropDefaultShadowAlgorithmExecutorTest.java | 18 +++-- .../DropShadowAlgorithmExecutorTest.java | 14 +++- .../update/DropShadowRuleExecutorTest.java | 22 ++++-- .../AlterDefaultShardingStrategyExecutor.java | 25 ++++--- ...terShardingTableReferenceRuleExecutor.java | 49 +++++++------ .../AlterShardingTableRuleExecutor.java | 13 ++-- ...CreateDefaultShardingStrategyExecutor.java | 34 +++++---- ...ateShardingTableReferenceRuleExecutor.java | 55 +++++++------- .../CreateShardingTableRuleExecutor.java | 13 ++-- .../DropDefaultShardingStrategyExecutor.java | 23 +++--- .../update/DropShardingAlgorithmExecutor.java | 47 ++++++------ .../update/DropShardingAuditorExecutor.java | 31 ++++---- .../DropShardingKeyGeneratorExecutor.java | 31 ++++---- .../DropShardingTableReferenceExecutor.java | 29 ++++---- .../update/DropShardingTableRuleExecutor.java | 33 +++++---- ...erDefaultShardingStrategyExecutorTest.java | 54 +++++++++----- ...hardingTableReferenceRuleExecutorTest.java | 12 +++- .../AlterShardingTableRuleExecutorTest.java | 16 ++++- ...teDefaultShardingStrategyExecutorTest.java | 72 +++++++++++++------ ...hardingTableReferenceRuleExecutorTest.java | 17 ++++- .../CreateShardingTableRuleExecutorTest.java | 27 ++++--- ...opDefaultShardingStrategyExecutorTest.java | 15 +++- .../DropShardingAlgorithmExecutorTest.java | 28 ++++++-- .../DropShardingAuditorExecutorTest.java | 16 ++++- .../DropShardingKeyGeneratorExecutorTest.java | 20 ++++-- ...ropShardingTableReferenceExecutorTest.java | 31 ++++++-- .../DropShardingTableRuleExecutorTest.java | 17 ++++- .../DatabaseRuleDefinitionExecuteEngine.java | 22 +++--- .../database/DatabaseRuleAlterExecutor.java | 12 ++-- .../database/DatabaseRuleCreateExecutor.java | 10 +-- .../DatabaseRuleDefinitionExecutor.java | 17 ++--- .../database/DatabaseRuleDropExecutor.java | 18 ++--- .../update/LoadSingleTableExecutor.java | 13 ++-- ...DefaultSingleTableStorageUnitExecutor.java | 13 ++-- .../update/UnloadSingleTableExecutor.java | 20 +++--- .../update/LoadSingleTableExecutorTest.java | 4 +- ...ultSingleTableStorageUnitExecutorTest.java | 3 +- ...cyDatabaseRuleDefinitionExecuteEngine.java | 25 +++---- .../FixtureDatabaseRuleCreateExecutor.java | 12 ++-- .../handler/distsql/fixture/FixtureRule.java | 29 ++++++++ 73 files changed, 1101 insertions(+), 618 deletions(-) create mode 100644 proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/FixtureRule.java diff --git a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleExecutor.java b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleExecutor.java index 70d8d93b9b2e8..ec7e7246c885c 100644 --- a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleExecutor.java +++ b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleExecutor.java @@ -20,6 +20,7 @@ import lombok.Setter; import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration; import org.apache.shardingsphere.broadcast.distsql.statement.CreateBroadcastTableRuleStatement; +import org.apache.shardingsphere.broadcast.rule.BroadcastRule; import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException; import org.apache.shardingsphere.distsql.handler.exception.storageunit.EmptyStorageUnitException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleCreateExecutor; @@ -27,30 +28,43 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import java.util.Collection; -import java.util.LinkedHashSet; +import java.util.Collections; +import java.util.HashSet; /** * Create broadcast table rule executor. */ @Setter -public final class CreateBroadcastTableRuleExecutor implements DatabaseRuleCreateExecutor { +public final class CreateBroadcastTableRuleExecutor implements DatabaseRuleCreateExecutor { private ShardingSphereDatabase database; + private BroadcastRule rule; + @Override - public void checkBeforeUpdate(final CreateBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final CreateBroadcastTableRuleStatement sqlStatement) { ShardingSpherePreconditions.checkState(!database.getResourceMetaData().getStorageUnits().isEmpty(), () -> new EmptyStorageUnitException(database.getName())); if (!sqlStatement.isIfNotExists()) { - checkDuplicate(sqlStatement, currentRuleConfig); + checkDuplicate(sqlStatement); } } + private void checkDuplicate(final CreateBroadcastTableRuleStatement sqlStatement) { + Collection duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement); + ShardingSpherePreconditions.checkState(duplicatedRuleNames.isEmpty(), () -> new DuplicateRuleException("Broadcast", sqlStatement.getTables())); + } + + private Collection getDuplicatedRuleNames(final CreateBroadcastTableRuleStatement sqlStatement) { + Collection result = new HashSet<>(null == rule ? Collections.emptySet() : rule.getTables()); + result.retainAll(sqlStatement.getTables()); + return result; + } + @Override public BroadcastRuleConfiguration buildToBeCreatedRuleConfiguration(final CreateBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) { Collection tables = sqlStatement.getTables(); if (sqlStatement.isIfNotExists()) { - Collection duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement, currentRuleConfig); - tables.removeIf(duplicatedRuleNames::contains); + tables.removeIf(getDuplicatedRuleNames(sqlStatement)::contains); } return new BroadcastRuleConfiguration(tables); } @@ -60,23 +74,9 @@ public void updateCurrentRuleConfiguration(final BroadcastRuleConfiguration curr currentRuleConfig.getTables().addAll(toBeCreatedRuleConfig.getTables()); } - private void checkDuplicate(final CreateBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) { - Collection duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement, currentRuleConfig); - ShardingSpherePreconditions.checkState(duplicatedRuleNames.isEmpty(), () -> new DuplicateRuleException("Broadcast", sqlStatement.getTables())); - } - - private Collection getDuplicatedRuleNames(final CreateBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) { - Collection result = new LinkedHashSet<>(); - if (null != currentRuleConfig && !currentRuleConfig.getTables().isEmpty()) { - result.addAll(currentRuleConfig.getTables()); - } - result.retainAll(sqlStatement.getTables()); - return result; - } - @Override - public Class getRuleConfigurationClass() { - return BroadcastRuleConfiguration.class; + public Class getRuleClass() { + return BroadcastRule.class; } @Override diff --git a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleExecutor.java b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleExecutor.java index 54af6295ba449..83e6bcef24788 100644 --- a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleExecutor.java +++ b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleExecutor.java @@ -20,6 +20,7 @@ import lombok.Setter; import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration; import org.apache.shardingsphere.broadcast.distsql.statement.DropBroadcastTableRuleStatement; +import org.apache.shardingsphere.broadcast.rule.BroadcastRule; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; import org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorCurrentRuleRequired; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor; @@ -35,19 +36,21 @@ */ @DistSQLExecutorCurrentRuleRequired("Broadcast") @Setter -public final class DropBroadcastTableRuleExecutor implements DatabaseRuleDropExecutor { +public final class DropBroadcastTableRuleExecutor implements DatabaseRuleDropExecutor { private ShardingSphereDatabase database; + private BroadcastRule rule; + @Override - public void checkBeforeUpdate(final DropBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropBroadcastTableRuleStatement sqlStatement) { if (!sqlStatement.isIfExists()) { - checkBroadcastTableRuleExist(sqlStatement, currentRuleConfig); + checkBroadcastTableRuleExist(sqlStatement); } } - private void checkBroadcastTableRuleExist(final DropBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) { - Collection currentRules = currentRuleConfig.getTables(); + private void checkBroadcastTableRuleExist(final DropBroadcastTableRuleStatement sqlStatement) { + Collection currentRules = rule.getConfiguration().getTables(); Collection notExistRules = sqlStatement.getTables().stream().filter(each -> !containsIgnoreCase(currentRules, each)).collect(Collectors.toList()); ShardingSpherePreconditions.checkState(notExistRules.isEmpty(), () -> new MissingRequiredRuleException("Broadcast", database.getName(), notExistRules)); } @@ -75,8 +78,8 @@ public boolean updateCurrentRuleConfiguration(final DropBroadcastTableRuleStatem } @Override - public Class getRuleConfigurationClass() { - return BroadcastRuleConfiguration.class; + public Class getRuleClass() { + return BroadcastRule.class; } @Override diff --git a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleExecutorTest.java b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleExecutorTest.java index 3a44d0d7175c6..5b32464bebe0c 100644 --- a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleExecutorTest.java +++ b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleExecutorTest.java @@ -19,6 +19,7 @@ import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration; import org.apache.shardingsphere.broadcast.distsql.statement.CreateBroadcastTableRuleStatement; +import org.apache.shardingsphere.broadcast.rule.BroadcastRule; import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException; import org.apache.shardingsphere.distsql.handler.exception.storageunit.EmptyStorageUnitException; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; @@ -43,28 +44,37 @@ class CreateBroadcastTableRuleExecutorTest { void assertCheckSQLStatementWithEmptyStorageUnit() { BroadcastRuleConfiguration currentConfig = mock(BroadcastRuleConfiguration.class); when(currentConfig.getTables()).thenReturn(Collections.singleton("t_address")); - CreateBroadcastTableRuleStatement sqlStatement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address")); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getResourceMetaData().getStorageUnits()).thenReturn(Collections.emptyMap()); executor.setDatabase(database); - assertThrows(EmptyStorageUnitException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); + BroadcastRule rule = mock(BroadcastRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + CreateBroadcastTableRuleStatement sqlStatement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address")); + assertThrows(EmptyStorageUnitException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test void assertCheckSQLStatementWithDuplicateBroadcastRule() { BroadcastRuleConfiguration currentConfig = mock(BroadcastRuleConfiguration.class); when(currentConfig.getTables()).thenReturn(Collections.singleton("t_address")); - CreateBroadcastTableRuleStatement sqlStatement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address")); executor.setDatabase(mockShardingSphereDatabase()); - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); + BroadcastRule rule = mock(BroadcastRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + CreateBroadcastTableRuleStatement sqlStatement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address")); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test void assertBuildToBeCreatedRuleConfiguration() { BroadcastRuleConfiguration currentConfig = new BroadcastRuleConfiguration(new LinkedList<>()); - CreateBroadcastTableRuleStatement sqlStatement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address")); executor.setDatabase(mockShardingSphereDatabase()); - executor.checkBeforeUpdate(sqlStatement, currentConfig); + BroadcastRule rule = mock(BroadcastRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + CreateBroadcastTableRuleStatement sqlStatement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address")); + executor.checkBeforeUpdate(sqlStatement); BroadcastRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement, currentConfig); executor.updateCurrentRuleConfiguration(currentConfig, toBeCreatedRuleConfig); assertThat(currentConfig.getTables().size(), is(1)); diff --git a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleExecutorTest.java b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleExecutorTest.java index 3ae7e25a9cae4..7773815108738 100644 --- a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleExecutorTest.java +++ b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleExecutorTest.java @@ -19,6 +19,7 @@ import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration; import org.apache.shardingsphere.broadcast.distsql.statement.DropBroadcastTableRuleStatement; +import org.apache.shardingsphere.broadcast.rule.BroadcastRule; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.junit.jupiter.api.BeforeEach; @@ -47,7 +48,10 @@ void setUp() { @Test void assertCheckSQLStatementWithoutToBeDroppedRule() { DropBroadcastTableRuleStatement sqlStatement = new DropBroadcastTableRuleStatement(false, Collections.singleton("t_address")); - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement, new BroadcastRuleConfiguration(Collections.emptyList()))); + BroadcastRule rule = mock(BroadcastRule.class); + when(rule.getConfiguration()).thenReturn(new BroadcastRuleConfiguration(Collections.emptyList())); + executor.setRule(rule); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test diff --git a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleExecutor.java b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleExecutor.java index 1fa59165d6b8a..be98f31be6b52 100644 --- a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleExecutor.java +++ b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleExecutor.java @@ -30,9 +30,10 @@ import org.apache.shardingsphere.encrypt.distsql.handler.converter.EncryptRuleStatementConverter; import org.apache.shardingsphere.encrypt.distsql.segment.EncryptRuleSegment; import org.apache.shardingsphere.encrypt.distsql.statement.AlterEncryptRuleStatement; +import org.apache.shardingsphere.encrypt.rule.EncryptRule; import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import java.util.Collection; @@ -46,19 +47,22 @@ */ @DistSQLExecutorCurrentRuleRequired("Encrypt") @Setter -public final class AlterEncryptRuleExecutor implements DatabaseRuleAlterExecutor { +public final class AlterEncryptRuleExecutor implements DatabaseRuleAlterExecutor { private ShardingSphereDatabase database; + private EncryptRule rule; + @Override - public void checkBeforeUpdate(final AlterEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { - checkToBeAlteredRules(sqlStatement, currentRuleConfig); + public void checkBeforeUpdate(final AlterEncryptRuleStatement sqlStatement) { + checkToBeAlteredRules(sqlStatement); checkColumnNames(sqlStatement); checkToBeAlteredEncryptors(sqlStatement); } - private void checkToBeAlteredRules(final AlterEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { - Collection currentEncryptTableNames = currentRuleConfig.getTables().stream().map(EncryptTableRuleConfiguration::getName).collect(Collectors.toList()); + private void checkToBeAlteredRules(final AlterEncryptRuleStatement sqlStatement) { + Collection currentEncryptTableNames = ((EncryptRuleConfiguration) rule.getConfiguration()).getTables() + .stream().map(EncryptTableRuleConfiguration::getName).collect(Collectors.toList()); Collection notExistEncryptTableNames = getToBeAlteredEncryptTableNames(sqlStatement).stream().filter(each -> !currentEncryptTableNames.contains(each)).collect(Collectors.toList()); if (!notExistEncryptTableNames.isEmpty()) { throw new MissingRequiredRuleException("Encrypt", database.getName(), notExistEncryptTableNames); @@ -134,8 +138,8 @@ private void dropUnusedEncryptor(final EncryptRuleConfiguration currentRuleConfi } @Override - public Class getRuleConfigurationClass() { - return EncryptRuleConfiguration.class; + public Class getRuleClass() { + return EncryptRule.class; } @Override diff --git a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleExecutor.java b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleExecutor.java index df11c50247067..884e5642de0fd 100644 --- a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleExecutor.java +++ b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleExecutor.java @@ -30,6 +30,7 @@ import org.apache.shardingsphere.encrypt.distsql.segment.EncryptColumnItemSegment; import org.apache.shardingsphere.encrypt.distsql.segment.EncryptRuleSegment; import org.apache.shardingsphere.encrypt.distsql.statement.CreateEncryptRuleStatement; +import org.apache.shardingsphere.encrypt.rule.EncryptRule; import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; @@ -44,14 +45,16 @@ * Create encrypt rule executor. */ @Setter -public final class CreateEncryptRuleExecutor implements DatabaseRuleCreateExecutor { +public final class CreateEncryptRuleExecutor implements DatabaseRuleCreateExecutor { private ShardingSphereDatabase database; + private EncryptRule rule; + @Override - public void checkBeforeUpdate(final CreateEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final CreateEncryptRuleStatement sqlStatement) { if (!sqlStatement.isIfNotExists()) { - checkDuplicateRuleNames(sqlStatement, currentRuleConfig); + checkDuplicateRuleNames(sqlStatement); } checkColumnNames(sqlStatement); checkAlgorithmTypes(sqlStatement); @@ -59,15 +62,15 @@ public void checkBeforeUpdate(final CreateEncryptRuleStatement sqlStatement, fin checkDataSources(); } - private void checkDuplicateRuleNames(final CreateEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { - Collection duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement, currentRuleConfig); + private void checkDuplicateRuleNames(final CreateEncryptRuleStatement sqlStatement) { + Collection duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement); ShardingSpherePreconditions.checkState(duplicatedRuleNames.isEmpty(), () -> new DuplicateRuleException("encrypt", database.getName(), duplicatedRuleNames)); } - private Collection getDuplicatedRuleNames(final CreateEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { + private Collection getDuplicatedRuleNames(final CreateEncryptRuleStatement sqlStatement) { Collection currentRuleNames = new LinkedHashSet<>(); - if (null != currentRuleConfig) { - currentRuleNames = currentRuleConfig.getTables().stream().map(EncryptTableRuleConfiguration::getName).collect(Collectors.toSet()); + if (null != rule) { + currentRuleNames = ((EncryptRuleConfiguration) rule.getConfiguration()).getTables().stream().map(EncryptTableRuleConfiguration::getName).collect(Collectors.toSet()); } return sqlStatement.getRules().stream().map(EncryptRuleSegment::getTableName).filter(currentRuleNames::contains).collect(Collectors.toSet()); } @@ -139,7 +142,7 @@ private void checkDataSources() { public EncryptRuleConfiguration buildToBeCreatedRuleConfiguration(final CreateEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { Collection segments = sqlStatement.getRules(); if (sqlStatement.isIfNotExists()) { - Collection duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement, currentRuleConfig); + Collection duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement); segments.removeIf(each -> duplicatedRuleNames.contains(each.getTableName())); } return EncryptRuleStatementConverter.convert(segments); @@ -152,8 +155,8 @@ public void updateCurrentRuleConfiguration(final EncryptRuleConfiguration curren } @Override - public Class getRuleConfigurationClass() { - return EncryptRuleConfiguration.class; + public Class getRuleClass() { + return EncryptRule.class; } @Override diff --git a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleExecutor.java b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleExecutor.java index 77b230a44e58f..8e6af31fb6011 100644 --- a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleExecutor.java +++ b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleExecutor.java @@ -25,9 +25,10 @@ import org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnItemRuleConfiguration; import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration; import org.apache.shardingsphere.encrypt.distsql.statement.DropEncryptRuleStatement; +import org.apache.shardingsphere.encrypt.rule.EncryptRule; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import java.util.Collection; import java.util.Collections; @@ -42,19 +43,22 @@ */ @DistSQLExecutorCurrentRuleRequired("Encrypt") @Setter -public final class DropEncryptRuleExecutor implements DatabaseRuleDropExecutor { +public final class DropEncryptRuleExecutor implements DatabaseRuleDropExecutor { private ShardingSphereDatabase database; + private EncryptRule rule; + @Override - public void checkBeforeUpdate(final DropEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropEncryptRuleStatement sqlStatement) { if (!sqlStatement.isIfExists()) { - checkToBeDroppedEncryptTableNames(sqlStatement, currentRuleConfig); + checkToBeDroppedEncryptTableNames(sqlStatement); } } - private void checkToBeDroppedEncryptTableNames(final DropEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { - Collection currentEncryptTableNames = currentRuleConfig.getTables().stream().map(EncryptTableRuleConfiguration::getName).collect(Collectors.toList()); + private void checkToBeDroppedEncryptTableNames(final DropEncryptRuleStatement sqlStatement) { + Collection currentEncryptTableNames = ((EncryptRuleConfiguration) rule.getConfiguration()).getTables() + .stream().map(EncryptTableRuleConfiguration::getName).collect(Collectors.toList()); Collection notExistedTableNames = sqlStatement.getTables().stream().filter(each -> !currentEncryptTableNames.contains(each)).collect(Collectors.toList()); ShardingSpherePreconditions.checkState(notExistedTableNames.isEmpty(), () -> new MissingRequiredRuleException("Encrypt", database.getName(), notExistedTableNames)); } @@ -106,8 +110,8 @@ private Collection findUnusedEncryptors(final EncryptRuleConfiguration c } @Override - public Class getRuleConfigurationClass() { - return EncryptRuleConfiguration.class; + public Class getRuleClass() { + return EncryptRule.class; } @Override diff --git a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleExecutorTest.java b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleExecutorTest.java index f6724ff06fa51..f2e2d3ddcee13 100644 --- a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleExecutorTest.java +++ b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleExecutorTest.java @@ -28,6 +28,7 @@ import org.apache.shardingsphere.encrypt.distsql.segment.EncryptColumnSegment; import org.apache.shardingsphere.encrypt.distsql.segment.EncryptRuleSegment; import org.apache.shardingsphere.encrypt.distsql.statement.AlterEncryptRuleStatement; +import org.apache.shardingsphere.encrypt.rule.EncryptRule; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundException; @@ -45,6 +46,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class AlterEncryptRuleExecutorTest { @@ -57,18 +59,27 @@ void setUp() { @Test void assertCheckSQLStatementWithoutToBeAlteredRules() { + EncryptRule rule = mock(EncryptRule.class); + when(rule.getConfiguration()).thenReturn(new EncryptRuleConfiguration(Collections.emptyList(), Collections.emptyMap())); + executor.setRule(rule); assertThrows(MissingRequiredRuleException.class, - () -> executor.checkBeforeUpdate(createSQLStatement("MD5"), new EncryptRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); + () -> executor.checkBeforeUpdate(createSQLStatement("MD5"))); } @Test void assertCheckSQLStatementWithoutToBeAlteredEncryptors() { - assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(createSQLStatement("INVALID_TYPE"), createCurrentRuleConfiguration())); + EncryptRule rule = mock(EncryptRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration()); + executor.setRule(rule); + assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(createSQLStatement("INVALID_TYPE"))); } @Test void assertCheckSQLStatementWithConflictColumnNames() { - assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(createConflictColumnNameSQLStatement(), createCurrentRuleConfiguration())); + EncryptRule rule = mock(EncryptRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration()); + executor.setRule(rule); + assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(createConflictColumnNameSQLStatement())); } @Test diff --git a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleExecutorTest.java b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleExecutorTest.java index 44960d09b9c3c..074c0f435fa91 100644 --- a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleExecutorTest.java +++ b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleExecutorTest.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.encrypt.distsql.segment.EncryptRuleSegment; import org.apache.shardingsphere.encrypt.distsql.statement.CreateEncryptRuleStatement; import org.apache.shardingsphere.encrypt.exception.algorithm.EncryptAlgorithmInitializationException; +import org.apache.shardingsphere.encrypt.rule.EncryptRule; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundException; import org.apache.shardingsphere.test.util.PropertiesBuilder; @@ -46,6 +47,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class CreateEncryptRuleExecutorTest { @@ -58,24 +60,33 @@ void setUp() { @Test void assertCheckSQLStatementWithDuplicateEncryptRule() { - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "MD5"), getCurrentRuleConfig())); + EncryptRule rule = mock(EncryptRule.class); + when(rule.getConfiguration()).thenReturn(getCurrentRuleConfig()); + executor.setRule(rule); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "MD5"))); } @Test void assertCheckSQLStatementWithoutToBeCreatedEncryptors() { - assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "INVALID_TYPE"), null)); + assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "INVALID_TYPE"))); } @Test void assertCheckSQLStatementWithConflictColumnNames() { - assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(createConflictColumnNameSQLStatement(), getCurrentRuleConfig())); + EncryptRule rule = mock(EncryptRule.class); + when(rule.getConfiguration()).thenReturn(getCurrentRuleConfig()); + executor.setRule(rule); + assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(createConflictColumnNameSQLStatement())); } @Test void assertCreateEncryptRuleWithIfNotExists() { EncryptRuleConfiguration currentRuleConfig = getCurrentRuleConfig(); + EncryptRule rule = mock(EncryptRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); CreateEncryptRuleStatement sqlStatement = createAESEncryptRuleSQLStatement(true); - executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); + executor.checkBeforeUpdate(sqlStatement); EncryptRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); assertThat(currentRuleConfig.getTables().size(), is(2)); @@ -127,9 +138,11 @@ private EncryptRuleConfiguration getCurrentRuleConfig() { @Test void assertCreateAESEncryptRuleWithPropertiesNotExists() { - EncryptRuleConfiguration currentRuleConfig = getCurrentRuleConfig(); CreateEncryptRuleStatement sqlStatement = createWrongAESEncryptorSQLStatement(); - assertThrows(EncryptAlgorithmInitializationException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentRuleConfig)); + EncryptRule rule = mock(EncryptRule.class); + when(rule.getConfiguration()).thenReturn(getCurrentRuleConfig()); + executor.setRule(rule); + assertThrows(EncryptAlgorithmInitializationException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } private CreateEncryptRuleStatement createWrongAESEncryptorSQLStatement() { diff --git a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleExecutorTest.java b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleExecutorTest.java index 05a32c3a6ed77..26571a74d2686 100644 --- a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleExecutorTest.java +++ b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleExecutorTest.java @@ -23,6 +23,7 @@ import org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration; import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration; import org.apache.shardingsphere.encrypt.distsql.statement.DropEncryptRuleStatement; +import org.apache.shardingsphere.encrypt.rule.EncryptRule; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.junit.jupiter.api.BeforeEach; @@ -42,6 +43,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class DropEncryptRuleExecutorTest { @@ -54,8 +56,11 @@ void setUp() { @Test void assertCheckSQLStatementWithoutToBeDroppedRule() { + EncryptRule rule = mock(EncryptRule.class); + when(rule.getConfiguration()).thenReturn(new EncryptRuleConfiguration(Collections.emptyList(), Collections.emptyMap())); + executor.setRule(rule); assertThrows(MissingRequiredRuleException.class, - () -> executor.checkBeforeUpdate(createSQLStatement("t_encrypt"), new EncryptRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); + () -> executor.checkBeforeUpdate(createSQLStatement("t_encrypt"))); } @Test @@ -74,9 +79,12 @@ void assertUpdateCurrentRuleConfigurationWithInUsedEncryptor() { @Test void assertUpdateCurrentRuleConfigurationWithIfExists() { - EncryptRuleConfiguration ruleConfig = createCurrentRuleConfiguration(); DropEncryptRuleStatement statement = createSQLStatement(true, "t_encrypt_1"); - executor.checkBeforeUpdate(statement, mock(EncryptRuleConfiguration.class)); + EncryptRule rule = mock(EncryptRule.class); + when(rule.getConfiguration()).thenReturn(new EncryptRuleConfiguration(Collections.emptyList(), Collections.emptyMap())); + executor.setRule(rule); + executor.checkBeforeUpdate(statement); + EncryptRuleConfiguration ruleConfig = createCurrentRuleConfiguration(); assertFalse(executor.updateCurrentRuleConfiguration(statement, ruleConfig)); assertThat(ruleConfig.getEncryptors().size(), is(3)); } diff --git a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleExecutor.java b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleExecutor.java index 81b55d704b0cf..5d76f0dd721ce 100644 --- a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleExecutor.java +++ b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleExecutor.java @@ -22,14 +22,15 @@ import org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorCurrentRuleRequired; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleAlterExecutor; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration; import org.apache.shardingsphere.mask.api.config.rule.MaskColumnRuleConfiguration; import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration; import org.apache.shardingsphere.mask.distsql.handler.converter.MaskRuleStatementConverter; import org.apache.shardingsphere.mask.distsql.segment.MaskRuleSegment; import org.apache.shardingsphere.mask.distsql.statement.AlterMaskRuleStatement; +import org.apache.shardingsphere.mask.rule.MaskRule; import java.util.Collection; import java.util.Collections; @@ -42,17 +43,19 @@ */ @DistSQLExecutorCurrentRuleRequired("Mask") @Setter -public final class AlterMaskRuleExecutor implements DatabaseRuleAlterExecutor { +public final class AlterMaskRuleExecutor implements DatabaseRuleAlterExecutor { private ShardingSphereDatabase database; + private MaskRule rule; + @Override - public void checkBeforeUpdate(final AlterMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { - checkToBeAlteredRules(sqlStatement, currentRuleConfig); + public void checkBeforeUpdate(final AlterMaskRuleStatement sqlStatement) { + checkToBeAlteredRules(sqlStatement); } - private void checkToBeAlteredRules(final AlterMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { - Collection currentMaskTableNames = currentRuleConfig.getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toList()); + private void checkToBeAlteredRules(final AlterMaskRuleStatement sqlStatement) { + Collection currentMaskTableNames = rule.getConfiguration().getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toList()); Collection notExistedMaskTableNames = getToBeAlteredMaskTableNames(sqlStatement).stream().filter(each -> !currentMaskTableNames.contains(each)).collect(Collectors.toList()); ShardingSpherePreconditions.checkState(notExistedMaskTableNames.isEmpty(), () -> new MissingRequiredRuleException("Mask", database.getName(), notExistedMaskTableNames)); } @@ -107,8 +110,8 @@ private void dropUnusedAlgorithms(final MaskRuleConfiguration currentRuleConfig) } @Override - public Class getRuleConfigurationClass() { - return MaskRuleConfiguration.class; + public Class getRuleClass() { + return MaskRule.class; } @Override diff --git a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleExecutor.java b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleExecutor.java index d858397ab1e57..999e890e7eb32 100644 --- a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleExecutor.java +++ b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleExecutor.java @@ -20,8 +20,8 @@ import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleCreateExecutor; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration; import org.apache.shardingsphere.mask.api.config.rule.MaskColumnRuleConfiguration; @@ -30,6 +30,7 @@ import org.apache.shardingsphere.mask.distsql.segment.MaskColumnSegment; import org.apache.shardingsphere.mask.distsql.segment.MaskRuleSegment; import org.apache.shardingsphere.mask.distsql.statement.CreateMaskRuleStatement; +import org.apache.shardingsphere.mask.rule.MaskRule; import org.apache.shardingsphere.mask.spi.MaskAlgorithm; import java.util.Collection; @@ -40,24 +41,26 @@ * Create mask rule executor. */ @Setter -public final class CreateMaskRuleExecutor implements DatabaseRuleCreateExecutor { +public final class CreateMaskRuleExecutor implements DatabaseRuleCreateExecutor { private ShardingSphereDatabase database; + private MaskRule rule; + private boolean ifNotExists; @Override - public void checkBeforeUpdate(final CreateMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final CreateMaskRuleStatement sqlStatement) { ifNotExists = sqlStatement.isIfNotExists(); if (!ifNotExists) { - checkDuplicatedRuleNames(sqlStatement, currentRuleConfig); + checkDuplicatedRuleNames(sqlStatement); } checkAlgorithms(sqlStatement); } - private void checkDuplicatedRuleNames(final CreateMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { - if (null != currentRuleConfig) { - Collection currentRuleNames = currentRuleConfig.getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toList()); + private void checkDuplicatedRuleNames(final CreateMaskRuleStatement sqlStatement) { + if (null != rule) { + Collection currentRuleNames = rule.getConfiguration().getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toList()); Collection duplicatedRuleNames = sqlStatement.getRules().stream().map(MaskRuleSegment::getTableName).filter(currentRuleNames::contains).collect(Collectors.toList()); ShardingSpherePreconditions.checkState(duplicatedRuleNames.isEmpty(), () -> new DuplicateRuleException("mask", database.getName(), duplicatedRuleNames)); } @@ -69,11 +72,6 @@ private void checkAlgorithms(final CreateMaskRuleStatement sqlStatement) { columns.forEach(each -> TypedSPILoader.checkService(MaskAlgorithm.class, each.getAlgorithm().getName(), each.getAlgorithm().getProps())); } - @Override - public Class getRuleConfigurationClass() { - return MaskRuleConfiguration.class; - } - @Override public MaskRuleConfiguration buildToBeCreatedRuleConfiguration(final CreateMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { return MaskRuleStatementConverter.convert(sqlStatement.getRules()); @@ -106,6 +104,11 @@ private void removeDuplicatedRules(final MaskRuleConfiguration currentRuleConfig toBeCreatedRuleConfig.getMaskAlgorithms().keySet().removeIf(toBeRemovedAlgorithms::contains); } + @Override + public Class getRuleClass() { + return MaskRule.class; + } + @Override public Class getType() { return CreateMaskRuleStatement.class; diff --git a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleExecutor.java b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleExecutor.java index b4aafe2abbfa9..e08f6f3a97b72 100644 --- a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleExecutor.java +++ b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleExecutor.java @@ -22,12 +22,13 @@ import org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorCurrentRuleRequired; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration; import org.apache.shardingsphere.mask.api.config.rule.MaskColumnRuleConfiguration; import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration; import org.apache.shardingsphere.mask.distsql.statement.DropMaskRuleStatement; +import org.apache.shardingsphere.mask.rule.MaskRule; import java.util.Collection; import java.util.Collections; @@ -42,19 +43,21 @@ */ @DistSQLExecutorCurrentRuleRequired("Mask") @Setter -public final class DropMaskRuleExecutor implements DatabaseRuleDropExecutor { +public final class DropMaskRuleExecutor implements DatabaseRuleDropExecutor { private ShardingSphereDatabase database; + private MaskRule rule; + @Override - public void checkBeforeUpdate(final DropMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropMaskRuleStatement sqlStatement) { if (!sqlStatement.isIfExists()) { - checkToBeDroppedMaskTableNames(sqlStatement, currentRuleConfig); + checkToBeDroppedMaskTableNames(sqlStatement); } } - private void checkToBeDroppedMaskTableNames(final DropMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { - Collection currentMaskTableNames = currentRuleConfig.getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toList()); + private void checkToBeDroppedMaskTableNames(final DropMaskRuleStatement sqlStatement) { + Collection currentMaskTableNames = rule.getConfiguration().getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toList()); Collection notExistedTableNames = sqlStatement.getTables().stream().filter(each -> !currentMaskTableNames.contains(each)).collect(Collectors.toList()); ShardingSpherePreconditions.checkState(notExistedTableNames.isEmpty(), () -> new MissingRequiredRuleException("Mask", database.getName(), notExistedTableNames)); } @@ -100,8 +103,8 @@ private Collection findUnusedAlgorithms(final MaskRuleConfiguration curr } @Override - public Class getRuleConfigurationClass() { - return MaskRuleConfiguration.class; + public Class getRuleClass() { + return MaskRule.class; } @Override diff --git a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleExecutorTest.java b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleExecutorTest.java index d1745749a6f74..862ec33367835 100644 --- a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleExecutorTest.java +++ b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleExecutorTest.java @@ -25,6 +25,7 @@ import org.apache.shardingsphere.mask.distsql.segment.MaskColumnSegment; import org.apache.shardingsphere.mask.distsql.segment.MaskRuleSegment; import org.apache.shardingsphere.mask.distsql.statement.AlterMaskRuleStatement; +import org.apache.shardingsphere.mask.rule.MaskRule; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -40,6 +41,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class AlterMaskRuleExecutorTest { @@ -52,13 +54,18 @@ void setUp() { @Test void assertCheckBeforeUpdateWithoutToBeAlteredRules() { - assertThrows(MissingRequiredRuleException.class, - () -> executor.checkBeforeUpdate(createSQLStatement("MD5"), new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); + MaskRule rule = mock(MaskRule.class); + when(rule.getConfiguration()).thenReturn(new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap())); + executor.setRule(rule); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("MD5"))); } @Test void assertCheckBeforeUpdateWithoutToBeAlteredAlgorithm() { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("INVALID_TYPE"), createCurrentRuleConfig())); + MaskRule rule = mock(MaskRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfig()); + executor.setRule(rule); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("INVALID_TYPE"))); } @Test @@ -66,7 +73,10 @@ void assertCheckBeforeUpdateWithIncompleteDataType() { MaskColumnSegment columnSegment = new MaskColumnSegment("user_id", new AlgorithmSegment("test", new Properties())); MaskRuleSegment ruleSegment = new MaskRuleSegment("t_mask", Collections.singleton(columnSegment)); AlterMaskRuleStatement statement = new AlterMaskRuleStatement(Collections.singleton(ruleSegment)); - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(statement, createCurrentRuleConfig())); + MaskRule rule = mock(MaskRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfig()); + executor.setRule(rule); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(statement)); } @Test diff --git a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleExecutorTest.java b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleExecutorTest.java index abbd8cc2f7a34..b7ed69f9d99b1 100644 --- a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleExecutorTest.java +++ b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleExecutorTest.java @@ -26,6 +26,7 @@ import org.apache.shardingsphere.mask.distsql.segment.MaskColumnSegment; import org.apache.shardingsphere.mask.distsql.segment.MaskRuleSegment; import org.apache.shardingsphere.mask.distsql.statement.CreateMaskRuleStatement; +import org.apache.shardingsphere.mask.rule.MaskRule; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -41,6 +42,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class CreateMaskRuleExecutorTest { @@ -53,19 +55,25 @@ void setUp() { @Test void assertCheckSQLStatementWithDuplicateMaskRule() { - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(createDuplicatedSQLStatement(false, "MD5"), getCurrentRuleConfig())); + MaskRule rule = mock(MaskRule.class); + when(rule.getConfiguration()).thenReturn(getCurrentRuleConfig()); + executor.setRule(rule); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(createDuplicatedSQLStatement(false, "MD5"))); } @Test void assertCheckSQLStatementWithInvalidAlgorithm() { - assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "INVALID_TYPE"), null)); + assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "INVALID_TYPE"))); } @Test void assertCreateMaskRule() { MaskRuleConfiguration currentRuleConfig = getCurrentRuleConfig(); CreateMaskRuleStatement sqlStatement = createSQLStatement(false, "MD5"); - executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); + MaskRule rule = mock(MaskRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); MaskRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); assertThat(currentRuleConfig.getTables().size(), is(4)); @@ -77,11 +85,16 @@ void assertCreateMaskRule() { void assertCreateMaskRuleWithIfNotExists() { MaskRuleConfiguration currentRuleConfig = getCurrentRuleConfig(); CreateMaskRuleStatement sqlStatement = createSQLStatement(false, "MD5"); - executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); + MaskRule rule = mock(MaskRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); MaskRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); sqlStatement = createSQLStatement(true, "MD5"); - executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); assertThat(currentRuleConfig.getTables().size(), is(4)); diff --git a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleExecutorTest.java b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleExecutorTest.java index ac0bcdfc4bb74..a81a40a74d134 100644 --- a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleExecutorTest.java +++ b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleExecutorTest.java @@ -23,6 +23,7 @@ import org.apache.shardingsphere.mask.api.config.rule.MaskColumnRuleConfiguration; import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration; import org.apache.shardingsphere.mask.distsql.statement.DropMaskRuleStatement; +import org.apache.shardingsphere.mask.rule.MaskRule; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -37,6 +38,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class DropMaskRuleExecutorTest { @@ -49,8 +51,10 @@ void setUp() { @Test void assertCheckSQLStatementWithoutToBeDroppedRule() { - assertThrows(MissingRequiredRuleException.class, - () -> executor.checkBeforeUpdate(createSQLStatement(false, "t_mask"), new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); + MaskRule rule = mock(MaskRule.class); + when(rule.getConfiguration()).thenReturn(new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap())); + executor.setRule(rule); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "t_mask"))); } @Test @@ -63,9 +67,14 @@ void assertUpdateCurrentRuleConfiguration() { @Test void assertUpdateCurrentRuleConfigurationWithIfExists() { - MaskRuleConfiguration ruleConfig = createCurrentRuleConfiguration(); DropMaskRuleStatement statement = createSQLStatement(true, "t_user"); - executor.checkBeforeUpdate(statement, mock(MaskRuleConfiguration.class)); + MaskRule rule = mock(MaskRule.class); + when(rule.getConfiguration()).thenReturn(new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap())); + executor.setRule(rule); + executor.checkBeforeUpdate(statement); + MaskRuleConfiguration ruleConfig = createCurrentRuleConfiguration(); + when(rule.getConfiguration()).thenReturn(ruleConfig); + executor.setRule(rule); assertFalse(executor.updateCurrentRuleConfiguration(statement, ruleConfig)); assertThat(ruleConfig.getTables().size(), is(1)); } diff --git a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleExecutor.java b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleExecutor.java index 71fd383875095..b1eaab81ca244 100644 --- a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleExecutor.java +++ b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleExecutor.java @@ -28,6 +28,7 @@ import org.apache.shardingsphere.readwritesplitting.distsql.handler.checker.ReadwriteSplittingRuleStatementChecker; import org.apache.shardingsphere.readwritesplitting.distsql.handler.converter.ReadwriteSplittingRuleStatementConverter; import org.apache.shardingsphere.readwritesplitting.distsql.statement.AlterReadwriteSplittingRuleStatement; +import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule; import java.util.Collection; import java.util.HashMap; @@ -42,13 +43,15 @@ */ @DistSQLExecutorCurrentRuleRequired("Readwrite-splitting") @Setter -public final class AlterReadwriteSplittingRuleExecutor implements DatabaseRuleAlterExecutor { +public final class AlterReadwriteSplittingRuleExecutor implements DatabaseRuleAlterExecutor { private ShardingSphereDatabase database; + private ReadwriteSplittingRule rule; + @Override - public void checkBeforeUpdate(final AlterReadwriteSplittingRuleStatement sqlStatement, final ReadwriteSplittingRuleConfiguration currentRuleConfig) { - ReadwriteSplittingRuleStatementChecker.checkAlteration(database, sqlStatement.getRules(), currentRuleConfig); + public void checkBeforeUpdate(final AlterReadwriteSplittingRuleStatement sqlStatement) { + ReadwriteSplittingRuleStatementChecker.checkAlteration(database, sqlStatement.getRules(), rule.getConfiguration()); } @Override @@ -93,8 +96,8 @@ private void addRuleConfiguration(final ReadwriteSplittingRuleConfiguration curr } @Override - public Class getRuleConfigurationClass() { - return ReadwriteSplittingRuleConfiguration.class; + public Class getRuleClass() { + return ReadwriteSplittingRule.class; } @Override diff --git a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleExecutor.java b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleExecutor.java index 04b186e193971..b03821296bf0e 100644 --- a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleExecutor.java +++ b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleExecutor.java @@ -26,6 +26,7 @@ import org.apache.shardingsphere.readwritesplitting.distsql.handler.converter.ReadwriteSplittingRuleStatementConverter; import org.apache.shardingsphere.readwritesplitting.distsql.segment.ReadwriteSplittingRuleSegment; import org.apache.shardingsphere.readwritesplitting.distsql.statement.CreateReadwriteSplittingRuleStatement; +import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule; import java.util.Collection; import java.util.LinkedList; @@ -35,13 +36,15 @@ * Create readwrite-splitting rule executor. */ @Setter -public final class CreateReadwriteSplittingRuleExecutor implements DatabaseRuleCreateExecutor { +public final class CreateReadwriteSplittingRuleExecutor implements DatabaseRuleCreateExecutor { private ShardingSphereDatabase database; + private ReadwriteSplittingRule rule; + @Override - public void checkBeforeUpdate(final CreateReadwriteSplittingRuleStatement sqlStatement, final ReadwriteSplittingRuleConfiguration currentRuleConfig) { - ReadwriteSplittingRuleStatementChecker.checkCreation(database, sqlStatement.getRules(), currentRuleConfig, sqlStatement.isIfNotExists()); + public void checkBeforeUpdate(final CreateReadwriteSplittingRuleStatement sqlStatement) { + ReadwriteSplittingRuleStatementChecker.checkCreation(database, sqlStatement.getRules(), null == rule ? null : rule.getConfiguration(), sqlStatement.isIfNotExists()); } @Override @@ -70,8 +73,8 @@ private Collection getDuplicatedRuleNames(final ReadwriteSplittingRuleCo } @Override - public Class getRuleConfigurationClass() { - return ReadwriteSplittingRuleConfiguration.class; + public Class getRuleClass() { + return ReadwriteSplittingRule.class; } @Override diff --git a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleExecutor.java b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleExecutor.java index 9266471e314a5..4087c2a6416e4 100644 --- a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleExecutor.java +++ b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleExecutor.java @@ -48,20 +48,22 @@ */ @DistSQLExecutorCurrentRuleRequired("Readwrite-splitting") @Setter -public final class DropReadwriteSplittingRuleExecutor implements DatabaseRuleDropExecutor { +public final class DropReadwriteSplittingRuleExecutor implements DatabaseRuleDropExecutor { private ShardingSphereDatabase database; + private ReadwriteSplittingRule rule; + @Override - public void checkBeforeUpdate(final DropReadwriteSplittingRuleStatement sqlStatement, final ReadwriteSplittingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropReadwriteSplittingRuleStatement sqlStatement) { if (!sqlStatement.isIfExists()) { - checkToBeDroppedRuleNames(sqlStatement, currentRuleConfig); + checkToBeDroppedRuleNames(sqlStatement); } checkToBeDroppedInUsed(sqlStatement); } - private void checkToBeDroppedRuleNames(final DropReadwriteSplittingRuleStatement sqlStatement, final ReadwriteSplittingRuleConfiguration currentRuleConfig) { - Collection currentRuleNames = currentRuleConfig.getDataSources().stream().map(ReadwriteSplittingDataSourceRuleConfiguration::getName).collect(Collectors.toList()); + private void checkToBeDroppedRuleNames(final DropReadwriteSplittingRuleStatement sqlStatement) { + Collection currentRuleNames = rule.getConfiguration().getDataSources().stream().map(ReadwriteSplittingDataSourceRuleConfiguration::getName).collect(Collectors.toList()); Collection notExistedRuleNames = sqlStatement.getNames().stream().filter(each -> !currentRuleNames.contains(each)).collect(Collectors.toList()); ShardingSpherePreconditions.checkState(notExistedRuleNames.isEmpty(), () -> new MissingRequiredRuleException("Readwrite-splitting", database.getName(), sqlStatement.getNames())); } @@ -140,8 +142,8 @@ public void operate(final DropReadwriteSplittingRuleStatement sqlStatement, fina } @Override - public Class getRuleConfigurationClass() { - return ReadwriteSplittingRuleConfiguration.class; + public Class getRuleClass() { + return ReadwriteSplittingRule.class; } @Override diff --git a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleExecutorTest.java b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleExecutorTest.java index 86d5f969ce70b..2fd2b66422335 100644 --- a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleExecutorTest.java +++ b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleExecutorTest.java @@ -28,6 +28,7 @@ import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration; import org.apache.shardingsphere.readwritesplitting.distsql.segment.ReadwriteSplittingRuleSegment; import org.apache.shardingsphere.readwritesplitting.distsql.statement.AlterReadwriteSplittingRuleStatement; +import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -66,55 +67,74 @@ void setUp() { @Test void assertCheckSQLStatementWithoutToBeAlteredRules() { - assertThrows(MissingRequiredRuleException.class, - () -> executor.checkBeforeUpdate(createSQLStatement("TEST"), new ReadwriteSplittingRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); + ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class); + when(rule.getConfiguration()).thenReturn(new ReadwriteSplittingRuleConfiguration(Collections.emptyList(), Collections.emptyMap())); + executor.setRule(rule); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("TEST"))); } @Test void assertCheckSQLStatementWithoutExistedResources() { when(resourceMetaData.getNotExistedDataSources(any())).thenReturn(Collections.singleton("read_ds_0")); - assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(createSQLStatement("TEST"), createCurrentRuleConfiguration())); + ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration()); + executor.setRule(rule); + assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(createSQLStatement("TEST"))); } @Test void assertCheckSQLStatementWithoutToBeAlteredLoadBalancers() { when(database.getRuleMetaData().findRules(any())).thenReturn(Collections.emptyList()); executor.setDatabase(database); - assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(createSQLStatement("INVALID_TYPE"), createCurrentRuleConfiguration())); + ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration()); + executor.setRule(rule); + assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(createSQLStatement("INVALID_TYPE"))); } @Test void assertCheckSQLStatementWithDuplicateWriteResourceNamesInStatement() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getResourceMetaData()).thenReturn(resourceMetaData); + ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfigurationWithMultipleRules()); + executor.setRule(rule); assertThrows(InvalidRuleConfigurationException.class, - () -> executor.checkBeforeUpdate(createSQLStatementWithDuplicateWriteResourceNames("readwrite_ds_0", "readwrite_ds_1", "TEST"), createCurrentRuleConfigurationWithMultipleRules())); + () -> executor.checkBeforeUpdate(createSQLStatementWithDuplicateWriteResourceNames("readwrite_ds_0", "readwrite_ds_1", "TEST"))); } @Test void assertCheckSQLStatementWithDuplicateWriteResourceNames() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getResourceMetaData()).thenReturn(resourceMetaData); + ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfigurationWithMultipleRules()); + executor.setRule(rule); assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate( - createSQLStatement("readwrite_ds_0", "ds_write_1", Arrays.asList("read_ds_0", "read_ds_1"), "TEST"), createCurrentRuleConfigurationWithMultipleRules())); + createSQLStatement("readwrite_ds_0", "ds_write_1", Arrays.asList("read_ds_0", "read_ds_1"), "TEST"))); } @Test void assertCheckSQLStatementWithDuplicateReadResourceNamesInStatement() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getResourceMetaData()).thenReturn(resourceMetaData); + ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfigurationWithMultipleRules()); + executor.setRule(rule); assertThrows(InvalidRuleConfigurationException.class, - () -> executor.checkBeforeUpdate(createSQLStatementWithDuplicateReadResourceNames("readwrite_ds_0", "readwrite_ds_1", "TEST"), createCurrentRuleConfigurationWithMultipleRules())); + () -> executor.checkBeforeUpdate(createSQLStatementWithDuplicateReadResourceNames("readwrite_ds_0", "readwrite_ds_1", "TEST"))); } @Test void assertCheckSQLStatementWithDuplicateReadResourceNames() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getResourceMetaData()).thenReturn(resourceMetaData); + ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfigurationWithMultipleRules()); + executor.setRule(rule); assertThrows(InvalidRuleConfigurationException.class, - () -> executor.checkBeforeUpdate( - createSQLStatement("readwrite_ds_1", "write_ds_1", Arrays.asList("read_ds_0_0", "read_ds_0_1"), "TEST"), createCurrentRuleConfigurationWithMultipleRules())); + () -> executor.checkBeforeUpdate(createSQLStatement("readwrite_ds_1", "write_ds_1", Arrays.asList("read_ds_0_0", "read_ds_0_1"), "TEST"))); } private AlterReadwriteSplittingRuleStatement createSQLStatement(final String loadBalancerTypeName) { diff --git a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleExecutorTest.java b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleExecutorTest.java index 771b562e8fe64..aef70223e244a 100644 --- a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleExecutorTest.java +++ b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleExecutorTest.java @@ -29,6 +29,7 @@ import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration; import org.apache.shardingsphere.readwritesplitting.distsql.segment.ReadwriteSplittingRuleSegment; import org.apache.shardingsphere.readwritesplitting.distsql.statement.CreateReadwriteSplittingRuleStatement; +import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; import org.junit.jupiter.api.BeforeEach; @@ -74,19 +75,25 @@ void setUp() { @Test void assertCheckSQLStatementWithDuplicateRuleNames() { when(resourceMetaData.getStorageUnits()).thenReturn(Collections.emptyMap()); - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("TEST"), createCurrentRuleConfiguration())); + ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration()); + executor.setRule(rule); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("TEST"))); } @Test void assertCheckSQLStatementWithDuplicateResource() { when(resourceMetaData.getStorageUnits()).thenReturn(Collections.singletonMap("write_ds", null)); - assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(createSQLStatement("write_ds", "TEST"), createCurrentRuleConfiguration())); + ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration()); + executor.setRule(rule); + assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(createSQLStatement("write_ds", "TEST"))); } @Test void assertCheckSQLStatementWithoutExistedResources() { when(resourceMetaData.getNotExistedDataSources(any())).thenReturn(Arrays.asList("read_ds_0", "read_ds_1")); - assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(createSQLStatement("TEST"), null)); + assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(createSQLStatement("TEST"))); } @Test @@ -97,37 +104,46 @@ void assertCheckSQLStatementWithDuplicateLogicResource() { ReadwriteSplittingRuleSegment ruleSegment = new ReadwriteSplittingRuleSegment("duplicate_ds", "write_ds_0", Arrays.asList("read_ds_0", "read_ds_1"), new AlgorithmSegment(null, new Properties())); executor.setDatabase(database); - assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, ruleSegment), null)); + assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, ruleSegment))); } @Test void assertCheckSQLStatementWithDuplicateWriteResourceNamesInStatement() { assertThrows(InvalidRuleConfigurationException.class, - () -> executor.checkBeforeUpdate(createSQLStatementWithDuplicateWriteResourceNames("write_ds_0", "write_ds_1", "TEST"), null)); + () -> executor.checkBeforeUpdate(createSQLStatementWithDuplicateWriteResourceNames("write_ds_0", "write_ds_1", "TEST"))); } @Test void assertCheckSQLStatementWithDuplicateWriteResourceNames() { + ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration()); + executor.setRule(rule); assertThrows(InvalidRuleConfigurationException.class, - () -> executor.checkBeforeUpdate(createSQLStatement("readwrite_ds_1", "ds_write", Arrays.asList("read_ds_0", "read_ds_1"), "TEST"), createCurrentRuleConfiguration())); + () -> executor.checkBeforeUpdate(createSQLStatement("readwrite_ds_1", "ds_write", Arrays.asList("read_ds_0", "read_ds_1"), "TEST"))); } @Test void assertCheckSQLStatementWithDuplicateReadResourceNamesInStatement() { - assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(createSQLStatementWithDuplicateReadResourceNames("write_ds_0", "write_ds_1", "TEST"), null)); + assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(createSQLStatementWithDuplicateReadResourceNames("write_ds_0", "write_ds_1", "TEST"))); } @Test void assertCheckSQLStatementWithDuplicateReadResourceNames() { + ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration()); + executor.setRule(rule); assertThrows(InvalidRuleConfigurationException.class, - () -> executor.checkBeforeUpdate(createSQLStatement("readwrite_ds_1", "write_ds_1", Arrays.asList("read_ds_0", "read_ds_1"), "TEST"), createCurrentRuleConfiguration())); + () -> executor.checkBeforeUpdate(createSQLStatement("readwrite_ds_1", "write_ds_1", Arrays.asList("read_ds_0", "read_ds_1"), "TEST"))); } @Test void assertCheckSQLStatementWithIfNotExists() { ReadwriteSplittingRuleSegment staticSegment = new ReadwriteSplittingRuleSegment("readwrite_ds_0", "write_ds_0", Arrays.asList("read_ds_2", "read_ds_3"), new AlgorithmSegment(null, new Properties())); - executor.checkBeforeUpdate(createSQLStatement(true, staticSegment), createCurrentRuleConfiguration()); + ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration()); + executor.setRule(rule); + executor.checkBeforeUpdate(createSQLStatement(true, staticSegment)); } @Test @@ -139,7 +155,7 @@ void assertUpdateSuccess() { new AlgorithmSegment("TEST", new Properties())); CreateReadwriteSplittingRuleStatement sqlStatement = createSQLStatement(false, staticSegment); executor.setDatabase(database); - executor.checkBeforeUpdate(sqlStatement, null); + executor.checkBeforeUpdate(sqlStatement); ReadwriteSplittingRuleConfiguration currentRuleConfig = new ReadwriteSplittingRuleConfiguration(new ArrayList<>(), new HashMap<>()); ReadwriteSplittingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); diff --git a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleExecutorTest.java b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleExecutorTest.java index 4cbebd7d3e936..7d432a8a9005b 100644 --- a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleExecutorTest.java +++ b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleExecutorTest.java @@ -28,6 +28,7 @@ import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration; import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration; import org.apache.shardingsphere.readwritesplitting.distsql.statement.DropReadwriteSplittingRuleStatement; +import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -65,15 +66,15 @@ void setUp() { @Test void assertCheckSQLStatementWithoutToBeDroppedRule() throws RuleDefinitionViolationException { - assertThrows(MissingRequiredRuleException.class, - () -> executor.checkBeforeUpdate(createSQLStatement(), new ReadwriteSplittingRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); + ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class); + when(rule.getConfiguration()).thenReturn(new ReadwriteSplittingRuleConfiguration(Collections.emptyList(), Collections.emptyMap())); + executor.setRule(rule); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement())); } @Test void assertCheckSQLStatementWithIfExists() throws RuleDefinitionViolationException { - executor.checkBeforeUpdate(new DropReadwriteSplittingRuleStatement(true, Collections.singleton("readwrite_ds")), - new ReadwriteSplittingRuleConfiguration(Collections.emptyList(), Collections.emptyMap())); - executor.checkBeforeUpdate(new DropReadwriteSplittingRuleStatement(true, Collections.singleton("readwrite_ds")), null); + executor.checkBeforeUpdate(new DropReadwriteSplittingRuleStatement(true, Collections.singleton("readwrite_ds"))); } @Test @@ -85,7 +86,10 @@ void assertCheckSQLStatementWithInUsed() throws RuleDefinitionViolationException when(dataNodeContainedRule.getAllDataNodes()).thenReturn(Collections.singletonMap("foo_ds", Collections.singleton(new DataNode("readwrite_ds.tbl")))); when(database.getRuleMetaData().findRules(DataNodeContainedRule.class)).thenReturn(Collections.singleton(dataNodeContainedRule)); executor.setDatabase(database); - assertThrows(RuleInUsedException.class, () -> executor.checkBeforeUpdate(createSQLStatement(), createCurrentRuleConfiguration())); + ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration()); + executor.setRule(rule); + assertThrows(RuleInUsedException.class, () -> executor.checkBeforeUpdate(createSQLStatement())); } @Test diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterDefaultShadowAlgorithmExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterDefaultShadowAlgorithmExecutor.java index ed274a7f5ec8b..b3ff26b4f9e2a 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterDefaultShadowAlgorithmExecutor.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterDefaultShadowAlgorithmExecutor.java @@ -31,6 +31,7 @@ import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.distsql.handler.checker.ShadowRuleStatementChecker; import org.apache.shardingsphere.shadow.distsql.statement.AlterDefaultShadowAlgorithmStatement; +import org.apache.shardingsphere.shadow.rule.ShadowRule; import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm; import java.util.Collections; @@ -41,22 +42,24 @@ */ @DistSQLExecutorCurrentRuleRequired("Shadow") @Setter -public final class AlterDefaultShadowAlgorithmExecutor implements DatabaseRuleAlterExecutor { +public final class AlterDefaultShadowAlgorithmExecutor implements DatabaseRuleAlterExecutor { private static final String DEFAULT_ALGORITHM_NAME = "default_shadow_algorithm"; private ShardingSphereDatabase database; + private ShadowRule rule; + @Override - public void checkBeforeUpdate(final AlterDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { - checkAlgorithms(sqlStatement.getShadowAlgorithmSegment().getAlgorithmSegment(), currentRuleConfig); + public void checkBeforeUpdate(final AlterDefaultShadowAlgorithmStatement sqlStatement) { + checkAlgorithms(sqlStatement.getShadowAlgorithmSegment().getAlgorithmSegment()); } - private void checkAlgorithms(final AlgorithmSegment algorithmSegment, final ShadowRuleConfiguration currentRuleConfig) { + private void checkAlgorithms(final AlgorithmSegment algorithmSegment) { checkAlgorithmCompleteness(algorithmSegment); checkAlgorithmType(algorithmSegment); ShadowRuleStatementChecker.checkExisted(Collections.singleton(DEFAULT_ALGORITHM_NAME), - currentRuleConfig.getShadowAlgorithms().keySet(), notExistedAlgorithms -> new MissingRequiredAlgorithmException("shadow", database.getName(), notExistedAlgorithms)); + rule.getConfiguration().getShadowAlgorithms().keySet(), notExistedAlgorithms -> new MissingRequiredAlgorithmException("shadow", database.getName(), notExistedAlgorithms)); } private void checkAlgorithmCompleteness(final AlgorithmSegment algorithmSegment) { @@ -87,8 +90,8 @@ public void updateCurrentRuleConfiguration(final ShadowRuleConfiguration current } @Override - public Class getRuleConfigurationClass() { - return ShadowRuleConfiguration.class; + public Class getRuleClass() { + return ShadowRule.class; } @Override diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterShadowRuleExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterShadowRuleExecutor.java index c38b7a2912093..04ff0dd30d4c1 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterShadowRuleExecutor.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterShadowRuleExecutor.java @@ -34,6 +34,7 @@ import org.apache.shardingsphere.shadow.distsql.segment.ShadowAlgorithmSegment; import org.apache.shardingsphere.shadow.distsql.segment.ShadowRuleSegment; import org.apache.shardingsphere.shadow.distsql.statement.AlterShadowRuleStatement; +import org.apache.shardingsphere.shadow.rule.ShadowRule; import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm; import java.util.Collection; @@ -44,20 +45,22 @@ */ @DistSQLExecutorCurrentRuleRequired("Shadow") @Setter -public final class AlterShadowRuleExecutor implements DatabaseRuleAlterExecutor { +public final class AlterShadowRuleExecutor implements DatabaseRuleAlterExecutor { private ShardingSphereDatabase database; + private ShadowRule rule; + @Override - public void checkBeforeUpdate(final AlterShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { - checkRuleNames(sqlStatement.getRules(), currentRuleConfig); + public void checkBeforeUpdate(final AlterShadowRuleStatement sqlStatement) { + checkRuleNames(sqlStatement.getRules()); ShadowRuleStatementChecker.checkStorageUnitsExist(ShadowRuleStatementSupporter.getStorageUnitNames(sqlStatement.getRules()), database); checkAlgorithms(sqlStatement.getRules()); checkAlgorithmType(sqlStatement); } - private void checkRuleNames(final Collection segments, final ShadowRuleConfiguration currentRuleConfig) { - Collection currentRuleNames = ShadowRuleStatementSupporter.getRuleNames(currentRuleConfig); + private void checkRuleNames(final Collection segments) { + Collection currentRuleNames = ShadowRuleStatementSupporter.getRuleNames(rule.getConfiguration()); Collection requiredRuleNames = ShadowRuleStatementSupporter.getRuleNames(segments); ShadowRuleStatementChecker.checkDuplicated(requiredRuleNames, duplicated -> new DuplicateRuleException("shadow", database.getName(), duplicated)); ShadowRuleStatementChecker.checkExisted(requiredRuleNames, currentRuleNames, notExistedRules -> new MissingRequiredRuleException("Shadow", notExistedRules)); @@ -94,8 +97,8 @@ private void updateTables(final Map currentTab } @Override - public Class getRuleConfigurationClass() { - return ShadowRuleConfiguration.class; + public Class getRuleClass() { + return ShadowRule.class; } @Override diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateDefaultShadowAlgorithmExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateDefaultShadowAlgorithmExecutor.java index 1c6c95bcf95d9..c4be594ccff92 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateDefaultShadowAlgorithmExecutor.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateDefaultShadowAlgorithmExecutor.java @@ -24,11 +24,12 @@ import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleCreateExecutor; import org.apache.shardingsphere.distsql.segment.AlgorithmSegment; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.distsql.statement.CreateDefaultShadowAlgorithmStatement; +import org.apache.shardingsphere.shadow.rule.ShadowRule; import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm; import java.util.Collection; @@ -41,33 +42,44 @@ * Create default shadow algorithm statement executor. */ @Setter -public final class CreateDefaultShadowAlgorithmExecutor implements DatabaseRuleCreateExecutor { +public final class CreateDefaultShadowAlgorithmExecutor implements DatabaseRuleCreateExecutor { private ShardingSphereDatabase database; + private ShadowRule rule; + @Override - public void checkBeforeUpdate(final CreateDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final CreateDefaultShadowAlgorithmStatement sqlStatement) { if (!sqlStatement.isIfNotExists()) { - checkExisted(currentRuleConfig); + checkAlgorithmExisted(); } - checkAlgorithmCompleteness(Collections.singleton(sqlStatement.getShadowAlgorithmSegment().getAlgorithmSegment())); + checkAlgorithmCompleteness(sqlStatement); checkAlgorithmType(sqlStatement); } - private void checkExisted(final ShadowRuleConfiguration currentRuleConfig) { - Collection duplicatedRuleNames = getDuplicatedRuleNames(currentRuleConfig); - ShardingSpherePreconditions.checkState(duplicatedRuleNames.isEmpty(), () -> new DuplicateAlgorithmException("Shadow", database.getName(), duplicatedRuleNames)); + private void checkAlgorithmExisted() { + Collection duplicatedAlgorithmNames = getDuplicatedAlgorithmNames(); + ShardingSpherePreconditions.checkState(duplicatedAlgorithmNames.isEmpty(), () -> new DuplicateAlgorithmException("Shadow", database.getName(), duplicatedAlgorithmNames)); + } + + private Collection getDuplicatedAlgorithmNames() { + Collection algorithmNames = null == rule ? Collections.emptyList() : rule.getShadowAlgorithms().keySet(); + return Stream.of("default_shadow_algorithm").filter(algorithmNames::contains).collect(Collectors.toSet()); + } + + private void checkAlgorithmCompleteness(final CreateDefaultShadowAlgorithmStatement sqlStatement) { + ShardingSpherePreconditions.checkState(!sqlStatement.getShadowAlgorithmSegment().getAlgorithmSegment().getName().isEmpty(), () -> new InvalidAlgorithmConfigurationException("Shadow")); } - private Collection getDuplicatedRuleNames(final ShadowRuleConfiguration currentRuleConfig) { - Collection currentAlgorithmNames = null == currentRuleConfig ? Collections.emptyList() : currentRuleConfig.getShadowAlgorithms().keySet(); - return Stream.of("default_shadow_algorithm").filter(currentAlgorithmNames::contains).collect(Collectors.toSet()); + private void checkAlgorithmType(final CreateDefaultShadowAlgorithmStatement sqlStatement) { + AlgorithmSegment shadowAlgorithmType = sqlStatement.getShadowAlgorithmSegment().getAlgorithmSegment(); + TypedSPILoader.checkService(ShadowAlgorithm.class, shadowAlgorithmType.getName(), shadowAlgorithmType.getProps()); } @Override public ShadowRuleConfiguration buildToBeCreatedRuleConfiguration(final CreateDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { ShadowRuleConfiguration result = new ShadowRuleConfiguration(); - if (getDuplicatedRuleNames(currentRuleConfig).isEmpty()) { + if (getDuplicatedAlgorithmNames().isEmpty()) { result = new ShadowRuleConfiguration(); result.setShadowAlgorithms(buildAlgorithmMap(sqlStatement)); result.setDefaultShadowAlgorithmName("default_shadow_algorithm"); @@ -75,6 +87,11 @@ public ShadowRuleConfiguration buildToBeCreatedRuleConfiguration(final CreateDef return result; } + private Map buildAlgorithmMap(final CreateDefaultShadowAlgorithmStatement sqlStatement) { + return Collections.singletonMap("default_shadow_algorithm", + new AlgorithmConfiguration(sqlStatement.getShadowAlgorithmSegment().getAlgorithmSegment().getName(), sqlStatement.getShadowAlgorithmSegment().getAlgorithmSegment().getProps())); + } + @Override public void updateCurrentRuleConfiguration(final ShadowRuleConfiguration currentRuleConfig, final ShadowRuleConfiguration toBeCreatedRuleConfig) { currentRuleConfig.getShadowAlgorithms().putAll(toBeCreatedRuleConfig.getShadowAlgorithms()); @@ -83,24 +100,9 @@ public void updateCurrentRuleConfiguration(final ShadowRuleConfiguration current } } - private Map buildAlgorithmMap(final CreateDefaultShadowAlgorithmStatement sqlStatement) { - return Collections.singletonMap("default_shadow_algorithm", new AlgorithmConfiguration(sqlStatement.getShadowAlgorithmSegment().getAlgorithmSegment().getName(), - sqlStatement.getShadowAlgorithmSegment().getAlgorithmSegment().getProps())); - } - - private void checkAlgorithmType(final CreateDefaultShadowAlgorithmStatement sqlStatement) { - AlgorithmSegment shadowAlgorithmType = sqlStatement.getShadowAlgorithmSegment().getAlgorithmSegment(); - TypedSPILoader.checkService(ShadowAlgorithm.class, shadowAlgorithmType.getName(), shadowAlgorithmType.getProps()); - } - - private void checkAlgorithmCompleteness(final Collection algorithmSegments) { - Collection incompleteAlgorithms = algorithmSegments.stream().filter(each -> each.getName().isEmpty()).collect(Collectors.toSet()); - ShardingSpherePreconditions.checkState(incompleteAlgorithms.isEmpty(), () -> new InvalidAlgorithmConfigurationException("shadow")); - } - @Override - public Class getRuleConfigurationClass() { - return ShadowRuleConfiguration.class; + public Class getRuleClass() { + return ShadowRule.class; } @Override diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleExecutor.java index 6c7eb779827b8..d1bb3ae8ba0bc 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleExecutor.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleExecutor.java @@ -20,8 +20,8 @@ import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleCreateExecutor; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration; @@ -31,6 +31,7 @@ import org.apache.shardingsphere.shadow.distsql.segment.ShadowAlgorithmSegment; import org.apache.shardingsphere.shadow.distsql.segment.ShadowRuleSegment; import org.apache.shardingsphere.shadow.distsql.statement.CreateShadowRuleStatement; +import org.apache.shardingsphere.shadow.rule.ShadowRule; import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm; import java.util.Collection; @@ -40,24 +41,26 @@ * Create shadow rule executor. */ @Setter -public final class CreateShadowRuleExecutor implements DatabaseRuleCreateExecutor { +public final class CreateShadowRuleExecutor implements DatabaseRuleCreateExecutor { private ShardingSphereDatabase database; + private ShadowRule rule; + @Override - public void checkBeforeUpdate(final CreateShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { - checkDuplicatedRules(sqlStatement, currentRuleConfig); + public void checkBeforeUpdate(final CreateShadowRuleStatement sqlStatement) { + checkDuplicatedRules(sqlStatement); checkStorageUnits(sqlStatement.getRules()); checkAlgorithms(sqlStatement.getRules()); checkAlgorithmType(sqlStatement.getRules()); } - private void checkDuplicatedRules(final CreateShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { + private void checkDuplicatedRules(final CreateShadowRuleStatement sqlStatement) { Collection toBeCreatedRuleNames = ShadowRuleStatementSupporter.getRuleNames(sqlStatement.getRules()); ShadowRuleStatementChecker.checkDuplicated(toBeCreatedRuleNames, duplicated -> new DuplicateRuleException("shadow", database.getName(), duplicated)); ShadowRuleStatementChecker.checkDuplicatedWithLogicDataSource(toBeCreatedRuleNames, database); if (!sqlStatement.isIfNotExists()) { - toBeCreatedRuleNames.retainAll(ShadowRuleStatementSupporter.getRuleNames(currentRuleConfig)); + toBeCreatedRuleNames.retainAll(ShadowRuleStatementSupporter.getRuleNames(null == rule ? null : rule.getConfiguration())); ShardingSpherePreconditions.checkState(toBeCreatedRuleNames.isEmpty(), () -> new DuplicateRuleException("shadow", database.getName(), toBeCreatedRuleNames)); } } @@ -98,8 +101,8 @@ private void updateTables(final Map currentTab } @Override - public Class getRuleConfigurationClass() { - return ShadowRuleConfiguration.class; + public Class getRuleClass() { + return ShadowRule.class; } @Override diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropDefaultShadowAlgorithmExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropDefaultShadowAlgorithmExecutor.java index 1b96dede62ba6..67c2c5eafcc4a 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropDefaultShadowAlgorithmExecutor.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropDefaultShadowAlgorithmExecutor.java @@ -25,6 +25,7 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.distsql.statement.DropDefaultShadowAlgorithmStatement; +import org.apache.shardingsphere.shadow.rule.ShadowRule; import java.util.Collections; @@ -33,20 +34,22 @@ */ @DistSQLExecutorCurrentRuleRequired("Shadow") @Setter -public final class DropDefaultShadowAlgorithmExecutor implements DatabaseRuleDropExecutor { +public final class DropDefaultShadowAlgorithmExecutor implements DatabaseRuleDropExecutor { private ShardingSphereDatabase database; + private ShadowRule rule; + @Override - public void checkBeforeUpdate(final DropDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropDefaultShadowAlgorithmStatement sqlStatement) { if (!sqlStatement.isIfExists()) { - checkAlgorithm(currentRuleConfig); + checkAlgorithm(); } } - private void checkAlgorithm(final ShadowRuleConfiguration currentRuleConfig) { + private void checkAlgorithm() { ShardingSpherePreconditions.checkNotNull( - currentRuleConfig.getDefaultShadowAlgorithmName(), () -> new MissingRequiredAlgorithmException("shadow", database.getName(), Collections.singleton("default"))); + rule.getConfiguration().getDefaultShadowAlgorithmName(), () -> new MissingRequiredAlgorithmException("shadow", database.getName(), Collections.singleton("default"))); } @Override @@ -71,8 +74,8 @@ public boolean updateCurrentRuleConfiguration(final DropDefaultShadowAlgorithmSt } @Override - public Class getRuleConfigurationClass() { - return ShadowRuleConfiguration.class; + public Class getRuleClass() { + return ShadowRule.class; } @Override diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowAlgorithmExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowAlgorithmExecutor.java index b2918d9e362d4..a018326ad3c83 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowAlgorithmExecutor.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowAlgorithmExecutor.java @@ -18,18 +18,19 @@ package org.apache.shardingsphere.shadow.distsql.handler.update; import lombok.Setter; -import org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorCurrentRuleRequired; -import org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.DistSQLException; import org.apache.shardingsphere.distsql.handler.exception.algorithm.AlgorithmInUsedException; import org.apache.shardingsphere.distsql.handler.exception.algorithm.MissingRequiredAlgorithmException; +import org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorCurrentRuleRequired; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.DistSQLException; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration; import org.apache.shardingsphere.shadow.distsql.handler.checker.ShadowRuleStatementChecker; import org.apache.shardingsphere.shadow.distsql.handler.supporter.ShadowRuleStatementSupporter; import org.apache.shardingsphere.shadow.distsql.statement.DropShadowAlgorithmStatement; +import org.apache.shardingsphere.shadow.rule.ShadowRule; import java.util.Collection; import java.util.Collections; @@ -43,26 +44,28 @@ */ @DistSQLExecutorCurrentRuleRequired("Shadow") @Setter -public final class DropShadowAlgorithmExecutor implements DatabaseRuleDropExecutor { +public final class DropShadowAlgorithmExecutor implements DatabaseRuleDropExecutor { private ShardingSphereDatabase database; + private ShadowRule rule; + @Override - public void checkBeforeUpdate(final DropShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropShadowAlgorithmStatement sqlStatement) { if (!sqlStatement.isIfExists()) { - checkAlgorithm(sqlStatement, currentRuleConfig); + checkAlgorithm(sqlStatement); } } - private void checkAlgorithm(final DropShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { - Collection currentAlgorithms = ShadowRuleStatementSupporter.getAlgorithmNames(currentRuleConfig); + private void checkAlgorithm(final DropShadowAlgorithmStatement sqlStatement) { + Collection currentAlgorithms = ShadowRuleStatementSupporter.getAlgorithmNames(rule.getConfiguration()); Collection requiredAlgorithms = sqlStatement.getNames(); - String defaultShadowAlgorithmName = currentRuleConfig.getDefaultShadowAlgorithmName(); + String defaultShadowAlgorithmName = rule.getConfiguration().getDefaultShadowAlgorithmName(); if (!sqlStatement.isIfExists()) { ShadowRuleStatementChecker.checkExisted( requiredAlgorithms, currentAlgorithms, notExistedAlgorithms -> new MissingRequiredAlgorithmException("shadow", database.getName(), notExistedAlgorithms)); } - checkAlgorithmInUsed(requiredAlgorithms, getAlgorithmInUse(currentRuleConfig), identical -> new AlgorithmInUsedException("Shadow", database.getName(), identical)); + checkAlgorithmInUsed(requiredAlgorithms, getAlgorithmInUse(), identical -> new AlgorithmInUsedException("Shadow", database.getName(), identical)); ShardingSpherePreconditions.checkState(!requiredAlgorithms.contains(defaultShadowAlgorithmName), () -> new AlgorithmInUsedException("Shadow", database.getName(), Collections.singleton(defaultShadowAlgorithmName))); } @@ -71,8 +74,8 @@ private void checkAlgorithmInUsed(final Collection requiredAlgorithms, f ShadowRuleStatementChecker.checkDuplicated(requiredAlgorithms, currentAlgorithms, thrower); } - private Collection getAlgorithmInUse(final ShadowRuleConfiguration currentRuleConfig) { - return currentRuleConfig.getTables().values().stream().filter(each -> !each.getDataSourceNames().isEmpty()).map(ShadowTableConfiguration::getShadowAlgorithmNames) + private Collection getAlgorithmInUse() { + return rule.getConfiguration().getTables().values().stream().filter(each -> !each.getDataSourceNames().isEmpty()).map(ShadowTableConfiguration::getShadowAlgorithmNames) .flatMap(Collection::stream).collect(Collectors.toSet()); } @@ -106,8 +109,8 @@ private Collection getEmptyTableRules(final Map getRuleConfigurationClass() { - return ShadowRuleConfiguration.class; + public Class getRuleClass() { + return ShadowRule.class; } @Override diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleExecutor.java index d8e0a5d689743..bbf18d149ad39 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleExecutor.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleExecutor.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration; import org.apache.shardingsphere.shadow.distsql.handler.checker.ShadowRuleStatementChecker; import org.apache.shardingsphere.shadow.distsql.statement.DropShadowRuleStatement; +import org.apache.shardingsphere.shadow.rule.ShadowRule; import java.util.Collection; import java.util.LinkedHashMap; @@ -42,29 +43,31 @@ */ @DistSQLExecutorCurrentRuleRequired("Shadow") @Setter -public final class DropShadowRuleExecutor implements DatabaseRuleDropExecutor { +public final class DropShadowRuleExecutor implements DatabaseRuleDropExecutor { private ShardingSphereDatabase database; + private ShadowRule rule; + @Override - public void checkBeforeUpdate(final DropShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropShadowRuleStatement sqlStatement) { if (!sqlStatement.isIfExists()) { - checkRuleExisted(sqlStatement, currentRuleConfig); + checkRuleExisted(sqlStatement); } } - private void checkRuleExisted(final DropShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { - ShadowRuleStatementChecker.checkExisted(sqlStatement.getNames(), getDataSourceNames(currentRuleConfig), + private void checkRuleExisted(final DropShadowRuleStatement sqlStatement) { + ShadowRuleStatementChecker.checkExisted(sqlStatement.getNames(), getDataSourceNames(), notExistedRuleNames -> new MissingRequiredRuleException("Shadow", database.getName(), notExistedRuleNames)); } - private Collection getDataSourceNames(final ShadowRuleConfiguration currentRuleConfig) { - return currentRuleConfig.getDataSources().stream().map(ShadowDataSourceConfiguration::getName).collect(Collectors.toList()); + private Collection getDataSourceNames() { + return rule.getConfiguration().getDataSources().stream().map(ShadowDataSourceConfiguration::getName).collect(Collectors.toList()); } @Override public boolean hasAnyOneToBeDropped(final DropShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { - return isExistRuleConfig(currentRuleConfig) && !getIdenticalData(sqlStatement.getNames(), getDataSourceNames(currentRuleConfig)).isEmpty(); + return isExistRuleConfig(currentRuleConfig) && !getIdenticalData(sqlStatement.getNames(), getDataSourceNames()).isEmpty(); } @Override @@ -130,8 +133,8 @@ private Collection findUnusedAlgorithms(final ShadowRuleConfiguration cu } @Override - public Class getRuleConfigurationClass() { - return ShadowRuleConfiguration.class; + public Class getRuleClass() { + return ShadowRule.class; } @Override diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterDefaultShadowAlgorithmExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterDefaultShadowAlgorithmExecutorTest.java index 1e168e2a1b9fa..8fcb43dc2b06d 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterDefaultShadowAlgorithmExecutorTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterDefaultShadowAlgorithmExecutorTest.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.shadow.distsql.handler.update.AlterDefaultShadowAlgorithmExecutor; import org.apache.shardingsphere.shadow.distsql.segment.ShadowAlgorithmSegment; import org.apache.shardingsphere.shadow.distsql.statement.AlterDefaultShadowAlgorithmStatement; +import org.apache.shardingsphere.shadow.rule.ShadowRule; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; import org.junit.jupiter.api.BeforeEach; @@ -62,29 +63,34 @@ void assertExecuteAlgorithmNotInMetaData() { when(currentConfig.getShadowAlgorithms()).thenReturn(Collections.singletonMap("sqlHintAlgorithm", new AlgorithmConfiguration("type", props))); AlterDefaultShadowAlgorithmStatement sqlStatement = new AlterDefaultShadowAlgorithmStatement( new ShadowAlgorithmSegment("default_shadow_algorithm", new AlgorithmSegment("SQL_HINT", props))); - assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test void assertExecuteInvalidAlgorithmType() { AlterDefaultShadowAlgorithmStatement sqlStatement = new AlterDefaultShadowAlgorithmStatement( new ShadowAlgorithmSegment("default_shadow_algorithm", new AlgorithmSegment("NOT_EXIST_SQL_HINT", PropertiesBuilder.build(new Property("type", "value"))))); - assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); + assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test void assertExecuteIncompletenessAlgorithm() { AlterDefaultShadowAlgorithmStatement sqlStatement = new AlterDefaultShadowAlgorithmStatement( new ShadowAlgorithmSegment("default_shadow_algorithm", new AlgorithmSegment("", PropertiesBuilder.build(new Property("type", "value"))))); - assertThrows(InvalidAlgorithmConfigurationException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); + assertThrows(InvalidAlgorithmConfigurationException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test void assertExecuteSuccess() { Properties props = PropertiesBuilder.build(new Property("type", "value")); when(currentConfig.getShadowAlgorithms()).thenReturn(Collections.singletonMap("default_shadow_algorithm", new AlgorithmConfiguration("type", props))); - AlterDefaultShadowAlgorithmStatement sqlStatement = new AlterDefaultShadowAlgorithmStatement( - new ShadowAlgorithmSegment("default_shadow_algorithm", new AlgorithmSegment("SQL_HINT", props))); - executor.checkBeforeUpdate(sqlStatement, currentConfig); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + AlterDefaultShadowAlgorithmStatement sqlStatement = new AlterDefaultShadowAlgorithmStatement(new ShadowAlgorithmSegment("default_shadow_algorithm", new AlgorithmSegment("SQL_HINT", props))); + executor.checkBeforeUpdate(sqlStatement); } } diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterShadowRuleExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterShadowRuleExecutorTest.java index 656fb7d439ead..36d5516b55923 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterShadowRuleExecutorTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterShadowRuleExecutorTest.java @@ -30,6 +30,7 @@ import org.apache.shardingsphere.shadow.distsql.segment.ShadowAlgorithmSegment; import org.apache.shardingsphere.shadow.distsql.segment.ShadowRuleSegment; import org.apache.shardingsphere.shadow.distsql.statement.AlterShadowRuleStatement; +import org.apache.shardingsphere.shadow.rule.ShadowRule; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; import org.junit.jupiter.api.BeforeEach; @@ -79,13 +80,19 @@ void setUp() { @Test void assertExecuteWithDuplicateRuleName() { ShadowRuleSegment ruleSegment = new ShadowRuleSegment("ruleName", null, null, null); - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(new AlterShadowRuleStatement(Arrays.asList(ruleSegment, ruleSegment)), currentConfig)); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(new AlterShadowRuleStatement(Arrays.asList(ruleSegment, ruleSegment)))); } @Test void assertExecuteWithRuleNameNotInMetaData() { ShadowRuleSegment ruleSegment = new ShadowRuleSegment("ruleName", null, null, null); - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(new AlterShadowRuleStatement(Collections.singleton(ruleSegment)), currentConfig)); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(new AlterShadowRuleStatement(Collections.singleton(ruleSegment)))); } @Test @@ -93,7 +100,10 @@ void assertExecuteWithNotExistResource() { List dataSources = Arrays.asList("ds", "ds0"); when(resourceMetaData.getNotExistedDataSources(any())).thenReturn(dataSources); AlterShadowRuleStatement sqlStatement = new AlterShadowRuleStatement(Collections.singleton(new ShadowRuleSegment("initRuleName1", "ds3", null, null))); - assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test @@ -102,7 +112,10 @@ void assertExecuteDuplicateAlgorithm() { AlterShadowRuleStatement sqlStatement = new AlterShadowRuleStatement(Arrays.asList( new ShadowRuleSegment("initRuleName1", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment))), new ShadowRuleSegment("initRuleName2", "ds1", null, Collections.singletonMap("t_order_1", Collections.singleton(segment))))); - assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test @@ -111,7 +124,10 @@ void assertExecuteDuplicateAlgorithmWithoutConfiguration() { AlterShadowRuleStatement sqlStatement = new AlterShadowRuleStatement(Arrays.asList( new ShadowRuleSegment("initRuleName1", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment))), new ShadowRuleSegment("initRuleName2", "ds1", null, Collections.singletonMap("t_order_1", Collections.singleton(segment))))); - assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test @@ -122,7 +138,10 @@ void assertExecuteSuccess() { AlterShadowRuleStatement sqlStatement = new AlterShadowRuleStatement(Arrays.asList( new ShadowRuleSegment("initRuleName1", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment1))), new ShadowRuleSegment("initRuleName2", "ds1", null, Collections.singletonMap("t_order_1", Collections.singleton(segment2))))); - executor.checkBeforeUpdate(sqlStatement, currentConfig); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); } @Test @@ -132,6 +151,9 @@ void assertExecuteSuccessWithoutProps() { AlterShadowRuleStatement sqlStatement = new AlterShadowRuleStatement(Arrays.asList( new ShadowRuleSegment("initRuleName1", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment1))), new ShadowRuleSegment("initRuleName2", "ds1", null, Collections.singletonMap("t_order_1", Collections.singleton(segment2))))); - executor.checkBeforeUpdate(sqlStatement, currentConfig); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); } } diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateDefaultShadowAlgorithmExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateDefaultShadowAlgorithmExecutorTest.java index 8471abc1d8049..982b9fc8c2fde 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateDefaultShadowAlgorithmExecutorTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateDefaultShadowAlgorithmExecutorTest.java @@ -24,6 +24,7 @@ import org.apache.shardingsphere.shadow.distsql.handler.update.CreateDefaultShadowAlgorithmExecutor; import org.apache.shardingsphere.shadow.distsql.segment.ShadowAlgorithmSegment; import org.apache.shardingsphere.shadow.distsql.statement.CreateDefaultShadowAlgorithmStatement; +import org.apache.shardingsphere.shadow.rule.ShadowRule; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; import org.junit.jupiter.api.BeforeEach; @@ -54,21 +55,27 @@ void setUp() { void assertExecuteWithInvalidAlgorithm() { CreateDefaultShadowAlgorithmStatement statement = mock(CreateDefaultShadowAlgorithmStatement.class); when(statement.getShadowAlgorithmSegment()).thenReturn(new ShadowAlgorithmSegment("algorithmName", new AlgorithmSegment("name", PropertiesBuilder.build(new Property("type", "value"))))); - assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(statement, currentConfig)); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(statement)); } @Test void assertExecuteSuccess() { - CreateDefaultShadowAlgorithmStatement statement = mock(CreateDefaultShadowAlgorithmStatement.class); - when(statement.getShadowAlgorithmSegment()).thenReturn( + CreateDefaultShadowAlgorithmStatement sqlStatement = mock(CreateDefaultShadowAlgorithmStatement.class); + when(sqlStatement.getShadowAlgorithmSegment()).thenReturn( new ShadowAlgorithmSegment("algorithmName", new AlgorithmSegment("SQL_HINT", PropertiesBuilder.build(new Property("type", "value"))))); - executor.checkBeforeUpdate(statement, currentConfig); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); } @Test void assertExecuteWithIfNotExists() { ShadowAlgorithmSegment shadowAlgorithmSegment = new ShadowAlgorithmSegment("algorithmName", new AlgorithmSegment("SQL_HINT", PropertiesBuilder.build(new Property("type", "value")))); - CreateDefaultShadowAlgorithmStatement statement = new CreateDefaultShadowAlgorithmStatement(true, shadowAlgorithmSegment); - executor.checkBeforeUpdate(statement, currentConfig); + CreateDefaultShadowAlgorithmStatement sqlStatement = new CreateDefaultShadowAlgorithmStatement(true, shadowAlgorithmSegment); + executor.checkBeforeUpdate(sqlStatement); } } diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateShadowRuleExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateShadowRuleExecutorTest.java index 733f11d7f3959..cba765be737ea 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateShadowRuleExecutorTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateShadowRuleExecutorTest.java @@ -31,6 +31,7 @@ import org.apache.shardingsphere.shadow.distsql.segment.ShadowAlgorithmSegment; import org.apache.shardingsphere.shadow.distsql.segment.ShadowRuleSegment; import org.apache.shardingsphere.shadow.distsql.statement.CreateShadowRuleStatement; +import org.apache.shardingsphere.shadow.rule.ShadowRule; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; import org.junit.jupiter.api.BeforeEach; @@ -77,14 +78,17 @@ void setUp() { @Test void assertExecuteWithDuplicateRuleName() { ShadowRuleSegment ruleSegment = new ShadowRuleSegment("ruleName", null, null, null); - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(new CreateShadowRuleStatement(false, Arrays.asList(ruleSegment, ruleSegment)), null)); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(new CreateShadowRuleStatement(false, Arrays.asList(ruleSegment, ruleSegment)))); } @Test void assertExecuteWithDuplicateRuleNameInMetaData() { when(currentConfig.getDataSources()).thenReturn(Collections.singleton(new ShadowDataSourceConfiguration("ruleName", "ds", "ds_shadow"))); ShadowRuleSegment ruleSegment = new ShadowRuleSegment("ruleName", null, null, null); - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(new CreateShadowRuleStatement(false, Collections.singleton(ruleSegment)), currentConfig)); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(new CreateShadowRuleStatement(false, Collections.singleton(ruleSegment)))); } @Test @@ -94,14 +98,17 @@ void assertExecuteWithDuplicateLogicResource() { when(database.getRuleMetaData().findRules(DataSourceContainedRule.class)).thenReturn(Collections.singleton(dataSourceContainedRule)); executor.setDatabase(database); ShadowRuleSegment ruleSegment = new ShadowRuleSegment("duplicate_ds", null, null, null); - assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(new CreateShadowRuleStatement(false, Collections.singleton(ruleSegment)), null)); + assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(new CreateShadowRuleStatement(false, Collections.singleton(ruleSegment)))); } @Test void assertExecuteWithNotExistResource() { when(resourceMetaData.getNotExistedDataSources(any())).thenReturn(Arrays.asList("ds0", "ds1")); CreateShadowRuleStatement sqlStatement = new CreateShadowRuleStatement(false, Collections.singleton(new ShadowRuleSegment("ruleName", "ds1", null, null))); - assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test @@ -110,7 +117,10 @@ void assertExecuteDuplicateAlgorithm() { CreateShadowRuleStatement sqlStatement = new CreateShadowRuleStatement(false, Arrays.asList( new ShadowRuleSegment("ruleName", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment))), new ShadowRuleSegment("ruleName", "ds1", null, Collections.singletonMap("t_order_1", Collections.singleton(segment))))); - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test @@ -119,7 +129,7 @@ void assertExecuteDuplicateAlgorithmWithoutConfiguration() { CreateShadowRuleStatement sqlStatement = new CreateShadowRuleStatement(false, Arrays.asList( new ShadowRuleSegment("ruleName", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment))), new ShadowRuleSegment("ruleName1", "ds1", null, Collections.singletonMap("t_order_1", Collections.singleton(segment))))); - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement, null)); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test @@ -127,7 +137,10 @@ void assertInvalidAlgorithmConfiguration() { ShadowAlgorithmSegment segment = new ShadowAlgorithmSegment("algorithmName", new AlgorithmSegment("type", PropertiesBuilder.build(new Property("type", "value")))); CreateShadowRuleStatement sqlStatement = new CreateShadowRuleStatement(false, Collections.singleton(new ShadowRuleSegment("ruleName", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment))))); - assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test @@ -135,7 +148,10 @@ void assertExecuteWithoutProps() { ShadowAlgorithmSegment segment = new ShadowAlgorithmSegment("algorithmName", new AlgorithmSegment("SQL_HINT", null)); CreateShadowRuleStatement sqlStatement = new CreateShadowRuleStatement(false, Collections.singleton(new ShadowRuleSegment("initRuleNameWithoutProps", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment))))); - executor.checkBeforeUpdate(sqlStatement, currentConfig); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); } @Test @@ -143,6 +159,9 @@ void assertExecuteWithIfNotExists() { ShadowAlgorithmSegment segment = new ShadowAlgorithmSegment("algorithmName", new AlgorithmSegment("SQL_HINT", PropertiesBuilder.build(new Property("type", "value")))); CreateShadowRuleStatement sqlStatement = new CreateShadowRuleStatement(true, Collections.singleton(new ShadowRuleSegment("initRuleName", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment))))); - executor.checkBeforeUpdate(sqlStatement, currentConfig); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); } } diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropDefaultShadowAlgorithmExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropDefaultShadowAlgorithmExecutorTest.java index 46791c81c6d4f..873080cced61b 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropDefaultShadowAlgorithmExecutorTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropDefaultShadowAlgorithmExecutorTest.java @@ -23,6 +23,7 @@ import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.distsql.handler.update.DropDefaultShadowAlgorithmExecutor; import org.apache.shardingsphere.shadow.distsql.statement.DropDefaultShadowAlgorithmStatement; +import org.apache.shardingsphere.shadow.rule.ShadowRule; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -34,6 +35,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class DropDefaultShadowAlgorithmExecutorTest { @@ -50,13 +52,15 @@ void setUp() { @Test void assertCheckWithoutDefaultAlgorithm() { - assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(new DropDefaultShadowAlgorithmStatement(false), currentConfig)); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(currentConfig); + executor.setRule(rule); + assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(new DropDefaultShadowAlgorithmStatement(false))); } @Test void assertCheckWithIfExists() { - executor.checkBeforeUpdate(new DropDefaultShadowAlgorithmStatement(true), currentConfig); - executor.checkBeforeUpdate(new DropDefaultShadowAlgorithmStatement(true), null); + executor.checkBeforeUpdate(new DropDefaultShadowAlgorithmStatement(true)); } @Test @@ -64,10 +68,10 @@ void assertUpdate() { ShadowRuleConfiguration ruleConfig = new ShadowRuleConfiguration(); ruleConfig.setDefaultShadowAlgorithmName("default"); ruleConfig.getShadowAlgorithms().put(ruleConfig.getDefaultShadowAlgorithmName(), mock(AlgorithmConfiguration.class)); - DropDefaultShadowAlgorithmStatement statement = new DropDefaultShadowAlgorithmStatement(false); - executor.checkBeforeUpdate(new DropDefaultShadowAlgorithmStatement(true), ruleConfig); - assertTrue(executor.hasAnyOneToBeDropped(statement, ruleConfig)); - executor.updateCurrentRuleConfiguration(statement, ruleConfig); + executor.checkBeforeUpdate(new DropDefaultShadowAlgorithmStatement(true)); + DropDefaultShadowAlgorithmStatement sqlStatement = new DropDefaultShadowAlgorithmStatement(false); + assertTrue(executor.hasAnyOneToBeDropped(sqlStatement, ruleConfig)); + executor.updateCurrentRuleConfiguration(sqlStatement, ruleConfig); assertNull(ruleConfig.getDefaultShadowAlgorithmName()); assertTrue(ruleConfig.getShadowAlgorithms().isEmpty()); } diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowAlgorithmExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowAlgorithmExecutorTest.java index d994a43d84cc3..1d467fa4f1d2a 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowAlgorithmExecutorTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowAlgorithmExecutorTest.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.distsql.handler.update.DropShadowAlgorithmExecutor; import org.apache.shardingsphere.shadow.distsql.statement.DropShadowAlgorithmStatement; +import org.apache.shardingsphere.shadow.rule.ShadowRule; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -30,6 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class DropShadowAlgorithmExecutorTest { @@ -43,15 +45,21 @@ void setUp() { @Test void assertExecuteWithIfExists() { DropShadowAlgorithmStatement sqlStatement = createSQLStatement(true, "ruleSegment"); - executor.checkBeforeUpdate(sqlStatement, mock(ShadowRuleConfiguration.class)); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(new ShadowRuleConfiguration()); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); } @Test void assertUpdate() { - DropShadowAlgorithmStatement sqlStatement = createSQLStatement("shadow_algorithm"); ShadowRuleConfiguration ruleConfig = new ShadowRuleConfiguration(); ruleConfig.getShadowAlgorithms().put("shadow_algorithm", new AlgorithmConfiguration("type", null)); - executor.checkBeforeUpdate(sqlStatement, ruleConfig); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(ruleConfig); + executor.setRule(rule); + DropShadowAlgorithmStatement sqlStatement = createSQLStatement("shadow_algorithm"); + executor.checkBeforeUpdate(sqlStatement); executor.updateCurrentRuleConfiguration(sqlStatement, ruleConfig); assertTrue(ruleConfig.getShadowAlgorithms().isEmpty()); } diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowRuleExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowRuleExecutorTest.java index 5625bc296a7cc..1272fbbcc6a63 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowRuleExecutorTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowRuleExecutorTest.java @@ -25,6 +25,7 @@ import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration; import org.apache.shardingsphere.shadow.distsql.handler.update.DropShadowRuleExecutor; import org.apache.shardingsphere.shadow.distsql.statement.DropShadowRuleStatement; +import org.apache.shardingsphere.shadow.rule.ShadowRule; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -39,6 +40,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class DropShadowRuleExecutorTest { @@ -51,20 +53,29 @@ void setUp() { @Test void assertCheckWithRuleNotExisted() { + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(new ShadowRuleConfiguration()); + executor.setRule(rule); assertThrows(MissingRequiredRuleException.class, - () -> executor.checkBeforeUpdate(createSQLStatement("notExistedRuleName"), mock(ShadowRuleConfiguration.class))); + () -> executor.checkBeforeUpdate(createSQLStatement("notExistedRuleName"))); } @Test void assertCheckWithIfExists() { - executor.checkBeforeUpdate(createSQLStatement(true, "notExistedRuleName"), mock(ShadowRuleConfiguration.class)); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(new ShadowRuleConfiguration()); + executor.setRule(rule); + executor.checkBeforeUpdate(createSQLStatement(true, "notExistedRuleName")); } @Test void assertUpdateCurrentRuleConfigurationWithUnusedAlgorithms() { DropShadowRuleStatement sqlStatement = createSQLStatement("shadow_group"); ShadowRuleConfiguration ruleConfig = createCurrentRuleConfiguration(); - executor.checkBeforeUpdate(sqlStatement, ruleConfig); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(ruleConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); assertTrue(executor.updateCurrentRuleConfiguration(sqlStatement, ruleConfig)); assertTrue(ruleConfig.getDataSources().isEmpty()); assertTrue(ruleConfig.getTables().isEmpty()); @@ -75,7 +86,10 @@ void assertUpdateCurrentRuleConfigurationWithUnusedAlgorithms() { void assertUpdateMultipleCurrentRuleConfigurationWithInUsedAlgorithms() { DropShadowRuleStatement sqlStatement = createSQLStatement("shadow_group"); ShadowRuleConfiguration ruleConfig = createMultipleCurrentRuleConfiguration(); - executor.checkBeforeUpdate(sqlStatement, ruleConfig); + ShadowRule rule = mock(ShadowRule.class); + when(rule.getConfiguration()).thenReturn(ruleConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); assertTrue(executor.updateCurrentRuleConfiguration(sqlStatement, ruleConfig)); assertTrue(ruleConfig.getDataSources().isEmpty()); assertTrue(ruleConfig.getTables().isEmpty()); diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterDefaultShardingStrategyExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterDefaultShardingStrategyExecutor.java index 3a37590fbf0be..2cb2d562388c9 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterDefaultShardingStrategyExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterDefaultShardingStrategyExecutor.java @@ -25,8 +25,8 @@ import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleAlterExecutor; import org.apache.shardingsphere.distsql.segment.AlgorithmSegment; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration; @@ -34,6 +34,7 @@ import org.apache.shardingsphere.sharding.distsql.handler.enums.ShardingStrategyLevelType; import org.apache.shardingsphere.sharding.distsql.handler.enums.ShardingStrategyType; import org.apache.shardingsphere.sharding.distsql.statement.AlterDefaultShardingStrategyStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import java.util.Optional; @@ -42,16 +43,18 @@ */ @DistSQLExecutorCurrentRuleRequired("Sharding") @Setter -public final class AlterDefaultShardingStrategyExecutor implements DatabaseRuleAlterExecutor { +public final class AlterDefaultShardingStrategyExecutor implements DatabaseRuleAlterExecutor { private ShardingSphereDatabase database; + private ShardingRule rule; + @Override - public void checkBeforeUpdate(final AlterDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final AlterDefaultShardingStrategyStatement sqlStatement) { if (!"none".equalsIgnoreCase(sqlStatement.getStrategyType())) { checkAlgorithm(sqlStatement); } - checkExist(sqlStatement, currentRuleConfig); + checkExist(sqlStatement); } private void checkAlgorithm(final AlterDefaultShardingStrategyStatement sqlStatement) { @@ -65,16 +68,16 @@ private boolean isAlgorithmDefinitionExists(final AlterDefaultShardingStrategySt return null != sqlStatement.getAlgorithmSegment(); } - private void checkExist(final AlterDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Optional strategyConfig = getStrategyConfiguration(currentRuleConfig, sqlStatement.getDefaultType()); + private void checkExist(final AlterDefaultShardingStrategyStatement sqlStatement) { + Optional strategyConfig = getStrategyConfiguration(sqlStatement.getDefaultType()); ShardingSpherePreconditions.checkState(strategyConfig.isPresent(), () -> new MissingRequiredRuleException(String.format("Default sharding %s strategy", sqlStatement.getDefaultType().toLowerCase()), database.getName())); } - private Optional getStrategyConfiguration(final ShardingRuleConfiguration currentRuleConfig, final String type) { + private Optional getStrategyConfiguration(final String type) { ShardingStrategyConfiguration result = type.equalsIgnoreCase(ShardingStrategyLevelType.TABLE.name()) - ? currentRuleConfig.getDefaultTableShardingStrategy() - : currentRuleConfig.getDefaultDatabaseShardingStrategy(); + ? rule.getConfiguration().getDefaultTableShardingStrategy() + : rule.getConfiguration().getDefaultDatabaseShardingStrategy(); return Optional.ofNullable(result); } @@ -132,8 +135,8 @@ public void updateCurrentRuleConfiguration(final ShardingRuleConfiguration curre } @Override - public Class getRuleConfigurationClass() { - return ShardingRuleConfiguration.class; + public Class getRuleClass() { + return ShardingRule.class; } @Override diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableReferenceRuleExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableReferenceRuleExecutor.java index 28a4edcfb72b2..94e972b485131 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableReferenceRuleExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableReferenceRuleExecutor.java @@ -23,8 +23,8 @@ import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; import org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorCurrentRuleRequired; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleAlterExecutor; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableReferenceRuleConfiguration; @@ -32,6 +32,7 @@ import org.apache.shardingsphere.sharding.distsql.handler.checker.ShardingTableRuleStatementChecker; import org.apache.shardingsphere.sharding.distsql.segment.table.TableReferenceRuleSegment; import org.apache.shardingsphere.sharding.distsql.statement.AlterShardingTableReferenceRuleStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import java.util.Arrays; import java.util.Collection; @@ -44,33 +45,35 @@ */ @DistSQLExecutorCurrentRuleRequired("Sharding") @Setter -public final class AlterShardingTableReferenceRuleExecutor implements DatabaseRuleAlterExecutor { +public final class AlterShardingTableReferenceRuleExecutor implements DatabaseRuleAlterExecutor { private ShardingSphereDatabase database; + private ShardingRule rule; + @Override - public void checkBeforeUpdate(final AlterShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - checkToBeAlteredRulesExisted(sqlStatement, currentRuleConfig); - checkDuplicatedTablesInShardingTableReferenceRules(sqlStatement, currentRuleConfig); - checkToBeReferencedShardingTablesExisted(sqlStatement, currentRuleConfig); - checkShardingTableReferenceRulesValid(sqlStatement, currentRuleConfig); + public void checkBeforeUpdate(final AlterShardingTableReferenceRuleStatement sqlStatement) { + checkToBeAlteredRulesExisted(sqlStatement); + checkDuplicatedTablesInShardingTableReferenceRules(sqlStatement); + checkToBeReferencedShardingTablesExisted(sqlStatement); + checkShardingTableReferenceRulesValid(sqlStatement); } - private void checkToBeAlteredRulesExisted(final AlterShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection currentRuleNames = currentRuleConfig.getBindingTableGroups().stream().map(ShardingTableReferenceRuleConfiguration::getName).collect(Collectors.toSet()); + private void checkToBeAlteredRulesExisted(final AlterShardingTableReferenceRuleStatement sqlStatement) { + Collection currentRuleNames = rule.getConfiguration().getBindingTableGroups().stream().map(ShardingTableReferenceRuleConfiguration::getName).collect(Collectors.toSet()); Collection notExistedRuleNames = sqlStatement.getRules().stream().map(TableReferenceRuleSegment::getName).filter(each -> !currentRuleNames.contains(each)).collect(Collectors.toSet()); ShardingSpherePreconditions.checkState(notExistedRuleNames.isEmpty(), () -> new MissingRequiredRuleException("Sharding table reference", database.getName(), notExistedRuleNames)); } - private void checkDuplicatedTablesInShardingTableReferenceRules(final AlterShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection currentReferencedTableNames = getReferencedTableNames(currentRuleConfig, getToBeAlteredRuleNames(sqlStatement)); + private void checkDuplicatedTablesInShardingTableReferenceRules(final AlterShardingTableReferenceRuleStatement sqlStatement) { + Collection currentReferencedTableNames = getReferencedTableNames(getToBeAlteredRuleNames(sqlStatement)); Collection duplicatedTableNames = sqlStatement.getTableNames().stream().filter(currentReferencedTableNames::contains).collect(Collectors.toSet()); ShardingSpherePreconditions.checkState(duplicatedTableNames.isEmpty(), () -> new DuplicateRuleException("sharding table reference", database.getName(), duplicatedTableNames)); } - private Collection getReferencedTableNames(final ShardingRuleConfiguration currentRuleConfig, final Collection getToBeAlteredRuleNames) { + private Collection getReferencedTableNames(final Collection getToBeAlteredRuleNames) { Collection result = new HashSet<>(); - currentRuleConfig.getBindingTableGroups().forEach(each -> { + rule.getConfiguration().getBindingTableGroups().forEach(each -> { if (!getToBeAlteredRuleNames.contains(each.getName())) { result.addAll(Arrays.stream(each.getReference().split(",")).map(String::trim).collect(Collectors.toSet())); } @@ -86,16 +89,16 @@ private Collection getToBeAlteredRuleNames(final ShardingRuleConfigurati return ruleConfig.getBindingTableGroups().stream().map(ShardingTableReferenceRuleConfiguration::getName).collect(Collectors.toSet()); } - private void checkToBeReferencedShardingTablesExisted(final AlterShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection existedShardingTables = getCurrentShardingTables(currentRuleConfig); + private void checkToBeReferencedShardingTablesExisted(final AlterShardingTableReferenceRuleStatement sqlStatement) { + Collection existedShardingTables = getCurrentShardingTables(); Collection notExistedShardingTables = sqlStatement.getTableNames().stream().filter(each -> !containsIgnoreCase(existedShardingTables, each)).collect(Collectors.toSet()); ShardingSpherePreconditions.checkState(notExistedShardingTables.isEmpty(), () -> new MissingRequiredRuleException("Sharding", database.getName(), notExistedShardingTables)); } - private Collection getCurrentShardingTables(final ShardingRuleConfiguration currentRuleConfig) { + private Collection getCurrentShardingTables() { Collection result = new HashSet<>(); - result.addAll(currentRuleConfig.getTables().stream().map(ShardingTableRuleConfiguration::getLogicTable).collect(Collectors.toSet())); - result.addAll(currentRuleConfig.getAutoTables().stream().map(ShardingAutoTableRuleConfiguration::getLogicTable).collect(Collectors.toSet())); + result.addAll(rule.getConfiguration().getTables().stream().map(ShardingTableRuleConfiguration::getLogicTable).collect(Collectors.toSet())); + result.addAll(rule.getConfiguration().getAutoTables().stream().map(ShardingAutoTableRuleConfiguration::getLogicTable).collect(Collectors.toSet())); return result; } @@ -103,10 +106,10 @@ private boolean containsIgnoreCase(final Collection currentRules, final return currentRules.stream().anyMatch(each -> each.equalsIgnoreCase(ruleName)); } - private void checkShardingTableReferenceRulesValid(final AlterShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection toBeAlteredShardingTableReferenceRules = buildToBeAlteredRuleConfiguration(sqlStatement, currentRuleConfig).getBindingTableGroups(); + private void checkShardingTableReferenceRulesValid(final AlterShardingTableReferenceRuleStatement sqlStatement) { + Collection toBeAlteredShardingTableReferenceRules = buildToBeAlteredRuleConfiguration(sqlStatement, rule.getConfiguration()).getBindingTableGroups(); Collection ruleNames = toBeAlteredShardingTableReferenceRules.stream().map(ShardingTableReferenceRuleConfiguration::getName).collect(Collectors.toList()); - ShardingSpherePreconditions.checkState(ShardingTableRuleStatementChecker.isValidBindingTableGroups(toBeAlteredShardingTableReferenceRules, currentRuleConfig), + ShardingSpherePreconditions.checkState(ShardingTableRuleStatementChecker.isValidBindingTableGroups(toBeAlteredShardingTableReferenceRules, rule.getConfiguration()), () -> new InvalidRuleConfigurationException("sharding table", ruleNames, Collections.singleton("invalid sharding table reference."))); } @@ -125,8 +128,8 @@ public void updateCurrentRuleConfiguration(final ShardingRuleConfiguration curre } @Override - public Class getRuleConfigurationClass() { - return ShardingRuleConfiguration.class; + public Class getRuleClass() { + return ShardingRule.class; } @Override diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableRuleExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableRuleExecutor.java index 69c2be3292c36..0954249c12274 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableRuleExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableRuleExecutor.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.sharding.distsql.handler.checker.ShardingTableRuleStatementChecker; import org.apache.shardingsphere.sharding.distsql.handler.converter.ShardingTableRuleStatementConverter; import org.apache.shardingsphere.sharding.distsql.statement.AlterShardingTableRuleStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import java.util.Collection; import java.util.stream.Collectors; @@ -36,13 +37,15 @@ */ @DistSQLExecutorCurrentRuleRequired("Sharding") @Setter -public final class AlterShardingTableRuleExecutor implements DatabaseRuleAlterExecutor { +public final class AlterShardingTableRuleExecutor implements DatabaseRuleAlterExecutor { private ShardingSphereDatabase database; + private ShardingRule rule; + @Override - public void checkBeforeUpdate(final AlterShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - ShardingTableRuleStatementChecker.checkAlteration(database, sqlStatement.getRules(), currentRuleConfig); + public void checkBeforeUpdate(final AlterShardingTableRuleStatement sqlStatement) { + ShardingTableRuleStatementChecker.checkAlteration(database, sqlStatement.getRules(), rule.getConfiguration()); } @Override @@ -97,8 +100,8 @@ private void addRuleConfiguration(final ShardingRuleConfiguration currentRuleCon } @Override - public Class getRuleConfigurationClass() { - return ShardingRuleConfiguration.class; + public Class getRuleClass() { + return ShardingRule.class; } @Override diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateDefaultShardingStrategyExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateDefaultShardingStrategyExecutor.java index 050b41bfce07e..2a40b312d897a 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateDefaultShardingStrategyExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateDefaultShardingStrategyExecutor.java @@ -24,8 +24,8 @@ import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleCreateExecutor; import org.apache.shardingsphere.distsql.segment.AlgorithmSegment; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration; @@ -33,6 +33,7 @@ import org.apache.shardingsphere.sharding.distsql.handler.enums.ShardingStrategyLevelType; import org.apache.shardingsphere.sharding.distsql.handler.enums.ShardingStrategyType; import org.apache.shardingsphere.sharding.distsql.statement.CreateDefaultShardingStrategyStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import java.util.Optional; @@ -40,17 +41,19 @@ * Create default sharding strategy executor. */ @Setter -public final class CreateDefaultShardingStrategyExecutor implements DatabaseRuleCreateExecutor { +public final class CreateDefaultShardingStrategyExecutor implements DatabaseRuleCreateExecutor { private ShardingSphereDatabase database; + private ShardingRule rule; + @Override - public void checkBeforeUpdate(final CreateDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final CreateDefaultShardingStrategyStatement sqlStatement) { if (!"none".equalsIgnoreCase(sqlStatement.getStrategyType())) { checkAlgorithm(sqlStatement); } if (!sqlStatement.isIfNotExists()) { - checkExist(sqlStatement, currentRuleConfig); + checkExist(sqlStatement); } } @@ -58,26 +61,21 @@ private void checkAlgorithm(final CreateDefaultShardingStrategyStatement sqlStat ShardingSpherePreconditions.checkState(ShardingStrategyType.contains(sqlStatement.getStrategyType()), () -> new InvalidAlgorithmConfigurationException(sqlStatement.getStrategyType())); ShardingSpherePreconditions.checkState(ShardingStrategyType.getValueOf(sqlStatement.getStrategyType()) .isValid(sqlStatement.getShardingColumn()), () -> new InvalidAlgorithmConfigurationException(sqlStatement.getStrategyType())); - ShardingSpherePreconditions.checkState(isAlgorithmDefinitionExists(sqlStatement), MissingRequiredAlgorithmException::new); - } - - private boolean isAlgorithmDefinitionExists(final CreateDefaultShardingStrategyStatement sqlStatement) { - return null != sqlStatement.getAlgorithmSegment(); + ShardingSpherePreconditions.checkNotNull(sqlStatement.getAlgorithmSegment(), MissingRequiredAlgorithmException::new); } - private void checkExist(final CreateDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - if (null == currentRuleConfig) { + private void checkExist(final CreateDefaultShardingStrategyStatement sqlStatement) { + if (null == rule) { return; } - Optional strategyConfig = getStrategyConfiguration(currentRuleConfig, sqlStatement.getDefaultType()); - ShardingSpherePreconditions.checkState(!strategyConfig.isPresent(), + ShardingSpherePreconditions.checkState(!getStrategyConfiguration(sqlStatement.getDefaultType()).isPresent(), () -> new DuplicateRuleException(String.format("default sharding %s strategy", sqlStatement.getDefaultType().toLowerCase()), database.getName())); } - private Optional getStrategyConfiguration(final ShardingRuleConfiguration currentRuleConfig, final String type) { + private Optional getStrategyConfiguration(final String type) { ShardingStrategyConfiguration result = type.equalsIgnoreCase(ShardingStrategyLevelType.TABLE.name()) - ? currentRuleConfig.getDefaultTableShardingStrategy() - : currentRuleConfig.getDefaultDatabaseShardingStrategy(); + ? rule.getConfiguration().getDefaultTableShardingStrategy() + : rule.getConfiguration().getDefaultDatabaseShardingStrategy(); return Optional.ofNullable(result); } @@ -138,8 +136,8 @@ public void updateCurrentRuleConfiguration(final ShardingRuleConfiguration curre } @Override - public Class getRuleConfigurationClass() { - return ShardingRuleConfiguration.class; + public Class getRuleClass() { + return ShardingRule.class; } @Override diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableReferenceRuleExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableReferenceRuleExecutor.java index a127753850c24..2936a9c2cdad2 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableReferenceRuleExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableReferenceRuleExecutor.java @@ -23,8 +23,8 @@ import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; import org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorCurrentRuleRequired; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleCreateExecutor; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableReferenceRuleConfiguration; @@ -32,6 +32,7 @@ import org.apache.shardingsphere.sharding.distsql.handler.checker.ShardingTableRuleStatementChecker; import org.apache.shardingsphere.sharding.distsql.segment.table.TableReferenceRuleSegment; import org.apache.shardingsphere.sharding.distsql.statement.CreateShardingTableReferenceRuleStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import java.util.Arrays; import java.util.Collection; @@ -44,53 +45,55 @@ */ @DistSQLExecutorCurrentRuleRequired("Sharding") @Setter -public final class CreateShardingTableReferenceRuleExecutor implements DatabaseRuleCreateExecutor { +public final class CreateShardingTableReferenceRuleExecutor implements DatabaseRuleCreateExecutor { private ShardingSphereDatabase database; + private ShardingRule rule; + @Override - public void checkBeforeUpdate(final CreateShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final CreateShardingTableReferenceRuleStatement sqlStatement) { if (!sqlStatement.isIfNotExists()) { - checkDuplicatedRuleNames(sqlStatement, currentRuleConfig); + checkDuplicatedRuleNames(sqlStatement); } - checkDuplicatedTablesInShardingTableReferenceRules(sqlStatement, currentRuleConfig); - checkToBeReferencedShardingTablesExisted(sqlStatement, currentRuleConfig); - checkShardingTableReferenceRulesValid(sqlStatement, currentRuleConfig); + checkDuplicatedTablesInShardingTableReferenceRules(sqlStatement); + checkToBeReferencedShardingTablesExisted(sqlStatement); + checkShardingTableReferenceRulesValid(sqlStatement); } - private void checkDuplicatedRuleNames(final CreateShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement, currentRuleConfig); + private void checkDuplicatedRuleNames(final CreateShardingTableReferenceRuleStatement sqlStatement) { + Collection duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement); ShardingSpherePreconditions.checkState(duplicatedRuleNames.isEmpty(), () -> new DuplicateRuleException("sharding table reference", database.getName(), duplicatedRuleNames)); } - private void checkDuplicatedTablesInShardingTableReferenceRules(final CreateShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection currentReferencedTableNames = getReferencedTableNames(currentRuleConfig); + private void checkDuplicatedTablesInShardingTableReferenceRules(final CreateShardingTableReferenceRuleStatement sqlStatement) { + Collection currentReferencedTableNames = getReferencedTableNames(); Collection duplicatedTableNames = sqlStatement.getTableNames().stream().filter(currentReferencedTableNames::contains).collect(Collectors.toSet()); ShardingSpherePreconditions.checkState(duplicatedTableNames.isEmpty(), () -> new DuplicateRuleException("sharding table reference", database.getName(), duplicatedTableNames)); } - private void checkToBeReferencedShardingTablesExisted(final CreateShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection existedShardingTables = getCurrentLogicTables(currentRuleConfig); + private void checkToBeReferencedShardingTablesExisted(final CreateShardingTableReferenceRuleStatement sqlStatement) { + Collection existedShardingTables = getCurrentLogicTables(); Collection notExistedShardingTables = sqlStatement.getTableNames().stream().filter(each -> !containsIgnoreCase(existedShardingTables, each)).collect(Collectors.toSet()); ShardingSpherePreconditions.checkState(notExistedShardingTables.isEmpty(), () -> new MissingRequiredRuleException("Sharding", database.getName(), notExistedShardingTables)); } - private Collection getCurrentLogicTables(final ShardingRuleConfiguration currentRuleConfig) { + private Collection getCurrentLogicTables() { Collection result = new HashSet<>(); - result.addAll(currentRuleConfig.getTables().stream().map(ShardingTableRuleConfiguration::getLogicTable).collect(Collectors.toSet())); - result.addAll(currentRuleConfig.getAutoTables().stream().map(ShardingAutoTableRuleConfiguration::getLogicTable).collect(Collectors.toSet())); + result.addAll(rule.getConfiguration().getTables().stream().map(ShardingTableRuleConfiguration::getLogicTable).collect(Collectors.toSet())); + result.addAll(rule.getConfiguration().getAutoTables().stream().map(ShardingAutoTableRuleConfiguration::getLogicTable).collect(Collectors.toSet())); return result; } - private void checkShardingTableReferenceRulesValid(final CreateShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection bindingTableGroups = buildToBeCreatedRuleConfiguration(sqlStatement, currentRuleConfig).getBindingTableGroups(); + private void checkShardingTableReferenceRulesValid(final CreateShardingTableReferenceRuleStatement sqlStatement) { + Collection bindingTableGroups = buildToBeCreatedRuleConfiguration(sqlStatement, rule.getConfiguration()).getBindingTableGroups(); Collection names = bindingTableGroups.stream().map(ShardingTableReferenceRuleConfiguration::getName).collect(Collectors.toList()); - ShardingSpherePreconditions.checkState(ShardingTableRuleStatementChecker.isValidBindingTableGroups(bindingTableGroups, currentRuleConfig), + ShardingSpherePreconditions.checkState(ShardingTableRuleStatementChecker.isValidBindingTableGroups(bindingTableGroups, rule.getConfiguration()), () -> new InvalidRuleConfigurationException("sharding table", names, Collections.singleton("invalid sharding table reference."))); } - private Collection getReferencedTableNames(final ShardingRuleConfiguration currentRuleConfig) { - return currentRuleConfig.getBindingTableGroups().stream().flatMap(each -> Arrays.stream(each.getReference().split(","))).map(String::trim).collect(Collectors.toList()); + private Collection getReferencedTableNames() { + return rule.getConfiguration().getBindingTableGroups().stream().flatMap(each -> Arrays.stream(each.getReference().split(","))).map(String::trim).collect(Collectors.toList()); } private boolean containsIgnoreCase(final Collection currentRules, final String ruleName) { @@ -101,7 +104,7 @@ private boolean containsIgnoreCase(final Collection currentRules, final public ShardingRuleConfiguration buildToBeCreatedRuleConfiguration(final CreateShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { Collection segments = sqlStatement.getRules(); if (sqlStatement.isIfNotExists()) { - Collection duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement, currentRuleConfig); + Collection duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement); segments.removeIf(each -> duplicatedRuleNames.contains(each.getName())); } ShardingRuleConfiguration result = new ShardingRuleConfiguration(); @@ -109,8 +112,8 @@ public ShardingRuleConfiguration buildToBeCreatedRuleConfiguration(final CreateS return result; } - private Collection getDuplicatedRuleNames(final CreateShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection currentRuleNames = currentRuleConfig.getBindingTableGroups().stream().map(ShardingTableReferenceRuleConfiguration::getName).collect(Collectors.toSet()); + private Collection getDuplicatedRuleNames(final CreateShardingTableReferenceRuleStatement sqlStatement) { + Collection currentRuleNames = rule.getConfiguration().getBindingTableGroups().stream().map(ShardingTableReferenceRuleConfiguration::getName).collect(Collectors.toSet()); return sqlStatement.getRules().stream().map(TableReferenceRuleSegment::getName).filter(currentRuleNames::contains).collect(Collectors.toSet()); } @@ -120,8 +123,8 @@ public void updateCurrentRuleConfiguration(final ShardingRuleConfiguration curre } @Override - public Class getRuleConfigurationClass() { - return ShardingRuleConfiguration.class; + public Class getRuleClass() { + return ShardingRule.class; } @Override diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableRuleExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableRuleExecutor.java index 59f2d8965a65b..dce4baf786a4f 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableRuleExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableRuleExecutor.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.sharding.distsql.handler.converter.ShardingTableRuleStatementConverter; import org.apache.shardingsphere.sharding.distsql.segment.table.AbstractTableRuleSegment; import org.apache.shardingsphere.sharding.distsql.statement.CreateShardingTableRuleStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import java.util.Collection; import java.util.Collections; @@ -37,13 +38,15 @@ * Create sharding table rule executor. */ @Setter -public final class CreateShardingTableRuleExecutor implements DatabaseRuleCreateExecutor { +public final class CreateShardingTableRuleExecutor implements DatabaseRuleCreateExecutor { private ShardingSphereDatabase database; + private ShardingRule rule; + @Override - public void checkBeforeUpdate(final CreateShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - ShardingTableRuleStatementChecker.checkCreation(database, sqlStatement.getRules(), sqlStatement.isIfNotExists(), currentRuleConfig); + public void checkBeforeUpdate(final CreateShardingTableRuleStatement sqlStatement) { + ShardingTableRuleStatementChecker.checkCreation(database, sqlStatement.getRules(), sqlStatement.isIfNotExists(), null == rule ? null : rule.getConfiguration()); } @Override @@ -78,8 +81,8 @@ private Collection getCurrentShardingTables(final ShardingRuleConfigurat } @Override - public Class getRuleConfigurationClass() { - return ShardingRuleConfiguration.class; + public Class getRuleClass() { + return ShardingRule.class; } @Override diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropDefaultShardingStrategyExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropDefaultShardingStrategyExecutor.java index a116be695b2e1..5ec4141626eb1 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropDefaultShardingStrategyExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropDefaultShardingStrategyExecutor.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.handler.enums.ShardingStrategyLevelType; import org.apache.shardingsphere.sharding.distsql.statement.DropDefaultShardingStrategyStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import java.util.Optional; @@ -35,27 +36,29 @@ */ @DistSQLExecutorCurrentRuleRequired("Sharding") @Setter -public final class DropDefaultShardingStrategyExecutor implements DatabaseRuleDropExecutor { +public final class DropDefaultShardingStrategyExecutor implements DatabaseRuleDropExecutor { private ShardingSphereDatabase database; + private ShardingRule rule; + @Override - public void checkBeforeUpdate(final DropDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropDefaultShardingStrategyStatement sqlStatement) { if (!sqlStatement.isIfExists()) { - checkExist(sqlStatement, currentRuleConfig); + checkExist(sqlStatement); } } - private void checkExist(final DropDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Optional strategyConfig = getStrategyConfiguration(currentRuleConfig, sqlStatement.getDefaultType()); + private void checkExist(final DropDefaultShardingStrategyStatement sqlStatement) { + Optional strategyConfig = getStrategyConfiguration(sqlStatement.getDefaultType()); ShardingSpherePreconditions.checkState(strategyConfig.isPresent(), () -> new MissingRequiredRuleException( String.format("Default sharding %s strategy", sqlStatement.getDefaultType().toLowerCase()), database.getName())); } - private Optional getStrategyConfiguration(final ShardingRuleConfiguration currentRuleConfig, final String type) { + private Optional getStrategyConfiguration(final String type) { ShardingStrategyConfiguration result = type.equalsIgnoreCase(ShardingStrategyLevelType.TABLE.name()) - ? currentRuleConfig.getDefaultTableShardingStrategy() - : currentRuleConfig.getDefaultDatabaseShardingStrategy(); + ? rule.getConfiguration().getDefaultTableShardingStrategy() + : rule.getConfiguration().getDefaultDatabaseShardingStrategy(); return Optional.ofNullable(result); } @@ -93,8 +96,8 @@ public boolean updateCurrentRuleConfiguration(final DropDefaultShardingStrategyS } @Override - public Class getRuleConfigurationClass() { - return ShardingRuleConfiguration.class; + public Class getRuleClass() { + return ShardingRule.class; } @Override diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAlgorithmExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAlgorithmExecutor.java index 5161fceda02b6..1fbca2f9236f1 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAlgorithmExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAlgorithmExecutor.java @@ -22,11 +22,12 @@ import org.apache.shardingsphere.distsql.handler.exception.algorithm.MissingRequiredAlgorithmException; import org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorCurrentRuleRequired; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.statement.DropShardingAlgorithmStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import java.util.Collection; import java.util.LinkedHashSet; @@ -37,35 +38,37 @@ */ @DistSQLExecutorCurrentRuleRequired("Sharding") @Setter -public final class DropShardingAlgorithmExecutor implements DatabaseRuleDropExecutor { +public final class DropShardingAlgorithmExecutor implements DatabaseRuleDropExecutor { private ShardingSphereDatabase database; + private ShardingRule rule; + @Override - public void checkBeforeUpdate(final DropShardingAlgorithmStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropShardingAlgorithmStatement sqlStatement) { if (!sqlStatement.isIfExists()) { - checkToBeDroppedShardingAlgorithms(sqlStatement, currentRuleConfig); + checkToBeDroppedShardingAlgorithms(sqlStatement); } - checkShardingAlgorithmsInUsed(sqlStatement, currentRuleConfig); + checkShardingAlgorithmsInUsed(sqlStatement); } - private void checkToBeDroppedShardingAlgorithms(final DropShardingAlgorithmStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection currentShardingAlgorithms = getCurrentShardingAlgorithms(currentRuleConfig); + private void checkToBeDroppedShardingAlgorithms(final DropShardingAlgorithmStatement sqlStatement) { + Collection currentShardingAlgorithms = getCurrentShardingAlgorithms(); Collection notExistedAlgorithms = sqlStatement.getNames().stream().filter(each -> !currentShardingAlgorithms.contains(each)).collect(Collectors.toList()); if (!notExistedAlgorithms.isEmpty()) { throw new MissingRequiredAlgorithmException(database.getName(), notExistedAlgorithms); } } - private void checkShardingAlgorithmsInUsed(final DropShardingAlgorithmStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection allInUsed = getAllOfAlgorithmsInUsed(currentRuleConfig); + private void checkShardingAlgorithmsInUsed(final DropShardingAlgorithmStatement sqlStatement) { + Collection allInUsed = getAllOfAlgorithmsInUsed(); Collection usedAlgorithms = sqlStatement.getNames().stream().filter(allInUsed::contains).collect(Collectors.toList()); ShardingSpherePreconditions.checkState(usedAlgorithms.isEmpty(), () -> new AlgorithmInUsedException("Sharding", database.getName(), usedAlgorithms)); } - private Collection getAllOfAlgorithmsInUsed(final ShardingRuleConfiguration shardingRuleConfig) { + private Collection getAllOfAlgorithmsInUsed() { Collection result = new LinkedHashSet<>(); - shardingRuleConfig.getTables().forEach(each -> { + rule.getConfiguration().getTables().forEach(each -> { if (null != each.getDatabaseShardingStrategy()) { result.add(each.getDatabaseShardingStrategy().getShardingAlgorithmName()); } @@ -73,20 +76,20 @@ private Collection getAllOfAlgorithmsInUsed(final ShardingRuleConfigurat result.add(each.getTableShardingStrategy().getShardingAlgorithmName()); } }); - shardingRuleConfig.getAutoTables().stream().filter(each -> null != each.getShardingStrategy()).forEach(each -> result.add(each.getShardingStrategy().getShardingAlgorithmName())); - ShardingStrategyConfiguration tableShardingStrategy = shardingRuleConfig.getDefaultTableShardingStrategy(); + rule.getConfiguration().getAutoTables().stream().filter(each -> null != each.getShardingStrategy()).forEach(each -> result.add(each.getShardingStrategy().getShardingAlgorithmName())); + ShardingStrategyConfiguration tableShardingStrategy = rule.getConfiguration().getDefaultTableShardingStrategy(); if (null != tableShardingStrategy && !tableShardingStrategy.getShardingAlgorithmName().isEmpty()) { result.add(tableShardingStrategy.getShardingAlgorithmName()); } - ShardingStrategyConfiguration databaseShardingStrategy = shardingRuleConfig.getDefaultDatabaseShardingStrategy(); + ShardingStrategyConfiguration databaseShardingStrategy = rule.getConfiguration().getDefaultDatabaseShardingStrategy(); if (null != databaseShardingStrategy && !databaseShardingStrategy.getShardingAlgorithmName().isEmpty()) { result.add(databaseShardingStrategy.getShardingAlgorithmName()); } return result; } - private Collection getCurrentShardingAlgorithms(final ShardingRuleConfiguration shardingRuleConfig) { - return shardingRuleConfig.getShardingAlgorithms().keySet(); + private Collection getCurrentShardingAlgorithms() { + return rule.getConfiguration().getShardingAlgorithms().keySet(); } @Override @@ -101,23 +104,23 @@ public ShardingRuleConfiguration buildToBeDroppedRuleConfiguration(final DropSha @Override public boolean updateCurrentRuleConfiguration(final DropShardingAlgorithmStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { for (String each : sqlStatement.getNames()) { - dropShardingAlgorithm(currentRuleConfig, each); + dropShardingAlgorithm(each); } return false; } @Override public boolean hasAnyOneToBeDropped(final DropShardingAlgorithmStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - return null != currentRuleConfig && !getIdenticalData(getCurrentShardingAlgorithms(currentRuleConfig), sqlStatement.getNames()).isEmpty(); + return null != currentRuleConfig && !getIdenticalData(getCurrentShardingAlgorithms(), sqlStatement.getNames()).isEmpty(); } - private void dropShardingAlgorithm(final ShardingRuleConfiguration currentRuleConfig, final String algorithmName) { - getCurrentShardingAlgorithms(currentRuleConfig).removeIf(algorithmName::equalsIgnoreCase); + private void dropShardingAlgorithm(final String algorithmName) { + getCurrentShardingAlgorithms().removeIf(algorithmName::equalsIgnoreCase); } @Override - public Class getRuleConfigurationClass() { - return ShardingRuleConfiguration.class; + public Class getRuleClass() { + return ShardingRule.class; } @Override diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAuditorExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAuditorExecutor.java index 8d1b8a8de493d..6b0a337fee4c5 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAuditorExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAuditorExecutor.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.statement.DropShardingAuditorStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import java.util.Collection; import java.util.LinkedHashSet; @@ -37,34 +38,36 @@ */ @DistSQLExecutorCurrentRuleRequired("Sharding auditor") @Setter -public final class DropShardingAuditorExecutor implements DatabaseRuleDropExecutor { +public final class DropShardingAuditorExecutor implements DatabaseRuleDropExecutor { private ShardingSphereDatabase database; + private ShardingRule rule; + @Override - public void checkBeforeUpdate(final DropShardingAuditorStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropShardingAuditorStatement sqlStatement) { if (!sqlStatement.isIfExists()) { - checkExist(sqlStatement, currentRuleConfig); + checkExist(sqlStatement); } - checkInUsed(sqlStatement, currentRuleConfig); + checkInUsed(sqlStatement); } - private void checkExist(final DropShardingAuditorStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection notExistAuditors = sqlStatement.getNames().stream().filter(each -> !currentRuleConfig.getAuditors().containsKey(each)).collect(Collectors.toList()); + private void checkExist(final DropShardingAuditorStatement sqlStatement) { + Collection notExistAuditors = sqlStatement.getNames().stream().filter(each -> !rule.getConfiguration().getAuditors().containsKey(each)).collect(Collectors.toList()); ShardingSpherePreconditions.checkState(notExistAuditors.isEmpty(), () -> new MissingRequiredAlgorithmException("Sharding auditor", database.getName(), notExistAuditors)); } - private void checkInUsed(final DropShardingAuditorStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection usedAuditors = getUsedAuditors(currentRuleConfig); + private void checkInUsed(final DropShardingAuditorStatement sqlStatement) { + Collection usedAuditors = getUsedAuditors(); Collection inUsedNames = sqlStatement.getNames().stream().filter(usedAuditors::contains).collect(Collectors.toList()); ShardingSpherePreconditions.checkState(inUsedNames.isEmpty(), () -> new AlgorithmInUsedException("Sharding auditor", database.getName(), inUsedNames)); } - private Collection getUsedAuditors(final ShardingRuleConfiguration shardingRuleConfig) { + private Collection getUsedAuditors() { Collection result = new LinkedHashSet<>(); - shardingRuleConfig.getTables().stream().filter(each -> null != each.getAuditStrategy()).forEach(each -> result.addAll(each.getAuditStrategy().getAuditorNames())); - shardingRuleConfig.getAutoTables().stream().filter(each -> null != each.getAuditStrategy()).forEach(each -> result.addAll(each.getAuditStrategy().getAuditorNames())); - ShardingAuditStrategyConfiguration auditStrategy = shardingRuleConfig.getDefaultAuditStrategy(); + rule.getConfiguration().getTables().stream().filter(each -> null != each.getAuditStrategy()).forEach(each -> result.addAll(each.getAuditStrategy().getAuditorNames())); + rule.getConfiguration().getAutoTables().stream().filter(each -> null != each.getAuditStrategy()).forEach(each -> result.addAll(each.getAuditStrategy().getAuditorNames())); + ShardingAuditStrategyConfiguration auditStrategy = rule.getConfiguration().getDefaultAuditStrategy(); if (null != auditStrategy && !auditStrategy.getAuditorNames().isEmpty()) { result.addAll(auditStrategy.getAuditorNames()); } @@ -92,8 +95,8 @@ public boolean hasAnyOneToBeDropped(final DropShardingAuditorStatement sqlStatem } @Override - public Class getRuleConfigurationClass() { - return ShardingRuleConfiguration.class; + public Class getRuleClass() { + return ShardingRule.class; } @Override diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingKeyGeneratorExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingKeyGeneratorExecutor.java index 99d9d36095fc8..eefc6e7b1bec6 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingKeyGeneratorExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingKeyGeneratorExecutor.java @@ -28,6 +28,7 @@ import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.statement.DropShardingKeyGeneratorStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import java.util.Collection; import java.util.LinkedHashSet; @@ -38,34 +39,36 @@ */ @DistSQLExecutorCurrentRuleRequired("Key generator") @Setter -public final class DropShardingKeyGeneratorExecutor implements DatabaseRuleDropExecutor { +public final class DropShardingKeyGeneratorExecutor implements DatabaseRuleDropExecutor { private ShardingSphereDatabase database; + private ShardingRule rule; + @Override - public void checkBeforeUpdate(final DropShardingKeyGeneratorStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropShardingKeyGeneratorStatement sqlStatement) { if (!sqlStatement.isIfExists()) { - checkExist(sqlStatement, currentRuleConfig); + checkExist(sqlStatement); } - checkInUsed(sqlStatement, currentRuleConfig); + checkInUsed(sqlStatement); } - private void checkExist(final DropShardingKeyGeneratorStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection notExistKeyGenerators = sqlStatement.getNames().stream().filter(each -> !currentRuleConfig.getKeyGenerators().containsKey(each)).collect(Collectors.toList()); + private void checkExist(final DropShardingKeyGeneratorStatement sqlStatement) { + Collection notExistKeyGenerators = sqlStatement.getNames().stream().filter(each -> !rule.getConfiguration().getKeyGenerators().containsKey(each)).collect(Collectors.toList()); ShardingSpherePreconditions.checkState(notExistKeyGenerators.isEmpty(), () -> new MissingRequiredAlgorithmException("Key generator", database.getName(), notExistKeyGenerators)); } - private void checkInUsed(final DropShardingKeyGeneratorStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection usedKeyGenerators = getUsedKeyGenerators(currentRuleConfig); + private void checkInUsed(final DropShardingKeyGeneratorStatement sqlStatement) { + Collection usedKeyGenerators = getUsedKeyGenerators(); Collection inUsedNames = sqlStatement.getNames().stream().filter(usedKeyGenerators::contains).collect(Collectors.toList()); ShardingSpherePreconditions.checkState(inUsedNames.isEmpty(), () -> new AlgorithmInUsedException("Key generator", database.getName(), inUsedNames)); } - private Collection getUsedKeyGenerators(final ShardingRuleConfiguration shardingRuleConfig) { + private Collection getUsedKeyGenerators() { Collection result = new LinkedHashSet<>(); - shardingRuleConfig.getTables().stream().filter(each -> null != each.getKeyGenerateStrategy()).forEach(each -> result.add(each.getKeyGenerateStrategy().getKeyGeneratorName())); - shardingRuleConfig.getAutoTables().stream().filter(each -> null != each.getKeyGenerateStrategy()).forEach(each -> result.add(each.getKeyGenerateStrategy().getKeyGeneratorName())); - KeyGenerateStrategyConfiguration keyGenerateStrategy = shardingRuleConfig.getDefaultKeyGenerateStrategy(); + rule.getConfiguration().getTables().stream().filter(each -> null != each.getKeyGenerateStrategy()).forEach(each -> result.add(each.getKeyGenerateStrategy().getKeyGeneratorName())); + rule.getConfiguration().getAutoTables().stream().filter(each -> null != each.getKeyGenerateStrategy()).forEach(each -> result.add(each.getKeyGenerateStrategy().getKeyGeneratorName())); + KeyGenerateStrategyConfiguration keyGenerateStrategy = rule.getConfiguration().getDefaultKeyGenerateStrategy(); if (null != keyGenerateStrategy && !Strings.isNullOrEmpty(keyGenerateStrategy.getKeyGeneratorName())) { result.add(keyGenerateStrategy.getKeyGeneratorName()); } @@ -93,8 +96,8 @@ public boolean hasAnyOneToBeDropped(final DropShardingKeyGeneratorStatement sqlS } @Override - public Class getRuleConfigurationClass() { - return ShardingRuleConfiguration.class; + public Class getRuleClass() { + return ShardingRule.class; } @Override diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableReferenceExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableReferenceExecutor.java index dbfc3afbbb96a..6bcf79ad861d3 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableReferenceExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableReferenceExecutor.java @@ -26,6 +26,7 @@ import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableReferenceRuleConfiguration; import org.apache.shardingsphere.sharding.distsql.statement.DropShardingTableReferenceRuleStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import java.util.Collection; import java.util.stream.Collectors; @@ -35,30 +36,32 @@ */ @DistSQLExecutorCurrentRuleRequired("Sharding") @Setter -public final class DropShardingTableReferenceExecutor implements DatabaseRuleDropExecutor { +public final class DropShardingTableReferenceExecutor implements DatabaseRuleDropExecutor { private ShardingSphereDatabase database; + private ShardingRule rule; + @Override - public void checkBeforeUpdate(final DropShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropShardingTableReferenceRuleStatement sqlStatement) { if (!sqlStatement.isIfExists()) { - checkCurrentRuleConfiguration(currentRuleConfig); - checkToBeDroppedShardingTableReferenceRules(sqlStatement, currentRuleConfig); + checkCurrentRuleConfiguration(); + checkToBeDroppedShardingTableReferenceRules(sqlStatement); } } - private void checkCurrentRuleConfiguration(final ShardingRuleConfiguration currentRuleConfig) { - ShardingSpherePreconditions.checkState(!currentRuleConfig.getBindingTableGroups().isEmpty(), () -> new MissingRequiredRuleException("Sharding table reference", database.getName())); + private void checkCurrentRuleConfiguration() { + ShardingSpherePreconditions.checkState(!rule.getConfiguration().getBindingTableGroups().isEmpty(), () -> new MissingRequiredRuleException("Sharding table reference", database.getName())); } - private void checkToBeDroppedShardingTableReferenceRules(final DropShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection currentRuleNames = getCurrentShardingTableReferenceRuleNames(currentRuleConfig); + private void checkToBeDroppedShardingTableReferenceRules(final DropShardingTableReferenceRuleStatement sqlStatement) { + Collection currentRuleNames = getCurrentShardingTableReferenceRuleNames(); Collection notExistedRuleNames = sqlStatement.getNames().stream().filter(each -> !containsIgnoreCase(currentRuleNames, each)).collect(Collectors.toList()); ShardingSpherePreconditions.checkState(notExistedRuleNames.isEmpty(), () -> new MissingRequiredRuleException("Sharding table reference", database.getName(), notExistedRuleNames)); } - private Collection getCurrentShardingTableReferenceRuleNames(final ShardingRuleConfiguration currentRuleConfig) { - return currentRuleConfig.getBindingTableGroups().stream().map(ShardingTableReferenceRuleConfiguration::getName).collect(Collectors.toList()); + private Collection getCurrentShardingTableReferenceRuleNames() { + return rule.getConfiguration().getBindingTableGroups().stream().map(ShardingTableReferenceRuleConfiguration::getName).collect(Collectors.toList()); } private boolean containsIgnoreCase(final Collection ruleNames, final String name) { @@ -82,12 +85,12 @@ public boolean updateCurrentRuleConfiguration(final DropShardingTableReferenceRu @Override public boolean hasAnyOneToBeDropped(final DropShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - return isExistRuleConfig(currentRuleConfig) && !getIdenticalData(getCurrentShardingTableReferenceRuleNames(currentRuleConfig), sqlStatement.getNames()).isEmpty(); + return isExistRuleConfig(currentRuleConfig) && !getIdenticalData(getCurrentShardingTableReferenceRuleNames(), sqlStatement.getNames()).isEmpty(); } @Override - public Class getRuleConfigurationClass() { - return ShardingRuleConfiguration.class; + public Class getRuleClass() { + return ShardingRule.class; } @Override diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleExecutor.java index 219b308875f2a..19935a11cd382 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleExecutor.java @@ -30,6 +30,7 @@ import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.statement.DropShardingTableRuleStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import java.util.Collection; import java.util.LinkedHashSet; @@ -42,20 +43,22 @@ */ @DistSQLExecutorCurrentRuleRequired("Sharding") @Setter -public final class DropShardingTableRuleExecutor implements DatabaseRuleDropExecutor { +public final class DropShardingTableRuleExecutor implements DatabaseRuleDropExecutor { private ShardingSphereDatabase database; + private ShardingRule rule; + @Override - public void checkBeforeUpdate(final DropShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropShardingTableRuleStatement sqlStatement) { if (!sqlStatement.isIfExists()) { - checkToBeDroppedShardingTableNames(sqlStatement, currentRuleConfig); + checkToBeDroppedShardingTableNames(sqlStatement); } - checkBindingTables(sqlStatement, currentRuleConfig); + checkBindingTables(sqlStatement); } - private void checkToBeDroppedShardingTableNames(final DropShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection currentShardingTableNames = getCurrentShardingTableNames(currentRuleConfig); + private void checkToBeDroppedShardingTableNames(final DropShardingTableRuleStatement sqlStatement) { + Collection currentShardingTableNames = getCurrentShardingTableNames(); Collection notExistedTableNames = getToBeDroppedShardingTableNames(sqlStatement).stream().filter(each -> !containsIgnoreCase(currentShardingTableNames, each)).collect(Collectors.toList()); ShardingSpherePreconditions.checkState(notExistedTableNames.isEmpty(), () -> new MissingRequiredRuleException("sharding", database.getName(), notExistedTableNames)); @@ -69,24 +72,24 @@ private boolean containsIgnoreCase(final Collection collection, final St return collection.stream().anyMatch(each -> each.equalsIgnoreCase(str)); } - private Collection getCurrentShardingTableNames(final ShardingRuleConfiguration shardingRuleConfig) { + private Collection getCurrentShardingTableNames() { Collection result = new LinkedList<>(); - result.addAll(shardingRuleConfig.getTables().stream().map(ShardingTableRuleConfiguration::getLogicTable).collect(Collectors.toList())); - result.addAll(shardingRuleConfig.getAutoTables().stream().map(ShardingAutoTableRuleConfiguration::getLogicTable).collect(Collectors.toList())); + result.addAll(rule.getConfiguration().getTables().stream().map(ShardingTableRuleConfiguration::getLogicTable).collect(Collectors.toList())); + result.addAll(rule.getConfiguration().getAutoTables().stream().map(ShardingAutoTableRuleConfiguration::getLogicTable).collect(Collectors.toList())); return result; } - private void checkBindingTables(final DropShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection bindingTables = getBindingTables(currentRuleConfig); + private void checkBindingTables(final DropShardingTableRuleStatement sqlStatement) { + Collection bindingTables = getBindingTables(); Collection usedTableNames = getToBeDroppedShardingTableNames(sqlStatement).stream().filter(each -> containsIgnoreCase(bindingTables, each)).collect(Collectors.toList()); if (!usedTableNames.isEmpty()) { throw new RuleInUsedException("Sharding", database.getName(), usedTableNames, "sharding table reference"); } } - private Collection getBindingTables(final ShardingRuleConfiguration shardingRuleConfig) { + private Collection getBindingTables() { Collection result = new LinkedHashSet<>(); - shardingRuleConfig.getBindingTableGroups().forEach(each -> result.addAll(Splitter.on(",").splitToList(each.getReference()))); + rule.getConfiguration().getBindingTableGroups().forEach(each -> result.addAll(Splitter.on(",").splitToList(each.getReference()))); return result; } @@ -162,8 +165,8 @@ private Collection findUnusedAuditors(final ShardingRuleConfiguration cu } @Override - public Class getRuleConfigurationClass() { - return ShardingRuleConfiguration.class; + public Class getRuleClass() { + return ShardingRule.class; } @Override diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterDefaultShardingStrategyExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterDefaultShardingStrategyExecutorTest.java index 33b10009102b9..b92b6a1c11279 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterDefaultShardingStrategyExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterDefaultShardingStrategyExecutorTest.java @@ -26,6 +26,7 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.handler.update.AlterDefaultShardingStrategyExecutor; import org.apache.shardingsphere.sharding.distsql.statement.AlterDefaultShardingStrategyStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; import org.junit.jupiter.api.BeforeEach; @@ -53,37 +54,49 @@ void setUp() { @Test void assertExecuteWithInvalidStrategyType() { + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(new ShardingRuleConfiguration()); + executor.setRule(rule); assertThrows(InvalidAlgorithmConfigurationException.class, - () -> executor.checkBeforeUpdate(new AlterDefaultShardingStrategyStatement("TABLE", "invalidType", null, null), new ShardingRuleConfiguration())); + () -> executor.checkBeforeUpdate(new AlterDefaultShardingStrategyStatement("TABLE", "invalidType", null, null))); } @Test void assertExecuteWithNotExist() { - AlterDefaultShardingStrategyStatement statement = new AlterDefaultShardingStrategyStatement("TABLE", "standard", "order_id", null); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("not_exist_algorithm", null); - assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(statement, currentRuleConfig)); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + AlterDefaultShardingStrategyStatement sqlStatement = new AlterDefaultShardingStrategyStatement("TABLE", "standard", "order_id", null); + assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test void assertExecuteWithUnmatchedStrategy() { - AlterDefaultShardingStrategyStatement statement = new AlterDefaultShardingStrategyStatement("TABLE", "standard", "order_id,user_id", null); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null); - assertThrows(InvalidAlgorithmConfigurationException.class, () -> executor.checkBeforeUpdate(statement, currentRuleConfig)); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + AlterDefaultShardingStrategyStatement sqlStatement = new AlterDefaultShardingStrategyStatement("TABLE", "standard", "order_id,user_id", null); + assertThrows(InvalidAlgorithmConfigurationException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test void assertAlterDefaultTableShardingStrategy() { - AlgorithmSegment algorithm = new AlgorithmSegment("order_id_algorithm", new Properties()); - AlterDefaultShardingStrategyStatement statement = new AlterDefaultShardingStrategyStatement("TABLE", "standard", "order_id", algorithm); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null); - executor.checkBeforeUpdate(statement, currentRuleConfig); - ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(statement, currentRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + AlgorithmSegment algorithm = new AlgorithmSegment("order_id_algorithm", new Properties()); + AlterDefaultShardingStrategyStatement sqlStatement = new AlterDefaultShardingStrategyStatement("TABLE", "standard", "order_id", algorithm); + executor.checkBeforeUpdate(sqlStatement); + ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig); StandardShardingStrategyConfiguration defaultTableShardingStrategy = (StandardShardingStrategyConfiguration) currentRuleConfig.getDefaultTableShardingStrategy(); assertThat(defaultTableShardingStrategy.getShardingAlgorithmName(), is("default_table_order_id_algorithm")); @@ -92,11 +105,14 @@ void assertAlterDefaultTableShardingStrategy() { @Test void assertAlterDefaultDatabaseShardingStrategy() { - AlgorithmSegment databaseAlgorithmSegment = new AlgorithmSegment("inline", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}"))); - AlterDefaultShardingStrategyStatement statement = new AlterDefaultShardingStrategyStatement("DATABASE", "standard", "user_id", databaseAlgorithmSegment); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); - executor.checkBeforeUpdate(statement, currentRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + AlgorithmSegment databaseAlgorithmSegment = new AlgorithmSegment("inline", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}"))); + AlterDefaultShardingStrategyStatement statement = new AlterDefaultShardingStrategyStatement("DATABASE", "standard", "user_id", databaseAlgorithmSegment); + executor.checkBeforeUpdate(statement); ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(statement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig); StandardShardingStrategyConfiguration defaultDatabaseShardingStrategy = (StandardShardingStrategyConfiguration) currentRuleConfig.getDefaultDatabaseShardingStrategy(); @@ -106,11 +122,14 @@ void assertAlterDefaultDatabaseShardingStrategy() { @Test void assertAlterDefaultTableShardingStrategyWithNoneShardingStrategyType() { - AlterDefaultShardingStrategyStatement sqlStatement = new AlterDefaultShardingStrategyStatement("TABLE", "none", null, null); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null); - executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + AlterDefaultShardingStrategyStatement sqlStatement = new AlterDefaultShardingStrategyStatement("TABLE", "none", null, null); + executor.checkBeforeUpdate(sqlStatement); ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig); NoneShardingStrategyConfiguration defaultTableShardingStrategy = (NoneShardingStrategyConfiguration) currentRuleConfig.getDefaultTableShardingStrategy(); @@ -120,10 +139,13 @@ void assertAlterDefaultTableShardingStrategyWithNoneShardingStrategyType() { @Test void assertAlterDefaultDatabaseShardingStrategyWithNoneShardingStrategyType() { - AlterDefaultShardingStrategyStatement sqlStatement = new AlterDefaultShardingStrategyStatement("DATABASE", "none", null, null); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); - executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + AlterDefaultShardingStrategyStatement sqlStatement = new AlterDefaultShardingStrategyStatement("DATABASE", "none", null, null); + executor.checkBeforeUpdate(sqlStatement); ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig); NoneShardingStrategyConfiguration defaultDatabaseShardingStrategy = (NoneShardingStrategyConfiguration) currentRuleConfig.getDefaultDatabaseShardingStrategy(); diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableReferenceRuleExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableReferenceRuleExecutorTest.java index 85092cf91db4d..7729ac9cb45e1 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableReferenceRuleExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableReferenceRuleExecutorTest.java @@ -25,6 +25,7 @@ import org.apache.shardingsphere.sharding.distsql.handler.update.AlterShardingTableReferenceRuleExecutor; import org.apache.shardingsphere.sharding.distsql.segment.table.TableReferenceRuleSegment; import org.apache.shardingsphere.sharding.distsql.statement.AlterShardingTableReferenceRuleStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -35,6 +36,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class AlterShardingTableReferenceRuleExecutorTest { @@ -47,12 +49,18 @@ void setUp() { @Test void assertCheckBeforeUpdateWithNotExistedRule() { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("notExisted", "t_1,t_2"), createCurrentRuleConfiguration())); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration()); + executor.setRule(rule); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("notExisted", "t_1,t_2"))); } @Test void assertCheckWithNotExistedTables() { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("reference_0", "t_3,t_4"), createCurrentRuleConfiguration())); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration()); + executor.setRule(rule); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("reference_0", "t_3,t_4"))); } @Test diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableRuleExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableRuleExecutorTest.java index 3cb536ebabc7d..4a972381a865e 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableRuleExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableRuleExecutorTest.java @@ -33,6 +33,7 @@ import org.apache.shardingsphere.sharding.distsql.segment.table.AutoTableRuleSegment; import org.apache.shardingsphere.sharding.distsql.segment.table.TableRuleSegment; import org.apache.shardingsphere.sharding.distsql.statement.AlterShardingTableRuleStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; @@ -73,7 +74,10 @@ void setUp() { @Test void assertUpdate() { AlterShardingTableRuleStatement sqlStatement = new AlterShardingTableRuleStatement(Arrays.asList(createCompleteAutoTableRule("t_order_item"), createCompleteTableRule("t_order"))); - executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig); assertThat(currentRuleConfig.getTables().size(), is(1)); @@ -98,7 +102,10 @@ void assertUpdate() { @Test void assertUpdateWithDifferentCase() { AlterShardingTableRuleStatement sqlStatement = new AlterShardingTableRuleStatement(Arrays.asList(createCompleteAutoTableRule("T_ORDER_ITEM"), createCompleteTableRule("T_ORDER"))); - executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig); assertThat(currentRuleConfig.getTables().size(), is(1)); @@ -123,7 +130,10 @@ void assertUpdateWithDifferentCase() { @Test void assertUpdateTableType() { AlterShardingTableRuleStatement sqlStatement = new AlterShardingTableRuleStatement(Arrays.asList(createCompleteAutoTableRule("t_order"), createCompleteTableRule("t_order_item"))); - executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig); assertThat(currentRuleConfig.getTables().size(), is(1)); diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateDefaultShardingStrategyExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateDefaultShardingStrategyExecutorTest.java index 998272ca51d9b..be73400c111d1 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateDefaultShardingStrategyExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateDefaultShardingStrategyExecutorTest.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.handler.update.CreateDefaultShardingStrategyExecutor; import org.apache.shardingsphere.sharding.distsql.statement.CreateDefaultShardingStrategyStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; import org.junit.jupiter.api.BeforeEach; @@ -55,43 +56,55 @@ void setUp() { @Test void assertExecuteWithInvalidStrategyType() { CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "invalidType", null, null); - assertThrows(InvalidAlgorithmConfigurationException.class, () -> executor.checkBeforeUpdate(statement, new ShardingRuleConfiguration())); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(new ShardingRuleConfiguration()); + executor.setRule(rule); + assertThrows(InvalidAlgorithmConfigurationException.class, () -> executor.checkBeforeUpdate(statement)); } @Test void assertExecuteWithoutAlgorithm() { CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "standard", "order_id", null); - assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(statement, null)); + assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(statement)); } @Test void assertExecuteWithExist() { - AlgorithmSegment algorithm = new AlgorithmSegment("order_id_algorithm", new Properties()); - CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "standard", "order_id", algorithm); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null); - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(statement, currentRuleConfig)); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + AlgorithmSegment algorithm = new AlgorithmSegment("order_id_algorithm", new Properties()); + CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "standard", "order_id", algorithm); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(statement)); } @Test void assertExecuteWithUnmatchedStrategy() { - CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "standard", "order_id,user_id", null); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null); - assertThrows(InvalidAlgorithmConfigurationException.class, () -> executor.checkBeforeUpdate(statement, currentRuleConfig)); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "standard", "order_id,user_id", null); + assertThrows(InvalidAlgorithmConfigurationException.class, () -> executor.checkBeforeUpdate(statement)); } @Test void assertCreateDefaultTableShardingStrategy() { - AlgorithmSegment algorithm = new AlgorithmSegment("order_id_algorithm", new Properties()); - CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "standard", "order_id", algorithm); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null); - executor.checkBeforeUpdate(statement, currentRuleConfig); - ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(statement, currentRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + AlgorithmSegment algorithm = new AlgorithmSegment("order_id_algorithm", new Properties()); + CreateDefaultShardingStrategyStatement sqlStatement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "standard", "order_id", algorithm); + executor.checkBeforeUpdate(sqlStatement); + ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); StandardShardingStrategyConfiguration defaultTableShardingStrategy = (StandardShardingStrategyConfiguration) currentRuleConfig.getDefaultTableShardingStrategy(); assertThat(defaultTableShardingStrategy.getShardingAlgorithmName(), is("default_table_order_id_algorithm")); @@ -103,7 +116,10 @@ void assertCreateDefaultDatabaseShardingStrategy() { AlgorithmSegment databaseAlgorithmSegment = new AlgorithmSegment("inline", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}"))); CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "DATABASE", "standard", "user_id", databaseAlgorithmSegment); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); - executor.checkBeforeUpdate(statement, currentRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(statement); ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(statement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); StandardShardingStrategyConfiguration defaultDatabaseShardingStrategy = (StandardShardingStrategyConfiguration) currentRuleConfig.getDefaultDatabaseShardingStrategy(); @@ -113,17 +129,20 @@ void assertCreateDefaultDatabaseShardingStrategy() { @Test void assertCreateDefaultTableShardingStrategyWithIfNotExists() { - AlgorithmSegment algorithm = new AlgorithmSegment("order_id_algorithm", new Properties()); - CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "standard", "order_id", algorithm); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null); - executor.checkBeforeUpdate(statement, currentRuleConfig); - ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(statement, currentRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + AlgorithmSegment algorithm = new AlgorithmSegment("order_id_algorithm", new Properties()); + CreateDefaultShardingStrategyStatement sqlStatement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "standard", "order_id", algorithm); + executor.checkBeforeUpdate(sqlStatement); + ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); algorithm = new AlgorithmSegment("user_id_algorithm", new Properties()); CreateDefaultShardingStrategyStatement statementWithIfNotExists = new CreateDefaultShardingStrategyStatement(true, "TABLE", "standard", "order_id", algorithm); - executor.checkBeforeUpdate(statementWithIfNotExists, currentRuleConfig); + executor.checkBeforeUpdate(statementWithIfNotExists); toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(statementWithIfNotExists, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); StandardShardingStrategyConfiguration defaultTableShardingStrategy = (StandardShardingStrategyConfiguration) currentRuleConfig.getDefaultTableShardingStrategy(); @@ -136,12 +155,15 @@ void assertCreateDefaultDatabaseShardingStrategyWithIfNotExists() { AlgorithmSegment databaseAlgorithmSegment = new AlgorithmSegment("inline", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}"))); CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "DATABASE", "standard", "user_id", databaseAlgorithmSegment); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); - executor.checkBeforeUpdate(statement, currentRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(statement); ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(statement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); databaseAlgorithmSegment = new AlgorithmSegment("inline", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${order_id % 2}"))); CreateDefaultShardingStrategyStatement statementWithIfNotExists = new CreateDefaultShardingStrategyStatement(true, "TABLE", "standard", "order_id", databaseAlgorithmSegment); - executor.checkBeforeUpdate(statementWithIfNotExists, currentRuleConfig); + executor.checkBeforeUpdate(statementWithIfNotExists); toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(statementWithIfNotExists, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); StandardShardingStrategyConfiguration defaultDatabaseShardingStrategy = (StandardShardingStrategyConfiguration) currentRuleConfig.getDefaultDatabaseShardingStrategy(); @@ -151,11 +173,14 @@ void assertCreateDefaultDatabaseShardingStrategyWithIfNotExists() { @Test void assertCreateDefaultTableShardingStrategyWithNoneShardingStrategyType() { - CreateDefaultShardingStrategyStatement sqlStatement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "none", null, null); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null); - executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + CreateDefaultShardingStrategyStatement sqlStatement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "none", null, null); + executor.checkBeforeUpdate(sqlStatement); ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); NoneShardingStrategyConfiguration defaultTableShardingStrategy = (NoneShardingStrategyConfiguration) currentRuleConfig.getDefaultTableShardingStrategy(); @@ -167,7 +192,10 @@ void assertCreateDefaultTableShardingStrategyWithNoneShardingStrategyType() { void assertCreateDefaultDatabaseShardingStrategyWithNoneShardingStrategyType() { CreateDefaultShardingStrategyStatement sqlStatement = new CreateDefaultShardingStrategyStatement(false, "DATABASE", "none", null, null); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); - executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); NoneShardingStrategyConfiguration defaultDatabaseShardingStrategy = (NoneShardingStrategyConfiguration) currentRuleConfig.getDefaultDatabaseShardingStrategy(); diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableReferenceRuleExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableReferenceRuleExecutorTest.java index 814813426804c..218b4b8095cc0 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableReferenceRuleExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableReferenceRuleExecutorTest.java @@ -26,6 +26,7 @@ import org.apache.shardingsphere.sharding.distsql.handler.update.CreateShardingTableReferenceRuleExecutor; import org.apache.shardingsphere.sharding.distsql.segment.table.TableReferenceRuleSegment; import org.apache.shardingsphere.sharding.distsql.statement.CreateShardingTableReferenceRuleStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -38,6 +39,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class CreateShardingTableReferenceRuleExecutorTest { @@ -50,7 +52,10 @@ void setUp() { @Test void assertCheckSQLStatementWithoutCurrentTableRule() { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "foo", "t_order,t_order_item"), new ShardingRuleConfiguration())); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(new ShardingRuleConfiguration()); + executor.setRule(rule); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "foo", "t_order,t_order_item"))); } private CreateShardingTableReferenceRuleStatement createSQLStatement(final boolean ifNotExists, final String name, final String reference) { @@ -61,14 +66,20 @@ private CreateShardingTableReferenceRuleStatement createSQLStatement(final boole @Test void assertCheckSQLStatementWithDuplicateTables() { - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "foo", "t_order,t_order_item"), getCurrentRuleConfig())); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(getCurrentRuleConfig()); + executor.setRule(rule); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "foo", "t_order,t_order_item"))); } @Test void assertUpdateWithIfNotExists() { CreateShardingTableReferenceRuleStatement sqlStatement = createSQLStatement(true, "foo", "t_order,t_order_item"); ShardingRuleConfiguration currentRuleConfig = getCurrentRuleConfig(); - executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); Collection referenceRuleConfigs = currentRuleConfig.getBindingTableGroups(); diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableRuleExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableRuleExecutorTest.java index ed09157c8a864..6472fa0711d77 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableRuleExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableRuleExecutorTest.java @@ -41,6 +41,7 @@ import org.apache.shardingsphere.sharding.distsql.segment.table.TableRuleSegment; import org.apache.shardingsphere.sharding.distsql.statement.CreateShardingTableRuleStatement; import org.apache.shardingsphere.sharding.exception.strategy.InvalidShardingStrategyConfigurationException; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor; import org.apache.shardingsphere.sql.parser.core.ParseASTNode; import org.apache.shardingsphere.sql.parser.core.SQLParserFactory; @@ -86,9 +87,12 @@ void setUp() { @Test void assertUpdate() { - CreateShardingTableRuleStatement statement = new CreateShardingTableRuleStatement(false, Arrays.asList(createCompleteAutoTableRule(), createCompleteTableRule())); - executor.checkBeforeUpdate(statement, currentRuleConfig); - ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(statement, currentRuleConfig); + CreateShardingTableRuleStatement sqlStatement = new CreateShardingTableRuleStatement(false, Arrays.asList(createCompleteAutoTableRule(), createCompleteTableRule())); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); + ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); assertThat(currentRuleConfig.getTables().size(), is(2)); Iterator tableRuleIterator = currentRuleConfig.getTables().iterator(); @@ -132,7 +136,7 @@ void assertCheckCreateShardingStatement() { + "TYPE(NAME='hash_mod',PROPERTIES('sharding-count'='6'))," + "KEY_GENERATE_STRATEGY(COLUMN=order_id,TYPE(NAME='snowflake')))"; CreateShardingTableRuleStatement distSQLStatement = (CreateShardingTableRuleStatement) getDistSQLStatement(sql); - executor.checkBeforeUpdate(distSQLStatement, null); + executor.checkBeforeUpdate(distSQLStatement); } @Test @@ -143,7 +147,7 @@ void assertCheckCreateShardingStatementThrows() { + "TYPE(NAME='inline',PROPERTIES('algorithm-expression'='t_order_item_${order_id % 4}'))," + "KEY_GENERATE_STRATEGY(COLUMN=order_id,TYPE(NAME='snowflake')))"; CreateShardingTableRuleStatement distSQLStatement = (CreateShardingTableRuleStatement) getDistSQLStatement(sql); - assertThrows(DistSQLException.class, () -> executor.checkBeforeUpdate(distSQLStatement, null)); + assertThrows(DistSQLException.class, () -> executor.checkBeforeUpdate(distSQLStatement)); } @Test @@ -154,7 +158,7 @@ void assertCheckCreateShardingStatementWithNoneDatabaseStrategy() { + "TABLE_STRATEGY(TYPE='standard',SHARDING_COLUMN=order_id,SHARDING_ALGORITHM(TYPE(NAME='inline',PROPERTIES('algorithm-expression'='t_order_${order_id % 2}'))))" + ");"; CreateShardingTableRuleStatement distSQLStatement = (CreateShardingTableRuleStatement) getDistSQLStatement(sql); - executor.checkBeforeUpdate(distSQLStatement, null); + executor.checkBeforeUpdate(distSQLStatement); } @Test @@ -165,7 +169,7 @@ void assertCheckCreateShardingStatementWithNoneDatabaseStrategyThrows() { + "TABLE_STRATEGY(TYPE='standard',SHARDING_COLUMN=order_id,SHARDING_ALGORITHM(TYPE(NAME='inline',PROPERTIES('algorithm-expression'='t_order_${order_id % 2}'))))" + ");"; CreateShardingTableRuleStatement distSQLStatement = (CreateShardingTableRuleStatement) getDistSQLStatement(sql); - assertThrows(InvalidShardingStrategyConfigurationException.class, () -> executor.checkBeforeUpdate(distSQLStatement, null)); + assertThrows(InvalidShardingStrategyConfigurationException.class, () -> executor.checkBeforeUpdate(distSQLStatement)); } @Test @@ -176,7 +180,7 @@ void assertCheckCreateShardingStatementWithNoneTableStrategy() { + "TABLE_STRATEGY(TYPE='NONE')" + ");"; CreateShardingTableRuleStatement distSQLStatement = (CreateShardingTableRuleStatement) getDistSQLStatement(sql); - executor.checkBeforeUpdate(distSQLStatement, null); + executor.checkBeforeUpdate(distSQLStatement); } @Test @@ -187,7 +191,7 @@ void assertCheckCreateShardingStatementWithNoneTableStrategyThrows() { + "TABLE_STRATEGY(TYPE='NONE')" + ");"; CreateShardingTableRuleStatement distSQLStatement = (CreateShardingTableRuleStatement) getDistSQLStatement(sql); - assertThrows(InvalidShardingStrategyConfigurationException.class, () -> executor.checkBeforeUpdate(distSQLStatement, null)); + assertThrows(InvalidShardingStrategyConfigurationException.class, () -> executor.checkBeforeUpdate(distSQLStatement)); } @Test @@ -196,7 +200,10 @@ void assertUpdateWithIfNotExistsStatement() { segments.add(createCompleteAutoTableRule()); segments.add(createCompleteTableRule()); CreateShardingTableRuleStatement statementWithIfNotExists = new CreateShardingTableRuleStatement(true, segments); - executor.checkBeforeUpdate(statementWithIfNotExists, currentRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(statementWithIfNotExists); executor.updateCurrentRuleConfiguration(currentRuleConfig, executor.buildToBeCreatedRuleConfiguration(statementWithIfNotExists, currentRuleConfig)); assertThat(currentRuleConfig.getTables().size(), is(2)); Iterator tableRuleIterator = currentRuleConfig.getTables().iterator(); diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropDefaultShardingStrategyExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropDefaultShardingStrategyExecutorTest.java index 6691b7baa7306..bf5ba0816722e 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropDefaultShardingStrategyExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropDefaultShardingStrategyExecutorTest.java @@ -24,6 +24,7 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.handler.update.DropDefaultShardingStrategyExecutor; import org.apache.shardingsphere.sharding.distsql.statement.DropDefaultShardingStrategyStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -39,6 +40,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class DropDefaultShardingStrategyExecutorTest { @@ -51,13 +53,20 @@ void setUp() { @Test void assertCheckSQLStatementWithoutExistedAlgorithm() { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("table"), new ShardingRuleConfiguration())); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(new ShardingRuleConfiguration()); + executor.setRule(rule); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("table"))); } @Test void assertCheckSQLStatementWithIfExists() { - executor.checkBeforeUpdate(new DropDefaultShardingStrategyStatement(true, "table"), new ShardingRuleConfiguration()); - executor.checkBeforeUpdate(new DropDefaultShardingStrategyStatement(true, "table"), null); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(new ShardingRuleConfiguration()); + executor.setRule(rule); + executor.checkBeforeUpdate(new DropDefaultShardingStrategyStatement(true, "table")); + executor.setRule(null); + executor.checkBeforeUpdate(new DropDefaultShardingStrategyStatement(true, "table")); } @Test diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAlgorithmExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAlgorithmExecutorTest.java index 7eb2e6e7b704f..2ff46eb4c5f3c 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAlgorithmExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAlgorithmExecutorTest.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.handler.update.DropShardingAlgorithmExecutor; import org.apache.shardingsphere.sharding.distsql.statement.DropShardingAlgorithmStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -42,6 +43,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class DropShardingAlgorithmExecutorTest { @@ -55,22 +57,34 @@ void setUp() { @Test void assertCheckSQLStatementWithoutExistedAlgorithm() throws RuleDefinitionViolationException { - assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(createSQLStatement("t_order"), new ShardingRuleConfiguration())); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(new ShardingRuleConfiguration()); + executor.setRule(rule); + assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(createSQLStatement("t_order"))); } @Test void assertCheckSQLStatementWithoutExistedAlgorithmWithIfExists() throws RuleDefinitionViolationException { - executor.checkBeforeUpdate(createSQLStatementWithIfExists("t_order"), new ShardingRuleConfiguration()); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(new ShardingRuleConfiguration()); + executor.setRule(rule); + executor.checkBeforeUpdate(createSQLStatementWithIfExists("t_order")); } @Test void assertCheckSQLStatementWithBindingTableRule() throws RuleDefinitionViolationException { - assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(createSQLStatement("t_order_tb_inline"), createCurrentRuleConfiguration())); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration()); + executor.setRule(rule); + assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(createSQLStatement("t_order_tb_inline"))); } @Test void assertCheckSQLStatementWithBindingTableRuleWithIfExists() throws RuleDefinitionViolationException { - assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(createSQLStatementWithIfExists("t_order_tb_inline"), createCurrentRuleConfiguration())); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration()); + executor.setRule(rule); + assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(createSQLStatementWithIfExists("t_order_tb_inline"))); } @Test @@ -79,6 +93,9 @@ void assertUpdateCurrentRuleConfiguration() { ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration(); assertThat(currentRuleConfig.getShardingAlgorithms().size(), is(3)); assertTrue(currentRuleConfig.getShardingAlgorithms().containsKey(toBeDroppedAlgorithmName)); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); executor.updateCurrentRuleConfiguration(createSQLStatement(toBeDroppedAlgorithmName), currentRuleConfig); assertThat(currentRuleConfig.getShardingAlgorithms().size(), is(2)); assertFalse(currentRuleConfig.getShardingAlgorithms().containsKey(toBeDroppedAlgorithmName)); @@ -91,6 +108,9 @@ void assertUpdateCurrentRuleConfigurationWithIfExists() { ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration(); assertThat(currentRuleConfig.getShardingAlgorithms().size(), is(3)); assertTrue(currentRuleConfig.getShardingAlgorithms().containsKey(toBeDroppedAlgorithmName)); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); executor.updateCurrentRuleConfiguration(createSQLStatementWithIfExists(toBeDroppedAlgorithmName), currentRuleConfig); assertThat(currentRuleConfig.getShardingAlgorithms().size(), is(2)); assertFalse(currentRuleConfig.getShardingAlgorithms().containsKey(toBeDroppedAlgorithmName)); diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAuditorExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAuditorExecutorTest.java index 0d63a83534b29..9ecf39487cb6e 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAuditorExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAuditorExecutorTest.java @@ -26,6 +26,7 @@ import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.handler.update.DropShardingAuditorExecutor; import org.apache.shardingsphere.sharding.distsql.statement.DropShardingAuditorStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -52,13 +53,19 @@ void setUp() { @Test void assertExecuteWithNotExist() { - assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(createSQLStatement("sharding_key_required_auditor"), new ShardingRuleConfiguration())); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(new ShardingRuleConfiguration()); + executor.setRule(rule); + assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(createSQLStatement("sharding_key_required_auditor"))); } @Test void assertExecuteWithNotExistWithIfExists() { + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(new ShardingRuleConfiguration()); + executor.setRule(rule); DropShardingAuditorStatement sqlStatement = new DropShardingAuditorStatement(true, Collections.singleton("sharding_key_required_auditor")); - executor.checkBeforeUpdate(sqlStatement, new ShardingRuleConfiguration()); + executor.checkBeforeUpdate(sqlStatement); } @Test @@ -74,7 +81,10 @@ void assertExecuteWithUsed() { ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", null)); currentRuleConfig.getAutoTables().add(createShardingAutoTableRuleConfiguration()); - assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(createSQLStatement("sharding_key_required_auditor"), currentRuleConfig)); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(createSQLStatement("sharding_key_required_auditor"))); } private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() { diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingKeyGeneratorExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingKeyGeneratorExecutorTest.java index 22bc574b87efc..10f52d5c9d496 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingKeyGeneratorExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingKeyGeneratorExecutorTest.java @@ -26,6 +26,7 @@ import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.handler.update.DropShardingKeyGeneratorExecutor; import org.apache.shardingsphere.sharding.distsql.statement.DropShardingKeyGeneratorStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -36,6 +37,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class DropShardingKeyGeneratorExecutorTest { @@ -49,14 +51,19 @@ void setUp() { @Test void assertExecuteWithNotExist() { DropShardingKeyGeneratorStatement sqlStatement = new DropShardingKeyGeneratorStatement(false, Collections.singleton("uuid_key_generator")); - assertThrows(MissingRequiredAlgorithmException.class, - () -> executor.checkBeforeUpdate(sqlStatement, new ShardingRuleConfiguration())); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(new ShardingRuleConfiguration()); + executor.setRule(rule); + assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test void assertExecuteWithNotExistWithIfExists() { DropShardingKeyGeneratorStatement sqlStatement = new DropShardingKeyGeneratorStatement(true, Collections.singleton("uuid_key_generator")); - executor.checkBeforeUpdate(sqlStatement, new ShardingRuleConfiguration()); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(new ShardingRuleConfiguration()); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); } @Test @@ -70,11 +77,14 @@ void assertDropSpecifiedKeyGenerator() { @Test void assertExecuteWithUsed() { - DropShardingKeyGeneratorStatement sqlStatement = new DropShardingKeyGeneratorStatement(false, Collections.singleton("uuid_key_generator")); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.getKeyGenerators().put("uuid_key_generator", new AlgorithmConfiguration("UUID", null)); currentRuleConfig.getAutoTables().add(createShardingAutoTableRuleConfiguration()); - assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentRuleConfig)); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + DropShardingKeyGeneratorStatement sqlStatement = new DropShardingKeyGeneratorStatement(false, Collections.singleton("uuid_key_generator")); + assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() { diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableReferenceExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableReferenceExecutorTest.java index de355304a8723..88ff376b611bf 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableReferenceExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableReferenceExecutorTest.java @@ -25,6 +25,7 @@ import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; import org.apache.shardingsphere.sharding.distsql.handler.update.DropShardingTableReferenceExecutor; import org.apache.shardingsphere.sharding.distsql.statement.DropShardingTableReferenceRuleStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -38,6 +39,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class DropShardingTableReferenceExecutorTest { @@ -50,23 +52,31 @@ void setUp() { @Test void assertCheckWithNotExistedShardingTableReferenceRule() { - assertThrows(MissingRequiredRuleException.class, - () -> executor.checkBeforeUpdate(new DropShardingTableReferenceRuleStatement(false, Collections.singleton("notExisted")), new ShardingRuleConfiguration())); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(new ShardingRuleConfiguration()); + executor.setRule(rule); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(new DropShardingTableReferenceRuleStatement(false, Collections.singleton("notExisted")))); } @Test void assertCheckWithIfExists() { DropShardingTableReferenceRuleStatement statement = new DropShardingTableReferenceRuleStatement(true, Collections.singleton("notExisted")); - executor.checkBeforeUpdate(statement, null); + executor.checkBeforeUpdate(statement); ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); shardingRuleConfig.setBindingTableGroups(Collections.singleton(new ShardingTableReferenceRuleConfiguration("foo", "t_3,t_4"))); - executor.checkBeforeUpdate(statement, shardingRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(shardingRuleConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(statement); } @Test void assertHasAnyOneToBeDropped() { ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration(); DropShardingTableReferenceRuleStatement sqlStatement = new DropShardingTableReferenceRuleStatement(true, Arrays.asList("reference_0", "reference_1")); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); assertTrue(executor.hasAnyOneToBeDropped(sqlStatement, currentRuleConfig)); } @@ -82,6 +92,9 @@ private ShardingRuleConfiguration createCurrentRuleConfiguration() { void assertHasNotAnyOneToBeDropped() { ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration(); DropShardingTableReferenceRuleStatement sqlStatement = new DropShardingTableReferenceRuleStatement(false, Collections.singleton("foo")); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); assertFalse(executor.hasAnyOneToBeDropped(sqlStatement, currentRuleConfig)); } @@ -90,7 +103,10 @@ void assertDropSpecifiedReferenceRuleConfiguration() { ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration(); currentRuleConfig.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("reference_1", "t_1,t_2")); DropShardingTableReferenceRuleStatement sqlStatement = new DropShardingTableReferenceRuleStatement(false, Collections.singleton("reference_1")); - executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); executor.updateCurrentRuleConfiguration(sqlStatement, currentRuleConfig); assertThat(currentRuleConfig.getBindingTableGroups().size(), is(1)); assertThat(currentRuleConfig.getBindingTableGroups().iterator().next().getReference(), is("t_order,t_order_item")); @@ -101,7 +117,10 @@ void assertDropMultipleReferenceRules() { ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration(); currentRuleConfig.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("reference_1", "t_1,t_2,t_3")); DropShardingTableReferenceRuleStatement sqlStatement = new DropShardingTableReferenceRuleStatement(false, Arrays.asList("reference_0", "reference_1")); - executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(currentRuleConfig); + executor.setRule(rule); + executor.checkBeforeUpdate(sqlStatement); executor.updateCurrentRuleConfiguration(sqlStatement, currentRuleConfig); assertTrue(currentRuleConfig.getBindingTableGroups().isEmpty()); } diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleExecutorTest.java index aaa55c452b06e..7c3e44275b16d 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleExecutorTest.java @@ -31,6 +31,7 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.handler.update.DropShardingTableRuleExecutor; import org.apache.shardingsphere.sharding.distsql.statement.DropShardingTableRuleStatement; +import org.apache.shardingsphere.sharding.rule.ShardingRule; import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment; import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue; import org.junit.jupiter.api.BeforeEach; @@ -49,6 +50,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class DropShardingTableRuleExecutorTest { @@ -61,18 +63,27 @@ void setUp() { @Test void assertCheckSQLStatementWithoutExistedTableRule() throws RuleDefinitionViolationException { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("t_order"), new ShardingRuleConfiguration())); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(new ShardingRuleConfiguration()); + executor.setRule(rule); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("t_order"))); } @Test void assertCheckSQLStatementIfExists() throws RuleDefinitionViolationException { DropShardingTableRuleStatement statement = new DropShardingTableRuleStatement(true, Collections.singleton(new TableNameSegment(0, 3, new IdentifierValue("t_order_if_exists")))); - executor.checkBeforeUpdate(statement, new ShardingRuleConfiguration()); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(new ShardingRuleConfiguration()); + executor.setRule(rule); + executor.checkBeforeUpdate(statement); } @Test void assertCheckSQLStatementWithBindingTableRule() throws RuleDefinitionViolationException { - assertThrows(RuleInUsedException.class, () -> executor.checkBeforeUpdate(createSQLStatement("t_order_item"), createCurrentRuleConfiguration())); + ShardingRule rule = mock(ShardingRule.class); + when(rule.getConfiguration()).thenReturn(createCurrentRuleConfiguration()); + executor.setRule(rule); + assertThrows(RuleInUsedException.class, () -> executor.checkBeforeUpdate(createSQLStatement("t_order_item"))); } @Test diff --git a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/database/DatabaseRuleDefinitionExecuteEngine.java b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/database/DatabaseRuleDefinitionExecuteEngine.java index 137f8defbadfa..8577da3e0653d 100644 --- a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/database/DatabaseRuleDefinitionExecuteEngine.java +++ b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/database/DatabaseRuleDefinitionExecuteEngine.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.mode.manager.ContextManager; import java.util.Optional; @@ -51,31 +52,28 @@ public final class DatabaseRuleDefinitionExecuteEngine { */ @SuppressWarnings("unchecked") public void executeUpdate() { - Class ruleConfigClass = executor.getRuleConfigurationClass(); - RuleConfiguration currentRuleConfig = findCurrentRuleConfiguration(database, ruleConfigClass).orElse(null); executor.setDatabase(database); - checkBeforeUpdate(currentRuleConfig); + Optional rule = database.getRuleMetaData().findSingleRule(executor.getRuleClass()); + executor.setRule(rule.orElse(null)); + checkBeforeUpdate(rule.orElse(null)); + RuleConfiguration currentRuleConfig = rule.map(ShardingSphereRule::getConfiguration).orElse(null); if (getRefreshStatus(currentRuleConfig)) { contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService() .switchActiveVersion(DatabaseRuleOperatorFactory.newInstance(contextManager, executor).operate(sqlStatement, database, currentRuleConfig)); } } - private Optional findCurrentRuleConfiguration(final ShardingSphereDatabase database, final Class ruleConfigClass) { - return database.getRuleMetaData().getConfigurations().stream().filter(each -> ruleConfigClass.isAssignableFrom(each.getClass())).findFirst(); - } - @SuppressWarnings("unchecked") - private void checkBeforeUpdate(final RuleConfiguration currentRuleConfig) { - Optional.ofNullable(executor.getClass().getAnnotation(DistSQLExecutorCurrentRuleRequired.class)).ifPresent(optional -> checkCurrentRule(currentRuleConfig, optional)); - executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); + private void checkBeforeUpdate(final ShardingSphereRule rule) { + Optional.ofNullable(executor.getClass().getAnnotation(DistSQLExecutorCurrentRuleRequired.class)).ifPresent(optional -> checkCurrentRule(rule, optional)); + executor.checkBeforeUpdate(sqlStatement); } - private void checkCurrentRule(final RuleConfiguration currentRuleConfig, final DistSQLExecutorCurrentRuleRequired currentRuleRequired) { + private void checkCurrentRule(final ShardingSphereRule rule, final DistSQLExecutorCurrentRuleRequired currentRuleRequired) { if (sqlStatement instanceof DropRuleStatement && ((DropRuleStatement) sqlStatement).isIfExists()) { return; } - ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException(currentRuleRequired.value(), database.getName())); + ShardingSpherePreconditions.checkNotNull(rule, () -> new MissingRequiredRuleException(currentRuleRequired.value(), database.getName())); } @SuppressWarnings({"rawtypes", "unchecked"}) diff --git a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/database/DatabaseRuleAlterExecutor.java b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/database/DatabaseRuleAlterExecutor.java index a902b41f3eca7..c8a87cb9c4b91 100644 --- a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/database/DatabaseRuleAlterExecutor.java +++ b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/database/DatabaseRuleAlterExecutor.java @@ -18,15 +18,17 @@ package org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; +import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; /** * Database rule alter executor. * * @param type of SQL statement - * @param type of rule configuration + * @param type of rule + * @param type of rule configuration */ -public interface DatabaseRuleAlterExecutor extends DatabaseRuleDefinitionExecutor { +public interface DatabaseRuleAlterExecutor extends DatabaseRuleDefinitionExecutor { /** * Build to be altered rule configuration. @@ -35,7 +37,7 @@ public interface DatabaseRuleAlterExecutor type of SQL statement - * @param type of rule configuration + * @param type of rule + * @param type of rule configuration */ -public interface DatabaseRuleCreateExecutor extends DatabaseRuleDefinitionExecutor { +public interface DatabaseRuleCreateExecutor extends DatabaseRuleDefinitionExecutor { /** * Build to be created rule configuration. @@ -35,7 +37,7 @@ public interface DatabaseRuleCreateExecutor type of SQL statement - * @param type of rule configuration + * @param type of rule */ @SingletonSPI -public interface DatabaseRuleDefinitionExecutor extends TypedSPI, DistSQLExecutorDatabaseAware { +public interface DatabaseRuleDefinitionExecutor extends TypedSPI, DistSQLExecutorDatabaseAware, DistSQLExecutorRuleAware { /** * Check before update. * * @param sqlStatement SQL statement - * @param currentRuleConfig current rule configuration */ - void checkBeforeUpdate(T sqlStatement, R currentRuleConfig); - - /** - * Get rule configuration class. - * - * @return rule configuration class - */ - Class getRuleConfigurationClass(); + void checkBeforeUpdate(T sqlStatement); @Override Class getType(); diff --git a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/database/DatabaseRuleDropExecutor.java b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/database/DatabaseRuleDropExecutor.java index aa8263edf1f77..269e04547aebb 100644 --- a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/database/DatabaseRuleDropExecutor.java +++ b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/database/DatabaseRuleDropExecutor.java @@ -19,6 +19,7 @@ import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; import java.util.Collection; @@ -26,11 +27,12 @@ /** * Database rule drop executor. - * + * * @param type of SQL statement - * @param type of rule configuration + * @param type of rule + * @param type of rule configuration */ -public interface DatabaseRuleDropExecutor extends DatabaseRuleDefinitionExecutor { +public interface DatabaseRuleDropExecutor extends DatabaseRuleDefinitionExecutor { /** * TODO Remove temporary default implementation @@ -40,7 +42,7 @@ public interface DatabaseRuleDropExecutor { +public final class LoadSingleTableExecutor implements DatabaseRuleCreateExecutor { private ShardingSphereDatabase database; @Override - public void checkBeforeUpdate(final LoadSingleTableStatement sqlStatement, final SingleRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final LoadSingleTableStatement sqlStatement) { String defaultSchemaName = new DatabaseTypeRegistry(database.getProtocolType()).getDefaultSchemaName(database.getName()); checkDuplicatedTables(sqlStatement, defaultSchemaName); checkStorageUnits(sqlStatement); @@ -162,8 +163,12 @@ public void updateCurrentRuleConfiguration(final SingleRuleConfiguration current } @Override - public Class getRuleConfigurationClass() { - return SingleRuleConfiguration.class; + public void setRule(final SingleRule rule) { + } + + @Override + public Class getRuleClass() { + return SingleRule.class; } @Override diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutor.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutor.java index 8dce0603066f7..aa22f8e56e868 100644 --- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutor.java +++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutor.java @@ -25,6 +25,7 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration; import org.apache.shardingsphere.single.distsql.statement.rdl.SetDefaultSingleTableStorageUnitStatement; +import org.apache.shardingsphere.single.rule.SingleRule; import java.util.Collection; import java.util.Collections; @@ -33,12 +34,12 @@ * Set default single table storage unit executor. */ @Setter -public final class SetDefaultSingleTableStorageUnitExecutor implements DatabaseRuleCreateExecutor { +public final class SetDefaultSingleTableStorageUnitExecutor implements DatabaseRuleCreateExecutor { private ShardingSphereDatabase database; @Override - public void checkBeforeUpdate(final SetDefaultSingleTableStorageUnitStatement sqlStatement, final SingleRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final SetDefaultSingleTableStorageUnitStatement sqlStatement) { checkStorageUnitExist(sqlStatement); } @@ -64,8 +65,12 @@ public void updateCurrentRuleConfiguration(final SingleRuleConfiguration current } @Override - public Class getRuleConfigurationClass() { - return SingleRuleConfiguration.class; + public void setRule(final SingleRule rule) { + } + + @Override + public Class getRuleClass() { + return SingleRule.class; } @Override diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnloadSingleTableExecutor.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnloadSingleTableExecutor.java index 170ab2143c48e..8ec4131c233cc 100644 --- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnloadSingleTableExecutor.java +++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnloadSingleTableExecutor.java @@ -43,16 +43,18 @@ */ @DistSQLExecutorCurrentRuleRequired("Single") @Setter -public final class UnloadSingleTableExecutor implements DatabaseRuleAlterExecutor { +public final class UnloadSingleTableExecutor implements DatabaseRuleAlterExecutor { private ShardingSphereDatabase database; + private SingleRule rule; + @Override - public void checkBeforeUpdate(final UnloadSingleTableStatement sqlStatement, final SingleRuleConfiguration currentRuleConfig) { - checkTables(sqlStatement, currentRuleConfig); + public void checkBeforeUpdate(final UnloadSingleTableStatement sqlStatement) { + checkTables(sqlStatement); } - private void checkTables(final UnloadSingleTableStatement sqlStatement, final SingleRuleConfiguration currentRuleConfig) { + private void checkTables(final UnloadSingleTableStatement sqlStatement) { if (sqlStatement.isUnloadAllTables()) { return; } @@ -62,7 +64,7 @@ private void checkTables(final UnloadSingleTableStatement sqlStatement, final Si for (String each : sqlStatement.getTables()) { checkTableExist(allTables, each); checkIsSingleTable(singleTables, each); - checkTableRuleExist(database.getName(), database.getProtocolType(), currentRuleConfig, singleRule.getDataNodesByTableName(each), each); + checkTableRuleExist(database.getName(), database.getProtocolType(), singleRule.getDataNodesByTableName(each), each); } } @@ -79,11 +81,11 @@ private void checkIsSingleTable(final Collection singleTables, final Str ShardingSpherePreconditions.checkState(singleTables.contains(tableName), () -> new SingleTableNotFoundException(tableName)); } - private void checkTableRuleExist(final String databaseName, final DatabaseType databaseType, final SingleRuleConfiguration currentRuleConfig, + private void checkTableRuleExist(final String databaseName, final DatabaseType databaseType, final Collection dataNodes, final String tableName) { ShardingSpherePreconditions.checkState(!dataNodes.isEmpty(), () -> new MissingRequiredRuleException("Single", databaseName, tableName)); DataNode dataNode = dataNodes.iterator().next(); - ShardingSpherePreconditions.checkState(currentRuleConfig.getTables().contains(dataNode.format(databaseType)), () -> new MissingRequiredRuleException("Single", databaseName, tableName)); + ShardingSpherePreconditions.checkState(rule.getConfiguration().getTables().contains(dataNode.format(databaseType)), () -> new MissingRequiredRuleException("Single", databaseName, tableName)); } @Override @@ -109,8 +111,8 @@ private String extractTableName(final String tableNode) { } @Override - public Class getRuleConfigurationClass() { - return SingleRuleConfiguration.class; + public Class getRuleClass() { + return SingleRule.class; } @Override diff --git a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutorTest.java b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutorTest.java index 044aabe02fbf2..1a9cfc41b17f1 100644 --- a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutorTest.java +++ b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutorTest.java @@ -69,7 +69,7 @@ void assertCheckWithDuplicatedTables() { when(schema.containsTable("foo")).thenReturn(true); LoadSingleTableStatement sqlStatement = new LoadSingleTableStatement(Collections.singletonList(new SingleTableSegment("ds_0", null, "foo"))); executor.setDatabase(database); - assertThrows(TableExistsException.class, () -> executor.checkBeforeUpdate(sqlStatement, mock(SingleRuleConfiguration.class))); + assertThrows(TableExistsException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test @@ -77,7 +77,7 @@ void assertCheckWithInvalidStorageUnit() { LoadSingleTableStatement sqlStatement = new LoadSingleTableStatement(Collections.singletonList(new SingleTableSegment("ds_0", null, "foo"))); when(database.getName()).thenReturn("foo_db"); executor.setDatabase(database); - assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(sqlStatement, mock(SingleRuleConfiguration.class))); + assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(sqlStatement)); } @Test diff --git a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutorTest.java b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutorTest.java index 877adb13eb731..c9688867c2bb6 100644 --- a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutorTest.java +++ b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutorTest.java @@ -41,8 +41,7 @@ class SetDefaultSingleTableStorageUnitExecutorTest { @Test void assertCheckWithInvalidResource() { executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); - assertThrows(MissingRequiredStorageUnitsException.class, - () -> executor.checkBeforeUpdate(new SetDefaultSingleTableStorageUnitStatement("bar_ds"), mock(SingleRuleConfiguration.class))); + assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(new SetDefaultSingleTableStorageUnitStatement("bar_ds"))); } @Test diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/legacy/LegacyDatabaseRuleDefinitionExecuteEngine.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/legacy/LegacyDatabaseRuleDefinitionExecuteEngine.java index 6f8d692e43b77..3afe442877918 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/legacy/LegacyDatabaseRuleDefinitionExecuteEngine.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/legacy/LegacyDatabaseRuleDefinitionExecuteEngine.java @@ -31,6 +31,7 @@ import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; @@ -63,31 +64,27 @@ public final class LegacyDatabaseRuleDefinitionExecuteEngine { */ @SuppressWarnings("unchecked") public void executeUpdate() { - Class ruleConfigClass = executor.getRuleConfigurationClass(); - RuleConfiguration currentRuleConfig = findCurrentRuleConfiguration(database, ruleConfigClass).orElse(null); executor.setDatabase(database); - checkBeforeUpdate(currentRuleConfig); + Optional rule = database.getRuleMetaData().findSingleRule(executor.getRuleClass()); + executor.setRule(rule.orElse(null)); + checkBeforeUpdate(rule.orElse(null)); + RuleConfiguration currentRuleConfig = rule.map(ShardingSphereRule::getConfiguration).orElse(null); if (getRefreshStatus(sqlStatement, currentRuleConfig, executor)) { - contextManager.getInstanceContext().getModeContextManager() - .alterRuleConfiguration(database.getName(), processSQLStatement(database, sqlStatement, executor, currentRuleConfig)); + contextManager.getInstanceContext().getModeContextManager().alterRuleConfiguration(database.getName(), processSQLStatement(database, sqlStatement, executor, currentRuleConfig)); } } - private Optional findCurrentRuleConfiguration(final ShardingSphereDatabase database, final Class ruleConfigClass) { - return database.getRuleMetaData().getConfigurations().stream().filter(each -> ruleConfigClass.isAssignableFrom(each.getClass())).findFirst(); - } - @SuppressWarnings("unchecked") - private void checkBeforeUpdate(final RuleConfiguration currentRuleConfig) { - Optional.ofNullable(executor.getClass().getAnnotation(DistSQLExecutorCurrentRuleRequired.class)).ifPresent(optional -> checkCurrentRule(currentRuleConfig, optional)); - executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); + private void checkBeforeUpdate(final ShardingSphereRule rule) { + Optional.ofNullable(executor.getClass().getAnnotation(DistSQLExecutorCurrentRuleRequired.class)).ifPresent(optional -> checkCurrentRule(rule, optional)); + executor.checkBeforeUpdate(sqlStatement); } - private void checkCurrentRule(final RuleConfiguration currentRuleConfig, final DistSQLExecutorCurrentRuleRequired currentRuleRequired) { + private void checkCurrentRule(final ShardingSphereRule rule, final DistSQLExecutorCurrentRuleRequired currentRuleRequired) { if (sqlStatement instanceof DropRuleStatement && ((DropRuleStatement) sqlStatement).isIfExists()) { return; } - ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException(currentRuleRequired.value(), database.getName())); + ShardingSpherePreconditions.checkNotNull(rule, () -> new MissingRequiredRuleException(currentRuleRequired.value(), database.getName())); } @SuppressWarnings({"unchecked", "rawtypes"}) diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/FixtureDatabaseRuleCreateExecutor.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/FixtureDatabaseRuleCreateExecutor.java index f25c8915a9561..eb26e5e2e6d5e 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/FixtureDatabaseRuleCreateExecutor.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/FixtureDatabaseRuleCreateExecutor.java @@ -20,10 +20,10 @@ import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleCreateExecutor; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -public final class FixtureDatabaseRuleCreateExecutor implements DatabaseRuleCreateExecutor { +public final class FixtureDatabaseRuleCreateExecutor implements DatabaseRuleCreateExecutor { @Override - public void checkBeforeUpdate(final CreateFixtureRuleStatement sqlStatement, final FixtureRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final CreateFixtureRuleStatement sqlStatement) { } @Override @@ -36,8 +36,12 @@ public void updateCurrentRuleConfiguration(final FixtureRuleConfiguration curren } @Override - public Class getRuleConfigurationClass() { - return FixtureRuleConfiguration.class; + public void setRule(final FixtureRule rule) { + } + + @Override + public Class getRuleClass() { + return FixtureRule.class; } @Override diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/FixtureRule.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/FixtureRule.java new file mode 100644 index 0000000000000..3705ea03e740e --- /dev/null +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/FixtureRule.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.proxy.backend.handler.distsql.fixture; + +import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; +import org.apache.shardingsphere.infra.rule.ShardingSphereRule; + +public final class FixtureRule implements ShardingSphereRule { + + @Override + public RuleConfiguration getConfiguration() { + return new FixtureRuleConfiguration(); + } +}