forked from ngaut/unistore
/
msg.go
183 lines (152 loc) · 4.36 KB
/
msg.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
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
173
174
175
176
177
178
179
180
181
182
183
// 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 (
"sync"
"time"
"github.com/ngaut/unistore/tikv/raftstore/raftlog"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/raft_cmdpb"
"github.com/zhangjinpeng1987/raft"
)
type MsgType int64
const (
MsgTypeNull MsgType = 0
MsgTypeRaftMessage MsgType = 1
MsgTypeRaftCmd MsgType = 2
MsgTypeSplitRegion MsgType = 3
MsgTypeComputeResult MsgType = 4
MsgTypeRegionApproximateSize MsgType = 5
MsgTypeRegionApproximateKeys MsgType = 6
MsgTypeCompactionDeclineBytes MsgType = 7
MsgTypeHalfSplitRegion MsgType = 8
MsgTypeMergeResult MsgType = 9
MsgTypeGcSnap MsgType = 10
MsgTypeClearRegionSize MsgType = 11
MsgTypeTick MsgType = 12
MsgTypeSignificantMsg MsgType = 13
MsgTypeStart MsgType = 14
MsgTypeApplyRes MsgType = 15
MsgTypeNoop MsgType = 16
MsgTypeStoreRaftMessage MsgType = 101
MsgTypeStoreSnapshotStats MsgType = 102
// Clear region size and keys for all regions in the range, so we can force them to re-calculate
// their size later.
MsgTypeStoreClearRegionSizeInRange MsgType = 104
MsgTypeStoreCompactedEvent MsgType = 105
MsgTypeStoreTick MsgType = 106
MsgTypeStoreStart MsgType = 107
MsgTypeFsmNormal MsgType = 201
MsgTypeFsmControl MsgType = 202
MsgTypeApply MsgType = 301
MsgTypeApplyRegistration MsgType = 302
MsgTypeApplyProposal MsgType = 303
MsgTypeApplyCatchUpLogs MsgType = 304
MsgTypeApplyLogsUpToDate MsgType = 305
MsgTypeApplyDestroy MsgType = 306
MsgTypeApplySnapshot MsgType = 307
msgDefaultChanSize = 1024
)
type Msg struct {
Type MsgType
RegionID uint64
Data interface{}
}
func NewPeerMsg(tp MsgType, regionID uint64, data interface{}) Msg {
return Msg{Type: tp, RegionID: regionID, Data: data}
}
func NewMsg(tp MsgType, data interface{}) Msg {
return Msg{Type: tp, Data: data}
}
type Callback struct {
resp *raft_cmdpb.RaftCmdResponse
wg sync.WaitGroup
raftBeginTime time.Time
raftDoneTime time.Time
applyBeginTime time.Time
applyDoneTime time.Time
}
func (cb *Callback) Done(resp *raft_cmdpb.RaftCmdResponse) {
if cb != nil {
cb.resp = resp
cb.wg.Done()
}
}
func NewCallback() *Callback {
cb := &Callback{}
cb.wg.Add(1)
return cb
}
type PeerTick int
const (
PeerTickRaft PeerTick = 0
PeerTickRaftLogGC PeerTick = 1
PeerTickSplitRegionCheck PeerTick = 2
PeerTickPdHeartbeat PeerTick = 3
PeerTickCheckMerge PeerTick = 4
PeerTickPeerStaleState PeerTick = 5
)
type StoreTick int
const (
StoreTickCompactCheck StoreTick = 0
StoreTickPdStoreHeartbeat StoreTick = 1
StoreTickSnapGC StoreTick = 2
StoreTickConsistencyCheck StoreTick = 3
)
type MsgSignificantType int
const (
MsgSignificantTypeStatus MsgSignificantType = 1
MsgSignificantTypeUnreachable MsgSignificantType = 2
)
type MsgSignificant struct {
Type MsgSignificantType
ToPeerID uint64
SnapshotStatus raft.SnapshotStatus
}
type MsgRaftCmd struct {
SendTime time.Time
Request raftlog.RaftLog
Callback *Callback
}
type MsgSplitRegion struct {
RegionEpoch *metapb.RegionEpoch
// It's an encoded key.
// TODO: support meta key.
SplitKeys [][]byte
Callback *Callback
}
type MsgComputeHashResult struct {
Index uint64
Hash []byte
}
type MsgHalfSplitRegion struct {
RegionEpoch *metapb.RegionEpoch
}
type MsgMergeResult struct {
TargetPeer *metapb.Peer
Stale bool
}
type SnapKeyWithSending struct {
SnapKey SnapKey
IsSending bool
}
type MsgGCSnap struct {
Snaps []SnapKeyWithSending
}
type MsgStoreClearRegionSizeInRange struct {
StartKey []byte
EndKey []byte
}
func newApplyMsg(apply *apply) Msg {
return Msg{Type: MsgTypeApply, Data: apply}
}