Skip to content

Commit

Permalink
feat: propagate exception if it's fatal
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Dec 14, 2022
1 parent 8590e25 commit 8eb51d4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.logging.Logger;

import static com.alibaba.ttl3.internal.util.Utils.newHashMap;
import static com.alibaba.ttl3.internal.util.Utils.propagateIfFatal;

/**
* {@link CompositeCrrTransmit} transmit all {@link CrrTransmit}
Expand Down Expand Up @@ -48,6 +49,7 @@ public Capture capture() {
try {
crrTransmit2Value.put(crrTransmit, crrTransmit.capture());
} catch (Throwable t) {
propagateIfFatal(t);
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "exception when capture for crrTransmit " + crrTransmit +
"(class " + crrTransmit.getClass().getName() + "), just ignored; cause: " + t, t);
Expand Down Expand Up @@ -77,6 +79,7 @@ public Backup replay(@NonNull Capture captured) {
Object transmitCaptured = entry.getValue();
crrTransmit2Value.put(crrTransmit, crrTransmit.replay(transmitCaptured));
} catch (Throwable t) {
propagateIfFatal(t);
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "exception when replay for crrTransmit " + crrTransmit +
"(class " + crrTransmit.getClass().getName() + "), just ignored; cause: " + t, t);
Expand Down Expand Up @@ -112,6 +115,7 @@ public Backup clear() {
try {
crrTransmit2Value.put(crrTransmit, crrTransmit.clear());
} catch (Throwable t) {
propagateIfFatal(t);
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "exception when clear for crrTransmit " + crrTransmit +
"(class " + crrTransmit.getClass().getName() + "), just ignored; cause: " + t, t);
Expand Down Expand Up @@ -140,6 +144,7 @@ public void restore(@NonNull Backup backup) {
Object transmitBackup = entry.getValue();
crrTransmit.restore(transmitBackup);
} catch (Throwable t) {
propagateIfFatal(t);
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "exception when restore for crrTransmit " + crrTransmit +
"(class " + crrTransmit.getClass().getName() + "), just ignored; cause: " + t, t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.alibaba.ttl3.internal.util.Utils.propagateIfFatal;

/**
* Composite CrrTransmitCallback.
*
Expand All @@ -26,6 +28,7 @@ Object beforeReplay() {
try {
cb.beforeReplay();
} catch (Throwable t) {
propagateIfFatal(t);
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "exception when beforeReplay for crrTransmitCallback " + cb +
"(class " + cb.getClass().getName() + "), just ignored; cause: " + t, t);
Expand All @@ -42,6 +45,7 @@ Object afterReplay(Object data) {
try {
cb.afterReplay();
} catch (Throwable t) {
propagateIfFatal(t);
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "exception when afterReplay for crrTransmitCallback " + cb +
"(class " + cb.getClass().getName() + "), just ignored; cause: " + t, t);
Expand All @@ -58,6 +62,7 @@ Object beforeRestore(Object data) {
try {
cb.beforeRestore();
} catch (Throwable t) {
propagateIfFatal(t);
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "exception when beforeRestore for crrTransmitCallback " + cb +
"(class " + cb.getClass().getName() + "), just ignored; cause: " + t, t);
Expand All @@ -74,6 +79,7 @@ void afterRestore(Object data) {
try {
cb.afterRestore();
} catch (Throwable t) {
propagateIfFatal(t);
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "exception when afterRestore for crrTransmitCallback " + cb +
"(class " + cb.getClass().getName() + "), just ignored; cause: " + t, t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import edu.umd.cs.findbugs.annotations.NonNull;

import static com.alibaba.ttl3.internal.util.Utils.propagateIfFatal;

final class EmptyTtlAgentStatus implements TtlAgentStatus {
@Override
public boolean isTtlAgentLoaded() {
Expand Down Expand Up @@ -34,6 +36,7 @@ static TtlAgentStatus getLoadedAgentOrEmpty() {
} catch (ClassNotFoundException e) {
ret = new EmptyTtlAgentStatus();
} catch (Exception e) {
propagateIfFatal(e);
throw new IllegalStateException("Fail to create the TTL agent instance(" + TTL_AGENT_CLASS
+ "), should be a bug! report to the TTL project. cause: " + e, e);
}
Expand Down
21 changes: 21 additions & 0 deletions ttl-core/src/main/java/com/alibaba/ttl3/internal/util/Utils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.alibaba.ttl3.internal.util;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -58,4 +59,24 @@ public static <K, V> ConcurrentMap<K, V> newConcurrentWeakHashMap(int expectedSi
private static int computeMapInitialCapacity(int expectedSize) {
return (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR);
}

/**
* @see <a href="https://scala-lang.org/api/3.1.3/scala/util/control/NonFatal$.html"><code>scala.util.control</code></a>
* @see <a href="https://github.com/scala/scala/blob/v2.13.9/src/library/scala/util/control/NonFatal.scala#L35-L43">github.com/scala/scala/blob/v2.13.9/src/library/scala/util/control/NonFatal.scala</a>
*/
public static void propagateIfFatal(@Nullable Throwable throwable) {
if (throwable == null) return;

if (throwable instanceof VirtualMachineError
|| throwable instanceof ThreadDeath
|| throwable instanceof InterruptedException
|| throwable instanceof LinkageError) {
sneakyThrow(throwable);
}
}

@SuppressWarnings("unchecked")
private static <E extends Throwable> void sneakyThrow(Throwable e) throws E {
throw (E) e;
}
}

0 comments on commit 8eb51d4

Please sign in to comment.