服务结构是否为每个有状态分区创建单例实例?

Does service fabric create singleton instance for each stateful partition?

假设我在一个 5 节点集群上创建了一个有 5 个分区的有状态服务结构服务。我看到每个节点每个节点都有 1 个分区。当我在 VS 中调试服务时,我注意到服务结构在所有 5 个分区中正好创建 5 个有状态服务实例 [基本上每个分区 1 个静态实例]。无论客户端调用多少次,都只有 5 个 class 的实例来处理请求。 以下说法是否正确?

这里有一些误解:

实例不同于进程,当你说你可以看到 Visual Studio 上的实例时,你实际上看到的是过程。在某些情况下,您可以在一个进程中拥有多个实例,而 visual studio 只会显示一个,但实际上有 5 个(在您的情况下)。这是由 SF Hosting Model configuration, where you define how it should behave, I have answered related questions here #1 and here #2

定义的

另一个是,无状态服务有实例,有状态服务有分区和副本。基于此,您看到的不是实例,它们是主副本(如果您定义复制> 1,则可能是次要副本)。在文档中查看更多详细信息 here

根据前面的详细信息,当您对服务进行分区时,根据您使用的托管模型,它可以在同一进程上创建多个副本或每个进程创建一个副本。

所有这一切只有一个问题,同一分区的副本数量限制为每个节点 1 个,要了解更多信息,请查看 this issue on github

关于您的问题:

No mater how many calls the clients make, there are only 5 instances of this class to serve requests from. Are the following statements true?

没有!

如果您正在谈论对 ASP.NET 端点的请求或远程调用,这不会影响实例计数,实例由 SF 配置定义,客户端请求不会影响它,它将被拆分在可用实例中,具体取决于您如何平衡这些请求。这些请求影响实例计数的唯一方式是当您在服务中定义自动缩放时。

当您的 Stateless\Stateful 服务的新 instance\replica 启动时(在同一进程内或外),将创建一个新对象。对这些服务的调用可能总是转到同一个对象,但情况并非总是如此,因为 SF 可能会重新平衡您的服务并终止这些实例以在其他地方创建新的实例,或者在出现故障的情况下。

有状态服务也有这种情况,除非您指定对辅助副本的读取,否则调用只会转到主要副本。如果次要副本提升为主要副本,则新调用不会重定向到之前的主要副本。比较常见的还是集群维护和应用更新。

Any class level member variables in the stateful service are essentially static [since it resolves to a singleton instance on that partition] and therefore require "lock" semantics when updating?

如果您使用共享托管模型,所有静态对象将与同一进程中的任何实例共享。

Since the client always resolves to a the "same" instance of the class all the time for a given partition, the client can re-use the "proxy" instance?

根据之前的回答,是的,任何单例 class 都将被共享。 .

How does this "singleton" model of stateful service creation affect performance and scalability when lot of clients call the same service instance hundreds of times a second. [under heavy load] I understand that there is a setting when configuring the V2 remoting listener where we can specify "MaxConcurrentCalls" via "FabricTransportRemotingListenerSettings".

我没理解清楚这里的问题,也许前面的回答能帮您解决问题,如果没有,请在下面留言,我会更新。

来自微软的确认。服务结构确实为每个分区创建了 "reliable service" 的 1 [ONE] 实例。此 "singleton" 实例将为所有客户端 remoting/http 调用提供服务。如果副本发生故障转移,将创建一个新实例。