Search in sources :

Example 11 with Cluster

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

the class ClusterConfigImpl method generate.

@Override
public Cluster generate() {
    Cluster cluster = generatedClusterRef.get();
    if (cluster == null) {
        synchronized (generatedClusterRef) {
            cluster = generatedClusterRef.get();
            if (cluster == null) {
                cluster = innerGenerate();
                generatedClusterRef.set(cluster);
            }
        }
    }
    return cluster;
}
Also used : Cluster(com.ctrip.framework.dal.cluster.client.Cluster)

Example 12 with Cluster

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

the class DalConfigureFactory method readClusters.

private Map<String, DatabaseSet> readClusters(Node databaseSetsNode, DalConnectionLocator locator, DalConfigCustomizedOption defaultOption) throws Exception {
    Map<String, DatabaseSet> databaseSets = new HashMap<>();
    ClusterManager clusterManager = new ClusterManagerImpl(locator.getIntegratedConfigProvider());
    List<Node> clusterList = getChildNodes(databaseSetsNode, CLUSTER);
    for (Node node : clusterList) {
        DalConfigCustomizedOption option = defaultOption.clone();
        String name = getDatabaseSetName(node);
        overrideDefaultConfig(node, option);
        Cluster cluster = readCluster(node, clusterManager, option);
        databaseSets.put(name, new ClusterDatabaseSet(name, cluster, locator, getSettings(node)));
    }
    return databaseSets;
}
Also used : DalConfigCustomizedOption(com.ctrip.framework.dal.cluster.client.config.DalConfigCustomizedOption) HashMap(java.util.HashMap) ClusterManagerImpl(com.ctrip.platform.dal.dao.cluster.ClusterManagerImpl) Cluster(com.ctrip.framework.dal.cluster.client.Cluster) ClusterManager(com.ctrip.platform.dal.dao.cluster.ClusterManager)

Example 13 with Cluster

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

the class ClusterManagerImpl method getOrCreateCluster.

@Override
public Cluster getOrCreateCluster(String clusterName, DalConfigCustomizedOption customizedOption) {
    if (StringUtils.isEmpty(clusterName))
        throw new DalRuntimeException("cluster name is empty");
    clusterName = StringUtils.toTrimmedLowerCase(clusterName);
    Cluster cluster = clusters.get(clusterName);
    if (cluster == null)
        synchronized (clusters) {
            cluster = clusters.get(clusterName);
            if (cluster == null) {
                cluster = createCluster(clusterName, customizedOption);
                clusters.put(clusterName, cluster);
            }
        }
    return cluster;
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) Cluster(com.ctrip.framework.dal.cluster.client.Cluster)

Example 14 with Cluster

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

the class DataSourceLocator method getDataSource.

public DataSource getDataSource(DataSourceIdentity id) {
    DataSource ds = cache.get(id);
    if (ds == null) {
        synchronized (cache) {
            ds = cache.get(id);
            if (ds == null) {
                try {
                    if (id instanceof ClusterInfoDelegateIdentity) {
                        ClusterInfo clusterInfo = ((ClusterInfoDelegateIdentity) id).getClusterInfo();
                        Cluster cluster = clusterInfo.getCluster();
                        if (cluster == null)
                            throw new RuntimeException("Cluster not created");
                        ds = createDataSource(id, clusterInfo, cluster);
                    } else
                        ds = createDataSource(id);
                    cache.put(id, ds);
                } catch (Throwable t) {
                    String msg = String.format("error when creating 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