gortiz commented on code in PR #15180:
URL: https://github.com/apache/pinot/pull/15180#discussion_r1984641541


##########
pinot-spi/src/main/java/org/apache/pinot/spi/query/QueryThreadContext.java:
##########
@@ -0,0 +1,614 @@
+/**
+ * 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.pinot.spi.query;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Supplier;
+import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import javax.annotation.Nullable;
+import org.apache.pinot.spi.env.PinotConfiguration;
+import org.apache.pinot.spi.executor.DecoratorExecutorService;
+import org.apache.pinot.spi.trace.LoggerConstants;
+import org.apache.pinot.spi.utils.CommonConstants;
+import org.apache.pinot.spi.utils.JsonUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.slf4j.MDC;
+
+/**
+ * The {@code QueryThreadContext} class is a thread-local context for storing 
common query-related information
+ * associated to the current thread.
+ *
+ * <p>It is used to pass information between different layers of the query 
execution stack without changing the
+ * method signatures. This is also used to populate the {@link MDC} context 
for logging.
+ *
+ * Use {@link #open()} to initialize the empty context. As any other {@link 
AutoCloseable} object, it should be used
+ * within a try-with-resources block to ensure the context is properly closed 
and removed from the thread-local storage.
+ *
+ * Sometimes it is necessary to copy the state of the {@link 
QueryThreadContext} from one thread to another. In this
+ * case, use the {@link #createMemento()} method to capture the state of the 
{@link QueryThreadContext} in the current
+ * thread and then use the {@link #open(Memento)} method to initialize the 
context in the target thread with the state
+ * captured in the {@link Memento} object. The API may be a bit cumbersome, 
but it ensures that the state is always
+ * copied between threads in a safe way and makes it impossible to use the 
{@link QueryThreadContext} from another
+ * thread by mistake.
+ *
+ * It is guaranteed that all server and broker threads running SSE and MSE 
will have this context initialized as soon
+ * as the request is received. Ingestion threads that use the query execution 
stack will also have this context
+ * initialized.
+ */
+public class QueryThreadContext {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(QueryThreadContext.class);
+  private static final ThreadLocal<Instance> THREAD_LOCAL = new 
ThreadLocal<>();
+  public static volatile boolean _strictMode = false;
+  private static final FakeInstance FAKE_INSTANCE = new FakeInstance();
+
+  static {
+    // This is a hack to know if assertions are enabled or not
+    boolean assertEnabled = false;
+    //CHECKSTYLE:OFF
+    assert assertEnabled = true;
+    //CHECKSTYLE:ON
+    _strictMode = assertEnabled;
+  }
+
+  /**
+   * Private constructor to prevent instantiation.
+   *
+   * Use {@link #open()} to initialize the context instead.
+   */
+  private QueryThreadContext() {
+  }
+
+  /**
+   * Sets the strict mode of the {@link QueryThreadContext} from the given 
configuration.
+   */
+  public static void onStartup(PinotConfiguration conf) {
+    String mode = 
conf.getProperty(CommonConstants.Query.CONFIG_OF_QUERY_CONTEXT_MODE);
+    if ("strict".equalsIgnoreCase(mode)) {
+      _strictMode = true;
+    }
+    if (mode != null && !mode.isEmpty()) {

Review Comment:
   The code is more difficult to read than what you suggested, but I wanted to 
fail in case something different to null, "" or "strict" is used. I think I can 
rewrite it to be a bit more readable.



-- 
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: commits-unsubscr...@pinot.apache.org

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


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

Reply via email to