/
msg.rs
384 lines (352 loc) · 11.6 KB
/
msg.rs
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright 2016 TiKV Project Authors. Licensed under Apache-2.0.
use std::fmt;
use std::time::Instant;
use kvproto::import_sstpb::SSTMeta;
use kvproto::metapb;
use kvproto::metapb::RegionEpoch;
use kvproto::pdpb::CheckPolicy;
use kvproto::raft_cmdpb::{RaftCmdRequest, RaftCmdResponse};
use kvproto::raft_serverpb::RaftMessage;
use raft::SnapshotStatus;
use crate::raftstore::store::fsm::apply::TaskRes as ApplyTaskRes;
use crate::raftstore::store::util::KeysInfoFormatter;
use crate::raftstore::store::SnapKey;
use crate::storage::kv::CompactedEvent;
use tikv_util::escape;
use super::RegionSnapshot;
#[derive(Debug, Clone)]
pub struct ReadResponse {
pub response: RaftCmdResponse,
pub snapshot: Option<RegionSnapshot>,
}
#[derive(Debug)]
pub struct WriteResponse {
pub response: RaftCmdResponse,
}
pub type ReadCallback = Box<dyn FnOnce(ReadResponse) + Send>;
pub type WriteCallback = Box<dyn FnOnce(WriteResponse) + Send>;
/// Variants of callbacks for `Msg`.
/// - `Read`: a callbak for read only requests including `StatusRequest`,
/// `GetRequest` and `SnapRequest`
/// - `Write`: a callback for write only requests including `AdminRequest`
/// `PutRequest`, `DeleteRequest` and `DeleteRangeRequest`.
pub enum Callback {
/// No callback.
None,
/// Read callback.
Read(ReadCallback),
/// Write callback.
Write(WriteCallback),
}
impl Callback {
pub fn invoke_with_response(self, resp: RaftCmdResponse) {
match self {
Callback::None => (),
Callback::Read(read) => {
let resp = ReadResponse {
response: resp,
snapshot: None,
};
read(resp);
}
Callback::Write(write) => {
let resp = WriteResponse { response: resp };
write(resp);
}
}
}
pub fn invoke_read(self, args: ReadResponse) {
match self {
Callback::Read(read) => read(args),
other => panic!("expect Callback::Read(..), got {:?}", other),
}
}
pub fn is_none(&self) -> bool {
match self {
Callback::None => true,
_ => false,
}
}
}
impl fmt::Debug for Callback {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Callback::None => write!(fmt, "Callback::None"),
Callback::Read(_) => write!(fmt, "Callback::Read(..)"),
Callback::Write(_) => write!(fmt, "Callback::Write(..)"),
}
}
}
bitflags! {
pub struct PeerTicks: u8 {
const RAFT = 0b00000001;
const RAFT_LOG_GC = 0b00000010;
const SPLIT_REGION_CHECK = 0b00000100;
const PD_HEARTBEAT = 0b00001000;
const CHECK_MERGE = 0b00010000;
const CHECK_PEER_STALE_STATE = 0b00100000;
}
}
impl PeerTicks {
#[inline]
pub fn tag(self) -> &'static str {
match self {
PeerTicks::RAFT => "raft",
PeerTicks::RAFT_LOG_GC => "raft_log_gc",
PeerTicks::SPLIT_REGION_CHECK => "split_region_check",
PeerTicks::PD_HEARTBEAT => "pd_heartbeat",
PeerTicks::CHECK_MERGE => "check_merge",
PeerTicks::CHECK_PEER_STALE_STATE => "check_peer_stale_state",
_ => unreachable!(),
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum StoreTick {
CompactCheck,
PdStoreHeartbeat,
SnapGc,
CompactLockCf,
ConsistencyCheck,
CleanupImportSST,
}
impl StoreTick {
#[inline]
pub fn tag(self) -> &'static str {
match self {
StoreTick::CompactCheck => "compact_check",
StoreTick::PdStoreHeartbeat => "pd_store_heartbeat",
StoreTick::SnapGc => "snap_gc",
StoreTick::CompactLockCf => "compact_lock_cf",
StoreTick::ConsistencyCheck => "consistency_check",
StoreTick::CleanupImportSST => "cleanup_import_sst",
}
}
}
/// Some significant messages sent to raftstore. Raftstore will dispatch these messages to Raft
/// groups to update some important internal status.
#[derive(Debug, PartialEq)]
pub enum SignificantMsg {
/// Reports whether the snapshot sending is successful or not.
SnapshotStatus {
region_id: u64,
to_peer_id: u64,
status: SnapshotStatus,
},
StoreUnreachable {
store_id: u64,
},
/// Reports `to_peer_id` is unreachable.
Unreachable {
region_id: u64,
to_peer_id: u64,
},
}
/// Message that will be sent to a peer.
///
/// These messages are not significant and can be dropped occasionally.
pub enum CasualMessage {
/// Split the target region into several partitions.
SplitRegion {
region_epoch: RegionEpoch,
// It's an encoded key.
// TODO: support meta key.
split_keys: Vec<Vec<u8>>,
callback: Callback,
},
/// Hash result of ComputeHash command.
ComputeHashResult {
index: u64,
hash: Vec<u8>,
},
/// Approximate size of target region.
RegionApproximateSize {
size: u64,
},
/// Approximate key count of target region.
RegionApproximateKeys {
keys: u64,
},
CompactionDeclinedBytes {
bytes: u64,
},
/// Half split the target region.
HalfSplitRegion {
region_epoch: RegionEpoch,
policy: CheckPolicy,
},
/// Result of querying pd whether a region is merged.
MergeResult {
target: metapb::Peer,
stale: bool,
},
/// Remove snapshot files in `snaps`.
GcSnap {
snaps: Vec<(SnapKey, bool)>,
},
/// Clear region size cache.
ClearRegionSize,
/// Indicate a target region is overlapped.
RegionOverlapped,
/// Notifies that a new snapshot has been generated.
SnapshotGenerated,
}
impl fmt::Debug for CasualMessage {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CasualMessage::ComputeHashResult { index, ref hash } => write!(
fmt,
"ComputeHashResult [index: {}, hash: {}]",
index,
escape(hash)
),
CasualMessage::SplitRegion { ref split_keys, .. } => write!(
fmt,
"Split region with {}",
KeysInfoFormatter(split_keys.iter())
),
CasualMessage::RegionApproximateSize { size } => {
write!(fmt, "Region's approximate size [size: {:?}]", size)
}
CasualMessage::RegionApproximateKeys { keys } => {
write!(fmt, "Region's approximate keys [keys: {:?}]", keys)
}
CasualMessage::CompactionDeclinedBytes { bytes } => {
write!(fmt, "compaction declined bytes {}", bytes)
}
CasualMessage::HalfSplitRegion { .. } => write!(fmt, "Half Split"),
CasualMessage::MergeResult { target, stale } => write! {
fmt,
"target: {:?}, successful: {}",
target, stale
},
CasualMessage::GcSnap { ref snaps } => write! {
fmt,
"gc snaps {:?}",
snaps
},
CasualMessage::ClearRegionSize => write! {
fmt,
"clear region size"
},
CasualMessage::RegionOverlapped => write!(fmt, "RegionOverlapped"),
CasualMessage::SnapshotGenerated => write!(fmt, "SnapshotGenerated"),
}
}
}
/// Raft command is the command that is expected to be proposed by the
/// leader of the target raft group.
#[derive(Debug)]
pub struct RaftCommand {
pub send_time: Instant,
pub request: RaftCmdRequest,
pub callback: Callback,
}
impl RaftCommand {
#[inline]
pub fn new(request: RaftCmdRequest, callback: Callback) -> RaftCommand {
RaftCommand {
request,
callback,
send_time: Instant::now(),
}
}
}
/// Message that can be sent to a peer.
pub enum PeerMsg {
/// Raft message is the message sent between raft nodes in the same
/// raft group. Messages need to be redirected to raftstore if target
/// peer doesn't exist.
RaftMessage(RaftMessage),
/// Raft command is the command that is expected to be proposed by the
/// leader of the target raft group. If it's failed to be sent, callback
/// usually needs to be called before dropping in case of resource leak.
RaftCommand(RaftCommand),
/// Tick is periodical task. If target peer doesn't exist there is a potential
/// that the raft node will not work anymore.
Tick(PeerTicks),
/// Result of applying committed entries. The message can't be lost.
ApplyRes { res: ApplyTaskRes },
/// Message that can't be lost but rarely created. If they are lost, real bad
/// things happen like some peers will be considered dead in the group.
SignificantMsg(SignificantMsg),
/// Start the FSM.
Start,
/// A message only used to notify a peer.
Noop,
/// Message that is not important and can be dropped occasionally.
CasualMessage(CasualMessage),
/// Ask region to report a heartbeat to PD.
HeartbeatPd,
}
impl fmt::Debug for PeerMsg {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PeerMsg::RaftMessage(_) => write!(fmt, "Raft Message"),
PeerMsg::RaftCommand(_) => write!(fmt, "Raft Command"),
PeerMsg::Tick(tick) => write! {
fmt,
"{:?}",
tick
},
PeerMsg::SignificantMsg(msg) => write!(fmt, "{:?}", msg),
PeerMsg::ApplyRes { res } => write!(fmt, "ApplyRes {:?}", res),
PeerMsg::Start => write!(fmt, "Startup"),
PeerMsg::Noop => write!(fmt, "Noop"),
PeerMsg::CasualMessage(msg) => write!(fmt, "CasualMessage {:?}", msg),
PeerMsg::HeartbeatPd => write!(fmt, "HeartbeatPd"),
}
}
}
pub enum StoreMsg {
RaftMessage(RaftMessage),
// For snapshot stats.
SnapshotStats,
ValidateSSTResult {
invalid_ssts: Vec<SSTMeta>,
},
// Clear region size and keys for all regions in the range, so we can force them to re-calculate
// their size later.
ClearRegionSizeInRange {
start_key: Vec<u8>,
end_key: Vec<u8>,
},
StoreUnreachable {
store_id: u64,
},
// Compaction finished event
CompactedEvent(CompactedEvent),
Tick(StoreTick),
Start {
store: metapb::Store,
},
}
impl fmt::Debug for StoreMsg {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
StoreMsg::RaftMessage(_) => write!(fmt, "Raft Message"),
StoreMsg::SnapshotStats => write!(fmt, "Snapshot stats"),
StoreMsg::StoreUnreachable { store_id } => {
write!(fmt, "Store {} is unreachable", store_id)
}
StoreMsg::CompactedEvent(ref event) => write!(fmt, "CompactedEvent cf {}", event.cf),
StoreMsg::ValidateSSTResult { .. } => write!(fmt, "Validate SST Result"),
StoreMsg::ClearRegionSizeInRange {
ref start_key,
ref end_key,
} => write!(
fmt,
"Clear Region size in range {:?} to {:?}",
start_key, end_key
),
StoreMsg::Tick(tick) => write!(fmt, "StoreTick {:?}", tick),
StoreMsg::Start { ref store } => write!(fmt, "Start store {:?}", store),
}
}
}
// TODO: remove this enum and utilize the actual message instead.
#[derive(Debug)]
pub enum Msg {
PeerMsg(PeerMsg),
StoreMsg(StoreMsg),
}