alievmirza commented on code in PR #1729:
URL: https://github.com/apache/ignite-3/pull/1729#discussion_r1128154601


##########
modules/distribution-zones/src/main/java/org/apache/ignite/internal/distributionzones/DistributionZoneManager.java:
##########
@@ -954,6 +1288,92 @@ public void onError(Throwable e) {
         };
     }
 
+    private WatchListener createMetastorageDataNodesListener() {

Review Comment:
   Please add javadoc with explanation what is going on in this listner 



##########
modules/distribution-zones/src/main/java/org/apache/ignite/internal/distributionzones/DistributionZoneManager.java:
##########
@@ -954,6 +1288,92 @@ public void onError(Throwable e) {
         };
     }
 
+    private WatchListener createMetastorageDataNodesListener() {
+        return new WatchListener() {
+            @Override
+            public void onUpdate(WatchEvent evt) {
+                if (!busyLock.enterBusy()) {
+                    throw new IgniteInternalException(NODE_STOPPING_ERR, new 
NodeStoppingException());
+                }
+
+                try {
+                    int zoneId = 0;
+
+                    Set<String> newDataNodes = null;
+
+                    long scaleUpRevision = 0;
+
+                    long scaleDownRevision = 0;
+
+                    for (EntryEvent event : evt.entryEvents()) {
+                        Entry e = event.newEntry();
+
+                        if (startsWith(e.key(), zoneDataNodesKey().bytes())) {
+                            zoneId = extractZoneId(e.key());
+
+                            byte[] dataNodesBytes = e.value();
+
+                            if (dataNodesBytes != null) {
+                                newDataNodes = 
DistributionZonesUtil.dataNodes(fromBytes(dataNodesBytes));
+                            } else {
+                                newDataNodes = emptySet();
+                            }
+                        } else if (startsWith(e.key(), 
zoneScaleUpChangeTriggerKey().bytes())) {
+                            if (e.value() != null) {
+                                scaleUpRevision = bytesToLong(e.value());
+                            }
+                        } else if (startsWith(e.key(), 
zoneScaleDownChangeTriggerKey().bytes())) {
+                            if (e.value() != null) {
+                                scaleDownRevision = bytesToLong(e.value());
+                            }
+                        }
+                    }
+
+                    synchronized (dataNodesMutex) {
+                        DataNodes dataNodesMeta = dataNodes.get(zoneId);
+
+                        if (dataNodesMeta != null) {
+                            dataNodesMeta.nodes(newDataNodes);
+
+                            if (scaleUpRevision > 0) {

Review Comment:
   this code could be refactored and optimised, we have code duplication `if 
(scale*Revision > 0) {`
   
   Also completion of futures from map can be extracted to a util method 



##########
modules/distribution-zones/src/test/java/org/apache/ignite/internal/distributionzones/DistributionZoneAwaitDataNodesTest.java:
##########
@@ -0,0 +1,718 @@
+/*
+ * 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;
+
+import static java.util.Collections.emptySet;
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static 
org.apache.ignite.configuration.annotation.ConfigurationType.DISTRIBUTED;
+import static 
org.apache.ignite.internal.distributionzones.DistributionZoneManager.DEFAULT_ZONE_ID;
+import static 
org.apache.ignite.internal.distributionzones.DistributionZoneManager.DEFAULT_ZONE_NAME;
+import static 
org.apache.ignite.internal.distributionzones.DistributionZonesUtil.toDataNodesMap;
+import static 
org.apache.ignite.internal.distributionzones.DistributionZonesUtil.zoneDataNodesKey;
+import static 
org.apache.ignite.internal.distributionzones.DistributionZonesUtil.zoneLogicalTopologyPrefix;
+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.zonesDataNodesPrefix;
+import static 
org.apache.ignite.internal.distributionzones.DistributionZonesUtil.zonesLogicalTopologyKey;
+import static 
org.apache.ignite.internal.distributionzones.DistributionZonesUtil.zonesLogicalTopologyVersionKey;
+import static 
org.apache.ignite.internal.testframework.IgniteTestUtils.waitForCondition;
+import static org.apache.ignite.internal.util.ByteUtils.longToBytes;
+import static org.apache.ignite.internal.util.ByteUtils.toBytes;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import 
org.apache.ignite.internal.cluster.management.ClusterManagementGroupManager;
+import org.apache.ignite.internal.cluster.management.raft.ClusterStateStorage;
+import 
org.apache.ignite.internal.cluster.management.raft.TestClusterStateStorage;
+import org.apache.ignite.internal.cluster.management.topology.LogicalTopology;
+import 
org.apache.ignite.internal.cluster.management.topology.LogicalTopologyImpl;
+import 
org.apache.ignite.internal.cluster.management.topology.LogicalTopologyServiceImpl;
+import org.apache.ignite.internal.configuration.ConfigurationManager;
+import 
org.apache.ignite.internal.configuration.storage.TestConfigurationStorage;
+import 
org.apache.ignite.internal.distributionzones.configuration.DistributionZonesConfiguration;
+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.EntryEvent;
+import org.apache.ignite.internal.metastorage.MetaStorageManager;
+import org.apache.ignite.internal.metastorage.WatchEvent;
+import org.apache.ignite.internal.metastorage.WatchListener;
+import org.apache.ignite.internal.metastorage.dsl.StatementResultImpl;
+import org.apache.ignite.internal.metastorage.impl.EntryImpl;
+import org.apache.ignite.internal.schema.configuration.TablesConfiguration;
+import org.apache.ignite.internal.testframework.IgniteAbstractTest;
+import org.apache.ignite.internal.vault.VaultManager;
+import org.apache.ignite.lang.ByteArray;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests awaiting data nodes algorithm in distribution zone manager in case 
when dataNodesAutoAdjustScaleUp
+ * or dataNodesAutoAdjustScaleDown equals to 0.
+ */
+public class DistributionZoneAwaitDataNodesTest extends IgniteAbstractTest {

Review Comment:
   also we need test scenarios, when non-immediate timer was triggered, and we 
try to get data nodes, I would expect that we receive recent propagated data 
nodes



##########
modules/distribution-zones/src/main/java/org/apache/ignite/internal/distributionzones/DistributionZoneManager.java:
##########
@@ -954,6 +1288,92 @@ public void onError(Throwable e) {
         };
     }
 
+    private WatchListener createMetastorageDataNodesListener() {
+        return new WatchListener() {
+            @Override
+            public void onUpdate(WatchEvent evt) {
+                if (!busyLock.enterBusy()) {
+                    throw new IgniteInternalException(NODE_STOPPING_ERR, new 
NodeStoppingException());
+                }
+
+                try {
+                    int zoneId = 0;
+
+                    Set<String> newDataNodes = null;
+
+                    long scaleUpRevision = 0;
+
+                    long scaleDownRevision = 0;
+
+                    for (EntryEvent event : evt.entryEvents()) {
+                        Entry e = event.newEntry();
+
+                        if (startsWith(e.key(), zoneDataNodesKey().bytes())) {
+                            zoneId = extractZoneId(e.key());
+
+                            byte[] dataNodesBytes = e.value();
+
+                            if (dataNodesBytes != null) {
+                                newDataNodes = 
DistributionZonesUtil.dataNodes(fromBytes(dataNodesBytes));
+                            } else {
+                                newDataNodes = emptySet();
+                            }
+                        } else if (startsWith(e.key(), 
zoneScaleUpChangeTriggerKey().bytes())) {
+                            if (e.value() != null) {
+                                scaleUpRevision = bytesToLong(e.value());
+                            }
+                        } else if (startsWith(e.key(), 
zoneScaleDownChangeTriggerKey().bytes())) {
+                            if (e.value() != null) {
+                                scaleDownRevision = bytesToLong(e.value());
+                            }
+                        }
+                    }
+
+                    synchronized (dataNodesMutex) {
+                        DataNodes dataNodesMeta = dataNodes.get(zoneId);
+
+                        if (dataNodesMeta != null) {
+                            dataNodesMeta.nodes(newDataNodes);
+
+                            if (scaleUpRevision > 0) {
+                                dataNodesMeta.scaleUpRevision(scaleUpRevision);
+                            }
+
+                            if (scaleDownRevision > 0) {
+                                
dataNodesMeta.scaleDownRevision(scaleDownRevision);
+                            }
+
+                            if (scaleUpRevision > 0) {
+                                SortedMap<Long, CompletableFuture<Void>> 
revisionScaleUpFuts =
+                                        
dataNodes.get(zoneId).revisionScaleUpFutures().headMap(scaleUpRevision, true);
+
+                                revisionScaleUpFuts.values().forEach(v -> 
v.complete(null));
+
+                                revisionScaleUpFuts.clear();
+                            }
+
+                            if (scaleDownRevision > 0) {
+                                SortedMap<Long, CompletableFuture<Void>> 
revisionScaleDownFuts =
+                                        
dataNodes.get(zoneId).revisionScaleDownFutures().headMap(scaleDownRevision, 
true);
+
+                                revisionScaleDownFuts.values().forEach(v -> 
v.complete(null));
+
+                                revisionScaleDownFuts.clear();
+                            }
+                        }
+                    }
+                } finally {
+                    busyLock.leaveBusy();
+                }
+            }
+
+            @Override
+            public void onError(Throwable e) {
+                LOG.warn("Unable to process logical topology event", e);

Review Comment:
   this is not a logical topology event



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