You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by sk...@apache.org on 2023/07/05 15:20:57 UTC

[ignite-3] branch main updated: IGNITE-19864 Introduce TraceableException (#2287)

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

sk0x50 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ignite-3.git


The following commit(s) were added to refs/heads/main by this push:
     new 6145deb7d9 IGNITE-19864 Introduce TraceableException (#2287)
6145deb7d9 is described below

commit 6145deb7d99ebde954e5d2b2f46609a9c521c209
Author: Slava Koptilin <sl...@gmail.com>
AuthorDate: Wed Jul 5 18:20:51 2023 +0300

    IGNITE-19864 Introduce TraceableException (#2287)
---
 .../apache/ignite/lang/IgniteCheckedException.java |  6 ++-
 .../org/apache/ignite/lang/IgniteException.java    |  6 ++-
 .../apache/ignite/lang/IgniteExceptionUtils.java   | 25 ++--------
 .../handler/ClientInboundMessageHandler.java       | 10 ++--
 .../lang/IgniteInternalCheckedException.java       |  6 ++-
 .../ignite/lang/IgniteInternalException.java       |  6 ++-
 .../org/apache/ignite/lang/TraceableException.java | 57 ++++++++++++++++++++++
 7 files changed, 85 insertions(+), 31 deletions(-)

diff --git a/modules/api/src/main/java/org/apache/ignite/lang/IgniteCheckedException.java b/modules/api/src/main/java/org/apache/ignite/lang/IgniteCheckedException.java
index c8a3b94277..9c736cb4fb 100755
--- a/modules/api/src/main/java/org/apache/ignite/lang/IgniteCheckedException.java
+++ b/modules/api/src/main/java/org/apache/ignite/lang/IgniteCheckedException.java
@@ -28,7 +28,7 @@ import java.util.UUID;
 /**
  * General Ignite exception. Used to indicate any error condition within a node.
  */
-public class IgniteCheckedException extends Exception {
+public class IgniteCheckedException extends Exception implements TraceableException {
     /** Serial version UID. */
     private static final long serialVersionUID = 0L;
 
@@ -165,6 +165,7 @@ public class IgniteCheckedException extends Exception {
      *
      * @return Full error code.
      */
+    @Override
     public int code() {
         return code;
     }
@@ -185,6 +186,7 @@ public class IgniteCheckedException extends Exception {
      * @see #code()
      * @return Error group.
      */
+    @Override
     public int groupCode() {
         return extractGroupCode(code);
     }
@@ -196,6 +198,7 @@ public class IgniteCheckedException extends Exception {
      * @see #groupCode()
      * @return Error code.
      */
+    @Override
     public int errorCode() {
         return extractErrorCode(code);
     }
@@ -205,6 +208,7 @@ public class IgniteCheckedException extends Exception {
      *
      * @return Unique identifier of the exception.
      */
+    @Override
     public UUID traceId() {
         return traceId;
     }
diff --git a/modules/api/src/main/java/org/apache/ignite/lang/IgniteException.java b/modules/api/src/main/java/org/apache/ignite/lang/IgniteException.java
index 130f2afc87..64a06ac5c9 100644
--- a/modules/api/src/main/java/org/apache/ignite/lang/IgniteException.java
+++ b/modules/api/src/main/java/org/apache/ignite/lang/IgniteException.java
@@ -30,7 +30,7 @@ import org.jetbrains.annotations.Nullable;
 /**
  * General Ignite exception. Used to indicate any error condition within a node.
  */
-public class IgniteException extends RuntimeException {
+public class IgniteException extends RuntimeException implements TraceableException {
     /** Serial version UID. */
     private static final long serialVersionUID = 0L;
 
@@ -205,6 +205,7 @@ public class IgniteException extends RuntimeException {
      *
      * @return Full error code.
      */
+    @Override
     public int code() {
         return code;
     }
@@ -225,6 +226,7 @@ public class IgniteException extends RuntimeException {
      * @see #code()
      * @return Error group.
      */
+    @Override
     public int groupCode() {
         return extractGroupCode(code);
     }
@@ -236,6 +238,7 @@ public class IgniteException extends RuntimeException {
      * @see #groupCode()
      * @return Error code.
      */
+    @Override
     public int errorCode() {
         return extractErrorCode(code);
     }
@@ -245,6 +248,7 @@ public class IgniteException extends RuntimeException {
      *
      * @return Unique identifier of the exception.
      */
+    @Override
     public UUID traceId() {
         return traceId;
     }
diff --git a/modules/api/src/main/java/org/apache/ignite/lang/IgniteExceptionUtils.java b/modules/api/src/main/java/org/apache/ignite/lang/IgniteExceptionUtils.java
index 7b447bd70c..b80700356d 100755
--- a/modules/api/src/main/java/org/apache/ignite/lang/IgniteExceptionUtils.java
+++ b/modules/api/src/main/java/org/apache/ignite/lang/IgniteExceptionUtils.java
@@ -142,15 +142,8 @@ public class IgniteExceptionUtils {
      * @return Trace identifier.
      */
     public static @Nullable UUID extractTraceIdFrom(Throwable t) {
-        // Perhaps, it would be nice to introduce a new interface IgniteTraceableException to overcome if else statements.
-        if (t instanceof IgniteException) {
-            return ((IgniteException) t).traceId();
-        } else if (t instanceof IgniteCheckedException) {
-            return ((IgniteCheckedException) t).traceId();
-        } else if (t instanceof IgniteInternalException) {
-            return ((IgniteInternalException) t).traceId();
-        } else if (t instanceof IgniteInternalCheckedException) {
-            return ((IgniteInternalCheckedException) t).traceId();
+        if (t instanceof TraceableException) {
+            return ((TraceableException) t).traceId();
         }
 
         return null;
@@ -163,15 +156,8 @@ public class IgniteExceptionUtils {
      * @return Trace identifier.
      */
     public static int extractCodeFrom(Throwable t) {
-        // Perhaps, it would be nice to introduce a new interface IgniteTraceableException to overcome if else statements.
-        if (t instanceof IgniteException) {
-            return ((IgniteException) t).code();
-        } else if (t instanceof IgniteCheckedException) {
-            return ((IgniteCheckedException) t).code();
-        } else if (t instanceof IgniteInternalException) {
-            return ((IgniteInternalException) t).code();
-        } else if (t instanceof IgniteInternalCheckedException) {
-            return ((IgniteInternalCheckedException) t).code();
+        if (t instanceof TraceableException) {
+            return ((TraceableException) t).code();
         }
 
         return INTERNAL_ERR;
@@ -298,8 +284,7 @@ public class IgniteExceptionUtils {
                 T exc = copy(constructor, traceId, code, message, cause);
 
                 // Make sure that error code and trace id are the same.
-                if (exc instanceof IgniteException || exc instanceof IgniteCheckedException || exc instanceof IgniteInternalException
-                        || exc instanceof IgniteInternalCheckedException) {
+                if (exc instanceof TraceableException) {
                     // Error code should be the same
                     assert code == extractCodeFrom(exc) :
                             "Unexpected error code [originCode=" + code + ", code=" + extractCodeFrom(exc) + ", err=" + exc + ']';
diff --git a/modules/client-handler/src/main/java/org/apache/ignite/client/handler/ClientInboundMessageHandler.java b/modules/client-handler/src/main/java/org/apache/ignite/client/handler/ClientInboundMessageHandler.java
index 392a3f707f..bb4790ee49 100644
--- a/modules/client-handler/src/main/java/org/apache/ignite/client/handler/ClientInboundMessageHandler.java
+++ b/modules/client-handler/src/main/java/org/apache/ignite/client/handler/ClientInboundMessageHandler.java
@@ -103,9 +103,9 @@ import org.apache.ignite.internal.security.authentication.UsernamePasswordReques
 import org.apache.ignite.internal.sql.engine.QueryProcessor;
 import org.apache.ignite.internal.table.IgniteTablesInternal;
 import org.apache.ignite.internal.util.ExceptionUtils;
-import org.apache.ignite.lang.IgniteCheckedException;
 import org.apache.ignite.lang.IgniteException;
 import org.apache.ignite.lang.IgniteInternalCheckedException;
+import org.apache.ignite.lang.TraceableException;
 import org.apache.ignite.network.ClusterNode;
 import org.apache.ignite.network.ClusterService;
 import org.apache.ignite.security.AuthenticationException;
@@ -395,12 +395,8 @@ public class ClientInboundMessageHandler extends ChannelInboundHandlerAdapter im
     private void writeErrorCore(Throwable err, ClientMessagePacker packer) {
         err = ExceptionUtils.unwrapCause(err);
 
-        if (err instanceof IgniteException) {
-            IgniteException iex = (IgniteException) err;
-            packer.packUuid(iex.traceId());
-            packer.packInt(iex.code());
-        } else if (err instanceof IgniteCheckedException) {
-            IgniteCheckedException iex = (IgniteCheckedException) err;
+        if (err instanceof TraceableException) {
+            TraceableException iex = (TraceableException) err;
             packer.packUuid(iex.traceId());
             packer.packInt(iex.code());
         } else {
diff --git a/modules/core/src/main/java/org/apache/ignite/lang/IgniteInternalCheckedException.java b/modules/core/src/main/java/org/apache/ignite/lang/IgniteInternalCheckedException.java
index 8778031b80..a96286ef63 100644
--- a/modules/core/src/main/java/org/apache/ignite/lang/IgniteInternalCheckedException.java
+++ b/modules/core/src/main/java/org/apache/ignite/lang/IgniteInternalCheckedException.java
@@ -30,7 +30,7 @@ import org.jetbrains.annotations.Nullable;
 /**
  * General internal checked exception. This exception is used to indicate any error condition within the node.
  */
-public class IgniteInternalCheckedException extends Exception {
+public class IgniteInternalCheckedException extends Exception implements TraceableException {
     /** Serial version uid. */
     private static final long serialVersionUID = 0L;
 
@@ -241,6 +241,7 @@ public class IgniteInternalCheckedException extends Exception {
      *
      * @return Full error code.
      */
+    @Override
     public int code() {
         return code;
     }
@@ -261,6 +262,7 @@ public class IgniteInternalCheckedException extends Exception {
      * @see #code()
      * @return Error group.
      */
+    @Override
     public int groupCode() {
         return extractGroupCode(code);
     }
@@ -272,6 +274,7 @@ public class IgniteInternalCheckedException extends Exception {
      * @see #groupCode()
      * @return Error code.
      */
+    @Override
     public int errorCode() {
         return extractErrorCode(code);
     }
@@ -281,6 +284,7 @@ public class IgniteInternalCheckedException extends Exception {
      *
      * @return Unique identifier of this exception.
      */
+    @Override
     public UUID traceId() {
         return traceId;
     }
diff --git a/modules/core/src/main/java/org/apache/ignite/lang/IgniteInternalException.java b/modules/core/src/main/java/org/apache/ignite/lang/IgniteInternalException.java
index 33d108c19a..af904e9380 100644
--- a/modules/core/src/main/java/org/apache/ignite/lang/IgniteInternalException.java
+++ b/modules/core/src/main/java/org/apache/ignite/lang/IgniteInternalException.java
@@ -30,7 +30,7 @@ import org.jetbrains.annotations.Nullable;
 /**
  * General internal exception. This exception is used to indicate any error condition within the node.
  */
-public class IgniteInternalException extends RuntimeException {
+public class IgniteInternalException extends RuntimeException implements TraceableException {
     /** Serial version uid. */
     private static final long serialVersionUID = 0L;
 
@@ -218,6 +218,7 @@ public class IgniteInternalException extends RuntimeException {
      *
      * @return Full error code.
      */
+    @Override
     public int code() {
         return code;
     }
@@ -238,6 +239,7 @@ public class IgniteInternalException extends RuntimeException {
      * @see #code()
      * @return Error group.
      */
+    @Override
     public int groupCode() {
         return extractGroupCode(code);
     }
@@ -249,6 +251,7 @@ public class IgniteInternalException extends RuntimeException {
      * @see #groupCode()
      * @return Error code.
      */
+    @Override
     public int errorCode() {
         return extractErrorCode(code);
     }
@@ -258,6 +261,7 @@ public class IgniteInternalException extends RuntimeException {
      *
      * @return Unique identifier of this exception.
      */
+    @Override
     public UUID traceId() {
         return traceId;
     }
diff --git a/modules/core/src/main/java/org/apache/ignite/lang/TraceableException.java b/modules/core/src/main/java/org/apache/ignite/lang/TraceableException.java
new file mode 100755
index 0000000000..1dee962d51
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/lang/TraceableException.java
@@ -0,0 +1,57 @@
+/*
+ * 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.ignite.lang;
+
+import java.util.UUID;
+
+/**
+ * Interface representing an exception that can be identified with trace identifier and provides an error code.
+ */
+public interface TraceableException {
+    /**
+     * Returns a unique identifier of the exception.
+     *
+     * @return Unique identifier of the exception.
+     */
+    UUID traceId();
+
+    /**
+     * Returns a full error code that includes the error's group and code, which uniquely identifies the problem within the group. This is a
+     * combination of two most-significant bytes for the error group and two least-significant bytes for the error code.
+     *
+     * @return Full error code.
+     */
+    public int code();
+
+    /**
+     * Returns an error group.
+     *
+     * @see #code()
+     * @return Error group.
+     */
+    public int groupCode();
+
+    /**
+     * Returns an error code that uniquely identifies the problem within a group.
+     *
+     * @see #code()
+     * @see #groupCode()
+     * @return Error code.
+     */
+    public int errorCode();
+}