-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
xiaochen-zhou
wants to merge
5
commits into
apache:dev
Choose a base branch
from
TongchengOpenSource:paimon_source_optimize
base: dev
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,15 +19,20 @@ | |
|
||
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.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
|
@@ -39,36 +44,51 @@ public class PaimonSourceSplitEnumerator | |
/** Source split enumerator context */ | ||
private final Context<PaimonSourceSplit> context; | ||
|
||
/** The splits that has assigned */ | ||
private final Set<PaimonSourceSplit> assignedSplit; | ||
private Map<Integer, List<PaimonSourceSplit>> pendingSplit; | ||
|
||
/** The splits that have not assigned */ | ||
private Set<PaimonSourceSplit> pendingSplit; | ||
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.pendingSplit = new HashMap<>(); | ||
this.shouldEnumerate = sourceState == null; | ||
if (sourceState != null) { | ||
this.shouldEnumerate = sourceState.isShouldEnumerate(); | ||
this.pendingSplit.putAll(sourceState.getPendingSplits()); | ||
} | ||
} | ||
|
||
@Override | ||
public void open() { | ||
this.pendingSplit = new HashSet<>(); | ||
this.pendingSplit = new HashedMap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will overwrite the init in the construct method. |
||
} | ||
|
||
@Override | ||
public void run() throws Exception { | ||
// do nothing | ||
Set<Integer> readers = context.registeredReaders(); | ||
if (shouldEnumerate) { | ||
Set<PaimonSourceSplit> newSplits = getTableSplits(); | ||
synchronized (stateLock) { | ||
addPendingSplit(newSplits); | ||
shouldEnumerate = false; | ||
} | ||
|
||
assignSplit(readers); | ||
} | ||
log.debug( | ||
"No more splits to assign." + " Sending NoMoreSplitsEvent to reader {}.", readers); | ||
readers.forEach(context::signalNoMoreSplits); | ||
} | ||
|
||
@Override | ||
|
@@ -79,8 +99,8 @@ public void close() throws IOException { | |
@Override | ||
public void addSplitsBack(List<PaimonSourceSplit> splits, int subtaskId) { | ||
if (!splits.isEmpty()) { | ||
pendingSplit.addAll(splits); | ||
assignSplit(subtaskId); | ||
addPendingSplit(splits); | ||
assignSplit(Collections.singletonList(subtaskId)); | ||
} | ||
} | ||
|
||
|
@@ -91,13 +111,17 @@ public int currentUnassignedSplitSize() { | |
|
||
@Override | ||
public void registerReader(int subtaskId) { | ||
pendingSplit = getTableSplits(); | ||
assignSplit(subtaskId); | ||
log.debug("Register reader {} to PaimonSourceSplitEnumerator.", subtaskId); | ||
if (!pendingSplit.isEmpty()) { | ||
assignSplit(Collections.singletonList(subtaskId)); | ||
} | ||
} | ||
|
||
@Override | ||
public PaimonSourceState snapshotState(long checkpointId) throws Exception { | ||
return new PaimonSourceState(assignedSplit); | ||
synchronized (stateLock) { | ||
return new PaimonSourceState(pendingSplit, shouldEnumerate); | ||
} | ||
} | ||
|
||
@Override | ||
|
@@ -110,36 +134,41 @@ public void handleSplitRequest(int subtaskId) { | |
// do nothing | ||
} | ||
|
||
private void addPendingSplit(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); | ||
pendingSplit.computeIfAbsent(ownerReader, r -> new ArrayList<>()).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) { | ||
List<PaimonSourceSplit> assignmentForReader = pendingSplit.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, assignmentForReader); | ||
} catch (Exception e) { | ||
log.error( | ||
"Failed to assign splits {} to reader {}", | ||
assignmentForReader, | ||
reader, | ||
e); | ||
pendingSplit.put(reader, 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 */ | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please optimize the code like this
this.shouldEnumerate = (sourceState == null || sourceState.isShouldEnumerate());
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can refer the org.apache.seatunnel.connectors.seatunnel.file.source.split.FileSourceSplitEnumerator in which we can keep the pendingSplit and assignedSplit at the sametime. It's more logical that way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I will optimize the code based on your suggestions