Skip to content

Commit a9b4396

Browse files
li-leyangomalley
authored andcommitted
HDFS-16518: Add shutdownhook to invalidate the KeyProviders in the cache
Fixes #4100 Signed-off-by: Owen O'Malley <[email protected]>
1 parent eb16421 commit a9b4396

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/KeyProviderCache.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.hadoop.conf.Configuration;
2727
import org.apache.hadoop.crypto.key.KeyProvider;
2828
import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
29+
import org.apache.hadoop.fs.FileSystem;
2930
import org.apache.hadoop.util.KMSUtil;
3031

3132
import org.apache.hadoop.classification.VisibleForTesting;
@@ -34,6 +35,7 @@
3435
import org.apache.hadoop.thirdparty.com.google.common.cache.RemovalListener;
3536
import org.apache.hadoop.thirdparty.com.google.common.cache.RemovalNotification;
3637

38+
import org.apache.hadoop.util.ShutdownHookManager;
3739
import org.slf4j.Logger;
3840
import org.slf4j.LoggerFactory;
3941

@@ -65,6 +67,9 @@ public void onRemoval(
6567
}
6668
})
6769
.build();
70+
71+
ShutdownHookManager.get().addShutdownHook(new KeyProviderCacheFinalizer(),
72+
SHUTDOWN_HOOK_PRIORITY);
6873
}
6974

7075
public KeyProvider get(final Configuration conf,
@@ -85,6 +90,26 @@ public KeyProvider call() throws Exception {
8590
}
8691
}
8792

93+
public static final int SHUTDOWN_HOOK_PRIORITY = FileSystem.SHUTDOWN_HOOK_PRIORITY - 1;
94+
95+
private class KeyProviderCacheFinalizer implements Runnable {
96+
@Override
97+
public synchronized void run() {
98+
invalidateCache();
99+
}
100+
}
101+
102+
/**
103+
* Invalidate cache. KeyProviders in the cache will be closed by cache hook.
104+
*/
105+
@VisibleForTesting
106+
synchronized void invalidateCache() {
107+
LOG.debug("Invalidating all cached KeyProviders.");
108+
if (cache != null) {
109+
cache.invalidateAll();
110+
}
111+
}
112+
88113
private URI createKeyProviderURI(Configuration conf) {
89114
final String providerUriStr = conf.getTrimmed(
90115
CommonConfigurationKeysPublic.HADOOP_SECURITY_KEY_PROVIDER_PATH);

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestKeyProviderCache.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class TestKeyProviderCache {
3232

3333
public static class DummyKeyProvider extends KeyProvider {
3434

35+
public static int CLOSE_CALL_COUNT = 0;
36+
3537
public DummyKeyProvider(Configuration conf) {
3638
super(conf);
3739
}
@@ -76,6 +78,10 @@ public KeyVersion rollNewVersion(String name, byte[] material)
7678
public void flush() throws IOException {
7779
}
7880

81+
@Override
82+
public void close() {
83+
CLOSE_CALL_COUNT += 1;
84+
}
7985
}
8086

8187
public static class Factory extends KeyProviderFactory {
@@ -124,6 +130,9 @@ public void testCache() throws Exception {
124130
Assert.assertFalse("Same KeyProviders returned !!",
125131
keyProvider1 == keyProvider4);
126132

133+
kpCache.invalidateCache();
134+
Assert.assertEquals("Expected number of closing calls doesn't match",
135+
3, DummyKeyProvider.CLOSE_CALL_COUNT);
127136
}
128137

129138
private URI getKeyProviderUriFromConf(Configuration conf) {

0 commit comments

Comments
 (0)