Search in sources :

Example 11 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class GridTaskCommandHandler method handleAsyncUnsafe.

/**
     * @param req Request.
     * @return Future.
     * @throws IgniteCheckedException On any handling exception.
     */
private IgniteInternalFuture<GridRestResponse> handleAsyncUnsafe(final GridRestRequest req) throws IgniteCheckedException {
    assert req instanceof GridRestTaskRequest : "Invalid command for topology handler: " + req;
    assert SUPPORTED_COMMANDS.contains(req.command());
    if (log.isDebugEnabled())
        log.debug("Handling task REST request: " + req);
    GridRestTaskRequest req0 = (GridRestTaskRequest) req;
    final GridFutureAdapter<GridRestResponse> fut = new GridFutureAdapter<>();
    final GridRestResponse res = new GridRestResponse();
    final GridClientTaskResultBean taskRestRes = new GridClientTaskResultBean();
    // Set ID placeholder for the case it wouldn't be available due to remote execution.
    taskRestRes.setId('~' + ctx.localNodeId().toString());
    final boolean locExec = req0.destinationId() == null || req0.destinationId().equals(ctx.localNodeId()) || ctx.discovery().node(req0.destinationId()) == null;
    switch(req.command()) {
        case EXE:
            {
                final boolean async = req0.async();
                final String name = req0.taskName();
                if (F.isEmpty(name))
                    throw new IgniteCheckedException(missingParameter("name"));
                final List<Object> params = req0.params();
                long timeout = req0.timeout();
                final UUID clientId = req.clientId();
                final IgniteInternalFuture<Object> taskFut;
                if (locExec) {
                    ctx.task().setThreadContextIfNotNull(TC_SUBJ_ID, clientId);
                    ctx.task().setThreadContext(TC_TIMEOUT, timeout);
                    Object arg = !F.isEmpty(params) ? params.size() == 1 ? params.get(0) : params.toArray() : null;
                    taskFut = ctx.task().execute(name, arg);
                } else {
                    // Using predicate instead of node intentionally
                    // in order to provide user well-structured EmptyProjectionException.
                    ClusterGroup prj = ctx.grid().cluster().forPredicate(F.nodeForNodeId(req.destinationId()));
                    ctx.task().setThreadContext(TC_NO_FAILOVER, true);
                    taskFut = ctx.closure().callAsync(BALANCE, new ExeCallable(name, params, timeout, clientId), prj.nodes());
                }
                if (async) {
                    if (locExec) {
                        IgniteUuid tid = ((ComputeTaskInternalFuture) taskFut).getTaskSession().getId();
                        taskDescs.put(tid, new TaskDescriptor(false, null, null));
                        taskRestRes.setId(tid.toString() + '~' + ctx.localNodeId().toString());
                        res.setResponse(taskRestRes);
                    } else
                        res.setError("Asynchronous task execution is not supported for routing request.");
                    fut.onDone(res);
                }
                taskFut.listen(new IgniteInClosure<IgniteInternalFuture<Object>>() {

                    @Override
                    public void apply(IgniteInternalFuture<Object> taskFut) {
                        try {
                            TaskDescriptor desc;
                            try {
                                desc = new TaskDescriptor(true, taskFut.get(), null);
                            } catch (IgniteCheckedException e) {
                                if (e.hasCause(ClusterTopologyCheckedException.class, ClusterGroupEmptyCheckedException.class))
                                    U.warn(log, "Failed to execute task due to topology issues (are all mapped " + "nodes alive?) [name=" + name + ", clientId=" + req.clientId() + ", err=" + e + ']');
                                else {
                                    if (!X.hasCause(e, VisorClusterGroupEmptyException.class))
                                        U.error(log, "Failed to execute task [name=" + name + ", clientId=" + req.clientId() + ']', e);
                                }
                                desc = new TaskDescriptor(true, null, e);
                            }
                            if (async && locExec) {
                                assert taskFut instanceof ComputeTaskInternalFuture;
                                IgniteUuid tid = ((ComputeTaskInternalFuture) taskFut).getTaskSession().getId();
                                taskDescs.put(tid, desc);
                            }
                            if (!async) {
                                if (desc.error() == null) {
                                    try {
                                        taskRestRes.setFinished(true);
                                        taskRestRes.setResult(desc.result());
                                        res.setResponse(taskRestRes);
                                        fut.onDone(res);
                                    } catch (IgniteException e) {
                                        fut.onDone(new IgniteCheckedException("Failed to marshal task result: " + desc.result(), e));
                                    }
                                } else
                                    fut.onDone(desc.error());
                            }
                        } finally {
                            if (!async && !fut.isDone())
                                fut.onDone(new IgniteCheckedException("Failed to execute task (see server logs for details)."));
                        }
                    }
                });
                break;
            }
        case RESULT:
            {
                String id = req0.taskId();
                if (F.isEmpty(id))
                    throw new IgniteCheckedException(missingParameter("id"));
                StringTokenizer st = new StringTokenizer(id, "~");
                if (st.countTokens() != 2)
                    throw new IgniteCheckedException("Failed to parse id parameter: " + id);
                String tidParam = st.nextToken();
                String resHolderIdParam = st.nextToken();
                taskRestRes.setId(id);
                try {
                    IgniteUuid tid = !F.isEmpty(tidParam) ? IgniteUuid.fromString(tidParam) : null;
                    UUID resHolderId = !F.isEmpty(resHolderIdParam) ? UUID.fromString(resHolderIdParam) : null;
                    if (tid == null || resHolderId == null)
                        throw new IgniteCheckedException("Failed to parse id parameter: " + id);
                    if (ctx.localNodeId().equals(resHolderId)) {
                        TaskDescriptor desc = taskDescs.get(tid);
                        if (desc == null)
                            throw new IgniteCheckedException("Task with provided id has never been started on provided node" + " [taskId=" + tidParam + ", taskResHolderId=" + resHolderIdParam + ']');
                        taskRestRes.setFinished(desc.finished());
                        if (desc.error() != null)
                            throw new IgniteCheckedException(desc.error().getMessage());
                        taskRestRes.setResult(desc.result());
                        res.setResponse(taskRestRes);
                    } else {
                        IgniteBiTuple<String, GridTaskResultResponse> t = requestTaskResult(resHolderId, tid);
                        if (t.get1() != null)
                            throw new IgniteCheckedException(t.get1());
                        GridTaskResultResponse taskRes = t.get2();
                        assert taskRes != null;
                        if (!taskRes.found())
                            throw new IgniteCheckedException("Task with provided id has never been started on provided node " + "[taskId=" + tidParam + ", taskResHolderId=" + resHolderIdParam + ']');
                        taskRestRes.setFinished(taskRes.finished());
                        if (taskRes.error() != null)
                            throw new IgniteCheckedException(taskRes.error());
                        taskRestRes.setResult(taskRes.result());
                        res.setResponse(taskRestRes);
                    }
                } catch (IllegalArgumentException e) {
                    String msg = "Failed to parse parameters [taskId=" + tidParam + ", taskResHolderId=" + resHolderIdParam + ", err=" + e.getMessage() + ']';
                    if (log.isDebugEnabled())
                        log.debug(msg);
                    throw new IgniteCheckedException(msg, e);
                }
                fut.onDone(res);
                break;
            }
        case NOOP:
            {
                fut.onDone(new GridRestResponse());
                break;
            }
        default:
            assert false : "Invalid command for task handler: " + req;
    }
    if (log.isDebugEnabled())
        log.debug("Handled task REST request [res=" + res + ", req=" + req + ']');
    return fut;
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridClientTaskResultBean(org.apache.ignite.internal.processors.rest.client.message.GridClientTaskResultBean) GridRestTaskRequest(org.apache.ignite.internal.processors.rest.request.GridRestTaskRequest) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteUuid(org.apache.ignite.lang.IgniteUuid) IgniteException(org.apache.ignite.IgniteException) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) List(java.util.List) UUID(java.util.UUID) ComputeTaskInternalFuture(org.apache.ignite.internal.ComputeTaskInternalFuture) StringTokenizer(java.util.StringTokenizer) ClusterGroupEmptyCheckedException(org.apache.ignite.internal.cluster.ClusterGroupEmptyCheckedException) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 12 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class GridTaskCommandHandler method requestTaskResult.

/**
     * @param resHolderId Result holder.
     * @param taskId Task ID.
     * @return Response from task holder.
     */
private IgniteBiTuple<String, GridTaskResultResponse> requestTaskResult(final UUID resHolderId, IgniteUuid taskId) {
    ClusterNode taskNode = ctx.discovery().node(resHolderId);
    if (taskNode == null)
        return F.t("Task result holder has left grid: " + resHolderId, null);
    // Tuple: error message-response.
    final IgniteBiTuple<String, GridTaskResultResponse> 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) {
            String err = null;
            GridTaskResultResponse res = null;
            if (!(msg instanceof GridTaskResultResponse))
                err = "Received unexpected message: " + msg;
            else if (!nodeId.equals(resHolderId))
                err = "Received task result response from unexpected node [resHolderId=" + resHolderId + ", nodeId=" + nodeId + ']';
            else
                // Sender and message type are fine.
                res = (GridTaskResultResponse) msg;
            try {
                res.result(U.unmarshal(ctx, res.resultBytes(), U.resolveClassLoader(ctx.config())));
            } catch (IgniteCheckedException e) {
                U.error(log, "Failed to unmarshal task result: " + res, e);
            }
            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 (resHolderId.equals(discoEvt.eventNode().id())) {
                lock.lock();
                try {
                    if (t.isEmpty()) {
                        t.set("Node that originated task execution has left grid: " + resHolderId, null);
                        cond.signalAll();
                    }
                } finally {
                    lock.unlock();
                }
            }
        }
    };
    // 1. Create unique topic name and register listener.
    Object topic = TOPIC_REST.topic("task-result", topicIdGen.getAndIncrement());
    try {
        ctx.io().addMessageListener(topic, msgLsnr);
        // 2. Send message.
        try {
            byte[] topicBytes = U.marshal(ctx, topic);
            ctx.io().sendToGridTopic(taskNode, TOPIC_REST, new GridTaskResultRequest(taskId, topic, topicBytes), SYSTEM_POOL);
        } catch (IgniteCheckedException e) {
            String errMsg = "Failed to send task result request [resHolderId=" + resHolderId + ", err=" + e.getMessage() + ']';
            if (log.isDebugEnabled())
                log.debug(errMsg);
            return F.t(errMsg, null);
        }
        // 3. Listen to discovery events.
        ctx.event().addLocalEventListener(discoLsnr, EVT_NODE_FAILED, EVT_NODE_LEFT);
        // 4. Check whether node has left before disco listener has been installed.
        taskNode = ctx.discovery().node(resHolderId);
        if (taskNode == null)
            return F.t("Task result holder has left grid: " + resHolderId, null);
        // 5. Wait for result.
        lock.lock();
        try {
            long netTimeout = ctx.config().getNetworkTimeout();
            if (t.isEmpty())
                cond.await(netTimeout, MILLISECONDS);
            if (t.isEmpty())
                t.set1("Timed out waiting for task result (consider increasing 'networkTimeout' " + "configuration property) [resHolderId=" + resHolderId + ", netTimeout=" + netTimeout + ']');
            // Return result
            return t;
        } catch (InterruptedException ignored) {
            Thread.currentThread().interrupt();
            return F.t("Interrupted while waiting for task result.", null);
        } 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) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) UUID(java.util.UUID)

Example 13 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class IgniteUtils method addLog4jNoOpLogger.

/**
     * Adds no-op logger to remove no-appender warning.
     *
     * @return Tuple with root log and no-op appender instances. No-op appender can be {@code null}
     *      if it did not found in classpath. Notice that in this case logging is not suppressed.
     * @throws IgniteCheckedException In case of failure to add no-op logger for Log4j.
     */
public static IgniteBiTuple<Object, Object> addLog4jNoOpLogger() throws IgniteCheckedException {
    Object rootLog;
    Object nullApp;
    try {
        // Add no-op logger to remove no-appender warning.
        Class<?> logCls = Class.forName("org.apache.log4j.Logger");
        rootLog = logCls.getMethod("getRootLogger").invoke(logCls);
        try {
            nullApp = Class.forName("org.apache.log4j.varia.NullAppender").newInstance();
        } catch (ClassNotFoundException ignore) {
            // log4j-over-slf4j library. No-appender warning will not be suppressed.
            return new IgniteBiTuple<>(rootLog, null);
        }
        Class appCls = Class.forName("org.apache.log4j.Appender");
        rootLog.getClass().getMethod("addAppender", appCls).invoke(rootLog, nullApp);
    } catch (Exception e) {
        throw new IgniteCheckedException("Failed to add no-op logger for Log4j.", e);
    }
    return new IgniteBiTuple<>(rootLog, nullApp);
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ClusterGroupEmptyException(org.apache.ignite.cluster.ClusterGroupEmptyException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) JMException(javax.management.JMException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) ComputeTaskCancelledException(org.apache.ignite.compute.ComputeTaskCancelledException) ComputeTaskTimeoutException(org.apache.ignite.compute.ComputeTaskTimeoutException) IgniteFutureCancelledException(org.apache.ignite.lang.IgniteFutureCancelledException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException) URISyntaxException(java.net.URISyntaxException) TransactionHeuristicException(org.apache.ignite.transactions.TransactionHeuristicException) ComputeTaskCancelledCheckedException(org.apache.ignite.internal.compute.ComputeTaskCancelledCheckedException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) ComputeTaskTimeoutCheckedException(org.apache.ignite.internal.compute.ComputeTaskTimeoutCheckedException) NoSuchElementException(java.util.NoSuchElementException) IgniteTxHeuristicCheckedException(org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) CancellationException(java.util.concurrent.CancellationException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) KeyManagementException(java.security.KeyManagementException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedObjectNameException(javax.management.MalformedObjectNameException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClusterGroupEmptyCheckedException(org.apache.ignite.internal.cluster.ClusterGroupEmptyCheckedException) SocketException(java.net.SocketException) SQLException(java.sql.SQLException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteFutureTimeoutException(org.apache.ignite.lang.IgniteFutureTimeoutException) NamingException(javax.naming.NamingException) IgniteFutureCancelledCheckedException(org.apache.ignite.internal.IgniteFutureCancelledCheckedException) IgniteDeploymentCheckedException(org.apache.ignite.internal.IgniteDeploymentCheckedException) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) IgniteException(org.apache.ignite.IgniteException) TransactionDeadlockException(org.apache.ignite.transactions.TransactionDeadlockException) FileNotFoundException(java.io.FileNotFoundException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) MalformedURLException(java.net.MalformedURLException) IgniteDeploymentException(org.apache.ignite.IgniteDeploymentException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException)

Example 14 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class ServerImpl method pingNode.

/**
     * Pings the node by its address to see if it's alive.
     *
     * @param addr Address of the node.
     * @param nodeId Node ID to ping. In case when client node ID is not null this node ID is an ID of the router node.
     * @param clientNodeId Client node ID.
     * @return ID of the remote node and "client exists" flag if node alive or {@code null} if the remote node has
     *         left a topology during the ping process.
     * @throws IgniteCheckedException If an error occurs.
     */
@Nullable
private IgniteBiTuple<UUID, Boolean> pingNode(InetSocketAddress addr, @Nullable UUID nodeId, @Nullable UUID clientNodeId) throws IgniteCheckedException {
    assert addr != null;
    UUID locNodeId = getLocalNodeId();
    IgniteSpiOperationTimeoutHelper timeoutHelper = new IgniteSpiOperationTimeoutHelper(spi, clientNodeId == null);
    if (F.contains(spi.locNodeAddrs, addr)) {
        if (clientNodeId == null)
            return F.t(getLocalNodeId(), false);
        ClientMessageWorker clientWorker = clientMsgWorkers.get(clientNodeId);
        if (clientWorker == null)
            return F.t(getLocalNodeId(), false);
        boolean clientPingRes;
        try {
            clientPingRes = clientWorker.ping(timeoutHelper);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new IgniteInterruptedCheckedException(e);
        }
        return F.t(getLocalNodeId(), clientPingRes);
    }
    GridPingFutureAdapter<IgniteBiTuple<UUID, Boolean>> fut = new GridPingFutureAdapter<>();
    GridPingFutureAdapter<IgniteBiTuple<UUID, Boolean>> oldFut = pingMap.putIfAbsent(addr, fut);
    if (oldFut != null)
        return oldFut.get();
    else {
        Collection<Throwable> errs = null;
        try {
            Socket sock = null;
            int reconCnt = 0;
            boolean openedSock = false;
            while (true) {
                try {
                    if (addr.isUnresolved())
                        addr = new InetSocketAddress(InetAddress.getByName(addr.getHostName()), addr.getPort());
                    long tstamp = U.currentTimeMillis();
                    sock = spi.createSocket();
                    fut.sock = sock;
                    sock = spi.openSocket(sock, addr, timeoutHelper);
                    openedSock = true;
                    spi.writeToSocket(sock, new TcpDiscoveryPingRequest(locNodeId, clientNodeId), timeoutHelper.nextTimeoutChunk(spi.getSocketTimeout()));
                    TcpDiscoveryPingResponse res = spi.readMessage(sock, null, timeoutHelper.nextTimeoutChunk(spi.getAckTimeout()));
                    if (locNodeId.equals(res.creatorNodeId())) {
                        if (log.isDebugEnabled())
                            log.debug("Ping response from local node: " + res);
                        break;
                    }
                    spi.stats.onClientSocketInitialized(U.currentTimeMillis() - tstamp);
                    IgniteBiTuple<UUID, Boolean> t = F.t(res.creatorNodeId(), res.clientExists());
                    fut.onDone(t);
                    return t;
                } catch (IOException | IgniteCheckedException e) {
                    if (nodeId != null && !nodeAlive(nodeId)) {
                        if (log.isDebugEnabled())
                            log.debug("Failed to ping the node (has left or leaving topology): [nodeId=" + nodeId + ']');
                        fut.onDone((IgniteBiTuple<UUID, Boolean>) null);
                        return null;
                    }
                    if (errs == null)
                        errs = new ArrayList<>();
                    errs.add(e);
                    reconCnt++;
                    if (!openedSock && reconCnt == 2)
                        break;
                    if (timeoutHelper.checkFailureTimeoutReached(e))
                        break;
                    else if (!spi.failureDetectionTimeoutEnabled() && reconCnt == spi.getReconnectCount())
                        break;
                } finally {
                    U.closeQuiet(sock);
                }
            }
        } catch (Throwable t) {
            fut.onDone(t);
            if (t instanceof Error)
                throw t;
            throw U.cast(t);
        } finally {
            if (!fut.isDone())
                fut.onDone(U.exceptionWithSuppressed("Failed to ping node by address: " + addr, errs));
            boolean b = pingMap.remove(addr, fut);
            assert b;
        }
        return fut.get();
    }
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) InetSocketAddress(java.net.InetSocketAddress) TcpDiscoveryPingResponse(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryPingResponse) IOException(java.io.IOException) IgniteSpiOperationTimeoutHelper(org.apache.ignite.spi.IgniteSpiOperationTimeoutHelper) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) UUID(java.util.UUID) TcpDiscoveryPingRequest(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryPingRequest) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class GridOffHeapPartitionedMapAbstractSelfTest method testIterator.

/**
     * @throws Exception If failed.
     */
public void testIterator() throws Exception {
    initCap = 10;
    map = newMap();
    final AtomicInteger rehashes = new AtomicInteger();
    final AtomicInteger releases = new AtomicInteger();
    map.eventListener(new GridOffHeapEventListener() {

        @Override
        public void onEvent(GridOffHeapEvent evt) {
            switch(evt) {
                case REHASH:
                    rehashes.incrementAndGet();
                    break;
                case RELEASE:
                    releases.incrementAndGet();
                    break;
                // No-op.
                default:
            }
        }
    });
    int max = 1024;
    Map<String, String> m = new HashMap<>(max * parts);
    for (int p = 0; p < parts; p++) {
        for (int i = 0; i < max; i++) {
            String key = string();
            String val = string();
            // info("Storing [i=" + i + ", key=" + key + ", val=" + val + ']');
            assertTrue(map.put(p, hash(key), key.getBytes(), val.getBytes()));
            assertTrue(map.contains(p, hash(key), key.getBytes()));
            assertNotNull(map.get(p, hash(key), key.getBytes()));
            assertEquals(new String(map.get(p, hash(key), key.getBytes())), val);
            m.put(key, val);
        }
    }
    int cnt = 0;
    try (GridCloseableIterator<IgniteBiTuple<byte[], byte[]>> it = map.iterator()) {
        while (it.hasNext()) {
            IgniteBiTuple<byte[], byte[]> t = it.next();
            String k = new String(t.get1());
            String v = new String(t.get2());
            // info("Entry [k=" + k + ", v=" + v + ']');
            assertEquals(m.get(k), v);
            cnt++;
        }
    }
    assertEquals(map.size(), cnt);
    assertEquals(max * parts, map.size());
    info("Stats [size=" + map.size() + ", rehashes=" + rehashes + ", releases=" + releases + ']');
    assertTrue(rehashes.get() > 0);
    assertEquals(rehashes.get(), releases.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple)

Aggregations

IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)64 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)35 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 IgniteException (org.apache.ignite.IgniteException)17 ArrayList (java.util.ArrayList)14 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)13 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)13 ClusterNode (org.apache.ignite.cluster.ClusterNode)11 HashMap (java.util.HashMap)10 UUID (java.util.UUID)10 IOException (java.io.IOException)9 List (java.util.List)9 Map (java.util.Map)8 CacheStorePartialUpdateException (org.apache.ignite.internal.processors.cache.CacheStorePartialUpdateException)8 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)8 LinkedHashMap (java.util.LinkedHashMap)7 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Event (org.apache.ignite.events.Event)6 IgfsPath (org.apache.ignite.igfs.IgfsPath)6