c# - Orleans grains 中的单线程

标签 c# .net-core actor orleans

我正在尝试了解 Microsoft Orleans 中的单线程 grains。我使用了 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 接口(interface)和实际的 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 Runtime Scheduling描述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 正在处理下一个请求。

问题:

  1. 那么,这是违反了 Orleans 单线程执行模型还是我在这里遗漏了什么?

  2. 此外,当我在 grain 中使用 Thread.sleep(10000) 而不是 Task.Delay(10000) 时,除了每次请求调用的额外警告外,我得到几乎相同的控制台输出 - 工作组中的任务 [Id=1, Status=RanToCompletion] [Activation: S127.0.0.1:11111:270246987*grn/6424EE47/00000028@cafcc6a5 #GrainType=Orleans2GettingStarted.TemperatureSensorGrain Placement=RandomPlacement State=Valid] 已过执行时间 0:00:10.0019256,大于 00:00:00.2000000。 这是否意味着理想情况下每个颗粒都应该在 200 毫秒内处理?如果 Cereal 加工的时间更长,会发生什么情况?

最佳答案

正如@DanWilson 在评论中所说,您正在观察这种行为,因为每个调用都是在单独的 grain 上进行的。

在 Orleans,每个 grain 实际上都是单线程的,但不是整个 silo 或 cluster。这意味着许多 grains 可以同时执行,这意味着向您的主机添加更多内核或添加更多机器将允许您扩展您的服务。

修改您的代码以仅选择一次 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 才能开始处理下一个请求。

关于c# - Orleans grains 中的单线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51527335/

相关文章:

c# - while循环中的HttpClient只执行一次

java - 使用 guice 注入(inject)与 actor 抛出空指针

scala - 如何在 OSGi 包中使用 Akka 实现真正的应用程序模块化?

c# - 如何重构这个 Linq 函数?

c# - 创建 XML 文档的最佳 .net 方法

c# - 配置Window Service Base Path并读取环境变量

SQL Server : A connection was successfully established with the server, 但随后在登录过程中发生错误

actor - Actor 模型框架和复杂事件处理 (CEP) 之间有什么区别?

c# - 以编程方式反汇编 CIL

c# - 处理网站和 cms 身份验证的用户身份