聊聊dkron的fsm
序
本文主要研究一下dkron的fsm
MessageType
// 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
dkronFSM
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做不同处理;最后根据msgType查找LogAppliers
小结
dkron的FSM根据不同msgType做不同处理,具体有applySetJob、applyDeleteJob、applyExecutionDone、applySetExecution方法。
doc
code-craft
spring boot , docker and so on 欢迎关注微信公众号: geek_luandun
推荐阅读
2022年终总结
最近两年开始陷入颓废中,博客也写的越来越少了。究其原因,主要还是陷入了职业倦怠期,最近一次跳槽感觉颇为失败,但是碍于给的薪资高,为了五斗米折腰,又加上最近行情不好,想要往外跳也跳不了,就这样子一直...
codecraft阅读 687
SegmentFault 思否正式开源问答社区软件 Answer
作为国内领先的新一代技术问答社区,SegmentFault 思否团队在社区建设上有着多年积累。Answer 不仅拥有搭建问答平台(Q&A Platform)的基础功能,还在产品设计上融入了开发团队对社区发展的思考,并将其经验产品...
SegmentFault思否赞 29阅读 4.2k评论 14
Golang 中 []byte 与 string 转换
string 类型和 []byte 类型是我们编程时最常使用到的数据结构。本文将探讨两者之间的转换方式,通过分析它们之间的内在联系来拨开迷雾。
机器铃砍菜刀赞 21阅读 54.6k评论 1
年度最佳【golang】map详解
这篇文章主要讲 map 的赋值、删除、查询、扩容的具体执行过程,仍然是从底层的角度展开。结合源码,看完本文一定会彻底明白 map 底层原理。
去去1002赞 14阅读 11k评论 2
年度最佳【golang】GMP调度详解
Golang最大的特色可以说是协程(goroutine)了, 协程让本来很复杂的异步编程变得简单, 让程序员不再需要面对回调地狱, 虽然现在引入了协程的语言越来越多, 但go中的协程仍然是实现的是最彻底的. 这篇文章将通过分析...
去去1002赞 13阅读 11.1k评论 4
【已结束】SegmentFault 思否技术征文丨浅谈 Go 语言框架
亲爱的开发者们:我们的 11 月技术征文如期而来,这次主题围绕 「 Go 」 语言,欢迎大家来参与分享~征文时间11 月 4 日 - 11 月 27 日 23:5911 月 28 日 18:00 前发布中奖名单参与条件新老思否作者均可参加征文...
SegmentFault思否赞 11阅读 4.6k评论 11
【Go微服务】开发gRPC总共分三步
之前我也有写过RPC相关的文章:《 Go RPC入门指南:RPC的使用边界在哪里?如何实现跨语言调用?》,详细介绍了RPC是什么,使用边界在哪里?并且用Go和php举例,实现了跨语言调用。不了解RPC的同学建议先读这篇文...
王中阳Go赞 8阅读 3.6k评论 6
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。