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]Remove scheduler in Dynamodb sink #5248

Merged
merged 2 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion docs/en/connector-v2/sink/AmazonDynamoDB.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Write data to Amazon DynamoDB
| secret_access_key | string | yes | - |
| table | string | yes | - |
| batch_size | string | no | 25 |
| batch_interval_ms | string | no | 1000 |
| common-options | | no | - |

### url [string]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class AmazonDynamoDBSourceOptions implements Serializable {
private Config schema;

public int batchSize = AmazonDynamoDBConfig.BATCH_SIZE.defaultValue();
public int batchIntervalMs = AmazonDynamoDBConfig.BATCH_INTERVAL_MS.defaultValue();

public AmazonDynamoDBSourceOptions(Config config) {
this.url = config.getString(AmazonDynamoDBConfig.URL.key());
Expand All @@ -57,8 +56,5 @@ public AmazonDynamoDBSourceOptions(Config config) {
if (config.hasPath(AmazonDynamoDBConfig.BATCH_SIZE.key())) {
this.batchSize = config.getInt(AmazonDynamoDBConfig.BATCH_SIZE.key());
}
if (config.hasPath(AmazonDynamoDBConfig.BATCH_INTERVAL_MS.key())) {
this.batchIntervalMs = config.getInt(AmazonDynamoDBConfig.BATCH_INTERVAL_MS.key());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.seatunnel.connectors.seatunnel.common.sink.AbstractSinkWriter;

import java.io.IOException;
import java.util.Optional;

public class AmazonDynamoDBWriter extends AbstractSinkWriter<SeaTunnelRow, Void> {

Expand All @@ -48,4 +49,10 @@ public void write(SeaTunnelRow element) throws IOException {
public void close() throws IOException {
dynamoDbSinkClient.close();
}

@Override
public Optional<Void> prepareCommit() {
dynamoDbSinkClient.flush();
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.seatunnel.connectors.seatunnel.amazondynamodb.serialize.DefaultSeaTunnelRowDeserializer;
import org.apache.seatunnel.connectors.seatunnel.amazondynamodb.serialize.SeaTunnelRowDeserializer;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
Expand All @@ -40,15 +39,9 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class DynamoDbSinkClient {
private final AmazonDynamoDBSourceOptions amazondynamodbSourceOptions;
private ScheduledExecutorService scheduler;
private ScheduledFuture<?> scheduledFuture;
private volatile boolean initialize;
private volatile Exception flushException;
private DynamoDbClient dynamoDbClient;
Expand All @@ -62,7 +55,7 @@ public DynamoDbSinkClient(
this.seaTunnelRowDeserializer = new DefaultSeaTunnelRowDeserializer(typeInfo);
}

private void tryInit() throws IOException {
private void tryInit() {
if (initialize) {
return;
}
Expand All @@ -78,25 +71,6 @@ private void tryInit() throws IOException {
amazondynamodbSourceOptions.getAccessKeyId(),
amazondynamodbSourceOptions.getSecretAccessKey())))
.build();

scheduler =
Executors.newSingleThreadScheduledExecutor(
new ThreadFactoryBuilder()
.setNameFormat("DdynamoDb-sink-output-%s")
.build());
scheduledFuture =
scheduler.scheduleAtFixedRate(
() -> {
try {
flush();
} catch (IOException e) {
flushException = e;
}
},
amazondynamodbSourceOptions.getBatchIntervalMs(),
amazondynamodbSourceOptions.getBatchIntervalMs(),
TimeUnit.MILLISECONDS);

initialize = true;
}

Expand All @@ -114,17 +88,13 @@ public synchronized void write(PutItemRequest putItemRequest) throws IOException
}

public synchronized void close() throws IOException {
if (scheduledFuture != null) {
scheduledFuture.cancel(false);
scheduler.shutdown();
}
if (dynamoDbClient != null) {
flush();
dynamoDbClient.close();
}
}

synchronized void flush() throws IOException {
synchronized void flush() {
checkFlushException();
if (batchList.isEmpty()) {
return;
Expand Down
Loading