use of com.ctrip.framework.dal.cluster.client.database.Database in project dal by ctripcorp.
the class ClusterDatabaseSetAdapter method checkCluster.
private boolean checkCluster(Cluster cluster) {
if (!cluster.dbShardingEnabled()) {
int theOnlyShard = cluster.getAllDbShards().iterator().next();
Database master = cluster.getMasterOnShard(theOnlyShard);
List<Database> slaves = cluster.getSlavesOnShard(theOnlyShard);
return master != null && (slaves == null || slaves.isEmpty());
}
return false;
}
use of com.ctrip.framework.dal.cluster.client.database.Database in project dal by ctripcorp.
the class ClusterDatabaseSet method getSlaveDbs.
@Override
public List<DataBase> getSlaveDbs() {
int shard = getTheOnlyDbShardIndex();
List<Database> clusterDatabases = cluster.getSlavesOnShard(shard);
List<DataBase> dataBases = new LinkedList<>();
for (Database clusterDatabase : clusterDatabases) {
dataBases.add(new ClusterDataBase(clusterDatabase));
}
return dataBases;
}
use of com.ctrip.framework.dal.cluster.client.database.Database in project dal by ctripcorp.
the class ClusterDatabaseSet method getMasterDbs.
@Override
public List<DataBase> getMasterDbs(String shard) {
Database clusterDatabase = cluster.getMasterOnShard(getDbShardIndex(shard));
List<DataBase> dataBases = new LinkedList<>();
dataBases.add(new ClusterDataBase(clusterDatabase));
return dataBases;
}
use of com.ctrip.framework.dal.cluster.client.database.Database 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);
}
use of com.ctrip.framework.dal.cluster.client.database.Database 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));
}
Aggregations