Example usage for java.util.concurrent LinkedBlockingQueue LinkedBlockingQueue

List of usage examples for java.util.concurrent LinkedBlockingQueue LinkedBlockingQueue

Introduction

In this page you can find the example usage for java.util.concurrent LinkedBlockingQueue LinkedBlockingQueue.

Prototype

public LinkedBlockingQueue() 

Source Link

Document

Creates a LinkedBlockingQueue with a capacity of Integer#MAX_VALUE .

Usage

From source file:com.pinterest.teletraan.ConfigHelper.java

public static TeletraanServiceContext setupContext(TeletraanServiceConfiguration configuration)
        throws Exception {
    TeletraanServiceContext context = new TeletraanServiceContext();

    BasicDataSource dataSource = configuration.getDataSourceFactory().build();
    context.setDataSource(dataSource);// w  w  w.  j ava 2s  .c  om

    context.setUserRolesDAO(new DBUserRolesDAOImpl(dataSource));
    context.setGroupRolesDAO(new DBGroupRolesDAOImpl(dataSource));
    context.setTokenRolesDAO(new DBTokenRolesDAOImpl(dataSource));

    context.setBuildDAO(new DBBuildDAOImpl(dataSource));
    context.setEnvironDAO(new DBEnvironDAOImpl(dataSource));
    context.setDeployDAO(new DBDeployDAOImpl(dataSource));
    context.setHotfixDAO(new DBHotfixDAOImpl(dataSource));
    context.setRatingDAO(new DBRatingsDAOImpl(dataSource));
    context.setPromoteDAO(new DBPromoteDAOImpl(dataSource));

    context.setDataDAO(new DBDataDAOImpl(dataSource));
    context.setUtilDAO(new DBUtilDAOImpl(dataSource));

    context.setConfigHistoryDAO(new DBConfigHistoryDAOImpl(dataSource));
    context.setHostDAO(new DBHostDAOImpl(dataSource));
    context.setGroupDAO(new DBGroupDAOImpl(dataSource));
    context.setAgentDAO(new DBAgentDAOImpl(dataSource));
    context.setAgentErrorDAO(new DBAgentErrorDAOImpl(dataSource));

    context.setClusterDAO(new DBClusterDAOImpl(dataSource));
    context.setBaseImageDAO(new DBBaseImageDAOImpl(dataSource));
    context.setHostTypeDAO(new DBHostTypeDAOImpl(dataSource));
    context.setSecurityZoneDAO(new DBSecurityZoneDAOImpl(dataSource));
    context.setPlacementDAO(new DBPlacementDAOImpl(dataSource));

    // TODO Arcee specific
    context.setAlarmDAO(new DBAlarmDAOImpl(dataSource));
    context.setImageDAO(new DBImageDAOImpl(dataSource));
    context.setGroupInfoDAO(new DBGroupInfoDAOImpl(dataSource));
    context.setHealthCheckDAO(new DBHealthCheckDAOImpl(dataSource));
    context.setHealthCheckErrorDAO(new DBHealthCheckErrorDAOImpl(dataSource));
    context.setnewInstanceReportDAO(new DBNewInstanceReportDAOImpl(dataSource));
    context.setAsgLifecycleEventDAO(new DBAsgLifecycleEventDAOImpl(dataSource));
    context.setManagingGroupDAO(new DBManaginGroupDAOImpl(dataSource));

    // Inject proper implemetation based on config
    context.setAuthorizer(configuration.getAuthorizationFactory().create(context));
    context.setSourceControlManager(configuration.getSourceControlFactory().create());
    context.setChatManager(configuration.getChatFactory().create());
    context.setMailManager(configuration.getEmailFactory().createMailManager());
    context.setHostGroupDAO(configuration.getHostGroupFactory().createHostGroupDAO());
    context.setMetricSource(configuration.getMetricSourceFactory().create());

    EventSenderFactory eventSenderFactory = configuration.getEventSenderFactory();
    if (eventSenderFactory != null) {
        context.setEventSender(eventSenderFactory.createEventSender());
    } else {
        context.setEventSender(new DefaultEventSender());
    }

    // AWS specific DAOs
    AWSFactory awsFactory = configuration.getAwsFactory();
    if (awsFactory != null) {
        context.setAutoScaleGroupManager(awsFactory.buildAwsAutoScalingManager());
        context.setAlarmManager(awsFactory.buildAwsAlarmManager());
        AmazonEC2Client ec2Client = awsFactory.buildEC2Client();
        context.setEc2Client(ec2Client);
        // TODO we should just use AwsConfigManager and get rid of the above 3
        context.setAwsConfigManager(awsFactory.buildAwsConfigManager());
        // TODO rename to manager
        context.setHostInfoDAO(new EC2HostInfoDAOImpl(ec2Client));
        context.setReservedInstanceInfoDAO(new ReservedInstanceFetcher(ec2Client));
        context.setClusterManager(new AwsVmManager(context.getAwsConfigManager()));
    } else {
        // TODO make sure if aws is null, all the workers related to aws still works
        context.setHostInfoDAO(new DefaultHostInfoDAOImpl());
        context.setClusterManager(new DefaultClusterManager());
    }

    /**
     Lastly, let us create the in-process background job executor, all transient, long
     running background jobs can be handled by this executor
     Currently we hard coded the parameters as:
            
     corePoolSize - the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set
     maximumPoolSize - the maximum number of threads to allow in the pool
     keepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.
     unit - the time unit for the keepAliveTime argument
     workQueue - the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
     */
    // TODO make the thread configrable
    ExecutorService jobPool = new ThreadPoolExecutor(1, 10, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
    context.setJobPool(jobPool);

    context.setDeployBoardUrlPrefix(configuration.getSystemFactory().getDashboardUrl());
    context.setChangeFeedUrl(configuration.getSystemFactory().getChangeFeedUrl());
    context.setQuboleAuthentication(configuration.getSystemFactory().getQuboleAuthentication());

    return context;
}

From source file:org.bpmscript.process.memory.MemoryBpmScriptEngine.java

/**
 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
 *///from  w ww. jav  a 2  s. c om
public void afterPropertiesSet() throws Exception {

    Assert.notNull(folder, "The folder property must be set");
    Assert.notNull(bus, "The bus property must be set");

    BlockingQueue<ILibraryToFile> libraryAssociationQueue = new LinkedBlockingQueue<ILibraryToFile>();
    BlockingQueue<ILibraryToFile> libraryChangeQueue = new LinkedBlockingQueue<ILibraryToFile>();

    versionedDefinitionManager = new MemoryVersionedDefinitionManager();
    bpmScriptManager = new MemoryBpmScriptManager();
    versionedDefinitionManager.setDefinitionManager(new MemoryDefinitionManager());
    InternalScriptChannel scriptChannel = new InternalScriptChannel();
    scriptChannel.setReplyTo(bus.getName() + ":" + bpmScriptNextChannelName);
    scriptChannel.setSender(bus);
    scriptChannel.setTimeoutAddress(bus.getName() + ":" + timeoutChannelName);
    scriptChannel.setErrorAddress(bus.getName() + ":" + errorChannelName);
    scriptChannel.setDefinitionConfigurationLookup(
            new ApplicationContextDefinitionConfigurationLookup(applicationContext));

    // javascript executor
    JavascriptProcessExecutor javascriptExecutor = new JavascriptProcessExecutor();
    JavascriptSerializingContinuationService javascriptContinuationService = new JavascriptSerializingContinuationService();
    javascriptContinuationService.setContinuationStore(bpmScriptManager);
    javascriptExecutor.setContinuationService(javascriptContinuationService);
    MemoryScopeStore scopeStore = new MemoryScopeStore();
    ScopeService scopeService = new ScopeService();
    scopeService.setLibraryAssociationQueue(libraryAssociationQueue);
    scopeService.setScopeStore(scopeStore);

    javascriptExecutor.setScopeService(scopeService);
    javascriptExecutor.setChannel(scriptChannel);

    // java executor
    JavaProcessExecutor javaExecutor = new JavaProcessExecutor();
    JavaSerializingContinuationService javaContinuationService = new JavaSerializingContinuationService();
    javaContinuationService.setContinuationJournal(bpmScriptManager);
    javaExecutor.setContinuationService(javaContinuationService);
    javaExecutor.setChannel(scriptChannel);

    engine = new BpmScriptEngine();
    engine.setInstanceManager(bpmScriptManager);
    ProcessExecutorLookup processExecutorLookup = new ProcessExecutorLookup();
    processExecutorLookup.addProcessExecutor(IJavascriptProcessDefinition.DEFINITION_TYPE_JAVASCRIPT,
            javascriptExecutor);
    processExecutorLookup.addProcessExecutor(IJavaProcessDefinition.DEFINITION_TYPE_JAVA, javaExecutor);
    engine.setProcessExecutorLookup(processExecutorLookup);
    engine.setContinuationJournal(bpmScriptManager);
    engine.setVersionedDefinitionManager(versionedDefinitionManager);

    MemorySyncChannel syncChannel = new MemorySyncChannel();

    bpmScriptFacade = new BpmScriptFacade();
    bpmScriptFacade.setAddress(bus.getName() + ":" + bpmScriptFirstChannelName);
    bpmScriptFacade.setReplyTo(bus.getName() + ":" + syncChannelName);
    bpmScriptFacade.setSender(bus);
    bpmScriptFacade.setSyncChannel(syncChannel);

    conversationCorrelator = new ConversationCorrelator();
    conversationCorrelator.setAddress(bus.getName() + ":" + bpmScriptNextChannelName);
    conversationCorrelator.setCorrelatorAddress(bus.getName() + ":" + conversationChannelName);
    conversationCorrelator.setReplyTo(bus.getName() + ":" + syncChannelName);
    conversationCorrelator.setSender(bus);
    conversationCorrelator.setSyncChannel(syncChannel);

    BpmScriptFirstAdapter bpmScriptFirstAdapter = new BpmScriptFirstAdapter(engine, bus);
    BpmScriptNextAdapter bpmScriptNextAdapter = new BpmScriptNextAdapter(engine, bus,
            bus.getName() + ":" + errorChannelName);

    TimeoutAdapter timeoutAdapter = new TimeoutAdapter(bus);

    ConversationCorrelatorAdapter conversationCorrelatorAdapter = new ConversationCorrelatorAdapter();
    conversationCorrelatorAdapter.setSender(bus);

    SpringAdapter springAdapter = new SpringAdapter(applicationContext, bus);
    bus.register(springChannelName, springAdapter);
    bus.register(bpmScriptFirstChannelName, bpmScriptFirstAdapter);
    bus.register(bpmScriptNextChannelName, bpmScriptNextAdapter);
    bus.register(timeoutChannelName, timeoutAdapter);
    bus.register(errorChannelName, new LoggingAdapter());
    bus.register(syncChannelName, syncChannel);
    bus.register(conversationChannelName, conversationCorrelatorAdapter);

    LibraryFileMonitor libraryFileMonitor = new LibraryFileMonitor();
    libraryFileMonitor.setLibraryAssociationQueue(libraryAssociationQueue);
    libraryFileMonitor.setLibraryChangeQueue(libraryChangeQueue);

    FolderDefinitionLoader folderDefinitionLoader = new FolderDefinitionLoader();
    folderDefinitionLoader.setEngine(engine);
    folderDefinitionLoader.setFolder(folder);
    folderDefinitionLoader.setLibraryChangeQueue(libraryChangeQueue);
    folderDefinitionLoader.setVersionedDefinitionManager(versionedDefinitionManager);
    folderDefinitionLoader.afterPropertiesSet();

    instanceKiller = new InstanceKiller();
    instanceKiller.setBpmScriptEngine(engine);
    instanceKiller.setContinuationJournal(bpmScriptManager);
    instanceKiller.setInstanceStatusManager(bpmScriptManager);

    autoStartManager = new AutoStartManager();
    autoStartManager.setBpmScriptFacade(bpmScriptFacade);
    autoStartManager.setInstanceKiller(instanceKiller);
    autoStartManager.setInstanceStatusManager(bpmScriptManager);
    autoStartManager.setVersionedDefinitionManager(versionedDefinitionManager);

}

From source file:libra.preprocess.common.kmerindex.KmerIndexReader.java

private void fillBuffer() throws IOException {
    if (!this.eof) {
        CompressedSequenceWritable lastBufferedKey = null;
        int added = 0;
        while (added < BUFFER_SIZE) {
            CompressedSequenceWritable key = new CompressedSequenceWritable();
            IntWritable val = new IntWritable();
            if (this.mapfileReaders[this.currentIndex].next(key, val)) {
                KmerIndexBufferEntry entry = new KmerIndexBufferEntry(key, val);
                if (!this.buffer.offer(entry)) {
                    throw new IOException("buffer is full");
                }/*from  ww w  .  j  a  v a 2s . co  m*/

                lastBufferedKey = key;
                added++;
            } else {
                // EOF of this part
                this.mapfileReaders[this.currentIndex].close();
                this.mapfileReaders[this.currentIndex] = null;
                this.currentIndex++;

                if (this.currentIndex == this.mapfileReaders.length) {
                    // last
                    this.eof = true;
                    break;
                } else {
                    this.mapfileReaders[this.currentIndex] = new IndexCloseableMapFileReader(this.fs,
                            this.indexPartFilePaths[this.currentIndex].toString(), this.conf);
                    this.mapfileReaders[this.currentIndex].closeIndex();
                }
            }
        }

        if (this.endKey != null && lastBufferedKey != null) {
            if (lastBufferedKey.compareTo(this.endKey) > 0) {
                // recheck buffer
                BlockingQueue<KmerIndexBufferEntry> new_buffer = new LinkedBlockingQueue<KmerIndexBufferEntry>();

                KmerIndexBufferEntry entry = this.buffer.poll();
                while (entry != null) {
                    if (entry.getKey().compareTo(this.endKey) <= 0) {
                        if (!new_buffer.offer(entry)) {
                            throw new IOException("buffer is full");
                        }
                    }

                    entry = this.buffer.poll();
                }

                this.buffer = new_buffer;
                this.eof = true;
            }
        }
    }
}

From source file:com.espertech.esper.filter.TestIndexTreeBuilderMultithreaded.java

private void performMultithreadedTest(FilterHandleSetNode topNode, int numberOfThreads, int numberOfRunnables,
        int numberOfSecondsSleep) throws Exception {
    log.info(".performMultithreadedTest Loading thread pool work queue,numberOfRunnables=" + numberOfRunnables);

    ThreadPoolExecutor pool = new ThreadPoolExecutor(0, numberOfThreads, 99999, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>());

    for (int i = 0; i < numberOfRunnables; i++) {
        IndexTreeBuilderRunnable runnable = new IndexTreeBuilderRunnable(eventType, topNode, testFilterSpecs,
                matchedEvents, unmatchedEvents);

        pool.execute(runnable);//from w  w w.j  a v  a2 s  .co  m
    }

    log.info(".performMultithreadedTest Starting thread pool, threads=" + numberOfThreads);
    pool.setCorePoolSize(numberOfThreads);

    // Sleep X seconds
    sleep(numberOfSecondsSleep);

    log.info(".performMultithreadedTest Completed, numberOfRunnables=" + numberOfRunnables
            + "  numberOfThreads=" + numberOfThreads + "  completed=" + pool.getCompletedTaskCount());

    pool.shutdown();
    pool.awaitTermination(1, TimeUnit.SECONDS);

    assertTrue(pool.getCompletedTaskCount() == numberOfRunnables);
}

From source file:com.nttec.everychan.ui.downloading.DownloadingService.java

@Override
public void onCreate() {
    super.onCreate();
    downloadingQueue = new LinkedBlockingQueue<DownloadingQueueItem>();
    sQueue = downloadingQueue;/*from   w ww. j a  va  2s  . c  om*/
    binder = new DownloadingServiceBinder(this);
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    settings = MainApplication.getInstance().settings;
    fileCache = MainApplication.getInstance().fileCache;
    downloadingLocker = MainApplication.getInstance().downloadingLocker;
    bitmapCache = MainApplication.getInstance().bitmapCache;
    Logger.d(TAG, "created downloading service");
}

From source file:com.ibm.crail.tools.CrailBenchmark.java

void writeAsync(String filename, int size, int loop, int batch, int storageClass, int locationClass)
        throws Exception {
    System.out.println("writeAsync, filename " + filename + ", size " + size + ", loop " + loop + ", batch "
            + batch + ", storageClass " + storageClass + ", locationClass " + locationClass);

    ConcurrentLinkedQueue<CrailBuffer> bufferQueue = new ConcurrentLinkedQueue<CrailBuffer>();
    for (int i = 0; i < batch; i++) {
        CrailBuffer buf = null;// ww w  . ja v  a 2 s .c  o m
        if (size == CrailConstants.BUFFER_SIZE) {
            buf = fs.allocateBuffer();
        } else if (size < CrailConstants.BUFFER_SIZE) {
            CrailBuffer _buf = fs.allocateBuffer();
            _buf.clear().limit(size);
            buf = _buf.slice();
        } else {
            buf = OffHeapBuffer.wrap(ByteBuffer.allocateDirect(size));
        }
        bufferQueue.add(buf);
    }

    //warmup
    warmUp(filename, warmup, bufferQueue);

    //benchmark
    System.out.println("starting benchmark...");
    LinkedBlockingQueue<Future<CrailResult>> futureQueue = new LinkedBlockingQueue<Future<CrailResult>>();
    HashMap<Integer, CrailBuffer> futureMap = new HashMap<Integer, CrailBuffer>();
    fs.getStatistics().reset();
    long _loop = (long) loop;
    long _bufsize = (long) CrailConstants.BUFFER_SIZE;
    long _capacity = _loop * _bufsize;
    double sumbytes = 0;
    double ops = 0;
    CrailFile file = fs.create(filename, CrailNodeType.DATAFILE, CrailStorageClass.get(storageClass),
            CrailLocationClass.get(locationClass)).get().asFile();
    CrailOutputStream directStream = file.getDirectOutputStream(_capacity);
    long start = System.currentTimeMillis();
    for (int i = 0; i < batch - 1 && ops < loop; i++) {
        CrailBuffer buf = bufferQueue.poll();
        buf.clear();
        Future<CrailResult> future = directStream.write(buf);
        futureQueue.add(future);
        futureMap.put(future.hashCode(), buf);
        ops = ops + 1.0;
    }
    while (ops < loop) {
        CrailBuffer buf = bufferQueue.poll();
        buf.clear();
        Future<CrailResult> future = directStream.write(buf);
        futureQueue.add(future);
        futureMap.put(future.hashCode(), buf);

        future = futureQueue.poll();
        future.get();
        buf = futureMap.get(future.hashCode());
        bufferQueue.add(buf);

        sumbytes = sumbytes + buf.capacity();
        ops = ops + 1.0;
    }
    while (!futureQueue.isEmpty()) {
        Future<CrailResult> future = futureQueue.poll();
        future.get();
        CrailBuffer buf = futureMap.get(future.hashCode());
        sumbytes = sumbytes + buf.capacity();
        ops = ops + 1.0;
    }
    long end = System.currentTimeMillis();
    double executionTime = ((double) (end - start)) / 1000.0;
    double throughput = 0.0;
    double latency = 0.0;
    double sumbits = sumbytes * 8.0;
    if (executionTime > 0) {
        throughput = sumbits / executionTime / 1000.0 / 1000.0;
        latency = 1000000.0 * executionTime / ops;
    }
    directStream.close();

    System.out.println("execution time " + executionTime);
    System.out.println("ops " + ops);
    System.out.println("sumbytes " + sumbytes);
    System.out.println("throughput " + throughput);
    System.out.println("latency " + latency);

    fs.getStatistics().print("close");
}

From source file:net.jradius.server.JRadiusServer.java

/**
 * Read the configuration and initialize the JRadiusServer
 * @throws SecurityException// w  ww .  j  a  v a2 s.  com
 * @throws IllegalArgumentException
 * @throws ClassNotFoundException
 * @throws NoSuchMethodException
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
private void initializeServer() throws Exception {
    RadiusLog.info("Initializing JRadius Server....");
    for (Iterator i = Configuration.getDictionaryConfigs().iterator(); i.hasNext();) {
        DictionaryConfigurationItem dictionaryConfig = (DictionaryConfigurationItem) i.next();
        RadiusLog.info("Loading dictionary: " + dictionaryConfig.getClassName());
        AttributeFactory.loadAttributeDictionary(
                (AttributeDictionary) Configuration.getBean(dictionaryConfig.getClassName()));
    }
    for (ListenerConfigurationItem listenerConfig : Configuration.getListenerConfigs()) {
        LinkedBlockingQueue<ListenerRequest> queue = new LinkedBlockingQueue<ListenerRequest>();
        createListenerWithConfigAndQueue(listenerConfig, queue);
        createProcessorsWithConfigAndQueue(listenerConfig, queue);
    }
    RadiusLog.info("JRadius Server succesfully Initialized.");
}

From source file:com.axibase.tsd.client.PlainSender.java

@Override
public void run() {
    if (messages == null) {
        messages = new LinkedBlockingQueue<String>();
    }/*from  w  w  w.ja v a  2 s.co  m*/
    HttpPost httpPost = null;
    try {
        SslConfigurator sslConfig = SslConfigurator.newInstance().securityProtocol("SSL");
        connectionManager = HttpClient.createConnectionManager(clientConfiguration, sslConfig);
        connectionManager.setDefaultConnectionConfig(ConnectionConfig.custom().setBufferSize(SMALL).build());
        httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
        httpPost = new HttpPost(fullUrl());
        httpPost.setHeader("Authorization", "Basic " + DatatypeConverter.printBase64Binary(
                (clientConfiguration.getUsername() + ":" + clientConfiguration.getPassword()).getBytes()));
        httpPost.setEntity(new BufferedHttpEntity(this));
    } catch (Throwable e) {
        log.error("Could not create http client: ", e);
        latch.countDown();
        close();
        return;
    }
    try {
        log.info("Start writing commands to {}", fullUrl());
        state = SenderState.WORKING;
        latch.countDown();
        response = httpClient.execute(httpPost);
    } catch (Throwable e) {
        log.error("Could not execute HTTP POST: {}", httpPost, e);
    } finally {
        log.warn("Http post execution is finished, close sender");
        close();
    }
}

From source file:com.connection.factory.SftpConnectionApacheLib.java

@Override
public List<RemoteFileObject> readAllFilesWalkinPath(String remotePath) {
    List<RemoteFileObject> willReturnObject = new ArrayList<>();
    Queue<RemoteFileObject> directorylist = new LinkedBlockingQueue<>();
    RemoteFileObject object = null;/*from   w  ww .  j av a2s .  com*/
    object = new FtpApacheFileObject(FileInfoEnum.DIRECTORY);
    object.setDirectPath(remotePath);
    directorylist.add(object);
    try {
        while (!directorylist.isEmpty()) {
            object = directorylist.poll();
            List<ChannelSftp.LsEntry> list = command.ls(object.getPath());
            for (ChannelSftp.LsEntry each : list) {
                if (each.getFilename().equals(".") || each.getFilename().equals("..")) {
                    continue;
                }
                RemoteFileObject objectTemp = null;
                SftpATTRS attributes = each.getAttrs();
                if (attributes.isDir()) {
                    objectTemp = new SftpApacheFileObject(FileInfoEnum.DIRECTORY);
                    objectTemp.setFileName(each.getFilename());
                    objectTemp.setAbsolutePath(object.getPath());
                    directorylist.add(objectTemp);
                } else if (attributes.isReg()) {
                    objectTemp = new SftpApacheFileObject(FileInfoEnum.FILE);
                    objectTemp.setFileName(each.getFilename());
                    objectTemp.setAbsolutePath(object.getPath());
                    objectTemp.setFileSize(attributes.getSize());
                    objectTemp.setDate(attributes.getMtimeString());
                    objectTemp.setFileType();
                    willReturnObject.add(objectTemp);
                }
            }
            object = null;
            list = null;
        }

    } catch (ConnectionException | SftpException ex) {
        ex.printStackTrace();
    }
    return willReturnObject;
}

From source file:org.apache.bookkeeper.bookie.InterleavedLedgerStorageTest.java

@Test
public void testConsistencyCheckConcurrentGC() throws Exception {
    final long signalDone = -1;
    final List<Exception> asyncErrors = new ArrayList<>();
    final LinkedBlockingQueue<Long> toCompact = new LinkedBlockingQueue<>();
    final Semaphore awaitingCompaction = new Semaphore(0);

    interleavedStorage.flush();//from  w  ww  .  ja va 2s .com
    final long lastLogId = entryLogger.getLeastUnflushedLogId();

    final MutableInt counter = new MutableInt(0);
    entryLogger.setCheckEntryTestPoint((ledgerId, entryId, entryLogId, pos) -> {
        if (entryLogId < lastLogId) {
            if (counter.intValue() % 100 == 0) {
                try {
                    toCompact.put(entryLogId);
                    awaitingCompaction.acquire();
                } catch (InterruptedException e) {
                    asyncErrors.add(e);
                }
            }
            counter.increment();
        }
    });

    Thread mutator = new Thread(() -> {
        EntryLogCompactor compactor = new EntryLogCompactor(conf, entryLogger, interleavedStorage,
                entryLogger::removeEntryLog);
        while (true) {
            Long next = null;
            try {
                next = toCompact.take();
                if (next == null || next == signalDone) {
                    break;
                }
                compactor.compact(entryLogger.getEntryLogMetadata(next));
            } catch (BufferedChannelBase.BufferedChannelClosedException e) {
                // next was already removed, ignore
            } catch (Exception e) {
                asyncErrors.add(e);
                break;
            } finally {
                if (next != null) {
                    awaitingCompaction.release();
                }
            }
        }
    });
    mutator.start();

    List<LedgerStorage.DetectedInconsistency> inconsistencies = interleavedStorage
            .localConsistencyCheck(Optional.empty());
    for (LedgerStorage.DetectedInconsistency e : inconsistencies) {
        LOG.error("Found: {}", e);
    }
    Assert.assertEquals(0, inconsistencies.size());

    toCompact.offer(signalDone);
    mutator.join();
    for (Exception e : asyncErrors) {
        throw e;
    }

    if (!conf.isEntryLogPerLedgerEnabled()) {
        Assert.assertNotEquals(0,
                statsProvider.getCounter(BOOKIE_SCOPE + "." + STORAGE_SCRUB_PAGE_RETRIES).get().longValue());
    }
}