apache ignite destroycache socket-timeout or very slow - caching

download the example here:https://github.com/apache/ignite/tree/master/modules/platforms/dotnet/examples
Adjust the code in ServerNode.Program.cs
namespace Apache.Ignite.Examples.ServerNode
{
public static class Program
{
public static void Main()
{
using (var ignite = Ignition.Start(Utils.GetServerNodeConfiguration()))
{
ignite.GetCluster().SetActive(true); **//add one line code here**
Utils.DeployDefaultServices(ignite);
.....
}
}
}
}
Adjust the code in Util.cs
public static IgniteConfiguration GetServerNodeConfiguration()
{
return new IgniteConfiguration
{
Localhost = "127.0.0.1",
.......
PeerAssemblyLoadingMode = PeerAssemblyLoadingMode.CurrentAppDomain,
**//add some code here to enable persistence**
DataStorageConfiguration = new DataStorageConfiguration
{
DefaultDataRegionConfiguration = new DataRegionConfiguration
{
Name = "Default_Region",
PersistenceEnabled = true
}
}
};
}
Add the destroycache method into Apache.Ignite.Examples.Thin.Sql.LinqThin.Program Main method like this:
public static void Main()
{
using (var ignite = Ignition.StartClient(Utils.GetThinClientConfiguration()))
{
Console.WriteLine();
Console.WriteLine(">>> Cache LINQ example started.");
........
var sw = Stopwatch.StartNew();
ignite.DestroyCache(OrganizationCacheName);
sw.Stop();
Console.WriteLine($"DESTROY COST {sw.ElapsedMilliseconds}");
.....
}
....
}
When execute the detroyCache method, it throws socket timeout exception sometimes, or it cost long time sometimes if destroy successfully.
But all is OK when destroyCache if donot enable persistence.

Creating and destroying caches can be slower with persistence, especially on HDD (as opposed to SSD).
You can increase socket timeout like this:
var cfg = new IgniteClientConfiguration
{
...
SocketTimeout = TimeSpan.FromSeconds(15)
};
That being said, I've tried the steps above and it prints DESTROY COST 140 on my machine (Core i7-9700, SSD).

Related

Weighted Fair queuing algorithm doing round-robin with just 2 packet flow

I tried implementing a simple simulation of the Weighted Fair queuing algorithm.
But I noticed an issue when using only 2 packet flow, the algorithm is not letting twice the amount of T1 packet as it should because they are half the size of T2 packet. Instead it simply do round-robin between the 2 flow.
I understand the problem is that it take some time for T1 to add another packet to the queue and the Weighted Fair queuing have the time to run when T1 queue is empty.
This does not seem to be the expected behavior, am I missing something in the algorithm that would fix this corner case?
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class Packet
{
public string name;
public ManualResetEventSlim mres;
public long virFinish;
public long packetSize;
public Packet(string pname, long p_virFinish, long p_packetSize)
{
name = pname;
mres = new ManualResetEventSlim(false);
virFinish = p_virFinish;
packetSize = p_packetSize;
}
}
public class Queue
{
public ConcurrentQueue<Packet> q = new ConcurrentQueue<Packet>();
public long virStart;
public long lastVirFinish;
}
public class FairQueue
{
long VirtualTime; // system virtual time
ConcurrentDictionary<string, Queue> queues = new ConcurrentDictionary<string, Queue>();
public FairQueue() {
VirtualTime = long.MinValue;
queues.GetOrAdd("t1", new Queue());
queues.GetOrAdd("t2", new Queue());
queues.GetOrAdd("t3", new Queue());
}
public ManualResetEventSlim Write(string queueName, long packetSize)
{
var queue = queues[queueName];
Volatile.Write(ref queue.virStart, Math.Max(VirtualTime, queue.lastVirFinish));
var packetFinish = queue.virStart + packetSize;
var packet = new Packet(queueName, packetFinish, packetSize);
queue.q.Enqueue(packet);
Volatile.Write(ref queue.lastVirFinish, packetFinish);
UpdateVirtualClock(queue);
return packet.mres;
}
private void UpdateVirtualClock(Queue queue)
{
long minStart = queue.virStart;
foreach (var item in queues)
{
if (!item.Value.q.IsEmpty)
{
minStart = Math.Min(item.Value.virStart, minStart);
}
}
Volatile.Write(ref VirtualTime, Math.Max(VirtualTime, minStart));
}
// return queue with smallest lastVirFinish
public Queue SelectQueue()
{
long minVirFinish = long.MaxValue; // infinity
Queue selected = null;
foreach (var queue in queues)
{
if (!queue.Value.q.IsEmpty)
{
var lastVirFinish = Volatile.Read(ref queue.Value.lastVirFinish);
if (lastVirFinish < minVirFinish) {
minVirFinish = queue.Value.lastVirFinish;
selected = queue.Value;
}
}
}
return selected;
}
public Packet Send()
{
var selectedQueue = SelectQueue();
if (selectedQueue != null)
{
Packet p;
selectedQueue.q.TryDequeue(out p);
/* Set the start and the finish times of the remaining packets in the queue */
if (!selectedQueue.q.IsEmpty)
{
var next = selectedQueue.q.Take(1).First();
selectedQueue.virStart = selectedQueue.lastVirFinish;
selectedQueue.lastVirFinish = selectedQueue.virStart + next.packetSize;
}
return p;
}
else
{
return null;
}
}
}
class Program
{
static void Main(string[] args)
{
FairQueue fq = new FairQueue();
// Task that enqueue packet
var t1 = Task.Factory.StartNew(
() => {
while (true)
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
var s1 = fq.Write("t1", 100);
s1.Wait();
sw.Stop();
}
}
);
// Task that enqueue packet
var t2 = Task.Factory.StartNew(
() => {
while (true)
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
var s1 = fq.Write("t2", 200);
s1.Wait();
sw.Stop();
}
}
);
// Loop that run the Weighted Fair Queue Algorithm
while (true)
{
var toSend = fq.Send();
if (toSend != null)
{
Console.WriteLine(toSend.name);
Thread.Sleep((int)toSend.packetSize);
toSend.mres.Set();
}
}
}
}
}

Xamarin Realm - when to close Realm

I have a Shared Project where I have changed the database to Realm instead of SQLite.
My problem is, if I close the Realm in my DatabaseManager, the result is removed. Therefore i have created a static singelton instance of the Realm, which all my DatabaseManager use. Now my app crash after short time on memory, and if i remove all my database-functions, it works.
I create my Realm-instance here:
public class RealmDatabase
{
private Realm mRealmDB;
public Realm RealmDB
{
get
{
if (mRealmDB == null || mRealmDB.IsClosed)
{
SetRealm ();
}
return mRealmDB;
}
}
static RealmDatabase cCurrentInstance;
public static RealmDatabase Current
{
get
{
if (cCurrentInstance == null)
cCurrentInstance = new RealmDatabase ();
return cCurrentInstance;
}
}
public RealmDatabase ()
{
}
private void SetRealm ()
{
var config = new RealmConfiguration ("DBName.realm", true);
mRealmDB = Realm.GetInstance (config);
}
public Transaction BeginTransaction ()
{
return RealmDB.BeginWrite ();
}
}
The I have my DatabaseManagler looking like this:
public class NewFreeUserManager
{
internal Realm RealmDB = RealmDatabase.Current.RealmDB;
static NewFreeUserManager cCurrentInstance;
public static NewFreeUserManager Current
{
get
{
if (cCurrentInstance == null)
cCurrentInstance = new NewFreeUserManager ();
return cCurrentInstance;
}
}
private NewFreeUserManager ()
{
}
internal bool Save (FreeUser freeuser)
{
try
{
using (var trans = RealmDB.BeginWrite ())
{
RealmDB.RemoveAll<FreeUser> ();
var fu = RealmDB.CreateObject<FreeUser> ();
fu = freeuser;
trans.Commit ();
}
return true;
}
catch (Exception e)
{
Console.WriteLine ("FreeUser save: " + e.ToString ());
return false;
}
}
internal FreeUser Get ()
{
return RealmDB.All<FreeUser> ().FirstOrDefault ();
}
}
Can anyone help me?
there are a few issues with your current setup that prevent you from persisting objects properly.
The first and very important one is that Realm instances are not thread-safe. That is, using them as singletons is strongly discouraged, unless you are certain that you'll never access them from another thread.
The second is more subtle, but in your save method you are calling:
var fu = RealmDB.CreateObject<FreeUser>();
fu = freeuser;
What it does is, effectively, you are creating an object in the Realm, and then assigning the variable to another object. This will not assign freeuser's properties to fu, it just replaces one reference with another. What you're looking for is Realm.Manage so your code should look like this:
using (var trans = RealmDB.BeginWrite())
{
RealmDB.Manage(freeuser);
trans.Commit();
}
Once you fix the second bug, you should be able to go back and close Realm instances when you don't need them anymore.

nancyfx cookies based session values in Get can't be found in Post and vice versa

I'm trying with Nancyfx self hosting. The problem is when I set Session["key"] = value in Get["path"], then I call it in Post["path"], I get empty and vice versa. Below is my code
public class Bootstrapper : DefaultNancyBootstrapper
{
protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
{
CookieBasedSessions.Enable(pipelines);
}
}
public class TestModule : NancyModule
{
public TestModule()
{
Get["/"] = _ =>
{
Session["App1"] = "Ola";
return Session["App1"] + " " + "Hello World";//"Ola Hello World"
};
Get["/about"] = _ =>
{
return Session["App1"];//"Ola Hello World"
};
Post["/create"] = _ =>
{
return Session["App1"];//emtpty
};
Post["/add"] = _ =>
{
return Session["App1"];//empty
};
}
}
class Program
{
static void Main(string[] args)
{
var cfg = new HostConfiguration();
cfg.UrlReservations.CreateAutomatically = true;
var host = new NancyHost(new Bootstrapper(), cfg, new Uri("http://localhost:5050"));
host.Start();
Console.ReadKey();
WebRequest wr1 = HttpWebRequest.Create("http://localhost:5050/create");
wr1.Method = "POST";
wr1.GetRequestStream();
StreamReader sr1 = new StreamReader(wr1.GetResponse().GetResponseStream());
Console.WriteLine(sr1.ReadToEnd());
Console.ReadKey();
WebRequest wr2 = HttpWebRequest.Create("http://localhost:5050/add");
wr2.Method = "POST";
wr2.GetRequestStream();
StreamReader sr2 = new StreamReader(wr2.GetResponse().GetResponseStream());
Console.WriteLine(sr2.ReadToEnd());
Console.ReadKey();
host.Stop();
}
}
With this problem, now I have no way to save login status or some necessary info. Do you guys have a solution?
As #phill points out the code you posted works as expected.
In fact your bootstrapper and module works, and the session is accessible from all the handlers.
The problem is that when you create new HttpWebRequests the cookies from earlier calls are not preserved. To carry over the cookies from one such request to the next do what this answer says.

Optimal detection of circular reference in object (currently brute forcing)

I have a Product object, which can depend on other products.
This dependency information is stored in a ServiceTemplate object.
These dependencies can be chained (i use the arrow to indicate that 3 depends on 2, and 2 depends on 1):
1 <= 2 <= 3
I need to prevent circular references, where with the above 1 could be set to depend on 3 this causing a loop.
This is hashed up from how i think it may work but is brute forcing the solution - what would be the optimal algorithm approach or is this already the best way?
class Program
{
class Product
{
public Product(int id)
{
this.id = id;
}
private readonly int id;
public int Id { get { return this.id; } }
}
class ServiceTemplate
{
// stores the list of other products that a product depends on
Dictionary<int, List<int>> relationships = new Dictionary<int, List<int>>();
public void SetRequires(Product on, Product requires)
{
if (WouldCauseCircularDepdndency(on.Id, requires.Id) == true)
throw new ArgumentException("circular dependencies not allowed");
List<int> list = null;
this.relationships.TryGetValue(on.Id, out list);
if(list==null)
{
list = new List<int>();
this.relationships.Add(on.Id, list);
}
list.Add(requires.Id);
}
private bool WouldCauseCircularDepdndency(int on, int requires)
{
// get relationships of product we will depend on
List<int> list = null;
this.relationships.TryGetValue(requires, out list);
if (list == null)
{
return false;
}
else if (list.Contains(on))
{
return true;
}
else
{
foreach (var id in list)
{
// traverse each child recursively
if (WouldCauseCircularDepdndency(on, id))
return true;
}
}
return false; // if we got this far, no circular references detected
}
}
static void Main(string[] args)
{
var windows = new Product(1);
var linux = new Product(2);
var mySql = new Product(3);
var ms_sql = new Product(4);
var cluster = new Product(5);
var other = new Product(6);
var config = new ServiceTemplate();
config.SetRequires(mySql, windows); // mySql requires windows
config.SetRequires(mySql, linux); // mySql requires linux
config.SetRequires(ms_sql, windows); // microsoft sql requires windows
config.SetRequires(cluster, ms_sql); // cluster requires microsoft sql
config.SetRequires(other, cluster);
// this should throw an exception due to circular dependency
config.SetRequires(windows, other);
/* at this point the config relationships dictionary is as follows:
3 => 1,2
4 => 1
5 => 4
5 => 6
1 => 6
*/
}
}
You could try topological sorting. If it can be constructed, you have no circular dependency. Otherwise, you have a cycle.
I ended up using the QuickGraph nuget package and an AdjacencyGraph<int,Edge<int>>, once the relationship is added i try and to a TopologicalSort() as advised by #Vesi:
class Program
{
class Product
{
public Product(int id)
{
this.id = id;
}
private readonly int id;
public int Id { get { return this.id; } }
}
class ServiceTemplate
{
// stores the list of other products that a product depends on
AdjacencyGraph<int, Edge<int>> relationshipGraph = new AdjacencyGraph<int, Edge<int>>();
public void SetRequires(Product on, Product requires)
{
var toAdd = new Edge<int>(on.Id, requires.Id);
this.relationshipGraph.AddVerticesAndEdge(toAdd);
try
{
var list = this.relationshipGraph.TopologicalSort();
}
catch (NonAcyclicGraphException)
{
this.relationshipGraph.RemoveEdge(toAdd);
throw new ArgumentException("Circular dependencies not allowed");
}
}
}
static void Main(string[] args)
{
var windows = new Product(1);
var linux = new Product(2);
var mySql = new Product(3);
var ms_sql = new Product(4);
var cluster = new Product(5);
var other = new Product(6);
var config = new ServiceTemplate();
config.SetRequires(mySql, windows); // mySql requires windows
config.SetRequires(mySql, linux); // mySql requires linux
config.SetRequires(ms_sql, windows); // microsoft sql requires windows
config.SetRequires(cluster, ms_sql); // cluster requires microsoft sql
config.SetRequires(other, cluster);
// this should throw an exception due to circular dependency
config.SetRequires(windows, other);
}
}

Generating a proxy via Reflection.Emit only works when started with Debugging

A task at university was to implement a simple proxy generator / interceptor mechanism using Reflection.Emit.
I came up with the following program.
It seems to work just fine inside Visual Studio in debug mode [F5] (Debug -> Start Debugging) but crashes most of the time when started without debugging [Ctrl + F5] (Debug -> Start Without Debugging).
What is the difference between these two modes? (I do not refer to Debug <> Release mode).
The issue occurs on multiple machines/setups (Win XP SP3 32bit and 64bit, Windows 7 32bit).
Click for pastebin.
// The proxy generator; I assume that the error is buried along the lines emitting the IL code
public static class ProxyGenerator
{
public static T Create<T>(object obj, IInterception interception)
{
Type type = obj.GetType();
TypeBuilder proxy = DefineProxy(type);
FieldBuilder wrappedField = DefinePrivateField(proxy, "wrappedObject", type);
FieldBuilder interceptionField = DefinePrivateField(proxy, "interception", interception.GetType());
DefineConstructor(proxy, wrappedField, interceptionField);
DefineInterfaceMethods(type, proxy, wrappedField, interceptionField);
return (T) Activator.CreateInstance(proxy.CreateType(), obj, interception);
}
private static TypeBuilder DefineProxy(Type type)
{
var assemblyName = new AssemblyName {Name = "GeneratedProxyAssembly"};
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("GeneratedProxyModule");
return moduleBuilder.DefineType(
type.Name + "Proxy",
type.Attributes,
typeof (object),
type.GetInterfaces());
}
private static FieldBuilder DefinePrivateField(TypeBuilder typeBuilder, string fieldName, Type fieldType)
{
return typeBuilder.DefineField(fieldName, fieldType, FieldAttributes.Private);
}
private static void DefineConstructor(TypeBuilder typeBuilder, params FieldBuilder[] parameters)
{
ConstructorBuilder ctor = typeBuilder.DefineConstructor(
MethodAttributes.Public, CallingConventions.Standard, parameters.Select(f => f.FieldType).ToArray());
// Emit constructor
ILGenerator g = ctor.GetILGenerator();
// Load "this" pointer and call base constructor
g.Emit(OpCodes.Ldarg_0);
g.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[0]));
// Store parameters in private fields
for (int i = 0; i < parameters.Length; i++)
{
// Load "this" pointer and parameter and store paramater in private field
g.Emit(OpCodes.Ldarg_0);
g.Emit(OpCodes.Ldarg, i + 1);
g.Emit(OpCodes.Stfld, parameters[i]);
}
// Return
g.Emit(OpCodes.Ret);
}
private static void DefineInterfaceMethods(Type type, TypeBuilder proxy, FieldInfo wrappedField, FieldInfo interceptionField)
{
// Loop through all interface methods
foreach (MethodInfo interfaceMethod in type.GetInterfaces().SelectMany(i => i.GetMethods()))
{
MethodInfo method = type.GetMethod(interfaceMethod.Name);
MethodBuilder methodBuilder = proxy.DefineMethod(
method.Name,
method.Attributes,
method.ReturnType,
method.GetParameters().Select(p => p.ParameterType).ToArray());
// Emit method
ILGenerator g = methodBuilder.GetILGenerator();
// Intercept before
EmitMethodCallOnMember(g, interceptionField, "Before", false);
// Delegate method call
EmitMethodCallOnMember(g, wrappedField, method.Name, true);
// Intercept after
EmitMethodCallOnMember(g, interceptionField, "After", false);
// Return
g.Emit(OpCodes.Ret);
}
}
private static void EmitMethodCallOnMember(ILGenerator g, FieldInfo field, string methodName, bool delegateParameters)
{
// Load "this" pointer to get address of field
g.Emit(OpCodes.Ldarg_0);
g.Emit(OpCodes.Ldflda, field);
MethodInfo method = field.FieldType.GetMethod(methodName);
if (delegateParameters)
{
// Load method parameters
for (int i = 0; i < method.GetParameters().Length; i++)
{
g.Emit(OpCodes.Ldarg, i + 1);
}
}
// Emit call
g.Emit(OpCodes.Call, method);
}
}
// Some infrastructure
public interface IInterception
{
void Before();
void After();
}
public class LogInterception : IInterception
{
public void Before()
{
Console.WriteLine("Before ... ");
}
public void After()
{
Console.WriteLine("... After");
}
}
public interface ITest
{
string DoSomething(string s1, string s2);
}
public class Test : ITest
{
public string DoSomething(string s1, string s2)
{
Console.WriteLine("... doing something ...");
return s1 + s2;
}
}
// The test program, expected output is down below
internal class Program
{
internal static void Main(string[] args)
{
var test = new Test();
var proxy = ProxyGenerator.Create<ITest>(test, new LogInterception());
Console.WriteLine(test.DoSomething("Hello", " World"));
Console.WriteLine("----------------------------------------");
Console.WriteLine(proxy.DoSomething("Hello", " World"));
Console.ReadKey();
}
}
Another question: What's the best way to narrow down such issues?
I tried to save the generated assembly to disk and open the resulting dll in Reflector but it appeared to be empty.
As mentioned above, when started in debug mode the program seems to work and prints the following output.
... doing something ...
Hello World
----------------------------------------
Before ...
... doing something ...
... After
Hello World
Thanks for your time.
Try to explicitly set x86 mode on project settings tab.
I got the fatal exception only when run program in x64 or AnyCpu mode.
Ah, I've got it. Replace Ldflda with Ldfld. It works fine even without debugger (I just ran .exe).
Ldflda is for fields you pass into method as parameters with ref or out keyword.

Resources