use of org.apache.zookeeper.client.HostProvider in project zookeeper by apache.
the class StaticHostProviderTest method testNextGoesRoundAndSleeps.
@Test
public void testNextGoesRoundAndSleeps() {
byte size = 2;
HostProvider hostProvider = getHostProvider(size);
while (size > 0) {
hostProvider.next(0);
--size;
}
long start = Time.currentElapsedTime();
hostProvider.next(1000);
long stop = Time.currentElapsedTime();
assertTrue(900 <= stop - start);
}
use of org.apache.zookeeper.client.HostProvider in project zookeeper by apache.
the class StaticHostProviderTest method testUpdateMigrationGoesRound.
@Test
public void testUpdateMigrationGoesRound() throws UnknownHostException {
HostProvider hostProvider = getHostProvider((byte) 4);
// old list (just the ports): 1238, 1237, 1236, 1235
Collection<InetSocketAddress> newList = new ArrayList<InetSocketAddress>(10);
for (byte i = 12; i > 2; i--) {
// 1246, 1245, 1244, 1243, 1242, 1241,
// 1240, 1239, 1238, 1237
newList.add(new InetSocketAddress(InetAddress.getByAddress(new byte[] { 10, 10, 10, i }), 1234 + i));
}
// servers from the old list that appear in the new list
Collection<InetSocketAddress> oldStaying = new ArrayList<InetSocketAddress>(2);
for (byte i = 4; i > 2; i--) {
// 1238, 1237
oldStaying.add(new InetSocketAddress(InetAddress.getByAddress(new byte[] { 10, 10, 10, i }), 1234 + i));
}
// servers in the new list that are not in the old list
Collection<InetSocketAddress> newComing = new ArrayList<InetSocketAddress>(10);
for (byte i = 12; i > 4; i--) {
// 1246, 1245, 1244, 1243, 1242, 1241, 1240, 1139
newComing.add(new InetSocketAddress(InetAddress.getByAddress(new byte[] { 10, 10, 10, i }), 1234 + i));
}
// Number of machines increases, my server is not in the new cluster
// load on old servers must be decreased, so must connect to one of the
// new servers
// i.e., pNew = 1.
boolean disconnectRequired = hostProvider.updateServerList(newList, new InetSocketAddress(InetAddress.getByAddress(new byte[] { 10, 10, 10, 1 }), 1235));
assertTrue(disconnectRequired);
// This means reconfigMode = true, and nextHostInReconfigMode will be
// called from next
// Since pNew = 1 we should first try the new servers
ArrayList<InetSocketAddress> seen = new ArrayList<InetSocketAddress>();
for (int i = 0; i < newComing.size(); i++) {
InetSocketAddress addr = hostProvider.next(0);
assertTrue(newComing.contains(addr));
assertTrue(!seen.contains(addr));
seen.add(addr);
}
// Next the old servers
seen.clear();
for (int i = 0; i < oldStaying.size(); i++) {
InetSocketAddress addr = hostProvider.next(0);
assertTrue(oldStaying.contains(addr));
assertTrue(!seen.contains(addr));
seen.add(addr);
}
// And now it goes back to normal next() so it should be everything
// together like in testNextGoesRound()
InetSocketAddress first = hostProvider.next(0);
assertTrue(first != null);
for (int i = 0; i < newList.size() - 1; i++) {
hostProvider.next(0);
}
assertEquals(first, hostProvider.next(0));
hostProvider.onConnected();
}
use of org.apache.zookeeper.client.HostProvider in project canal by alibaba.
the class ZooKeeperx method configMutliCluster.
// ===============================
public void configMutliCluster(ZooKeeper zk) {
if (_servers.size() == 1) {
return;
}
String cluster1 = _servers.get(0);
try {
if (_servers.size() > 1) {
// 强制的声明accessible
ReflectionUtils.makeAccessible(clientCnxnField);
ReflectionUtils.makeAccessible(hostProviderField);
ReflectionUtils.makeAccessible(serverAddressesField);
// 添加第二组集群列表
for (int i = 1; i < _servers.size(); i++) {
String cluster = _servers.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);
}
}
use of org.apache.zookeeper.client.HostProvider in project zookeeper by apache.
the class ClientReconnectTest method testClientReconnect.
@Test
public void testClientReconnect() throws IOException, InterruptedException {
HostProvider hostProvider = mock(HostProvider.class);
when(hostProvider.size()).thenReturn(1);
InetSocketAddress inaddr = new InetSocketAddress("127.0.0.1", 1111);
when(hostProvider.next(anyLong())).thenReturn(inaddr);
ZooKeeper zk = mock(ZooKeeper.class);
when(zk.getClientConfig()).thenReturn(new ZKClientConfig());
sc = SocketChannel.open();
ClientCnxnSocketNIO nioCnxn = new MockCnxn();
ClientWatchManager watcher = mock(ClientWatchManager.class);
ClientCnxn clientCnxn = new ClientCnxn("tmp", hostProvider, 5000, zk, watcher, nioCnxn, false);
clientCnxn.start();
countDownLatch.await(5000, TimeUnit.MILLISECONDS);
Assert.assertTrue(countDownLatch.getCount() == 0);
clientCnxn.close();
}
use of org.apache.zookeeper.client.HostProvider in project zookeeper by apache.
the class StaticHostProviderTest method testLiteralIPNoReverseNS.
@Test
public void testLiteralIPNoReverseNS() throws Exception {
byte size = 30;
HostProvider hostProvider = getHostProviderUnresolved(size);
for (int i = 0; i < size; i++) {
InetSocketAddress next = hostProvider.next(0);
assertTrue(next instanceof InetSocketAddress);
assertTrue(!next.isUnresolved());
assertTrue(!next.toString().startsWith("/"));
// Do NOT trigger the reverse name service lookup.
String hostname = next.getHostString();
// In this case, the hostname equals literal IP address.
hostname.equals(next.getAddress().getHostAddress());
}
}
Aggregations