github-advanced-security[bot] commented on code in PR #19921:
URL: https://github.com/apache/druid/pull/19921#discussion_r3743262564
##########
indexing-service/src/test/java/org/apache/druid/indexing/overlord/GlobalTaskLockboxTest.java:
##########
@@ -1848,6 +1866,222 @@
validator.expectRevokedLocks(appendLock0, appendLock2, exclusiveLock,
replaceLock, sharedLock);
}
+ @Test
+ public void testKillLockCompatibility()
+ {
+ final Task killTask = newKillTask(MEDIUM_PRIORITY);
+ final TaskLock theLock = validator.expectKillLockCreated(killTask,
Intervals.of("2017/2018"));
+
+ // A KILL lock cannot coexist with another KILL lock on an overlapping
interval
+ validator.expectKillLockNotGranted(newKillTask(MEDIUM_PRIORITY),
Intervals.of("2017-05-01/2017-06-01"));
+
+ // A KILL lock can coexist with all other lock types
+ final TaskLock exclusiveLock = validator.expectLockCreated(
+ TaskLockType.EXCLUSIVE,
+ Intervals.of("2017-05-01/2017-06-01"),
+ MEDIUM_PRIORITY
+ );
+ final TaskLock sharedLock = validator.expectLockCreated(
+ TaskLockType.SHARED,
+ Intervals.of("2017-05-01/2017-06-01"),
+ MEDIUM_PRIORITY
+ );
+ final TaskLock replaceLock = validator.expectLockCreated(
+ TaskLockType.REPLACE,
+ Intervals.of("2017-03-01/2017-09-01"),
+ MEDIUM_PRIORITY
+ );
+ final TaskLock appendLock = validator.expectLockCreated(
+ TaskLockType.APPEND,
+ Intervals.of("2017-05-01/2017-06-01"),
+ MEDIUM_PRIORITY
+ );
+
+ validator.expectActiveLocks(theLock, exclusiveLock, sharedLock,
replaceLock, appendLock);
+ validator.expectRevokedLocks();
+ }
+
+ @Test
+ public void testKillLockCanRevokeIncompatibleKillLock()
+ {
+ final TaskLock lowPriorityKillLock = validator.expectKillLockCreated(
+ newKillTask(LOW_PRIORITY),
+ Intervals.of("2017-05-01/2017-06-01")
+ );
+
+ // A higher-priority KILL lock can revoke a lower-priority KILL lock
+ final TaskLock highPriorityKillLock = validator.expectKillLockCreated(
+ newKillTask(HIGH_PRIORITY),
+ Intervals.of("2017/2018")
+ );
+
+ validator.expectActiveLocks(highPriorityKillLock);
+ validator.expectRevokedLocks(lowPriorityKillLock);
+ }
+
+ @Test
+ public void testKillLockCannotRevokeHigherPriorityKillLock()
+ {
+ validator.expectKillLockCreated(newKillTask(HIGH_PRIORITY),
Intervals.of("2017-05-01/2017-06-01"));
+ validator.expectKillLockNotGranted(newKillTask(LOW_PRIORITY),
Intervals.of("2017/2018"));
+ }
+
+ @Test
+ public void testOnlyKillTaskCanAcquireKillLock()
+ {
+ final Task nonKillTask = NoopTask.ofPriority(MEDIUM_PRIORITY);
+ lockbox.add(nonKillTask);
+ taskStorage.insert(nonKillTask, TaskStatus.running(nonKillTask.getId()));
+
+ Assert.assertThrows(
+ ISE.class,
+ () -> lockbox.tryLock(nonKillTask, new
TimeChunkLockRequest(TaskLockType.KILL, nonKillTask, Intervals.of("2017/2018"),
null))
+ );
+ }
+
+ @Test
+ public void testKillLockAcquireAndReleaseWithoutTaskInStorage()
+ {
+ final Task killTask = newKillTask(MEDIUM_PRIORITY);
+
+ // Add task to lockbox only — do NOT insert into taskStorage (mirrors
EmbeddedKillTask behaviour)
+ lockbox.add(killTask);
+
+ final LockResult result = lockbox.tryLock(
+ killTask,
+ new TimeChunkLockRequest(TaskLockType.KILL, killTask,
Intervals.of("2017/2018"), null)
+ );
+ Assert.assertTrue(result.isOk());
+ final TaskLock killLock = result.getTaskLock();
+ Assert.assertNotNull(killLock);
+ Assert.assertEquals(TaskLockType.KILL, killLock.getType());
+ Assert.assertFalse(killLock.isRevoked());
+
+ // Verify the lock blocks another KILL on an overlapping interval
+ final Task otherKillTask = newKillTask(MEDIUM_PRIORITY);
+ lockbox.add(otherKillTask);
+ final LockResult blockedResult = lockbox.tryLock(
+ otherKillTask,
+ new TimeChunkLockRequest(TaskLockType.KILL, otherKillTask,
Intervals.of("2017-06-01/2017-07-01"), null)
+ );
+ Assert.assertFalse(blockedResult.isOk());
+
+ // Releasing the task removes the lock; the blocked task can now acquire
+ lockbox.remove(killTask);
+
+ final LockResult afterRelease = lockbox.tryLock(
+ otherKillTask,
+ new TimeChunkLockRequest(TaskLockType.KILL, otherKillTask,
Intervals.of("2017-06-01/2017-07-01"), null)
+ );
+ Assert.assertTrue(afterRelease.isOk());
+
+ lockbox.remove(otherKillTask);
+ }
+
+ @Test
+ public void testKillLockNotRestoredAfterSyncFromStorage()
+ {
+ // Acquire a KILL lock for a task that was never inserted into taskStorage
+ final Task killTask = newKillTask(MEDIUM_PRIORITY);
+ lockbox.add(killTask);
+ final LockResult result = lockbox.tryLock(
+ killTask,
+ new TimeChunkLockRequest(TaskLockType.KILL, killTask,
Intervals.of("2017/2018"), null)
+ );
+ Assert.assertTrue(result.isOk());
+
+ // Sync from storage — the kill task was not persisted, so the lock must
disappear
+ final GlobalTaskLockbox newBox = new GlobalTaskLockbox(taskStorage,
metadataStorageCoordinator);
+ newBox.syncFromStorage();
+
+ // No locks should be present after sync
+ final Set<TaskLock> locksAfterSync = taskStorage.getActiveTasks()
+ .stream()
+ .flatMap(t ->
taskStorage.getLocks(t.getId()).stream())
+
.collect(Collectors.toSet());
+ Assert.assertTrue(locksAfterSync.isEmpty());
+
+ // A new KILL lock on the same interval should now be grantable
+ final Task newKillTask = newKillTask(MEDIUM_PRIORITY);
+ newBox.add(newKillTask);
+ final LockResult newResult = newBox.tryLock(
+ newKillTask,
+ new TimeChunkLockRequest(TaskLockType.KILL, newKillTask,
Intervals.of("2017/2018"), null)
+ );
+ Assert.assertTrue(newResult.isOk());
+
+ newBox.remove(newKillTask);
+ }
+
+ @Test
+ public void testKillLockRevocationByHigherPriorityKillTaskNotInStorage()
+ {
+ // Low-priority kill task not in storage acquires a KILL lock
+ final Task lowPriorityKillTask = newKillTask(LOW_PRIORITY);
+ lockbox.add(lowPriorityKillTask);
+ final LockResult lowResult = lockbox.tryLock(
+ lowPriorityKillTask,
+ new TimeChunkLockRequest(TaskLockType.KILL, lowPriorityKillTask,
Intervals.of("2017-06-01/2017-07-01"), null)
+ );
+ Assert.assertTrue(lowResult.isOk());
+ final TaskLock lowPriorityLock = lowResult.getTaskLock();
Review Comment:
## CodeQL / Unread local variable
Variable 'TaskLock lowPriorityLock' is never read.
[Show more
details](https://github.com/apache/druid/security/code-scanning/11762)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]