Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
924060929 committed May 9, 2024
1 parent 8710817 commit 9c5c874
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ private void distribute(LogicalPlanAdapter logicalPlanAdapter) throws UserExcept
return;
}
DistributePlanner distributePlanner = new DistributePlanner(fragments);
List<DistributedPlan> distributedPlans = distributePlanner.plan();
distributePlanner.plan();
}

private PhysicalPlan postProcess(PhysicalPlan physicalPlan) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,50 @@

package org.apache.doris.nereids.trees.plans.distribute;

import org.apache.doris.nereids.util.Utils;
import org.apache.doris.nereids.worker.NativeBackendWorkerManager;
import org.apache.doris.nereids.worker.RoundRobinWorkerSelector;
import org.apache.doris.nereids.worker.WorkerManager;
import org.apache.doris.nereids.worker.job.AssignedJob;
import org.apache.doris.nereids.worker.job.AssignedJobBuilder;
import org.apache.doris.nereids.worker.job.UnassignedJob;
import org.apache.doris.nereids.worker.job.UnassignedJobBuilder;
import org.apache.doris.planner.PlanFragment;
import org.apache.doris.planner.PlanFragmentId;

import com.google.common.collect.ListMultimap;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;

/** DistributePlanner */
public class DistributePlanner {
private final List<PlanFragment> fragments;
private final FragmentIdMapping<PlanFragment> idToFragments;

public DistributePlanner(List<PlanFragment> fragments) {
this.fragments = Utils.fastToImmutableList(
this.idToFragments = FragmentIdMapping.buildFragmentMapping(
Objects.requireNonNull(fragments, "fragments can not be null")
);
}

public List<DistributedPlan> plan() {
List<UnassignedJob> unassignedJobs = UnassignedJobBuilder.buildJobs(fragments);
WorkerManager workerManager = new NativeBackendWorkerManager(new RoundRobinWorkerSelector());
// List<AssignedJob> assignedJobs = workerManager.offerJobs(unassignedJobs);
return null;
public Map<PlanFragmentId, DistributedPlan> plan() {
FragmentIdMapping<UnassignedJob> fragmentJobs = UnassignedJobBuilder.buildJobs(idToFragments);
ListMultimap<PlanFragmentId, AssignedJob> instanceJobs = AssignedJobBuilder.buildJobs(fragmentJobs);
return buildDistributePlans(fragmentJobs, instanceJobs);
}

private Map<PlanFragmentId, DistributedPlan> buildDistributePlans(
Map<PlanFragmentId, UnassignedJob> idToUnassignedJobs,
ListMultimap<PlanFragmentId, AssignedJob> idToAssignedJobs) {
FragmentIdMapping<DistributedPlan> idToDistributedPlans = new FragmentIdMapping<>(idToFragments.size());
for (Entry<PlanFragmentId, PlanFragment> kv : idToFragments.entrySet()) {
PlanFragmentId fragmentId = kv.getKey();
PlanFragment fragment = kv.getValue();

UnassignedJob fragmentJob = idToUnassignedJobs.get(fragmentId);
List<AssignedJob> instanceJobs = idToAssignedJobs.get(fragmentId);

List<DistributedPlan> childrenPlans = idToDistributedPlans.getByChildrenFragments(fragment);
idToDistributedPlans.put(fragmentId, new DistributedPlan(fragmentJob, instanceJobs, childrenPlans));
}
return idToDistributedPlans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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.doris.nereids.trees.plans.distribute;

import org.apache.doris.planner.PlanFragment;
import org.apache.doris.planner.PlanFragmentId;

import com.google.common.collect.ImmutableList;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* FragmentIdMapping:
* key: PlanFragmentId
* value: T
*/
public class FragmentIdMapping<T> extends LinkedHashMap<PlanFragmentId, T> {
public FragmentIdMapping(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
}

public FragmentIdMapping(int initialCapacity) {
super(initialCapacity);
}

public FragmentIdMapping() {
}

public FragmentIdMapping(Map<? extends PlanFragmentId, ? extends T> m) {
super(m);
}

public FragmentIdMapping(int initialCapacity, float loadFactor, boolean accessOrder) {
super(initialCapacity, loadFactor, accessOrder);
}

public List<T> getByChildrenFragments(PlanFragment fragment) {
List<PlanFragment> children = fragment.getChildren();
ImmutableList.Builder<T> values = ImmutableList.builderWithExpectedSize(children.size());
for (PlanFragment child : children) {
values.add(get(child.getFragmentId()));
}
return values.build();
}

public static FragmentIdMapping<PlanFragment> buildFragmentMapping(List<PlanFragment> fragments) {
FragmentIdMapping<PlanFragment> idToFragments = new FragmentIdMapping<>(fragments.size());
for (PlanFragment fragment : fragments) {
idToFragments.put(fragment.getFragmentId(), fragment);
}
return idToFragments;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import org.apache.doris.nereids.worker.job.AssignedJob;
import org.apache.doris.nereids.worker.job.UnassignedJob;
import org.apache.doris.planner.PlanFragmentId;

import com.google.common.collect.ListMultimap;

import java.util.List;

Expand All @@ -31,7 +34,8 @@ public NativeBackendWorkerManager(WorkerSelector workerSelector) {
}

@Override
public List<AssignedJob> offerJob(UnassignedJob unassignedJob, List<AssignedJob> inputs) {
return offerJob(workerSelector, unassignedJob, inputs);
public List<AssignedJob> offerJob(
UnassignedJob unassignedJob, ListMultimap<PlanFragmentId, AssignedJob> inputJobs) {
return offerJob(workerSelector, unassignedJob, inputJobs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import org.apache.doris.nereids.worker.job.UnassignedLeafJob;
import org.apache.doris.nereids.worker.job.UnassignedNearStorageJob;
import org.apache.doris.nereids.worker.job.UnassignedNearStorageJob.DataId;
import org.apache.doris.planner.PlanFragmentId;

import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;

Expand All @@ -35,13 +37,14 @@

/** WorkerManager */
public interface WorkerManager {
List<AssignedJob> offerJob(UnassignedJob unassignedJob, List<AssignedJob> inputs);
List<AssignedJob> offerJob(UnassignedJob unassignedJob, ListMultimap<PlanFragmentId, AssignedJob> inputJobs);

/** offerJob */
default List<AssignedJob> offerJob(
WorkerSelector workerSelector, UnassignedJob unassignedJob, List<AssignedJob> inputs) {
WorkerSelector workerSelector, UnassignedJob unassignedJob,
ListMultimap<PlanFragmentId, AssignedJob> inputJobs) {
if (unassignedJob instanceof CustomAssignmentJob) {
return ((CustomAssignmentJob) unassignedJob).customAssignment(inputs);
return ((CustomAssignmentJob) unassignedJob).customAssignment(inputJobs);
} else if (unassignedJob instanceof UnassignedNearStorageJob) {
// scan native olap table, we can assign a worker near the storage
return offerScanJob(workerSelector, (UnassignedNearStorageJob) unassignedJob);
Expand All @@ -50,7 +53,7 @@ default List<AssignedJob> offerJob(
// for example, select literal without table, or scan an external table
return offerNonScanDatasourceJob(workerSelector, (UnassignedLeafJob) unassignedJob);
} else {
return offerExchangeJob(workerSelector, unassignedJob, inputs);
return offerExchangeJob(workerSelector, unassignedJob, inputJobs);
}
}

Expand Down Expand Up @@ -82,7 +85,8 @@ default List<AssignedJob> offerNonScanDatasourceJob(WorkerSelector workerSelecto

/** offerExchangeJob */
default List<AssignedJob> offerExchangeJob(
WorkerSelector workerSelector, UnassignedJob exchangeJob, List<AssignedJob> inputs) {
WorkerSelector workerSelector, UnassignedJob exchangeJob,
ListMultimap<PlanFragmentId, AssignedJob> inputJobs) {

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public AbstractUnassignedJob(PlanFragment fragment, List<UnassignedJob> children
this.fragment = Objects.requireNonNull(fragment, "fragment can not be null");
}

@Override
public PlanFragment getFragment() {
return fragment;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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.doris.nereids.worker.job;

import org.apache.doris.nereids.worker.NativeBackendWorkerManager;
import org.apache.doris.nereids.worker.RoundRobinWorkerSelector;
import org.apache.doris.nereids.worker.WorkerManager;
import org.apache.doris.planner.PlanFragmentId;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/** AssignedJobBuilder */
public class AssignedJobBuilder {
public static ListMultimap<PlanFragmentId, AssignedJob> buildJobs(
Map<PlanFragmentId, UnassignedJob> unassignedJobs) {
WorkerManager workerManager = new NativeBackendWorkerManager(new RoundRobinWorkerSelector());
ListMultimap<PlanFragmentId, AssignedJob> allAssignedJobs = ArrayListMultimap.create();
for (Entry<PlanFragmentId, UnassignedJob> kv : unassignedJobs.entrySet()) {
PlanFragmentId fragmentId = kv.getKey();
UnassignedJob unassignedJob = kv.getValue();
ListMultimap<PlanFragmentId, AssignedJob> inputAssignedJobs
= getInputAssignedJobs(unassignedJob, allAssignedJobs);
List<AssignedJob> fragmentAssignedJobs = workerManager.offerJob(unassignedJob, inputAssignedJobs);
allAssignedJobs.putAll(fragmentId, fragmentAssignedJobs);
}
return allAssignedJobs;
}

private static ListMultimap<PlanFragmentId, AssignedJob> getInputAssignedJobs(
UnassignedJob unassignedJob, ListMultimap<PlanFragmentId, AssignedJob> assignedJobs) {
ListMultimap<PlanFragmentId, AssignedJob> inputJobs = ArrayListMultimap.create();
for (UnassignedJob inputJob : unassignedJob.children()) {
PlanFragmentId inputId = inputJob.getFragment().getFragmentId();
inputJobs.putAll(inputId, assignedJobs.get(inputId));
}
return inputJobs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@

package org.apache.doris.nereids.worker.job;

import org.apache.doris.planner.PlanFragmentId;

import com.google.common.collect.ListMultimap;

import java.util.List;

/** CustomAssignmentJob */
public interface CustomAssignmentJob {
List<AssignedJob> customAssignment(List<AssignedJob> inputs);
List<AssignedJob> customAssignment(ListMultimap<PlanFragmentId, AssignedJob> inputJobs);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.doris.nereids.trees.TreeNode;
import org.apache.doris.nereids.worker.Worker;
import org.apache.doris.nereids.worker.job.UnassignedNearStorageJob.DataId;
import org.apache.doris.planner.PlanFragment;

import java.util.List;

Expand All @@ -33,6 +34,8 @@ default int computeDegreeOfParallelism() {
return 1;
}

PlanFragment getFragment();

// generate an instance job
// e.g. build an instance job by a backends and the replica ids it contains
AssignedJob assignWorkerAndDataSources(Worker worker, List<DataId> dataSourceIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.doris.nereids.worker.job;

import org.apache.doris.nereids.trees.plans.distribute.FragmentIdMapping;
import org.apache.doris.planner.ExchangeNode;
import org.apache.doris.planner.OlapScanNode;
import org.apache.doris.planner.PlanFragment;
Expand All @@ -25,28 +26,30 @@

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
* UnassignedJobBuilder.
* build UnassignedJob by fragment
*/
public class UnassignedJobBuilder {
public static List<UnassignedJob> buildJobs(List<PlanFragment> fragments) {
/**
* build job from fragment.
*/
public static FragmentIdMapping<UnassignedJob> buildJobs(FragmentIdMapping<PlanFragment> fragments) {
// build from leaf to parent
Map<PlanFragmentId, UnassignedJob> unassignedJobs = Maps.newLinkedHashMap();
ImmutableList.Builder<UnassignedJob> jobs = ImmutableList.builderWithExpectedSize(fragments.size());
for (int i = fragments.size() - 1; i >= 0; i--) {
PlanFragment planFragment = fragments.get(i);
List<UnassignedJob> inputJobs = getInputJobs(planFragment, unassignedJobs);
UnassignedJob unassignedJob = buildJob(planFragment, inputJobs);
unassignedJobs.put(planFragment.getFragmentId(), unassignedJob);
jobs.add(unassignedJob);
FragmentIdMapping<UnassignedJob> unassignedJobs = new FragmentIdMapping<>(fragments.size());
for (Entry<PlanFragmentId, PlanFragment> kv : fragments.entrySet()) {
PlanFragmentId fragmentId = kv.getKey();
PlanFragment fragment = kv.getValue();

List<UnassignedJob> inputJobs = unassignedJobs.getByChildrenFragments(fragment);
UnassignedJob unassignedJob = buildJob(fragment, inputJobs);
unassignedJobs.put(fragmentId, unassignedJob);
}
return jobs.build();
return unassignedJobs;
}

private static UnassignedJob buildJob(PlanFragment planFragment, List<UnassignedJob> inputJobs) {
Expand Down Expand Up @@ -138,15 +141,4 @@ private static UnassignedGatherJob buildGatherJob(PlanFragment planFragment, Lis
"Gather node should only have one input job");
return new UnassignedGatherJob(planFragment, exchangeNodes.get(0), inputJobs.get(0));
}

private static List<UnassignedJob> getInputJobs(
PlanFragment planFragment, Map<PlanFragmentId, UnassignedJob> fragmentIdToJob) {
List<PlanFragment> childrenFragments = planFragment.getChildren();
ImmutableList.Builder<UnassignedJob> inputJobs
= ImmutableList.builderWithExpectedSize(childrenFragments.size());
for (PlanFragment childrenFragment : childrenFragments) {
inputJobs.add(fragmentIdToJob.get(childrenFragment.getFragmentId()));
}
return inputJobs.build();
}
}

0 comments on commit 9c5c874

Please sign in to comment.