-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce FaultTolerantPartitioningScheme
To encapsulate partition assignment logic
- Loading branch information
Showing
10 changed files
with
243 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...rino-main/src/main/java/io/trino/execution/scheduler/FaultTolerantPartitioningScheme.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Licensed 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 io.trino.execution.scheduler; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.collect.ImmutableList; | ||
import io.trino.metadata.InternalNode; | ||
import io.trino.metadata.Split; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.ToIntFunction; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.base.Preconditions.checkState; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class FaultTolerantPartitioningScheme | ||
{ | ||
private final int partitionCount; | ||
private final Optional<int[]> bucketToPartitionMap; | ||
private final Optional<ToIntFunction<Split>> splitToBucketFunction; | ||
private final Optional<List<InternalNode>> partitionToNodeMap; | ||
|
||
@VisibleForTesting | ||
FaultTolerantPartitioningScheme( | ||
int partitionCount, | ||
Optional<int[]> bucketToPartitionMap, | ||
Optional<ToIntFunction<Split>> splitToBucketFunction, | ||
Optional<List<InternalNode>> partitionToNodeMap) | ||
{ | ||
checkArgument(partitionCount > 0, "partitionCount must be greater than zero"); | ||
this.partitionCount = partitionCount; | ||
this.bucketToPartitionMap = requireNonNull(bucketToPartitionMap, "bucketToPartitionMap is null"); | ||
this.splitToBucketFunction = requireNonNull(splitToBucketFunction, "splitToBucketFunction is null"); | ||
requireNonNull(partitionToNodeMap, "partitionToNodeMap is null"); | ||
partitionToNodeMap.ifPresent(map -> checkArgument( | ||
map.size() == partitionCount, | ||
"partitionToNodeMap size (%s) must be equal to partitionCount (%s)", | ||
map.size(), | ||
partitionCount)); | ||
this.partitionToNodeMap = partitionToNodeMap.map(ImmutableList::copyOf); | ||
} | ||
|
||
public int getPartitionCount() | ||
{ | ||
return partitionCount; | ||
} | ||
|
||
public Optional<int[]> getBucketToPartitionMap() | ||
{ | ||
return bucketToPartitionMap; | ||
} | ||
|
||
public int getPartition(Split split) | ||
{ | ||
checkState(bucketToPartitionMap.isPresent(), "bucketToPartitionMap is expected to be present"); | ||
checkState(splitToBucketFunction.isPresent(), "splitToBucketFunction is expected to be present"); | ||
int bucket = splitToBucketFunction.get().applyAsInt(split); | ||
checkState( | ||
bucketToPartitionMap.get().length > bucket, | ||
"invalid bucketToPartitionMap size (%s), bucket to partition mapping not found for bucket %s", | ||
bucketToPartitionMap.get().length, | ||
bucket); | ||
return bucketToPartitionMap.get()[bucket]; | ||
} | ||
|
||
public Optional<InternalNode> getNodeRequirement(int partition) | ||
{ | ||
checkArgument(partition < partitionCount, "partition is expected to be less than %s", partitionCount); | ||
return partitionToNodeMap.map(map -> map.get(partition)); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...in/src/main/java/io/trino/execution/scheduler/FaultTolerantPartitioningSchemeFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Licensed 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 io.trino.execution.scheduler; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import io.trino.Session; | ||
import io.trino.metadata.InternalNode; | ||
import io.trino.sql.planner.NodePartitioningManager; | ||
import io.trino.sql.planner.PartitioningHandle; | ||
|
||
import javax.annotation.concurrent.NotThreadSafe; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.IntStream; | ||
|
||
import static io.trino.sql.planner.SystemPartitioningHandle.FIXED_HASH_DISTRIBUTION; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
@NotThreadSafe | ||
public class FaultTolerantPartitioningSchemeFactory | ||
{ | ||
private final NodePartitioningManager nodePartitioningManager; | ||
private final Session session; | ||
private final int partitionCount; | ||
|
||
private final Map<PartitioningHandle, FaultTolerantPartitioningScheme> cache = new HashMap<>(); | ||
|
||
public FaultTolerantPartitioningSchemeFactory(NodePartitioningManager nodePartitioningManager, Session session, int partitionCount) | ||
{ | ||
this.nodePartitioningManager = requireNonNull(nodePartitioningManager, "nodePartitioningManager is null"); | ||
this.session = requireNonNull(session, "session is null"); | ||
this.partitionCount = partitionCount; | ||
} | ||
|
||
public FaultTolerantPartitioningScheme get(PartitioningHandle handle) | ||
{ | ||
return cache.computeIfAbsent(handle, this::create); | ||
} | ||
|
||
private FaultTolerantPartitioningScheme create(PartitioningHandle partitioningHandle) | ||
{ | ||
if (partitioningHandle.equals(FIXED_HASH_DISTRIBUTION)) { | ||
return new FaultTolerantPartitioningScheme( | ||
partitionCount, | ||
Optional.of(IntStream.range(0, partitionCount).toArray()), | ||
Optional.empty(), | ||
Optional.empty()); | ||
} | ||
if (partitioningHandle.getCatalogHandle().isPresent()) { | ||
// TODO This caps the number of partitions to the number of available nodes. Perhaps a better approach is required for fault tolerant execution. | ||
BucketNodeMap bucketNodeMap = nodePartitioningManager.getBucketNodeMap(session, partitioningHandle); | ||
int bucketCount = bucketNodeMap.getBucketCount(); | ||
int[] bucketToPartition = new int[bucketCount]; | ||
// make sure all buckets mapped to the same node map to the same partition, such that locality requirements are respected in scheduling | ||
Map<InternalNode, Integer> nodeToPartition = new HashMap<>(); | ||
List<InternalNode> partitionToNodeMap = new ArrayList<>(); | ||
for (int bucket = 0; bucket < bucketCount; bucket++) { | ||
InternalNode node = bucketNodeMap.getAssignedNode(bucket); | ||
Integer partitionId = nodeToPartition.get(node); | ||
if (partitionId == null) { | ||
partitionId = partitionToNodeMap.size(); | ||
nodeToPartition.put(node, partitionId); | ||
partitionToNodeMap.add(node); | ||
} | ||
bucketToPartition[bucket] = partitionId; | ||
} | ||
return new FaultTolerantPartitioningScheme( | ||
partitionToNodeMap.size(), | ||
Optional.of(bucketToPartition), | ||
Optional.of(bucketNodeMap.getSplitToBucketFunction()), | ||
Optional.of(ImmutableList.copyOf(partitionToNodeMap))); | ||
} | ||
return new FaultTolerantPartitioningScheme(1, Optional.empty(), Optional.empty(), Optional.empty()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.