Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Elias-Schoof
Copy link
Contributor

This PR ensures that Togglz 4 is able to work with DynamoDB tables which were previously managed by Togglz 3 or lower.

Problem

As part of Release 4.0.0-M3, this commit upgraded the DynamoDBStateRepository from AWS SDK version 1 to version 2. Before the upgrade, the toggle feature state was stored in DynamoDB using com.amazonaws.services.dynamodbv2.document.Item.withJSON:

String json = objectMapper.writeValueAsString(FeatureStateStorageWrapper.wrapperForFeatureState(featureState));
Item featureStateEntry =
  new Item()
    .withPrimaryKey(primaryKeyAttribute, featureState.getFeature().name())
    .withJSON(FEATURE_STATE_ATTRIBUTE_NAME, json);
table.putItem(featureStateEntry);

As a result of using withJSON, the DynamoDB client unpacked the JSON string and turned it into a map. Here is an example using Togglz 2.9.8:
image

After the upgrade, the DynamoDBStateRepository instead stores the toggle feature state as a (JSON) string with AttributeValue.builder().s(json).build():

String json = objectMapper.writeValueAsString(FeatureStateStorageWrapper.wrapperForFeatureState(featureState));
UpdateItemRequest request = UpdateItemRequest.builder()
  .tableName(tableName)
  .key(Map.of(primaryKeyAttribute, AttributeValue.builder()
    .s(featureState.getFeature().name())
    .build()))
  .attributeUpdates(Map.of(FEATURE_STATE_ATTRIBUTE_NAME, AttributeValueUpdate.builder()
    .action(AttributeAction.PUT)
    .value(AttributeValue.builder()
      .s(json)
      .build())
    .build()))
  .build();
dynamoDbClient.updateItem(request);

Here is an example of the same feature using Togglz 4.3.0:
image

The change breaks backwards compatibility. Togglz 4 cannot read features from a table which was managed by Togglz 3 or lower. The object mapper will throw a NullPointerException.

Proposed solution

The main goal of the proposed solution is to be compatible with Togglz 3 and lower and with existing releases of Togglz 4.
To achieve this, the read method DynamoDBStateRepository.getFeatureState is adapted to understand both representations: The string representation of version 4 and the map representations of versions 3 and lower.
The write method DynamoDBStateRepository.setFeatureState remains unchanged and still stores feature state as a (JSON) string.

As a result, a feature is automatically "migrated" from the old map representation to the new string representation when it is changed with setFeatureState. Until all features have been changed at least one, a DynamoDB table can contain some features with the old representation and some features with the new representation.

@codecov-commenter
Copy link

Codecov Report

Patch and project coverage have no change.

Comparison is base (e2eabf4) 55.42% compared to head (7cf10fa) 55.42%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #1120   +/-   ##
=========================================
  Coverage     55.42%   55.42%           
  Complexity      928      928           
=========================================
  Files           180      180           
  Lines          4601     4601           
  Branches        603      603           
=========================================
  Hits           2550     2550           
  Misses         1893     1893           
  Partials        158      158           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bennetelli bennetelli self-requested a review September 25, 2023 07:31
@bennetelli bennetelli merged commit 5a5fb14 into togglz:master Sep 25, 2023
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.

3 participants