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

[Fix][Connector-V2] Fixed clickhouse connectors cannot stop under multiple parallelism #7921

Merged
merged 16 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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 @@ -17,6 +17,7 @@

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

import org.apache.seatunnel.api.source.Boundedness;
import org.apache.seatunnel.api.source.Collector;
import org.apache.seatunnel.api.source.SourceReader;
import org.apache.seatunnel.api.table.type.SeaTunnelRow;
Expand All @@ -28,13 +29,15 @@
import com.clickhouse.client.ClickHouseNode;
import com.clickhouse.client.ClickHouseRequest;
import com.clickhouse.client.ClickHouseResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

@Slf4j
public class ClickhouseSourceReader implements SourceReader<SeaTunnelRow, ClickhouseSourceSplit> {

private final List<ClickHouseNode> servers;
Expand All @@ -43,6 +46,7 @@ public class ClickhouseSourceReader implements SourceReader<SeaTunnelRow, Clickh
private final SourceReader.Context readerContext;
private ClickHouseRequest<?> request;
private final String sql;
private volatile boolean noMoreSplit;

private final List<ClickhouseSourceSplit> splits;

Expand Down Expand Up @@ -75,28 +79,35 @@ public void close() throws IOException {

@Override
public void pollNext(Collector<SeaTunnelRow> output) throws Exception {
if (!splits.isEmpty()) {
try (ClickHouseResponse response = this.request.query(sql).executeAndWait()) {
response.stream()
.forEach(
record -> {
Object[] values =
new Object[this.rowTypeInfo.getFieldNames().length];
for (int i = 0; i < record.size(); i++) {
if (record.getValue(i).isNullOrEmpty()) {
values[i] = null;
} else {
values[i] =
TypeConvertUtil.valueUnwrap(
this.rowTypeInfo.getFieldType(i),
record.getValue(i));
synchronized (output.getCheckpointLock()) {
if (!splits.isEmpty()) {
try (ClickHouseResponse response = this.request.query(sql).executeAndWait()) {
response.stream()
.forEach(
record -> {
Object[] values =
new Object[this.rowTypeInfo.getFieldNames().length];
for (int i = 0; i < record.size(); i++) {
if (record.getValue(i).isNullOrEmpty()) {
values[i] = null;
} else {
values[i] =
TypeConvertUtil.valueUnwrap(
this.rowTypeInfo.getFieldType(i),
record.getValue(i));
}
}
}
output.collect(new SeaTunnelRow(values));
});
output.collect(new SeaTunnelRow(values));
});
}
this.readerContext.signalNoMoreElement();
this.splits.clear();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Extract clickhouse Close the task method

                this.readerContext.signalNoMoreElement();
                this.splits.clear();

if (noMoreSplit && splits.isEmpty()) {
log.info("Closed the bounded ClickHouse source");
this.readerContext.signalNoMoreElement();
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK

this.splits.clear();
}
this.readerContext.signalNoMoreElement();
this.splits.clear();
}
}

Expand All @@ -111,7 +122,9 @@ public void addSplits(List<ClickhouseSourceSplit> splits) {
}

@Override
public void handleNoMoreSplits() {}
public void handleNoMoreSplits() {
noMoreSplit = true;
}

@Override
public void notifyCheckpointComplete(long checkpointId) throws Exception {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public void registerReader(int subtaskId) {
if (assigned < 0) {
assigned = subtaskId;
context.assignSplit(subtaskId, new ClickhouseSourceSplit());
context.signalNoMoreSplits(subtaskId);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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


        if (assigned < 0) {
            assigned = subtaskId;
            context.assignSplit(subtaskId, new ClickhouseSourceSplit());
        }
        context.signalNoMoreSplits(subtaskId);

context.signalNoMoreSplits(subtaskId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public void testClickhouse(TestContainer container) throws Exception {
clearSinkTable();
}

@TestTemplate
public void testSourceParallelism(TestContainer container) throws Exception {
Container.ExecResult execResult = container.executeJob("/clickhouse_to_console.conf");
Assertions.assertEquals(0, execResult.getExitCode());
}

@BeforeAll
@Override
public void startUp() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
######
###### This config file is a demonstration of streaming processing in seatunnel config
######

env {
parallelism = 3
job.mode = "BATCH"
}

source {
# This is a example source plugin **only for test and demonstrate the feature source plugin**
Clickhouse {
host = "clickhouse:8123"
database = "default"
sql = "select * from source_table"
username = "default"
password = ""
result_table_name = "source_table"
}
# If you would like to get more information about how to configure seatunnel and see full list of source plugins,
# please go to https://seatunnel.apache.org/docs/connector-v2/source/ClickhouseSource
}

sink {
console {
}
# If you would like to get more information about how to configure seatunnel and see full list of sink plugins,
# please go to https://seatunnel.apache.org/docs/connector-v2/sink
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ seatunnel:
backup-count: 1
queue-type: blockingqueue
print-execution-info-interval: 60
print-job-metrics-info-interval: 60
Copy link
Contributor

Choose a reason for hiding this comment

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

Default value of print-job-metrics-info-interval is 60. This is valid without change

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 know that the default value is also used if it is not changed here, but here is a parameter to let you know where you can change it.
Otherwise, if you run an example in the project and want to change the parameters, the results will not work

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Default value of print-job-metrics-info-interval is 60. This is valid without change

I think this parameter can be added here, at least adding the default value will not cause any other bad effects
But it worked for me, at first I wanted to change the configuration, I didn't know where to change, I searched a lot of places in the project, and it didn't take effect after the change, and then it was someone else who pointed out that I knew to change here

Copy link
Contributor

Choose a reason for hiding this comment

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

get

Copy link
Member

Choose a reason for hiding this comment

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

You can configure it in your local environment. But do not commit it. Please revert it.

slot-service:
dynamic-slot: true
checkpoint:
Expand Down
Loading