use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.
the class StatCommandTest method setUp.
@Before
public void setUp() throws IOException {
outputWriter = new StringWriter();
ServerCnxn serverCnxnMock = mock(ServerCnxn.class);
LeaderZooKeeperServer zks = mock(LeaderZooKeeperServer.class);
when(zks.isRunning()).thenReturn(true);
providerMock = mock(ServerStats.Provider.class);
when(zks.serverStats()).thenReturn(new ServerStats(providerMock));
ZKDatabase zkDatabaseMock = mock(ZKDatabase.class);
when(zks.getZKDatabase()).thenReturn(zkDatabaseMock);
Leader leaderMock = mock(Leader.class);
when(leaderMock.getProposalStats()).thenReturn(new ProposalStats());
when(zks.getLeader()).thenReturn(leaderMock);
ServerCnxnFactory serverCnxnFactory = mock(ServerCnxnFactory.class);
ServerCnxn serverCnxn = mock(ServerCnxn.class);
List<ServerCnxn> connections = new ArrayList<>();
connections.add(serverCnxn);
when(serverCnxnFactory.getConnections()).thenReturn(connections);
statCommand = new StatCommand(new PrintWriter(outputWriter), serverCnxnMock, FourLetterCommands.statCmd);
statCommand.setZkServer(zks);
statCommand.setFactory(serverCnxnFactory);
}
use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.
the class ACLTest method testNullACL.
@Test
public void testNullACL() throws Exception {
File tmpDir = ClientBase.createTmpDir();
ClientBase.setupTestEnv();
ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
f.startup(zks);
ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
try {
// case 1 : null ACL with create
try {
zk.create("/foo", "foo".getBytes(), null, CreateMode.PERSISTENT);
Assert.fail("Expected InvalidACLException for null ACL parameter");
} catch (InvalidACLException e) {
// Expected. Do nothing
}
// case 2 : null ACL with other create API
try {
zk.create("/foo", "foo".getBytes(), null, CreateMode.PERSISTENT, null);
Assert.fail("Expected InvalidACLException for null ACL parameter");
} catch (InvalidACLException e) {
// Expected. Do nothing
}
// case 3 : null ACL with setACL
try {
zk.setACL("/foo", null, 0);
Assert.fail("Expected InvalidACLException for null ACL parameter");
} catch (InvalidACLException e) {
// Expected. Do nothing
}
} finally {
zk.close();
f.shutdown();
zks.shutdown();
Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT));
}
}
use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.
the class ClientPortBindTest method testBindByAddress.
/**
* Verify that the server binds to the specified address
*/
@Test
public void testBindByAddress() throws Exception {
String bindAddress = null;
Enumeration<NetworkInterface> intfs = NetworkInterface.getNetworkInterfaces();
// if we have a loopback and it has an address use it
while (intfs.hasMoreElements()) {
NetworkInterface i = intfs.nextElement();
try {
if (i.isLoopback()) {
Enumeration<InetAddress> addrs = i.getInetAddresses();
while (addrs.hasMoreElements()) {
InetAddress a = addrs.nextElement();
if (a.isLoopbackAddress()) {
bindAddress = a.getHostAddress();
break;
}
}
}
} catch (SocketException se) {
LOG.warn("Couldn't find loopback interface: " + se.getMessage());
}
}
if (bindAddress == null) {
LOG.warn("Unable to determine loop back address, skipping test");
return;
}
final int PORT = PortAssignment.unique();
LOG.info("Using " + bindAddress + " as the bind address");
final String HOSTPORT = bindAddress + ":" + PORT;
LOG.info("Using " + HOSTPORT + " as the host/port");
File tmpDir = ClientBase.createTmpDir();
ClientBase.setupTestEnv();
ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
ServerCnxnFactory f = ServerCnxnFactory.createFactory(new InetSocketAddress(bindAddress, PORT), -1);
f.startup(zks);
LOG.info("starting up the the server, waiting");
Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
try {
zk.close();
} finally {
f.shutdown();
zks.shutdown();
Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT));
}
}
use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.
the class RecoveryTest method testRecovery.
/**
* Verify that if a server goes down that clients will reconnect
* automatically after the server is restarted. Note that this requires the
* server to restart within the connection timeout period.
*
* Also note that the client latches are used to eliminate any chance
* of spurrious connectionloss exceptions on the read ops. Specifically
* a sync operation will throw this exception if the server goes down
* (as recognized by the client) during the operation. If the operation
* occurs after the server is down, but before the client recognizes
* that the server is down (ping) then the op will throw connectionloss.
*/
@Test
public void testRecovery() throws Exception {
File tmpDir = ClientBase.createTmpDir();
ClientBase.setupTestEnv();
ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
int oldSnapCount = SyncRequestProcessor.getSnapCount();
SyncRequestProcessor.setSnapCount(1000);
try {
final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
f.startup(zks);
LOG.info("starting up the the server, waiting");
Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
startSignal = new CountDownLatch(1);
ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this);
startSignal.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
Assert.assertTrue("count == 0", startSignal.getCount() == 0);
String path;
LOG.info("starting creating nodes");
for (int i = 0; i < 10; i++) {
path = "/" + i;
zk.create(path, (path + "!").getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
for (int j = 0; j < 10; j++) {
String subpath = path + "/" + j;
zk.create(subpath, (subpath + "!").getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
for (int k = 0; k < 20; k++) {
String subsubpath = subpath + "/" + k;
zk.create(subsubpath, (subsubpath + "!").getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}
}
f.shutdown();
zks.shutdown();
Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT));
zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
f = ServerCnxnFactory.createFactory(PORT, -1);
startSignal = new CountDownLatch(1);
f.startup(zks);
Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
startSignal.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
Assert.assertTrue("count == 0", startSignal.getCount() == 0);
Stat stat = new Stat();
for (int i = 0; i < 10; i++) {
path = "/" + i;
LOG.info("Checking " + path);
Assert.assertEquals(new String(zk.getData(path, false, stat)), path + "!");
for (int j = 0; j < 10; j++) {
String subpath = path + "/" + j;
Assert.assertEquals(new String(zk.getData(subpath, false, stat)), subpath + "!");
for (int k = 0; k < 20; k++) {
String subsubpath = subpath + "/" + k;
Assert.assertEquals(new String(zk.getData(subsubpath, false, stat)), subsubpath + "!");
}
}
}
f.shutdown();
zks.shutdown();
Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT));
zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
f = ServerCnxnFactory.createFactory(PORT, -1);
startSignal = new CountDownLatch(1);
f.startup(zks);
Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
startSignal.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
Assert.assertTrue("count == 0", startSignal.getCount() == 0);
stat = new Stat();
LOG.info("Check 2");
for (int i = 0; i < 10; i++) {
path = "/" + i;
Assert.assertEquals(new String(zk.getData(path, false, stat)), path + "!");
for (int j = 0; j < 10; j++) {
String subpath = path + "/" + j;
Assert.assertEquals(new String(zk.getData(subpath, false, stat)), subpath + "!");
for (int k = 0; k < 20; k++) {
String subsubpath = subpath + "/" + k;
Assert.assertEquals(new String(zk.getData(subsubpath, false, stat)), subsubpath + "!");
}
}
}
zk.close();
f.shutdown();
zks.shutdown();
Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT));
} finally {
SyncRequestProcessor.setSnapCount(oldSnapCount);
}
}
use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.
the class RepeatStartupTest method testFail.
/**
* bring up 5 quorum peers and then shut them down
* and then bring one of the nodes as server
*
* @throws Exception might be thrown here
*/
@Test
public void testFail() throws Exception {
QuorumBase qb = new QuorumBase();
qb.setUp();
System.out.println("Comment: the servers are at " + qb.hostPort);
ZooKeeper zk = qb.createClient();
zk.create("/test", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.close();
QuorumBase.shutdown(qb.s1);
QuorumBase.shutdown(qb.s2);
QuorumBase.shutdown(qb.s3);
QuorumBase.shutdown(qb.s4);
QuorumBase.shutdown(qb.s5);
String hp = qb.hostPort.split(",")[0];
ZooKeeperServer zks = new ZooKeeperServer(qb.s1.getTxnFactory().getSnapDir(), qb.s1.getTxnFactory().getDataDir(), 3000);
final int PORT = Integer.parseInt(hp.split(":")[1]);
ServerCnxnFactory factory = ServerCnxnFactory.createFactory(PORT, -1);
factory.startup(zks);
System.out.println("Comment: starting factory");
Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp("127.0.0.1:" + PORT, QuorumTest.CONNECTION_TIMEOUT));
factory.shutdown();
zks.shutdown();
Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + PORT, QuorumTest.CONNECTION_TIMEOUT));
System.out.println("Comment: shutting down standalone");
}
Aggregations