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] Improve doris sink to random use be #6132

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
Expand Down Expand Up @@ -76,7 +75,6 @@ public class DorisSinkWriter
private transient Thread executorThread;
private transient volatile Exception loadException = null;
private List<BackendV2.BackendRowV2> backends;
private long pos;

public DorisSinkWriter(
SinkWriter.Context context,
Expand Down Expand Up @@ -156,7 +154,7 @@ public void write(SeaTunnelRow element) throws IOException {
@Override
public Optional<DorisCommitInfo> prepareCommit() throws IOException {
RespContent respContent = flush();
if (!dorisConfig.getEnable2PC()) {
if (!dorisConfig.getEnable2PC() || respContent == null) {
return Optional.empty();
}
long txnId = respContent.getTxnId();
Expand All @@ -165,12 +163,12 @@ public Optional<DorisCommitInfo> prepareCommit() throws IOException {
new DorisCommitInfo(dorisStreamLoad.getHostPort(), dorisStreamLoad.getDb(), txnId));
}

@NonNull private RespContent flush() throws IOException {
private RespContent flush() throws IOException {
// disable exception checker before stop load.
loading = false;
checkState(dorisStreamLoad != null);
RespContent respContent = dorisStreamLoad.stopLoad();
if (!DORIS_SUCCESS_STATUS.contains(respContent.getStatus())) {
if (respContent != null && !DORIS_SUCCESS_STATUS.contains(respContent.getStatus())) {
String errMsg =
String.format(
"stream load error: %s, see more in %s",
Expand Down Expand Up @@ -255,12 +253,10 @@ public void close() throws IOException {

@VisibleForTesting
Hisoka-X marked this conversation as resolved.
Show resolved Hide resolved
public String getAvailableBackend() {
long tmp = pos + backends.size();
while (pos < tmp) {
BackendV2.BackendRowV2 backend = backends.get((int) (pos % backends.size()));
Collections.shuffle(backends);
for (BackendV2.BackendRowV2 backend : backends) {
String res = backend.toBackendString();
if (tryHttpConnection(res)) {
pos++;
return res;
}
}
Expand All @@ -279,7 +275,6 @@ public boolean tryHttpConnection(String backend) {
return true;
} catch (Exception ex) {
log.warn("Failed to connect to backend:{}", backend, ex);
pos++;
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public class DorisStreamLoad implements Serializable {
private Future<CloseableHttpResponse> pendingLoadFuture;
private final CloseableHttpClient httpClient;
private final ExecutorService executorService;
private boolean loadBatchFirstRecord;
private volatile boolean loadBatchFirstRecord;
private String label;
private long recordCount = 0;

public DorisStreamLoad(
Expand Down Expand Up @@ -191,6 +192,8 @@ public void abortPreCommit(String labelSuffix, long chkID) throws Exception {
public void writeRecord(byte[] record) throws IOException {
if (loadBatchFirstRecord) {
loadBatchFirstRecord = false;
recordStream.startInput();
startStreamLoad();
} else {
recordStream.write(lineDelimiter);
}
Expand All @@ -214,21 +217,29 @@ public RespContent handlePreCommitResponse(CloseableHttpResponse response) throw
}

public RespContent stopLoad() throws IOException {
recordStream.endInput();
log.info("stream load stopped.");
checkState(pendingLoadFuture != null);
try {
return handlePreCommitResponse(pendingLoadFuture.get());
} catch (Exception e) {
throw new DorisConnectorException(DorisConnectorErrorCode.STREAM_LOAD_FAILED, e);
if (pendingLoadFuture != null) {
log.info("stream load stopped.");
recordStream.endInput();
try {
return handlePreCommitResponse(pendingLoadFuture.get());
} catch (Exception e) {
throw new DorisConnectorException(DorisConnectorErrorCode.STREAM_LOAD_FAILED, e);
} finally {
pendingLoadFuture = null;
}
} else {
return null;
}
}

public void startLoad(String label) throws IOException {
loadBatchFirstRecord = true;
recordCount = 0;
this.label = label;
}

private void startStreamLoad() {
HttpPutBuilder putBuilder = new HttpPutBuilder();
recordStream.startInput();
log.info("stream load started for {}", label);
try {
InputStreamEntity entity = new InputStreamEntity(recordStream);
Expand Down