com.staros.heartbeat.HeartbeatManager Maven / Gradle / Ivy
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.staros.heartbeat;
import com.staros.util.Config;
import com.staros.worker.WorkerManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.concurrent.atomic.AtomicBoolean;
public class HeartbeatManager {
private static final Logger LOG = LogManager.getLogger(HeartbeatManager.class);
private AtomicBoolean running;
private final int heartbeatInterval; // in second
private Thread heartbeatThread; // background thread, TODO: use thread pool
private WorkerManager workerManager;
public HeartbeatManager(WorkerManager workerManager) {
this.running = new AtomicBoolean(false);
int interval = Config.WORKER_HEARTBEAT_INTERVAL_SEC;
if (interval < 1 || interval > 60) {
LOG.warn("worker heartbeat interval {} is not suitable, change it to default 10.", interval);
interval = 10;
}
this.heartbeatInterval = interval;
this.heartbeatThread = new Thread() {
public void run() {
runHeartbeatThread();
}
};
this.workerManager = workerManager;
}
public void start() {
if (!running.compareAndSet(false /* expect */, true /* wanted */)) {
return;
}
heartbeatThread.start();
}
public void stop() {
if (!running.compareAndSet(true /* expect */, false /* wanted */)) {
return;
}
try {
heartbeatThread.interrupt();
heartbeatThread.join();
} catch (InterruptedException e) {
LOG.warn("join heartbeat thread failed! {}", e.getMessage());
}
}
// TODO: send epoch, which should be abtained from star manager ha module
private void runHeartbeatThread() {
while (true) {
if (!running.get()) {
break;
}
LOG.debug("running heartbeat once.");
workerManager.sendHeartbeatForAll();
try {
Thread.sleep(heartbeatInterval * 1000);
} catch (InterruptedException e) {
LOG.warn("heartbeat thread interrupted! {}", e.getMessage());
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy