55
55
import static org .mockito .ArgumentMatchers .any ;
56
56
import static org .mockito .ArgumentMatchers .anyLong ;
57
57
import static org .mockito .ArgumentMatchers .eq ;
58
- import static org .mockito .ArgumentMatchers .isNull ;
58
+ import static org .mockito .ArgumentMatchers .refEq ;
59
59
import static org .mockito .Mockito .atLeastOnce ;
60
60
import static org .mockito .Mockito .doAnswer ;
61
61
import static org .mockito .Mockito .doReturn ;
@@ -75,7 +75,7 @@ class TestStorageBasedLockProvider {
75
75
private HeartbeatManager mockHeartbeatManager ;
76
76
private Logger mockLogger ;
77
77
private final String ownerId = UUID .randomUUID ().toString ();
78
- private static final int DEFAULT_LOCK_VALIDITY_MS = 5000 ;
78
+ private static final int DEFAULT_LOCK_VALIDITY_MS = 10000 ;
79
79
80
80
@ BeforeEach
81
81
void setupLockProvider () {
@@ -174,10 +174,13 @@ void testTryLockForTimeUnitFailsToAcquireLockEventually() throws Exception {
174
174
175
175
@ Test
176
176
void testTryLockSuccess () {
177
- when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , null ));
178
- StorageLockData data = new StorageLockData (false , System .currentTimeMillis () + DEFAULT_LOCK_VALIDITY_MS , ownerId );
177
+ long t0 = 1_000L ;
178
+ when (lockProvider .getCurrentEpochMs ())
179
+ .thenReturn (t0 );
180
+ when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , Option .empty ()));
181
+ StorageLockData data = new StorageLockData (false , t0 + DEFAULT_LOCK_VALIDITY_MS , ownerId );
179
182
StorageLockFile realLockFile = new StorageLockFile (data , "v1" );
180
- when (mockLockService .tryUpsertLockFile (any ( ), isNull ( )))
183
+ when (mockLockService .tryUpsertLockFile (refEq ( data ), eq ( Option . empty () )))
181
184
.thenReturn (Pair .of (LockUpsertResult .SUCCESS , Option .of (realLockFile )));
182
185
when (mockHeartbeatManager .startHeartbeatForThread (any ())).thenReturn (true );
183
186
@@ -189,10 +192,10 @@ void testTryLockSuccess() {
189
192
190
193
@ Test
191
194
void testTryLockSuccessButFailureToStartHeartbeat () {
192
- when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , null ));
195
+ when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , Option . empty () ));
193
196
StorageLockData data = new StorageLockData (false , System .currentTimeMillis () + DEFAULT_LOCK_VALIDITY_MS , ownerId );
194
197
StorageLockFile realLockFile = new StorageLockFile (data , "v1" );
195
- when (mockLockService .tryUpsertLockFile (any (), isNull ( )))
198
+ when (mockLockService .tryUpsertLockFile (any (), eq ( Option . empty () )))
196
199
.thenReturn (Pair .of (LockUpsertResult .SUCCESS , Option .of (realLockFile )));
197
200
when (mockHeartbeatManager .startHeartbeatForThread (any ())).thenReturn (false );
198
201
when (mockLockService .tryUpsertLockFile (any (), eq (Option .of (realLockFile ))))
@@ -204,10 +207,10 @@ void testTryLockSuccessButFailureToStartHeartbeat() {
204
207
205
208
@ Test
206
209
void testTryLockFailsFromOwnerMismatch () {
207
- when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , null ));
210
+ when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , Option . empty () ));
208
211
StorageLockFile returnedLockFile = new StorageLockFile (
209
212
new StorageLockData (false , System .currentTimeMillis () + DEFAULT_LOCK_VALIDITY_MS , "different-owner" ), "v1" );
210
- when (mockLockService .tryUpsertLockFile (any (), isNull ( )))
213
+ when (mockLockService .tryUpsertLockFile (any (), eq ( Option . empty () )))
211
214
.thenReturn (Pair .of (LockUpsertResult .SUCCESS , Option .of (returnedLockFile )));
212
215
213
216
HoodieLockException ex = assertThrows (HoodieLockException .class , () -> lockProvider .tryLock ());
@@ -227,15 +230,15 @@ void testTryLockFailsDueToExistingLock() {
227
230
228
231
@ Test
229
232
void testTryLockFailsToUpdateFile () {
230
- when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , null ));
231
- when (mockLockService .tryUpsertLockFile (any (), isNull ( )))
232
- .thenReturn (Pair .of (LockUpsertResult .ACQUIRED_BY_OTHERS , null ));
233
+ when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , Option . empty () ));
234
+ when (mockLockService .tryUpsertLockFile (any (), eq ( Option . empty () )))
235
+ .thenReturn (Pair .of (LockUpsertResult .ACQUIRED_BY_OTHERS , Option . empty () ));
233
236
assertFalse (lockProvider .tryLock ());
234
237
}
235
238
236
239
@ Test
237
240
void testTryLockFailsDueToUnknownState () {
238
- when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .UNKNOWN_ERROR , null ));
241
+ when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .UNKNOWN_ERROR , Option . empty () ));
239
242
assertFalse (lockProvider .tryLock ());
240
243
}
241
244
@@ -257,10 +260,10 @@ void testTryLockSucceedsWhenExistingLockExpiredByTime() {
257
260
258
261
@ Test
259
262
void testTryLockReentrancySucceeds () {
260
- when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , null ));
263
+ when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , Option . empty () ));
261
264
StorageLockData data = new StorageLockData (false , System .currentTimeMillis () + DEFAULT_LOCK_VALIDITY_MS , ownerId );
262
265
StorageLockFile realLockFile = new StorageLockFile (data , "v1" );
263
- when (mockLockService .tryUpsertLockFile (any (), isNull ( )))
266
+ when (mockLockService .tryUpsertLockFile (any (), eq ( Option . empty () )))
264
267
.thenReturn (Pair .of (LockUpsertResult .SUCCESS , Option .of (realLockFile )));
265
268
when (mockHeartbeatManager .startHeartbeatForThread (any ())).thenReturn (true );
266
269
@@ -284,11 +287,11 @@ void testTryLockReentrancyAfterLockExpiredByTime() {
284
287
StorageLockData data = new StorageLockData (false , System .currentTimeMillis () - DEFAULT_LOCK_VALIDITY_MS , ownerId );
285
288
StorageLockFile expiredLock = new StorageLockFile (data , "v1" );
286
289
doReturn (expiredLock ).when (lockProvider ).getLock ();
287
- when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , null ));
290
+ when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , Option . empty () ));
288
291
StorageLockData validData = new StorageLockData (false , System .currentTimeMillis () - DEFAULT_LOCK_VALIDITY_MS ,
289
292
ownerId );
290
293
StorageLockFile validLock = new StorageLockFile (validData , "v2" );
291
- when (mockLockService .tryUpsertLockFile (any (), isNull ( )))
294
+ when (mockLockService .tryUpsertLockFile (any (), eq ( Option . empty () )))
292
295
.thenReturn (Pair .of (LockUpsertResult .SUCCESS , Option .of (validLock )));
293
296
when (mockHeartbeatManager .startHeartbeatForThread (any ())).thenReturn (true );
294
297
@@ -309,11 +312,11 @@ void testTryLockReentrancyAfterLockSetExpired() {
309
312
StorageLockData data = new StorageLockData (true , System .currentTimeMillis () + DEFAULT_LOCK_VALIDITY_MS , ownerId );
310
313
StorageLockFile expiredLock = new StorageLockFile (data , "v1" );
311
314
doReturn (expiredLock ).when (lockProvider ).getLock ();
312
- when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , null ));
315
+ when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , Option . empty () ));
313
316
StorageLockData validData = new StorageLockData (false , System .currentTimeMillis () - DEFAULT_LOCK_VALIDITY_MS ,
314
317
ownerId );
315
318
StorageLockFile validLock = new StorageLockFile (validData , "v2" );
316
- when (mockLockService .tryUpsertLockFile (any (), isNull ( )))
319
+ when (mockLockService .tryUpsertLockFile (any (), eq ( Option . empty () )))
317
320
.thenReturn (Pair .of (LockUpsertResult .SUCCESS , Option .of (validLock )));
318
321
when (mockHeartbeatManager .startHeartbeatForThread (any ())).thenReturn (true );
319
322
@@ -334,17 +337,17 @@ void testTryLockHeartbeatStillActive() {
334
337
StorageLockData data = new StorageLockData (true , System .currentTimeMillis () + DEFAULT_LOCK_VALIDITY_MS , ownerId );
335
338
StorageLockFile expiredLock = new StorageLockFile (data , "v1" );
336
339
doReturn (expiredLock ).when (lockProvider ).getLock ();
337
- when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , null ));
340
+ when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , Option . empty () ));
338
341
when (mockHeartbeatManager .hasActiveHeartbeat ()).thenReturn (true );
339
342
assertThrows (HoodieLockException .class , () -> lockProvider .tryLock ());
340
343
}
341
344
342
345
@ Test
343
346
void testUnlockSucceedsAndReentrancy () {
344
- when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , null ));
347
+ when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , Option . empty () ));
345
348
StorageLockData data = new StorageLockData (false , System .currentTimeMillis () + DEFAULT_LOCK_VALIDITY_MS , ownerId );
346
349
StorageLockFile realLockFile = new StorageLockFile (data , "v1" );
347
- when (mockLockService .tryUpsertLockFile (any (), isNull ( )))
350
+ when (mockLockService .tryUpsertLockFile (any (), eq ( Option . empty () )))
348
351
.thenReturn (Pair .of (LockUpsertResult .SUCCESS , Option .of (realLockFile )));
349
352
when (mockHeartbeatManager .startHeartbeatForThread (any ())).thenReturn (true );
350
353
when (mockHeartbeatManager .stopHeartbeat (true )).thenReturn (true );
@@ -363,10 +366,10 @@ void testUnlockSucceedsAndReentrancy() {
363
366
364
367
@ Test
365
368
void testUnlockFailsToStopHeartbeat () {
366
- when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , null ));
369
+ when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , Option . empty () ));
367
370
StorageLockData data = new StorageLockData (false , System .currentTimeMillis () + DEFAULT_LOCK_VALIDITY_MS , ownerId );
368
371
StorageLockFile realLockFile = new StorageLockFile (data , "v1" );
369
- when (mockLockService .tryUpsertLockFile (any (), isNull ( )))
372
+ when (mockLockService .tryUpsertLockFile (any (), eq ( Option . empty () )))
370
373
.thenReturn (Pair .of (LockUpsertResult .SUCCESS , Option .of (realLockFile )));
371
374
when (mockHeartbeatManager .startHeartbeatForThread (any ())).thenReturn (true );
372
375
assertTrue (lockProvider .tryLock ());
@@ -378,10 +381,10 @@ void testUnlockFailsToStopHeartbeat() {
378
381
379
382
@ Test
380
383
void testCloseFailsToStopHeartbeat () {
381
- when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , null ));
384
+ when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , Option . empty () ));
382
385
StorageLockData data = new StorageLockData (false , System .currentTimeMillis () + DEFAULT_LOCK_VALIDITY_MS , ownerId );
383
386
StorageLockFile realLockFile = new StorageLockFile (data , "v1" );
384
- when (mockLockService .tryUpsertLockFile (any (), isNull ( )))
387
+ when (mockLockService .tryUpsertLockFile (any (), eq ( Option . empty () )))
385
388
.thenReturn (Pair .of (LockUpsertResult .SUCCESS , Option .of (realLockFile )));
386
389
when (mockHeartbeatManager .startHeartbeatForThread (any ())).thenReturn (true );
387
390
assertTrue (lockProvider .tryLock ());
@@ -427,7 +430,7 @@ void testRenewLockUnableToUpsertLockFileButNotFatal() {
427
430
// Signal the upsert attempt failed, but may be transient. See interface for
428
431
// more details.
429
432
when (mockLockService .tryUpsertLockFile (any (), eq (Option .of (lockFile ))))
430
- .thenReturn (Pair .of (LockUpsertResult .UNKNOWN_ERROR , null ));
433
+ .thenReturn (Pair .of (LockUpsertResult .UNKNOWN_ERROR , Option . empty () ));
431
434
assertTrue (lockProvider .renewLock ());
432
435
}
433
436
@@ -439,7 +442,7 @@ void testRenewLockUnableToUpsertLockFileFatal() {
439
442
// Signal the upsert attempt failed, but may be transient. See interface for
440
443
// more details.
441
444
when (mockLockService .tryUpsertLockFile (any (), eq (Option .of (lockFile ))))
442
- .thenReturn (Pair .of (LockUpsertResult .UNKNOWN_ERROR , null ));
445
+ .thenReturn (Pair .of (LockUpsertResult .UNKNOWN_ERROR , Option . empty () ));
443
446
// renewLock return true so it will be retried.
444
447
assertTrue (lockProvider .renewLock ());
445
448
@@ -522,10 +525,10 @@ void testCloseWithErrorForHeartbeatManager() throws Exception {
522
525
523
526
@ Test
524
527
public void testShutdownHookViaReflection () throws Exception {
525
- when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , null ));
528
+ when (mockLockService .readCurrentLockFile ()).thenReturn (Pair .of (LockGetResult .NOT_EXISTS , Option . empty () ));
526
529
StorageLockData data = new StorageLockData (false , System .currentTimeMillis () + DEFAULT_LOCK_VALIDITY_MS , ownerId );
527
530
StorageLockFile realLockFile = new StorageLockFile (data , "v1" );
528
- when (mockLockService .tryUpsertLockFile (any (), isNull ( )))
531
+ when (mockLockService .tryUpsertLockFile (any (), eq ( Option . empty () )))
529
532
.thenReturn (Pair .of (LockUpsertResult .SUCCESS , Option .of (realLockFile )));
530
533
when (mockHeartbeatManager .startHeartbeatForThread (any ())).thenReturn (true );
531
534
0 commit comments