Browse Source

Merge 1f885902ce into 21ea755acf

pull/14465/merge
YU XINLE 3 hours ago
committed by GitHub
parent
commit
b472cd2aed
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 50
      dao/src/main/java/org/thingsboard/server/dao/ai/AiModelServiceImpl.java

50
dao/src/main/java/org/thingsboard/server/dao/ai/AiModelServiceImpl.java

@ -4,6 +4,7 @@ package org.thingsboard.server.dao.ai;
import com.google.common.util.concurrent.FluentFuture; import com.google.common.util.concurrent.FluentFuture;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener; import org.springframework.transaction.event.TransactionalEventListener;
@ -29,12 +30,12 @@ import java.util.UUID;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor; import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import static org.thingsboard.server.dao.service.Validator.validatePageLink; import static org.thingsboard.server.dao.service.Validator.validatePageLink;
@Slf4j
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
class AiModelServiceImpl extends CachedVersionedEntityService<AiModelCacheKey, AiModel, AiModelCacheEvictEvent> implements AiModelService { class AiModelServiceImpl extends CachedVersionedEntityService<AiModelCacheKey, AiModel, AiModelCacheEvictEvent> implements AiModelService {
private final DataValidator<AiModel> aiModelValidator; private final DataValidator<AiModel> aiModelValidator;
private final JpaExecutorService jpaExecutor; private final JpaExecutorService jpaExecutor;
private final AiModelDao aiModelDao; private final AiModelDao aiModelDao;
@ -44,9 +45,12 @@ class AiModelServiceImpl extends CachedVersionedEntityService<AiModelCacheKey, A
var cacheKey = event.cacheKey(); var cacheKey = event.cacheKey();
if (event instanceof AiModelCacheEvictEvent.Saved savedEvent) { if (event instanceof AiModelCacheEvictEvent.Saved savedEvent) {
cache.put(cacheKey, savedEvent.savedModel()); cache.put(cacheKey, savedEvent.savedModel());
log.debug("[{}][{}] Cache updated with saved AI Model", cacheKey.tenantId(), cacheKey.aiModelId());
} else if (event instanceof AiModelCacheEvictEvent.Deleted) { } else if (event instanceof AiModelCacheEvictEvent.Deleted) {
cache.evict(cacheKey); cache.evict(cacheKey);
log.debug("[{}][{}] Cache evicted for deleted AI Model", cacheKey.tenantId(), cacheKey.aiModelId());
} else { } else {
log.warn("Unsupported event type received: {}", event.getClass().getSimpleName());
throw new UnsupportedOperationException("Unsupported event type: " + event.getClass().getSimpleName()); throw new UnsupportedOperationException("Unsupported event type: " + event.getClass().getSimpleName());
} }
} }
@ -61,6 +65,7 @@ class AiModelServiceImpl extends CachedVersionedEntityService<AiModelCacheKey, A
public AiModel save(AiModel aiModel, boolean doValidate) { public AiModel save(AiModel aiModel, boolean doValidate) {
AiModel oldAiModel = null; AiModel oldAiModel = null;
if (doValidate) { if (doValidate) {
log.debug("[{}] Validating AI Model: {}", aiModel.getTenantId(), aiModel.getName());
oldAiModel = aiModelValidator.validate(aiModel, AiModel::getTenantId); oldAiModel = aiModelValidator.validate(aiModel, AiModel::getTenantId);
} else if (aiModel.getId() != null) { } else if (aiModel.getId() != null) {
oldAiModel = findAiModelById(aiModel.getTenantId(), aiModel.getId()).orElse(null); oldAiModel = findAiModelById(aiModel.getTenantId(), aiModel.getId()).orElse(null);
@ -68,30 +73,45 @@ class AiModelServiceImpl extends CachedVersionedEntityService<AiModelCacheKey, A
AiModel savedModel; AiModel savedModel;
try { try {
// CR FIX: Changed to debug level
log.debug("[{}] Saving AI Model: name={}, id={}", aiModel.getTenantId(), aiModel.getName(), aiModel.getId());
savedModel = aiModelDao.saveAndFlush(aiModel.getTenantId(), aiModel); savedModel = aiModelDao.saveAndFlush(aiModel.getTenantId(), aiModel);
eventPublisher.publishEvent(SaveEntityEvent.builder().tenantId(savedModel.getTenantId()).entityId(savedModel.getId()) eventPublisher.publishEvent(SaveEntityEvent.builder()
.entity(savedModel).oldEntity(oldAiModel).created(oldAiModel == null).broadcastEvent(true).build()); .tenantId(savedModel.getTenantId())
.entityId(savedModel.getId())
.entity(savedModel)
.oldEntity(oldAiModel)
.created(oldAiModel == null)
.broadcastEvent(true)
.build());
// CR FIX: Changed to debug level
log.debug("[{}][{}] AI Model saved successfully", savedModel.getTenantId(), savedModel.getId());
} catch (Exception e) { } catch (Exception e) {
// CR FIX: Removed e.getMessage() redundancy, kept only the exception object
log.error("[{}][{}] Failed to save AI Model", aiModel.getTenantId(), aiModel.getId(), e);
checkConstraintViolation(e, checkConstraintViolation(e,
"ai_model_name_unq_key", "AI model with such name already exist!", "ai_model_name_unq_key", "AI model with such name already exist!",
"ai_model_external_id_unq_key", "AI model with such external ID already exists!"); "ai_model_external_id_unq_key", "AI model with such external ID already exists!");
throw e; throw e;
} }
var cacheKey = AiModelCacheKey.of(savedModel.getTenantId(), savedModel.getId()); var cacheKey = AiModelCacheKey.of(savedModel.getTenantId(), savedModel.getId());
publishEvictEvent(new AiModelCacheEvictEvent.Saved(cacheKey, savedModel)); publishEvictEvent(new AiModelCacheEvictEvent.Saved(cacheKey, savedModel));
return savedModel; return savedModel;
} }
@Override @Override
public Optional<AiModel> findAiModelById(TenantId tenantId, AiModelId modelId) { public Optional<AiModel> findAiModelById(TenantId tenantId, AiModelId modelId) {
log.debug("[{}] Fetching AI Model by Id: {}", tenantId, modelId);
return Optional.ofNullable(aiModelDao.findById(tenantId, modelId.getId())); return Optional.ofNullable(aiModelDao.findById(tenantId, modelId.getId()));
} }
@Override @Override
public PageData<AiModel> findAiModelsByTenantId(TenantId tenantId, PageLink pageLink) { public PageData<AiModel> findAiModelsByTenantId(TenantId tenantId, PageLink pageLink) {
validatePageLink(pageLink, AiModelEntity.ALLOWED_SORT_PROPERTIES); validatePageLink(pageLink, AiModelEntity.ALLOWED_SORT_PROPERTIES);
log.debug("[{}] Fetching AI Models page: {}", tenantId, pageLink);
return aiModelDao.findAllByTenantId(tenantId, pageLink); return aiModelDao.findAllByTenantId(tenantId, pageLink);
} }
@ -126,7 +146,7 @@ class AiModelServiceImpl extends CachedVersionedEntityService<AiModelCacheKey, A
@Override @Override
public FluentFuture<Optional<HasId<?>>> findEntityAsync(TenantId tenantId, EntityId entityId) { public FluentFuture<Optional<HasId<?>>> findEntityAsync(TenantId tenantId, EntityId entityId) {
return findAiModelByTenantIdAndIdAsync(tenantId, new AiModelId(entityId.getId())) return findAiModelByTenantIdAndIdAsync(tenantId, new AiModelId(entityId.getId()))
.transform(modelOpt -> modelOpt.map(model -> model), directExecutor()); // necessary to cast to HasId<?> .transform(modelOpt -> modelOpt.map(model -> model), directExecutor()); // necessary to cast to HasId<?>
} }
@Override @Override
@ -143,15 +163,20 @@ class AiModelServiceImpl extends CachedVersionedEntityService<AiModelCacheKey, A
private boolean deleteByTenantIdAndIdInternal(TenantId tenantId, UUID modelId) { private boolean deleteByTenantIdAndIdInternal(TenantId tenantId, UUID modelId) {
AiModel aiModel = findAiModelById(tenantId, new AiModelId(modelId)).orElse(null); AiModel aiModel = findAiModelById(tenantId, new AiModelId(modelId)).orElse(null);
if (aiModel == null) { if (aiModel == null) {
log.warn("[{}][{}] Attempted to delete AI Model but not found", tenantId, modelId);
return false; return false;
} }
boolean deleted = aiModelDao.deleteByTenantIdAndId(tenantId, aiModel.getId()); boolean deleted = aiModelDao.deleteByTenantIdAndId(tenantId, aiModel.getId());
if (deleted) { if (deleted) {
publishEvictEvent(new AiModelCacheEvictEvent.Deleted(AiModelCacheKey.of(tenantId, aiModel.getId()))); publishEvictEvent(new AiModelCacheEvictEvent.Deleted(AiModelCacheKey.of(tenantId, aiModel.getId())));
eventPublisher.publishEvent(DeleteEntityEvent.builder().tenantId(tenantId).entityId(aiModel.getId()).entity(aiModel).build()); eventPublisher.publishEvent(DeleteEntityEvent.builder()
.tenantId(tenantId)
.entityId(aiModel.getId())
.entity(aiModel)
.build());
// CR FIX: Changed to debug level
log.debug("[{}][{}] Successfully deleted AI Model: {}", tenantId, modelId, aiModel.getName());
} }
return deleted; return deleted;
} }
@ -159,12 +184,15 @@ class AiModelServiceImpl extends CachedVersionedEntityService<AiModelCacheKey, A
@Transactional @Transactional
public void deleteByTenantId(TenantId tenantId) { public void deleteByTenantId(TenantId tenantId) {
Set<AiModelId> deleted = aiModelDao.deleteByTenantId(tenantId); Set<AiModelId> deleted = aiModelDao.deleteByTenantId(tenantId);
deleted.forEach(id -> publishEvictEvent(new AiModelCacheEvictEvent.Deleted(AiModelCacheKey.of(tenantId, id)))); deleted.forEach(id -> {
publishEvictEvent(new AiModelCacheEvictEvent.Deleted(AiModelCacheKey.of(tenantId, id)));
// CR FIX: Changed to debug level
log.debug("[{}][{}] Deleted AI Model as part of tenant cleanup", tenantId, id);
});
} }
@Override @Override
public EntityType getEntityType() { public EntityType getEntityType() {
return EntityType.AI_MODEL; return EntityType.AI_MODEL;
} }
} }

Loading…
Cancel
Save