Commit 67de61e6d5094de6a986e8b173f03f141973fbb5

Authored by YevhenBondarenko
1 parent 18e52d2c

fixed OtaPackage data cache

... ... @@ -782,15 +782,15 @@ public class DeviceController extends BaseController {
782 782 }
783 783
784 784 @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
785   - @RequestMapping(value = "/devices/count", method = RequestMethod.GET)
  785 + @RequestMapping(value = "/devices/count/{otaPackageType}", method = RequestMethod.GET)
786 786 @ResponseBody
787   - public Long countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(@RequestParam(required = false) String otaPackageType,
788   - @RequestParam(required = false) String deviceProfileId) throws ThingsboardException {
  787 + public Long countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(@PathVariable("otaPackageType") String otaPackageType,
  788 + @RequestParam String deviceProfileId) throws ThingsboardException {
789 789 checkParameter("OtaPackageType", otaPackageType);
790 790 checkParameter("DeviceProfileId", deviceProfileId);
791 791 try {
792 792 return deviceService.countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(
793   - getCurrentUser().getTenantId(), new DeviceProfileId(UUID.fromString(deviceProfileId)), OtaPackageType.valueOf(deviceProfileId));
  793 + getCurrentUser().getTenantId(), new DeviceProfileId(UUID.fromString(deviceProfileId)), OtaPackageType.valueOf(otaPackageType));
794 794 } catch (Exception e) {
795 795 throw handleException(e);
796 796 }
... ...
... ... @@ -374,6 +374,9 @@ caffeine:
374 374 otaPackages:
375 375 timeToLiveInMinutes: 60
376 376 maxSize: 10
  377 + otaPackagesData:
  378 + timeToLiveInMinutes: 60
  379 + maxSize: 10
377 380 edges:
378 381 timeToLiveInMinutes: 1440
379 382 maxSize: 0
... ...
... ... @@ -21,6 +21,7 @@ import org.springframework.cache.CacheManager;
21 21 import org.springframework.stereotype.Service;
22 22
23 23 import static org.thingsboard.server.common.data.CacheConstants.OTA_PACKAGE_CACHE;
  24 +import static org.thingsboard.server.common.data.CacheConstants.OTA_PACKAGE_DATA_CACHE;
24 25
25 26 @Service
26 27 @ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "caffeine", matchIfMissing = true)
... ... @@ -36,7 +37,7 @@ public class CaffeineOtaPackageCache implements OtaPackageDataCache {
36 37
37 38 @Override
38 39 public byte[] get(String key, int chunkSize, int chunk) {
39   - byte[] data = cacheManager.getCache(OTA_PACKAGE_CACHE).get(key, byte[].class);
  40 + byte[] data = cacheManager.getCache(OTA_PACKAGE_DATA_CACHE).get(key, byte[].class);
40 41
41 42 if (chunkSize < 1) {
42 43 return data;
... ... @@ -58,11 +59,11 @@ public class CaffeineOtaPackageCache implements OtaPackageDataCache {
58 59
59 60 @Override
60 61 public void put(String key, byte[] value) {
61   - cacheManager.getCache(OTA_PACKAGE_CACHE).putIfAbsent(key, value);
  62 + cacheManager.getCache(OTA_PACKAGE_DATA_CACHE).putIfAbsent(key, value);
62 63 }
63 64
64 65 @Override
65 66 public void evict(String key) {
66   - cacheManager.getCache(OTA_PACKAGE_CACHE).evict(key);
  67 + cacheManager.getCache(OTA_PACKAGE_DATA_CACHE).evict(key);
67 68 }
68 69 }
... ...
... ... @@ -22,6 +22,7 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
22 22 import org.springframework.stereotype.Service;
23 23
24 24 import static org.thingsboard.server.common.data.CacheConstants.OTA_PACKAGE_CACHE;
  25 +import static org.thingsboard.server.common.data.CacheConstants.OTA_PACKAGE_DATA_CACHE;
25 26
26 27 @Service
27 28 @ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis")
... ... @@ -63,6 +64,6 @@ public class RedisOtaPackageDataCache implements OtaPackageDataCache {
63 64 }
64 65
65 66 private byte[] toOtaPackageCacheKey(String key) {
66   - return String.format("%s::%s", OTA_PACKAGE_CACHE, key).getBytes();
  67 + return String.format("%s::%s", OTA_PACKAGE_DATA_CACHE, key).getBytes();
67 68 }
68 69 }
... ...
... ... @@ -30,4 +30,5 @@ public class CacheConstants {
30 30 public static final String ATTRIBUTES_CACHE = "attributes";
31 31 public static final String TOKEN_OUTDATAGE_TIME_CACHE = "tokensOutdatageTime";
32 32 public static final String OTA_PACKAGE_CACHE = "otaPackages";
  33 + public static final String OTA_PACKAGE_DATA_CACHE = "otaPackagesData";
33 34 }
... ...
... ... @@ -221,8 +221,6 @@ public class BaseOtaPackageService implements OtaPackageService {
221 221 @Override
222 222 protected void validateUpdate(TenantId tenantId, OtaPackageInfo otaPackage) {
223 223 OtaPackageInfo otaPackageOld = otaPackageInfoDao.findById(tenantId, otaPackage.getUuidId());
224   -
225   - validateUpdateDeviceProfile(otaPackage, otaPackageOld);
226 224 BaseOtaPackageService.validateUpdate(otaPackage, otaPackageOld);
227 225 }
228 226 };
... ... @@ -261,7 +259,6 @@ public class BaseOtaPackageService implements OtaPackageService {
261 259 protected void validateUpdate(TenantId tenantId, OtaPackage otaPackage) {
262 260 OtaPackage otaPackageOld = otaPackageDao.findById(tenantId, otaPackage.getUuidId());
263 261
264   - validateUpdateDeviceProfile(otaPackage, otaPackageOld);
265 262 BaseOtaPackageService.validateUpdate(otaPackage, otaPackageOld);
266 263
267 264 if (otaPackageOld.getData() != null && !otaPackageOld.getData().equals(otaPackage.getData())) {
... ... @@ -270,14 +267,6 @@ public class BaseOtaPackageService implements OtaPackageService {
270 267 }
271 268 };
272 269
273   - private void validateUpdateDeviceProfile(OtaPackageInfo otaPackage, OtaPackageInfo otaPackageOld) {
274   - if (otaPackageOld.getDeviceProfileId() != null && !otaPackageOld.getDeviceProfileId().equals(otaPackage.getDeviceProfileId())) {
275   - if (otaPackageInfoDao.isOtaPackageUsed(otaPackageOld.getId(), otaPackage.getType(), otaPackageOld.getDeviceProfileId())) {
276   - throw new DataValidationException("Can`t update deviceProfileId because otaPackage is already in use!");
277   - }
278   - }
279   - }
280   -
281 270 private static void validateUpdate(OtaPackageInfo otaPackage, OtaPackageInfo otaPackageOld) {
282 271 if (!otaPackageOld.getType().equals(otaPackage.getType())) {
283 272 throw new DataValidationException("Updating type is prohibited!");
... ... @@ -291,6 +280,10 @@ public class BaseOtaPackageService implements OtaPackageService {
291 280 throw new DataValidationException("Updating otaPackage version is prohibited!");
292 281 }
293 282
  283 + if (!otaPackageOld.getDeviceProfileId().equals(otaPackage.getDeviceProfileId())) {
  284 + throw new DataValidationException("Updating otaPackage deviceProfile is prohibited!");
  285 + }
  286 +
294 287 if (otaPackageOld.getFileName() != null && !otaPackageOld.getFileName().equals(otaPackage.getFileName())) {
295 288 throw new DataValidationException("Updating otaPackage file name is prohibited!");
296 289 }
... ...
... ... @@ -408,7 +408,7 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest {
408 408 }
409 409
410 410 @Test
411   - public void testUpdateDeviceProfileIdWithReferenceByDevice() {
  411 + public void testUpdateDeviceProfileId() {
412 412 OtaPackage firmware = new OtaPackage();
413 413 firmware.setTenantId(tenantId);
414 414 firmware.setDeviceProfileId(deviceProfileId);
... ... @@ -422,20 +422,12 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest {
422 422 firmware.setData(DATA);
423 423 OtaPackage savedFirmware = otaPackageService.saveOtaPackage(firmware);
424 424
425   - Device device = new Device();
426   - device.setTenantId(tenantId);
427   - device.setName("My device");
428   - device.setDeviceProfileId(deviceProfileId);
429   - device.setFirmwareId(savedFirmware.getId());
430   - Device savedDevice = deviceService.saveDevice(device);
431   -
432 425 try {
433 426 thrown.expect(DataValidationException.class);
434   - thrown.expectMessage("Can`t update deviceProfileId because otaPackage is already in use!");
  427 + thrown.expectMessage("Updating otaPackage deviceProfile is prohibited!");
435 428 savedFirmware.setDeviceProfileId(null);
436 429 otaPackageService.saveOtaPackage(savedFirmware);
437 430 } finally {
438   - deviceService.deleteDevice(tenantId, savedDevice.getId());
439 431 otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId());
440 432 }
441 433 }
... ... @@ -472,38 +464,6 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest {
472 464 }
473 465
474 466 @Test
475   - public void testUpdateDeviceProfileIdWithReferenceByDeviceProfile() {
476   - DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Test Device Profile");
477   - DeviceProfile savedDeviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile);
478   -
479   - OtaPackage firmware = new OtaPackage();
480   - firmware.setTenantId(tenantId);
481   - firmware.setDeviceProfileId(savedDeviceProfile.getId());
482   - firmware.setType(FIRMWARE);
483   - firmware.setTitle(TITLE);
484   - firmware.setVersion(VERSION);
485   - firmware.setFileName(FILE_NAME);
486   - firmware.setContentType(CONTENT_TYPE);
487   - firmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM);
488   - firmware.setChecksum(CHECKSUM);
489   - firmware.setData(DATA);
490   - OtaPackage savedFirmware = otaPackageService.saveOtaPackage(firmware);
491   -
492   - savedDeviceProfile.setFirmwareId(savedFirmware.getId());
493   - deviceProfileService.saveDeviceProfile(savedDeviceProfile);
494   -
495   - try {
496   - thrown.expect(DataValidationException.class);
497   - thrown.expectMessage("Can`t update deviceProfileId because otaPackage is already in use!");
498   - savedFirmware.setDeviceProfileId(null);
499   - otaPackageService.saveOtaPackage(savedFirmware);
500   - } finally {
501   - deviceProfileService.deleteDeviceProfile(tenantId, savedDeviceProfile.getId());
502   - otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId());
503   - }
504   - }
505   -
506   - @Test
507 467 public void testFindFirmwareById() {
508 468 OtaPackage firmware = new OtaPackage();
509 469 firmware.setTenantId(tenantId);
... ...
... ... @@ -39,6 +39,9 @@ caffeine.specs.deviceProfiles.maxSize=100000
39 39 caffeine.specs.otaPackages.timeToLiveInMinutes=1440
40 40 caffeine.specs.otaPackages.maxSize=100000
41 41
  42 +caffeine.specs.otaPackagesData.timeToLiveInMinutes=1440
  43 +caffeine.specs.otaPackagesData.maxSize=100000
  44 +
42 45 caffeine.specs.edges.timeToLiveInMinutes=1440
43 46 caffeine.specs.edges.maxSize=100000
44 47
... ...