This repository has been archived by the owner on Sep 26, 2020. It is now read-only.
/
commands.go
116 lines (92 loc) · 3.27 KB
/
commands.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package fsm
import (
"fmt"
"github.com/nash-io/jocko/jocko/structs"
"github.com/nash-io/jocko/log"
)
func init() {
registerCommand(structs.RegisterNodeRequestType, (*FSM).applyRegisterNode)
registerCommand(structs.DeregisterNodeRequestType, (*FSM).applyDeregisterNode)
registerCommand(structs.RegisterTopicRequestType, (*FSM).applyRegisterTopic)
registerCommand(structs.DeregisterTopicRequestType, (*FSM).applyDeregisterTopic)
registerCommand(structs.RegisterPartitionRequestType, (*FSM).applyRegisterPartition)
registerCommand(structs.DeregisterPartitionRequestType, (*FSM).applyDeregisterPartition)
registerCommand(structs.RegisterGroupRequestType, (*FSM).applyRegisterGroup)
}
func (c *FSM) applyRegisterGroup(buf []byte, index uint64) interface{} {
var req structs.RegisterGroupRequest
if err := structs.Decode(buf, &req); err != nil {
panic(fmt.Errorf("failed to decode request: %v", err))
}
if err := c.state.EnsureGroup(index, &req.Group); err != nil {
log.Error.Printf("EnsureNode error: %s", err)
return err
}
return nil
}
func (c *FSM) applyRegisterNode(buf []byte, index uint64) interface{} {
var req structs.RegisterNodeRequest
if err := structs.Decode(buf, &req); err != nil {
panic(fmt.Errorf("failed to decode request: %v", err))
}
if err := c.state.EnsureNode(index, &req.Node); err != nil {
log.Error.Printf("EnsureNode error: %s", err)
return err
}
return nil
}
func (c *FSM) applyDeregisterNode(buf []byte, index uint64) interface{} {
var req structs.DeregisterNodeRequest
if err := structs.Decode(buf, &req); err != nil {
panic(fmt.Errorf("failed to decode request: %v", err))
}
if err := c.state.DeleteNode(index, req.Node.Node); err != nil {
log.Error.Printf("DeleteNode error: %s", err)
return err
}
return nil
}
func (c *FSM) applyRegisterTopic(buf []byte, index uint64) interface{} {
var req structs.RegisterTopicRequest
if err := structs.Decode(buf, &req); err != nil {
panic(fmt.Errorf("failed to decode request: %v", err))
}
if err := c.state.EnsureTopic(index, &req.Topic); err != nil {
log.Error.Printf("EnsureTopic error: %s", err)
return err
}
return nil
}
func (c *FSM) applyDeregisterTopic(buf []byte, index uint64) interface{} {
var req structs.DeregisterTopicRequest
if err := structs.Decode(buf, &req); err != nil {
panic(fmt.Errorf("failed to decode request: %v", err))
}
if err := c.state.DeleteTopic(index, req.Topic.Topic); err != nil {
log.Error.Printf("DeleteTopic error: %s", err)
return err
}
return nil
}
func (c *FSM) applyRegisterPartition(buf []byte, index uint64) interface{} {
var req structs.RegisterPartitionRequest
if err := structs.Decode(buf, &req); err != nil {
panic(fmt.Errorf("failed to decode request: %v", err))
}
if err := c.state.EnsurePartition(index, &req.Partition); err != nil {
log.Error.Printf("EnsurePartition error: %s", err)
return err
}
return nil
}
func (c *FSM) applyDeregisterPartition(buf []byte, index uint64) interface{} {
var req structs.DeregisterPartitionRequest
if err := structs.Decode(buf, &req); err != nil {
panic(fmt.Errorf("failed to decode request: %v", err))
}
if err := c.state.DeletePartition(index, req.Partition.Topic, req.Partition.Partition); err != nil {
log.Error.Printf("DeletePartition error: %s", err)
return err
}
return nil
}