Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is dkron's fsm like?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about what the fsm of dkron is like. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

MessageType// MessageType is the type to encode FSM commands.type MessageType uint8const (/ / SetJobType is the command used to store a job in the store. SetJobType MessageType = iota / / DeleteJobType is the command used to delete a Job from the store. DeleteJobType / / SetExecutionType is the command used to store an Execution to the store. SetExecutionType / / DeleteExecutionsType is the command used to delete executions from the store. DeleteExecutionsType / / ExecutionDoneType is the command to perform the logic needed once an exeuction / / is done. ExecutionDoneType)

MessageType can be divided into SetJobType, DeleteJobType, SetExecutionType, DeleteExecutionsType, ExecutionDoneType

DkronFSMtype dkronFSM struct {store Storage / / proAppliers holds the set of pro only LogAppliers proAppliers LogAppliers} / / NewFSM is used to construct a newFSM with a blank statefunc newFSM (store Storage, logAppliers LogAppliers) * dkronFSM {return & dkronFSM {store: store, proAppliers: logAppliers }} / / Apply applies a Raft log entry to the key-value store.func (d * dkronFSM) Apply (l * raft.Log) interface {} {buf: = l.Data msgType: = MessageType (buf [0]) log.WithField ("command" MsgType) .Debug ("fsm: received command") switch msgType {case SetJobType: return d.applySetJob (Buf [1:]) case DeleteJobType: return d.applyDeleteJob (Buf [1:]) case ExecutionDoneType: return d.applyExecutionDone (Buf [1:]) case SetExecutionType: return d.applySetExecution (buf [1:]) :]} / / Check enterprise only message types. If applier, ok: = d.proAppliers [msgType]; ok {return applier (buf [1:], l.Index)} return nil} func (d * dkronFSM) applySetJob (buf [] byte) interface {} {var pj dkronpb.Job if err: = proto.Unmarshal (buf, & pj) Err! = nil {return err} job: = NewJobFromProto (& pj) if err: = d.store.SetJob (job, false); err! = nil {return err} return nil} func (d * dkronFSM) applyDeleteJob (buf [] byte) interface {} {var djr dkronpb.DeleteJobRequest if err: = proto.Unmarshal (buf, & djr) Err! = nil {return err} job, err: = d.store.DeleteJob (djr.GetJobName ()) if err! = nil {return err} return job} func (d * dkronFSM) applyExecutionDone (buf [] byte) interface {} {var execDoneReq dkronpb.ExecutionDoneRequest if err: = proto.Unmarshal (buf, & execDoneReq) Err! = nil {return err} execution: = NewExecutionFromProto (execDoneReq.Execution) log.WithField ("execution", execution.Key (). WithField ("output", string (execution.Output)) Debug ("fsm: Setting execution") _, err: = d.store.SetExecutionDone (execution) return err} func (d * dkronFSM) applySetExecution (buf [] byte) interface {} {var pbex dkronpb.Execution if err: = proto.Unmarshal (buf, & pbex) Err! = nil {return err} execution: = NewExecutionFromProto (& pbex) key, err: = d.store.SetExecution (execution) if err! = nil {return err} return key} / / Snapshot returns a snapshot of the key-value store. We wrap// the things we need in dkronSnapshot and then send that over to Persist.// Persist encodes the needed data from dkronSnapshot and transport it to// Restore where the necessary data is replicated into the finite state machine.// This allows the consensus algorithm to truncate the replicated log.func (d * dkronFSM) Snapshot () (raft.FSMSnapshot, error) {return & dkronSnapshot {store: d.store} Nil} / / Restore stores the key-value store to a previous state.func (d * dkronFSM) Restore (r io.ReadCloser) error {defer r.Close () return d.store.Restore (r)} / / LogApplier is the definition of a function that can apply a Raft logtype LogApplier func (buf [] byte, index uint64) interface {}

DkronFSM defines store and proAppliers attributes; the Apply method saves the log of raft to KV storage, which is processed differently according to different msgType; finally, it looks up LogAppliers according to msgType

Summary

The FSM of dkron is processed differently according to different msgType, such as applySetJob, applyDeleteJob, applyExecutionDone and applySetExecution.

After reading the above, do you have any further understanding of dkron's fsm? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report