forked from ngaut/unistore
/
read.go
120 lines (105 loc) · 3.17 KB
/
read.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
117
118
119
120
// Copyright 2019-present PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package raftstore
import (
"fmt"
stdatomic "sync/atomic"
"time"
"unsafe"
"github.com/ngaut/unistore/tikv/raftstore/raftlog"
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/errorpb"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/raft_cmdpb"
"github.com/uber-go/atomic"
)
type LeaderChecker interface {
IsLeader(ctx *kvrpcpb.Context, router *RaftstoreRouter) *errorpb.Error
}
type leaderChecker struct {
peerID uint64
invalid atomic.Bool
term atomic.Uint64
appliedIndexTerm atomic.Uint64
leaderLease unsafe.Pointer // *RemoteLease
region unsafe.Pointer // *metapb.Region
}
func (c *leaderChecker) IsLeader(ctx *kvrpcpb.Context, router *RaftstoreRouter) *errorpb.Error {
snapTime := time.Now()
isExpired, err := c.isExpired(ctx, &snapTime)
if err != nil {
return RaftstoreErrToPbError(err)
}
if !isExpired {
return nil
}
cb := NewCallback()
req := new(raft_cmdpb.Request)
req.CmdType = raft_cmdpb.CmdType_Snap
header := &raft_cmdpb.RaftRequestHeader{
RegionId: ctx.RegionId,
Peer: ctx.Peer,
RegionEpoch: ctx.RegionEpoch,
Term: ctx.Term,
SyncLog: ctx.SyncLog,
}
cmd := &raft_cmdpb.RaftCmdRequest{
Header: header,
Requests: []*raft_cmdpb.Request{req},
}
msg := &MsgRaftCmd{
SendTime: time.Now(),
Request: raftlog.NewRequest(cmd),
Callback: cb,
}
err = router.router.sendRaftCommand(msg)
if err != nil {
return RaftstoreErrToPbError(err)
}
cb.wg.Wait()
if cb.resp.Header.Error != nil {
return cb.resp.Header.Error
}
return nil
}
func (c *leaderChecker) isExpired(ctx *kvrpcpb.Context, snapTime *time.Time) (bool, error) {
if c.invalid.Load() {
return false, &ErrRegionNotFound{RegionId: ctx.RegionId}
}
peerID := c.peerID
term := c.term.Load()
region := (*metapb.Region)(stdatomic.LoadPointer(&c.region))
lease := (*RemoteLease)(stdatomic.LoadPointer(&c.leaderLease))
appliedIndexTerm := c.appliedIndexTerm.Load()
if ctx.Peer.Id != peerID {
return false, errors.Errorf("mismatch peer id %d != %d", ctx.Peer.Id, peerID)
}
if !(ctx.Term == 0 || term <= ctx.Term+1) {
return false, &ErrStaleCommand{}
}
if ctx.RegionEpoch == nil {
return false, errors.New("missing epoch")
}
// for get request, we don't care ConfVersion.
if ctx.RegionEpoch.Version != region.RegionEpoch.Version {
err := &ErrEpochNotMatch{}
err.Message = fmt.Sprintf("current epoch of region %d is %s, but you sent %s",
region.Id, region.RegionEpoch, ctx.RegionEpoch)
return false, err
}
if appliedIndexTerm != term {
return true, nil
}
return lease.Inspect(snapTime) == LeaseState_Expired, nil
}