Copilot commented on code in PR #18643:
URL: https://github.com/apache/iotdb/pull/18643#discussion_r4070119846


##########
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/protocol/session/QueryOwnershipTest.java:
##########
@@ -0,0 +1,272 @@
+/*
+ * 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.iotdb.db.protocol.session;
+
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.protocol.thrift.impl.ClientRPCServiceImpl;
+import org.apache.iotdb.db.queryengine.plan.Coordinator;
+import org.apache.iotdb.db.queryengine.plan.execution.IQueryExecution;
+import org.apache.iotdb.rpc.TSStatusCode;
+import org.apache.iotdb.service.rpc.thrift.TSCloseOperationReq;
+import org.apache.iotdb.service.rpc.thrift.TSFetchResultsReq;
+import org.apache.iotdb.service.rpc.thrift.TSFetchResultsResp;
+
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import java.lang.reflect.Field;
+import java.net.Socket;
+import java.util.Map;
+import java.util.Optional;
+
+public class QueryOwnershipTest {
+
+  private static final long STATEMENT_ID = 1L;
+  private static final long QUERY_ID = 2L;
+
+  private static int previousDataNodeId;
+
+  @BeforeClass
+  public static void setUp() {
+    // the coordinator builds its query id generator from the data node id of 
this node
+    previousDataNodeId = 
IoTDBDescriptor.getInstance().getConfig().getDataNodeId();
+    IoTDBDescriptor.getInstance().getConfig().setDataNodeId(0);
+  }
+
+  @AfterClass
+  public static void tearDown() {
+    
IoTDBDescriptor.getInstance().getConfig().setDataNodeId(previousDataNodeId);
+  }
+
+  @Test
+  public void testQueryIdsAreBoundToTheSessionThatSubmittedTheQuery() {
+    ClientSession owner = createSession("user");
+    owner.addStatementId(STATEMENT_ID);
+    owner.addQueryId(STATEMENT_ID, QUERY_ID);
+
+    ClientSession anotherSession = createSession("user");
+    anotherSession.addStatementId(STATEMENT_ID);
+
+    Assert.assertTrue(owner.containsQueryId(STATEMENT_ID, QUERY_ID));
+    // clients that do not send a statement id together with the query id are 
still served
+    Assert.assertTrue(owner.containsQueryId(null, QUERY_ID));
+    Assert.assertFalse(anotherSession.containsQueryId(STATEMENT_ID, QUERY_ID));
+    Assert.assertFalse(anotherSession.containsQueryId(null, QUERY_ID));
+    Assert.assertFalse(owner.containsQueryId(STATEMENT_ID + 1, QUERY_ID));
+  }
+
+  @Test
+  public void testFetchResultsRejectsQueryOfAnotherSession() throws Exception {
+    ClientSession anotherSession = createSession("user");
+    anotherSession.addStatementId(STATEMENT_ID);
+    anotherSession.setLogin(true);
+
+    Map<Long, IQueryExecution> queryExecutionMap = getQueryExecutionMap();
+    queryExecutionMap.put(QUERY_ID, mockQueryExecution());
+    try {
+      withCurrentSession(
+          anotherSession,
+          () -> {
+            ClientRPCServiceImpl service = new ClientRPCServiceImpl();
+            TSFetchResultsReq request = createFetchResultsReq(anotherSession);
+            Assert.assertEquals(
+                TSStatusCode.NO_PERMISSION.getStatusCode(),
+                service.fetchResults(request).getStatus().getCode());
+            Assert.assertEquals(
+                TSStatusCode.NO_PERMISSION.getStatusCode(),
+                service.fetchResultsV2(request).getStatus().getCode());

Review Comment:
   The V1 contract exercised here is the null-statement-id path, but this 
helper sets `statementId` and the same request is used for both endpoints. A 
regression in the null scan would therefore pass this test while breaking 
`IoTDBJDBCDataSet`, whose V1 fetch omits the statement id; construct the V1 
request without setting it and keep the statement-bearing request for V2.



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

Reply via email to