Search in sources :

Example 6 with Database

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;
}
Also used : Database(com.ctrip.framework.dal.cluster.client.database.Database) DatabaseShard(com.ctrip.framework.dal.cluster.client.shard.DatabaseShard)

Example 7 with Database

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);
}
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 8 with Database

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;
    }
}
Also used : HashMap(java.util.HashMap) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec) Database(com.ctrip.framework.dal.cluster.client.database.Database) ConnectionString(com.ctrip.framework.dal.cluster.client.database.ConnectionString) DataSourceConfigure(com.ctrip.platform.dal.dao.configure.DataSourceConfigure)

Example 9 with Database

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;
}
Also used : Database(com.ctrip.framework.dal.cluster.client.database.Database)

Example 10 with Database

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;
}
Also used : Database(com.ctrip.framework.dal.cluster.client.database.Database)

Aggregations

Database (com.ctrip.framework.dal.cluster.client.database.Database)14 Cluster (com.ctrip.framework.dal.cluster.client.Cluster)3 HostSpec (com.ctrip.framework.dal.cluster.client.base.HostSpec)3 ConnectionString (com.ctrip.framework.dal.cluster.client.database.ConnectionString)3 ClusterRouteStrategyConfig (com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig)3 ClusterInfo (com.ctrip.platform.dal.dao.configure.ClusterInfo)3 CaseInsensitiveProperties (com.ctrip.framework.dal.cluster.client.util.CaseInsensitiveProperties)2 Test (org.junit.Test)2 ClusterType (com.ctrip.framework.dal.cluster.client.cluster.ClusterType)1 RouteStrategyEnum (com.ctrip.framework.dal.cluster.client.cluster.RouteStrategyEnum)1 LocalizationConfig (com.ctrip.framework.dal.cluster.client.config.LocalizationConfig)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 CustomDataSourceFactory (com.ctrip.framework.dal.cluster.client.extended.CustomDataSourceFactory)1 DatabaseShard (com.ctrip.framework.dal.cluster.client.shard.DatabaseShard)1 DataSourceConfigure (com.ctrip.platform.dal.dao.configure.DataSourceConfigure)1 MultiMasterStrategy (com.ctrip.platform.dal.dao.datasource.cluster.strategy.multi.MultiMasterStrategy)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1