Search in sources :

Example 11 with Database

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(String shard) {
    List<Database> clusterDatabases = cluster.getSlavesOnShard(getDbShardIndex(shard));
    List<DataBase> dataBases = new LinkedList<>();
    for (Database clusterDatabase : clusterDatabases) {
        dataBases.add(new ClusterDataBase(clusterDatabase));
    }
    return dataBases;
}
Also used : Database(com.ctrip.framework.dal.cluster.client.database.Database)

Example 12 with Database

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

the class ClusterDynamicDataSource method createCustomDataSource.

protected DataSource createCustomDataSource() {
    CustomDataSourceFactory dataSourceFactory = getCustomDataSourceFactory();
    List<Database> databases = cluster.getDatabases();
    Set<HostSpec> hostsInfos = new HashSet<>();
    databases.forEach(database -> {
        ConnectionString connString = database.getConnectionString();
        HostSpec host = HostSpec.of(connString.getPrimaryHost(), connString.getPrimaryPort(), database.getZone());
        hostsInfos.add(host);
    });
    return dataSourceFactory.createDataSource(hostsInfos, getProperties(databases.get(0)));
}
Also used : 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) CustomDataSourceFactory(com.ctrip.framework.dal.cluster.client.extended.CustomDataSourceFactory)

Example 13 with Database

use of com.ctrip.framework.dal.cluster.client.database.Database 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 14 with Database

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

the class GroupDataSource method createReadDataSource.

protected void createReadDataSource() {
    int slaveIndex = 0;
    for (Database database : cluster.getSlavesOnShard(clusterInfo.getShardIndex())) {
        ClusterInfo slaveClusterInfo = clusterInfo.cloneSlaveWithIndex(slaveIndex);
        readDataSource.put(database, locator.getDataSource(slaveClusterInfo));
        slaveIndex++;
    }
    readDataSource.put(cluster.getMasterOnShard(clusterInfo.getShardIndex()), createWriteDataSource());
}
Also used : ClusterInfo(com.ctrip.platform.dal.dao.configure.ClusterInfo) 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