Search in sources :

Example 31 with IgniteInClosure

use of org.apache.ignite.lang.IgniteInClosure in project ignite by apache.

the class CacheMvccSqlQueriesAbstractTest method updateSingleValue.

/**
 * @param singleNode {@code True} for test with single node.
 * @param locQry Local query flag.
 * @throws Exception If failed.
 */
private void updateSingleValue(boolean singleNode, final boolean locQry) throws Exception {
    final int VALS = 100;
    final int writers = 4;
    final int readers = 4;
    final int INC_BY = 110;
    final IgniteInClosure<IgniteCache<Object, Object>> init = new IgniteInClosure<IgniteCache<Object, Object>>() {

        @Override
        public void apply(IgniteCache<Object, Object> cache) {
            Map<Integer, MvccTestSqlIndexValue> vals = new HashMap<>();
            for (int i = 0; i < VALS; i++) vals.put(i, new MvccTestSqlIndexValue(i));
            cache.putAll(vals);
        }
    };
    GridInClosure3<Integer, List<TestCache>, AtomicBoolean> writer = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {

        @Override
        public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            int cnt = 0;
            while (!stop.get()) {
                TestCache<Integer, MvccTestSqlIndexValue> cache = randomCache(caches, rnd);
                try {
                    Integer key = rnd.nextInt(VALS);
                    while (true) {
                        try {
                            cache.cache.invoke(key, new CacheEntryProcessor<Integer, MvccTestSqlIndexValue, Object>() {

                                @Override
                                public Object process(MutableEntry<Integer, MvccTestSqlIndexValue> e, Object... args) {
                                    Integer key = e.getKey();
                                    MvccTestSqlIndexValue val = e.getValue();
                                    int newIdxVal;
                                    if (val.idxVal1 < INC_BY) {
                                        assertEquals(key.intValue(), val.idxVal1);
                                        newIdxVal = val.idxVal1 + INC_BY;
                                    } else {
                                        assertEquals(INC_BY + key, val.idxVal1);
                                        newIdxVal = key;
                                    }
                                    e.setValue(new MvccTestSqlIndexValue(newIdxVal));
                                    return null;
                                }
                            });
                            break;
                        } catch (CacheException e) {
                            MvccFeatureChecker.assertMvccWriteConflict(e);
                        }
                    }
                } finally {
                    cache.readUnlock();
                }
            }
            info("Writer finished, updates: " + cnt);
        }
    };
    GridInClosure3<Integer, List<TestCache>, AtomicBoolean> reader = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {

        @Override
        public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            List<SqlFieldsQuery> fieldsQrys = new ArrayList<>();
            fieldsQrys.add(new SqlFieldsQuery("select _key, idxVal1 from MvccTestSqlIndexValue where idxVal1=?").setLocal(locQry));
            fieldsQrys.add(new SqlFieldsQuery("select _key, idxVal1 from MvccTestSqlIndexValue where idxVal1=? or idxVal1=?").setLocal(locQry));
            fieldsQrys.add(new SqlFieldsQuery("select _key, idxVal1 from MvccTestSqlIndexValue where _key=?").setLocal(locQry));
            List<SqlQuery<Integer, MvccTestSqlIndexValue>> sqlQrys = new ArrayList<>();
            sqlQrys.add(new SqlQuery<Integer, MvccTestSqlIndexValue>(MvccTestSqlIndexValue.class, "idxVal1=?").setLocal(locQry));
            sqlQrys.add(new SqlQuery<Integer, MvccTestSqlIndexValue>(MvccTestSqlIndexValue.class, "idxVal1=? or idxVal1=?").setLocal(locQry));
            sqlQrys.add(new SqlQuery<Integer, MvccTestSqlIndexValue>(MvccTestSqlIndexValue.class, "_key=?").setLocal(locQry));
            while (!stop.get()) {
                Integer key = rnd.nextInt(VALS);
                int qryIdx = rnd.nextInt(3);
                TestCache<Integer, MvccTestSqlIndexValue> cache = randomCache(caches, rnd);
                List<List<?>> res;
                try {
                    if (rnd.nextBoolean()) {
                        SqlFieldsQuery qry = fieldsQrys.get(qryIdx);
                        if (qryIdx == 1)
                            qry.setArgs(key, key + INC_BY);
                        else
                            qry.setArgs(key);
                        res = cache.cache.query(qry).getAll();
                    } else {
                        SqlQuery<Integer, MvccTestSqlIndexValue> qry = sqlQrys.get(qryIdx);
                        if (qryIdx == 1)
                            qry.setArgs(key, key + INC_BY);
                        else
                            qry.setArgs(key);
                        res = new ArrayList<>();
                        for (IgniteCache.Entry<Integer, MvccTestSqlIndexValue> e : cache.cache.query(qry).getAll()) {
                            List<Object> row = new ArrayList<>(2);
                            row.add(e.getKey());
                            row.add(e.getValue().idxVal1);
                            res.add(row);
                        }
                    }
                } finally {
                    cache.readUnlock();
                }
                assertTrue(qryIdx == 0 || !res.isEmpty());
                if (!res.isEmpty()) {
                    assertEquals(1, res.size());
                    List<?> resVals = res.get(0);
                    Integer key0 = (Integer) resVals.get(0);
                    Integer val0 = (Integer) resVals.get(1);
                    assertEquals(key, key0);
                    assertTrue(val0.equals(key) || val0.equals(key + INC_BY));
                }
            }
            if (idx == 0) {
                SqlFieldsQuery qry = new SqlFieldsQuery("select _key, idxVal1 from MvccTestSqlIndexValue");
                TestCache<Integer, MvccTestSqlIndexValue> cache = randomCache(caches, rnd);
                List<List<?>> res;
                try {
                    res = cache.cache.query(qry).getAll();
                } finally {
                    cache.readUnlock();
                }
                assertEquals(VALS, res.size());
                for (List<?> vals : res) info("Value: " + vals);
            }
        }
    };
    int srvs;
    int clients;
    if (singleNode) {
        srvs = 1;
        clients = 0;
    } else {
        srvs = 4;
        clients = 2;
    }
    readWriteTest(null, srvs, clients, 0, DFLT_PARTITION_COUNT, writers, readers, DFLT_TEST_TIME, new InitIndexing(Integer.class, MvccTestSqlIndexValue.class), init, writer, reader);
    for (Ignite node : G.allGrids()) checkActiveQueriesCleanup(node);
}
Also used : HashMap(java.util.HashMap) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) GridInClosure3(org.apache.ignite.internal.util.lang.GridInClosure3) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite) SqlQuery(org.apache.ignite.cache.query.SqlQuery) IgniteCache(org.apache.ignite.IgniteCache) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure)

Example 32 with IgniteInClosure

use of org.apache.ignite.lang.IgniteInClosure in project ignite by apache.

the class RunningQueriesTest method getConfiguration.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteConfiguration getConfiguration(String gridName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(gridName);
    cfg.setDiscoverySpi(new TcpDiscoverySpi() {

        @Override
        public void sendCustomEvent(DiscoverySpiCustomMessage msg) throws IgniteException {
            if (CustomMessageWrapper.class.isAssignableFrom(msg.getClass())) {
                DiscoveryCustomMessage delegate = ((CustomMessageWrapper) msg).delegate();
                if (DynamicCacheChangeBatch.class.isAssignableFrom(delegate.getClass())) {
                    ((DynamicCacheChangeBatch) delegate).requests().stream().filter((c) -> !c.cacheName().equalsIgnoreCase("default")).findAny().ifPresent((c) -> {
                        try {
                            awaitTimeout();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });
                } else if (SchemaProposeDiscoveryMessage.class.isAssignableFrom(delegate.getClass())) {
                    try {
                        awaitTimeout();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            super.sendCustomEvent(msg);
        }
    });
    cfg.setCommunicationSpi(new TcpCommunicationSpi() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void sendMessage(ClusterNode node, Message msg, IgniteInClosure<IgniteException> ackC) {
            if (GridIoMessage.class.isAssignableFrom(msg.getClass())) {
                Message gridMsg = ((GridIoMessage) msg).message();
                if (GridNearAtomicSingleUpdateFilterRequest.class.isAssignableFrom(gridMsg.getClass()) || GridNearAtomicFullUpdateRequest.class.isAssignableFrom(gridMsg.getClass())) {
                    try {
                        awaitTimeout();
                    } catch (Exception ignore) {
                    }
                }
            }
            super.sendMessage(node, msg, ackC);
        }
    });
    return cfg;
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Connection(java.sql.Connection) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) GridNearAtomicFullUpdateRequest(org.apache.ignite.internal.processors.cache.distributed.dht.atomic.GridNearAtomicFullUpdateRequest) TimeoutException(java.util.concurrent.TimeoutException) IgniteEx(org.apache.ignite.internal.IgniteEx) SqlQuery(org.apache.ignite.cache.query.SqlQuery) Cache(javax.cache.Cache) QueryEntity(org.apache.ignite.cache.QueryEntity) CyclicBarrier(java.util.concurrent.CyclicBarrier) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) Collection(java.util.Collection) IgniteException(org.apache.ignite.IgniteException) Set(java.util.Set) Description(org.junit.runner.Description) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) GridIoMessage(org.apache.ignite.internal.managers.communication.GridIoMessage) DiscoverySpiCustomMessage(org.apache.ignite.spi.discovery.DiscoverySpiCustomMessage) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) Objects(java.util.Objects) TestWatcher(org.junit.rules.TestWatcher) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Stream(java.util.stream.Stream) Message(org.apache.ignite.plugin.extensions.communication.Message) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) TcpCommunicationSpi(org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi) IgniteUtils.resolveIgnitePath(org.apache.ignite.internal.util.IgniteUtils.resolveIgnitePath) CustomMessageWrapper(org.apache.ignite.internal.managers.discovery.CustomMessageWrapper) GridNearAtomicSingleUpdateFilterRequest(org.apache.ignite.internal.processors.cache.distributed.dht.atomic.GridNearAtomicSingleUpdateFilterRequest) SQLException(java.sql.SQLException) ClusterNode(org.apache.ignite.cluster.ClusterNode) DiscoveryCustomMessage(org.apache.ignite.internal.managers.discovery.DiscoveryCustomMessage) IgniteH2Indexing(org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) G(org.apache.ignite.internal.util.typedef.G) SchemaProposeDiscoveryMessage(org.apache.ignite.internal.processors.query.schema.message.SchemaProposeDiscoveryMessage) AbstractIndexingCommonTest(org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) Ignore(org.junit.Ignore) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Statement(java.sql.Statement) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi) Assert(org.junit.Assert) Collections(java.util.Collections) DynamicCacheChangeBatch(org.apache.ignite.internal.processors.cache.DynamicCacheChangeBatch) ClusterNode(org.apache.ignite.cluster.ClusterNode) CustomMessageWrapper(org.apache.ignite.internal.managers.discovery.CustomMessageWrapper) GridIoMessage(org.apache.ignite.internal.managers.communication.GridIoMessage) GridIoMessage(org.apache.ignite.internal.managers.communication.GridIoMessage) DiscoverySpiCustomMessage(org.apache.ignite.spi.discovery.DiscoverySpiCustomMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) DiscoveryCustomMessage(org.apache.ignite.internal.managers.discovery.DiscoveryCustomMessage) SchemaProposeDiscoveryMessage(org.apache.ignite.internal.processors.query.schema.message.SchemaProposeDiscoveryMessage) DiscoveryCustomMessage(org.apache.ignite.internal.managers.discovery.DiscoveryCustomMessage) TimeoutException(java.util.concurrent.TimeoutException) IgniteException(org.apache.ignite.IgniteException) SQLException(java.sql.SQLException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) TcpCommunicationSpi(org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi) DiscoverySpiCustomMessage(org.apache.ignite.spi.discovery.DiscoverySpiCustomMessage) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) IgniteException(org.apache.ignite.IgniteException) DynamicCacheChangeBatch(org.apache.ignite.internal.processors.cache.DynamicCacheChangeBatch) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi)

Example 33 with IgniteInClosure

use of org.apache.ignite.lang.IgniteInClosure in project ignite by apache.

the class CacheMvccAbstractTest method updateNObjectsTest.

/**
 * @param N Number of object to update in single transaction.
 * @param srvs Number of server nodes.
 * @param clients Number of client nodes.
 * @param cacheBackups Number of cache backups.
 * @param cacheParts Number of cache partitions.
 * @param time Test time.
 * @param readMode Read mode.
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
protected void updateNObjectsTest(final int N, final int srvs, final int clients, int cacheBackups, int cacheParts, long time, @Nullable IgniteInClosure<CacheConfiguration> cfgC, ReadMode readMode, WriteMode writeMode, RestartMode restartMode) throws Exception {
    final int TOTAL = 20;
    assert N <= TOTAL;
    info("updateNObjectsTest [n=" + N + ", total=" + TOTAL + ']');
    final int writers = 4;
    final int readers = 4;
    final IgniteInClosure<IgniteCache<Object, Object>> init = new IgniteInClosure<IgniteCache<Object, Object>>() {

        @Override
        public void apply(IgniteCache<Object, Object> cache) {
            final IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
            Map<Integer, Integer> vals = new LinkedHashMap<>();
            for (int i = 0; i < TOTAL; i++) vals.put(i, N);
            try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                writeAllByMode(cache, vals, writeMode, INTEGER_CODEC);
                tx.commit();
            }
        }
    };
    GridInClosure3<Integer, List<TestCache>, AtomicBoolean> writer = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {

        @Override
        public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            int cnt = 0;
            while (!stop.get()) {
                TestCache<Integer, Integer> cache = randomCache(caches, rnd);
                IgniteTransactions txs = cache.cache.unwrap(Ignite.class).transactions();
                TreeSet<Integer> keys = new TreeSet<>();
                while (keys.size() < N) keys.add(rnd.nextInt(TOTAL));
                try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                    tx.timeout(TX_TIMEOUT);
                    Map<Integer, Integer> curVals = readAllByMode(cache.cache, keys, readMode, INTEGER_CODEC);
                    assertEquals(N, curVals.size());
                    Map<Integer, Integer> newVals = new TreeMap<>();
                    for (Map.Entry<Integer, Integer> e : curVals.entrySet()) newVals.put(e.getKey(), e.getValue() + 1);
                    writeAllByMode(cache.cache, newVals, writeMode, INTEGER_CODEC);
                    tx.commit();
                } catch (Exception e) {
                    handleTxException(e);
                } finally {
                    cache.readUnlock();
                }
                cnt++;
            }
            info("Writer finished, updates: " + cnt);
        }
    };
    GridInClosure3<Integer, List<TestCache>, AtomicBoolean> reader = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {

        @Override
        public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            Set<Integer> keys = new LinkedHashSet<>();
            while (!stop.get()) {
                while (keys.size() < TOTAL) keys.add(rnd.nextInt(TOTAL));
                TestCache<Integer, Integer> cache = randomCache(caches, rnd);
                Map<Integer, Integer> vals = null;
                try {
                    vals = readAllByMode(cache.cache, keys, readMode, INTEGER_CODEC);
                } catch (Exception e) {
                    handleTxException(e);
                } finally {
                    cache.readUnlock();
                }
                assertEquals("vals=" + vals, TOTAL, vals.size());
                int sum = 0;
                for (int i = 0; i < TOTAL; i++) {
                    Integer val = vals.get(i);
                    assertNotNull(val);
                    sum += val;
                }
                assertEquals(0, sum % N);
            }
            if (idx == 0) {
                TestCache<Integer, Integer> cache = randomCache(caches, rnd);
                Map<Integer, Integer> vals;
                try {
                    vals = readAllByMode(cache.cache, keys, readMode, INTEGER_CODEC);
                } finally {
                    cache.readUnlock();
                }
                int sum = 0;
                for (int i = 0; i < TOTAL; i++) {
                    Integer val = vals.get(i);
                    info("Value [id=" + i + ", val=" + val + ']');
                    sum += val;
                }
                info("Sum [sum=" + sum + ", mod=" + sum % N + ']');
            }
        }
    };
    readWriteTest(restartMode, srvs, clients, cacheBackups, cacheParts, writers, readers, time, cfgC, init, writer, reader);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IgniteTransactions(org.apache.ignite.IgniteTransactions) LinkedHashMap(java.util.LinkedHashMap) GridInClosure3(org.apache.ignite.internal.util.lang.GridInClosure3) TreeSet(java.util.TreeSet) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) List(java.util.List) ArrayList(java.util.ArrayList) IgniteCache(org.apache.ignite.IgniteCache) TreeMap(java.util.TreeMap) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteFutureCancelledCheckedException(org.apache.ignite.internal.IgniteFutureCancelledCheckedException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) CacheException(javax.cache.CacheException) TransactionException(org.apache.ignite.transactions.TransactionException) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 34 with IgniteInClosure

use of org.apache.ignite.lang.IgniteInClosure in project ignite by apache.

the class IgniteWalReaderTest method testPutAllTxIntoTwoNodes.

/**
 * Tests transaction generation and WAL for putAll cache operation.
 *
 * @throws Exception if failed.
 */
@Test
public void testPutAllTxIntoTwoNodes() throws Exception {
    Ignite ignite = startGrid("node0");
    Ignite ignite1 = startGrid(1);
    ignite.cluster().active(true);
    Map<Object, IndexedObject> map = new TreeMap<>();
    int cntEntries = 1000;
    for (int i = 0; i < cntEntries; i++) map.put(i, new IndexedObject(i));
    ignite.cache(CACHE_NAME).putAll(map);
    ignite.cluster().active(false);
    String subfolderName1 = genDbSubfolderName(ignite, 0);
    String subfolderName2 = genDbSubfolderName(ignite1, 1);
    stopAllGrids();
    String workDir = U.defaultWorkDirectory();
    IgniteWalIteratorFactory factory = new IgniteWalIteratorFactory(log);
    StringBuilder sb = new StringBuilder();
    Map<GridCacheOperation, Integer> operationsFound = new EnumMap<>(GridCacheOperation.class);
    IgniteInClosure<DataRecord> drHnd = dataRecord -> {
        sb.append("{");
        for (int i = 0; i < dataRecord.entryCount(); i++) {
            DataEntry entry = dataRecord.get(i);
            GridCacheOperation op = entry.op();
            Integer cnt = operationsFound.get(op);
            operationsFound.put(op, cnt == null ? 1 : (cnt + 1));
            if (entry instanceof UnwrapDataEntry) {
                final UnwrapDataEntry entry1 = (UnwrapDataEntry) entry;
                sb.append(entry1.op()).append(" for ").append(entry1.unwrappedKey());
                final GridCacheVersion ver = entry.nearXidVersion();
                sb.append(", ");
                if (ver != null)
                    sb.append("tx=").append(ver).append(", ");
            }
        }
        sb.append("}\n");
    };
    scanIterateAndCount(factory, createIteratorParametersBuilder(workDir, subfolderName1).filesOrDirs(workDir + "/db/wal/" + subfolderName1, workDir + "/db/wal/archive/" + subfolderName1), 1, 1, null, drHnd);
    scanIterateAndCount(factory, createIteratorParametersBuilder(workDir, subfolderName2).filesOrDirs(workDir + "/db/wal/" + subfolderName2, workDir + "/db/wal/archive/" + subfolderName2), 1, 1, null, drHnd);
    Integer createsFound = operationsFound.get(CREATE);
    if (log.isInfoEnabled())
        log.info(sb.toString());
    assertTrue("Create operations should be found in log: " + operationsFound, createsFound != null && createsFound > 0);
    assertTrue("Create operations count should be at least " + cntEntries + " in log: " + operationsFound, createsFound >= cntEntries);
}
Also used : IgniteWalIteratorFactory(org.apache.ignite.internal.processors.cache.persistence.wal.reader.IgniteWalIteratorFactory) CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) BinaryObject(org.apache.ignite.binary.BinaryObject) MVCC_DATA_RECORD(org.apache.ignite.internal.pagemem.wal.record.WALRecord.RecordType.MVCC_DATA_RECORD) ObjectOutput(java.io.ObjectOutput) Transaction(org.apache.ignite.transactions.Transaction) Random(java.util.Random) IgniteEx(org.apache.ignite.internal.IgniteEx) DATA_RECORD_V2(org.apache.ignite.internal.pagemem.wal.record.WALRecord.RecordType.DATA_RECORD_V2) CacheRebalanceMode(org.apache.ignite.cache.CacheRebalanceMode) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) IgniteSystemProperties(org.apache.ignite.IgniteSystemProperties) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NullLogger(org.apache.ignite.logger.NullLogger) TxRecord(org.apache.ignite.internal.pagemem.wal.record.TxRecord) CREATE(org.apache.ignite.internal.processors.cache.GridCacheOperation.CREATE) Map(java.util.Map) Cache(javax.cache.Cache) DELETE(org.apache.ignite.internal.processors.cache.GridCacheOperation.DELETE) PdsFolderResolver.genNewStyleSubfolderName(org.apache.ignite.internal.processors.cache.persistence.filename.PdsFolderResolver.genNewStyleSubfolderName) WALMode(org.apache.ignite.configuration.WALMode) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Externalizable(java.io.Externalizable) EnumMap(java.util.EnumMap) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) WALRecord(org.apache.ignite.internal.pagemem.wal.record.WALRecord) UUID(java.util.UUID) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) DataEntry(org.apache.ignite.internal.pagemem.wal.record.DataEntry) IgniteCache(org.apache.ignite.IgniteCache) Serializable(java.io.Serializable) GridCacheOperation(org.apache.ignite.internal.processors.cache.GridCacheOperation) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) IteratorParametersBuilder(org.apache.ignite.internal.processors.cache.persistence.wal.reader.IgniteWalIteratorFactory.IteratorParametersBuilder) EVT_WAL_SEGMENT_COMPACTED(org.apache.ignite.events.EventType.EVT_WAL_SEGMENT_COMPACTED) MvccFeatureChecker(org.apache.ignite.testframework.MvccFeatureChecker) DFLT_STORE_DIR(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.DFLT_STORE_DIR) ObjectInput(java.io.ObjectInput) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) NotNull(org.jetbrains.annotations.NotNull) MarshalledDataEntry(org.apache.ignite.internal.pagemem.wal.record.MarshalledDataEntry) WALIterator(org.apache.ignite.internal.pagemem.wal.WALIterator) IgniteWalIteratorFactory(org.apache.ignite.internal.processors.cache.persistence.wal.reader.IgniteWalIteratorFactory) ClusterState(org.apache.ignite.cluster.ClusterState) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) U(org.apache.ignite.internal.util.typedef.internal.U) EVT_WAL_SEGMENT_ARCHIVED(org.apache.ignite.events.EventType.EVT_WAL_SEGMENT_ARCHIVED) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UnwrappedDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrappedDataEntry) UnwrapDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrapDataEntry) IgniteBiInClosure(org.apache.ignite.lang.IgniteBiInClosure) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) Assume(org.junit.Assume) IgniteEvents(org.apache.ignite.IgniteEvents) ACTIVE(org.apache.ignite.cluster.ClusterState.ACTIVE) Arrays.fill(java.util.Arrays.fill) Iterator(java.util.Iterator) IOException(java.io.IOException) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) File(java.io.File) T2(org.apache.ignite.internal.util.typedef.T2) TimeUnit(java.util.concurrent.TimeUnit) WithSystemProperty(org.apache.ignite.testframework.junits.WithSystemProperty) AtomicLong(java.util.concurrent.atomic.AtomicLong) TreeMap(java.util.TreeMap) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) RecordUtils(org.apache.ignite.testframework.wal.record.RecordUtils) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) WalSegmentArchivedEvent(org.apache.ignite.events.WalSegmentArchivedEvent) Assert(org.junit.Assert) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) UnwrapDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrapDataEntry) TreeMap(java.util.TreeMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DataEntry(org.apache.ignite.internal.pagemem.wal.record.DataEntry) MarshalledDataEntry(org.apache.ignite.internal.pagemem.wal.record.MarshalledDataEntry) UnwrappedDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrappedDataEntry) UnwrapDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrapDataEntry) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) Ignite(org.apache.ignite.Ignite) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) GridCacheOperation(org.apache.ignite.internal.processors.cache.GridCacheOperation) EnumMap(java.util.EnumMap) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 35 with IgniteInClosure

use of org.apache.ignite.lang.IgniteInClosure in project ignite by apache.

the class IgniteWalReaderTest method testPrimaryFlagOnTwoNodes.

/**
 * Tests transaction generation and WAL for putAll cache operation.
 *
 * @throws Exception if failed.
 */
@Test
public void testPrimaryFlagOnTwoNodes() throws Exception {
    backupCnt = 1;
    IgniteEx ignite = startGrid("node0");
    Ignite ignite1 = startGrid(1);
    ignite.cluster().state(ACTIVE);
    IgniteCache<Integer, IndexedObject> cache = ignite.cache(CACHE_NAME);
    backupCnt = 0;
    int cntEntries = 100;
    List<Integer> keys = findKeys(ignite.localNode(), cache, cntEntries, 0, 0);
    Map<Integer, IndexedObject> map = new TreeMap<>();
    for (Integer key : keys) map.putIfAbsent(key, new IndexedObject(key));
    cache.putAll(map);
    ignite.cluster().active(false);
    String subfolderName1 = genDbSubfolderName(ignite, 0);
    String subfolderName2 = genDbSubfolderName(ignite1, 1);
    stopAllGrids();
    String workDir = U.defaultWorkDirectory();
    IgniteWalIteratorFactory factory = new IgniteWalIteratorFactory(log);
    Map<GridCacheOperation, Integer> operationsFound = new EnumMap<>(GridCacheOperation.class);
    IgniteInClosure<DataRecord> drHnd = dataRecord -> {
        for (int i = 0; i < dataRecord.entryCount(); i++) {
            DataEntry entry = dataRecord.get(i);
            GridCacheOperation op = entry.op();
            Integer cnt = operationsFound.get(op);
            operationsFound.put(op, cnt == null ? 1 : (cnt + 1));
        }
    };
    scanIterateAndCount(factory, createIteratorParametersBuilder(workDir, subfolderName1).filesOrDirs(workDir + "/db/wal/" + subfolderName1, workDir + "/db/wal/archive/" + subfolderName1), 1, 1, null, drHnd);
    primary = false;
    scanIterateAndCount(factory, createIteratorParametersBuilder(workDir, subfolderName2).filesOrDirs(workDir + "/db/wal/" + subfolderName2, workDir + "/db/wal/archive/" + subfolderName2), 1, 1, null, drHnd);
}
Also used : IgniteWalIteratorFactory(org.apache.ignite.internal.processors.cache.persistence.wal.reader.IgniteWalIteratorFactory) CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) BinaryObject(org.apache.ignite.binary.BinaryObject) MVCC_DATA_RECORD(org.apache.ignite.internal.pagemem.wal.record.WALRecord.RecordType.MVCC_DATA_RECORD) ObjectOutput(java.io.ObjectOutput) Transaction(org.apache.ignite.transactions.Transaction) Random(java.util.Random) IgniteEx(org.apache.ignite.internal.IgniteEx) DATA_RECORD_V2(org.apache.ignite.internal.pagemem.wal.record.WALRecord.RecordType.DATA_RECORD_V2) CacheRebalanceMode(org.apache.ignite.cache.CacheRebalanceMode) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) IgniteSystemProperties(org.apache.ignite.IgniteSystemProperties) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NullLogger(org.apache.ignite.logger.NullLogger) TxRecord(org.apache.ignite.internal.pagemem.wal.record.TxRecord) CREATE(org.apache.ignite.internal.processors.cache.GridCacheOperation.CREATE) Map(java.util.Map) Cache(javax.cache.Cache) DELETE(org.apache.ignite.internal.processors.cache.GridCacheOperation.DELETE) PdsFolderResolver.genNewStyleSubfolderName(org.apache.ignite.internal.processors.cache.persistence.filename.PdsFolderResolver.genNewStyleSubfolderName) WALMode(org.apache.ignite.configuration.WALMode) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Externalizable(java.io.Externalizable) EnumMap(java.util.EnumMap) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) WALRecord(org.apache.ignite.internal.pagemem.wal.record.WALRecord) UUID(java.util.UUID) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) DataEntry(org.apache.ignite.internal.pagemem.wal.record.DataEntry) IgniteCache(org.apache.ignite.IgniteCache) Serializable(java.io.Serializable) GridCacheOperation(org.apache.ignite.internal.processors.cache.GridCacheOperation) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) IteratorParametersBuilder(org.apache.ignite.internal.processors.cache.persistence.wal.reader.IgniteWalIteratorFactory.IteratorParametersBuilder) EVT_WAL_SEGMENT_COMPACTED(org.apache.ignite.events.EventType.EVT_WAL_SEGMENT_COMPACTED) MvccFeatureChecker(org.apache.ignite.testframework.MvccFeatureChecker) DFLT_STORE_DIR(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.DFLT_STORE_DIR) ObjectInput(java.io.ObjectInput) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) NotNull(org.jetbrains.annotations.NotNull) MarshalledDataEntry(org.apache.ignite.internal.pagemem.wal.record.MarshalledDataEntry) WALIterator(org.apache.ignite.internal.pagemem.wal.WALIterator) IgniteWalIteratorFactory(org.apache.ignite.internal.processors.cache.persistence.wal.reader.IgniteWalIteratorFactory) ClusterState(org.apache.ignite.cluster.ClusterState) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) U(org.apache.ignite.internal.util.typedef.internal.U) EVT_WAL_SEGMENT_ARCHIVED(org.apache.ignite.events.EventType.EVT_WAL_SEGMENT_ARCHIVED) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UnwrappedDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrappedDataEntry) UnwrapDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrapDataEntry) IgniteBiInClosure(org.apache.ignite.lang.IgniteBiInClosure) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) Assume(org.junit.Assume) IgniteEvents(org.apache.ignite.IgniteEvents) ACTIVE(org.apache.ignite.cluster.ClusterState.ACTIVE) Arrays.fill(java.util.Arrays.fill) Iterator(java.util.Iterator) IOException(java.io.IOException) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) File(java.io.File) T2(org.apache.ignite.internal.util.typedef.T2) TimeUnit(java.util.concurrent.TimeUnit) WithSystemProperty(org.apache.ignite.testframework.junits.WithSystemProperty) AtomicLong(java.util.concurrent.atomic.AtomicLong) TreeMap(java.util.TreeMap) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) RecordUtils(org.apache.ignite.testframework.wal.record.RecordUtils) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) WalSegmentArchivedEvent(org.apache.ignite.events.WalSegmentArchivedEvent) Assert(org.junit.Assert) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) TreeMap(java.util.TreeMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DataEntry(org.apache.ignite.internal.pagemem.wal.record.DataEntry) MarshalledDataEntry(org.apache.ignite.internal.pagemem.wal.record.MarshalledDataEntry) UnwrappedDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrappedDataEntry) UnwrapDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrapDataEntry) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) GridCacheOperation(org.apache.ignite.internal.processors.cache.GridCacheOperation) EnumMap(java.util.EnumMap) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

IgniteInClosure (org.apache.ignite.lang.IgniteInClosure)39 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)25 List (java.util.List)22 ArrayList (java.util.ArrayList)20 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)18 HashMap (java.util.HashMap)17 Map (java.util.Map)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 Ignite (org.apache.ignite.Ignite)15 Test (org.junit.Test)15 UUID (java.util.UUID)14 IgniteCache (org.apache.ignite.IgniteCache)14 IgniteException (org.apache.ignite.IgniteException)13 Collection (java.util.Collection)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)12 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)12 U (org.apache.ignite.internal.util.typedef.internal.U)12 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)12 Nullable (org.jetbrains.annotations.Nullable)12