Remote Assembly Loading
Overview
Many Ignite APIs involve remote code execution. For example, Ignite compute tasks are serialized, sent to remote nodes, and executed there. However, by default, .NET assemblies (DLL files) with those tasks in, must be loaded on remote nodes in order to instantiate and deserialize tasks' instances.
Before version 2.1 you had to manually load assemblies (using -assembly
swith with Apache.Ignite.exe
or some other ways).
Starting Ignite 2.1 you can take advantage of the remote assembly loading feature, that can be enabled with the
IgniteConfiguration.PeerAssemblyLoadingMode
flag. This configuration property needs to have the same value on all nodes
in the cluster. Another available mode is CurrentAppDomain
.
CurrentAppDomain Mode
PeerAssemblyLoadingMode.CurrentAppDomain
enables automatic on-demand assembly requests to other nodes in cluster,
loading assemblies into AppDomain where Ignite node runs.
Consider the following code:
// Print Hello World on all cluster nodes.
ignite.GetCompute().Broadcast(new HelloAction());
class HelloAction : IComputeAction
{
public void Invoke()
{
Console.WriteLine("Hello World!");
}
}
-
Ignite serializes the
HelloAction
instance and broadcasts to every node in the cluster. -
Remote nodes attempt to deserialize the
HelloAction
instance. If there is no such class in the currently loaded or referenced assemblies, the nodes request an assembly with the class from the node that initiated the compute task or from other nodes (if necessary). -
The assembly file is sent from the originating or other node as a byte array and loaded with the
Assembly.Load(byte[])
method.
Versioning
Assembly-qualified type name includes the assembly version and is used to resolve types.
If you keep the cluster running, do this change in the logic and see how the assembly gets reloaded automatically:
-
Modify
HelloAction
intance to print something else -
Change AssemblyVersion
-
Recompile and run the application code
-
The new version of the assembly will be deployed and executed on other nodes.
Note, if you keep the AssemblyVersion
unchanged, Ignite will use existing assembly that was previously loaded, since
there are no changes in the type name.
Assemblies with different versions can co-exist and be used side by side. Some nodes can continue running old code, while other nodes can execute computations with a newer version of the same class.
The AssemblyVersion
attribute can include asterisk () to enable the auto-increment on build:
[assembly: AssemblyVersion("1.0.")]
.
This way you can keep the cluster running, repeatedly modify and run computations, and new assembly versions will be deployed every time.
Dependencies
Dependent assemblies are also loaded automatically, e.g. when ComputeAction
calls some code from a different assembly.
Keep that in mind when using heavy frameworks and libraries: single compute call can cause lots of assemblies to be sent over the network.
Unloading
AppDomain
can be unloaded with all assemblies.Currently available CurrentAppDomain
mode uses existing AppDomain
, which means all peer-deployed assemblies will stay
loaded while current AppDomain
lives. This may cause increased memory usage.
Example
PeerAssemblyLoadingExample can be used to try out the remote assembly loading feature in practice:
-
Create a new Console Application in Visual Studio
-
Install the Ignite.NET NuGet package
Install-Package Apache.Ignite
-
Open the
packages\Apache.Ignite.2.1\lib\net40
folder -
Add the
peerAssemblyLoadingMode='CurrentAppDomain'
attribute to<igniteConfiguration>
element -
Run
Apache.Ignite.exe
(one or more times), leave the processes running -
Change
[AssemblyVersion]
inAssemblyInfo.cs
to1.0.*
-
Modify
Program.cs
in Visual Studio as shown belowusing System; using Apache.Ignite.Core; using Apache.Ignite.Core.Compute; using Apache.Ignite.Core.Deployment; namespace ConsoleApp { class Program { static void Main(string[] args) { var cfg = new IgniteConfiguration { PeerAssemblyLoadingMode = PeerAssemblyLoadingMode.CurrentAppDomain }; using (var ignite = Ignition.Start(cfg)) { ignite.GetCompute().Broadcast(new HelloAction()); } } class HelloAction : IComputeAction { public void Invoke() { Console.WriteLine("Hello, World!"); } } } }
<igniteConfiguration peerAssemblyLoadingMode='CurrentAppDomain' />
... [assembly: AssemblyVersion("1.0.*")] ...
-
Run the project and observe the
"Hello, World!"
output in the console of allApache.Ignite.exe
windows. -
Change the
"Hello, World!"
text to something else and run the program again -
Observe different output on the nodes started with
Apache.Ignite.exe
earlier.
Apache, Apache Ignite, the Apache feather and the Apache Ignite logo are either registered trademarks or trademarks of The Apache Software Foundation.