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);