Search in sources :

Example 51 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.

the class GridCacheQueryTransformerSelfTest method testLocalInjection.

/**
 * @throws Exception If failed.
 */
@Test
public void testLocalInjection() throws Exception {
    IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
    try {
        for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
        Collection<List<Boolean>> lists = grid().compute().broadcast(new IgniteCallable<List<Boolean>>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public List<Boolean> call() throws Exception {
                IgniteClosure<Cache.Entry<Integer, Value>, Boolean> transformer = new IgniteClosure<Cache.Entry<Integer, Value>, Boolean>() {

                    @IgniteInstanceResource
                    Ignite ignite;

                    @Override
                    public Boolean apply(Cache.Entry<Integer, Value> e) {
                        return ignite != null;
                    }
                };
                return ignite.cache("test-cache").query(new ScanQuery<Integer, Value>().setLocal(true), transformer).getAll();
            }
        });
        List<Boolean> res = new ArrayList<>(F.flatCollections(lists));
        assertEquals(50, res.size());
        for (int i = 0; i < 50; i++) assertEquals(Boolean.TRUE, res.get(i));
    } finally {
        cache.destroy();
    }
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) ArrayList(java.util.ArrayList) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 52 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.

the class GridCacheQueryTransformerSelfTest method testLocal.

/**
 * @throws Exception If failed.
 */
@Test
public void testLocal() throws Exception {
    IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
    try {
        for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
        Collection<List<Integer>> lists = grid().compute().broadcast(new IgniteCallable<List<Integer>>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public List<Integer> call() throws Exception {
                IgniteClosure<Cache.Entry<Integer, Value>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, Value>, Integer>() {

                    @Override
                    public Integer apply(Cache.Entry<Integer, Value> e) {
                        return e.getValue().idx;
                    }
                };
                return ignite.cache("test-cache").query(new ScanQuery<Integer, Value>().setLocal(true), transformer).getAll();
            }
        });
        List<Integer> res = new ArrayList<>(F.flatCollections(lists));
        assertEquals(50, res.size());
        Collections.sort(res);
        for (int i = 0; i < 50; i++) assertEquals(i * 100, res.get(i).intValue());
    } finally {
        cache.destroy();
    }
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) ArrayList(java.util.ArrayList) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 53 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.

the class GridCacheQueryTransformerSelfTest method testLocalKeepBinaryFiltered.

/**
 * @throws Exception If failed.
 */
@Test
public void testLocalKeepBinaryFiltered() throws Exception {
    IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
    try {
        for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
        Collection<List<Integer>> lists = grid().compute().broadcast(new IgniteCallable<List<Integer>>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public List<Integer> call() throws Exception {
                IgniteBiPredicate<Integer, BinaryObject> filter = new IgniteBiPredicate<Integer, BinaryObject>() {

                    @Override
                    public boolean apply(Integer k, BinaryObject v) {
                        return v.<Integer>field("idx") % 1000 == 0;
                    }
                };
                IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer>() {

                    @Override
                    public Integer apply(Cache.Entry<Integer, BinaryObject> e) {
                        return e.getValue().field("idx");
                    }
                };
                return ignite.cache("test-cache").withKeepBinary().query(new ScanQuery<>(filter).setLocal(true), transformer).getAll();
            }
        });
        List<Integer> res = new ArrayList<>(F.flatCollections(lists));
        assertEquals(5, res.size());
        Collections.sort(res);
        for (int i = 0; i < 5; i++) assertEquals(i * 1000, res.get(i).intValue());
    } finally {
        cache.destroy();
    }
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) ArrayList(java.util.ArrayList) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) BinaryObject(org.apache.ignite.binary.BinaryObject) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 54 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.

the class GridCacheQueryTransformerSelfTest method testLocalKeepBinary.

/**
 * @throws Exception If failed.
 */
@Test
public void testLocalKeepBinary() throws Exception {
    IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
    try {
        for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
        Collection<List<Integer>> lists = grid().compute().broadcast(new IgniteCallable<List<Integer>>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public List<Integer> call() throws Exception {
                IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer>() {

                    @Override
                    public Integer apply(Cache.Entry<Integer, BinaryObject> e) {
                        return e.getValue().field("idx");
                    }
                };
                return ignite.cache("test-cache").withKeepBinary().query(new ScanQuery<Integer, BinaryObject>().setLocal(true), transformer).getAll();
            }
        });
        List<Integer> res = new ArrayList<>(F.flatCollections(lists));
        assertEquals(50, res.size());
        Collections.sort(res);
        for (int i = 0; i < 50; i++) assertEquals(i * 100, res.get(i).intValue());
    } finally {
        cache.destroy();
    }
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) ArrayList(java.util.ArrayList) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) BinaryObject(org.apache.ignite.binary.BinaryObject) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 55 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.

the class BaselineAutoAdjustTest method testExchangeMerge.

/**
 * Tests that merging exchanges properly triggers baseline changing.
 *
 * @throws Exception If failed.
 */
@Test
public void testExchangeMerge() throws Exception {
    // Latch that waits for PME (intTopVer == 3.0)
    CountDownLatch exchangeWorkerLatch = new CountDownLatch(1);
    // Lyficycle bean is needed in order to register EVT_NODE_JOIN lister that is called
    // right after GridCachePartitionExchangeManager and before GridClusterStateProcessor.
    lifecycleBean = new LifecycleBean() {

        /**
         * Ignite instance.
         */
        @IgniteInstanceResource
        IgniteEx ignite;

        /**
         * {@inheritDoc}
         */
        @Override
        public void onLifecycleEvent(LifecycleEventType evt) throws IgniteException {
            if (evt == LifecycleEventType.BEFORE_NODE_START) {
                ignite.context().internalSubscriptionProcessor().registerDistributedMetastorageListener(new DistributedMetastorageLifecycleListener() {

                    @Override
                    public void onReadyForRead(ReadableDistributedMetaStorage metastorage) {
                        ignite.context().event().addDiscoveryEventListener((evt, disco) -> {
                            if (evt.type() == EVT_NODE_JOINED && evt.topologyVersion() == 3) {
                                try {
                                    // Let's wait for exchange worker starts PME
                                    // that related to the first node joined the cluster.
                                    exchangeWorkerLatch.await(getTestTimeout(), MILLISECONDS);
                                } catch (InterruptedException e) {
                                    throw new IgniteException("exchangeWorkerLatch has been interrupted.", e);
                                }
                            }
                        }, EVT_NODE_JOINED);
                    }
                });
            }
        }
    };
    // Start the coordinator node.
    IgniteEx crd = startGrid(0);
    // This bean is only required on the coordinator node.
    lifecycleBean = null;
    // Latch indicates that EVT_NODE_JOINED (topVer == 4.0) was processed by all listeners.
    CountDownLatch nodeJoinLatch = new CountDownLatch(1);
    // This listener is the last one in the queue of handlers.
    crd.context().event().addDiscoveryEventListener((evt, disco) -> {
        if (evt.type() == EVT_NODE_JOINED && evt.topologyVersion() == 4)
            nodeJoinLatch.countDown();
    }, EVT_NODE_JOINED);
    IgniteEx nonCrd = startGrid(1);
    crd.cluster().state(ACTIVE);
    crd.cluster().baselineAutoAdjustEnabled(true);
    crd.cluster().baselineAutoAdjustTimeout(autoAdjustTimeout);
    awaitPartitionMapExchange(false, true, null);
    TestRecordingCommunicationSpi spi1 = TestRecordingCommunicationSpi.spi(nonCrd);
    spi1.blockMessages((node, msg) -> msg instanceof GridDhtPartitionsSingleMessage);
    // Target major topology version (4 nodes)
    long targetTopVer = 4;
    // Let's block exchange process in order to merge two following exchanges (3.0 && 4.0).
    crd.context().cache().context().exchange().mergeExchangesTestWaitVersion(new AffinityTopologyVersion(targetTopVer, 0), null);
    AtomicInteger cnt = new AtomicInteger(G.allGrids().size());
    runMultiThreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            startGrid(cnt.getAndIncrement());
            return null;
        }
    }, 2, "async-grid-starter");
    // Make sure that PME is in progress.
    assertTrue("Failed to wait for PME [topVer=3]", spi1.waitForBlocked(1, getTestTimeout()));
    assertTrue("Failed to wait for the first started exchange.", waitForCondition(() -> {
        GridDhtPartitionsExchangeFuture fut = crd.context().cache().context().exchange().lastTopologyFuture();
        return fut.initialVersion().topologyVersion() == 3;
    }, getTestTimeout()));
    // This guarantees that BaselineAutoAdjustData listens to real GridDhtPartitionsExchangeFuture
    // instead of readyAffinityFuture.
    exchangeWorkerLatch.countDown();
    assertTrue("Failed to wait for processing node join event [topVer=3]", nodeJoinLatch.await(getTestTimeout(), MILLISECONDS));
    // Unblock PME
    spi1.stopBlock();
    assertTrue("Failed to wait for changing baseline in " + autoAdjustTimeout * 2 + " ms.", waitForCondition(() -> crd.cluster().currentBaselineTopology().size() == targetTopVer, autoAdjustTimeout * 2));
}
Also used : ReadableDistributedMetaStorage(org.apache.ignite.internal.processors.metastorage.ReadableDistributedMetaStorage) GridDhtPartitionsExchangeFuture(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridDhtPartitionsSingleMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsSingleMessage) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) IgniteEx(org.apache.ignite.internal.IgniteEx) LifecycleBean(org.apache.ignite.lifecycle.LifecycleBean) DistributedMetastorageLifecycleListener(org.apache.ignite.internal.processors.metastorage.DistributedMetastorageLifecycleListener) LifecycleEventType(org.apache.ignite.lifecycle.LifecycleEventType) GridCommonTest(org.apache.ignite.testframework.junits.common.GridCommonTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)59 Ignite (org.apache.ignite.Ignite)48 Test (org.junit.Test)40 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)28 IgniteException (org.apache.ignite.IgniteException)24 IgniteEx (org.apache.ignite.internal.IgniteEx)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 IgniteCache (org.apache.ignite.IgniteCache)15 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)11 List (java.util.List)9 UUID (java.util.UUID)9 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)9 ClusterNode (org.apache.ignite.cluster.ClusterNode)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 IgniteLogger (org.apache.ignite.IgniteLogger)7 IgniteCallable (org.apache.ignite.lang.IgniteCallable)7 Callable (java.util.concurrent.Callable)6