Commit c621d41b1b833cb618eb2a679e8da91a203b9b35

Authored by Sergey Matvienko
Committed by Andrew Shvayka
1 parent f02304e2

version upgrade: cache cleanup service added to clear caches that can not deseri…

…alize anymore due to schema upgrade
@@ -31,6 +31,7 @@ import org.thingsboard.server.service.install.TsDatabaseSchemaService; @@ -31,6 +31,7 @@ import org.thingsboard.server.service.install.TsDatabaseSchemaService;
31 import org.thingsboard.server.service.install.TsLatestDatabaseSchemaService; 31 import org.thingsboard.server.service.install.TsLatestDatabaseSchemaService;
32 import org.thingsboard.server.service.install.migrate.EntitiesMigrateService; 32 import org.thingsboard.server.service.install.migrate.EntitiesMigrateService;
33 import org.thingsboard.server.service.install.migrate.TsLatestMigrateService; 33 import org.thingsboard.server.service.install.migrate.TsLatestMigrateService;
  34 +import org.thingsboard.server.service.install.update.CacheCleanupService;
34 import org.thingsboard.server.service.install.update.DataUpdateService; 35 import org.thingsboard.server.service.install.update.DataUpdateService;
35 36
36 @Service 37 @Service
@@ -74,6 +75,9 @@ public class ThingsboardInstallService { @@ -74,6 +75,9 @@ public class ThingsboardInstallService {
74 @Autowired 75 @Autowired
75 private DataUpdateService dataUpdateService; 76 private DataUpdateService dataUpdateService;
76 77
  78 + @Autowired
  79 + private CacheCleanupService cacheCleanupService;
  80 +
77 @Autowired(required = false) 81 @Autowired(required = false)
78 private EntitiesMigrateService entitiesMigrateService; 82 private EntitiesMigrateService entitiesMigrateService;
79 83
@@ -85,6 +89,8 @@ public class ThingsboardInstallService { @@ -85,6 +89,8 @@ public class ThingsboardInstallService {
85 if (isUpgrade) { 89 if (isUpgrade) {
86 log.info("Starting ThingsBoard Upgrade from version {} ...", upgradeFromVersion); 90 log.info("Starting ThingsBoard Upgrade from version {} ...", upgradeFromVersion);
87 91
  92 + cacheCleanupService.clearCache(upgradeFromVersion);
  93 +
88 if ("2.5.0-cassandra".equals(upgradeFromVersion)) { 94 if ("2.5.0-cassandra".equals(upgradeFromVersion)) {
89 log.info("Migrating ThingsBoard entities data from cassandra to SQL database ..."); 95 log.info("Migrating ThingsBoard entities data from cassandra to SQL database ...");
90 entitiesMigrateService.migrate(); 96 entitiesMigrateService.migrate();
@@ -207,6 +213,9 @@ public class ThingsboardInstallService { @@ -207,6 +213,9 @@ public class ThingsboardInstallService {
207 log.info("Updating system data..."); 213 log.info("Updating system data...");
208 systemDataLoaderService.updateSystemWidgets(); 214 systemDataLoaderService.updateSystemWidgets();
209 break; 215 break;
  216 +
  217 + //TODO update CacheCleanupService on the next version upgrade
  218 +
210 default: 219 default:
211 throw new RuntimeException("Unable to upgrade ThingsBoard, unsupported fromVersion: " + upgradeFromVersion); 220 throw new RuntimeException("Unable to upgrade ThingsBoard, unsupported fromVersion: " + upgradeFromVersion);
212 221
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.service.install.update;
  17 +
  18 +public interface CacheCleanupService {
  19 +
  20 + void clearCache(String fromVersion) throws Exception;
  21 +
  22 +}
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.service.install.update;
  17 +
  18 +import lombok.extern.slf4j.Slf4j;
  19 +import org.springframework.beans.factory.annotation.Autowired;
  20 +import org.springframework.cache.Cache;
  21 +import org.springframework.cache.CacheManager;
  22 +import org.springframework.context.annotation.Profile;
  23 +import org.springframework.stereotype.Service;
  24 +
  25 +import java.util.Objects;
  26 +
  27 +@Service
  28 +@Profile("install")
  29 +@Slf4j
  30 +public class DefaultCacheCleanupService implements CacheCleanupService {
  31 +
  32 + @Autowired
  33 + CacheManager cacheManager;
  34 +
  35 + /**
  36 + * Cleanup caches that can not deserialize anymore due to schema upgrade or data update using sql scripts.
  37 + * Refer to SqlDatabaseUpgradeService and /data/upgrage/*.sql
  38 + * to discover which tables were changed
  39 + * */
  40 + @Override
  41 + public void clearCache(String fromVersion) throws Exception {
  42 + switch (fromVersion) {
  43 + case "3.0.1":
  44 + log.info("Clear cache to upgrade from version 3.0.1 to 3.1.0 ...");
  45 + clearAllCaches();
  46 + //do not break to show explicit calls for next versions
  47 + case "3.1.1":
  48 + log.info("Clear cache to upgrade from version 3.1.1 to 3.2.0 ...");
  49 + clearCacheByName("devices");
  50 + clearCacheByName("deviceProfiles");
  51 + clearCacheByName("tenantProfiles");
  52 + case "3.2.2":
  53 + log.info("Clear cache to upgrade from version 3.2.2 to 3.3.0 ...");
  54 + clearCacheByName("devices");
  55 + clearCacheByName("deviceProfiles");
  56 + clearCacheByName("tenantProfiles");
  57 + clearCacheByName("relations");
  58 +
  59 + break;
  60 + default:
  61 + throw new RuntimeException("Unable to update cache, unsupported fromVersion: " + fromVersion);
  62 + }
  63 + }
  64 +
  65 + void clearAllCaches() {
  66 + cacheManager.getCacheNames().forEach(this::clearCacheByName);
  67 + }
  68 +
  69 + void clearCacheByName(final String cacheName) {
  70 + Cache cache = cacheManager.getCache(cacheName);
  71 + Objects.requireNonNull(cache, "Cache does not exist for name " + cacheName);
  72 + cache.clear();
  73 + }
  74 +
  75 +}