Skip to content
Permalink
Browse files

AbstractQueryProtocol#isValid optimize avoid spliting galeraAllowable…

…State values every call

(cherry picked from commit 4316936)
  • Loading branch information...
fgaule authored and rusher committed Aug 23, 2018
1 parent 50a06ae commit a9482f133a369e670ff65a65473920458b286b69
Showing with 22 additions and 25 deletions.
  1. +22 −25 src/main/java/org/mariadb/jdbc/internal/protocol/AbstractQueryProtocol.java
@@ -92,10 +92,7 @@
import java.net.URL;
import java.nio.charset.Charset;
import java.sql.*;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.ServiceLoader;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadPoolExecutor;
@@ -104,17 +101,20 @@
import static org.mariadb.jdbc.internal.com.Packet.*;
import static org.mariadb.jdbc.internal.util.SqlStates.*;


public class AbstractQueryProtocol extends AbstractConnectProtocol implements Protocol {
protected static ThreadPoolExecutor readScheduler = null;

private static final Logger logger = LoggerFactory.getLogger(AbstractQueryProtocol.class);
private static final String CHECK_GALERA_STATE_QUERY = "show status like 'wsrep_local_state'";
protected static ThreadPoolExecutor readScheduler = null;

private int transactionIsolationLevel = 0;
private InputStream localInfileInputStream;
private long maxRows; /* max rows returned by a statement */
private volatile int statementIdToRelease = -1;
private FutureTask activeFutureTask = null;
private final LogQueryTool logQuery;
private boolean interrupted;
private final Set<String> galeraAllowedStates;

/**
* Get a protocol instance.
@@ -126,6 +126,9 @@
AbstractQueryProtocol(final UrlParser urlParser, final GlobalStateInfo globalInfo, final ReentrantLock lock) {
super(urlParser, globalInfo, lock);
logQuery = new LogQueryTool(options);
galeraAllowedStates = urlParser.getOptions().galeraAllowedState == null ?
new HashSet<String>() :
new HashSet<String>(Arrays.asList(urlParser.getOptions().galeraAllowedState.split(",")));
}

/**
@@ -360,15 +363,15 @@ public boolean executeBatchClient(boolean mustExecuteOnMaster, Results results,
/**
* Execute clientPrepareQuery batch.
*
* @param results results
* @param sql sql command
* @param serverPrepareResult prepare result if exist
* @param parametersList List of parameters
* @param results results
* @param sql sql command
* @param serverPrepareResult prepare result if exist
* @param parametersList List of parameters
* @return if executed
* @throws SQLException exception
*/
private boolean executeBulkBatch(Results results, String sql, ServerPrepareResult serverPrepareResult,
final List<ParameterHolder[]> parametersList) throws SQLException {
final List<ParameterHolder[]> parametersList) throws SQLException {

//**************************************************************************************
// Ensure BULK can be use :
@@ -542,7 +545,7 @@ private void initializeBatchReader() {
* @throws SQLException exception
*/
private void executeBatchMulti(Results results, final ClientPrepareResult clientPrepareResult,
final List<ParameterHolder[]> parametersList) throws SQLException {
final List<ParameterHolder[]> parametersList) throws SQLException {

cmdPrologue();
initializeBatchReader();
@@ -560,7 +563,6 @@ public void sendCmd(PacketOutputStream writer, Results results,
writer.flush();
}


@Override
public SQLException handleResultException(SQLException qex, Results results,
List<ParameterHolder[]> parametersList, List<String> queries, int currentCounter,
@@ -578,13 +580,11 @@ public SQLException handleResultException(SQLException qex, Results results,
return logQuery.exceptionWithQuery(sql.toString(), qex, explicitClosed);
}


@Override
public int getParamCount() {
return clientPrepareResult.getQueryParts().size() - 1;
}


@Override
public int getTotalExecutionNumber() {
return parametersList.size();
@@ -1081,37 +1081,36 @@ public boolean isValid(int timeout) throws SQLException {
int initialTimeout = -1;
try {
initialTimeout = socket.getSoTimeout();
if (initialTimeout == 0) socket.setSoTimeout(timeout);
if (isMasterConnection() && urlParser.getOptions().galeraAllowedState != null) {
if (initialTimeout == 0) { socket.setSoTimeout(timeout); }
if (isMasterConnection() && !galeraAllowedStates.isEmpty()) {
//this is a galera node.
//checking not only that node is responding, but that galera state is allowed.
Results results = new Results();
executeQuery(true, results, "show status like 'wsrep_local_state'");
executeQuery(true, results, CHECK_GALERA_STATE_QUERY);
results.commandEnd();
ResultSet rs = results.getResultSet();
if (rs == null || !rs.next()) return false;
String[] allowedValues = urlParser.getOptions().galeraAllowedState.split(",");
return Arrays.asList(allowedValues).contains(rs.getString(2)); //SYNC mode
return rs != null && rs.next() && galeraAllowedStates.contains(rs.getString(2));
}

return ping();

} catch (SocketException socketException) {
logger.trace("Connection is not valid", socketException);
connected = false;
return false;
} finally {

//set back initial socket timeout
try {
if (initialTimeout != -1) socket.setSoTimeout(initialTimeout);
if (initialTimeout != -1) { socket.setSoTimeout(initialTimeout); }
} catch (SocketException socketException) {
logger.warn("Could not set socket timeout back to " + initialTimeout, socketException);
connected = false;
//eat
}
}
}


@Override
public String getCatalog() throws SQLException {

@@ -1463,7 +1462,6 @@ public int getAutoIncrementIncrement() throws SQLException {
return autoIncrementIncrement;
}


/**
* Read ERR_Packet.
*
@@ -1674,7 +1672,6 @@ public ServerPrepareResult addPrepareInCache(String key, ServerPrepareResult ser
return serverPrepareStatementCache.put(key, serverPrepareResult);
}


private void cmdPrologue() throws SQLException {

//load active result if any so buffer are clean for next query

0 comments on commit a9482f1

Please sign in to comment.
You can’t perform that action at this time.