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-4137 .NET: Atomic examples added
Date Thu, 17 Nov 2016 09:56:19 GMT
Repository: ignite
Updated Branches:
  refs/heads/master f3b9ded50 -> 19a62db57


IGNITE-4137 .NET: Atomic examples added


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

Branch: refs/heads/master
Commit: 19a62db571c5dd8bb01ddee607a5a77d45ae2001
Parents: f3b9ded
Author: Pavel Tupitsyn <ptupitsyn@apache.org>
Authored: Thu Nov 17 12:56:11 2016 +0300
Committer: Pavel Tupitsyn <ptupitsyn@apache.org>
Committed: Thu Nov 17 12:56:11 2016 +0300

----------------------------------------------------------------------
 .../Apache.Ignite.Examples.csproj               |  3 +
 .../DataStructures/AtomicLongExample.cs         | 65 +++++++++++++++
 .../DataStructures/AtomicReferenceExample.cs    | 65 +++++++++++++++
 .../DataStructures/AtomicSequenceExample.cs     | 88 ++++++++++++++++++++
 .../Apache.Ignite.ExamplesDll.csproj            |  3 +
 .../DataStructures/AtomicLongIncrementAction.cs | 49 +++++++++++
 .../AtomicReferenceModifyAction.cs              | 61 ++++++++++++++
 .../AtomicSequenceIncrementAction.cs            | 49 +++++++++++
 8 files changed, 383 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/19a62db5/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 2583381..b601529 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
@@ -60,6 +60,9 @@
     <Compile Include="Datagrid\StoreExample.cs" />
     <Compile Include="Datagrid\TransactionDeadlockDetectionExample.cs" />
     <Compile Include="Datagrid\TransactionExample.cs" />
+    <Compile Include="DataStructures\AtomicLongExample.cs" />
+    <Compile Include="DataStructures\AtomicReferenceExample.cs" />
+    <Compile Include="DataStructures\AtomicSequenceExample.cs" />
     <Compile Include="Events\EventsExample.cs" />
     <Compile Include="Messaging\MessagingExample.cs" />
     <Compile Include="Misc\LifecycleExample.cs" />

http://git-wip-us.apache.org/repos/asf/ignite/blob/19a62db5/modules/platforms/dotnet/examples/Apache.Ignite.Examples/DataStructures/AtomicLongExample.cs
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/DataStructures/AtomicLongExample.cs
b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/DataStructures/AtomicLongExample.cs
new file mode 100644
index 0000000..86f28a5
--- /dev/null
+++ b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/DataStructures/AtomicLongExample.cs
@@ -0,0 +1,65 @@
+/*
+ * 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.Examples.DataStructures
+{
+    using System;
+    using System.Threading;
+    using Apache.Ignite.Core;
+    using Apache.Ignite.Core.DataStructures;
+    using Apache.Ignite.ExamplesDll.DataStructures;
+
+    /// <summary>
+    /// The example demonstrates the usage of the distributed atomic long data structure,
which has functionality 
+    /// similar to <see cref="Interlocked"/>, but provides cluster-wide atomicity guarantees.
+    /// <para />
+    /// 1) Set this class as startup object (Apache.Ignite.Examples project -> right-click
-> Properties ->
+    ///     Application -> Startup object);
+    /// 2) Start example (F5 or Ctrl+F5).
+    /// <para />
+    /// This example can be run with standalone Apache Ignite.NET node:
+    /// 1) Run %IGNITE_HOME%/platforms/dotnet/bin/Apache.Ignite.exe:
+    /// Apache.Ignite.exe -configFileName=platforms\dotnet\examples\apache.ignite.examples\app.config
-assembly=[path_to_Apache.Ignite.ExamplesDll.dll]
+    /// 2) Start example.
+    /// </summary>
+    public static class AtomicLongExample
+    {
+        [STAThread]
+        public static void Main()
+        {
+            using (var ignite = Ignition.StartFromApplicationConfiguration())
+            {
+                Console.WriteLine();
+                Console.WriteLine(">>> Atomic long example started.");
+
+                IAtomicLong atomicLong = ignite.GetAtomicLong(AtomicLongIncrementAction.AtomicLongName,
0, true);
+
+                Console.WriteLine(">>> Atomic long initial value: " + atomicLong.Read());
+
+                // Broadcast an action that increments AtomicLong a number of times.
+                ignite.GetCompute().Broadcast(new AtomicLongIncrementAction());
+
+                // Actual value will depend on a number of participating nodes.
+                Console.WriteLine("\n>>> Atomic long current value: " + atomicLong.Read());
+            }
+
+            Console.WriteLine("\n>>> Check output on all nodes.");
+            Console.WriteLine("\n>>> Example finished, press any key to exit ...");
+            Console.ReadKey();
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/19a62db5/modules/platforms/dotnet/examples/Apache.Ignite.Examples/DataStructures/AtomicReferenceExample.cs
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/DataStructures/AtomicReferenceExample.cs
b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/DataStructures/AtomicReferenceExample.cs
new file mode 100644
index 0000000..9d44bcf
--- /dev/null
+++ b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/DataStructures/AtomicReferenceExample.cs
@@ -0,0 +1,65 @@
+/*
+ * 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.Examples.DataStructures
+{
+    using System;
+    using Apache.Ignite.Core;
+    using Apache.Ignite.Core.DataStructures;
+    using Apache.Ignite.ExamplesDll.DataStructures;
+
+    /// <summary>
+    /// The example demonstrates the usage of the distributed atomic reference data structure.
+    /// <para />
+    /// 1) Set this class as startup object (Apache.Ignite.Examples project -> right-click
-> Properties ->
+    ///     Application -> Startup object);
+    /// 2) Start example (F5 or Ctrl+F5).
+    /// <para />
+    /// This example can be run with standalone Apache Ignite.NET node:
+    /// 1) Run %IGNITE_HOME%/platforms/dotnet/bin/Apache.Ignite.exe:
+    /// Apache.Ignite.exe -configFileName=platforms\dotnet\examples\apache.ignite.examples\app.config
-assembly=[path_to_Apache.Ignite.ExamplesDll.dll]
+    /// 2) Start example.
+    /// </summary>
+    public static class AtomicReferenceExample
+    {
+        [STAThread]
+        public static void Main()
+        {
+            using (var ignite = Ignition.StartFromApplicationConfiguration())
+            {
+                Console.WriteLine();
+                Console.WriteLine(">>> Atomic reference example started.");
+
+                // Create atomic reference with a value of empty Guid.
+                IAtomicReference<Guid> atomicRef = ignite.GetAtomicReference(
+                    AtomicReferenceModifyAction.AtomicReferenceName, Guid.Empty, true);
+
+                // Make sure initial value is set to Empty.
+                atomicRef.Write(Guid.Empty);
+
+                // Attempt to modify the value on each node. Only one node will succeed.
+                ignite.GetCompute().Broadcast(new AtomicReferenceModifyAction());
+                
+                // Print current value which is equal to the Id of the node that has modified
the reference first.
+                Console.WriteLine("\n>>> Current atomic reference value: " + atomicRef.Read());
+            }
+
+            Console.WriteLine("\n>>> Example finished, press any key to exit ...");
+            Console.ReadKey();
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/19a62db5/modules/platforms/dotnet/examples/Apache.Ignite.Examples/DataStructures/AtomicSequenceExample.cs
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/DataStructures/AtomicSequenceExample.cs
b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/DataStructures/AtomicSequenceExample.cs
new file mode 100644
index 0000000..f08c998
--- /dev/null
+++ b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/DataStructures/AtomicSequenceExample.cs
@@ -0,0 +1,88 @@
+/*
+ * 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.Examples.DataStructures
+{
+    using System;
+    using System.Threading;
+    using Apache.Ignite.Core;
+    using Apache.Ignite.Core.DataStructures;
+    using Apache.Ignite.Core.DataStructures.Configuration;
+    using Apache.Ignite.Core.Discovery.Tcp;
+    using Apache.Ignite.Core.Discovery.Tcp.Multicast;
+    using Apache.Ignite.ExamplesDll.DataStructures;
+
+    /// <summary>
+    /// The example demonstrates the usage of the distributed atomic sequence data structure,
which has functionality 
+    /// similar to <see cref="Interlocked"/>, but provides cluster-wide atomicity guarantees.
+    /// <para />
+    /// 1) Set this class as startup object (Apache.Ignite.Examples project -> right-click
-> Properties ->
+    ///     Application -> Startup object);
+    /// 2) Start example (F5 or Ctrl+F5).
+    /// <para />
+    /// This example can be run with standalone Apache Ignite.NET node:
+    /// 1) Run %IGNITE_HOME%/platforms/dotnet/bin/Apache.Ignite.exe:
+    /// Apache.Ignite.exe -configFileName=platforms\dotnet\examples\apache.ignite.examples\app.config
-assembly=[path_to_Apache.Ignite.ExamplesDll.dll]
+    /// 2) Start example.
+    /// </summary>
+    public static class AtomicSequenceExample
+    {
+        [STAThread]
+        public static void Main()
+        {
+            var atomicCfg = new AtomicConfiguration
+            {
+                // Each node reserves 10 numbers to itself, so that 10 increments can be
done locally, 
+                // without communicating to other nodes. After that, another 10 elements
are reserved.
+                AtomicSequenceReserveSize = 10
+            };
+
+            var cfg = new IgniteConfiguration
+            {
+                DiscoverySpi = new TcpDiscoverySpi
+                {
+                    IpFinder = new TcpDiscoveryMulticastIpFinder
+                    {
+                        Endpoints = new[] { "127.0.0.1:47500" }
+                    }
+                },
+                AtomicConfiguration = atomicCfg
+            };
+
+            using (var ignite = Ignition.Start(cfg))
+            {
+                Console.WriteLine();
+                Console.WriteLine(">>> Atomic sequence example started.");
+
+                IAtomicSequence atomicSequence = 
+                    ignite.GetAtomicSequence(AtomicSequenceIncrementAction.AtomicSequenceName,
0, true);
+
+                Console.WriteLine(">>> Atomic sequence initial value: " + atomicSequence.Read());
+
+                // Broadcast an action that increments AtomicSequence a number of times.
+                ignite.GetCompute().Broadcast(new AtomicSequenceIncrementAction());
+
+                // Actual value will depend on number of participating nodes.
+                Console.WriteLine("\n>>> Atomic sequence current value: " + atomicSequence.Read());
+            }
+
+            Console.WriteLine("\n>>> Check output on all nodes.");
+            Console.WriteLine("\n>>> Example finished, press any key to exit ...");
+            Console.ReadKey();
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/19a62db5/modules/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/Apache.Ignite.ExamplesDll.csproj
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/Apache.Ignite.ExamplesDll.csproj
b/modules/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/Apache.Ignite.ExamplesDll.csproj
index a41c2f4..4ab2d4b 100644
--- a/modules/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/Apache.Ignite.ExamplesDll.csproj
+++ b/modules/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/Apache.Ignite.ExamplesDll.csproj
@@ -50,6 +50,9 @@
     <Compile Include="Datagrid\ContinuousQueryFilter.cs" />
     <Compile Include="Datagrid\EmployeeStore.cs" />
     <Compile Include="Datagrid\ScanQueryFilter.cs" />
+    <Compile Include="DataStructures\AtomicReferenceModifyAction.cs" />
+    <Compile Include="DataStructures\AtomicSequenceIncrementAction.cs" />
+    <Compile Include="DataStructures\AtomicLongIncrementAction.cs" />
     <Compile Include="Events\LocalListener.cs" />
     <Compile Include="Messaging\LocalListener.cs" />
     <Compile Include="Messaging\RemoteOrderedListener.cs" />

http://git-wip-us.apache.org/repos/asf/ignite/blob/19a62db5/modules/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/DataStructures/AtomicLongIncrementAction.cs
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/DataStructures/AtomicLongIncrementAction.cs
b/modules/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/DataStructures/AtomicLongIncrementAction.cs
new file mode 100644
index 0000000..87732f8
--- /dev/null
+++ b/modules/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/DataStructures/AtomicLongIncrementAction.cs
@@ -0,0 +1,49 @@
+/*
+ * 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.ExamplesDll.DataStructures
+{
+    using System;
+    using Apache.Ignite.Core;
+    using Apache.Ignite.Core.Compute;
+    using Apache.Ignite.Core.DataStructures;
+    using Apache.Ignite.Core.Resource;
+
+    /// <summary>
+    /// Increments atomic long.
+    /// </summary>
+    [Serializable]
+    public class AtomicLongIncrementAction : IComputeAction
+    {
+        /** */
+        public const string AtomicLongName = "dotnet_atomic_long";
+
+        /** */
+        [InstanceResource] private readonly IIgnite _ignite;
+
+        /// <summary>
+        /// Invokes action.
+        /// </summary>
+        public void Invoke()
+        {
+            IAtomicLong atomicLong = _ignite.GetAtomicLong(AtomicLongName, 0, true);
+
+            for (int i = 0; i < 20; i++)
+                Console.WriteLine(">>> AtomicLong value has been incremented: "
+ atomicLong.Increment());
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/19a62db5/modules/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/DataStructures/AtomicReferenceModifyAction.cs
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/DataStructures/AtomicReferenceModifyAction.cs
b/modules/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/DataStructures/AtomicReferenceModifyAction.cs
new file mode 100644
index 0000000..196a2d1
--- /dev/null
+++ b/modules/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/DataStructures/AtomicReferenceModifyAction.cs
@@ -0,0 +1,61 @@
+/*
+ * 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.ExamplesDll.DataStructures
+{
+    using System;
+    using Apache.Ignite.Core;
+    using Apache.Ignite.Core.Compute;
+    using Apache.Ignite.Core.DataStructures;
+    using Apache.Ignite.Core.Resource;
+
+    /// <summary>
+    /// Increments atomic sequence.
+    /// </summary>
+    [Serializable]
+    public class AtomicReferenceModifyAction : IComputeAction
+    {
+        /** */
+        public const string AtomicReferenceName = "dotnet_atomic_reference";
+
+        /** */
+        [InstanceResource] private readonly IIgnite _ignite;
+
+        /// <summary>
+        /// Invokes action.
+        /// </summary>
+        public void Invoke()
+        {
+            // Get or create the atomic reference.
+            IAtomicReference<Guid> atomicRef = _ignite.GetAtomicReference(AtomicReferenceName,
Guid.Empty, true);
+
+            // Get local node id.
+            Guid localNodeId = _ignite.GetCluster().GetLocalNode().Id;
+
+            // Replace empty value with current node id.
+            Guid expectedValue = Guid.Empty;
+
+            Guid originalValue = atomicRef.CompareExchange(localNodeId, expectedValue);
+
+            if (originalValue == expectedValue)
+                Console.WriteLine(">>> Successfully updated atomic reference on
node {0}", localNodeId);
+            else
+                Console.WriteLine(">>> Failed to update atomic reference on node
{0}, actual value is {1}", 
+                    localNodeId, originalValue);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/19a62db5/modules/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/DataStructures/AtomicSequenceIncrementAction.cs
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/DataStructures/AtomicSequenceIncrementAction.cs
b/modules/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/DataStructures/AtomicSequenceIncrementAction.cs
new file mode 100644
index 0000000..c7112b9
--- /dev/null
+++ b/modules/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/DataStructures/AtomicSequenceIncrementAction.cs
@@ -0,0 +1,49 @@
+/*
+ * 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.ExamplesDll.DataStructures
+{
+    using System;
+    using Apache.Ignite.Core;
+    using Apache.Ignite.Core.Compute;
+    using Apache.Ignite.Core.DataStructures;
+    using Apache.Ignite.Core.Resource;
+
+    /// <summary>
+    /// Increments atomic sequence.
+    /// </summary>
+    [Serializable]
+    public class AtomicSequenceIncrementAction : IComputeAction
+    {
+        /** */
+        public const string AtomicSequenceName = "dotnet_atomic_sequence";
+
+        /** */
+        [InstanceResource] private readonly IIgnite _ignite;
+
+        /// <summary>
+        /// Invokes action.
+        /// </summary>
+        public void Invoke()
+        {
+            IAtomicSequence atomicSequence = _ignite.GetAtomicSequence(AtomicSequenceName,
0, true);
+
+            for (int i = 0; i < 20; i++)
+                Console.WriteLine(">>> AtomicSequence value has been incremented:
" + atomicSequence.Increment());
+        }
+    }
+}


Mime
View raw message