Search in sources :

Example 36 with Condition

use of java.util.concurrent.locks.Condition in project ignite by apache.

the class GridJobProcessor method requestJobSiblings.

/**
 * @param ses Session.
 * @return Siblings.
 * @throws IgniteCheckedException If failed.
 */
public Collection<ComputeJobSibling> requestJobSiblings(final ComputeTaskSession ses) throws IgniteCheckedException {
    assert ses != null;
    final UUID taskNodeId = ses.getTaskNodeId();
    ClusterNode taskNode = ctx.discovery().node(taskNodeId);
    if (taskNode == null)
        throw new IgniteCheckedException("Node that originated task execution has left grid: " + taskNodeId);
    // Tuple: error message-response.
    final IgniteBiTuple<String, GridJobSiblingsResponse> t = new IgniteBiTuple<>();
    final Lock lock = new ReentrantLock();
    final Condition cond = lock.newCondition();
    GridMessageListener msgLsnr = new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg, byte plc) {
            String err = null;
            GridJobSiblingsResponse res = null;
            if (!(msg instanceof GridJobSiblingsResponse))
                err = "Received unexpected message: " + msg;
            else if (!nodeId.equals(taskNodeId))
                err = "Received job siblings response from unexpected node [taskNodeId=" + taskNodeId + ", nodeId=" + nodeId + ']';
            else {
                // Sender and message type are fine.
                res = (GridJobSiblingsResponse) msg;
                if (res.jobSiblings() == null) {
                    try {
                        res.unmarshalSiblings(marsh);
                    } catch (IgniteCheckedException e) {
                        U.error(log, "Failed to unmarshal job siblings.", e);
                        err = e.getMessage();
                    }
                }
            }
            lock.lock();
            try {
                if (t.isEmpty()) {
                    t.set(err, res);
                    cond.signalAll();
                }
            } finally {
                lock.unlock();
            }
        }
    };
    GridLocalEventListener discoLsnr = new GridLocalEventListener() {

        @Override
        public void onEvent(Event evt) {
            assert evt instanceof DiscoveryEvent && (evt.type() == EVT_NODE_FAILED || evt.type() == EVT_NODE_LEFT) : "Unexpected event: " + evt;
            DiscoveryEvent discoEvt = (DiscoveryEvent) evt;
            if (taskNodeId.equals(discoEvt.eventNode().id())) {
                lock.lock();
                try {
                    if (t.isEmpty()) {
                        t.set("Node that originated task execution has left grid: " + taskNodeId, null);
                        cond.signalAll();
                    }
                } finally {
                    lock.unlock();
                }
            }
        }
    };
    boolean loc = ctx.localNodeId().equals(taskNodeId);
    // 1. Create unique topic name.
    Object topic = TOPIC_JOB_SIBLINGS.topic(ses.getId(), topicIdGen.getAndIncrement());
    try {
        // 2. Register listener.
        ctx.io().addMessageListener(topic, msgLsnr);
        // 3. Send message.
        ctx.io().sendToGridTopic(taskNode, TOPIC_JOB_SIBLINGS, new GridJobSiblingsRequest(ses.getId(), loc ? topic : null, loc ? null : U.marshal(marsh, topic)), SYSTEM_POOL);
        // 4. Listen to discovery events.
        ctx.event().addLocalEventListener(discoLsnr, EVT_NODE_FAILED, EVT_NODE_LEFT);
        // 5. Check whether node has left before disco listener has been installed.
        taskNode = ctx.discovery().node(taskNodeId);
        if (taskNode == null)
            throw new IgniteCheckedException("Node that originated task execution has left grid: " + taskNodeId);
        // 6. Wait for result.
        lock.lock();
        try {
            long netTimeout = ctx.config().getNetworkTimeout();
            if (t.isEmpty())
                cond.await(netTimeout, MILLISECONDS);
            if (t.isEmpty())
                throw new IgniteCheckedException("Timed out waiting for job siblings (consider increasing" + "'networkTimeout' configuration property) [ses=" + ses + ", netTimeout=" + netTimeout + ']');
            // Error is set?
            if (t.get1() != null)
                throw new IgniteCheckedException(t.get1());
            else
                // Return result
                return t.get2().jobSiblings();
        } catch (InterruptedException e) {
            throw new IgniteCheckedException("Interrupted while waiting for job siblings response: " + ses, e);
        } finally {
            lock.unlock();
        }
    } finally {
        ctx.io().removeMessageListener(topic, msgLsnr);
        ctx.event().removeLocalEventListener(discoLsnr);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) GridJobSiblingsResponse(org.apache.ignite.internal.GridJobSiblingsResponse) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) GridSpinReadWriteLock(org.apache.ignite.internal.util.GridSpinReadWriteLock) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) GridJobSiblingsRequest(org.apache.ignite.internal.GridJobSiblingsRequest) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) JobEvent(org.apache.ignite.events.JobEvent) TaskEvent(org.apache.ignite.events.TaskEvent) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) UUID(java.util.UUID)

Example 37 with Condition

use of java.util.concurrent.locks.Condition in project ignite by apache.

the class OffheapReadWriteLock method waitAcquireWriteLock.

/**
 * Acquires write lock in waiting loop.
 *
 * @param lock Lock address.
 * @param lockIdx Lock index.
 * @param tag Validation tag.
 * @return {@code True} if lock was acquired, {@code false} if tag validation failed.
 */
private boolean waitAcquireWriteLock(long lock, int lockIdx, int tag) {
    ReentrantLock lockObj = locks[lockIdx];
    Condition waitCond = writeConditions[lockIdx];
    assert lockObj.isHeldByCurrentThread();
    boolean interrupted = false;
    try {
        while (true) {
            try {
                long state = GridUnsafe.getLongVolatile(null, lock);
                if (!checkTag(state, tag)) {
                    // We cannot lock with this tag, release waiter.
                    long updated = updateState(state, 0, 0, -1);
                    if (GridUnsafe.compareAndSwapLong(null, lock, state, updated)) {
                        int writeWaitCnt = writersWaitCount(updated);
                        int readWaitCnt = readersWaitCount(updated);
                        signalNextWaiter(writeWaitCnt, readWaitCnt, lockIdx);
                        return false;
                    }
                } else if (canWriteLock(state)) {
                    long updated = updateState(state, -1, 0, -1);
                    if (GridUnsafe.compareAndSwapLong(null, lock, state, updated))
                        return true;
                } else
                    waitCond.await();
            } catch (InterruptedException ignore) {
                interrupted = true;
            }
        }
    } finally {
        if (interrupted)
            Thread.currentThread().interrupt();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition)

Example 38 with Condition

use of java.util.concurrent.locks.Condition in project ignite by apache.

the class OffheapReadWriteLock method signalNextWaiter.

/**
 * @param writeWaitCnt Writers wait count.
 * @param readWaitCnt Readers wait count.
 * @param idx Lock index.
 */
private void signalNextWaiter(int writeWaitCnt, int readWaitCnt, int idx) {
    // to this particular lock, we can't wake up just one of them.
    if (writeWaitCnt == 0) {
        Condition readCondition = readConditions[idx];
        readCondition.signalAll();
    } else if (readWaitCnt == 0) {
        Condition writeCond = writeConditions[idx];
        writeCond.signalAll();
    } else {
        // We have both writers and readers.
        if (USE_RANDOM_RW_POLICY) {
            boolean write = (balancers[idx].incrementAndGet() & 0x1) == 0;
            Condition cond = (write ? writeConditions : readConditions)[idx];
            cond.signalAll();
        } else {
            Condition cond = writeConditions[idx];
            cond.signalAll();
        }
    }
}
Also used : Condition(java.util.concurrent.locks.Condition)

Example 39 with Condition

use of java.util.concurrent.locks.Condition in project knime-core by knime.

the class WorkflowManager method waitWhileInExecution.

/**
 * Causes the current thread to wait until the the workflow has reached a non-executing state unless a given timeout
 * elapsed.
 *
 * @param mutex The lock to sleep on (m_workflowMutex of the nodes/wfm to be waited for)
 * @param time the maximum time to wait (0 or negative for waiting infinitely)
 * @param unit the time unit of the {@code time} argument
 * @return {@code false} if the waiting time detectably elapsed before return from the method, else {@code true}. It
 *         returns {@code true} if the time argument is 0 or negative.
 * @throws InterruptedException if the current thread is interrupted
 */
private static boolean waitWhileInExecution(final WorkflowLock workflowLock, final NodeContainerStateObservable[] ncs, final long time, final TimeUnit unit) throws InterruptedException {
    final ReentrantLock lock = workflowLock.getReentrantLock();
    final Condition whileInExecCondition = lock.newCondition();
    NodeStateChangeListener listener = new NodeStateChangeListener() {

        @Override
        public void stateChanged(final NodeStateEvent stateEvent) {
            lock.lock();
            try {
                if (!containsExecutingNode(ncs)) {
                    whileInExecCondition.signalAll();
                }
            } finally {
                lock.unlock();
            }
        }
    };
    lock.lockInterruptibly();
    Arrays.stream(ncs).filter(nc -> nc != null).forEach(nc -> nc.addNodeStateChangeListener(listener));
    try {
        if (!containsExecutingNode(ncs)) {
            return true;
        }
        if (time > 0) {
            return whileInExecCondition.await(time, unit);
        } else {
            whileInExecCondition.await();
            return true;
        }
    } finally {
        lock.unlock();
        Arrays.stream(ncs).filter(nc -> nc != null).forEach(nc -> nc.removeNodeStateChangeListener(listener));
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) Arrays(java.util.Arrays) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) EXECUTING(org.knime.core.node.workflow.InternalNodeContainerState.EXECUTING) ReferencedFile(org.knime.core.internal.ReferencedFile) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IWriteFileStoreHandler(org.knime.core.data.filestore.internal.IWriteFileStoreHandler) InteractiveNode(org.knime.core.node.interactive.InteractiveNode) CoreException(org.eclipse.core.runtime.CoreException) StringUtils(org.apache.commons.lang3.StringUtils) AbstractQuickFormValueInConfiguration(org.knime.core.quickform.AbstractQuickFormValueInConfiguration) NodeView(org.knime.core.node.NodeView) NodeContainerExecutionStatus(org.knime.core.node.workflow.execresult.NodeContainerExecutionStatus) Node(org.knime.core.node.Node) Vector(java.util.Vector) Matcher(java.util.regex.Matcher) WorkflowFileStoreHandlerRepository(org.knime.core.data.filestore.internal.WorkflowFileStoreHandlerRepository) Map(java.util.Map) FileStoreHandlerRepository(org.knime.core.data.filestore.internal.FileStoreHandlerRepository) LockFailedException(org.knime.core.util.LockFailedException) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) EXECUTINGREMOTELY(org.knime.core.node.workflow.InternalNodeContainerState.EXECUTINGREMOTELY) Set(java.util.Set) ResolverUtil(org.knime.core.util.pathresolve.ResolverUtil) Executors(java.util.concurrent.Executors) POSTEXECUTE(org.knime.core.node.workflow.InternalNodeContainerState.POSTEXECUTE) InputNode(org.knime.core.node.dialog.InputNode) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Stream(java.util.stream.Stream) CONFIGURED(org.knime.core.node.workflow.InternalNodeContainerState.CONFIGURED) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ThreadNodeExecutionJobManager(org.knime.core.node.exec.ThreadNodeExecutionJobManager) CollapseIntoMetaNodeResult(org.knime.core.node.workflow.action.CollapseIntoMetaNodeResult) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) VirtualParallelizedChunkPortObjectInNodeModel(org.knime.core.node.workflow.virtual.parchunk.VirtualParallelizedChunkPortObjectInNodeModel) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) VMFileLocker(org.knime.core.util.VMFileLocker) RestoredFlowLoopContext(org.knime.core.node.workflow.FlowLoopContext.RestoredFlowLoopContext) SplitType(org.knime.core.node.workflow.NodeContainer.NodeContainerSettings.SplitType) AbstractQuickFormConfiguration(org.knime.core.quickform.AbstractQuickFormConfiguration) EXECUTED_QUEUED(org.knime.core.node.workflow.InternalNodeContainerState.EXECUTED_QUEUED) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) NotConfigurableException(org.knime.core.node.NotConfigurableException) UpdateStatus(org.knime.core.node.workflow.MetaNodeTemplateInformation.UpdateStatus) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) WorkflowExecutionResult(org.knime.core.node.workflow.execresult.WorkflowExecutionResult) DialogNode(org.knime.core.node.dialog.DialogNode) LinkedHashSet(java.util.LinkedHashSet) VirtualParallelizedChunkNodeInput(org.knime.core.node.workflow.virtual.parchunk.VirtualParallelizedChunkNodeInput) KNIMEConstants(org.knime.core.node.KNIMEConstants) LoadVersion(org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion) QuickFormInputNode(org.knime.core.quickform.in.QuickFormInputNode) Executor(java.util.concurrent.Executor) PREEXECUTE(org.knime.core.node.workflow.InternalNodeContainerState.PREEXECUTE) InteractiveView(org.knime.core.node.interactive.InteractiveView) FileOutputStream(java.io.FileOutputStream) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) File(java.io.File) ViewContent(org.knime.core.node.interactive.ViewContent) UNCONFIGURED_MARKEDFOREXEC(org.knime.core.node.workflow.InternalNodeContainerState.UNCONFIGURED_MARKEDFOREXEC) OutputNode(org.knime.core.node.dialog.OutputNode) Platform(org.eclipse.core.runtime.Platform) LoopStatus(org.knime.core.node.workflow.NativeNodeContainer.LoopStatus) FileUtil(org.knime.core.util.FileUtil) CONFIGURED_QUEUED(org.knime.core.node.workflow.InternalNodeContainerState.CONFIGURED_QUEUED) NodeType(org.knime.core.node.NodeFactory.NodeType) ExpandSubnodeResult(org.knime.core.node.workflow.action.ExpandSubnodeResult) ListIterator(java.util.ListIterator) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) URL(java.net.URL) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) WorkflowLoadResult(org.knime.core.node.workflow.WorkflowPersistor.WorkflowLoadResult) ReexecutionCallback(org.knime.core.node.interactive.ReexecutionCallback) MetaNodeLinkUpdateResult(org.knime.core.node.workflow.WorkflowPersistor.MetaNodeLinkUpdateResult) FileFilterUtils(org.apache.commons.io.filefilter.FileFilterUtils) NodeDialogPane(org.knime.core.node.NodeDialogPane) VirtualParallelizedChunkPortObjectInNodeFactory(org.knime.core.node.workflow.virtual.parchunk.VirtualParallelizedChunkPortObjectInNodeFactory) IExtensionRegistry(org.eclipse.core.runtime.IExtensionRegistry) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) AbstractNodeView(org.knime.core.node.AbstractNodeView) ConvenienceMethods(org.knime.core.node.util.ConvenienceMethods) URI(java.net.URI) ThreadFactory(java.util.concurrent.ThreadFactory) ContainerTable(org.knime.core.data.container.ContainerTable) MetaNodeToSubNodeResult(org.knime.core.node.workflow.action.MetaNodeToSubNodeResult) PortType(org.knime.core.node.port.PortType) NodeAndInports(org.knime.core.node.workflow.Workflow.NodeAndInports) CONFIGURED_MARKEDFOREXEC(org.knime.core.node.workflow.InternalNodeContainerState.CONFIGURED_MARKEDFOREXEC) Collection(java.util.Collection) CredentialsNode(org.knime.core.node.workflow.CredentialsStore.CredentialsNode) NodeModel(org.knime.core.node.NodeModel) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) BufferedDataTable(org.knime.core.node.BufferedDataTable) WorkflowPortTemplate(org.knime.core.node.workflow.WorkflowPersistor.WorkflowPortTemplate) LoadResultEntryType(org.knime.core.node.workflow.WorkflowPersistor.LoadResultEntry.LoadResultEntryType) NodeContainerTemplateLinkUpdateResult(org.knime.core.node.workflow.WorkflowPersistor.NodeContainerTemplateLinkUpdateResult) HiLiteHandler(org.knime.core.node.property.hilite.HiLiteHandler) Entry(java.util.Map.Entry) Optional(java.util.Optional) CheckUtils(org.knime.core.node.util.CheckUtils) CheckUtils.checkState(org.knime.core.node.util.CheckUtils.checkState) PortObject(org.knime.core.node.port.PortObject) NodeContainerExecutionResult(org.knime.core.node.workflow.execresult.NodeContainerExecutionResult) IntStream(java.util.stream.IntStream) IOCase(org.apache.commons.io.IOCase) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) NodeExecutionJobManagerPool(org.knime.core.node.util.NodeExecutionJobManagerPool) SingleNodeContainerSettings(org.knime.core.node.workflow.SingleNodeContainer.SingleNodeContainerSettings) EXECUTED_MARKEDFOREXEC(org.knime.core.node.workflow.InternalNodeContainerState.EXECUTED_MARKEDFOREXEC) HashMap(java.util.HashMap) NodeSettings(org.knime.core.node.NodeSettings) ConnectionType(org.knime.core.node.workflow.ConnectionContainer.ConnectionType) Function(java.util.function.Function) Pair(org.knime.core.util.Pair) HashSet(java.util.HashSet) IDLE(org.knime.core.node.workflow.InternalNodeContainerState.IDLE) ExternalNodeData(org.knime.core.node.dialog.ExternalNodeData) ConnectionContainerTemplate(org.knime.core.node.workflow.WorkflowPersistor.ConnectionContainerTemplate) NodeLogger(org.knime.core.node.NodeLogger) NodeCreationContext(org.knime.core.node.NodeCreationContext) EXECUTED(org.knime.core.node.workflow.InternalNodeContainerState.EXECUTED) LoadResult(org.knime.core.node.workflow.WorkflowPersistor.LoadResult) LinkedList(java.util.LinkedList) Role(org.knime.core.node.workflow.MetaNodeTemplateInformation.Role) InteractiveWebViewsResult(org.knime.core.node.workflow.action.InteractiveWebViewsResult) SubNodeToMetaNodeResult(org.knime.core.node.workflow.action.SubNodeToMetaNodeResult) OutputStream(java.io.OutputStream) NodeFactory(org.knime.core.node.NodeFactory) Iterator(java.util.Iterator) ReentrantLock(java.util.concurrent.locks.ReentrantLock) TemplateType(org.knime.core.node.workflow.MetaNodeTemplateInformation.TemplateType) MetaPortInfo(org.knime.core.node.port.MetaPortInfo) VirtualParallelizedChunkPortObjectOutNodeFactory(org.knime.core.node.workflow.virtual.parchunk.VirtualParallelizedChunkPortObjectOutNodeFactory) MetaNodeDialogNode(org.knime.core.node.dialog.MetaNodeDialogNode) ParallelizedChunkContent(org.knime.core.node.workflow.virtual.parchunk.ParallelizedChunkContent) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) Type(org.knime.core.node.workflow.NodeMessage.Type) TimeUnit(java.util.concurrent.TimeUnit) Condition(java.util.concurrent.locks.Condition) IEarlyStartup(org.knime.core.util.IEarlyStartup) ParallelizedChunkContentMaster(org.knime.core.node.workflow.virtual.parchunk.ParallelizedChunkContentMaster) IFileStoreHandler(org.knime.core.data.filestore.internal.IFileStoreHandler) NodeProperty(org.knime.core.node.workflow.NodePropertyChangedEvent.NodeProperty) Collections(java.util.Collections)

Example 40 with Condition

use of java.util.concurrent.locks.Condition in project JGroups by belaban.

the class NAKACK_StressTest method start.

private static void start(final int num_threads, final int num_msgs, boolean oob) {
    final NAKACK2 nak = new NAKACK2();
    final AtomicInteger counter = new AtomicInteger(num_msgs);
    final AtomicLong seqno = new AtomicLong(1);
    final AtomicInteger delivered_msgs = new AtomicInteger(0);
    final Lock lock = new ReentrantLock();
    final Condition all_msgs_delivered = lock.newCondition();
    final ConcurrentLinkedQueue<Long> delivered_msg_list = new ConcurrentLinkedQueue<>();
    final Address local_addr = Util.createRandomAddress("A");
    final Address sender = Util.createRandomAddress("B");
    nak.setDownProtocol(new Protocol() {

        public Object down(Event evt) {
            return null;
        }
    });
    nak.setUpProtocol(new Protocol() {

        public Object up(Message msg) {
            delivered_msgs.incrementAndGet();
            NakAckHeader2 hdr = msg.getHeader(NAKACK_ID);
            if (hdr != null)
                delivered_msg_list.add(hdr.getSeqno());
            if (delivered_msgs.get() >= num_msgs) {
                lock.lock();
                try {
                    all_msgs_delivered.signalAll();
                } finally {
                    lock.unlock();
                }
            }
            return null;
        }

        public void up(MessageBatch batch) {
            for (Message msg : batch) {
                delivered_msgs.incrementAndGet();
                NakAckHeader2 hdr = (NakAckHeader2) msg.getHeader(NAKACK_ID);
                if (hdr != null)
                    delivered_msg_list.add(hdr.getSeqno());
                if (delivered_msgs.get() >= num_msgs) {
                    lock.lock();
                    try {
                        all_msgs_delivered.signalAll();
                    } finally {
                        lock.unlock();
                    }
                }
            }
        }
    });
    nak.setDiscardDeliveredMsgs(true);
    nak.down(new Event(Event.SET_LOCAL_ADDRESS, local_addr));
    nak.down(new Event(Event.BECOME_SERVER));
    View view = View.create(local_addr, 1, local_addr, sender);
    nak.down(new Event(Event.VIEW_CHANGE, view));
    MutableDigest digest = new MutableDigest(view.getMembersRaw());
    digest.set(local_addr, 0, 0);
    digest.set(sender, 0, 0);
    nak.down(new Event(Event.SET_DIGEST, digest));
    final CountDownLatch latch = new CountDownLatch(1);
    Sender[] adders = new Sender[num_threads];
    for (int i = 0; i < adders.length; i++) {
        adders[i] = new Sender(nak, latch, counter, seqno, oob, sender);
        adders[i].start();
    }
    long start = System.currentTimeMillis();
    // starts all adders
    latch.countDown();
    int max_tries = 30;
    lock.lock();
    try {
        while (delivered_msgs.get() < num_msgs && max_tries-- > 0) {
            try {
                all_msgs_delivered.await(1000, TimeUnit.MILLISECONDS);
                System.out.println("received " + delivered_msgs.get() + " msgs");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    } finally {
        lock.unlock();
    }
    long time = System.currentTimeMillis() - start;
    double requests_sec = num_msgs / (time / 1000.0);
    System.out.printf("\nTime: %d ms, %.2f requests / sec\n", time, requests_sec);
    System.out.println("Delivered messages: " + delivered_msg_list.size());
    if (delivered_msg_list.size() < 100)
        System.out.println("Elements: " + delivered_msg_list);
    nak.stop();
    List<Long> results = new ArrayList<>(delivered_msg_list);
    if (oob)
        Collections.sort(results);
    assert results.size() == num_msgs : "expected " + num_msgs + ", but got " + results.size();
    System.out.println("Checking results consistency");
    int i = 1;
    for (Long num : results) {
        if (num != i) {
            assert i == num : "expected " + i + " but got " + num;
            return;
        }
        i++;
    }
    System.out.println("OK");
}
Also used : ArrayList(java.util.ArrayList) MessageBatch(org.jgroups.util.MessageBatch) Protocol(org.jgroups.stack.Protocol) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) CountDownLatch(java.util.concurrent.CountDownLatch) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) NAKACK2(org.jgroups.protocols.pbcast.NAKACK2) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) NakAckHeader2(org.jgroups.protocols.pbcast.NakAckHeader2) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) MutableDigest(org.jgroups.util.MutableDigest)

Aggregations

Condition (java.util.concurrent.locks.Condition)45 ReentrantLock (java.util.concurrent.locks.ReentrantLock)30 Lock (java.util.concurrent.locks.Lock)18 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)6 HttpResponse (org.apache.http.HttpResponse)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 ByteBuffer (java.nio.ByteBuffer)3 User (com.lonepulse.robozombie.model.User)2 InvocationException (com.lonepulse.robozombie.proxy.InvocationException)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 Executor (java.util.concurrent.Executor)2 Test (org.testng.annotations.Test)2