From ecca0e5df3d363ae1d22a5aeaa156b0dcf7d3bc1 Mon Sep 17 00:00:00 2001 From: Sergey Matvienko Date: Thu, 12 Aug 2021 19:18:45 +0300 Subject: [PATCH 1/3] device service cache eviction fix for saveDeviceWithCredentials --- .../thingsboard/server/dao/device/DeviceServiceImpl.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java index bbcd81984f..5c6cd7a72b 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java @@ -207,7 +207,10 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe return doSaveDevice(device, null, true); } - @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}") + @Caching(evict= { + @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}"), + @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.id}") + }) @Override public Device saveDeviceWithCredentials(Device device, DeviceCredentials deviceCredentials) { if (device.getId() == null) { @@ -631,7 +634,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name()); } } - removeDeviceFromCacheById(savedDevice.getTenantId(), savedDevice.getId()); + removeDeviceFromCacheById(savedDevice.getTenantId(), savedDevice.getId()); // eviction by name is described as annotation @CacheEvict above return savedDevice; } @@ -721,6 +724,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe } if (!old.getName().equals(device.getName())) { removeDeviceFromCacheByName(tenantId, old.getName()); + removeDeviceFromCacheById(tenantId, device.getId()); } } From d4a2e5a52ad92b2657240e7db22d4482ad9b8310 Mon Sep 17 00:00:00 2001 From: Sergey Matvienko Date: Thu, 12 Aug 2021 20:08:42 +0300 Subject: [PATCH 2/3] device service: transactional for saveDeviceWithAccessToken and saveDeviceWithCredentials --- .../org/thingsboard/server/dao/device/DeviceServiceImpl.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java index 5c6cd7a72b..39b5eb7eac 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java @@ -184,6 +184,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}"), @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.id}") }) + @Transactional @Override public Device saveDeviceWithAccessToken(Device device, String accessToken) { return doSaveDevice(device, accessToken, true); @@ -211,6 +212,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}"), @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.id}") }) + @Transactional @Override public Device saveDeviceWithCredentials(Device device, DeviceCredentials deviceCredentials) { if (device.getId() == null) { From af4132d489d521fcbabdaa5fdc8c3d8714410b57 Mon Sep 17 00:00:00 2001 From: Sergey Matvienko Date: Thu, 12 Aug 2021 20:12:02 +0300 Subject: [PATCH 3/3] test: refactored NoSecLwM2MIntegrationTest.testSoftwareUpdateByObject9 (but test case seems not valid) --- .../lwm2m/sql/NoSecLwM2MIntegrationTest.java | 45 +++++++------------ 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/transport/lwm2m/sql/NoSecLwM2MIntegrationTest.java b/application/src/test/java/org/thingsboard/server/transport/lwm2m/sql/NoSecLwM2MIntegrationTest.java index 4a2e7c7a2b..168e3cbdec 100644 --- a/application/src/test/java/org/thingsboard/server/transport/lwm2m/sql/NoSecLwM2MIntegrationTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/lwm2m/sql/NoSecLwM2MIntegrationTest.java @@ -19,7 +19,6 @@ import com.fasterxml.jackson.core.type.TypeReference; import lombok.extern.slf4j.Slf4j; import org.junit.After; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.device.credentials.lwm2m.NoSecClientCredentials; @@ -37,8 +36,9 @@ import java.util.UUID; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.hasSize; import static org.thingsboard.rest.client.utils.RestJsonConverter.toTimeseries; import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.DOWNLOADED; import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.DOWNLOADING; @@ -174,7 +174,7 @@ public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest { Assert.assertEquals(expectedStatuses, statuses); } finally { - if(client != null) { + if (client != null) { client.destroy(); } } @@ -219,11 +219,10 @@ public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest { * Check the detailed log output to learn how Awaitility polling the API and when exactly expected result appears * */ @Test - @Ignore public void testSoftwareUpdateByObject9() throws Exception { //given - final List expectedStatuses = Collections.unmodifiableList(Arrays.asList( - QUEUED, INITIATED, DOWNLOADING, DOWNLOADING, DOWNLOADING, DOWNLOADED, VERIFIED, UPDATED)); + final List expectedStatuses = List.of( + QUEUED, INITIATED, DOWNLOADING, DOWNLOADING, DOWNLOADING, DOWNLOADED, VERIFIED, UPDATED); createDeviceProfile(OTA_TRANSPORT_CONFIGURATION); NoSecClientCredentials clientCredentials = new NoSecClientCredentials(); @@ -231,39 +230,29 @@ public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest { final Device device = createDevice(clientCredentials); device.setSoftwareId(createSoftware().getId()); - log.warn("Saving by API " + device); - final Device savedDevice = doPost("/api/device", device, Device.class); - Assert.assertNotNull(savedDevice); - log.warn("Device saved by API {}", savedDevice); - - log.warn("AWAIT atMost {} SECONDS on get device by API...", TIMEOUT); - await() - .atMost(TIMEOUT, TimeUnit.SECONDS) - .until(() -> getDeviceFromAPI(device.getId().getId()), is(savedDevice)); - log.warn("Got device by API."); + final Device savedDevice = doPost("/api/device", device, Device.class); //sync call + assertThat(savedDevice).as("saved device").isNotNull(); + assertThat(getDeviceFromAPI(device.getId().getId())).as("fetched device").isEqualTo(savedDevice); //when log.warn("Init the client..."); client = new LwM2MTestClient(executor, "OTA_" + ENDPOINT); client.init(SECURITY, COAP_CONFIG); - log.warn("Init done"); log.warn("AWAIT atMost {} SECONDS on timeseries List by API with list size {}...", TIMEOUT, expectedStatuses.size()); - await() + List ts = await("await on timeseries") .atMost(30, TimeUnit.SECONDS) - .until(() -> getSwStateTelemetryFromAPI(device.getId().getId()) - .size(), is(expectedStatuses.size())); - log.warn("Got an expected await condition!"); + .until(() -> getSwStateTelemetryFromAPI(device.getId().getId()), hasSize(expectedStatuses.size())); + log.warn("Got the ts: {}", ts); - //then - log.warn("Fetching ts for the final asserts"); - List ts = getSwStateTelemetryFromAPI(device.getId().getId()); - log.warn("Got an ts {}", ts); + ts.sort(Comparator.comparingLong(TsKvEntry::getTs)); + log.warn("Ts ordered: {}", ts); + ts.forEach((x) -> log.warn("ts: {} ", x)); - List statuses = ts.stream().sorted(Comparator.comparingLong(TsKvEntry::getTs)).map(KvEntry::getValueAsString).map(OtaPackageUpdateStatus::valueOf).collect(Collectors.toList()); - log.warn("Converted ts to statuses {}", statuses); + List statuses = ts.stream().map(KvEntry::getValueAsString).map(OtaPackageUpdateStatus::valueOf).collect(Collectors.toList()); + log.warn("Converted ts to statuses: {}", statuses); - Assert.assertEquals(expectedStatuses, statuses); + assertThat(statuses).isEqualTo(expectedStatuses); } private Device getDeviceFromAPI(UUID deviceId) throws Exception {