denis-chudov commented on code in PR #7482:
URL: https://github.com/apache/ignite-3/pull/7482#discussion_r2741028422


##########
modules/transactions/src/main/java/org/apache/ignite/internal/tx/impl/TxManagerImpl.java:
##########
@@ -587,7 +588,8 @@ private HybridTimestamp 
currentReadTimestamp(HybridTimestamp beginTx) {
             // Remote partition already has different consistency token, so we 
can't commit this transaction anyway.
             // Even when graceful primary replica switch is done, we can get 
here only if the write intent that requires resolution
             // is not under lock.
-            // TODO https://issues.apache.org/jira/browse/IGNITE-27386 the 
reason of rollback needs to be explained.
+            tx.recordAbortReason(new 
PrimaryReplicaExpiredException(senderGroupId, enlistment.consistencyToken(), 
null, null));

Review Comment:
   is there any chance that the transaction may be aborted in parallel due to 
another exception?



##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/storage/InternalTableImpl.java:
##########
@@ -757,6 +757,8 @@ private static <T> CompletableFuture<T> postEnlist(
             }
 
             if (e != null) {
+                tx0.recordAbortReason(e);

Review Comment:
   I think there is a better way to handle this.
   We have weird methods like 
'ReadWriteTransactionImpl#rollbackTimeoutExceededAsync` and similar which 
assume that transaction may be finished either by the user or due to timeout. 
Obviously, it maybe aborted due to server exception but there is no applicable 
method.
   
   I would suggest to add method like `rollbackWithException`, unify it with 
rollbackTimeoutExceeded, which would also end up with dedicated 
`TxManagerImpl#finish` method and finish consistent with exception that is set 
to the meta.
   
   Also it would remove `recordExceptionInfo` separated from `tx#finish`.



##########
modules/transactions/src/main/java/org/apache/ignite/internal/tx/TxStateMetaExceptionInfo.java:
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.internal.tx;
+
+import static org.apache.ignite.lang.ErrorGroups.Common.INTERNAL_ERR;
+
+import java.util.Objects;
+import java.util.UUID;
+import org.apache.ignite.internal.util.ExceptionUtils;
+import org.apache.ignite.lang.TraceableException;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Transaction abort exception information stored in {@link TxStateMeta}.
+ *
+ * <p>This information is stored for cases when a transaction is aborted 
exceptionally and the original response to the user
+ * is lost (for example, due to disconnections). The stored information can 
later be used to understand the abort reason.
+ */
+public class TxStateMetaExceptionInfo {
+    /** Exception class name. */
+    private final String exceptionClassName;
+
+    /** Full Ignite error code (group + code), see {@link 
TraceableException#code()}. */
+    private final int code;
+
+    /** Trace id of the exception. If exception is not traceable will be null. 
*/
+    private final @Nullable UUID traceId;
+
+    /** Exception message. */
+    private final @Nullable String message;
+
+    /** Original exception object. */
+    private final @Nullable Throwable throwable;
+
+    /**
+     * Constructor.
+     *
+     * @param exceptionClassName Exception class name.
+     * @param code Full error code (group + code).
+     * @param traceId Trace id.
+     * @param message Exception message.
+     */
+    public TxStateMetaExceptionInfo(String exceptionClassName, int code, 
@Nullable UUID traceId, @Nullable String message) {
+        this.exceptionClassName = exceptionClassName;
+        this.code = code;
+        this.traceId = traceId;
+        this.message = message;
+        this.throwable = null;
+    }
+
+    private TxStateMetaExceptionInfo(
+            String exceptionClassName,
+            int code,
+            @Nullable UUID traceId,
+            @Nullable String message,
+            @Nullable Throwable throwable
+    ) {
+        this.exceptionClassName = exceptionClassName;
+        this.code = code;
+        this.traceId = traceId;
+        this.message = message;
+        this.throwable = throwable;
+    }
+
+    /**
+     * Creates an instance from a throwable.
+     *
+     * <p>If the throwable implements {@link TraceableException}, its {@link 
TraceableException#code() code} and
+     * {@link TraceableException#traceId() traceId} are used. Otherwise {@link 
INTERNAL_ERR} and {@link #traceId()} produced from
+     * the throwable are used.
+     *
+     * @param throwable Throwable.
+     * @return Exception info.
+     */
+    public static TxStateMetaExceptionInfo fromThrowable(Throwable throwable) {
+        Throwable unwrapped = 
ExceptionUtils.unwrapRootCause(ExceptionUtils.unwrapCause(throwable));
+
+        if (unwrapped instanceof TraceableException) {
+            TraceableException traceable = (TraceableException) unwrapped;
+
+            return new TxStateMetaExceptionInfo(
+                    unwrapped.getClass().getName(),
+                    traceable.code(),
+                    traceable.traceId(),
+                    unwrapped.getMessage(),
+                    unwrapped
+            );
+        }
+
+        return new TxStateMetaExceptionInfo(
+                unwrapped.getClass().getName(),
+                INTERNAL_ERR,
+                null,
+                unwrapped.getMessage(),
+                unwrapped
+        );
+    }
+
+    public String exceptionClassName() {
+        return exceptionClassName;
+    }
+
+    public int code() {
+        return code;
+    }
+
+    public @Nullable UUID traceId() {
+        return traceId;
+    }
+
+    public @Nullable String message() {
+        return message;
+    }
+
+    public @Nullable Throwable throwable() {
+        return throwable;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        TxStateMetaExceptionInfo that = (TxStateMetaExceptionInfo) o;
+
+        return code == that.code
+                && exceptionClassName.equals(that.exceptionClassName)
+                && Objects.equals(traceId, that.traceId)
+                && Objects.equals(message, that.message);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(exceptionClassName, code, traceId, message);
+    }
+
+    @Override
+    public String toString() {
+        return "TxStateMetaExceptionInfo{"
+                + "exceptionClassName='" + exceptionClassName + '\''
+                + ", code=" + code
+                + ", traceId=" + traceId
+                + ", message='" + message + '\''
+                + '}';

Review Comment:
   ```suggestion
           return "TxStateMetaExceptionInfo["
                   + "exceptionClassName='" + exceptionClassName + '\''
                   + ", code=" + code
                   + ", traceId=" + traceId
                   + ", message='" + message + '\''
                   + ']';
   ```
   In Ignite we usually use `[ ]`



##########
modules/transactions/src/main/java/org/apache/ignite/internal/tx/TxStateMeta.java:
##########
@@ -191,6 +237,23 @@ public TxState txState() {
         return txLabel;
     }
 
+    public @Nullable TxStateMetaExceptionInfo exceptionInfo() {
+        return exceptionInfo;
+    }
+
+    /**
+     * Records exceptional information by mutating tx state or by creating a 
new one.
+     *
+     * @param old previous TxStateMeta.
+     * @param throwable to record
+     */
+    public static TxStateMeta recordExceptionInfo(TxStateMeta old, Throwable 
throwable) {
+        TxStateMetaExceptionInfo exceptionInfo = fromThrowable(throwable);
+        return old == null
+                ? builder(old, ABORTED).exceptionInfo(exceptionInfo).build()

Review Comment:
   Very dangerous, don't set tx state to ABORTED before actually calling 
`tx#finish`.



##########
modules/transactions/src/main/java/org/apache/ignite/internal/tx/impl/ReadWriteTransactionImpl.java:
##########
@@ -316,6 +317,10 @@ public boolean isRolledBackWithTimeoutExceeded() {
      * @param e Fail reason.
      */
     public void fail(TransactionException e) {
+        txManager.updateTxMeta(
+                id(), old -> recordExceptionInfo(old, e)
+        );

Review Comment:
   It's not needed here. See the usage: tx is not actually started, so the 
state is useless (and will never be vacuumized because #finish is not called)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to