pub struct ReplicatedLog<IO: Io> { /* private fields */ }Expand description
Raftアルゴリズムに基づく分散複製ログ.
利用者はpropose_commandメソッドを使って、コマンドをログに複製保存し、
発生するEventをハンドリングすることで、
整合性のある複製状態機械を実現することが可能となる.
ReplicatedLogはStreamトレイトを実装しているが、
これは無限ストリームであり、エラー時を除いて終了することはない.
ただし、構成変更によりノードがクラスタから切り離された場合は、
最終的には、イベントが生成されることは無くなる.
this.local_history().config().is_known_node()メソッドを使うことで、
クラスタ内に属しているかどうかは判定可能なので、利用者側が明示的に確認して、
不要になったReplicatedLogインスタンスを回収することは可能.
Implementations§
Source§impl<IO: Io> ReplicatedLog<IO>
impl<IO: Io> ReplicatedLog<IO>
Sourcepub fn new(
node_id: NodeId,
members: ClusterMembers,
io: IO,
metric_builder: &MetricBuilder,
) -> Result<Self>
pub fn new( node_id: NodeId, members: ClusterMembers, io: IO, metric_builder: &MetricBuilder, ) -> Result<Self>
membersで指定されたクラスタに属するReplicatedLogのローカルインスタンス(ノード)を生成する.
ローカルノードのIDはnode_idで指定するが、これがmembersの含まれている必要は必ずしもない.
例えば、クラスタの構成変更に伴い、新規ノードを追加したい場合には、
membersに現行構成を指定することが望ましいが、このケースでは、
node_idはmembersの中には含まれないことになる.
なお、ノードの再起動時を除いて、node_idには対象クラスタの歴史の中でユニークなIDを
割り当てるのが望ましい.
(レアケースではあるが、新規追加ノードを、以前に存在したノードと誤認識されてしまうと、
分散ログの整合性が壊れてしまう危険性があるため)
また、以前のノードを再起動したい場合でも、もし永続ストレージが壊れている等の理由で、 前回の状態を正確に復元できないのであれば、 ノード名を変更して、新規ノード追加扱いにした方が安全である.
Sourcepub fn metrics(&self) -> &Arc<RaftlogMetrics>
pub fn metrics(&self) -> &Arc<RaftlogMetrics>
raftlog のメトリクスを返す。
Sourcepub fn propose_command(&mut self, command: Vec<u8>) -> Result<ProposalId>
pub fn propose_command(&mut self, command: Vec<u8>) -> Result<ProposalId>
新しいコマンドを提案する.
提案が承認(コミット)された場合には、返り値のLogPositionを含む
Event::Committedイベントが返される.
もし返り値のLogPositionとは分岐したEvent::Committedが返された場合には、
この提案が棄却されたことを示している.
§Errors
非リーダノードに対して、このメソッドが実行された場合には、
ErrorKind::NotLeaderを理由としたエラーが返される.
Sourcepub fn propose_config(
&mut self,
new_members: ClusterMembers,
) -> Result<ProposalId>
pub fn propose_config( &mut self, new_members: ClusterMembers, ) -> Result<ProposalId>
新しいクラスタ構成(新メンバ群)を提案する.
提案が承認(コミット)された場合には、返り値のLogPositionを含む
Event::Committedイベントが返される.
ただし、承認された場合であっても、それは新旧混合状態の構成が承認されただけであり、
新メンバのみの構成への移行完了を把握したい場合には、後続のコミットイベントの
追跡を行う必要がある.
もし返り値のLogPositionとは分岐したEvent::Committedが返された場合には、
この提案が棄却されたことを示している.
複数の構成変更を並行して実施することは可能だが、 その場合は、最後に提案されたものが最終的な構成として採用される.
§Errors
非リーダノードに対して、このメソッドが実行された場合には、
ErrorKind::NotLeaderを理由としたエラーが返される.
Sourcepub fn heartbeat(&mut self) -> Result<SequenceNumber>
pub fn heartbeat(&mut self) -> Result<SequenceNumber>
強制的にハートビートメッセージ(i.e., AppendEntriesCall)をブロードキャストする.
返り値は、送信メッセージのシーケンス番号.
last_heartbeat_ackメソッドを用いることで、
このハートビートに対して、過半数以上の応答を得られた
タイミングを把握することが可能.
また、リーダのコミットを即座にフォロワーに伝えたい場合にも、
このメソッドが活用可能。
(Event::Committedをリーダが生成した直後にheartbeatメソッドを呼び出せば良い)
なおノードの役割が非リーダに変わった場合には、 応答待機中のハートビートは全て破棄されるので注意が必要.
§Errors
非リーダノードに対して、このメソッドが実行された場合には、
ErrorKind::NotLeaderを理由としたエラーが返される.
Sourcepub fn install_snapshot(
&mut self,
new_head: LogIndex,
snapshot: Vec<u8>,
) -> Result<()>
pub fn install_snapshot( &mut self, new_head: LogIndex, snapshot: Vec<u8>, ) -> Result<()>
ローカルログにスナップショットをインストールする.
new_headが新しいローカルログの先頭位置となり、
snapshotはその地点までのコマンド群が適用済みの状態機械のスナップショット、となる.
§Errors
既にローカルログに対するスナップショットのインストールが進行中の場合には、
ErrorKind::Busyを理由としてエラーが返される.
また現在のログの先頭よりも前の地点のスナップショットをインストールしようとした場合には、
ErrorKind::InvalidInputを理由としたエラーが返される.
Sourcepub fn start_election(&mut self)
pub fn start_election(&mut self)
新しい選挙を開始する.
何らかの手段で現在のリーダのダウンを検知した場合に呼び出される.
Sourcepub fn local_node(&self) -> &Node
pub fn local_node(&self) -> &Node
ローカルノードの情報を返す.
Sourcepub fn local_history(&self) -> &LogHistory
pub fn local_history(&self) -> &LogHistory
ローカルログの履歴を返す.
Sourcepub fn proposal_queue_len(&self) -> usize
pub fn proposal_queue_len(&self) -> usize
ローカルログへの書き込み待ちの状態の提案群の数を返す.
この値は、ローカルストレージの詰まり具合を把握するために有用である.
「ローカルログへは追記完了 and コミット待ち」の個数は
知りたい場合にはlocal_historyメソッド経由で取得可能.
ローカルノードが非リーダである場合には、常に0が返される.
Sourcepub fn is_snapshot_installing(&self) -> bool
pub fn is_snapshot_installing(&self) -> bool
スナップショットをインストール中の場合にはtrueを返す.
このメソッドがtrueを返している間は、
新しいスナップショットのインストールを行うことはできない.
Sourcepub fn last_heartbeat_ack(&self) -> SequenceNumber
pub fn last_heartbeat_ack(&self) -> SequenceNumber
過半数以上の応答を得られた最新のハートビート(i.e., AppendEntriesCall) のシーケンス番号を返す.
この値は、同じ選挙期間に関しては減少することはないことが保証されている.
§注意
ハートビートを行うのはリーダノードのみなので、それ以外のノードに関しては、 このメソッドが返す値は意味を持たない.
Sourcepub fn cluster_config(&self) -> &ClusterConfig
pub fn cluster_config(&self) -> &ClusterConfig
現在のクラスタ構成を返す.
Trait Implementations§
Source§impl<IO: Io> Stream for ReplicatedLog<IO>
impl<IO: Io> Stream for ReplicatedLog<IO>
Source§fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error>
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error>
None if
the stream is finished. Read moreSource§fn wait(self) -> Wait<Self>where
Self: Sized,
fn wait(self) -> Wait<Self>where
Self: Sized,
Source§fn into_future(self) -> StreamFuture<Self>where
Self: Sized,
fn into_future(self) -> StreamFuture<Self>where
Self: Sized,
Future. Read moreSource§fn filter<F>(self, f: F) -> Filter<Self, F>
fn filter<F>(self, f: F) -> Filter<Self, F>
Source§fn filter_map<F, B>(self, f: F) -> FilterMap<Self, F>
fn filter_map<F, B>(self, f: F) -> FilterMap<Self, F>
Source§fn then<F, U>(self, f: F) -> Then<Self, F, U>
fn then<F, U>(self, f: F) -> Then<Self, F, U>
f. Read moreSource§fn and_then<F, U>(self, f: F) -> AndThen<Self, F, U>
fn and_then<F, U>(self, f: F) -> AndThen<Self, F, U>
f. Read moreSource§fn or_else<F, U>(self, f: F) -> OrElse<Self, F, U>
fn or_else<F, U>(self, f: F) -> OrElse<Self, F, U>
f. Read moreSource§fn collect(self) -> Collect<Self>where
Self: Sized,
fn collect(self) -> Collect<Self>where
Self: Sized,
Source§fn fold<F, T, Fut>(self, init: T, f: F) -> Fold<Self, F, Fut, T>where
F: FnMut(T, Self::Item) -> Fut,
Fut: IntoFuture<Item = T>,
Self::Error: From<<Fut as IntoFuture>::Error>,
Self: Sized,
fn fold<F, T, Fut>(self, init: T, f: F) -> Fold<Self, F, Fut, T>where
F: FnMut(T, Self::Item) -> Fut,
Fut: IntoFuture<Item = T>,
Self::Error: From<<Fut as IntoFuture>::Error>,
Self: Sized,
Source§fn skip_while<P, R>(self, pred: P) -> SkipWhile<Self, P, R>
fn skip_while<P, R>(self, pred: P) -> SkipWhile<Self, P, R>
true. Read moreSource§fn take_while<P, R>(self, pred: P) -> TakeWhile<Self, P, R>
fn take_while<P, R>(self, pred: P) -> TakeWhile<Self, P, R>
true. Read moreSource§fn for_each<F, U>(self, f: F) -> ForEach<Self, F, U>
fn for_each<F, U>(self, f: F) -> ForEach<Self, F, U>
Source§fn from_err<E>(self) -> FromErr<Self, E>
fn from_err<E>(self) -> FromErr<Self, E>
From for
this stream’s Error, returning a new stream. Read moreSource§fn take(self, amt: u64) -> Take<Self>where
Self: Sized,
fn take(self, amt: u64) -> Take<Self>where
Self: Sized,
amt items of the underlying stream. Read moreSource§fn skip(self, amt: u64) -> Skip<Self>where
Self: Sized,
fn skip(self, amt: u64) -> Skip<Self>where
Self: Sized,
amt items of the underlying stream. Read moreSource§fn fuse(self) -> Fuse<Self>where
Self: Sized,
fn fuse(self) -> Fuse<Self>where
Self: Sized,
poll will never again be called once it has
finished. Read moreSource§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Source§fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
Source§fn merge<S>(self, other: S) -> Merge<Self, S>
fn merge<S>(self, other: S) -> Merge<Self, S>
select nowSource§fn zip<S>(self, other: S) -> Zip<Self, S>
fn zip<S>(self, other: S) -> Zip<Self, S>
Source§fn peekable(self) -> Peekable<Self>where
Self: Sized,
fn peekable(self) -> Peekable<Self>where
Self: Sized,
peek method. Read more