From 1625c339ff49f5a90c24028bf450781e28f5f4e7 Mon Sep 17 00:00:00 2001 From: ylinzhu <46126022+ylinzhu@users.noreply.github.com> Date: Fri, 19 Aug 2022 09:47:32 +0800 Subject: [PATCH] =?UTF-8?q?fix=20heartbeat=20timeout=20inner=201371=20?= =?UTF-8?q?=EF=BC=88cherry=20pick=EF=BC=89=20(#3369)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../datasource/PhysicalDbInstance.java | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/actiontech/dble/backend/datasource/PhysicalDbInstance.java b/src/main/java/com/actiontech/dble/backend/datasource/PhysicalDbInstance.java index 6f5caaf55..809656971 100644 --- a/src/main/java/com/actiontech/dble/backend/datasource/PhysicalDbInstance.java +++ b/src/main/java/com/actiontech/dble/backend/datasource/PhysicalDbInstance.java @@ -48,6 +48,8 @@ public abstract class PhysicalDbInstance { private final LongAdder writeCount = new LongAdder(); private final AtomicBoolean isInitial = new AtomicBoolean(false); + private AtomicBoolean initHeartbeat = new AtomicBoolean(false); + // connection pool private ConnectionPool connectionPool; @@ -340,15 +342,19 @@ public abstract class PhysicalDbInstance { } heartbeat.start(); - heartbeat.setScheduledFuture(Scheduler.getInstance().getScheduledExecutor().scheduleAtFixedRate(() -> { - if (DbleServer.getInstance().getConfig().isFullyConfigured()) { - if (TimeUtil.currentTimeMillis() < heartbeatRecoveryTime) { - return; - } + if (initHeartbeat.compareAndSet(false, true)) { + heartbeat.setScheduledFuture(Scheduler.getInstance().getScheduledExecutor().scheduleAtFixedRate(() -> { + if (DbleServer.getInstance().getConfig().isFullyConfigured()) { + if (TimeUtil.currentTimeMillis() < heartbeatRecoveryTime) { + return; + } - heartbeat.heartbeat(); - } - }, 0L, config.getPoolConfig().getHeartbeatPeriodMillis(), MILLISECONDS)); + heartbeat.heartbeat(); + } + }, 0L, config.getPoolConfig().getHeartbeatPeriodMillis(), MILLISECONDS)); + } else { + LOGGER.warn("init dbInstance[{}] heartbeat, but it has been initialized, skip initialization.", heartbeat.getSource().getName()); + } } public void start(String reason) { @@ -361,6 +367,9 @@ public abstract class PhysicalDbInstance { public void stop(String reason, boolean closeFront) { heartbeat.stop(reason); + if (!heartbeat.isStop()) { + initHeartbeat.set(false); + } if (dbGroupConfig.getRwSplitMode() != RW_SPLIT_OFF || dbGroup.getWriteDbInstance() == this) { LOGGER.info("stop connection pool of physical db instance[{}], due to {}", name, reason); connectionPool.stop(reason, closeFront);