Search in sources :

Example 1 with DatabaseSet

use of com.ctrip.platform.dal.dao.configure.DatabaseSet in project dal by ctripcorp.

the class DalConnectionManager method getConnectionFromDSLocator.

private DalConnection getConnectionFromDSLocator(DalHints hints, boolean isMaster, boolean isSelect) throws SQLException {
    Connection conn;
    String allInOneKey;
    DatabaseSet dbSet = config.getDatabaseSet(logicDbName);
    String shardId = null;
    if (dbSet.isShardingSupported()) {
        DalShardingStrategy strategy = dbSet.getStrategy();
        // In case the sharding strategy indicate that master shall be used
        isMaster |= strategy.isMaster(config, logicDbName, hints);
        shardId = hints.getShardId();
        if (shardId == null)
            shardId = strategy.locateDbShard(config, logicDbName, hints);
        if (shardId == null)
            throw new DalException(ErrorCode.ShardLocated, logicDbName);
        dbSet.validate(shardId);
        allInOneKey = dbSet.getRandomRealDbName(hints, shardId, isMaster, isSelect);
    } else {
        allInOneKey = dbSet.getRandomRealDbName(hints, isMaster, isSelect);
    }
    try {
        conn = locator.getConnection(allInOneKey);
        DbMeta meta = DbMeta.createIfAbsent(allInOneKey, dbSet.getDatabaseCategory(), shardId, isMaster, conn);
        return new DalConnection(conn, meta);
    } catch (Throwable e) {
        throw new DalException(ErrorCode.CantGetConnection, e, allInOneKey);
    }
}
Also used : DatabaseSet(com.ctrip.platform.dal.dao.configure.DatabaseSet) DalShardingStrategy(com.ctrip.platform.dal.dao.strategy.DalShardingStrategy) DalException(com.ctrip.platform.dal.exceptions.DalException) Connection(java.sql.Connection)

Example 2 with DatabaseSet

use of com.ctrip.platform.dal.dao.configure.DatabaseSet in project dal by ctripcorp.

the class SmartReadRouteStrategy method locateDbShard.

@Override
public String locateDbShard(DalConfigure configure, String logicDbName, DalHints hints) {
    String shard = null;
    DalEventEnum operation = (DalEventEnum) hints.get(DalHintEnum.operation);
    DatabaseSet dbSet = configure.getDatabaseSet(logicDbName);
    if (operation == DalEventEnum.QUERY) {
        Integer lastUpdateTime = dbTimeoutMap.get(logicDbName);
        // No update from server started
        if (lastUpdateTime == null) {
        //				return dbSet.getSlaveDbs(dbSet.getAllShards().iterator().next());
        }
    } else {
    }
    //		return (Set<String>)hints.get(DalHintEnum.shards);
    return null;
}
Also used : DalEventEnum(com.ctrip.platform.dal.dao.DalEventEnum) DatabaseSet(com.ctrip.platform.dal.dao.configure.DatabaseSet)

Example 3 with DatabaseSet

use of com.ctrip.platform.dal.dao.configure.DatabaseSet in project dal by ctripcorp.

the class DalShardingHelper method shuffle.

/**
	 * Shuffle by given values like id list for DB shard
	 * @param logicDbName
	 * @param parameters
	 * @return
	 * @throws SQLException
	 */
public static Map<String, List<?>> shuffle(String logicDbName, List<?> parameters) throws SQLException {
    Map<String, List<?>> shuffled = new HashMap<>();
    DalConfigure config = DalClientFactory.getDalConfigure();
    DatabaseSet dbSet = config.getDatabaseSet(logicDbName);
    DalShardingStrategy strategy = dbSet.getStrategy();
    DalHints tmpHints = new DalHints();
    for (int i = 0; i < parameters.size(); i++) {
        Object value = parameters.get(i);
        String tmpShardId = strategy.locateDbShard(config, logicDbName, tmpHints.setShardValue(value));
        // If this can not be located
        if (tmpShardId == null)
            throw new NullPointerException("Can not locate shard id for " + value);
        dbSet.validate(tmpShardId);
        List pojosInShard = shuffled.get(tmpShardId);
        if (pojosInShard == null) {
            pojosInShard = new LinkedList();
            shuffled.put(tmpShardId, pojosInShard);
        }
        pojosInShard.add(value);
    }
    detectDistributedTransaction(shuffled.keySet());
    return shuffled;
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DatabaseSet(com.ctrip.platform.dal.dao.configure.DatabaseSet) DalShardingStrategy(com.ctrip.platform.dal.dao.strategy.DalShardingStrategy) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList)

Example 4 with DatabaseSet

use of com.ctrip.platform.dal.dao.configure.DatabaseSet in project dal by ctripcorp.

the class DalShardingHelper method locateShardId.

/**
	 * Try to locate DB shard id by hints. If can not be located, return false.
	 * @param logicDbName
	 * @param hints
	 * @return true if shard id can be located
	 * @throws SQLException
	 */
private static boolean locateShardId(String logicDbName, DalHints hints) throws SQLException {
    DalConfigure config = DalClientFactory.getDalConfigure();
    DatabaseSet dbSet = config.getDatabaseSet(logicDbName);
    String shardId = dbSet.getStrategy().locateDbShard(config, logicDbName, hints);
    if (shardId == null)
        return false;
    // Fail fast asap
    dbSet.validate(shardId);
    hints.inShard(shardId);
    return true;
}
Also used : DatabaseSet(com.ctrip.platform.dal.dao.configure.DatabaseSet) DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure)

Example 5 with DatabaseSet

use of com.ctrip.platform.dal.dao.configure.DatabaseSet in project dal by ctripcorp.

the class DalShardingHelper method shuffleByTable.

/**
	 * Shuffle by table shard id.
	 * @param logicDbName
	 * @param pojos
	 * @return
	 * @throws SQLException
	 */
public static Map<String, Map<Integer, Map<String, ?>>> shuffleByTable(String logicDbName, String tableShardId, Map<Integer, Map<String, ?>> pojos) throws SQLException {
    Map<String, Map<Integer, Map<String, ?>>> shuffled = new HashMap<>();
    DalConfigure config = DalClientFactory.getDalConfigure();
    DatabaseSet dbSet = config.getDatabaseSet(logicDbName);
    DalShardingStrategy strategy = dbSet.getStrategy();
    DalHints tmpHints = new DalHints();
    for (Integer index : pojos.keySet()) {
        Map<String, ?> fields = pojos.get(index);
        String shardId = tableShardId == null ? strategy.locateTableShard(config, logicDbName, tmpHints.setFields(fields)) : tableShardId;
        Map<Integer, Map<String, ?>> pojosInShard = shuffled.get(shardId);
        if (pojosInShard == null) {
            pojosInShard = new LinkedHashMap<>();
            shuffled.put(shardId, pojosInShard);
        }
        pojosInShard.put(index, fields);
    }
    return shuffled;
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DatabaseSet(com.ctrip.platform.dal.dao.configure.DatabaseSet) DalShardingStrategy(com.ctrip.platform.dal.dao.strategy.DalShardingStrategy) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

DatabaseSet (com.ctrip.platform.dal.dao.configure.DatabaseSet)6 DalConfigure (com.ctrip.platform.dal.dao.configure.DalConfigure)4 DalShardingStrategy (com.ctrip.platform.dal.dao.strategy.DalShardingStrategy)4 DalHints (com.ctrip.platform.dal.dao.DalHints)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)2 DalEventEnum (com.ctrip.platform.dal.dao.DalEventEnum)1 DalException (com.ctrip.platform.dal.exceptions.DalException)1 Connection (java.sql.Connection)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1