use of com.ctrip.framework.dal.cluster.client.base.HostSpec 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.base.HostSpec in project dal by ctripcorp.
the class ReadCurrentZoneSlavesFirstStrategy method pickRead.
@Override
public HostSpec pickRead(DalHints dalHints) throws HostNotExpectedException {
HostSpec hostSpec = hintsRoute(dalHints);
if (hostSpec != null)
return hostSpec;
hostSpec = pickCurrentZoneSlave();
return hostSpec == null ? pickSlaveFirst() : hostSpec;
}
use of com.ctrip.framework.dal.cluster.client.base.HostSpec 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);
}
use of com.ctrip.framework.dal.cluster.client.base.HostSpec in project dal by ctripcorp.
the class MajorityHostValidator method validate.
@Override
public boolean validate(HostConnection connection) throws SQLException {
try {
HostSpec currentHost = connection.getHost();
if (connection.isWrapperFor(ConnectionDelegate.class)) {
ConnectionDelegate connectionDelegate = connection.unwrap(ConnectionDelegate.class);
if (connectionDelegate.getDelegated() == null) {
LOGGER.warn(CREATE_CONNECTION_FAILED + currentHost.toString());
LOGGER.logEvent(CAT_LOG_TYPE, CREATE_CONNECTION_FAILED, currentHost.toString());
asyncValidate(orderHosts);
return false;
}
}
ValidateResult validateResult = validateAndUpdate(connection, currentHost, configuredHosts.size());
LOGGER.info(VALIDATE_RESULT + currentHost.toString() + ":" + validateResult.validateResult);
LOGGER.logEvent(CAT_LOG_TYPE, currentHost.toString() + ":" + validateResult.validateResult, validateResult.toString());
if (!validateResult.validateResult) {
asyncValidate(orderHosts);
}
return validateResult.validateResult;
} catch (SQLException e) {
asyncValidate(orderHosts);
throw e;
}
}
use of com.ctrip.framework.dal.cluster.client.base.HostSpec in project dal by ctripcorp.
the class MajorityHostValidator method doubleCheckOnlineStatus.
protected boolean doubleCheckOnlineStatus(String currentMemberId, HostSpec currentHostSpec) {
CountDownLatch latch = new CountDownLatch(1);
List<Future> futures = new ArrayList<>();
AtomicInteger onlineCount = new AtomicInteger(1);
AtomicInteger finishedCount = new AtomicInteger(1);
for (HostSpec hostSpec : configuredHosts) {
if (!currentHostSpec.equals(hostSpec)) {
Future future = doubleCheckService.submit(() -> {
try (Connection connection = getValidatedConnection(hostSpec)) {
boolean result = doubleCheckValidate(connection, currentMemberId);
if (result)
onlineCount.incrementAndGet();
} catch (Exception e) {
LOGGER.warn("doubleCheckOnlineStatus", e);
} finally {
finishedCount.incrementAndGet();
// online count is more than half or failed count is more than half
if (onlineCount.get() * 2 > configuredHosts.size() || (finishedCount.get() - onlineCount.get()) * 2 > configuredHosts.size())
latch.countDown();
}
});
futures.add(future);
}
}
try {
latch.await(1, TimeUnit.SECONDS);
} catch (Throwable e) {
}
for (Future future : futures) {
try {
future.cancel(true);
} catch (Throwable t) {
}
}
return onlineCount.get() * 2 > configuredHosts.size();
}
Aggregations