4 changed files with 137 additions and 14 deletions
@ -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); |
||||
|
|
||||
|
} |
||||
@ -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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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…
Reference in new issue