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

szetszwo 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 3b0d42bca09 HDDS-15192. Remove SCMHAInvocationHandler and the related 
code (#10474)
3b0d42bca09 is described below

commit 3b0d42bca09818e66192fe9c5d3a3faf48cac1dc
Author: Bolin Lin <[email protected]>
AuthorDate: Wed Jun 10 04:17:14 2026 -0400

    HDDS-15192. Remove SCMHAInvocationHandler and the related code (#10474)
---
 .../certificate/authority/CertificateStore.java    |   4 -
 .../hadoop/hdds/scm/ha/SCMHAInvocationHandler.java | 164 ---------------------
 .../hadoop/hdds/scm/ha/SCMHAManagerStub.java       |  20 +--
 .../apache/hadoop/hdds/scm/ha/SCMRatisServer.java  |  13 +-
 .../hadoop/hdds/scm/ha/SCMRatisServerImpl.java     |  10 +-
 .../apache/hadoop/hdds/scm/ha/SCMStateMachine.java |  33 +----
 .../hadoop/hdds/scm/ha/invoker/ScmInvoker.java     |  31 +++-
 .../hdds/scm/ha/TestReplicationAnnotation.java     |   9 +-
 8 files changed, 45 insertions(+), 239 deletions(-)

diff --git 
a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/security/x509/certificate/authority/CertificateStore.java
 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/security/x509/certificate/authority/CertificateStore.java
index d1dd3c25125..cfc5af5e655 100644
--- 
a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/security/x509/certificate/authority/CertificateStore.java
+++ 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/security/x509/certificate/authority/CertificateStore.java
@@ -41,10 +41,6 @@ public interface CertificateStore extends SCMHandler {
   /**
    * Writes a new certificate that was issued to the persistent store.
    *
-   * Note: Don't rename this method, as it is used in
-   * SCMHAInvocationHandler#invokeRatis. If for any case renaming this
-   * method name is required, change it over there.
-   *
    * @param serialID - Certificate Serial Number.
    * @param certificate - Certificate to persist.
    * @param role - OM/DN/SCM.
diff --git 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMHAInvocationHandler.java
 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMHAInvocationHandler.java
deleted file mode 100644
index aa35fbf6fbf..00000000000
--- 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMHAInvocationHandler.java
+++ /dev/null
@@ -1,164 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.hadoop.hdds.scm.ha;
-
-import java.io.IOException;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeoutException;
-import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.RequestType;
-import org.apache.hadoop.hdds.scm.exceptions.SCMException;
-import org.apache.hadoop.hdds.scm.exceptions.SCMException.ResultCodes;
-import org.apache.hadoop.hdds.scm.metadata.Replicate;
-import org.apache.hadoop.util.Time;
-import org.apache.ratis.protocol.exceptions.NotLeaderException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * InvocationHandler which checks for {@link Replicate} annotation and
- * dispatches the request to Ratis Server.
- */
-public class SCMHAInvocationHandler implements InvocationHandler {
-
-  private static final Logger LOG = LoggerFactory
-      .getLogger(SCMHAInvocationHandler.class);
-
-  private final RequestType requestType;
-  private final Object localHandler;
-  private final SCMRatisServer ratisHandler;
-
-  public SCMHAInvocationHandler(final RequestType requestType,
-      final Object localHandler,
-      final SCMRatisServer ratisHandler) {
-    this.requestType = requestType;
-    this.localHandler = localHandler;
-    this.ratisHandler = ratisHandler;
-    if (ratisHandler != null) {
-      ratisHandler.registerStateMachineHandler(requestType, localHandler);
-    }
-  }
-
-  @Override
-  public Object invoke(final Object proxy, final Method method,
-                       final Object[] args) throws SCMException {
-    // Javadoc for InvocationHandler#invoke specifies that args will be null
-    // if the method takes no arguments. Convert this to an empty array for
-    // easier handling.
-    Object[] convertedArgs = (args == null) ? new Object[]{} : args;
-    long startTime = Time.monotonicNow();
-    final Object result =
-        ratisHandler != null && method.isAnnotationPresent(Replicate.class) ?
-            invokeRatis(method, convertedArgs) :
-            invokeLocal(method, convertedArgs);
-    if (LOG.isDebugEnabled()) {
-      LOG.debug("Call: {} took {} ms", method, Time.monotonicNow() - 
startTime);
-    }
-    return result;
-  }
-
-  /**
-   * TODO.
-   */
-  private Object invokeLocal(Method method, Object[] args)
-      throws SCMException {
-    if (LOG.isTraceEnabled()) {
-      LOG.trace("Invoking method {} on target {} with arguments {}",
-          method, localHandler, args);
-    }
-    try {
-      return method.invoke(localHandler, args);
-    } catch (Exception e) {
-      throw translateException(e);
-    }
-  }
-
-  /**
-   * TODO.
-   */
-  private Object invokeRatis(Method method, Object[] args)
-      throws SCMException {
-    if (LOG.isTraceEnabled()) {
-      LOG.trace("Invoking method {} on target {}", method, ratisHandler);
-    }
-
-    try {
-      switch (method.getAnnotation(Replicate.class).invocationType()) {
-      case CLIENT:
-        return invokeRatisClient(method, args);
-      case DIRECT:
-      default:
-        return invokeRatisServer(method, args);
-      }
-    } catch (Exception e) {
-      throw translateException(e);
-    }
-  }
-
-  private Object invokeRatisServer(Method method, Object[] args)
-      throws Exception {
-    SCMRatisRequest scmRatisRequest = SCMRatisRequest.of(requestType,
-        method.getName(), method.getParameterTypes(), args);
-    final SCMRatisResponse response = ratisHandler.submitRequest(
-        scmRatisRequest);
-    if (response.isSuccess()) {
-      return response.getResult();
-    }
-    throw response.getException();
-  }
-
-  private Object invokeRatisClient(Method method, Object[] args)
-      throws Exception {
-    final SCMRatisRequest scmRatisRequest = SCMRatisRequest.of(requestType,
-        method.getName(), method.getParameterTypes(), args);
-    final SCMRatisResponse response = HASecurityUtils.submitScmRequestToRatis(
-        ratisHandler.getDivision().getGroup(),
-        ratisHandler.getGrpcTlsConfig(),
-        scmRatisRequest.encode());
-    if (response.isSuccess()) {
-      return response.getResult();
-    }
-    throw response.getException();
-  }
-
-  public static SCMException translateException(Throwable t) {
-    if (t instanceof SCMException) {
-      return (SCMException) t;
-    }
-    if (t instanceof ExecutionException
-        || t instanceof InvocationTargetException) {
-      return translateException(t.getCause());
-    }
-
-    ResultCodes result;
-    if (t instanceof TimeoutException) {
-      result = ResultCodes.TIMEOUT;
-    } else if (t instanceof NotLeaderException) {
-      result = ResultCodes.SCM_NOT_LEADER;
-    } else if (t instanceof IOException) {
-      result = ResultCodes.IO_EXCEPTION;
-    } else {
-      result = ResultCodes.INTERNAL_ERROR;
-    }
-
-    return new SCMException(t, result);
-  }
-
-}
diff --git 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMHAManagerStub.java
 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMHAManagerStub.java
index a4e25420564..0106f6d46fa 100644
--- 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMHAManagerStub.java
+++ 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMHAManagerStub.java
@@ -17,6 +17,8 @@
 
 package org.apache.hadoop.hdds.scm.ha;
 
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.base.Preconditions;
 import java.io.IOException;
 import java.util.ArrayList;
@@ -167,9 +169,6 @@ public TermIndex installCheckpoint(DBCheckpoint 
dbCheckpoint) {
 
   private class RatisServerStub implements SCMRatisServer {
 
-    private Map<RequestType, Object> handlers =
-        new EnumMap<>(RequestType.class);
-
     private Map<RequestType, ScmInvoker<?>> invokers =
         new EnumMap<>(RequestType.class);
 
@@ -180,13 +179,8 @@ public void start() {
     }
 
     @Override
-    public void registerStateMachineHandler(final RequestType handlerType,
-        final Object handler) {
-      if (handler instanceof ScmInvoker) {
-        invokers.put(handlerType, (ScmInvoker<?>) handler);
-      } else {
-        handlers.put(handlerType, handler);
-      }
+    public void registerStateMachineHandler(final ScmInvoker<?> handler) {
+      invokers.put(handler.getType(), handler);
     }
 
     @Override
@@ -225,10 +219,8 @@ public boolean triggerSnapshot() throws IOException {
 
     private Message process(final SCMRatisRequest request) throws Exception {
       final ScmInvoker<?> invoker = invokers.get(request.getType());
-      if (invoker != null) {
-        return invoker.invokeLocal(request.getOperation(), 
request.getArguments());
-      }
-      return SCMStateMachine.process(request, handlers.get(request.getType()));
+      requireNonNull(invoker, "invoker == null");
+      return invoker.invokeLocal(request.getOperation(), 
request.getArguments());
     }
 
     @Override
diff --git 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisServer.java
 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisServer.java
index 43d87915493..216d276568e 100644
--- 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisServer.java
+++ 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisServer.java
@@ -18,11 +18,9 @@
 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;
-import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.RequestType;
 import org.apache.hadoop.hdds.scm.AddSCMRequest;
 import org.apache.hadoop.hdds.scm.RemoveSCMRequest;
 import org.apache.hadoop.hdds.scm.ha.invoker.ScmInvoker;
@@ -38,7 +36,7 @@ public interface SCMRatisServer {
 
   void start() throws IOException;
 
-  void registerStateMachineHandler(RequestType handlerType, Object handler);
+  void registerStateMachineHandler(ScmInvoker<?> handler);
 
   SCMRatisResponse submitRequest(SCMRatisRequest request)
       throws IOException, ExecutionException, InterruptedException,
@@ -73,15 +71,8 @@ SCMRatisResponse submitRequest(SCMRatisRequest request)
   RaftPeerId getLeaderId();
 
   default <T extends SCMHandler> T getProxyHandler(ScmInvoker<T> invoker) {
-    registerStateMachineHandler(invoker.getType(), invoker);
+    registerStateMachineHandler(invoker);
     return invoker.getProxy();
   }
 
-  default <T extends SCMHandler> T getProxyHandler(Class<T> intf, T impl) {
-    final SCMHAInvocationHandler invocationHandler =
-        new SCMHAInvocationHandler(impl.getType(), impl, this);
-    return intf.cast(Proxy.newProxyInstance(getClass().getClassLoader(),
-        new Class<?>[] {intf}, invocationHandler));
-  }
-
 }
diff --git 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisServerImpl.java
 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisServerImpl.java
index 49a258b95de..81ebf1c2cc7 100644
--- 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisServerImpl.java
+++ 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisServerImpl.java
@@ -36,7 +36,6 @@
 import org.apache.hadoop.hdds.HddsUtils;
 import org.apache.hadoop.hdds.conf.ConfigurationSource;
 import org.apache.hadoop.hdds.conf.OzoneConfiguration;
-import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.RequestType;
 import org.apache.hadoop.hdds.ratis.RatisHelper;
 import org.apache.hadoop.hdds.scm.AddSCMRequest;
 import org.apache.hadoop.hdds.scm.RemoveSCMRequest;
@@ -222,13 +221,8 @@ public SCMStateMachine getSCMStateMachine() {
   }
 
   @Override
-  public void registerStateMachineHandler(final RequestType handlerType,
-                                          final Object handler) {
-    if (handler instanceof ScmInvoker) {
-      stateMachine.registerInvoker(handlerType, (ScmInvoker) handler);
-    } else {
-      stateMachine.registerHandler(handlerType, handler);
-    }
+  public void registerStateMachineHandler(final ScmInvoker<?> handler) {
+    stateMachine.registerInvoker(handler.getType(), handler);
   }
 
   @Override
diff --git 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMStateMachine.java
 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMStateMachine.java
index a8a7d6068f0..267ddc6c5b5 100644
--- 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMStateMachine.java
+++ 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMStateMachine.java
@@ -22,10 +22,7 @@
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.ThreadFactoryBuilder;
-import com.google.protobuf.InvalidProtocolBufferException;
 import java.io.IOException;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
 import java.util.Collection;
 import java.util.EnumMap;
 import java.util.List;
@@ -78,7 +75,6 @@ public class SCMStateMachine extends BaseStateMachine {
       LoggerFactory.getLogger(SCMStateMachine.class);
 
   private StorageContainerManager scm;
-  private Map<RequestType, Object> handlers;
   private Map<RequestType, ScmInvoker<?>> invokers;
   private SCMHADBTransactionBuffer transactionBuffer;
   private final SCMMetrics metrics;
@@ -96,7 +92,6 @@ public class SCMStateMachine extends BaseStateMachine {
   public SCMStateMachine(final StorageContainerManager scm,
       SCMHADBTransactionBuffer buffer) {
     this.scm = scm;
-    this.handlers = new EnumMap<>(RequestType.class);
     this.invokers = new EnumMap<>(RequestType.class);
     this.transactionBuffer = buffer;
     this.metrics = scm.getMetrics();
@@ -120,10 +115,6 @@ public SCMStateMachine() {
     this.metrics = null;
   }
 
-  public void registerHandler(RequestType type, Object handler) {
-    handlers.put(type, handler);
-  }
-
   private void addRatisEvent(String message) {
     if (metrics != null) {
       metrics.addRatisEvent(message);
@@ -201,28 +192,8 @@ public CompletableFuture<Message> applyTransaction(
 
   private Message process(final SCMRatisRequest request) throws Exception {
     final ScmInvoker<?> invoker = invokers.get(request.getType());
-    if (invoker != null) {
-      return invoker.invokeLocal(request.getOperation(), 
request.getArguments());
-    }
-    return process(request, handlers.get(request.getType()));
-  }
-
-  public static Message process(final SCMRatisRequest request, Object handler) 
throws Exception {
-    try {
-      if (handler == null) {
-        throw new IOException("No handler found for request type " +
-            request.getType());
-      }
-
-      final Method method = 
handler.getClass().getMethod(request.getOperation(), 
request.getParameterTypes());
-      final Object result = method.invoke(handler, request.getArguments());
-      return SCMRatisResponse.encode(result, method.getReturnType());
-    } catch (NoSuchMethodException | SecurityException ex) {
-      throw new InvalidProtocolBufferException(ex.getMessage());
-    } catch (InvocationTargetException e) {
-      final Exception targetEx = (Exception) e.getTargetException();
-      throw targetEx != null ? targetEx : e;
-    }
+    requireNonNull(invoker, "invoker == null");
+    return invoker.invokeLocal(request.getOperation(), request.getArguments());
   }
 
   @Override
diff --git 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/invoker/ScmInvoker.java
 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/invoker/ScmInvoker.java
index cfd19aeb159..e20098a5a83 100644
--- 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/invoker/ScmInvoker.java
+++ 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/invoker/ScmInvoker.java
@@ -17,17 +17,21 @@
 
 package org.apache.hadoop.hdds.scm.ha.invoker;
 
-import static 
org.apache.hadoop.hdds.scm.ha.SCMHAInvocationHandler.translateException;
-
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeoutException;
 import java.util.function.Function;
 import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.RequestType;
 import org.apache.hadoop.hdds.scm.exceptions.SCMException;
+import org.apache.hadoop.hdds.scm.exceptions.SCMException.ResultCodes;
 import org.apache.hadoop.hdds.scm.ha.HASecurityUtils;
 import org.apache.hadoop.hdds.scm.ha.SCMHandler;
 import org.apache.hadoop.hdds.scm.ha.SCMRatisRequest;
 import org.apache.hadoop.hdds.scm.ha.SCMRatisResponse;
 import org.apache.hadoop.hdds.scm.ha.SCMRatisServer;
 import org.apache.ratis.protocol.Message;
+import org.apache.ratis.protocol.exceptions.NotLeaderException;
 
 /**
  * Invokes methods without using reflection.
@@ -93,9 +97,30 @@ final Object invokeReplicateClient(NameAndParameterTypes 
method, Object[] args)
     }
   }
 
+  static SCMException translateException(Throwable t) {
+    if (t instanceof SCMException) {
+      return (SCMException) t;
+    }
+    if (t instanceof ExecutionException || t instanceof 
InvocationTargetException) {
+      return translateException(t.getCause());
+    }
+
+    final ResultCodes result;
+    if (t instanceof TimeoutException) {
+      result = ResultCodes.TIMEOUT;
+    } else if (t instanceof NotLeaderException) {
+      result = ResultCodes.SCM_NOT_LEADER;
+    } else if (t instanceof IOException) {
+      result = ResultCodes.IO_EXCEPTION;
+    } else {
+      result = ResultCodes.INTERNAL_ERROR;
+    }
+    return new SCMException(t, result);
+  }
+
   interface NameAndParameterTypes {
     String name();
-    
+
     Class<?>[] getParameterTypes(int numArgs);
   }
 }
diff --git 
a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestReplicationAnnotation.java
 
b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestReplicationAnnotation.java
index b2f2c30c41e..be06ee66247 100644
--- 
a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestReplicationAnnotation.java
+++ 
b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestReplicationAnnotation.java
@@ -28,11 +28,12 @@
 import java.util.UUID;
 import java.util.concurrent.ExecutionException;
 import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
-import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol;
 import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.RequestType;
 import org.apache.hadoop.hdds.scm.AddSCMRequest;
 import org.apache.hadoop.hdds.scm.RemoveSCMRequest;
 import org.apache.hadoop.hdds.scm.container.ContainerStateManager;
+import org.apache.hadoop.hdds.scm.ha.invoker.ContainerStateManagerInvoker;
+import org.apache.hadoop.hdds.scm.ha.invoker.ScmInvoker;
 import org.apache.ratis.grpc.GrpcTlsConfig;
 import org.apache.ratis.protocol.RaftPeerId;
 import org.apache.ratis.protocol.exceptions.NotLeaderException;
@@ -54,8 +55,7 @@ public void start() throws IOException {
       }
 
       @Override
-      public void registerStateMachineHandler(
-          SCMRatisProtocol.RequestType handlerType, Object handler) {
+      public void registerStateMachineHandler(ScmInvoker<?> handler) {
       }
 
       @Override
@@ -127,7 +127,8 @@ public void testReplicateAnnotationBasic() throws Throwable 
{
     ContainerStateManager impl = mock(ContainerStateManager.class);
     when(impl.getType()).thenReturn(RequestType.CONTAINER);
 
-    ContainerStateManager proxy = 
scmRatisServer.getProxyHandler(ContainerStateManager.class, impl);
+    ContainerStateManager proxy = scmRatisServer.getProxyHandler(
+        new ContainerStateManagerInvoker(impl, scmRatisServer));
 
     IOException e =
         assertThrows(IOException.class,


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

Reply via email to