Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 72 additions & 16 deletions src/main/java/software/amazon/awssdk/crt/CrtResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,15 @@ public CrtResource() {
* @param resource The resource to add a reference to
*/
public void addReferenceTo(CrtResource resource) {
if (debugNativeObjects) {
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
String.format("%s(%d) is adding a reference to %s(%d)",
this.getClass().getCanonicalName(), id, resource.getClass().getCanonicalName(), resource.id));
}
resource.addRef();
synchronized(this) {
referencedResources.add(resource);
}

if (debugNativeObjects) {
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource, String.format("Instance of class %s(%d) is adding a reference to instance of class %s(%d)", this.getClass().getCanonicalName(), id, resource.getClass().getCanonicalName(), resource.id));
}
}

/**
Expand All @@ -146,9 +147,13 @@ public void removeReferenceTo(CrtResource resource) {

if (debugNativeObjects) {
if (removed) {
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource, String.format("Instance of class %s(%d) is removing a reference to instance of class %s(%d)", this.getClass().getCanonicalName(), id, resource.getClass().getCanonicalName(), resource.id));
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
String.format("%s(%d) is removing a reference to %s(%d)",
this.getClass().getCanonicalName(), id, resource.getClass().getCanonicalName(), resource.id));
} else {
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource, String.format("Instance of class %s(%d) erroneously tried to remove a reference to instance of class %s(%d) that it was not referencing", this.getClass().getCanonicalName(), id, resource.getClass().getCanonicalName(), resource.id));
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
String.format("%s(%d) erroneously tried to remove a reference to %s(%d) that it was not referencing",
this.getClass().getCanonicalName(), id, resource.getClass().getCanonicalName(), resource.id));
}
}

Expand Down Expand Up @@ -209,7 +214,8 @@ protected void acquireNativeHandle(long handle) {
*/
private void release() {
if (debugNativeObjects) {
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource, String.format("Releasing class %s(%d)", this.getClass().getCanonicalName(), id));
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
String.format("Releasing class %s(%d)", this.getClass().getCanonicalName(), id));

synchronized(CrtResource.class) {
CRT_RESOURCES.remove(id);
Expand Down Expand Up @@ -237,7 +243,25 @@ public long getNativeHandle() {
* Increments the reference count to this resource.
*/
public void addRef() {
refCount.incrementAndGet();
int updatedRefs = refCount.incrementAndGet();
if (debugNativeObjects) {
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
String.format("%s(%d) is adding a reference. refCount is now %d",
this.getClass().getCanonicalName(), id, updatedRefs));
}
}

/**
* Increments the reference count to this resource with a description.
* @param desc Descrption string of why the reference is being incremented.
*/
public void addRef(String desc) {
int updatedRefs = refCount.incrementAndGet();
if (debugNativeObjects) {
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
String.format("%s(%d) is adding a reference for: (%s). RefCount is now %d",
this.getClass().getCanonicalName(), id, desc, updatedRefs));
}
}

/**
Expand Down Expand Up @@ -274,7 +298,11 @@ public boolean isNull() {

@Override
public void close() {
decRef();
decRef("close() called");
}

public void close(String desc) {
decRef(desc);
}

/**
Expand All @@ -286,12 +314,37 @@ public void decRef(CrtResource decRefInstigator) {
int remainingRefs = refCount.decrementAndGet();

if (debugNativeObjects) {
if (decRefInstigator != null) {
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource, String.format(
"DecRef instance of class %s(%d) called by %s(%d). %d remaining refs", this.getClass().getCanonicalName(), id,
decRefInstigator.getClass().getCanonicalName(), decRefInstigator.id, remainingRefs));
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource, String.format(
"DecRef of %s(%d) called by %s(%d). %d remaining refs", this.getClass().getCanonicalName(), id,
decRefInstigator.getClass().getCanonicalName(), decRefInstigator.id, remainingRefs));
}

if (remainingRefs != 0) {
return;
}

release();

if (canReleaseReferencesImmediately()) {
releaseReferences();
}
}

/**
* Decrements the reference count to this resource with a description.
* @param desc Descrption string of why the reference is being decremented.
*/
public void decRef(String desc) {
int remainingRefs = refCount.decrementAndGet();

if (debugNativeObjects) {
if (desc != null) {
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
String.format("DecRef on %s(%d) for: (%s). %d remaining refs",
this.getClass().getCanonicalName(), id, desc, remainingRefs));
} else {
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource, String.format("DecRef instance of class %s(%d) via self.close(). %d remaining refs",
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
String.format("Defref on %s(%d). %d remaining refs",
this.getClass().getCanonicalName(), id, remainingRefs));
}
}
Expand All @@ -306,11 +359,12 @@ public void decRef(CrtResource decRefInstigator) {
releaseReferences();
}
}

/**
* Decrements the reference count to this resource.
*/
public void decRef() {
decRef(null);
decRef((String)null);
}

/**
Expand All @@ -320,7 +374,9 @@ public void decRef() {
*/
protected void releaseReferences() {
if (debugNativeObjects) {
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource, String.format("Instance of class %s(%d) closing all referenced objects", this.getClass().getCanonicalName(), id));
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
String.format("%s(%d) closing all referenced objects",
this.getClass().getCanonicalName(), id));
}

synchronized(this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public CompletableFuture<Void> getContinuationClosedFuture() {
@Override
public void close() {
if (continuation != null) {
continuation.decRef();
continuation.decRef("close() called");
continuation = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void close() {
if (clientConnection.isOpen()) {
clientConnection.closeConnection(0);
}
clientConnection.decRef();
clientConnection.decRef("close() called");
clientConnection = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public CompletableFuture<Void> getContinuationClosedFuture() {
@Override
public void close() {
if (continuation != null) {
continuation.decRef();
continuation.decRef("close() called");
continuation = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void close() {
if (connection.isConnectionOpen()) {
connection.closeConnection(0);
}
connection.decRef();
connection.decRef("close() called");
connection = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private void onShutdownComplete() {
public static void closeStaticDefault() {
synchronized (ClientBootstrap.class) {
if (staticDefaultClientBootstrap != null) {
staticDefaultClientBootstrap.close();
staticDefaultClientBootstrap.close("closeStaticDefault");
}
staticDefaultClientBootstrap = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static void setStaticDefaultNumThreads(int numThreads) {
public static void closeStaticDefault() {
synchronized (EventLoopGroup.class) {
if (staticDefaultEventLoopGroup != null) {
staticDefaultEventLoopGroup.close();
staticDefaultEventLoopGroup.close("closeStaticDefault");
}
staticDefaultEventLoopGroup = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static void setStaticDefaultMaxEntries(int maxEntries) {
public static void closeStaticDefault() {
synchronized (HostResolver.class) {
if (staticDefaultResolver != null) {
staticDefaultResolver.close();
staticDefaultResolver.close("closeStaticDefault");
}
staticDefaultResolver = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@
import org.junit.BeforeClass;
import org.junit.After;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.rules.TestName;


import java.util.Optional;

public class CrtTestFixture {

private CrtTestContext context;

@Rule
public TestName testName = new TestName();

public final CrtTestContext getContext() {
return context;
}
Expand Down Expand Up @@ -230,6 +236,7 @@ public static void setupOnce() {
/* The setup function will be run before every test */
@Before
public void setup() {
System.out.println("[TEST START] " + testName.getMethodName());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/awslabs/aws-crt-java/actions/runs/19476392289/job/55739151489?pr=938#step:4:2041
Do we really want all of those print out in the log? Is there an example about this helping debugging?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are times when tests fail but there is no record of which test from a class of tests was the one that failed. I've seen it most when tests time out and logs need to be truncated.

Log.log(Log.LogLevel.Debug, LogSubject.JavaCrtGeneral, "CrtTestFixture setup begin");

// TODO this CrtTestContext should be removed as we are using System Properties
Expand Down Expand Up @@ -258,6 +265,7 @@ public void tearDown() {
}
}
Log.log(Log.LogLevel.Debug, LogSubject.JavaCrtGeneral, "CrtTestFixture tearDown end");
System.out.println("[TEST END] " + testName.getMethodName());
}

protected TlsContext createTlsContextOptions(byte[] trustStore) {
Expand Down
Loading