use of org.apache.distributedlog.zk.ZKTransaction in project bookkeeper by apache.
the class TestLedgerAllocatorPool method testConcurrentAllocation.
@Test(timeout = 60000)
public void testConcurrentAllocation() throws Exception {
final int numAllocators = 5;
String allocationPath = "/concurrentAllocation";
final LedgerAllocatorPool pool = new LedgerAllocatorPool(allocationPath, numAllocators, dlConf, zkc, bkc, allocationExecutor);
final ConcurrentMap<Long, LedgerHandle> allocatedLedgers = new ConcurrentHashMap<Long, LedgerHandle>();
final AtomicInteger numFailures = new AtomicInteger(0);
Thread[] allocationThreads = new Thread[numAllocators];
for (int i = 0; i < numAllocators; i++) {
final int tid = i;
allocationThreads[i] = new Thread() {
int numLedgers = 50;
@Override
public void run() {
try {
for (int i = 0; i < numLedgers; i++) {
pool.allocate();
ZKTransaction txn = newTxn();
LedgerHandle lh = Utils.ioResult(pool.tryObtain(txn, NULL_LISTENER));
Utils.ioResult(txn.execute());
lh.close();
allocatedLedgers.putIfAbsent(lh.getId(), lh);
logger.info("[thread {}] allocate {}th ledger {}", new Object[] { tid, i, lh.getId() });
}
} catch (Exception ioe) {
numFailures.incrementAndGet();
}
}
};
}
for (Thread t : allocationThreads) {
t.start();
}
for (Thread t : allocationThreads) {
t.join();
}
assertEquals(0, numFailures.get());
assertEquals(50 * numAllocators, allocatedLedgers.size());
Utils.close(pool);
}
use of org.apache.distributedlog.zk.ZKTransaction in project bookkeeper by apache.
the class TestLedgerAllocatorPool method testObtainWhenNoAllocator.
@Test(timeout = 60000)
public void testObtainWhenNoAllocator() throws Exception {
String allocationPath = "/obtainWhenNoAllocator";
LedgerAllocatorPool pool = new LedgerAllocatorPool(allocationPath, 0, dlConf, zkc, bkc, allocationExecutor);
ZKTransaction txn = newTxn();
try {
Utils.ioResult(pool.tryObtain(txn, NULL_LISTENER));
fail("Should fail obtain ledger handle if there is no allocator.");
} catch (SimpleLedgerAllocator.AllocationException ae) {
fail("Should fail obtain ledger handle if there is no allocator.");
} catch (IOException ioe) {
// expected.
}
Utils.close(pool);
}
use of org.apache.distributedlog.zk.ZKTransaction in project bookkeeper by apache.
the class TestLedgerAllocator method testCloseAllocatorDuringObtaining.
@Test(timeout = 60000)
public void testCloseAllocatorDuringObtaining() throws Exception {
String allocationPath = "/allocation2";
SimpleLedgerAllocator allocator = createAllocator(allocationPath);
allocator.allocate();
ZKTransaction txn = newTxn();
// close during obtaining ledger.
LedgerHandle lh = Utils.ioResult(allocator.tryObtain(txn, NULL_LISTENER));
Utils.close(allocator);
byte[] data = zkc.get().getData(allocationPath, false, null);
assertEquals((Long) lh.getId(), Long.valueOf(new String(data, UTF_8)));
// the ledger is not deleted
bkc.get().openLedger(lh.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes(UTF_8));
}
use of org.apache.distributedlog.zk.ZKTransaction in project bookkeeper by apache.
the class TestLedgerAllocator method testAllocatorWithoutEnoughBookies.
@Test(timeout = 60000)
public void testAllocatorWithoutEnoughBookies() throws Exception {
String allocationPath = "/allocator-without-enough-bookies";
DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
confLocal.addConfiguration(conf);
confLocal.setEnsembleSize(numBookies * 2);
confLocal.setWriteQuorumSize(numBookies * 2);
SimpleLedgerAllocator allocator1 = createAllocator(allocationPath, confLocal);
allocator1.allocate();
ZKTransaction txn1 = newTxn();
try {
Utils.ioResult(allocator1.tryObtain(txn1, NULL_LISTENER));
fail("Should fail allocating ledger if there aren't enough bookies");
} catch (AllocationException ioe) {
// expected
assertEquals(Phase.ERROR, ioe.getPhase());
}
byte[] data = zkc.get().getData(allocationPath, false, null);
assertEquals(0, data.length);
}
use of org.apache.distributedlog.zk.ZKTransaction in project bookkeeper by apache.
the class TestLedgerAllocator method testConcurrentAllocation.
@Test(timeout = 60000)
public void testConcurrentAllocation() throws Exception {
String allcationPath = "/" + runtime.getMethodName();
SimpleLedgerAllocator allocator = createAllocator(allcationPath);
allocator.allocate();
ZKTransaction txn1 = newTxn();
CompletableFuture<LedgerHandle> obtainFuture1 = allocator.tryObtain(txn1, NULL_LISTENER);
ZKTransaction txn2 = newTxn();
CompletableFuture<LedgerHandle> obtainFuture2 = allocator.tryObtain(txn2, NULL_LISTENER);
assertTrue(obtainFuture2.isDone());
assertTrue(obtainFuture2.isCompletedExceptionally());
try {
Utils.ioResult(obtainFuture2);
fail("Should fail the concurrent obtain since there is already a transaction obtaining the ledger handle");
} catch (SimpleLedgerAllocator.ConcurrentObtainException cbe) {
// expected
}
}
Aggregations