Commit f647c69f509db991039f7b0dd7a990c7fa860f2c

Authored by Dmytro Shvaika
Committed by Andrew Shvayka
1 parent 39f0d92e

cherry-pick bfdd52ce from fix/ttlCleanUpServices and added the upgrade from 3.1.1

... ... @@ -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
... ...
... ... @@ -175,6 +175,11 @@ public class ThingsboardInstallService {
175 175 case "3.1.0":
176 176 log.info("Upgrading ThingsBoard from version 3.1.0 to 3.1.1 ...");
177 177 databaseEntitiesUpgradeService.upgradeDatabase("3.1.0");
  178 + case "3.1.1":
  179 + log.info("Upgrading ThingsBoard from version 3.1.1 to 3.1.2 ...");
  180 + if (databaseTsUpgradeService != null) {
  181 + databaseTsUpgradeService.upgradeDatabase("3.1.1");
  182 + }
178 183 log.info("Updating system data...");
179 184 systemDataLoaderService.updateSystemWidgets();
180 185 break;
... ...
... ... @@ -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 "3.1.1":
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 "3.1.1":
  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 "3.1.1":
  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 }
... ...
... ... @@ -52,7 +52,7 @@ public class EventsCleanUpService extends AbstractCleanUpService {
52 52 }
53 53
54 54 @Override
55   - protected void doCleanUp(Connection connection) {
  55 + protected void doCleanUp(Connection connection) throws SQLException {
56 56 long totalEventsRemoved = executeQuery(connection, "call cleanup_events_by_ttl(" + ttl + ", " + debugTtl + ", 0);");
57 57 log.info("Total events removed by TTL: [{}]", totalEventsRemoved);
58 58 }
... ...
... ... @@ -23,6 +23,7 @@ import org.thingsboard.server.dao.util.PsqlDao;
23 23 import org.thingsboard.server.dao.util.SqlTsDao;
24 24
25 25 import java.sql.Connection;
  26 +import java.sql.SQLException;
26 27
27 28 @SqlTsDao
28 29 @PsqlDao
... ... @@ -34,10 +35,10 @@ public class PsqlTimeseriesCleanUpService extends AbstractTimeseriesCleanUpServi
34 35 private String partitionType;
35 36
36 37 @Override
37   - protected void doCleanUp(Connection connection) {
38   - long totalPartitionsRemoved = executeQuery(connection, "call drop_partitions_by_max_ttl('" + partitionType + "'," + systemTtl + ", 0);");
39   - log.info("Total partitions removed by TTL: [{}]", totalPartitionsRemoved);
40   - long totalEntitiesTelemetryRemoved = executeQuery(connection, "call cleanup_timeseries_by_ttl('" + ModelConstants.NULL_UUID + "'," + systemTtl + ", 0);");
41   - log.info("Total telemetry removed stats by TTL for entities: [{}]", totalEntitiesTelemetryRemoved);
  38 + protected void doCleanUp(Connection connection) throws SQLException {
  39 + long totalPartitionsRemoved = executeQuery(connection, "call drop_partitions_by_max_ttl('" + partitionType + "'," + systemTtl + ", 0);");
  40 + log.info("Total partitions removed by TTL: [{}]", totalPartitionsRemoved);
  41 + long totalEntitiesTelemetryRemoved = executeQuery(connection, "call cleanup_timeseries_by_ttl('" + ModelConstants.NULL_UUID + "'," + systemTtl + ", 0);");
  42 + log.info("Total telemetry removed stats by TTL for entities: [{}]", totalEntitiesTelemetryRemoved);
42 43 }
43 44 }
\ No newline at end of file
... ...
... ... @@ -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 + "'," + systemTtl + ", 0);");
33 34 log.info("Total telemetry removed stats by TTL for entities: [{}]", totalEntitiesTelemetryRemoved);
34 35 }
... ...
... ... @@ -84,6 +84,7 @@ BEGIN
84 84 AND tablename like 'ts_kv_' || '%'
85 85 AND tablename != 'ts_kv_latest'
86 86 AND tablename != 'ts_kv_dictionary'
  87 + AND tablename != 'ts_kv_indefinite'
87 88 LOOP
88 89 IF partition != partition_by_max_ttl_date THEN
89 90 IF partition_year IS NOT NULL THEN
... ...