Search in sources :

Example 1 with RouteStrategy

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());
}
Also used : RouteStrategy(com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec) Test(org.junit.Test)

Example 2 with RouteStrategy

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);
}
Also used : MGRStrategy(com.ctrip.platform.dal.dao.datasource.cluster.strategy.multi.mgr.MGRStrategy) RouteStrategy(com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy) OBStrategy(com.ctrip.platform.dal.dao.datasource.cluster.strategy.multi.ob.OBStrategy) Test(org.junit.Test)

Example 3 with RouteStrategy

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;
}
Also used : ConnectionFactoryAware(com.ctrip.platform.dal.dao.datasource.cluster.strategy.ConnectionFactoryAware) ValidatingConnectionValidatorHolder(com.ctrip.platform.dal.dao.datasource.cluster.strategy.ValidatingConnectionValidatorHolder) RouteStrategy(com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy) HostConnectionValidatorHolder(com.ctrip.platform.dal.dao.datasource.cluster.strategy.HostConnectionValidatorHolder)

Example 4 with RouteStrategy

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());
}
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 5 with RouteStrategy

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);
    }
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) RouteStrategy(com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy)

Aggregations

RouteStrategy (com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy)6 HostSpec (com.ctrip.framework.dal.cluster.client.base.HostSpec)2 DalRuntimeException (com.ctrip.platform.dal.exceptions.DalRuntimeException)2 Test (org.junit.Test)2 ClusterRouteStrategyConfig (com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig)1 CaseInsensitiveProperties (com.ctrip.framework.dal.cluster.client.util.CaseInsensitiveProperties)1 ConnectionFactoryAware (com.ctrip.platform.dal.dao.datasource.cluster.strategy.ConnectionFactoryAware)1 HostConnectionValidatorHolder (com.ctrip.platform.dal.dao.datasource.cluster.strategy.HostConnectionValidatorHolder)1 ValidatingConnectionValidatorHolder (com.ctrip.platform.dal.dao.datasource.cluster.strategy.ValidatingConnectionValidatorHolder)1 MGRStrategy (com.ctrip.platform.dal.dao.datasource.cluster.strategy.multi.mgr.MGRStrategy)1 OBStrategy (com.ctrip.platform.dal.dao.datasource.cluster.strategy.multi.ob.OBStrategy)1 HostValidatorAware (com.ctrip.platform.dal.dao.datasource.cluster.strategy.multi.validator.HostValidatorAware)1 Map (java.util.Map)1 Set (java.util.Set)1 Before (org.junit.Before)1