Skip to content

Commit

Permalink
Add check the storage unit is not empty when create broadcast table r…
Browse files Browse the repository at this point in the history
…ule (#29028)

* Add check the storage unit is not empty when create broadcast table rule

* Add test for CreateBroadcastTableRuleStatement

* optimize CreateBroadcastTableRuleStatementUpdaterTest

* update method name
  • Loading branch information
jiangML authored Nov 14, 2023
1 parent 92dbc4b commit ec55e98
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration;
import org.apache.shardingsphere.broadcast.distsql.statement.CreateBroadcastTableRuleStatement;
import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException;
import org.apache.shardingsphere.distsql.handler.exception.storageunit.EmptyStorageUnitException;
import org.apache.shardingsphere.distsql.handler.update.RuleDefinitionCreateUpdater;
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.LinkedHashSet;
Expand All @@ -34,6 +35,7 @@ public final class CreateBroadcastTableRuleStatementUpdater implements RuleDefin

@Override
public void checkSQLStatement(final ShardingSphereDatabase database, final CreateBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) {
ShardingSpherePreconditions.checkState(!database.getResourceMetaData().getStorageUnits().isEmpty(), () -> new EmptyStorageUnitException(database.getName()));
if (!sqlStatement.isIfNotExists()) {
checkDuplicate(sqlStatement, currentRuleConfig);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration;
import org.apache.shardingsphere.broadcast.distsql.statement.CreateBroadcastTableRuleStatement;
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;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.junit.jupiter.api.Test;

import java.util.Collections;
Expand All @@ -29,30 +31,47 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
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 CreateBroadcastTableRuleStatementUpdaterTest {

private final CreateBroadcastTableRuleStatementUpdater updater = new CreateBroadcastTableRuleStatementUpdater();

@Test
void assertCheckSQLStatementWithEmptyStorageUnit() {
BroadcastRuleConfiguration currentConfig = mock(BroadcastRuleConfiguration.class);
when(currentConfig.getTables()).thenReturn(Collections.singleton("t_address"));
CreateBroadcastTableRuleStatement statement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address"));
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getResourceMetaData().getStorageUnits()).thenReturn(Collections.emptyMap());
assertThrows(EmptyStorageUnitException.class, () -> updater.checkSQLStatement(database, statement, currentConfig));
}

@Test
void assertCheckSQLStatementWithDuplicateBroadcastRule() {
BroadcastRuleConfiguration currentConfig = mock(BroadcastRuleConfiguration.class);
when(currentConfig.getTables()).thenReturn(Collections.singleton("t_address"));
CreateBroadcastTableRuleStatement statement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address"));
assertThrows(DuplicateRuleException.class, () -> updater.checkSQLStatement(mock(ShardingSphereDatabase.class), statement, currentConfig));
assertThrows(DuplicateRuleException.class, () -> updater.checkSQLStatement(mockShardingSphereDatabase(), statement, currentConfig));
}

@Test
void assertBuildToBeCreatedRuleConfiguration() {
BroadcastRuleConfiguration currentConfig = new BroadcastRuleConfiguration(new LinkedList<>());
CreateBroadcastTableRuleStatement statement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address"));
updater.checkSQLStatement(mock(ShardingSphereDatabase.class), statement, currentConfig);
updater.checkSQLStatement(mockShardingSphereDatabase(), statement, currentConfig);
BroadcastRuleConfiguration toBeCreatedRuleConfig = updater.buildToBeCreatedRuleConfiguration(currentConfig, statement);
updater.updateCurrentRuleConfiguration(currentConfig, toBeCreatedRuleConfig);
assertThat(currentConfig.getTables().size(), is(1));
assertThat(currentConfig.getTables().iterator().next(), is("t_address"));
updater.updateCurrentRuleConfiguration(currentConfig, toBeCreatedRuleConfig);
}

private ShardingSphereDatabase mockShardingSphereDatabase() {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getResourceMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("ds_0", mock(StorageUnit.class)));
return database;
}
}

0 comments on commit ec55e98

Please sign in to comment.