今天就跟大家聊聊有關(guān)dkron的fsm是怎樣的,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)廊坊免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
// MessageType is the type to encode FSM commands. type MessageType uint8 const ( // 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可以分為SetJobType、DeleteJobType、SetExecutionType、DeleteExecutionsType、ExecutionDoneType
type dkronFSM struct { store Storage // proAppliers holds the set of pro only LogAppliers proAppliers LogAppliers } // NewFSM is used to construct a new FSM with a blank state func 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 log type LogApplier func(buf []byte, index uint64) interface{}
dkronFSM定義了store、proAppliers屬性;Apply方法將raft的log保存到KV存儲中,具體分不同msgType做不同處理;最后根據(jù)msgType查找LogAppliers
dkron的FSM根據(jù)不同msgType做不同處理,具體有applySetJob、applyDeleteJob、applyExecutionDone、applySetExecution方法。
看完上述內(nèi)容,你們對dkron的fsm是怎樣的有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
分享名稱:dkron的fsm是怎樣的
轉(zhuǎn)載源于:http://jinyejixie.com/article48/gcedhp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)頁設(shè)計公司、軟件開發(fā)、品牌網(wǎng)站設(shè)計、營銷型網(wǎng)站建設(shè)、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)