Unverified Commit 778d482b authored by gaoyan's avatar gaoyan Committed by GitHub

[Feature]add metrics delete on write (#3643)

parent 91f8708e
......@@ -60,7 +60,7 @@ public class MetricsContextHolder {
protected static final MetricsContextHolder instance = new MetricsContextHolder();
private final List<MetricsVO> metricsVOS = new CopyOnWriteArrayList<>();
private final AtomicLong lastDumpTime = new AtomicLong(System.currentTimeMillis());
private final AtomicLong lastDumpTime = new AtomicLong(0);
static {
String sql = String.format(
......
......@@ -36,7 +36,6 @@ import org.dinky.function.constant.PathConstant;
import org.dinky.function.pool.UdfCodePool;
import org.dinky.job.ClearJobHistoryTask;
import org.dinky.job.FlinkJobTask;
import org.dinky.job.SystemMetricsTask;
import org.dinky.resource.BaseResourceManager;
import org.dinky.scheduler.client.ProjectClient;
import org.dinky.scheduler.exception.SchedulerException;
......@@ -54,7 +53,6 @@ import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.springframework.boot.ApplicationArguments;
......@@ -146,23 +144,6 @@ public class SystemInit implements ApplicationRunner {
* init task monitor
*/
private void initDaemon() {
SystemConfiguration sysConfig = SystemConfiguration.getInstances();
// Init system metrics task
DaemonTask sysMetricsTask = DaemonTask.build(new DaemonTaskConfig(SystemMetricsTask.TYPE));
Configuration<Boolean> metricsSysEnable = sysConfig.getMetricsSysEnable();
Configuration<Integer> sysGatherTiming = sysConfig.getMetricsSysGatherTiming();
Consumer<Configuration<?>> metricsListener = c -> {
c.addChangeEvent(x -> {
schedule.removeSchedule(sysMetricsTask.getType());
PeriodicTrigger trigger = new PeriodicTrigger(sysGatherTiming.getValue());
if (metricsSysEnable.getValue()) schedule.addSchedule(sysMetricsTask, trigger);
});
};
metricsListener.accept(metricsSysEnable);
metricsListener.accept(sysGatherTiming);
metricsSysEnable.runChangeEvent();
// Init clear job history task
DaemonTask clearJobHistoryTask = DaemonTask.build(new DaemonTaskConfig(ClearJobHistoryTask.TYPE));
schedule.addSchedule(clearJobHistoryTask, new PeriodicTrigger(1, TimeUnit.HOURS));
......
......@@ -26,6 +26,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import lombok.extern.slf4j.Slf4j;
......@@ -34,16 +35,18 @@ public enum SqliteUtil {
INSTANCE;
private Connection connection;
private final AtomicLong lastRecyle = new AtomicLong(0);
static {
try {
SqliteUtil.INSTANCE.connect("dinky.db");
SqliteUtil.INSTANCE.recyleData();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void connect(String dbPath) throws SQLException {
private void connect(String dbPath) throws SQLException {
connection = DriverManager.getConnection("jdbc:sqlite:" + dbPath);
}
......@@ -57,11 +60,24 @@ public enum SqliteUtil {
}
}
public void write(String tableName, String columns, String values) throws SQLException {
String sql = String.format("INSERT INTO %s (%s) VALUES (%s);", tableName, columns, values);
public void executeSql(String sql) throws SQLException {
Statement pstmt = connection.createStatement();
pstmt.executeUpdate(sql);
connection.commit();
}
try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.executeUpdate();
public void recyleData() {
long now = System.currentTimeMillis();
if (now - lastRecyle.get() < 1000 * 60 * 60) {
return;
}
lastRecyle.set(now);
try {
String sql = "DELETE FROM dinky_metrics WHERE heart_time <= datetime('now', '-7 days')";
executeSql(sql);
executeSql("VACUUM");
} catch (SQLException e) {
log.error("Failed to recyle database: " + e.getMessage());
}
}
......@@ -81,6 +97,7 @@ public enum SqliteUtil {
} catch (SQLException e) {
log.error("Failed to write to SQLite: " + e.getMessage());
}
recyleData();
}
private static String createInsertSql(String tableName, List<String> columns) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment