Example usage for org.apache.zookeeper KeeperException code

List of usage examples for org.apache.zookeeper KeeperException code

Introduction

In this page you can find the example usage for org.apache.zookeeper KeeperException code.

Prototype

Code code

To view the source code for org.apache.zookeeper KeeperException code.

Click Source Link

Usage

From source file:org.apache.distributedlog.impl.subscription.ZKSubscriptionStateStore.java

License:Apache License

CompletableFuture<DLSN> getLastCommitPositionFromZK() {
    final CompletableFuture<DLSN> result = new CompletableFuture<DLSN>();
    try {/*ww  w.j  a  v  a 2  s . co m*/
        logger.debug("Reading last commit position from path {}", zkPath);
        zooKeeperClient.get().getData(zkPath, false, new AsyncCallback.DataCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
                logger.debug("Read last commit position from path {}: rc = {}", zkPath, rc);
                if (KeeperException.Code.NONODE.intValue() == rc) {
                    result.complete(DLSN.NonInclusiveLowerBound);
                } else if (KeeperException.Code.OK.intValue() != rc) {
                    result.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc), path));
                } else {
                    try {
                        DLSN dlsn = DLSN.deserialize(new String(data, Charsets.UTF_8));
                        result.complete(dlsn);
                    } catch (Exception t) {
                        logger.warn("Invalid last commit position found from path {}", zkPath, t);
                        // invalid dlsn recorded in subscription state store
                        result.complete(DLSN.NonInclusiveLowerBound);
                    }
                }
            }
        }, null);
    } catch (ZooKeeperClient.ZooKeeperConnectionException zkce) {
        result.completeExceptionally(zkce);
    } catch (InterruptedException ie) {
        result.completeExceptionally(new DLInterruptedException("getLastCommitPosition was interrupted", ie));
    }
    return result;
}

From source file:org.apache.distributedlog.impl.TestZKLogSegmentMetadataStore.java

License:Apache License

@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 {/*from  www.  j  a  v  a2  s.co  m*/
        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());
    }
}

From source file:org.apache.distributedlog.impl.TestZKLogSegmentMetadataStore.java

License:Apache License

@Test(timeout = 60000)
public void testDeleteNonExistentLogSegment() throws Exception {
    LogSegmentMetadata segment = createLogSegment(1L);
    Transaction<Object> deleteTxn = lsmStore.transaction();
    lsmStore.deleteLogSegment(deleteTxn, segment, null);
    try {//from w  w w .j ava 2s  .  c  om
        Utils.ioResult(deleteTxn.execute());
        fail("Should fail deletion 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());
    }
}

From source file:org.apache.distributedlog.impl.TestZKLogSegmentMetadataStore.java

License:Apache License

@Test(timeout = 60000)
public void testUpdateNonExistentLogSegment() throws Exception {
    LogSegmentMetadata segment = createLogSegment(1L);
    Transaction<Object> updateTxn = lsmStore.transaction();
    lsmStore.updateLogSegment(updateTxn, segment);
    try {// w  w w . j ava 2s .c o m
        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());
    }
}

From source file:org.apache.distributedlog.impl.TestZKLogSegmentMetadataStore.java

License:Apache License

@Test(timeout = 60000)
public void testCreateDeleteLogSegmentFailure() throws Exception {
    LogSegmentMetadata segment1 = createLogSegment(1L);
    LogSegmentMetadata segment2 = createLogSegment(2L);
    LogSegmentMetadata segment3 = createLogSegment(3L);
    // create log segment 1
    Transaction<Object> createTxn = lsmStore.transaction();
    lsmStore.createLogSegment(createTxn, segment1, null);
    Utils.ioResult(createTxn.execute());
    // the log segment should be created
    assertNotNull("LogSegment " + segment1 + " should be created",
            zkc.get().exists(segment1.getZkPath(), false));
    // delete log segment 1 and delete log segment 2
    Transaction<Object> createDeleteTxn = lsmStore.transaction();
    lsmStore.deleteLogSegment(createDeleteTxn, segment1, null);
    lsmStore.deleteLogSegment(createDeleteTxn, segment2, null);
    lsmStore.createLogSegment(createDeleteTxn, segment3, null);
    try {/*from   w ww. j av a2  s  . c  om*/
        Utils.ioResult(createDeleteTxn.execute());
        fail("Should fail transaction if one operation failed");
    } catch (Throwable t) {
        assertTrue("Transaction is aborted", t instanceof ZKException);
        ZKException zke = (ZKException) t;
        assertEquals("Transaction is aborted", KeeperException.Code.NONODE, zke.getKeeperExceptionCode());
    }
    // segment 1 should not be deleted
    assertNotNull("LogSegment " + segment1 + " should not be deleted",
            zkc.get().exists(segment1.getZkPath(), false));
    // segment 3 should not be created
    assertNull("LogSegment " + segment3 + " should be created", zkc.get().exists(segment3.getZkPath(), false));
}

From source file:org.apache.distributedlog.impl.TestZKLogSegmentMetadataStore.java

License:Apache License

@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/*from w  w w .jav  a  2s  .  c  om*/
                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);
}

From source file:org.apache.distributedlog.impl.TestZKLogSegmentMetadataStore.java

License:Apache License

@Test(timeout = 60000)
public void testStoreMaxLogSegmentSequenceNumberOnNonExistentPath() throws Exception {
    Transaction<Object> updateTxn = lsmStore.transaction();
    Versioned<Long> value = new Versioned<Long>(999L, new LongVersion(10));
    final CompletableFuture<Version> result = new CompletableFuture<Version>();
    String nonExistentPath = rootZkPath + "/non-existent";
    LogMetadata metadata = mock(LogMetadata.class);
    when(metadata.getLogSegmentsPath()).thenReturn(nonExistentPath);
    lsmStore.storeMaxLogSegmentSequenceNumber(updateTxn, metadata, value,
            new Transaction.OpListener<Version>() {
                @Override/*from  ww  w. j  a v a  2 s.com*/
                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 path doesn't exist");
    } catch (ZKException zke) {
        assertEquals(KeeperException.Code.NONODE, zke.getKeeperExceptionCode());
    }
    try {
        Utils.ioResult(result);
        fail("Should fail on storing log segment sequence number if path doesn't exist");
    } catch (ZKException ke) {
        assertEquals(KeeperException.Code.NONODE, ke.getKeeperExceptionCode());
    }
}

From source file:org.apache.distributedlog.impl.TestZKLogSegmentMetadataStore.java

License:Apache License

@Test(timeout = 60000)
public void testStoreMaxTxnIdBadVersion() throws Exception {
    Transaction<Object> updateTxn = lsmStore.transaction();
    Versioned<Long> value = new Versioned<Long>(999L, new LongVersion(10));
    final CompletableFuture<Version> result = new CompletableFuture<Version>();
    LogMetadataForWriter metadata = mock(LogMetadataForWriter.class);
    when(metadata.getMaxTxIdPath()).thenReturn(rootZkPath);
    lsmStore.storeMaxTxnId(updateTxn, metadata, value, new Transaction.OpListener<Version>() {
        @Override/*from   www  . jav  a2  s . c o m*/
        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 record transaction id if providing bad version");
    } catch (ZKException zke) {
        assertEquals(KeeperException.Code.BADVERSION, zke.getKeeperExceptionCode());
    }
    try {
        Utils.ioResult(result);
        fail("Should fail on storing log record transaction id 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);
}

From source file:org.apache.distributedlog.impl.TestZKLogSegmentMetadataStore.java

License:Apache License

@Test(timeout = 60000)
public void testStoreMaxTxnIdOnNonExistentPath() throws Exception {
    Transaction<Object> updateTxn = lsmStore.transaction();
    Versioned<Long> value = new Versioned<Long>(999L, new LongVersion(10));
    final CompletableFuture<Version> result = new CompletableFuture<Version>();
    String nonExistentPath = rootZkPath + "/non-existent";
    LogMetadataForWriter metadata = mock(LogMetadataForWriter.class);
    when(metadata.getMaxTxIdPath()).thenReturn(nonExistentPath);
    lsmStore.storeMaxTxnId(updateTxn, metadata, value, new Transaction.OpListener<Version>() {
        @Override/*w ww  .j  a v a  2 s.co m*/
        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 record transaction id if path doesn't exist");
    } catch (ZKException zke) {
        assertEquals(KeeperException.Code.NONODE, zke.getKeeperExceptionCode());
    }
    try {
        Utils.ioResult(result);
        fail("Should fail on storing log record transaction id if path doesn't exist");
    } catch (ZKException ze) {
        assertEquals(KeeperException.Code.NONODE, ze.getKeeperExceptionCode());
    }
}

From source file:org.apache.distributedlog.impl.ZKLogMetadataStore.java

License:Apache License

@Override
public CompletableFuture<Iterator<String>> getLogs(String logNamePrefix) {
    final CompletableFuture<Iterator<String>> promise = new CompletableFuture<Iterator<String>>();
    final String nsRootPath;
    if (StringUtils.isEmpty(logNamePrefix)) {
        nsRootPath = namespace.getPath();
    } else {//w  w w . ja  va  2 s  .  co  m
        nsRootPath = namespace.getPath() + "/" + logNamePrefix;
    }
    try {
        final ZooKeeper zk = zkc.get();
        zk.sync(nsRootPath, new AsyncCallback.VoidCallback() {
            @Override
            public void processResult(int syncRc, String syncPath, Object ctx) {
                if (KeeperException.Code.OK.intValue() == syncRc) {
                    zk.getChildren(nsRootPath, false, new AsyncCallback.Children2Callback() {
                        @Override
                        public void processResult(int rc, String path, Object ctx, List<String> children,
                                Stat stat) {
                            if (KeeperException.Code.OK.intValue() == rc) {
                                List<String> results = Lists.newArrayListWithExpectedSize(children.size());
                                for (String child : children) {
                                    if (!isReservedStreamName(child)) {
                                        results.add(child);
                                    }
                                }
                                promise.complete(results.iterator());
                            } else if (KeeperException.Code.NONODE.intValue() == rc) {
                                List<String> streams = Lists.newLinkedList();
                                promise.complete(streams.iterator());
                            } else {
                                promise.completeExceptionally(new ZKException(
                                        "Error reading namespace " + nsRootPath, KeeperException.Code.get(rc)));
                            }
                        }
                    }, null);
                } else if (KeeperException.Code.NONODE.intValue() == syncRc) {
                    List<String> streams = Lists.newLinkedList();
                    promise.complete(streams.iterator());
                } else {
                    promise.completeExceptionally(new ZKException("Error reading namespace " + nsRootPath,
                            KeeperException.Code.get(syncRc)));
                }
            }
        }, null);
        zkc.get();
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        promise.completeExceptionally(e);
    } catch (InterruptedException e) {
        promise.completeExceptionally(e);
    }
    return promise;
}