Search in sources :

Example 51 with CacheGroupContext

use of org.apache.ignite.internal.processors.cache.CacheGroupContext in project ignite by apache.

the class ValidateIndexesClosure method processIndex.

/**
 * @param cacheCtxWithIdx Cache context and appropriate index.
 * @param idleChecker Idle check closure.
 */
private Map<String, ValidateIndexesPartitionResult> processIndex(T2<GridCacheContext, Index> cacheCtxWithIdx, IgniteInClosure<Integer> idleChecker) {
    if (validateCtx.isCancelled())
        return emptyMap();
    GridCacheContext ctx = cacheCtxWithIdx.get1();
    Index idx = cacheCtxWithIdx.get2();
    ValidateIndexesPartitionResult idxValidationRes = new ValidateIndexesPartitionResult();
    boolean enoughIssues = false;
    Cursor cursor = null;
    try (Session session = mvccSession(cacheCtxWithIdx.get1())) {
        cursor = idx.find(session, null, null);
        if (cursor == null)
            throw new IgniteCheckedException("Can't iterate through index: " + idx);
    } catch (Throwable t) {
        IndexValidationIssue is = new IndexValidationIssue(null, ctx.name(), idx.getName(), t);
        log.error("Find in index failed: " + is.toString());
        idxValidationRes.reportIssue(is);
        enoughIssues = true;
    }
    final boolean skipConditions = checkFirst > 0 || checkThrough > 0;
    final boolean bothSkipConditions = checkFirst > 0 && checkThrough > 0;
    long current = 0;
    long processedNumber = 0;
    KeyCacheObject previousKey = null;
    while (!enoughIssues && !validateCtx.isCancelled()) {
        KeyCacheObject h2key = null;
        try {
            try {
                if (!cursor.next())
                    break;
            } catch (DbException e) {
                if (X.hasCause(e, CorruptedTreeException.class))
                    throw new IgniteCheckedException("Key is present in SQL index, but is missing in corresponding " + "data page. Previous successfully read key: " + CacheObjectUtils.unwrapBinaryIfNeeded(ctx.cacheObjectContext(), previousKey, true, true), X.cause(e, CorruptedTreeException.class));
                throw e;
            }
            H2CacheRow h2Row = (H2CacheRow) cursor.get();
            if (skipConditions) {
                if (bothSkipConditions) {
                    if (processedNumber > checkFirst)
                        break;
                    else if (current++ % checkThrough > 0)
                        continue;
                    else
                        processedNumber++;
                } else {
                    if (checkFirst > 0) {
                        if (current++ > checkFirst)
                            break;
                    } else {
                        if (current++ % checkThrough > 0)
                            continue;
                    }
                }
            }
            h2key = h2Row.key();
            if (h2Row.link() != 0L) {
                CacheDataRow cacheDataStoreRow = ctx.group().offheap().read(ctx, h2key);
                if (cacheDataStoreRow == null)
                    throw new IgniteCheckedException("Key is present in SQL index, but can't be found in CacheDataTree.");
            } else
                throw new IgniteCheckedException("Invalid index row, possibly deleted " + h2Row);
        } catch (Throwable t) {
            Object o = CacheObjectUtils.unwrapBinaryIfNeeded(ctx.cacheObjectContext(), h2key, true, true);
            IndexValidationIssue is = new IndexValidationIssue(String.valueOf(o), ctx.name(), idx.getName(), t);
            log.error("Failed to lookup key: " + is.toString());
            enoughIssues |= idxValidationRes.reportIssue(is);
        } finally {
            previousKey = h2key;
        }
    }
    CacheGroupContext group = ctx.group();
    String uniqueIdxName = String.format("[cacheGroup=%s, cacheGroupId=%s, cache=%s, cacheId=%s, idx=%s]", group.name(), group.groupId(), ctx.name(), ctx.cacheId(), idx.getName());
    idleChecker.apply(group.groupId());
    processedIndexes.incrementAndGet();
    printProgressOfIndexValidationIfNeeded();
    return Collections.singletonMap(uniqueIdxName, idxValidationRes);
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) Index(org.h2.index.Index) H2CacheRow(org.apache.ignite.internal.processors.query.h2.opt.H2CacheRow) Cursor(org.h2.index.Cursor) DbException(org.h2.message.DbException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CorruptedTreeException(org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) Session(org.h2.engine.Session) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 52 with CacheGroupContext

use of org.apache.ignite.internal.processors.cache.CacheGroupContext in project ignite by apache.

the class ValidateIndexesClosure method integrityCheckIndexesPartitions.

/**
 * @param grpIds Group ids.
 * @param idleChecker Idle check closure.
 */
private Map<Integer, IndexIntegrityCheckIssue> integrityCheckIndexesPartitions(Set<Integer> grpIds, IgniteInClosure<Integer> idleChecker) {
    if (!checkCrc)
        return Collections.emptyMap();
    List<Future<T2<Integer, IndexIntegrityCheckIssue>>> integrityCheckFutures = new ArrayList<>(grpIds.size());
    Map<Integer, IndexIntegrityCheckIssue> integrityCheckResults = new HashMap<>();
    int curFut = 0;
    IgniteCacheDatabaseSharedManager db = ignite.context().cache().context().database();
    try {
        for (Integer grpId : grpIds) {
            final CacheGroupContext grpCtx = ignite.context().cache().cacheGroup(grpId);
            if (grpCtx == null || !grpCtx.persistenceEnabled()) {
                integrityCheckedIndexes.incrementAndGet();
                continue;
            }
            Future<T2<Integer, IndexIntegrityCheckIssue>> checkFut = calcExecutor.submit(new Callable<T2<Integer, IndexIntegrityCheckIssue>>() {

                @Override
                public T2<Integer, IndexIntegrityCheckIssue> call() {
                    IndexIntegrityCheckIssue issue = integrityCheckIndexPartition(grpCtx, idleChecker);
                    return new T2<>(grpCtx.groupId(), issue);
                }
            });
            integrityCheckFutures.add(checkFut);
        }
        for (Future<T2<Integer, IndexIntegrityCheckIssue>> fut : integrityCheckFutures) {
            T2<Integer, IndexIntegrityCheckIssue> res = fut.get();
            if (res.getValue() != null)
                integrityCheckResults.put(res.getKey(), res.getValue());
        }
    } catch (InterruptedException | ExecutionException e) {
        for (int j = curFut; j < integrityCheckFutures.size(); j++) integrityCheckFutures.get(j).cancel(false);
        throw unwrapFutureException(e);
    }
    return integrityCheckResults;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Future(java.util.concurrent.Future) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) ExecutionException(java.util.concurrent.ExecutionException) T2(org.apache.ignite.internal.util.typedef.T2) IgniteCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.IgniteCacheDatabaseSharedManager)

Example 53 with CacheGroupContext

use of org.apache.ignite.internal.processors.cache.CacheGroupContext in project ignite by apache.

the class ValidateIndexesClosure method collectGroupIds.

/**
 * Detect groups for further analysis.
 *
 * @return Collection of group id.
 */
private Set<Integer> collectGroupIds() {
    Set<Integer> grpIds = new HashSet<>();
    Set<String> missingCaches = new HashSet<>();
    if (cacheNames != null) {
        for (String cacheName : cacheNames) {
            DynamicCacheDescriptor desc = ignite.context().cache().cacheDescriptor(cacheName);
            if (desc == null)
                missingCaches.add(cacheName);
            else if (ignite.context().cache().cacheGroup(desc.groupId()).affinityNode())
                grpIds.add(desc.groupId());
        }
        if (!missingCaches.isEmpty()) {
            String errStr = "The following caches do not exist: " + String.join(", ", missingCaches);
            throw new IgniteException(errStr);
        }
    } else {
        Collection<CacheGroupContext> groups = ignite.context().cache().cacheGroups();
        for (CacheGroupContext grp : groups) {
            if (!grp.systemCache() && !grp.isLocal() && grp.affinityNode())
                grpIds.add(grp.groupId());
        }
    }
    return grpIds;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) DynamicCacheDescriptor(org.apache.ignite.internal.processors.cache.DynamicCacheDescriptor) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) HashSet(java.util.HashSet)

Example 54 with CacheGroupContext

use of org.apache.ignite.internal.processors.cache.CacheGroupContext in project ignite by apache.

the class IgnitePdsIndexingDefragmentationTest method testMultipleIndexes.

/**
 * @throws Exception If failed.
 */
@Test
public void testMultipleIndexes() throws Exception {
    startGrid(0).cluster().state(ClusterState.ACTIVE);
    IgniteCache<?, ?> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    cache.query(new SqlFieldsQuery("CREATE TABLE TEST (ID INT PRIMARY KEY, VAL_INT INT, VAL_OBJ LONG)"));
    final String cacheName = "SQL_default_TEST";
    cache.query(new SqlFieldsQuery("CREATE INDEX TEST_VAL_INT ON TEST(VAL_INT)"));
    cache.query(new SqlFieldsQuery("CREATE INDEX TEST_VAL_OBJ ON TEST(VAL_OBJ)"));
    for (int i = 0; i < ADDED_KEYS_COUNT; i++) cache.query(new SqlFieldsQuery("INSERT INTO TEST VALUES (?, ?, ?)").setArgs(i, i, (long) i));
    cache.query(new SqlFieldsQuery("DELETE FROM TEST WHERE MOD(ID, 2) = 0"));
    createMaintenanceRecord(cacheName);
    CacheGroupContext grp = grid(0).context().cache().cacheGroup(CU.cacheId(cacheName));
    forceCheckpoint();
    // Restart first time.
    stopGrid(0);
    defragmentAndValidateSizesDecreasedAfterDefragmentation(0, grp);
    startGrid(0);
    // Reinit cache object.
    cache = grid(0).cache(DEFAULT_CACHE_NAME);
    assertTrue(explainQuery(cache, "EXPLAIN SELECT * FROM TEST WHERE ID > 0").contains("_key_pk_proxy"));
    cache.query(new SqlFieldsQuery("SELECT * FROM TEST WHERE ID > 0")).getAll();
    assertTrue(explainQuery(cache, "EXPLAIN SELECT * FROM TEST WHERE VAL_INT > 0").contains("test_val_int"));
    cache.query(new SqlFieldsQuery("SELECT * FROM TEST WHERE VAL_INT > 0")).getAll();
    assertTrue(explainQuery(cache, "EXPLAIN SELECT * FROM TEST WHERE VAL_OBJ > 0").contains("test_val_obj"));
    cache.query(new SqlFieldsQuery("SELECT * FROM TEST WHERE VAL_OBJ > 0")).getAll();
}
Also used : CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteCacheUpdateSqlQuerySelfTest(org.apache.ignite.internal.processors.cache.IgniteCacheUpdateSqlQuerySelfTest) Test(org.junit.Test)

Example 55 with CacheGroupContext

use of org.apache.ignite.internal.processors.cache.CacheGroupContext in project ignite by apache.

the class IgniteTxConcurrentRemoveObjectsTest method checkTxLeavesObjectsInLocalPartition.

/**
 * Too many deletes in single transaction may overflow {@link GridDhtLocalPartition#rmvQueue} and entries will be
 * deleted synchronously in {@link GridDhtLocalPartition#onDeferredDelete(int, KeyCacheObject, GridCacheVersion)}.
 * This should not corrupt internal map state in {@link GridDhtLocalPartition}.
 *
 * @throws Exception If failed.
 */
public void checkTxLeavesObjectsInLocalPartition(CacheConfiguration<Integer, String> ccfg, TransactionConcurrency optimistic, TransactionIsolation isolation) throws Exception {
    IgniteEx igniteEx = grid(0);
    igniteEx.getOrCreateCache(ccfg);
    try (IgniteDataStreamer<Integer, String> dataStreamer = igniteEx.dataStreamer(DEFAULT_CACHE_NAME)) {
        for (int i = 0; i < CACHE_ENTRIES_COUNT; i++) dataStreamer.addData(i, UUID.randomUUID().toString());
    }
    IgniteEx client = startClientGrid(getConfiguration().setIgniteInstanceName(UUID.randomUUID().toString()));
    awaitPartitionMapExchange();
    assertEquals(CACHE_ENTRIES_COUNT, client.getOrCreateCache(DEFAULT_CACHE_NAME).size());
    try (Transaction tx = client.transactions().txStart(optimistic, isolation)) {
        IgniteCache<Integer, String> cache = client.getOrCreateCache(cacheConfiguration());
        for (int v = 0; v < CACHE_ENTRIES_COUNT; v++) {
            cache.get(v);
            cache.remove(v);
        }
        tx.commit();
    }
    GridTestUtils.waitForCondition(() -> igniteEx.context().cache().cacheGroups().stream().filter(CacheGroupContext::userCache).flatMap(cgctx -> cgctx.topology().localPartitions().stream()).mapToInt(GridDhtLocalPartition::internalSize).max().orElse(-1) == 0, 500L);
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) SERIALIZABLE(org.apache.ignite.transactions.TransactionIsolation.SERIALIZABLE) Transaction(org.apache.ignite.transactions.Transaction) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteEx(org.apache.ignite.internal.IgniteEx) Test(org.junit.Test) UUID(java.util.UUID) REPEATABLE_READ(org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) IgniteCache(org.apache.ignite.IgniteCache) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) WithSystemProperty(org.apache.ignite.testframework.junits.WithSystemProperty) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) IGNITE_CACHE_REMOVED_ENTRIES_TTL(org.apache.ignite.IgniteSystemProperties.IGNITE_CACHE_REMOVED_ENTRIES_TTL) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext)

Aggregations

CacheGroupContext (org.apache.ignite.internal.processors.cache.CacheGroupContext)103 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)31 IgniteEx (org.apache.ignite.internal.IgniteEx)29 Map (java.util.Map)27 HashMap (java.util.HashMap)24 IgniteException (org.apache.ignite.IgniteException)22 ArrayList (java.util.ArrayList)21 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 Test (org.junit.Test)20 GridDhtLocalPartition (org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition)19 List (java.util.List)17 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)17 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)16 GridDhtPartitionTopology (org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology)16 HashSet (java.util.HashSet)13 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)12 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)12 GridCacheSharedContext (org.apache.ignite.internal.processors.cache.GridCacheSharedContext)12 Set (java.util.Set)11 Collection (java.util.Collection)10