-
Notifications
You must be signed in to change notification settings - Fork 8.9k
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
HADOOP-17126. implement non-guava Precondition checkNotNull #3050
Conversation
🎊 +1 overall
This message was automatically generated. |
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.
Left few comments. Nice one @amahussein, this one is heavily used in the code base.
@InterfaceAudience.Private | ||
@InterfaceStability.Unstable | ||
public final class Validate { | ||
public static final Logger LOG = |
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.
nit: private
would be better
public static final Logger LOG = | ||
LoggerFactory.getLogger(Validate.class); | ||
|
||
public static final String VALIDATE_IS_NOT_NULL_EX_MESSAGE = |
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.
nit: we can keep default package private scope.
String msg = VALIDATE_IS_NOT_NULL_EX_MESSAGE; | ||
try { | ||
// note that we can get NPE evaluating the message itself; | ||
// but we do not want this to override the actual NPE. | ||
msg = msgSupplier.get(); | ||
} catch (Exception e) { | ||
// ideally we want to log the error to capture. This may cause log files | ||
// to bloat. On the other hand, swallowing the exception may hide a bug | ||
// in the caller. Debug level is a good compromise between the two | ||
// concerns. | ||
LOG.debug("Error formatting message: {}", e.getMessage()); | ||
} |
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.
nit: This one is fine, but how about single assignment? (only if you feel this one is good to use)
String msg;
try {
// note that we can get NPE evaluating the message itself;
// but we do not want this to override the actual NPE.
msg = msgSupplier.get();
} catch (Exception e) {
msg = VALIDATE_IS_NOT_NULL_EX_MESSAGE;
// ideally we want to log the error to capture. This may cause log files
// to bloat. On the other hand, swallowing the exception may hide a bug
// in the caller. Debug level is a good compromise between the two
// concerns.
LOG.debug("Error formatting message: {}", e.getMessage());
}
* <p>This class replaces {@code guava.Preconditions} which provides helpers | ||
* to validate the following conditions: |
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.
With this, good to mention: Javadocs for majority of APIs in this class are taken from which Guava release version.
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 did not copy the code from guava; but I see your point.
It is going to be helpful to have that added to some APIs. I will review the APIs and see which ones need this info added to their Javadoc.
try { | ||
msg = String.format(message, values); | ||
} catch (Exception e) { | ||
LOG.debug("Error formatting message: {}", e.getMessage()); |
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.
As it is already at DEBUG level, do we also want to print stacktrace by providing e
as last argument? Perhaps for some hidden case, it might provide good visibility at DEBUG level.
*/ | ||
@InterfaceAudience.Private | ||
@InterfaceStability.Unstable | ||
public final class Validate { |
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 we keep the same name i.e Preconditions
, only import will require changes in follow-up PRs and it would look clean.
Given that Preconditions
is heavily used in the code base, from change as well as review perspectives, it will be much cleaner. WDYT?
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.
Sounds like a good idea. I will rename the class.
🎊 +1 overall
This message was automatically generated. |
🎊 +1 overall
This message was automatically generated. |
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.
+1 (non-binding)
@busbey Can you please take a look at this PR? |
@steveloughran This PR was one of the steps to replace the guava dependency in common module. |
|
|
||
import org.apache.hadoop.classification.InterfaceAudience; | ||
import org.apache.hadoop.classification.InterfaceStability; | ||
import org.apache.hadoop.thirdparty.com.google.common.annotations.VisibleForTesting; |
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.
Do we need this? It makes backporting this patch on its own harder?
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.
This reminds me of the decision HBase community made sometime back to remove usage of VisibleForTesting entirely from the codebase and replaced them with IA.Private. Here is the commit
now, for the rest of the hadoop-common JIRA, should we create a new JIRA for that and mark the current one as closed (with versions)? I think it'd be best for tracking like that |
NOTICE
In order to replace Guava Preconditions, we need to implement our own versions of the API.
This PR is to create
checkNotNull
in a new package dubbedunguava
.This is splitting the PR that was opened previously #2134
The plan is as follows
package org.apache.hadoop.util.unguava
;class Validate
org.apache.hadoop.util.unguava.Validate
with the following interfacecheckNotNull(final T obj)
checkNotNull(final T reference, final Object errorMessage)
checkNotNull(final T obj, final String message, final Object... values)
checkNotNull(final T obj,final Supplier<String> msgSupplier)
guava.preconditions
usedString.lenientformat
which suppressed exceptions caused by string formatting of the exception message . So, in order to avoid changing the behavior, the implementation catches exceptions triggered by building the message (IllegalFormat, InsufficientArg, NullPointer..etc)guava.Preconditions.checkNotNull
byunguava.Validate.checkNotNull
Similar Jiras will be created to implement
checkState
,checkArgument
,checkIndex