jackye1995 commented on code in PR #4575:
URL: https://github.com/apache/iceberg/pull/4575#discussion_r852429805


##########
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 notice here that although we use session Id as the cache key for the 
headers map, what we are setting are just headers for identity and credentials. 
Basically the identity and credentials are the determining factor of the cache 
content (`Catalog` in the `BaseSessionCatalog`, and header map here). This is 
very similar to 
https://github.com/prestodb/presto/blob/master/presto-iceberg/src/main/java/com/facebook/presto/iceberg/IcebergResourceFactory.java#L88.
   
   That means if those information do not change, the same cached content could 
be shared across sessions. For example, here if 2 sessions have the same 
identity and credential, they actually have the same headers. So does it make 
more sense to use `content.identity() + content.credential()` as the cache key? 
This could greatly improve the cache hit rate, instead of creating something 
new for each session. This is especially helpful if the session is short-lived, 
but the caller credential is shared across sessions.
   
   



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

Reply via email to