Interleaving of Orleans tasks

久未见 提交于 2020-01-05 08:36:29

问题


There is a question here where Orleans task execution in context of ContinueWith is discussed. I modified the example a bit more to check if two tasks of the same grain in Orleans can be made to interleave without the [Reentrant] flag. The entire code is here. The highlights of the code are as follows:

// Client code
var hashGenerator = client.GetGrain<IHashGeneratorGrain>(0);
Console.WriteLine("Client making a call");
hashGenerator.Call_A_ToTemp();
await Task.Delay(10000);
hashGenerator.Call_B_ToTemp();


// HashGeneratorGrain
public async Task Call_A_ToTemp()
{
    Console.WriteLine("Making call A to a fellow grain");
    ITempGrain grain = this.GrainFactory.GetGrain<ITempGrain>(1);

    // CallA() returns successfully in 20s that is less than the timeout.
    grain.CallA().ContinueWith(async (t)=> 
    {
       if (t.IsCompletedSuccessfully)
       {
          Console.WriteLine("Task success");
          Console.WriteLine("Call_A_ToTemp: Counter value now is: " + counter);
          var selfGrain = this.GrainFactory.GetGrain<IHashGeneratorGrain>(0);
          await selfGrain.IncCounter();
          Console.WriteLine("Call_A_ToTemp: after increment: " + this.counter);
       }
    }
    );
}

public async Task IncCounter() 
{
   this.counter += 1;
}

public async Task Call_B_ToTemp()
{
   Console.WriteLine("Call_B_ToTemp called");
   Console.WriteLine("Call_B_ToTemp: Counter value now is: " + counter);
   Console.WriteLine("Call_B_ToTemp: now sleep for 20s");
   await Task.Delay(20000);
   Console.WriteLine("Call_B_ToTemp: after sleep value: " + counter);
   this.counter += 1;
   Console.WriteLine("Call_B_ToTemp: after increment: " + this.counter);
}

// TempGrain
public async Task CallA()
{
    Console.WriteLine("Call A came to TempGrain");
    await Task.Delay(20000);
}

The output of this was:

Client making a call
Making call A to a fellow grain
Call A came to TempGrain
Call_B_ToTemp called
Call_B_ToTemp: Counter value now is: 0
Call_B_ToTemp: now sleep for 20s
Task success
Call_A_ToTemp: Counter value now is: 0
Call_A_ToTemp: after increment: 1
Call_B_ToTemp: after sleep value: 1
Call_B_ToTemp: after increment: 2

As we can see, Call_B_ToTemp() didn't complete, but IncCounter() was called in between and after it exited, Call_B_ToTemp() executed and finished.

Doesn't this violate the single threadedness of Orleans grains?

来源:https://stackoverflow.com/questions/54513019/interleaving-of-orleans-tasks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!