Example usage for com.google.common.collect Queues newLinkedBlockingQueue

List of usage examples for com.google.common.collect Queues newLinkedBlockingQueue

Introduction

In this page you can find the example usage for com.google.common.collect Queues newLinkedBlockingQueue.

Prototype

public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue() 

Source Link

Document

Creates an empty LinkedBlockingQueue with a capacity of Integer#MAX_VALUE .

Usage

From source file:com.nirmata.workflow.details.Scheduler.java

void run() {
    CountDownLatch initLatch = new CountDownLatch(2);

    BlockingQueue<RunId> updatedRunIds = Queues.newLinkedBlockingQueue();
    completedTasksCache.getListenable().addListener((client, event) -> {
        if (event.getType() == PathChildrenCacheEvent.Type.INITIALIZED) {
            initLatch.countDown();/*from   w  w  w. j  ava2s . co  m*/
        } else if (event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED) {
            RunId runId = new RunId(
                    ZooKeeperConstants.getRunIdFromCompletedTasksPath(event.getData().getPath()));
            updatedRunIds.add(runId);
        }
    });
    runsCache.getListenable().addListener((client, event) -> {
        if (event.getType() == PathChildrenCacheEvent.Type.INITIALIZED) {
            initLatch.countDown();
        } else if (event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED) {
            RunId runId = new RunId(ZooKeeperConstants.getRunIdFromRunPath(event.getData().getPath()));
            updatedRunIds.add(runId);
        } else if (event.getType() == PathChildrenCacheEvent.Type.CHILD_UPDATED) {
            RunnableTask runnableTask = workflowManager.getSerializer().deserialize(event.getData().getData(),
                    RunnableTask.class);
            if (runnableTask.getParentRunId().isPresent()) {
                updatedRunIds.add(runnableTask.getParentRunId().get());
            }
        }
    });

    try {
        completedTasksCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
        startedTasksCache.start(PathChildrenCache.StartMode.NORMAL);
        runsCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);

        initLatch.await();

        while (!Thread.currentThread().isInterrupted()) {
            RunId runId = updatedRunIds.poll(autoCleanerHolder.getRunPeriod().toMillis(),
                    TimeUnit.MILLISECONDS);
            if (runId != null) {
                updateTasks(runId);
            }
            if (autoCleanerHolder.shouldRun()) {
                autoCleanerHolder.run(workflowManager.getAdmin());
            }
        }
    } catch (InterruptedException dummy) {
        Thread.currentThread().interrupt();
    } catch (Throwable e) {
        log.error("Error while running scheduler", e);
    } finally {
        queues.invalidateAll();
        queues.cleanUp();
        CloseableUtils.closeQuietly(completedTasksCache);
        CloseableUtils.closeQuietly(startedTasksCache);
        CloseableUtils.closeQuietly(runsCache);
    }
}

From source file:org.apache.drill.exec.physical.impl.xsort.MSortTemplate.java

@Override
public void sort(final VectorContainer container) {
    final Stopwatch watch = new Stopwatch();
    watch.start();/*from  w  w  w. j  a v a  2s.  c  o  m*/
    while (runStarts.size() > 1) {

        // check if we're cancelled/failed frequently
        if (!context.shouldContinue()) {
            return;
        }

        int outIndex = 0;
        newRunStarts = Queues.newLinkedBlockingQueue();
        newRunStarts.add(outIndex);
        final int size = runStarts.size();
        for (int i = 0; i < size / 2; i++) {
            final int left = runStarts.poll();
            final int right = runStarts.poll();
            Integer end = runStarts.peek();
            if (end == null) {
                end = vector4.getTotalCount();
            }
            outIndex = merge(left, right, end, outIndex);
            if (outIndex < vector4.getTotalCount()) {
                newRunStarts.add(outIndex);
            }
        }
        if (outIndex < vector4.getTotalCount()) {
            copyRun(outIndex, vector4.getTotalCount());
        }
        final SelectionVector4 tmp = aux.createNewWrapperCurrent(desiredRecordBatchCount);
        aux.clear();
        aux = this.vector4.createNewWrapperCurrent(desiredRecordBatchCount);
        vector4.clear();
        this.vector4 = tmp.createNewWrapperCurrent(desiredRecordBatchCount);
        tmp.clear();
        runStarts = newRunStarts;
    }
    aux.clear();
}

From source file:org.apache.drill.exec.physical.impl.xsort.managed.MSortTemplate.java

/**
 * Merge a set of pre-sorted runs to produce a combined
 * result set. Merging is done in the selection vector, record data does
 * not move.//w  w w.j a  v  a  2s .  c o  m
 * <p>
 * Runs are merge pairwise in multiple passes, providing performance
 * of O(n * m * log(n)), where n = number of runs, m = number of records
 * per run.
 */

@Override
public void sort() {
    while (runStarts.size() > 1) {
        final int totalCount = this.vector4.getTotalCount();

        // check if we're cancelled/failed recently
        if (!context.getExecutorState().shouldContinue()) {
            return;
        }

        int outIndex = 0;
        final Queue<Integer> newRunStarts = Queues.newLinkedBlockingQueue();
        newRunStarts.add(outIndex);
        final int size = runStarts.size();
        for (int i = 0; i < size / 2; i++) {
            final int left = runStarts.poll();
            final int right = runStarts.poll();
            Integer end = runStarts.peek();
            if (end == null) {
                end = totalCount;
            }
            outIndex = merge(left, right, end, outIndex);
            if (outIndex < vector4.getTotalCount()) {
                newRunStarts.add(outIndex);
            }
        }
        if (outIndex < totalCount) {
            copyRun(outIndex, totalCount);
        }
        @SuppressWarnings("resource")
        final SelectionVector4 tmp = aux.createNewWrapperCurrent(desiredRecordBatchCount);
        aux.clear();
        aux = vector4.createNewWrapperCurrent(desiredRecordBatchCount);
        vector4.clear();
        vector4 = tmp.createNewWrapperCurrent(desiredRecordBatchCount);
        tmp.clear();
        runStarts = newRunStarts;
    }
    aux.clear();
}

From source file:jp.tkgktyk.wearablepad.MainActivity.java

private void initMessageQueue() {
    mMessages = Queues.newLinkedBlockingQueue();
    new Thread(new Runnable() {
        @Override/*from w ww  .j  av a2s . c om*/
        public void run() {
            try {
                while (mRunning) {
                    final TouchMessage message = mMessages.take();
                    final int count = mMessages.size();
                    MyApp.logD("queue size = " + count);
                    TouchMessage lastMessage;
                    if (mMessages.size() != 0) {
                        if (message.event == TouchMessage.EVENT_MOVE) {
                            lastMessage = compressAndSend(count, TouchMessage.EVENT_MOVE, message);
                        } else if (message.event == TouchMessage.EVENT_DRAG) {
                            lastMessage = compressAndSend(count, TouchMessage.EVENT_DRAG, message);
                        } else {
                            send(message);
                            lastMessage = message;
                        }
                    } else {
                        send(message);
                        lastMessage = message;
                    }
                    switch (lastMessage.event) {
                    case TouchMessage.EVENT_MOVE:
                    case TouchMessage.EVENT_DRAG:
                        SystemClock.sleep(100);
                        break;
                    }
                }
            } catch (InterruptedException e) {
                MyApp.logE(e);
            }
        }

        private TouchMessage compressAndSend(int count, int event, TouchMessage baseMessage)
                throws InterruptedException {
            TouchMessage message2 = null;
            for (int i = 0; i < count; ++i) {
                message2 = mMessages.take();
                if (message2.event == event) {
                    baseMessage.x += message2.x;
                    baseMessage.y += message2.y;
                    message2 = null;
                    MyApp.logD("compress scroll event#" + i + ": " + event);
                } else {
                    break;
                }
            }
            if (message2 == null) {
                send(baseMessage);
                return baseMessage;
            } else {
                send(baseMessage);
                send(message2);
                return message2;
            }
        }

        private void send(TouchMessage message) {
            MyApp.logD("event: " + message.event + ", x: " + message.x + ", y: " + message.y);
            final byte[] data = ParcelableUtil.marshall(message);
            mNodes = Wearable.NodeApi.getConnectedNodes(mClient).await();
            for (Node node : mNodes.getNodes()) {
                MessageApi.SendMessageResult result = Wearable.MessageApi
                        .sendMessage(mClient, node.getId(), "", data).await();
                if (!result.getStatus().isSuccess()) {
                    MyApp.logD("isSuccess is false");
                }
            }
        }
    }).start();
}

From source file:com.mapr.workGroup.Worker.java

private BlockingQueue<ProgressNote.Update> getUpdates(final File updateLogFile) throws IOException {
    final BlockingQueue<ProgressNote.Update> updateQueue = Queues.newLinkedBlockingQueue();
    getWatcher().watch(updateLogFile, new Watcher.Watch() {
        private long updateOffset = 0;
        private FileChannel updateLockChannel;
        private InputStream updateLockStream;

        {/*from  w  w  w  . ja  va  2  s  .  c om*/
            updateLockChannel = FileChannel.open(updateLogFile.toPath(), StandardOpenOption.READ);
            updateLockStream = Channels.newInputStream(updateLockChannel);
        }

        @Override
        public void changeNotify(Watcher watcher, File f) {
            try {
                updateLockChannel.position(updateOffset);
                ProgressNote.Update update = ProgressNote.Update.parseDelimitedFrom(updateLockStream);
                updateOffset = updateLockChannel.position();
                updateQueue.add(update);
            } catch (EOFException e) {
                // ignore EOF ... more data will come soon
            } catch (IOException e) {
                throw new RuntimeException("Can't read update log", e);
            }
        }

        @Override
        public void timeoutNotify(Watcher watcher, File f) {
            throw new UnsupportedOperationException("Default operation");
        }
    }, Constants.getWorkerProgressTimeout());
    return updateQueue;
}

From source file:com.mapr.workGroup.Worker.java

/**
 * Monitors the coordinators output file looking for assignments for us.
 *
 * @throws IOException/*from   www .j  a  v  a  2 s .c om*/
 */
private BlockingQueue<ClusterState.Assignment> getAssignments() throws IOException {
    final BlockingQueue<ClusterState.Assignment> toDo = Queues.newLinkedBlockingQueue();
    final File coordinatorLockFile = new File(getBaseDirectory(), Constants.getCoordinatorLock());

    // start scanning for directions.  This watcher filters the messages that come through
    // so that only the important ones come through the assignment queue
    getWatcher().watch(coordinatorLockFile, new Watcher.Watch() {
        boolean assigned = false;
        private long coordinatorOffset = 0;
        private MessageInput<ClusterState.Assignment> coordinatorLockStream;

        {
            Path path = new File(getBaseDirectory(), Constants.getCoordinatorLock()).toPath();
            coordinatorLockStream = new MessageInput<ClusterState.Assignment>(path) {
                @Override
                public ClusterState.Assignment parse(InputStream in) throws IOException {
                    return ClusterState.Assignment.parseDelimitedFrom(in);
                }
            };
        }

        @Override
        public void changeNotify(Watcher watcher, File f) {
            try {
                ClusterState.Assignment assignment = coordinatorLockStream.read();
                switch (assignment.getType()) {
                case WORKER_EXIT:
                case DRONE:
                    if (assignment.getId().equals(getId().toStringUtf8())) {
                        toDo.add(assignment);
                    }
                    break;

                case ALL_EXIT:
                    toDo.add(assignment);
                    break;

                case WORKER:
                    if (assignment.getId().equals(getId().toStringUtf8())) {
                        toDo.add(assignment);
                        assigned = true;
                    }
                    break;

                default:
                    // TODO shut everything down somehow
                    throw new RuntimeException("Can't happen, got " + assignment.toString());
                }
            } catch (EOFException e1) {
                // message not ready yet
            } catch (IOException e) {
                throw new RuntimeException("Error reading coordinator lock file", e);
            }
        }

        @Override
        public void timeoutNotify(Watcher watcher, File f) {
            if (!assigned) {
                toDo.add(ClusterState.Assignment.newBuilder().setType(ClusterState.WorkerType.WORKER_TIMEOUT)
                        .build());
            }
        }
    }, Constants.getWorkerStartupTimeout());
    return toDo;
}

From source file:indigo.runtime.InteractiveAnalysis.java

@SuppressWarnings("unchecked")
private void init(ProgramSpecification spec, ConflictResolutionPolicy resolutionPolicy,
        OperationGenerator opGenerator, Iterator<String> input, PrintStream consoleOutput,
        PrintStream resultOutput) throws IOException {
    this.dependenciesForPredicate = spec.getDependenciesForPredicate();

    Set<Operation> operations = new HashSet<>();
    operations.addAll(spec.getOperations());
    for (String predicateName : dependenciesForPredicate.keySet()) {
        for (Operation op : spec.getOperations()) {
            if (op.containsPredicate(predicateName)) {
                Set<PredicateAssignment> newEffects = new HashSet<>();
                newEffects.addAll(op.getEffects());
                newEffects.addAll(dependenciesForPredicate.get(predicateName).stream()
                        .map(dep -> new GenericPredicateAssignment(dep, dep.negateValue()))
                        .collect(Collectors.toSet()));
                operations.add(new GenericOperation("#" + op.opName(), newEffects, op.getParameters(),
                        op.getPreConditions()));
            }//from  ww w .  ja v  a2 s . co m
        }
    }
    this.spec = spec;
    this.analysis = new IndigoAnalyzer(spec, resolutionPolicy != null, opGenerator);
    this.rootContext = AnalysisContext.getNewContext(operations, resolutionPolicy,
            GenericPredicateFactory.getFactory());
    this.currentContext = rootContext;

    this.idempotenceTestQueue = Queues.newLinkedBlockingQueue();
    this.unresolvedPairwiseConflicts = Queues.newLinkedBlockingQueue();
    this.toFixQueue = Queues.newLinkedBlockingQueue();
    currentContext = rootContext.childContext(false);

    operations.forEach(op -> {
        idempotenceTestQueue.add(new SingleOperationTest(op.opName()));
        trackedOperations.add(op.opName());
    });

    in = input;
    out = consoleOutput;
    resultOut = resultOutput;

    refillQueueWithOperationPairs(spec.getOperationsNames(), spec.getOperationsNames());
    // dumpContext();

}