Search in sources :

Example 21 with HostSpec

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());
    }
}
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 22 with HostSpec

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;
}
Also used : HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec)

Example 23 with 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);
}
Also used : DalMetadataException(com.ctrip.framework.dal.cluster.client.exception.DalMetadataException) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec)

Example 24 with HostSpec

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;
    }
}
Also used : ConnectionDelegate(com.ctrip.platform.dal.dao.datasource.cluster.ConnectionDelegate) SQLException(java.sql.SQLException) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec)

Example 25 with HostSpec

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) HostConnection(com.ctrip.platform.dal.dao.datasource.cluster.HostConnection) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec) MySQLSyntaxErrorException(com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException) SQLException(java.sql.SQLException)

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