Source:
public void testConcurrentEntryLogCreations() throws Exception { ServerConfiguration conf = TestBKConfiguration.newServerConfiguration(); conf.setLedgerDirNames(ledgerDirs); conf.setEntryLogFilePreAllocationEnabled(true); conf.setEntryLogPerLedgerEnabled(true); LedgerDirsManager ledgerDirsManager = new LedgerDirsManager(conf, conf.getLedgerDirs(), new DiskChecker(conf.getDiskUsageThreshold(), conf.getDiskUsageWarnThreshold())); EntryLogger entryLogger = new EntryLogger(conf, ledgerDirsManager); EntryLogManagerForEntryLogPerLedger entrylogManager = (EntryLogManagerForEntryLogPerLedger) entryLogger.getEntryLogManager(); int numOfLedgers = 10; int numOfThreadsForSameLedger = 10; AtomicInteger createdEntryLogs = new AtomicInteger(0); CountDownLatch startLatch = new CountDownLatch(1); CountDownLatch createdLatch = new CountDownLatch(numOfLedgers * numOfThreadsForSameLedger); for (long i = 0; i < numOfLedgers; i++) { for (int j = 0; j < numOfThreadsForSameLedger; j++) { long ledgerId = i; new Thread() { @Override public void run() { try { startLatch.await(); entrylogManager.createNewLog(ledgerId); createdEntryLogs.incrementAndGet(); } catch (InterruptedException | IOException e) { LOG.error("Got exception while trying to createNewLog for Ledger: " + ledgerId, e); } finally { createdLatch.countDown(); } } }.start(); } } startLatch.countDown(); createdLatch.await(5, TimeUnit.SECONDS); Assert.assertEquals("Created EntryLogs", numOfLedgers * numOfThreadsForSameLedger, createdEntryLogs.get()); Assert.assertEquals("Active currentlogs size", numOfLedgers, entrylogManager.getCopyOfCurrentLogs().size()); Assert.assertEquals("Rotated entrylogs size", (numOfThreadsForSameLedger - 1) * numOfLedgers, entrylogManager.getRotatedLogChannels().size()); Assert.assertEquals("PreviousAllocatedEntryLogId", numOfLedgers * numOfThreadsForSameLedger, entryLogger.getPreviousAllocatedEntryLogId()); }

Target:
public void testConcurrentEntryLogCreations() throws Exception { ServerConfiguration conf = TestBKConfiguration.newServerConfiguration(); conf.setLedgerDirNames(ledgerDirs); conf.setEntryLogFilePreAllocationEnabled(true); conf.setEntryLogPerLedgerEnabled(true); LedgerDirsManager ledgerDirsManager = new LedgerDirsManager(conf, conf.getLedgerDirs(), new DiskChecker(conf.getDiskUsageThreshold(), conf.getDiskUsageWarnThreshold())); EntryLogger entryLogger = new EntryLogger(conf, ledgerDirsManager); EntryLogManagerForEntryLogPerLedger entrylogManager = (EntryLogManagerForEntryLogPerLedger) entryLogger.getEntryLogManager(); int numOfLedgers = 10; int numOfThreadsForSameLedger = 10; AtomicInteger createdEntryLogs = new AtomicInteger(0); CountDownLatch startLatch = new CountDownLatch(1); CountDownLatch createdLatch = new CountDownLatch(numOfLedgers * numOfThreadsForSameLedger); for (long i = 0; i < numOfLedgers; i++) { for (int j = 0; j < numOfThreadsForSameLedger; j++) { long ledgerId = i; new Thread(() -> { try { startLatch.await(); entrylogManager.createNewLog(ledgerId); createdEntryLogs.incrementAndGet(); } catch (InterruptedException | IOException e) { LOG.error("Got exception while trying to createNewLog for Ledger: " + ledgerId, e); } finally { createdLatch.countDown(); } }).start(); } } startLatch.countDown(); createdLatch.await(5, TimeUnit.SECONDS); Assert.assertEquals("Created EntryLogs", numOfLedgers * numOfThreadsForSameLedger, createdEntryLogs.get()); Assert.assertEquals("Active currentlogs size", numOfLedgers, entrylogManager.getCopyOfCurrentLogs().size()); Assert.assertEquals("Rotated entrylogs size", (numOfThreadsForSameLedger - 1) * numOfLedgers, entrylogManager.getRotatedLogChannels().size()); Assert.assertEquals("PreviousAllocatedEntryLogId", numOfLedgers * numOfThreadsForSameLedger, entryLogger.getPreviousAllocatedEntryLogId()); }