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
use rt::WatchSender;
use crate::RaftTypeConfig;
use crate::errors::ReplicationClosed;
use crate::progress::stream_id::StreamId;
use crate::replication::replicate::Replicate;
use crate::replication::snapshot_transmitter_handle::SnapshotTransmitterHandle;
use crate::type_config::alias::JoinHandleOf;
use crate::type_config::alias::WatchSenderOf;
/// The handle to a spawned replication stream.
pub(crate) struct ReplicationHandle<C>
where C: RaftTypeConfig
{
/// Identifies the target's current progress entry and the replication stream that serves it.
/// See [`StreamId`] for the ID's lifetime.
pub(crate) stream_id: StreamId,
/// The node metadata that the stream's network client was created from.
///
/// A client may capture it, such as a connection address, so a stream is reused only for
/// the same metadata.
pub(crate) node: C::Node,
/// The log plan for the replication task to run.
///
/// It holds the plan that [`Command::Replicate`] or [`Command::ClearReplicate`] sent last, or
/// `None` before either has run.
///
/// [`Command::Replicate`]: crate::engine::Command::Replicate
/// [`Command::ClearReplicate`]: crate::engine::Command::ClearReplicate
pub(crate) replicate_tx: WatchSenderOf<C, Option<Replicate<C>>>,
/// Sender for the cancellation signal; dropping this stops replication.
pub(crate) cancel_tx: WatchSenderOf<C, ()>,
/// Sender for the backoff reset signal; sending on it ends the active backoff of replication.
pub(crate) backoff_reset_tx: WatchSenderOf<C, ()>,
/// The spawn handle of the `ReplicationCore` task.
pub(crate) join_handle: Option<JoinHandleOf<C, Result<(), ReplicationClosed>>>,
/// Handle to the snapshot transmitter task, if one is running.
pub(crate) snapshot_transmit_handle: Option<SnapshotTransmitterHandle<C>>,
}
impl<C> ReplicationHandle<C>
where C: RaftTypeConfig
{
pub(crate) fn new(
stream_id: StreamId,
node: C::Node,
replicate_tx: WatchSenderOf<C, Option<Replicate<C>>>,
cancel_tx: WatchSenderOf<C, ()>,
backoff_reset_tx: WatchSenderOf<C, ()>,
) -> Self {
Self {
stream_id,
node,
join_handle: None,
replicate_tx,
snapshot_transmit_handle: None,
cancel_tx,
backoff_reset_tx,
}
}
/// Signal the replication task to end its active backoff.
pub(crate) fn reset_backoff(&self) {
let send_res = self.backoff_reset_tx.send(());
if send_res.is_err() {
tracing::warn!(
"Failed to reset backoff for replication stream {}: channel closed",
self.stream_id
);
}
}
}