use of com.twitter.distributedlog.zk.ZKTransaction in project distributedlog by twitter.
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 {
FutureUtils.result(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 com.twitter.distributedlog.zk.ZKTransaction in project distributedlog by twitter.
the class TestLedgerAllocatorPool method testRescueAllocators.
@Test(timeout = 60000)
public void testRescueAllocators() throws Exception {
String allocationPath = "/rescueAllocators";
int numAllocators = 3;
LedgerAllocatorPool pool = new LedgerAllocatorPool(allocationPath, numAllocators, dlConf, zkc, bkc, allocationExecutor);
List<ZKTransaction> pendingTxns = Lists.newArrayListWithExpectedSize(numAllocators);
List<String> allocatePaths = Lists.newArrayListWithExpectedSize(numAllocators);
for (int i = 0; i < numAllocators; i++) {
ZKTransaction txn = newTxn();
pool.allocate();
LedgerHandle lh = FutureUtils.result(pool.tryObtain(txn, NULL_LISTENER));
// get the corresponding ledger allocator
SimpleLedgerAllocator sla = pool.getLedgerAllocator(lh);
String slaPath = sla.allocatePath;
logger.info("Allocated ledger {} from path {}", lh.getId(), slaPath);
pendingTxns.add(txn);
allocatePaths.add(slaPath);
}
for (int i = 0; i < numAllocators; i++) {
ZKTransaction txn = pendingTxns.get(i);
String slaPath = allocatePaths.get(i);
// execute the transaction to confirm/abort obtain
FutureUtils.result(txn.execute());
// introduce error to individual ledger allocator
byte[] data = zkc.get().getData(slaPath, false, new Stat());
zkc.get().setData(slaPath, data, -1);
}
int numSuccess = 0;
Set<String> allocatedPathSet = new HashSet<String>();
while (numSuccess < 2 * numAllocators) {
try {
pool.allocate();
ZKTransaction txn = newTxn();
LedgerHandle lh = FutureUtils.result(pool.tryObtain(txn, NULL_LISTENER));
// get the corresponding ledger allocator
SimpleLedgerAllocator sla = pool.getLedgerAllocator(lh);
String slaPath = sla.allocatePath;
logger.info("Allocated ledger {} from path {}", lh.getId(), slaPath);
allocatedPathSet.add(slaPath);
FutureUtils.result(txn.execute());
++numSuccess;
} catch (IOException ioe) {
// continue
}
}
assertEquals(2 * numAllocators, numSuccess);
assertEquals(numAllocators, allocatedPathSet.size());
Utils.close(pool);
}
use of com.twitter.distributedlog.zk.ZKTransaction in project distributedlog by twitter.
the class TestLedgerAllocatorPool method testAllocateMultipleLedgers.
@Test(timeout = 60000)
public void testAllocateMultipleLedgers() throws Exception {
String allocationPath = "/" + runtime.getMethodName();
int numAllocators = 5;
final LedgerAllocatorPool pool = new LedgerAllocatorPool(allocationPath, numAllocators, dlConf, zkc, bkc, allocationExecutor);
int numLedgers = 20;
Set<LedgerHandle> allocatedLedgers = new HashSet<LedgerHandle>();
for (int i = 0; i < numLedgers; i++) {
pool.allocate();
ZKTransaction txn = newTxn();
LedgerHandle lh = FutureUtils.result(pool.tryObtain(txn, NULL_LISTENER));
FutureUtils.result(txn.execute());
allocatedLedgers.add(lh);
}
assertEquals(numLedgers, allocatedLedgers.size());
}
use of com.twitter.distributedlog.zk.ZKTransaction in project distributedlog by twitter.
the class TestLedgerAllocatorPool method testConcurrentAllocation.
@Test
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 = FutureUtils.result(pool.tryObtain(txn, NULL_LISTENER));
FutureUtils.result(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 com.twitter.distributedlog.zk.ZKTransaction in project distributedlog by twitter.
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 {
FutureUtils.result(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);
}
Aggregations