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

xyuanlu pushed a commit to branch metaclient
in repository https://gitbox.apache.org/repos/asf/helix.git


The following commit(s) were added to refs/heads/metaclient by this push:
     new 573b1c9de Add metaclient code and code translator from ZkException code
573b1c9de is described below

commit 573b1c9dea650fa5bd07837414724201d58a519b
Author: xyuanlu <[email protected]>
AuthorDate: Thu Feb 2 16:10:37 2023 -0800

    Add metaclient code and code translator from ZkException code
---
 .../metaclient/exception/MetaClientException.java  | 99 ++++++++++++++++++++++
 .../metaclient/impl/zk/util/ZkMetaClientUtil.java  | 85 +++++++++++++++++++
 2 files changed, 184 insertions(+)

diff --git 
a/meta-client/src/main/java/org/apache/helix/metaclient/exception/MetaClientException.java
 
b/meta-client/src/main/java/org/apache/helix/metaclient/exception/MetaClientException.java
index 6620799ca..3854fadaf 100644
--- 
a/meta-client/src/main/java/org/apache/helix/metaclient/exception/MetaClientException.java
+++ 
b/meta-client/src/main/java/org/apache/helix/metaclient/exception/MetaClientException.java
@@ -36,4 +36,103 @@ public class MetaClientException extends RuntimeException {
     super(cause);
   }
 
+  public enum ReturnCode {
+    /** Connection to the server has been lost. */
+    CONNECTION_LOSS(-105, "Connection to the server has been lost.") ,
+
+    /** Operation is unimplemented. */
+    UNIMPLEMENTED(-104, "Operation is unimplemented."),
+
+    /** Operation timeout. */
+    OPERATION_TIMEOUT(-103, "Operation timeout.") {
+      @Override
+      public MetaClientException createMetaClientException() {
+        return new MetaClientTimeoutException();
+      }
+    },
+
+    /** Either a runtime or data inconsistency was found. */
+    CONSISTENCY_ERROR(-102, "Inconsistency was found."),
+
+    /** Session is moved or expired or non-exist. */
+    SESSION_ERROR(-101, "Session is moved or expired or non-exist."),
+
+    /** Indicates a system and server-side errors not defined by following 
codes.
+     * It also indicate a range. Any value smaller or equal than this 
indicating error from
+     * server side.
+     */
+    DB_SYSTEM_ERROR(-100, "System and server-side errors."),
+
+    /** The listener does not exists. */
+    INVALID_LISTENER(-9, "Listener does not exists."),
+
+    /** Authentication failed. */
+    AUTH_FAILED(-8, "authentication failed"),
+
+    /** Invalid arguments. */
+    INVALID_ARGUMENTS(-7, "Invalid arguments"),
+
+    /** Version conflict. Return when caller tries to edit an entry with a 
specific version but
+     * the actual version of the entry on server is different. */
+    BAD_VERSION(-6, "Version conflict.") {
+      @Override
+      public MetaClientException createMetaClientException() {
+        return new MetaClientBadVersionException();
+      }
+    },
+
+    /** Entry already exists. Return when try to create a duplicated entry. */
+    ENTRY_EXISTS(-5, "Entry already exists."),
+
+    /** The client is not Authenticated. */
+    NO_AUTH(-4, "Not Authenticated.") ,
+
+    /** Entry does not exist. */
+    NO_SUCH_ENTRY(-3, "Entry does not exist.") {
+      @Override
+      public MetaClientException createMetaClientException() {
+        return new MetaClientNoNodeException();
+      }
+    },
+
+    /**The entry has sub entries. Return when operation can only be down at 
entry with no
+     * sub entries. (i.e. unrecursively delete an entry . )*/
+    NOT_LEAF_ENTRY(-2, "The entry has sub entries."),
+
+    /** Indicates a system and DB client or a usage errors not defined by 
following codes.
+     * It also indicate a range. Any value smaller or equal than this and 
larger than
+     * DB_SYSTEM_ERROR indicating error from client or caused by wrong usage.
+     */
+    DB_USER_ERROR(-1, "Client or usage error."),
+
+    /** Everything is OK. */
+    OK(0, "OK") {
+      @Override
+      public MetaClientException createMetaClientException() {
+        return null;
+      }
+    };
+
+    private final int _intValue;
+    private final String _message;
+
+    ReturnCode(int codeIntValue, String message) {
+      _intValue = codeIntValue;
+      _message = message;
+    }
+
+    public String getMessage() {
+      return _message;
+    }
+
+    public int getIntValue() {
+      return _intValue;
+    }
+
+    public MetaClientException createMetaClientException() {
+      // TODO: add more code translation when new exception class is created.
+      return new MetaClientException();
+    }
+  }
+
 }
diff --git 
a/meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java
 
b/meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java
index dc92e706b..cd625f169 100644
--- 
a/meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java
+++ 
b/meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java
@@ -238,4 +238,89 @@ public class ZkMetaClientUtil {
     }
     return new MetaClientException(e);
   }
+
+  /**
+   * This function translate and group Zk exception code to metaclient code.
+   * It currently includes all ZK code on 3.6.3.
+   */
+  public static MetaClientException.ReturnCode 
translateZooKeeperCodeToMetaClientCode(
+      KeeperException.Code zkCode) {
+    // TODO: add log to track ZK origional code.
+    switch (zkCode) {
+      case AUTHFAILED:
+      case SESSIONCLOSEDREQUIRESASLAUTH:
+      case INVALIDACL:
+        return MetaClientException.ReturnCode.AUTH_FAILED;
+
+      case CONNECTIONLOSS:
+        return MetaClientException.ReturnCode.CONNECTION_LOSS;
+
+      case BADARGUMENTS:
+        return MetaClientException.ReturnCode.INVALID_ARGUMENTS;
+
+      case BADVERSION:
+        return MetaClientException.ReturnCode.BAD_VERSION;
+
+      case NOAUTH:
+        return MetaClientException.ReturnCode.NO_AUTH;
+
+      case NOWATCHER:
+        return MetaClientException.ReturnCode.INVALID_LISTENER;
+
+      case NOTEMPTY:
+        return MetaClientException.ReturnCode.NOT_LEAF_ENTRY;
+
+      case NODEEXISTS:
+        return MetaClientException.ReturnCode.ENTRY_EXISTS;
+
+      case SESSIONEXPIRED:
+      case SESSIONMOVED:
+      case UNKNOWNSESSION:
+        return MetaClientException.ReturnCode.SESSION_ERROR;
+
+      case NONODE:
+        return MetaClientException.ReturnCode.NO_SUCH_ENTRY;
+
+      case OPERATIONTIMEOUT:
+        return MetaClientException.ReturnCode.OPERATION_TIMEOUT;
+
+      case OK:
+        return MetaClientException.ReturnCode.OK;
+
+      case UNIMPLEMENTED:
+        return MetaClientException.ReturnCode.UNIMPLEMENTED;
+
+      case RUNTIMEINCONSISTENCY:
+      case DATAINCONSISTENCY:
+        return MetaClientException.ReturnCode.CONSISTENCY_ERROR;
+
+      case SYSTEMERROR:
+      case MARSHALLINGERROR:
+      case NEWCONFIGNOQUORUM:
+      case RECONFIGINPROGRESS:
+        return MetaClientException.ReturnCode.DB_SYSTEM_ERROR;
+
+      case NOCHILDRENFOREPHEMERALS:
+      case INVALIDCALLBACK:
+      case NOTREADONLY:
+      case EPHEMERALONLOCALSESSION:
+      case RECONFIGDISABLED:
+        return MetaClientException.ReturnCode.DB_USER_ERROR;
+
+      /*
+       * APIERROR is ZooKeeper Code value separator. It is never thrown by ZK 
server,
+       * ZK error codes greater than its value are user or client errors and 
values less than
+       * this indicate a ZK server.
+       * Note: there are some mismatch between ZK doc and Zk code intValue 
define. We are comparing
+       * ordinal instead of using intValue().
+       *   
https://zookeeper.apache.org/doc/r3.6.2/apidocs/zookeeper-server/index.html?org/apache/zookeeper/KeeperException.Code.html
+       */
+      default:
+        if (zkCode.ordinal() < KeeperException.Code.APIERROR.ordinal()
+            && zkCode.ordinal() >= KeeperException.Code.SYSTEMERROR.ordinal()) 
{
+          return MetaClientException.ReturnCode.DB_SYSTEM_ERROR;
+        }
+        return MetaClientException.ReturnCode.DB_USER_ERROR;
+    }
+  }
 }

Reply via email to