Search in sources :

Example 6 with ZKException

use of org.apache.distributedlog.exceptions.ZKException in project bookkeeper by apache.

the class FederatedZKLogMetadataStore method createLogInNamespaceSync.

void createLogInNamespaceSync(URI uri, String logName) throws InterruptedException, IOException, KeeperException {
    Transaction txn = zkc.get().transaction();
    // we don't have the zk version yet. set it to 0 instead of -1, to prevent non CAS operation.
    int zkVersion = null == zkSubnamespacesVersion.get() ? 0 : zkSubnamespacesVersion.get();
    txn.setData(zkSubnamespacesPath, uri.getPath().getBytes(UTF_8), zkVersion);
    String logPath = uri.getPath() + "/" + logName;
    txn.create(logPath, new byte[0], zkc.getDefaultACL(), CreateMode.PERSISTENT);
    try {
        txn.commit();
        // if the transaction succeed, the zk version is advanced
        setZkSubnamespacesVersion(zkVersion + 1);
    } catch (KeeperException ke) {
        List<OpResult> opResults = ke.getResults();
        OpResult createResult = opResults.get(1);
        if (createResult instanceof OpResult.ErrorResult) {
            OpResult.ErrorResult errorResult = (OpResult.ErrorResult) createResult;
            if (Code.NODEEXISTS.intValue() == errorResult.getErr()) {
                throw new LogExistsException("Log " + logName + " already exists");
            }
        }
        OpResult setResult = opResults.get(0);
        if (setResult instanceof OpResult.ErrorResult) {
            OpResult.ErrorResult errorResult = (OpResult.ErrorResult) setResult;
            if (Code.BADVERSION.intValue() == errorResult.getErr()) {
                throw KeeperException.create(Code.BADVERSION);
            }
        }
        throw new ZKException("ZK exception in creating log " + logName + " in " + uri, ke);
    }
}
Also used : ZKException(org.apache.distributedlog.exceptions.ZKException) LogExistsException(org.apache.distributedlog.exceptions.LogExistsException) Transaction(org.apache.zookeeper.Transaction) OpResult(org.apache.zookeeper.OpResult) List(java.util.List) KeeperException(org.apache.zookeeper.KeeperException)

Example 7 with ZKException

use of org.apache.distributedlog.exceptions.ZKException 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 8 with ZKException

use of org.apache.distributedlog.exceptions.ZKException in project bookkeeper by apache.

the class TestZKLogSegmentMetadataStore method testCreateLogSegment.

@Test(timeout = 60000)
public void testCreateLogSegment() throws Exception {
    LogSegmentMetadata segment = createLogSegment(1L);
    Transaction<Object> createTxn = lsmStore.transaction();
    lsmStore.createLogSegment(createTxn, segment, null);
    Utils.ioResult(createTxn.execute());
    // the log segment should be created
    assertNotNull("LogSegment " + segment + " should be created", zkc.get().exists(segment.getZkPath(), false));
    LogSegmentMetadata segment2 = createLogSegment(1L);
    Transaction<Object> createTxn2 = lsmStore.transaction();
    lsmStore.createLogSegment(createTxn2, segment2, null);
    try {
        Utils.ioResult(createTxn2.execute());
        fail("Should fail if log segment exists");
    } catch (Throwable t) {
        // expected
        assertTrue("Should throw NodeExistsException if log segment exists", t instanceof ZKException);
        ZKException zke = (ZKException) t;
        assertEquals("Should throw NodeExistsException if log segment exists", KeeperException.Code.NODEEXISTS, zke.getKeeperExceptionCode());
    }
}
Also used : ZKException(org.apache.distributedlog.exceptions.ZKException) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 9 with ZKException

use of org.apache.distributedlog.exceptions.ZKException in project bookkeeper by apache.

the class TestZKLogSegmentMetadataStore method testUpdateNonExistentLogSegment.

@Test(timeout = 60000)
public void testUpdateNonExistentLogSegment() throws Exception {
    LogSegmentMetadata segment = createLogSegment(1L);
    Transaction<Object> updateTxn = lsmStore.transaction();
    lsmStore.updateLogSegment(updateTxn, segment);
    try {
        Utils.ioResult(updateTxn.execute());
        fail("Should fail update if log segment doesn't exist");
    } catch (Throwable t) {
        assertTrue("Should throw NoNodeException if log segment doesn't exist", t instanceof ZKException);
        ZKException zke = (ZKException) t;
        assertEquals("Should throw NoNodeException if log segment doesn't exist", KeeperException.Code.NONODE, zke.getKeeperExceptionCode());
    }
}
Also used : ZKException(org.apache.distributedlog.exceptions.ZKException) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 10 with ZKException

use of org.apache.distributedlog.exceptions.ZKException in project bookkeeper by apache.

the class TestZKLogSegmentMetadataStore method testStoreMaxLogSegmentSequenceNumberBadVersion.

@Test(timeout = 60000)
public void testStoreMaxLogSegmentSequenceNumberBadVersion() throws Exception {
    Transaction<Object> updateTxn = lsmStore.transaction();
    Versioned<Long> value = new Versioned<Long>(999L, new LongVersion(10));
    final CompletableFuture<Version> result = new CompletableFuture<Version>();
    LogMetadata metadata = mock(LogMetadata.class);
    when(metadata.getLogSegmentsPath()).thenReturn(rootZkPath);
    lsmStore.storeMaxLogSegmentSequenceNumber(updateTxn, metadata, value, new Transaction.OpListener<Version>() {

        @Override
        public void onCommit(Version r) {
            result.complete(r);
        }

        @Override
        public void onAbort(Throwable t) {
            result.completeExceptionally(t);
        }
    });
    try {
        Utils.ioResult(updateTxn.execute());
        fail("Should fail on storing log segment sequence number if providing bad version");
    } catch (ZKException zke) {
        assertEquals(KeeperException.Code.BADVERSION, zke.getKeeperExceptionCode());
    }
    try {
        Utils.ioResult(result);
        fail("Should fail on storing log segment sequence number if providing bad version");
    } catch (ZKException ze) {
        assertEquals(KeeperException.Code.BADVERSION, ze.getKeeperExceptionCode());
    }
    Stat stat = new Stat();
    byte[] data = zkc.get().getData(rootZkPath, false, stat);
    assertEquals(0, stat.getVersion());
    assertEquals(0, data.length);
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) CompletableFuture(java.util.concurrent.CompletableFuture) ZKException(org.apache.distributedlog.exceptions.ZKException) Stat(org.apache.zookeeper.data.Stat) Transaction(org.apache.distributedlog.util.Transaction) LongVersion(org.apache.bookkeeper.versioning.LongVersion) Version(org.apache.bookkeeper.versioning.Version) LongVersion(org.apache.bookkeeper.versioning.LongVersion) LogMetadata(org.apache.distributedlog.metadata.LogMetadata) Test(org.junit.Test)

Aggregations

ZKException (org.apache.distributedlog.exceptions.ZKException)31 Test (org.junit.Test)15 CompletableFuture (java.util.concurrent.CompletableFuture)8 LongVersion (org.apache.bookkeeper.versioning.LongVersion)8 Versioned (org.apache.bookkeeper.versioning.Versioned)8 Stat (org.apache.zookeeper.data.Stat)7 List (java.util.List)6 ZooKeeperClient (org.apache.distributedlog.ZooKeeperClient)5 DLInterruptedException (org.apache.distributedlog.exceptions.DLInterruptedException)5 ZooKeeper (org.apache.zookeeper.ZooKeeper)5 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)4 Version (org.apache.bookkeeper.versioning.Version)4 LogSegmentMetadata (org.apache.distributedlog.LogSegmentMetadata)4 ZKTransaction (org.apache.distributedlog.zk.ZKTransaction)4 KeeperException (org.apache.zookeeper.KeeperException)4 ZooKeeperConnectionException (org.apache.distributedlog.ZooKeeperClient.ZooKeeperConnectionException)3 LogExistsException (org.apache.distributedlog.exceptions.LogExistsException)3 LogNotFoundException (org.apache.distributedlog.exceptions.LogNotFoundException)3 Transaction (org.apache.distributedlog.util.Transaction)3 AsyncCallback (org.apache.zookeeper.AsyncCallback)3