use of com.ctrip.framework.dal.cluster.client.database.Database in project dal by ctripcorp.
the class DefaultCluster method getDatabases.
@Override
public List<Database> getDatabases() {
List<Database> databases = new LinkedList<>();
for (DatabaseShard databaseShard : databaseShards.values()) {
databases.addAll(databaseShard.getMasters());
databases.addAll(databaseShard.getSlaves());
}
return databases;
}
use of com.ctrip.framework.dal.cluster.client.database.Database 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);
}
use of com.ctrip.framework.dal.cluster.client.database.Database in project dal by ctripcorp.
the class ClusterDataSource method createInnerDataSource.
protected MultiHostDataSource createInnerDataSource() {
try {
List<Database> databases = cluster.getDatabases();
Map<HostSpec, DataSourceConfigure> dataSourceConfigs = new HashMap<>();
databases.forEach(database -> {
ConnectionString connString = database.getConnectionString();
HostSpec host = HostSpec.of(connString.getPrimaryHost(), connString.getPrimaryPort(), database.getZone());
DataSourceIdentity id = new ClusterDataSourceIdentity(database);
DataSourceConfigure config = provider.getDataSourceConfigure(id);
dataSourceConfigs.put(host, config);
});
MultiHostClusterProperties properties = new MultiHostClusterPropertiesAdapter(cluster.getRouteStrategyConfig(), cluster.getClusterName());
LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format(CAT_LOG_NAME_MULTI_HOST, getClusterName()), "");
return new MultiHostDataSource(buildShardMeta(dataSourceConfigs.keySet()), dataSourceConfigs, properties);
} catch (Throwable t) {
LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format(CAT_LOG_NAME_MULTI_HOST_FAIL, getClusterName()), "");
throw t;
}
}
use of com.ctrip.framework.dal.cluster.client.database.Database in project dal by ctripcorp.
the class ClusterDatabaseSet method getDatabases.
@Override
public Map<String, DataBase> getDatabases() {
List<Database> clusterDatabases = cluster.getDatabases();
Map<String, DataBase> dataBases = new HashMap<>();
for (Database clusterDatabase : clusterDatabases) {
DataBase dataBase = new ClusterDataBase(clusterDatabase);
dataBases.put(dataBase.getName(), dataBase);
}
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() {
int shard = getTheOnlyDbShardIndex();
Database clusterDatabase = cluster.getMasterOnShard(shard);
List<DataBase> dataBases = new LinkedList<>();
dataBases.add(new ClusterDataBase(clusterDatabase));
return dataBases;
}
Aggregations