rdblue commented on code in PR #6058:
URL: https://github.com/apache/iceberg/pull/6058#discussion_r1014113140


##########
core/src/main/java/org/apache/iceberg/EnvironmentContext.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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;
+
+import java.util.Map;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+
+public class EnvironmentContext {
+  private EnvironmentContext() {}
+
+  private static final Map<String, String> PROPERTIES = 
Maps.newConcurrentMap();
+  private static final ThreadLocal<Map<String, String>> 
THREAD_LOCAL_PROPERTIES =
+      ThreadLocal.withInitial(Maps::newConcurrentMap);
+
+  /**
+   * Returns a {@link Map} of all properties, including the {@link 
ThreadLocal} ones.
+   *
+   * @return A {@link Map} of all properties, including the {@link 
ThreadLocal} ones.
+   */
+  public static Map<String, String> get() {
+    return ImmutableMap.<String, String>builder()
+        .putAll(PROPERTIES)
+        .putAll(THREAD_LOCAL_PROPERTIES.get())
+        .build();
+  }
+
+  /**
+   * Will add the given key/value pair in a global properties map.
+   *
+   * @param key The key to add
+   * @param value The value to add
+   */
+  public static void put(String key, String value) {
+    PROPERTIES.put(key, value);
+  }
+
+  /**
+   * Will add the given key/value pair in a {@link ThreadLocal} properties 
map. Will remove the
+   * entry for the given key if the value is <code>null</code>.
+   *
+   * @param key The key to add
+   * @param value The value to add
+   */
+  public static void putLocal(String key, String value) {

Review Comment:
   This pattern can leak information outside the context where it is correct 
because the value is not removed. For example, if this is called by a task in a 
thread-pool then the next task in that thread would inherit the context.
   
   That's why I suggested using a `Callable` to fetch thread-local values when 
`get` is called. That way they are always fresh.



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