adnanhemani commented on code in PR #2574: URL: https://github.com/apache/polaris/pull/2574#discussion_r2361321465
########## runtime/service/src/main/java/org/apache/polaris/service/events/listeners/inmemory/InMemoryEventListener.java: ########## @@ -0,0 +1,141 @@ +/* + * 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.inmemory; + +import static org.apache.polaris.service.logging.LoggingMDCFilter.REQUEST_ID_KEY; + +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; +import com.github.benmanes.caffeine.cache.RemovalCause; +import com.google.common.annotations.VisibleForTesting; +import io.smallrye.common.annotation.Identifier; +import io.smallrye.mutiny.infrastructure.Infrastructure; +import io.smallrye.mutiny.operators.multi.processors.UnicastProcessor; +import jakarta.annotation.Nullable; +import jakarta.annotation.PreDestroy; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import jakarta.ws.rs.container.ContainerRequestContext; +import jakarta.ws.rs.core.Context; +import jakarta.ws.rs.core.SecurityContext; +import java.time.Clock; +import java.time.Duration; +import java.util.List; +import java.util.Objects; +import org.apache.polaris.core.PolarisCallContext; +import org.apache.polaris.core.context.CallContext; +import org.apache.polaris.core.context.RealmContext; +import org.apache.polaris.core.entity.PolarisEvent; +import org.apache.polaris.core.persistence.MetaStoreManagerFactory; +import org.apache.polaris.service.events.listeners.InMemoryBufferEventListenerConfiguration; +import org.apache.polaris.service.events.listeners.PolarisPersistenceEventListener; +import org.eclipse.microprofile.faulttolerance.Fallback; +import org.eclipse.microprofile.faulttolerance.Retry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@ApplicationScoped +@Identifier("persistence-in-memory") +public class InMemoryEventListener extends PolarisPersistenceEventListener { + + private static final Logger LOGGER = LoggerFactory.getLogger(InMemoryEventListener.class); + + @Inject CallContext callContext; + @Inject Clock clock; + @Inject MetaStoreManagerFactory metaStoreManagerFactory; + @Inject InMemoryBufferEventListenerConfiguration configuration; + + @Context SecurityContext securityContext; + @Context ContainerRequestContext requestContext; + + @VisibleForTesting + final LoadingCache<String, UnicastProcessor<PolarisEvent>> processors = + Caffeine.newBuilder() + .expireAfterAccess(Duration.ofHours(1)) + .evictionListener( + (String realmId, UnicastProcessor<?> processor, RemovalCause cause) -> + processor.onComplete()) + .build(this::createProcessor); + + @Override + protected void processEvent(PolarisEvent event) { + var realmId = callContext.getRealmContext().getRealmIdentifier(); + processEvent(realmId, event); + } + + protected void processEvent(String realmId, PolarisEvent event) { + var processor = Objects.requireNonNull(processors.get(realmId)); + processor.onNext(event); + } + + @Override + protected ContextSpecificInformation getContextSpecificInformation() { + var principal = securityContext.getUserPrincipal(); + var principalName = principal == null ? null : principal.getName(); + return new ContextSpecificInformation(clock.millis(), principalName); Review Comment: This one is on me, I forgot to put it in the original PR - I will enhance this in the future to include activated roles. -- 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: issues-unsubscr...@polaris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org