sririshindra commented on code in PR #4225: URL: https://github.com/apache/polaris/pull/4225#discussion_r3231030623
########## runtime/service/src/main/java/org/apache/polaris/service/events/listeners/DefaultPersistenceEventHandler.java: ########## @@ -0,0 +1,249 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.polaris.service.events.listeners; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Objects; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.rest.requests.RenameTableRequest; +import org.apache.polaris.core.admin.model.Catalog; +import org.apache.polaris.core.admin.model.Principal; +import org.apache.polaris.core.auth.PolarisPrincipal; +import org.apache.polaris.core.entity.PolarisEvent.ResourceType; +import org.apache.polaris.service.events.EventAttributeMap; +import org.apache.polaris.service.events.EventAttributes; +import org.apache.polaris.service.events.PolarisEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +final class DefaultPersistenceEventHandler implements PersistenceEventHandler { + + private static final Logger LOGGER = + LoggerFactory.getLogger(DefaultPersistenceEventHandler.class); + private static final String SERIALIZATION_ERROR_PAYLOAD = "{\"error\":\"Serialization failed\"}"; + + static final String ALL_CATALOGS_SCOPE = "_all_catalogs"; + static final String ROOT_NAMESPACE_SCOPE = "_root_namespace"; + static final String TABLE_SCOPE = "_table_scope"; + static final String VIEW_SCOPE = "_view_scope"; + + private final PersistenceAttributeSecurityFilter securityFilter; + private final ObjectMapper objectMapper; + + DefaultPersistenceEventHandler() { + this(new PersistenceAttributeSecurityFilter(), new ObjectMapper()); + } + + DefaultPersistenceEventHandler( + PersistenceAttributeSecurityFilter securityFilter, ObjectMapper objectMapper) { + this.securityFilter = Objects.requireNonNull(securityFilter, "securityFilter"); + this.objectMapper = Objects.requireNonNull(objectMapper, "objectMapper"); + } + + @Override + public org.apache.polaris.core.entity.PolarisEvent toPersistenceEvent(PolarisEvent event) { + EventAttributeMap attributes = event.attributes(); + String catalogName = resolveCatalogName(attributes); + ResourceType resourceType = resolveResourceType(event, attributes); + + org.apache.polaris.core.entity.PolarisEvent persistenceEvent = + new org.apache.polaris.core.entity.PolarisEvent( + catalogName, + event.metadata().eventId().toString(), + event.metadata().requestId().orElse(null), + event.type().name(), + event.metadata().timestamp().toEpochMilli(), + event.metadata().user().map(PolarisPrincipal::getName).orElse(null), + resourceType, + resolveResourceIdentifier(resourceType, attributes, catalogName)); + + Map<String, String> additionalProperties = + new LinkedHashMap<>(securityFilter.filterAllowlistedAttributes(attributes)); + additionalProperties.putAll( + securityFilter.filterAllowlistedTelemetryContext(event.metadata().openTelemetryContext())); + if (!additionalProperties.isEmpty()) { + persistenceEvent.setAdditionalProperties( + serializeAdditionalProperties(event, additionalProperties)); + } + + return persistenceEvent; + } + + private String serializeAdditionalProperties( + PolarisEvent event, Map<String, String> additionalProperties) { + try { + return objectMapper.writeValueAsString(additionalProperties); + } catch (JsonProcessingException e) { + LOGGER.warn( + "Failed to serialize additional properties for eventType={} eventId={}; persisting fallback payload", + event.type(), + event.metadata().eventId()); + LOGGER.debug( + "Serialization failure details for eventType={} eventId={}", + event.type(), + event.metadata().eventId(), + e); + return SERIALIZATION_ERROR_PAYLOAD; + } + } + + private static String resolveCatalogName(EventAttributeMap attributes) { + return attributes + .get(EventAttributes.CATALOG_NAME) + .or(() -> attributes.get(EventAttributes.CATALOG).map(Catalog::getName)) + .orElse(ALL_CATALOGS_SCOPE); + } + + private static ResourceType resolveResourceType( + PolarisEvent event, EventAttributeMap attributes) { + return switch (event.type().category()) { + case CATALOG -> ResourceType.CATALOG; + case NAMESPACE -> ResourceType.NAMESPACE; + case TABLE -> ResourceType.TABLE; + case VIEW -> ResourceType.VIEW; + // Persistence resource type schema supports CATALOG/NAMESPACE/TABLE/VIEW only. Review Comment: All event types are now persisted unconditionally. ResourceType is a 4-value classification field (CATALOG/NAMESPACE/TABLE/VIEW) used for indexing — not a filtering gate. Every event that reaches the listener is mapped to one of these types via PolarisEventType.category() and persisted. Event type filtering remains configurable at the listener registration level in PolarisEventListenerConfiguration for operators who want a subset. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
