sandynz commented on code in PR #28734:
URL: https://github.com/apache/shardingsphere/pull/28734#discussion_r1359878990
##########
kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/importer/sink/PipelineDataSourceSink.java:
##########
@@ -133,67 +136,47 @@ private void tryFlush(final DataSource dataSource, final
List<DataRecord> buffer
}
}
- private void doFlush(final DataSource dataSource, final List<DataRecord>
buffer) throws SQLException {
+ private void doFlush(final DataSource dataSource, final List<DataRecord>
buffer, final boolean enableTransaction) throws SQLException {
try (Connection connection = dataSource.getConnection()) {
- connection.setAutoCommit(false);
- switch (buffer.get(0).getType()) {
- case IngestDataChangeType.INSERT:
- if (null != rateLimitAlgorithm) {
- rateLimitAlgorithm.intercept(JobOperationType.INSERT,
1);
- }
- executeBatchInsert(connection, buffer);
- break;
- case IngestDataChangeType.UPDATE:
- if (null != rateLimitAlgorithm) {
- rateLimitAlgorithm.intercept(JobOperationType.UPDATE,
1);
- }
- executeUpdate(connection, buffer);
- break;
- case IngestDataChangeType.DELETE:
- if (null != rateLimitAlgorithm) {
- rateLimitAlgorithm.intercept(JobOperationType.DELETE,
1);
- }
- executeBatchDelete(connection, buffer);
- break;
- default:
- break;
+ if (enableTransaction) {
+ connection.setAutoCommit(false);
}
- connection.commit();
- }
- }
-
- private void doFlush(final Connection connection, final List<DataRecord>
buffer) {
- // TODO it's better use transaction, but execute delete maybe not
effect when open transaction of PostgreSQL sometimes
- for (DataRecord each : buffer) {
- try {
- doFlush(connection, each);
- } catch (final SQLException ex) {
- throw new
PipelineImporterJobWriteException(String.format("Write failed, record=%s",
each), ex);
+ Set<String> dataRecordTypes =
buffer.stream().map(DataRecord::getType).collect(Collectors.toSet());
Review Comment:
Group by data record type is done before, it's better not do it any more
##########
kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/importer/sink/PipelineDataSourceSink.java:
##########
@@ -133,67 +136,47 @@ private void tryFlush(final DataSource dataSource, final
List<DataRecord> buffer
}
}
- private void doFlush(final DataSource dataSource, final List<DataRecord>
buffer) throws SQLException {
+ private void doFlush(final DataSource dataSource, final List<DataRecord>
buffer, final boolean enableTransaction) throws SQLException {
try (Connection connection = dataSource.getConnection()) {
- connection.setAutoCommit(false);
- switch (buffer.get(0).getType()) {
- case IngestDataChangeType.INSERT:
- if (null != rateLimitAlgorithm) {
- rateLimitAlgorithm.intercept(JobOperationType.INSERT,
1);
- }
- executeBatchInsert(connection, buffer);
- break;
- case IngestDataChangeType.UPDATE:
- if (null != rateLimitAlgorithm) {
- rateLimitAlgorithm.intercept(JobOperationType.UPDATE,
1);
- }
- executeUpdate(connection, buffer);
- break;
- case IngestDataChangeType.DELETE:
- if (null != rateLimitAlgorithm) {
- rateLimitAlgorithm.intercept(JobOperationType.DELETE,
1);
- }
- executeBatchDelete(connection, buffer);
- break;
- default:
- break;
+ if (enableTransaction) {
+ connection.setAutoCommit(false);
}
- connection.commit();
- }
- }
Review Comment:
Could we just remove added parameter `enableTransaction`? Judge whether to
enable transaction by `buffer.size`. And keep these code not deleted. It could
be simpler.
##########
kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/importer/sink/PipelineDataSourceSink.java:
##########
@@ -105,7 +105,10 @@ private PipelineJobProgressUpdatedParameter flush(final
DataSource dataSource, f
flushInternal(dataSource, each.getBatchDeleteDataRecords());
flushInternal(dataSource, each.getBatchInsertDataRecords());
flushInternal(dataSource, each.getBatchUpdateDataRecords());
- sequentialFlush(dataSource, each.getNonBatchRecords());
+ if (each.getNonBatchRecords().isEmpty()) {
+ continue;
+ }
+ tryFlush(dataSource, each.getNonBatchRecords(), false);
Review Comment:
Could we use for each of `each.getNonBatchRecords()` and invoke `doFlush`?
Then `doFlush` could be simplified.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]