You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2021/06/23 17:47:13 UTC

[GitHub] [beam] zhoufek commented on a change in pull request #15037: [BEAM-12474] Write PubsubIO parsing errors to dead-letter topic

zhoufek commented on a change in pull request #15037:
URL: https://github.com/apache/beam/pull/15037#discussion_r657330871



##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/util/ComparableThrowable.java
##########
@@ -0,0 +1,112 @@
+/*
+ * 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.beam.sdk.util;
+
+import java.io.Serializable;
+import java.util.Arrays;
+import javax.annotation.Nullable;
+
+/**
+ * A wrapper around a {@link Throwable} for use with coders.
+ *
+ * <p>The {@link Throwable} class is serializable. However, it, along with man child classes like
+ * {@link Exception}, does not override {@link Object#equals(Object)}. As a result, t1.equals(t2)
+ * will return false if t2 is a deserialized instance of serialized t1. This makes it appear as if
+ * coders are mutating the value, which can lead to things like log spam. This wrapper overrides
+ * {@link Object#equals(Object)} to get around this issue. The hash code remains the same as the
+ * underlying {@link Throwable}.
+ *
+ * <p>The equality comparison is transitive, meaning that for two {@link Throwable}s t1 and t2, the
+ * causes must be equal. This can occur either because they are both null or because they are both
+ * equal after being wrapped.
+ *
+ * <p>Note that this is simply a best effort based on properties like instance type, stack trace,
+ * message, and cause. It cannot guarantee that the state that led to the exception is the same,
+ * unless it is fully captured in the message, nor can it differentiate between two exceptions
+ * thrown at different times or in different processes. For this reason, this is not suitable for a
+ * general-purpose {@link Throwable} equality comparison. It merely exists to try to avoid false
+ * positives in a mutation check for coders.
+ *
+ * <p>Due to the above, this does not support comparison to a raw {@link Throwable}.
+ */
+public class ComparableThrowable implements Serializable {

Review comment:
       Willing to accept suggestions on a different name. The best I could come up with is ComparableThrowable, but that might be confusing due to not implementing the Comparable interface. I guess implementing the interface is itself an option, but I don't think there's much value in that.




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org