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);
}
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;
}
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;
}
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();
}
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);
}
Aggregations