use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter in project ignite by apache.
the class GridCacheAdapter method localEntries.
/**
* {@inheritDoc}
*/
@Override
public final Iterable<Cache.Entry<K, V>> localEntries(CachePeekMode[] peekModes) throws IgniteCheckedException {
assert peekModes != null;
ctx.checkSecurity(SecurityPermission.CACHE_READ);
PeekModes modes = parsePeekModes(peekModes, false);
Collection<Iterator<Cache.Entry<K, V>>> its = new ArrayList<>();
final boolean keepBinary = ctx.keepBinary();
if (ctx.isLocal()) {
modes.primary = true;
modes.backup = true;
}
if (modes.offheap) {
if (modes.heap && modes.near && ctx.isNear())
its.add(ctx.near().nearEntries().iterator());
if (modes.primary || modes.backup) {
AffinityTopologyVersion topVer = ctx.affinity().affinityTopologyVersion();
IgniteCacheOffheapManager offheapMgr = ctx.isNear() ? ctx.near().dht().context().offheap() : ctx.offheap();
MvccSnapshot mvccSnapshot = ctx.mvccEnabled() ? MvccUtils.MVCC_MAX_SNAPSHOT : null;
its.add(offheapMgr.cacheEntriesIterator(ctx, modes.primary, modes.backup, topVer, ctx.keepBinary(), mvccSnapshot, null));
}
} else if (modes.heap) {
if (ctx.mvccEnabled())
return F.emptyIterator();
if (modes.near && ctx.isNear())
its.add(ctx.near().nearEntries().iterator());
if (modes.primary || modes.backup) {
GridDhtCacheAdapter<K, V> cache = ctx.isNear() ? ctx.near().dht() : ctx.dht();
its.add(cache.localEntriesIterator(modes.primary, modes.backup, keepBinary));
}
}
final Iterator<Cache.Entry<K, V>> it = F.flatIterators(its);
return new Iterable<Cache.Entry<K, V>>() {
@Override
public Iterator<Cache.Entry<K, V>> iterator() {
return it;
}
@Override
public String toString() {
return "CacheLocalEntries []";
}
};
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter in project ignite by apache.
the class GridCacheProcessor method createCacheContext.
/**
* @param cfg Cache configuration to use to create cache.
* @param grp Cache group.
* @param pluginMgr Cache plugin manager.
* @param desc Cache descriptor.
* @param locStartTopVer Current topology version.
* @param cacheObjCtx Cache object context.
* @param affNode {@code True} if local node affinity node.
* @param updatesAllowed Updates allowed flag.
* @param disabledAfterStart If true, then we will discard restarting state from proxies. If false then we will
* change state of proxies to restarting
* @return Cache context.
* @throws IgniteCheckedException If failed to create cache.
*/
private GridCacheContext<?, ?> createCacheContext(CacheConfiguration<?, ?> cfg, CacheGroupContext grp, @Nullable CachePluginManager pluginMgr, DynamicCacheDescriptor desc, AffinityTopologyVersion locStartTopVer, CacheObjectContext cacheObjCtx, boolean affNode, boolean updatesAllowed, boolean disabledAfterStart, boolean recoveryMode) throws IgniteCheckedException {
assert cfg != null;
if (cfg.getCacheStoreFactory() instanceof GridCacheLoaderWriterStoreFactory) {
GridCacheLoaderWriterStoreFactory factory = (GridCacheLoaderWriterStoreFactory) cfg.getCacheStoreFactory();
prepare(cfg, factory.loaderFactory(), false);
prepare(cfg, factory.writerFactory(), false);
} else
prepare(cfg, cfg.getCacheStoreFactory(), false);
CacheStore cfgStore = cfg.getCacheStoreFactory() != null ? cfg.getCacheStoreFactory().create() : null;
ValidationOnNodeJoinUtils.validate(ctx.config(), cfg, desc.cacheType(), cfgStore, ctx, log, (x, y) -> {
try {
assertParameter(x, y);
} catch (IgniteCheckedException ex) {
return ex;
}
return null;
});
if (pluginMgr == null)
pluginMgr = new CachePluginManager(ctx, cfg);
pluginMgr.validate();
if (!recoveryMode && cfg.getAtomicityMode() == TRANSACTIONAL_SNAPSHOT && grp.affinityNode())
sharedCtx.coordinators().ensureStarted();
sharedCtx.jta().registerCache(cfg);
// Skip suggestions for internal caches.
if (desc.cacheType().userCache())
suggestOptimizations(cfg, cfgStore != null);
Collection<Object> toPrepare = new ArrayList<>();
if (cfgStore instanceof GridCacheLoaderWriterStore) {
toPrepare.add(((GridCacheLoaderWriterStore) cfgStore).loader());
toPrepare.add(((GridCacheLoaderWriterStore) cfgStore).writer());
} else
toPrepare.add(cfgStore);
prepare(cfg, toPrepare);
U.startLifecycleAware(lifecycleAwares(grp, cfg, cfgStore));
boolean nearEnabled = GridCacheUtils.isNearEnabled(cfg);
CacheCompressionManager compressMgr = new CacheCompressionManager();
GridCacheAffinityManager affMgr = new GridCacheAffinityManager();
GridCacheEventManager evtMgr = new GridCacheEventManager();
CacheEvictionManager evictMgr = (nearEnabled || cfg.isOnheapCacheEnabled()) ? new GridCacheEvictionManager() : new CacheOffheapEvictionManager();
GridCacheQueryManager qryMgr = cfg.getCacheMode() == LOCAL ? new GridCacheLocalQueryManager() : new GridCacheDistributedQueryManager();
CacheContinuousQueryManager contQryMgr = new CacheContinuousQueryManager();
CacheDataStructuresManager dataStructuresMgr = new CacheDataStructuresManager();
GridCacheTtlManager ttlMgr = new GridCacheTtlManager();
CacheConflictResolutionManager rslvrMgr = pluginMgr.createComponent(CacheConflictResolutionManager.class);
GridCacheDrManager drMgr = pluginMgr.createComponent(GridCacheDrManager.class);
CacheStoreManager storeMgr = pluginMgr.createComponent(CacheStoreManager.class);
PlatformCacheManager platformMgr = ctx.platform().cacheManager();
if (cfgStore == null)
storeMgr.initialize(cfgStore, sesHolders);
else
initializationProtector.protect(cfgStore, () -> storeMgr.initialize(cfgStore, sesHolders));
GridCacheContext<?, ?> cacheCtx = new GridCacheContext(ctx, sharedCtx, cfg, grp, desc.cacheType(), locStartTopVer, desc.deploymentId(), affNode, updatesAllowed, desc.cacheConfiguration().isStatisticsEnabled(), recoveryMode, /*
* Managers in starting order!
* ===========================
*/
compressMgr, evtMgr, storeMgr, evictMgr, qryMgr, contQryMgr, dataStructuresMgr, ttlMgr, drMgr, rslvrMgr, pluginMgr, affMgr, platformMgr);
cacheCtx.cacheObjectContext(cacheObjCtx);
GridCacheAdapter cache = null;
switch(cfg.getCacheMode()) {
case LOCAL:
{
switch(cfg.getAtomicityMode()) {
case TRANSACTIONAL:
case TRANSACTIONAL_SNAPSHOT:
{
cache = new GridLocalCache(cacheCtx);
break;
}
case ATOMIC:
{
cache = new GridLocalAtomicCache(cacheCtx);
break;
}
default:
{
assert false : "Invalid cache atomicity mode: " + cfg.getAtomicityMode();
}
}
break;
}
case PARTITIONED:
case REPLICATED:
{
if (nearEnabled) {
switch(cfg.getAtomicityMode()) {
case TRANSACTIONAL:
case TRANSACTIONAL_SNAPSHOT:
{
cache = new GridNearTransactionalCache(cacheCtx);
break;
}
case ATOMIC:
{
cache = new GridNearAtomicCache(cacheCtx);
break;
}
default:
{
assert false : "Invalid cache atomicity mode: " + cfg.getAtomicityMode();
}
}
} else {
switch(cfg.getAtomicityMode()) {
case TRANSACTIONAL:
case TRANSACTIONAL_SNAPSHOT:
{
cache = cacheCtx.affinityNode() ? new GridDhtColocatedCache(cacheCtx) : new GridDhtColocatedCache(cacheCtx, new GridNoStorageCacheMap());
break;
}
case ATOMIC:
{
cache = cacheCtx.affinityNode() ? new GridDhtAtomicCache(cacheCtx) : new GridDhtAtomicCache(cacheCtx, new GridNoStorageCacheMap());
break;
}
default:
{
assert false : "Invalid cache atomicity mode: " + cfg.getAtomicityMode();
}
}
}
break;
}
default:
{
assert false : "Invalid cache mode: " + cfg.getCacheMode();
}
}
cache.active(!disabledAfterStart);
cacheCtx.cache(cache);
GridCacheContext<?, ?> ret = cacheCtx;
/*
* Create DHT cache.
* ================
*/
if (cfg.getCacheMode() != LOCAL && nearEnabled) {
/*
* Specifically don't create the following managers
* here and reuse the one from Near cache:
* 1. GridCacheVersionManager
* 2. GridCacheIoManager
* 3. GridCacheDeploymentManager
* 4. GridCacheQueryManager (note, that we start it for DHT cache though).
* 5. CacheContinuousQueryManager (note, that we start it for DHT cache though).
* 6. GridCacheDgcManager
* 7. GridCacheTtlManager.
* 8. PlatformCacheManager.
* ===============================================
*/
evictMgr = cfg.isOnheapCacheEnabled() ? new GridCacheEvictionManager() : new CacheOffheapEvictionManager();
evtMgr = new GridCacheEventManager();
pluginMgr = new CachePluginManager(ctx, cfg);
drMgr = pluginMgr.createComponent(GridCacheDrManager.class);
cacheCtx = new GridCacheContext(ctx, sharedCtx, cfg, grp, desc.cacheType(), locStartTopVer, desc.deploymentId(), affNode, true, desc.cacheConfiguration().isStatisticsEnabled(), recoveryMode, /*
* Managers in starting order!
* ===========================
*/
compressMgr, evtMgr, storeMgr, evictMgr, qryMgr, contQryMgr, dataStructuresMgr, ttlMgr, drMgr, rslvrMgr, pluginMgr, affMgr, platformMgr);
cacheCtx.cacheObjectContext(cacheObjCtx);
GridDhtCacheAdapter dht = null;
switch(cfg.getAtomicityMode()) {
case TRANSACTIONAL:
case TRANSACTIONAL_SNAPSHOT:
{
assert cache instanceof GridNearTransactionalCache;
GridNearTransactionalCache near = (GridNearTransactionalCache) cache;
GridDhtCache dhtCache = cacheCtx.affinityNode() ? new GridDhtCache(cacheCtx) : new GridDhtCache(cacheCtx, new GridNoStorageCacheMap());
dhtCache.near(near);
near.dht(dhtCache);
dht = dhtCache;
break;
}
case ATOMIC:
{
assert cache instanceof GridNearAtomicCache;
GridNearAtomicCache near = (GridNearAtomicCache) cache;
GridDhtAtomicCache dhtCache = cacheCtx.affinityNode() ? new GridDhtAtomicCache(cacheCtx) : new GridDhtAtomicCache(cacheCtx, new GridNoStorageCacheMap());
dhtCache.near(near);
near.dht(dhtCache);
dht = dhtCache;
break;
}
default:
{
assert false : "Invalid cache atomicity mode: " + cfg.getAtomicityMode();
}
}
cacheCtx.cache(dht);
}
if (!CU.isUtilityCache(cache.name()) && !CU.isSystemCache(cache.name())) {
registerMbean(cache.localMxBean(), cache.name(), false);
registerMbean(cache.clusterMxBean(), cache.name(), false);
}
return ret;
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter in project ignite by apache.
the class GridCacheUtils method createBackupPostProcessingClosure.
/**
* Creates closure that saves initial value to backup partition.
* <p>
* Useful only when store with readThrough is used. In situation when
* get() on backup node returns successful result, it's expected that
* localPeek() will be successful as well. But it isn't true when
* primary node loaded value from local store, in this case backups
* will remain non-initialized.
* <br>
* To meet that requirement the value requested from primary should
* be saved on backup during get().
* </p>
*
* @param topVer Topology version.
* @param log Logger.
* @param cctx Cache context.
* @param key Key.
* @param expiryPlc Expiry policy.
* @param readThrough Read through.
* @param skipVals Skip values.
*/
@Nullable
public static BackupPostProcessingClosure createBackupPostProcessingClosure(final AffinityTopologyVersion topVer, final IgniteLogger log, final GridCacheContext cctx, @Nullable final KeyCacheObject key, @Nullable final IgniteCacheExpiryPolicy expiryPlc, boolean readThrough, boolean skipVals) {
if (cctx.mvccEnabled() || !readThrough || skipVals || (key != null && !cctx.affinity().backupsByKey(key, topVer).contains(cctx.localNode())))
return null;
return new BackupPostProcessingClosure() {
private void process(KeyCacheObject key, CacheObject val, GridCacheVersion ver, GridDhtCacheAdapter colocated) {
while (true) {
GridCacheEntryEx entry = null;
cctx.shared().database().checkpointReadLock();
try {
entry = colocated.entryEx(key, topVer);
entry.initialValue(val, ver, expiryPlc == null ? 0 : expiryPlc.forCreate(), expiryPlc == null ? 0 : toExpireTime(expiryPlc.forCreate()), true, topVer, GridDrType.DR_BACKUP, true, false);
break;
} catch (GridCacheEntryRemovedException ignore) {
if (log.isDebugEnabled())
log.debug("Got removed entry during postprocessing (will retry): " + entry);
} catch (IgniteCheckedException e) {
U.error(log, "Error saving backup value: " + entry, e);
throw new GridClosureException(e);
} catch (GridDhtInvalidPartitionException ignored) {
break;
} finally {
if (entry != null)
entry.touch();
cctx.shared().database().checkpointReadUnlock();
}
}
}
@Override
public void apply(CacheObject val, GridCacheVersion ver) {
process(key, val, ver, cctx.dht());
}
@Override
public void apply(Collection<GridCacheEntryInfo> infos) {
if (!F.isEmpty(infos)) {
GridCacheAffinityManager aff = cctx.affinity();
ClusterNode locNode = cctx.localNode();
GridDhtCacheAdapter colocated = cctx.cache().isNear() ? ((GridNearCacheAdapter) cctx.cache()).dht() : cctx.dht();
for (GridCacheEntryInfo info : infos) {
// Save backup value.
if (aff.backupsByKey(info.key(), topVer).contains(locNode))
process(info.key(), info.value(), info.version(), colocated);
}
}
}
};
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter in project ignite by apache.
the class GridCacheNearTxMultiNodeSelfTest method testTxCleanup.
/**
* @throws Exception If failed.
*/
@SuppressWarnings({ "unchecked" })
@Test
public void testTxCleanup() throws Exception {
backups = 1;
Ignite ignite = startGrids(GRID_CNT);
try {
Integer mainKey = 0;
ClusterNode priNode = ignite.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(mainKey);
ClusterNode backupNode = F.first(F.view(ignite.affinity(DEFAULT_CACHE_NAME).mapKeyToPrimaryAndBackups(mainKey), F.notIn(F.asList(priNode))));
ClusterNode otherNode = F.first(ignite.cluster().forPredicate(F.notIn(F.asList(priNode, backupNode))).nodes());
assert priNode != backupNode;
assert backupNode != otherNode;
assert priNode != otherNode;
final Ignite priIgnite = grid(priNode);
Ignite backupIgnite = grid(backupNode);
Ignite otherIgnite = grid(otherNode);
List<Ignite> ignites = F.asList(otherIgnite, priIgnite, backupIgnite);
int cntr = 0;
// Update main key from all nodes.
for (Ignite g : ignites) g.cache(DEFAULT_CACHE_NAME).put(mainKey, ++cntr);
info("Updated mainKey from all nodes.");
int keyCnt = 200;
Set<Integer> keys = new TreeSet<>();
// Populate cache from all nodes.
for (int i = 1; i <= keyCnt; i++) {
keys.add(i);
Ignite g = F.rand(ignites);
g.cache(DEFAULT_CACHE_NAME).put(new AffinityKey<>(i, mainKey), Integer.toString(cntr++));
}
IgniteCache cache = priIgnite.cache(DEFAULT_CACHE_NAME);
Transaction tx = priIgnite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
try {
cache.get(mainKey);
cache.removeAll(keys);
cache.put(mainKey, ++cntr);
tx.commit();
} catch (Error | Exception e) {
error("Transaction failed: " + tx, e);
throw e;
} finally {
tx.close();
}
stopGrid(priIgnite.name(), true);
stopGrid(backupIgnite.name(), true);
Ignite newIgnite = startGrid(GRID_CNT);
ignites = F.asList(otherIgnite, newIgnite);
for (Ignite g : ignites) {
GridNearCacheAdapter near = ((IgniteKernal) g).internalCache(DEFAULT_CACHE_NAME).context().near();
GridDhtCacheAdapter dht = near.dht();
checkTm(g, near.context().tm());
checkTm(g, dht.context().tm());
}
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter in project ignite by apache.
the class IgniteFailoverAbstractBenchmark method awaitPartitionMapExchange.
/**
* Awaits for partitiona map exchage.
*
* @param ignite Ignite.
* @throws Exception If failed.
*/
@SuppressWarnings("BusyWait")
protected static void awaitPartitionMapExchange(Ignite ignite) throws Exception {
IgniteLogger log = ignite.log();
log.info("Waiting for finishing of a partition exchange on node: " + ignite);
IgniteKernal kernal = (IgniteKernal) ignite;
while (true) {
boolean partitionsExchangeFinished = true;
for (IgniteInternalCache<?, ?> cache : kernal.cachesx(null)) {
log.info("Checking cache: " + cache.name());
GridCacheAdapter<?, ?> c = kernal.internalCache(cache.name());
if (!(c instanceof GridDhtCacheAdapter))
break;
GridDhtCacheAdapter<?, ?> dht = (GridDhtCacheAdapter<?, ?>) c;
GridDhtPartitionFullMap partMap = dht.topology().partitionMap(true);
for (Map.Entry<UUID, GridDhtPartitionMap> e : partMap.entrySet()) {
log.info("Checking node: " + e.getKey());
for (Map.Entry<Integer, GridDhtPartitionState> e1 : e.getValue().entrySet()) {
if (e1.getValue() != GridDhtPartitionState.OWNING) {
log.info("Undesired state [id=" + e1.getKey() + ", state=" + e1.getValue() + ']');
partitionsExchangeFinished = false;
break;
}
}
if (!partitionsExchangeFinished)
break;
}
if (!partitionsExchangeFinished)
break;
}
if (partitionsExchangeFinished)
return;
Thread.sleep(100);
}
}
Aggregations