Skip to content

Commit

Permalink
for #85 master slave init version
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed May 27, 2016
1 parent e1bbc5c commit 9a8f069
Show file tree
Hide file tree
Showing 123 changed files with 3,261 additions and 116 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@

[基于暗示(Hint)的分片键值注册方法](http://dangdangdotcom.github.io/sharding-jdbc/post/hint_shardingvalue/)

[读写分离](http://dangdangdotcom.github.io/sharding-jdbc/post/master_slave)

[柔性事务](http://dangdangdotcom.github.io/sharding-jdbc/post/soft_transaction)

[目录结构说明](http://dangdangdotcom.github.io/sharding-jdbc/post/directory_structure)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public abstract class AbstractClosureShardingAlgorithmTest {

@Test
public void assertEqual() {
Collection<String> result = createClosureShardingAlgorithm().doSharding(Collections.singletonList("target_1"), Collections.<ShardingValue<?>>singletonList(new ShardingValue<>("target", "id", 1L)));
Collection<String> result = createClosureShardingAlgorithm().doSharding(
Collections.singletonList("target_1"), Collections.<ShardingValue<?>>singletonList(new ShardingValue<>("target", "id", 1L)));
assertThat(result.size(), is(1));
assertThat(result, hasItem("target_1"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@
<value>t_order_3</value>
</list>
</constructor-arg>
<constructor-arg index="3" ref="dataSourceRule"/>
<constructor-arg index="4">
<constructor-arg index="3">
<null />
</constructor-arg>
<constructor-arg index="5" ref="databaseShardingStrategy"/>
<constructor-arg index="6" ref="tableShardingStrategy"/>
<constructor-arg index="4" ref="dataSourceRule"/>
<constructor-arg index="5">
<null />
</constructor-arg>
<constructor-arg index="6" ref="databaseShardingStrategy"/>
<constructor-arg index="7" ref="tableShardingStrategy"/>
</bean>

<bean id="orderItemTableRule" class="com.dangdang.ddframe.rdb.sharding.api.rule.TableRule">
Expand All @@ -47,12 +50,15 @@
<value>t_order_item_3</value>
</list>
</constructor-arg>
<constructor-arg index="3" ref="dataSourceRule"/>
<constructor-arg index="4">
<constructor-arg index="3">
<null />
</constructor-arg>
<constructor-arg index="4" ref="dataSourceRule"/>
<constructor-arg index="5">
<null />
</constructor-arg>
<constructor-arg index="5" ref="databaseShardingStrategy"/>
<constructor-arg index="6" ref="tableShardingStrategy"/>
<constructor-arg index="6" ref="databaseShardingStrategy"/>
<constructor-arg index="7" ref="tableShardingStrategy"/>
</bean>

<bean id="databaseShardingStrategy" class="com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy">
Expand Down Expand Up @@ -86,6 +92,9 @@
<constructor-arg index="4">
<null />
</constructor-arg>
<constructor-arg index="5">
<null />
</constructor-arg>
</bean>

<bean id="shardingDataSource" class="com.dangdang.ddframe.rdb.sharding.api.ShardingDataSourceFactory" factory-method="createDataSource">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@
<value>t_order_3</value>
</list>
</constructor-arg>
<constructor-arg index="3" ref="dataSourceRule"/>
<constructor-arg index="4">
<constructor-arg index="3">
<null />
</constructor-arg>
<constructor-arg index="4" ref="dataSourceRule"/>
<constructor-arg index="5">
<null />
</constructor-arg>
<constructor-arg index="6">
<null />
</constructor-arg>
<constructor-arg index="7">
<null />
</constructor-arg>
</bean>

<bean id="orderItemTableRule" class="com.dangdang.ddframe.rdb.sharding.api.rule.TableRule">
Expand All @@ -50,16 +53,19 @@
<value>t_order_item_3</value>
</list>
</constructor-arg>
<constructor-arg index="3" ref="dataSourceRule"/>
<constructor-arg index="4">
<constructor-arg index="3">
<null />
</constructor-arg>
<constructor-arg index="4" ref="dataSourceRule"/>
<constructor-arg index="5">
<null />
</constructor-arg>
<constructor-arg index="6">
<null />
</constructor-arg>
<constructor-arg index="7">
<null />
</constructor-arg>
</bean>

<bean id="databaseShardingStrategy" class="com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy">
Expand Down Expand Up @@ -89,6 +95,9 @@
</constructor-arg>
<constructor-arg index="3" ref="databaseShardingStrategy"/>
<constructor-arg index="4" ref="tableShardingStrategy"/>
<constructor-arg index="5">
<null />
</constructor-arg>
</bean>

<bean id="shardingDataSource" class="com.dangdang.ddframe.rdb.sharding.api.ShardingDataSourceFactory" factory-method="createDataSource">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public DataSource getDataSource(final String name) {
*
* @return 默认数据源实例
*/
// TODO getDefaultDataSource暂时不支持读写分离
public Optional<DataSource> getDefaultDataSource() {
return Optional.fromNullable(dataSourceMap.get(defaultDataSourceName));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.rule;

import com.dangdang.ddframe.rdb.sharding.api.strategy.slave.RoundRobinSlaveLoadBalanceStrategy;
import com.dangdang.ddframe.rdb.sharding.api.strategy.slave.SlaveLoadBalanceStrategy;
import com.dangdang.ddframe.rdb.sharding.parser.result.router.SQLStatementType;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.List;

/**
* 读写分离配置规则.
*
* @author zhangliang
*/
@RequiredArgsConstructor
@Getter
public final class MasterSlaveRule {

private static final ThreadLocal<Boolean> WAS_UPDATED = new ThreadLocal<Boolean>() {

@Override
protected Boolean initialValue() {
return false;
}
};

private final String logicDataSource;

private final String masterDataSource;

private final List<String> slaveDataSources;

private final SlaveLoadBalanceStrategy slaveLoadBalanceStrategy = new RoundRobinSlaveLoadBalanceStrategy();

/**
* 获取主或从节点的数据源名称.
*
* @param sqlStatementType SQL类型
* @return 主或从节点的数据源名称
*/
public String getMasterOrSlaveDataSource(final SQLStatementType sqlStatementType) {
if (SQLStatementType.SELECT != sqlStatementType || WAS_UPDATED.get()) {
WAS_UPDATED.set(true);
return masterDataSource;
}
return slaveLoadBalanceStrategy.getDataSource(logicDataSource, slaveDataSources);
}

/**
* 判断该真实数据源是否属于本读写分离规则配置.
*
* @param actualDataSource 真实数据源名称
* @return 是否属于本读写分离规则配置
*/
public boolean within(final String actualDataSource) {
return actualDataSource.equals(masterDataSource) || slaveDataSources.contains(actualDataSource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import lombok.Builder;
import com.google.common.collect.Maps;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
Expand All @@ -38,7 +42,6 @@
* @author zhangliang
*/
@Getter
@Builder
public final class ShardingRule {

private final DataSourceRule dataSourceRule;
Expand All @@ -47,9 +50,12 @@ public final class ShardingRule {

private final Collection<BindingTableRule> bindingTableRules;

private DatabaseShardingStrategy databaseShardingStrategy;
private final DatabaseShardingStrategy databaseShardingStrategy;

private TableShardingStrategy tableShardingStrategy;
private final TableShardingStrategy tableShardingStrategy;

@Getter(AccessLevel.NONE)
private final Map<String, MasterSlaveRule> masterSlaveRuleMap;

/**
* 全属性构造器.
Expand All @@ -63,7 +69,7 @@ public final class ShardingRule {
@Deprecated
public ShardingRule(
final DataSourceRule dataSourceRule, final Collection<TableRule> tableRules, final Collection<BindingTableRule> bindingTableRules,
final DatabaseShardingStrategy databaseShardingStrategy, final TableShardingStrategy tableShardingStrategy) {
final DatabaseShardingStrategy databaseShardingStrategy, final TableShardingStrategy tableShardingStrategy, final Collection<MasterSlaveRule> masterSlaveRules) {
Preconditions.checkNotNull(dataSourceRule);
Preconditions.checkNotNull(tableRules);
this.dataSourceRule = dataSourceRule;
Expand All @@ -73,6 +79,22 @@ public ShardingRule(
Collections.<String>emptyList(), new NoneDatabaseShardingAlgorithm()) : databaseShardingStrategy;
this.tableShardingStrategy = null == tableShardingStrategy ? new TableShardingStrategy(
Collections.<String>emptyList(), new NoneTableShardingAlgorithm()) : tableShardingStrategy;
masterSlaveRuleMap = Maps.uniqueIndex(null == masterSlaveRules ? Collections.<MasterSlaveRule>emptyList() : masterSlaveRules, new Function<MasterSlaveRule, String>() {

@Override
public String apply(final MasterSlaveRule input) {
return input.getLogicDataSource();
}
});
}

/**
* 获取表规则配置对象构建器.
*
* @return 分片规则配置对象构建器
*/
public static ShardingRuleBuilder builder() {
return new ShardingRuleBuilder();
}

/**
Expand Down Expand Up @@ -203,4 +225,108 @@ public Collection<String> getAllShardingColumns() {
}
return result;
}

/**
* 根据逻辑数据源名称获取读写分离配置.
*
* @param logicDataSource 逻辑数据源名称
* @return 读写分离配置
*/
public Optional<MasterSlaveRule> findMasterSlaveRule(final String logicDataSource) {
return Optional.fromNullable(masterSlaveRuleMap.get(logicDataSource));
}

/**
* 分片规则配置对象构建器.
*/
@RequiredArgsConstructor
public static class ShardingRuleBuilder {

private DataSourceRule dataSourceRule;

private Collection<TableRule> tableRules;

private Collection<BindingTableRule> bindingTableRules;

private DatabaseShardingStrategy databaseShardingStrategy;

private TableShardingStrategy tableShardingStrategy;

private Collection<MasterSlaveRule> masterSlaveRules;

/**
* 构建数据源配置规则.
*
* @param dataSourceRule 数据源配置规则
* @return 分片规则配置对象构建器
*/
public ShardingRuleBuilder dataSourceRule(final DataSourceRule dataSourceRule) {
this.dataSourceRule = dataSourceRule;
return this;
}

/**
* 构建表配置规则.
*
* @param tableRules 表配置规则
* @return 分片规则配置对象构建器
*/
public ShardingRuleBuilder tableRules(final Collection<TableRule> tableRules) {
this.tableRules = tableRules;
return this;
}

/**
* 构建绑定表配置规则.
*
* @param bindingTableRules 绑定表配置规则
* @return 分片规则配置对象构建器
*/
public ShardingRuleBuilder bindingTableRules(final Collection<BindingTableRule> bindingTableRules) {
this.bindingTableRules = bindingTableRules;
return this;
}

/**
* 构建默认分库策略.
*
* @param databaseShardingStrategy 默认分库策略
* @return 分片规则配置对象构建器
*/
public ShardingRuleBuilder databaseShardingStrategy(final DatabaseShardingStrategy databaseShardingStrategy) {
this.databaseShardingStrategy = databaseShardingStrategy;
return this;
}

/**
* 构建数据源分片规则.
*
* @param tableShardingStrategy 默认分表策略
* @return 分片规则配置对象构建器
*/
public ShardingRuleBuilder tableShardingStrategy(final TableShardingStrategy tableShardingStrategy) {
this.tableShardingStrategy = tableShardingStrategy;
return this;
}

/**
* 构建数据源分片规则.
*
* @param masterSlaveRules 读写分离策略集合
* @return 分片规则配置对象构建器
*/
public ShardingRuleBuilder masterSlaveRules(final Collection<MasterSlaveRule> masterSlaveRules) {
this.masterSlaveRules = masterSlaveRules;
return this;
}

/**
* 构建分片规则配置对象.
*
* @return 分片规则配置对象
*/
public ShardingRule build() {
return new ShardingRule(dataSourceRule, tableRules, bindingTableRules, databaseShardingStrategy, tableShardingStrategy, masterSlaveRules);
}
}
}
Loading

0 comments on commit 9a8f069

Please sign in to comment.