LedgerAllocatorPool implements LedgerAllocator { @Override public void allocate() throws IOException { SimpleLedgerAllocator allocator; synchronized (this) { if (pendingList.isEmpty()) { throw new IOException("No ledger allocator available under " + poolPath + "."); } else { allocator = pendingList.removeFirst(); } } boolean success = false; try { allocator.allocate(); synchronized (this) { allocatingList.addLast(allocator); } success = true; } finally { if (!success) { rescueAllocator(allocator); } } } LedgerAllocatorPool(String poolPath, int corePoolSize,
                               DistributedLogConfiguration conf,
                               ZooKeeperClient zkc,
                               BookKeeperClient bkc,
                               ScheduledExecutorService scheduledExecutorService); @Override void start(); @VisibleForTesting synchronized int obtainMapSize(); @Override void allocate(); @Override Future<LedgerHandle> tryObtain(final Transaction<Object> txn,
                                          final Transaction.OpListener<LedgerHandle> listener); @Override Future<Void> asyncClose(); @Override Future<Void> delete();  }
@Test(timeout = 60000) public void testAllocateWhenNoAllocator() throws Exception { String allocationPath = "/allocateWhenNoAllocator"; LedgerAllocatorPool pool = new LedgerAllocatorPool(allocationPath, 0, dlConf, zkc, bkc, allocationExecutor); try { pool.allocate(); fail("Should fail to allocate ledger if there isn't allocator."); } catch (SimpleLedgerAllocator.AllocationException ae) { fail("Should fail to allocate ledger if there isn't allocator."); } catch (IOException ioe) { } Utils.close(pool); }