HADOOP-17608. Fix NPE in TestKMS (#2828)

This commit is contained in:
Akira Ajisaka 2021-04-01 01:57:31 +09:00 committed by GitHub
parent 6fd0c661b6
commit d69088a097
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 18 deletions

View File

@ -18,6 +18,8 @@
package org.apache.hadoop.crypto.key.kms.server; package org.apache.hadoop.crypto.key.kms.server;
import java.util.function.Supplier; import java.util.function.Supplier;
import org.apache.commons.lang3.ThreadUtils;
import org.apache.hadoop.thirdparty.com.google.common.cache.LoadingCache; import org.apache.hadoop.thirdparty.com.google.common.cache.LoadingCache;
import org.apache.curator.test.TestingServer; import org.apache.curator.test.TestingServer;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
@ -525,6 +527,7 @@ public class TestKMS {
if (ssl) { if (ssl) {
sslFactory = new SSLFactory(SSLFactory.Mode.CLIENT, conf); sslFactory = new SSLFactory(SSLFactory.Mode.CLIENT, conf);
try { try {
// the first reloader thread is created here
sslFactory.init(); sslFactory.init();
} catch (GeneralSecurityException ex) { } catch (GeneralSecurityException ex) {
throw new IOException(ex); throw new IOException(ex);
@ -541,31 +544,29 @@ public class TestKMS {
final URI uri = createKMSUri(getKMSUrl()); final URI uri = createKMSUri(getKMSUrl());
if (ssl) { if (ssl) {
// the second reloader thread is created here
KeyProvider testKp = createProvider(uri, conf); KeyProvider testKp = createProvider(uri, conf);
ThreadGroup threadGroup = Thread.currentThread().getThreadGroup(); Collection<Thread> reloaderThreads =
while (threadGroup.getParent() != null) { ThreadUtils.findThreadsByName(SSL_RELOADER_THREAD_NAME);
threadGroup = threadGroup.getParent(); // now there are two active reloader threads
} assertEquals(2, reloaderThreads.size());
Thread[] threads = new Thread[threadGroup.activeCount()]; // Explicitly close the provider so we can verify
threadGroup.enumerate(threads); // the second reloader thread is shutdown
Thread reloaderThread = null;
for (Thread thread : threads) {
if ((thread.getName() != null)
&& (thread.getName().contains(SSL_RELOADER_THREAD_NAME))) {
reloaderThread = thread;
}
}
Assert.assertTrue("Reloader is not alive", reloaderThread.isAlive());
// Explicitly close the provider so we can verify the internal thread
// is shutdown
testKp.close(); testKp.close();
boolean reloaderStillAlive = true; boolean reloaderStillAlive = true;
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
reloaderStillAlive = reloaderThread.isAlive(); for (Thread thread : reloaderThreads) {
if (!reloaderStillAlive) break; if (!thread.isAlive()) {
reloaderStillAlive = false;
break;
}
}
Thread.sleep(1000); Thread.sleep(1000);
} }
Assert.assertFalse("Reloader is still alive", reloaderStillAlive); Assert.assertFalse("Reloader is still alive", reloaderStillAlive);
reloaderThreads =
ThreadUtils.findThreadsByName(SSL_RELOADER_THREAD_NAME);
assertEquals(1, reloaderThreads.size());
} }
if (kerberos) { if (kerberos) {