-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinternal.go
More file actions
63 lines (52 loc) · 1.34 KB
/
internal.go
File metadata and controls
63 lines (52 loc) · 1.34 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
package adapter
import (
"context"
"github.com/bootjp/elastickv/kv"
pb "github.com/bootjp/elastickv/proto"
"github.com/cockroachdb/errors"
"github.com/hashicorp/raft"
)
func NewInternal(txm kv.Transactional, r *raft.Raft, clock *kv.HLC) *Internal {
return &Internal{
raft: r,
transactionManager: txm,
clock: clock,
}
}
type Internal struct {
raft *raft.Raft
transactionManager kv.Transactional
clock *kv.HLC
pb.UnimplementedInternalServer
}
var _ pb.InternalServer = (*Internal)(nil)
var ErrNotLeader = errors.New("not leader")
var ErrLeaderNotFound = errors.New("leader not found")
func (i *Internal) Forward(_ context.Context, req *pb.ForwardRequest) (*pb.ForwardResponse, error) {
if i.raft.State() != raft.Leader {
return nil, errors.WithStack(ErrNotLeader)
}
// Ensure leader issues start_ts when followers forward txn groups without it.
if req.IsTxn {
var startTs uint64
for _, r := range req.Requests {
if r.Ts == 0 {
if startTs == 0 {
startTs = i.clock.Next()
}
r.Ts = startTs
}
}
}
r, err := i.transactionManager.Commit(req.Requests)
if err != nil {
return &pb.ForwardResponse{
Success: false,
CommitIndex: 0,
}, errors.WithStack(err)
}
return &pb.ForwardResponse{
Success: true,
CommitIndex: r.CommitIndex,
}, nil
}