-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfsm.go
More file actions
172 lines (147 loc) · 4.04 KB
/
fsm.go
File metadata and controls
172 lines (147 loc) · 4.04 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package kv
import (
"context"
"io"
"log/slog"
"os"
pb "github.com/bootjp/elastickv/proto"
"github.com/bootjp/elastickv/store"
"github.com/cockroachdb/errors"
"github.com/hashicorp/raft"
"google.golang.org/protobuf/proto"
)
type kvFSM struct {
store store.MVCCStore
log *slog.Logger
}
type FSM interface {
raft.FSM
}
func NewKvFSM(store store.MVCCStore) FSM {
return &kvFSM{
store: store,
log: slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelWarn,
})),
}
}
var _ FSM = (*kvFSM)(nil)
var _ raft.FSM = (*kvFSM)(nil)
var ErrUnknownRequestType = errors.New("unknown request type")
func (f *kvFSM) Apply(l *raft.Log) interface{} {
ctx := context.TODO()
r := &pb.Request{}
err := proto.Unmarshal(l.Data, r)
if err != nil {
return errors.WithStack(err)
}
err = f.handleRequest(ctx, r, r.Ts)
if err != nil {
return errors.WithStack(err)
}
return nil
}
func (f *kvFSM) handleRequest(ctx context.Context, r *pb.Request, commitTS uint64) error {
switch {
case r.IsTxn:
return f.handleTxnRequest(ctx, r, commitTS)
default:
return f.handleRawRequest(ctx, r, commitTS)
}
}
func (f *kvFSM) handleRawRequest(ctx context.Context, r *pb.Request, commitTS uint64) error {
muts, err := toStoreMutations(r.Mutations)
if err != nil {
return errors.WithStack(err)
}
// Raw requests always commit against the latest state; use commitTS as both
// the validation snapshot and the commit timestamp.
return errors.WithStack(f.store.ApplyMutations(ctx, muts, commitTS, commitTS))
}
var ErrNotImplemented = errors.New("not implemented")
func (f *kvFSM) Snapshot() (raft.FSMSnapshot, error) {
buf, err := f.store.Snapshot()
if err != nil {
return nil, errors.WithStack(err)
}
return &kvFSMSnapshot{
buf,
}, nil
}
func (f *kvFSM) Restore(r io.ReadCloser) error {
defer r.Close()
return errors.WithStack(f.store.Restore(r))
}
func (f *kvFSM) handleTxnRequest(ctx context.Context, r *pb.Request, commitTS uint64) error {
switch r.Phase {
case pb.Phase_PREPARE:
return f.handlePrepareRequest(ctx, r)
case pb.Phase_COMMIT:
return f.handleCommitRequest(ctx, r, commitTS)
case pb.Phase_ABORT:
return f.handleAbortRequest(ctx, r)
case pb.Phase_NONE:
// not reached
return errors.WithStack(ErrUnknownRequestType)
default:
return errors.WithStack(ErrUnknownRequestType)
}
}
func (f *kvFSM) validateConflicts(ctx context.Context, muts []*pb.Mutation, startTS uint64) error {
seen := make(map[string]struct{}, len(muts))
for _, mut := range muts {
keyStr := string(mut.Key)
if _, ok := seen[keyStr]; ok {
continue
}
seen[keyStr] = struct{}{}
latest, exists, err := f.store.LatestCommitTS(ctx, mut.Key)
if err != nil {
return errors.WithStack(err)
}
if exists && latest > startTS {
return errors.Wrapf(store.ErrWriteConflict, "key: %s", string(mut.Key))
}
}
return nil
}
func (f *kvFSM) handlePrepareRequest(ctx context.Context, r *pb.Request) error {
err := f.validateConflicts(ctx, r.Mutations, r.Ts)
f.log.InfoContext(ctx, "handlePrepareRequest finish")
return errors.WithStack(err)
}
func (f *kvFSM) handleCommitRequest(ctx context.Context, r *pb.Request, commitTS uint64) error {
muts, err := toStoreMutations(r.Mutations)
if err != nil {
return errors.WithStack(err)
}
if err := f.validateConflicts(ctx, r.Mutations, r.Ts); err != nil {
return errors.WithStack(err)
}
return errors.WithStack(f.store.ApplyMutations(ctx, muts, r.Ts, commitTS))
}
func (f *kvFSM) handleAbortRequest(_ context.Context, _ *pb.Request) error {
// OCC does not rely on locks; abort is a no-op.
return nil
}
func toStoreMutations(muts []*pb.Mutation) ([]*store.KVPairMutation, error) {
out := make([]*store.KVPairMutation, 0, len(muts))
for _, mut := range muts {
switch mut.Op {
case pb.Op_PUT:
out = append(out, &store.KVPairMutation{
Op: store.OpTypePut,
Key: mut.Key,
Value: mut.Value,
})
case pb.Op_DEL:
out = append(out, &store.KVPairMutation{
Op: store.OpTypeDelete,
Key: mut.Key,
})
default:
return nil, ErrUnknownRequestType
}
}
return out, nil
}