Search in sources :

Example 1 with ConnectStringParser

use of org.apache.zookeeper.client.ConnectStringParser in project zookeeper by apache.

the class ConnectStringParserTest method testSingleServerChrootPath.

@Test
public void testSingleServerChrootPath() {
    String chrootPath = "/hallo/welt";
    String servers = "10.10.10.1";
    assertChrootPath(chrootPath, new ConnectStringParser(servers + chrootPath));
    servers = "[2001:db8:1::242:ac11:2]";
    assertChrootPath(chrootPath, new ConnectStringParser(servers + chrootPath));
}
Also used : ConnectStringParser(org.apache.zookeeper.client.ConnectStringParser) Test(org.junit.jupiter.api.Test)

Example 2 with ConnectStringParser

use of org.apache.zookeeper.client.ConnectStringParser in project zookeeper by apache.

the class ConnectStringParserTest method testParseServersWithoutPort.

@Test
public void testParseServersWithoutPort() {
    String servers = "10.10.10.1,10.10.10.2";
    ConnectStringParser parser = new ConnectStringParser(servers);
    assertEquals("10.10.10.1", parser.getServerAddresses().get(0).getHostString());
    assertEquals(DEFAULT_PORT, parser.getServerAddresses().get(0).getPort());
    assertEquals("10.10.10.2", parser.getServerAddresses().get(1).getHostString());
    assertEquals(DEFAULT_PORT, parser.getServerAddresses().get(1).getPort());
    servers = "[2001:db8:1::242:ac11:2],[2001:db8:85a3:8d3:1319:8a2e:370:7348]";
    parser = new ConnectStringParser(servers);
    assertEquals("2001:db8:1::242:ac11:2", parser.getServerAddresses().get(0).getHostString());
    assertEquals(DEFAULT_PORT, parser.getServerAddresses().get(0).getPort());
    assertEquals("2001:db8:85a3:8d3:1319:8a2e:370:7348", parser.getServerAddresses().get(1).getHostString());
    assertEquals(DEFAULT_PORT, parser.getServerAddresses().get(1).getPort());
}
Also used : ConnectStringParser(org.apache.zookeeper.client.ConnectStringParser) Test(org.junit.jupiter.api.Test)

Example 3 with ConnectStringParser

use of org.apache.zookeeper.client.ConnectStringParser in project canal by alibaba.

the class ZooKeeperx method configMutliCluster.

// ===============================
public void configMutliCluster(ZooKeeper zk) {
    if (_serversList.size() == 1) {
        return;
    }
    String cluster1 = _serversList.get(0);
    try {
        if (_serversList.size() > 1) {
            // 强制的声明accessible
            ReflectionUtils.makeAccessible(clientCnxnField);
            ReflectionUtils.makeAccessible(hostProviderField);
            ReflectionUtils.makeAccessible(serverAddressesField);
            // 添加第二组集群列表
            for (int i = 1; i < _serversList.size(); i++) {
                String cluster = _serversList.get(i);
                // 强制获取zk中的地址信息
                ClientCnxn cnxn = (ClientCnxn) ReflectionUtils.getField(clientCnxnField, zk);
                HostProvider hostProvider = (HostProvider) ReflectionUtils.getField(hostProviderField, cnxn);
                List<InetSocketAddress> serverAddrs = (List<InetSocketAddress>) ReflectionUtils.getField(serverAddressesField, hostProvider);
                // 添加第二组集群列表
                serverAddrs.addAll(new ConnectStringParser(cluster).getServerAddresses());
            }
        }
    } catch (Exception e) {
        try {
            if (zk != null) {
                zk.close();
            }
        } catch (InterruptedException ie) {
        // ignore interrupt
        }
        throw new ZkException("zookeeper_create_error, serveraddrs=" + cluster1, e);
    }
}
Also used : ConnectStringParser(org.apache.zookeeper.client.ConnectStringParser) ZkException(org.I0Itec.zkclient.exception.ZkException) InetSocketAddress(java.net.InetSocketAddress) StaticHostProvider(org.apache.zookeeper.client.StaticHostProvider) HostProvider(org.apache.zookeeper.client.HostProvider) List(java.util.List) ClientCnxn(org.apache.zookeeper.ClientCnxn) ZkException(org.I0Itec.zkclient.exception.ZkException) IOException(java.io.IOException)

Example 4 with ConnectStringParser

use of org.apache.zookeeper.client.ConnectStringParser in project samza by apache.

the class ZkCoordinationServiceFactory method createZkPath.

/**
   * if ZkConnectString contains some path at the end, it needs to be created when connecting for the first time.
   * @param zkConnect - connect string
   * @param zkClient - zkClient object to talk to the ZK
   */
public static void createZkPath(String zkConnect, ZkClient zkClient) {
    ConnectStringParser parser = new ConnectStringParser(zkConnect);
    String path = parser.getChrootPath();
    LOG.info("path =" + path);
    if (!Strings.isNullOrEmpty(path)) {
        // create this path in zk
        LOG.info("first connect. creating path =" + path + " in ZK " + parser.getServerAddresses());
        if (!zkClient.exists(path)) {
            // will create parents if needed and will not throw exception if exists
            zkClient.createPersistent(path, true);
        }
    }
}
Also used : ConnectStringParser(org.apache.zookeeper.client.ConnectStringParser)

Example 5 with ConnectStringParser

use of org.apache.zookeeper.client.ConnectStringParser in project otter by alibaba.

the class ZooKeeperx method configMutliCluster.

// ===============================
public void configMutliCluster(ZooKeeper zk) {
    if (_serversList.size() == 1) {
        return;
    }
    String cluster1 = _serversList.get(0);
    try {
        if (_serversList.size() > 1) {
            // 强制的声明accessible
            ReflectionUtils.makeAccessible(clientCnxnField);
            ReflectionUtils.makeAccessible(hostProviderField);
            ReflectionUtils.makeAccessible(serverAddressesField);
            // 添加第二组集群列表
            for (int i = 1; i < _serversList.size(); i++) {
                String cluster = _serversList.get(i);
                // 强制获取zk中的地址信息
                ClientCnxn cnxn = (ClientCnxn) ReflectionUtils.getField(clientCnxnField, zk);
                HostProvider hostProvider = (HostProvider) ReflectionUtils.getField(hostProviderField, cnxn);
                List<InetSocketAddress> serverAddrs = (List<InetSocketAddress>) ReflectionUtils.getField(serverAddressesField, hostProvider);
                // 添加第二组集群列表
                serverAddrs.addAll(new ConnectStringParser(cluster).getServerAddresses());
            }
        }
    } catch (Exception e) {
        try {
            if (zk != null) {
                zk.close();
            }
        } catch (InterruptedException ie) {
        // ignore interrupt
        }
        throw new ZkException("zookeeper_create_error, serveraddrs=" + cluster1, e);
    }
}
Also used : ConnectStringParser(org.apache.zookeeper.client.ConnectStringParser) ZkException(org.I0Itec.zkclient.exception.ZkException) InetSocketAddress(java.net.InetSocketAddress) StaticHostProvider(org.apache.zookeeper.client.StaticHostProvider) HostProvider(org.apache.zookeeper.client.HostProvider) List(java.util.List) ClientCnxn(org.apache.zookeeper.ClientCnxn) ZkException(org.I0Itec.zkclient.exception.ZkException) IOException(java.io.IOException)

Aggregations

ConnectStringParser (org.apache.zookeeper.client.ConnectStringParser)11 Test (org.junit.jupiter.api.Test)6 InetSocketAddress (java.net.InetSocketAddress)3 HostProvider (org.apache.zookeeper.client.HostProvider)3 StaticHostProvider (org.apache.zookeeper.client.StaticHostProvider)3 IOException (java.io.IOException)2 List (java.util.List)2 ZkException (org.I0Itec.zkclient.exception.ZkException)2 ClientCnxn (org.apache.zookeeper.ClientCnxn)2 SamzaException (org.apache.samza.SamzaException)1 ZKClientConfig (org.apache.zookeeper.client.ZKClientConfig)1