Skip to content

Commit

Permalink
MSQ worker: Support in-memory shuffles. (apache#16790)
Browse files Browse the repository at this point in the history
* MSQ worker: Support in-memory shuffles.

This patch is a follow-up to apache#16168, adding worker-side support for
in-memory shuffles. Changes include:

1) Worker-side code now respects the same context parameter "maxConcurrentStages"
   that was added to the controller in apache#16168. The parameter remains undocumented
   for now, to give us a chance to more fully develop and test this functionality.

1) WorkerImpl is broken up into WorkerImpl, RunWorkOrder, and RunWorkOrderListener
   to improve readability.

2) WorkerImpl has a new StageOutputHolder + StageOutputReader concept, which
   abstract over memory-based or file-based stage results.

3) RunWorkOrder is updated to create in-memory stage output channels when
   instructed to.

4) ControllerResource is updated to add /doneReadingInput/, so the controller
   can tell when workers that sort, but do not gather statistics, are done reading
   their inputs.

5) WorkerMemoryParameters is updated to consider maxConcurrentStages.

Additionally, WorkerChatHandler is split into WorkerResource, so as to match
ControllerChatHandler and ControllerResource.

* Updates for static checks, test coverage.

* Fixes.

* Remove exception.

* Changes from review.

* Address static check.

* Changes from review.

* Improvements to docs and method names.

* Update comments, add test.

* Additional javadocs.

* Fix throws.

* Fix worker stopping in tests.

* Fix stuck test.
  • Loading branch information
gianm authored and sreemanamala committed Aug 6, 2024
1 parent b0a4c51 commit 20fd1cb
Show file tree
Hide file tree
Showing 60 changed files with 4,650 additions and 2,131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ void resultsComplete(
);

/**
* Returns the current list of task ids, ordered by worker number. The Nth task has worker number N.
* Returns the current list of worker IDs, ordered by worker number. The Nth worker has worker number N.
*/
List<String> getTaskIds();
List<String> getWorkerIds();

@Nullable
TaskReport.ReportMap liveReports();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,25 @@
import org.apache.druid.msq.indexing.error.MSQErrorReport;
import org.apache.druid.msq.kernel.StageDefinition;
import org.apache.druid.msq.kernel.StageId;
import org.apache.druid.msq.kernel.WorkOrder;
import org.apache.druid.msq.statistics.PartialKeyStatisticsInformation;

import javax.annotation.Nullable;
import java.io.Closeable;
import java.io.IOException;
import java.util.List;

/**
* Client for the multi-stage query controller. Used by a Worker task.
* Client for the multi-stage query controller. Used by a {@link Worker}. Each instance is specific to a single query,
* meaning it communicates with a single controller.
*/
public interface ControllerClient extends AutoCloseable
public interface ControllerClient extends Closeable
{
/**
* Client side method to update the controller with partial key statistics information for a particular stage and worker.
* Controller's implementation collates all the information for a stage to fetch key statistics from workers.
* Client side method to update the controller with partial key statistics information for a particular stage
* and worker. The controller collates all the information for a stage to fetch key statistics from workers.
*
* Only used when {@link StageDefinition#mustGatherResultKeyStatistics()}.
*/
void postPartialKeyStatistics(
StageId stageId,
Expand Down Expand Up @@ -86,11 +91,16 @@ void postWorkerError(
/**
* Client side method to inform the controller about the warnings generated by the given worker.
*/
void postWorkerWarning(
List<MSQErrorReport> MSQErrorReports
) throws IOException;
void postWorkerWarning(List<MSQErrorReport> MSQErrorReports) throws IOException;

List<String> getTaskList() throws IOException;
/**
* Client side method for retrieving the list of worker IDs from the controller. These IDs can be passed to
* {@link WorkerClient} methods to communicate with other workers. Not necessary when the {@link WorkOrder} has
* {@link WorkOrder#getWorkerIds()} set.
*
* @see Controller#getWorkerIds() for the controller side
*/
List<String> getWorkerIds() throws IOException;

/**
* Close this client. Idempotent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,7 @@ private List<SegmentIdWithShardSpec> generateSegmentIdsWithShardSpecsForReplace(
}

@Override
public List<String> getTaskIds()
public List<String> getWorkerIds()
{
if (workerManager == null) {
return Collections.emptyList();
Expand Down Expand Up @@ -1260,7 +1260,7 @@ private void contactWorkersForStage(
{
// Sorted copy of target worker numbers to ensure consistent iteration order.
final List<Integer> workersCopy = Ordering.natural().sortedCopy(workers);
final List<String> workerIds = getTaskIds();
final List<String> workerIds = getWorkerIds();
final List<ListenableFuture<Void>> workerFutures = new ArrayList<>(workersCopy.size());

try {
Expand Down Expand Up @@ -1488,7 +1488,7 @@ private List<Interval> findIntervalsToDrop(final Set<DataSegment> publishedSegme
private CounterSnapshotsTree getCountersFromAllTasks()
{
final CounterSnapshotsTree retVal = new CounterSnapshotsTree();
final List<String> taskList = getTaskIds();
final List<String> taskList = getWorkerIds();

final List<ListenableFuture<CounterSnapshotsTree>> futures = new ArrayList<>();

Expand All @@ -1508,7 +1508,7 @@ private CounterSnapshotsTree getCountersFromAllTasks()

private void postFinishToAllTasks()
{
final List<String> taskList = getTaskIds();
final List<String> taskList = getWorkerIds();

final List<ListenableFuture<Void>> futures = new ArrayList<>();

Expand Down Expand Up @@ -2963,7 +2963,7 @@ private void startQueryResultsReader()
}

final StageId finalStageId = queryKernel.getStageId(queryDef.getFinalStageDefinition().getStageNumber());
final List<String> taskIds = getTaskIds();
final List<String> taskIds = getWorkerIds();

final InputChannelFactory inputChannelFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public static ControllerMemoryParameters createProductionInstance(
memoryIntrospector.totalMemoryInJvm(),
usableMemoryInJvm,
numControllersInJvm,
memoryIntrospector.numProcessorsInJvm()
memoryIntrospector.numProcessorsInJvm(),
0
)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.exec;

import org.apache.druid.frame.processor.OutputChannel;
import org.apache.druid.frame.processor.OutputChannelFactory;
import org.apache.druid.frame.processor.PartitionedOutputChannel;

import java.io.IOException;

/**
* Decorator for {@link OutputChannelFactory} that notifies a {@link Listener} whenever a channel is opened.
*/
public class ListeningOutputChannelFactory implements OutputChannelFactory
{
private final OutputChannelFactory delegate;
private final Listener listener;

public ListeningOutputChannelFactory(final OutputChannelFactory delegate, final Listener listener)
{
this.delegate = delegate;
this.listener = listener;
}

@Override
public OutputChannel openChannel(final int partitionNumber) throws IOException
{
return notifyListener(delegate.openChannel(partitionNumber));
}


@Override
public OutputChannel openNilChannel(final int partitionNumber)
{
return notifyListener(delegate.openNilChannel(partitionNumber));
}

@Override
public PartitionedOutputChannel openPartitionedChannel(
final String name,
final boolean deleteAfterRead
)
{
throw new UnsupportedOperationException("Listening to partitioned channels is not supported");
}

private OutputChannel notifyListener(OutputChannel channel)
{
listener.channelOpened(channel);
return channel;
}

public interface Listener
{
void channelOpened(OutputChannel channel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
public enum OutputChannelMode
{
/**
* In-memory output channels. Stage shuffle data does not hit disk. This mode requires a consumer stage to run
* at the same time as its corresponding producer stage. See {@link ControllerQueryKernelUtils#computeStageGroups} for the
* logic that determines when we can use in-memory channels.
* In-memory output channels. Stage shuffle data does not hit disk. In-memory channels do not fully buffer stage
* output. They use a blocking queue; see {@link RunWorkOrder#makeStageOutputChannelFactory()}.
*
* Because stage output is not fully buffered, this mode requires a consumer stage to run at the same time as its
* corresponding producer stage. See {@link ControllerQueryKernelUtils#computeStageGroups} for the logic that
* determines when we can use in-memory channels.
*/
MEMORY("memory"),

Expand Down
Loading

0 comments on commit 20fd1cb

Please sign in to comment.