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;
}
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());
}
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());
}
}
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());
}
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);
}
Aggregations