Skip to content

Commit

Permalink
Format code and optimize imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonjkeller committed Jan 8, 2022
1 parent 16a8291 commit cfc8733
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 248 deletions.
2 changes: 2 additions & 0 deletions instrumentation/java.completable-future-jdk8/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# java.completable-future-jdk8

Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,46 @@
import util.TokenAwareRunnable;
import util.TokenDelegateExecutor;


@Weave(type = MatchType.ExactClass, originalName = "java.util.concurrent.CompletableFuture")
public class CompletableFuture_Instrumentation<T> {

@Weave(type = MatchType.BaseClass, originalName = "java.util.concurrent.CompletableFuture$Async")
abstract static class Async extends ForkJoinTask<Void>
implements Runnable, CompletableFuture.AsynchronousCompletionTask {
public final Void getRawResult() { return null; }
public final void setRawResult(Void v) { }
public final void run() { exec(); }
}

private static boolean noParallelism(Executor e) {
return (e == ForkJoinPool.commonPool() &&
ForkJoinPool.getCommonPoolParallelism() <= 1);
}

private static Executor useTokenDelegateExecutor(Executor e) {
if (null == e || e instanceof TokenDelegateExecutor) {
return e;
} else {
return new TokenDelegateExecutor(e);
@Weave(type = MatchType.BaseClass, originalName = "java.util.concurrent.CompletableFuture$Async")
abstract static class Async extends ForkJoinTask<Void>
implements Runnable, CompletableFuture.AsynchronousCompletionTask {
public final Void getRawResult() {
return null;
}

public final void setRawResult(Void v) {
}

public final void run() {
exec();
}
}

private static boolean noParallelism(Executor e) {
return (e == ForkJoinPool.commonPool() &&
ForkJoinPool.getCommonPoolParallelism() <= 1);
}

private static Executor useTokenDelegateExecutor(Executor e) {
if (null == e || e instanceof TokenDelegateExecutor) {
return e;
} else {
return new TokenDelegateExecutor(e);
}
}
}

static void execAsync(Executor e, CompletableFuture_Instrumentation.Async r) {
if (noParallelism(e))
new Thread(new TokenAwareRunnable(r)).start();
else {
Executor tde = useTokenDelegateExecutor(e);
if(null != tde) {
tde.execute(r);

static void execAsync(Executor e, CompletableFuture_Instrumentation.Async r) {
if (noParallelism(e)) {
new Thread(new TokenAwareRunnable(r)).start();
} else {
Executor tde = useTokenDelegateExecutor(e);
if (null != tde) {
tde.execute(r);
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,43 @@

public class TokenAndRefUtils {

public static AgentBridge.TokenAndRefCount getThreadTokenAndRefCount() {
AgentBridge.TokenAndRefCount tokenAndRefCount = AgentBridge.activeToken.get();
if (tokenAndRefCount == null) {
Transaction tx = AgentBridge.getAgent().getTransaction(false);
if (tx != null) {
tokenAndRefCount = new AgentBridge.TokenAndRefCount(tx.getToken(),
AgentBridge.getAgent().getTracedMethod(), new AtomicInteger(1));
}
} else {
tokenAndRefCount.refCount.incrementAndGet();
public static AgentBridge.TokenAndRefCount getThreadTokenAndRefCount() {
AgentBridge.TokenAndRefCount tokenAndRefCount = AgentBridge.activeToken.get();
if (tokenAndRefCount == null) {
Transaction tx = AgentBridge.getAgent().getTransaction(false);
if (tx != null) {
tokenAndRefCount = new AgentBridge.TokenAndRefCount(tx.getToken(),
AgentBridge.getAgent().getTracedMethod(), new AtomicInteger(1));
}
} else {
tokenAndRefCount.refCount.incrementAndGet();
}
return tokenAndRefCount;
}
return tokenAndRefCount;
}

public static void setThreadTokenAndRefCount(AgentBridge.TokenAndRefCount tokenAndRefCount) {
if (tokenAndRefCount != null) {
AgentBridge.activeToken.set(tokenAndRefCount);
tokenAndRefCount.token.link();
public static void setThreadTokenAndRefCount(AgentBridge.TokenAndRefCount tokenAndRefCount) {
if (tokenAndRefCount != null) {
AgentBridge.activeToken.set(tokenAndRefCount);
tokenAndRefCount.token.link();
}
}
}

public static void clearThreadTokenAndRefCountAndTxn(AgentBridge.TokenAndRefCount tokenAndRefCount) {
AgentBridge.activeToken.remove();
if (tokenAndRefCount != null && tokenAndRefCount.refCount.decrementAndGet() == 0) {
tokenAndRefCount.token.expire();
tokenAndRefCount.token = null;
public static void clearThreadTokenAndRefCountAndTxn(AgentBridge.TokenAndRefCount tokenAndRefCount) {
AgentBridge.activeToken.remove();
if (tokenAndRefCount != null && tokenAndRefCount.refCount.decrementAndGet() == 0) {
tokenAndRefCount.token.expire();
tokenAndRefCount.token = null;
}
}
}

public static void logTokenInfo(AgentBridge.TokenAndRefCount tokenAndRefCount, String msg) {
if (AgentBridge.getAgent().getLogger().isLoggable(Level.FINEST)) {
String tokenMsg = (tokenAndRefCount != null && tokenAndRefCount.token != null)
? String.format("[%s:%s:%d]", tokenAndRefCount.token, tokenAndRefCount.token.getTransaction(),
tokenAndRefCount.refCount.get())
: "[Empty token]";
AgentBridge.getAgent().getLogger().log(Level.FINEST, MessageFormat.format("{0}: token info {1}", tokenMsg, msg));

public static void logTokenInfo(AgentBridge.TokenAndRefCount tokenAndRefCount, String msg) {
if (AgentBridge.getAgent().getLogger().isLoggable(Level.FINEST)) {
String tokenMsg = (tokenAndRefCount != null && tokenAndRefCount.token != null)
? String.format("[%s:%s:%d]", tokenAndRefCount.token, tokenAndRefCount.token.getTransaction(),
tokenAndRefCount.refCount.get())
: "[Empty token]";
AgentBridge.getAgent().getLogger().log(Level.FINEST, MessageFormat.format("{0}: token info {1}", tokenMsg, msg));
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,34 @@

import com.newrelic.agent.bridge.AgentBridge;

import static util.TokenAndRefUtils.*;

import static util.TokenAndRefUtils.clearThreadTokenAndRefCountAndTxn;
import static util.TokenAndRefUtils.getThreadTokenAndRefCount;
import static util.TokenAndRefUtils.logTokenInfo;
import static util.TokenAndRefUtils.setThreadTokenAndRefCount;

public final class TokenAwareRunnable implements Runnable {
private final Runnable delegate;
private final Runnable delegate;

private AgentBridge.TokenAndRefCount tokenAndRefCount;
private AgentBridge.TokenAndRefCount tokenAndRefCount;

public TokenAwareRunnable(Runnable delegate) {
this.delegate = delegate;
//get token state from calling Thread
this.tokenAndRefCount = getThreadTokenAndRefCount();
logTokenInfo(tokenAndRefCount, "TokenAwareRunnable token info set");
}
public TokenAwareRunnable(Runnable delegate) {
this.delegate = delegate;
//get token state from calling Thread
this.tokenAndRefCount = getThreadTokenAndRefCount();
logTokenInfo(tokenAndRefCount, "TokenAwareRunnable token info set");
}

@Override
public void run() {
try {
if (delegate != null) {
logTokenInfo(tokenAndRefCount, "Token info set in thread");
setThreadTokenAndRefCount(tokenAndRefCount);
delegate.run();
}
} finally {
logTokenInfo(tokenAndRefCount, "Clearing token info from thread ");
clearThreadTokenAndRefCountAndTxn(tokenAndRefCount);
@Override
public void run() {
try {
if (delegate != null) {
logTokenInfo(tokenAndRefCount, "Token info set in thread");
setThreadTokenAndRefCount(tokenAndRefCount);
delegate.run();
}
} finally {
logTokenInfo(tokenAndRefCount, "Clearing token info from thread ");
clearThreadTokenAndRefCountAndTxn(tokenAndRefCount);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import java.util.concurrent.Executor;

public class TokenDelegateExecutor implements Executor {
public final Executor delegate;
public final Executor delegate;

public TokenDelegateExecutor(final Executor delegate) {
this.delegate = delegate;
}
public TokenDelegateExecutor(final Executor delegate) {
this.delegate = delegate;
}

@Override
public void execute(Runnable runnable) {
runnable = new TokenAwareRunnable(runnable);
delegate.execute(runnable);
}
@Override
public void execute(Runnable runnable) {
runnable = new TokenAwareRunnable(runnable);
delegate.execute(runnable);
}
}
2 changes: 2 additions & 0 deletions instrumentation/java.completable-future-jdk8u40/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# java.completable-future-jdk8u40

Loading

0 comments on commit cfc8733

Please sign in to comment.