-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
DataNodeRouter.java
105 lines (92 loc) · 4.56 KB
/
DataNodeRouter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* 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.infra.route;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteResult;
import org.apache.shardingsphere.infra.route.decorator.RouteDecorator;
import org.apache.shardingsphere.infra.route.decorator.UnconfiguredSchemaRouteDecorator;
import org.apache.shardingsphere.infra.route.hook.SPIRoutingHook;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.order.OrderedSPIRegistry;
import org.apache.shardingsphere.sql.parser.binder.SQLStatementContextFactory;
import org.apache.shardingsphere.sql.parser.binder.statement.CommonSQLStatementContext;
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* Data node router.
*/
public final class DataNodeRouter {
static {
ShardingSphereServiceLoader.register(RouteDecorator.class);
}
private final ShardingSphereMetaData metaData;
private final ConfigurationProperties props;
@SuppressWarnings("rawtypes")
private final Map<ShardingSphereRule, RouteDecorator> decorators;
private final SPIRoutingHook routingHook;
public DataNodeRouter(final ShardingSphereMetaData metaData, final ConfigurationProperties props, final Collection<ShardingSphereRule> rules) {
this.metaData = metaData;
this.props = props;
decorators = OrderedSPIRegistry.getRegisteredServices(rules, RouteDecorator.class);
routingHook = new SPIRoutingHook();
}
/**
* Route SQL.
*
* @param sqlStatement SQL statement
* @param sql SQL
* @param parameters SQL parameters
* @return route context
*/
public RouteContext route(final SQLStatement sqlStatement, final String sql, final List<Object> parameters) {
routingHook.start(sql);
try {
RouteContext result = executeRoute(sqlStatement, parameters);
routingHook.finishSuccess(result, metaData.getRuleSchemaMetaData().getConfiguredSchemaMetaData());
return result;
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
routingHook.finishFailure(ex);
throw ex;
}
}
@SuppressWarnings({"unchecked", "rawtypes"})
private RouteContext executeRoute(final SQLStatement sqlStatement, final List<Object> parameters) {
RouteContext result = createRouteContext(sqlStatement, parameters);
for (Entry<ShardingSphereRule, RouteDecorator> entry : decorators.entrySet()) {
result = entry.getValue().decorate(result, metaData, entry.getKey(), props);
}
return new UnconfiguredSchemaRouteDecorator().decorate(result, metaData);
}
private RouteContext createRouteContext(final SQLStatement sqlStatement, final List<Object> parameters) {
try {
SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance(metaData.getRuleSchemaMetaData().getSchemaMetaData(), parameters, sqlStatement);
return new RouteContext(sqlStatementContext, parameters, new RouteResult());
// TODO should pass parameters for master-slave
} catch (final IndexOutOfBoundsException ex) {
return new RouteContext(new CommonSQLStatementContext<>(sqlStatement), parameters, new RouteResult());
}
}
}