Search in sources :

Example 1 with GridTuple

use of org.apache.ignite.internal.util.lang.GridTuple in project ignite by apache.

the class PlatformDotNetCacheStore method load.

/** {@inheritDoc} */
@Nullable
@Override
public V load(final K key) {
    try {
        final GridTuple<V> val = new GridTuple<>();
        doInvoke(new IgniteInClosureX<BinaryRawWriterEx>() {

            @Override
            public void applyx(BinaryRawWriterEx writer) throws IgniteCheckedException {
                writer.writeByte(OP_LOAD);
                writer.writeLong(session());
                writer.writeString(ses.cacheName());
                writer.writeObject(key);
            }
        }, new IgniteInClosureX<BinaryRawReaderEx>() {

            @Override
            public void applyx(BinaryRawReaderEx reader) {
                val.set((V) reader.readObjectDetached());
            }
        });
        return val.get();
    } catch (IgniteCheckedException e) {
        throw new CacheLoaderException(e);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheLoaderException(javax.cache.integration.CacheLoaderException) GridTuple(org.apache.ignite.internal.util.lang.GridTuple) BinaryRawReaderEx(org.apache.ignite.internal.binary.BinaryRawReaderEx) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with GridTuple

use of org.apache.ignite.internal.util.lang.GridTuple in project ignite by apache.

the class GridCacheAbstractRemoveFailureTest method putAndRemove.

/**
     * @param duration Test duration.
     * @param txConcurrency Transaction concurrency if test explicit transaction.
     * @param txIsolation Transaction isolation if test explicit transaction.
     * @throws Exception If failed.
     */
private void putAndRemove(long duration, final TransactionConcurrency txConcurrency, final TransactionIsolation txIsolation) throws Exception {
    assertEquals(testClientNode(), (boolean) grid(0).configuration().isClientMode());
    grid(0).destroyCache(DEFAULT_CACHE_NAME);
    CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setCacheMode(cacheMode());
    if (cacheMode() == PARTITIONED)
        ccfg.setBackups(1);
    ccfg.setAtomicityMode(atomicityMode());
    ccfg.setNearConfiguration(nearCache());
    final IgniteCache<Integer, Integer> sndCache0 = grid(0).createCache(ccfg);
    final AtomicBoolean stop = new AtomicBoolean();
    final AtomicLong cntr = new AtomicLong();
    final AtomicLong errCntr = new AtomicLong();
    // Expected values in cache.
    final Map<Integer, GridTuple<Integer>> expVals = new ConcurrentHashMap8<>();
    final AtomicReference<CyclicBarrier> cmp = new AtomicReference<>();
    IgniteInternalFuture<?> updateFut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Thread.currentThread().setName("update-thread");
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            IgniteTransactions txs = sndCache0.unwrap(Ignite.class).transactions();
            while (!stop.get()) {
                for (int i = 0; i < 100; i++) {
                    int key = rnd.nextInt(KEYS_CNT);
                    boolean put = rnd.nextInt(0, 100) > 10;
                    while (true) {
                        try {
                            if (put) {
                                boolean failed = false;
                                if (txConcurrency != null) {
                                    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
                                        sndCache0.put(key, i);
                                        tx.commit();
                                    } catch (CacheException | IgniteException e) {
                                        if (!X.hasCause(e, ClusterTopologyCheckedException.class)) {
                                            log.error("Unexpected error: " + e);
                                            throw e;
                                        }
                                        failed = true;
                                    }
                                } else
                                    sndCache0.put(key, i);
                                if (!failed)
                                    expVals.put(key, F.t(i));
                            } else {
                                boolean failed = false;
                                if (txConcurrency != null) {
                                    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
                                        sndCache0.remove(key);
                                        tx.commit();
                                    } catch (CacheException | IgniteException e) {
                                        if (!X.hasCause(e, ClusterTopologyCheckedException.class)) {
                                            log.error("Unexpected error: " + e);
                                            throw e;
                                        }
                                        failed = true;
                                    }
                                } else
                                    sndCache0.remove(key);
                                if (!failed)
                                    expVals.put(key, F.<Integer>t(null));
                            }
                            break;
                        } catch (CacheException e) {
                            if (put)
                                log.error("Put failed [key=" + key + ", val=" + i + ']', e);
                            else
                                log.error("Remove failed [key=" + key + ']', e);
                            errCntr.incrementAndGet();
                        }
                    }
                }
                cntr.addAndGet(100);
                CyclicBarrier barrier = cmp.get();
                if (barrier != null) {
                    log.info("Wait data check.");
                    barrier.await(60_000, TimeUnit.MILLISECONDS);
                    log.info("Finished wait data check.");
                }
            }
            return null;
        }
    });
    IgniteInternalFuture<?> killFut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Thread.currentThread().setName("restart-thread");
            while (!stop.get()) {
                U.sleep(random(KILL_DELAY.get1(), KILL_DELAY.get2()));
                killAndRestart(stop);
                CyclicBarrier barrier = cmp.get();
                if (barrier != null) {
                    log.info("Wait data check.");
                    barrier.await(60_000, TimeUnit.MILLISECONDS);
                    log.info("Finished wait data check.");
                }
            }
            return null;
        }
    });
    try {
        long stopTime = duration + U.currentTimeMillis();
        long nextAssert = U.currentTimeMillis() + ASSERT_FREQ;
        while (U.currentTimeMillis() < stopTime) {
            long start = System.nanoTime();
            long ops = cntr.longValue();
            U.sleep(1000);
            long diff = cntr.longValue() - ops;
            double time = (System.nanoTime() - start) / 1_000_000_000d;
            long opsPerSecond = (long) (diff / time);
            log.info("Operations/second: " + opsPerSecond);
            if (U.currentTimeMillis() >= nextAssert) {
                CyclicBarrier barrier = new CyclicBarrier(3, new Runnable() {

                    @Override
                    public void run() {
                        try {
                            cmp.set(null);
                            log.info("Checking cache content.");
                            assertCacheContent(expVals);
                            log.info("Finished check cache content.");
                        } catch (Throwable e) {
                            log.error("Unexpected error: " + e, e);
                            throw e;
                        }
                    }
                });
                log.info("Start cache content check.");
                cmp.set(barrier);
                try {
                    barrier.await(60_000, TimeUnit.MILLISECONDS);
                } catch (TimeoutException e) {
                    U.dumpThreads(log);
                    fail("Failed to check cache content: " + e);
                }
                log.info("Cache content check done.");
                nextAssert = System.currentTimeMillis() + ASSERT_FREQ;
            }
        }
    } finally {
        stop.set(true);
    }
    killFut.get();
    updateFut.get();
    log.info("Test finished. Update errors: " + errCntr.get());
}
Also used : CacheException(javax.cache.CacheException) IgniteTransactions(org.apache.ignite.IgniteTransactions) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) TimeoutException(java.util.concurrent.TimeoutException) ConcurrentHashMap8(org.jsr166.ConcurrentHashMap8) GridTuple(org.apache.ignite.internal.util.lang.GridTuple) AtomicReference(java.util.concurrent.atomic.AtomicReference) TimeoutException(java.util.concurrent.TimeoutException) CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) Transaction(org.apache.ignite.transactions.Transaction) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 3 with GridTuple

use of org.apache.ignite.internal.util.lang.GridTuple in project ignite by apache.

the class GridMarshallerPerformanceTest method testGridMarshaller.

/**
     * @throws Exception If failed.
     */
public void testGridMarshaller() throws Exception {
    final GridTuple<byte[]> tuple = new GridTuple<>();
    final BinaryMarshaller marsh = createStandaloneBinaryMarshaller();
    IgniteInClosure<TestObject> writer = new CIX1<TestObject>() {

        @Override
        public void applyx(TestObject obj) throws IgniteCheckedException {
            tuple.set(marsh.marshal(obj));
        }
    };
    IgniteOutClosure<TestObject> reader = new COX<TestObject>() {

        @Override
        public TestObject applyx() throws IgniteCheckedException {
            return marsh.unmarshal(tuple.get(), null);
        }
    };
    runTest("GridMarshaller", writer, reader);
}
Also used : CIX1(org.apache.ignite.internal.util.typedef.CIX1) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) GridTuple(org.apache.ignite.internal.util.lang.GridTuple) COX(org.apache.ignite.internal.util.typedef.COX)

Example 4 with GridTuple

use of org.apache.ignite.internal.util.lang.GridTuple in project ignite by apache.

the class GridDeploymentCommunication method sendResourceRequest.

/**
     * Sends request to the remote node and wait for response. If there is
     * no response until threshold time, method returns null.
     *
     *
     * @param rsrcName Resource name.
     * @param clsLdrId Class loader ID.
     * @param dstNode Remote node request should be sent to.
     * @param threshold Time in milliseconds when request is decided to
     *      be obsolete.
     * @return Either response value or {@code null} if timeout occurred.
     * @throws IgniteCheckedException Thrown if there is no connection with remote node.
     */
@SuppressWarnings({ "SynchronizationOnLocalVariableOrMethodParameter" })
GridDeploymentResponse sendResourceRequest(final String rsrcName, IgniteUuid clsLdrId, final ClusterNode dstNode, long threshold) throws IgniteCheckedException {
    assert rsrcName != null;
    assert dstNode != null;
    assert clsLdrId != null;
    Collection<UUID> nodeIds = activeReqNodeIds.get();
    if (nodeIds != null && nodeIds.contains(dstNode.id())) {
        if (log.isDebugEnabled())
            log.debug("Node attempts to load resource from one of the requesters " + "[rsrcName=" + rsrcName + ", dstNodeId=" + dstNode.id() + ", requesters=" + nodeIds + ']');
        GridDeploymentResponse fake = new GridDeploymentResponse();
        fake.success(false);
        fake.errorMessage("Node attempts to load resource from one of the requesters " + "[rsrcName=" + rsrcName + ", dstNodeId=" + dstNode.id() + ", requesters=" + nodeIds + ']');
        return fake;
    }
    Object resTopic = TOPIC_CLASSLOAD.topic(IgniteUuid.fromUuid(ctx.localNodeId()));
    GridDeploymentRequest req = new GridDeploymentRequest(resTopic, clsLdrId, rsrcName, false);
    // Send node IDs chain with request.
    req.nodeIds(nodeIds);
    final Object qryMux = new Object();
    final GridTuple<GridDeploymentResponse> res = new GridTuple<>();
    GridLocalEventListener discoLsnr = new GridLocalEventListener() {

        @Override
        public void onEvent(Event evt) {
            assert evt instanceof DiscoveryEvent;
            assert evt.type() == EVT_NODE_LEFT || evt.type() == EVT_NODE_FAILED;
            DiscoveryEvent discoEvt = (DiscoveryEvent) evt;
            UUID nodeId = discoEvt.eventNode().id();
            if (!nodeId.equals(dstNode.id()))
                // Not a destination node.
                return;
            GridDeploymentResponse fake = new GridDeploymentResponse();
            String errMsg = "Originating node left grid (resource will not be peer loaded) " + "[nodeId=" + dstNode.id() + ", rsrc=" + rsrcName + ']';
            U.warn(log, errMsg);
            fake.success(false);
            fake.errorMessage(errMsg);
            // because originating node has left grid.
            synchronized (qryMux) {
                res.set(fake);
                qryMux.notifyAll();
            }
        }
    };
    GridMessageListener resLsnr = new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg) {
            assert nodeId != null;
            assert msg != null;
            synchronized (qryMux) {
                if (!(msg instanceof GridDeploymentResponse)) {
                    U.error(log, "Received unknown peer class loading response [node=" + nodeId + ", msg=" + msg + ']');
                } else
                    res.set((GridDeploymentResponse) msg);
                qryMux.notifyAll();
            }
        }
    };
    try {
        ctx.io().addMessageListener(resTopic, resLsnr);
        // The destination node has potentially left grid here but in this case
        // Communication manager will throw the exception while sending message.
        ctx.event().addLocalEventListener(discoLsnr, EVT_NODE_FAILED, EVT_NODE_LEFT);
        long start = U.currentTimeMillis();
        if (req.responseTopic() != null && !ctx.localNodeId().equals(dstNode.id()))
            req.responseTopicBytes(U.marshal(marsh, req.responseTopic()));
        ctx.io().sendToGridTopic(dstNode, TOPIC_CLASSLOAD, req, GridIoPolicy.P2P_POOL);
        if (log.isDebugEnabled())
            log.debug("Sent peer class loading request [node=" + dstNode.id() + ", req=" + req + ']');
        synchronized (qryMux) {
            try {
                long timeout = threshold - start;
                if (log.isDebugEnabled()) {
                    log.debug("Waiting for peer response from node [node=" + dstNode.id() + ", timeout=" + timeout + ']');
                }
                while (res.get() == null && timeout > 0) {
                    qryMux.wait(timeout);
                    timeout = threshold - U.currentTimeMillis();
                }
            } catch (InterruptedException e) {
                // Interrupt again to get it in the users code.
                Thread.currentThread().interrupt();
                throw new IgniteCheckedException("Got interrupted while waiting for response from node: " + dstNode.id(), e);
            }
        }
        if (res.get() == null) {
            U.warn(log, "Failed to receive peer response from node within duration [node=" + dstNode.id() + ", duration=" + (U.currentTimeMillis() - start) + ']');
        } else if (log.isDebugEnabled())
            log.debug("Received peer loading response [node=" + dstNode.id() + ", res=" + res.get() + ']');
        return res.get();
    } finally {
        ctx.event().removeLocalEventListener(discoLsnr, EVT_NODE_FAILED, EVT_NODE_LEFT);
        ctx.io().removeMessageListener(resTopic, resLsnr);
    }
}
Also used : GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) GridTuple(org.apache.ignite.internal.util.lang.GridTuple) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Event(org.apache.ignite.events.Event) UUID(java.util.UUID)

Example 5 with GridTuple

use of org.apache.ignite.internal.util.lang.GridTuple in project ignite by apache.

the class GridCacheAbstractRemoveFailureTest method assertCacheContent.

/**
     * @param expVals Expected values in cache.
     */
@SuppressWarnings({ "TooBroadScope", "ConstantIfStatement" })
private void assertCacheContent(Map<Integer, GridTuple<Integer>> expVals) {
    assert !expVals.isEmpty();
    Collection<Integer> failedKeys = new HashSet<>();
    for (int i = 0; i < GRID_CNT; i++) {
        Ignite ignite = grid(i);
        IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME);
        for (Map.Entry<Integer, GridTuple<Integer>> expVal : expVals.entrySet()) {
            Integer val = cache.get(expVal.getKey());
            if (!F.eq(expVal.getValue().get(), val)) {
                failedKeys.add(expVal.getKey());
                boolean primary = affinity(cache).isPrimary(ignite.cluster().localNode(), expVal.getKey());
                boolean backup = affinity(cache).isBackup(ignite.cluster().localNode(), expVal.getKey());
                log.error("Unexpected cache data [exp=" + expVal + ", actual=" + val + ", nodePrimary=" + primary + ", nodeBackup=" + backup + ", nodeIdx" + i + ", nodeId=" + ignite.cluster().localNode().id() + ']');
            }
        }
    }
    assertTrue("Unexpected data for keys: " + failedKeys, failedKeys.isEmpty());
}
Also used : GridTuple(org.apache.ignite.internal.util.lang.GridTuple) Ignite(org.apache.ignite.Ignite) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

GridTuple (org.apache.ignite.internal.util.lang.GridTuple)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteException (org.apache.ignite.IgniteException)2 HashSet (java.util.HashSet)1 Map (java.util.Map)1 UUID (java.util.UUID)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CacheException (javax.cache.CacheException)1 CacheLoaderException (javax.cache.integration.CacheLoaderException)1 Ignite (org.apache.ignite.Ignite)1 IgniteTransactions (org.apache.ignite.IgniteTransactions)1 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)1 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)1 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)1