Skip to content

Commit 8416d44

Browse files
committed
HADOOP-16341. ShutDownHookManager: Regressed performance on Hook removals after HADOOP-15679
1 parent 738fab3 commit 8416d44

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ShutdownHookManager.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ TimeUnit getTimeUnit() {
254254
private AtomicBoolean shutdownInProgress = new AtomicBoolean(false);
255255

256256
//private to constructor to ensure singularity
257-
private ShutdownHookManager() {
257+
@VisibleForTesting
258+
@InterfaceAudience.Private
259+
ShutdownHookManager() {
258260
}
259261

260262
/**
@@ -267,8 +269,8 @@ private ShutdownHookManager() {
267269
@VisibleForTesting
268270
List<HookEntry> getShutdownHooksInOrder() {
269271
List<HookEntry> list;
270-
synchronized (MGR.hooks) {
271-
list = new ArrayList<>(MGR.hooks);
272+
synchronized (hooks) {
273+
list = new ArrayList<>(hooks);
272274
}
273275
Collections.sort(list, new Comparator<HookEntry>() {
274276

@@ -342,7 +344,9 @@ public boolean removeShutdownHook(Runnable shutdownHook) {
342344
throw new IllegalStateException("Shutdown in progress, cannot remove a " +
343345
"shutdownHook");
344346
}
345-
return hooks.remove(new HookEntry(shutdownHook, 0));
347+
// hooks are only == by runnable
348+
return hooks.remove(new HookEntry(shutdownHook, 0, TIMEOUT_MINIMUM,
349+
TIME_UNIT_DEFAULT));
346350
}
347351

348352
/**
@@ -354,7 +358,8 @@ public boolean removeShutdownHook(Runnable shutdownHook) {
354358
@InterfaceAudience.Public
355359
@InterfaceStability.Stable
356360
public boolean hasShutdownHook(Runnable shutdownHook) {
357-
return hooks.contains(new HookEntry(shutdownHook, 0));
361+
return hooks.contains(new HookEntry(shutdownHook, 0, TIMEOUT_MINIMUM,
362+
TIME_UNIT_DEFAULT));
358363
}
359364

360365
/**

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestShutdownHookManager.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,10 @@ public class TestShutdownHookManager {
4343
LoggerFactory.getLogger(TestShutdownHookManager.class.getName());
4444

4545
/**
46-
* remove all the shutdown hooks so that they never get invoked later
47-
* on in this test process.
46+
* A new instance of ShutdownHookManager to ensure parallel tests
47+
* don't have shared context
4848
*/
49-
@After
50-
public void clearShutdownHooks() {
51-
ShutdownHookManager.get().clearShutdownHooks();
52-
}
49+
private final ShutdownHookManager mgr = new ShutdownHookManager();
5350

5451
/**
5552
* Verify hook registration, then execute the hook callback stage
@@ -58,7 +55,6 @@ public void clearShutdownHooks() {
5855
*/
5956
@Test
6057
public void shutdownHookManager() {
61-
ShutdownHookManager mgr = ShutdownHookManager.get();
6258
assertNotNull("No ShutdownHookManager", mgr);
6359
assertEquals(0, mgr.getShutdownHooksInOrder().size());
6460
Hook hook1 = new Hook("hook1", 0, false);
@@ -193,7 +189,6 @@ public void testShutdownTimeoutBadConfiguration() throws Throwable {
193189
*/
194190
@Test
195191
public void testDuplicateRegistration() throws Throwable {
196-
ShutdownHookManager mgr = ShutdownHookManager.get();
197192
Hook hook = new Hook("hook1", 0, false);
198193

199194
// add the hook
@@ -222,6 +217,20 @@ public void testDuplicateRegistration() throws Throwable {
222217

223218
}
224219

220+
@Test
221+
public void testShutdownRemove() throws Throwable {
222+
assertNotNull("No ShutdownHookManager", mgr);
223+
assertEquals(0, mgr.getShutdownHooksInOrder().size());
224+
Hook hook1 = new Hook("hook1", 0, false);
225+
Hook hook2 = new Hook("hook2", 0, false);
226+
mgr.addShutdownHook(hook1, 9); // create Hook1 with priority 9
227+
assertTrue(mgr.hasShutdownHook(hook1)); // hook1 lookup works
228+
assertEquals(1, mgr.getShutdownHooksInOrder().size()); // 1 hook
229+
assertFalse(mgr.removeShutdownHook(hook2)); // can't delete hook2
230+
assertTrue(mgr.removeShutdownHook(hook1)); // can delete hook1
231+
assertEquals(0, mgr.getShutdownHooksInOrder().size()); // no more hooks
232+
}
233+
225234
private static final AtomicInteger INVOCATION_COUNT = new AtomicInteger();
226235

227236
/**

0 commit comments

Comments
 (0)