Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support multi replica route and exeute (#5748)
* for code style * refactor ExecuteGroupDecorator * refactor ExecuteGroupEngine * Add ReplicaRouteDecorator * add ReplicaExecuteGroupDecorator * refactor ReplicaExecuteGroupDecorator * Add replica executor * adjust order * Merge branch 'master' into dev # Conflicts: # shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-route/src/main/java/org/apache/shardingsphere/masterslave/route/engine/MasterSlaveRouteDecorator.java
- Loading branch information
Showing
35 changed files
with
673 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,6 @@ public String getRuleTagName() { | |
|
||
@Override | ||
public int getOrder() { | ||
return 10; | ||
return 12; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...n/java/org/apache/shardingsphere/replica/execute/executor/ReplicaSQLExecutorCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* 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.replica.execute.executor; | ||
|
||
import org.apache.shardingsphere.infra.executor.sql.execute.jdbc.executor.SQLExecutorCallback; | ||
import org.apache.shardingsphere.infra.spi.order.OrderedSPI; | ||
import org.apache.shardingsphere.replica.rule.ReplicaRule; | ||
|
||
/** | ||
* SQL executor callback for replica. | ||
* | ||
* @param <T> class type of return value | ||
*/ | ||
public abstract class ReplicaSQLExecutorCallback<T> implements SQLExecutorCallback<T>, OrderedSPI<ReplicaRule> { | ||
|
||
@Override | ||
public final int getOrder() { | ||
return 5; | ||
} | ||
|
||
@Override | ||
public final Class<ReplicaRule> getTypeClass() { | ||
return ReplicaRule.class; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...in/java/org/apache/shardingsphere/replica/execute/group/ReplicaExecuteGroupDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.replica.execute.group; | ||
|
||
import org.apache.shardingsphere.infra.executor.kernel.InputGroup; | ||
import org.apache.shardingsphere.infra.executor.sql.StorageResourceExecuteUnit; | ||
import org.apache.shardingsphere.infra.executor.sql.group.ExecuteGroupDecorator; | ||
import org.apache.shardingsphere.replica.rule.ReplicaRule; | ||
|
||
import java.util.Collection; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Execute group decorator for replica. | ||
* | ||
* @param <T> type of input value | ||
*/ | ||
public final class ReplicaExecuteGroupDecorator<T extends StorageResourceExecuteUnit> implements ExecuteGroupDecorator<T, ReplicaRule> { | ||
|
||
@Override | ||
public Collection<InputGroup<T>> decorate(final ReplicaRule rule, final Collection<InputGroup<T>> inputGroups) { | ||
Map<String, InputGroup<T>> result = new LinkedHashMap<>(inputGroups.size(), 1); | ||
for (InputGroup<T> each : inputGroups) { | ||
T sample = each.getInputs().get(0); | ||
String dataSourceName = sample.getExecutionUnit().getDataSourceName(); | ||
Optional<String> logicDataSource = rule.findLogicDataSource(dataSourceName); | ||
if (logicDataSource.isPresent() && result.containsKey(dataSourceName)) { | ||
result.get(dataSourceName).getInputs().addAll(each.getInputs()); | ||
} else { | ||
result.put(dataSourceName, each); | ||
} | ||
} | ||
return result.values(); | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return 5; | ||
} | ||
|
||
@Override | ||
public Class<ReplicaRule> getTypeClass() { | ||
return ReplicaRule.class; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...ETA-INF/services/org.apache.shardingsphere.infra.executor.sql.group.ExecuteGroupDecorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
org.apache.shardingsphere.replica.execute.group.ReplicaExecuteGroupDecorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...e/src/main/java/org/apache/shardingsphere/replica/route/engine/ReplicaRouteDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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.replica.route.engine; | ||
|
||
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; | ||
import org.apache.shardingsphere.infra.database.DefaultSchema; | ||
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; | ||
import org.apache.shardingsphere.infra.route.context.RouteContext; | ||
import org.apache.shardingsphere.infra.route.context.RouteMapper; | ||
import org.apache.shardingsphere.infra.route.context.RouteResult; | ||
import org.apache.shardingsphere.infra.route.context.RouteUnit; | ||
import org.apache.shardingsphere.infra.route.decorator.RouteDecorator; | ||
import org.apache.shardingsphere.replica.rule.ReplicaRule; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.LinkedList; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Route decorator for replica. | ||
*/ | ||
public final class ReplicaRouteDecorator implements RouteDecorator<ReplicaRule> { | ||
|
||
@Override | ||
public RouteContext decorate(final RouteContext routeContext, final ShardingSphereMetaData metaData, final ReplicaRule replicaRule, final ConfigurationProperties properties) { | ||
if (routeContext.getRouteResult().getRouteUnits().isEmpty()) { | ||
RouteResult routeResult = new RouteResult(); | ||
for (String each : replicaRule.getSingleReplicaDataSources()) { | ||
routeResult.getRouteUnits().add(new RouteUnit(new RouteMapper(DefaultSchema.LOGIC_NAME, each), Collections.emptyList())); | ||
} | ||
return new RouteContext(routeContext.getSqlStatementContext(), Collections.emptyList(), routeResult); | ||
} | ||
Collection<RouteUnit> toBeRemoved = new LinkedList<>(); | ||
Collection<RouteUnit> toBeAdded = new LinkedList<>(); | ||
for (RouteUnit each : routeContext.getRouteResult().getRouteUnits()) { | ||
String dataSourceName = each.getDataSourceMapper().getLogicName(); | ||
Optional<Collection<String>> replicaDataSources = replicaRule.findReplicaDataSources(dataSourceName); | ||
if (!replicaDataSources.isPresent()) { | ||
continue; | ||
} | ||
toBeRemoved.add(each); | ||
for (String replicaDataSource : replicaDataSources.get()) { | ||
toBeAdded.add(new RouteUnit(new RouteMapper(dataSourceName, replicaDataSource), each.getTableMappers())); | ||
} | ||
} | ||
routeContext.getRouteResult().getRouteUnits().removeAll(toBeRemoved); | ||
routeContext.getRouteResult().getRouteUnits().addAll(toBeAdded); | ||
return routeContext; | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return 12; | ||
} | ||
|
||
@Override | ||
public Class<ReplicaRule> getTypeClass() { | ||
return ReplicaRule.class; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...esources/META-INF/services/org.apache.shardingsphere.infra.route.decorator.RouteDecorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
org.apache.shardingsphere.replica.route.engine.ReplicaRouteDecorator |
Oops, something went wrong.