Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improve][Connector-V2] Improve Paimon source split enumerator #6766

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@

import org.apache.seatunnel.api.source.SourceSplitEnumerator;

import org.apache.commons.collections.map.HashedMap;
import org.apache.paimon.table.Table;
import org.apache.paimon.table.source.Split;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -39,36 +43,50 @@ public class PaimonSourceSplitEnumerator
/** Source split enumerator context */
private final Context<PaimonSourceSplit> context;

/** The splits that has assigned */
private final Set<PaimonSourceSplit> assignedSplit;
private final Set<PaimonSourceSplit> pendingSplits = new HashSet<>();

/** The splits that have not assigned */
private Set<PaimonSourceSplit> pendingSplit;
private Map<Integer, Set<PaimonSourceSplit>> assignedSplits;

private volatile boolean shouldEnumerate;

private final Object stateLock = new Object();

/** The table that wants to read */
private final Table table;

public PaimonSourceSplitEnumerator(Context<PaimonSourceSplit> context, Table table) {
this.context = context;
this.table = table;
this.assignedSplit = new HashSet<>();
this(context, table, null);
}

public PaimonSourceSplitEnumerator(
Context<PaimonSourceSplit> context, Table table, PaimonSourceState sourceState) {
this.context = context;
this.table = table;
this.assignedSplit = sourceState.getAssignedSplits();
this.shouldEnumerate = (sourceState == null || sourceState.isShouldEnumerate());
this.assignedSplits = new HashedMap();
if (sourceState != null) {
this.assignedSplits.putAll(sourceState.getAssignedSplits());
}
}

@Override
public void open() {
this.pendingSplit = new HashSet<>();
this.pendingSplits.addAll(getTableSplits());
}

@Override
public void run() throws Exception {
// do nothing
Set<Integer> readers = context.registeredReaders();
if (shouldEnumerate) {
synchronized (stateLock) {
addAssignSplit(pendingSplits);
shouldEnumerate = false;
}
assignSplit(readers);
}
log.debug(
"No more splits to assign." + " Sending NoMoreSplitsEvent to reader {}.", readers);
readers.forEach(context::signalNoMoreSplits);
}

@Override
Expand All @@ -79,25 +97,29 @@ public void close() throws IOException {
@Override
public void addSplitsBack(List<PaimonSourceSplit> splits, int subtaskId) {
if (!splits.isEmpty()) {
pendingSplit.addAll(splits);
assignSplit(subtaskId);
addAssignSplit(splits);
assignSplit(Collections.singletonList(subtaskId));
}
}

@Override
public int currentUnassignedSplitSize() {
return pendingSplit.size();
return pendingSplits.size();
}

@Override
public void registerReader(int subtaskId) {
pendingSplit = getTableSplits();
assignSplit(subtaskId);
log.debug("Register reader {} to PaimonSourceSplitEnumerator.", subtaskId);
if (!pendingSplits.isEmpty()) {
assignSplit(Collections.singletonList(subtaskId));
}
}

@Override
public PaimonSourceState snapshotState(long checkpointId) throws Exception {
return new PaimonSourceState(assignedSplit);
synchronized (stateLock) {
return new PaimonSourceState(assignedSplits, shouldEnumerate);
}
}

@Override
Expand All @@ -110,36 +132,44 @@ public void handleSplitRequest(int subtaskId) {
// do nothing
}

private void addAssignSplit(Collection<PaimonSourceSplit> splits) {
int readerCount = context.currentParallelism();
for (PaimonSourceSplit split : splits) {
int ownerReader = getSplitOwner(split.splitId(), readerCount);
log.info("Assigning {} to {} reader.", split.getSplit().toString(), ownerReader);
// remove the assigned splits from pending splits
pendingSplits.remove(split);
// save the state of assigned splits
assignedSplits.computeIfAbsent(ownerReader, r -> new HashSet<>()).add(split);
}
}

/** Assign split by reader task id */
private void assignSplit(int taskId) {
ArrayList<PaimonSourceSplit> currentTaskSplits = new ArrayList<>();
if (context.currentParallelism() == 1) {
// if parallelism == 1, we should assign all the splits to reader
currentTaskSplits.addAll(pendingSplit);
} else {
// if parallelism > 1, according to hashCode of split's id to determine whether to
// allocate the current task
for (PaimonSourceSplit fileSourceSplit : pendingSplit) {
final int splitOwner =
getSplitOwner(fileSourceSplit.splitId(), context.currentParallelism());
if (splitOwner == taskId) {
currentTaskSplits.add(fileSourceSplit);
private void assignSplit(Collection<Integer> readers) {

log.debug("Assign pendingSplits to readers {}", readers);

for (int reader : readers) {
Set<PaimonSourceSplit> assignmentForReader = assignedSplits.remove(reader);
if (assignmentForReader != null && !assignmentForReader.isEmpty()) {
log.info(
"Assign splits {} to reader {}",
assignmentForReader.stream()
.map(p -> p.getSplit().toString())
.collect(Collectors.joining(",")),
reader);
try {
context.assignSplit(reader, new ArrayList<>(assignmentForReader));
} catch (Exception e) {
log.error(
"Failed to assign splits {} to reader {}",
assignmentForReader,
reader,
e);
pendingSplits.addAll(assignmentForReader);
}
}
}
// assign splits
context.assignSplit(taskId, currentTaskSplits);
// save the state of assigned splits
assignedSplit.addAll(currentTaskSplits);
// remove the assigned splits from pending splits
currentTaskSplits.forEach(split -> pendingSplit.remove(split));
log.info(
"SubTask {} is assigned to [{}]",
taskId,
currentTaskSplits.stream()
.map(PaimonSourceSplit::splitId)
.collect(Collectors.joining(",")));
context.signalNoMoreSplits(taskId);
}

/** Get all splits of table */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@

package org.apache.seatunnel.connectors.seatunnel.paimon.source;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.io.Serializable;
import java.util.Map;
import java.util.Set;

@AllArgsConstructor
@Getter
/** Paimon connector source state, saves the splits has assigned to readers. */
public class PaimonSourceState implements Serializable {

private static final long serialVersionUID = 1L;

private final Set<PaimonSourceSplit> assignedSplits;

public PaimonSourceState(Set<PaimonSourceSplit> assignedSplits) {
this.assignedSplits = assignedSplits;
}

public Set<PaimonSourceSplit> getAssignedSplits() {
return assignedSplits;
}
private final Map<Integer, Set<PaimonSourceSplit>> assignedSplits;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change Set to Map here? Any considerations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change Set to Map here? Any considerations?

I think there is a small optimization,avoid the traversal operation when removing the allocated PaimonSourceSplit from pendingSplit. use set like FileSourceSplitEnumerator, a nested loop will be formed in the FileSourceSplitEnumerator#run() method.

private boolean shouldEnumerate;
}