sergeyuttsel commented on code in PR #2095:
URL: https://github.com/apache/ignite-3/pull/2095#discussion_r1277501510


##########
modules/distribution-zones/src/main/java/org/apache/ignite/internal/distributionzones/causalitydatanodes/CausalityDataNodesEngine.java:
##########
@@ -0,0 +1,717 @@
+/*
+ * 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.distributionzones.causalitydatanodes;
+
+import static java.lang.Math.max;
+import static java.util.Collections.emptySet;
+import static java.util.stream.Collectors.toSet;
+import static 
org.apache.ignite.internal.distributionzones.DistributionZoneManager.DEFAULT_ZONE_ID;
+import static 
org.apache.ignite.internal.distributionzones.DistributionZoneManager.IMMEDIATE_TIMER_VALUE;
+import static 
org.apache.ignite.internal.distributionzones.DistributionZonesUtil.filterDataNodes;
+import static 
org.apache.ignite.internal.distributionzones.DistributionZonesUtil.zoneDataNodesKey;
+import static 
org.apache.ignite.internal.distributionzones.DistributionZonesUtil.zoneScaleDownChangeTriggerKey;
+import static 
org.apache.ignite.internal.distributionzones.DistributionZonesUtil.zoneScaleUpChangeTriggerKey;
+import static 
org.apache.ignite.internal.distributionzones.DistributionZonesUtil.zoneVersionedConfigurationKey;
+import static 
org.apache.ignite.internal.distributionzones.DistributionZonesUtil.zonesLogicalTopologyKey;
+import static org.apache.ignite.internal.util.ByteUtils.bytesToLong;
+import static org.apache.ignite.internal.util.ByteUtils.fromBytes;
+import static org.apache.ignite.internal.util.ByteUtils.toBytes;
+import static org.apache.ignite.lang.ErrorGroups.Common.NODE_STOPPING_ERR;
+
+import java.io.Serializable;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentSkipListMap;
+import java.util.function.Consumer;
+import org.apache.ignite.internal.distributionzones.DistributionZoneManager;
+import 
org.apache.ignite.internal.distributionzones.DistributionZoneManager.Augmentation;
+import 
org.apache.ignite.internal.distributionzones.DistributionZoneManager.ZoneState;
+import org.apache.ignite.internal.distributionzones.DistributionZonesUtil;
+import org.apache.ignite.internal.distributionzones.Node;
+import org.apache.ignite.internal.distributionzones.NodeWithAttributes;
+import 
org.apache.ignite.internal.distributionzones.configuration.DistributionZoneView;
+import org.apache.ignite.internal.metastorage.Entry;
+import org.apache.ignite.internal.metastorage.MetaStorageManager;
+import org.apache.ignite.internal.util.IgniteSpinBusyLock;
+import org.apache.ignite.internal.vault.VaultEntry;
+import org.apache.ignite.internal.vault.VaultManager;
+import org.apache.ignite.lang.ByteArray;
+import org.apache.ignite.lang.DistributionZoneNotFoundException;
+import org.apache.ignite.lang.IgniteException;
+import org.apache.ignite.lang.NodeStoppingException;
+
+/**
+ * Causality data nodes manager.
+ */
+public class CausalityDataNodesEngine {
+    /** Busy lock to stop synchronously. */
+    private final IgniteSpinBusyLock busyLock;
+
+    /** Meta Storage manager. */
+    private final MetaStorageManager msManager;
+
+    /** Vault manager. */
+    private final VaultManager vaultMgr;
+
+    /** Distribution zones manager. */
+    private final DistributionZoneManager distributionZoneManager;
+
+    /**
+     * Map with states for distribution zones. States are needed to track 
nodes that we want to add or remove from the data nodes,
+     * schedule and stop scale up and scale down processes.
+     */
+    private final Map<Integer, ZoneState> zonesState;
+
+    /**
+     * The map which contains configuration changes which trigger zone's data 
nodes recalculation.
+     * zoneId -> (revision -> zoneConfiguration).
+     * TODO IGNITE-20050 Clean up this map.
+     */
+    private final ConcurrentHashMap<Integer, ConcurrentSkipListMap<Long, 
ZoneConfiguration>> zonesVersionedCfg;
+
+    /**
+     * The constructor.
+     *
+     * @param busyLock Busy lock to stop synchronously.
+     * @param msManager Meta Storage manager.
+     * @param vaultMgr Vault manager.
+     * @param zonesState Map with states for distribution zones.
+     * @param distributionZoneManager Distribution zones manager.
+     */
+    public CausalityDataNodesEngine(
+            IgniteSpinBusyLock busyLock,
+            MetaStorageManager msManager,
+            VaultManager vaultMgr,
+            Map<Integer, ZoneState> zonesState,
+            DistributionZoneManager distributionZoneManager
+    ) {
+        this.busyLock = busyLock;
+        this.msManager = msManager;
+        this.vaultMgr = vaultMgr;
+        this.zonesState = zonesState;
+        this.distributionZoneManager = distributionZoneManager;
+
+        zonesVersionedCfg = new ConcurrentHashMap<>();
+    }
+
+    /**
+     * Gets data nodes of the zone using causality token.
+     *
+     * <p>Return data nodes or throw the exception:
+     * {@link IllegalArgumentException} if causalityToken or zoneId is not 
valid.
+     * {@link DistributionZoneNotFoundException} if the zone with the provided 
zoneId does not exist.
+     *
+     * @param causalityToken Causality token.
+     * @param zoneId Zone id.
+     * @return The data nodes for the zoneId.
+     */
+    public Set<String> dataNodes(long causalityToken, int zoneId) {
+        if (causalityToken < 1) {
+            throw new IllegalArgumentException("causalityToken must be greater 
then zero [causalityToken=" + causalityToken + '"');
+        }
+
+        if (zoneId < DEFAULT_ZONE_ID) {
+            throw new IllegalArgumentException("zoneId cannot be a negative 
number [zoneId=" + zoneId + '"');
+        }
+
+        if (!busyLock.enterBusy()) {
+            throw new IgniteException(NODE_STOPPING_ERR, new 
NodeStoppingException());
+        }
+
+        try {
+            ConcurrentSkipListMap<Long, ZoneConfiguration> versionedCfg = 
zonesVersionedCfg.get(zoneId);
+
+            // Get the latest configuration and configuration revision for a 
given causality token
+            Map.Entry<Long, ZoneConfiguration> zoneLastCfgEntry = 
versionedCfg.floorEntry(causalityToken);
+
+            if (zoneLastCfgEntry == null) {
+                // It means that the zone does not exist on a given causality 
token.
+                throw new DistributionZoneNotFoundException(zoneId);
+            }
+
+            long lastCfgRevision = zoneLastCfgEntry.getKey();
+
+            ZoneConfiguration zoneLastCfg = zoneLastCfgEntry.getValue();
+
+            String filter = zoneLastCfg.getFilter();
+
+            boolean isZoneRemoved = zoneLastCfg.getIsRemoved();
+
+            if (isZoneRemoved) {
+                // It means that the zone was removed on a given causality 
token.
+                throw new DistributionZoneNotFoundException(zoneId);
+            }
+
+            // Get revisions of the last scale up and scale down event which 
triggered immediate data nodes recalculation.
+            long lastScaleUpRevision = 
getRevisionsOfLastScaleUpEvent(causalityToken, zoneId);
+            long lastScaleDownRevision = 
getRevisionsOfLastScaleDownEvent(causalityToken, zoneId);
+
+            if (lastCfgRevision == versionedCfg.firstKey()
+                    && lastCfgRevision >= lastScaleUpRevision
+                    && lastCfgRevision >= lastScaleDownRevision
+            ) {
+                // It means that the zone was created but the data nodes value 
had not updated yet.
+                // So the data nodes value will be equals to the logical 
topology on the lastCfgRevision.
+
+                Entry topologyEntry = 
msManager.getLocally(zonesLogicalTopologyKey(), zoneLastCfgEntry.getKey());
+
+                if (topologyEntry.value() == null) {

Review Comment:
   I agree that topologyEntry must be not null here. I added assert.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to