-
Notifications
You must be signed in to change notification settings - Fork 379
Use new Request Context for each realm during implicit bootstrap #3411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * 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.config; | ||
|
|
||
| import io.smallrye.common.annotation.Identifier; | ||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.enterprise.context.control.ActivateRequestContext; | ||
| import jakarta.inject.Inject; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.Callable; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.apache.polaris.core.persistence.MetaStoreManagerFactory; | ||
| import org.apache.polaris.core.persistence.bootstrap.RootCredentialsSet; | ||
| import org.apache.polaris.core.persistence.dao.entity.PrincipalSecretsResult; | ||
| import org.apache.polaris.service.context.catalog.RealmContextHolder; | ||
|
|
||
| /** Utility class for running per-realm bootstrap tasks each in a fresh Request Context. */ | ||
| @ApplicationScoped | ||
| class Bootstrapper { | ||
| private final ExecutorService executor; | ||
| private final RealmContextHolder realmContextHolder; | ||
| private final MetaStoreManagerFactory factory; | ||
|
|
||
| @Inject | ||
| Bootstrapper( | ||
| // Note: this executor is expected to NOT propagate CDI contexts to tasks. | ||
| @Identifier("task-executor") ExecutorService executor, | ||
| RealmContextHolder realmContextHolder, | ||
| MetaStoreManagerFactory factory) { | ||
| this.executor = executor; | ||
| this.realmContextHolder = realmContextHolder; | ||
| this.factory = factory; | ||
| } | ||
|
|
||
| Map<String, PrincipalSecretsResult> bootstrapRealms( | ||
| Iterable<String> realmIds, RootCredentialsSet rootCredentialsSet) { | ||
| HashMap<String, PrincipalSecretsResult> result = new HashMap<>(); | ||
| for (String realmId : realmIds) { | ||
| Task t = new Task(realmContextHolder, realmId, rootCredentialsSet, factory); | ||
| try { | ||
| // Submit an async task per realm to ensure it runs in a fresh RequestContext. | ||
| // Note: simultaneous bootstrap of multiple realms is an edge case - no need | ||
| // to optimize for fast concurrent completion. | ||
| result.putAll(executor.submit(t).get(2, TimeUnit.MINUTES)); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private record Task( | ||
| RealmContextHolder realmContextHolder, | ||
| String realmId, | ||
| RootCredentialsSet rootCredentialsSet, | ||
| MetaStoreManagerFactory factory) | ||
snazy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| implements Callable<Map<String, PrincipalSecretsResult>> { | ||
|
|
||
| @Override | ||
| @ActivateRequestContext | ||
| public Map<String, PrincipalSecretsResult> call() { | ||
| // Note: each call to this method runs in a new CDI request context. | ||
| // Make the realm ID effective in the current request context. | ||
| realmContextHolder.set(() -> realmId); | ||
| return factory.bootstrapRealms(Collections.singleton(realmId), rootCredentialsSet); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -271,7 +271,7 @@ public StsClientsPool stsClientsPool( | |
| */ | ||
| public void maybeBootstrap( | ||
| @Observes Startup event, | ||
| MetaStoreManagerFactory factory, | ||
| Bootstrapper bootstrapper, | ||
| PersistenceConfiguration config, | ||
| RealmContextConfiguration realmContextConfiguration) { | ||
| var rootCredentialsSet = RootCredentialsSet.fromEnvironment(); | ||
|
|
@@ -285,7 +285,7 @@ public void maybeBootstrap( | |
| RootCredentialsSet.ENVIRONMENT_VARIABLE, | ||
| RootCredentialsSet.SYSTEM_PROPERTY); | ||
|
|
||
| var result = factory.bootstrapRealms(realmIds, rootCredentialsSet); | ||
| var result = bootstrapper.bootstrapRealms(realmIds, rootCredentialsSet); | ||
|
|
||
| result.forEach( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this function doesn't really need
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense - will fix
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went with @adutra 's suggestion... WDYT? |
||
| (realm, secrets) -> { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.