collado-mike commented on code in PR #493: URL: https://github.com/apache/polaris/pull/493#discussion_r1870398814
########## polaris-service/src/main/java/org/apache/polaris/service/context/RealmScopeContext.java: ########## @@ -0,0 +1,82 @@ +/* + * 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.context; + +import jakarta.inject.Singleton; +import java.lang.annotation.Annotation; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.polaris.core.context.CallContext; +import org.apache.polaris.core.context.RealmContext; +import org.apache.polaris.core.context.RealmScope; +import org.glassfish.hk2.api.ActiveDescriptor; +import org.glassfish.hk2.api.Context; +import org.glassfish.hk2.api.ServiceHandle; + +@Singleton +public class RealmScopeContext implements Context<RealmScope> { + private final Map<String, Map<ActiveDescriptor<?>, Object>> contexts = new ConcurrentHashMap<>(); + + @Override + public Class<? extends Annotation> getScope() { + return RealmScope.class; + } + + @SuppressWarnings("unchecked") + @Override + public <U> U findOrCreate(ActiveDescriptor<U> activeDescriptor, ServiceHandle<?> root) { Review Comment: It seems to me that's exactly what the `@RealmScoped` annotation is doing. The `RealmScopeContext` maintains the cache per-realm and manages the instantiated values for each `ActiveDescriptor` for the currently active realm. This is a centralized place where we can do a lot of cleanup of stale realms (e.g., somebody made a call two days ago, but hasn't invoked an API since), rather than having to sprinkle this cache eviction in a bunch of different bean-specific factories. I think the contention is whether we should be relying on a ThreadLocal to access the current `RealmContext`. I can see where there are flaws with this approach (e.g., we already ran into one during the asynchronous cleanup of metadata files after table drop), but at the same time, I think most (every?) implementation of `@RequestScoped` relies on ThreadLocal variables (e.g., AFAICT, ArC relies on [ThreadLocalCurrentContext](https://github.com/quarkusio/quarkus/blob/main/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/ThreadLocalCurrentContext.java) in its [RequestContext](https://github.com/quarkusio/quarkus/blob/main/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/RequestContext.java#L66) ). Injecting an accessor to retrieve the current realm makes a lot of sense to me, but at the end of the day, I think we're still going to be relying on ThreadLocals. -- 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]
