use of org.apache.zookeeper.ZooKeeper in project bookkeeper by apache.
the class Utils method sync.
/**
* Sync zookeeper client on given <i>path</i>.
*
* @param zkc
* zookeeper client
* @param path
* path to sync
* @return zookeeper client after sync
* @throws IOException
*/
public static ZooKeeper sync(ZooKeeperClient zkc, String path) throws IOException {
ZooKeeper zk;
try {
zk = zkc.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new DLInterruptedException("Interrupted on checking if log " + path + " exists", e);
}
final CountDownLatch syncLatch = new CountDownLatch(1);
final AtomicInteger syncResult = new AtomicInteger(0);
zk.sync(path, new AsyncCallback.VoidCallback() {
@Override
public void processResult(int rc, String path, Object ctx) {
syncResult.set(rc);
syncLatch.countDown();
}
}, null);
try {
syncLatch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new DLInterruptedException("Interrupted on syncing zookeeper connection", e);
}
if (KeeperException.Code.OK.intValue() != syncResult.get()) {
throw new ZKException("Error syncing zookeeper connection ", KeeperException.Code.get(syncResult.get()));
}
return zk;
}
use of org.apache.zookeeper.ZooKeeper in project bookkeeper by apache.
the class LedgerMetadataCreationTest method testParentNodeDeletion.
@Test
public void testParentNodeDeletion() throws Exception {
/*
* run this testcase only for HierarchicalLedgerManager and
* LongHierarchicalLedgerManager, since we do recursive zNode deletes
* only for HierarchicalLedgerManager
*/
Assume.assumeTrue((baseClientConf.getLedgerManagerFactoryClass().equals(HierarchicalLedgerManagerFactory.class) || baseClientConf.getLedgerManagerFactoryClass().equals(LongHierarchicalLedgerManagerFactory.class)));
ZooKeeper zkc = new ZooKeeper(zkUtil.getZooKeeperConnectString(), 10000, null);
BookKeeper bookKeeper = new BookKeeper(baseClientConf);
bookKeeper.createLedgerAdv(1, 3, 2, 2, DigestType.CRC32, "passwd".getBytes(), null);
String ledgersRootPath = baseClientConf.getZkLedgersRootPath();
String parentZnodePath;
if (baseClientConf.getLedgerManagerFactoryClass().equals(HierarchicalLedgerManagerFactory.class)) {
/*
* in HierarchicalLedgerManager (ledgersRootPath)/00/0000/L0001
* would be the path of the znode for ledger - 1. So when ledger - 1
* is deleted, (ledgersRootPath)/00 should also be deleted since
* there are no other children znodes
*/
parentZnodePath = ledgersRootPath + "/00";
} else {
/*
* in LongHierarchicalLedgerManager
* (ledgersRootPath)/000/0000/0000/0000/L0001 would be the path of
* the znode for ledger - 1. So when ledger - 1 is deleted,
* (ledgersRootPath)/000 should also be deleted since there are no
* other children znodes
*/
parentZnodePath = ledgersRootPath + "/000";
}
assertTrue(parentZnodePath + " zNode should exist", null != zkc.exists(parentZnodePath, false));
bookKeeper.deleteLedger(1);
assertTrue(parentZnodePath + " zNode should not exist anymore", null == zkc.exists(parentZnodePath, false));
bookKeeper.close();
zkc.close();
}
use of org.apache.zookeeper.ZooKeeper in project bookkeeper by apache.
the class ZooKeeperUtil method expireSession.
public void expireSession(ZooKeeper zk) throws Exception {
long id = zk.getSessionId();
byte[] password = zk.getSessionPasswd();
ZooKeeperWatcherBase w = new ZooKeeperWatcherBase(10000);
ZooKeeper zk2 = new ZooKeeper(getZooKeeperConnectString(), zk.getSessionTimeout(), w, id, password);
w.waitForConnection();
zk2.close();
}
use of org.apache.zookeeper.ZooKeeper in project bookkeeper by apache.
the class TestReplicationWorker method testRWZKConnectionLost.
/**
* Test that the replication worker will not shutdown on a simple ZK disconnection.
*/
@Test
public void testRWZKConnectionLost() throws Exception {
try (ZooKeeper zk = ZooKeeperClient.newBuilder().connectString(zkUtil.getZooKeeperConnectString()).sessionTimeoutMs(10000).build()) {
ReplicationWorker rw = new ReplicationWorker(zk, baseConf);
rw.start();
for (int i = 0; i < 10; i++) {
if (rw.isRunning()) {
break;
}
Thread.sleep(1000);
}
assertTrue("Replication worker should be running", rw.isRunning());
stopZKCluster();
// Wait for disconnection to be picked up
for (int i = 0; i < 10; i++) {
if (!zk.getState().isConnected()) {
break;
}
Thread.sleep(1000);
}
assertFalse(zk.getState().isConnected());
startZKCluster();
assertTrue("Replication worker should still be running", rw.isRunning());
}
}
use of org.apache.zookeeper.ZooKeeper in project bookkeeper by apache.
the class TestReplicationWorker method test2RWsShouldCompeteForReplicationOf2FragmentsAndCompleteReplication.
/**
* Tests that replication worker1 should take one fragment replication and
* other replication worker also should compete for the replication.
*/
@Test
public void test2RWsShouldCompeteForReplicationOf2FragmentsAndCompleteReplication() throws Exception {
LedgerHandle lh = bkc.createLedger(2, 2, BookKeeper.DigestType.CRC32, TESTPASSWD);
for (int i = 0; i < 10; i++) {
lh.addEntry(data);
}
lh.close();
BookieSocketAddress replicaToKill = LedgerHandleAdapter.getLedgerMetadata(lh).getEnsembles().get(0L).get(0);
LOG.info("Killing Bookie", replicaToKill);
ServerConfiguration killedBookieConfig = killBookie(replicaToKill);
killAllBookies(lh, null);
// Starte RW1
BookieSocketAddress newBkAddr1 = startNewBookieAndReturnAddress();
LOG.info("New Bookie addr : {}", newBkAddr1);
ReplicationWorker rw1 = new ReplicationWorker(zkc, baseConf);
// Starte RW2
BookieSocketAddress newBkAddr2 = startNewBookieAndReturnAddress();
LOG.info("New Bookie addr : {}", newBkAddr2);
ZooKeeper zkc1 = ZooKeeperClient.newBuilder().connectString(zkUtil.getZooKeeperConnectString()).sessionTimeoutMs(10000).build();
ReplicationWorker rw2 = new ReplicationWorker(zkc1, baseConf);
rw1.start();
rw2.start();
try {
underReplicationManager.markLedgerUnderreplicated(lh.getId(), replicaToKill.toString());
int counter = 10;
while (counter-- > 0) {
assertTrue("Expecting that replication should not complete", ReplicationTestUtil.isLedgerInUnderReplication(zkc, lh.getId(), basePath));
Thread.sleep(100);
}
// restart killed bookie
bs.add(startBookie(killedBookieConfig));
bsConfs.add(killedBookieConfig);
while (ReplicationTestUtil.isLedgerInUnderReplication(zkc, lh.getId(), basePath)) {
Thread.sleep(100);
}
// Should be able to read the entries from 0-9
verifyRecoveredLedgers(lh, 0, 9);
} finally {
rw1.shutdown();
rw2.shutdown();
zkc1.close();
}
}
Aggregations