提交 9756aaba 编写于 作者: T terrymanu

decouple master-slave with PreparedQueryShardingEngine and SimpleQueryShardingEngine

上级 1921604a
......@@ -17,12 +17,14 @@
package org.apache.shardingsphere.core.shard;
import org.apache.shardingsphere.underlying.common.constant.properties.ShardingSphereProperties;
import org.apache.shardingsphere.underlying.common.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.sql.parser.SQLParseEngine;
import org.apache.shardingsphere.core.route.PreparedStatementRoutingEngine;
import org.apache.shardingsphere.core.route.ShardingRouteContext;
import org.apache.shardingsphere.core.route.router.masterslave.MasterSlaveRouteDecorator;
import org.apache.shardingsphere.core.route.router.sharding.ShardingRouter;
import org.apache.shardingsphere.core.rule.MasterSlaveRule;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.SQLParseEngine;
import org.apache.shardingsphere.underlying.common.constant.properties.ShardingSphereProperties;
import org.apache.shardingsphere.underlying.common.metadata.ShardingSphereMetaData;
import java.util.ArrayList;
import java.util.List;
......@@ -41,12 +43,14 @@ import java.util.List;
*/
public final class PreparedQueryShardingEngine extends BaseShardingEngine {
private final PreparedStatementRoutingEngine routingEngine;
private final ShardingRule shardingRule;
private final ShardingRouter shardingRouter;
public PreparedQueryShardingEngine(final String sql,
final ShardingRule shardingRule, final ShardingSphereProperties properties, final ShardingSphereMetaData metaData, final SQLParseEngine sqlParseEngine) {
public PreparedQueryShardingEngine(final ShardingRule shardingRule, final ShardingSphereProperties properties, final ShardingSphereMetaData metaData, final SQLParseEngine sqlParseEngine) {
super(shardingRule, properties, metaData);
routingEngine = new PreparedStatementRoutingEngine(sql, shardingRule, metaData, sqlParseEngine);
this.shardingRule = shardingRule;
shardingRouter = new ShardingRouter(shardingRule, metaData, sqlParseEngine);
}
@Override
......@@ -56,6 +60,10 @@ public final class PreparedQueryShardingEngine extends BaseShardingEngine {
@Override
protected ShardingRouteContext route(final String sql, final List<Object> parameters) {
return routingEngine.route(parameters);
ShardingRouteContext result = shardingRouter.route(sql, parameters, true);
for (MasterSlaveRule each : shardingRule.getMasterSlaveRules()) {
result = (ShardingRouteContext) new MasterSlaveRouteDecorator(each).decorate(result);
}
return result;
}
}
......@@ -17,12 +17,14 @@
package org.apache.shardingsphere.core.shard;
import org.apache.shardingsphere.underlying.common.constant.properties.ShardingSphereProperties;
import org.apache.shardingsphere.underlying.common.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.sql.parser.SQLParseEngine;
import org.apache.shardingsphere.core.route.ShardingRouteContext;
import org.apache.shardingsphere.core.route.StatementRoutingEngine;
import org.apache.shardingsphere.core.route.router.masterslave.MasterSlaveRouteDecorator;
import org.apache.shardingsphere.core.route.router.sharding.ShardingRouter;
import org.apache.shardingsphere.core.rule.MasterSlaveRule;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.SQLParseEngine;
import org.apache.shardingsphere.underlying.common.constant.properties.ShardingSphereProperties;
import org.apache.shardingsphere.underlying.common.metadata.ShardingSphereMetaData;
import java.util.Collections;
import java.util.List;
......@@ -41,11 +43,14 @@ import java.util.List;
*/
public final class SimpleQueryShardingEngine extends BaseShardingEngine {
private final StatementRoutingEngine routingEngine;
private final ShardingRule shardingRule;
private final ShardingRouter shardingRouter;
public SimpleQueryShardingEngine(final ShardingRule shardingRule, final ShardingSphereProperties properties, final ShardingSphereMetaData metaData, final SQLParseEngine sqlParseEngine) {
super(shardingRule, properties, metaData);
routingEngine = new StatementRoutingEngine(shardingRule, metaData, sqlParseEngine);
this.shardingRule = shardingRule;
shardingRouter = new ShardingRouter(shardingRule, metaData, sqlParseEngine);
}
@Override
......@@ -55,6 +60,10 @@ public final class SimpleQueryShardingEngine extends BaseShardingEngine {
@Override
protected ShardingRouteContext route(final String sql, final List<Object> parameters) {
return routingEngine.route(sql);
ShardingRouteContext result = shardingRouter.route(sql, Collections.emptyList(), false);
for (MasterSlaveRule each : shardingRule.getMasterSlaveRules()) {
result = (ShardingRouteContext) new MasterSlaveRouteDecorator(each).decorate(result);
}
return result;
}
}
......@@ -18,12 +18,12 @@
package org.apache.shardingsphere.core.shard;
import lombok.SneakyThrows;
import org.apache.shardingsphere.underlying.common.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.underlying.common.metadata.table.TableMetas;
import org.apache.shardingsphere.core.route.PreparedStatementRoutingEngine;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.core.route.router.sharding.ShardingRouter;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.sql.parser.SQLParseEngine;
import org.apache.shardingsphere.underlying.common.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.underlying.common.metadata.table.TableMetas;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -41,7 +41,7 @@ import static org.mockito.Mockito.when;
public final class PreparedQueryShardingEngineTest extends BaseShardingEngineTest {
@Mock
private PreparedStatementRoutingEngine routingEngine;
private ShardingRouter shardingRouter;
private PreparedQueryShardingEngine shardingEngine;
......@@ -56,25 +56,25 @@ public final class PreparedQueryShardingEngineTest extends BaseShardingEngineTes
when(shardingRule.getEncryptRule()).thenReturn(encryptRule);
ShardingSphereMetaData shardingSphereMetaData = mock(ShardingSphereMetaData.class);
when(shardingSphereMetaData.getTables()).thenReturn(mock(TableMetas.class));
shardingEngine = new PreparedQueryShardingEngine(getSql(), shardingRule, getProperties(), shardingSphereMetaData, mock(SQLParseEngine.class));
shardingEngine = new PreparedQueryShardingEngine(shardingRule, getProperties(), shardingSphereMetaData, mock(SQLParseEngine.class));
setRoutingEngine();
}
@SneakyThrows
private void setRoutingEngine() {
Field field = PreparedQueryShardingEngine.class.getDeclaredField("routingEngine");
Field field = PreparedQueryShardingEngine.class.getDeclaredField("shardingRouter");
field.setAccessible(true);
field.set(shardingEngine, routingEngine);
field.set(shardingEngine, shardingRouter);
}
protected void assertShard() {
when(routingEngine.route(getParameters())).thenReturn(createSQLRouteContext());
when(shardingRouter.route(getSql(), getParameters(), true)).thenReturn(createSQLRouteContext());
assertExecutionContext(shardingEngine.shard(getSql(), getParameters()));
}
@Test(expected = SQLException.class)
public void assertWithRouteException() {
when(routingEngine.route(getParameters())).thenThrow(SQLException.class);
when(shardingRouter.route(getSql(), getParameters(), true)).thenThrow(SQLException.class);
shardingEngine.shard(getSql(), getParameters());
}
}
......@@ -18,12 +18,12 @@
package org.apache.shardingsphere.core.shard;
import lombok.SneakyThrows;
import org.apache.shardingsphere.core.route.router.sharding.ShardingRouter;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.sql.parser.SQLParseEngine;
import org.apache.shardingsphere.underlying.common.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.underlying.common.metadata.table.TableMetas;
import org.apache.shardingsphere.sql.parser.SQLParseEngine;
import org.apache.shardingsphere.core.route.StatementRoutingEngine;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.Mock;
......@@ -39,7 +39,7 @@ import static org.mockito.Mockito.when;
public final class SimpleQueryShardingEngineTest extends BaseShardingEngineTest {
@Mock
private StatementRoutingEngine routingEngine;
private ShardingRouter shardingRouter;
private SimpleQueryShardingEngine shardingEngine;
......@@ -60,13 +60,13 @@ public final class SimpleQueryShardingEngineTest extends BaseShardingEngineTest
@SneakyThrows
private void setRoutingEngine() {
Field field = SimpleQueryShardingEngine.class.getDeclaredField("routingEngine");
Field field = SimpleQueryShardingEngine.class.getDeclaredField("shardingRouter");
field.setAccessible(true);
field.set(shardingEngine, routingEngine);
field.set(shardingEngine, shardingRouter);
}
protected void assertShard() {
when(routingEngine.route(getSql())).thenReturn(createSQLRouteContext());
when(shardingRouter.route(getSql(), Collections.emptyList(), false)).thenReturn(createSQLRouteContext());
assertExecutionContext(shardingEngine.shard(getSql(), getParameters()));
}
}
......@@ -111,7 +111,7 @@ public final class ShardingPreparedStatement extends AbstractShardingPreparedSta
this.connection = connection;
this.sql = sql;
ShardingRuntimeContext runtimeContext = connection.getRuntimeContext();
shardingEngine = new PreparedQueryShardingEngine(sql, runtimeContext.getRule(), runtimeContext.getProperties(), runtimeContext.getMetaData(), runtimeContext.getParseEngine());
shardingEngine = new PreparedQueryShardingEngine(runtimeContext.getRule(), runtimeContext.getProperties(), runtimeContext.getMetaData(), runtimeContext.getParseEngine());
preparedStatementExecutor = new PreparedStatementExecutor(resultSetType, resultSetConcurrency, resultSetHoldability, returnGeneratedKeys, connection);
batchPreparedStatementExecutor = new BatchPreparedStatementExecutor(resultSetType, resultSetConcurrency, resultSetHoldability, returnGeneratedKeys, connection);
}
......
......@@ -84,7 +84,7 @@ public final class PreparedStatementExecutorWrapper implements JDBCExecutorWrapp
private ExecutionContext doShardingRoute(final String sql) {
PreparedQueryShardingEngine shardingEngine = new PreparedQueryShardingEngine(
sql, logicSchema.getShardingRule(), ShardingProxyContext.getInstance().getProperties(), logicSchema.getMetaData(), logicSchema.getParseEngine());
logicSchema.getShardingRule(), ShardingProxyContext.getInstance().getProperties(), logicSchema.getMetaData(), logicSchema.getParseEngine());
return shardingEngine.shard(sql, parameters);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册