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

Adding ttl_delete parameter to metadata for DynamoDB #4982

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

LeeroyHannigan
Copy link

Description

This change allows users to identify the source of a REMOVE event from DynamoDB streams, if it was a manual delete or a TTL delete.

Issues Resolved

Resolves #4560

Check List

  • New functionality includes testing.
  • New functionality has a documentation issue. Please link to it in this PR.
  • New functionality has javadoc added
  • Commits are signed with a real name per the DCO

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

graytaylor0
graytaylor0 previously approved these changes Sep 27, 2024
Copy link
Member

@graytaylor0 graytaylor0 left a comment

Choose a reason for hiding this comment

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

Thanks for making this contribution! Made some minor comments that would be nice to address but overall does look good.

* @throws Exception Exception if failed to write to buffer.
*/
public void addToBuffer(final AcknowledgementSet acknowledgementSet,
final Map<String, Object> data,
final Map<String, Object> keys,
final long eventCreationTimeMillis,
final long eventVersionNumber,
final String eventName) throws Exception {
final String eventName,
final Boolean userIdentity) throws Exception {
Copy link
Member

Choose a reason for hiding this comment

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

This boolean should named according to whether it's a TTL delete. Alternatively, it may make sense to make this a String and do the boolean conversion to TTL delete in this method

@@ -102,6 +105,7 @@ public void addToBuffer(final AcknowledgementSet acknowledgementSet,
eventMetadata.setAttribute(DDB_STREAM_EVENT_NAME_METADATA_ATTRIBUTE, eventName);
eventMetadata.setAttribute(EVENT_NAME_BULK_ACTION_METADATA_ATTRIBUTE, mapStreamEventNameToBulkAction(eventName));
eventMetadata.setAttribute(EVENT_VERSION_FROM_TIMESTAMP, eventVersionNumber);
eventMetadata.setAttribute(DDB_STREAM_EVENT_USER_IDENTITY, userIdentity);
Copy link
Member

Choose a reason for hiding this comment

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

It might make sense to only add this metadata attribute when it is a TTL delete. That way in the configuration later on for routes and things of that sort, users can just do a simple check on

getMetadata("ttl_delete") != null

rather than having to check explicitly for a true value with getMetadata("ttl_delete") == true . I am fine with it either way though.

Copy link
Member

Choose a reason for hiding this comment

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

Using a simple boolean (true/false) rather than three options (null/false/true) is easier to deal with.

If anything, I'd change this from capital Boolean which allows null to lowercase boolean which as a primitive can only be true or false.

@@ -80,6 +80,7 @@ public void writeToBuffer(final AcknowledgementSet acknowledgementSet, List<Reco
int eventCount = 0;
for (Record record : records) {
final long bytes = record.dynamodb().sizeBytes();
final Boolean userIdentity = record.userIdentity() != null && "dynamodb.amazonaws.com".equals(record.userIdentity().principalId());
Copy link
Member

Choose a reason for hiding this comment

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

This variable should be named isTtlDelete, or the userIdentity could be passed to addToBuffer directly as a String like my comment above.

Also, would be good to have "dynamodb.amazonaws.com" as a private static variable whenever it is used.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, these would be good improvements to the readability.

Copy link
Author

Choose a reason for hiding this comment

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

I moved the logic to addToBuffer but instead of passing a String, I was forced to pass the Identity as it also contains Type which was suggested for checking.

Copy link
Member

@dlvenable dlvenable left a comment

Choose a reason for hiding this comment

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

Thank you @LeeroyHannigan for this nice feature! I have a few comments, many also shared by @graytaylor0 .

@@ -102,6 +105,7 @@ public void addToBuffer(final AcknowledgementSet acknowledgementSet,
eventMetadata.setAttribute(DDB_STREAM_EVENT_NAME_METADATA_ATTRIBUTE, eventName);
eventMetadata.setAttribute(EVENT_NAME_BULK_ACTION_METADATA_ATTRIBUTE, mapStreamEventNameToBulkAction(eventName));
eventMetadata.setAttribute(EVENT_VERSION_FROM_TIMESTAMP, eventVersionNumber);
eventMetadata.setAttribute(DDB_STREAM_EVENT_USER_IDENTITY, userIdentity);
Copy link
Member

Choose a reason for hiding this comment

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

Using a simple boolean (true/false) rather than three options (null/false/true) is easier to deal with.

If anything, I'd change this from capital Boolean which allows null to lowercase boolean which as a primitive can only be true or false.

@@ -80,6 +80,7 @@ public void writeToBuffer(final AcknowledgementSet acknowledgementSet, List<Reco
int eventCount = 0;
for (Record record : records) {
final long bytes = record.dynamodb().sizeBytes();
final Boolean userIdentity = record.userIdentity() != null && "dynamodb.amazonaws.com".equals(record.userIdentity().principalId());
Copy link
Member

Choose a reason for hiding this comment

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

Yes, these would be good improvements to the readability.

@@ -21,4 +21,5 @@ public class MetadataKeyAttributes {
static final String DDB_STREAM_EVENT_NAME_METADATA_ATTRIBUTE = "dynamodb_event_name";

static final String EVENT_TABLE_NAME_METADATA_ATTRIBUTE = "table_name";
static final String DDB_STREAM_EVENT_USER_IDENTITY = "ttl_delete";
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
static final String DDB_STREAM_EVENT_USER_IDENTITY = "ttl_delete";
static final String DDB_STREAM_EVENT_IS_TTL_DELETE = "ttl_delete";

Copy link
Author

Choose a reason for hiding this comment

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

Renamed static variable

when(streamConfig.getStreamViewForRemoves()).thenReturn(StreamViewType.OLD_IMAGE);

final Identity userIdentity = Identity.builder()
.principalId("dynamodb.amazonaws.com")
Copy link
Member

Choose a reason for hiding this comment

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

Please include a test case where the userIdentity is not null, but the principal is something other than this known principle.

It would cover the case of && "dynamodb.amazonaws.com".equals(record.userIdentity().principalId()).

@LeeroyHannigan
Copy link
Author

Thank you both for review. Incorporated both your feedback, let me know if the current version is acceptable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add metadata to DynamoDB stream Events to indicate if they are deleted via TTL
3 participants