use of com.ctrip.framework.dal.cluster.client.exception.DalMetadataException 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.exception.DalMetadataException 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.exception.DalMetadataException in project dal by ctripcorp.
the class ReadCurrentZoneSlavesOnlyStrategy method init.
@Override
public void init(Set<HostSpec> hostSpecs, CaseInsensitiveProperties strategyProperties) {
super.init(hostSpecs, strategyProperties);
for (HostSpec hostSpec : hostSpecs) {
if (!zoneToHost.containsKey(hostSpec.getTrimLowerCaseZone())) {
List<HostSpec> hosts = new ArrayList<>();
hosts.add(hostSpec);
zoneToHost.put(hostSpec.getTrimLowerCaseZone(), hosts);
} else {
zoneToHost.get(hostSpec.getTrimLowerCaseZone()).add(hostSpec);
}
}
if (StringUtils.isTrimmedEmpty(currentZone))
throw new DalMetadataException(ZONE_MSG_LOST);
}
use of com.ctrip.framework.dal.cluster.client.exception.DalMetadataException 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());
}
}
use of com.ctrip.framework.dal.cluster.client.exception.DalMetadataException in project dal by ctripcorp.
the class ReadMasterZoneSlavesOnlyStrategy method init.
@Override
public void init(Set<HostSpec> hostSpecs, CaseInsensitiveProperties strategyProperties) {
super.init(hostSpecs, strategyProperties);
for (HostSpec hostSpec : hostSpecs) {
if (hostSpec.isMaster()) {
masterZone = hostSpec.getTrimLowerCaseZone();
continue;
}
if (!zoneToHost.containsKey(hostSpec.getTrimLowerCaseZone())) {
List<HostSpec> hosts = new ArrayList<>();
hosts.add(hostSpec);
zoneToHost.put(hostSpec.getTrimLowerCaseZone(), hosts);
} else {
zoneToHost.get(hostSpec.getTrimLowerCaseZone()).add(hostSpec);
}
}
if (StringUtils.isTrimmedEmpty(masterZone))
throw new DalMetadataException(ZONE_MSG_LOST);
}
Aggregations