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


##########
modules/distribution-zones/src/main/java/org/apache/ignite/internal/distributionzones/causalitydatanodes/CausalityDataNodesEngine.java:
##########
@@ -0,0 +1,681 @@
+/*
+ * 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 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 
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.distributionzones.rebalance.DistributionZoneRebalanceEngine;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import org.apache.ignite.internal.metastorage.Entry;
+import org.apache.ignite.internal.metastorage.MetaStorageManager;
+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.IgniteBiTuple;
+
+/**
+ * Causality data nodes manager.
+ */
+public class CausalityDataNodesEngine {
+    /** The logger. */
+    private static final IgniteLogger LOG = 
Loggers.forClass(DistributionZoneRebalanceEngine.class);
+
+    /** Meta Storage manager. */
+    private final MetaStorageManager msManager;
+
+    /** Vault manager. */
+    private final VaultManager vaultMgr;
+
+    /**
+     * 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;
+
+    /**
+     * zoneId -> (revision -> zoneConfiguration).
+     */
+    private final ConcurrentHashMap<Integer, ConcurrentSkipListMap<Long, 
ZoneConfiguration>> zonesVersionedCfg;
+
+    /**
+     * Local mapping of {@code nodeId} -> node's attributes, where {@code 
nodeId} is a node id, that changes between restarts.
+     * This map is updated every time we receive a topology event in a {@code 
topologyWatchListener}.
+     * TODO: https://issues.apache.org/jira/browse/IGNITE-19491 properly clean 
up this map
+     *
+     * @see <a 
href="https://github.com/apache/ignite-3/blob/main/modules/distribution-zones/tech-notes/filters.md";>Filter
 documentation</a>
+     */
+    private Map<String, Map<String, String>> nodesAttributes;
+
+    /**
+     * The constructor.
+     *
+     * @param msManager msManager.
+     * @param vaultMgr vaultMgr.
+     * @param zonesState zonesState.
+     * @param nodesAttributes nodesAttributes.
+     */
+    public CausalityDataNodesEngine(
+            MetaStorageManager msManager,
+            VaultManager vaultMgr,
+            Map<Integer, ZoneState> zonesState,
+            Map<String, Map<String, String>> nodesAttributes
+    ) {
+        this.msManager = msManager;
+        this.vaultMgr = vaultMgr;
+        this.zonesState = zonesState;
+        this.nodesAttributes = nodesAttributes;
+
+        zonesVersionedCfg = new ConcurrentHashMap<>();
+    }
+
+    /**
+     * Gets data nodes of the zone using causality token.
+     *
+     * <p>Return data nodes or throw the {@link 
DistributionZoneNotFoundException} if the zone with the provided {@code zoneId}
+     * does not exist.
+     *
+     * @param causalityToken Causality token.
+     * @param zoneId Zone id.
+     * @return The future which will be completed with data nodes for the 
zoneId or with exception.
+     */
+    public Set<String> dataNodes(long causalityToken, int zoneId) {
+        LOG.info("+++++++ dataNodes " + causalityToken + " " + 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 + '"');
+        }
+
+        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.
+        IgniteBiTuple<Long, Long> revisions = 
getRevisionsOfLastScaleUpAndScaleDownEvents(causalityToken, zoneId);
+        long lastScaleUpRevision = revisions.get1();
+        long lastScaleDownRevision = revisions.get2();
+
+        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());
+
+            Set<NodeWithAttributes> logicalTopology = 
fromBytes(topologyEntry.value());
+
+            Set<Node> logicalTopologyNodes = logicalTopology.stream().map(n -> 
n.node()).collect(toSet());
+
+            Set<String> dataNodesNames = filterDataNodes(logicalTopologyNodes, 
filter, nodesAttributes);
+
+            return dataNodesNames;
+        }
+
+        LOG.info("+++++++ dataNodes lastScaleUpRevision " + 
lastScaleUpRevision);
+        LOG.info("+++++++ dataNodes lastScaleDownRevision " + 
lastScaleDownRevision);
+
+        ZoneState zoneState = zonesState.get(zoneId);
+
+        LOG.info("+++++++ dataNodes zoneState " + zoneState);
+
+        ConcurrentSkipListMap<Long, Augmentation> subAugmentationMap = null;
+
+        // On the data nodes recalculation we write new data nodes to the meta 
storage then clear the augmentation map.
+        // Therefore, first we need to read the augmentation map before it is 
cleared and then read the last data nodes value
+        // from the meta storage.
+        // The zoneState can be null if the zone was removed.
+        if (zoneState != null) {
+            subAugmentationMap = new 
ConcurrentSkipListMap<>(zoneState.topologyAugmentationMap()
+                    .headMap(causalityToken, true));
+        }
+
+        // Search the revisions of zoneScaleUpChangeTriggerKey and 
zoneScaleDownChangeTriggerKey with value greater or equals
+        // to expected one.
+        long scaleUpDataNodesRevision = searchTriggerKey(lastScaleUpRevision, 
zoneId, zoneScaleUpChangeTriggerKey(zoneId));
+        long scaleDownDataNodesRevision = 
searchTriggerKey(lastScaleDownRevision, zoneId, 
zoneScaleDownChangeTriggerKey(zoneId));
+
+        // Choose the highest revision.
+        long dataNodesRevision = max(causalityToken, 
max(scaleUpDataNodesRevision, scaleDownDataNodesRevision));
+
+        LOG.info("+++++++ dataNodes scaleUpDataNodesRevision " + 
scaleUpDataNodesRevision);
+        LOG.info("+++++++ dataNodes scaleDownDataNodesRevision " + 
scaleDownDataNodesRevision);
+
+        // Read data nodes value from the meta storage on dataNodesRevision 
and associated trigger keys.
+        Entry dataNodesEntry = msManager.getLocally(zoneDataNodesKey(zoneId), 
dataNodesRevision);
+        Entry scaleUpChangeTriggerKey = 
msManager.getLocally(zoneScaleUpChangeTriggerKey(zoneId), dataNodesRevision);
+        Entry scaleDownChangeTriggerKey = 
msManager.getLocally(zoneScaleDownChangeTriggerKey(zoneId), dataNodesRevision);
+
+        if (dataNodesEntry.value() == null) {
+            LOG.info("+++++++ dataNodes The zone was removed not 
idempotently");
+            // The zone was removed.
+            // In this case it is impossible to find out the data nodes value 
idempotently.
+            return emptySet();
+        }
+
+        Set<Node> baseDataNodes = 
DistributionZonesUtil.dataNodes(fromBytes(dataNodesEntry.value()));
+        long scaleUpTriggerRevision = 
bytesToLong(scaleUpChangeTriggerKey.value());
+        long scaleDownTriggerRevision = 
bytesToLong(scaleDownChangeTriggerKey.value());
+
+        LOG.info("+++++++ dataNodes scaleUpTriggerRevision " + 
scaleUpTriggerRevision);
+        LOG.info("+++++++ dataNodes scaleDownTriggerRevision " + 
scaleDownTriggerRevision);
+
+        LOG.info("+++++++ dataNodes baseDataNodes " + baseDataNodes);
+
+        Set<Node> finalDataNodes = new HashSet<>(baseDataNodes);
+
+        LOG.info("+++++++ dataNodes subAugmentationMap " + subAugmentationMap);
+
+        // If the subAugmentationMap is null then it means that the zone was 
removed. In this case all nodes from topologyAugmentationMap
+        // must be already written to the meta storage.
+        if (subAugmentationMap != null) {
+            // Update the data nodes set with pending data from augmentation 
map
+            subAugmentationMap.forEach((rev, augmentation) -> {
+                if (augmentation.addition() && rev > scaleUpTriggerRevision && 
rev <= lastScaleUpRevision) {
+                    for (Node node : augmentation.nodes()) {
+                        LOG.info("+++++++ dataNodes finalDataNodes.add " + 
node);
+                        finalDataNodes.add(node);
+                    }
+                }
+
+                if (!augmentation.addition() && rev > scaleDownTriggerRevision 
&& rev <= lastScaleDownRevision) {
+                    for (Node node : augmentation.nodes()) {
+                        LOG.info("+++++++ dataNodes finalDataNodes.remove " + 
node);
+                        finalDataNodes.remove(node);
+                    }
+                }
+            });
+        }
+
+        // Apply the filter to get the final data nodes set.
+        Set<String> result = filterDataNodes(finalDataNodes, filter, 
nodesAttributes);
+
+        LOG.info("+++++++ dataNodes result " + result);
+
+        return result;
+    }
+
+    /**
+     * These revisions correspond to the last configuration and topology 
events after which need to wait for the data nodes recalculation.
+     * These events are: a zone creation, changing a scale up timer to 
immediate, changing a scale down timer to immediate,
+     * changing a filter, deleting a zone, topology changes with the adding 
nodes, topology changes with removing nodes.
+     *
+     * @param causalityToken causalityToken.
+     * @param zoneId zoneId.
+     * @return Revisions.
+     */
+    private IgniteBiTuple<Long, Long> 
getRevisionsOfLastScaleUpAndScaleDownEvents(

Review Comment:
   Fixed.



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