Commit 612a17af3bc95ccc84e9f738d4121390031436b1
Committed by
Andrew Shvayka
1 parent
27c9ad95
Clean up cache in case entity was renamed
Showing
6 changed files
with
96 additions
and
23 deletions
... | ... | @@ -16,7 +16,6 @@ |
16 | 16 | package org.thingsboard.server.dao.asset; |
17 | 17 | |
18 | 18 | |
19 | -import com.google.common.base.Function; | |
20 | 19 | import com.google.common.util.concurrent.Futures; |
21 | 20 | import com.google.common.util.concurrent.ListenableFuture; |
22 | 21 | import com.google.common.util.concurrent.MoreExecutors; |
... | ... | @@ -46,11 +45,10 @@ import org.thingsboard.server.common.data.id.EntityId; |
46 | 45 | import org.thingsboard.server.common.data.id.TenantId; |
47 | 46 | import org.thingsboard.server.common.data.page.PageData; |
48 | 47 | import org.thingsboard.server.common.data.page.PageLink; |
49 | -import org.thingsboard.server.common.data.page.TimePageLink; | |
50 | 48 | import org.thingsboard.server.common.data.relation.EntityRelation; |
51 | 49 | import org.thingsboard.server.common.data.relation.EntitySearchDirection; |
52 | -import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration; | |
53 | 50 | import org.thingsboard.server.common.data.relation.RelationTypeGroup; |
51 | +import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration; | |
54 | 52 | import org.thingsboard.server.dao.customer.CustomerDao; |
55 | 53 | import org.thingsboard.server.dao.entity.AbstractEntityService; |
56 | 54 | import org.thingsboard.server.dao.exception.DataValidationException; |
... | ... | @@ -59,8 +57,8 @@ import org.thingsboard.server.dao.service.PaginatedRemover; |
59 | 57 | import org.thingsboard.server.dao.tenant.TbTenantProfileCache; |
60 | 58 | import org.thingsboard.server.dao.tenant.TenantDao; |
61 | 59 | |
62 | -import javax.annotation.Nullable; | |
63 | 60 | import java.util.ArrayList; |
61 | +import java.util.Arrays; | |
64 | 62 | import java.util.Collections; |
65 | 63 | import java.util.Comparator; |
66 | 64 | import java.util.List; |
... | ... | @@ -180,15 +178,16 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ |
180 | 178 | throw new RuntimeException("Exception while finding entity views for assetId [" + assetId + "]", e); |
181 | 179 | } |
182 | 180 | |
183 | - List<Object> list = new ArrayList<>(); | |
184 | - list.add(asset.getTenantId()); | |
185 | - list.add(asset.getName()); | |
186 | - Cache cache = cacheManager.getCache(ASSET_CACHE); | |
187 | - cache.evict(list); | |
181 | + removeAssetFromCacheByName(asset.getTenantId(), asset.getName()); | |
188 | 182 | |
189 | 183 | assetDao.removeById(tenantId, assetId.getId()); |
190 | 184 | } |
191 | 185 | |
186 | + private void removeAssetFromCacheByName(TenantId tenantId, String name) { | |
187 | + Cache cache = cacheManager.getCache(ASSET_CACHE); | |
188 | + cache.evict(Arrays.asList(tenantId, name)); | |
189 | + } | |
190 | + | |
192 | 191 | @Override |
193 | 192 | public PageData<Asset> findAssetsByTenantId(TenantId tenantId, PageLink pageLink) { |
194 | 193 | log.trace("Executing findAssetsByTenantId, tenantId [{}], pageLink [{}]", tenantId, pageLink); |
... | ... | @@ -397,6 +396,13 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ |
397 | 396 | |
398 | 397 | @Override |
399 | 398 | protected void validateUpdate(TenantId tenantId, Asset asset) { |
399 | + Asset old = assetDao.findById(asset.getTenantId(), asset.getId().getId()); | |
400 | + if (old == null) { | |
401 | + throw new DataValidationException("Can't update non existing asset!"); | |
402 | + } | |
403 | + if (!old.getName().equals(asset.getName())) { | |
404 | + removeAssetFromCacheByName(tenantId, old.getName()); | |
405 | + } | |
400 | 406 | } |
401 | 407 | |
402 | 408 | @Override | ... | ... |
... | ... | @@ -84,6 +84,7 @@ import org.thingsboard.common.util.JacksonUtil; |
84 | 84 | |
85 | 85 | import javax.annotation.Nullable; |
86 | 86 | import java.util.ArrayList; |
87 | +import java.util.Arrays; | |
87 | 88 | import java.util.Collections; |
88 | 89 | import java.util.Comparator; |
89 | 90 | import java.util.List; |
... | ... | @@ -245,7 +246,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
245 | 246 | ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); |
246 | 247 | if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_name_unq_key")) { |
247 | 248 | // remove device from cache in case null value cached in the distributed redis. |
248 | - removeDeviceFromCache(device.getTenantId(), device.getName()); | |
249 | + removeDeviceFromCacheByName(device.getTenantId(), device.getName()); | |
249 | 250 | throw new DataValidationException("Device with such name already exists!"); |
250 | 251 | } else { |
251 | 252 | throw t; |
... | ... | @@ -322,17 +323,14 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
322 | 323 | } |
323 | 324 | deleteEntityRelations(tenantId, deviceId); |
324 | 325 | |
325 | - removeDeviceFromCache(tenantId, device.getName()); | |
326 | + removeDeviceFromCacheByName(tenantId, device.getName()); | |
326 | 327 | |
327 | 328 | deviceDao.removeById(tenantId, deviceId.getId()); |
328 | 329 | } |
329 | 330 | |
330 | - private void removeDeviceFromCache(TenantId tenantId, String name) { | |
331 | - List<Object> list = new ArrayList<>(); | |
332 | - list.add(tenantId); | |
333 | - list.add(name); | |
331 | + private void removeDeviceFromCacheByName(TenantId tenantId, String name) { | |
334 | 332 | Cache cache = cacheManager.getCache(DEVICE_CACHE); |
335 | - cache.evict(list); | |
333 | + cache.evict(Arrays.asList(tenantId, name)); | |
336 | 334 | } |
337 | 335 | |
338 | 336 | @Override |
... | ... | @@ -671,6 +669,9 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
671 | 669 | if (old == null) { |
672 | 670 | throw new DataValidationException("Can't update non existing device!"); |
673 | 671 | } |
672 | + if (!old.getName().equals(device.getName())) { | |
673 | + removeDeviceFromCacheByName(tenantId, old.getName()); | |
674 | + } | |
674 | 675 | } |
675 | 676 | |
676 | 677 | @Override | ... | ... |
... | ... | @@ -58,7 +58,6 @@ import org.thingsboard.server.common.data.id.TenantId; |
58 | 58 | import org.thingsboard.server.common.data.id.UserId; |
59 | 59 | import org.thingsboard.server.common.data.page.PageData; |
60 | 60 | import org.thingsboard.server.common.data.page.PageLink; |
61 | -import org.thingsboard.server.common.data.page.TimePageLink; | |
62 | 61 | import org.thingsboard.server.common.data.relation.EntityRelation; |
63 | 62 | import org.thingsboard.server.common.data.relation.EntitySearchDirection; |
64 | 63 | import org.thingsboard.server.common.data.relation.RelationTypeGroup; |
... | ... | @@ -80,6 +79,7 @@ import javax.annotation.PostConstruct; |
80 | 79 | import java.net.InetSocketAddress; |
81 | 80 | import java.net.Proxy; |
82 | 81 | import java.util.ArrayList; |
82 | +import java.util.Arrays; | |
83 | 83 | import java.util.Collections; |
84 | 84 | import java.util.Comparator; |
85 | 85 | import java.util.HashMap; |
... | ... | @@ -222,17 +222,18 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic |
222 | 222 | |
223 | 223 | Edge edge = edgeDao.findById(tenantId, edgeId.getId()); |
224 | 224 | |
225 | - List<Object> list = new ArrayList<>(); | |
226 | - list.add(edge.getTenantId()); | |
227 | - list.add(edge.getName()); | |
228 | - Cache cache = cacheManager.getCache(EDGE_CACHE); | |
229 | - cache.evict(list); | |
230 | - | |
231 | 225 | deleteEntityRelations(tenantId, edgeId); |
232 | 226 | |
227 | + removeEdgeFromCacheByName(edge.getTenantId(), edge.getName()); | |
228 | + | |
233 | 229 | edgeDao.removeById(tenantId, edgeId.getId()); |
234 | 230 | } |
235 | 231 | |
232 | + private void removeEdgeFromCacheByName(TenantId tenantId, String name) { | |
233 | + Cache cache = cacheManager.getCache(EDGE_CACHE); | |
234 | + cache.evict(Arrays.asList(tenantId, name)); | |
235 | + } | |
236 | + | |
236 | 237 | @Override |
237 | 238 | public PageData<Edge> findEdgesByTenantId(TenantId tenantId, PageLink pageLink) { |
238 | 239 | log.trace("Executing findEdgesByTenantId, tenantId [{}], pageLink [{}]", tenantId, pageLink); |
... | ... | @@ -423,6 +424,10 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic |
423 | 424 | |
424 | 425 | @Override |
425 | 426 | protected void validateUpdate(TenantId tenantId, Edge edge) { |
427 | + Edge old = edgeDao.findById(edge.getTenantId(), edge.getId().getId()); | |
428 | + if (!old.getName().equals(edge.getName())) { | |
429 | + removeEdgeFromCacheByName(tenantId, old.getName()); | |
430 | + } | |
426 | 431 | } |
427 | 432 | |
428 | 433 | @Override | ... | ... |
... | ... | @@ -632,4 +632,25 @@ public abstract class BaseAssetServiceTest extends AbstractServiceTest { |
632 | 632 | customerService.deleteCustomer(tenantId, customerId); |
633 | 633 | } |
634 | 634 | |
635 | + @Test | |
636 | + public void testCleanCacheIfAssetRenamed() { | |
637 | + String assetNameBeforeRename = RandomStringUtils.randomAlphanumeric(15); | |
638 | + String assetNameAfterRename = RandomStringUtils.randomAlphanumeric(15); | |
639 | + | |
640 | + Asset asset = new Asset(); | |
641 | + asset.setTenantId(tenantId); | |
642 | + asset.setName(assetNameBeforeRename); | |
643 | + asset.setType("default"); | |
644 | + assetService.saveAsset(asset); | |
645 | + | |
646 | + Asset savedAsset = assetService.findAssetByTenantIdAndName(tenantId, assetNameBeforeRename); | |
647 | + | |
648 | + savedAsset.setName(assetNameAfterRename); | |
649 | + assetService.saveAsset(savedAsset); | |
650 | + | |
651 | + Asset renamedAsset = assetService.findAssetByTenantIdAndName(tenantId, assetNameBeforeRename); | |
652 | + | |
653 | + Assert.assertNull("Can't find asset by name in cache if it was renamed", renamedAsset); | |
654 | + assetService.deleteAsset(tenantId, savedAsset.getId()); | |
655 | + } | |
635 | 656 | } | ... | ... |
... | ... | @@ -793,4 +793,25 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { |
793 | 793 | customerService.deleteCustomer(tenantId, customerId); |
794 | 794 | } |
795 | 795 | |
796 | + @Test | |
797 | + public void testCleanCacheIfDeviceRenamed() { | |
798 | + String deviceNameBeforeRename = RandomStringUtils.randomAlphanumeric(15); | |
799 | + String deviceNameAfterRename = RandomStringUtils.randomAlphanumeric(15); | |
800 | + | |
801 | + Device device = new Device(); | |
802 | + device.setTenantId(tenantId); | |
803 | + device.setName(deviceNameBeforeRename); | |
804 | + device.setType("default"); | |
805 | + deviceService.saveDevice(device); | |
806 | + | |
807 | + Device savedDevice = deviceService.findDeviceByTenantIdAndName(tenantId, deviceNameBeforeRename); | |
808 | + | |
809 | + savedDevice.setName(deviceNameAfterRename); | |
810 | + deviceService.saveDevice(savedDevice); | |
811 | + | |
812 | + Device renamedDevice = deviceService.findDeviceByTenantIdAndName(tenantId, deviceNameBeforeRename); | |
813 | + | |
814 | + Assert.assertNull("Can't find device by name in cache if it was renamed", renamedDevice); | |
815 | + deviceService.deleteDevice(tenantId, savedDevice.getId()); | |
816 | + } | |
796 | 817 | } | ... | ... |
... | ... | @@ -595,4 +595,23 @@ public abstract class BaseEdgeServiceTest extends AbstractServiceTest { |
595 | 595 | return edge; |
596 | 596 | } |
597 | 597 | |
598 | + @Test | |
599 | + public void testCleanCacheIfEdgeRenamed() { | |
600 | + String edgeNameBeforeRename = RandomStringUtils.randomAlphanumeric(15); | |
601 | + String edgeNameAfterRename = RandomStringUtils.randomAlphanumeric(15); | |
602 | + | |
603 | + Edge edge = constructEdge(tenantId, edgeNameBeforeRename, "default"); | |
604 | + edgeService.saveEdge(edge); | |
605 | + | |
606 | + Edge savedEdge = edgeService.findEdgeByTenantIdAndName(tenantId, edgeNameBeforeRename); | |
607 | + | |
608 | + savedEdge.setName(edgeNameAfterRename); | |
609 | + edgeService.saveEdge(savedEdge); | |
610 | + | |
611 | + Edge renamedEdge = edgeService.findEdgeByTenantIdAndName(tenantId, edgeNameBeforeRename); | |
612 | + | |
613 | + Assert.assertNull("Can't find edge by name in cache if it was renamed", renamedEdge); | |
614 | + edgeService.deleteEdge(tenantId, savedEdge.getId()); | |
615 | + } | |
616 | + | |
598 | 617 | } |
\ No newline at end of file | ... | ... |