You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by pt...@apache.org on 2023/04/12 04:18:21 UTC

[ignite] branch master updated: IGNITE-19222 .NET: Add IgniteClientConfiguration.EnableClusterDiscovery (#10642)

This is an automated email from the ASF dual-hosted git repository.

ptupitsyn pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new 3ecb062d91b IGNITE-19222 .NET: Add IgniteClientConfiguration.EnableClusterDiscovery (#10642)
3ecb062d91b is described below

commit 3ecb062d91b21fb23e14a7cbe0805468aef3c398
Author: Pavel Tupitsyn <pt...@apache.org>
AuthorDate: Wed Apr 12 07:18:08 2023 +0300

    IGNITE-19222 .NET: Add IgniteClientConfiguration.EnableClusterDiscovery (#10642)
    
    * Add `IgniteClientConfiguration.EnableClusterDiscovery`.
    * Allow any combinations of `EnableClusterDiscovery` and `EnablePartitionAwareness`:
      * Only `EnablePartitionAwareness`: use configured endpoints to route requests.
      * Only `EnableClusterDiscovery`: use discovered endpoints for failover, but maintain only one connection.
      * Both enabled: discover all, connect to all.
      * Both disabled: maintain one connection, use only configured endpoints for failover.
---
 .../Client/Cache/PartitionAwarenessTest.cs         | 48 +------------
 .../PartitionAwarenessWithClusterDiscoveryTest.cs  | 79 ++++++++++++++++++++++
 .../Client/Cluster/ClientClusterDiscoveryTests.cs  |  4 +-
 .../Cluster/ClientClusterDiscoveryTestsBase.cs     | 32 +++++++++
 .../Client/IgniteClientConfiguration.cs            | 22 ++++++
 .../IgniteClientConfigurationSection.xsd           |  5 ++
 .../Impl/Client/ClientFailoverSocket.cs            | 44 +++++++-----
 7 files changed, 172 insertions(+), 62 deletions(-)

diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cache/PartitionAwarenessTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cache/PartitionAwarenessTest.cs
index d6adb4929ad..2ae51699bae 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cache/PartitionAwarenessTest.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cache/PartitionAwarenessTest.cs
@@ -40,7 +40,7 @@ namespace Apache.Ignite.Core.Tests.Client.Cache
         private const int ServerCount = 3;
 
         /** */
-        private ICacheClient<int, int> _cache;
+        protected ICacheClient<int, int> _cache;
 
         /// <summary>
         /// Initializes a new instance of the <see cref="PartitionAwarenessTest"/> class.
@@ -151,44 +151,6 @@ namespace Apache.Ignite.Core.Tests.Client.Cache
             Assert.AreEqual(expected, ex.Message);
         }
 
-#if NETCOREAPP // TODO: IGNITE-15710
-        [Test]
-        public void CacheGet_NewNodeEnteredTopology_RequestIsRoutedToNewNode()
-        {
-            // Warm-up.
-            Assert.AreEqual(1, _cache.Get(1));
-
-            // Before topology change.
-            Assert.AreEqual(12, _cache.Get(12));
-            Assert.AreEqual(1, GetClientRequestGridIndex());
-
-            Assert.AreEqual(14, _cache.Get(14));
-            Assert.AreEqual(2, GetClientRequestGridIndex());
-
-            // After topology change.
-            var cfg = GetIgniteConfiguration();
-            cfg.AutoGenerateIgniteInstanceName = true;
-
-            using (Ignition.Start(cfg))
-            {
-                TestUtils.WaitForTrueCondition(() =>
-                {
-                    // Keys 12 and 14 belong to a new node now (-1).
-                    Assert.AreEqual(12, _cache.Get(12));
-                    if (GetClientRequestGridIndex() != -1)
-                    {
-                        return false;
-                    }
-
-                    Assert.AreEqual(14, _cache.Get(14));
-                    Assert.AreEqual(-1, GetClientRequestGridIndex());
-
-                    return true;
-                }, 6000);
-            }
-        }
-#endif
-
         [Test]
         [TestCase(1, 1)]
         [TestCase(2, 0)]
@@ -496,8 +458,6 @@ namespace Apache.Ignite.Core.Tests.Client.Cache
         [TestCase("default-grp-partitioned-set", null, CacheMode.Partitioned, 4, 1)]
         [TestCase("custom-grp-partitioned-set", "testIgniteSet1", CacheMode.Partitioned, 1, 1)]
         [TestCase("custom-grp-partitioned-set", "testIgniteSet1", CacheMode.Partitioned, 3, 0)]
-        [TestCase("custom-grp-replicated-set", "testIgniteSet2", CacheMode.Replicated, 1, 1)]
-        [TestCase("custom-grp-replicated-set", "testIgniteSet2", CacheMode.Replicated, 3, 1)]
         public void IgniteSet_RequestIsRoutedToPrimaryNode(
             string name, string groupName, CacheMode cacheMode, int item, int gridIdx)
         {
@@ -528,9 +488,6 @@ namespace Apache.Ignite.Core.Tests.Client.Cache
         [TestCase("custom-grp-partitioned-set-2", "testIgniteSetColocated1", CacheMode.Partitioned, 1, 1)]
         [TestCase("custom-grp-partitioned-set-2", "testIgniteSetColocated1", CacheMode.Partitioned, 2, 1)]
         [TestCase("custom-grp-partitioned-set-2", "testIgniteSetColocated1", CacheMode.Partitioned, 3, 1)]
-        [TestCase("custom-grp-replicated-set-2", "testIgniteSetColocated2", CacheMode.Replicated, 1, 1)]
-        [TestCase("custom-grp-replicated-set-2", "testIgniteSetColocated2", CacheMode.Replicated, 2, 1)]
-        [TestCase("custom-grp-replicated-set-2", "testIgniteSetColocated2", CacheMode.Replicated, 3, 1)]
         public void IgniteSetColocated_RequestIsRoutedToPrimaryNode(
             string name, string groupName, CacheMode cacheMode, int item, int gridIdx)
         {
@@ -559,13 +516,14 @@ namespace Apache.Ignite.Core.Tests.Client.Cache
             var cfg = base.GetClientConfiguration();
 
             cfg.EnablePartitionAwareness = true;
+            cfg.EnableClusterDiscovery = false;
             cfg.Endpoints.Add(string.Format("{0}:{1}", IPAddress.Loopback, IgniteClientConfiguration.DefaultPort + 1));
             cfg.Endpoints.Add(string.Format("{0}:{1}", IPAddress.Loopback, IgniteClientConfiguration.DefaultPort + 2));
 
             return cfg;
         }
 
-        private int GetClientRequestGridIndex(string message = null, string prefix = null)
+        protected int GetClientRequestGridIndex(string message = null, string prefix = null)
         {
             message = message ?? "Get";
 
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cache/PartitionAwarenessWithClusterDiscoveryTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cache/PartitionAwarenessWithClusterDiscoveryTest.cs
new file mode 100644
index 00000000000..7b9b4a29eb3
--- /dev/null
+++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cache/PartitionAwarenessWithClusterDiscoveryTest.cs
@@ -0,0 +1,79 @@
+/*
+ * 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.
+ */
+
+namespace Apache.Ignite.Core.Tests.Client.Cache
+{
+    using System.Linq;
+    using Apache.Ignite.Core.Client;
+    using NUnit.Framework;
+
+    /// <summary>
+    /// Tests Partition Awareness functionality combined with Cluster Discovery.
+    /// </summary>
+    public class PartitionAwarenessWithClusterDiscoveryTest : PartitionAwarenessTest
+    {
+#if NETCOREAPP // TODO: IGNITE-15710
+        [Test]
+        public void CacheGet_NewNodeEnteredTopology_RequestIsRoutedToNewNode()
+        {
+            // Warm-up.
+            Assert.AreEqual(1, _cache.Get(1));
+
+            // Before topology change.
+            Assert.AreEqual(12, _cache.Get(12));
+            Assert.AreEqual(1, GetClientRequestGridIndex());
+
+            Assert.AreEqual(14, _cache.Get(14));
+            Assert.AreEqual(2, GetClientRequestGridIndex());
+
+            // After topology change.
+            var cfg = GetIgniteConfiguration();
+            cfg.AutoGenerateIgniteInstanceName = true;
+
+            using (Ignition.Start(cfg))
+            {
+                TestUtils.WaitForTrueCondition(() =>
+                {
+                    // Keys 12 and 14 belong to a new node now (-1).
+                    Assert.AreEqual(12, _cache.Get(12));
+                    if (GetClientRequestGridIndex() != -1)
+                    {
+                        return false;
+                    }
+
+                    Assert.AreEqual(14, _cache.Get(14));
+                    Assert.AreEqual(-1, GetClientRequestGridIndex());
+
+                    return true;
+                }, 6000);
+            }
+        }
+#endif
+
+
+        protected override IgniteClientConfiguration GetClientConfiguration()
+        {
+            var cfg = base.GetClientConfiguration();
+
+            // Enable discovery and keep only one endpoint, let the client discover others.
+            cfg.EnableClusterDiscovery = true;
+            cfg.Endpoints = cfg.Endpoints.Take(1).ToList();
+
+            return cfg;
+        }
+    }
+}
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cluster/ClientClusterDiscoveryTests.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cluster/ClientClusterDiscoveryTests.cs
index dec7946b4c9..bbab57ff46b 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cluster/ClientClusterDiscoveryTests.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cluster/ClientClusterDiscoveryTests.cs
@@ -82,12 +82,14 @@ namespace Apache.Ignite.Core.Tests.Client.Cluster
         /// Tests that originally known node can leave and client maintains connections to other cluster nodes.
         /// </summary>
         [Test]
-        public void TestClientMaintainsConnectionWhenOriginalNodeLeaves()
+        public void TestClientMaintainsConnectionWhenOriginalNodeLeaves(
+            [Values(true, false)] bool enablePartitionAwareness)
         {
             // Client knows about single server node initially.
             var ignite = Ignition.Start(GetIgniteConfiguration());
             var cfg = GetClientConfiguration();
             cfg.Endpoints = new[] {IPAddress.Loopback + ":10803"};
+            cfg.EnablePartitionAwareness = enablePartitionAwareness;
 
             // Client starts and discovers other server nodes.
             var client = Ignition.StartClient(cfg);
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cluster/ClientClusterDiscoveryTestsBase.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cluster/ClientClusterDiscoveryTestsBase.cs
index 31617f0d526..349c518f0f5 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cluster/ClientClusterDiscoveryTestsBase.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cluster/ClientClusterDiscoveryTestsBase.cs
@@ -60,6 +60,38 @@ namespace Apache.Ignite.Core.Tests.Client.Cluster
                 AssertClientConnectionCount(client, 3);
             }
         }
+
+        /// <summary>
+        /// Tests that client with one initial endpoint discovers all servers.
+        /// </summary>
+        [Test]
+        public void TestDisabledDiscovery()
+        {
+            var cfg = new IgniteClientConfiguration(GetClientConfiguration())
+            {
+                EnableClusterDiscovery = false
+            };
+
+            using var client = Ignition.StartClient(cfg);
+
+            AssertClientConnectionCount(client, 1);
+        }
+
+        /// <summary>
+        /// Tests that client with one initial endpoint discovers all servers.
+        /// </summary>
+        [Test]
+        public void TestDisabledPartitionAwareness()
+        {
+            var cfg = new IgniteClientConfiguration(GetClientConfiguration())
+            {
+                EnablePartitionAwareness = false
+            };
+
+            using var client = Ignition.StartClient(cfg);
+
+            AssertClientConnectionCount(client, 1);
+        }
 #endif
 
         /// <summary>
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Client/IgniteClientConfiguration.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Client/IgniteClientConfiguration.cs
index 87717ab40b6..c6754e750ee 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core/Client/IgniteClientConfiguration.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core/Client/IgniteClientConfiguration.cs
@@ -60,6 +60,11 @@ namespace Apache.Ignite.Core.Client
         /// </summary>
         public const bool DefaultEnablePartitionAwareness = true;
 
+        /// <summary>
+        /// Default value of <see cref="EnableClusterDiscovery" /> property.
+        /// </summary>
+        public const bool DefaultEnableClusterDiscovery = true;
+
         /// <summary>
         /// Default socket timeout.
         /// </summary>
@@ -84,6 +89,7 @@ namespace Apache.Ignite.Core.Client
             SocketTimeout = DefaultSocketTimeout;
             Logger = new ConsoleLogger();
             EnablePartitionAwareness = DefaultEnablePartitionAwareness;
+            EnableClusterDiscovery = DefaultEnableClusterDiscovery;
         }
 
         /// <summary>
@@ -130,6 +136,7 @@ namespace Apache.Ignite.Core.Client
             Endpoints = cfg.Endpoints == null ? null : cfg.Endpoints.ToList();
             ReconnectDisabled = cfg.ReconnectDisabled;
             EnablePartitionAwareness = cfg.EnablePartitionAwareness;
+            EnableClusterDiscovery = cfg.EnableClusterDiscovery;
             Logger = cfg.Logger;
             ProtocolVersion = cfg.ProtocolVersion;
 
@@ -239,10 +246,25 @@ namespace Apache.Ignite.Core.Client
         /// To do so, connection is established to every known server node at all times.
         /// <para />
         /// When false: only one connection is established at a given moment to a random server node.
+        /// <para />
+        /// See also <see cref="EnableClusterDiscovery"/>.
         /// </summary>
         [DefaultValue(DefaultEnablePartitionAwareness)]
         public bool EnablePartitionAwareness { get; set; }
 
+        /// <summary>
+        /// Gets or sets a value indicating whether Cluster Discovery should be enabled.
+        /// <para />
+        /// Default is true: Ignite will maintain an actual list of all server nodes in the cluster and connect to
+        /// them when necessary.
+        /// When <see cref="EnablePartitionAwareness"/> is false, this list of nodes will be used for failover.
+        /// When <see cref="EnablePartitionAwareness"/> is true, connections will be established to all known nodes.
+        /// <para />
+        /// When false: Ignite client will connect only to nodes in the <see cref="Endpoints"/> list.
+        /// </summary>
+        [DefaultValue(DefaultEnableClusterDiscovery)]
+        public bool EnableClusterDiscovery { get; set; }
+
         /// <summary>
         /// Gets or sets the logger.
         /// Default is <see cref="ConsoleLogger"/>. Set to <c>null</c> to disable logging.
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/IgniteClientConfigurationSection.xsd b/modules/platforms/dotnet/Apache.Ignite.Core/IgniteClientConfigurationSection.xsd
index 9b4a74016e1..bd9068da9ec 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core/IgniteClientConfigurationSection.xsd
+++ b/modules/platforms/dotnet/Apache.Ignite.Core/IgniteClientConfigurationSection.xsd
@@ -335,6 +335,11 @@
                     <xs:documentation>Enables affinity-aware connection: client will establish connection to every known server and route requests to primary nodes for cache operations.</xs:documentation>
                 </xs:annotation>
             </xs:attribute>
+            <xs:attribute name="enableClusterDiscovery" type="xs:boolean">
+                <xs:annotation>
+                    <xs:documentation>Enables cluster discovery: client will maintain an actual list of all server nodes and connect to all of them.</xs:documentation>
+                </xs:annotation>
+            </xs:attribute>
             <xs:attribute name="userName" type="xs:string">
                 <xs:annotation>
                     <xs:documentation>Username to be used to connect to secured cluster.</xs:documentation>
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Client/ClientFailoverSocket.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Client/ClientFailoverSocket.cs
index 263f4f6ecc6..21721468e7a 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Client/ClientFailoverSocket.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Client/ClientFailoverSocket.cs
@@ -509,7 +509,12 @@ namespace Apache.Ignite.Core.Impl.Client
                 );
             }
 
-            if (!_socket.Features.HasFeature(ClientBitmaskFeature.ClusterGroupGetNodesEndpoints))
+            if (!_config.EnableClusterDiscovery)
+            {
+                _enableDiscovery = false;
+            }
+
+            if (_enableDiscovery && !_socket.Features.HasFeature(ClientBitmaskFeature.ClusterGroupGetNodesEndpoints))
             {
                 _enableDiscovery = false;
 
@@ -587,27 +592,34 @@ namespace Apache.Ignite.Core.Impl.Client
         {
             _affinityTopologyVersion = affinityTopologyVersion;
 
-            if (_discoveryTopologyVersion < affinityTopologyVersion.Version &&_config.EnablePartitionAwareness)
+            if (!_config.EnablePartitionAwareness && !_enableDiscovery)
+            {
+                return;
+            }
+
+            if (_discoveryTopologyVersion >= affinityTopologyVersion.Version)
+            {
+                return;
+            }
+
+            ThreadPool.QueueUserWorkItem(_ =>
             {
-                ThreadPool.QueueUserWorkItem(_ =>
+                try
                 {
-                    try
+                    lock (_topologyUpdateLock)
                     {
-                        lock (_topologyUpdateLock)
+                        if (!_disposed)
                         {
-                            if (!_disposed)
-                            {
-                                DiscoverEndpoints();
-                                InitSocketMap();
-                            }
+                            DiscoverEndpoints();
+                            InitSocketMap();
                         }
                     }
-                    catch (Exception e)
-                    {
-                        _logger.Log(LogLevel.Error, e, "Failed to update topology information");
-                    }
-                });
-            }
+                }
+                catch (Exception e)
+                {
+                    _logger.Log(LogLevel.Error, e, "Failed to update topology information");
+                }
+            });
         }
 
         /// <summary>