Search in sources :

Example 11 with IgniteInstanceResource

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

the class IgfsSecondaryFileSystemInjectionSelfTest method testInjectPrimaryByField.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings({ "UnusedDeclaration" })
public void testInjectPrimaryByField() throws Exception {
    secondary = new TestBaseSecondaryFsMock() {

        @FileSystemResource
        private IgfsImpl igfs;

        @LoggerResource
        private IgniteLogger log;

        @IgniteInstanceResource
        private Ignite ig;

        @Override
        boolean checkInjection(Ignite ignite, IgniteFileSystem primary) {
            return igfs == primary && log instanceof IgniteLogger && ig == ignite;
        }
    };
    Ignite ig = startGrid(0);
    IgniteFileSystem igfs = ig.fileSystem(IGFS_NAME);
    assert secondary.checkInjection(ig, igfs);
}
Also used : LoggerResource(org.apache.ignite.resources.LoggerResource) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) Ignite(org.apache.ignite.Ignite) IgniteFileSystem(org.apache.ignite.IgniteFileSystem) FileSystemResource(org.apache.ignite.resources.FileSystemResource) IgniteLogger(org.apache.ignite.IgniteLogger)

Example 12 with IgniteInstanceResource

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

the class IgfsSecondaryFileSystemInjectionSelfTest method testInjectPrimaryByMethods.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings({ "UnusedDeclaration" })
public void testInjectPrimaryByMethods() throws Exception {
    secondary = new TestBaseSecondaryFsMock() {

        /**
         * Ignite instance.
         */
        private Ignite ig;

        /**
         * IGFS instance.
         */
        private IgniteFileSystem igfs;

        /**
         * Logger injected flag
         */
        private boolean logSet;

        /**
         * @param igfs Primary IGFS.
         */
        @FileSystemResource
        void setPrimaryIgfs(IgfsImpl igfs) {
            this.igfs = igfs;
        }

        /**
         * @param log Ignite logger.
         */
        @LoggerResource
        void setIgLogger(IgniteLogger log) {
            logSet = log instanceof IgniteLogger;
        }

        /**
         * @param ig Ignite instance.
         */
        @IgniteInstanceResource
        void setIgniteInst(Ignite ig) {
            this.ig = ig;
        }

        @Override
        boolean checkInjection(Ignite ignite, IgniteFileSystem primary) {
            return ignite == ig && primary == igfs && logSet;
        }
    };
    Ignite ig = startGrid(0);
    IgniteFileSystem igfs = ig.fileSystem(IGFS_NAME);
    assert secondary.checkInjection(ig, igfs);
}
Also used : LoggerResource(org.apache.ignite.resources.LoggerResource) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) Ignite(org.apache.ignite.Ignite) IgniteFileSystem(org.apache.ignite.IgniteFileSystem) FileSystemResource(org.apache.ignite.resources.FileSystemResource) IgniteLogger(org.apache.ignite.IgniteLogger)

Example 13 with IgniteInstanceResource

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

the class GridCacheAbstractDataStructuresFailoverSelfTest method testAtomicSequenceInitialization.

/**
 * @throws Exception If failed.
 */
public void testAtomicSequenceInitialization() throws Exception {
    int threadCnt = 3;
    final AtomicInteger idx = new AtomicInteger(gridCount());
    IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new CA() {

        @Override
        public void apply() {
            int id = idx.getAndIncrement();
            try {
                log.info("Start node: " + id);
                startGrid(id);
                Thread.sleep(1000);
            } catch (Exception e) {
                throw F.wrap(e);
            } finally {
                stopGrid(id);
                info("Thread finished.");
            }
        }
    }, threadCnt, "test-thread");
    while (!fut.isDone()) {
        grid(0).compute().call(new IgniteCallable<Object>() {

            /**
             */
            @IgniteInstanceResource
            private Ignite g;

            @Override
            public Object call() throws Exception {
                IgniteAtomicSequence seq = g.atomicSequence(STRUCTURE_NAME, 1, true);
                assert seq != null;
                for (int i = 0; i < 1000; i++) seq.getAndIncrement();
                return null;
            }
        });
    }
    fut.get();
}
Also used : IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) Ignite(org.apache.ignite.Ignite) CA(org.apache.ignite.internal.util.typedef.CA) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException)

Example 14 with IgniteInstanceResource

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

the class GridCacheQueueApiSelfAbstractTest method testAffinityCall.

/**
 * @throws Exception If failed.
 */
public void testAffinityCall() throws Exception {
    final CollectionConfiguration colCfg = collectionConfiguration();
    colCfg.setCollocated(false);
    colCfg.setGroupName("testGroup");
    colCfg.setCacheMode(CacheMode.PARTITIONED);
    try (final IgniteQueue<Integer> queue1 = grid(0).queue("Queue1", 0, colCfg)) {
        GridTestUtils.assertThrows(log, new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                queue1.affinityCall(new IgniteCallable<Object>() {

                    @Override
                    public Object call() {
                        return null;
                    }
                });
                return null;
            }
        }, IgniteException.class, "Failed to execute affinityCall() for non-collocated queue: " + queue1.name() + ". This operation is supported only for collocated queues.");
    }
    colCfg.setCollocated(true);
    try (final IgniteQueue<Integer> queue2 = grid(0).queue("Queue2", 0, colCfg)) {
        queue2.add(100);
        Integer res = queue2.affinityCall(new IgniteCallable<Integer>() {

            @IgniteInstanceResource
            private IgniteEx ignite;

            @Override
            public Integer call() {
                assertTrue(ignite.cachex(cctx(queue2).cache().name()).affinity().isPrimaryOrBackup(ignite.cluster().localNode(), "Queue2"));
                return queue2.take();
            }
        });
        assertEquals(100, res.intValue());
    }
}
Also used : IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteEx(org.apache.ignite.internal.IgniteEx) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration) IgniteException(org.apache.ignite.IgniteException)

Example 15 with IgniteInstanceResource

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

the class IgniteTxPessimisticOriginatingNodeFailureAbstractSelfTest method testTxOriginatingNodeFails.

/**
 * @param keys Keys to update.
 * @param fullFailure Flag indicating whether to simulate rollback state.
 * @throws Exception If failed.
 */
protected void testTxOriginatingNodeFails(Collection<Integer> keys, final boolean fullFailure) throws Exception {
    assertFalse(keys.isEmpty());
    final Collection<IgniteKernal> grids = new ArrayList<>();
    ClusterNode txNode = grid(originatingNode()).localNode();
    for (int i = 1; i < gridCount(); i++) grids.add((IgniteKernal) grid(i));
    failingNodeId = grid(0).localNode().id();
    final Map<Integer, String> map = new HashMap<>();
    final String initVal = "initialValue";
    for (Integer key : keys) {
        grid(originatingNode()).cache(DEFAULT_CACHE_NAME).put(key, initVal);
        map.put(key, String.valueOf(key));
    }
    Map<Integer, Collection<ClusterNode>> nodeMap = new HashMap<>();
    info("Node being checked: " + grid(1).localNode().id());
    for (Integer key : keys) {
        Collection<ClusterNode> nodes = new ArrayList<>();
        nodes.addAll(grid(1).affinity(DEFAULT_CACHE_NAME).mapKeyToPrimaryAndBackups(key));
        nodes.remove(txNode);
        nodeMap.put(key, nodes);
    }
    info("Starting tx [values=" + map + ", topVer=" + ((IgniteKernal) grid(1)).context().discovery().topologyVersion() + ']');
    if (fullFailure)
        ignoreMessages(ignoreMessageClasses(), F.asList(grid(1).localNode().id()));
    final IgniteEx originatingNodeGrid = grid(originatingNode());
    GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            IgniteCache<Integer, String> cache = originatingNodeGrid.cache(DEFAULT_CACHE_NAME);
            assertNotNull(cache);
            Transaction tx = originatingNodeGrid.transactions().txStart();
            assertEquals(PESSIMISTIC, tx.concurrency());
            try {
                cache.putAll(map);
                info("Before commitAsync");
                IgniteFuture<?> fut = tx.commitAsync();
                info("Got future for commitAsync().");
                fut.get(3, TimeUnit.SECONDS);
            } catch (IgniteFutureTimeoutException ignored) {
                info("Failed to wait for commit future completion [fullFailure=" + fullFailure + ']');
            }
            return null;
        }
    }).get();
    info(">>> Stopping originating node " + txNode);
    G.stop(grid(originatingNode()).name(), true);
    ignoreMessages(Collections.<Class<?>>emptyList(), Collections.<UUID>emptyList());
    info(">>> Stopped originating node: " + txNode.id());
    boolean txFinished = GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            for (IgniteKernal g : grids) {
                GridCacheAdapter<?, ?> cache = g.internalCache(DEFAULT_CACHE_NAME);
                IgniteTxManager txMgr = cache.isNear() ? ((GridNearCacheAdapter) cache).dht().context().tm() : cache.context().tm();
                int txNum = txMgr.idMapSize();
                if (txNum != 0)
                    return false;
            }
            return true;
        }
    }, 10000);
    assertTrue(txFinished);
    info("Transactions finished.");
    for (Map.Entry<Integer, Collection<ClusterNode>> e : nodeMap.entrySet()) {
        final Integer key = e.getKey();
        final String val = map.get(key);
        assertFalse(e.getValue().isEmpty());
        for (ClusterNode node : e.getValue()) {
            final UUID checkNodeId = node.id();
            compute(G.ignite(checkNodeId).cluster().forNode(node)).call(new IgniteCallable<Void>() {

                /**
                 */
                @IgniteInstanceResource
                private Ignite ignite;

                @Override
                public Void call() throws Exception {
                    IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
                    assertNotNull(cache);
                    assertEquals("Failed to check entry value on node: " + checkNodeId, fullFailure ? initVal : val, cache.localPeek(key));
                    return null;
                }
            });
        }
    }
    for (Map.Entry<Integer, String> e : map.entrySet()) {
        for (Ignite g : G.allGrids()) assertEquals(fullFailure ? initVal : e.getValue(), g.cache(DEFAULT_CACHE_NAME).get(e.getKey()));
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) GridNearCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheAdapter) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) IgniteTxManager(org.apache.ignite.internal.processors.cache.transactions.IgniteTxManager) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteFutureTimeoutException(org.apache.ignite.lang.IgniteFutureTimeoutException) IgniteException(org.apache.ignite.IgniteException) IgniteFutureTimeoutException(org.apache.ignite.lang.IgniteFutureTimeoutException) Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)46 Ignite (org.apache.ignite.Ignite)38 IgniteException (org.apache.ignite.IgniteException)18 IgniteCache (org.apache.ignite.IgniteCache)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 IgniteEx (org.apache.ignite.internal.IgniteEx)10 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)8 UUID (java.util.UUID)7 IgniteCallable (org.apache.ignite.lang.IgniteCallable)7 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 IgniteLogger (org.apache.ignite.IgniteLogger)6 ClusterNode (org.apache.ignite.cluster.ClusterNode)6 LoggerResource (org.apache.ignite.resources.LoggerResource)6 Collection (java.util.Collection)5 List (java.util.List)5 Callable (java.util.concurrent.Callable)5 Cache (javax.cache.Cache)5 ScanQuery (org.apache.ignite.cache.query.ScanQuery)5