Commit bfdd52cefdc6bf781b31f99e6990d59383749f8e

Authored by Dmytro Shvaika
1 parent 8ec23f22

fix drop partitions by max ttl procedure

... ... @@ -64,6 +64,7 @@ BEGIN
64 64 AND tablename like 'ts_kv_' || '%'
65 65 AND tablename != 'ts_kv_latest'
66 66 AND tablename != 'ts_kv_dictionary'
  67 + AND tablename != 'ts_kv_indefinite'
67 68 LOOP
68 69 IF partition != partition_by_max_ttl_date THEN
69 70 IF partition_year IS NOT NULL THEN
... ...
... ... @@ -146,6 +146,11 @@ public class ThingsboardInstallService {
146 146 databaseTsUpgradeService.upgradeDatabase("2.5.0");
147 147 }
148 148
  149 + case "2.5.4":
  150 + log.info("Upgrading ThingsBoard from version 2.5.4 to 2.5.5 ...");
  151 + if (databaseTsUpgradeService != null) {
  152 + databaseTsUpgradeService.upgradeDatabase("2.5.4");
  153 + }
149 154
150 155 log.info("Updating system data...");
151 156
... ...
... ... @@ -49,6 +49,7 @@ public class CassandraTsDatabaseUpgradeService extends AbstractCassandraDatabase
49 49 log.info("Schema updated.");
50 50 break;
51 51 case "2.5.0":
  52 + case "2.5.4":
52 53 break;
53 54 default:
54 55 throw new RuntimeException("Unable to upgrade Cassandra database, unsupported fromVersion: " + fromVersion);
... ...
... ... @@ -195,6 +195,12 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe
195 195 executeQuery(conn, "UPDATE tb_schema_settings SET schema_version = 2005001");
196 196 }
197 197 break;
  198 + case "2.5.4":
  199 + try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
  200 + log.info("Load Drop Partitions functions ...");
  201 + loadSql(conn, LOAD_DROP_PARTITIONS_FUNCTIONS_SQL);
  202 + }
  203 + break;
198 204 default:
199 205 throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion);
200 206 }
... ...
... ... @@ -177,6 +177,8 @@ public class TimescaleTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgr
177 177 executeQuery(conn, "UPDATE tb_schema_settings SET schema_version = 2005001");
178 178 }
179 179 break;
  180 + case "2.5.4":
  181 + break;
180 182 default:
181 183 throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion);
182 184 }
... ...
... ... @@ -38,19 +38,15 @@ public abstract class AbstractCleanUpService {
38 38 @Value("${spring.datasource.password}")
39 39 protected String dbPassword;
40 40
41   - protected long executeQuery(Connection conn, String query) {
42   - long removed = 0L;
43   - try {
44   - Statement statement = conn.createStatement();
  41 + protected long executeQuery(Connection conn, String query) throws SQLException {
  42 + try (Statement statement = conn.createStatement()) {
45 43 ResultSet resultSet = statement.executeQuery(query);
46   - getWarnings(statement);
  44 + if (log.isDebugEnabled()) {
  45 + getWarnings(statement);
  46 + }
47 47 resultSet.next();
48   - removed = resultSet.getLong(1);
49   - log.debug("Successfully executed query: {}", query);
50   - } catch (SQLException e) {
51   - log.debug("Failed to execute query: {} due to: {}", query, e.getMessage());
  48 + return resultSet.getLong(1);
52 49 }
53   - return removed;
54 50 }
55 51
56 52 protected void getWarnings(Statement statement) throws SQLException {
... ... @@ -65,6 +61,6 @@ public abstract class AbstractCleanUpService {
65 61 }
66 62 }
67 63
68   - protected abstract void doCleanUp(Connection connection);
  64 + protected abstract void doCleanUp(Connection connection) throws SQLException;
69 65
70 66 }
... ...
... ... @@ -54,7 +54,7 @@ public class EventsCleanUpService extends AbstractCleanUpService {
54 54 }
55 55
56 56 @Override
57   - protected void doCleanUp(Connection connection) {
  57 + protected void doCleanUp(Connection connection) throws SQLException {
58 58 long totalEventsRemoved = executeQuery(connection, "call cleanup_events_by_ttl(" + ttl + ", " + debugTtl + ", 0);");
59 59 log.info("Total events removed by TTL: [{}]", totalEventsRemoved);
60 60 }
... ...
... ... @@ -22,6 +22,7 @@ import org.thingsboard.server.dao.model.ModelConstants;
22 22 import org.thingsboard.server.dao.util.PsqlTsDao;
23 23
24 24 import java.sql.Connection;
  25 +import java.sql.SQLException;
25 26
26 27 @PsqlTsDao
27 28 @Service
... ... @@ -32,7 +33,7 @@ public class PsqlTimeseriesCleanUpService extends AbstractTimeseriesCleanUpServi
32 33 private String partitionType;
33 34
34 35 @Override
35   - protected void doCleanUp(Connection connection) {
  36 + protected void doCleanUp(Connection connection) throws SQLException {
36 37 long totalPartitionsRemoved = executeQuery(connection, "call drop_partitions_by_max_ttl('" + partitionType + "'," + systemTtl + ", 0);");
37 38 log.info("Total partitions removed by TTL: [{}]", totalPartitionsRemoved);
38 39 long totalEntitiesTelemetryRemoved = executeQuery(connection, "call cleanup_timeseries_by_ttl('" + ModelConstants.NULL_UUID_STR + "'," + systemTtl + ", 0);");
... ...
... ... @@ -21,6 +21,7 @@ import org.thingsboard.server.dao.model.ModelConstants;
21 21 import org.thingsboard.server.dao.util.TimescaleDBTsDao;
22 22
23 23 import java.sql.Connection;
  24 +import java.sql.SQLException;
24 25
25 26 @TimescaleDBTsDao
26 27 @Service
... ... @@ -28,7 +29,7 @@ import java.sql.Connection;
28 29 public class TimescaleTimeseriesCleanUpService extends AbstractTimeseriesCleanUpService {
29 30
30 31 @Override
31   - protected void doCleanUp(Connection connection) {
  32 + protected void doCleanUp(Connection connection) throws SQLException {
32 33 long totalEntitiesTelemetryRemoved = executeQuery(connection, "call cleanup_timeseries_by_ttl('" + ModelConstants.NULL_UUID_STR + "'," + systemTtl + ", 0);");
33 34 log.info("Total telemetry removed stats by TTL for entities: [{}]", totalEntitiesTelemetryRemoved);
34 35 }
... ...
... ... @@ -105,6 +105,7 @@ BEGIN
105 105 AND tablename like 'ts_kv_' || '%'
106 106 AND tablename != 'ts_kv_latest'
107 107 AND tablename != 'ts_kv_dictionary'
  108 + AND tablename != 'ts_kv_indefinite'
108 109 LOOP
109 110 IF partition != partition_by_max_ttl_date THEN
110 111 IF partition_year IS NOT NULL THEN
... ...