singhpk234 commented on code in PR #1287:
URL: https://github.com/apache/polaris/pull/1287#discussion_r2051219067


##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcMetaStoreManagerFactory.java:
##########
@@ -0,0 +1,296 @@
+/*
+ * 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.extension.persistence.relational.jdbc;
+
+import io.smallrye.common.annotation.Identifier;
+import jakarta.annotation.Nullable;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Supplier;
+import javax.sql.DataSource;
+import org.apache.polaris.core.PolarisCallContext;
+import org.apache.polaris.core.PolarisDefaultDiagServiceImpl;
+import org.apache.polaris.core.PolarisDiagnostics;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+import org.apache.polaris.core.entity.PolarisEntity;
+import org.apache.polaris.core.entity.PolarisEntityConstants;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.entity.PolarisPrincipalSecrets;
+import org.apache.polaris.core.persistence.BasePersistence;
+import org.apache.polaris.core.persistence.MetaStoreManagerFactory;
+import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
+import org.apache.polaris.core.persistence.PrincipalSecretsGenerator;
+import org.apache.polaris.core.persistence.bootstrap.RootCredentialsSet;
+import org.apache.polaris.core.persistence.cache.EntityCache;
+import org.apache.polaris.core.persistence.dao.entity.BaseResult;
+import org.apache.polaris.core.persistence.dao.entity.EntityResult;
+import org.apache.polaris.core.persistence.dao.entity.PrincipalSecretsResult;
+import 
org.apache.polaris.core.persistence.transactional.TransactionalMetaStoreManagerImpl;
+import org.apache.polaris.core.storage.PolarisStorageIntegrationProvider;
+import org.apache.polaris.core.storage.cache.StorageCredentialCache;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The implementation of Configuration interface for configuring the {@link 
PolarisMetaStoreManager}
+ * using a JDBC backed by SQL metastore. TODO: refactor - <a
+ * href="https://github.com/apache/polaris/pull/1287/files#r2047487588";>...</a>
+ */
+@ApplicationScoped
+@Identifier("relational-jdbc")
+public class JdbcMetaStoreManagerFactory implements MetaStoreManagerFactory {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(JdbcMetaStoreManagerFactory.class);
+
+  final Map<String, PolarisMetaStoreManager> metaStoreManagerMap = new 
HashMap<>();
+  final Map<String, StorageCredentialCache> storageCredentialCacheMap = new 
HashMap<>();
+  final Map<String, EntityCache> entityCacheMap = new HashMap<>();
+  final Map<String, Supplier<BasePersistence>> sessionSupplierMap = new 
HashMap<>();
+  protected final PolarisDiagnostics diagServices = new 
PolarisDefaultDiagServiceImpl();
+  // TODO: Pending discussion of if we should have one Database per realm or 1 
schema per realm
+  // or realm should be a primary key on all the tables.
+  @Inject DataSource dataSource;
+  @Inject PolarisStorageIntegrationProvider storageIntegrationProvider;
+
+  protected JdbcMetaStoreManagerFactory() {}
+
+  protected PrincipalSecretsGenerator secretsGenerator(
+      RealmContext realmContext, @Nullable RootCredentialsSet 
rootCredentialsSet) {
+    if (rootCredentialsSet != null) {
+      return PrincipalSecretsGenerator.bootstrap(
+          realmContext.getRealmIdentifier(), rootCredentialsSet);
+    } else {
+      return PrincipalSecretsGenerator.RANDOM_SECRETS;
+    }
+  }
+
+  protected PolarisMetaStoreManager createNewMetaStoreManager() {
+    return new TransactionalMetaStoreManagerImpl();
+  }
+
+  private void initializeForRealm(
+      RealmContext realmContext, RootCredentialsSet rootCredentialsSet) {
+    DatasourceOperations databaseOperations = new 
DatasourceOperations(dataSource);
+    // TODO: see if we need to take script from Quarkus or can we just
+    // use the script committed in the repo.
+    try {
+      
databaseOperations.executeScript("scripts/postgres/schema-v1-postgres.sql");
+    } catch (SQLException e) {
+      throw new RuntimeException(
+          String.format("Error executing sql script: %s", e.getMessage()), e);
+    }
+    sessionSupplierMap.put(
+        realmContext.getRealmIdentifier(),
+        () ->
+            new JdbcBasePersistenceImpl(
+                databaseOperations,
+                secretsGenerator(realmContext, rootCredentialsSet),
+                storageIntegrationProvider,
+                realmContext.getRealmIdentifier()));
+
+    PolarisMetaStoreManager metaStoreManager = createNewMetaStoreManager();
+    metaStoreManagerMap.put(realmContext.getRealmIdentifier(), 
metaStoreManager);
+  }
+
+  @Override
+  public synchronized Map<String, PrincipalSecretsResult> bootstrapRealms(
+      Iterable<String> realms, RootCredentialsSet rootCredentialsSet) {
+    Map<String, PrincipalSecretsResult> results = new HashMap<>();
+
+    for (String realm : realms) {
+      RealmContext realmContext = () -> realm;
+      if (!metaStoreManagerMap.containsKey(realmContext.getRealmIdentifier())) 
{
+        initializeForRealm(realmContext, rootCredentialsSet);
+        PrincipalSecretsResult secretsResult =
+            bootstrapServiceAndCreatePolarisPrincipalForRealm(
+                realmContext, 
metaStoreManagerMap.get(realmContext.getRealmIdentifier()));
+
+        if (rootCredentialsSet.credentials().containsKey(realm)) {
+          LOGGER.info("Bootstrapped realm {} using preset credentials.", 
realm);
+        }
+
+        results.put(realmContext.getRealmIdentifier(), secretsResult);
+      }
+    }
+
+    return Map.copyOf(results);
+  }
+
+  @Override
+  public Map<String, BaseResult> purgeRealms(Iterable<String> realms) {
+    Map<String, BaseResult> results = new HashMap<>();
+
+    for (String realm : realms) {
+      PolarisMetaStoreManager metaStoreManager = 
getOrCreateMetaStoreManager(() -> realm);
+      BasePersistence session = getOrCreateSessionSupplier(() -> realm).get();
+
+      PolarisCallContext callContext = new PolarisCallContext(session, 
diagServices);
+      BaseResult result = metaStoreManager.purge(callContext);
+      results.put(realm, result);
+
+      storageCredentialCacheMap.remove(realm);
+      sessionSupplierMap.remove(realm);
+      metaStoreManagerMap.remove(realm);
+    }
+
+    return Map.copyOf(results);
+  }
+
+  @Override
+  public synchronized PolarisMetaStoreManager getOrCreateMetaStoreManager(
+      RealmContext realmContext) {
+    if (!metaStoreManagerMap.containsKey(realmContext.getRealmIdentifier())) {
+      initializeForRealm(realmContext, null);
+      checkPolarisServiceBootstrappedForRealm(
+          realmContext, 
metaStoreManagerMap.get(realmContext.getRealmIdentifier()));
+    }
+    return metaStoreManagerMap.get(realmContext.getRealmIdentifier());
+  }
+
+  @Override
+  public synchronized Supplier<BasePersistence> getOrCreateSessionSupplier(
+      RealmContext realmContext) {
+    if (!sessionSupplierMap.containsKey(realmContext.getRealmIdentifier())) {
+      initializeForRealm(realmContext, null);
+      checkPolarisServiceBootstrappedForRealm(
+          realmContext, 
metaStoreManagerMap.get(realmContext.getRealmIdentifier()));
+    } else {
+      checkPolarisServiceBootstrappedForRealm(
+          realmContext, 
metaStoreManagerMap.get(realmContext.getRealmIdentifier()));
+    }
+    return sessionSupplierMap.get(realmContext.getRealmIdentifier());
+  }
+
+  @Override
+  public synchronized StorageCredentialCache getOrCreateStorageCredentialCache(
+      RealmContext realmContext) {
+    if 
(!storageCredentialCacheMap.containsKey(realmContext.getRealmIdentifier())) {
+      storageCredentialCacheMap.put(
+          realmContext.getRealmIdentifier(), new StorageCredentialCache());
+    }
+
+    return storageCredentialCacheMap.get(realmContext.getRealmIdentifier());
+  }
+
+  @Override
+  public synchronized EntityCache getOrCreateEntityCache(RealmContext 
realmContext) {
+    if (!entityCacheMap.containsKey(realmContext.getRealmIdentifier())) {
+      PolarisMetaStoreManager metaStoreManager = 
getOrCreateMetaStoreManager(realmContext);
+      entityCacheMap.put(realmContext.getRealmIdentifier(), new 
EntityCache(metaStoreManager));
+    }
+
+    return entityCacheMap.get(realmContext.getRealmIdentifier());
+  }
+
+  /**
+   * This method bootstraps service for a given realm: i.e. creates all the 
needed entities in the
+   * metastore and creates a root service principal. After that we rotate the 
root principal
+   * credentials and print them to stdout

Review Comment:
   i didn't find either took it from LocamMetaStoreMangeFactory, removed the 
stdout part.



-- 
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

Reply via email to