Clean up cache in case entity was renamed
This commit is contained in:
		
							parent
							
								
									27c9ad95e1
								
							
						
					
					
						commit
						612a17af3b
					
				@ -16,7 +16,6 @@
 | 
			
		||||
package org.thingsboard.server.dao.asset;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
import com.google.common.base.Function;
 | 
			
		||||
import com.google.common.util.concurrent.Futures;
 | 
			
		||||
import com.google.common.util.concurrent.ListenableFuture;
 | 
			
		||||
import com.google.common.util.concurrent.MoreExecutors;
 | 
			
		||||
@ -46,11 +45,10 @@ import org.thingsboard.server.common.data.id.EntityId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.page.PageData;
 | 
			
		||||
import org.thingsboard.server.common.data.page.PageLink;
 | 
			
		||||
import org.thingsboard.server.common.data.page.TimePageLink;
 | 
			
		||||
import org.thingsboard.server.common.data.relation.EntityRelation;
 | 
			
		||||
import org.thingsboard.server.common.data.relation.EntitySearchDirection;
 | 
			
		||||
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration;
 | 
			
		||||
import org.thingsboard.server.common.data.relation.RelationTypeGroup;
 | 
			
		||||
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration;
 | 
			
		||||
import org.thingsboard.server.dao.customer.CustomerDao;
 | 
			
		||||
import org.thingsboard.server.dao.entity.AbstractEntityService;
 | 
			
		||||
import org.thingsboard.server.dao.exception.DataValidationException;
 | 
			
		||||
@ -59,8 +57,8 @@ import org.thingsboard.server.dao.service.PaginatedRemover;
 | 
			
		||||
import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
 | 
			
		||||
import org.thingsboard.server.dao.tenant.TenantDao;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.Comparator;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
@ -180,15 +178,16 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ
 | 
			
		||||
            throw new RuntimeException("Exception while finding entity views for assetId [" + assetId + "]", e);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        List<Object> list = new ArrayList<>();
 | 
			
		||||
        list.add(asset.getTenantId());
 | 
			
		||||
        list.add(asset.getName());
 | 
			
		||||
        Cache cache = cacheManager.getCache(ASSET_CACHE);
 | 
			
		||||
        cache.evict(list);
 | 
			
		||||
        removeAssetFromCacheByName(asset.getTenantId(), asset.getName());
 | 
			
		||||
 | 
			
		||||
        assetDao.removeById(tenantId, assetId.getId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void removeAssetFromCacheByName(TenantId tenantId, String name) {
 | 
			
		||||
        Cache cache = cacheManager.getCache(ASSET_CACHE);
 | 
			
		||||
        cache.evict(Arrays.asList(tenantId, name));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public PageData<Asset> findAssetsByTenantId(TenantId tenantId, PageLink pageLink) {
 | 
			
		||||
        log.trace("Executing findAssetsByTenantId, tenantId [{}], pageLink [{}]", tenantId, pageLink);
 | 
			
		||||
@ -397,6 +396,13 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
                protected void validateUpdate(TenantId tenantId, Asset asset) {
 | 
			
		||||
                    Asset old = assetDao.findById(asset.getTenantId(), asset.getId().getId());
 | 
			
		||||
                    if (old == null) {
 | 
			
		||||
                        throw new DataValidationException("Can't update non existing asset!");
 | 
			
		||||
                    }
 | 
			
		||||
                    if (!old.getName().equals(asset.getName())) {
 | 
			
		||||
                        removeAssetFromCacheByName(tenantId, old.getName());
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
 | 
			
		||||
@ -84,6 +84,7 @@ import org.thingsboard.common.util.JacksonUtil;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.Comparator;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
@ -245,7 +246,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
 | 
			
		||||
            ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
 | 
			
		||||
            if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_name_unq_key")) {
 | 
			
		||||
                // remove device from cache in case null value cached in the distributed redis.
 | 
			
		||||
                removeDeviceFromCache(device.getTenantId(), device.getName());
 | 
			
		||||
                removeDeviceFromCacheByName(device.getTenantId(), device.getName());
 | 
			
		||||
                throw new DataValidationException("Device with such name already exists!");
 | 
			
		||||
            } else {
 | 
			
		||||
                throw t;
 | 
			
		||||
@ -322,17 +323,14 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
 | 
			
		||||
        }
 | 
			
		||||
        deleteEntityRelations(tenantId, deviceId);
 | 
			
		||||
 | 
			
		||||
        removeDeviceFromCache(tenantId, device.getName());
 | 
			
		||||
        removeDeviceFromCacheByName(tenantId, device.getName());
 | 
			
		||||
 | 
			
		||||
        deviceDao.removeById(tenantId, deviceId.getId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void removeDeviceFromCache(TenantId tenantId, String name) {
 | 
			
		||||
        List<Object> list = new ArrayList<>();
 | 
			
		||||
        list.add(tenantId);
 | 
			
		||||
        list.add(name);
 | 
			
		||||
    private void removeDeviceFromCacheByName(TenantId tenantId, String name) {
 | 
			
		||||
        Cache cache = cacheManager.getCache(DEVICE_CACHE);
 | 
			
		||||
        cache.evict(list);
 | 
			
		||||
        cache.evict(Arrays.asList(tenantId, name));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@ -671,6 +669,9 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
 | 
			
		||||
                    if (old == null) {
 | 
			
		||||
                        throw new DataValidationException("Can't update non existing device!");
 | 
			
		||||
                    }
 | 
			
		||||
                    if (!old.getName().equals(device.getName())) {
 | 
			
		||||
                        removeDeviceFromCacheByName(tenantId, old.getName());
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
 | 
			
		||||
@ -58,7 +58,6 @@ import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.UserId;
 | 
			
		||||
import org.thingsboard.server.common.data.page.PageData;
 | 
			
		||||
import org.thingsboard.server.common.data.page.PageLink;
 | 
			
		||||
import org.thingsboard.server.common.data.page.TimePageLink;
 | 
			
		||||
import org.thingsboard.server.common.data.relation.EntityRelation;
 | 
			
		||||
import org.thingsboard.server.common.data.relation.EntitySearchDirection;
 | 
			
		||||
import org.thingsboard.server.common.data.relation.RelationTypeGroup;
 | 
			
		||||
@ -80,6 +79,7 @@ import javax.annotation.PostConstruct;
 | 
			
		||||
import java.net.InetSocketAddress;
 | 
			
		||||
import java.net.Proxy;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.Comparator;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
@ -222,17 +222,18 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic
 | 
			
		||||
 | 
			
		||||
        Edge edge = edgeDao.findById(tenantId, edgeId.getId());
 | 
			
		||||
 | 
			
		||||
        List<Object> list = new ArrayList<>();
 | 
			
		||||
        list.add(edge.getTenantId());
 | 
			
		||||
        list.add(edge.getName());
 | 
			
		||||
        Cache cache = cacheManager.getCache(EDGE_CACHE);
 | 
			
		||||
        cache.evict(list);
 | 
			
		||||
 | 
			
		||||
        deleteEntityRelations(tenantId, edgeId);
 | 
			
		||||
 | 
			
		||||
        removeEdgeFromCacheByName(edge.getTenantId(), edge.getName());
 | 
			
		||||
 | 
			
		||||
        edgeDao.removeById(tenantId, edgeId.getId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void removeEdgeFromCacheByName(TenantId tenantId, String name) {
 | 
			
		||||
        Cache cache = cacheManager.getCache(EDGE_CACHE);
 | 
			
		||||
        cache.evict(Arrays.asList(tenantId, name));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public PageData<Edge> findEdgesByTenantId(TenantId tenantId, PageLink pageLink) {
 | 
			
		||||
        log.trace("Executing findEdgesByTenantId, tenantId [{}], pageLink [{}]", tenantId, pageLink);
 | 
			
		||||
@ -423,6 +424,10 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
                protected void validateUpdate(TenantId tenantId, Edge edge) {
 | 
			
		||||
                    Edge old = edgeDao.findById(edge.getTenantId(), edge.getId().getId());
 | 
			
		||||
                    if (!old.getName().equals(edge.getName())) {
 | 
			
		||||
                        removeEdgeFromCacheByName(tenantId, old.getName());
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
 | 
			
		||||
@ -632,4 +632,25 @@ public abstract class BaseAssetServiceTest extends AbstractServiceTest {
 | 
			
		||||
        customerService.deleteCustomer(tenantId, customerId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testCleanCacheIfAssetRenamed() {
 | 
			
		||||
        String assetNameBeforeRename = RandomStringUtils.randomAlphanumeric(15);
 | 
			
		||||
        String assetNameAfterRename = RandomStringUtils.randomAlphanumeric(15);
 | 
			
		||||
 | 
			
		||||
        Asset asset = new Asset();
 | 
			
		||||
        asset.setTenantId(tenantId);
 | 
			
		||||
        asset.setName(assetNameBeforeRename);
 | 
			
		||||
        asset.setType("default");
 | 
			
		||||
        assetService.saveAsset(asset);
 | 
			
		||||
 | 
			
		||||
        Asset savedAsset = assetService.findAssetByTenantIdAndName(tenantId, assetNameBeforeRename);
 | 
			
		||||
 | 
			
		||||
        savedAsset.setName(assetNameAfterRename);
 | 
			
		||||
        assetService.saveAsset(savedAsset);
 | 
			
		||||
 | 
			
		||||
        Asset renamedAsset = assetService.findAssetByTenantIdAndName(tenantId, assetNameBeforeRename);
 | 
			
		||||
 | 
			
		||||
        Assert.assertNull("Can't find asset by name in cache if it was renamed", renamedAsset);
 | 
			
		||||
        assetService.deleteAsset(tenantId, savedAsset.getId());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -793,4 +793,25 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest {
 | 
			
		||||
        customerService.deleteCustomer(tenantId, customerId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testCleanCacheIfDeviceRenamed() {
 | 
			
		||||
        String deviceNameBeforeRename = RandomStringUtils.randomAlphanumeric(15);
 | 
			
		||||
        String deviceNameAfterRename = RandomStringUtils.randomAlphanumeric(15);
 | 
			
		||||
 | 
			
		||||
        Device device = new Device();
 | 
			
		||||
        device.setTenantId(tenantId);
 | 
			
		||||
        device.setName(deviceNameBeforeRename);
 | 
			
		||||
        device.setType("default");
 | 
			
		||||
        deviceService.saveDevice(device);
 | 
			
		||||
 | 
			
		||||
        Device savedDevice = deviceService.findDeviceByTenantIdAndName(tenantId, deviceNameBeforeRename);
 | 
			
		||||
 | 
			
		||||
        savedDevice.setName(deviceNameAfterRename);
 | 
			
		||||
        deviceService.saveDevice(savedDevice);
 | 
			
		||||
 | 
			
		||||
        Device renamedDevice = deviceService.findDeviceByTenantIdAndName(tenantId, deviceNameBeforeRename);
 | 
			
		||||
 | 
			
		||||
        Assert.assertNull("Can't find device by name in cache if it was renamed", renamedDevice);
 | 
			
		||||
        deviceService.deleteDevice(tenantId, savedDevice.getId());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -595,4 +595,23 @@ public abstract class BaseEdgeServiceTest extends AbstractServiceTest {
 | 
			
		||||
        return edge;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testCleanCacheIfEdgeRenamed() {
 | 
			
		||||
        String edgeNameBeforeRename = RandomStringUtils.randomAlphanumeric(15);
 | 
			
		||||
        String edgeNameAfterRename = RandomStringUtils.randomAlphanumeric(15);
 | 
			
		||||
 | 
			
		||||
        Edge edge = constructEdge(tenantId, edgeNameBeforeRename, "default");
 | 
			
		||||
        edgeService.saveEdge(edge);
 | 
			
		||||
 | 
			
		||||
        Edge savedEdge = edgeService.findEdgeByTenantIdAndName(tenantId, edgeNameBeforeRename);
 | 
			
		||||
 | 
			
		||||
        savedEdge.setName(edgeNameAfterRename);
 | 
			
		||||
        edgeService.saveEdge(savedEdge);
 | 
			
		||||
 | 
			
		||||
        Edge renamedEdge = edgeService.findEdgeByTenantIdAndName(tenantId, edgeNameBeforeRename);
 | 
			
		||||
 | 
			
		||||
        Assert.assertNull("Can't find edge by name in cache if it was renamed", renamedEdge);
 | 
			
		||||
        edgeService.deleteEdge(tenantId, savedEdge.getId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user