Search in sources :

Example 51 with ZooKeeper

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;
}
Also used : ZKException(org.apache.distributedlog.exceptions.ZKException) ZooKeeper(org.apache.zookeeper.ZooKeeper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncCallback(org.apache.zookeeper.AsyncCallback) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException) CountDownLatch(java.util.concurrent.CountDownLatch) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException)

Example 52 with ZooKeeper

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();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) BookKeeper(org.apache.bookkeeper.client.BookKeeper) Test(org.junit.Test)

Example 53 with ZooKeeper

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();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ZooKeeperWatcherBase(org.apache.bookkeeper.zookeeper.ZooKeeperWatcherBase)

Example 54 with ZooKeeper

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());
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) Test(org.junit.Test)

Example 55 with ZooKeeper

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();
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) Test(org.junit.Test)

Aggregations

ZooKeeper (org.apache.zookeeper.ZooKeeper)605 Test (org.junit.jupiter.api.Test)190 KeeperException (org.apache.zookeeper.KeeperException)153 Test (org.junit.Test)100 Stat (org.apache.zookeeper.data.Stat)86 IOException (java.io.IOException)83 CountDownLatch (java.util.concurrent.CountDownLatch)80 WatchedEvent (org.apache.zookeeper.WatchedEvent)70 Watcher (org.apache.zookeeper.Watcher)59 ArrayList (java.util.ArrayList)47 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)41 TestableZooKeeper (org.apache.zookeeper.TestableZooKeeper)38 Timeout (org.junit.jupiter.api.Timeout)32 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)31 File (java.io.File)28 HashMap (java.util.HashMap)26 CompletableFuture (java.util.concurrent.CompletableFuture)22 Test (org.testng.annotations.Test)21 AsyncCallback (org.apache.zookeeper.AsyncCallback)19 ACL (org.apache.zookeeper.data.ACL)19