From defaeff903364628e4d12e358b609b2cebf74de3 Mon Sep 17 00:00:00 2001 From: sonika-shah <58761340+sonika-shah@users.noreply.github.com> Date: Mon, 17 Feb 2025 14:47:24 +0530 Subject: [PATCH] Fix : Add typeRegistry steps when reindexing by ops (#19820) * Fix : Add typeRegistry steps when reindexing by ops * change to log.error in catch (cherry picked from commit 63eb9c30742b50290eaed2555c0a918a40872da2) --- .../openmetadata/service/TypeRegistry.java | 34 +++++++++++++++++++ .../service/resources/types/TypeResource.java | 2 +- .../service/util/OpenMetadataOperations.java | 4 +++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/TypeRegistry.java b/openmetadata-service/src/main/java/org/openmetadata/service/TypeRegistry.java index 44cef8666f4..b48083d2bed 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/TypeRegistry.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/TypeRegistry.java @@ -14,15 +14,22 @@ package org.openmetadata.service; import static org.openmetadata.common.utils.CommonUtil.listOrEmpty; +import static org.openmetadata.service.Entity.ADMIN_USER_NAME; +import static org.openmetadata.service.resources.types.TypeResource.PROPERTIES_FIELD; import com.networknt.schema.JsonSchema; +import java.util.List; import java.util.Map; +import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import lombok.extern.slf4j.Slf4j; import org.openmetadata.schema.entity.Type; +import org.openmetadata.schema.entity.type.Category; import org.openmetadata.schema.entity.type.CustomProperty; import org.openmetadata.service.exception.CatalogExceptionMessage; import org.openmetadata.service.exception.EntityNotFoundException; +import org.openmetadata.service.jdbi3.TypeRepository; +import org.openmetadata.service.util.EntityUtil; import org.openmetadata.service.util.FullyQualifiedName; import org.openmetadata.service.util.JsonUtils; @@ -49,6 +56,33 @@ public class TypeRegistry { return INSTANCE; } + public final void initialize(TypeRepository repository) { + // Load types defined in OpenMetadata schemas + long now = System.currentTimeMillis(); + List types = JsonUtils.getTypes(); + types.forEach( + type -> { + type.withId(UUID.randomUUID()).withUpdatedBy(ADMIN_USER_NAME).withUpdatedAt(now); + LOG.debug("Loading type {}", type.getName()); + try { + EntityUtil.Fields fields = repository.getFields(PROPERTIES_FIELD); + try { + Type storedType = repository.getByName(null, type.getName(), fields); + type.setId(storedType.getId()); + // If entity type already exists, then carry forward custom properties + if (storedType.getCategory().equals(Category.Entity)) { + type.setCustomProperties(storedType.getCustomProperties()); + } + } catch (Exception e) { + LOG.error("Creating entity that does not exist ", e); + } + repository.addToRegistry(type); + } catch (Exception e) { + LOG.error("Error loading type {}", type.getName(), e); + } + }); + } + public void addType(Type type) { TYPES.put(type.getName(), type); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/types/TypeResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/types/TypeResource.java index dc90db9aea2..fe21a590dca 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/types/TypeResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/types/TypeResource.java @@ -118,7 +118,7 @@ public class TypeResource extends EntityResource { type.setCustomProperties(storedType.getCustomProperties()); } } catch (Exception e) { - LOG.debug("Creating entity that does not exist ", e); + LOG.error("Creating entity that does not exist ", e); } this.repository.createOrUpdate(null, type); this.repository.addToRegistry(type); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/util/OpenMetadataOperations.java b/openmetadata-service/src/main/java/org/openmetadata/service/util/OpenMetadataOperations.java index 2151991e913..1a183c7ff40 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/util/OpenMetadataOperations.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/util/OpenMetadataOperations.java @@ -63,6 +63,7 @@ import org.openmetadata.schema.type.Include; import org.openmetadata.sdk.PipelineServiceClientInterface; import org.openmetadata.service.Entity; import org.openmetadata.service.OpenMetadataApplicationConfig; +import org.openmetadata.service.TypeRegistry; import org.openmetadata.service.apps.ApplicationHandler; import org.openmetadata.service.apps.scheduler.AppScheduler; import org.openmetadata.service.clients.pipeline.PipelineServiceClientFactory; @@ -77,6 +78,7 @@ import org.openmetadata.service.jdbi3.IngestionPipelineRepository; import org.openmetadata.service.jdbi3.ListFilter; import org.openmetadata.service.jdbi3.MigrationDAO; import org.openmetadata.service.jdbi3.SystemRepository; +import org.openmetadata.service.jdbi3.TypeRepository; import org.openmetadata.service.jdbi3.UserRepository; import org.openmetadata.service.jdbi3.locator.ConnectionType; import org.openmetadata.service.migration.api.MigrationWorkflow; @@ -515,6 +517,8 @@ public class OpenMetadataOperations implements Callable { ApplicationHandler.initialize(config); CollectionRegistry.getInstance().loadSeedData(jdbi, config, null, null, null, true); ApplicationHandler.initialize(config); + TypeRepository typeRepository = (TypeRepository) Entity.getEntityRepository(Entity.TYPE); + TypeRegistry.instance().initialize(typeRepository); AppScheduler.initialize(config, collectionDAO, searchRepository); String appName = "SearchIndexingApplication"; Set entities =