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);
}
}
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));
}
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;
}
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());
}
}
});
}
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;
}
Aggregations