-
Notifications
You must be signed in to change notification settings - Fork 18
Implement cleaner to replace deprecated finalizers #845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
b423599
to
62c1c17
Compare
src/main/java/com/ibm/crypto/plus/provider/OpenJCEPlusProvider.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/crypto/plus/provider/OpenJCEPlusProvider.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/crypto/plus/provider/OpenJCEPlusProvider.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/crypto/plus/provider/OpenJCEPlusProvider.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/crypto/plus/provider/OpenJCEPlusProvider.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/crypto/plus/provider/OpenJCEPlusProvider.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/crypto/plus/provider/OpenJCEPlusProvider.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/crypto/plus/provider/OpenJCEPlusProvider.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/crypto/plus/provider/OpenJCEPlusProvider.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/crypto/plus/provider/OpenJCEPlusProvider.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/crypto/plus/provider/OpenJCEPlusProvider.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/crypto/plus/provider/OpenJCEPlusProvider.java
Outdated
Show resolved
Hide resolved
I believe your
Instead of
Notice the This might explain why the |
ae88020
to
1d79d60
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
a892894
to
90e8b73
Compare
src/main/java/com/ibm/crypto/plus/provider/OpenJCEPlusProvider.java
Outdated
Show resolved
Hide resolved
} | ||
} | ||
} catch (OCKException e) { | ||
e.printStackTrace(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit worried about printing stack traces on a mass scale if something goes wrong here but not sure we have any other options?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to limit how many times the stack trace prints with a counter inside the catch block? Since it could be more than a one time issue but if the cleaning is failing so frequently that it enters this catch block more than a certain number of times it will run into OOM so there should be a limit to how many times we think it needs to print.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems like a reasonable approach to me and better then any idea that i have at the moment. I assume we cannot throw an exception from this method at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can throw an exception just fine. The question is do you want to fail if it happens once?
} | ||
|
||
public void registerCleanable(Object owner, Runnable cleanAction) { | ||
Cleaner cleaner = cleaners[Math.abs(count.getAndIncrement()) % numCleaners]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do think we should validate that this works ok in and around the max integer of 2,147,483,648
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested out Math.floorMod()
as well as resetting the count variable at Integer.MAX_VALUE
and the latter seems like the better option because we can reset the value so that the next consecutive cleaner thread in the array is used, where the Math.floorMod()
that depends on the number of cleaner threads and we can't control it. In either case the default number of cleaners (2) is fine but for a different number of cleaners the distribution of cleaners would become imbalanced over time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK seems reasonable to me to just reset the counter once we reach Integer.MAX_VALUE
. I think the code will be more easy to understand also.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Math.floorMod()
is not useful in integer calculations anyway. The check and resetting can be a bit time consuming, especially in an AtomicInteger
. I don't see what the problem with using abs()
is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the problem is that abs()
of Integer.MAX_VALUE
+ 1 returns a negative value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
abs()
on Integer.MIN_VALUE
returns a negative value, and then the %
operator in Java works differently than in math, normally modulo should always return a positive number but in Java using %
on a negative operand returns a negative value.
2525ece
to
cf7c428
Compare
The cleaner is the replacement for the deprecated finalize() method for cleaning objects before garbage collection. Each class that was previously using finalize() will have an anonymous function that cleans up the necessary resources. The number of cleaner threads can be set by a property with the default value being 2. The object and its anonymous function are registered to one of the cleaner threads in round robin order. Signed-off-by: Sabrina Lee <[email protected]>
0ca9a8e
to
d9656b2
Compare
|
||
abstract void setOCKExceptionCause(Exception exception, Throwable ockException); | ||
|
||
private static class CleanerThreadFactory implements ThreadFactory { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're not gonna change anything in the thread, then the custom ThreadFactory
is not needed at all and can be removed.
The cleaner is the replacement for the deprecated finalize() method for cleaning objects before garbage collection.
Each class that was previously using finalize() will have an anonymous function that cleans up the necessary resources. The number of cleaner threads can be set by a property with the default value being 2. The object and its anonymous function are registered to one of the cleaner threads in round robin order.
Signed-off-by: Sabrina Lee [email protected]