Merge pull request #10968 from thingsboard/fix/tenant-not-found

Fix "Tenant not found" errors on tenant deletion
This commit is contained in:
Viacheslav Klimov 2024-06-12 14:11:33 +03:00 committed by GitHub
commit 3d1e133080
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 45 additions and 9 deletions

View File

@ -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.Caffeine;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.dao.notification.NotificationRuleService;
import jakarta.annotation.PostConstruct;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -71,7 +71,14 @@ public class DefaultNotificationRulesCache implements NotificationRulesCache {
if (event.getEvent() == ComponentLifecycleEvent.DELETED) {
lock.writeLock().lock(); // locking in case rules for tenant are fetched while evicting
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 {
lock.writeLock().unlock();
}

View File

@ -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
}

View File

@ -17,18 +17,20 @@ package org.thingsboard.server.dao.eventsourcing;
import lombok.Builder;
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.TenantId;
@Builder
@Data
public class DeleteEntityEvent<T> {
private final TenantId tenantId;
private final EntityId entityId;
private final T entity;
private final String body;
private final ActionCause cause;
@Builder.Default
private final long ts = System.currentTimeMillis();
}

View File

@ -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.TenantId;
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.relation.RelationService;
@ -59,7 +60,7 @@ public class CleanUpService {
if (!skippedEntities.contains(entityType)) {
cleanUpRelatedData(tenantId, entityId);
}
if (entityType == EntityType.USER) {
if (entityType == EntityType.USER && event.getCause() != ActionCause.TENANT_DELETION) {
submitTask(HousekeeperTask.unassignAlarms((User) event.getEntity()));
}
} catch (Throwable e) {

View File

@ -39,8 +39,6 @@ import org.thingsboard.server.dao.entity.AbstractCachedEntityService;
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent;
import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent;
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.service.PaginatedRemover;
import org.thingsboard.server.dao.service.Validator;
@ -168,8 +166,10 @@ public class TenantServiceImpl extends AbstractCachedEntityService<TenantId, Ten
adminSettingsService.deleteAdminSettingsByTenantId(tenantId);
mobileAppSettingsService.deleteByTenantId(tenantId);
notificationSettingsService.deleteNotificationSettings(tenantId);
tenantDao.removeById(tenantId, tenantId.getId());
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
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_TARGET, EntityType.QUEUE_STATS, EntityType.CUSTOMER
);
eventPublisher.publishEvent(DeleteEntityEvent.builder().tenantId(tenantId).entityId(tenantId).entity(tenant).build());
}
@Override

View File

@ -53,6 +53,7 @@ import org.thingsboard.server.common.data.settings.UserSettings;
import org.thingsboard.server.common.data.settings.UserSettingsType;
import org.thingsboard.server.dao.entity.AbstractCachedEntityService;
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.DeleteEntityEvent;
import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent;
@ -293,6 +294,10 @@ public class UserServiceImpl extends AbstractCachedEntityService<UserCacheKey, U
@Override
@Transactional
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");
UserId userId = user.getId();
log.trace("[{}] Executing deleteUser [{}]", tenantId, userId);
@ -307,7 +312,9 @@ public class UserServiceImpl extends AbstractCachedEntityService<UserCacheKey, U
eventPublisher.publishEvent(DeleteEntityEvent.builder()
.tenantId(tenantId)
.entityId(userId)
.entity(user).build());
.entity(user)
.cause(cause)
.build());
}
@Override
@ -564,7 +571,7 @@ public class UserServiceImpl extends AbstractCachedEntityService<UserCacheKey, U
@Override
protected void removeEntity(TenantId tenantId, User user) {
deleteUser(tenantId, user);
deleteUser(tenantId, user, ActionCause.TENANT_DELETION);
}
};