Skip to content

Commit

Permalink
Fixed FinalizationStateManagerImpl, SCMCertStore & ScmSecretKeyStateB…
Browse files Browse the repository at this point in the history
…uilder
  • Loading branch information
nandakumar131 committed Feb 19, 2025
1 parent 9d7356c commit f3c24ff
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.google.protobuf.InvalidProtocolBufferException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumMap;
Expand Down Expand Up @@ -293,14 +292,5 @@ public RaftPeerId getLeaderId() {
return leaderId;
}

@Override
@SuppressWarnings("unchecked")
public <T> T getProxyHandler(RequestType type, Class<T> intf, T impl) {
final SCMHAInvocationHandler invocationHandler =
new SCMHAInvocationHandler(type, impl, this);
return (T) Proxy.newProxyInstance(getClass().getClassLoader(),
new Class<?>[] {intf}, invocationHandler);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.hadoop.hdds.scm.ha;

import java.io.IOException;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -70,5 +71,11 @@ SCMRatisResponse submitRequest(SCMRatisRequest request)

RaftPeerId getLeaderId();

<T> T getProxyHandler(RequestType type, Class<T> intf, T supplier);
default <T> T getProxyHandler(final RequestType type, final Class<T> intf, final T impl) {
final SCMHAInvocationHandler invocationHandler =
new SCMHAInvocationHandler(type, impl, this);
return intf.cast(Proxy.newProxyInstance(getClass().getClassLoader(),
new Class<?>[] {intf}, invocationHandler));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.google.common.base.Preconditions;
import jakarta.annotation.Nullable;
import java.io.IOException;
import java.lang.reflect.Proxy;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -431,13 +430,4 @@ public RaftPeer getLeader() {
}
}

@Override
@SuppressWarnings("unchecked")
public <T> T getProxyHandler(final RequestType type, final Class<T> intf, final T impl) {
final SCMHAInvocationHandler invocationHandler =
new SCMHAInvocationHandler(type, impl, this);
return (T) Proxy.newProxyInstance(getClass().getClassLoader(),
new Class<?>[] {intf}, invocationHandler);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

package org.apache.hadoop.hdds.scm.security;

import java.lang.reflect.Proxy;
import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol;
import org.apache.hadoop.hdds.scm.ha.SCMHAInvocationHandler;
import org.apache.hadoop.hdds.scm.ha.SCMRatisServer;
import org.apache.hadoop.hdds.security.symmetric.SecretKeyState;
import org.apache.hadoop.hdds.security.symmetric.SecretKeyStateImpl;
Expand Down Expand Up @@ -47,13 +45,7 @@ public ScmSecretKeyStateBuilder setRatisServer(

public SecretKeyState build() {
final SecretKeyState impl = new SecretKeyStateImpl(secretKeyStore);

final SCMHAInvocationHandler scmhaInvocationHandler =
new SCMHAInvocationHandler(SCMRatisProtocol.RequestType.SECRET_KEY,
impl, scmRatisServer);

return (SecretKeyState) Proxy.newProxyInstance(
SCMHAInvocationHandler.class.getClassLoader(),
new Class<?>[]{SecretKeyState.class}, scmhaInvocationHandler);
return scmRatisServer.getProxyHandler(SCMRatisProtocol.RequestType.SECRET_KEY,
SecretKeyState.class, impl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import com.google.common.base.Preconditions;
import java.io.IOException;
import java.lang.reflect.Proxy;
import java.math.BigInteger;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
Expand All @@ -31,7 +30,6 @@
import java.util.concurrent.locks.ReentrantLock;
import org.apache.hadoop.hdds.protocol.proto.HddsProtos.NodeType;
import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol;
import org.apache.hadoop.hdds.scm.ha.SCMHAInvocationHandler;
import org.apache.hadoop.hdds.scm.ha.SCMRatisServer;
import org.apache.hadoop.hdds.scm.metadata.SCMMetadataStore;
import org.apache.hadoop.hdds.security.exception.SCMSecurityException;
Expand Down Expand Up @@ -225,17 +223,9 @@ public Builder setRatisServer(final SCMRatisServer ratisServer) {
}

public CertificateStore build() {
final SCMCertStore scmCertStore = new SCMCertStore(metadataStore
);

final SCMHAInvocationHandler scmhaInvocationHandler =
new SCMHAInvocationHandler(SCMRatisProtocol.RequestType.CERT_STORE,
scmCertStore, scmRatisServer);

return (CertificateStore) Proxy.newProxyInstance(
SCMHAInvocationHandler.class.getClassLoader(),
new Class<?>[]{CertificateStore.class}, scmhaInvocationHandler);

final SCMCertStore scmCertStore = new SCMCertStore(metadataStore);
return scmRatisServer.getProxyHandler(SCMRatisProtocol.RequestType.CERT_STORE,
CertificateStore.class, scmCertStore);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import java.io.IOException;
import java.lang.reflect.Proxy;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol;
import org.apache.hadoop.hdds.scm.ha.SCMHAInvocationHandler;
import org.apache.hadoop.hdds.scm.ha.SCMRatisServer;
import org.apache.hadoop.hdds.scm.metadata.DBTransactionBuffer;
import org.apache.hadoop.hdds.scm.metadata.Replicate;
Expand Down Expand Up @@ -331,14 +329,9 @@ public FinalizationStateManager build() throws IOException {
Preconditions.checkNotNull(finalizationStore);
Preconditions.checkNotNull(transactionBuffer);
Preconditions.checkNotNull(upgradeFinalizer);
final SCMHAInvocationHandler invocationHandler =
new SCMHAInvocationHandler(SCMRatisProtocol.RequestType.FINALIZE,
new FinalizationStateManagerImpl(this),
scmRatisServer);

return (FinalizationStateManager) Proxy.newProxyInstance(
SCMHAInvocationHandler.class.getClassLoader(),
new Class<?>[]{FinalizationStateManager.class}, invocationHandler);

return scmRatisServer.getProxyHandler(SCMRatisProtocol.RequestType.FINALIZE,
FinalizationStateManager.class, new FinalizationStateManagerImpl(this));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
Expand All @@ -43,7 +42,6 @@
* Tests on {@link org.apache.hadoop.hdds.scm.metadata.Replicate}.
*/
public class TestReplicationAnnotation {
private SCMHAInvocationHandler scmhaInvocationHandler;
private SCMRatisServer scmRatisServer;

@BeforeEach
Expand Down Expand Up @@ -119,25 +117,15 @@ public RaftPeerId getLeaderId() {
return RaftPeerId.valueOf(UUID.randomUUID().toString());
}

@Override
public <T> T getProxyHandler(RequestType type, Class<T> intf, T impl) {
return impl;
}

};
}

@Test
public void testReplicateAnnotationBasic() throws Throwable {

scmhaInvocationHandler = new SCMHAInvocationHandler(
RequestType.CONTAINER, null, scmRatisServer);
ContainerStateManager proxy = scmRatisServer.getProxyHandler(RequestType.CONTAINER,
ContainerStateManager.class, null);

ContainerStateManager proxy =
(ContainerStateManager) Proxy.newProxyInstance(
SCMHAInvocationHandler.class.getClassLoader(),
new Class<?>[]{ContainerStateManager.class},
scmhaInvocationHandler);
IOException e =
assertThrows(IOException.class,
() -> proxy.addContainer(HddsProtos.ContainerInfoProto.getDefaultInstance()));
Expand Down

0 comments on commit f3c24ff

Please sign in to comment.