Skip to content

Commit

Permalink
fix #49
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Apr 12, 2016
1 parent 4471685 commit aff0fee
Show file tree
Hide file tree
Showing 21 changed files with 523 additions and 316 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
props:
metrics.second.period: true

dataSource:
db0: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: org.h2.Driver
Expand Down Expand Up @@ -42,4 +39,7 @@ defaultDatabaseStrategy:
algorithmExpression: table_test
defaultTableStrategy:
shardingColumns: id, order_id
algorithmClassName: com.dangdang.ddframe.rdb.sharding.config.yaml.algorithm.MultiAlgorithm
algorithmClassName: com.dangdang.ddframe.rdb.sharding.config.yaml.algorithm.MultiAlgorithm

props:
metrics.enable: true
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
props:
metrics.second.period: true

tables:
config:
actualTables: t_config
Expand Down Expand Up @@ -29,4 +26,7 @@ defaultDatabaseStrategy:
algorithmExpression: table_test
defaultTableStrategy:
shardingColumns: id, order_id
algorithmClassName: com.dangdang.ddframe.rdb.sharding.config.yaml.algorithm.MultiAlgorithm
algorithmClassName: com.dangdang.ddframe.rdb.sharding.config.yaml.algorithm.MultiAlgorithm

props:
metrics.enable: true
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@

package com.dangdang.ddframe.rdb.sharding.api;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import com.dangdang.ddframe.rdb.sharding.api.config.ShardingConfiguration;
import com.dangdang.ddframe.rdb.sharding.api.config.ShardingConfigurationConstant;
import com.dangdang.ddframe.rdb.sharding.api.props.ShardingProperties;
import com.dangdang.ddframe.rdb.sharding.api.props.ShardingPropertiesConstant;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.exception.ShardingJdbcException;
import com.dangdang.ddframe.rdb.sharding.executor.ExecutorEngine;
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingConnection;
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingContext;
import com.dangdang.ddframe.rdb.sharding.jdbc.adapter.AbstractDataSourceAdapter;
import com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext;
import com.dangdang.ddframe.rdb.sharding.router.SQLRouteEngine;
import com.dangdang.ddframe.rdb.sharding.metrics.ThreadLocalObjectContainer;
import com.dangdang.ddframe.rdb.sharding.router.SQLRouteEngine;
import com.google.common.base.Preconditions;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
* 支持分片的数据源.
*
Expand All @@ -52,21 +52,22 @@ public ShardingDataSource(final ShardingRule shardingRule) {
public ShardingDataSource(final ShardingRule shardingRule, final Properties props) {
Preconditions.checkNotNull(shardingRule);
Preconditions.checkNotNull(props);
ShardingConfiguration configuration = new ShardingConfiguration(props);
initThreadLocalObjectContainer(configuration);
ShardingProperties shardingProperties = new ShardingProperties(props);
initThreadLocalObjectContainer(shardingProperties);
DatabaseType type;
try {
type = DatabaseType.valueFrom(ShardingConnection.getDatabaseMetaDataFromDataSource(shardingRule.getDataSourceRule().getDataSources()).getDatabaseProductName());
} catch (final SQLException ex) {
throw new ShardingJdbcException("Can not get database product name", ex);
}
context = new ShardingContext(shardingRule, new SQLRouteEngine(shardingRule, type), new ExecutorEngine(configuration));
context = new ShardingContext(shardingRule, new SQLRouteEngine(shardingRule, type), new ExecutorEngine(shardingProperties));
}

private void initThreadLocalObjectContainer(final ShardingConfiguration configuration) {
if (configuration.getConfig(ShardingConfigurationConstant.METRICS_ENABLE, boolean.class)) {
threadLocalObjectContainer.initItem(new MetricsContext(configuration.getConfig(ShardingConfigurationConstant.METRICS_SECOND_PERIOD, long.class),
configuration.getConfig(ShardingConfigurationConstant.METRICS_PACKAGE_NAME, String.class)));
private void initThreadLocalObjectContainer(final ShardingProperties shardingProperties) {
if (shardingProperties.getValue(ShardingPropertiesConstant.METRICS_ENABLE)) {
long metricsMillisecondPeriod = shardingProperties.getValue(ShardingPropertiesConstant.METRICS_MILLISECONDS_PERIOD);
String metricsPackageName = shardingProperties.getValue(ShardingPropertiesConstant.METRICS_PACKAGE_NAME);
threadLocalObjectContainer.initItem(new MetricsContext(metricsMillisecondPeriod, metricsPackageName));
}
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.api.props;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;
import java.util.Set;

import com.dangdang.ddframe.rdb.sharding.util.StringUtil;
import com.google.common.base.Joiner;

/**
* Sharding-JDBC的配置属性.
*
* @author gaohongtao
* @author zhangliang
*/
public final class ShardingProperties {

private final Properties props;

public ShardingProperties(final Properties props) {
this.props = props;
validate();
}

private void validate() {
Set<String> propertyNames = props.stringPropertyNames();
Collection<String> errorMessages = new ArrayList<>(propertyNames.size());
for (String each : propertyNames) {
ShardingPropertiesConstant shardingPropertiesConstant = ShardingPropertiesConstant.findByKey(each);
if (null == shardingPropertiesConstant) {
continue;
}
Class<?> type = shardingPropertiesConstant.getType();
String value = props.getProperty(each);
if (type == boolean.class && !StringUtil.isBooleanValue(value)) {
errorMessages.add(getErrorMessage(shardingPropertiesConstant, value));
continue;
}
if (type == int.class && !StringUtil.isIntValue(value)) {
errorMessages.add(getErrorMessage(shardingPropertiesConstant, value));
continue;
}
if (type == long.class && !StringUtil.isLongValue(value)) {
errorMessages.add(getErrorMessage(shardingPropertiesConstant, value));
continue;
}
}
if (!errorMessages.isEmpty()) {
throw new IllegalArgumentException(Joiner.on(" ").join(errorMessages));
}
}

private String getErrorMessage(final ShardingPropertiesConstant shardingPropertiesConstant, final String invalidValue) {
return String.format("Value '%s' of '%s' cannot convert to type '%s'.", invalidValue, shardingPropertiesConstant.getKey(), shardingPropertiesConstant.getType().getName());
}

/**
* 获取配置项属性值.
*
* @param shardingPropertiesConstant 配置项常量
* @param <T> 返回值类型
* @return 配置项属性值
*/
@SuppressWarnings("unchecked")
public <T> T getValue(final ShardingPropertiesConstant shardingPropertiesConstant) {
String result = props.getProperty(shardingPropertiesConstant.getKey(), shardingPropertiesConstant.getDefaultValue());
if (boolean.class == shardingPropertiesConstant.getType()) {
return (T) Boolean.valueOf(result);
}
if (int.class == shardingPropertiesConstant.getType()) {
return (T) Integer.valueOf(result);
}
if (long.class == shardingPropertiesConstant.getType()) {
return (T) Long.valueOf(result);
}
return (T) result;
}
}
Loading

0 comments on commit aff0fee

Please sign in to comment.