21
21
import java .io .IOException ;
22
22
import java .io .InputStream ;
23
23
import java .io .OutputStream ;
24
- import java .lang .reflect .Constructor ;
25
24
import java .net .BindException ;
26
25
import java .net .InetAddress ;
27
26
import java .net .InetSocketAddress ;
46
45
47
46
import javax .net .SocketFactory ;
48
47
49
- import org .apache .hadoop .security .AccessControlException ;
50
- import org .apache .hadoop .thirdparty .com .google .common .cache .Cache ;
51
- import org .apache .hadoop .thirdparty .com .google .common .cache .CacheBuilder ;
52
-
53
48
import org .apache .commons .net .util .SubnetUtils ;
54
49
import org .apache .commons .net .util .SubnetUtils .SubnetInfo ;
55
50
import org .apache .hadoop .classification .InterfaceAudience ;
58
53
import org .apache .hadoop .fs .CommonConfigurationKeysPublic ;
59
54
import org .apache .hadoop .ipc .Server ;
60
55
import org .apache .hadoop .ipc .VersionedProtocol ;
56
+ import org .apache .hadoop .security .AccessControlException ;
61
57
import org .apache .hadoop .security .SecurityUtil ;
58
+ import org .apache .hadoop .thirdparty .com .google .common .cache .Cache ;
59
+ import org .apache .hadoop .thirdparty .com .google .common .cache .CacheBuilder ;
62
60
import org .apache .hadoop .util .ReflectionUtils ;
63
61
import org .apache .hadoop .util .Preconditions ;
62
+ import org .apache .hadoop .util .dynamic .DynConstructors ;
64
63
65
64
import org .slf4j .Logger ;
66
65
import org .slf4j .LoggerFactory ;
@@ -941,10 +940,59 @@ public static IOException wrapException(final String destHost,
941
940
942
941
}
943
942
} catch (IOException ex ) {
944
- return (IOException ) new IOException ("Failed on local exception: "
945
- + exception + "; Host Details : "
946
- + getHostDetailsAsString (destHost , destPort , localHost ))
947
- .initCause (exception );
943
+ try {
944
+ return new IOException ("Failed on local exception: "
945
+ + exception + "; Host Details : "
946
+ + getHostDetailsAsString (destHost , destPort , localHost ), exception );
947
+ } catch (Exception ignore ) {
948
+ // in worst case, return the original exception
949
+ return exception ;
950
+ }
951
+ }
952
+ }
953
+
954
+ /**
955
+ * Return an @{@link IOException} of the same type as the input exception but with
956
+ * a modified exception message that includes the node name.
957
+ *
958
+ * @param ioe existing exception.
959
+ * @param nodeName name of the node.
960
+ * @return IOException
961
+ */
962
+ public static IOException addNodeNameToIOException (final IOException ioe , final String nodeName ) {
963
+ try {
964
+ final Throwable cause = ioe .getCause ();
965
+ IOException newIoe = null ;
966
+ if (cause != null ) {
967
+ try {
968
+ DynConstructors .Ctor <? extends IOException > ctor =
969
+ new DynConstructors .Builder ()
970
+ .impl (ioe .getClass (), String .class , Throwable .class )
971
+ .buildChecked ();
972
+ newIoe = ctor .newInstance (nodeName + ": " + ioe .getMessage (), cause );
973
+ } catch (NoSuchMethodException e ) {
974
+ // no matching constructor - try next approach below
975
+ }
976
+ }
977
+ if (newIoe == null ) {
978
+ DynConstructors .Ctor <? extends IOException > ctor =
979
+ new DynConstructors .Builder ()
980
+ .impl (ioe .getClass (), String .class )
981
+ .buildChecked ();
982
+ newIoe = ctor .newInstance (nodeName + ": " + ioe .getMessage ());
983
+ if (cause != null ) {
984
+ try {
985
+ newIoe .initCause (cause );
986
+ } catch (Exception e ) {
987
+ // Unable to initCause. Ignore the exception.
988
+ }
989
+ }
990
+ }
991
+ newIoe .setStackTrace (ioe .getStackTrace ());
992
+ return newIoe ;
993
+ } catch (Exception e ) {
994
+ // Unable to create new exception. Return the original exception.
995
+ return ioe ;
948
996
}
949
997
}
950
998
@@ -957,9 +1005,22 @@ private static <T extends IOException> T wrapWithMessage(
957
1005
T exception , String msg ) throws T {
958
1006
Class <? extends Throwable > clazz = exception .getClass ();
959
1007
try {
960
- Constructor <? extends Throwable > ctor = clazz .getConstructor (String .class );
961
- Throwable t = ctor .newInstance (msg );
962
- return (T )(t .initCause (exception ));
1008
+ try {
1009
+ DynConstructors .Ctor <T > ctor =
1010
+ new DynConstructors .Builder ()
1011
+ .impl (clazz , String .class , Throwable .class )
1012
+ .buildChecked ();
1013
+ return ctor .newInstance (msg , exception );
1014
+ } catch (NoSuchMethodException e ) {
1015
+ // no matching constructor - try next approach below
1016
+ }
1017
+ DynConstructors .Ctor <T > ctor =
1018
+ new DynConstructors .Builder ()
1019
+ .impl (clazz , String .class )
1020
+ .buildChecked ();
1021
+ T newException = ctor .newInstance (msg );
1022
+ newException .initCause (exception );
1023
+ return newException ;
963
1024
} catch (NoSuchMethodException e ) {
964
1025
return exception ;
965
1026
} catch (Throwable e ) {
@@ -1114,7 +1175,7 @@ public static Set<Integer> getFreeSocketPorts(int numOfPorts) {
1114
1175
1115
1176
/**
1116
1177
* Return an @{@link InetAddress} to bind to. If bindWildCardAddress is true
1117
- * than returns null.
1178
+ * then returns null.
1118
1179
*
1119
1180
* @param localAddr local addr.
1120
1181
* @param bindWildCardAddress bind wildcard address.
0 commit comments