In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces how to analyze ESGrain and ESRepGrain in the Actor-ES framework, the content is very detailed, interested friends can refer to, hope to be helpful to you.
ESGrain
Life cycle
ESGrain in Ray inherits from Grain and extends the life cycle of Grain. The life cycle of Grain is included in the document appendix: 1-Grain life cycle-translation notes. Md
ESGrain overrides the OnActivateAsync method of Grain. The initialization process of ESGrain is as follows:
Initialize State in ESGrain
Call ReadSnapshotAsync () to read the snapshot.
If no snapshot is obtained, the call to InitState () initializes ESGrain,InitState () according to the code in InitState () is a virtual method that can be rewritten by a specific ESGrain to customize initialization.
Read the event library, replay events, and get the latest State.
Tip: in actual development, you can rewrite InitState (), where the initialization of the state is customized based on the data in the relational database.
Use
State
ESGrain data is stored in State, and when ESGrain is activated, State data is stored in memory and persisted as snapshots. When defining ESGrain, you need to define State and implement the IState interface. By default, serialization uses the protocol buffer,State class to add the protocol buffer feature. The IState interface defines the basic part of the State, that is, the base part of the example, and beyond the base is the data that the current actor needs to store.
Sample code:
[ProtoContract (ImplicitFields = ImplicitFields.AllFields)] public class AccountState: IState {# region base public string StateId {get; set;} public uint Version {get; set;} public uint DoingVersion {get; set;} public DateTime VersionTime {get; set;} # endregion public decimal Balance {get; set;}
Event
Data is transferred between ESGrain through Event. For Event compilation, please refer to Event for .md.
EventHandles
Using ESGrain to raise an event is generally based on two considerations: 1. Transfer data to Handler;2. Modify the data in State. Modifying the data in ESGrain is achieved by code in EventHandle.
Use:
Implement IEventHandle
The implementation defines the events to be handled in Apply.
Sample code:
Public class AccountEventHandle: IEventHandle
{
Public void Apply (object state, IEvent evt)
{
If (state is AccountState actorState)
{
Switch (evt)
{
Case AmountAddEvent value: AmountAddEventHandle (actorState, value); break
Case AmountTransferEvent value: AmountTransferEventHandle (actorState, value); break
Default: break
}
}
}
Private void AmountTransferEventHandle (AccountState state, AmountTransferEvent evt)
{
State.Balance = evt.Balance
}
Private void AmountAddEventHandle (AccountState state, AmountAddEvent evt)
{
State.Balance = evt.Balance
}
}
The data in State is stored in memory, and a large amount of data is stored in State. In a way, State can be regarded as an in-memory database.
In Ray, modifying the data of State is done through EventHandle (there is only one way).
ESGrain species
Ray provides both MongoESGrain and SqlGrain by default.
ESGrain description:
The type of K:StateId.
State for S:ESGrain.
W:MessageInfo .
Complete ESGrain example
When writing ESGrain
Clear RabbitPub.
Clear MongoStorage.
Inherit MongoESGrain or SqlGrain.
Implement the ESGrain interface.
If you need to rewrite the OnActivateAsync.
Write Actor methods of interest
If you need to send an event: 1. Define events; 2. Write EventHandler.
[RabbitMQ.RabbitPub ("Account", "account")]
[MongoStorage ("Test", "Account")]
Public sealed class Account: MongoESGrain, IAccount
{
Protected override string GrainId = > this.GetPrimaryKeyString ()
Static IEventHandle _ eventHandle = new AccountEventHandle ()
Protected override IEventHandle EventHandle = > _ eventHandle
Public override Task OnActivateAsync ()
{
Return base.OnActivateAsync ()
}
Public Task Transfer (string toAccountId, decimal amount)
{
Var evt = new AmountTransferEvent (toAccountId, amount, this.State.Balance-amount)
Return RaiseEvent (evt) .AsTask ()
}
Public Task AddAmount (decimal amount, string uniqueId = null)
{
Var evt = new AmountAddEvent (amount, this.State.Balance + amount)
Return RaiseEvent (evt, uniqueId: uniqueId). AsTask ()
}
[AlwaysInterleave]
Public Task GetBalance ()
{
Return Task.FromResult (this.State.Balance)
}
}
ESRepGrain
ESGrain defaults to the main Actor. When the pressure of a single Actor is too high, the replica actor of the actor can be realized, and the replica actor is mainly used to deal with: 1. The operation of reading 2. Other asynchronous operations that are not written.
The mechanism for maintaining synchronization between the primary actor and the replica actor:
The main actor raises the event, passes the message to the replica actor in the CoreHandler, and replays the event in the replica actor.
The main actor and the replica actor persist the same snapshot library and event library. Will also be activated from the same library.
Life cycle
Similar to the main actor.
Use
Similar to ESGrain, the comparison is as follows:
ESGrainESRepGrain explicit RabbitPub does not need to explicitly MongoStorage explicit MongoStorage inherits MongoESGrain or SqlGrain inherits MongoESRepGrain or SqlRepGrain implements a custom copy of the ESGrain interface Actor interface if you need to rewrite OnActivateAsync if you need to rewrite OnActivateAsync write interested Actor methods write interested Actor methods if you need to send events: 1. Define events; 2. Writing EventHandler does not raise events
Example
[MongoStorage ("Test", "Account")]
Public sealed class AccountRep: MongoESRepGrain, IAccountRep
{
Protected override string GrainId = > this.GetPrimaryKeyString ()
Static IEventHandle _ eventHandle = new AccountEventHandle ()
Protected override IEventHandle EventHandle = > _ eventHandle
Public Task GetBalance ()
{
Return Task.FromResult (this.State.Balance)
}
}
On how to analyze the Actor-ES framework of ESGrain and ESRepGrain to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.