Commit 385263d9 authored by WangKai's avatar WangKai
Browse files

Merge remote-tracking branch 'remotes/upstream/dev' into dev

parents 571e73e1 658a9db5
Loading
Loading
Loading
Loading
+1 −49
Original line number Diff line number Diff line
@@ -17,12 +17,7 @@

package io.shardingjdbc.core.api.config;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import io.shardingjdbc.core.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithm;
import io.shardingjdbc.core.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithmType;
import io.shardingjdbc.core.exception.ShardingJdbcException;
import io.shardingjdbc.core.rule.MasterSlaveRule;
import lombok.Getter;
import lombok.Setter;

@@ -44,48 +39,5 @@ public class MasterSlaveRuleConfiguration {
    
    private Collection<String> slaveDataSourceNames = new LinkedList<>();
    
    private MasterSlaveLoadBalanceAlgorithmType loadBalanceAlgorithmType;
    
    private String loadBalanceAlgorithmClassName;
    
    /**
     * Build master-slave rule.
     *
     * @return sharding rule
     */
    public MasterSlaveRule build() {
        Preconditions.checkNotNull(name, "name cannot be null.");
        Preconditions.checkNotNull(masterDataSourceName, "masterDataSourceName cannot be null.");
        Preconditions.checkNotNull(slaveDataSourceNames, "slaveDataSourceNames cannot be null.");
        Preconditions.checkArgument(!slaveDataSourceNames.isEmpty(), "slaveDataSourceNames cannot be empty.");
        return new MasterSlaveRule(name, masterDataSourceName, slaveDataSourceNames, getLoadBalanceAlgorithm());
    }
    
    private MasterSlaveLoadBalanceAlgorithm getLoadBalanceAlgorithm() {
        MasterSlaveLoadBalanceAlgorithm result;
        if (null != loadBalanceAlgorithmType) {
            result = loadBalanceAlgorithmType.getAlgorithm();
        } else {
            result = Strings.isNullOrEmpty(loadBalanceAlgorithmClassName) ? null : newInstance(loadBalanceAlgorithmClassName);
        }
        return result;
    }
    
    /**
     * New instance.
     * 
     * @param masterSlaveLoadBalanceAlgorithmClassName master-slave load balance algorithm class name
     * @return master-slave load balance algorithm
     */
    public MasterSlaveLoadBalanceAlgorithm newInstance(final String masterSlaveLoadBalanceAlgorithmClassName) {
        try {
            Class<?> result = Class.forName(masterSlaveLoadBalanceAlgorithmClassName);
            if (!MasterSlaveLoadBalanceAlgorithm.class.isAssignableFrom(result)) {
                throw new ShardingJdbcException("Class %s should be implement %s", masterSlaveLoadBalanceAlgorithmClassName, MasterSlaveLoadBalanceAlgorithm.class.getName());
            }
            return (MasterSlaveLoadBalanceAlgorithm) result.newInstance();
        } catch (final ReflectiveOperationException ex) {
            throw new ShardingJdbcException("Class %s should have public privilege and no argument constructor", masterSlaveLoadBalanceAlgorithmClassName);
        }
    }
    private MasterSlaveLoadBalanceAlgorithm loadBalanceAlgorithm;
}
+12 −3
Original line number Diff line number Diff line
@@ -15,14 +15,14 @@
 * </p>
 */

package io.shardingjdbc.core.rule;
package io.shardingjdbc.core.exception;

/**
 * Sharding rule exception.
 *
 * @author zhangliang
 */
public final class ShardingRuleException extends RuntimeException {
public final class ShardingConfigurationException extends RuntimeException {
    
    private static final long serialVersionUID = -1360264079938958332L;
    
@@ -32,7 +32,16 @@ public final class ShardingRuleException extends RuntimeException {
     * @param errorMessage formatted error message
     * @param args arguments of error message
     */
    public ShardingRuleException(final String errorMessage, final Object... args) {
    public ShardingConfigurationException(final String errorMessage, final Object... args) {
        super(String.format(errorMessage, args));
    }
    
    /**
     * Constructs an exception with cause exception. 
     *
     * @param cause cause exception
     */
    public ShardingConfigurationException(final Exception cause) {
        super(cause);
    }
}
+3 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package io.shardingjdbc.core.rule;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import io.shardingjdbc.core.exception.ShardingConfigurationException;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@@ -70,14 +71,14 @@ public final class BindingTableRule {
            }
        }
        if (-1 == index) {
            throw new ShardingRuleException("Actual table [%s].[%s] is not in table config", dataSource, otherActualTable);
            throw new ShardingConfigurationException("Actual table [%s].[%s] is not in table config", dataSource, otherActualTable);
        }
        for (TableRule each : tableRules) {
            if (each.getLogicTable().equals(logicTable.toLowerCase())) {
                return each.getActualDataNodes().get(index).getTableName().toLowerCase();
            }
        }
        throw new ShardingRuleException("Cannot find binding actual table, data source: %s, logic table: %s, other actual table: %s", dataSource, logicTable, otherActualTable);
        throw new ShardingConfigurationException("Cannot find binding actual table, data source: %s, logic table: %s, other actual table: %s", dataSource, logicTable, otherActualTable);
    }
    
    Collection<String> getAllLogicTables() {
+2 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
package io.shardingjdbc.core.rule;

import com.google.common.base.Splitter;
import io.shardingjdbc.core.exception.ShardingConfigurationException;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@@ -49,7 +50,7 @@ public class DataNode {
     */
    public DataNode(final String dataNode) {
        if (!isValidDataNode(dataNode)) {
            throw new ShardingRuleException("Invalid format for actual data nodes: '%s'", dataNode);
            throw new ShardingConfigurationException("Invalid format for actual data nodes: '%s'", dataNode);
        }
        List<String> segments = Splitter.on(DELIMITER).splitToList(dataNode);
        dataSourceName = segments.get(0);
+7 −10
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ package io.shardingjdbc.core.rule;
import com.google.common.base.Preconditions;
import io.shardingjdbc.core.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithm;
import io.shardingjdbc.core.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithmType;
import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration;
import lombok.Getter;

import java.util.Collection;
@@ -38,20 +39,16 @@ public final class MasterSlaveRule {
    
    private final Collection<String> slaveDataSourceNames;
    
    private final MasterSlaveLoadBalanceAlgorithm strategy;
    private final MasterSlaveLoadBalanceAlgorithm loadBalanceAlgorithm;
    
    public MasterSlaveRule(final String name, final String masterDataSourceName, final Collection<String> slaveDataSourceNames) {
        this(name, masterDataSourceName, slaveDataSourceNames, null);
    }
    
    public MasterSlaveRule(final String name, final String masterDataSourceName, final Collection<String> slaveDataSourceNames, final MasterSlaveLoadBalanceAlgorithm strategy) {
    public MasterSlaveRule(final MasterSlaveRuleConfiguration config) {
        this.name = config.getName();
        this.masterDataSourceName = config.getMasterDataSourceName();
        this.slaveDataSourceNames = config.getSlaveDataSourceNames();
        this.loadBalanceAlgorithm = null == config.getLoadBalanceAlgorithm() ? MasterSlaveLoadBalanceAlgorithmType.getDefaultAlgorithmType().getAlgorithm() : config.getLoadBalanceAlgorithm();
        Preconditions.checkNotNull(name);
        Preconditions.checkNotNull(masterDataSourceName);
        Preconditions.checkNotNull(slaveDataSourceNames);
        Preconditions.checkState(!slaveDataSourceNames.isEmpty());
        this.name = name;
        this.masterDataSourceName = masterDataSourceName;
        this.slaveDataSourceNames = slaveDataSourceNames;
        this.strategy = null == strategy ? MasterSlaveLoadBalanceAlgorithmType.getDefaultAlgorithmType().getAlgorithm() : strategy;
    }
}
Loading