Search in sources :

Example 1 with Cluster

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

the class ConnectionAction method initLogEntry.

public void initLogEntry(String logicDbName, DalHints hints) {
    this.entry = logger.createLogEntry();
    DatabaseSet databaseSet = DalClientFactory.getDalConfigure().getDatabaseSet(logicDbName);
    if (databaseSet instanceof ClusterDatabaseSet) {
        Cluster cluster = ((ClusterDatabaseSet) databaseSet).getCluster();
        entry.setCluster(cluster);
        entry.setClusterName(cluster.getClusterName().toLowerCase());
        LocalizationConfig localizationConfig = cluster.getLocalizationConfig();
        if (localizationConfig != null)
            entry.setDbZone(localizationConfig.getZoneId());
    }
    entry.setLogicDbName(logicDbName);
    entry.setDbCategory(DalClientFactory.getDalConfigure().getDatabaseSet(logicDbName).getDatabaseCategory());
    entry.setClientVersion(Version.getVersion());
    entry.setClientZone(envUtils.getZone());
    entry.setSensitive(hints.is(DalHintEnum.sensitive));
    entry.setEvent(operation);
    entry.setShardingCategory(shardingCategory);
    wrapSql();
    entry.setCallString(callString);
    if (sqls != null)
        entry.setSqls(sqls);
    else
        entry.setSqls(sql);
    if (null != parametersList) {
        String[] params = new String[parametersList.length];
        for (int i = 0; i < parametersList.length; i++) {
            params[i] = parametersList[i].toLogString();
        }
        entry.setPramemters(params);
    } else if (parameters != null) {
        entry.setPramemters(parameters.toLogString());
        hints.setParameters(parameters);
    }
}
Also used : ClusterDatabaseSet(com.ctrip.platform.dal.dao.configure.ClusterDatabaseSet) DatabaseSet(com.ctrip.platform.dal.dao.configure.DatabaseSet) LocalizationConfig(com.ctrip.framework.dal.cluster.client.config.LocalizationConfig) Cluster(com.ctrip.framework.dal.cluster.client.Cluster) ClusterDatabaseSet(com.ctrip.platform.dal.dao.configure.ClusterDatabaseSet)

Example 2 with Cluster

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

the class DalConnectionManager method clusterSelect.

protected DataBase clusterSelect(DatabaseSet dbSet, DalHints hints, String shard, boolean isMaster, boolean isSelect) {
    Cluster cluster = ((ClusterDatabaseSet) dbSet).getCluster();
    logReadStrategy(cluster);
    DatabaseShard databaseShard;
    if (StringUtils.isEmpty(shard)) {
        Set<Integer> shards = cluster.getAllDbShards();
        if (shards.size() == 0)
            throw new DalRuntimeException("no shards found for this cluster");
        if (shards.size() > 1)
            throw new DalRuntimeException("multiple shards detected for non sharding cluster");
        databaseShard = cluster.getDatabaseShard(shards.iterator().next());
    } else
        databaseShard = cluster.getDatabaseShard(Integer.valueOf(shard));
    if (isMaster || !isSelect) {
        return new ClusterDataBase(databaseShard.getMasters().iterator().next());
    }
    return new ClusterDataBase(databaseShard.selectDatabaseFromReadStrategy(hints));
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) Cluster(com.ctrip.framework.dal.cluster.client.Cluster) DatabaseShard(com.ctrip.framework.dal.cluster.client.shard.DatabaseShard)

Example 3 with Cluster

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

the class ClusterDatabaseSetAdapter method tryAdapt.

private ClusterDatabaseSet tryAdapt(DefaultDatabaseSet defaultDatabaseSet) {
    try {
        DataBase master = defaultDatabaseSet.getMasterDbs().iterator().next();
        if ((master instanceof DefaultDataBase) && !(master instanceof ProviderDataBase)) {
            String databaseKey = master.getConnectionString();
            Map<String, DalConnectionString> failedConnectionStrings = DataSourceConfigureLocatorManager.getInstance().getFailedConnectionStrings();
            if (failedConnectionStrings == null || !failedConnectionStrings.containsKey(databaseKey)) {
                ClusterInfo clusterInfo = clusterInfoProvider.getClusterInfo(databaseKey);
                if (clusterInfo != null && clusterInfo.getRole() == DatabaseRole.MASTER && !clusterInfo.dbSharding()) {
                    String clusterName = clusterInfo.getClusterName();
                    // todo-lhj 弄明白怎么回事
                    Cluster cluster = clusterManager.getOrCreateCluster(clusterName, new DefaultDalConfigCustomizedOption());
                    if (checkCluster(cluster)) {
                        LOGGER.logEvent(DalLogTypes.DAL_VALIDATION, "ClusterAdaptSucceeded", String.format("databaseSet: %s, clusterName: %s", defaultDatabaseSet.getName(), clusterName));
                        return new ClusterDatabaseSet(defaultDatabaseSet.getName(), cluster, connectionLocator);
                    }
                }
            }
        }
        LOGGER.logEvent(DalLogTypes.DAL_VALIDATION, "ClusterAdaptSkipped", String.format("databaseSet: %s", defaultDatabaseSet.getName()));
    } catch (Throwable t) {
        LOGGER.logEvent(DalLogTypes.DAL_VALIDATION, "ClusterAdaptFailed", String.format("databaseSet: %s", defaultDatabaseSet.getName()));
        LOGGER.warn("Adapt DefaultDatabaseSet to ClusterDatabaseSet exception", t);
    }
    return null;
}
Also used : Cluster(com.ctrip.framework.dal.cluster.client.Cluster)

Example 4 with Cluster

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

the class DynamicCluster method doSwitch.

public void doSwitch(ClusterConfig current) throws Exception {
    String logName = String.format(CAT_LOG_NAME_FORMAT, getClusterName());
    LOGGER.logTransaction(CAT_LOG_TYPE, logName, "", () -> {
        Cluster curr = clusterConfig.generate();
        Cluster prev = innerCluster.getAndSet(curr);
        ((DefaultCluster) curr).setLastLocalizationConfig(prev.getLocalizationConfig());
        try {
            if (prev.getClusterType() != ClusterType.DRC && curr.getClusterType() == ClusterType.DRC)
                LOGGER.logEvent(CAT_LOG_TYPE, String.format(CAT_EVENT_NAME_NORMAL_TO_DRC, getClusterName()), "");
            else if (prev.getClusterType() == ClusterType.DRC && curr.getClusterType() != ClusterType.DRC)
                LOGGER.logEvent(CAT_LOG_TYPE, String.format(CAT_EVENT_NAME_DRC_TO_NORMAL, getClusterName()), "");
        } catch (Throwable t) {
        // ignore
        }
        // TODO: TO BE REFACTORED
        ClusterSwitchedEvent event = new ClusterSwitchedEvent(curr, prev);
        for (Listener<ClusterSwitchedEvent> listener : getListeners()) {
            try {
                listener.onChanged(event);
            } catch (Throwable t) {
                LOGGER.logEvent(CAT_LOG_TYPE, logName, "ListenerError: " + listener.toString());
            }
        }
    });
}
Also used : DefaultCluster(com.ctrip.framework.dal.cluster.client.cluster.DefaultCluster) Cluster(com.ctrip.framework.dal.cluster.client.Cluster) ClusterSwitchedEvent(com.ctrip.framework.dal.cluster.client.cluster.ClusterSwitchedEvent) DefaultCluster(com.ctrip.framework.dal.cluster.client.cluster.DefaultCluster)

Example 5 with Cluster

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

the class DataSourceLocator method getDataSource.

public DataSource getDataSource(ClusterInfo clusterInfo, DalConfigCustomizedOption customizedOption) {
    Cluster cluster = clusterInfo.getCluster();
    if (cluster == null) {
        cluster = clusterManager.getOrCreateCluster(clusterInfo.getClusterName(), customizedOption);
        clusterInfo.setCluster(cluster);
    }
    DataSourceIdentity id = clusterInfo.toDataSourceIdentity();
    DataSource ds = cache.get(id);
    if (ds == null) {
        synchronized (cache) {
            ds = cache.get(id);
            if (ds == null) {
                try {
                    ds = createDataSource(id, clusterInfo, cluster);
                    cache.put(id, ds);
                } catch (Throwable t) {
                    String msg = String.format("error when creating cluster datasource: %s", id.getId());
                    LOGGER.error(msg, t);
                    throw new RuntimeException(msg, t);
                }
            }
        }
    }
    return ds;
}
Also used : Cluster(com.ctrip.framework.dal.cluster.client.Cluster) DataSource(javax.sql.DataSource)

Aggregations

Cluster (com.ctrip.framework.dal.cluster.client.Cluster)14 Database (com.ctrip.framework.dal.cluster.client.database.Database)3 Test (org.junit.Test)3 LocalizationConfig (com.ctrip.framework.dal.cluster.client.config.LocalizationConfig)2 ClusterRouteStrategyConfig (com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig)2 ClusterInfo (com.ctrip.platform.dal.dao.configure.ClusterInfo)2 DalRuntimeException (com.ctrip.platform.dal.exceptions.DalRuntimeException)2 DataSource (javax.sql.DataSource)2 ClusterSwitchedEvent (com.ctrip.framework.dal.cluster.client.cluster.ClusterSwitchedEvent)1 ClusterType (com.ctrip.framework.dal.cluster.client.cluster.ClusterType)1 DefaultCluster (com.ctrip.framework.dal.cluster.client.cluster.DefaultCluster)1 RouteStrategyEnum (com.ctrip.framework.dal.cluster.client.cluster.RouteStrategyEnum)1 ClusterConfig (com.ctrip.framework.dal.cluster.client.config.ClusterConfig)1 ClusterConfigProvider (com.ctrip.framework.dal.cluster.client.config.ClusterConfigProvider)1 DalConfigCustomizedOption (com.ctrip.framework.dal.cluster.client.config.DalConfigCustomizedOption)1 DefaultLocalConfigProvider (com.ctrip.framework.dal.cluster.client.config.DefaultLocalConfigProvider)1 DatabaseCategory (com.ctrip.framework.dal.cluster.client.database.DatabaseCategory)1 DatabaseRole (com.ctrip.framework.dal.cluster.client.database.DatabaseRole)1 DatabaseShard (com.ctrip.framework.dal.cluster.client.shard.DatabaseShard)1 CaseInsensitiveProperties (com.ctrip.framework.dal.cluster.client.util.CaseInsensitiveProperties)1