forked from goraft/raft
-
Notifications
You must be signed in to change notification settings - Fork 14
/
command.go
76 lines (64 loc) · 1.92 KB
/
command.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
package raft
import (
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"
)
var commandTypes map[string]Command
func init() {
commandTypes = map[string]Command{}
}
// Command represents an action to be taken on the replicated state machine.
type Command interface {
CommandName() string
}
// CommandApply represents the interface to apply a command to the server.
type CommandApply interface {
Apply(Context) (interface{}, error)
}
// deprecatedCommandApply represents the old interface to apply a command to the server.
type deprecatedCommandApply interface {
Apply(Server) (interface{}, error)
}
type CommandEncoder interface {
Encode(w io.Writer) error
Decode(r io.Reader) error
}
// Creates a new instance of a command by name.
func newCommand(name string, data []byte) (Command, error) {
// Find the registered command.
command := commandTypes[name]
if command == nil {
return nil, fmt.Errorf("raft.Command: Unregistered command type: %s", name)
}
// Make a copy of the command.
v := reflect.New(reflect.Indirect(reflect.ValueOf(command)).Type()).Interface()
copy, ok := v.(Command)
if !ok {
panic(fmt.Sprintf("raft: Unable to copy command: %s (%v)", command.CommandName(), reflect.ValueOf(v).Kind().String()))
}
// If data for the command was passed in the decode it.
if data != nil {
if encoder, ok := copy.(CommandEncoder); ok {
if err := encoder.Decode(bytes.NewReader(data)); err != nil {
return nil, err
}
} else {
if err := json.NewDecoder(bytes.NewReader(data)).Decode(copy); err != nil {
return nil, err
}
}
}
return copy, nil
}
// Registers a command by storing a reference to an instance of it.
func RegisterCommand(command Command) {
if command == nil {
panic(fmt.Sprintf("raft: Cannot register nil"))
} else if commandTypes[command.CommandName()] != nil {
panic(fmt.Sprintf("raft: Duplicate registration: %s", command.CommandName()))
}
commandTypes[command.CommandName()] = command
}