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;
}
}
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);
}
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);
}
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());
}
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)));
}
Aggregations