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,6 +64,7 @@ BEGIN
64 AND tablename like 'ts_kv_' || '%' 64 AND tablename like 'ts_kv_' || '%'
65 AND tablename != 'ts_kv_latest' 65 AND tablename != 'ts_kv_latest'
66 AND tablename != 'ts_kv_dictionary' 66 AND tablename != 'ts_kv_dictionary'
  67 + AND tablename != 'ts_kv_indefinite'
67 LOOP 68 LOOP
68 IF partition != partition_by_max_ttl_date THEN 69 IF partition != partition_by_max_ttl_date THEN
69 IF partition_year IS NOT NULL THEN 70 IF partition_year IS NOT NULL THEN
@@ -175,6 +175,11 @@ public class ThingsboardInstallService { @@ -175,6 +175,11 @@ public class ThingsboardInstallService {
175 case "3.1.0": 175 case "3.1.0":
176 log.info("Upgrading ThingsBoard from version 3.1.0 to 3.1.1 ..."); 176 log.info("Upgrading ThingsBoard from version 3.1.0 to 3.1.1 ...");
177 databaseEntitiesUpgradeService.upgradeDatabase("3.1.0"); 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 log.info("Updating system data..."); 183 log.info("Updating system data...");
179 systemDataLoaderService.updateSystemWidgets(); 184 systemDataLoaderService.updateSystemWidgets();
180 break; 185 break;
@@ -49,6 +49,7 @@ public class CassandraTsDatabaseUpgradeService extends AbstractCassandraDatabase @@ -49,6 +49,7 @@ public class CassandraTsDatabaseUpgradeService extends AbstractCassandraDatabase
49 log.info("Schema updated."); 49 log.info("Schema updated.");
50 break; 50 break;
51 case "2.5.0": 51 case "2.5.0":
  52 + case "3.1.1":
52 break; 53 break;
53 default: 54 default:
54 throw new RuntimeException("Unable to upgrade Cassandra database, unsupported fromVersion: " + fromVersion); 55 throw new RuntimeException("Unable to upgrade Cassandra database, unsupported fromVersion: " + fromVersion);
@@ -195,6 +195,12 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe @@ -195,6 +195,12 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe
195 executeQuery(conn, "UPDATE tb_schema_settings SET schema_version = 2005001"); 195 executeQuery(conn, "UPDATE tb_schema_settings SET schema_version = 2005001");
196 } 196 }
197 break; 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 default: 204 default:
199 throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion); 205 throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion);
200 } 206 }
@@ -177,6 +177,8 @@ public class TimescaleTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgr @@ -177,6 +177,8 @@ public class TimescaleTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgr
177 executeQuery(conn, "UPDATE tb_schema_settings SET schema_version = 2005001"); 177 executeQuery(conn, "UPDATE tb_schema_settings SET schema_version = 2005001");
178 } 178 }
179 break; 179 break;
  180 + case "3.1.1":
  181 + break;
180 default: 182 default:
181 throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion); 183 throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion);
182 } 184 }
@@ -38,19 +38,15 @@ public abstract class AbstractCleanUpService { @@ -38,19 +38,15 @@ public abstract class AbstractCleanUpService {
38 @Value("${spring.datasource.password}") 38 @Value("${spring.datasource.password}")
39 protected String dbPassword; 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 ResultSet resultSet = statement.executeQuery(query); 43 ResultSet resultSet = statement.executeQuery(query);
46 - getWarnings(statement); 44 + if (log.isDebugEnabled()) {
  45 + getWarnings(statement);
  46 + }
47 resultSet.next(); 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 protected void getWarnings(Statement statement) throws SQLException { 52 protected void getWarnings(Statement statement) throws SQLException {
@@ -65,6 +61,6 @@ public abstract class AbstractCleanUpService { @@ -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,7 +52,7 @@ public class EventsCleanUpService extends AbstractCleanUpService {
52 } 52 }
53 53
54 @Override 54 @Override
55 - protected void doCleanUp(Connection connection) { 55 + protected void doCleanUp(Connection connection) throws SQLException {
56 long totalEventsRemoved = executeQuery(connection, "call cleanup_events_by_ttl(" + ttl + ", " + debugTtl + ", 0);"); 56 long totalEventsRemoved = executeQuery(connection, "call cleanup_events_by_ttl(" + ttl + ", " + debugTtl + ", 0);");
57 log.info("Total events removed by TTL: [{}]", totalEventsRemoved); 57 log.info("Total events removed by TTL: [{}]", totalEventsRemoved);
58 } 58 }
@@ -23,6 +23,7 @@ import org.thingsboard.server.dao.util.PsqlDao; @@ -23,6 +23,7 @@ import org.thingsboard.server.dao.util.PsqlDao;
23 import org.thingsboard.server.dao.util.SqlTsDao; 23 import org.thingsboard.server.dao.util.SqlTsDao;
24 24
25 import java.sql.Connection; 25 import java.sql.Connection;
  26 +import java.sql.SQLException;
26 27
27 @SqlTsDao 28 @SqlTsDao
28 @PsqlDao 29 @PsqlDao
@@ -34,10 +35,10 @@ public class PsqlTimeseriesCleanUpService extends AbstractTimeseriesCleanUpServi @@ -34,10 +35,10 @@ public class PsqlTimeseriesCleanUpService extends AbstractTimeseriesCleanUpServi
34 private String partitionType; 35 private String partitionType;
35 36
36 @Override 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 }
@@ -21,6 +21,7 @@ import org.thingsboard.server.dao.model.ModelConstants; @@ -21,6 +21,7 @@ import org.thingsboard.server.dao.model.ModelConstants;
21 import org.thingsboard.server.dao.util.TimescaleDBTsDao; 21 import org.thingsboard.server.dao.util.TimescaleDBTsDao;
22 22
23 import java.sql.Connection; 23 import java.sql.Connection;
  24 +import java.sql.SQLException;
24 25
25 @TimescaleDBTsDao 26 @TimescaleDBTsDao
26 @Service 27 @Service
@@ -28,7 +29,7 @@ import java.sql.Connection; @@ -28,7 +29,7 @@ import java.sql.Connection;
28 public class TimescaleTimeseriesCleanUpService extends AbstractTimeseriesCleanUpService { 29 public class TimescaleTimeseriesCleanUpService extends AbstractTimeseriesCleanUpService {
29 30
30 @Override 31 @Override
31 - protected void doCleanUp(Connection connection) { 32 + protected void doCleanUp(Connection connection) throws SQLException {
32 long totalEntitiesTelemetryRemoved = executeQuery(connection, "call cleanup_timeseries_by_ttl('" + ModelConstants.NULL_UUID + "'," + systemTtl + ", 0);"); 33 long totalEntitiesTelemetryRemoved = executeQuery(connection, "call cleanup_timeseries_by_ttl('" + ModelConstants.NULL_UUID + "'," + systemTtl + ", 0);");
33 log.info("Total telemetry removed stats by TTL for entities: [{}]", totalEntitiesTelemetryRemoved); 34 log.info("Total telemetry removed stats by TTL for entities: [{}]", totalEntitiesTelemetryRemoved);
34 } 35 }
@@ -84,6 +84,7 @@ BEGIN @@ -84,6 +84,7 @@ BEGIN
84 AND tablename like 'ts_kv_' || '%' 84 AND tablename like 'ts_kv_' || '%'
85 AND tablename != 'ts_kv_latest' 85 AND tablename != 'ts_kv_latest'
86 AND tablename != 'ts_kv_dictionary' 86 AND tablename != 'ts_kv_dictionary'
  87 + AND tablename != 'ts_kv_indefinite'
87 LOOP 88 LOOP
88 IF partition != partition_by_max_ttl_date THEN 89 IF partition != partition_by_max_ttl_date THEN
89 IF partition_year IS NOT NULL THEN 90 IF partition_year IS NOT NULL THEN