Browse Source

Event cleanup HSQLDB support

pull/4910/head
Igor Kulikov 5 years ago
parent
commit
92f9285024
  1. 22
      dao/src/main/java/org/thingsboard/server/dao/sql/event/EventCleanupRepository.java
  2. 60
      dao/src/main/java/org/thingsboard/server/dao/sql/event/HsqlEventCleanupRepository.java
  3. 18
      dao/src/main/java/org/thingsboard/server/dao/sql/event/JpaBaseEventDao.java
  4. 51
      dao/src/main/java/org/thingsboard/server/dao/sql/event/PsqlEventCleanupRepository.java

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

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

18
dao/src/main/java/org/thingsboard/server/dao/sql/event/JpaBaseEventDao.java

@ -65,6 +65,9 @@ public class JpaBaseEventDao extends JpaAbstractDao<EventEntity, Event> implemen
@Autowired
private EventInsertRepository eventInsertRepository;
@Autowired
private EventCleanupRepository eventCleanupRepository;
@Override
protected Class<EventEntity> getEntityClass() {
return EventEntity.class;
@ -262,20 +265,7 @@ public class JpaBaseEventDao extends JpaAbstractDao<EventEntity, Event> 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<Event> save(EventEntity entity, boolean ifNotExists) {

51
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);
}
}
}
Loading…
Cancel
Save