Skip to content

Commit

Permalink
discard read-only connection
Browse files Browse the repository at this point in the history
  • Loading branch information
c7ch23en committed May 25, 2020
1 parent af65699 commit 2e34111
Show file tree
Hide file tree
Showing 20 changed files with 1,193 additions and 267 deletions.
2 changes: 1 addition & 1 deletion dal-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.ctrip.platform</groupId>
<artifactId>dal-client-parent</artifactId>
<version>2.0.19-SNAPSHOT</version>
<version>2.0.19</version>
</parent>
<artifactId>dal-client</artifactId>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public enum DatabaseCategory {
MySql("%s=IFNULL(?,%s) ", "CURRENT_TIMESTAMP", new int[] {1043, 1159, 1161},
new int[] {1021, 1037, 1038, 1039, 1040, 1041, 1154, 1158, 1160, 1189, 1190, 1205, 1218, 1219, 1220}) {

private static final String READONLY_EXCEPTION_SQL_STATE = "HY000";
private static final int READONLY_EXCEPTION_ERROR_CODE = 1290;
private static final String READONLY_EXCEPTION_MESSAGE_KEYWORD = "--read-only";

private AbstractDalPropertiesLocator mySqlDalPropertiesLocator =
DalPropertiesManager.getInstance().getMySqlDalPropertiesLocator();

Expand Down Expand Up @@ -54,7 +58,16 @@ public String buildPage(String selectSqlTemplate, int start, int count) {
// SQLError.SQL_STATE_COMMUNICATION_LINK_FAILURE
public boolean isSpecificException(SQLException exception) {
Map<String, ErrorCodeInfo> map = mySqlDalPropertiesLocator.getErrorCodes();
return matchSpecificError(exception, map);
return matchSpecificError(exception, map) || isReadonlyException(exception);
}

private boolean isReadonlyException(SQLException exception) {
if (exception != null && READONLY_EXCEPTION_SQL_STATE.equalsIgnoreCase(exception.getSQLState()) &&
READONLY_EXCEPTION_ERROR_CODE == exception.getErrorCode()) {
String message = exception.getMessage();
return message != null && message.toLowerCase().contains(READONLY_EXCEPTION_MESSAGE_KEYWORD);
}
return false;
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.Set;

import com.ctrip.framework.dal.cluster.client.Cluster;
import com.ctrip.platform.dal.common.enums.ShardingCategory;
import com.ctrip.platform.dal.common.enums.TableParseSwitch;
import com.ctrip.platform.dal.dao.DalClientFactory;
Expand All @@ -14,6 +15,8 @@
import com.ctrip.platform.dal.dao.DalHints;
import com.ctrip.platform.dal.dao.StatementParameters;
import com.ctrip.platform.dal.dao.Version;
import com.ctrip.platform.dal.dao.configure.ClusterDatabaseSet;
import com.ctrip.platform.dal.dao.configure.DatabaseSet;
import com.ctrip.platform.dal.dao.configure.dalproperties.DalPropertiesLocator;
import com.ctrip.platform.dal.dao.configure.dalproperties.DalPropertiesManager;
import com.ctrip.platform.dal.dao.task.DalContextConfigure;
Expand Down Expand Up @@ -156,6 +159,11 @@ public void populateDbMeta() {

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.setClusterName(cluster.getClusterName().toLowerCase());
}
entry.setLogicDbName(logicDbName);
entry.setDbCategory(DalClientFactory.getDalConfigure().getDatabaseSet(logicDbName).getDatabaseCategory());
entry.setClientVersion(Version.getVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class DalConnection {
private String shardId;
private DbMeta meta;
private DalLogger logger;
private boolean needDiscard;

public DalConnection(Connection conn, boolean master, String shardId, DbMeta meta) throws SQLException {
this.oldIsolationLevel = conn.getTransactionIsolation();
Expand Down Expand Up @@ -66,7 +65,6 @@ public void applyHints(DalHints hints) throws SQLException {
}

public void error(Throwable e) {
needDiscard |= isSpecificException(e);
}

public void close() {
Expand All @@ -85,41 +83,11 @@ public void close() {
}

try {
if (needDiscard) {
markDiscard(conn);
}

conn.close();
} catch (Throwable e) {
logger.error("Close connection failed!", e);
}
conn = null;
}

private boolean isSpecificException(Throwable e) {
// Filter wrapping exception
while (e != null && e instanceof DalException) {
e = e.getCause();
}

while (e != null && !(e instanceof SQLException)) {
e = e.getCause();
}

if (e == null)
return false;

DatabaseCategory dbCategory = meta.getDatabaseCategory();
SQLException se = (SQLException) e;
if (dbCategory.isSpecificException(se))
return true;

return isSpecificException(se.getNextException());
}

private void markDiscard(Connection conn) throws SQLException {
PooledConnection pConn = (PooledConnection) conn.unwrap(PooledConnection.class);
pConn.setDiscarded(true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.ctrip.platform.dal.dao.configure.DataSourceConfigureLocatorManager;
import com.ctrip.platform.dal.dao.datasource.DataSourceIdentity;
import com.ctrip.platform.dal.dao.datasource.DataSourceName;
import com.ctrip.platform.dal.dao.datasource.jdbc.ClusterDatabaseMetaData;
import com.ctrip.platform.dal.dao.helper.DalElementFactory;
import com.ctrip.platform.dal.dao.helper.LoggerHelper;
import com.ctrip.platform.dal.dao.log.ILogger;
Expand Down Expand Up @@ -60,6 +61,7 @@ private void init(Connection conn, DataSourceIdentity id, DatabaseCategory dbCat
} catch (Throwable e) {
ilogger.error(e.getMessage(),e);
}

}

public void populate(LogEntry entry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class LogEntry implements ILogEntry{
private boolean transactional;
private long duration;
private String logicDbName;
private String clusterName;
private DatabaseCategory dbCategory;
private String databaseName;
private String dataBaseKeyName;
Expand Down Expand Up @@ -169,7 +170,15 @@ public void setLogicDbName(String logicDbName) {
this.logicDbName = logicDbName;
}

public DatabaseCategory getDbCategory() {
public String getClusterName() {
return clusterName;
}

public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}

public DatabaseCategory getDbCategory() {
return dbCategory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class DynamicCluster extends ListenableSupport<ClusterSwitchedEvent> implements Cluster {

private static final ILogger LOGGER = DalElementFactory.DEFAULT.getILogger();
private static final String CAT_LOG_TYPE = "DAL.cluster";
private static final String CAT_LOG_TYPE = "DAL.configure";
private static final String CAT_LOG_NAME_FORMAT = "SwitchCluster:%s";
private static final String CAT_EVENT_NAME_NORMAL_TO_DRC = "NormalToDrc:%s";
private static final String CAT_EVENT_NAME_DRC_TO_NORMAL = "DrcToNormal:%s";
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.ctrip.platform.dal.dao.datasource;

import com.ctrip.framework.dal.cluster.client.config.LocalizationConfig;
import com.ctrip.platform.dal.dao.datasource.jdbc.DalDatabaseMetaData;

import java.sql.*;

/**
* @author c7ch23en
*/
public class LocalizedDatabaseMetaDataImpl implements LocalizedDatabaseMetaData {
public class LocalizedDatabaseMetaDataImpl implements DalDatabaseMetaData {

private LocalizationConfig config;
private DatabaseMetaData metaData;

public LocalizedDatabaseMetaDataImpl(DatabaseMetaData metaData) {
this(null, metaData);
}
private final LocalizationConfig config;
private final DatabaseMetaData metaData;

public LocalizedDatabaseMetaDataImpl(LocalizationConfig config, DatabaseMetaData metaData) {
this.config = config;
Expand All @@ -37,7 +34,7 @@ public String getURL() throws SQLException {
}

@Override
public String getLocalizedURL() throws SQLException {
public String getExtendedURL() throws SQLException {
String url = getURL();
if (config != null && config.getZoneId() != null) {
url = url + "::" + config.getZoneId().toUpperCase();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.ctrip.platform.dal.dao.datasource.jdbc;

/**
* @author c7ch23en
*/
public interface ClusterDatabaseMetaData extends DalDatabaseMetaData {

String getClusterName();

int getShardIndex();

boolean isMaster();

}
Loading

0 comments on commit 2e34111

Please sign in to comment.