ContinueWith 不尊重奥尔良单线程性质
Orleans single threaded nature not respected by ContinueWith
我有以下代码 (https://github.com/avinash0161/OrleansExperiments/tree/c0155b4b0c8c1bfe60aea8624f2cc83a52853dc7):
// Client code
Console.WriteLine("Client making a call");
var hashGenerator = client.GetGrain<IGrainA>(0);
hashGenerator.Call_A_ToTemp();
await Task.Delay(1000);
hashGenerator.Call_B_ToTemp();
// GrainA code
public async Task Call_A_ToTemp()
{
Console.WriteLine("Making call A to a fellow grain");
IGrainB grain = this.GrainFactory.GetGrain<IGrainB>(1);
grain.CallA().ContinueWith((t)=>
{
if(t.IsFaulted)
{
// Silo message timeout is 32s so t.IsFaulted is true
Console.WriteLine("Task Faulted");
Call_A_ToTemp();
}
});
}
public async Task Call_B_ToTemp()
{
Console.WriteLine("Making call B to a fellow grain");
IGrainB grain = this.GrainFactory.GetGrain<IGrainB>(1);
await grain.CallB();
}
// GrainB code
public async Task CallA()
{
Console.WriteLine("Call A came to GrainB");
await Task.Delay(34000); // more than timeout for the caller
}
public Task CallB()
{
Console.WriteLine("Call B came to GrainB");
return Task.CompletedTask;
}
这段代码的输出是:
Client making a call
Making call A to a fellow grain
Call A came to GrainB
Making call B to a fellow grain
Task Faulted <---------------- This comes after Call_B_ToTemp executes
Making call A to a fellow grain
我们可以看到,Call_B_ToTemp在Call_A_ToTemp完全执行之前执行(Call_A_ToTemp的ContinueWith部分稍后执行)。这是预期的吗?它是否违反了谷物的单线程性质?
当我将 Call_A_ToTemp() 中的代码替换为:
public async Task Call_A_ToTemp()
{
Console.WriteLine("Making call A to a fellow grain");
IGrainB grain = this.GrainFactory.GetGrain<IGrainB>(1);
bool isSuccess = false;
while (! isSuccess)
{
try
{
await grain.CallA();
isSuccess = true;
} catch(TimeoutException){
Console.WriteLine("task faulted");
}
}
}
代码现在保留了单线程性质,在执行 Call_A_ToTemp() 的所有 ContinueWith
部分之前,不会调用 Call_B_ToTemp。控制台输出如下:
Client making a call
Making call A to a fellow grain
Call A came to GrainB
Task Faulted
Making call A to a fellow grain
有人可以解释一下吗?有ContinueWith
时是否违反单线程性质?
没有违反单线程的性质。您项目中的编译警告清楚地说明了问题的根源。特别是:This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
方法 async Task Call_A_ToTemp()
从不等待对 grain B 的调用。相反,它会在发出调用后立即 returns。因为 Call_A_ToTemp()
编辑的 Task
return 立即完成,允许在 grain 上执行另一个调用。一旦 grain.CallA()
完成,继续(ContinueWith(...)
)将尽快在 grain 的 TaskScheduler
上执行(例如,当 grain 正在等待另一个调用或闲置时)。
相反,如果等待调用或从方法中删除了 async
并且代码更改为 return grain.CallA().ContinueWith(...)
调用,则将观察到预期的行为。即,将代码更改为此将为您提供预期的结果:
// removed 'async' here, since we're not awaiting anything.
// using 'async' is preferred, but this is to demonstrate a point about
// using ContinueWith and un-awaited calls
public Task Call_A_ToTemp()
{
Console.WriteLine("Making call A to a fellow grain");
IGrainB grain = this.GrainFactory.GetGrain<IGrainB>(1);
// Note the 'return' here
return grain.CallA().ContinueWith((t)=>
{
if(t.IsFaulted)
{
// Silo message timeout is 32s so t.IsFaulted is true
Console.WriteLine("Task Faulted");
Call_A_ToTemp();
}
});
}
我有以下代码 (https://github.com/avinash0161/OrleansExperiments/tree/c0155b4b0c8c1bfe60aea8624f2cc83a52853dc7):
// Client code
Console.WriteLine("Client making a call");
var hashGenerator = client.GetGrain<IGrainA>(0);
hashGenerator.Call_A_ToTemp();
await Task.Delay(1000);
hashGenerator.Call_B_ToTemp();
// GrainA code
public async Task Call_A_ToTemp()
{
Console.WriteLine("Making call A to a fellow grain");
IGrainB grain = this.GrainFactory.GetGrain<IGrainB>(1);
grain.CallA().ContinueWith((t)=>
{
if(t.IsFaulted)
{
// Silo message timeout is 32s so t.IsFaulted is true
Console.WriteLine("Task Faulted");
Call_A_ToTemp();
}
});
}
public async Task Call_B_ToTemp()
{
Console.WriteLine("Making call B to a fellow grain");
IGrainB grain = this.GrainFactory.GetGrain<IGrainB>(1);
await grain.CallB();
}
// GrainB code
public async Task CallA()
{
Console.WriteLine("Call A came to GrainB");
await Task.Delay(34000); // more than timeout for the caller
}
public Task CallB()
{
Console.WriteLine("Call B came to GrainB");
return Task.CompletedTask;
}
这段代码的输出是:
Client making a call
Making call A to a fellow grain
Call A came to GrainB
Making call B to a fellow grain
Task Faulted <---------------- This comes after Call_B_ToTemp executes
Making call A to a fellow grain
我们可以看到,Call_B_ToTemp在Call_A_ToTemp完全执行之前执行(Call_A_ToTemp的ContinueWith部分稍后执行)。这是预期的吗?它是否违反了谷物的单线程性质?
当我将 Call_A_ToTemp() 中的代码替换为:
public async Task Call_A_ToTemp()
{
Console.WriteLine("Making call A to a fellow grain");
IGrainB grain = this.GrainFactory.GetGrain<IGrainB>(1);
bool isSuccess = false;
while (! isSuccess)
{
try
{
await grain.CallA();
isSuccess = true;
} catch(TimeoutException){
Console.WriteLine("task faulted");
}
}
}
代码现在保留了单线程性质,在执行 Call_A_ToTemp() 的所有 ContinueWith
部分之前,不会调用 Call_B_ToTemp。控制台输出如下:
Client making a call
Making call A to a fellow grain
Call A came to GrainB
Task Faulted
Making call A to a fellow grain
有人可以解释一下吗?有ContinueWith
时是否违反单线程性质?
没有违反单线程的性质。您项目中的编译警告清楚地说明了问题的根源。特别是:This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
方法 async Task Call_A_ToTemp()
从不等待对 grain B 的调用。相反,它会在发出调用后立即 returns。因为 Call_A_ToTemp()
编辑的 Task
return 立即完成,允许在 grain 上执行另一个调用。一旦 grain.CallA()
完成,继续(ContinueWith(...)
)将尽快在 grain 的 TaskScheduler
上执行(例如,当 grain 正在等待另一个调用或闲置时)。
相反,如果等待调用或从方法中删除了 async
并且代码更改为 return grain.CallA().ContinueWith(...)
调用,则将观察到预期的行为。即,将代码更改为此将为您提供预期的结果:
// removed 'async' here, since we're not awaiting anything.
// using 'async' is preferred, but this is to demonstrate a point about
// using ContinueWith and un-awaited calls
public Task Call_A_ToTemp()
{
Console.WriteLine("Making call A to a fellow grain");
IGrainB grain = this.GrainFactory.GetGrain<IGrainB>(1);
// Note the 'return' here
return grain.CallA().ContinueWith((t)=>
{
if(t.IsFaulted)
{
// Silo message timeout is 32s so t.IsFaulted is true
Console.WriteLine("Task Faulted");
Call_A_ToTemp();
}
});
}