21
21
import org .slf4j .LoggerFactory ;
22
22
import software .amazon .awssdk .utils .StringUtils ;
23
23
import software .amazon .lambda .powertools .idempotency .IdempotencyConfig ;
24
- import software .amazon .lambda .powertools .idempotency .exceptions .IdempotencyItemAlreadyExistsException ;
25
- import software .amazon .lambda .powertools .idempotency .exceptions .IdempotencyItemNotFoundException ;
26
- import software .amazon .lambda .powertools .idempotency .exceptions .IdempotencyKeyException ;
27
- import software .amazon .lambda .powertools .idempotency .exceptions .IdempotencyValidationException ;
24
+ import software .amazon .lambda .powertools .idempotency .exceptions .*;
28
25
import software .amazon .lambda .powertools .idempotency .internal .cache .LRUCache ;
29
26
import software .amazon .lambda .powertools .utilities .JsonConfig ;
30
27
34
31
import java .security .NoSuchAlgorithmException ;
35
32
import java .time .Instant ;
36
33
import java .time .temporal .ChronoUnit ;
37
- import java .util .Map ;
38
- import java .util .OptionalLong ;
39
- import java .util .OptionalInt ;
34
+ import java .util .*;
40
35
import java .util .stream .Stream ;
41
- import java .util .Spliterators ;
42
- import java .util .Spliterator ;
43
36
import java .util .stream .StreamSupport ;
44
37
45
38
import static software .amazon .lambda .powertools .core .internal .LambdaConstants .LAMBDA_FUNCTION_NAME_ENV ;
@@ -130,6 +123,9 @@ public void saveSuccess(JsonNode data, Object result, Instant now) {
130
123
} catch (JsonProcessingException e ) {
131
124
// TODO : throw ?
132
125
throw new RuntimeException ("Error while serializing the response" , e );
126
+ } catch (NullIdempotencyKeyException e ) {
127
+ // missing idempotency key => non-idempotent transaction, we do not store the data, simply return
128
+ return ;
133
129
}
134
130
}
135
131
@@ -140,7 +136,13 @@ public void saveSuccess(JsonNode data, Object result, Instant now) {
140
136
* @param now
141
137
*/
142
138
public void saveInProgress (JsonNode data , Instant now , OptionalInt remainingTimeInMs ) throws IdempotencyItemAlreadyExistsException {
143
- String idempotencyKey = getHashedIdempotencyKey (data );
139
+ String idempotencyKey ;
140
+ try {
141
+ idempotencyKey = getHashedIdempotencyKey (data );
142
+ } catch (NullIdempotencyKeyException e ) {
143
+ // missing idempotency key => non-idempotent transaction, we do not store the data, simply return
144
+ return ;
145
+ }
144
146
145
147
if (retrieveFromCache (idempotencyKey , now ) != null ) {
146
148
throw new IdempotencyItemAlreadyExistsException ();
@@ -170,7 +172,13 @@ public void saveInProgress(JsonNode data, Instant now, OptionalInt remainingTime
170
172
* @param throwable The throwable thrown by the function
171
173
*/
172
174
public void deleteRecord (JsonNode data , Throwable throwable ) {
173
- String idemPotencyKey = getHashedIdempotencyKey (data );
175
+ String idemPotencyKey ;
176
+ try {
177
+ idemPotencyKey = getHashedIdempotencyKey (data );
178
+ } catch (NullIdempotencyKeyException e ) {
179
+ // missing idempotency key => non-idempotent transaction, we do not delete the data, simply return
180
+ return ;
181
+ }
174
182
175
183
LOG .debug ("Function raised an exception {}. " +
176
184
"Clearing in progress record in persistence store for idempotency key: {}" ,
@@ -190,7 +198,13 @@ public void deleteRecord(JsonNode data, Throwable throwable) {
190
198
* @throws IdempotencyItemNotFoundException Exception thrown if no record exists in persistence store with the idempotency key
191
199
*/
192
200
public DataRecord getRecord (JsonNode data , Instant now ) throws IdempotencyValidationException , IdempotencyItemNotFoundException {
193
- String idemPotencyKey = getHashedIdempotencyKey (data );
201
+ String idemPotencyKey ;
202
+ try {
203
+ idemPotencyKey = getHashedIdempotencyKey (data );
204
+ } catch (NullIdempotencyKeyException e ) {
205
+ // missing idempotency key => non-idempotent transaction, we do not get the data, simply return nothing
206
+ return null ;
207
+ }
194
208
195
209
DataRecord cachedRecord = retrieveFromCache (idemPotencyKey , now );
196
210
if (cachedRecord != null ) {
@@ -211,7 +225,7 @@ public DataRecord getRecord(JsonNode data, Instant now) throws IdempotencyValida
211
225
* @param data incoming data
212
226
* @return Hashed representation of the data extracted by the jmespath expression
213
227
*/
214
- private String getHashedIdempotencyKey (JsonNode data ) {
228
+ private String getHashedIdempotencyKey (JsonNode data ) throws NullIdempotencyKeyException {
215
229
JsonNode node = data ;
216
230
217
231
if (eventKeyJMESPath != null ) {
@@ -221,8 +235,10 @@ private String getHashedIdempotencyKey(JsonNode data) {
221
235
if (isMissingIdemPotencyKey (node )) {
222
236
if (throwOnNoIdempotencyKey ) {
223
237
throw new IdempotencyKeyException ("No data found to create a hashed idempotency key" );
238
+ } else {
239
+ LOG .warn ("No data found to create a hashed idempotency key. JMESPath: {}" , eventKeyJMESPath );
240
+ throw new NullIdempotencyKeyException ("No data found to create a hashed idempotency key" );
224
241
}
225
- LOG .warn ("No data found to create a hashed idempotency key. JMESPath: {}" , eventKeyJMESPath );
226
242
}
227
243
228
244
String hash = generateHash (node );
0 commit comments