Skip to content

Commit

Permalink
Refactor DistSQLExecutorAwareSetter and DistSQLExecutor (#30303)
Browse files Browse the repository at this point in the history
  • Loading branch information
RaigorJiang authored Feb 26, 2024
1 parent 13e498b commit 87e63ab
Show file tree
Hide file tree
Showing 26 changed files with 125 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext;
import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -45,13 +46,15 @@ class ShowBroadcastTableRuleExecutorTest {

@BeforeEach
void setUp() {
engine = new DistSQLQueryExecuteEngine(mock(ShowBroadcastTableRulesStatement.class), null, mockContextManager(), mock(DistSQLConnectionContext.class));
engine = new DistSQLQueryExecuteEngine(mock(ShowBroadcastTableRulesStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class));
}

private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(result.getDatabase("foo_db")).thenReturn(database);
BroadcastRule rule = mockBroadcastRule();
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(BroadcastRule.class)).thenReturn(Optional.of(rule));
when(database.getRuleMetaData().findSingleRule(BroadcastRule.class)).thenReturn(Optional.of(rule));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -50,14 +51,16 @@ class ShowEncryptRuleExecutorTest {

@BeforeEach
void setUp() {
engine = new DistSQLQueryExecuteEngine(mock(ShowEncryptRulesStatement.class), null, mockContextManager(), mock(DistSQLConnectionContext.class));
engine = new DistSQLQueryExecuteEngine(mock(ShowEncryptRulesStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class));
}

private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(result.getDatabase("foo_db")).thenReturn(database);
EncryptRule rule = mock(EncryptRule.class);
when(rule.getConfiguration()).thenReturn(getRuleConfiguration());
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(EncryptRule.class)).thenReturn(Optional.of(rule));
when(database.getRuleMetaData().findSingleRule(EncryptRule.class)).thenReturn(Optional.of(rule));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
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;
Expand Down Expand Up @@ -49,14 +50,16 @@ class ShowMaskRuleExecutorTest {

@BeforeEach
void setUp() {
engine = new DistSQLQueryExecuteEngine(mock(ShowMaskRulesStatement.class), null, mockContextManager(), mock(DistSQLConnectionContext.class));
engine = new DistSQLQueryExecuteEngine(mock(ShowMaskRulesStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class));
}

private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(result.getDatabase("foo_db")).thenReturn(database);
MaskRule rule = mock(MaskRule.class);
when(rule.getConfiguration()).thenReturn(getRuleConfiguration());
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.of(rule));
when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.of(rule));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.identifier.type.exportable.constant.ExportableConstants;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
Expand Down Expand Up @@ -51,15 +52,17 @@ class ShowReadwriteSplittingRuleExecutorTest {
private DistSQLQueryExecuteEngine engine;

private DistSQLQueryExecuteEngine setUp(final ShowReadwriteSplittingRulesStatement statement, final ReadwriteSplittingRuleConfiguration configuration) {
return new DistSQLQueryExecuteEngine(statement, null, mockContextManager(configuration), mock(DistSQLConnectionContext.class));
return new DistSQLQueryExecuteEngine(statement, "foo_db", mockContextManager(configuration), mock(DistSQLConnectionContext.class));
}

private ContextManager mockContextManager(final ReadwriteSplittingRuleConfiguration configuration) {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(result.getDatabase("foo_db")).thenReturn(database);
ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class);
when(rule.getConfiguration()).thenReturn(configuration);
when(rule.getExportData()).thenReturn(createExportedData());
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(ReadwriteSplittingRule.class)).thenReturn(Optional.of(rule));
when(database.getRuleMetaData().findSingleRule(ReadwriteSplittingRule.class)).thenReturn(Optional.of(rule));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
Expand Down Expand Up @@ -49,14 +50,16 @@ class ShowDefaultShadowAlgorithmExecutorTest {

@BeforeEach
void setUp() {
engine = new DistSQLQueryExecuteEngine(mock(ShowDefaultShadowAlgorithmStatement.class), null, mockContextManager(), mock(DistSQLConnectionContext.class));
engine = new DistSQLQueryExecuteEngine(mock(ShowDefaultShadowAlgorithmStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class));
}

private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(result.getDatabase("foo_db")).thenReturn(database);
ShadowRule rule = mock(ShadowRule.class);
when(rule.getConfiguration()).thenReturn(createRuleConfiguration());
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(ShadowRule.class)).thenReturn(Optional.of(rule));
when(database.getRuleMetaData().findSingleRule(ShadowRule.class)).thenReturn(Optional.of(rule));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
Expand Down Expand Up @@ -49,14 +50,16 @@ class ShowShadowAlgorithmsExecutorTest {

@BeforeEach
void setUp() {
engine = new DistSQLQueryExecuteEngine(mock(ShowShadowAlgorithmsStatement.class), null, mockContextManager(), mock(DistSQLConnectionContext.class));
engine = new DistSQLQueryExecuteEngine(mock(ShowShadowAlgorithmsStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class));
}

private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(result.getDatabase("foo_db")).thenReturn(database);
ShadowRule rule = mock(ShadowRule.class);
when(rule.getConfiguration()).thenReturn(createRuleConfiguration());
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(ShadowRule.class)).thenReturn(Optional.of(rule));
when(database.getRuleMetaData().findSingleRule(ShadowRule.class)).thenReturn(Optional.of(rule));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
Expand Down Expand Up @@ -49,14 +50,16 @@ class ShowShadowRuleExecutorTest {

@BeforeEach
void setUp() {
engine = new DistSQLQueryExecuteEngine(mock(ShowShadowRulesStatement.class), null, mockContextManager(), mock(DistSQLConnectionContext.class));
engine = new DistSQLQueryExecuteEngine(mock(ShowShadowRulesStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class));
}

private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(result.getDatabase("foo_db")).thenReturn(database);
ShadowRule rule = mock(ShadowRule.class);
when(rule.getConfiguration()).thenReturn(createRuleConfiguration());
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(ShadowRule.class)).thenReturn(Optional.of(rule));
when(database.getRuleMetaData().findSingleRule(ShadowRule.class)).thenReturn(Optional.of(rule));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
Expand Down Expand Up @@ -50,14 +51,16 @@ class ShowShadowTableRulesExecutorTest {

@BeforeEach
void setUp() {
engine = new DistSQLQueryExecuteEngine(mock(ShowShadowTableRulesStatement.class), null, mockContextManager(), mock(DistSQLConnectionContext.class));
engine = new DistSQLQueryExecuteEngine(mock(ShowShadowTableRulesStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class));
}

private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(result.getDatabase("foo_db")).thenReturn(database);
ShadowRule rule = mock(ShadowRule.class);
when(rule.getConfiguration()).thenReturn(createRuleConfiguration());
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(ShadowRule.class)).thenReturn(Optional.of(rule));
when(database.getRuleMetaData().findSingleRule(ShadowRule.class)).thenReturn(Optional.of(rule));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ComplexShardingStrategyConfiguration;
Expand Down Expand Up @@ -49,14 +50,16 @@ class ShowDefaultShardingStrategyExecutorTest {
private DistSQLQueryExecuteEngine engine;

DistSQLQueryExecuteEngine setUp(final ShardingRuleConfiguration configuration) {
return new DistSQLQueryExecuteEngine(mock(ShowDefaultShardingStrategyStatement.class), null, mockContextManager(configuration), mock(DistSQLConnectionContext.class));
return new DistSQLQueryExecuteEngine(mock(ShowDefaultShardingStrategyStatement.class), "foo_db", mockContextManager(configuration), mock(DistSQLConnectionContext.class));
}

private ContextManager mockContextManager(final ShardingRuleConfiguration configuration) {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(result.getDatabase("foo_db")).thenReturn(database);
ShardingRule rule = mock(ShardingRule.class);
when(rule.getConfiguration()).thenReturn(configuration);
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(ShardingRule.class)).thenReturn(Optional.of(rule));
when(database.getRuleMetaData().findSingleRule(ShardingRule.class)).thenReturn(Optional.of(rule));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingAlgorithmsStatement;
Expand All @@ -47,14 +48,16 @@ class ShowShardingAlgorithmExecutorTest {

@BeforeEach
void setUp() {
engine = new DistSQLQueryExecuteEngine(mock(ShowShardingAlgorithmsStatement.class), null, mockContextManager(), mock(DistSQLConnectionContext.class));
engine = new DistSQLQueryExecuteEngine(mock(ShowShardingAlgorithmsStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class));
}

private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(result.getDatabase("foo_db")).thenReturn(database);
ShardingRule rule = mock(ShardingRule.class);
when(rule.getConfiguration()).thenReturn(createRuleConfiguration());
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(ShardingRule.class)).thenReturn(Optional.of(rule));
when(database.getRuleMetaData().findSingleRule(ShardingRule.class)).thenReturn(Optional.of(rule));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingAuditorsStatement;
Expand All @@ -47,14 +48,16 @@ class ShowShardingAuditorsExecutorTest {

@BeforeEach
void setUp() {
engine = new DistSQLQueryExecuteEngine(mock(ShowShardingAuditorsStatement.class), null, mockContextManager(), mock(DistSQLConnectionContext.class));
engine = new DistSQLQueryExecuteEngine(mock(ShowShardingAuditorsStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class));
}

private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(result.getDatabase("foo_db")).thenReturn(database);
ShardingRule rule = mock(ShardingRule.class);
when(rule.getConfiguration()).thenReturn(createRuleConfiguration());
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(ShardingRule.class)).thenReturn(Optional.of(rule));
when(database.getRuleMetaData().findSingleRule(ShardingRule.class)).thenReturn(Optional.of(rule));
return result;
}

Expand Down
Loading

0 comments on commit 87e63ab

Please sign in to comment.