- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试了解 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 正在处理下一个请求。
问题:
那么,这是违反了 Orleans 单线程执行模型还是我在这里遗漏了什么?
此外,当我在 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/
如果我有一个 grain(或客户端)要经常向另一个 grain 发送消息(每分钟几次,持续几个小时),访问该 grain 的最佳实践是什么?我是从工厂拿到它,使用它,然后丢弃它——每次都获得一个新的
我正在尝试了解 Microsoft Orleans 中的单线程 grains。我使用了 here 中的代码并对其进行了一些修改以测试我的场景。 我的客户端代码和筒仓构建代码 static as
我有一个用 Java 构建的现有系统,我们目前通过添加 JSP 和类来扩展它,但我们得到了一个已部署的项目。我想考虑使用 Grails 添加新功能,但我想知道如何让这两个部分进入 Tomcat 中的相
我想知道如何正确访问Environmental Variables与 SaltStack Grains 。 我一直关注documentation但一直无法让它发挥作用。这是我的代码: /srv/sal
我正在使用 Mircosoft Orleans 作为基础开发工作流引擎,因为它提供了许多有用的功能,例如自动分配工作和处理故障转移。 我有三种 Cereal : 工作流 - 保存工作流中的信息以及工作
在我们的 Orleans 设置中,我们有一些经常相互调用的 Grains。这些 Grain 调用的性能是否受到它们位于不同 Silos 中的影响? 是否有可能以某种方式告诉 Orleans 尝试激活与
根据 Kimball 的数据仓库工具包 "The grain must be declared before choosing dimensions or facts because every ca
Django 的静态文件功能允许您指定某些目录以“收集”到面向公众的文件夹中。这很好,但是有什么方法可以比仅仅拥有某些文件夹来获得更细粒度的控制呢?例如,是否有指定... 包括特定文件 排除特定文件
https://github.com/dotnet/reactive 有重大更新项目: v4.0 changes Ix Async 4.0 has a breaking change from pri
我正在使用 salt-cloud 来部署虚拟机,并且我正在尝试使用 Saltstack Reactor 系统将它们注册到我的 DNS 中。 我有一个带有这个触发器的 reactor.conf: rea
我有 5 种类型的用户: 管理员 (所有权限) 创建者(权限“v”、“a”、“m”、“c”) 经理(权限“v”、“a”、“m”) 分析师 (权限“v”、“a”) 查看者 (权限“v”) 这是一个分层设
saltstack 文档中说添加: {% set node_type = salt['grains.get']('node_type', '') %} {% if node_type %} 'no
我有一个具有以下结构的奥尔良应用程序: public interface IGraintest : Orleans.IGrainWithGuidCompoundKey { Task Init(
我有一个具有以下结构的奥尔良应用程序: public interface IGraintest : Orleans.IGrainWithGuidCompoundKey { Task Init(
假设我有以下内容: @Entity class Product { @OneToOne(Lazy) ProductType type; } @Entity class ProductType
我正在关注 orleans example在此示例中,它们不是在同一个应用程序域中同时运行筒仓和客户端,而是遍历,以便两者都可以独立启动。我按照建议进行了更改。我修改了 IGrain1.cs 以接受字
我一直在制作 Spark Streaming 的原型(prototype) 1.6.1在 Mesos 上使用 kafka 接收器 0.28以粗粒度模式运行的集群。 我有 6 个 mesos 奴隶,每个
问题/环境 我的 Web 应用程序的目标是成为我们公司数据库的便捷接口(interface)。 我正在使用: Scalatra (作为最小网络框架) Jetty (作为 servlet 容器) SBT
我创建了一个新的grails应用程序 我将asset-pipeline-gradle版本更改为2.3.8 我使用命令部署应用程序 grails dev war 我只运行命令 java -jar App
我是一名优秀的程序员,十分优秀!