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 4 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,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;

Expand All @@ -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;
Copy link
Contributor

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());

Copy link
Contributor

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.

Copy link
Contributor Author

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.

OK, I will optimize the code based on your suggestions

if (sourceState != null) {
this.shouldEnumerate = sourceState.isShouldEnumerate();
this.pendingSplit.putAll(sourceState.getPendingSplits());
}
}

@Override
public void open() {
this.pendingSplit = new HashSet<>();
this.pendingSplit = new HashedMap();
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand All @@ -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));
}
}

Expand All @@ -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
Expand All @@ -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 */
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.Set;
import java.util.List;
import java.util.Map;

@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, List<PaimonSourceSplit>> pendingSplits;
private boolean shouldEnumerate;
}