gianm commented on code in PR #16790:
URL: https://github.com/apache/druid/pull/16790#discussion_r1690318058
##########
extensions-core/multi-stage-query/src/main/java/org/apache/druid/msq/rpc/ResourcePermissionMapper.java:
##########
@@ -23,11 +23,9 @@
import java.util.List;
-/**
- * Provides HTTP resources such as {@link ControllerResource} with information
about which permissions are needed
- * for requests.
- */
public interface ResourcePermissionMapper
{
List<ResourceAction> getAdminPermissions();
+
+ List<ResourceAction> getQueryPermissions(String queryId);
Review Comment:
Kept this parameter. It will be useful in the future.
##########
extensions-core/multi-stage-query/src/main/java/org/apache/druid/msq/shuffle/output/ChannelStageOutputReader.java:
##########
@@ -0,0 +1,238 @@
+/*
+ * 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.msq.shuffle.output;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.primitives.Ints;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import it.unimi.dsi.fastutil.bytes.ByteArrays;
+import org.apache.druid.common.guava.FutureUtils;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.frame.channel.ByteTracker;
+import org.apache.druid.frame.channel.ReadableFrameChannel;
+import org.apache.druid.frame.file.FrameFileWriter;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.msq.exec.OutputChannelMode;
+import org.apache.druid.msq.kernel.controller.ControllerQueryKernelUtils;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.WritableByteChannel;
+import java.util.ArrayDeque;
+import java.util.Deque;
+
+/**
+ * Reader for {@link ReadableFrameChannel}.
+ *
+ * Because this reader returns an underlying channel directly, it must only be
used when it is certain that
+ * only a single consumer exists, i.e., when using output mode {@link
OutputChannelMode#MEMORY}. See
+ * {@link ControllerQueryKernelUtils#canUseMemoryOutput} for the code that
ensures that there is only a single
+ * consumer in the in-memory case.
+ */
+public class ChannelStageOutputReader implements StageOutputReader
+{
+ enum State
+ {
+ INIT,
+ LOCAL,
+ REMOTE,
+ CLOSED
+ }
+
+ private final ReadableFrameChannel channel;
+ private final FrameFileWriter writer;
+
+ /**
+ * Pair of chunk size + chunk InputStream.
+ */
+ private final Deque<byte[]> chunks = new ArrayDeque<>();
+
+ /**
+ * State of this reader.
+ */
+ private State state = State.INIT;
+
+ /**
+ * Position within the overall stream.
+ */
+ private long cursor;
+
+ /**
+ * Offset of the first chunk in {@link #chunks} which corresponds to {@link
#cursor}.
+ */
+ private int positionWithinFirstChunk;
+
+ /**
+ * Whether {@link FrameFileWriter#close()} is called on {@link #writer}.
+ */
+ private boolean didCloseWriter;
+
+ public ChannelStageOutputReader(final ReadableFrameChannel channel)
+ {
+ this.channel = channel;
+ this.writer = FrameFileWriter.open(new ChunkAcceptor(), null,
ByteTracker.unboundedTracker());
+ }
+
+ @Override
+ public synchronized ListenableFuture<InputStream> readRemotelyFrom(final
long offset)
+ {
+ if (state == State.INIT) {
+ state = State.REMOTE;
+ } else if (state == State.LOCAL) {
+ throw new ISE("Cannot read both remotely and locally");
+ } else if (state == State.CLOSED) {
+ throw new ISE("Closed");
+ }
+
+ if (offset < cursor) {
+ return Futures.immediateFailedFuture(
+ new ISE("Offset[%,d] no longer available, current cursor is[%,d]",
offset, cursor));
+ }
+
+ while (chunks.isEmpty() || offset > cursor) {
+ // Fetch additional chunks if needed.
+ if (chunks.isEmpty()) {
+ if (didCloseWriter) {
+ if (offset == cursor) {
+ return Futures.immediateFuture(new
ByteArrayInputStream(ByteArrays.EMPTY_ARRAY));
+ } else {
+ throw DruidException.defensive(
+ "Channel finished but cursor[%,d] does not match requested
offset[%,d]",
+ cursor,
+ offset
+ );
+ }
+ } else if (channel.isFinished()) {
+ try {
+ writer.close();
+ }
+ catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+
+ didCloseWriter = true;
+ continue;
+ } else if (channel.canRead()) {
+ try {
+ writer.writeFrame(channel.read(), FrameFileWriter.NO_PARTITION);
+ }
+ catch (Exception e) {
+ try {
+ writer.abort();
+ }
+ catch (IOException e2) {
+ e.addSuppressed(e2);
+ }
+
+ throw new RuntimeException(e);
+ }
+ } else {
+ return FutureUtils.transformAsync(channel.readabilityFuture(),
ignored -> readRemotelyFrom(offset));
Review Comment:
That's why it's called `ignored` ;)
--
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]