use of org.apache.zookeeper.test.ClientBase.CountdownWatcher in project zookeeper by apache.
the class FollowerResyncConcurrencyTest method followerResyncCrashTest.
public void followerResyncCrashTest(boolean useTxnLogResync) throws Throwable {
final Semaphore sem = new Semaphore(0);
QuorumUtil qu = new QuorumUtil(1);
qu.startAll();
CountdownWatcher watcher1 = new CountdownWatcher();
CountdownWatcher watcher2 = new CountdownWatcher();
CountdownWatcher watcher3 = new CountdownWatcher();
int index = 1;
while (qu.getPeer(index).peer.leader == null) {
index++;
}
Leader leader = qu.getPeer(index).peer.leader;
assertNotNull(leader);
if (useTxnLogResync) {
// Set the factor to high value so that this test case always
// resync using txnlog
qu.getPeer(index).peer.getActiveServer().getZKDatabase().setSnapshotSizeFactor(1000);
} else {
// Disable sending DIFF using txnlog, so that this test still
// testing the ZOOKEEPER-962 bug
qu.getPeer(index).peer.getActiveServer().getZKDatabase().setSnapshotSizeFactor(-1);
}
/* Reusing the index variable to select a follower to connect to */
index = (index == 1) ? 2 : 1;
LOG.info("Connecting to follower: {}", index);
qu.shutdown(index);
final ZooKeeper zk3 = createClient(qu.getPeer(3).peer.getClientPort(), watcher3);
LOG.info("zk3 has session id 0x{}", Long.toHexString(zk3.getSessionId()));
zk3.create("/mybar", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
qu.restart(index);
final ZooKeeper zk1 = createClient(qu.getPeer(index).peer.getClientPort(), watcher1);
LOG.info("zk1 has session id 0x{}", Long.toHexString(zk1.getSessionId()));
final ZooKeeper zk2 = createClient(qu.getPeer(index).peer.getClientPort(), watcher2);
LOG.info("zk2 has session id 0x{}", Long.toHexString(zk2.getSessionId()));
zk1.create("/first", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// Prepare a thread that will create znodes.
Thread mytestfooThread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 3000; i++) {
// Here we create 3000 znodes
zk3.create("/mytestfoo", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL, (rc, path, ctx, name) -> {
pending.decrementAndGet();
counter.incrementAndGet();
if (rc != 0) {
errors.incrementAndGet();
}
if (counter.get() == 16200) {
sem.release();
}
}, null);
pending.incrementAndGet();
if (i % 10 == 0) {
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
}
}
});
// initial data is written.
for (int i = 0; i < 13000; i++) {
// Here we create 13000 znodes
zk3.create("/mybar", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL, (rc, path, ctx, name) -> {
pending.decrementAndGet();
counter.incrementAndGet();
if (rc != 0) {
errors.incrementAndGet();
}
if (counter.get() == 16200) {
sem.release();
}
}, null);
pending.incrementAndGet();
if (i == 5000) {
qu.shutdown(index);
LOG.info("Shutting down s1");
}
if (i == 12000) {
// Start the prepared thread so that it is writing znodes while
// the follower is restarting. On the first restart, the follow
// should use txnlog to catchup. For subsequent restart, the
// follower should use a diff to catchup.
mytestfooThread.start();
LOG.info("Restarting follower: {}", index);
qu.restart(index);
Thread.sleep(300);
LOG.info("Shutdown follower: {}", index);
qu.shutdown(index);
Thread.sleep(300);
LOG.info("Restarting follower: {}", index);
qu.restart(index);
LOG.info("Setting up server: {}", index);
}
if ((i % 1000) == 0) {
Thread.sleep(1000);
}
if (i % 50 == 0) {
zk2.create("/newbaz", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL, (rc, path, ctx, name) -> {
pending.decrementAndGet();
counter.incrementAndGet();
if (rc != 0) {
errors.incrementAndGet();
}
if (counter.get() == 16200) {
sem.release();
}
}, null);
pending.incrementAndGet();
}
}
// Wait until all updates return
if (!sem.tryAcquire(ClientBase.CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)) {
LOG.warn("Did not aquire semaphore fast enough");
}
mytestfooThread.join(ClientBase.CONNECTION_TIMEOUT);
if (mytestfooThread.isAlive()) {
LOG.error("mytestfooThread is still alive");
}
assertTrue(waitForPendingRequests(60));
assertTrue(waitForSync(qu, index, 10));
verifyState(qu, index, leader);
zk1.close();
zk2.close();
zk3.close();
qu.shutdownAll();
}
use of org.apache.zookeeper.test.ClientBase.CountdownWatcher in project zookeeper by apache.
the class DuplicateLocalSessionUpgradeTest method testLocalSessionUpgrade.
private void testLocalSessionUpgrade(boolean testLeader) throws Exception {
int leaderIdx = qb.getLeaderIndex();
assertFalse(leaderIdx == -1, "No leader in quorum?");
int followerIdx = (leaderIdx + 1) % 5;
int testPeerIdx = testLeader ? leaderIdx : followerIdx;
String[] hostPorts = qb.hostPort.split(",");
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = qb.createClient(watcher, hostPorts[testPeerIdx], CONNECTION_TIMEOUT);
watcher.waitForConnected(CONNECTION_TIMEOUT);
final String firstPath = "/first";
final String secondPath = "/ephemeral";
// Just create some node so that we know the current zxid
zk.create(firstPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// Now, try an ephemeral node. This will trigger session upgrade
// so there will be createSession request inject into the pipeline
// prior to this request
zk.create(secondPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
Stat firstStat = zk.exists(firstPath, null);
assertNotNull(firstStat);
Stat secondStat = zk.exists(secondPath, null);
assertNotNull(secondStat);
long zxidDiff = secondStat.getCzxid() - firstStat.getCzxid();
// If there is only one createSession request in between, zxid diff
// will be exactly 2. The alternative way of checking is to actually
// read txnlog but this should be sufficient
assertEquals(2L, zxidDiff);
}
use of org.apache.zookeeper.test.ClientBase.CountdownWatcher in project zookeeper by apache.
the class QuorumDigestAuthTest method testValidCredentials.
/**
* Test to verify that server is able to start with valid credentials
*/
@Test
@Timeout(value = 30)
public void testValidCredentials() throws Exception {
Map<String, String> authConfigs = new HashMap<String, String>();
authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true");
authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true");
authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true");
String connectStr = startQuorum(3, authConfigs, 3);
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher);
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
for (int i = 0; i < 10; i++) {
zk.create("/" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
zk.close();
}
use of org.apache.zookeeper.test.ClientBase.CountdownWatcher in project zookeeper by apache.
the class QuorumDigestAuthTest method testValidCredentialsWithMultiAddresses.
/**
* Test to verify that server is able to start with valid credentials
* when using multiple Quorum / Election addresses
*/
@Test
@Timeout(value = 30)
public void testValidCredentialsWithMultiAddresses() throws Exception {
Map<String, String> authConfigs = new HashMap<String, String>();
authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true");
authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true");
authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true");
String connectStr = startMultiAddressQuorum(3, authConfigs, 3);
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher);
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
for (int i = 0; i < 10; i++) {
zk.create("/" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
zk.close();
}
use of org.apache.zookeeper.test.ClientBase.CountdownWatcher in project zookeeper by apache.
the class QuorumKerberosHostBasedAuthTest method testConnectBadServer.
/**
* Test to verify that the bad server connection to the quorum should be rejected.
*/
@Test
@Timeout(value = 120)
public void testConnectBadServer() throws Exception {
String serverPrincipal = hostServerPrincipal.substring(0, hostServerPrincipal.lastIndexOf("@"));
Map<String, String> authConfigs = new HashMap<String, String>();
authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true");
authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true");
authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true");
authConfigs.put(QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL, serverPrincipal);
String connectStr = startQuorum(3, authConfigs, 3);
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher);
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
for (int i = 0; i < 10; i++) {
zk.create("/" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
zk.close();
String quorumCfgSection = mt.get(0).getQuorumCfgSection();
StringBuilder sb = new StringBuilder();
sb.append(quorumCfgSection);
int myid = mt.size() + 1;
final int clientPort = PortAssignment.unique();
String server = String.format("server.%d=localhost:%d:%d:participant", myid, PortAssignment.unique(), PortAssignment.unique());
sb.append(server + "\n");
quorumCfgSection = sb.toString();
authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_LOGIN_CONTEXT, "QuorumLearnerMyHost");
MainThread badServer = new MainThread(myid, clientPort, quorumCfgSection, authConfigs);
badServer.start();
watcher = new CountdownWatcher();
connectStr = "127.0.0.1:" + clientPort;
zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher);
try {
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT / 3);
fail("Must throw exception as the myHost is not an authorized one!");
} catch (TimeoutException e) {
// expected
} finally {
zk.close();
badServer.shutdown();
badServer.deleteBaseDir();
}
}
Aggregations