Search in sources :

Example 1 with HostSpec

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

the class ClusterConfigAdapter method buildMultiHostClusterConfig.

private ClusterConfig buildMultiHostClusterConfig(MultiHostConnectionStringConfigure configure) {
    ClusterConfigWithNoVersion clusterConfig = new ClusterConfigWithNoVersion(configure.getName(), ClusterType.NORMAL, DatabaseCategory.MYSQL);
    DatabaseShardConfigImpl databaseShardConfig = new DatabaseShardConfigImpl(clusterConfig, 0);
    List<HostSpec> hosts = configure.getHosts();
    hosts.forEach(host -> {
        DatabaseConfigImpl databaseConfig = new DatabaseConfigImpl(databaseShardConfig);
        databaseConfig.setIp(host.host());
        databaseConfig.setPort(host.port());
        databaseConfig.setZone(host.zone());
        databaseConfig.setDbName(configure.getDbName());
        databaseConfig.setUid(configure.getUserName());
        databaseConfig.setPwd(configure.getPassword());
        databaseShardConfig.addDatabaseConfig(databaseConfig);
    });
    clusterConfig.addDatabaseShardConfig(databaseShardConfig);
    DBModel dbModel = configure.getDbModel();
    String routeStrategyName = DBModel.MGR == dbModel ? RouteStrategyEnum.WRITE_ORDERED.getAlias() : RouteStrategyEnum.WRITE_CURRENT_ZONE_FIRST.getAlias();
    DefaultClusterRouteStrategyConfig routeStrategy = new DefaultClusterRouteStrategyConfig(routeStrategyName);
    if (configure.getZonesPriority() != null)
        routeStrategy.setProperty(MultiMasterStrategy.ZONES_PRIORITY, configure.getZonesPriority());
    if (configure.getFailoverTimeMS() != null)
        routeStrategy.setProperty(MultiMasterStrategy.FAILOVER_TIME_MS, String.valueOf(configure.getFailoverTimeMS()));
    if (configure.getBlacklistTimeoutMS() != null)
        routeStrategy.setProperty(MultiMasterStrategy.BLACKLIST_TIMEOUT_MS, String.valueOf(configure.getBlacklistTimeoutMS()));
    if (configure.getFixedValidatePeriodMS() != null)
        routeStrategy.setProperty(MultiMasterStrategy.FIXED_VALIDATE_PERIOD_MS, String.valueOf(configure.getFixedValidatePeriodMS()));
    clusterConfig.setRouteStrategyConfig(routeStrategy);
    clusterConfig.setCustomizedOption(new DefaultDalConfigCustomizedOption());
    return clusterConfig;
}
Also used : DefaultClusterRouteStrategyConfig(com.ctrip.framework.dal.cluster.client.multihost.DefaultClusterRouteStrategyConfig) DatabaseShardConfigImpl(com.ctrip.framework.dal.cluster.client.config.DatabaseShardConfigImpl) DBModel(com.ctrip.platform.dal.common.enums.DBModel) DatabaseConfigImpl(com.ctrip.framework.dal.cluster.client.config.DatabaseConfigImpl) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec)

Example 2 with HostSpec

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

the class ReadCurrentZoneSlavesFirstStrategyTest method pickRead.

@Test
public void pickRead() {
    ReadCurrentZoneSlavesFirstStrategy strategy = new ReadCurrentZoneSlavesFirstStrategy();
    strategy.init(produceHostSpec(), null);
    strategy.currentZone = "shaoy";
    HostSpec hostSpec = strategy.pickRead(new DalHints());
    assertEquals(strategy.currentZone, hostSpec.getTrimLowerCaseZone());
    strategy.currentZone = "sharb";
    hostSpec = strategy.pickRead(new DalHints());
    assertEquals(strategy.currentZone, hostSpec.getTrimLowerCaseZone());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec) Test(org.junit.Test)

Example 3 with HostSpec

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

the class ReadCurrentZoneSlavesOnlyStrategyTest method pickCurrentZoneSlaveOnly.

@Test
public void pickCurrentZoneSlaveOnly() {
    ReadCurrentZoneSlavesOnlyStrategy strategy = new ReadCurrentZoneSlavesOnlyStrategy();
    strategy.init(ReadCurrentZoneSlavesFirstStrategyTest.produceHostSpec(), null);
    strategy.currentZone = "shaoy";
    HostSpec hostSpec = strategy.pickRead(new DalHints());
    assertEquals(strategy.currentZone, hostSpec.getTrimLowerCaseZone());
    strategy.currentZone = "";
    try {
        hostSpec = strategy.pickRead(new DalHints());
        fail();
    } catch (Throwable t) {
        assertEquals(true, t instanceof DalMetadataException);
        assertEquals(" has no database in ", t.getMessage());
    }
    strategy.currentZone = "shaxy";
    try {
        hostSpec = strategy.pickRead(new DalHints());
        fail();
    } catch (Throwable t) {
        assertEquals(true, t instanceof DalMetadataException);
        assertEquals(" has no database in " + strategy.currentZone, t.getMessage());
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalMetadataException(com.ctrip.framework.dal.cluster.client.exception.DalMetadataException) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec) ReadCurrentZoneSlavesFirstStrategyTest.produceHostSpec(com.ctrip.framework.dal.cluster.client.shard.read.ReadCurrentZoneSlavesFirstStrategyTest.produceHostSpec) Test(org.junit.Test)

Example 4 with HostSpec

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

the class ReadMasterZoneSlavesOnlyStrategyTest method pickMasterZoneSlaveOnly.

@Test
public void pickMasterZoneSlaveOnly() {
    ReadMasterZoneSlavesOnlyStrategy strategy = new ReadMasterZoneSlavesOnlyStrategy();
    strategy.init(produceHostSpec(), null);
    try {
        strategy.pickMasterZoneSlaveOnly();
        fail();
    } catch (Throwable t) {
        assertTrue(t instanceof DalMetadataException);
    }
    ReadMasterZoneSlavesOnlyStrategy strategy1 = new ReadMasterZoneSlavesOnlyStrategy();
    strategy1.init(masterZoneHasSlave(), null);
    HostSpec hostSpec = strategy1.pickMasterZoneSlaveOnly();
    assertEquals(strategy1.masterZone, hostSpec.getTrimLowerCaseZone());
    assertFalse(hostSpec.isMaster());
}
Also used : DalMetadataException(com.ctrip.framework.dal.cluster.client.exception.DalMetadataException) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec) Test(org.junit.Test) ReadCurrentZoneSlavesFirstStrategyTest(com.ctrip.framework.dal.cluster.client.shard.read.ReadCurrentZoneSlavesFirstStrategyTest)

Example 5 with HostSpec

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

the class MultiHostDataSourceTest method testNormal.

@Test
public void testNormal() throws Exception {
    MultiHostDataSource dataSource = new MockMultiHostDataSource(mockDataSourceConfigs(), mockClusterProperties(), "zone1");
    HostSpec expectedHost = HostSpec.of("10.32.20.117", 3306);
    testNormal(dataSource, expectedHost);
}
Also used : HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec) Test(org.junit.Test)

Aggregations

HostSpec (com.ctrip.framework.dal.cluster.client.base.HostSpec)30 Test (org.junit.Test)11 DalMetadataException (com.ctrip.framework.dal.cluster.client.exception.DalMetadataException)5 SQLException (java.sql.SQLException)5 ConnectionString (com.ctrip.framework.dal.cluster.client.database.ConnectionString)3 Database (com.ctrip.framework.dal.cluster.client.database.Database)3 DalHints (com.ctrip.platform.dal.dao.DalHints)3 HashMap (java.util.HashMap)3 DataSourceConfigure (com.ctrip.platform.dal.dao.configure.DataSourceConfigure)2 RouteStrategy (com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy)2 Connection (java.sql.Connection)2 Map (java.util.Map)2 DatabaseConfigImpl (com.ctrip.framework.dal.cluster.client.config.DatabaseConfigImpl)1 DatabaseShardConfigImpl (com.ctrip.framework.dal.cluster.client.config.DatabaseShardConfigImpl)1 CustomDataSourceFactory (com.ctrip.framework.dal.cluster.client.extended.CustomDataSourceFactory)1 ClusterRouteStrategyConfig (com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig)1 DefaultClusterRouteStrategyConfig (com.ctrip.framework.dal.cluster.client.multihost.DefaultClusterRouteStrategyConfig)1 ReadCurrentZoneSlavesFirstStrategyTest (com.ctrip.framework.dal.cluster.client.shard.read.ReadCurrentZoneSlavesFirstStrategyTest)1 ReadCurrentZoneSlavesFirstStrategyTest.produceHostSpec (com.ctrip.framework.dal.cluster.client.shard.read.ReadCurrentZoneSlavesFirstStrategyTest.produceHostSpec)1 CaseInsensitiveProperties (com.ctrip.framework.dal.cluster.client.util.CaseInsensitiveProperties)1