简体   繁体   English

Service Fabric参与者中的静态对象

[英]Static objects in Service Fabric actors

There is a service fabric actor with the following definition which is registered and then unregistered by implementing IRemindable interface. 有一个具有以下定义的服务结构IRemindable通过实现IRemindable接口可以注册然后取消注册。 This actor has a static dictionary too. 该演员也有一个静态字典。

[StatePersistence(StatePersistence.None)]
[ActorService(Name = "ProcessorActorService")]
public class ProcessorActor : BaseActor, IRemindable
{
 private static Dictionary<object,object> _myDictionary;

}

My understanding is that service fabric creates multiple instances of the same actor if each actor's guid is different at instantiation time. 我的理解是,如果每个参与者的GUID在实例化时都不同,那么服务结构将创建同一参与者的多个实例。 When each actor instance is created the reminder's action adds an object to the dictionary for processing later on. 在创建每个actor实例时,提醒动作将一个对象添加到字典中以供以后处理。

My expectation/understanding has been such that the dictionary's scope is within the same instantiated actor, but when the next actor's instance comes to life, I realize that the _myDictionary has the nodes that were added in another actor! 我的期望/理解是,字典的作用域在同一个实例化的actor中,但是当下一个actor的实例出现时,我意识到_myDictionary具有在另一个actor中添加的节点! So, it kind of gives the impression that the dictionary is shared among all live instances of the same actor type! 因此,给人的印象是该词典在同一参与者类型的所有活动实例之间共享! Is that a correct understanding or the same already-instantiated actor is called again?! 是再次呼唤正确的认识还是已经被实例化的演员?

Since the actor implements IRemindable I expect the GC to remove the actor from the cluster after unregistering it but it does not seem to be happening. 由于actor实现了IRemindable我希望GC在注销集群后将其从集群中删除,但似乎没有发生。 Is it possible that the static dictionary causes the actor's instance to stick around? 静态字典是否可能导致参与者的实例出现问题?

I think it's more of a .Net issue than a Service Fabric issue. 我认为这更多是.Net问题而不是Service Fabric问题。 As far as I remember, static instances are only collected once the AppDomain is collected (usually when the process terminates). 据我所知,仅在收集AppDomain之后 (通常在进程终止时)才收集静态实例。

Take a look at Fundamentals of Garbage Collection on how and when instances get collected. 看一下垃圾收集的基础知识,了解如何以及何时收集实例。

Regarding how to maintain state, I suggest you take a look at Introduction to Service Fabric Reliable Actors , it might help you achieve what you want. 关于如何维护状态,建议您看一下Service Fabric可靠角色简介 ,它可能会帮助您实现所需的目标。

Update 更新资料

Also, take a look at Reliable Actors state management . 另外,请参阅可靠参与者状态管理

Hope it helps! 希望能帮助到你!

My understanding is that service fabric creates multiple instances of the same actor if each actor's guid is different at instantiation time. 我的理解是,如果每个参与者的GUID在实例化时都不同,那么服务结构将创建同一参与者的多个实例。

This is correct. 这是对的。 Every new ID generates a new instance of the Actor. 每个新的ID都会生成Actor的新实例。


From the docs : 文档

Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. 使用static修饰符声明一个静态成员,该成员属于类型本身而不是特定对象。

When you declare private static Dictionary<object,object> _myDictionary; 当声明private static Dictionary<object,object> _myDictionary; as static you are telling to the CLR that the field does not belong to an instance of that object but to the type definition, in this case it generates a single instance that belongs to the Actor type and not the the objects created and destroyed by the runtime. 作为静态,您要告诉CLR该字段不属于该对象的实例,而是属于类型定义,在这种情况下,它将生成一个属于Actor类型的实例,而不是由该Actor创建和销毁的对象。运行。

The correct way to save the actor state is using the state manager, like this: 保存角色状态的正确方法是使用状态管理器,如下所示:

[StatePersistence(StatePersistence.Persisted)]
class MyActor : Actor, IMyActor
{
  public MyActor(ActorService actorService, ActorId actorId)
    : base(actorService, actorId)
  {
  }

  public Task<int> GetCountAsync()
  {
    return this.StateManager.GetStateAsync<int>("MyState");
  }
} 

The documentation explains it in more details. 文档对其进行了更详细的说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM