lirui-apache commented on code in PR #6698:
URL: https://github.com/apache/iceberg/pull/6698#discussion_r1133447712


##########
hive-metastore/src/main/java/org/apache/iceberg/hive/CachedClientPool.java:
##########
@@ -87,4 +106,125 @@ public <R> R run(Action<R, IMetaStoreClient, TException> 
action, boolean retry)
       throws TException, InterruptedException {
     return clientPool().run(action, retry);
   }
+
+  @VisibleForTesting
+  static Key toKey(List<Supplier<Object>> suppliers) {
+    return 
Key.of(suppliers.stream().map(Supplier::get).collect(Collectors.toList()));
+  }
+
+  @VisibleForTesting
+  static List<Supplier<Object>> extractKeySuppliers(String cacheKeys, 
Configuration conf) {
+    URIElement uri = 
URIElement.of(conf.get(HiveConf.ConfVars.METASTOREURIS.varname, ""));
+    if (cacheKeys == null || cacheKeys.isEmpty()) {
+      return Collections.singletonList(() -> uri);
+    }
+
+    // generate key elements in a certain order, so that the Key instances are 
comparable
+    Set<KeyElementType> types = 
Sets.newTreeSet(Comparator.comparingInt(Enum::ordinal));
+    Map<String, String> confElements = Maps.newTreeMap();
+    for (String element : cacheKeys.split(",", -1)) {
+      String trimmed = element.trim();
+      if (trimmed.toLowerCase(Locale.ROOT).startsWith(CONF_ELEMENT_PREFIX)) {
+        String key = trimmed.substring(CONF_ELEMENT_PREFIX.length());
+        ValidationException.check(
+            !confElements.containsKey(key), "Conf key element %s already 
specified", key);
+        confElements.put(key, conf.get(key));
+      } else {
+        KeyElementType type = KeyElementType.valueOf(trimmed.toUpperCase());
+        switch (type) {
+          case URI:
+          case UGI:
+          case USER_NAME:
+            ValidationException.check(
+                types.add(type), "%s key element already specified", 
type.name());
+            break;
+          default:
+            throw new ValidationException("Unknown key element %s", trimmed);
+        }
+      }
+    }
+    ImmutableList.Builder<Supplier<Object>> suppliers = 
ImmutableList.builder();
+    for (KeyElementType type : types) {
+      switch (type) {
+        case URI:
+          suppliers.add(() -> uri);
+          break;
+        case UGI:
+          suppliers.add(
+              () -> {
+                try {
+                  return UserGroupInformation.getCurrentUser();
+                } catch (IOException e) {
+                  throw new UncheckedIOException(e);
+                }
+              });
+          break;
+        case USER_NAME:
+          suppliers.add(
+              () -> {
+                try {
+                  String userName = 
UserGroupInformation.getCurrentUser().getUserName();
+                  return UserNameElement.of(userName);
+                } catch (IOException e) {
+                  throw new UncheckedIOException(e);
+                }
+              });
+          break;
+        default:
+          throw new RuntimeException("Unexpected key element " + type.name());
+      }
+    }
+    for (String key : confElements.keySet()) {
+      ConfElement element = ConfElement.of(key, confElements.get(key));
+      suppliers.add(() -> element);
+    }
+    return suppliers.build();
+  }
+
+  @Value.Immutable

Review Comment:
   The strings and lists are used in cache keys and need to be compared. I 
think we can either assume "how could a user name have the same value as an HMS 
URI", or we can wrap them in different classes to make sure they won't be 
equivalent. Personally I prefer the latter. But since we generally agree URI 
should be mandatory, I guess we can remove these two wrappers for now.



-- 
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...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to