This is an automated email from the ASF dual-hosted git repository.

duong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new b7cde03bb3 HDDS-10089. ManagedSecretKey.macInstances should not be 
ThreadLocal (#5946)
b7cde03bb3 is described below

commit b7cde03bb3bf872000243377ef544a477abdc962
Author: Sumit Agrawal <[email protected]>
AuthorDate: Thu Jan 11 12:23:06 2024 +0530

    HDDS-10089. ManagedSecretKey.macInstances should not be ThreadLocal (#5946)
---
 .../hadoop/hdds/security/symmetric/ManagedSecretKey.java      | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git 
a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/security/symmetric/ManagedSecretKey.java
 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/security/symmetric/ManagedSecretKey.java
index 2ff44daf9b..6313671dd1 100644
--- 
a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/security/symmetric/ManagedSecretKey.java
+++ 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/security/symmetric/ManagedSecretKey.java
@@ -19,6 +19,8 @@
 package org.apache.hadoop.hdds.security.symmetric;
 
 import com.google.protobuf.ByteString;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import org.apache.hadoop.hdds.protocol.proto.SCMSecretKeyProtocolProtos;
 import org.apache.hadoop.security.token.TokenIdentifier;
 import org.apache.hadoop.util.ProtobufUtils;
@@ -41,7 +43,7 @@ public final class ManagedSecretKey {
   private final Instant creationTime;
   private final Instant expiryTime;
   private final SecretKey secretKey;
-  private final ThreadLocal<Mac> macInstances;
+  private final Map<Long, Mac> macInstances = new ConcurrentHashMap();
 
   public ManagedSecretKey(UUID id,
                           Instant creationTime,
@@ -51,9 +53,12 @@ public final class ManagedSecretKey {
     this.creationTime = creationTime;
     this.expiryTime = expiryTime;
     this.secretKey = secretKey;
+  }
 
+  private Mac getMac() {
     // This help reuse Mac instances for the same thread.
-    macInstances = ThreadLocal.withInitial(() -> {
+    long threadId = Thread.currentThread().getId();
+    return macInstances.computeIfAbsent(threadId, k -> {
       try {
         return Mac.getInstance(secretKey.getAlgorithm());
       } catch (NoSuchAlgorithmException e) {
@@ -105,7 +110,7 @@ public final class ManagedSecretKey {
 
   public byte[] sign(byte[] data) {
     try {
-      Mac mac = macInstances.get();
+      Mac mac = getMac();
       mac.init(secretKey);
       return mac.doFinal(data);
     } catch (InvalidKeyException e) {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to