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


##########
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/InIdSetTransformFunction.java:
##########
@@ -59,18 +74,39 @@ public void init(List<TransformFunction> arguments, 
Map<String, ColumnContext> c
         "Second argument for IN_ID_SET transform function must be a literal 
string of the base64 encoded IdSet");
 
     _transformFunction = arguments.get(0);
+    String serializedIdSet = ((LiteralTransformFunction) 
arguments.get(1)).getStringLiteral();
+    // The IdSet is only read after this, which is safe from the threads of 
several segments
+    _idSet = _queryContext != null
+        ? _queryContext.getOrComputeSharedValue(IdSet.class, serializedIdSet, 
InIdSetTransformFunction::deserialize)

Review Comment:
   The sharing is per `QueryContext`. In MSE, each leaf worker and table type 
builds its own `ServerQueryRequest` (see `ServerPlanRequestUtils`). So a hybrid 
table, or several leaf workers on the same server, still decode the IdSet more 
than once. That's fine, but the description says "one time per server request", 
which can read as one time per server.
   
   Also, a failed decode is not cached, so each segment runs the decode again 
and fails again. It's probably not worth handling, I just wanted to mention it.



##########
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/InIdSetTransformFunction.java:
##########
@@ -42,12 +46,23 @@
 public class InIdSetTransformFunction extends BaseTransformFunction {
   private TransformFunction _transformFunction;
   private IdSet _idSet;
+  // The query the function is initialized for, if any. The segments of a 
query share one deserialized IdSet, which can
+  // hold millions of ids.
+  @Nullable
+  private QueryContext _queryContext;

Review Comment:
   nit: `_queryContext` is a field only to pass state from the 3-arg `init` to 
the 2-arg one. If someone calls `init(arguments, columnContextMap)` on an 
instance that was initialized with a query context before, it silently uses the 
old context. A private helper that takes a nullable `QueryContext` would avoid 
that coupling.



##########
pinot-spi/src/main/java/org/apache/pinot/spi/utils/CommonConstants.java:
##########
@@ -1426,6 +1426,11 @@ public static class Grpc {
       public static final String KEY_OF_GRPC_TLS_ENABLED = 
"pinot.broker.grpc.tls.enabled";
       public static final String KEY_OF_GRPC_TLS_PORT = 
"pinot.broker.grpc.tls.port";
       public static final String KEY_OF_GRPC_TLS_PREFIX = 
"pinot.broker.grpctls";
+      /// Maximum size, in bytes, of a request the broker's gRPC query 
endpoint accepts. Must be positive. The default
+      /// is gRPC's default. Queries with large literals (e.g. an `IN_ID_SET` 
IdSet of millions of ids) can need more.
+      public static final String KEY_OF_GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES =

Review Comment:
   Each of these three keys makes sense on its own. But with 
`pinot.query.runner.max.msg.size.bytes` and the `GrpcConfig` 
`maxInboundMessageSizeBytes`, we now have five limits for the same concept, and 
their names have different shapes. Could we document them together, with the 
hop that each one covers? The broker gRPC limit (4 MB by default) is now the 
smallest one in the chain, so it's the first limit users will hit.



##########
pinot-core/src/test/java/org/apache/pinot/core/operator/transform/function/InIdSetTransformFunctionTest.java:
##########
@@ -0,0 +1,132 @@
+/**
+ * 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.core.operator.transform.function;
+
+import java.io.IOException;
+import java.lang.ref.ReferenceQueue;
+import java.lang.ref.WeakReference;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.request.context.RequestContextUtils;
+import org.apache.pinot.core.operator.ColumnContext;
+import org.apache.pinot.core.query.request.context.QueryContext;
+import 
org.apache.pinot.core.query.request.context.utils.QueryContextConverterUtils;
+import org.apache.pinot.core.query.utils.idset.IdSet;
+import org.apache.pinot.core.query.utils.idset.IdSets;
+import org.apache.pinot.spi.data.FieldSpec.DataType;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotSame;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertSame;
+import static org.testng.Assert.assertTrue;
+
+
+/// Tests the IN_ID_SET transform function on a segment; `IdSetQueriesTest` 
runs it in queries.
+public class InIdSetTransformFunctionTest extends BaseTransformFunctionTest {
+
+  @Test
+  public void testSharesTheIdSetAcrossTheSegmentsOfAQuery()
+      throws IOException {
+    IdSet idSet = createIdSet(0, NUM_ROWS / 2);
+    ExpressionContext expression = createExpression(idSet);
+    QueryContext queryContext = createQueryContext();
+
+    // Each segment of a query builds its own transform function, but they 
share one deserialized IdSet
+    InIdSetTransformFunction segment1Function = 
createTransformFunction(expression, queryContext);
+    InIdSetTransformFunction segment2Function = 
createTransformFunction(expression, queryContext);
+    assertNotSame(segment2Function, segment1Function);
+    assertSame(segment2Function.getIdSet(), segment1Function.getIdSet());
+    assertResults(segment2Function, idSet);
+
+    // Another IdSet of the same query is deserialized on its own
+    IdSet otherIdSet = createIdSet(NUM_ROWS / 2, NUM_ROWS);
+    InIdSetTransformFunction otherIdSetFunction = 
createTransformFunction(createExpression(otherIdSet), queryContext);
+    assertNotSame(otherIdSetFunction.getIdSet(), segment1Function.getIdSet());
+    assertResults(otherIdSetFunction, otherIdSet);
+
+    // Another query deserializes its own IdSet
+    InIdSetTransformFunction otherQueryFunction = 
createTransformFunction(expression, createQueryContext());
+    assertNotSame(otherQueryFunction.getIdSet(), segment1Function.getIdSet());
+  }
+
+  /// The query context that shares the IdSet must not keep it after the query.
+  @Test
+  public void testIdSetDoesNotOutliveTheQuery()
+      throws Exception {
+    ReferenceQueue<IdSet> queue = new ReferenceQueue<>();
+    WeakReference<IdSet> idSetRef = new WeakReference<>(createIdSetForQuery(), 
queue);
+    assertTrue(awaitCleared(queue), "The IdSet must be collected after the 
query");
+    // Also keeps the reference reachable until it is enqueued
+    assertNull(idSetRef.get());
+  }
+
+  /// Returns the IdSet that a query deserializes, and drops the query.
+  private IdSet createIdSetForQuery()
+      throws IOException {
+    return createTransformFunction(createExpression(createIdSet(0, NUM_ROWS / 
2)), createQueryContext()).getIdSet();
+  }
+
+  /// Waits for the reference to be enqueued, collecting garbage until then or 
for up to 5 seconds.
+  private static boolean awaitCleared(ReferenceQueue<?> queue)
+      throws InterruptedException {
+    long deadlineMs = System.currentTimeMillis() + 5_000L;
+    do {
+      System.gc();

Review Comment:
   This test needs `System.gc()` to take effect within 5 seconds. That doesn't 
hold with `-XX:+DisableExplicitGC`, and it can be slow on a busy CI runner. I'm 
not asking for a change, but if this test becomes flaky, this is the first 
place I'd look.



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