Class NetworkAddressUtils


  • @ThreadSafe
    public final class NetworkAddressUtils
    extends java.lang.Object
    Common network address related utilities shared by all components in Alluxio.
    • Field Detail

      • WILDCARD_ADDRESS

        public static final java.lang.String WILDCARD_ADDRESS
        See Also:
        Constant Field Values
      • WINDOWS

        public static final boolean WINDOWS
        Checks if the underlying OS is Windows.
    • Method Detail

      • assertValidPort

        public static void assertValidPort​(int port)
        Checks if the given port is valid.
        Parameters:
        port - the port to check
      • getConnectAddress

        public static java.net.InetSocketAddress getConnectAddress​(NetworkAddressUtils.ServiceAttributeProvider service,
                                                                   AlluxioConfiguration conf)
        Helper method to get the InetSocketAddress address for client to communicate with the service.
        Parameters:
        service - the service name used to connect
        conf - the configuration containing the address to connect to
        Returns:
        the service address that a client (typically outside the service machine) uses to communicate with service.
      • getConnectHost

        public static java.lang.String getConnectHost​(NetworkAddressUtils.ServiceAttributeProvider service,
                                                      AlluxioConfiguration conf)
        Provides an externally resolvable hostname for client to communicate with the service. If the hostname is not explicitly specified, Alluxio will try to use the bind host. If the bind host is wildcard, Alluxio will automatically determine an appropriate hostname from local machine. The various possibilities shown in the following table:
        Hostname Scenarios
        Specified Hostname Specified Bind Host Enable Network IP Address Used Returned Connect Host
        hostname hostname true/false hostname
        not defined hostname true/false hostname
        hostname 0.0.0.0 or not defined true/false hostname
        not defined 0.0.0.0 or not defined false local hostname
        not defined 0.0.0.0 or not defined true local IP address
        Parameters:
        service - Service type used to connect
        conf - configuration
        Returns:
        the externally resolvable hostname that the client can use to communicate with the service.
      • getPort

        public static int getPort​(NetworkAddressUtils.ServiceAttributeProvider service,
                                  AlluxioConfiguration conf)
        Gets the port number on a given service type. If user defined port number is not explicitly specified, Alluxio will use the default service port.
        Parameters:
        service - Service type used to connect
        conf - Alluxio configuration
        Returns:
        the service port number
      • getBindAddress

        public static java.net.InetSocketAddress getBindAddress​(NetworkAddressUtils.ServiceAttributeProvider service,
                                                                AlluxioConfiguration conf)
        Helper method to get the bind hostname for a given service.
        Parameters:
        service - the service name
        conf - Alluxio configuration
        Returns:
        the InetSocketAddress the service will bind to
      • getBindHost

        public static java.lang.String getBindHost​(NetworkAddressUtils.ServiceAttributeProvider service,
                                                   AlluxioConfiguration conf)
        Helper method to get the InetSocketAddress bind address on a given service.

        Host bind information searching order:

        1. System properties or environment variables via alluxio-env.sh
        2. Default properties via alluxio-default.properties file
        3. A externally resolvable local hostname for the host this JVM is running on
        Parameters:
        service - the service name
        conf - Alluxio configuration
        Returns:
        the bind hostname
      • getClientHostName

        public static java.lang.String getClientHostName​(AlluxioConfiguration conf)
        Gets the local hostname to be used by the client. If this isn't configured, a non-loopback local hostname will be looked up.
        Parameters:
        conf - Alluxio configuration
        Returns:
        the local hostname for the client
      • getLocalNodeName

        public static java.lang.String getLocalNodeName​(AlluxioConfiguration conf)
        Gets a local node name from configuration if it is available, falling back on localhost lookup.
        Parameters:
        conf - Alluxio configuration
        Returns:
        the local node name
      • getLocalHostName

        public static java.lang.String getLocalHostName​(int timeoutMs)
        Gets a local host name for the host this JVM is running on.
        Parameters:
        timeoutMs - Timeout in milliseconds to use for checking that a possible local host is reachable
        Returns:
        the local host name, which is not based on a loopback ip address
      • getLocalHostMetricName

        public static java.lang.String getLocalHostMetricName​(int timeoutMs)
        Gets a local hostname for the host this JVM is running on with '.' replaced with '_' for metrics usage.
        Parameters:
        timeoutMs - Timeout in milliseconds to use for checking that a possible local host is reachable
        Returns:
        the metrics system friendly local host name
      • getLocalIpAddress

        public static java.lang.String getLocalIpAddress​(int timeoutMs)
        Gets a local IP address for the host this JVM is running on.
        Parameters:
        timeoutMs - Timeout in milliseconds to use for checking that a possible local IP is reachable
        Returns:
        the local ip address, which is not a loopback address and is reachable
      • isServing

        public static boolean isServing​(java.lang.String host,
                                        int port)
        Parameters:
        host - the host to try to connect to
        port - the port to try to connect on
        Returns:
        whether a socket connection can be made to the given host on the given port
      • isLocalAddress

        public static boolean isLocalAddress​(java.lang.String targetAddress,
                                             int timeoutMs)
        Parameters:
        targetAddress - the target address, hostname or IP
        timeoutMs - Timeout in milliseconds to use for checking that a possible local host is reachable
        Returns:
        true if the target address is the local address, false otherwise
      • resolveHostName

        @Nullable
        public static java.lang.String resolveHostName​(java.lang.String hostname)
                                                throws java.net.UnknownHostException
        Resolves a given hostname by a canonical hostname. When a hostname alias (e.g., those specified in /etc/hosts) is given, the alias may not be resolvable on other hosts in a cluster unless the same alias is defined there.
        Parameters:
        hostname - the input hostname, which could be an alias
        Returns:
        the canonical form of the hostname, or null if it is null or empty
        Throws:
        java.net.UnknownHostException - if the given hostname cannot be resolved
      • resolveIpAddress

        public static java.lang.String resolveIpAddress​(java.lang.String hostname)
                                                 throws java.net.UnknownHostException
        Resolves a given hostname IP address.
        Parameters:
        hostname - the input hostname, which could be an alias
        Returns:
        the hostname IP address
        Throws:
        java.net.UnknownHostException - if the given hostname cannot be resolved
      • getFqdnHost

        public static java.lang.String getFqdnHost​(java.net.InetSocketAddress addr)
        Gets FQDN(Full Qualified Domain Name) from Java representations of network address, except String representation which should be handled by resolveHostName(String) which will handle the situation where hostname is null.
        Parameters:
        addr - the input network address representation, can not be null
        Returns:
        the resolved FQDN host name
      • getFqdnHost

        public static java.lang.String getFqdnHost​(WorkerNetAddress addr)
                                            throws java.net.UnknownHostException
        Gets FQDN(Full Qualified Domain Name) from Alluxio representation of network address.
        Parameters:
        addr - the input network address representation
        Returns:
        the resolved FQDN host name
        Throws:
        java.net.UnknownHostException - if the host is not known
      • parseInetSocketAddress

        @Nullable
        public static java.net.InetSocketAddress parseInetSocketAddress​(java.lang.String address)
                                                                 throws java.io.IOException
        Parses InetSocketAddress from a String.
        Parameters:
        address - socket address to parse
        Returns:
        InetSocketAddress of the String
        Throws:
        java.io.IOException
      • getDataPortSocketAddress

        public static java.net.SocketAddress getDataPortSocketAddress​(WorkerNetAddress netAddress,
                                                                      AlluxioConfiguration conf)
        Extracts dataPort socket address from Alluxio representation of network address.
        Parameters:
        netAddress - the input network address representation
        conf - Alluxio configuration
        Returns:
        the socket address
      • getRpcPortSocketAddress

        public static java.net.SocketAddress getRpcPortSocketAddress​(WorkerNetAddress netAddress,
                                                                     AlluxioConfiguration conf)
        Extracts rpcPort socket address from Alluxio representation of network address.
        Parameters:
        netAddress - the input network address representation
        conf - Alluxio configuration
        Returns:
        the socket address
      • pingService

        public static void pingService​(java.net.InetSocketAddress address,
                                       alluxio.grpc.ServiceType serviceType,
                                       AlluxioConfiguration conf,
                                       UserState userState)
                                throws AlluxioStatusException
        Test if the input address is serving an Alluxio service. This method make use of the gRPC protocol for performing service communication. This method throws UnauthenticatedException if the user is not authenticated, StatusRuntimeException If the host not reachable or does not serve the given service.
        Parameters:
        address - the network address to ping
        serviceType - the Alluxio service type
        conf - Alluxio configuration
        userState - the UserState
        Throws:
        AlluxioStatusException
      • containsLocalIp

        public static boolean containsLocalIp​(java.util.List<java.net.InetSocketAddress> clusterAddresses,
                                              AlluxioConfiguration conf)
        Parameters:
        clusterAddresses - addresses of all nodes in the Raft cluster
        conf - Alluxio configuration
        Returns:
        true if the cluster addresses contain the local IP, false otherwise