35
35
import java .time .Instant ;
36
36
import java .time .temporal .ChronoUnit ;
37
37
import java .util .Map ;
38
- import java .util .OptionalLong ;
38
+ import java .util .Optional ;
39
39
import java .util .OptionalInt ;
40
- import java .util .stream .Stream ;
41
- import java .util .Spliterators ;
40
+ import java .util .OptionalLong ;
42
41
import java .util .Spliterator ;
42
+ import java .util .Spliterators ;
43
+ import java .util .stream .Stream ;
43
44
import java .util .stream .StreamSupport ;
44
45
45
46
import static software .amazon .lambda .powertools .core .internal .LambdaConstants .LAMBDA_FUNCTION_NAME_ENV ;
@@ -117,8 +118,13 @@ public void saveSuccess(JsonNode data, Object result, Instant now) {
117
118
} else {
118
119
responseJson = writer .writeValueAsString (result );
119
120
}
121
+ Optional <String > hashedIdempotencyKey = getHashedIdempotencyKey (data );
122
+ if (!hashedIdempotencyKey .isPresent ()) {
123
+ // missing idempotency key => non-idempotent transaction, we do not store the data, simply return
124
+ return ;
125
+ }
120
126
DataRecord record = new DataRecord (
121
- getHashedIdempotencyKey ( data ),
127
+ hashedIdempotencyKey . get ( ),
122
128
DataRecord .Status .COMPLETED ,
123
129
getExpiryEpochSecond (now ),
124
130
responseJson ,
@@ -140,8 +146,13 @@ public void saveSuccess(JsonNode data, Object result, Instant now) {
140
146
* @param now
141
147
*/
142
148
public void saveInProgress (JsonNode data , Instant now , OptionalInt remainingTimeInMs ) throws IdempotencyItemAlreadyExistsException {
143
- String idempotencyKey = getHashedIdempotencyKey (data );
149
+ Optional <String > hashedIdempotencyKey = getHashedIdempotencyKey (data );
150
+ if (!hashedIdempotencyKey .isPresent ()) {
151
+ // missing idempotency key => non-idempotent transaction, we do not store the data, simply return
152
+ return ;
153
+ }
144
154
155
+ String idempotencyKey = hashedIdempotencyKey .get ();
145
156
if (retrieveFromCache (idempotencyKey , now ) != null ) {
146
157
throw new IdempotencyItemAlreadyExistsException ();
147
158
}
@@ -170,8 +181,13 @@ public void saveInProgress(JsonNode data, Instant now, OptionalInt remainingTime
170
181
* @param throwable The throwable thrown by the function
171
182
*/
172
183
public void deleteRecord (JsonNode data , Throwable throwable ) {
173
- String idemPotencyKey = getHashedIdempotencyKey (data );
184
+ Optional <String > hashedIdempotencyKey = getHashedIdempotencyKey (data );
185
+ if (!hashedIdempotencyKey .isPresent ()) {
186
+ // missing idempotency key => non-idempotent transaction, we do not delete the data, simply return
187
+ return ;
188
+ }
174
189
190
+ String idemPotencyKey = hashedIdempotencyKey .get ();
175
191
LOG .debug ("Function raised an exception {}. " +
176
192
"Clearing in progress record in persistence store for idempotency key: {}" ,
177
193
throwable .getClass (),
@@ -190,8 +206,13 @@ public void deleteRecord(JsonNode data, Throwable throwable) {
190
206
* @throws IdempotencyItemNotFoundException Exception thrown if no record exists in persistence store with the idempotency key
191
207
*/
192
208
public DataRecord getRecord (JsonNode data , Instant now ) throws IdempotencyValidationException , IdempotencyItemNotFoundException {
193
- String idemPotencyKey = getHashedIdempotencyKey (data );
209
+ Optional <String > hashedIdempotencyKey = getHashedIdempotencyKey (data );
210
+ if (!hashedIdempotencyKey .isPresent ()) {
211
+ // missing idempotency key => non-idempotent transaction, we do not get the data, simply return nothing
212
+ return null ;
213
+ }
194
214
215
+ String idemPotencyKey = hashedIdempotencyKey .get ();
195
216
DataRecord cachedRecord = retrieveFromCache (idemPotencyKey , now );
196
217
if (cachedRecord != null ) {
197
218
LOG .debug ("Idempotency record found in cache with idempotency key: {}" , idemPotencyKey );
@@ -211,7 +232,7 @@ public DataRecord getRecord(JsonNode data, Instant now) throws IdempotencyValida
211
232
* @param data incoming data
212
233
* @return Hashed representation of the data extracted by the jmespath expression
213
234
*/
214
- private String getHashedIdempotencyKey (JsonNode data ) {
235
+ private Optional < String > getHashedIdempotencyKey (JsonNode data ) {
215
236
JsonNode node = data ;
216
237
217
238
if (eventKeyJMESPath != null ) {
@@ -221,13 +242,15 @@ private String getHashedIdempotencyKey(JsonNode data) {
221
242
if (isMissingIdemPotencyKey (node )) {
222
243
if (throwOnNoIdempotencyKey ) {
223
244
throw new IdempotencyKeyException ("No data found to create a hashed idempotency key" );
245
+ } else {
246
+ LOG .warn ("No data found to create a hashed idempotency key. JMESPath: {}" , eventKeyJMESPath );
247
+ return Optional .empty ();
224
248
}
225
- LOG .warn ("No data found to create a hashed idempotency key. JMESPath: {}" , eventKeyJMESPath );
226
249
}
227
250
228
251
String hash = generateHash (node );
229
252
hash = functionName + "#" + hash ;
230
- return hash ;
253
+ return Optional . of ( hash ) ;
231
254
}
232
255
233
256
private boolean isMissingIdemPotencyKey (JsonNode data ) {
0 commit comments