Skip to content

Commit

Permalink
refactor test code
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Feb 24, 2025
1 parent 0601433 commit 0c31a67
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@

import javax.net.SocketFactory;

import org.apache.hadoop.security.AccessControlException;
import org.apache.hadoop.thirdparty.com.google.common.cache.Cache;
import org.apache.hadoop.thirdparty.com.google.common.cache.CacheBuilder;

import org.apache.commons.net.util.SubnetUtils;
import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
import org.apache.hadoop.classification.InterfaceAudience;
Expand All @@ -58,9 +54,13 @@
import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
import org.apache.hadoop.ipc.Server;
import org.apache.hadoop.ipc.VersionedProtocol;
import org.apache.hadoop.security.AccessControlException;
import org.apache.hadoop.security.SecurityUtil;
import org.apache.hadoop.thirdparty.com.google.common.cache.Cache;
import org.apache.hadoop.thirdparty.com.google.common.cache.CacheBuilder;
import org.apache.hadoop.util.ReflectionUtils;
import org.apache.hadoop.util.Preconditions;
import org.apache.hadoop.util.dynamic.DynConstructors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -1114,7 +1114,7 @@ public static Set<Integer> getFreeSocketPorts(int numOfPorts) {

/**
* Return an @{@link InetAddress} to bind to. If bindWildCardAddress is true
* than returns null.
* then returns null.
*
* @param localAddr local addr.
* @param bindWildCardAddress bind wildcard address.
Expand All @@ -1127,4 +1127,49 @@ public static InetAddress bindToLocalAddress(InetAddress localAddr, boolean
}
return null;
}

/**
* Return an @{@link IOException} of the same type as the input exception but with
* a modified exception message that includes the node name.
*
* @param ioe existing exception.
* @param nodeName name of the node.
* @return IOException
*/
public static IOException addNodeNameToIOException(final IOException ioe, final String nodeName) {
try {
final Throwable cause = ioe.getCause();
IOException newIoe = null;
if (cause != null) {
try {
DynConstructors.Ctor<? extends IOException> ctor =
new DynConstructors.Builder()
.impl(ioe.getClass(), String.class, Throwable.class)
.buildChecked();
newIoe = ctor.newInstance(nodeName + ": " + ioe.getMessage(), cause);
} catch (NoSuchMethodException e) {
// no matching constructor - try next approach below
}
}
if (newIoe == null) {
DynConstructors.Ctor<? extends IOException> ctor =
new DynConstructors.Builder()
.impl(ioe.getClass(), String.class)
.buildChecked();
newIoe = ctor.newInstance(nodeName + ": " + ioe.getMessage());
if (cause != null) {
try {
newIoe.initCause(cause);
} catch (Exception e) {
// Unable to initCause. Ignore the exception.
}
}
}
newIoe.setStackTrace(ioe.getStackTrace());
return newIoe;
} catch (Exception e) {
// Unable to create new exception. Return the original exception.
return ioe;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@
import org.apache.hadoop.util.Lists;
import org.apache.hadoop.util.Progressable;
import org.apache.hadoop.util.StringUtils;
import org.apache.hadoop.util.dynamic.DynConstructors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -845,38 +844,7 @@ private T runWithRetry() throws IOException {
if (node == null) {
node = url.getAuthority();
}
try {
final Throwable cause = ioe.getCause();
IOException newIoe = null;
if (cause != null) {
try {
DynConstructors.Ctor<? extends IOException> ctor =
new DynConstructors.Builder()
.impl(ioe.getClass(), String.class, Throwable.class)
.buildChecked();
newIoe = ctor.newInstance(node + ": " + ioe.getMessage(), cause);
} catch (NoSuchMethodException e) {
// no matching constructor - try next approach below
}
}
if (newIoe == null) {
DynConstructors.Ctor<? extends IOException> ctor =
new DynConstructors.Builder()
.impl(ioe.getClass(), String.class)
.buildChecked();
newIoe = ctor.newInstance(node + ": " + ioe.getMessage());
if (cause != null) {
try {
newIoe.initCause(cause);
} catch (Exception e) {
// Unable to initCause. Ignore the exception.
}
}
}
newIoe.setStackTrace(ioe.getStackTrace());
ioe = newIoe;
} catch (Exception e) {
}
ioe = NetUtils.addNodeNameToIOException(ioe, node);
shouldRetry(ioe, retry);
}
}
Expand Down

0 comments on commit 0c31a67

Please # to comment.