Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/query/catalog/src/plan/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,27 @@ impl Partitions {
let partitions = match self.kind {
PartitionsShuffleKind::Seq => self.partitions.clone(),
PartitionsShuffleKind::Mod => {
// Sort by hash%executor_nums.
let mut parts = self
.partitions
.iter()
.map(|p| (p.hash() % num_executors as u64, p.clone()))
.collect::<Vec<_>>();
parts.sort_by(|a, b| a.0.cmp(&b.0));
parts.into_iter().map(|x| x.1).collect()
// Assign partitions to executors by hash % num_executors.
// This ensures cache affinity: the same partition always goes to the same executor,
// as long as the cluster membership remains unchanged.
let mut executor_part: HashMap<String, Partitions> = HashMap::new();

for p in self.partitions.iter() {
let idx = (p.hash() % num_executors as u64) as usize;
let executor_id = &executors_sorted[idx].id;
executor_part
.entry(executor_id.clone())
.or_default()
.partitions
.push(p.clone());
}

// Ensure all executors have an entry (even if empty).
for e in &executors_sorted {
executor_part.entry(e.id.clone()).or_default();
}

return Ok(executor_part);
}
PartitionsShuffleKind::ConsistentHash => {
let mut scale = 0;
Expand Down
16 changes: 8 additions & 8 deletions src/query/catalog/tests/it/testdata/partition-reshuffle.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ Partitions { kind: Seq, partitions: [] }
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"0"}] }
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"1"}] }
PartitionsShuffleKind::Mod : 10 partitions of 3 executors
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"0"}, {"type":"fuse_lazy","loc":"2"}, {"type":"fuse_lazy","loc":"4"}] }
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"9"}, {"type":"fuse_lazy","loc":"1"}, {"type":"fuse_lazy","loc":"3"}] }
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"5"}, {"type":"fuse_lazy","loc":"7"}, {"type":"fuse_lazy","loc":"6"}, {"type":"fuse_lazy","loc":"8"}] }
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"0"}, {"type":"fuse_lazy","loc":"2"}, {"type":"fuse_lazy","loc":"4"}, {"type":"fuse_lazy","loc":"9"}] }
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"1"}, {"type":"fuse_lazy","loc":"3"}, {"type":"fuse_lazy","loc":"5"}, {"type":"fuse_lazy","loc":"7"}] }
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"6"}, {"type":"fuse_lazy","loc":"8"}] }
PartitionsShuffleKind::Mod : 11 partitions of 3 executors
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"0"}, {"type":"fuse_lazy","loc":"2"}, {"type":"fuse_lazy","loc":"4"}] }
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"9"}, {"type":"fuse_lazy","loc":"1"}, {"type":"fuse_lazy","loc":"3"}, {"type":"fuse_lazy","loc":"5"}] }
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"7"}, {"type":"fuse_lazy","loc":"10"}, {"type":"fuse_lazy","loc":"6"}, {"type":"fuse_lazy","loc":"8"}] }
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"0"}, {"type":"fuse_lazy","loc":"2"}, {"type":"fuse_lazy","loc":"4"}, {"type":"fuse_lazy","loc":"9"}] }
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"1"}, {"type":"fuse_lazy","loc":"3"}, {"type":"fuse_lazy","loc":"5"}, {"type":"fuse_lazy","loc":"7"}, {"type":"fuse_lazy","loc":"10"}] }
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"6"}, {"type":"fuse_lazy","loc":"8"}] }
PartitionsShuffleKind::Mod : 11 partitions of 2 executors
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"4"}, {"type":"fuse_lazy","loc":"5"}, {"type":"fuse_lazy","loc":"0"}, {"type":"fuse_lazy","loc":"1"}, {"type":"fuse_lazy","loc":"2"}] }
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"3"}, {"type":"fuse_lazy","loc":"6"}, {"type":"fuse_lazy","loc":"7"}, {"type":"fuse_lazy","loc":"8"}, {"type":"fuse_lazy","loc":"9"}, {"type":"fuse_lazy","loc":"10"}] }
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"4"}, {"type":"fuse_lazy","loc":"5"}] }
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"0"}, {"type":"fuse_lazy","loc":"1"}, {"type":"fuse_lazy","loc":"2"}, {"type":"fuse_lazy","loc":"3"}, {"type":"fuse_lazy","loc":"6"}, {"type":"fuse_lazy","loc":"7"}, {"type":"fuse_lazy","loc":"8"}, {"type":"fuse_lazy","loc":"9"}, {"type":"fuse_lazy","loc":"10"}] }
PartitionsShuffleKind::ConsistentHash : 11 partitions of 2 executors
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"4"}, {"type":"fuse_lazy","loc":"5"}, {"type":"fuse_lazy","loc":"6"}, {"type":"fuse_lazy","loc":"7"}] }
Partitions { kind: Seq, partitions: [{"type":"fuse_lazy","loc":"0"}, {"type":"fuse_lazy","loc":"1"}, {"type":"fuse_lazy","loc":"2"}, {"type":"fuse_lazy","loc":"3"}, {"type":"fuse_lazy","loc":"8"}, {"type":"fuse_lazy","loc":"9"}, {"type":"fuse_lazy","loc":"10"}] }
Expand Down
Loading