diff --git a/src/main/java/com/actiontech/dble/config/helper/GetAndSyncDbInstanceKeyVariables.java b/src/main/java/com/actiontech/dble/config/helper/GetAndSyncDbInstanceKeyVariables.java index 3ee799402f..3f35ded146 100644 --- a/src/main/java/com/actiontech/dble/config/helper/GetAndSyncDbInstanceKeyVariables.java +++ b/src/main/java/com/actiontech/dble/config/helper/GetAndSyncDbInstanceKeyVariables.java @@ -69,9 +69,11 @@ public KeyVariables call() { while (!isFinish) { boolean await = finishCond.await(ds.getConfig().getPoolConfig().getHeartbeatPeriodMillis(), TimeUnit.MILLISECONDS); if (!await) { - fail = true; - isFinish = true; - LOGGER.warn("test conn timeout,TCP connection may be lost"); + if (!isFinish) { + fail = true; + isFinish = true; + LOGGER.warn("test conn timeout,TCP connection may be lost"); + } } } } catch (InterruptedException e) { @@ -86,89 +88,85 @@ public KeyVariables call() { private class GetDbInstanceKeyVariablesListener implements SQLQueryResultListener>> { @Override public void onResult(SQLQueryResult> result) { - if (result.isSuccess()) { - ds.setTestConnSuccess(true); - keyVariables = new KeyVariables(); - keyVariables.setLowerCase(!result.getResult().get(COLUMN_LOWER_CASE).equals("0")); - - keyVariables.setAutocommit(result.getResult().get(COLUMN_AUTOCOMMIT).equals("1")); - keyVariables.setTargetAutocommit(SystemConfig.getInstance().getAutocommit() == 1); - - String isolation = result.getResult().get(columnIsolation); - switch (isolation) { - case "READ-COMMITTED": - keyVariables.setIsolation(Isolations.READ_COMMITTED); - break; - case "READ-UNCOMMITTED": - keyVariables.setIsolation(Isolations.READ_UNCOMMITTED); - break; - case "REPEATABLE-READ": - keyVariables.setIsolation(Isolations.REPEATABLE_READ); - break; - case "SERIALIZABLE": - keyVariables.setIsolation(Isolations.SERIALIZABLE); - break; - default: - break; + boolean isSuccess = result.isSuccess(); + ds.setTestConnSuccess(isSuccess); + lock.lock(); + try { + if (isFinish) { + if (fail) + LOGGER.info("test conn timeout, need to resynchronize"); + return; } - keyVariables.setTargetIsolation(SystemConfig.getInstance().getTxIsolation()); - keyVariables.setMaxPacketSize(Integer.parseInt(result.getResult().get(COLUMN_MAX_PACKET))); - keyVariables.setTargetMaxPacketSize(SystemConfig.getInstance().getMaxPacketSize() + KeyVariables.MARGIN_PACKET_SIZE); - keyVariables.setReadOnly(result.getResult().get(COLUMN_READONLY).equals("1")); - keyVariables.setVersion(result.getResult().get(COLUMN_VERSION)); - keyVariables.setBackLog(Integer.parseInt(result.getResult().get(COLUMN_BACK_LOG))); + if (isSuccess) { + ds.setTestConnSuccess(true); + keyVariables = new KeyVariables(); + keyVariables.setLowerCase(!result.getResult().get(COLUMN_LOWER_CASE).equals("0")); - if (needSync) { - boolean checkNeedSync = false; - if (keyVariables.getTargetMaxPacketSize() > keyVariables.getMaxPacketSize()) { - checkNeedSync = true; - } - if (keyVariables.isTargetAutocommit() != keyVariables.isAutocommit()) { - checkNeedSync = true; - } else { - ds.setAutocommitSynced(true); - } - if (keyVariables.getTargetIsolation() != keyVariables.getIsolation()) { - checkNeedSync = true; - } else { - ds.setIsolationSynced(true); + keyVariables.setAutocommit(result.getResult().get(COLUMN_AUTOCOMMIT).equals("1")); + keyVariables.setTargetAutocommit(SystemConfig.getInstance().getAutocommit() == 1); + + String isolation = result.getResult().get(columnIsolation); + switch (isolation) { + case "READ-COMMITTED": + keyVariables.setIsolation(Isolations.READ_COMMITTED); + break; + case "READ-UNCOMMITTED": + keyVariables.setIsolation(Isolations.READ_UNCOMMITTED); + break; + case "REPEATABLE-READ": + keyVariables.setIsolation(Isolations.REPEATABLE_READ); + break; + case "SERIALIZABLE": + keyVariables.setIsolation(Isolations.SERIALIZABLE); + break; + default: + break; } + keyVariables.setTargetIsolation(SystemConfig.getInstance().getTxIsolation()); + keyVariables.setMaxPacketSize(Integer.parseInt(result.getResult().get(COLUMN_MAX_PACKET))); + keyVariables.setTargetMaxPacketSize(SystemConfig.getInstance().getMaxPacketSize() + KeyVariables.MARGIN_PACKET_SIZE); + keyVariables.setReadOnly(result.getResult().get(COLUMN_READONLY).equals("1")); + keyVariables.setVersion(result.getResult().get(COLUMN_VERSION)); + keyVariables.setBackLog(Integer.parseInt(result.getResult().get(COLUMN_BACK_LOG))); - if (checkNeedSync) { - SyncDbInstanceKeyVariables task = new SyncDbInstanceKeyVariables(keyVariables, ds); - boolean synced = false; - try { - synced = task.call(); - } catch (Exception e) { - LOGGER.warn("SyncDbInstanceKeyVariables error", e); + if (needSync) { + boolean checkNeedSync = false; + if (keyVariables.getTargetMaxPacketSize() > keyVariables.getMaxPacketSize()) { + checkNeedSync = true; } - if (synced) { + if (keyVariables.isTargetAutocommit() != keyVariables.isAutocommit()) { + checkNeedSync = true; + } else { ds.setAutocommitSynced(true); + } + if (keyVariables.getTargetIsolation() != keyVariables.getIsolation()) { + checkNeedSync = true; + } else { ds.setIsolationSynced(true); - keyVariables.setMaxPacketSize(keyVariables.getTargetMaxPacketSize()); } - } - } - } else { - ds.setTestConnSuccess(false); - } - handleFinished(); - } + if (checkNeedSync) { + SyncDbInstanceKeyVariables task = new SyncDbInstanceKeyVariables(keyVariables, ds); + boolean synced = false; + try { + synced = task.call(); + } catch (Exception e) { + LOGGER.warn("SyncDbInstanceKeyVariables error", e); + } - private void handleFinished() { - lock.lock(); - try { + if (synced) { + ds.setAutocommitSynced(true); + ds.setIsolationSynced(true); + keyVariables.setMaxPacketSize(keyVariables.getTargetMaxPacketSize()); + } + } + } + } isFinish = true; finishCond.signal(); - if (fail) { - keyVariables = null; - LOGGER.info("test conn timeout, need to resynchronize"); - } } finally { lock.unlock(); } } } - }