Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handles LogEntry::Retire #3

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ bytecodec = "0.4"
protobuf_codec = "0.2"
raftlog = "0.5"
trackable = "0.2"

[patch.crates-io]
raftlog = { path = "../raftlog" }
1 change: 1 addition & 0 deletions protobuf/log.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ message LogEntry {
oneof data {
bytes command = 2;
ClusterConfig config = 3;
string successor = 4;
}
}

Expand Down
22 changes: 12 additions & 10 deletions src/log.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
//! Encoders and decoders for the constituents defined in `raftlog::log` module.
use bytecodec::combinator::PreEncode;
use protobuf_codec::field::branch::Branch2;
use protobuf_codec::field::branch::Branch3;
use protobuf_codec::field::num::{F1, F2, F3, F4, F5};
use protobuf_codec::field::{
FieldDecoder, FieldEncoder, Fields, MaybeDefault, MessageFieldDecoder, MessageFieldEncoder,
Oneof, Optional,
};
use protobuf_codec::field::{FieldDecoder, FieldEncoder, Fields, MaybeDefault, MessageFieldDecoder, MessageFieldEncoder, Oneof, Optional};
use protobuf_codec::message::{MessageDecoder, MessageEncoder};
use protobuf_codec::scalar::{BytesDecoder, BytesEncoder, Uint64Decoder, Uint64Encoder};
use protobuf_codec::scalar::{BytesDecoder, BytesEncoder, Uint64Decoder, Uint64Encoder, StringDecoder, StringEncoder};
use raftlog::log::{LogEntry, LogPosition, LogPrefix};
use raftlog::node::NodeId;

use state::{ClusterConfigDecoder, ClusterConfigEncoder};

Expand All @@ -22,6 +20,7 @@ pub struct LogEntryDecoder {
Oneof<(
FieldDecoder<F2, BytesDecoder>,
MessageFieldDecoder<F3, ClusterConfigDecoder>,
FieldDecoder<F4, StringDecoder>,
)>,
>,
)>,
Expand All @@ -31,8 +30,9 @@ impl_message_decode!(LogEntryDecoder, LogEntry, |t: (u64, _)| {
let term = t.0.into();
Ok(match t.1 {
None => LogEntry::Noop { term },
Some(Branch2::A(command)) => LogEntry::Command { term, command },
Some(Branch2::B(config)) => LogEntry::Config { term, config },
Some(Branch3::A(command)) => LogEntry::Command { term, command },
Some(Branch3::B(config)) => LogEntry::Config { term, config },
Some(Branch3::C(successor)) => LogEntry::Retire { term, successor: NodeId::new(successor) },
})
});

Expand All @@ -46,15 +46,17 @@ pub struct LogEntryEncoder {
Oneof<(
FieldEncoder<F2, BytesEncoder>,
MessageFieldEncoder<F3, PreEncode<ClusterConfigEncoder>>,
FieldEncoder<F4, StringEncoder>,
)>,
>,
)>,
>,
}
impl_sized_message_encode!(LogEntryEncoder, LogEntry, |item: Self::Item| match item {
LogEntry::Noop { term } => (term.as_u64(), None),
LogEntry::Command { term, command } => (term.as_u64(), Some(Branch2::A(command))),
LogEntry::Config { term, config } => (term.as_u64(), Some(Branch2::B(config))),
LogEntry::Command { term, command } => (term.as_u64(), Some(Branch3::A(command))),
LogEntry::Config { term, config } => (term.as_u64(), Some(Branch3::B(config))),
LogEntry::Retire { term, successor } => (term.as_u64(), Some(Branch3::C(successor.into_string()))),
});

/// Decoder for `LogPrefix`.
Expand Down