From 3abe14280896ce5d7556c60e137599f3ca0d12a7 Mon Sep 17 00:00:00 2001 From: wenyh1 <2365151147@qq.com> Date: Mon, 23 Oct 2023 16:56:03 +0800 Subject: [PATCH] [inner-2129] When getKeyVariables, npe occasionally appears in dble.log (cherry picked from commit 5ddd6a8a18b7130cfc8e2a402d6b1c51416202f5) --- .../GetAndSyncDbInstanceKeyVariables.java | 78 ++++++++++--------- 1 file changed, 43 insertions(+), 35 deletions(-) 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 3ee799402..f4ace9bb7 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 class GetAndSyncDbInstanceKeyVariables implements Callable 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,95 @@ public class GetAndSyncDbInstanceKeyVariables implements Callable 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")); + boolean isSuccess = result.isSuccess(); + ds.setTestConnSuccess(isSuccess); + if (isFinish) { + if (fail) + LOGGER.info("test conn timeout, need to resynchronize"); + return; + } + KeyVariables keyVariablesTmp = null; + if (isSuccess) { + keyVariablesTmp = new KeyVariables(); + keyVariablesTmp.setLowerCase(!result.getResult().get(COLUMN_LOWER_CASE).equals("0")); - keyVariables.setAutocommit(result.getResult().get(COLUMN_AUTOCOMMIT).equals("1")); - keyVariables.setTargetAutocommit(SystemConfig.getInstance().getAutocommit() == 1); + keyVariablesTmp.setAutocommit(result.getResult().get(COLUMN_AUTOCOMMIT).equals("1")); + keyVariablesTmp.setTargetAutocommit(SystemConfig.getInstance().getAutocommit() == 1); String isolation = result.getResult().get(columnIsolation); switch (isolation) { case "READ-COMMITTED": - keyVariables.setIsolation(Isolations.READ_COMMITTED); + keyVariablesTmp.setIsolation(Isolations.READ_COMMITTED); break; case "READ-UNCOMMITTED": - keyVariables.setIsolation(Isolations.READ_UNCOMMITTED); + keyVariablesTmp.setIsolation(Isolations.READ_UNCOMMITTED); break; case "REPEATABLE-READ": - keyVariables.setIsolation(Isolations.REPEATABLE_READ); + keyVariablesTmp.setIsolation(Isolations.REPEATABLE_READ); break; case "SERIALIZABLE": - keyVariables.setIsolation(Isolations.SERIALIZABLE); + keyVariablesTmp.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))); + keyVariablesTmp.setTargetIsolation(SystemConfig.getInstance().getTxIsolation()); + keyVariablesTmp.setMaxPacketSize(Integer.parseInt(result.getResult().get(COLUMN_MAX_PACKET))); + keyVariablesTmp.setTargetMaxPacketSize(SystemConfig.getInstance().getMaxPacketSize() + KeyVariables.MARGIN_PACKET_SIZE); + keyVariablesTmp.setReadOnly(result.getResult().get(COLUMN_READONLY).equals("1")); + keyVariablesTmp.setVersion(result.getResult().get(COLUMN_VERSION)); + keyVariablesTmp.setBackLog(Integer.parseInt(result.getResult().get(COLUMN_BACK_LOG))); if (needSync) { boolean checkNeedSync = false; - if (keyVariables.getTargetMaxPacketSize() > keyVariables.getMaxPacketSize()) { + if (keyVariablesTmp.getTargetMaxPacketSize() > keyVariablesTmp.getMaxPacketSize()) { checkNeedSync = true; } - if (keyVariables.isTargetAutocommit() != keyVariables.isAutocommit()) { + if (keyVariablesTmp.isTargetAutocommit() != keyVariablesTmp.isAutocommit()) { checkNeedSync = true; } else { ds.setAutocommitSynced(true); } - if (keyVariables.getTargetIsolation() != keyVariables.getIsolation()) { + if (keyVariablesTmp.getTargetIsolation() != keyVariablesTmp.getIsolation()) { checkNeedSync = true; } else { ds.setIsolationSynced(true); } if (checkNeedSync) { - SyncDbInstanceKeyVariables task = new SyncDbInstanceKeyVariables(keyVariables, ds); + SyncDbInstanceKeyVariables task = new SyncDbInstanceKeyVariables(keyVariablesTmp, ds); boolean synced = false; try { synced = task.call(); } catch (Exception e) { LOGGER.warn("SyncDbInstanceKeyVariables error", e); } + if (synced) { ds.setAutocommitSynced(true); ds.setIsolationSynced(true); - keyVariables.setMaxPacketSize(keyVariables.getTargetMaxPacketSize()); + keyVariablesTmp.setMaxPacketSize(keyVariablesTmp.getTargetMaxPacketSize()); } } } - } else { - ds.setTestConnSuccess(false); } - - handleFinished(); + handleFinished(keyVariablesTmp); } - private void handleFinished() { + private void handleFinished(KeyVariables keyVariablesTmp) { lock.lock(); try { - isFinish = true; - finishCond.signal(); - if (fail) { - keyVariables = null; - LOGGER.info("test conn timeout, need to resynchronize"); + if (isFinish) { + if (fail) + LOGGER.info("test conn timeout, need to resynchronize"); + } else { + keyVariables = keyVariablesTmp; + isFinish = true; } + finishCond.signal(); } finally { lock.unlock(); } } } - }