alex-plekhanov commented on code in PR #11758:
URL: https://github.com/apache/ignite/pull/11758#discussion_r1898327564


##########
modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/topology/PartitionReservationManager.java:
##########
@@ -0,0 +1,441 @@
+/*
+ * 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.topology;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.failure.FailureContext;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.managers.communication.GridIoPolicy;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cache.CacheInvalidStateException;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import 
org.apache.ignite.internal.processors.cache.distributed.dht.GridReservable;
+import 
org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture;
+import 
org.apache.ignite.internal.processors.cache.distributed.dht.preloader.PartitionsExchangeAware;
+import org.apache.ignite.internal.processors.tracing.MTC;
+import org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings;
+import org.apache.ignite.internal.util.lang.GridPlainRunnable;
+import org.apache.ignite.internal.util.typedef.CI1;
+import org.apache.ignite.internal.util.typedef.F;
+import org.jetbrains.annotations.Nullable;
+
+import static org.apache.ignite.failure.FailureType.CRITICAL_ERROR;
+import static 
org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion.NONE;
+import static 
org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState.LOST;
+import static 
org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState.OWNING;
+import static 
org.apache.ignite.internal.processors.tracing.SpanType.SQL_PARTITIONS_RESERVE;
+
+/**
+ * Class responsible for partition reservation for queries executed on local 
node. Prevents partitions from being
+ * evicted from node during query execution.
+ */
+public class PartitionReservationManager implements PartitionsExchangeAware {
+    /** Special instance of reservable object for REPLICATED caches. */
+    private static final ReplicatedReservable REPLICATED_RESERVABLE = new 
ReplicatedReservable();
+
+    /** Kernal context. */
+    private final GridKernalContext ctx;
+
+    /**
+     * Group reservations cache. When affinity version is not changed and all 
primary partitions must be reserved we get
+     * group reservation from this map instead of create new reservation group.
+     */
+    private final ConcurrentMap<PartitionReservationKey, GridReservable> 
reservations = new ConcurrentHashMap<>();
+
+    /** Logger. */
+    private final IgniteLogger log;
+
+    /**
+     * Constructor.
+     *
+     * @param ctx Context.
+     */
+    public PartitionReservationManager(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(PartitionReservationManager.class);
+
+        ctx.cache().context().exchange().registerExchangeAwareComponent(this);
+    }
+
+    /**
+     * @param top Partition topology.
+     * @param p Partition ID.
+     * @return Partition.
+     */
+    private static GridDhtLocalPartition partition(GridDhtPartitionTopology 
top, int p) {
+        return top.localPartition(p, NONE, false);
+    }
+
+    /**
+     * @param cacheIds Cache IDs.
+     * @param reqTopVer Topology version from request.
+     * @param explicitParts Explicit partitions list.
+     * @param nodeId Node ID.
+     * @param reqId Request ID.
+     * @return PartitionReservation instance with reservation result.
+     * @throws IgniteCheckedException If failed.
+     */
+    public PartitionReservation reservePartitions(
+        @Nullable List<Integer> cacheIds,
+        AffinityTopologyVersion reqTopVer,
+        final int[] explicitParts,
+        UUID nodeId,
+        long reqId
+    ) throws IgniteCheckedException {
+        try (TraceSurroundings ignored = 
MTC.support(ctx.tracing().create(SQL_PARTITIONS_RESERVE, MTC.span()))) {
+            assert reqTopVer != null;
+
+            AffinityTopologyVersion topVer = 
ctx.cache().context().exchange().lastAffinityChangedTopologyVersion(reqTopVer);
+
+            if (F.isEmpty(cacheIds))
+                return new PartitionReservation(Collections.emptyList());
+
+            Collection<Integer> partIds = partsToCollection(explicitParts);
+
+            List<GridReservable> reserved = new ArrayList<>();
+
+            for (int i = 0; i < cacheIds.size(); i++) {
+                GridCacheContext<?, ?> cctx = 
ctx.cache().context().cacheContext(cacheIds.get(i));
+
+                // Cache was not found, probably was not deployed yet.
+                if (cctx == null) {
+                    return new PartitionReservation(reserved,
+                        String.format("Failed to reserve partitions for query 
(cache is not " +
+                                "found on local node) [localNodeId=%s, 
rmtNodeId=%s, reqId=%s, affTopVer=%s, cacheId=%s]",
+                            ctx.localNodeId(), nodeId, reqId, topVer, 
cacheIds.get(i)));
+                }
+
+                if (!cctx.rebalanceEnabled())
+                    continue;
+
+                String err = reservePartitions(reserved, cctx, partIds, 
topVer, nodeId, "reqId=" + reqId);
+
+                if (err != null)
+                    return new PartitionReservation(reserved, err);
+            }
+
+            return new PartitionReservation(reserved);
+        }
+    }
+
+    /**
+     * @param cctx Cache context.
+     * @param reqTopVer Topology version from request.
+     * @param explicitParts Explicit partitions list.
+     * @param nodeId Node ID.
+     * @param qryInfo Query info.
+     * @return PartitionReservation instance with reservation result.
+     * @throws IgniteCheckedException If failed.
+     */
+    public PartitionReservation reservePartitions(
+        GridCacheContext<?, ?> cctx,
+        AffinityTopologyVersion reqTopVer,
+        final int[] explicitParts,

Review Comment:
   See comment about IDEA warning



##########
modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/topology/PartitionReservationManager.java:
##########
@@ -0,0 +1,441 @@
+/*
+ * 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.topology;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.failure.FailureContext;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.managers.communication.GridIoPolicy;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cache.CacheInvalidStateException;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import 
org.apache.ignite.internal.processors.cache.distributed.dht.GridReservable;
+import 
org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture;
+import 
org.apache.ignite.internal.processors.cache.distributed.dht.preloader.PartitionsExchangeAware;
+import org.apache.ignite.internal.processors.tracing.MTC;
+import org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings;
+import org.apache.ignite.internal.util.lang.GridPlainRunnable;
+import org.apache.ignite.internal.util.typedef.CI1;
+import org.apache.ignite.internal.util.typedef.F;
+import org.jetbrains.annotations.Nullable;
+
+import static org.apache.ignite.failure.FailureType.CRITICAL_ERROR;
+import static 
org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion.NONE;
+import static 
org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState.LOST;
+import static 
org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState.OWNING;
+import static 
org.apache.ignite.internal.processors.tracing.SpanType.SQL_PARTITIONS_RESERVE;
+
+/**
+ * Class responsible for partition reservation for queries executed on local 
node. Prevents partitions from being
+ * evicted from node during query execution.
+ */
+public class PartitionReservationManager implements PartitionsExchangeAware {
+    /** Special instance of reservable object for REPLICATED caches. */
+    private static final ReplicatedReservable REPLICATED_RESERVABLE = new 
ReplicatedReservable();
+
+    /** Kernal context. */
+    private final GridKernalContext ctx;
+
+    /**
+     * Group reservations cache. When affinity version is not changed and all 
primary partitions must be reserved we get
+     * group reservation from this map instead of create new reservation group.
+     */
+    private final ConcurrentMap<PartitionReservationKey, GridReservable> 
reservations = new ConcurrentHashMap<>();
+
+    /** Logger. */
+    private final IgniteLogger log;
+
+    /**
+     * Constructor.
+     *
+     * @param ctx Context.
+     */
+    public PartitionReservationManager(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(PartitionReservationManager.class);
+
+        ctx.cache().context().exchange().registerExchangeAwareComponent(this);
+    }
+
+    /**
+     * @param top Partition topology.
+     * @param p Partition ID.
+     * @return Partition.
+     */
+    private static GridDhtLocalPartition partition(GridDhtPartitionTopology 
top, int p) {
+        return top.localPartition(p, NONE, false);
+    }
+
+    /**
+     * @param cacheIds Cache IDs.
+     * @param reqTopVer Topology version from request.
+     * @param explicitParts Explicit partitions list.
+     * @param nodeId Node ID.
+     * @param reqId Request ID.
+     * @return PartitionReservation instance with reservation result.
+     * @throws IgniteCheckedException If failed.
+     */
+    public PartitionReservation reservePartitions(
+        @Nullable List<Integer> cacheIds,
+        AffinityTopologyVersion reqTopVer,
+        final int[] explicitParts,
+        UUID nodeId,
+        long reqId
+    ) throws IgniteCheckedException {
+        try (TraceSurroundings ignored = 
MTC.support(ctx.tracing().create(SQL_PARTITIONS_RESERVE, MTC.span()))) {
+            assert reqTopVer != null;
+
+            AffinityTopologyVersion topVer = 
ctx.cache().context().exchange().lastAffinityChangedTopologyVersion(reqTopVer);
+
+            if (F.isEmpty(cacheIds))
+                return new PartitionReservation(Collections.emptyList());
+
+            Collection<Integer> partIds = partsToCollection(explicitParts);
+
+            List<GridReservable> reserved = new ArrayList<>();
+
+            for (int i = 0; i < cacheIds.size(); i++) {
+                GridCacheContext<?, ?> cctx = 
ctx.cache().context().cacheContext(cacheIds.get(i));
+
+                // Cache was not found, probably was not deployed yet.
+                if (cctx == null) {
+                    return new PartitionReservation(reserved,
+                        String.format("Failed to reserve partitions for query 
(cache is not " +
+                                "found on local node) [localNodeId=%s, 
rmtNodeId=%s, reqId=%s, affTopVer=%s, cacheId=%s]",
+                            ctx.localNodeId(), nodeId, reqId, topVer, 
cacheIds.get(i)));
+                }
+
+                if (!cctx.rebalanceEnabled())
+                    continue;
+
+                String err = reservePartitions(reserved, cctx, partIds, 
topVer, nodeId, "reqId=" + reqId);
+
+                if (err != null)
+                    return new PartitionReservation(reserved, err);
+            }
+
+            return new PartitionReservation(reserved);
+        }
+    }
+
+    /**
+     * @param cctx Cache context.
+     * @param reqTopVer Topology version from request.
+     * @param explicitParts Explicit partitions list.
+     * @param nodeId Node ID.
+     * @param qryInfo Query info.
+     * @return PartitionReservation instance with reservation result.
+     * @throws IgniteCheckedException If failed.
+     */
+    public PartitionReservation reservePartitions(
+        GridCacheContext<?, ?> cctx,
+        AffinityTopologyVersion reqTopVer,
+        final int[] explicitParts,
+        UUID nodeId,
+        String qryInfo
+    ) throws IgniteCheckedException {
+        try (TraceSurroundings ignored = 
MTC.support(ctx.tracing().create(SQL_PARTITIONS_RESERVE, MTC.span()))) {
+            assert reqTopVer != null;
+
+            AffinityTopologyVersion topVer = 
ctx.cache().context().exchange().lastAffinityChangedTopologyVersion(reqTopVer);
+
+            Collection<Integer> partIds = partsToCollection(explicitParts);
+
+            List<GridReservable> reserved = new ArrayList<>();
+
+            String err = reservePartitions(reserved, cctx, partIds, topVer, 
nodeId, qryInfo);
+
+            return new PartitionReservation(reserved, err);
+        }
+    }
+
+    /**
+     * @return Error message or {@code null}.
+     */
+    private @Nullable String reservePartitions(
+        List<GridReservable> reserved,
+        GridCacheContext<?, ?> cctx,
+        Collection<Integer> partIds,

Review Comment:
   Done



-- 
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.

To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to