fixed OtaPackage data cache
This commit is contained in:
parent
18e52d2c17
commit
67de61e6d5
@ -782,15 +782,15 @@ public class DeviceController extends BaseController {
|
||||
}
|
||||
|
||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
|
||||
@RequestMapping(value = "/devices/count", method = RequestMethod.GET)
|
||||
@RequestMapping(value = "/devices/count/{otaPackageType}", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public Long countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(@RequestParam(required = false) String otaPackageType,
|
||||
@RequestParam(required = false) String deviceProfileId) throws ThingsboardException {
|
||||
public Long countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(@PathVariable("otaPackageType") String otaPackageType,
|
||||
@RequestParam String deviceProfileId) throws ThingsboardException {
|
||||
checkParameter("OtaPackageType", otaPackageType);
|
||||
checkParameter("DeviceProfileId", deviceProfileId);
|
||||
try {
|
||||
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) {
|
||||
throw handleException(e);
|
||||
}
|
||||
|
||||
@ -374,6 +374,9 @@ caffeine:
|
||||
otaPackages:
|
||||
timeToLiveInMinutes: 60
|
||||
maxSize: 10
|
||||
otaPackagesData:
|
||||
timeToLiveInMinutes: 60
|
||||
maxSize: 10
|
||||
edges:
|
||||
timeToLiveInMinutes: 1440
|
||||
maxSize: 0
|
||||
|
||||
@ -21,6 +21,7 @@ import org.springframework.cache.CacheManager;
|
||||
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_DATA_CACHE;
|
||||
|
||||
@Service
|
||||
@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "caffeine", matchIfMissing = true)
|
||||
@ -36,7 +37,7 @@ public class CaffeineOtaPackageCache implements OtaPackageDataCache {
|
||||
|
||||
@Override
|
||||
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) {
|
||||
return data;
|
||||
@ -58,11 +59,11 @@ public class CaffeineOtaPackageCache implements OtaPackageDataCache {
|
||||
|
||||
@Override
|
||||
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
|
||||
public void evict(String key) {
|
||||
cacheManager.getCache(OTA_PACKAGE_CACHE).evict(key);
|
||||
cacheManager.getCache(OTA_PACKAGE_DATA_CACHE).evict(key);
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,6 +22,7 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
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_DATA_CACHE;
|
||||
|
||||
@Service
|
||||
@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis")
|
||||
@ -63,6 +64,6 @@ public class RedisOtaPackageDataCache implements OtaPackageDataCache {
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,4 +30,5 @@ public class CacheConstants {
|
||||
public static final String ATTRIBUTES_CACHE = "attributes";
|
||||
public static final String TOKEN_OUTDATAGE_TIME_CACHE = "tokensOutdatageTime";
|
||||
public static final String OTA_PACKAGE_CACHE = "otaPackages";
|
||||
public static final String OTA_PACKAGE_DATA_CACHE = "otaPackagesData";
|
||||
}
|
||||
|
||||
@ -221,8 +221,6 @@ public class BaseOtaPackageService implements OtaPackageService {
|
||||
@Override
|
||||
protected void validateUpdate(TenantId tenantId, OtaPackageInfo otaPackage) {
|
||||
OtaPackageInfo otaPackageOld = otaPackageInfoDao.findById(tenantId, otaPackage.getUuidId());
|
||||
|
||||
validateUpdateDeviceProfile(otaPackage, otaPackageOld);
|
||||
BaseOtaPackageService.validateUpdate(otaPackage, otaPackageOld);
|
||||
}
|
||||
};
|
||||
@ -261,7 +259,6 @@ public class BaseOtaPackageService implements OtaPackageService {
|
||||
protected void validateUpdate(TenantId tenantId, OtaPackage otaPackage) {
|
||||
OtaPackage otaPackageOld = otaPackageDao.findById(tenantId, otaPackage.getUuidId());
|
||||
|
||||
validateUpdateDeviceProfile(otaPackage, otaPackageOld);
|
||||
BaseOtaPackageService.validateUpdate(otaPackage, otaPackageOld);
|
||||
|
||||
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) {
|
||||
if (!otaPackageOld.getType().equals(otaPackage.getType())) {
|
||||
throw new DataValidationException("Updating type is prohibited!");
|
||||
@ -291,6 +280,10 @@ public class BaseOtaPackageService implements OtaPackageService {
|
||||
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())) {
|
||||
throw new DataValidationException("Updating otaPackage file name is prohibited!");
|
||||
}
|
||||
|
||||
@ -408,7 +408,7 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateDeviceProfileIdWithReferenceByDevice() {
|
||||
public void testUpdateDeviceProfileId() {
|
||||
OtaPackage firmware = new OtaPackage();
|
||||
firmware.setTenantId(tenantId);
|
||||
firmware.setDeviceProfileId(deviceProfileId);
|
||||
@ -422,20 +422,12 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest {
|
||||
firmware.setData(DATA);
|
||||
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 {
|
||||
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);
|
||||
otaPackageService.saveOtaPackage(savedFirmware);
|
||||
} finally {
|
||||
deviceService.deleteDevice(tenantId, savedDevice.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
|
||||
public void testFindFirmwareById() {
|
||||
OtaPackage firmware = new OtaPackage();
|
||||
|
||||
@ -39,6 +39,9 @@ caffeine.specs.deviceProfiles.maxSize=100000
|
||||
caffeine.specs.otaPackages.timeToLiveInMinutes=1440
|
||||
caffeine.specs.otaPackages.maxSize=100000
|
||||
|
||||
caffeine.specs.otaPackagesData.timeToLiveInMinutes=1440
|
||||
caffeine.specs.otaPackagesData.maxSize=100000
|
||||
|
||||
caffeine.specs.edges.timeToLiveInMinutes=1440
|
||||
caffeine.specs.edges.maxSize=100000
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user