Orleans grains 中的单线程
Single-Threading in Orleans grains
我正在尝试了解 Microsoft Orleans 中的单线程处理。我使用了 here 中的代码并对其进行了一些修改以测试我的场景。
我的客户端代码和筒仓构建代码
static async Task Main(string[] args)
{
var siloBuilder = new SiloHostBuilder()
.UseLocalhostClustering()
.UseDashboard(options => { })
.Configure<ClusterOptions>(options =>
{
options.ClusterId = "dev";
options.ServiceId = "Orleans2GettingStarted";
})
.Configure<EndpointOptions>(options =>
options.AdvertisedIPAddress = IPAddress.Loopback)
.ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Warning).AddConsole());
using (var host = siloBuilder.Build())
{
await host.StartAsync();
var clientBuilder = new ClientBuilder()
.UseLocalhostClustering()
.Configure<ClusterOptions>(options =>
{
options.ClusterId = "dev";
options.ServiceId = "Orleans2GettingStarted";
})
.ConfigureLogging(logging => logging.AddConsole());
using (var client = clientBuilder.Build())
{
await client.Connect();
var random = new Random();
string sky = "blue";
while (sky == "blue") // if run in Ireland, it exits loop immediately
{
Console.WriteLine("Client giving another request");
int grainId = random.Next(0, 500);
double temperature = random.NextDouble() * 40;
var sensor = client.GetGrain<ITemperatureSensorGrain>(grainId);
// Not awaiting this task so that next call to grain
// can be made without waiting for current call to complete
Task t = sensor.SubmitTemperatureAsync((float)temperature);
Thread.Sleep(1000);
}
}
}
}
我的grain接口和实际的grain实现
public interface ITemperatureSensorGrain : IGrainWithIntegerKey
{
Task SubmitTemperatureAsync(float temperature);
}
public class TemperatureSensorGrain : Grain, ITemperatureSensorGrain
{
public async Task SubmitTemperatureAsync(float temperature)
{
long grainId = this.GetPrimaryKeyLong();
Console.WriteLine($"{grainId} received temperature: {temperature}");
await Task.Delay(10000);
// Thread.Sleep(10000);
Console.WriteLine($"{grainId} complete");
// return Task.CompletedTask;
}
}
我基本上做的是每 1 秒向 grains 发送一次请求,而我允许 grain 中的每个方法调用至少需要 10 秒。现在,根据 grains 的单线程执行和描述的 Orleans 运行时调度 here, 我希望请求将排队并且下一个请求不会被 grain 占用,除非当前请求的方法完成。 但是,控制台输出并未证实这一点。控制台输出为:
Client giving another request
344 received temperature: 8.162848
Client giving another request
357 received temperature: 10.32219
Client giving another request
26 received temperature: 1.166182
Client giving another request
149 received temperature: 37.74038
Client giving another request
60 received temperature: 26.72013
Client giving another request
218 received temperature: 24.19116
Client giving another request
269 received temperature: 17.1897
Client giving another request
318 received temperature: 8.562404
Client giving another request
372 received temperature: 8.865559
Client giving another request
443 received temperature: 5.254442
Client giving another request
344 complete <-------------- The first request completed here
97 received temperature: 19.24687
这很清楚,在当前请求完成之前,grain 正在处理下一个请求。
问题:
那么,这是违反了 Orleans 单线程执行模型还是我在这里遗漏了什么?
此外,当我在 grain 中使用 Thread.sleep(10000) 而不是 Task.Delay(10000) 时,我得到几乎相同的控制台输出,除了一个额外的警告每次请求调用 -
Task [Id=1, Status=RanToCompletion] in WorkGroup [Activation: S127.0.0.1:11111:270246987*grn/6424EE47/00000028@cafcc6a5 #GrainType=Orleans2GettingStarted.TemperatureSensorGrain Placement=RandomPlacement State=Valid] took elapsed time 0:00:10.0019256 for execution, which is longer than 00:00:00.2000000
。
这是否意味着理想情况下每个颗粒都应该在 200 毫秒内处理?如果谷物加工时间更长会发生什么?
正如@DanWilson 在评论中所说,您正在观察这种行为,因为每次调用都是在不同的粒度上进行的。
在奥尔良,每个谷物实际上都是单线程的,但不是整个筒仓或集群。这意味着许多谷物可以同时执行,这意味着向您的主机添加更多内核或添加更多机器将允许您扩展您的服务。
将您的代码修改为 select a grainId
一次(通过将其移出循环),我看到了这个示例执行:
137 received temperature: 18.74616
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
137 complete
137 received temperature: 20.03226
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
137 complete
137 received temperature: 21.4471
这是您所期望的:许多请求正在排队(每秒一个),但每个请求需要 10 秒才能完成,然后 grain 才能开始处理下一个请求。
我正在尝试了解 Microsoft Orleans 中的单线程处理。我使用了 here 中的代码并对其进行了一些修改以测试我的场景。
我的客户端代码和筒仓构建代码
static async Task Main(string[] args)
{
var siloBuilder = new SiloHostBuilder()
.UseLocalhostClustering()
.UseDashboard(options => { })
.Configure<ClusterOptions>(options =>
{
options.ClusterId = "dev";
options.ServiceId = "Orleans2GettingStarted";
})
.Configure<EndpointOptions>(options =>
options.AdvertisedIPAddress = IPAddress.Loopback)
.ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Warning).AddConsole());
using (var host = siloBuilder.Build())
{
await host.StartAsync();
var clientBuilder = new ClientBuilder()
.UseLocalhostClustering()
.Configure<ClusterOptions>(options =>
{
options.ClusterId = "dev";
options.ServiceId = "Orleans2GettingStarted";
})
.ConfigureLogging(logging => logging.AddConsole());
using (var client = clientBuilder.Build())
{
await client.Connect();
var random = new Random();
string sky = "blue";
while (sky == "blue") // if run in Ireland, it exits loop immediately
{
Console.WriteLine("Client giving another request");
int grainId = random.Next(0, 500);
double temperature = random.NextDouble() * 40;
var sensor = client.GetGrain<ITemperatureSensorGrain>(grainId);
// Not awaiting this task so that next call to grain
// can be made without waiting for current call to complete
Task t = sensor.SubmitTemperatureAsync((float)temperature);
Thread.Sleep(1000);
}
}
}
}
我的grain接口和实际的grain实现
public interface ITemperatureSensorGrain : IGrainWithIntegerKey
{
Task SubmitTemperatureAsync(float temperature);
}
public class TemperatureSensorGrain : Grain, ITemperatureSensorGrain
{
public async Task SubmitTemperatureAsync(float temperature)
{
long grainId = this.GetPrimaryKeyLong();
Console.WriteLine($"{grainId} received temperature: {temperature}");
await Task.Delay(10000);
// Thread.Sleep(10000);
Console.WriteLine($"{grainId} complete");
// return Task.CompletedTask;
}
}
我基本上做的是每 1 秒向 grains 发送一次请求,而我允许 grain 中的每个方法调用至少需要 10 秒。现在,根据 grains 的单线程执行和描述的 Orleans 运行时调度 here, 我希望请求将排队并且下一个请求不会被 grain 占用,除非当前请求的方法完成。 但是,控制台输出并未证实这一点。控制台输出为:
Client giving another request
344 received temperature: 8.162848
Client giving another request
357 received temperature: 10.32219
Client giving another request
26 received temperature: 1.166182
Client giving another request
149 received temperature: 37.74038
Client giving another request
60 received temperature: 26.72013
Client giving another request
218 received temperature: 24.19116
Client giving another request
269 received temperature: 17.1897
Client giving another request
318 received temperature: 8.562404
Client giving another request
372 received temperature: 8.865559
Client giving another request
443 received temperature: 5.254442
Client giving another request
344 complete <-------------- The first request completed here
97 received temperature: 19.24687
这很清楚,在当前请求完成之前,grain 正在处理下一个请求。
问题:
那么,这是违反了 Orleans 单线程执行模型还是我在这里遗漏了什么?
此外,当我在 grain 中使用 Thread.sleep(10000) 而不是 Task.Delay(10000) 时,我得到几乎相同的控制台输出,除了一个额外的警告每次请求调用 -
Task [Id=1, Status=RanToCompletion] in WorkGroup [Activation: S127.0.0.1:11111:270246987*grn/6424EE47/00000028@cafcc6a5 #GrainType=Orleans2GettingStarted.TemperatureSensorGrain Placement=RandomPlacement State=Valid] took elapsed time 0:00:10.0019256 for execution, which is longer than 00:00:00.2000000
。 这是否意味着理想情况下每个颗粒都应该在 200 毫秒内处理?如果谷物加工时间更长会发生什么?
正如@DanWilson 在评论中所说,您正在观察这种行为,因为每次调用都是在不同的粒度上进行的。
在奥尔良,每个谷物实际上都是单线程的,但不是整个筒仓或集群。这意味着许多谷物可以同时执行,这意味着向您的主机添加更多内核或添加更多机器将允许您扩展您的服务。
将您的代码修改为 select a grainId
一次(通过将其移出循环),我看到了这个示例执行:
137 received temperature: 18.74616
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
137 complete
137 received temperature: 20.03226
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
Client giving another request
137 complete
137 received temperature: 21.4471
这是您所期望的:许多请求正在排队(每秒一个),但每个请求需要 10 秒才能完成,然后 grain 才能开始处理下一个请求。