This is an automated email from the ASF dual-hosted git repository.

FrankChen021 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/druid.git


The following commit(s) were added to refs/heads/master by this push:
     new 35bf5a9bc73 fix: ComposingReadableFrameChannel reopening the last 
channel. (#20716)
35bf5a9bc73 is described below

commit 35bf5a9bc738d8107b92bf3a4c58d63b4ceaf9c2
Author: Gian Merlino <[email protected]>
AuthorDate: Sun Oct 25 00:57:59 2026 -0700

    fix: ComposingReadableFrameChannel reopening the last channel. (#20716)
    
    ComposingReadableFrameChannel was able to read the last channel multiple
    times in certain cases, leading to incorrect results. This patch adds
    a "finished" flag to prevent it.
---
 .../channel/ComposingReadableFrameChannel.java     | 60 +++++++----------
 .../channel/ComposingReadableFrameChannelTest.java | 75 ++++++++++++++++++++++
 2 files changed, 99 insertions(+), 36 deletions(-)

diff --git 
a/processing/src/main/java/org/apache/druid/frame/channel/ComposingReadableFrameChannel.java
 
b/processing/src/main/java/org/apache/druid/frame/channel/ComposingReadableFrameChannel.java
index 76aed14fe76..88969e8318e 100644
--- 
a/processing/src/main/java/org/apache/druid/frame/channel/ComposingReadableFrameChannel.java
+++ 
b/processing/src/main/java/org/apache/druid/frame/channel/ComposingReadableFrameChannel.java
@@ -42,6 +42,7 @@ public class ComposingReadableFrameChannel implements 
ReadableFrameChannel
   private final List<Supplier<ReadableFrameChannel>> channels;
   private ReadableFrameChannel currentChannel;
   private int currentIndex;
+  private boolean finished;
 
   public ComposingReadableFrameChannel(
       int partition,
@@ -70,36 +71,13 @@ public class ComposingReadableFrameChannel implements 
ReadableFrameChannel
   @Override
   public boolean isFinished()
   {
-    initCurrentChannel();
-    if (!currentChannel.isFinished()) {
-      return false;
-    }
-    currentChannel.close();
-    currentChannel = null;
-    if (isLastIndex()) {
-      return true;
-    }
-    ++currentIndex;
-    return isFinished();
+    return advancePastFinishedChannels();
   }
 
   @Override
   public boolean canRead()
   {
-    initCurrentChannel();
-    if (currentChannel.canRead()) {
-      return true;
-    }
-    if (currentChannel.isFinished()) {
-      currentChannel.close();
-      currentChannel = null;
-      if (isLastIndex()) {
-        return false;
-      }
-      ++currentIndex;
-      return canRead();
-    }
-    return false;
+    return !advancePastFinishedChannels() && currentChannel.canRead();
   }
 
   @Override
@@ -111,17 +89,10 @@ public class ComposingReadableFrameChannel implements 
ReadableFrameChannel
   @Override
   public ListenableFuture<?> readabilityFuture()
   {
-    initCurrentChannel();
-    if (!currentChannel.isFinished()) {
-      return currentChannel.readabilityFuture();
-    }
-    currentChannel.close();
-    currentChannel = null;
-    if (isLastIndex()) {
+    if (advancePastFinishedChannels()) {
       return Futures.immediateFuture(true);
     }
-    ++currentIndex;
-    return readabilityFuture();
+    return currentChannel.readabilityFuture();
   }
 
   @Override
@@ -132,9 +103,26 @@ public class ComposingReadableFrameChannel implements 
ReadableFrameChannel
     }
   }
 
-  private boolean isLastIndex()
+  /**
+   * Closes finished channels and moves on to the next one, until reaching a 
channel that is not finished. Returns
+   * whether all channels are finished.
+   */
+  private boolean advancePastFinishedChannels()
   {
-    return currentIndex == channels.size() - 1;
+    while (!finished) {
+      initCurrentChannel();
+      if (!currentChannel.isFinished()) {
+        return false;
+      }
+      currentChannel.close();
+      currentChannel = null;
+      if (currentIndex == channels.size() - 1) {
+        finished = true;
+      } else {
+        ++currentIndex;
+      }
+    }
+    return true;
   }
 
   private void initCurrentChannel()
diff --git 
a/processing/src/test/java/org/apache/druid/frame/channel/ComposingReadableFrameChannelTest.java
 
b/processing/src/test/java/org/apache/druid/frame/channel/ComposingReadableFrameChannelTest.java
new file mode 100644
index 00000000000..aebe77020e9
--- /dev/null
+++ 
b/processing/src/test/java/org/apache/druid/frame/channel/ComposingReadableFrameChannelTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.druid.frame.channel;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import org.apache.druid.query.rowsandcols.RowsAndColumns;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+
+public class ComposingReadableFrameChannelTest
+{
+  @Test
+  public void testFinishedChannelsAreNotReopened()
+  {
+    final RowsAndColumns rac1 = Mockito.mock(RowsAndColumns.class);
+    final RowsAndColumns rac2 = Mockito.mock(RowsAndColumns.class);
+
+    // Each supplier opens a new channel that reads from the beginning, like 
durable storage channels do.
+    final ComposingReadableFrameChannel channel = new 
ComposingReadableFrameChannel(
+        0,
+        ImmutableList.of(() -> singleFrameChannel(rac1), () -> 
singleFrameChannel(rac2)),
+        new HashSet<>(ImmutableSet.of(0, 1))
+    );
+
+    final List<RowsAndColumns> racsRead = new ArrayList<>();
+    while (!channel.isFinished()) {
+      if (channel.canRead()) {
+        racsRead.add(channel.read());
+      }
+    }
+
+    Assertions.assertEquals(ImmutableList.of(rac1, rac2), racsRead);
+    Assertions.assertTrue(channel.isFinished());
+    Assertions.assertFalse(channel.canRead());
+    Assertions.assertTrue(channel.readabilityFuture().isDone());
+    channel.close();
+  }
+
+  private static ReadableFrameChannel singleFrameChannel(final RowsAndColumns 
rac)
+  {
+    final BlockingQueueFrameChannel channel = new BlockingQueueFrameChannel(1);
+    try {
+      channel.writable().write(rac);
+      channel.writable().close();
+    }
+    catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+    return channel.readable();
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to