ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ptupit...@apache.org
Subject ignite git commit: IGNITE-4116 .NET: Add ClientReconnectExample
Date Thu, 17 Nov 2016 10:46:37 GMT
Repository: ignite
Updated Branches:
  refs/heads/master 7a15a1330 -> efac70530


IGNITE-4116 .NET: Add ClientReconnectExample

This closes #1199


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/efac7053
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/efac7053
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/efac7053

Branch: refs/heads/master
Commit: efac70530884a7216b809307d9841018cc725fe8
Parents: 7a15a13
Author: Pavel Tupitsyn <ptupitsyn@apache.org>
Authored: Thu Nov 17 13:46:30 2016 +0300
Committer: Pavel Tupitsyn <ptupitsyn@apache.org>
Committed: Thu Nov 17 13:46:30 2016 +0300

----------------------------------------------------------------------
 .../Examples/ExamplesTest.cs                    |  33 ++--
 .../Apache.Ignite.Examples.csproj               |   1 +
 .../Misc/ClientReconnectExample.cs              | 157 +++++++++++++++++++
 3 files changed, 179 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/efac7053/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Examples/ExamplesTest.cs
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Examples/ExamplesTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Examples/ExamplesTest.cs
index f6730d7..064b110 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Examples/ExamplesTest.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Examples/ExamplesTest.cs
@@ -20,6 +20,7 @@ namespace Apache.Ignite.Core.Tests.Examples
     using System;
     using System.Collections.Generic;
     using System.IO;
+    using System.Linq;
     using Apache.Ignite.Core.Tests.Process;
     using Apache.Ignite.ExamplesDll.Compute;
     using NUnit.Framework;
@@ -37,7 +38,7 @@ namespace Apache.Ignite.Core.Tests.Examples
         /// Tests the example in a single node mode.
         /// </summary>
         /// <param name="example">The example to run.</param>
-        [Test, TestCaseSource("TestCases")]
+        [Test, TestCaseSource("TestCasesLocal")]
         public void TestLocalNode(Example example)
         {
             example.Run();
@@ -47,7 +48,7 @@ namespace Apache.Ignite.Core.Tests.Examples
         /// Tests the example with standalone Apache.Ignite.exe nodes.
         /// </summary>
         /// <param name="example">The example to run.</param>
-        [Test, TestCaseSource("TestCases")]
+        [Test, TestCaseSource("TestCasesRemote")]
         public void TestRemoteNodes(Example example)
         {
             TestRemoteNodes(example, false);
@@ -57,7 +58,7 @@ namespace Apache.Ignite.Core.Tests.Examples
         /// Tests the example with standalone Apache.Ignite.exe nodes while local node is
in client mode.
         /// </summary>
         /// <param name="example">The example to run.</param>
-        [Test, TestCaseSource("TestCases")]
+        [Test, TestCaseSource("TestCasesRemote")]
         public void TestRemoteNodesClientMode(Example example)
         {
             TestRemoteNodes(example, true);
@@ -70,13 +71,7 @@ namespace Apache.Ignite.Core.Tests.Examples
         /// <param name="clientMode">Client mode flag.</param>
         private static void TestRemoteNodes(Example example, bool clientMode)
         {
-            // Exclude LifecycleExample
-            if (string.IsNullOrEmpty(example.ConfigPath))
-            {
-                Assert.AreEqual("LifecycleExample", example.Name);
-
-                return;
-            }
+            Assert.IsNotEmpty(example.ConfigPath);
 
             var configPath = Path.Combine(PathUtil.IgniteHome, PathUtil.DevPrefix, example.ConfigPath);
 
@@ -142,13 +137,27 @@ namespace Apache.Ignite.Core.Tests.Examples
         }
 
         /// <summary>
-        /// Gets the test cases.
+        /// Gets the test cases for local-only scenario.
         /// </summary>
         // ReSharper disable once MemberCanBePrivate.Global
         // ReSharper disable once MemberCanBeMadeStatic.Global
-        public IEnumerable<Example> TestCases
+        public IEnumerable<Example> TestCasesLocal
         {
             get { return Example.GetExamples(); }
         }
+
+        /// <summary>
+        /// Gets the test cases for remote node scenario.
+        /// </summary>
+        // ReSharper disable once MemberCanBePrivate.Global
+        // ReSharper disable once MemberCanBeMadeStatic.Global
+        public IEnumerable<Example> TestCasesRemote
+        {
+            get
+            {
+                return Example.GetExamples()
+                    .Where(x => !new[] {"LifecycleExample", "ClientReconnectExample"}.Contains(x.Name));
+            }
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/efac7053/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj
b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj
index b601529..7f25a12 100644
--- a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj
+++ b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj
@@ -65,6 +65,7 @@
     <Compile Include="DataStructures\AtomicSequenceExample.cs" />
     <Compile Include="Events\EventsExample.cs" />
     <Compile Include="Messaging\MessagingExample.cs" />
+    <Compile Include="Misc\ClientReconnectExample.cs" />
     <Compile Include="Misc\LifecycleExample.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Services\IMapService.cs" />

http://git-wip-us.apache.org/repos/asf/ignite/blob/efac7053/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Misc/ClientReconnectExample.cs
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Misc/ClientReconnectExample.cs
b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Misc/ClientReconnectExample.cs
new file mode 100644
index 0000000..eb0e21b
--- /dev/null
+++ b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Misc/ClientReconnectExample.cs
@@ -0,0 +1,157 @@
+/*
+ * 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.
+ */
+using System;
+using System.Threading;
+using Apache.Ignite.Core;
+using Apache.Ignite.Core.Cache;
+using Apache.Ignite.Core.Common;
+
+namespace Apache.Ignite.Examples.Misc
+{
+    using Apache.Ignite.Core.Cache.Configuration;
+    using Apache.Ignite.Core.Discovery.Tcp;
+    using Apache.Ignite.Core.Discovery.Tcp.Static;
+    using Apache.Ignite.Core.Events;
+
+    /// <summary>
+    /// This example demonstrates the usage of client's automatic reconnection feature.
+    /// NOTE: There must be no other cluster nodes running on the host.
+    /// <para />
+    /// 1) Build the project Apache.Ignite.ExamplesDll (select it -> right-click ->
Build).
+    ///    Apache.Ignite.ExamplesDll.dll must appear in %IGNITE_HOME%/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/bin/${Platform]/${Configuration}
folder;
+    /// 2) Set this class as startup object (Apache.Ignite.Examples project -> right-click
-> Properties ->
+    ///     Application -> Startup object);
+    /// 3) Start example (F5 or Ctrl+F5).
+    /// </summary>
+    public class ClientReconnectExample
+    {
+        private const string CacheName = "dotnet_client_reconnect_cache";
+
+        [STAThread]
+        public static void Main()
+        {
+            Console.WriteLine();
+            Console.WriteLine(">>> Client reconnect example started.");
+
+            var evt = new ManualResetEvent(false);
+            ThreadPool.QueueUserWorkItem(_ => RunServer(evt));
+
+            Ignition.ClientMode = true;
+
+            using (var ignite = Ignition.StartFromApplicationConfiguration())
+            {
+                Console.WriteLine(">>> Client node connected to the cluster.");
+
+                if (ignite.GetCluster().GetNodes().Count > 2)
+                    throw new Exception("Extra nodes detected. " +
+                                        "ClientReconnectExample should be run without external
nodes.");
+
+                var cache = ignite.GetCache<int, string>(CacheName);
+
+                for (var i = 0; i < 10; i++)
+                {
+                    try
+                    {
+                        cache.Put(i, "val" + i);
+                        
+                        Thread.Sleep(500);
+
+                        Console.WriteLine(">>> Put value with key:" + i);
+                    }
+                    catch (CacheException e)
+                    {
+                        var disconnectedException = e.InnerException as ClientDisconnectedException;
+
+                        if (disconnectedException != null)
+                        {
+                            Console.WriteLine(">>> Client disconnected from the
cluster.");
+
+                            disconnectedException.ClientReconnectTask.Wait();
+
+                            Console.WriteLine(">>> Client reconnected to the cluster.");
+
+                            // Updating the reference to the cache. The client reconnected
to the new cluster.
+                            cache = ignite.GetCache<int, string>(CacheName);
+                        }
+                        else
+                        {
+                            throw;
+                        }
+                    }
+
+                }
+
+                // Stop the server node.
+                evt.Set();
+
+                Console.WriteLine();
+                Console.WriteLine(">>> Example finished, press any key to exit ...");
+                Console.ReadKey();
+            }
+        }
+
+        /// <summary>
+        /// Runs the server node.
+        /// </summary>
+        /// <param name="evt"></param>
+        private static void RunServer(WaitHandle evt)
+        {
+            var cfg = new IgniteConfiguration
+            {
+                // Nodes within a single process are distinguished by GridName property.
+                GridName = "serverNode",
+
+                // Discovery settings are the same as in app.config.
+                DiscoverySpi = new TcpDiscoverySpi
+                {
+                    IpFinder = new TcpDiscoveryStaticIpFinder
+                    {
+                        Endpoints = new[] {"127.0.0.1:47500"}
+                    }
+                },
+
+                CacheConfiguration = new[] {new CacheConfiguration(CacheName)},
+
+                IncludedEventTypes = new[] {EventType.NodeJoined}
+            };
+
+            // Start a server node.
+            using (var ignite = Ignition.Start(cfg))
+            {
+                Console.WriteLine(">>> Server node started.");
+
+                // Wait for the client node to join.
+                if (ignite.GetCluster().GetNodes().Count == 1)
+                    ignite.GetEvents().WaitForLocal(EventType.NodeJoined);
+
+                // Wait some time while client node performs cache operations.
+                Thread.Sleep(2000);
+            }
+
+            // Wait for client to detect the disconnect.
+            Thread.Sleep(5000);
+
+            Console.WriteLine(">>> Restarting server node...");
+
+            // Start the server again.
+            using (Ignition.Start(cfg))
+            {
+                evt.WaitOne();
+            }
+        }
+    }
+}


Mime
View raw message