Mmuzaf commented on a change in pull request #6951: Ignite 11073 12069 P2P Rebalance collaboration work URL: https://github.com/apache/ignite/pull/6951#discussion_r410701845
########## File path: modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/IgnitePartitionPreloadManager.java ########## @@ -0,0 +1,337 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.processors.cache.distributed.dht.preloader; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; +import org.apache.ignite.IgniteSystemProperties; +import org.apache.ignite.cache.CacheRebalanceMode; +import org.apache.ignite.cluster.BaselineNode; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.internal.IgniteFeatures; +import org.apache.ignite.internal.IgniteInternalFuture; +import org.apache.ignite.internal.processors.affinity.AffinityAssignment; +import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion; +import org.apache.ignite.internal.processors.cache.CacheGroupContext; +import org.apache.ignite.internal.processors.cache.GridCacheContext; +import org.apache.ignite.internal.processors.cache.GridCacheSharedManagerAdapter; +import org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition; +import org.apache.ignite.internal.util.future.GridFinishedFuture; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.internal.util.typedef.internal.CU; + +import static org.apache.ignite.IgniteSystemProperties.IGNITE_DISABLE_WAL_DURING_REBALANCING; +import static org.apache.ignite.IgniteSystemProperties.IGNITE_FILE_REBALANCE_ENABLED; +import static org.apache.ignite.IgniteSystemProperties.IGNITE_FILE_REBALANCE_THRESHOLD; +import static org.apache.ignite.cache.CacheAtomicityMode.ATOMIC; +import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_PDS_WAL_REBALANCE_THRESHOLD; +import static org.apache.ignite.internal.processors.cache.GridCacheUtils.UTILITY_CACHE_NAME; +import static org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState.MOVING; + +/** + * DHT cache partition files preloader. + */ +public class IgnitePartitionPreloadManager extends GridCacheSharedManagerAdapter { + /** */ + private final boolean fileRebalanceEnabled = + IgniteSystemProperties.getBoolean(IGNITE_FILE_REBALANCE_ENABLED, true); + + /** */ + private final long fileRebalanceThreshold = + IgniteSystemProperties.getLong(IGNITE_FILE_REBALANCE_THRESHOLD, DFLT_PDS_WAL_REBALANCE_THRESHOLD); + + /** Lock. */ + private final Lock lock = new ReentrantLock(); + + /** Partition File rebalancing routine. */ + private volatile PartitionPreloadingRoutine partPreloadingRoutine; + + /** {@inheritDoc} */ + @Override protected void stop0(boolean cancel) { + lock.lock(); + + try { + if (partPreloadingRoutine != null) + partPreloadingRoutine.onDone(false); + } + finally { + lock.unlock(); + } + } + + /** + * @param exchId Exchange ID. + * @param exchFut Completed exchange future. Can be {@code null} if forced or reassigned generation occurs. + * @param rebalanceId Rebalance id. + * @param forcePreload {@code True} if preload requested by {@link ForceRebalanceExchangeTask}. + * @return Cache group identifiers with future assignments that will be ready when partitions are preloaded. + */ + public Map<Integer, IgniteInternalFuture<GridDhtPreloaderAssignments>> generateAssignments( + GridDhtPartitionExchangeId exchId, + GridDhtPartitionsExchangeFuture exchFut, + long rebalanceId, + boolean forcePreload + ) { + Map<UUID, Map<Integer, Set<Integer>>> filePreloadingAssignments = new HashMap<>(); + Map<Integer, IgniteInternalFuture<GridDhtPreloaderAssignments>> futAssigns = new HashMap<>(); + + for (final CacheGroupContext grp : cctx.cache().cacheGroups()) { + long delay = grp.config().getRebalanceDelay(); + + GridDhtPreloaderAssignments assigns = null; + + // Don't delay for dummy reassigns to avoid infinite recursion. + if ((delay == 0 || forcePreload) && !cctx.snapshot().partitionsAreFrozen(grp)) + assigns = grp.preloader().generateAssignments(exchId, exchFut); + + GridDhtLocalPartition anyPart; + + if (F.isEmpty(assigns) || assigns.cancelled() || !supports(grp) || forcePreload || + (anyPart = F.first(grp.topology().currentLocalPartitions())) == null || anyPart.active()) { + futAssigns.put(grp.groupId(), new GridFinishedFuture<>(assigns)); + + continue; + } + + for (Map.Entry<ClusterNode, GridDhtPartitionDemandMessage> e0 : assigns.entrySet()) { + Map<Integer, Set<Integer>> grpAssigns = + filePreloadingAssignments.computeIfAbsent(e0.getKey().id(), v -> new HashMap<>()); + + grpAssigns.put(grp.groupId(), e0.getValue().partitions().fullSet()); + } + } + + if (!filePreloadingAssignments.isEmpty()) + futAssigns.putAll(preloadAsync(exchFut, rebalanceId, filePreloadingAssignments)); + + return futAssigns; + } + + /** + * Initiates new partitions preload process from given {@code assignments}. + * + * @param exchFut Exchange future. + * @param rebalanceId Rebalance id. + * @param assignments A map of cache assignments grouped by node. + * @return Cache group identifiers with future assignments that will be ready when partitions are preloaded. + */ + private Map<Integer, IgniteInternalFuture<GridDhtPreloaderAssignments>> preloadAsync( + GridDhtPartitionsExchangeFuture exchFut, + long rebalanceId, + Map<UUID, Map<Integer, Set<Integer>>> assignments + ) { + lock.lock(); + + try { + if (isStopping()) + return Collections.emptyMap(); + + assert partPreloadingRoutine == null || partPreloadingRoutine.isDone(); + + // Start new rebalance session. + partPreloadingRoutine = new PartitionPreloadingRoutine(exchFut, cctx, rebalanceId, assignments); + + return partPreloadingRoutine.startPartitionsPreloading(); + } + finally { + lock.unlock(); + } + } + + /** + * Callback on exchange done, should be invoked before initialize file page store. + * + * @param resVer Exchange result version. + * @param grp Cache group. + * @param cntrs Partition counters. + * @param globalSizes Global partition sizes. + * @param suppliers Historical suppliers. + */ + public void onExchange( + AffinityTopologyVersion resVer, + CacheGroupContext grp, + CachePartitionFullCountersMap cntrs, + Map<Integer, Long> globalSizes, + IgniteDhtPartitionHistorySuppliersMap suppliers + ) { + assert !cctx.kernalContext().clientNode() : "File preloader should not be created on the client node"; + + PartitionPreloadingRoutine preloadRoutine = partPreloadingRoutine; + + // Abort the current parttition preloading if it is still in progress. + if (preloadRoutine != null && !preloadRoutine.isDone()) + preloadRoutine.cancel(); + + if (!supports(grp)) + return; + + boolean disable = filePreloadingApplicable(resVer, grp, cntrs, globalSizes, suppliers); + + // Should rebuild indexes if the last partition files preloading routine was incomplete. + boolean rebuildIdx = false; + + // At this point, cache updates are queued, and we can safely + // switch partitions to inactive mode and vice versa. + for (GridDhtLocalPartition part : grp.topology().currentLocalPartitions()) { + if (disable) + part.disable(); + else if (part.enable() && cctx.pageStore().exists(grp.groupId(), part.id())) + rebuildIdx = true; + } + + if (rebuildIdx) + cctx.database().rebuildIndexes(grp); + } + + /** + * Check whether file rebalancing is supported for the cache group. + * + * @param grp Cache group. + * @param nodes List of Nodes. + * @return {@code True} if file rebalancing is applicable for specified cache group and all nodes supports it. + */ + public boolean supports(CacheGroupContext grp, Collection<ClusterNode> nodes) { + assert nodes != null && !nodes.isEmpty(); + + if (!supports(grp)) + return false; + + if (!IgniteFeatures.allNodesSupports(nodes, IgniteFeatures.CACHE_PARTITION_FILE_REBALANCE)) + return false; + + Map<Integer, Long> globalSizes = grp.topology().globalPartSizes(); + + if (globalSizes.isEmpty()) + return false; + + for (int p = 0; p < grp.affinity().partitions(); p++) { + Long size = globalSizes.get(p); + + if (size != null && size > fileRebalanceThreshold) + return true; + } + + return false; + } + + /** + * Check whether file rebalancing is supported for the cache group. + * + * @param grp Cache group. + * @return {@code True} if file rebalancing is applicable for specified cache group. + */ + public boolean supports(CacheGroupContext grp) { + if (grp == null || !fileRebalanceEnabled || !grp.persistenceEnabled() || grp.isLocal()) + return false; + + if (!IgniteSystemProperties.getBoolean(IGNITE_DISABLE_WAL_DURING_REBALANCING, true)) + return false; + + if (grp.config().getRebalanceDelay() == -1 || grp.config().getRebalanceMode() != CacheRebalanceMode.ASYNC) + return false; + + // Do not rebalance system cache with files as they are not exists. + assert grp.groupId() != CU.cacheId(UTILITY_CACHE_NAME) : "Should not preload utility cache partitions"; + + for (GridCacheContext ctx : grp.caches()) { + if (ctx.config().getAtomicityMode() == ATOMIC) + return false; + } + + return !grp.mvccEnabled(); + } + + /** + * @param grp Group. + * @return {@code True} If the last rebalance attempt was incomplete for specified cache group. + */ + public boolean incompleteRebalance(CacheGroupContext grp) { Review comment: Please, Use `grpId` as a parameter here. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services