Search in sources :

Example 41 with Cache

use of javax.cache.Cache in project ignite by apache.

the class CacheKeepBinaryIterationTest method doTestScanQuery.

/**
 * @param ccfg Cache configuration.
 */
private void doTestScanQuery(final CacheConfiguration<Object, Object> ccfg, boolean keepBinary, boolean primitives) throws Exception {
    IgniteCache<Object, Object> cache = grid(0).createCache(ccfg);
    assertEquals(0, cache.size());
    try {
        for (int i = 0; i < KEYS; i++) if (primitives)
            cache.put(i, i);
        else
            cache.put(new QueryTestKey(i), new QueryTestValue(i));
        for (int i = 0; i < getServerNodeCount(); i++) {
            IgniteCache<Object, Object> cache0 = grid(i).cache(ccfg.getName());
            if (keepBinary)
                cache0 = cache0.withKeepBinary();
            ScanQuery<Object, Object> qry = new ScanQuery<>();
            qry.setLocal(true);
            int size = 0;
            try (QueryCursor<Cache.Entry<Object, Object>> cur = cache0.query(qry)) {
                for (Cache.Entry<Object, Object> e : cur) {
                    Object key = e.getKey();
                    Object val = e.getValue();
                    if (!primitives) {
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, keepBinary == key instanceof BinaryObject);
                        assertTrue("Got unexpected object: " + val.getClass() + ", keepBinary: " + keepBinary, keepBinary == val instanceof BinaryObject);
                    } else {
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, key instanceof Integer);
                        assertTrue("Got unexpected object: " + val.getClass() + ", keepBinary: " + keepBinary, val instanceof Integer);
                    }
                    ++size;
                }
            }
            assertTrue(size > 0);
        }
    } finally {
        if (ccfg.getEvictionPolicy() != null) {
            // TODO: IGNITE-3462. Fixes evictionPolicy issues at cache destroy.
            stopAllGrids();
            startGridsMultiThreaded(getServerNodeCount());
        } else
            grid(0).destroyCache(ccfg.getName());
    }
}
Also used : ScanQuery(org.apache.ignite.cache.query.ScanQuery) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 42 with Cache

use of javax.cache.Cache in project ignite by apache.

the class CacheKeepBinaryIterationTest method doTestLocalEntries.

/**
 * @param ccfg Cache configuration.
 */
private void doTestLocalEntries(final CacheConfiguration<Object, Object> ccfg, boolean keepBinary, boolean primitives) throws Exception {
    IgniteCache<Object, Object> cache = grid(0).createCache(ccfg);
    assertEquals(0, cache.size());
    try {
        for (int i = 0; i < KEYS; i++) if (primitives)
            cache.put(i, i);
        else
            cache.put(new QueryTestKey(i), new QueryTestValue(i));
        for (int i = 0; i < getServerNodeCount(); i++) {
            IgniteCache<Object, Object> cache0 = grid(i).cache(ccfg.getName());
            if (keepBinary)
                cache0 = cache0.withKeepBinary();
            for (CachePeekMode mode : CachePeekMode.values()) {
                int size = 0;
                for (Cache.Entry<Object, Object> e : cache0.localEntries(mode)) {
                    Object key = e.getKey();
                    Object val = e.getValue();
                    if (!primitives) {
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, keepBinary == key instanceof BinaryObject);
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, keepBinary == val instanceof BinaryObject);
                    } else {
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, key instanceof Integer);
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, val instanceof Integer);
                    }
                    ++size;
                }
                if (mode == CachePeekMode.ALL || mode == CachePeekMode.PRIMARY || mode == CachePeekMode.BACKUP || (mode == CachePeekMode.NEAR && i == 0 && ccfg.getNearConfiguration() != null))
                    assertTrue("Zero result at mode: " + mode, size > 0);
            }
        }
    } finally {
        if (ccfg.getEvictionPolicy() != null) {
            // TODO: IGNITE-3462. Fixes evictionPolicy issues at cache destroy.
            stopAllGrids();
            startGridsMultiThreaded(getServerNodeCount());
        } else
            grid(0).destroyCache(ccfg.getName());
    }
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) CachePeekMode(org.apache.ignite.cache.CachePeekMode) BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 43 with Cache

use of javax.cache.Cache in project ignite by apache.

the class GridCacheContinuousQueryAbstractSelfTest method afterTest.

/**
 * {@inheritDoc}
 */
@Override
protected void afterTest() throws Exception {
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            for (int i = 0; i < gridCount(); i++) {
                if (grid(i).cluster().nodes().size() != gridCount())
                    return false;
            }
            return true;
        }
    }, 3000);
    for (int i = 0; i < gridCount(); i++) assertEquals(gridCount(), grid(i).cluster().nodes().size());
    for (int i = 0; i < gridCount(); i++) {
        for (int j = 0; j < 5; j++) {
            try {
                IgniteCache<Object, Object> cache = grid(i).cache(DEFAULT_CACHE_NAME);
                for (Cache.Entry<Object, Object> entry : cache.localEntries(new CachePeekMode[] { CachePeekMode.ALL })) cache.remove(entry.getKey());
                break;
            } catch (IgniteException e) {
                if (j == 4)
                    throw new Exception("Failed to clear cache for grid: " + i, e);
                U.warn(log, "Failed to clear cache for grid (will retry in 500 ms) [gridIdx=" + i + ", err=" + e.getMessage() + ']');
                U.sleep(500);
            }
        }
    }
    // Wait for all routines are unregistered
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            for (int i = 0; i < gridCount(); i++) {
                GridContinuousProcessor proc = grid(i).context().continuous();
                if (((Map) U.field(proc, "rmtInfos")).size() > 0)
                    return false;
            }
            return true;
        }
    }, 3000);
    for (int i = 0; i < gridCount(); i++) {
        GridContinuousProcessor proc = grid(i).context().continuous();
        assertEquals(String.valueOf(i), 1, ((Map) U.field(proc, "locInfos")).size());
        assertEquals(String.valueOf(i), 0, ((Map) U.field(proc, "rmtInfos")).size());
        assertEquals(String.valueOf(i), 0, ((Map) U.field(proc, "startFuts")).size());
        assertEquals(String.valueOf(i), 0, ((Map) U.field(proc, "stopFuts")).size());
        assertEquals(String.valueOf(i), 0, ((Map) U.field(proc, "bufCheckThreads")).size());
        CacheContinuousQueryManager mgr = grid(i).context().cache().internalCache(DEFAULT_CACHE_NAME).context().continuousQueries();
        assertEquals(0, ((Map) U.field(mgr, "lsnrs")).size());
    }
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) IgniteException(org.apache.ignite.IgniteException) GridContinuousProcessor(org.apache.ignite.internal.processors.continuous.GridContinuousProcessor) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) IgniteException(org.apache.ignite.IgniteException) CacheWriterException(javax.cache.integration.CacheWriterException) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 44 with Cache

use of javax.cache.Cache in project ignite by apache.

the class GridCacheContinuousQueryAbstractSelfTest method testInitialQueryAndUpdates.

/**
 * @throws Exception If failed.
 */
public void testInitialQueryAndUpdates() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    qry.setInitialQuery(new ScanQuery<>(new P2<Integer, Integer>() {

        @Override
        public boolean apply(Integer k, Integer v) {
            return k >= 5;
        }
    }));
    final Map<Integer, Integer> map = new ConcurrentHashMap<>();
    final CountDownLatch latch = new CountDownLatch(2);
    qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> e : evts) {
                map.put(e.getKey(), e.getValue());
                latch.countDown();
            }
        }
    });
    for (int i = 0; i < 10; i++) cache.put(i, i);
    try (QueryCursor<Cache.Entry<Integer, Integer>> cur = cache.query(qry)) {
        List<Cache.Entry<Integer, Integer>> res = cur.getAll();
        Collections.sort(res, new Comparator<Cache.Entry<Integer, Integer>>() {

            @Override
            public int compare(Cache.Entry<Integer, Integer> e1, Cache.Entry<Integer, Integer> e2) {
                return e1.getKey().compareTo(e2.getKey());
            }
        });
        assertEquals(5, res.size());
        int exp = 5;
        for (Cache.Entry<Integer, Integer> e : res) {
            assertEquals(exp, e.getKey().intValue());
            assertEquals(exp, e.getValue().intValue());
            exp++;
        }
        cache.put(10, 10);
        cache.put(11, 11);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS) : latch.getCount();
        assertEquals(2, map.size());
        for (int i = 11; i < 12; i++) assertEquals(i, (int) map.get(i));
    }
}
Also used : P2(org.apache.ignite.internal.util.typedef.P2) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 45 with Cache

use of javax.cache.Cache in project ignite by apache.

the class DistributedLinearRegressionWithLSQRTrainerAndNormalizationExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws InterruptedException {
    System.out.println();
    System.out.println(">>> Linear regression model over sparse distributed matrix API usage example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        // Create IgniteThread, we must work with SparseDistributedMatrix inside IgniteThread
        // because we create ignite cache internally.
        IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), SparseDistributedMatrixExample.class.getSimpleName(), () -> {
            IgniteCache<Integer, double[]> dataCache = getTestCache(ignite);
            System.out.println(">>> Create new normalization trainer object.");
            NormalizationTrainer<Integer, double[]> normalizationTrainer = new NormalizationTrainer<>();
            System.out.println(">>> Perform the training to get the normalization preprocessor.");
            NormalizationPreprocessor<Integer, double[]> preprocessor = normalizationTrainer.fit(new CacheBasedDatasetBuilder<>(ignite, dataCache), (k, v) -> Arrays.copyOfRange(v, 1, v.length), 4);
            System.out.println(">>> Create new linear regression trainer object.");
            LinearRegressionLSQRTrainer<Integer, double[]> trainer = new LinearRegressionLSQRTrainer<>();
            System.out.println(">>> Perform the training to get the model.");
            LinearRegressionModel mdl = trainer.fit(new CacheBasedDatasetBuilder<>(ignite, dataCache), preprocessor, (k, v) -> v[0], 4);
            System.out.println(">>> Linear regression model: " + mdl);
            System.out.println(">>> ---------------------------------");
            System.out.println(">>> | Prediction\t| Ground Truth\t|");
            System.out.println(">>> ---------------------------------");
            try (QueryCursor<Cache.Entry<Integer, double[]>> observations = dataCache.query(new ScanQuery<>())) {
                for (Cache.Entry<Integer, double[]> observation : observations) {
                    Integer key = observation.getKey();
                    double[] val = observation.getValue();
                    double groundTruth = val[0];
                    double prediction = mdl.apply(new DenseLocalOnHeapVector(preprocessor.apply(key, val)));
                    System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
                }
            }
            System.out.println(">>> ---------------------------------");
        });
        igniteThread.start();
        igniteThread.join();
    }
}
Also used : LinearRegressionModel(org.apache.ignite.ml.regressions.linear.LinearRegressionModel) NormalizationTrainer(org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer) LinearRegressionLSQRTrainer(org.apache.ignite.ml.regressions.linear.LinearRegressionLSQRTrainer) SparseDistributedMatrixExample(org.apache.ignite.examples.ml.math.matrix.SparseDistributedMatrixExample) Ignite(org.apache.ignite.Ignite) IgniteThread(org.apache.ignite.thread.IgniteThread) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Aggregations

Cache (javax.cache.Cache)139 IgniteCache (org.apache.ignite.IgniteCache)94 Ignite (org.apache.ignite.Ignite)49 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)30 ScanQuery (org.apache.ignite.cache.query.ScanQuery)21 ArrayList (java.util.ArrayList)20 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)20 List (java.util.List)18 SqlQuery (org.apache.ignite.cache.query.SqlQuery)18 CacheManager (javax.cache.CacheManager)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 ClusterNode (org.apache.ignite.cluster.ClusterNode)15 Map (java.util.Map)14 Collection (java.util.Collection)13 IgniteException (org.apache.ignite.IgniteException)12 BinaryObject (org.apache.ignite.binary.BinaryObject)12 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)11 HashMap (java.util.HashMap)11 Test (org.junit.Test)11 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)10