jackye1995 commented on code in PR #4575: URL: https://github.com/apache/iceberg/pull/4575#discussion_r853347091
########## core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java: ########## @@ -0,0 +1,479 @@ +/* + * 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.iceberg.rest; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import java.io.Closeable; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; +import org.apache.hadoop.conf.Configuration; +import org.apache.iceberg.BaseTable; +import org.apache.iceberg.CatalogProperties; +import org.apache.iceberg.CatalogUtil; +import org.apache.iceberg.MetadataUpdate; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SortOrder; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.Transaction; +import org.apache.iceberg.Transactions; +import org.apache.iceberg.catalog.BaseSessionCatalog; +import org.apache.iceberg.catalog.Catalog; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.exceptions.NoSuchNamespaceException; +import org.apache.iceberg.exceptions.NoSuchTableException; +import org.apache.iceberg.hadoop.Configurable; +import org.apache.iceberg.io.FileIO; +import org.apache.iceberg.io.ResolvingFileIO; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.rest.requests.CreateNamespaceRequest; +import org.apache.iceberg.rest.requests.CreateTableRequest; +import org.apache.iceberg.rest.requests.UpdateNamespacePropertiesRequest; +import org.apache.iceberg.rest.responses.ConfigResponse; +import org.apache.iceberg.rest.responses.CreateNamespaceResponse; +import org.apache.iceberg.rest.responses.GetNamespaceResponse; +import org.apache.iceberg.rest.responses.ListNamespacesResponse; +import org.apache.iceberg.rest.responses.ListTablesResponse; +import org.apache.iceberg.rest.responses.LoadTableResponse; +import org.apache.iceberg.rest.responses.UpdateNamespacePropertiesResponse; +import org.apache.iceberg.util.Pair; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RESTSessionCatalog extends BaseSessionCatalog implements Configurable<Configuration>, Closeable { + private static final Logger LOG = LoggerFactory.getLogger(RESTSessionCatalog.class); + private final Function<Map<String, String>, RESTClient> clientBuilder; + private final Cache<String, Map<String, String>> headers = Caffeine.newBuilder().build(); + private Map<String, String> baseHeaders = ImmutableMap.of(); + private RESTClient client = null; + private String catalogName = null; + private Map<String, String> properties = null; + private ResourcePaths paths = null; + private Object conf = null; + private FileIO io = null; + + public RESTSessionCatalog() { + this(new HTTPClientFactory()); + } + + RESTSessionCatalog(Function<Map<String, String>, RESTClient> clientBuilder) { + this.clientBuilder = clientBuilder; + } + + @Override + public void initialize(String name, Map<String, String> props) { + Preconditions.checkArgument(props != null, "Invalid configuration: null"); + this.baseHeaders = RESTUtil.extractPrefixMap(props, "header."); + ConfigResponse config = fetchConfig(props); + Map<String, String> mergedProps = config.merge(props); + this.client = clientBuilder.apply(mergedProps); + this.catalogName = name; + this.properties = mergedProps; + this.paths = ResourcePaths.forCatalogProperties(properties); + String ioImpl = properties.get(CatalogProperties.FILE_IO_IMPL); + this.io = CatalogUtil.loadFileIO(ioImpl != null ? ioImpl : ResolvingFileIO.class.getName(), properties, conf); + } + + Map<String, String> headers(SessionContext context) { + return headers.get(context.sessionId(), id -> { + ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); + builder.putAll(baseHeaders); + + if (context.identity() != null) { Review Comment: I see. If we are focusing on the consensus part, I think it is pretty aligned with the [TrinoCatalog](https://github.com/trinodb/trino/blob/master/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/catalog/TrinoCatalog.java) interface, there is a converter needed for a Trino [ConnectorSession](https://github.com/trinodb/trino/blob/master/core/trino-spi/src/main/java/io/trino/spi/connector/ConnectorSession.java) but that is overall straightforward. The reason I bring up this identity and credential part here is that maybe Iceberg's `SessionContext` can be an interface but not an implementation, and we have `SessionCatalog<T extends SessionContext>`, and `SessionContext` has one important method `cacheKey()` that can be used to define if 2 sessions should use the same underlying catalog, and the cache key can on the extreme end be the session ID to have 1 different catalog for each session, or be a composite of identity and credential values. In Trino, then we can have `IcebergConnectorSession implements SessionContext` and `TrinoCatalog implements SessionCatalog<IcebergConnectorSession>` as a way to simplify the `TrinoCatalog` class. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
