Package org.apache.hadoop.net
Class NetUtils
java.lang.Object
org.apache.hadoop.net.NetUtils
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic IOExceptionaddNodeNameToIOException(IOException ioe, String nodeName) Return an @IOExceptionof the same type as the input exception but with a modified exception message that includes the node name.static voidaddStaticResolution(String host, String resolvedName) Adds a static resolution for host.static InetAddressbindToLocalAddress(InetAddress localAddr, boolean bindWildCardAddress) Return an @InetAddressto bind to.static voidconnect(Socket socket, SocketAddress address, int timeout) This is a drop-in replacement forSocket.connect(SocketAddress, int).static voidconnect(Socket socket, SocketAddress endpoint, SocketAddress localAddr, int timeout) Likeconnect(Socket, SocketAddress, int)but also takes a local address and port to bind the socket to.static InetSocketAddresscreateSocketAddr(String target) Util method to build socket addr from either.static InetSocketAddresscreateSocketAddr(String target, int defaultPort) Util method to build socket addr from either.static InetSocketAddresscreateSocketAddr(String target, int defaultPort, String configName) Create an InetSocketAddress from the given target string and default port.static InetSocketAddresscreateSocketAddr(String target, int defaultPort, String configName, boolean useCacheIfPresent) Create an InetSocketAddress from the given target string and default port.static InetSocketAddresscreateSocketAddr(String target, int defaultPort, String configName, boolean useCacheIfPresent, boolean isResolved) static InetSocketAddresscreateSocketAddrForHost(String host, int port) Create a socket address with the given host and port.static InetSocketAddresscreateSocketAddrUnresolved(String target) This is used to get all the resolutions that were added usingaddStaticResolution(String, String).static URIgetCanonicalUri(URI uri, int defaultPort) Resolve the uri's hostname and add the default port if not in the uristatic InetSocketAddressReturns an InetSocketAddress that a client can use to connect to the given listening address.static InetSocketAddressgetConnectAddress(Server server) Returns InetSocketAddress that a client can use to connect to the server.static SocketFactoryGet the default socket factory as specified by the configuration parameterhadoop.rpc.socket.factory.defaultstatic intReturn a free port number.getFreeSocketPorts(int numOfPorts) Return free ports.static StringReturn hostname without throwing exception.static StringgetHostNameOfIP(String ipPort) Attempt to obtain the host name of the given string which contains an IP address and an optional port.static StringCompose a "host:port" string from the address.static SocketInputWrappergetInputStream(Socket socket) Same asgetInputStream(socket, socket.getSoTimeout()).static SocketInputWrappergetInputStream(Socket socket, long timeout) Return aSocketInputWrapperfor the socket and set the given timeout.static List<InetAddress>Return an InetAddress for each interface that matches the given subnet specified using CIDR notation.static StringReturn hostname without throwing exception.static InetAddressgetLocalInetAddress(String host) Checks ifhostis a local host name and returnInetAddresscorresponding to that address.static OutputStreamgetOutputStream(Socket socket) Same as getOutputStream(socket, 0).static OutputStreamgetOutputStream(Socket socket, long timeout) Returns OutputStream for the socket.static intGet port as integer from host port string like host:port.static SocketFactorygetSocketFactory(Configuration conf, Class<?> clazz) Get the socket factory for the given class according to its configuration parameterhadoop.rpc.socket.factory.class.<ClassName>.static SocketFactorygetSocketFactoryFromProperty(Configuration conf, String propValue) Get the socket factory corresponding to the given proxy URI.static StringgetStaticResolution(String host) Retrieves the resolved name for the passed host.static booleanisLocalAddress(InetAddress addr) Given an InetAddress, checks to see if the address is a local address, by comparing the address with all the interfaces on the node.static booleanisValidSubnet(String subnet) isValidSubnet.static StringnormalizeHostName(String name) Given a string representation of a host, return its ip address in textual presentation.normalizeHostNames(Collection<String> names) Given a collection of string representation of hosts, return a list of corresponding IP addresses in the textual representation.static StringnormalizeIP2HostName(String ipPort) Attempt to normalize the given string to "host:port" if it like "ip:port".static voidverifyHostnames(String[] names) Performs a sanity check on the list of hostnames/IPs to verify they at least appear to be valid.static IOExceptionwrapException(String destHost, int destPort, String localHost, int localPort, IOException exception) Take an IOException , the local host port and remote host port details and return an IOException with the input exception as the cause and also include the host details.
-
Field Details
-
UNKNOWN_HOST
text included in wrapped exceptions if the host is null: "(unknown)"- See Also:
-
HADOOP_WIKI
Base URL of the Hadoop Wiki: "http://wiki.apache.org/hadoop/"- See Also:
-
-
Constructor Details
-
NetUtils
public NetUtils()
-
-
Method Details
-
getSocketFactory
Get the socket factory for the given class according to its configuration parameterhadoop.rpc.socket.factory.class.<ClassName>. When no such parameter exists then fall back on the default socket factory as configured byhadoop.rpc.socket.factory.class.default. If this default socket factory is not configured, then fall back on the JVM default socket factory.- Parameters:
conf- the configurationclazz- the class (usually aVersionedProtocol)- Returns:
- a socket factory
-
getDefaultSocketFactory
Get the default socket factory as specified by the configuration parameterhadoop.rpc.socket.factory.default- Parameters:
conf- the configuration- Returns:
- the default socket factory as specified in the configuration or the JVM default socket factory if the configuration does not contain a default socket factory property.
-
getSocketFactoryFromProperty
Get the socket factory corresponding to the given proxy URI. If the given proxy URI corresponds to an absence of configuration parameter, returns null. If the URI is malformed raises an exception.- Parameters:
conf- configuration.propValue- the property which is the class name of the SocketFactory to instantiate; assumed non null and non empty.- Returns:
- a socket factory as defined in the property value.
-
createSocketAddr
Util method to build socket addr from either. <host>:<port> <fs>://<host>:<port>/<path>- Parameters:
target- target.- Returns:
- socket addr.
-
createSocketAddrUnresolved
-
createSocketAddr
Util method to build socket addr from either. <host> <host>:<port> <fs>://<host>:<port>/<path>- Parameters:
target- target.defaultPort- default port.- Returns:
- socket addr.
-
createSocketAddr
Create an InetSocketAddress from the given target string and default port. If the string cannot be parsed correctly, theconfigNameparameter is used as part of the exception message, allowing the user to better diagnose the misconfiguration.- Parameters:
target- a string of either "host" or "host:port"defaultPort- the default port iftargetdoes not include a port numberconfigName- the name of the configuration from whichtargetwas loaded. This is used in the exception message in the case that parsing fails.- Returns:
- socket addr.
-
createSocketAddr
public static InetSocketAddress createSocketAddr(String target, int defaultPort, String configName, boolean useCacheIfPresent) Create an InetSocketAddress from the given target string and default port. If the string cannot be parsed correctly, theconfigNameparameter is used as part of the exception message, allowing the user to better diagnose the misconfiguration.- Parameters:
target- a string of either "host" or "host:port"defaultPort- the default port iftargetdoes not include a port numberconfigName- the name of the configuration from whichtargetwas loaded. This is used in the exception message in the case that parsing fails.useCacheIfPresent- Whether use cache when create URI- Returns:
- socket addr
-
createSocketAddr
public static InetSocketAddress createSocketAddr(String target, int defaultPort, String configName, boolean useCacheIfPresent, boolean isResolved) -
createSocketAddrForHost
Create a socket address with the given host and port. The hostname might be replaced with another host that was set viaaddStaticResolution(String, String). The value of hadoop.security.token.service.use_ip will determine whether the standard java host resolver is used, or if the fully qualified resolver is used.- Parameters:
host- the hostname or IP use to instantiate the objectport- the port number- Returns:
- InetSocketAddress
-
getCanonicalUri
Resolve the uri's hostname and add the default port if not in the uri- Parameters:
uri- to resolvedefaultPort- if none is given- Returns:
- URI
-
addStaticResolution
Adds a static resolution for host. This can be used for setting up hostnames with names that are fake to point to a well known host. For e.g. in some testcases we require to have daemons with different hostnames running on the same machine. In order to create connections to these daemons, one can set up mappings from those hostnames to "localhost".getStaticResolution(String)can be used to query for the actual hostname.- Parameters:
host- the hostname or IP use to instantiate the object.resolvedName- resolved name.
-
getStaticResolution
Retrieves the resolved name for the passed host. The resolved name must have been set earlier usingaddStaticResolution(String, String)- Parameters:
host- the hostname or IP use to instantiate the object.- Returns:
- the resolution
-
getAllStaticResolutions
This is used to get all the resolutions that were added usingaddStaticResolution(String, String). The return value is a List each element of which contains an array of String of the form String[0]=hostname, String[1]=resolved-hostname- Returns:
- the list of resolutions
-
getConnectAddress
Returns InetSocketAddress that a client can use to connect to the server. Server.getListenerAddress() is not correct when the server binds to "0.0.0.0". This returns "hostname:port" of the server, or "127.0.0.1:port" when the getListenerAddress() returns "0.0.0.0:port".- Parameters:
server- server.- Returns:
- socket address that a client can use to connect to the server.
-
getConnectAddress
Returns an InetSocketAddress that a client can use to connect to the given listening address.- Parameters:
addr- of a listener- Returns:
- socket address that a client can use to connect to the server.
-
getInputStream
Same asgetInputStream(socket, socket.getSoTimeout()).- Parameters:
socket- socket.- Returns:
- SocketInputWrapper for reading from the socket.
- Throws:
IOException- raised on errors performing I/O.- See Also:
-
getInputStream
Return aSocketInputWrapperfor the socket and set the given timeout. If the socket does not have an associated channel, then its socket timeout will be set to the specified value. Otherwise, aSocketInputStreamwill be created which reads with the configured timeout. Any socket created using socket factories returned byNetUtils(), must use this interface instead ofSocket.getInputStream(). In general, this should be called only once on each socket: see the note inSocketInputWrapper.setTimeout(long)for more information.- Parameters:
socket- socket.timeout- timeout in milliseconds. zero for waiting as long as necessary.- Returns:
- SocketInputWrapper for reading from the socket.
- Throws:
IOException- raised on errors performing I/O.- See Also:
-
getOutputStream
Same as getOutputStream(socket, 0). Timeout of zero implies write will wait until data is available.
From documentation forgetOutputStream(Socket, long):
Returns OutputStream for the socket. If the socket has an associated SocketChannel then it returns aSocketOutputStreamwith the given timeout. If the socket does not have a channel,Socket.getOutputStream()is returned. In the later case, the timeout argument is ignored and the write will wait until data is available.
Any socket created using socket factories returned byNetUtils, must use this interface instead ofSocket.getOutputStream().- Parameters:
socket- socket.- Returns:
- OutputStream for writing to the socket.
- Throws:
IOException- raised on errors performing I/O.- See Also:
-
getOutputStream
Returns OutputStream for the socket. If the socket has an associated SocketChannel then it returns aSocketOutputStreamwith the given timeout. If the socket does not have a channel,Socket.getOutputStream()is returned. In the later case, the timeout argument is ignored and the write will wait until data is available.
Any socket created using socket factories returned byNetUtils, must use this interface instead ofSocket.getOutputStream().- Parameters:
socket- socket.timeout- timeout in milliseconds. This may not always apply. zero for waiting as long as necessary.- Returns:
- OutputStream for writing to the socket.
- Throws:
IOException- raised on errors performing I/O.- See Also:
-
connect
This is a drop-in replacement forSocket.connect(SocketAddress, int). In the case of normal sockets that don't have associated channels, this just invokessocket.connect(endpoint, timeout). Ifsocket.getChannel()returns a non-null channel, connect is implemented using Hadoop's selectors. This is done mainly to avoid Sun's connect implementation from creating thread-local selectors, since Hadoop does not have control on when these are closed and could end up taking all the available file descriptors.- Parameters:
socket- socket.address- the remote addresstimeout- timeout in milliseconds- Throws:
IOException- raised on errors performing I/O.- See Also:
-
connect
public static void connect(Socket socket, SocketAddress endpoint, SocketAddress localAddr, int timeout) throws IOException Likeconnect(Socket, SocketAddress, int)but also takes a local address and port to bind the socket to.- Parameters:
socket- socket.endpoint- the remote addresslocalAddr- the local address to bind the socket totimeout- timeout in milliseconds- Throws:
IOException- raised on errors performing I/O.
-
normalizeHostName
Given a string representation of a host, return its ip address in textual presentation.- Parameters:
name- a string representation of a host: either a textual representation its IP address or its host name- Returns:
- its IP address in the string format
-
normalizeHostNames
Given a collection of string representation of hosts, return a list of corresponding IP addresses in the textual representation.- Parameters:
names- a collection of string representations of hosts- Returns:
- a list of corresponding IP addresses in the string format
- See Also:
-
verifyHostnames
Performs a sanity check on the list of hostnames/IPs to verify they at least appear to be valid.- Parameters:
names- - List of hostnames/IPs- Throws:
UnknownHostException- Unknown Host Exception.
-
getHostNameOfIP
Attempt to obtain the host name of the given string which contains an IP address and an optional port.- Parameters:
ipPort- string of form ip[:port]- Returns:
- Host name or null if the name can not be determined
-
normalizeIP2HostName
Attempt to normalize the given string to "host:port" if it like "ip:port".- Parameters:
ipPort- maybe lik ip:port or host:port.- Returns:
- host:port
-
getLocalHostname
Return hostname without throwing exception. The returned hostname String format is "hostname".- Returns:
- hostname
-
getHostname
Return hostname without throwing exception. The returned hostname String format is "hostname/ip address".- Returns:
- hostname
-
getHostPortString
Compose a "host:port" string from the address.- Parameters:
addr- address.- Returns:
- hort port string.
-
getPortFromHostPortString
Get port as integer from host port string like host:port.- Parameters:
addr- host + port string like host:port.- Returns:
- an integer value representing the port.
- Throws:
IllegalArgumentException- if the input is not in the correct format.
-
getLocalInetAddress
Checks ifhostis a local host name and returnInetAddresscorresponding to that address.- Parameters:
host- the specified host- Returns:
- a valid local
InetAddressor null - Throws:
SocketException- if an I/O error occurs
-
isLocalAddress
Given an InetAddress, checks to see if the address is a local address, by comparing the address with all the interfaces on the node.- Parameters:
addr- address to check if it is local node's address- Returns:
- true if the address corresponds to the local node
-
wrapException
public static IOException wrapException(String destHost, int destPort, String localHost, int localPort, IOException exception) Take an IOException , the local host port and remote host port details and return an IOException with the input exception as the cause and also include the host details. The new exception provides the stack trace of the place where the exception is thrown and some extra diagnostics information. If the exception is of type BindException, ConnectException, UnknownHostException, SocketTimeoutException or has a String constructor, return a new one of the same type; Otherwise return an IOException.- Parameters:
destHost- target host (nullable)destPort- target portlocalHost- local host (nullable)localPort- local portexception- the caught exception.- Returns:
- an exception to throw
-
addNodeNameToIOException
Return an @IOExceptionof the same type as the input exception but with a modified exception message that includes the node name.- Parameters:
ioe- existing exception.nodeName- name of the node.- Returns:
- IOException
-
isValidSubnet
isValidSubnet.- Parameters:
subnet- subnet.- Returns:
- true if the given string is a subnet specified using CIDR notation, false otherwise
-
getIPs
Return an InetAddress for each interface that matches the given subnet specified using CIDR notation.- Parameters:
subnet- subnet specified using CIDR notationreturnSubinterfaces- whether to return IPs associated with subinterfaces- Returns:
- ips.
- Throws:
IllegalArgumentException- if subnet is invalid
-
getFreeSocketPort
public static int getFreeSocketPort()Return a free port number. There is no guarantee it will remain free, so it should be used immediately.- Returns:
- A free port for binding a local socket
-
getFreeSocketPorts
Return free ports. There is no guarantee they will remain free, so ports should be used immediately. The number of free ports returned by this method should match argumentnumOfPorts. Num of ports provided in the argument should not exceed 25.- Parameters:
numOfPorts- Number of free ports to acquire.- Returns:
- Free ports for binding a local socket.
-
bindToLocalAddress
Return an @InetAddressto bind to. If bindWildCardAddress is true then returns null.- Parameters:
localAddr- local addr.bindWildCardAddress- bind wildcard address.- Returns:
- InetAddress
-