Search in sources :

Example 16 with HostSpec

use of com.ctrip.framework.dal.cluster.client.base.HostSpec in project dal by ctripcorp.

the class ClusterDataSource method createInnerDataSource.

protected MultiHostDataSource createInnerDataSource() {
    try {
        List<Database> databases = cluster.getDatabases();
        Map<HostSpec, DataSourceConfigure> dataSourceConfigs = new HashMap<>();
        databases.forEach(database -> {
            ConnectionString connString = database.getConnectionString();
            HostSpec host = HostSpec.of(connString.getPrimaryHost(), connString.getPrimaryPort(), database.getZone());
            DataSourceIdentity id = new ClusterDataSourceIdentity(database);
            DataSourceConfigure config = provider.getDataSourceConfigure(id);
            dataSourceConfigs.put(host, config);
        });
        MultiHostClusterProperties properties = new MultiHostClusterPropertiesAdapter(cluster.getRouteStrategyConfig(), cluster.getClusterName());
        LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format(CAT_LOG_NAME_MULTI_HOST, getClusterName()), "");
        return new MultiHostDataSource(buildShardMeta(dataSourceConfigs.keySet()), dataSourceConfigs, properties);
    } catch (Throwable t) {
        LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format(CAT_LOG_NAME_MULTI_HOST_FAIL, getClusterName()), "");
        throw t;
    }
}
Also used : HashMap(java.util.HashMap) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec) Database(com.ctrip.framework.dal.cluster.client.database.Database) ConnectionString(com.ctrip.framework.dal.cluster.client.database.ConnectionString) DataSourceConfigure(com.ctrip.platform.dal.dao.configure.DataSourceConfigure)

Example 17 with HostSpec

use of com.ctrip.framework.dal.cluster.client.base.HostSpec in project dal by ctripcorp.

the class MultiHostDataSource method getConnection.

@Override
public Connection getConnection() throws SQLException {
    DalHints dalHints = new DalHints();
    HostSpec hostSpec = routeStrategy.pickNode(dalHints);
    Connection targetConnection;
    try {
        targetConnection = connFactory.getPooledConnectionForHost(hostSpec);
    } catch (Exception e) {
        routeStrategy.interceptException(null, new DefaultHostConnection(null, hostSpec));
        throw e;
    }
    return new DefaultHostConnection(targetConnection, hostSpec);
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) Connection(java.sql.Connection) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec) InvalidConnectionException(com.ctrip.platform.dal.exceptions.InvalidConnectionException) SQLException(java.sql.SQLException)

Example 18 with HostSpec

use of com.ctrip.framework.dal.cluster.client.base.HostSpec in project dal by ctripcorp.

the class ZoneDividedStrategyContext method accept.

@Override
public RouteStrategy accept(StrategyTransformer transformer) {
    for (HostSpec hostSpec : hostSpecs) {
        String zone = hostSpec.zone();
        Set<HostSpec> hostSpecSet = get(zone);
        if (hostSpecSet == null) {
            hostSpecSet = new HashSet<>();
            put(zone, hostSpecSet);
        }
        hostSpecSet.add(hostSpec);
    }
    return transformer.visit(this);
}
Also used : HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec)

Example 19 with HostSpec

use of com.ctrip.framework.dal.cluster.client.base.HostSpec in project dal by ctripcorp.

the class CompositeStrategyTransformer method visit.

@Override
public RouteStrategy visit(StrategyContext strategyContext) {
    try {
        if (strategyContext instanceof ZoneDividedStrategyContext) {
            ZoneDividedStrategyContext obStrategyContext = (ZoneDividedStrategyContext) strategyContext;
            CompositeRoundRobinStrategy localizedAccessStrategy = new CompositeRoundRobinStrategy();
            if (obStrategyContext.getZone() != null) {
                localizedAccessStrategy.setZone(obStrategyContext.getZone().toUpperCase());
            }
            for (Map.Entry<String, Set<HostSpec>> entry : obStrategyContext.entrySet()) {
                String zone = entry.getKey();
                Set<HostSpec> hostSpecSet = entry.getValue();
                RouteStrategy strategy = new ValidatorAwareRoundRobinStrategy();
                strategy.init(hostSpecSet, obStrategyContext.getStrategyProperties());
                if (strategy instanceof HostValidatorAware) {
                    ((HostValidatorAware) strategy).setHostValidator(obStrategyContext.getHostValidator());
                }
                localizedAccessStrategy.put(zone.toUpperCase(), strategy);
            }
            return localizedAccessStrategy;
        }
    } catch (Exception e) {
        throw new DalRuntimeException("generate error", e);
    }
    throw new DalRuntimeException("StrategyContext mismatch for " + strategyContext.getClass());
}
Also used : HostValidatorAware(com.ctrip.platform.dal.dao.datasource.cluster.strategy.multi.validator.HostValidatorAware) Set(java.util.Set) RouteStrategy(com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec) DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) Map(java.util.Map)

Example 20 with HostSpec

use of com.ctrip.framework.dal.cluster.client.base.HostSpec in project dal by ctripcorp.

the class ClusterDynamicDataSource method createCustomDataSource.

protected DataSource createCustomDataSource() {
    CustomDataSourceFactory dataSourceFactory = getCustomDataSourceFactory();
    List<Database> databases = cluster.getDatabases();
    Set<HostSpec> hostsInfos = new HashSet<>();
    databases.forEach(database -> {
        ConnectionString connString = database.getConnectionString();
        HostSpec host = HostSpec.of(connString.getPrimaryHost(), connString.getPrimaryPort(), database.getZone());
        hostsInfos.add(host);
    });
    return dataSourceFactory.createDataSource(hostsInfos, getProperties(databases.get(0)));
}
Also used : Database(com.ctrip.framework.dal.cluster.client.database.Database) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec) ConnectionString(com.ctrip.framework.dal.cluster.client.database.ConnectionString) CustomDataSourceFactory(com.ctrip.framework.dal.cluster.client.extended.CustomDataSourceFactory)

Aggregations

HostSpec (com.ctrip.framework.dal.cluster.client.base.HostSpec)30 Test (org.junit.Test)11 DalMetadataException (com.ctrip.framework.dal.cluster.client.exception.DalMetadataException)5 SQLException (java.sql.SQLException)5 ConnectionString (com.ctrip.framework.dal.cluster.client.database.ConnectionString)3 Database (com.ctrip.framework.dal.cluster.client.database.Database)3 DalHints (com.ctrip.platform.dal.dao.DalHints)3 HashMap (java.util.HashMap)3 DataSourceConfigure (com.ctrip.platform.dal.dao.configure.DataSourceConfigure)2 RouteStrategy (com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy)2 Connection (java.sql.Connection)2 Map (java.util.Map)2 DatabaseConfigImpl (com.ctrip.framework.dal.cluster.client.config.DatabaseConfigImpl)1 DatabaseShardConfigImpl (com.ctrip.framework.dal.cluster.client.config.DatabaseShardConfigImpl)1 CustomDataSourceFactory (com.ctrip.framework.dal.cluster.client.extended.CustomDataSourceFactory)1 ClusterRouteStrategyConfig (com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig)1 DefaultClusterRouteStrategyConfig (com.ctrip.framework.dal.cluster.client.multihost.DefaultClusterRouteStrategyConfig)1 ReadCurrentZoneSlavesFirstStrategyTest (com.ctrip.framework.dal.cluster.client.shard.read.ReadCurrentZoneSlavesFirstStrategyTest)1 ReadCurrentZoneSlavesFirstStrategyTest.produceHostSpec (com.ctrip.framework.dal.cluster.client.shard.read.ReadCurrentZoneSlavesFirstStrategyTest.produceHostSpec)1 CaseInsensitiveProperties (com.ctrip.framework.dal.cluster.client.util.CaseInsensitiveProperties)1