use of org.apache.ignite.internal.GridKernalContext in project ignite by apache.
the class GridCacheNestedTxAbstractTest method testTwoTx.
/**
* JUnit.
*
* @throws Exception If failed.
*/
@Test
public void testTwoTx() throws Exception {
final IgniteCache<String, Integer> c = grid(0).cache(DEFAULT_CACHE_NAME);
GridKernalContext ctx = ((IgniteKernal) grid(0)).context();
c.put(CNTR_KEY, 0);
for (int i = 0; i < 10; i++) {
try (Transaction tx = grid(0).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
c.get(CNTR_KEY);
ctx.closure().callLocalSafe((new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
assertFalse(((GridCacheAdapter) c).context().tm().inUserTx());
assertNull(((GridCacheAdapter) c).context().tm().userTx());
return true;
}
}), true);
tx.commit();
}
}
}
use of org.apache.ignite.internal.GridKernalContext in project ignite by apache.
the class AbstractReadRepairTest method setDifferentValuesForSameKey.
/**
*/
private InconsistentMapping setDifferentValuesForSameKey(int key, boolean misses, boolean nulls, ReadRepairStrategy strategy) throws Exception {
List<Ignite> nodes = new ArrayList<>();
Map<Ignite, T2<Integer, GridCacheVersion>> mapping = new HashMap<>();
Ignite primary = primaryNode(key, DEFAULT_CACHE_NAME);
ThreadLocalRandom rnd = ThreadLocalRandom.current();
if (rnd.nextBoolean()) {
// Reversed order.
nodes.addAll(backupNodes(key, DEFAULT_CACHE_NAME));
nodes.add(primary);
} else {
nodes.add(primary);
nodes.addAll(backupNodes(key, DEFAULT_CACHE_NAME));
}
if (// Random order.
rnd.nextBoolean())
Collections.shuffle(nodes);
IgniteInternalCache<Integer, Integer> internalCache = (grid(1)).cachex(DEFAULT_CACHE_NAME);
GridCacheVersionManager mgr = ((GridCacheAdapter) internalCache.cache()).context().shared().versions();
int incVal = 0;
Integer primVal = null;
Collection<T2<Integer, GridCacheVersion>> vals = new ArrayList<>();
if (misses) {
List<Ignite> keeped = nodes.subList(0, rnd.nextInt(1, nodes.size()));
nodes.stream().filter(node -> !keeped.contains(node)).forEach(node -> {
T2<Integer, GridCacheVersion> nullT2 = new T2<>(null, null);
vals.add(nullT2);
mapping.put(node, nullT2);
});
// Recording nulls (missed values).
nodes = keeped;
}
boolean rmvd = false;
boolean incVer = rnd.nextBoolean();
GridCacheVersion ver = null;
for (Ignite node : nodes) {
IgniteInternalCache<Integer, Integer> cache = ((IgniteEx) node).cachex(DEFAULT_CACHE_NAME);
GridCacheAdapter<Integer, Integer> adapter = (GridCacheAdapter) cache.cache();
GridCacheEntryEx entry = adapter.entryEx(key);
if (ver == null || incVer)
// Incremental version.
ver = mgr.next(entry.context().kernalContext().discovery().topologyVersion());
boolean rmv = nulls && (!rmvd || rnd.nextBoolean());
Integer val = rmv ? null : rnd.nextBoolean() ? /*increment or same as previously*/
++incVal : incVal;
T2<Integer, GridCacheVersion> valVer = new T2<>(val, val != null ? ver : null);
vals.add(valVer);
mapping.put(node, valVer);
GridKernalContext kctx = ((IgniteEx) node).context();
// Incremental value.
byte[] bytes = kctx.cacheObjects().marshal(entry.context().cacheObjectContext(), rmv ? -1 : val);
try {
kctx.cache().context().database().checkpointReadLock();
boolean init = entry.initialValue(new CacheObjectImpl(null, bytes), ver, 0, 0, false, AffinityTopologyVersion.NONE, GridDrType.DR_NONE, false, false);
if (rmv) {
if (cache.configuration().getAtomicityMode() == ATOMIC)
entry.innerUpdate(ver, ((IgniteEx) node).localNode().id(), ((IgniteEx) node).localNode().id(), GridCacheOperation.DELETE, null, null, false, false, false, false, null, false, false, false, false, AffinityTopologyVersion.NONE, null, GridDrType.DR_NONE, 0, 0, null, false, false, null, null, null, null, false);
else
entry.innerRemove(null, ((IgniteEx) node).localNode().id(), ((IgniteEx) node).localNode().id(), false, false, false, false, false, null, AffinityTopologyVersion.NONE, CU.empty0(), GridDrType.DR_NONE, null, null, null, 1L);
rmvd = true;
assertFalse(entry.hasValue());
} else
assertTrue(entry.hasValue());
assertTrue("iterableKey " + key + " already inited", init);
if (node.equals(primary))
primVal = val;
} finally {
((IgniteEx) node).context().cache().context().database().checkpointReadUnlock();
}
}
assertEquals(vals.size(), mapping.size());
assertEquals(vals.size(), internalCache.configuration().getCacheMode() == REPLICATED ? serverNodesCount() : backupsCount() + 1);
if (!misses && !nulls)
// Primary value set.
assertTrue(primVal != null);
Integer fixed;
boolean consistent;
boolean repairable;
if (vals.stream().distinct().count() == 1) {
// Consistent value.
consistent = true;
repairable = true;
fixed = vals.iterator().next().getKey();
} else {
consistent = false;
// Currently, Atomic caches can not be repaired.
repairable = atomicityMode() != ATOMIC;
switch(strategy) {
case LWW:
if (misses || rmvd || !incVer) {
repairable = false;
// Should never be returned.
fixed = Integer.MIN_VALUE;
} else
fixed = incVal;
break;
case PRIMARY:
fixed = primVal;
break;
case RELATIVE_MAJORITY:
// Should never be returned.
fixed = Integer.MIN_VALUE;
Map<T2<Integer, GridCacheVersion>, Integer> counts = new HashMap<>();
for (T2<Integer, GridCacheVersion> val : vals) {
counts.putIfAbsent(val, 0);
counts.compute(val, (k, v) -> v + 1);
}
int[] sorted = counts.values().stream().sorted(Comparator.reverseOrder()).mapToInt(v -> v).toArray();
int max = sorted[0];
if (sorted.length > 1 && sorted[1] == max)
repairable = false;
if (repairable)
for (Map.Entry<T2<Integer, GridCacheVersion>, Integer> count : counts.entrySet()) if (count.getValue().equals(max)) {
fixed = count.getKey().getKey();
break;
}
break;
case REMOVE:
fixed = null;
break;
case CHECK_ONLY:
repairable = false;
// Should never be returned.
fixed = Integer.MIN_VALUE;
break;
default:
throw new UnsupportedOperationException(strategy.toString());
}
}
return new InconsistentMapping(mapping, primVal, fixed, repairable, consistent);
}
use of org.apache.ignite.internal.GridKernalContext in project ignite by apache.
the class MaintenanceRegistrySimpleTest method initContext.
/**
*/
private GridKernalContext initContext(boolean persistenceEnabled) throws IgniteCheckedException {
String dfltWorkDir = U.defaultWorkDirectory();
GridKernalContext kctx = new StandaloneGridKernalContext(log, null, null) {
@Override
protected IgniteConfiguration prepareIgniteConfiguration() {
IgniteConfiguration cfg = super.prepareIgniteConfiguration();
cfg.setDataStorageConfiguration(new DataStorageConfiguration().setDefaultDataRegionConfiguration(new DataRegionConfiguration().setPersistenceEnabled(persistenceEnabled)));
return cfg;
}
@Override
public PdsFoldersResolver pdsFolderResolver() {
return new PdsFoldersResolver() {
@Override
public PdsFolderSettings resolveFolders() {
return new PdsFolderSettings(new File(dfltWorkDir), U.maskForFileName(""));
}
};
}
};
return kctx;
}
use of org.apache.ignite.internal.GridKernalContext in project ignite by apache.
the class LightweightCheckpointTest method testLightCheckpointAbleToStoreOnlyGivenDataRegion.
/**
* 1. Start the one node with disabled WAL and with two caches.
* 2. Disable default checkpoint.
* 3. Create light checkpoint for one cache and configure checkpoint listener for it.
* 4. Fill the both caches.
* 5. Trigger the light checkpoint and wait for the finish.
* 6. Stop the node and start it again.
* 7. Expected: Cache which was checkpointed would have the all data meanwhile second cache would be empty.
*
* @throws Exception if fail.
*/
@Test
public void testLightCheckpointAbleToStoreOnlyGivenDataRegion() throws Exception {
// given: One started node with default cache and cache which won't be checkpointed.
IgniteEx ignite0 = startGrid(0);
ignite0.cluster().active(true);
IgniteCache<Integer, Object> checkpointedCache = ignite0.cache(DEFAULT_CACHE_NAME);
IgniteCache<Integer, Object> notCheckpointedCache = ignite0.cache(NOT_CHECKPOINTED_CACHE);
GridKernalContext context = ignite0.context();
GridCacheDatabaseSharedManager db = (GridCacheDatabaseSharedManager) (context.cache().context().database());
waitForCondition(() -> !db.getCheckpointer().currentProgress().inProgress(), 10_000);
// and: disable the default checkpoint.
db.enableCheckpoints(false);
DataRegion regionForCheckpoint = db.dataRegion(DFLT_DATA_REG_DEFAULT_NAME);
// and: Create light checkpoint with only one region.
LightweightCheckpointManager lightweightCheckpointManager = new LightweightCheckpointManager(context::log, context.igniteInstanceName(), "light-test-checkpoint", context.workersRegistry(), context.config().getDataStorageConfiguration(), () -> Arrays.asList(regionForCheckpoint), grpId -> getPageMemoryForCacheGroup(grpId, db, context), PageMemoryImpl.ThrottlingPolicy.CHECKPOINT_BUFFER_ONLY, context.cache().context().snapshot(), db.persistentStoreMetricsImpl(), context.longJvmPauseDetector(), context.failure(), context.cache());
// and: Add checkpoint listener for DEFAULT_CACHE in order of storing the meta pages.
lightweightCheckpointManager.addCheckpointListener((CheckpointListener) context.cache().cacheGroup(groupIdForCache(ignite0, DEFAULT_CACHE_NAME)).offheap(), regionForCheckpoint);
lightweightCheckpointManager.start();
// when: Fill the caches
for (int j = 0; j < 1024; j++) {
checkpointedCache.put(j, j);
notCheckpointedCache.put(j, j);
}
// and: Trigger and wait for the checkpoint.
lightweightCheckpointManager.forceCheckpoint("test", null).futureFor(CheckpointState.FINISHED).get();
// and: Stop and start node.
stopAllGrids();
ignite0 = startGrid(0);
ignite0.cluster().active(true);
checkpointedCache = ignite0.cache(DEFAULT_CACHE_NAME);
notCheckpointedCache = ignite0.cache(NOT_CHECKPOINTED_CACHE);
// then: Checkpointed cache should have all data meanwhile uncheckpointed cache should be empty.
for (int j = 1; j < 1024; j++) {
assertEquals(j, checkpointedCache.get(j));
assertNull(notCheckpointedCache.get(j));
}
GridCacheDatabaseSharedManager db2 = (GridCacheDatabaseSharedManager) (ignite0.context().cache().context().database());
waitForCondition(() -> !db2.getCheckpointer().currentProgress().inProgress(), 10_000);
String nodeFolderName = ignite0.context().pdsFolderResolver().resolveFolders().folderName();
File cpMarkersDir = Paths.get(U.defaultWorkDirectory(), "db", nodeFolderName, "cp").toFile();
// then: Expected only two pairs checkpoint markers - both from the start of node.
assertEquals(4, cpMarkersDir.listFiles().length);
}
use of org.apache.ignite.internal.GridKernalContext in project ignite by apache.
the class IgniteWalIteratorSwitchSegmentTest method initiate.
/**
* Initiate WAL manager.
*
* @param serVer WAL serializer version.
* @param workDir Work directory path.
* @return Tuple of WAL manager and WAL record serializer.
* @throws IgniteCheckedException If some think failed.
*/
private T2<IgniteWriteAheadLogManager, RecordSerializer> initiate(int serVer, String workDir) throws IgniteCheckedException {
GridKernalContext kctx = new StandaloneGridKernalContext(log, null, null) {
@Override
protected IgniteConfiguration prepareIgniteConfiguration() {
IgniteConfiguration cfg = super.prepareIgniteConfiguration();
cfg.setDataStorageConfiguration(new DataStorageConfiguration().setWalSegmentSize(SEGMENT_SIZE).setWalRecordIteratorBufferSize(SEGMENT_SIZE / 2).setWalMode(WALMode.FSYNC).setWalPath(workDir + WORK_SUB_DIR).setWalArchivePath(workDir + ARCHIVE_SUB_DIR).setFileIOFactory(new RandomAccessFileIOFactory()));
cfg.setEventStorageSpi(new NoopEventStorageSpi());
return cfg;
}
@Override
public GridInternalSubscriptionProcessor internalSubscriptionProcessor() {
return new GridInternalSubscriptionProcessor(this);
}
@Override
public GridEventStorageManager event() {
return new GridEventStorageManager(this);
}
};
IgniteWriteAheadLogManager walMgr = new FileWriteAheadLogManager(kctx);
GridTestUtils.setFieldValue(walMgr, "serializerVer", serVer);
GridCacheSharedContext<?, ?> ctx = new GridCacheSharedContext<>(kctx, null, null, null, null, walMgr, new WalStateManager(kctx), new GridCacheDatabaseSharedManager(kctx), null, null, null, null, null, new GridCacheIoManager(), null, null, null, null, null, null, null);
walMgr.start(ctx);
walMgr.onActivate(kctx);
walMgr.resumeLogging(null);
RecordSerializer recordSerializer = new RecordSerializerFactoryImpl(ctx).createSerializer(walMgr.serializerVersion());
return new T2<>(walMgr, recordSerializer);
}
Aggregations