Merge pull request #10968 from thingsboard/fix/tenant-not-found
Fix "Tenant not found" errors on tenant deletion
This commit is contained in:
commit
3d1e133080
@ -17,6 +17,7 @@ package org.thingsboard.server.service.notification.rule.cache;
|
|||||||
|
|
||||||
import com.github.benmanes.caffeine.cache.Cache;
|
import com.github.benmanes.caffeine.cache.Cache;
|
||||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@ -29,7 +30,6 @@ import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent;
|
|||||||
import org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg;
|
import org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg;
|
||||||
import org.thingsboard.server.dao.notification.NotificationRuleService;
|
import org.thingsboard.server.dao.notification.NotificationRuleService;
|
||||||
|
|
||||||
import jakarta.annotation.PostConstruct;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -71,7 +71,14 @@ public class DefaultNotificationRulesCache implements NotificationRulesCache {
|
|||||||
if (event.getEvent() == ComponentLifecycleEvent.DELETED) {
|
if (event.getEvent() == ComponentLifecycleEvent.DELETED) {
|
||||||
lock.writeLock().lock(); // locking in case rules for tenant are fetched while evicting
|
lock.writeLock().lock(); // locking in case rules for tenant are fetched while evicting
|
||||||
try {
|
try {
|
||||||
evict(event.getTenantId());
|
for (var triggerType : NotificationRuleTriggerType.values()) {
|
||||||
|
String key = key(event.getTenantId(), triggerType);
|
||||||
|
/*
|
||||||
|
* temporarily putting empty value because right after tenant deletion
|
||||||
|
* the rules are still in the db, we don't want them to be fetched
|
||||||
|
* */
|
||||||
|
cache.put(key, Collections.emptyList());
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
lock.writeLock().unlock();
|
lock.writeLock().unlock();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,20 @@
|
|||||||
|
/**
|
||||||
|
* Copyright © 2016-2024 The Thingsboard Authors
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.thingsboard.server.dao.eventsourcing;
|
||||||
|
|
||||||
|
public enum ActionCause {
|
||||||
|
TENANT_DELETION
|
||||||
|
}
|
||||||
@ -17,18 +17,20 @@ package org.thingsboard.server.dao.eventsourcing;
|
|||||||
|
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.thingsboard.server.common.data.id.EdgeId;
|
|
||||||
import org.thingsboard.server.common.data.id.EntityId;
|
import org.thingsboard.server.common.data.id.EntityId;
|
||||||
import org.thingsboard.server.common.data.id.TenantId;
|
import org.thingsboard.server.common.data.id.TenantId;
|
||||||
|
|
||||||
@Builder
|
@Builder
|
||||||
@Data
|
@Data
|
||||||
public class DeleteEntityEvent<T> {
|
public class DeleteEntityEvent<T> {
|
||||||
|
|
||||||
private final TenantId tenantId;
|
private final TenantId tenantId;
|
||||||
private final EntityId entityId;
|
private final EntityId entityId;
|
||||||
private final T entity;
|
private final T entity;
|
||||||
private final String body;
|
private final String body;
|
||||||
|
private final ActionCause cause;
|
||||||
|
|
||||||
@Builder.Default
|
@Builder.Default
|
||||||
private final long ts = System.currentTimeMillis();
|
private final long ts = System.currentTimeMillis();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -27,6 +27,7 @@ import org.thingsboard.server.common.data.housekeeper.HousekeeperTask;
|
|||||||
import org.thingsboard.server.common.data.id.EntityId;
|
import org.thingsboard.server.common.data.id.EntityId;
|
||||||
import org.thingsboard.server.common.data.id.TenantId;
|
import org.thingsboard.server.common.data.id.TenantId;
|
||||||
import org.thingsboard.server.common.msg.housekeeper.HousekeeperClient;
|
import org.thingsboard.server.common.msg.housekeeper.HousekeeperClient;
|
||||||
|
import org.thingsboard.server.dao.eventsourcing.ActionCause;
|
||||||
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent;
|
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent;
|
||||||
import org.thingsboard.server.dao.relation.RelationService;
|
import org.thingsboard.server.dao.relation.RelationService;
|
||||||
|
|
||||||
@ -59,7 +60,7 @@ public class CleanUpService {
|
|||||||
if (!skippedEntities.contains(entityType)) {
|
if (!skippedEntities.contains(entityType)) {
|
||||||
cleanUpRelatedData(tenantId, entityId);
|
cleanUpRelatedData(tenantId, entityId);
|
||||||
}
|
}
|
||||||
if (entityType == EntityType.USER) {
|
if (entityType == EntityType.USER && event.getCause() != ActionCause.TENANT_DELETION) {
|
||||||
submitTask(HousekeeperTask.unassignAlarms((User) event.getEntity()));
|
submitTask(HousekeeperTask.unassignAlarms((User) event.getEntity()));
|
||||||
}
|
}
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
|
|||||||
@ -39,8 +39,6 @@ import org.thingsboard.server.dao.entity.AbstractCachedEntityService;
|
|||||||
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent;
|
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent;
|
||||||
import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent;
|
import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent;
|
||||||
import org.thingsboard.server.dao.mobile.MobileAppSettingsService;
|
import org.thingsboard.server.dao.mobile.MobileAppSettingsService;
|
||||||
import org.thingsboard.server.dao.notification.NotificationRequestService;
|
|
||||||
import org.thingsboard.server.dao.notification.NotificationRuleService;
|
|
||||||
import org.thingsboard.server.dao.notification.NotificationSettingsService;
|
import org.thingsboard.server.dao.notification.NotificationSettingsService;
|
||||||
import org.thingsboard.server.dao.service.PaginatedRemover;
|
import org.thingsboard.server.dao.service.PaginatedRemover;
|
||||||
import org.thingsboard.server.dao.service.Validator;
|
import org.thingsboard.server.dao.service.Validator;
|
||||||
@ -168,8 +166,10 @@ public class TenantServiceImpl extends AbstractCachedEntityService<TenantId, Ten
|
|||||||
adminSettingsService.deleteAdminSettingsByTenantId(tenantId);
|
adminSettingsService.deleteAdminSettingsByTenantId(tenantId);
|
||||||
mobileAppSettingsService.deleteByTenantId(tenantId);
|
mobileAppSettingsService.deleteByTenantId(tenantId);
|
||||||
notificationSettingsService.deleteNotificationSettings(tenantId);
|
notificationSettingsService.deleteNotificationSettings(tenantId);
|
||||||
|
|
||||||
tenantDao.removeById(tenantId, tenantId.getId());
|
tenantDao.removeById(tenantId, tenantId.getId());
|
||||||
publishEvictEvent(new TenantEvictEvent(tenantId, true));
|
publishEvictEvent(new TenantEvictEvent(tenantId, true));
|
||||||
|
eventPublisher.publishEvent(DeleteEntityEvent.builder().tenantId(tenantId).entityId(tenantId).entity(tenant).build());
|
||||||
|
|
||||||
cleanUpService.removeTenantEntities(tenantId, // don't forget to implement deleteEntity from EntityDaoService when adding entity type to this list
|
cleanUpService.removeTenantEntities(tenantId, // don't forget to implement deleteEntity from EntityDaoService when adding entity type to this list
|
||||||
EntityType.ENTITY_VIEW, EntityType.WIDGETS_BUNDLE, EntityType.WIDGET_TYPE,
|
EntityType.ENTITY_VIEW, EntityType.WIDGETS_BUNDLE, EntityType.WIDGET_TYPE,
|
||||||
@ -179,7 +179,6 @@ public class TenantServiceImpl extends AbstractCachedEntityService<TenantId, Ten
|
|||||||
EntityType.NOTIFICATION_REQUEST, EntityType.NOTIFICATION_RULE, EntityType.NOTIFICATION_TEMPLATE,
|
EntityType.NOTIFICATION_REQUEST, EntityType.NOTIFICATION_RULE, EntityType.NOTIFICATION_TEMPLATE,
|
||||||
EntityType.NOTIFICATION_TARGET, EntityType.QUEUE_STATS, EntityType.CUSTOMER
|
EntityType.NOTIFICATION_TARGET, EntityType.QUEUE_STATS, EntityType.CUSTOMER
|
||||||
);
|
);
|
||||||
eventPublisher.publishEvent(DeleteEntityEvent.builder().tenantId(tenantId).entityId(tenantId).entity(tenant).build());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -53,6 +53,7 @@ import org.thingsboard.server.common.data.settings.UserSettings;
|
|||||||
import org.thingsboard.server.common.data.settings.UserSettingsType;
|
import org.thingsboard.server.common.data.settings.UserSettingsType;
|
||||||
import org.thingsboard.server.dao.entity.AbstractCachedEntityService;
|
import org.thingsboard.server.dao.entity.AbstractCachedEntityService;
|
||||||
import org.thingsboard.server.dao.entity.EntityCountService;
|
import org.thingsboard.server.dao.entity.EntityCountService;
|
||||||
|
import org.thingsboard.server.dao.eventsourcing.ActionCause;
|
||||||
import org.thingsboard.server.dao.eventsourcing.ActionEntityEvent;
|
import org.thingsboard.server.dao.eventsourcing.ActionEntityEvent;
|
||||||
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent;
|
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent;
|
||||||
import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent;
|
import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent;
|
||||||
@ -293,6 +294,10 @@ public class UserServiceImpl extends AbstractCachedEntityService<UserCacheKey, U
|
|||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteUser(TenantId tenantId, User user) {
|
public void deleteUser(TenantId tenantId, User user) {
|
||||||
|
deleteUser(tenantId, user, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteUser(TenantId tenantId, User user, ActionCause cause) {
|
||||||
Objects.requireNonNull(user, "User is null");
|
Objects.requireNonNull(user, "User is null");
|
||||||
UserId userId = user.getId();
|
UserId userId = user.getId();
|
||||||
log.trace("[{}] Executing deleteUser [{}]", tenantId, userId);
|
log.trace("[{}] Executing deleteUser [{}]", tenantId, userId);
|
||||||
@ -307,7 +312,9 @@ public class UserServiceImpl extends AbstractCachedEntityService<UserCacheKey, U
|
|||||||
eventPublisher.publishEvent(DeleteEntityEvent.builder()
|
eventPublisher.publishEvent(DeleteEntityEvent.builder()
|
||||||
.tenantId(tenantId)
|
.tenantId(tenantId)
|
||||||
.entityId(userId)
|
.entityId(userId)
|
||||||
.entity(user).build());
|
.entity(user)
|
||||||
|
.cause(cause)
|
||||||
|
.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -564,7 +571,7 @@ public class UserServiceImpl extends AbstractCachedEntityService<UserCacheKey, U
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void removeEntity(TenantId tenantId, User user) {
|
protected void removeEntity(TenantId tenantId, User user) {
|
||||||
deleteUser(tenantId, user);
|
deleteUser(tenantId, user, ActionCause.TENANT_DELETION);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user