Skip to content

Commit

Permalink
fix #1419 (#1420)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromevdl committed Oct 12, 2023
1 parent 3da32e8 commit 1165a36
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
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

0 comments on commit 1165a36

Please sign in to comment.