-
Notifications
You must be signed in to change notification settings - Fork 330
Add acknowledgment checkpointing with fixes to previous issues #6002
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
415c4c3
Revert "Revert "Revert "Revert DDB checkpoint acknowledgment changes …
graytaylor0 4ab4877
Fix issues with acknowledgment timeouts and data loss with checkpoint…
graytaylor0 4f36f88
Add more test coverage
graytaylor0 9137963
Add more test coverage
graytaylor0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
.../java/org/opensearch/dataprepper/plugins/source/dynamodb/model/ShardCheckpointStatus.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| * | ||
| */ | ||
|
|
||
| package org.opensearch.dataprepper.plugins.source.dynamodb.model; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
|
|
||
| public class ShardCheckpointStatus { | ||
| private final String sequenceNumber; | ||
|
|
||
| private final boolean isFinalAcknowledgmentForPartition; | ||
| private AcknowledgmentStatus acknowledgeStatus; | ||
| private final long createTimestamp; | ||
| private Long acknowledgedTimestamp; | ||
|
|
||
| public enum AcknowledgmentStatus { | ||
| POSITIVE_ACK, | ||
| NEGATIVE_ACK, | ||
| NO_ACK | ||
| } | ||
|
|
||
| public ShardCheckpointStatus(final String sequenceNumber, final long createTimestamp, final boolean isFinalAcknowledgmentForPartition) { | ||
| this.sequenceNumber = sequenceNumber; | ||
| this.acknowledgeStatus = AcknowledgmentStatus.NO_ACK; | ||
| this.createTimestamp = createTimestamp; | ||
| this.isFinalAcknowledgmentForPartition = isFinalAcknowledgmentForPartition; | ||
| } | ||
|
|
||
| public void setAcknowledgedTimestamp(final Long acknowledgedTimestamp) { | ||
| this.acknowledgedTimestamp = acknowledgedTimestamp; | ||
| } | ||
|
|
||
| public void setAcknowledged(final AcknowledgmentStatus acknowledgmentStatus) { | ||
| this.acknowledgeStatus = acknowledgmentStatus; | ||
| } | ||
|
|
||
| public String getSequenceNumber() { | ||
| return sequenceNumber; | ||
| } | ||
|
|
||
| public boolean isPositiveAcknowledgement() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add unit tests for these methods with logic.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do |
||
| return this.acknowledgeStatus == AcknowledgmentStatus.POSITIVE_ACK; | ||
| } | ||
|
|
||
| public boolean isNegativeAcknowledgement() { | ||
| return this.acknowledgeStatus == AcknowledgmentStatus.NEGATIVE_ACK; | ||
| } | ||
|
|
||
| public boolean isFinalAcknowledgmentForPartition() { | ||
| return isFinalAcknowledgmentForPartition; | ||
| } | ||
|
|
||
| public boolean isExpired(final Duration expiredDuration) { | ||
| return Duration.between(Instant.ofEpochMilli(createTimestamp), Instant.now()).compareTo(expiredDuration) > 0; | ||
| } | ||
|
|
||
| } | ||
266 changes: 266 additions & 0 deletions
266
...rg/opensearch/dataprepper/plugins/source/dynamodb/stream/ShardAcknowledgementManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,266 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| * | ||
| */ | ||
|
|
||
| package org.opensearch.dataprepper.plugins.source.dynamodb.stream; | ||
|
|
||
| import org.opensearch.dataprepper.model.acknowledgements.AcknowledgementSet; | ||
| import org.opensearch.dataprepper.model.acknowledgements.AcknowledgementSetManager; | ||
| import org.opensearch.dataprepper.model.source.coordinator.enhanced.EnhancedSourceCoordinator; | ||
| import org.opensearch.dataprepper.common.concurrent.BackgroundThreadFactory; | ||
| import org.opensearch.dataprepper.model.source.coordinator.enhanced.EnhancedSourcePartition; | ||
| import org.opensearch.dataprepper.plugins.source.dynamodb.DynamoDBSourceConfig; | ||
| import org.opensearch.dataprepper.plugins.source.dynamodb.coordination.partition.StreamPartition; | ||
| import org.opensearch.dataprepper.plugins.source.dynamodb.coordination.state.StreamProgressState; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentLinkedQueue; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import org.opensearch.dataprepper.plugins.source.dynamodb.model.ShardCheckpointStatus; | ||
|
|
||
| public class ShardAcknowledgementManager { | ||
| private static final Logger LOG = LoggerFactory.getLogger(ShardAcknowledgementManager.class); | ||
|
|
||
| private static final String NULL_SEQUENCE_NUMBER = "null"; | ||
|
|
||
| private static final long WAIT_FOR_ACKNOWLEDGMENTS_TIMEOUT = 10L; | ||
|
|
||
| static final Duration CHECKPOINT_INTERVAL = Duration.ofMinutes(2); | ||
|
|
||
| private final DynamoDBSourceConfig dynamoDBSourceConfig; | ||
| private final Map<StreamPartition, ConcurrentLinkedQueue<ShardCheckpointStatus>> checkpoints = new ConcurrentHashMap<>(); | ||
| private final ConcurrentHashMap<StreamPartition, ConcurrentHashMap<String, ShardCheckpointStatus>> ackStatuses = new ConcurrentHashMap<>(); | ||
|
|
||
| private final AcknowledgementSetManager acknowledgementSetManager; | ||
|
|
||
| private final EnhancedSourceCoordinator sourceCoordinator; | ||
|
|
||
| private final ExecutorService executorService; | ||
| private final List<StreamPartition> partitionsToRemove; | ||
| private final List<StreamPartition> partitionsToGiveUp; | ||
| private boolean shutdownTriggered; | ||
|
|
||
| private Instant lastCheckpointTime; | ||
|
|
||
| public ShardAcknowledgementManager(final AcknowledgementSetManager acknowledgementSetManager, | ||
| final EnhancedSourceCoordinator sourceCoordinator, | ||
| final DynamoDBSourceConfig dynamoDBSourceConfig, | ||
| final Consumer<StreamPartition> stopWorkerConsumer | ||
| ) { | ||
| this.acknowledgementSetManager = acknowledgementSetManager; | ||
| this.sourceCoordinator = sourceCoordinator; | ||
| this.dynamoDBSourceConfig = dynamoDBSourceConfig; | ||
| this.executorService = Executors.newSingleThreadExecutor(BackgroundThreadFactory.defaultExecutorThreadFactory("dynamodb-shard-ack-monitor")); | ||
| this.partitionsToRemove = Collections.synchronizedList(new ArrayList<>()); | ||
| this.partitionsToGiveUp = Collections.synchronizedList(new ArrayList<>()); | ||
| this.lastCheckpointTime = Instant.now(); | ||
|
|
||
| executorService.submit(() -> monitorAcknowledgments(stopWorkerConsumer)); | ||
| } | ||
|
|
||
| void monitorAcknowledgments(final Consumer<StreamPartition> stopWorkerConsumer) { | ||
| while (!Thread.currentThread().isInterrupted()) { | ||
| boolean exit = runMonitorAcknowledgmentLoop(stopWorkerConsumer); | ||
| if (exit) { | ||
| break; | ||
| } | ||
|
|
||
| try { | ||
| Thread.sleep(2000); | ||
| } catch (InterruptedException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| LOG.info("Exiting acknowledgment manager"); | ||
| } | ||
|
|
||
| boolean runMonitorAcknowledgmentLoop(final Consumer<StreamPartition> stopWorkerConsumer) { | ||
| removePartitions(); | ||
| if (shutdownTriggered) { | ||
| LOG.info("Shutdown was triggered giving up partitions and exiting cleanly"); | ||
| for (final StreamPartition streamPartition : checkpoints.keySet()) { | ||
| sourceCoordinator.giveUpPartition(streamPartition); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| for (final StreamPartition streamPartition : checkpoints.keySet()) { | ||
| try { | ||
| final StreamProgressState streamProgressState = streamPartition.getProgressState().orElseThrow(); | ||
| final ConcurrentLinkedQueue<ShardCheckpointStatus> checkpointStatuses = checkpoints.get(streamPartition); | ||
| ShardCheckpointStatus latestCheckpointForShard = null; | ||
| boolean gaveUpPartition = false; | ||
| while (!checkpointStatuses.isEmpty()) { | ||
| updateOwnershipForAllShardPartitions(); | ||
|
|
||
| if (checkpointStatuses.peek().isPositiveAcknowledgement()) { | ||
| latestCheckpointForShard = checkpointStatuses.poll(); | ||
| if (latestCheckpointForShard != null) { | ||
| ackStatuses.get(streamPartition).remove(latestCheckpointForShard.getSequenceNumber()); | ||
| } | ||
| } else if (checkpointStatuses.peek().isNegativeAcknowledgement() | ||
| || checkpointStatuses.peek().isExpired(dynamoDBSourceConfig.getShardAcknowledgmentTimeout())) { | ||
| handleFailure(streamPartition, streamProgressState, latestCheckpointForShard); | ||
| gaveUpPartition = true; | ||
|
|
||
| if (checkpointStatuses.peek().isNegativeAcknowledgement()) { | ||
| LOG.warn("Received negative acknowledgment for partition {} with sequence number {}, giving up partition", | ||
| streamPartition.getPartitionKey(), checkpointStatuses.peek().getSequenceNumber()); | ||
| } else { | ||
| LOG.warn("Acknowledgment timed out for partition {} with sequence number {}, giving up partition", | ||
| streamPartition.getPartitionKey(), checkpointStatuses.peek().getSequenceNumber()); | ||
| } | ||
| break; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (!gaveUpPartition) { | ||
| updateOwnershipForAllShardPartitions(); | ||
| } | ||
|
|
||
| if (gaveUpPartition || latestCheckpointForShard == null) { | ||
| continue; | ||
| } | ||
|
|
||
| if (latestCheckpointForShard.isFinalAcknowledgmentForPartition()) { | ||
| handleCompletedShard(streamPartition); | ||
| } else { | ||
| streamProgressState.setSequenceNumber(Objects.equals(latestCheckpointForShard.getSequenceNumber(), NULL_SEQUENCE_NUMBER) ? null : latestCheckpointForShard.getSequenceNumber()); | ||
| sourceCoordinator.saveProgressStateForPartition(streamPartition, dynamoDBSourceConfig.getShardAcknowledgmentTimeout()); | ||
| LOG.debug("Checkpointed shard {} with latest sequence number acknowledged {}", streamPartition.getShardId(), latestCheckpointForShard.getSequenceNumber()); | ||
| } | ||
| if (partitionsToGiveUp.contains(streamPartition)) { | ||
| partitionsToRemove.add(streamPartition); | ||
| sourceCoordinator.giveUpPartition(streamPartition); | ||
| } | ||
|
|
||
| } catch (final Exception e) { | ||
| LOG.error("Received exception while monitoring acknowledgments for stream partition {}", streamPartition.getPartitionKey(), e); | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public AcknowledgementSet createAcknowledgmentSet( | ||
| final StreamPartition streamPartition, | ||
| final String sequenceNumber, | ||
| final boolean isFinalSetForPartition) { | ||
| final String sequenceNumberNoNull = sequenceNumber == null ? NULL_SEQUENCE_NUMBER : sequenceNumber; | ||
| final ShardCheckpointStatus shardCheckpointStatus = new ShardCheckpointStatus(sequenceNumber, Instant.now().toEpochMilli(), isFinalSetForPartition); | ||
| checkpoints.computeIfAbsent(streamPartition, segment -> new ConcurrentLinkedQueue<>()).add(shardCheckpointStatus); | ||
| ackStatuses.computeIfAbsent(streamPartition, segment -> new ConcurrentHashMap<>()); | ||
| ackStatuses.get(streamPartition).put(sequenceNumberNoNull, shardCheckpointStatus); | ||
|
|
||
| return acknowledgementSetManager.create((result) -> { | ||
| if (ackStatuses.containsKey(streamPartition) && ackStatuses.get(streamPartition).containsKey(sequenceNumberNoNull)) { | ||
| final ShardCheckpointStatus ackCheckpointStatus = ackStatuses.get(streamPartition).get(sequenceNumberNoNull); | ||
|
|
||
| ackCheckpointStatus.setAcknowledgedTimestamp(Instant.now().toEpochMilli()); | ||
|
|
||
| if (result) { | ||
| LOG.debug("Received acknowledgment of completion from sink for partition {} with sequence number {}", | ||
| streamPartition.getPartitionKey(), sequenceNumberNoNull); | ||
| ackCheckpointStatus.setAcknowledged(ShardCheckpointStatus.AcknowledgmentStatus.POSITIVE_ACK); | ||
| } else { | ||
| LOG.warn("Negative acknowledgment received for partition {} with sequence number {}", | ||
| streamPartition.getPartitionKey(), sequenceNumberNoNull); | ||
| ackCheckpointStatus.setAcknowledged(ShardCheckpointStatus.AcknowledgmentStatus.NEGATIVE_ACK); | ||
| } | ||
| } | ||
| }, dynamoDBSourceConfig.getShardAcknowledgmentTimeout()); | ||
| } | ||
|
|
||
| void updateOwnershipForAllShardPartitions() { | ||
| if (Duration.between(lastCheckpointTime, Instant.now()).compareTo(CHECKPOINT_INTERVAL) > 0) { | ||
| for (final StreamPartition streamPartition : checkpoints.keySet()) { | ||
| if (!partitionsToRemove.contains(streamPartition)) { | ||
| sourceCoordinator.saveProgressStateForPartition(streamPartition, dynamoDBSourceConfig.getShardAcknowledgmentTimeout()); | ||
| } | ||
| } | ||
|
|
||
| lastCheckpointTime = Instant.now(); | ||
| } | ||
| } | ||
|
|
||
| private void handleFailure(final StreamPartition streamPartition, | ||
| final StreamProgressState streamProgressState, | ||
| final ShardCheckpointStatus latestCheckpointForShard) { | ||
| if (latestCheckpointForShard != null) { | ||
| streamProgressState.setSequenceNumber(latestCheckpointForShard.getSequenceNumber()); | ||
| sourceCoordinator.saveProgressStateForPartition(streamPartition, dynamoDBSourceConfig.getShardAcknowledgmentTimeout()); | ||
| } | ||
| partitionsToRemove.add(streamPartition); | ||
| sourceCoordinator.giveUpPartition(streamPartition); | ||
| partitionsToGiveUp.remove(streamPartition); | ||
| } | ||
|
|
||
| private void handleCompletedShard(final StreamPartition streamPartition) { | ||
| sourceCoordinator.completePartition(streamPartition); | ||
| partitionsToRemove.add(streamPartition); | ||
| partitionsToGiveUp.remove(streamPartition); | ||
| LOG.info("Received all acknowledgments for partition {}, marking partition as completed", streamPartition.getPartitionKey()); | ||
| } | ||
|
|
||
| public void shutdown() { | ||
| shutdownTriggered = true; | ||
| executorService.shutdown(); | ||
| try { | ||
| if (!executorService.awaitTermination(WAIT_FOR_ACKNOWLEDGMENTS_TIMEOUT, TimeUnit.MINUTES)) { | ||
| executorService.shutdownNow(); | ||
| } | ||
| } catch (InterruptedException e) { | ||
| executorService.shutdownNow(); | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| } | ||
|
|
||
| private void removePartitions() { | ||
| partitionsToRemove.forEach(streamPartition -> { | ||
| checkpoints.remove(streamPartition); | ||
| ackStatuses.remove(streamPartition); | ||
| }); | ||
|
|
||
| partitionsToRemove.clear(); | ||
| } | ||
|
|
||
| public void giveUpPartition(final StreamPartition streamPartition) { | ||
| if (!partitionsToGiveUp.contains(streamPartition)) { | ||
| LOG.debug("Adding partition {} to give up list", streamPartition.getPartitionKey()); | ||
| partitionsToGiveUp.add(streamPartition); | ||
| } | ||
| } | ||
|
|
||
| public boolean isExportDone(StreamPartition streamPartition) { | ||
| Optional<EnhancedSourcePartition> globalPartition = sourceCoordinator.getPartition(streamPartition.getStreamArn()); | ||
| return globalPartition.isPresent(); | ||
| } | ||
|
|
||
| public void startUpdatingOwnershipForShard(final StreamPartition streamPartition) { | ||
| checkpoints.computeIfAbsent(streamPartition, segment -> new ConcurrentLinkedQueue<>()); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shardIds across different Stream-ARNs will be different? Just making sure shardIds won't get repeated
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They should be unique, but we will evaluate if we ever enable support for more than one table in the future.