Skip to content

Commit b1cb8c6

Browse files
committed
PR aws-powertools#717: changes post review
1 parent 265d282 commit b1cb8c6

File tree

6 files changed

+51
-55
lines changed

6 files changed

+51
-55
lines changed

powertools-idempotency/src/main/java/software/amazon/lambda/powertools/idempotency/IdempotencyConfig.java

+5-13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
package software.amazon.lambda.powertools.idempotency;
1515

16+
import software.amazon.lambda.powertools.utilities.cache.LRUCache;
17+
1618
/**
1719
* Configuration of the idempotency feature. Use the {@link Builder} to create an instance.
1820
*/
@@ -76,7 +78,7 @@ public static Builder builder() {
7678
public static class Builder {
7779

7880
private int localCacheMaxItems = 256;
79-
private boolean useLocalCache = false;
81+
private boolean useLocalCache = true;
8082
private int expirationInSeconds = 60 * 60; // 1 hour
8183
private String eventKeyJMESPath;
8284
private String payloadValidationJMESPath;
@@ -140,27 +142,17 @@ public Builder withLocalCacheMaxItems(int localCacheMaxItems) {
140142
}
141143

142144
/**
143-
* Whether to locally cache idempotency results, by default false
145+
* Whether to locally cache idempotency results, by default true
144146
*
145147
* @param useLocalCache boolean that indicate if a local cache must be used in addition to the persistence store.
146-
* If set to true, will use the {@link software.amazon.lambda.powertools.idempotency.persistence.cache.LRUCache}
148+
* If set to true, will use the {@link LRUCache}
147149
* @return the instance of the builder (to chain operations)
148150
*/
149151
public Builder withUseLocalCache(boolean useLocalCache) {
150152
this.useLocalCache = useLocalCache;
151153
return this;
152154
}
153155

154-
/**
155-
* Locally cache idempotency results.
156-
* Same as {@link #withUseLocalCache(boolean)} forced as true
157-
*
158-
* @return the instance of the builder (to chain operations)
159-
*/
160-
public Builder withUseLocalCache() {
161-
return withUseLocalCache(true);
162-
}
163-
164156
/**
165157
* The number of seconds to wait before a record is expired
166158
*

powertools-idempotency/src/main/java/software/amazon/lambda/powertools/idempotency/internal/IdempotencyHandler.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private Object processIdempotency() throws Throwable {
8484
} catch (IdempotencyKeyException ike) {
8585
throw ike;
8686
} catch (Exception e) {
87-
throw new IdempotencyPersistenceLayerException("Failed to save in progress record to idempotency store", e);
87+
throw new IdempotencyPersistenceLayerException("Failed to save in progress record to idempotency store. If you believe this is a powertools bug, please open an issue.", e);
8888
}
8989
return getFunctionResponse();
9090
}
@@ -101,10 +101,10 @@ private DataRecord getIdempotencyRecord() {
101101
// This code path will only be triggered if the record is removed between saveInProgress and getRecord
102102
LOG.debug("An existing idempotency record was deleted before we could fetch it");
103103
throw new IdempotencyInconsistentStateException("saveInProgress and getRecord return inconsistent results", e);
104-
} catch (IdempotencyValidationException ve) {
105-
throw ve;
104+
} catch (IdempotencyValidationException | IdempotencyKeyException vke) {
105+
throw vke;
106106
} catch (Exception e) {
107-
throw new IdempotencyPersistenceLayerException("Failed to get record from idempotency store", e);
107+
throw new IdempotencyPersistenceLayerException("Failed to get record from idempotency store. If you believe this is a powertools bug, please open an issue.", e);
108108
}
109109
}
110110

@@ -142,16 +142,18 @@ private Object getFunctionResponse() throws Throwable {
142142
// also raises an exception
143143
try {
144144
persistenceStore.deleteRecord(data, handlerException);
145+
} catch (IdempotencyKeyException ke) {
146+
throw ke;
145147
} catch (Exception e) {
146-
throw new IdempotencyPersistenceLayerException("Failed to delete record from idempotency store", e);
148+
throw new IdempotencyPersistenceLayerException("Failed to delete record from idempotency store. If you believe this is a powertools bug, please open an issue.", e);
147149
}
148150
throw handlerException;
149151
}
150152

151153
try {
152154
persistenceStore.saveSuccess(data, response, Instant.now());
153155
} catch (Exception e) {
154-
throw new IdempotencyPersistenceLayerException("Failed to update record state to success in idempotency store", e);
156+
throw new IdempotencyPersistenceLayerException("Failed to update record state to success in idempotency store. If you believe this is a powertools bug, please open an issue.", e);
155157
}
156158
return response;
157159
}

powertools-idempotency/src/main/java/software/amazon/lambda/powertools/idempotency/persistence/BasePersistenceStore.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import software.amazon.lambda.powertools.idempotency.exceptions.IdempotencyItemNotFoundException;
2727
import software.amazon.lambda.powertools.idempotency.exceptions.IdempotencyKeyException;
2828
import software.amazon.lambda.powertools.idempotency.exceptions.IdempotencyValidationException;
29-
import software.amazon.lambda.powertools.idempotency.persistence.cache.LRUCache;
3029
import software.amazon.lambda.powertools.utilities.JsonConfig;
30+
import software.amazon.lambda.powertools.utilities.cache.LRUCache;
3131

3232
import java.math.BigInteger;
3333
import java.nio.charset.StandardCharsets;
@@ -70,7 +70,7 @@ public abstract class BasePersistenceStore implements PersistenceStore {
7070
*/
7171
public void configure(IdempotencyConfig config, String functionName) {
7272
String funcEnv = System.getenv(Constants.LAMBDA_FUNCTION_NAME_ENV);
73-
this.functionName = funcEnv != null ? funcEnv : "test-func";
73+
this.functionName = funcEnv != null ? funcEnv : "testFunction";
7474
if (!StringUtils.isEmpty(functionName)) {
7575
this.functionName += "." + functionName;
7676
}

powertools-idempotency/src/main/java/software/amazon/lambda/powertools/idempotency/persistence/DynamoDBPersistenceStore.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void putRecord(DataRecord record, Instant now) throws IdempotencyItemAlre
131131
);
132132
} catch (ConditionalCheckFailedException e) {
133133
LOG.debug("Failed to put record for already existing idempotency key: {}", record.getIdempotencyKey());
134-
throw new IdempotencyItemAlreadyExistsException("Failed to put record for already existing idempotency key: {}", e);
134+
throw new IdempotencyItemAlreadyExistsException("Failed to put record for already existing idempotency key: " + record.getIdempotencyKey(), e);
135135
}
136136
}
137137

powertools-idempotency/src/test/java/software/amazon/lambda/powertools/idempotency/internal/IdempotencyAspectTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ public void idempotencyOnSubMethodAnnotated_firstCall_shouldPutInStore() {
205205

206206
ArgumentCaptor<Basket> resultCaptor = ArgumentCaptor.forClass(Basket.class);
207207
verify(store).saveSuccess(any(), resultCaptor.capture(), any());
208-
assertThat(resultCaptor.getValue().getProducts()).contains(basket.getProducts().get(0));
209-
assertThat(resultCaptor.getValue().getProducts()).contains(new Product(0, "fake", 0));
208+
assertThat(resultCaptor.getValue().getProducts()).contains(basket.getProducts().get(0), new Product(0, "fake", 0));
210209
}
211210

212211
@Test

0 commit comments

Comments
 (0)