public static Pair<String, Integer> validateHostAndPort(String hostPort, boolean resolveHost) {
        hostPort = hostPort.replaceAll("\\s+", "");
        if (hostPort.isEmpty()) {
            throw new SemanticException("Invalid host port: " + hostPort);
        }

        String[] hostInfo;
        try {
            hostInfo = NetUtils.resolveHostInfoFromHostPort(hostPort);
        } catch (AnalysisException e) {
            throw new SemanticException("Invalid host port: " + hostPort, e);
        }
        String host = hostInfo[0];
        if (Strings.isNullOrEmpty(host)) {
            throw new SemanticException("Host is null");
        }

        int heartbeatPort;
        try {
            // validate host
            if (resolveHost && !InetAddressValidator.getInstance().isValid(host)) {
                // maybe this is a hostname
                // if no IP address for the host could be found, 'getByName'
                // will throw
                // UnknownHostException
                InetAddress inetAddress = InetAddress.getByName(host);
                host = inetAddress.getHostAddress();
            }

            // validate port
            heartbeatPort = Integer.parseInt(hostInfo[1]);

            if (heartbeatPort <= 0 || heartbeatPort >= 65536) {
                throw new SemanticException("Port is out of range: " + heartbeatPort);
            }

            return new Pair<>(host, heartbeatPort);
        } catch (UnknownHostException e) {
            throw new SemanticException("Unknown host: " + e.getMessage());
        } catch (Exception e) {
            throw new SemanticException("Encounter unknown exception: " + e.getMessage());
        }
    }
@Test
    public void testGetHostAndPort() {
        String ipv4 = "192.168.1.2:9050";
        String ipv6 = "[fe80::5054:ff:fec9:dee0]:9050";
        String ipv6Error = "fe80::5054:ff:fec9:dee0:dee0";
        try {
            Pair<String, Integer> ipv4Addr = SystemInfoService.validateHostAndPort(ipv4, false);
            Assert.assertEquals("192.168.1.2", ipv4Addr.first);
            Assert.assertEquals(9050, ipv4Addr.second.intValue());
        } catch (SemanticException e) {
            e.printStackTrace();
            Assert.fail();
        }
        try {
            Pair<String, Integer> ipv6Addr = SystemInfoService.validateHostAndPort(ipv6, false);
            Assert.assertEquals("fe80::5054:ff:fec9:dee0", ipv6Addr.first);
            Assert.assertEquals(9050, ipv6Addr.second.intValue());
        } catch (SemanticException e) {
            e.printStackTrace();
            Assert.fail();
        }
        try {
            SystemInfoService.validateHostAndPort(ipv6Error, false);
            Assert.fail();
        } catch (SemanticException e) {
            e.printStackTrace();
        }
    }