use of com.twitter.distributedlog.exceptions.ZKException in project distributedlog by twitter.
the class TestLedgerAllocator method testBadVersionOnTwoAllocators.
@Test(timeout = 60000)
public void testBadVersionOnTwoAllocators() throws Exception {
String allocationPath = "/allocation-bad-version";
zkc.get().create(allocationPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Stat stat = new Stat();
byte[] data = zkc.get().getData(allocationPath, false, stat);
Versioned<byte[]> allocationData = new Versioned<byte[]>(data, new ZkVersion(stat.getVersion()));
SimpleLedgerAllocator allocator1 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
SimpleLedgerAllocator allocator2 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
allocator1.allocate();
// wait until allocated
ZKTransaction txn1 = newTxn();
LedgerHandle lh = FutureUtils.result(allocator1.tryObtain(txn1, NULL_LISTENER));
allocator2.allocate();
ZKTransaction txn2 = newTxn();
try {
FutureUtils.result(allocator2.tryObtain(txn2, NULL_LISTENER));
fail("Should fail allocating on second allocator as allocator1 is starting allocating something.");
} catch (ZKException zke) {
assertEquals(KeeperException.Code.BADVERSION, zke.getKeeperExceptionCode());
}
FutureUtils.result(txn1.execute());
Utils.close(allocator1);
Utils.close(allocator2);
long eid = lh.addEntry("hello world".getBytes());
lh.close();
LedgerHandle readLh = bkc.get().openLedger(lh.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
Enumeration<LedgerEntry> entries = readLh.readEntries(eid, eid);
int i = 0;
while (entries.hasMoreElements()) {
LedgerEntry entry = entries.nextElement();
assertEquals("hello world", new String(entry.getEntry(), UTF_8));
++i;
}
assertEquals(1, i);
}
use of com.twitter.distributedlog.exceptions.ZKException in project distributedlog by twitter.
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);
FutureUtils.result(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);
try {
FutureUtils.result(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());
}
}
use of com.twitter.distributedlog.exceptions.ZKException in project distributedlog by twitter.
the class TestZKLogSegmentMetadataStore method testCreateDeleteLogSegmentFailure.
@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);
FutureUtils.result(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);
lsmStore.deleteLogSegment(createDeleteTxn, segment2);
lsmStore.createLogSegment(createDeleteTxn, segment3);
try {
FutureUtils.result(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));
}
use of com.twitter.distributedlog.exceptions.ZKException in project distributedlog by twitter.
the class TestDistributedLogTool method testToolCreateZkAclId.
@Test(timeout = 60000)
public void testToolCreateZkAclId() throws Exception {
createStream(defaultUri, "0", "CreateAclStream", defaultPrivilegedZkAclId);
try {
DistributedLogManager dlm = DLMTestUtil.createNewDLM("0CreateAclStream", conf, defaultUri);
DLMTestUtil.generateCompletedLogSegments(dlm, conf, 3, 1000);
dlm.close();
} catch (ZKException ex) {
assertEquals(KeeperException.Code.NOAUTH, ex.getKeeperExceptionCode());
}
}
use of com.twitter.distributedlog.exceptions.ZKException in project distributedlog by twitter.
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) {
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) {
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;
}
Aggregations