Search in sources :

Example 1 with INDEX_PARTITION

use of org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION in project ignite by apache.

the class LoadAllWarmUpStrategy method loadDataInfo.

/**
 * Calculation of cache groups, partitions and count of pages that can load
 * into data region. Calculation starts and includes an index partition for
 * each group.
 *
 * @param region Data region.
 * @return Loadable groups and partitions.
 * @throws IgniteCheckedException – if faild.
 */
protected Map<CacheGroupContext, List<LoadPartition>> loadDataInfo(DataRegion region) throws IgniteCheckedException {
    // Get cache groups of data region.
    List<CacheGroupContext> regionGrps = grpCtxSup.get().stream().filter(grpCtx -> region.equals(grpCtx.dataRegion())).collect(toList());
    long availableLoadPageCnt = availableLoadPageCount(region);
    // Computing groups, partitions, and pages to load into data region.
    Map<CacheGroupContext, List<LoadPartition>> loadableGrps = new LinkedHashMap<>();
    for (int i = 0; i < regionGrps.size() && availableLoadPageCnt > 0; i++) {
        CacheGroupContext grp = regionGrps.get(i);
        // Index partition in priority.
        List<GridDhtLocalPartition> locParts = grp.topology().localPartitions();
        for (int j = -1; j < locParts.size() && availableLoadPageCnt > 0; j++) {
            int p = j == -1 ? INDEX_PARTITION : locParts.get(j).id();
            long partPageCnt = grp.shared().pageStore().pages(grp.groupId(), p);
            if (partPageCnt > 0) {
                long pageCnt = (availableLoadPageCnt - partPageCnt) >= 0 ? partPageCnt : availableLoadPageCnt;
                availableLoadPageCnt -= pageCnt;
                loadableGrps.computeIfAbsent(grp, grpCtx -> new ArrayList<>()).add(new LoadPartition(p, pageCnt));
            }
        }
    }
    return loadableGrps;
}
Also used : DataRegion(org.apache.ignite.internal.processors.cache.persistence.DataRegion) GridToStringExclude(org.apache.ignite.internal.util.tostring.GridToStringExclude) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteLogger(org.apache.ignite.IgniteLogger) Supplier(java.util.function.Supplier) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) ArrayList(java.util.ArrayList) INDEX_PARTITION(org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION) LinkedHashMap(java.util.LinkedHashMap) LoadAllWarmUpConfiguration(org.apache.ignite.configuration.LoadAllWarmUpConfiguration) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Map(java.util.Map) PageMemoryEx(org.apache.ignite.internal.processors.cache.persistence.pagemem.PageMemoryEx) S(org.apache.ignite.internal.util.typedef.internal.S) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) GridCacheProcessor(org.apache.ignite.internal.processors.cache.GridCacheProcessor) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with INDEX_PARTITION

use of org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION in project ignite by apache.

the class SnapshotRestoreProcess method preload.

/**
 * @param reqId Request id.
 * @return Future which will be completed when the preload ends.
 */
private IgniteInternalFuture<Boolean> preload(UUID reqId) {
    if (ctx.clientNode())
        return new GridFinishedFuture<>();
    SnapshotRestoreContext opCtx0 = opCtx;
    GridFutureAdapter<Boolean> retFut = new GridFutureAdapter<>();
    if (opCtx0 == null)
        return new GridFinishedFuture<>(new IgniteCheckedException("Snapshot restore process has incorrect restore state: " + reqId));
    if (opCtx0.dirs.isEmpty())
        return new GridFinishedFuture<>();
    try {
        if (ctx.isStopping())
            throw new NodeStoppingException("Node is stopping: " + ctx.localNodeId());
        Set<SnapshotMetadata> allMetas = opCtx0.metasPerNode.values().stream().flatMap(List::stream).collect(Collectors.toSet());
        AbstractSnapshotVerificationTask.checkMissedMetadata(allMetas);
        IgniteSnapshotManager snpMgr = ctx.cache().context().snapshotMgr();
        synchronized (this) {
            opCtx0.stopFut = new IgniteFutureImpl<>(retFut.chain(f -> null));
        }
        if (log.isInfoEnabled()) {
            log.info("Starting snapshot preload operation to restore cache groups " + "[reqId=" + reqId + ", snapshot=" + opCtx0.snpName + ", caches=" + F.transform(opCtx0.dirs, FilePageStoreManager::cacheGroupName) + ']');
        }
        CompletableFuture<Void> metaFut = ctx.localNodeId().equals(opCtx0.opNodeId) ? CompletableFuture.runAsync(() -> {
            try {
                SnapshotMetadata meta = F.first(opCtx0.metasPerNode.get(opCtx0.opNodeId));
                File binDir = binaryWorkDir(snpMgr.snapshotLocalDir(opCtx0.snpName).getAbsolutePath(), meta.folderName());
                ctx.cacheObjects().updateMetadata(binDir, opCtx0.stopChecker);
            } catch (Throwable t) {
                log.error("Unable to perform metadata update operation for the cache groups restore process", t);
                opCtx0.errHnd.accept(t);
            }
        }, snpMgr.snapshotExecutorService()) : CompletableFuture.completedFuture(null);
        Map<String, GridAffinityAssignmentCache> affCache = new HashMap<>();
        for (StoredCacheData data : opCtx0.cfgs.values()) {
            affCache.computeIfAbsent(CU.cacheOrGroupName(data.config()), grp -> calculateAffinity(ctx, data.config(), opCtx0.discoCache));
        }
        Map<Integer, Set<PartitionRestoreFuture>> allParts = new HashMap<>();
        Map<Integer, Set<PartitionRestoreFuture>> rmtLoadParts = new HashMap<>();
        ClusterNode locNode = ctx.cache().context().localNode();
        List<SnapshotMetadata> locMetas = opCtx0.metasPerNode.get(locNode.id());
        // First preload everything from the local node.
        for (File dir : opCtx0.dirs) {
            String cacheOrGrpName = cacheGroupName(dir);
            int grpId = CU.cacheId(cacheOrGrpName);
            File tmpCacheDir = formatTmpDirName(dir);
            tmpCacheDir.mkdir();
            Set<PartitionRestoreFuture> leftParts;
            // Partitions contained in the snapshot.
            Set<Integer> availParts = new HashSet<>();
            for (SnapshotMetadata meta : allMetas) {
                Set<Integer> parts = meta.partitions().get(grpId);
                if (parts != null)
                    availParts.addAll(parts);
            }
            List<List<ClusterNode>> assignment = affCache.get(cacheOrGrpName).idealAssignment().assignment();
            Set<PartitionRestoreFuture> partFuts = availParts.stream().filter(p -> p != INDEX_PARTITION && assignment.get(p).contains(locNode)).map(p -> new PartitionRestoreFuture(p, opCtx0.processedParts)).collect(Collectors.toSet());
            allParts.put(grpId, partFuts);
            rmtLoadParts.put(grpId, leftParts = new HashSet<>(partFuts));
            if (leftParts.isEmpty())
                continue;
            SnapshotMetadata full = findMetadataWithSamePartitions(locMetas, grpId, leftParts.stream().map(p -> p.partId).collect(Collectors.toSet()));
            for (SnapshotMetadata meta : full == null ? locMetas : Collections.singleton(full)) {
                if (leftParts.isEmpty())
                    break;
                File snpCacheDir = new File(ctx.cache().context().snapshotMgr().snapshotLocalDir(opCtx0.snpName), Paths.get(databaseRelativePath(meta.folderName()), dir.getName()).toString());
                leftParts.removeIf(partFut -> {
                    boolean doCopy = ofNullable(meta.partitions().get(grpId)).orElse(Collections.emptySet()).contains(partFut.partId);
                    if (doCopy) {
                        copyLocalAsync(ctx.cache().context().snapshotMgr(), opCtx0, snpCacheDir, tmpCacheDir, partFut);
                    }
                    return doCopy;
                });
                if (meta == full) {
                    assert leftParts.isEmpty() : leftParts;
                    if (log.isInfoEnabled()) {
                        log.info("The snapshot was taken on the same cluster topology. The index will be copied to " + "restoring cache group if necessary [reqId=" + reqId + ", snapshot=" + opCtx0.snpName + ", dir=" + dir.getName() + ']');
                    }
                    File idxFile = new File(snpCacheDir, FilePageStoreManager.getPartitionFileName(INDEX_PARTITION));
                    if (idxFile.exists()) {
                        PartitionRestoreFuture idxFut;
                        allParts.computeIfAbsent(grpId, g -> new HashSet<>()).add(idxFut = new PartitionRestoreFuture(INDEX_PARTITION, opCtx0.processedParts));
                        copyLocalAsync(ctx.cache().context().snapshotMgr(), opCtx0, snpCacheDir, tmpCacheDir, idxFut);
                    }
                }
            }
        }
        // Load other partitions from remote nodes.
        List<PartitionRestoreFuture> rmtAwaitParts = rmtLoadParts.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
        // This is necessary for sending only one partitions request per each cluster node.
        Map<UUID, Map<Integer, Set<Integer>>> snpAff = snapshotAffinity(opCtx0.metasPerNode.entrySet().stream().filter(e -> !e.getKey().equals(ctx.localNodeId())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)), (grpId, partId) -> rmtLoadParts.get(grpId) != null && rmtLoadParts.get(grpId).remove(new PartitionRestoreFuture(partId, opCtx0.processedParts)));
        Map<Integer, File> grpToDir = opCtx0.dirs.stream().collect(Collectors.toMap(d -> CU.cacheId(FilePageStoreManager.cacheGroupName(d)), d -> d));
        try {
            if (log.isInfoEnabled() && !snpAff.isEmpty()) {
                log.info("Trying to request partitions from remote nodes " + "[reqId=" + reqId + ", snapshot=" + opCtx0.snpName + ", map=" + snpAff.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> partitionsMapToCompactString(e.getValue()))) + ']');
            }
            for (Map.Entry<UUID, Map<Integer, Set<Integer>>> m : snpAff.entrySet()) {
                ctx.cache().context().snapshotMgr().requestRemoteSnapshotFiles(m.getKey(), opCtx0.snpName, m.getValue(), opCtx0.stopChecker, (snpFile, t) -> {
                    if (opCtx0.stopChecker.getAsBoolean())
                        throw new IgniteInterruptedException("Snapshot remote operation request cancelled.");
                    if (t == null) {
                        int grpId = CU.cacheId(cacheGroupName(snpFile.getParentFile()));
                        int partId = partId(snpFile.getName());
                        PartitionRestoreFuture partFut = F.find(allParts.get(grpId), null, new IgnitePredicate<PartitionRestoreFuture>() {

                            @Override
                            public boolean apply(PartitionRestoreFuture f) {
                                return f.partId == partId;
                            }
                        });
                        assert partFut != null : snpFile.getAbsolutePath();
                        File tmpCacheDir = formatTmpDirName(grpToDir.get(grpId));
                        Path partFile = Paths.get(tmpCacheDir.getAbsolutePath(), snpFile.getName());
                        try {
                            Files.move(snpFile.toPath(), partFile);
                            partFut.complete(partFile);
                        } catch (Exception e) {
                            opCtx0.errHnd.accept(e);
                            completeListExceptionally(rmtAwaitParts, e);
                        }
                    } else {
                        opCtx0.errHnd.accept(t);
                        completeListExceptionally(rmtAwaitParts, t);
                    }
                });
            }
        } catch (IgniteCheckedException e) {
            opCtx0.errHnd.accept(e);
            completeListExceptionally(rmtAwaitParts, e);
        }
        List<PartitionRestoreFuture> allPartFuts = allParts.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
        int size = allPartFuts.size();
        opCtx0.totalParts = size;
        CompletableFuture.allOf(allPartFuts.toArray(new CompletableFuture[size])).runAfterBothAsync(metaFut, () -> {
            try {
                if (opCtx0.stopChecker.getAsBoolean())
                    throw new IgniteInterruptedException("The operation has been stopped on temporary directory switch.");
                for (File src : opCtx0.dirs) Files.move(formatTmpDirName(src).toPath(), src.toPath(), StandardCopyOption.ATOMIC_MOVE);
            } catch (IOException e) {
                throw new IgniteException(e);
            }
        }, snpMgr.snapshotExecutorService()).whenComplete((r, t) -> opCtx0.errHnd.accept(t)).whenComplete((res, t) -> {
            Throwable t0 = ofNullable(opCtx0.err.get()).orElse(t);
            if (t0 == null)
                retFut.onDone(true);
            else {
                log.error("Unable to restore cache group(s) from a snapshot " + "[reqId=" + opCtx.reqId + ", snapshot=" + opCtx.snpName + ']', t0);
                retFut.onDone(t0);
            }
        });
    } catch (Exception ex) {
        opCtx0.errHnd.accept(ex);
        return new GridFinishedFuture<>(ex);
    }
    return retFut;
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) StoredCacheData(org.apache.ignite.internal.processors.cache.StoredCacheData) CacheObjectBinaryProcessorImpl.binaryWorkDir(org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl.binaryWorkDir) RESTORE_CACHE_GROUP_SNAPSHOT_ROLLBACK(org.apache.ignite.internal.util.distributed.DistributedProcess.DistributedProcessType.RESTORE_CACHE_GROUP_SNAPSHOT_ROLLBACK) BooleanSupplier(java.util.function.BooleanSupplier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Path(java.nio.file.Path) IgniteFuture(org.apache.ignite.lang.IgniteFuture) FilePageStoreManager.cacheGroupName(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.cacheGroupName) EventType(org.apache.ignite.events.EventType) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) Set(java.util.Set) FilePageStoreManager(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) FilePageStoreManager.partId(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.partId) Serializable(java.io.Serializable) INDEX_PARTITION(org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) CU(org.apache.ignite.internal.util.typedef.internal.CU) Optional(java.util.Optional) SNAPSHOT_RESTORE_CACHE_GROUP(org.apache.ignite.internal.IgniteFeatures.SNAPSHOT_RESTORE_CACHE_GROUP) IgniteFutureImpl(org.apache.ignite.internal.util.future.IgniteFutureImpl) CACHE_GRP_DIR_PREFIX(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.CACHE_GRP_DIR_PREFIX) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) ClusterSnapshotFuture(org.apache.ignite.internal.processors.cache.persistence.snapshot.IgniteSnapshotManager.ClusterSnapshotFuture) ClusterState(org.apache.ignite.cluster.ClusterState) U(org.apache.ignite.internal.util.typedef.internal.U) IgniteSnapshotManager.databaseRelativePath(org.apache.ignite.internal.processors.cache.persistence.snapshot.IgniteSnapshotManager.databaseRelativePath) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) IgniteLogger(org.apache.ignite.IgniteLogger) IgniteFeatures(org.apache.ignite.internal.IgniteFeatures) DiscoCache(org.apache.ignite.internal.managers.discovery.DiscoCache) AtomicReference(java.util.concurrent.atomic.AtomicReference) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) GridKernalContext(org.apache.ignite.internal.GridKernalContext) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) BiPredicate(java.util.function.BiPredicate) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) IgniteIllegalStateException(org.apache.ignite.IgniteIllegalStateException) S(org.apache.ignite.internal.util.typedef.internal.S) METASTORAGE_CACHE_NAME(org.apache.ignite.internal.processors.cache.persistence.metastorage.MetaStorage.METASTORAGE_CACHE_NAME) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) RESTORE_CACHE_GROUP_SNAPSHOT_PREPARE(org.apache.ignite.internal.util.distributed.DistributedProcess.DistributedProcessType.RESTORE_CACHE_GROUP_SNAPSHOT_PREPARE) RESTORE_CACHE_GROUP_SNAPSHOT_PRELOAD(org.apache.ignite.internal.util.distributed.DistributedProcess.DistributedProcessType.RESTORE_CACHE_GROUP_SNAPSHOT_PRELOAD) DistributedProcess(org.apache.ignite.internal.util.distributed.DistributedProcess) F(org.apache.ignite.internal.util.typedef.F) Files(java.nio.file.Files) Optional.ofNullable(java.util.Optional.ofNullable) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IOException(java.io.IOException) GridAffinityAssignmentCache(org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache) MetricRegistry(org.apache.ignite.internal.processors.metric.MetricRegistry) File(java.io.File) Consumer(java.util.function.Consumer) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) RESTORE_CACHE_GROUP_SNAPSHOT_START(org.apache.ignite.internal.util.distributed.DistributedProcess.DistributedProcessType.RESTORE_CACHE_GROUP_SNAPSHOT_START) DiscoveryDataClusterState(org.apache.ignite.internal.processors.cluster.DiscoveryDataClusterState) Paths(java.nio.file.Paths) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) IgniteFinishedFutureImpl(org.apache.ignite.internal.util.future.IgniteFinishedFutureImpl) Collections(java.util.Collections) IgniteUuid(org.apache.ignite.lang.IgniteUuid) Set(java.util.Set) HashSet(java.util.HashSet) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridAffinityAssignmentCache(org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache) IgniteException(org.apache.ignite.IgniteException) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) List(java.util.List) ArrayList(java.util.ArrayList) UUID(java.util.UUID) HashSet(java.util.HashSet) ClusterNode(org.apache.ignite.cluster.ClusterNode) Path(java.nio.file.Path) IgniteSnapshotManager.databaseRelativePath(org.apache.ignite.internal.processors.cache.persistence.snapshot.IgniteSnapshotManager.databaseRelativePath) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IOException(java.io.IOException) StoredCacheData(org.apache.ignite.internal.processors.cache.StoredCacheData) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IgniteIllegalStateException(org.apache.ignite.IgniteIllegalStateException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with INDEX_PARTITION

use of org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION in project ignite by apache.

the class SnapshotPartitionsVerifyHandler method invoke.

/**
 * {@inheritDoc}
 */
@Override
public Map<PartitionKeyV2, PartitionHashRecordV2> invoke(SnapshotHandlerContext opCtx) throws IgniteCheckedException {
    SnapshotMetadata meta = opCtx.metadata();
    Set<Integer> grps = F.isEmpty(opCtx.groups()) ? new HashSet<>(meta.partitions().keySet()) : opCtx.groups().stream().map(CU::cacheId).collect(Collectors.toSet());
    Set<File> partFiles = new HashSet<>();
    IgniteSnapshotManager snpMgr = cctx.snapshotMgr();
    for (File dir : snpMgr.snapshotCacheDirectories(meta.snapshotName(), meta.folderName())) {
        int grpId = CU.cacheId(cacheGroupName(dir));
        if (!grps.remove(grpId))
            continue;
        Set<Integer> parts = meta.partitions().get(grpId) == null ? Collections.emptySet() : new HashSet<>(meta.partitions().get(grpId));
        for (File part : cachePartitionFiles(dir)) {
            int partId = partId(part.getName());
            if (!parts.remove(partId))
                continue;
            partFiles.add(part);
        }
        if (!parts.isEmpty()) {
            throw new IgniteException("Snapshot data doesn't contain required cache group partition " + "[grpId=" + grpId + ", snpName=" + meta.snapshotName() + ", consId=" + meta.consistentId() + ", missed=" + parts + ", meta=" + meta + ']');
        }
    }
    if (!grps.isEmpty()) {
        throw new IgniteException("Snapshot data doesn't contain required cache groups " + "[grps=" + grps + ", snpName=" + meta.snapshotName() + ", consId=" + meta.consistentId() + ", meta=" + meta + ']');
    }
    Map<PartitionKeyV2, PartitionHashRecordV2> res = new ConcurrentHashMap<>();
    ThreadLocal<ByteBuffer> buff = ThreadLocal.withInitial(() -> ByteBuffer.allocateDirect(meta.pageSize()).order(ByteOrder.nativeOrder()));
    GridKernalContext snpCtx = snpMgr.createStandaloneKernalContext(meta.snapshotName(), meta.folderName());
    for (GridComponent comp : snpCtx) comp.start();
    try {
        U.doInParallel(snpMgr.snapshotExecutorService(), partFiles, part -> {
            String grpName = cacheGroupName(part.getParentFile());
            int grpId = CU.cacheId(grpName);
            int partId = partId(part.getName());
            FilePageStoreManager storeMgr = (FilePageStoreManager) cctx.pageStore();
            try (FilePageStore pageStore = (FilePageStore) storeMgr.getPageStoreFactory(grpId, false).createPageStore(getTypeByPartId(partId), part::toPath, val -> {
            })) {
                if (partId == INDEX_PARTITION) {
                    checkPartitionsPageCrcSum(() -> pageStore, INDEX_PARTITION, FLAG_IDX);
                    return null;
                }
                if (grpId == MetaStorage.METASTORAGE_CACHE_ID) {
                    checkPartitionsPageCrcSum(() -> pageStore, partId, FLAG_DATA);
                    return null;
                }
                ByteBuffer pageBuff = buff.get();
                pageBuff.clear();
                pageStore.read(0, pageBuff, true);
                long pageAddr = GridUnsafe.bufferAddress(pageBuff);
                PagePartitionMetaIO io = PageIO.getPageIO(pageBuff);
                GridDhtPartitionState partState = fromOrdinal(io.getPartitionState(pageAddr));
                if (partState != OWNING) {
                    throw new IgniteCheckedException("Snapshot partitions must be in the OWNING " + "state only: " + partState);
                }
                long updateCntr = io.getUpdateCounter(pageAddr);
                long size = io.getSize(pageAddr);
                if (log.isDebugEnabled()) {
                    log.debug("Partition [grpId=" + grpId + ", id=" + partId + ", counter=" + updateCntr + ", size=" + size + "]");
                }
                // Snapshot partitions must always be in OWNING state.
                // There is no `primary` partitions for snapshot.
                PartitionKeyV2 key = new PartitionKeyV2(grpId, partId, grpName);
                PartitionHashRecordV2 hash = calculatePartitionHash(key, updateCntr, meta.consistentId(), GridDhtPartitionState.OWNING, false, size, snpMgr.partitionRowIterator(snpCtx, grpName, partId, pageStore));
                assert hash != null : "OWNING must have hash: " + key;
                res.put(key, hash);
            } catch (IOException e) {
                throw new IgniteCheckedException(e);
            }
            return null;
        });
    } catch (Throwable t) {
        log.error("Error executing handler: ", t);
        throw t;
    } finally {
        for (GridComponent comp : snpCtx) comp.stop(true);
    }
    return res;
}
Also used : FLAG_DATA(org.apache.ignite.internal.pagemem.PageIdAllocator.FLAG_DATA) PartitionHashRecordV2(org.apache.ignite.internal.processors.cache.verify.PartitionHashRecordV2) IdleVerifyUtility.calculatePartitionHash(org.apache.ignite.internal.processors.cache.verify.IdleVerifyUtility.calculatePartitionHash) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) IgniteLogger(org.apache.ignite.IgniteLogger) ByteBuffer(java.nio.ByteBuffer) IdleVerifyUtility.checkPartitionsPageCrcSum(org.apache.ignite.internal.processors.cache.verify.IdleVerifyUtility.checkPartitionsPageCrcSum) ArrayList(java.util.ArrayList) GridKernalContext(org.apache.ignite.internal.GridKernalContext) HashSet(java.util.HashSet) ClusterNode(org.apache.ignite.cluster.ClusterNode) PagePartitionMetaIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PagePartitionMetaIO) GridDhtPartitionState.fromOrdinal(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState.fromOrdinal) Map(java.util.Map) PageIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO) FilePageStoreManager.cachePartitionFiles(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.cachePartitionFiles) GridDhtPartitionState(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState) FilePageStore(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStore) GridStringBuilder(org.apache.ignite.internal.util.GridStringBuilder) F(org.apache.ignite.internal.util.typedef.F) FilePageStoreManager.cacheGroupName(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.cacheGroupName) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GridComponent(org.apache.ignite.internal.GridComponent) Set(java.util.Set) GridUnsafe(org.apache.ignite.internal.util.GridUnsafe) IOException(java.io.IOException) FilePageStoreManager(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager) MetaStorage(org.apache.ignite.internal.processors.cache.persistence.metastorage.MetaStorage) OWNING(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState.OWNING) Collectors(java.util.stream.Collectors) File(java.io.File) FilePageStoreManager.partId(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.partId) INDEX_PARTITION(org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION) ByteOrder(java.nio.ByteOrder) List(java.util.List) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) PartitionKeyV2(org.apache.ignite.internal.processors.cache.verify.PartitionKeyV2) CU(org.apache.ignite.internal.util.typedef.internal.CU) GroupPartitionId.getTypeByPartId(org.apache.ignite.internal.processors.cache.persistence.partstate.GroupPartitionId.getTypeByPartId) Collections(java.util.Collections) IdleVerifyResultV2(org.apache.ignite.internal.processors.cache.verify.IdleVerifyResultV2) FLAG_IDX(org.apache.ignite.internal.pagemem.PageIdAllocator.FLAG_IDX) GridKernalContext(org.apache.ignite.internal.GridKernalContext) GridComponent(org.apache.ignite.internal.GridComponent) FilePageStoreManager(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager) FilePageStore(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStore) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) PartitionKeyV2(org.apache.ignite.internal.processors.cache.verify.PartitionKeyV2) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashSet(java.util.HashSet) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) CU(org.apache.ignite.internal.util.typedef.internal.CU) PartitionHashRecordV2(org.apache.ignite.internal.processors.cache.verify.PartitionHashRecordV2) GridDhtPartitionState(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState) PagePartitionMetaIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PagePartitionMetaIO) File(java.io.File)

Example 4 with INDEX_PARTITION

use of org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION in project ignite by apache.

the class PageMemoryImplTest method runThrottlingEmptifyCpBufFirst.

/**
 * @throws Exception if failed.
 */
public void runThrottlingEmptifyCpBufFirst(PageMemoryImpl.ThrottlingPolicy plc) throws Exception {
    TestPageStoreManager pageStoreMgr = new TestPageStoreManager();
    final List<FullPageId> allocated = new ArrayList<>();
    int pagesForStartThrottling = 10;
    // Number of pages which were poll from checkpoint buffer for throttling.
    AtomicInteger cpBufferPollPages = new AtomicInteger();
    // Create a 1 mb page memory.
    PageMemoryImpl memory = createPageMemory(1, plc, pageStoreMgr, pageStoreMgr, (IgniteInClosure<FullPageId>) fullPageId -> {
        assertEquals(cpBufferPollPages.incrementAndGet(), pageStoreMgr.storedPages.size());
    });
    assert pagesForStartThrottling < memory.checkpointBufferPagesSize() / 3;
    for (int i = 0; i < pagesForStartThrottling + (memory.checkpointBufferPagesSize() * 2 / 3); i++) {
        long id = memory.allocatePage(1, INDEX_PARTITION, FLAG_IDX);
        FullPageId fullId = new FullPageId(id, 1);
        allocated.add(fullId);
        writePage(memory, fullId, (byte) 1);
    }
    GridMultiCollectionWrapper<FullPageId> markedPages = memory.beginCheckpoint(new GridFinishedFuture());
    for (int i = 0; i < pagesForStartThrottling + (memory.checkpointBufferPagesSize() * 2 / 3); i++) writePage(memory, allocated.get(i), (byte) 1);
    doCheckpoint(markedPages, memory, pageStoreMgr);
    // There is 'pagesForStartThrottling - 1' because we should write pagesForStartThrottling pages
    // from checkpoint buffer before throttling will be disabled but at least one page always would be written
    // outside of throttling and in our case we certainly know that this page is also contained in checkpoint buffer
    // (because all of our pages are in checkpoint buffer).
    assertEquals(pagesForStartThrottling - 1, cpBufferPollPages.get());
}
Also used : PageStore(org.apache.ignite.internal.pagemem.store.PageStore) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) ByteBuffer(java.nio.ByteBuffer) NoopMetricExporterSpi(org.apache.ignite.spi.metric.noop.NoopMetricExporterSpi) GridSystemViewManager(org.apache.ignite.internal.managers.systemview.GridSystemViewManager) UnsafeMemoryProvider(org.apache.ignite.internal.mem.unsafe.UnsafeMemoryProvider) IgnitePluginProcessor(org.apache.ignite.internal.processors.plugin.IgnitePluginProcessor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LongAdderMetric(org.apache.ignite.internal.processors.metric.impl.LongAdderMetric) Map(java.util.Map) PageUtils(org.apache.ignite.internal.pagemem.PageUtils) GridTestLog4jLogger(org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger) FailureProcessor(org.apache.ignite.internal.processors.failure.FailureProcessor) CheckpointProgressImpl(org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointProgressImpl) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) IgniteOutClosure(org.apache.ignite.lang.IgniteOutClosure) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) DirectMemoryProvider(org.apache.ignite.internal.mem.DirectMemoryProvider) GridInternalSubscriptionProcessor(org.apache.ignite.internal.processors.subscription.GridInternalSubscriptionProcessor) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) INDEX_PARTITION(org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION) ByteOrder(java.nio.ByteOrder) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) IgnitePageStoreManager(org.apache.ignite.internal.pagemem.store.IgnitePageStoreManager) PerformanceStatisticsProcessor(org.apache.ignite.internal.processors.performancestatistics.PerformanceStatisticsProcessor) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) IgniteOutOfMemoryException(org.apache.ignite.internal.mem.IgniteOutOfMemoryException) JmxSystemViewExporterSpi(org.apache.ignite.internal.managers.systemview.JmxSystemViewExporterSpi) FLAG_IDX(org.apache.ignite.internal.pagemem.PageIdAllocator.FLAG_IDX) CHECKPOINT_POOL_OVERFLOW_ERROR_MSG(org.apache.ignite.internal.processors.cache.persistence.pagemem.PageMemoryImpl.CHECKPOINT_POOL_OVERFLOW_ERROR_MSG) FullPageId(org.apache.ignite.internal.pagemem.FullPageId) DataRegionMetricsImpl(org.apache.ignite.internal.processors.cache.persistence.DataRegionMetricsImpl) GridMultiCollectionWrapper(org.apache.ignite.internal.util.GridMultiCollectionWrapper) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CheckpointProgress(org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointProgress) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) NoopEventStorageSpi(org.apache.ignite.spi.eventstorage.NoopEventStorageSpi) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) PageIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO) GridEncryptionManager(org.apache.ignite.internal.managers.encryption.GridEncryptionManager) NoOpFailureHandler(org.apache.ignite.failure.NoOpFailureHandler) NoopEncryptionSpi(org.apache.ignite.spi.encryption.noop.NoopEncryptionSpi) Test(org.junit.Test) IgniteCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.IgniteCacheDatabaseSharedManager) GridTestKernalContext(org.apache.ignite.testframework.junits.GridTestKernalContext) Mockito(org.mockito.Mockito) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) PluginProvider(org.apache.ignite.plugin.PluginProvider) DummyPageIO(org.apache.ignite.internal.processors.cache.persistence.DummyPageIO) PageStoreWriter(org.apache.ignite.internal.processors.cache.persistence.PageStoreWriter) GridMetricManager(org.apache.ignite.internal.processors.metric.GridMetricManager) GridInClosure3X(org.apache.ignite.internal.util.lang.GridInClosure3X) GridEventStorageManager(org.apache.ignite.internal.managers.eventstorage.GridEventStorageManager) Collections(java.util.Collections) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) FullPageId(org.apache.ignite.internal.pagemem.FullPageId) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture)

Example 5 with INDEX_PARTITION

use of org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION in project ignite by apache.

the class PageMemoryImplTest method testCheckpointBufferCantOverflowWithThrottlingMixedLoad.

/**
 * @throws Exception If failed.
 */
private void testCheckpointBufferCantOverflowWithThrottlingMixedLoad(PageMemoryImpl.ThrottlingPolicy plc) throws Exception {
    PageMemoryImpl memory = createPageMemory(plc, null);
    List<FullPageId> pages = new ArrayList<>();
    for (int i = 0; i < (MAX_SIZE - 10) * MB / PAGE_SIZE / 2; i++) {
        long pageId = memory.allocatePage(1, INDEX_PARTITION, FLAG_IDX);
        FullPageId fullPageId = new FullPageId(pageId, 1);
        pages.add(fullPageId);
        acquireAndReleaseWriteLock(memory, fullPageId);
    }
    memory.beginCheckpoint(new GridFinishedFuture());
    CheckpointMetricsTracker mockTracker = Mockito.mock(CheckpointMetricsTracker.class);
    for (FullPageId checkpointPage : pages) memory.checkpointWritePage(checkpointPage, ByteBuffer.allocate(PAGE_SIZE), (fullPageId, buffer, tag) -> {
    // No-op.
    }, mockTracker);
    memory.finishCheckpoint();
    for (int i = (int) ((MAX_SIZE - 10) * MB / PAGE_SIZE / 2); i < (MAX_SIZE - 20) * MB / PAGE_SIZE; i++) {
        long pageId = memory.allocatePage(1, INDEX_PARTITION, FLAG_IDX);
        FullPageId fullPageId = new FullPageId(pageId, 1);
        pages.add(fullPageId);
        acquireAndReleaseWriteLock(memory, fullPageId);
    }
    memory.beginCheckpoint(new GridFinishedFuture());
    // Mix pages in checkpoint with clean pages
    Collections.shuffle(pages);
    AtomicBoolean stop = new AtomicBoolean(false);
    try {
        GridTestUtils.runAsync(() -> {
            for (FullPageId page : pages) {
                if (// Mark dirty 50% of pages
                ThreadLocalRandom.current().nextDouble() < 0.5)
                    try {
                        acquireAndReleaseWriteLock(memory, page);
                        if (stop.get())
                            break;
                    } catch (IgniteCheckedException e) {
                        log.error("runAsync ended with exception", e);
                        fail();
                    }
            }
        }).get(5_000);
    } catch (IgniteFutureTimeoutCheckedException ignore) {
    // Expected.
    } finally {
        stop.set(true);
    }
    memory.finishCheckpoint();
    LongAdderMetric totalThrottlingTime = U.field(memory.metrics(), "totalThrottlingTime");
    assertNotNull(totalThrottlingTime);
    assertTrue(totalThrottlingTime.value() > 0);
}
Also used : PageStore(org.apache.ignite.internal.pagemem.store.PageStore) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) ByteBuffer(java.nio.ByteBuffer) NoopMetricExporterSpi(org.apache.ignite.spi.metric.noop.NoopMetricExporterSpi) GridSystemViewManager(org.apache.ignite.internal.managers.systemview.GridSystemViewManager) UnsafeMemoryProvider(org.apache.ignite.internal.mem.unsafe.UnsafeMemoryProvider) IgnitePluginProcessor(org.apache.ignite.internal.processors.plugin.IgnitePluginProcessor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LongAdderMetric(org.apache.ignite.internal.processors.metric.impl.LongAdderMetric) Map(java.util.Map) PageUtils(org.apache.ignite.internal.pagemem.PageUtils) GridTestLog4jLogger(org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger) FailureProcessor(org.apache.ignite.internal.processors.failure.FailureProcessor) CheckpointProgressImpl(org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointProgressImpl) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) IgniteOutClosure(org.apache.ignite.lang.IgniteOutClosure) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) DirectMemoryProvider(org.apache.ignite.internal.mem.DirectMemoryProvider) GridInternalSubscriptionProcessor(org.apache.ignite.internal.processors.subscription.GridInternalSubscriptionProcessor) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) INDEX_PARTITION(org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION) ByteOrder(java.nio.ByteOrder) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) IgnitePageStoreManager(org.apache.ignite.internal.pagemem.store.IgnitePageStoreManager) PerformanceStatisticsProcessor(org.apache.ignite.internal.processors.performancestatistics.PerformanceStatisticsProcessor) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) IgniteOutOfMemoryException(org.apache.ignite.internal.mem.IgniteOutOfMemoryException) JmxSystemViewExporterSpi(org.apache.ignite.internal.managers.systemview.JmxSystemViewExporterSpi) FLAG_IDX(org.apache.ignite.internal.pagemem.PageIdAllocator.FLAG_IDX) CHECKPOINT_POOL_OVERFLOW_ERROR_MSG(org.apache.ignite.internal.processors.cache.persistence.pagemem.PageMemoryImpl.CHECKPOINT_POOL_OVERFLOW_ERROR_MSG) FullPageId(org.apache.ignite.internal.pagemem.FullPageId) DataRegionMetricsImpl(org.apache.ignite.internal.processors.cache.persistence.DataRegionMetricsImpl) GridMultiCollectionWrapper(org.apache.ignite.internal.util.GridMultiCollectionWrapper) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CheckpointProgress(org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointProgress) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) NoopEventStorageSpi(org.apache.ignite.spi.eventstorage.NoopEventStorageSpi) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) PageIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO) GridEncryptionManager(org.apache.ignite.internal.managers.encryption.GridEncryptionManager) NoOpFailureHandler(org.apache.ignite.failure.NoOpFailureHandler) NoopEncryptionSpi(org.apache.ignite.spi.encryption.noop.NoopEncryptionSpi) Test(org.junit.Test) IgniteCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.IgniteCacheDatabaseSharedManager) GridTestKernalContext(org.apache.ignite.testframework.junits.GridTestKernalContext) Mockito(org.mockito.Mockito) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) PluginProvider(org.apache.ignite.plugin.PluginProvider) DummyPageIO(org.apache.ignite.internal.processors.cache.persistence.DummyPageIO) PageStoreWriter(org.apache.ignite.internal.processors.cache.persistence.PageStoreWriter) GridMetricManager(org.apache.ignite.internal.processors.metric.GridMetricManager) GridInClosure3X(org.apache.ignite.internal.util.lang.GridInClosure3X) GridEventStorageManager(org.apache.ignite.internal.managers.eventstorage.GridEventStorageManager) Collections(java.util.Collections) ArrayList(java.util.ArrayList) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) LongAdderMetric(org.apache.ignite.internal.processors.metric.impl.LongAdderMetric) FullPageId(org.apache.ignite.internal.pagemem.FullPageId)

Aggregations

List (java.util.List)7 Map (java.util.Map)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)7 INDEX_PARTITION (org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 Collections (java.util.Collections)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 FLAG_IDX (org.apache.ignite.internal.pagemem.PageIdAllocator.FLAG_IDX)5 PageIO (org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO)5 File (java.io.File)4 ByteBuffer (java.nio.ByteBuffer)4 HashSet (java.util.HashSet)4 Set (java.util.Set)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 IgniteException (org.apache.ignite.IgniteException)4 GridCacheSharedContext (org.apache.ignite.internal.processors.cache.GridCacheSharedContext)4 U (org.apache.ignite.internal.util.typedef.internal.U)4 IOException (java.io.IOException)3 ByteOrder (java.nio.ByteOrder)3