use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter in project ignite by apache.
the class GridCacheProcessor method createCache.
/**
* @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 createCache(CacheConfiguration<?, ?> cfg, CacheGroupContext grp, @Nullable CachePluginManager pluginMgr, DynamicCacheDescriptor desc, AffinityTopologyVersion locStartTopVer, CacheObjectContext cacheObjCtx, boolean affNode, boolean updatesAllowed, boolean disabledAfterStart) 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;
validate(ctx.config(), cfg, desc.cacheType(), cfgStore);
if (pluginMgr == null)
pluginMgr = new CachePluginManager(ctx, cfg);
pluginMgr.validate();
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);
GridCacheAffinityManager affMgr = new GridCacheAffinityManager();
GridCacheEventManager evtMgr = new GridCacheEventManager();
CacheEvictionManager evictMgr = (nearEnabled || cfg.isOnheapCacheEnabled()) ? new GridCacheEvictionManager() : new CacheOffheapEvictionManager();
GridCacheQueryManager qryMgr = queryManager(cfg);
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);
storeMgr.initialize(cfgStore, sesHolders);
GridCacheContext<?, ?> cacheCtx = new GridCacheContext(ctx, sharedCtx, cfg, grp, desc.cacheType(), locStartTopVer, affNode, updatesAllowed, /*
* Managers in starting order!
* ===========================
*/
evtMgr, storeMgr, evictMgr, qryMgr, contQryMgr, dataStructuresMgr, ttlMgr, drMgr, rslvrMgr, pluginMgr, affMgr);
cacheCtx.statisticsEnabled(desc.cacheConfiguration().isStatisticsEnabled());
cacheCtx.cacheObjectContext(cacheObjCtx);
GridCacheAdapter cache = null;
switch(cfg.getCacheMode()) {
case LOCAL:
{
switch(cfg.getAtomicityMode()) {
case TRANSACTIONAL:
{
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:
{
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:
{
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.
* ===============================================
*/
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, affNode, true, /*
* Managers in starting order!
* ===========================
*/
evtMgr, storeMgr, evictMgr, qryMgr, contQryMgr, dataStructuresMgr, ttlMgr, drMgr, rslvrMgr, pluginMgr, affMgr);
cacheCtx.statisticsEnabled(desc.cacheConfiguration().isStatisticsEnabled());
cacheCtx.cacheObjectContext(cacheObjCtx);
GridDhtCacheAdapter dht = null;
switch(cfg.getAtomicityMode()) {
case TRANSACTIONAL:
{
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 GridCacheProcessor method onKernalStart.
/**
* @param cache Cache.
* @throws IgniteCheckedException If failed.
*/
private void onKernalStart(GridCacheAdapter<?, ?> cache) throws IgniteCheckedException {
GridCacheContext<?, ?> ctx = cache.context();
// Start DHT cache as well.
if (isNearEnabled(ctx)) {
GridDhtCacheAdapter dht = ctx.near().dht();
GridCacheContext<?, ?> dhtCtx = dht.context();
for (GridCacheManager mgr : dhtManagers(dhtCtx)) mgr.onKernalStart();
dht.onKernalStart();
if (log.isDebugEnabled())
log.debug("Executed onKernalStart() callback for DHT cache: " + dht.name());
}
for (GridCacheManager mgr : F.view(ctx.managers(), F0.notContains(dhtExcludes(ctx)))) mgr.onKernalStart();
cache.onKernalStart();
if (ctx.events().isRecordable(EventType.EVT_CACHE_STARTED))
ctx.events().addEvent(EventType.EVT_CACHE_STARTED);
if (log.isDebugEnabled())
log.debug("Executed onKernalStart() callback for cache [name=" + cache.name() + ", mode=" + cache.configuration().getCacheMode() + ']');
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter in project ignite by apache.
the class GridCacheProcessor method onKernalStop.
/**
* @param cache Cache to stop.
* @param cancel Cancel flag.
*/
@SuppressWarnings("unchecked")
private void onKernalStop(GridCacheAdapter<?, ?> cache, boolean cancel) {
GridCacheContext ctx = cache.context();
if (isNearEnabled(ctx)) {
GridDhtCacheAdapter dht = ctx.near().dht();
if (dht != null) {
GridCacheContext<?, ?> dhtCtx = dht.context();
for (GridCacheManager mgr : dhtManagers(dhtCtx)) mgr.onKernalStop(cancel);
dht.onKernalStop();
}
}
List<GridCacheManager> mgrs = ctx.managers();
Collection<GridCacheManager> excludes = dhtExcludes(ctx);
// Reverse order.
for (ListIterator<GridCacheManager> it = mgrs.listIterator(mgrs.size()); it.hasPrevious(); ) {
GridCacheManager mgr = it.previous();
if (!excludes.contains(mgr))
mgr.onKernalStop(cancel);
}
cache.onKernalStop();
if (!ctx.isRecoveryMode() && ctx.events().isRecordable(EventType.EVT_CACHE_STOPPED))
ctx.events().addEvent(EventType.EVT_CACHE_STOPPED);
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter in project ignite by apache.
the class GridCacheProcessor method stopCache.
/**
* @param cache Cache to stop.
* @param cancel Cancel flag.
* @param destroy Destroy data flag. Setting to <code>true</code> will remove all cache data.
* @param clearDbObjects If {@code false} DB objects don't removed (used for cache.close() on client node).
*/
@SuppressWarnings({ "unchecked" })
private void stopCache(GridCacheAdapter<?, ?> cache, boolean cancel, boolean destroy, boolean clearDbObjects) {
GridCacheContext ctx = cache.context();
try {
if (!cache.isNear() && ctx.shared().wal() != null) {
try {
ctx.shared().wal().flush(null, false);
} catch (IgniteCheckedException e) {
U.error(log, "Failed to flush write-ahead log on cache stop " + "[cache=" + ctx.name() + "]", e);
}
}
sharedCtx.removeCacheContext(ctx);
cache.stop();
cache.removeMetrics(destroy);
GridCacheContextInfo cacheInfo = new GridCacheContextInfo(ctx, false);
if (!clearDbObjects)
ctx.kernalContext().query().getIndexing().closeCacheOnClient(ctx.name());
else
ctx.kernalContext().query().onCacheStop(cacheInfo, !cache.context().group().persistenceEnabled() || destroy);
if (isNearEnabled(ctx)) {
GridDhtCacheAdapter dht = ctx.near().dht();
// Check whether dht cache has been started.
if (dht != null) {
dht.stop();
dht.removeMetrics(destroy);
GridCacheContext<?, ?> dhtCtx = dht.context();
List<GridCacheManager> dhtMgrs = dhtManagers(dhtCtx);
for (ListIterator<GridCacheManager> it = dhtMgrs.listIterator(dhtMgrs.size()); it.hasPrevious(); ) {
GridCacheManager mgr = it.previous();
mgr.stop(cancel, destroy);
}
}
}
List<GridCacheManager> mgrs = ctx.managers();
Collection<GridCacheManager> excludes = dhtExcludes(ctx);
// Reverse order.
for (ListIterator<GridCacheManager> it = mgrs.listIterator(mgrs.size()); it.hasPrevious(); ) {
GridCacheManager mgr = it.previous();
if (!excludes.contains(mgr))
mgr.stop(cancel, destroy);
}
ctx.kernalContext().continuous().onCacheStop(ctx);
ctx.kernalContext().cache().context().snapshot().onCacheStop(ctx, destroy);
ctx.kernalContext().coordinators().onCacheStop(ctx);
ctx.group().stopCache(ctx, destroy);
U.stopLifecycleAware(log, lifecycleAwares(ctx.group(), cache.configuration(), ctx.store().configuredStore()));
IgnitePageStoreManager pageStore;
if (destroy && (pageStore = sharedCtx.pageStore()) != null) {
try {
pageStore.removeCacheData(new StoredCacheData(ctx.config()));
} catch (IgniteCheckedException e) {
U.error(log, "Failed to delete cache configuration data while destroying cache" + "[cache=" + ctx.name() + "]", e);
}
}
if (log.isInfoEnabled()) {
if (ctx.group().sharedGroup())
log.info("Stopped cache [cacheName=" + cache.name() + ", group=" + ctx.group().name() + ']');
else
log.info("Stopped cache [cacheName=" + cache.name() + ']');
}
} finally {
cleanup(ctx);
}
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter in project ignite by apache.
the class IgniteTxHandler method mvccEnlistBatch.
/**
* Writes updated values on the backup node.
*
* @param tx Transaction.
* @param ctx Cache context.
* @param op Operation.
* @param keys Keys.
* @param vals Values sent from the primary node.
* @param snapshot Mvcc snapshot.
* @param batchNum Batch number.
* @param futId Future id.
* @throws IgniteCheckedException If failed.
*/
public void mvccEnlistBatch(GridDhtTxRemote tx, GridCacheContext ctx, EnlistOperation op, List<KeyCacheObject> keys, List<Message> vals, MvccSnapshot snapshot, IgniteUuid futId, int batchNum) throws IgniteCheckedException {
assert keys != null && (vals == null || vals.size() == keys.size());
assert tx != null;
GridDhtCacheAdapter dht = ctx.dht();
tx.addActiveCache(ctx, false);
for (int i = 0; i < keys.size(); i++) {
KeyCacheObject key = keys.get(i);
assert key != null;
int part = ctx.affinity().partition(key);
try {
GridDhtLocalPartition locPart = ctx.topology().localPartition(part, tx.topologyVersion(), false);
if (locPart != null && locPart.reserve()) {
try {
// Skip renting partitions.
if (locPart.state() == RENTING) {
tx.addInvalidPartition(ctx.cacheId(), part);
continue;
}
CacheObject val = null;
EntryProcessor entryProc = null;
Object[] invokeArgs = null;
boolean needOldVal = tx.txState().useMvccCaching(ctx.cacheId());
Message val0 = vals != null ? vals.get(i) : null;
CacheEntryInfoCollection entries = val0 instanceof CacheEntryInfoCollection ? (CacheEntryInfoCollection) val0 : null;
if (entries == null && !op.isDeleteOrLock() && !op.isInvoke())
val = (val0 instanceof CacheObject) ? (CacheObject) val0 : null;
if (entries == null && op.isInvoke()) {
assert val0 instanceof GridInvokeValue;
GridInvokeValue invokeVal = (GridInvokeValue) val0;
entryProc = invokeVal.entryProcessor();
invokeArgs = invokeVal.invokeArgs();
}
assert entries != null || entryProc != null || !op.isInvoke() : "entryProc=" + entryProc + ", op=" + op;
GridDhtCacheEntry entry = dht.entryExx(key, tx.topologyVersion());
GridCacheUpdateTxResult updRes;
while (true) {
ctx.shared().database().checkpointReadLock();
try {
if (entries == null) {
switch(op) {
case DELETE:
updRes = entry.mvccRemove(tx, ctx.localNodeId(), tx.topologyVersion(), snapshot, false, needOldVal, null, false);
break;
case INSERT:
case TRANSFORM:
case UPSERT:
case UPDATE:
updRes = entry.mvccSet(tx, ctx.localNodeId(), val, entryProc, invokeArgs, 0, tx.topologyVersion(), snapshot, op.cacheOperation(), false, false, needOldVal, null, false, false);
break;
default:
throw new IgniteSQLException("Cannot acquire lock for operation [op= " + op + "]" + "Operation is unsupported at the moment ", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
}
} else {
updRes = entry.mvccUpdateRowsWithPreloadInfo(tx, ctx.localNodeId(), tx.topologyVersion(), entries.infos(), op.cacheOperation(), snapshot, futId, batchNum);
}
break;
} catch (GridCacheEntryRemovedException ignore) {
entry = dht.entryExx(key);
} finally {
ctx.shared().database().checkpointReadUnlock();
}
}
if (!updRes.filtered())
ctx.shared().mvccCaching().addEnlisted(key, updRes.newValue(), 0, 0, tx.xidVersion(), updRes.oldValue(), tx.local(), tx.topologyVersion(), snapshot, ctx.cacheId(), tx, futId, batchNum);
assert updRes.updateFuture() == null : "Entry should not be locked on the backup";
} finally {
locPart.release();
}
} else
tx.addInvalidPartition(ctx.cacheId(), part);
} catch (GridDhtInvalidPartitionException e) {
tx.addInvalidPartition(ctx.cacheId(), e.partition());
}
}
}
Aggregations