Search in sources :

Example 1 with ClusterRouteStrategyConfig

use of com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig in project dal by ctripcorp.

the class ApiDataSourceIdentityTest method testMultiHost.

@Test
public void testMultiHost() {
    provider.setNormalConfig(false);
    ApiDataSourceIdentity id = new ApiDataSourceIdentity(provider);
    ClusterInfo clusterInfo = id.getClusterInfo();
    Assert.assertEquals(DB_NAME, clusterInfo.getClusterName());
    Assert.assertEquals(0, clusterInfo.getShardIndex().intValue());
    Assert.assertEquals(DatabaseRole.MASTER, clusterInfo.getRole());
    Cluster cluster = clusterInfo.getCluster();
    Assert.assertEquals(DB_NAME, cluster.getClusterName());
    Assert.assertEquals(ClusterType.NORMAL, cluster.getClusterType());
    Assert.assertEquals(DatabaseCategory.MYSQL, cluster.getDatabaseCategory());
    Assert.assertFalse(cluster.dbShardingEnabled());
    Assert.assertEquals(3, cluster.getDatabases().size());
    Set<String> hosts = cluster.getDatabases().stream().map(database -> database.getConnectionString().getPrimaryHost()).collect(Collectors.toSet());
    Assert.assertEquals(1, hosts.size());
    Assert.assertTrue(hosts.contains("localhost"));
    Set<Integer> ports = cluster.getDatabases().stream().map(database -> database.getConnectionString().getPrimaryPort()).collect(Collectors.toSet());
    Assert.assertEquals(3, ports.size());
    Assert.assertTrue(ports.contains(3306));
    Assert.assertTrue(ports.contains(3307));
    Assert.assertTrue(ports.contains(3308));
    Set<String> zones = cluster.getDatabases().stream().map(database -> database.getZone().toLowerCase()).collect(Collectors.toSet());
    Assert.assertEquals(3, zones.size());
    Assert.assertTrue(zones.contains("z1"));
    Assert.assertTrue(zones.contains("z2"));
    Assert.assertTrue(zones.contains("z3"));
    ClusterRouteStrategyConfig routeStrategy = cluster.getRouteStrategyConfig();
    Assert.assertEquals(RouteStrategyEnum.WRITE_ORDERED.getAlias(), routeStrategy.routeStrategyName());
    CaseInsensitiveProperties properties = routeStrategy.routeStrategyProperties();
    Assert.assertEquals("z3,z2,z1", properties.get(MultiMasterStrategy.ZONES_PRIORITY));
    Assert.assertEquals("10000", properties.get(MultiMasterStrategy.FAILOVER_TIME_MS));
    Assert.assertNull(properties.get(MultiMasterStrategy.BLACKLIST_TIMEOUT_MS));
    Assert.assertNull(properties.get(MultiMasterStrategy.FIXED_VALIDATE_PERIOD_MS));
}
Also used : MultiMasterStrategy(com.ctrip.platform.dal.dao.datasource.cluster.strategy.multi.MultiMasterStrategy) ClusterInfo(com.ctrip.platform.dal.dao.configure.ClusterInfo) Set(java.util.Set) Test(org.junit.Test) Database(com.ctrip.framework.dal.cluster.client.database.Database) DatabaseCategory(com.ctrip.framework.dal.cluster.client.database.DatabaseCategory) Collectors(java.util.stream.Collectors) ClusterType(com.ctrip.framework.dal.cluster.client.cluster.ClusterType) CaseInsensitiveProperties(com.ctrip.framework.dal.cluster.client.util.CaseInsensitiveProperties) RouteStrategyEnum(com.ctrip.framework.dal.cluster.client.cluster.RouteStrategyEnum) ClusterRouteStrategyConfig(com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig) DatabaseRole(com.ctrip.framework.dal.cluster.client.database.DatabaseRole) Cluster(com.ctrip.framework.dal.cluster.client.Cluster) Assert(org.junit.Assert) ClusterInfo(com.ctrip.platform.dal.dao.configure.ClusterInfo) CaseInsensitiveProperties(com.ctrip.framework.dal.cluster.client.util.CaseInsensitiveProperties) ClusterRouteStrategyConfig(com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig) Cluster(com.ctrip.framework.dal.cluster.client.Cluster) Test(org.junit.Test)

Example 2 with ClusterRouteStrategyConfig

use of com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig in project dal by ctripcorp.

the class ApiDataSourceIdentityTest method testNormal.

@Test
public void testNormal() {
    provider.setNormalConfig(true);
    ApiDataSourceIdentity id = new ApiDataSourceIdentity(provider);
    ClusterInfo clusterInfo = id.getClusterInfo();
    Assert.assertEquals(DB_NAME, clusterInfo.getClusterName());
    Assert.assertEquals(0, clusterInfo.getShardIndex().intValue());
    Assert.assertEquals(DatabaseRole.MASTER, clusterInfo.getRole());
    Cluster cluster = clusterInfo.getCluster();
    Assert.assertEquals(DB_NAME, cluster.getClusterName());
    Assert.assertEquals(ClusterType.NORMAL, cluster.getClusterType());
    Assert.assertEquals(DatabaseCategory.MYSQL, cluster.getDatabaseCategory());
    Assert.assertFalse(cluster.dbShardingEnabled());
    Assert.assertEquals(1, cluster.getDatabases().size());
    Database database = cluster.getDatabases().iterator().next();
    Assert.assertEquals("localhost", database.getConnectionString().getPrimaryHost());
    Assert.assertEquals(3306, database.getConnectionString().getPrimaryPort());
    ClusterRouteStrategyConfig routeStrategy = cluster.getRouteStrategyConfig();
    Assert.assertNotNull(routeStrategy);
}
Also used : ClusterInfo(com.ctrip.platform.dal.dao.configure.ClusterInfo) ClusterRouteStrategyConfig(com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig) Database(com.ctrip.framework.dal.cluster.client.database.Database) Cluster(com.ctrip.framework.dal.cluster.client.Cluster) Test(org.junit.Test)

Example 3 with ClusterRouteStrategyConfig

use of com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig in project dal by ctripcorp.

the class DatabaseShardImpl method initReadStrategy.

public void initReadStrategy() {
    ClusterRouteStrategyConfig config = databaseShardConfig.getClusterConfig().getRouteStrategyConfig();
    if (config == null || config.multiMaster()) {
        return;
    }
    routeStrategy = (ReadStrategy) config.generate();
    List<Database> databases = new ArrayList<>();
    databases.addAll(masters);
    databases.addAll(slaves);
    Set<HostSpec> hostSpecs = new HashSet<>();
    databases.forEach(database -> {
        ConnectionString connString = database.getConnectionString();
        HostSpec host = HostSpec.of(connString.getPrimaryHost(), connString.getPrimaryPort(), database.getZone(), database.isMaster());
        hostToDataBase.putIfAbsent(host, database);
        hostSpecs.add(host);
    });
    try {
        routeStrategy.init(hostSpecs, new CaseInsensitiveProperties());
    } catch (DalMetadataException error) {
        throw new DalMetadataException(databaseShardConfig.getClusterConfig().getClusterName() + error.getMessage());
    }
}
Also used : CaseInsensitiveProperties(com.ctrip.framework.dal.cluster.client.util.CaseInsensitiveProperties) ClusterRouteStrategyConfig(com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig) DalMetadataException(com.ctrip.framework.dal.cluster.client.exception.DalMetadataException) 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)

Example 4 with ClusterRouteStrategyConfig

use of com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig in project dal by ctripcorp.

the class MultiHostClusterPropertiesAdapterTest method setUp.

@Before
public void setUp() throws Exception {
    mgrRouteStrategyConfig = new ClusterRouteStrategyConfig() {

        @Override
        public RouteStrategy generate() {
            return doGenerate(routeStrategyName());
        }

        @Override
        public String routeStrategyName() {
            return RouteStrategyEnum.WRITE_ORDERED.name();
        }

        @Override
        public boolean multiMaster() {
            return true;
        }

        @Override
        public CaseInsensitiveProperties routeStrategyProperties() {
            return null;
        }
    };
    obRouteStrategyConfig = new ClusterRouteStrategyConfig() {

        @Override
        public RouteStrategy generate() {
            return doGenerate(routeStrategyName());
        }

        @Override
        public String routeStrategyName() {
            return RouteStrategyEnum.WRITE_CURRENT_ZONE_FIRST.getAlias();
        }

        @Override
        public boolean multiMaster() {
            return true;
        }

        @Override
        public CaseInsensitiveProperties routeStrategyProperties() {
            return null;
        }
    };
    customRouteStrategyConfig = new ClusterRouteStrategyConfig() {

        @Override
        public RouteStrategy generate() {
            return doGenerate(routeStrategyName());
        }

        @Override
        public String routeStrategyName() {
            return CUSTOM_STRATEGY;
        }

        @Override
        public boolean multiMaster() {
            return false;
        }

        @Override
        public CaseInsensitiveProperties routeStrategyProperties() {
            return null;
        }
    };
}
Also used : CaseInsensitiveProperties(com.ctrip.framework.dal.cluster.client.util.CaseInsensitiveProperties) ClusterRouteStrategyConfig(com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig) RouteStrategy(com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy) Before(org.junit.Before)

Aggregations

ClusterRouteStrategyConfig (com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig)4 Database (com.ctrip.framework.dal.cluster.client.database.Database)3 CaseInsensitiveProperties (com.ctrip.framework.dal.cluster.client.util.CaseInsensitiveProperties)3 Cluster (com.ctrip.framework.dal.cluster.client.Cluster)2 ClusterInfo (com.ctrip.platform.dal.dao.configure.ClusterInfo)2 Test (org.junit.Test)2 HostSpec (com.ctrip.framework.dal.cluster.client.base.HostSpec)1 ClusterType (com.ctrip.framework.dal.cluster.client.cluster.ClusterType)1 RouteStrategyEnum (com.ctrip.framework.dal.cluster.client.cluster.RouteStrategyEnum)1 ConnectionString (com.ctrip.framework.dal.cluster.client.database.ConnectionString)1 DatabaseCategory (com.ctrip.framework.dal.cluster.client.database.DatabaseCategory)1 DatabaseRole (com.ctrip.framework.dal.cluster.client.database.DatabaseRole)1 DalMetadataException (com.ctrip.framework.dal.cluster.client.exception.DalMetadataException)1 RouteStrategy (com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy)1 MultiMasterStrategy (com.ctrip.platform.dal.dao.datasource.cluster.strategy.multi.MultiMasterStrategy)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Assert (org.junit.Assert)1 Before (org.junit.Before)1