Search in sources :

Example 11 with IgniteDataStreamer

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

the class IgniteClientReconnectFailoverTest method testReconnectStreamerApi.

/**
 * @throws Exception If failed.
 */
public void testReconnectStreamerApi() throws Exception {
    final Ignite client = grid(serverCount());
    reconnectFailover(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            stream(ATOMIC_CACHE);
            stream(TX_CACHE);
            return null;
        }

        private void stream(String cacheName) {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            try (IgniteDataStreamer<Integer, Integer> streamer = client.dataStreamer(cacheName)) {
                streamer.allowOverwrite(true);
                streamer.perNodeBufferSize(10);
                for (int i = 0; i < 100; i++) streamer.addData(rnd.nextInt(100_000), 0);
            }
        }
    });
}
Also used : IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException)

Example 12 with IgniteDataStreamer

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

the class IgniteCacheClearDuringRebalanceTest method populate.

/**
 * @param node Ignite node;
 * @throws Exception If failed.
 */
private void populate(final Ignite node) throws Exception {
    final AtomicInteger id = new AtomicInteger();
    final int tCnt = Runtime.getRuntime().availableProcessors();
    final byte[] data = new byte[1024];
    ThreadLocalRandom.current().nextBytes(data);
    GridTestUtils.runMultiThreaded(new Runnable() {

        @Override
        public void run() {
            try (IgniteDataStreamer<Object, Object> str = node.dataStreamer(CACHE_NAME)) {
                int idx = id.getAndIncrement();
                str.autoFlushFrequency(0);
                for (int i = idx; i < 500_000; i += tCnt) {
                    str.addData(i, data);
                    if (i % (100 * tCnt) == idx)
                        str.flush();
                }
                str.flush();
            }
        }
    }, tCnt, "ldr");
    assertEquals(500_000, node.cache(CACHE_NAME).size());
}
Also used : IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 13 with IgniteDataStreamer

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

the class IgnitePdsCacheRebalancingAbstractTest method testPartitionCounterConsistencyOnUnstableTopology.

/**
 * @throws Exception If failed
 */
public void testPartitionCounterConsistencyOnUnstableTopology() throws Exception {
    System.setProperty(IgniteSystemProperties.IGNITE_PDS_MAX_CHECKPOINT_MEMORY_HISTORY_SIZE, "1");
    final Ignite ig = startGrids(4);
    ig.cluster().active(true);
    int k = 0;
    try (IgniteDataStreamer ds = ig.dataStreamer(cacheName)) {
        ds.allowOverwrite(true);
        for (int k0 = k; k < k0 + 50_000; k++) ds.addData(k, k);
    }
    for (int t = 0; t < 5; t++) {
        int t0 = t;
        IgniteInternalFuture fut = GridTestUtils.runAsync(() -> {
            try {
                stopGrid(3);
                // Clear checkpoint history to avoid rebalance from WAL.
                forceCheckpoint();
                forceCheckpoint();
                // Wait for data load.
                U.sleep(500);
                IgniteEx ig0 = startGrid(3);
                // Wait for node join.
                U.sleep(2000);
                if (t0 % 2 == 1) {
                    stopGrid(2);
                    awaitPartitionMapExchange();
                    // Clear checkpoint history to avoid rebalance from WAL.
                    forceCheckpoint();
                    forceCheckpoint();
                    startGrid(2);
                    awaitPartitionMapExchange();
                }
                ig0.cache(cacheName).rebalance().get();
            } catch (Exception e) {
                error("Unable to start/stop grid", e);
                throw new RuntimeException(e);
            }
        });
        try (IgniteDataStreamer ds = ig.dataStreamer(cacheName)) {
            ds.allowOverwrite(true);
            while (!fut.isDone()) {
                int k0 = k;
                for (; k < k0 + 3; k++) ds.addData(k, k);
                U.sleep(1);
            }
        } catch (Exception e) {
            log.error("Unable to write data", e);
        }
        fut.get();
        log.info("Checking data...");
        Map<Integer, Long> cntrs = new HashMap<>();
        for (int g = 0; g < 4; g++) {
            IgniteEx ig0 = grid(g);
            for (GridDhtLocalPartition part : ig0.cachex(cacheName).context().topology().currentLocalPartitions()) {
                if (cntrs.containsKey(part.id()))
                    assertEquals(String.valueOf(part.id()), (long) cntrs.get(part.id()), part.updateCounter());
                else
                    cntrs.put(part.id(), part.updateCounter());
            }
            for (int k0 = 0; k0 < k; k0++) assertEquals(String.valueOf(k0) + " " + g, k0, ig0.cache(cacheName).get(k0));
        }
        assertEquals(ig.affinity(cacheName).partitions(), cntrs.size());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtLocalPartition) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture)

Example 14 with IgniteDataStreamer

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

the class StreamVisitorExample method main.

public static void main(String[] args) throws Exception {
    // Mark this cluster member as client.
    Ignition.setClientMode(true);
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        if (!ExamplesUtils.hasServerNodes(ignite))
            return;
        // Market data cache with default configuration.
        CacheConfiguration<String, Double> mktDataCfg = new CacheConfiguration<>("marketTicks");
        // Financial instrument cache configuration.
        CacheConfiguration<String, Instrument> instCfg = new CacheConfiguration<>("instCache");
        // Index key and value for querying financial instruments.
        // Note that Instrument class has @QuerySqlField annotation for secondary field indexing.
        instCfg.setIndexedTypes(String.class, Instrument.class);
        // Auto-close caches at the end of the example.
        try (IgniteCache<String, Double> mktCache = ignite.getOrCreateCache(mktDataCfg);
            IgniteCache<String, Instrument> instCache = ignite.getOrCreateCache(instCfg)) {
            try (IgniteDataStreamer<String, Double> mktStmr = ignite.dataStreamer(mktCache.getName())) {
                // Note that we receive market data, but do not populate 'mktCache' (it remains empty).
                // Instead we update the instruments in the 'instCache'.
                // Since both, 'instCache' and 'mktCache' use the same key, updates are collocated.
                mktStmr.receiver(StreamVisitor.from((cache, e) -> {
                    String symbol = e.getKey();
                    Double tick = e.getValue();
                    Instrument inst = instCache.get(symbol);
                    if (inst == null)
                        inst = new Instrument(symbol);
                    // Don't populate market cache, as we don't use it for querying.
                    // Update cached instrument based on the latest market tick.
                    inst.update(tick);
                    instCache.put(symbol, inst);
                }));
                // Stream 10 million market data ticks into the system.
                for (int i = 1; i <= 10_000_000; i++) {
                    int idx = RAND.nextInt(INSTRUMENTS.length);
                    // Use gaussian distribution to ensure that
                    // numbers closer to 0 have higher probability.
                    double price = round2(INITIAL_PRICES[idx] + RAND.nextGaussian());
                    mktStmr.addData(INSTRUMENTS[idx], price);
                    if (i % 500_000 == 0)
                        System.out.println("Number of tuples streamed into Ignite: " + i);
                }
            }
            // Select top 3 best performing instruments.
            SqlFieldsQuery top3qry = new SqlFieldsQuery("select symbol, (latest - open) from Instrument order by (latest - open) desc limit 3");
            // Execute queries.
            List<List<?>> top3 = instCache.query(top3qry).getAll();
            System.out.println("Top performing financial instruments: ");
            // Print top 10 words.
            ExamplesUtils.printQueryResults(top3);
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(mktDataCfg.getName());
            ignite.destroyCache(instCfg.getName());
        }
    }
}
Also used : QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) ExamplesUtils(org.apache.ignite.examples.ExamplesUtils) Random(java.util.Random) Ignite(org.apache.ignite.Ignite) ExampleNodeStartup(org.apache.ignite.examples.ExampleNodeStartup) StreamVisitor(org.apache.ignite.stream.StreamVisitor) IgniteCache(org.apache.ignite.IgniteCache) Serializable(java.io.Serializable) List(java.util.List) Ignition(org.apache.ignite.Ignition) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Ignite(org.apache.ignite.Ignite) List(java.util.List) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 15 with IgniteDataStreamer

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

the class ColumnDecisionTreeTrainerBenchmark method loadVectorsIntoSparseDistributedMatrixCache.

/**
 * Load vectors into sparse distributed matrix.
 *
 * @param cacheName Name of cache where matrix is stored.
 * @param uuid UUID of matrix.
 * @param iter Iterator over vectors.
 * @param vectorSize size of vectors.
 */
private void loadVectorsIntoSparseDistributedMatrixCache(String cacheName, UUID uuid, Iterator<? extends org.apache.ignite.ml.math.Vector> iter, int vectorSize) {
    try (IgniteDataStreamer<SparseMatrixKey, Map<Integer, Double>> streamer = Ignition.localIgnite().dataStreamer(cacheName)) {
        int sampleIdx = 0;
        streamer.allowOverwrite(true);
        streamer.receiver(StreamTransformer.from((e, arg) -> {
            Map<Integer, Double> val = e.getValue();
            if (val == null)
                val = new Int2DoubleOpenHashMap();
            val.putAll((Map<Integer, Double>) arg[0]);
            e.setValue(val);
            return null;
        }));
        // Feature index -> (sample index -> value)
        Map<Integer, Map<Integer, Double>> batch = new HashMap<>();
        IntStream.range(0, vectorSize).forEach(i -> batch.put(i, new HashMap<>()));
        int batchSize = 1000;
        while (iter.hasNext()) {
            org.apache.ignite.ml.math.Vector next = iter.next();
            for (int i = 0; i < vectorSize; i++) batch.get(i).put(sampleIdx, next.getX(i));
            X.println("Sample index: " + sampleIdx);
            if (sampleIdx % batchSize == 0) {
                batch.keySet().forEach(fi -> streamer.addData(new SparseMatrixKey(fi, uuid, fi), batch.get(fi)));
                IntStream.range(0, vectorSize).forEach(i -> batch.put(i, new HashMap<>()));
            }
            sampleIdx++;
        }
        if (sampleIdx % batchSize != 0) {
            batch.keySet().forEach(fi -> streamer.addData(new SparseMatrixKey(fi, uuid, fi), batch.get(fi)));
            IntStream.range(0, vectorSize).forEach(i -> batch.put(i, new HashMap<>()));
        }
    }
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) Arrays(java.util.Arrays) FeaturesCache(org.apache.ignite.ml.trees.trainers.columnbased.caches.FeaturesCache) IgniteTestResources(org.apache.ignite.testframework.junits.IgniteTestResources) Random(java.util.Random) BiIndex(org.apache.ignite.ml.trees.trainers.columnbased.BiIndex) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) SparseDistributedMatrixStorage(org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage) VarianceSplitCalculator(org.apache.ignite.ml.trees.trainers.columnbased.contsplitcalcs.VarianceSplitCalculator) Vector(org.apache.ignite.ml.math.Vector) Estimators(org.apache.ignite.ml.estimators.Estimators) Map(java.util.Map) X(org.apache.ignite.internal.util.typedef.X) Level(org.apache.log4j.Level) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) MatrixColumnDecisionTreeTrainerInput(org.apache.ignite.ml.trees.trainers.columnbased.MatrixColumnDecisionTreeTrainerInput) LabeledVectorDouble(org.apache.ignite.ml.structures.LabeledVectorDouble) BaseDecisionTreeTest(org.apache.ignite.ml.trees.BaseDecisionTreeTest) IgniteTriFunction(org.apache.ignite.ml.math.functions.IgniteTriFunction) ProjectionsCache(org.apache.ignite.ml.trees.trainers.columnbased.caches.ProjectionsCache) UUID(java.util.UUID) StreamTransformer(org.apache.ignite.stream.StreamTransformer) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) ContextCache(org.apache.ignite.ml.trees.trainers.columnbased.caches.ContextCache) DoubleStream(java.util.stream.DoubleStream) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Stream(java.util.stream.Stream) SparseMatrixKey(org.apache.ignite.ml.math.distributed.keys.impl.SparseMatrixKey) SplitCache(org.apache.ignite.ml.trees.trainers.columnbased.caches.SplitCache) RegionCalculators(org.apache.ignite.ml.trees.trainers.columnbased.regcalcs.RegionCalculators) IntStream(java.util.stream.IntStream) DecisionTreeModel(org.apache.ignite.ml.trees.models.DecisionTreeModel) IgniteFunction(org.apache.ignite.ml.math.functions.IgniteFunction) Model(org.apache.ignite.ml.Model) HashMap(java.util.HashMap) Function(java.util.function.Function) GiniSplitCalculator(org.apache.ignite.ml.trees.trainers.columnbased.contsplitcalcs.GiniSplitCalculator) BiIndexedCacheColumnDecisionTreeTrainerInput(org.apache.ignite.ml.trees.trainers.columnbased.BiIndexedCacheColumnDecisionTreeTrainerInput) CacheWriteSynchronizationMode(org.apache.ignite.cache.CacheWriteSynchronizationMode) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) MnistUtils(org.apache.ignite.ml.util.MnistUtils) LinkedList(java.util.LinkedList) Properties(java.util.Properties) Iterator(java.util.Iterator) ContinuousSplitCalculators(org.apache.ignite.ml.trees.trainers.columnbased.contsplitcalcs.ContinuousSplitCalculators) IOException(java.io.IOException) SplitDataGenerator(org.apache.ignite.ml.trees.SplitDataGenerator) Int2DoubleOpenHashMap(it.unimi.dsi.fastutil.ints.Int2DoubleOpenHashMap) Ignition(org.apache.ignite.Ignition) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) Tracer(org.apache.ignite.ml.math.Tracer) StorageConstants(org.apache.ignite.ml.math.StorageConstants) Assert(org.junit.Assert) Collections(java.util.Collections) ColumnDecisionTreeTrainer(org.apache.ignite.ml.trees.trainers.columnbased.ColumnDecisionTreeTrainer) GridCacheProcessor(org.apache.ignite.internal.processors.cache.GridCacheProcessor) InputStream(java.io.InputStream) CacheMode(org.apache.ignite.cache.CacheMode) HashMap(java.util.HashMap) Int2DoubleOpenHashMap(it.unimi.dsi.fastutil.ints.Int2DoubleOpenHashMap) Vector(org.apache.ignite.ml.math.Vector) Int2DoubleOpenHashMap(it.unimi.dsi.fastutil.ints.Int2DoubleOpenHashMap) SparseMatrixKey(org.apache.ignite.ml.math.distributed.keys.impl.SparseMatrixKey) Map(java.util.Map) HashMap(java.util.HashMap) Int2DoubleOpenHashMap(it.unimi.dsi.fastutil.ints.Int2DoubleOpenHashMap)

Aggregations

IgniteDataStreamer (org.apache.ignite.IgniteDataStreamer)24 Ignite (org.apache.ignite.Ignite)16 IgniteCache (org.apache.ignite.IgniteCache)10 List (java.util.List)6 CacheException (javax.cache.CacheException)6 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)6 Random (java.util.Random)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Ignition (org.apache.ignite.Ignition)5 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)4 ExampleNodeStartup (org.apache.ignite.examples.ExampleNodeStartup)4 ExamplesUtils (org.apache.ignite.examples.ExamplesUtils)4 Callable (java.util.concurrent.Callable)3 IgniteEx (org.apache.ignite.internal.IgniteEx)3 StreamTransformer (org.apache.ignite.stream.StreamTransformer)3 Serializable (java.io.Serializable)2 PreparedStatement (java.sql.PreparedStatement)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2