use of com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy in project dal by ctripcorp.
the class CompositeRoundRobinAccessStrategyTest method pickNode.
@Test
public void pickNode() throws SQLException {
HostSpec hostSpec = routeStrategy.pickNode(dalHints);
// pick from local
Assert.assertEquals(getRequestZone(), hostSpec.zone());
HostSpec hostSpec1 = routeStrategy.pickNode(dalHints);
// pick from local
Assert.assertEquals(getRequestZone(), hostSpec1.zone());
// round robin, so not equal
Assert.assertNotEquals(hostSpec, hostSpec1);
// test request zone is down
if (routeStrategy.isWrapperFor(Map.class)) {
RouteStrategy localRouteStrategy = (RouteStrategy) routeStrategy.unwrap(Map.class).remove(SHAXY);
Assert.assertNotNull(localRouteStrategy);
}
hostSpec = routeStrategy.pickNode(dalHints);
// not pick from local
Assert.assertNotEquals(getRequestZone(), hostSpec.zone());
}
use of com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy in project dal by ctripcorp.
the class MultiHostClusterPropertiesAdapterTest method getRouteStrategy.
@Test
public void getRouteStrategy() {
clusterPropertiesAdapter = new MultiHostClusterPropertiesAdapter(mgrRouteStrategyConfig, CLUSTER_NAME);
String routeStrategy = clusterPropertiesAdapter.routeStrategyName();
Assert.assertEquals(RouteStrategyEnum.WRITE_ORDERED.name(), routeStrategy);
RouteStrategy multiHostStrategy = clusterPropertiesAdapter.generate();
Assert.assertTrue(multiHostStrategy instanceof MGRStrategy);
clusterPropertiesAdapter = new MultiHostClusterPropertiesAdapter(obRouteStrategyConfig, CLUSTER_NAME);
routeStrategy = clusterPropertiesAdapter.routeStrategyName();
Assert.assertEquals(RouteStrategyEnum.WRITE_CURRENT_ZONE_FIRST.getAlias(), routeStrategy);
multiHostStrategy = clusterPropertiesAdapter.generate();
Assert.assertTrue(multiHostStrategy instanceof OBStrategy);
clusterPropertiesAdapter = new MultiHostClusterPropertiesAdapter(mgrRouteStrategyConfig, CLUSTER_NAME);
}
use of com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy in project dal by ctripcorp.
the class MultiHostDataSource method prepareRouteStrategy.
protected RouteStrategy prepareRouteStrategy() {
RouteStrategy strategy = this.clusterProperties.generate();
strategy.init(shardMeta.configuredHosts(), clusterProperties.routeStrategyProperties());
if (strategy instanceof ConnectionFactoryAware) {
((ConnectionFactoryAware) strategy).setConnectionFactory(this.connFactory);
}
if (strategy instanceof HostConnectionValidatorHolder) {
this.connValidator = ((HostConnectionValidatorHolder) strategy).getHostConnectionValidator();
}
if (strategy instanceof ValidatingConnectionValidatorHolder) {
this.validatingConnValidator = ((ValidatingConnectionValidatorHolder) strategy).getValidatingConnectionValidator();
}
return strategy;
}
use of com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy 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.platform.dal.dao.datasource.cluster.strategy.RouteStrategy in project dal by ctripcorp.
the class DefaultClusterRouteStrategyConfig method generate.
@Override
public RouteStrategy generate() {
String strategyName = routeStrategyName();
String clazz = RouteStrategyEnum.parse(strategyName);
try {
return (com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy) Class.forName(clazz).newInstance();
} catch (Throwable t) {
String msg = "Error constructing route strategy: " + strategyName;
throw new DalRuntimeException(msg, t);
}
}
Aggregations