This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraft_server.go
88 lines (80 loc) · 2.42 KB
/
raft_server.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
package arctonyx
import (
"context"
"github.com/kataras/go-errors"
)
// ClusterServiceServer is the server API for ClusterService service.
type clusterServer struct {
Store
}
func (server *clusterServer) GetSequenceChunk(ctx context.Context, request *SequenceChunkRequest) (*SequenceChunkResponse, error) {
return server.getSequenceChunk(request.SequenceName)
}
func (server *clusterServer) SendCommand(ctx context.Context, command *Command) (*CommandResponse, error) {
switch command.Operation {
case Operation_DELETE:
return server.serverDelete(*command)
case Operation_SET:
return server.serverSet(*command)
case Operation_GET:
return server.serverGet(*command)
default:
return nil, errors.New("could not handle operation %d").Format(command.Operation)
}
}
func (server *clusterServer) Join(ctx context.Context, join *JoinRequest) (*JoinResponse, error) {
response := &JoinResponse{}
if err := server.Store.join(join.Id, join.RaftAddress); err != nil {
response.IsSuccess = false
response.ErrorMessage = err.Error()
} else {
response.IsSuccess = true
}
return response, nil
}
func (server *clusterServer) GetNodeID(ctx context.Context, join *GetNodeIdRequest) (*GetNodeIdResponse, error) {
nodeId, err := server.NextSequenceValueById("_node_ids_")
if err != nil {
return nil, err
}
response := &GetNodeIdResponse{
NodeId:*nodeId,
}
return response, nil
}
func (server *clusterServer) serverGet(command Command) (*CommandResponse, error) {
response := &CommandResponse{
Operation:command.Operation,
}
value, err := server.Store.Get(command.Key)
if err != nil {
response.ErrorMessage = err.Error()
response.IsSuccess = false
} else {
response.Value = value
response.IsSuccess = true
}
return response, err
}
func (server *clusterServer) serverSet(command Command) (*CommandResponse, error) {
response := &CommandResponse{}
if err := server.Store.Set(command.Key, command.Value); err != nil {
response.ErrorMessage = err.Error()
response.IsSuccess = false
} else {
response.IsSuccess = true
}
response.Operation = command.Operation
return response, nil
}
func (server *clusterServer) serverDelete(command Command) (*CommandResponse, error) {
response := &CommandResponse{}
if err := server.Store.Delete(command.Key); err != nil {
response.ErrorMessage = err.Error()
response.IsSuccess = false
} else {
response.IsSuccess = true
}
response.Operation = command.Operation
return response, nil
}