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
use crate::store::{CasualMessage, PeerMsg, RaftCommand, RaftRouter, StoreMsg};
use crate::{DiscardReason, Error, Result};
use crossbeam::channel::TrySendError;
use engine_traits::{KvEngine, RaftEngine, Snapshot};
use kvproto::raft_serverpb::RaftMessage;
use std::sync::mpsc;
pub trait Transport: Send + Clone {
fn send(&mut self, msg: RaftMessage) -> Result<()>;
fn need_flush(&self) -> bool;
fn flush(&mut self);
}
pub trait CasualRouter<EK>: Send
where
EK: KvEngine,
{
fn send(&self, region_id: u64, msg: CasualMessage<EK>) -> Result<()>;
}
pub trait ProposalRouter<S>
where
S: Snapshot,
{
fn send(&self, cmd: RaftCommand<S>) -> std::result::Result<(), TrySendError<RaftCommand<S>>>;
}
pub trait StoreRouter<EK>: Send
where
EK: KvEngine,
{
fn send(&self, msg: StoreMsg<EK>) -> Result<()>;
}
impl<EK, ER> CasualRouter<EK> for RaftRouter<EK, ER>
where
EK: KvEngine,
ER: RaftEngine,
{
#[inline]
fn send(&self, region_id: u64, msg: CasualMessage<EK>) -> Result<()> {
match self.router.send(region_id, PeerMsg::CasualMessage(msg)) {
Ok(()) => Ok(()),
Err(TrySendError::Full(_)) => Err(Error::Transport(DiscardReason::Full)),
Err(TrySendError::Disconnected(_)) => Err(Error::RegionNotFound(region_id)),
}
}
}
impl<EK, ER> ProposalRouter<EK::Snapshot> for RaftRouter<EK, ER>
where
EK: KvEngine,
ER: RaftEngine,
{
#[inline]
fn send(
&self,
cmd: RaftCommand<EK::Snapshot>,
) -> std::result::Result<(), TrySendError<RaftCommand<EK::Snapshot>>> {
self.send_raft_command(cmd)
}
}
impl<EK, ER> StoreRouter<EK> for RaftRouter<EK, ER>
where
EK: KvEngine,
ER: RaftEngine,
{
#[inline]
fn send(&self, msg: StoreMsg<EK>) -> Result<()> {
match self.send_control(msg) {
Ok(()) => Ok(()),
Err(TrySendError::Full(_)) => Err(Error::Transport(DiscardReason::Full)),
Err(TrySendError::Disconnected(_)) => {
Err(Error::Transport(DiscardReason::Disconnected))
}
}
}
}
impl<EK> CasualRouter<EK> for mpsc::SyncSender<(u64, CasualMessage<EK>)>
where
EK: KvEngine,
{
fn send(&self, region_id: u64, msg: CasualMessage<EK>) -> Result<()> {
match self.try_send((region_id, msg)) {
Ok(()) => Ok(()),
Err(mpsc::TrySendError::Disconnected(_)) => {
Err(Error::Transport(DiscardReason::Disconnected))
}
Err(mpsc::TrySendError::Full(_)) => Err(Error::Transport(DiscardReason::Full)),
}
}
}
impl<S: Snapshot> ProposalRouter<S> for mpsc::SyncSender<RaftCommand<S>> {
fn send(&self, cmd: RaftCommand<S>) -> std::result::Result<(), TrySendError<RaftCommand<S>>> {
match self.try_send(cmd) {
Ok(()) => Ok(()),
Err(mpsc::TrySendError::Disconnected(cmd)) => Err(TrySendError::Disconnected(cmd)),
Err(mpsc::TrySendError::Full(cmd)) => Err(TrySendError::Full(cmd)),
}
}
}
impl<EK> StoreRouter<EK> for mpsc::Sender<StoreMsg<EK>>
where
EK: KvEngine,
{
fn send(&self, msg: StoreMsg<EK>) -> Result<()> {
match self.send(msg) {
Ok(()) => Ok(()),
Err(mpsc::SendError(_)) => Err(Error::Transport(DiscardReason::Disconnected)),
}
}
}