Search in sources :

Example 6 with Cluster

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

the class DataSourceLocator method createRefreshableDataSource.

private RefreshableDataSource createRefreshableDataSource(DataSourceIdentity id) throws SQLException {
    if (id instanceof ClusterDataSourceIdentity) {
        Database database = ((ClusterDataSourceIdentity) id).getDatabase();
        Cluster cluster = database.getCluster();
        ClusterInfo clusterInfo = new ClusterInfo(database.getClusterName(), database.getShardIndex(), database.isMaster() ? DatabaseRole.MASTER : DatabaseRole.SLAVE, cluster != null && cluster.dbShardingEnabled(), cluster);
        try {
            if (cluster != null && cluster.getClusterType() == ClusterType.DRC) {
                LocalizationConfig localizationConfig = cluster.getLocalizationConfig();
                LocalizationConfig lastLocalizationConfig = cluster.getLastLocalizationConfig();
                LocalizationValidator validator = factory.createValidator(clusterInfo, localizationConfig, lastLocalizationConfig);
                LOGGER.logEvent(LOG_TYPE_CREATE_DATASOURCE, String.format(LOG_NAME_CREATE_DRC_DATASOURCE, clusterInfo.toString()), localizationConfig.toString());
                return new LocalizedDataSource(validator, id, provider.getDataSourceConfigure(id));
            }
        } catch (Exception e) {
            LOGGER.logEvent(LOG_TYPE_CREATE_DATASOURCE, String.format(LOG_NAME_CREATE_DRC_DATASOURCE_FAIL, clusterInfo.toString()), e.getMessage());
            throw e;
        }
    }
    LOGGER.logEvent(LOG_TYPE_CREATE_DATASOURCE, String.format(LOG_NAME_CREATE_NORMAL_DATASOURCE, id.getId()), "");
    SingleDataSourceConfigureProvider dataSourceConfigureProvider = new SingleDataSourceConfigureProvider(id, provider);
    return new ForceSwitchableDataSource(id, dataSourceConfigureProvider);
}
Also used : LocalizationConfig(com.ctrip.framework.dal.cluster.client.config.LocalizationConfig) Database(com.ctrip.framework.dal.cluster.client.database.Database) Cluster(com.ctrip.framework.dal.cluster.client.Cluster) SQLException(java.sql.SQLException)

Example 7 with Cluster

use of com.ctrip.framework.dal.cluster.client.Cluster 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 8 with Cluster

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

the class ClusterDatabaseSetTest method testNonShardingCluster.

@Test
public void testNonShardingCluster() {
    ClusterConfigProvider provider = new DefaultLocalConfigProvider("NonShardingCluster");
    // todo-lhj
    ClusterConfig config = provider.getClusterConfig(new DefaultDalConfigCustomizedOption());
    Cluster cluster = config.generate();
    ClusterDatabaseSet databaseSet = new ClusterDatabaseSet("NonShardingCluster", cluster, new DalConnectionLocator() {

        @Override
        public void setup(Collection<DatabaseSet> databaseSets) {
        }

        @Override
        public Connection getConnection(String name) throws Exception {
            return null;
        }

        @Override
        public Connection getConnection(String name, ConnectionAction action) throws Exception {
            return null;
        }

        @Override
        public Connection getConnection(DataSourceIdentity id) throws Exception {
            return null;
        }

        @Override
        public Connection getConnection(DataSourceIdentity id, ConnectionAction action) throws Exception {
            return null;
        }

        @Override
        public IntegratedConfigProvider getIntegratedConfigProvider() {
            return null;
        }

        @Override
        public void setupCluster(Cluster cluster) {
        }

        @Override
        public void uninstallCluster(Cluster cluster) {
        }

        @Override
        public void initialize(Map<String, String> settings) throws Exception {
        }
    });
    Assert.assertFalse(databaseSet.isShardingSupported());
    Assert.assertEquals(1, databaseSet.getMasterDbs().size());
    Assert.assertEquals(0, databaseSet.getSlaveDbs().size());
}
Also used : DataSourceIdentity(com.ctrip.platform.dal.dao.datasource.DataSourceIdentity) Connection(java.sql.Connection) Cluster(com.ctrip.framework.dal.cluster.client.Cluster) ClusterConfigProvider(com.ctrip.framework.dal.cluster.client.config.ClusterConfigProvider) ConnectionAction(com.ctrip.platform.dal.dao.client.ConnectionAction) DefaultLocalConfigProvider(com.ctrip.framework.dal.cluster.client.config.DefaultLocalConfigProvider) ClusterConfig(com.ctrip.framework.dal.cluster.client.config.ClusterConfig) DalConnectionLocator(com.ctrip.platform.dal.dao.client.DalConnectionLocator) Test(org.junit.Test)

Example 9 with Cluster

use of com.ctrip.framework.dal.cluster.client.Cluster 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 10 with Cluster

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

the class TraceableClusterDataSourceIdentity method createSqlContext.

@Override
public SqlContext createSqlContext() {
    Cluster cluster = getDatabase().getCluster();
    ClusterDbSqlContext context = new ClusterDbSqlContext(cluster, getShardIndex(), getDatabaseRole());
    if (cluster != null && cluster.getLocalizationConfig() != null)
        context.populateDbZone(cluster.getLocalizationConfig().getZoneId());
    return context;
}
Also used : ClusterDbSqlContext(com.ctrip.platform.dal.dao.datasource.log.ClusterDbSqlContext) Cluster(com.ctrip.framework.dal.cluster.client.Cluster)

Aggregations

Cluster (com.ctrip.framework.dal.cluster.client.Cluster)14 Database (com.ctrip.framework.dal.cluster.client.database.Database)3 Test (org.junit.Test)3 LocalizationConfig (com.ctrip.framework.dal.cluster.client.config.LocalizationConfig)2 ClusterRouteStrategyConfig (com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig)2 ClusterInfo (com.ctrip.platform.dal.dao.configure.ClusterInfo)2 DalRuntimeException (com.ctrip.platform.dal.exceptions.DalRuntimeException)2 DataSource (javax.sql.DataSource)2 ClusterSwitchedEvent (com.ctrip.framework.dal.cluster.client.cluster.ClusterSwitchedEvent)1 ClusterType (com.ctrip.framework.dal.cluster.client.cluster.ClusterType)1 DefaultCluster (com.ctrip.framework.dal.cluster.client.cluster.DefaultCluster)1 RouteStrategyEnum (com.ctrip.framework.dal.cluster.client.cluster.RouteStrategyEnum)1 ClusterConfig (com.ctrip.framework.dal.cluster.client.config.ClusterConfig)1 ClusterConfigProvider (com.ctrip.framework.dal.cluster.client.config.ClusterConfigProvider)1 DalConfigCustomizedOption (com.ctrip.framework.dal.cluster.client.config.DalConfigCustomizedOption)1 DefaultLocalConfigProvider (com.ctrip.framework.dal.cluster.client.config.DefaultLocalConfigProvider)1 DatabaseCategory (com.ctrip.framework.dal.cluster.client.database.DatabaseCategory)1 DatabaseRole (com.ctrip.framework.dal.cluster.client.database.DatabaseRole)1 DatabaseShard (com.ctrip.framework.dal.cluster.client.shard.DatabaseShard)1 CaseInsensitiveProperties (com.ctrip.framework.dal.cluster.client.util.CaseInsensitiveProperties)1