fixed OtaPackage data cache

This commit is contained in:
YevhenBondarenko 2021-06-02 11:40:20 +03:00
parent 18e52d2c17
commit 67de61e6d5
8 changed files with 23 additions and 61 deletions

View File

@ -782,15 +782,15 @@ public class DeviceController extends BaseController {
} }
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/devices/count", method = RequestMethod.GET) @RequestMapping(value = "/devices/count/{otaPackageType}", method = RequestMethod.GET)
@ResponseBody @ResponseBody
public Long countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(@RequestParam(required = false) String otaPackageType, public Long countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(@PathVariable("otaPackageType") String otaPackageType,
@RequestParam(required = false) String deviceProfileId) throws ThingsboardException { @RequestParam String deviceProfileId) throws ThingsboardException {
checkParameter("OtaPackageType", otaPackageType); checkParameter("OtaPackageType", otaPackageType);
checkParameter("DeviceProfileId", deviceProfileId); checkParameter("DeviceProfileId", deviceProfileId);
try { try {
return deviceService.countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage( return deviceService.countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(
getCurrentUser().getTenantId(), new DeviceProfileId(UUID.fromString(deviceProfileId)), OtaPackageType.valueOf(deviceProfileId)); getCurrentUser().getTenantId(), new DeviceProfileId(UUID.fromString(deviceProfileId)), OtaPackageType.valueOf(otaPackageType));
} catch (Exception e) { } catch (Exception e) {
throw handleException(e); throw handleException(e);
} }

View File

@ -374,6 +374,9 @@ caffeine:
otaPackages: otaPackages:
timeToLiveInMinutes: 60 timeToLiveInMinutes: 60
maxSize: 10 maxSize: 10
otaPackagesData:
timeToLiveInMinutes: 60
maxSize: 10
edges: edges:
timeToLiveInMinutes: 1440 timeToLiveInMinutes: 1440
maxSize: 0 maxSize: 0

View File

@ -21,6 +21,7 @@ import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import static org.thingsboard.server.common.data.CacheConstants.OTA_PACKAGE_CACHE; import static org.thingsboard.server.common.data.CacheConstants.OTA_PACKAGE_CACHE;
import static org.thingsboard.server.common.data.CacheConstants.OTA_PACKAGE_DATA_CACHE;
@Service @Service
@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "caffeine", matchIfMissing = true) @ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "caffeine", matchIfMissing = true)
@ -36,7 +37,7 @@ public class CaffeineOtaPackageCache implements OtaPackageDataCache {
@Override @Override
public byte[] get(String key, int chunkSize, int chunk) { public byte[] get(String key, int chunkSize, int chunk) {
byte[] data = cacheManager.getCache(OTA_PACKAGE_CACHE).get(key, byte[].class); byte[] data = cacheManager.getCache(OTA_PACKAGE_DATA_CACHE).get(key, byte[].class);
if (chunkSize < 1) { if (chunkSize < 1) {
return data; return data;
@ -58,11 +59,11 @@ public class CaffeineOtaPackageCache implements OtaPackageDataCache {
@Override @Override
public void put(String key, byte[] value) { public void put(String key, byte[] value) {
cacheManager.getCache(OTA_PACKAGE_CACHE).putIfAbsent(key, value); cacheManager.getCache(OTA_PACKAGE_DATA_CACHE).putIfAbsent(key, value);
} }
@Override @Override
public void evict(String key) { public void evict(String key) {
cacheManager.getCache(OTA_PACKAGE_CACHE).evict(key); cacheManager.getCache(OTA_PACKAGE_DATA_CACHE).evict(key);
} }
} }

View File

@ -22,6 +22,7 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import static org.thingsboard.server.common.data.CacheConstants.OTA_PACKAGE_CACHE; import static org.thingsboard.server.common.data.CacheConstants.OTA_PACKAGE_CACHE;
import static org.thingsboard.server.common.data.CacheConstants.OTA_PACKAGE_DATA_CACHE;
@Service @Service
@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis") @ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis")
@ -63,6 +64,6 @@ public class RedisOtaPackageDataCache implements OtaPackageDataCache {
} }
private byte[] toOtaPackageCacheKey(String key) { private byte[] toOtaPackageCacheKey(String key) {
return String.format("%s::%s", OTA_PACKAGE_CACHE, key).getBytes(); return String.format("%s::%s", OTA_PACKAGE_DATA_CACHE, key).getBytes();
} }
} }

View File

@ -30,4 +30,5 @@ public class CacheConstants {
public static final String ATTRIBUTES_CACHE = "attributes"; public static final String ATTRIBUTES_CACHE = "attributes";
public static final String TOKEN_OUTDATAGE_TIME_CACHE = "tokensOutdatageTime"; public static final String TOKEN_OUTDATAGE_TIME_CACHE = "tokensOutdatageTime";
public static final String OTA_PACKAGE_CACHE = "otaPackages"; public static final String OTA_PACKAGE_CACHE = "otaPackages";
public static final String OTA_PACKAGE_DATA_CACHE = "otaPackagesData";
} }

View File

@ -221,8 +221,6 @@ public class BaseOtaPackageService implements OtaPackageService {
@Override @Override
protected void validateUpdate(TenantId tenantId, OtaPackageInfo otaPackage) { protected void validateUpdate(TenantId tenantId, OtaPackageInfo otaPackage) {
OtaPackageInfo otaPackageOld = otaPackageInfoDao.findById(tenantId, otaPackage.getUuidId()); OtaPackageInfo otaPackageOld = otaPackageInfoDao.findById(tenantId, otaPackage.getUuidId());
validateUpdateDeviceProfile(otaPackage, otaPackageOld);
BaseOtaPackageService.validateUpdate(otaPackage, otaPackageOld); BaseOtaPackageService.validateUpdate(otaPackage, otaPackageOld);
} }
}; };
@ -261,7 +259,6 @@ public class BaseOtaPackageService implements OtaPackageService {
protected void validateUpdate(TenantId tenantId, OtaPackage otaPackage) { protected void validateUpdate(TenantId tenantId, OtaPackage otaPackage) {
OtaPackage otaPackageOld = otaPackageDao.findById(tenantId, otaPackage.getUuidId()); OtaPackage otaPackageOld = otaPackageDao.findById(tenantId, otaPackage.getUuidId());
validateUpdateDeviceProfile(otaPackage, otaPackageOld);
BaseOtaPackageService.validateUpdate(otaPackage, otaPackageOld); BaseOtaPackageService.validateUpdate(otaPackage, otaPackageOld);
if (otaPackageOld.getData() != null && !otaPackageOld.getData().equals(otaPackage.getData())) { if (otaPackageOld.getData() != null && !otaPackageOld.getData().equals(otaPackage.getData())) {
@ -270,14 +267,6 @@ public class BaseOtaPackageService implements OtaPackageService {
} }
}; };
private void validateUpdateDeviceProfile(OtaPackageInfo otaPackage, OtaPackageInfo otaPackageOld) {
if (otaPackageOld.getDeviceProfileId() != null && !otaPackageOld.getDeviceProfileId().equals(otaPackage.getDeviceProfileId())) {
if (otaPackageInfoDao.isOtaPackageUsed(otaPackageOld.getId(), otaPackage.getType(), otaPackageOld.getDeviceProfileId())) {
throw new DataValidationException("Can`t update deviceProfileId because otaPackage is already in use!");
}
}
}
private static void validateUpdate(OtaPackageInfo otaPackage, OtaPackageInfo otaPackageOld) { private static void validateUpdate(OtaPackageInfo otaPackage, OtaPackageInfo otaPackageOld) {
if (!otaPackageOld.getType().equals(otaPackage.getType())) { if (!otaPackageOld.getType().equals(otaPackage.getType())) {
throw new DataValidationException("Updating type is prohibited!"); throw new DataValidationException("Updating type is prohibited!");
@ -291,6 +280,10 @@ public class BaseOtaPackageService implements OtaPackageService {
throw new DataValidationException("Updating otaPackage version is prohibited!"); throw new DataValidationException("Updating otaPackage version is prohibited!");
} }
if (!otaPackageOld.getDeviceProfileId().equals(otaPackage.getDeviceProfileId())) {
throw new DataValidationException("Updating otaPackage deviceProfile is prohibited!");
}
if (otaPackageOld.getFileName() != null && !otaPackageOld.getFileName().equals(otaPackage.getFileName())) { if (otaPackageOld.getFileName() != null && !otaPackageOld.getFileName().equals(otaPackage.getFileName())) {
throw new DataValidationException("Updating otaPackage file name is prohibited!"); throw new DataValidationException("Updating otaPackage file name is prohibited!");
} }

View File

@ -408,7 +408,7 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest {
} }
@Test @Test
public void testUpdateDeviceProfileIdWithReferenceByDevice() { public void testUpdateDeviceProfileId() {
OtaPackage firmware = new OtaPackage(); OtaPackage firmware = new OtaPackage();
firmware.setTenantId(tenantId); firmware.setTenantId(tenantId);
firmware.setDeviceProfileId(deviceProfileId); firmware.setDeviceProfileId(deviceProfileId);
@ -422,20 +422,12 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest {
firmware.setData(DATA); firmware.setData(DATA);
OtaPackage savedFirmware = otaPackageService.saveOtaPackage(firmware); OtaPackage savedFirmware = otaPackageService.saveOtaPackage(firmware);
Device device = new Device();
device.setTenantId(tenantId);
device.setName("My device");
device.setDeviceProfileId(deviceProfileId);
device.setFirmwareId(savedFirmware.getId());
Device savedDevice = deviceService.saveDevice(device);
try { try {
thrown.expect(DataValidationException.class); thrown.expect(DataValidationException.class);
thrown.expectMessage("Can`t update deviceProfileId because otaPackage is already in use!"); thrown.expectMessage("Updating otaPackage deviceProfile is prohibited!");
savedFirmware.setDeviceProfileId(null); savedFirmware.setDeviceProfileId(null);
otaPackageService.saveOtaPackage(savedFirmware); otaPackageService.saveOtaPackage(savedFirmware);
} finally { } finally {
deviceService.deleteDevice(tenantId, savedDevice.getId());
otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId()); otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId());
} }
} }
@ -471,38 +463,6 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest {
} }
} }
@Test
public void testUpdateDeviceProfileIdWithReferenceByDeviceProfile() {
DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Test Device Profile");
DeviceProfile savedDeviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile);
OtaPackage firmware = new OtaPackage();
firmware.setTenantId(tenantId);
firmware.setDeviceProfileId(savedDeviceProfile.getId());
firmware.setType(FIRMWARE);
firmware.setTitle(TITLE);
firmware.setVersion(VERSION);
firmware.setFileName(FILE_NAME);
firmware.setContentType(CONTENT_TYPE);
firmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM);
firmware.setChecksum(CHECKSUM);
firmware.setData(DATA);
OtaPackage savedFirmware = otaPackageService.saveOtaPackage(firmware);
savedDeviceProfile.setFirmwareId(savedFirmware.getId());
deviceProfileService.saveDeviceProfile(savedDeviceProfile);
try {
thrown.expect(DataValidationException.class);
thrown.expectMessage("Can`t update deviceProfileId because otaPackage is already in use!");
savedFirmware.setDeviceProfileId(null);
otaPackageService.saveOtaPackage(savedFirmware);
} finally {
deviceProfileService.deleteDeviceProfile(tenantId, savedDeviceProfile.getId());
otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId());
}
}
@Test @Test
public void testFindFirmwareById() { public void testFindFirmwareById() {
OtaPackage firmware = new OtaPackage(); OtaPackage firmware = new OtaPackage();

View File

@ -39,6 +39,9 @@ caffeine.specs.deviceProfiles.maxSize=100000
caffeine.specs.otaPackages.timeToLiveInMinutes=1440 caffeine.specs.otaPackages.timeToLiveInMinutes=1440
caffeine.specs.otaPackages.maxSize=100000 caffeine.specs.otaPackages.maxSize=100000
caffeine.specs.otaPackagesData.timeToLiveInMinutes=1440
caffeine.specs.otaPackagesData.maxSize=100000
caffeine.specs.edges.timeToLiveInMinutes=1440 caffeine.specs.edges.timeToLiveInMinutes=1440
caffeine.specs.edges.maxSize=100000 caffeine.specs.edges.maxSize=100000