From 92f9285024096059493da100ccf87b179ccab53a Mon Sep 17 00:00:00 2001 From: Igor Kulikov Date: Wed, 14 Jul 2021 17:05:42 +0300 Subject: [PATCH] Event cleanup HSQLDB support --- .../dao/sql/event/EventCleanupRepository.java | 22 +++++++ .../sql/event/HsqlEventCleanupRepository.java | 60 +++++++++++++++++++ .../server/dao/sql/event/JpaBaseEventDao.java | 18 ++---- .../sql/event/PsqlEventCleanupRepository.java | 51 ++++++++++++++++ 4 files changed, 137 insertions(+), 14 deletions(-) create mode 100644 dao/src/main/java/org/thingsboard/server/dao/sql/event/EventCleanupRepository.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/sql/event/HsqlEventCleanupRepository.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/sql/event/PsqlEventCleanupRepository.java diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/event/EventCleanupRepository.java b/dao/src/main/java/org/thingsboard/server/dao/sql/event/EventCleanupRepository.java new file mode 100644 index 0000000000..7554ceed84 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/event/EventCleanupRepository.java @@ -0,0 +1,22 @@ +/** + * Copyright © 2016-2021 The Thingsboard Authors + * + * 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 + * + * http://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 org.thingsboard.server.dao.sql.event; + +public interface EventCleanupRepository { + + void cleanupEvents(long otherEventsTtl, long debugEventsTtl); + +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/event/HsqlEventCleanupRepository.java b/dao/src/main/java/org/thingsboard/server/dao/sql/event/HsqlEventCleanupRepository.java new file mode 100644 index 0000000000..1ae4d03262 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/event/HsqlEventCleanupRepository.java @@ -0,0 +1,60 @@ +/** + * Copyright © 2016-2021 The Thingsboard Authors + * + * 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 + * + * http://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 org.thingsboard.server.dao.sql.event; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Repository; +import org.thingsboard.server.dao.sql.JpaAbstractDaoListeningExecutorService; +import org.thingsboard.server.dao.util.HsqlDao; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +@Slf4j +@HsqlDao +@Repository +public class HsqlEventCleanupRepository extends JpaAbstractDaoListeningExecutorService implements EventCleanupRepository { + + @Override + public void cleanupEvents(long otherEventsTtl, long debugEventsTtl) { + long otherExpirationTime = System.currentTimeMillis() - otherEventsTtl * 1000; + try (Connection connection = dataSource.getConnection(); + PreparedStatement stmt = connection.prepareStatement("DELETE FROM event WHERE ts < ? AND event_type != 'DEBUG_RULE_NODE' AND event_type != 'DEBUG_RULE_CHAIN'")) { + stmt.setLong(1, otherExpirationTime); + stmt.execute(); + try (ResultSet resultSet = stmt.getResultSet()){ + resultSet.next(); + log.info("Events removed by ttl: [{}]", resultSet.getLong(1)); + } + } catch (SQLException e) { + log.error("SQLException occurred during events TTL task execution ", e); + } + long debugExpirationTime = System.currentTimeMillis() - debugEventsTtl * 1000; + try (Connection connection = dataSource.getConnection(); + PreparedStatement stmt = connection.prepareStatement("DELETE FROM event WHERE ts < ? AND (event_type = 'DEBUG_RULE_NODE' OR event_type = 'DEBUG_RULE_CHAIN')")) { + stmt.setLong(1, debugExpirationTime); + stmt.execute(); + try (ResultSet resultSet = stmt.getResultSet()){ + resultSet.next(); + log.info("Debug Events removed by ttl: [{}]", resultSet.getLong(1)); + } + } catch (SQLException e) { + log.error("SQLException occurred during events TTL task execution ", e); + } + } +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/event/JpaBaseEventDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/event/JpaBaseEventDao.java index ee09bb7a90..c35247ad21 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/event/JpaBaseEventDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/event/JpaBaseEventDao.java @@ -65,6 +65,9 @@ public class JpaBaseEventDao extends JpaAbstractDao implemen @Autowired private EventInsertRepository eventInsertRepository; + @Autowired + private EventCleanupRepository eventCleanupRepository; + @Override protected Class getEntityClass() { return EventEntity.class; @@ -262,20 +265,7 @@ public class JpaBaseEventDao extends JpaAbstractDao implemen @Override public void cleanupEvents(long otherEventsTtl, long debugEventsTtl) { log.info("Going to cleanup old events using debug events ttl: {}s and other events ttl: {}s", debugEventsTtl, otherEventsTtl); - try (Connection connection = dataSource.getConnection(); - PreparedStatement stmt = connection.prepareStatement("call cleanup_events_by_ttl(?,?,?)")) { - stmt.setLong(1, otherEventsTtl); - stmt.setLong(2, debugEventsTtl); - stmt.setLong(3, 0); - stmt.execute(); - printWarnings(stmt); - try (ResultSet resultSet = stmt.getResultSet()){ - resultSet.next(); - log.info("Total events removed by TTL: [{}]", resultSet.getLong(1)); - } - } catch (SQLException e) { - log.error("SQLException occurred during events TTL task execution ", e); - } + eventCleanupRepository.cleanupEvents(otherEventsTtl, debugEventsTtl); } public Optional save(EventEntity entity, boolean ifNotExists) { diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/event/PsqlEventCleanupRepository.java b/dao/src/main/java/org/thingsboard/server/dao/sql/event/PsqlEventCleanupRepository.java new file mode 100644 index 0000000000..b19c8b0712 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/event/PsqlEventCleanupRepository.java @@ -0,0 +1,51 @@ +/** + * Copyright © 2016-2021 The Thingsboard Authors + * + * 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 + * + * http://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 org.thingsboard.server.dao.sql.event; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Repository; +import org.thingsboard.server.dao.sql.JpaAbstractDaoListeningExecutorService; +import org.thingsboard.server.dao.util.PsqlDao; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +@Slf4j +@PsqlDao +@Repository +public class PsqlEventCleanupRepository extends JpaAbstractDaoListeningExecutorService implements EventCleanupRepository { + + @Override + public void cleanupEvents(long otherEventsTtl, long debugEventsTtl) { + try (Connection connection = dataSource.getConnection(); + PreparedStatement stmt = connection.prepareStatement("call cleanup_events_by_ttl(?,?,?)")) { + stmt.setLong(1, otherEventsTtl); + stmt.setLong(2, debugEventsTtl); + stmt.setLong(3, 0); + stmt.execute(); + printWarnings(stmt); + try (ResultSet resultSet = stmt.getResultSet()){ + resultSet.next(); + log.info("Total events removed by TTL: [{}]", resultSet.getLong(1)); + } + } catch (SQLException e) { + log.error("SQLException occurred during events TTL task execution ", e); + } + } + +}