Skip to content

fix: array jmespath fail in idempotency module #1420

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 1 commit into from
Sep 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;
Expand Down Expand Up @@ -122,17 +121,17 @@ public void saveSuccess(JsonNode data, Object result, Instant now) {
// missing idempotency key => non-idempotent transaction, we do not store the data, simply return
return;
}
DataRecord record = new DataRecord(
DataRecord dataRecord = new DataRecord(
hashedIdempotencyKey.get(),
DataRecord.Status.COMPLETED,
getExpiryEpochSecond(now),
responseJson,
getHashedPayload(data)
);
LOG.debug("Function successfully executed. Saving record to persistence store with idempotency key: {}",
record.getIdempotencyKey());
updateRecord(record);
saveToCache(record);
dataRecord.getIdempotencyKey());
updateRecord(dataRecord);
saveToCache(dataRecord);
} catch (JsonProcessingException e) {
// TODO : throw ?
throw new RuntimeException("Error while serializing the response", e);
Expand Down Expand Up @@ -164,16 +163,16 @@ public void saveInProgress(JsonNode data, Instant now, OptionalInt remainingTime
OptionalLong.of(now.plus(remainingTimeInMs.getAsInt(), ChronoUnit.MILLIS).toEpochMilli());
}

DataRecord record = new DataRecord(
DataRecord dataRecord = new DataRecord(
idempotencyKey,
DataRecord.Status.INPROGRESS,
getExpiryEpochSecond(now),
null,
getHashedPayload(data),
inProgressExpirationMsTimestamp
);
LOG.debug("saving in progress record for idempotency key: {}", record.getIdempotencyKey());
putRecord(record, now);
LOG.debug("saving in progress record for idempotency key: {}", dataRecord.getIdempotencyKey());
putRecord(dataRecord, now);
}

/**
Expand Down Expand Up @@ -223,10 +222,10 @@ public DataRecord getRecord(JsonNode data, Instant now)
return cachedRecord;
}

DataRecord record = getRecord(idemPotencyKey);
saveToCache(record);
validatePayload(data, record);
return record;
DataRecord dataRecord = getRecord(idemPotencyKey);
saveToCache(dataRecord);
validatePayload(data, dataRecord);
return dataRecord;
}

/**
Expand Down Expand Up @@ -258,10 +257,10 @@ private Optional<String> getHashedIdempotencyKey(JsonNode data) {

private boolean isMissingIdemPotencyKey(JsonNode data) {
if (data.isContainerNode()) {
Stream<Map.Entry<String, JsonNode>> stream =
StreamSupport.stream(Spliterators.spliteratorUnknownSize(data.fields(), Spliterator.ORDERED),
Stream<JsonNode> stream =
StreamSupport.stream(Spliterators.spliteratorUnknownSize(data.elements(), Spliterator.ORDERED),
false);
return stream.allMatch(e -> e.getValue().isNull());
return stream.allMatch(JsonNode::isNull);
}
return data.isNull();
}
Expand Down Expand Up @@ -378,10 +377,10 @@ private DataRecord retrieveFromCache(String idempotencyKey, Instant now) {
return null;
}

DataRecord record = cache.get(idempotencyKey);
if (record != null) {
if (!record.isExpired(now)) {
return record;
DataRecord dataRecord = cache.get(idempotencyKey);
if (dataRecord != null) {
if (!dataRecord.isExpired(now)) {
return dataRecord;
}
LOG.debug("Removing expired local cache record for idempotency key: {}", idempotencyKey);
deleteFromCache(idempotencyKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,28 @@ public void idempotencyOnSubMethodAnnotated_keyJMESPath_shouldPutInStoreWithKey(
"testFunction.createBasket#a1d0c6e83f027327d8461063f4ac58a6");
}

@Test
public void idempotencyOnSubMethodAnnotated_keyJMESPathArray_shouldPutInStoreWithKey() {
BasePersistenceStore persistenceStore = spy(BasePersistenceStore.class);

Idempotency.config()
.withPersistenceStore(persistenceStore)
.withConfig(IdempotencyConfig.builder().withEventKeyJMESPath("[id,name]").build())
.configure();

// WHEN
IdempotencyInternalFunctionInternalKey function = new IdempotencyInternalFunctionInternalKey();
Product p = new Product(42, "fake product", 12);
function.handleRequest(p, context);

// THEN
ArgumentCaptor<DataRecord> recordCaptor = ArgumentCaptor.forClass(DataRecord.class);
verify(persistenceStore).putRecord(recordCaptor.capture(), any());
// eec7cd392d9e3bb20deb2c9676697c3c = MD5([42,"fake product"])
assertThat(recordCaptor.getValue().getIdempotencyKey()).isEqualTo(
"testFunction.createBasket#eec7cd392d9e3bb20deb2c9676697c3c");
}

@Test
public void idempotencyOnSubMethodNotAnnotated_shouldThrowException() {
Idempotency.config()
Expand Down