Search in sources :

Example 11 with IgniteCache

use of org.apache.ignite.IgniteCache in project ignite by apache.

the class GridManagerAdapter method onKernalStart.

/**
 * {@inheritDoc}
 */
@Override
public final void onKernalStart(boolean active) throws IgniteCheckedException {
    for (final IgniteSpi spi : spis) {
        try {
            spi.onContextInitialized(new IgniteSpiContext() {

                @Override
                public boolean isStopping() {
                    return ctx.isStopping();
                }

                @Override
                public Collection<ClusterNode> remoteNodes() {
                    return ctx.discovery().remoteNodes();
                }

                @Override
                public Collection<ClusterNode> nodes() {
                    return ctx.discovery().allNodes();
                }

                @Override
                public ClusterNode localNode() {
                    return ctx.discovery().localNode();
                }

                @Override
                public Collection<ClusterNode> remoteDaemonNodes() {
                    final Collection<ClusterNode> all = ctx.discovery().daemonNodes();
                    return !localNode().isDaemon() ? all : F.view(all, new IgnitePredicate<ClusterNode>() {

                        @Override
                        public boolean apply(ClusterNode n) {
                            return n.isDaemon();
                        }
                    });
                }

                @Nullable
                @Override
                public ClusterNode node(UUID nodeId) {
                    A.notNull(nodeId, "nodeId");
                    return ctx.discovery().node(nodeId);
                }

                @Override
                public boolean pingNode(UUID nodeId) {
                    A.notNull(nodeId, "nodeId");
                    try {
                        return ctx.discovery().pingNode(nodeId);
                    } catch (IgniteCheckedException e) {
                        throw U.convertException(e);
                    }
                }

                @Override
                public void send(ClusterNode node, Serializable msg, String topic) throws IgniteSpiException {
                    A.notNull(node, "node");
                    A.notNull(msg, "msg");
                    A.notNull(topic, "topic");
                    try {
                        if (msg instanceof Message)
                            ctx.io().sendToCustomTopic(node, topic, (Message) msg, SYSTEM_POOL);
                        else
                            ctx.io().sendUserMessage(Collections.singletonList(node), msg, topic, false, 0, false);
                    } catch (IgniteCheckedException e) {
                        throw unwrapException(e);
                    }
                }

                @Override
                public void addLocalMessageListener(Object topic, IgniteBiPredicate<UUID, ?> p) {
                    A.notNull(topic, "topic");
                    A.notNull(p, "p");
                    ctx.io().addUserMessageListener(topic, p);
                }

                @Override
                public void removeLocalMessageListener(Object topic, IgniteBiPredicate<UUID, ?> p) {
                    A.notNull(topic, "topic");
                    A.notNull(topic, "p");
                    ctx.io().removeUserMessageListener(topic, p);
                }

                @SuppressWarnings("deprecation")
                @Override
                public void addMessageListener(GridMessageListener lsnr, String topic) {
                    A.notNull(lsnr, "lsnr");
                    A.notNull(topic, "topic");
                    ctx.io().addMessageListener(topic, lsnr);
                }

                @SuppressWarnings("deprecation")
                @Override
                public boolean removeMessageListener(GridMessageListener lsnr, String topic) {
                    A.notNull(lsnr, "lsnr");
                    A.notNull(topic, "topic");
                    return ctx.io().removeMessageListener(topic, lsnr);
                }

                @Override
                public void addLocalEventListener(GridLocalEventListener lsnr, int... types) {
                    A.notNull(lsnr, "lsnr");
                    ctx.event().addLocalEventListener(lsnr, types);
                }

                @Override
                public boolean removeLocalEventListener(GridLocalEventListener lsnr) {
                    A.notNull(lsnr, "lsnr");
                    return ctx.event().removeLocalEventListener(lsnr);
                }

                @Override
                public boolean isEventRecordable(int... types) {
                    for (int t : types) if (!ctx.event().isRecordable(t))
                        return false;
                    return true;
                }

                @Override
                public void recordEvent(Event evt) {
                    A.notNull(evt, "evt");
                    if (ctx.event().isRecordable(evt.type()))
                        ctx.event().record(evt);
                }

                @Override
                public void registerPort(int port, IgnitePortProtocol proto) {
                    ctx.ports().registerPort(port, proto, spi.getClass());
                }

                @Override
                public void deregisterPort(int port, IgnitePortProtocol proto) {
                    ctx.ports().deregisterPort(port, proto, spi.getClass());
                }

                @Override
                public void deregisterPorts() {
                    ctx.ports().deregisterPorts(spi.getClass());
                }

                @Nullable
                @Override
                public <K, V> V get(String cacheName, K key) {
                    return ctx.cache().<K, V>jcache(cacheName).get(key);
                }

                @Nullable
                @Override
                public <K, V> V put(String cacheName, K key, V val, long ttl) {
                    try {
                        if (ttl > 0) {
                            ExpiryPolicy plc = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
                            IgniteCache<K, V> cache = ctx.cache().<K, V>publicJCache(cacheName).withExpiryPolicy(plc);
                            return cache.getAndPut(key, val);
                        } else
                            return ctx.cache().<K, V>jcache(cacheName).getAndPut(key, val);
                    } catch (IgniteCheckedException e) {
                        throw CU.convertToCacheException(e);
                    }
                }

                @Nullable
                @Override
                public <K, V> V putIfAbsent(String cacheName, K key, V val, long ttl) {
                    try {
                        if (ttl > 0) {
                            ExpiryPolicy plc = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
                            IgniteCache<K, V> cache = ctx.cache().<K, V>publicJCache(cacheName).withExpiryPolicy(plc);
                            return cache.getAndPutIfAbsent(key, val);
                        } else
                            return ctx.cache().<K, V>jcache(cacheName).getAndPutIfAbsent(key, val);
                    } catch (IgniteCheckedException e) {
                        throw CU.convertToCacheException(e);
                    }
                }

                @Nullable
                @Override
                public <K, V> V remove(String cacheName, K key) {
                    return ctx.cache().<K, V>jcache(cacheName).getAndRemove(key);
                }

                @Override
                public <K> boolean containsKey(String cacheName, K key) {
                    return ctx.cache().cache(cacheName).containsKey(key);
                }

                @Override
                public int partition(String cacheName, Object key) {
                    return ctx.cache().cache(cacheName).affinity().partition(key);
                }

                @Override
                public IgniteNodeValidationResult validateNode(ClusterNode node) {
                    for (GridComponent comp : ctx) {
                        IgniteNodeValidationResult err = comp.validateNode(node);
                        if (err != null)
                            return err;
                    }
                    return null;
                }

                @Nullable
                @Override
                public IgniteNodeValidationResult validateNode(ClusterNode node, DiscoveryDataBag discoData) {
                    for (GridComponent comp : ctx) {
                        if (comp.discoveryDataType() == null)
                            continue;
                        IgniteNodeValidationResult err = comp.validateNode(node, discoData.newJoinerDiscoveryData(comp.discoveryDataType().ordinal()));
                        if (err != null)
                            return err;
                    }
                    return null;
                }

                @Override
                public Collection<SecuritySubject> authenticatedSubjects() {
                    try {
                        return ctx.security().authenticatedSubjects();
                    } catch (IgniteCheckedException e) {
                        throw U.convertException(e);
                    }
                }

                @Override
                public SecuritySubject authenticatedSubject(UUID subjId) {
                    try {
                        return ctx.security().authenticatedSubject(subjId);
                    } catch (IgniteCheckedException e) {
                        throw U.convertException(e);
                    }
                }

                @Override
                public MessageFormatter messageFormatter() {
                    return ctx.io().formatter();
                }

                @Override
                public MessageFactory messageFactory() {
                    return ctx.io().messageFactory();
                }

                @Override
                public boolean tryFailNode(UUID nodeId, @Nullable String warning) {
                    return ctx.discovery().tryFailNode(nodeId, warning);
                }

                @Override
                public void failNode(UUID nodeId, @Nullable String warning) {
                    ctx.discovery().failNode(nodeId, warning);
                }

                @Override
                public void addTimeoutObject(IgniteSpiTimeoutObject obj) {
                    ctx.timeout().addTimeoutObject(new GridSpiTimeoutObject(obj));
                }

                @Override
                public void removeTimeoutObject(IgniteSpiTimeoutObject obj) {
                    ctx.timeout().removeTimeoutObject(new GridSpiTimeoutObject(obj));
                }

                @Override
                public Map<String, Object> nodeAttributes() {
                    return ctx.nodeAttributes();
                }

                /**
                 * @param e Exception to handle.
                 * @return GridSpiException Converted exception.
                 */
                private IgniteSpiException unwrapException(IgniteCheckedException e) {
                    // Avoid double-wrapping.
                    if (e.getCause() instanceof IgniteSpiException)
                        return (IgniteSpiException) e.getCause();
                    return new IgniteSpiException("Failed to execute SPI context method.", e);
                }
            });
        } catch (IgniteSpiException e) {
            throw new IgniteCheckedException("Failed to initialize SPI context.", e);
        }
    }
    onKernalStart0();
}
Also used : IgniteSpiContext(org.apache.ignite.spi.IgniteSpiContext) Serializable(java.io.Serializable) IgniteSpiTimeoutObject(org.apache.ignite.spi.IgniteSpiTimeoutObject) Message(org.apache.ignite.plugin.extensions.communication.Message) GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) IgniteNodeValidationResult(org.apache.ignite.spi.IgniteNodeValidationResult) GridComponent(org.apache.ignite.internal.GridComponent) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) MessageFormatter(org.apache.ignite.plugin.extensions.communication.MessageFormatter) GridSpiTimeoutObject(org.apache.ignite.internal.processors.timeout.GridSpiTimeoutObject) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgnitePortProtocol(org.apache.ignite.spi.IgnitePortProtocol) DiscoveryDataBag(org.apache.ignite.spi.discovery.DiscoveryDataBag) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) UUID(java.util.UUID) ClusterNode(org.apache.ignite.cluster.ClusterNode) MessageFactory(org.apache.ignite.plugin.extensions.communication.MessageFactory) SecuritySubject(org.apache.ignite.plugin.security.SecuritySubject) IgniteCache(org.apache.ignite.IgniteCache) IgniteSpi(org.apache.ignite.spi.IgniteSpi) Duration(javax.cache.expiry.Duration) Collection(java.util.Collection) Event(org.apache.ignite.events.Event) IgniteSpiTimeoutObject(org.apache.ignite.spi.IgniteSpiTimeoutObject) GridSpiTimeoutObject(org.apache.ignite.internal.processors.timeout.GridSpiTimeoutObject) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with IgniteCache

use of org.apache.ignite.IgniteCache in project ignite by apache.

the class IgniteDiagnosticMessagesTest method testSeveralLongRunningTxs.

/**
 * @throws Exception If failed.
 */
public void testSeveralLongRunningTxs() throws Exception {
    int timeout = 3500;
    System.setProperty(IGNITE_LONG_OPERATIONS_DUMP_TIMEOUT, String.valueOf(timeout));
    try {
        testSpi = true;
        startGrid(0);
        GridStringLogger strLog = this.strLog = new GridStringLogger();
        strLog.logLength(1024 * 100);
        startGrid(1);
        awaitPartitionMapExchange();
        CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
        ccfg.setWriteSynchronizationMode(FULL_SYNC);
        ccfg.setCacheMode(PARTITIONED);
        ccfg.setAtomicityMode(TRANSACTIONAL);
        final Ignite node0 = ignite(0);
        final Ignite node1 = ignite(1);
        node0.createCache(ccfg);
        UUID id0 = node0.cluster().localNode().id();
        TestRecordingCommunicationSpi.spi(node0).blockMessages(GridNearLockResponse.class, node1.name());
        IgniteCache<Object, Object> cache = node0.cache(DEFAULT_CACHE_NAME);
        int txCnt = 4;
        final List<Integer> keys = primaryKeys(cache, txCnt, 0);
        final AtomicInteger idx = new AtomicInteger();
        IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                IgniteCache<Object, Object> cache = node1.cache(DEFAULT_CACHE_NAME);
                try (Transaction tx = node1.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                    Integer key = keys.get(idx.getAndIncrement() % keys.size());
                    cache.putIfAbsent(key, String.valueOf(key));
                    tx.commit();
                }
                return null;
            }
        }, txCnt * 2, "tx");
        U.sleep(timeout * 2);
        assertFalse(fut.isDone());
        TestRecordingCommunicationSpi.spi(node0).stopBlock();
        fut.get();
        String log = strLog.toString();
        assertTrue(log.contains("Cache entries [cacheId=" + CU.cacheId(DEFAULT_CACHE_NAME) + ", cacheName=" + DEFAULT_CACHE_NAME + "]:"));
        assertTrue(countTxKeysInASingleBlock(log) == txCnt);
        assertTrue(log.contains("General node info [id=" + id0));
    } finally {
        System.clearProperty(IGNITE_LONG_OPERATIONS_DUMP_TIMEOUT);
    }
}
Also used : GridStringLogger(org.apache.ignite.testframework.GridStringLogger) IgniteCache(org.apache.ignite.IgniteCache) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 13 with IgniteCache

use of org.apache.ignite.IgniteCache in project ignite by apache.

the class IgniteVariousConnectionNumberTest method runOperations.

/**
 * @param time Execution time.
 * @throws Exception If failed.
 */
private void runOperations(final long time) throws Exception {
    final AtomicInteger idx = new AtomicInteger();
    GridTestUtils.runMultiThreaded(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Ignite node = ignite(idx.getAndIncrement() % NODES);
            IgniteCache cache = node.cache(DEFAULT_CACHE_NAME);
            long stopTime = U.currentTimeMillis() + time;
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            while (U.currentTimeMillis() < stopTime) {
                cache.put(rnd.nextInt(10_000), 0);
                node.compute().broadcast(new DummyJob());
            }
            return null;
        }
    }, NODES * 10, "test-thread");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteCache(org.apache.ignite.IgniteCache) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite)

Example 14 with IgniteCache

use of org.apache.ignite.IgniteCache in project ignite by apache.

the class GridCacheGetAndTransformStoreAbstractTest method testGetAndTransform.

/**
 * @throws Exception If failed.
 */
public void testGetAndTransform() throws Exception {
    final AtomicBoolean finish = new AtomicBoolean();
    try {
        startGrid(0);
        startGrid(1);
        startGrid(2);
        final Processor entryProcessor = new Processor();
        IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                IgniteCache<Integer, String> c = jcache(ThreadLocalRandom.current().nextInt(3));
                while (!finish.get() && !Thread.currentThread().isInterrupted()) {
                    c.get(ThreadLocalRandom.current().nextInt(100));
                    c.put(ThreadLocalRandom.current().nextInt(100), "s");
                    c.invoke(ThreadLocalRandom.current().nextInt(100), entryProcessor);
                }
                return null;
            }
        }, 20);
        Thread.sleep(15_000);
        finish.set(true);
        fut.get();
    } finally {
        stopGrid(0);
        stopGrid(1);
        stopGrid(2);
        while (jcache().localSize() != 0) jcache().clear();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EntryProcessor(javax.cache.processor.EntryProcessor) IgniteCache(org.apache.ignite.IgniteCache)

Example 15 with IgniteCache

use of org.apache.ignite.IgniteCache in project ignite by apache.

the class CacheClientStoreSelfTest method testReplicatedLoadFromClient.

/**
 * Load cache created on client as REPLICATED and see if it only loaded on servers
 */
public void testReplicatedLoadFromClient() throws Exception {
    cacheMode = CacheMode.REPLICATED;
    factory = new Factory3();
    startGrids(2);
    Ignite client = startGrid("client-1");
    IgniteCache cache = client.cache(CACHE_NAME);
    cache.loadCache(null);
    assertEquals(0, cache.localSize(CachePeekMode.ALL));
    assertEquals(10, grid(0).cache(CACHE_NAME).localSize(CachePeekMode.ALL));
    assertEquals(10, grid(1).cache(CACHE_NAME).localSize(CachePeekMode.ALL));
    assert !loadedFromClient : "Loaded data from client!";
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) Ignite(org.apache.ignite.Ignite)

Aggregations

IgniteCache (org.apache.ignite.IgniteCache)403 Ignite (org.apache.ignite.Ignite)233 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)137 Cache (javax.cache.Cache)98 Transaction (org.apache.ignite.transactions.Transaction)87 ArrayList (java.util.ArrayList)69 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)65 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)60 Map (java.util.Map)55 IgniteException (org.apache.ignite.IgniteException)55 List (java.util.List)51 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)48 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)46 HashMap (java.util.HashMap)45 CacheException (javax.cache.CacheException)43 Random (java.util.Random)37 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)32 CountDownLatch (java.util.concurrent.CountDownLatch)30 ClusterNode (org.apache.ignite.cluster.ClusterNode)30 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)28