Search in sources :

Example 41 with GridCacheDatabaseSharedManager

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

the class GridCacheProcessor method onExchangeDone.

/**
 * Callback invoked when first exchange future for dynamic cache is completed.
 *
 * @param cacheStartVer Started caches version to create proxy for.
 * @param exchActions Change requests.
 * @param err Error.
 */
@SuppressWarnings("unchecked")
public void onExchangeDone(AffinityTopologyVersion cacheStartVer, @Nullable ExchangeActions exchActions, @Nullable Throwable err) {
    initCacheProxies(cacheStartVer, err);
    if (exchActions == null)
        return;
    if (exchActions.systemCachesStarting() && exchActions.stateChangeRequest() == null) {
        ctx.dataStructures().restoreStructuresState(ctx);
        ctx.service().updateUtilityCache();
    }
    if (err == null) {
        // Force checkpoint if there is any cache stop request
        if (exchActions.cacheStopRequests().size() > 0) {
            try {
                sharedCtx.database().waitForCheckpoint("caches stop");
            } catch (IgniteCheckedException e) {
                U.error(log, "Failed to wait for checkpoint finish during cache stop.", e);
            }
        }
        for (ExchangeActions.CacheActionData action : exchActions.cacheStopRequests()) {
            CacheGroupContext gctx = cacheGrps.get(action.descriptor().groupId());
            // Cancel all operations blocking gateway
            if (gctx != null) {
                final String msg = "Failed to wait for topology update, cache group is stopping.";
                // If snapshot operation in progress we must throw CacheStoppedException
                // for correct cache proxy restart. For more details see
                // IgniteCacheProxy.cacheException()
                gctx.affinity().cancelFutures(new CacheStoppedException(msg));
            }
            stopGateway(action.request());
            sharedCtx.database().checkpointReadLock();
            try {
                prepareCacheStop(action.request().cacheName(), action.request().destroy());
            } finally {
                sharedCtx.database().checkpointReadUnlock();
            }
        }
        sharedCtx.database().checkpointReadLock();
        try {
            // Do not invoke checkpoint listeners for groups are going to be destroyed to prevent metadata corruption.
            for (ExchangeActions.CacheGroupActionData action : exchActions.cacheGroupsToStop()) {
                Integer groupId = action.descriptor().groupId();
                CacheGroupContext grp = cacheGrps.get(groupId);
                if (grp != null && grp.persistenceEnabled() && sharedCtx.database() instanceof GridCacheDatabaseSharedManager) {
                    GridCacheDatabaseSharedManager mngr = (GridCacheDatabaseSharedManager) sharedCtx.database();
                    mngr.removeCheckpointListener((DbCheckpointListener) grp.offheap());
                }
            }
        } finally {
            sharedCtx.database().checkpointReadUnlock();
        }
        List<IgniteBiTuple<CacheGroupContext, Boolean>> stoppedGroups = new ArrayList<>();
        for (ExchangeActions.CacheGroupActionData action : exchActions.cacheGroupsToStop()) {
            Integer groupId = action.descriptor().groupId();
            if (cacheGrps.containsKey(groupId)) {
                stoppedGroups.add(F.t(cacheGrps.get(groupId), action.destroy()));
                stopCacheGroup(groupId);
            }
        }
        if (!sharedCtx.kernalContext().clientNode())
            sharedCtx.database().onCacheGroupsStopped(stoppedGroups);
        if (exchActions.deactivate())
            sharedCtx.deactivate();
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ArrayList(java.util.ArrayList)

Example 42 with GridCacheDatabaseSharedManager

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

the class GridDhtPartitionTopologyImpl method initPartitions0.

/**
 * @param affVer Affinity version to use.
 * @param exchFut Exchange future.
 * @param updateSeq Update sequence.
 * @return {@code True} if partitions must be refreshed.
 */
private boolean initPartitions0(AffinityTopologyVersion affVer, GridDhtPartitionsExchangeFuture exchFut, long updateSeq) {
    List<List<ClusterNode>> aff = grp.affinity().readyAssignments(affVer);
    boolean needRefresh = false;
    if (grp.affinityNode()) {
        ClusterNode loc = ctx.localNode();
        ClusterNode oldest = discoCache.oldestAliveServerNode();
        GridDhtPartitionExchangeId exchId = exchFut.exchangeId();
        assert grp.affinity().lastVersion().equals(affVer) : "Invalid affinity [topVer=" + grp.affinity().lastVersion() + ", grp=" + grp.cacheOrGroupName() + ", affVer=" + affVer + ", fut=" + exchFut + ']';
        int num = grp.affinity().partitions();
        if (grp.rebalanceEnabled()) {
            boolean added = exchFut.cacheGroupAddedOnExchange(grp.groupId(), grp.receivedFrom());
            boolean first = added || (loc.equals(oldest) && loc.id().equals(exchId.nodeId()) && exchId.isJoined());
            if (first) {
                assert exchId.isJoined() || added;
                for (int p = 0; p < num; p++) {
                    if (localNode(p, aff) || initLocalPartition(p, discoCache)) {
                        GridDhtLocalPartition locPart = createPartition(p);
                        if (grp.persistenceEnabled()) {
                            GridCacheDatabaseSharedManager db = (GridCacheDatabaseSharedManager) grp.shared().database();
                            locPart.restoreState(db.readPartitionState(grp, locPart.id()));
                        } else {
                            boolean owned = locPart.own();
                            assert owned : "Failed to own partition for oldest node [grp=" + grp.cacheOrGroupName() + ", part=" + locPart + ']';
                            if (log.isDebugEnabled())
                                log.debug("Owned partition for oldest node [grp=" + grp.cacheOrGroupName() + ", part=" + locPart + ']');
                        }
                        needRefresh = true;
                        updateSeq = updateLocal(p, locPart.state(), updateSeq, affVer);
                    }
                }
            } else
                createPartitions(affVer, aff, updateSeq);
        } else {
            // the partitions this node is not responsible for.
            for (int p = 0; p < num; p++) {
                GridDhtLocalPartition locPart = localPartition0(p, affVer, false, true, false);
                boolean belongs = localNode(p, aff);
                if (locPart != null) {
                    if (!belongs) {
                        GridDhtPartitionState state = locPart.state();
                        if (state.active()) {
                            locPart.rent(false);
                            updateSeq = updateLocal(p, locPart.state(), updateSeq, affVer);
                            if (log.isDebugEnabled()) {
                                log.debug("Evicting partition with rebalancing disabled (it does not belong to " + "affinity) [grp=" + grp.cacheOrGroupName() + ", part=" + locPart + ']');
                            }
                        }
                    } else
                        locPart.own();
                } else if (belongs) {
                    locPart = createPartition(p);
                    locPart.own();
                    updateLocal(p, locPart.state(), updateSeq, affVer);
                }
            }
        }
    }
    updateRebalanceVersion(aff);
    return needRefresh;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) List(java.util.List) ArrayList(java.util.ArrayList) GridDhtPartitionExchangeId(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionExchangeId)

Example 43 with GridCacheDatabaseSharedManager

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

the class IgnitePdsCheckpointSimulationWithRealCpDisabledTest method testDataWalEntries.

/**
 * @throws Exception if failed.
 */
public void testDataWalEntries() throws Exception {
    IgniteEx ig = startGrid(0);
    ig.active(true);
    GridCacheSharedContext<Object, Object> sharedCtx = ig.context().cache().context();
    GridCacheContext<Object, Object> cctx = sharedCtx.cache().cache(cacheName).context();
    GridCacheDatabaseSharedManager db = (GridCacheDatabaseSharedManager) sharedCtx.database();
    IgniteWriteAheadLogManager wal = sharedCtx.wal();
    assertTrue(wal.isAlwaysWriteFullPages());
    db.enableCheckpoints(false).get();
    final int cnt = 10;
    List<DataEntry> entries = new ArrayList<>(cnt);
    for (int i = 0; i < cnt; i++) {
        GridCacheOperation op = i % 2 == 0 ? GridCacheOperation.UPDATE : GridCacheOperation.DELETE;
        KeyCacheObject key = cctx.toCacheKeyObject(i);
        CacheObject val = null;
        if (op != GridCacheOperation.DELETE)
            val = cctx.toCacheObject("value-" + i);
        entries.add(new DataEntry(cctx.cacheId(), key, val, op, null, cctx.versions().next(), 0L, cctx.affinity().partition(i), i));
    }
    UUID cpId = UUID.randomUUID();
    WALPointer start = wal.log(new CheckpointRecord(cpId, null));
    wal.fsync(start);
    for (DataEntry entry : entries) wal.log(new DataRecord(entry));
    // Data will not be written to the page store.
    stopAllGrids();
    ig = startGrid(0);
    ig.active(true);
    sharedCtx = ig.context().cache().context();
    cctx = sharedCtx.cache().cache(cacheName).context();
    db = (GridCacheDatabaseSharedManager) sharedCtx.database();
    wal = sharedCtx.wal();
    db.enableCheckpoints(false).get();
    try (PartitionMetaStateRecordExcludeIterator it = new PartitionMetaStateRecordExcludeIterator(wal.replay(start))) {
        IgniteBiTuple<WALPointer, WALRecord> cpRecordTup = it.next();
        assert cpRecordTup.get2() instanceof CheckpointRecord;
        assertEquals(start, cpRecordTup.get1());
        CheckpointRecord cpRec = (CheckpointRecord) cpRecordTup.get2();
        assertEquals(cpId, cpRec.checkpointId());
        assertNull(cpRec.checkpointMark());
        assertFalse(cpRec.end());
        int idx = 0;
        CacheObjectContext coctx = cctx.cacheObjectContext();
        while (idx < entries.size()) {
            IgniteBiTuple<WALPointer, WALRecord> dataRecTup = it.next();
            assert dataRecTup.get2() instanceof DataRecord;
            DataRecord dataRec = (DataRecord) dataRecTup.get2();
            DataEntry entry = entries.get(idx);
            assertEquals(1, dataRec.writeEntries().size());
            DataEntry readEntry = dataRec.writeEntries().get(0);
            assertEquals(entry.cacheId(), readEntry.cacheId());
            assertEquals(entry.key().<Integer>value(coctx, true), readEntry.key().<Integer>value(coctx, true));
            assertEquals(entry.op(), readEntry.op());
            if (entry.op() == GridCacheOperation.UPDATE)
                assertEquals(entry.value().value(coctx, true), readEntry.value().value(coctx, true));
            else
                assertNull(entry.value());
            assertEquals(entry.writeVersion(), readEntry.writeVersion());
            assertEquals(entry.nearXidVersion(), readEntry.nearXidVersion());
            assertEquals(entry.partitionCounter(), readEntry.partitionCounter());
            idx++;
        }
    }
}
Also used : WALRecord(org.apache.ignite.internal.pagemem.wal.record.WALRecord) IgniteWriteAheadLogManager(org.apache.ignite.internal.pagemem.wal.IgniteWriteAheadLogManager) GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) ArrayList(java.util.ArrayList) CheckpointRecord(org.apache.ignite.internal.pagemem.wal.record.CheckpointRecord) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) DataEntry(org.apache.ignite.internal.pagemem.wal.record.DataEntry) IgniteEx(org.apache.ignite.internal.IgniteEx) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) GridCacheOperation(org.apache.ignite.internal.processors.cache.GridCacheOperation) UUID(java.util.UUID) WALPointer(org.apache.ignite.internal.pagemem.wal.WALPointer) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 44 with GridCacheDatabaseSharedManager

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

the class WalRecoveryTxLogicalRecordsTest method testCheckpointHistory.

/**
 * @throws Exception if failed.
 */
public void testCheckpointHistory() throws Exception {
    Ignite ignite = startGrid();
    ignite.cluster().active(true);
    try {
        GridCacheDatabaseSharedManager dbMgr = (GridCacheDatabaseSharedManager) ((IgniteEx) ignite).context().cache().context().database();
        dbMgr.waitForCheckpoint("test");
        // This number depends on wal history size.
        int entries = WAL_HIST_SIZE * 2;
        IgniteCache<Integer, Integer> cache = ignite.cache(CACHE_NAME);
        for (int i = 0; i < entries; i++) {
            // Put to partition 0.
            cache.put(i * PARTS, i * PARTS);
            // Put to partition 1.
            cache.put(i * PARTS + 1, i * PARTS + 1);
            dbMgr.waitForCheckpoint("test");
        }
        GridCacheDatabaseSharedManager.CheckpointHistory hist = dbMgr.checkpointHistory();
        assertTrue(hist.checkpoints().size() <= WAL_HIST_SIZE);
        File cpDir = dbMgr.checkpointDirectory();
        File[] cpFiles = cpDir.listFiles();
        // starts & ends + node_start
        assertTrue(cpFiles.length <= WAL_HIST_SIZE * 2 + 1);
    } finally {
        stopAllGrids();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) File(java.io.File)

Example 45 with GridCacheDatabaseSharedManager

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

the class DiskPageCompressionIntegrationTest method doTestPageCompression.

/**
 * @throws Exception If failed.
 */
@Override
protected void doTestPageCompression() throws Exception {
    IgniteEx ignite = startGrid(0);
    ignite.cluster().active(true);
    String cacheName = "test";
    CacheConfiguration<Integer, TestVal> ccfg = new CacheConfiguration<Integer, TestVal>().setName(cacheName).setBackups(0).setAtomicityMode(ATOMIC).setIndexedTypes(Integer.class, TestVal.class).setDiskPageCompression(compression).setDiskPageCompressionLevel(compressionLevel);
    IgniteCache<Integer, TestVal> cache = ignite.getOrCreateCache(ccfg);
    int cnt = 2_000;
    for (int i = 0; i < cnt; i++) assertTrue(cache.putIfAbsent(i, new TestVal(i)));
    for (int i = 0; i < cnt; i += 2) assertEquals(new TestVal(i), cache.getAndRemove(i));
    GridCacheDatabaseSharedManager dbMgr = ((GridCacheDatabaseSharedManager) ignite.context().cache().context().database());
    dbMgr.forceCheckpoint("test compression").futureFor(FINISHED).get();
    FilePageStoreManager storeMgr = dbMgr.getFileStoreManager();
    checkFileIOFactory(storeMgr.getPageStoreFileIoFactory());
    // Wait for metrics update.
    Thread.sleep(100);
    long storeSize = ignite.dataStorageMetrics().getStorageSize();
    long sparseStoreSize = ignite.dataStorageMetrics().getSparseStorageSize();
    assertTrue("storeSize: " + storeSize, storeSize > 0);
    if (U.isLinux()) {
        assertTrue("sparseSize: " + sparseStoreSize, sparseStoreSize > 0);
        assertTrue(storeSize + " > " + sparseStoreSize, storeSize > sparseStoreSize);
    } else
        assertTrue(sparseStoreSize < 0);
    GridCacheContext<?, ?> cctx = ignite.cachex(cacheName).context();
    int cacheId = cctx.cacheId();
    int groupId = cctx.groupId();
    assertEquals(cacheId, groupId);
    MetricRegistry mreg = ignite.context().metric().registry(metricName(CACHE_GROUP_METRICS_PREFIX, cctx.group().cacheOrGroupName()));
    storeSize = mreg.<LongMetric>findMetric("StorageSize").value();
    sparseStoreSize = mreg.<LongMetric>findMetric("SparseStorageSize").value();
    assertTrue("storeSize: " + storeSize, storeSize > 0);
    if (U.isLinux()) {
        assertTrue("sparseSize: " + sparseStoreSize, sparseStoreSize > 0);
        assertTrue(storeSize + " > " + sparseStoreSize, storeSize > sparseStoreSize);
    } else
        assertTrue(sparseStoreSize < 0);
    int parts = cctx.affinity().partitions();
    for (int i = 0; i < parts; i++) {
        PageStore store = storeMgr.getStore(cacheId, i);
        long realSize = store.size();
        long virtualSize = store.getPageSize() * store.pages();
        long sparseSize = store.getSparseSize();
        assertTrue(virtualSize > 0);
        error("virt: " + virtualSize + ",  real: " + realSize + ",  sparse: " + sparseSize);
        if (!store.exists())
            continue;
        if (virtualSize > sparseSize)
            return;
    }
    fail("No files were compacted.");
}
Also used : GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) MetricRegistry(org.apache.ignite.internal.processors.metric.MetricRegistry) PageStore(org.apache.ignite.internal.pagemem.store.PageStore) FilePageStoreManager(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager) LongMetric(org.apache.ignite.spi.metric.LongMetric) IgniteEx(org.apache.ignite.internal.IgniteEx) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

GridCacheDatabaseSharedManager (org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager)85 IgniteEx (org.apache.ignite.internal.IgniteEx)54 Test (org.junit.Test)48 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)33 Ignite (org.apache.ignite.Ignite)20 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)17 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)15 ArrayList (java.util.ArrayList)13 File (java.io.File)12 DataStorageConfiguration (org.apache.ignite.configuration.DataStorageConfiguration)12 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)12 List (java.util.List)10 IgniteWriteAheadLogManager (org.apache.ignite.internal.pagemem.wal.IgniteWriteAheadLogManager)10 WALPointer (org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 IgniteCache (org.apache.ignite.IgniteCache)9 GridCacheSharedContext (org.apache.ignite.internal.processors.cache.GridCacheSharedContext)9 CheckpointListener (org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointListener)9