未验证 提交 46be46ae 编写于 作者: J Juan Pan(Trista) 提交者: GitHub

Add SchemaContextsBuilder (#5606)

上级 fe5bce52
/*
* 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.shardingjdbc.jdbc.core.context;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.sharding.core.log.ConfigurationLogger;
import org.apache.shardingsphere.underlying.common.auth.Authentication;
import org.apache.shardingsphere.underlying.common.config.DatabaseAccessConfiguration;
import org.apache.shardingsphere.underlying.common.config.RuleConfiguration;
import org.apache.shardingsphere.underlying.common.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.underlying.common.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.underlying.common.context.SchemaContext;
import org.apache.shardingsphere.underlying.common.context.SchemaContexts;
import org.apache.shardingsphere.underlying.common.context.ShardingSphereSchema;
import org.apache.shardingsphere.underlying.common.context.runtime.CachedDatabaseMetaData;
import org.apache.shardingsphere.underlying.common.context.runtime.RuntimeContext;
import org.apache.shardingsphere.underlying.common.database.type.DatabaseType;
import org.apache.shardingsphere.underlying.common.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.underlying.common.metadata.datasource.DataSourceMetas;
import org.apache.shardingsphere.underlying.common.metadata.schema.RuleSchemaMetaData;
import org.apache.shardingsphere.underlying.common.metadata.schema.RuleSchemaMetaDataLoader;
import org.apache.shardingsphere.underlying.common.rule.ShardingSphereRule;
import org.apache.shardingsphere.underlying.common.rule.ShardingSphereRulesBuilder;
import org.apache.shardingsphere.underlying.executor.kernel.ExecutorKernel;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
@Slf4j(topic = "ShardingSphere-metadata")
public final class SchemaContextsBuilder {
private final Map<String, DataSource> dataSources;
private final DatabaseType databaseType;
private final Collection<RuleConfiguration> configurations;
private final Collection<ShardingSphereRule> rules;
private final ConfigurationProperties properties;
private final ExecutorKernel executorKernel;
private final CachedDatabaseMetaData cachedDatabaseMetaData;
private final ShardingSphereMetaData metaData;
public SchemaContextsBuilder(final Map<String, DataSource> dataSources,
final DatabaseType databaseType, final Collection<RuleConfiguration> configurations, final Properties props) throws SQLException {
this.dataSources = dataSources;
this.databaseType = databaseType;
this.configurations = configurations;
rules = ShardingSphereRulesBuilder.build(configurations, dataSources.keySet());
properties = new ConfigurationProperties(null == props ? new Properties() : props);
executorKernel = new ExecutorKernel(properties.<Integer>getValue(ConfigurationPropertyKey.EXECUTOR_SIZE));
cachedDatabaseMetaData = createCachedDatabaseMetaData(dataSources);
metaData = createMetaData(dataSources, databaseType);
log(configurations, props);
}
/**
* Build.
*
* @return SchemaContexts
*/
public SchemaContexts build() {
return new SchemaContexts(Collections.singleton(createSchemaContext()), properties, new Authentication());
}
private SchemaContext createSchemaContext() {
return new SchemaContext(new ShardingSphereSchema(databaseType, configurations, rules, dataSources, metaData),
new RuntimeContext(cachedDatabaseMetaData, executorKernel));
}
private CachedDatabaseMetaData createCachedDatabaseMetaData(final Map<String, DataSource> dataSourceMap) throws SQLException {
try (Connection connection = dataSourceMap.values().iterator().next().getConnection()) {
return new CachedDatabaseMetaData(connection.getMetaData());
}
}
private ShardingSphereMetaData createMetaData(final Map<String, DataSource> dataSourceMap, final DatabaseType databaseType) throws SQLException {
long start = System.currentTimeMillis();
DataSourceMetas dataSourceMetas = new DataSourceMetas(databaseType, getDatabaseAccessConfigurationMap(dataSourceMap));
RuleSchemaMetaData ruleSchemaMetaData = new RuleSchemaMetaDataLoader(rules).load(databaseType, dataSourceMap, properties, executorKernel.getExecutorService().getExecutorService());
ShardingSphereMetaData result = new ShardingSphereMetaData(dataSourceMetas, ruleSchemaMetaData);
log.info("Meta data load finished, cost {} milliseconds.", System.currentTimeMillis() - start);
return result;
}
private Map<String, DatabaseAccessConfiguration> getDatabaseAccessConfigurationMap(final Map<String, DataSource> dataSourceMap) throws SQLException {
Map<String, DatabaseAccessConfiguration> result = new LinkedHashMap<>(dataSourceMap.size(), 1);
for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
DataSource dataSource = entry.getValue();
try (Connection connection = dataSource.getConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
result.put(entry.getKey(), new DatabaseAccessConfiguration(metaData.getURL(), metaData.getUserName(), null));
}
}
return result;
}
private void log(final Collection<RuleConfiguration> configurations, final Properties props) {
ConfigurationLogger.log(configurations);
ConfigurationLogger.log(props);
}
}
......@@ -22,9 +22,11 @@ import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.ShardingSphereConnection;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.context.RuntimeContext;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.context.SchemaContextsBuilder;
import org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationDataSource;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.apache.shardingsphere.underlying.common.config.RuleConfiguration;
import org.apache.shardingsphere.underlying.common.context.SchemaContexts;
import org.apache.shardingsphere.underlying.common.database.type.DatabaseType;
import org.apache.shardingsphere.underlying.common.database.type.DatabaseTypes;
......@@ -50,6 +52,8 @@ public final class ShardingSphereDataSource extends AbstractUnsupportedOperation
private final RuntimeContext runtimeContext;
private final SchemaContexts schemaContexts;
@Setter
private PrintWriter logWriter = new PrintWriter(System.out);
......@@ -57,6 +61,7 @@ public final class ShardingSphereDataSource extends AbstractUnsupportedOperation
this.dataSourceMap = dataSourceMap;
databaseType = createDatabaseType();
runtimeContext = new RuntimeContext(dataSourceMap, databaseType, configurations, props);
schemaContexts = new SchemaContextsBuilder(dataSourceMap, databaseType, configurations, props).build();
}
private DatabaseType createDatabaseType() throws SQLException {
......
......@@ -17,12 +17,10 @@
package org.apache.shardingsphere.underlying.common.context;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.underlying.common.config.RuleConfiguration;
import org.apache.shardingsphere.underlying.common.database.type.DatabaseType;
import org.apache.shardingsphere.underlying.common.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.underlying.common.rule.ShardingSphereRule;
import org.apache.shardingsphere.underlying.common.rule.ShardingSphereRulesBuilder;
import javax.sql.DataSource;
import java.util.Collection;
......@@ -30,7 +28,6 @@ import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
@RequiredArgsConstructor
public final class ShardingSphereSchema {
private final DatabaseType databaseType;
......@@ -43,11 +40,11 @@ public final class ShardingSphereSchema {
private ShardingSphereMetaData metaData;
public ShardingSphereSchema(final DatabaseType databaseType, final Collection<RuleConfiguration> configurations,
public ShardingSphereSchema(final DatabaseType databaseType, final Collection<RuleConfiguration> configurations, final Collection<ShardingSphereRule> rules,
final Map<String, DataSource> dataSourceMap, final ShardingSphereMetaData shardingSphereMetaData) {
this.databaseType = databaseType;
this.configurations.addAll(configurations);
rules = ShardingSphereRulesBuilder.build(configurations, dataSourceMap.keySet());
this.rules = rules;
this.dataSources.putAll(dataSourceMap);
metaData = shardingSphereMetaData;
}
......
......@@ -17,14 +17,5 @@
package org.apache.shardingsphere.underlying.common.context.runtime;
import org.apache.shardingsphere.underlying.common.config.properties.ConfigurationProperties;
public interface RuntimeExecutorKernel {
/**
* Refresh executor kernel.
*
* @param properties properties
*/
void refresh(ConfigurationProperties properties);
}
......@@ -20,6 +20,7 @@ package org.apache.shardingsphere.underlying.executor.kernel;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.ListenableFuture;
import lombok.Getter;
import org.apache.shardingsphere.underlying.common.context.runtime.RuntimeExecutorKernel;
import org.apache.shardingsphere.underlying.common.exception.ShardingSphereException;
import org.apache.shardingsphere.underlying.executor.kernel.impl.ShardingSphereExecutorService;
......@@ -35,7 +36,7 @@ import java.util.concurrent.ExecutionException;
/**
* Executor kernel.
*/
public final class ExecutorKernel implements AutoCloseable {
public final class ExecutorKernel implements AutoCloseable, RuntimeExecutorKernel {
@Getter
private final ShardingSphereExecutorService executorService;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册