Commit bfdd52cefdc6bf781b31f99e6990d59383749f8e

Authored by Dmytro Shvaika
1 parent 8ec23f22

fix drop partitions by max ttl procedure

@@ -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
@@ -146,6 +146,11 @@ public class ThingsboardInstallService { @@ -146,6 +146,11 @@ public class ThingsboardInstallService {
146 databaseTsUpgradeService.upgradeDatabase("2.5.0"); 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 log.info("Updating system data..."); 155 log.info("Updating system data...");
151 156
@@ -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 "2.5.4":
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 "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 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 "2.5.4":
  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 }
@@ -54,7 +54,7 @@ public class EventsCleanUpService extends AbstractCleanUpService { @@ -54,7 +54,7 @@ public class EventsCleanUpService extends AbstractCleanUpService {
54 } 54 }
55 55
56 @Override 56 @Override
57 - protected void doCleanUp(Connection connection) { 57 + protected void doCleanUp(Connection connection) throws SQLException {
58 long totalEventsRemoved = executeQuery(connection, "call cleanup_events_by_ttl(" + ttl + ", " + debugTtl + ", 0);"); 58 long totalEventsRemoved = executeQuery(connection, "call cleanup_events_by_ttl(" + ttl + ", " + debugTtl + ", 0);");
59 log.info("Total events removed by TTL: [{}]", totalEventsRemoved); 59 log.info("Total events removed by TTL: [{}]", totalEventsRemoved);
60 } 60 }
@@ -22,6 +22,7 @@ import org.thingsboard.server.dao.model.ModelConstants; @@ -22,6 +22,7 @@ import org.thingsboard.server.dao.model.ModelConstants;
22 import org.thingsboard.server.dao.util.PsqlTsDao; 22 import org.thingsboard.server.dao.util.PsqlTsDao;
23 23
24 import java.sql.Connection; 24 import java.sql.Connection;
  25 +import java.sql.SQLException;
25 26
26 @PsqlTsDao 27 @PsqlTsDao
27 @Service 28 @Service
@@ -32,7 +33,7 @@ public class PsqlTimeseriesCleanUpService extends AbstractTimeseriesCleanUpServi @@ -32,7 +33,7 @@ public class PsqlTimeseriesCleanUpService extends AbstractTimeseriesCleanUpServi
32 private String partitionType; 33 private String partitionType;
33 34
34 @Override 35 @Override
35 - protected void doCleanUp(Connection connection) { 36 + protected void doCleanUp(Connection connection) throws SQLException {
36 long totalPartitionsRemoved = executeQuery(connection, "call drop_partitions_by_max_ttl('" + partitionType + "'," + systemTtl + ", 0);"); 37 long totalPartitionsRemoved = executeQuery(connection, "call drop_partitions_by_max_ttl('" + partitionType + "'," + systemTtl + ", 0);");
37 log.info("Total partitions removed by TTL: [{}]", totalPartitionsRemoved); 38 log.info("Total partitions removed by TTL: [{}]", totalPartitionsRemoved);
38 long totalEntitiesTelemetryRemoved = executeQuery(connection, "call cleanup_timeseries_by_ttl('" + ModelConstants.NULL_UUID_STR + "'," + systemTtl + ", 0);"); 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,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_STR + "'," + systemTtl + ", 0);"); 33 long totalEntitiesTelemetryRemoved = executeQuery(connection, "call cleanup_timeseries_by_ttl('" + ModelConstants.NULL_UUID_STR + "'," + 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 }
@@ -105,6 +105,7 @@ BEGIN @@ -105,6 +105,7 @@ BEGIN
105 AND tablename like 'ts_kv_' || '%' 105 AND tablename like 'ts_kv_' || '%'
106 AND tablename != 'ts_kv_latest' 106 AND tablename != 'ts_kv_latest'
107 AND tablename != 'ts_kv_dictionary' 107 AND tablename != 'ts_kv_dictionary'
  108 + AND tablename != 'ts_kv_indefinite'
108 LOOP 109 LOOP
109 IF partition != partition_by_max_ttl_date THEN 110 IF partition != partition_by_max_ttl_date THEN
110 IF partition_year IS NOT NULL THEN 111 IF partition_year IS NOT NULL THEN