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

Support rsm read #10

Merged
merged 1 commit into from
Mar 3, 2024
Merged
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
1 change: 1 addition & 0 deletions client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ TVoidTask Client(TPoller& poller, TSocket socket) {

TLine line;
TCommandRequest header;
header.Flags = TCommandRequest::EWrite;
header.Type = static_cast<uint32_t>(TCommandRequest::MessageType);
auto lineReader = TLineReader(input, 2*1024);
auto byteWriter = TByteWriter(socket);
Expand Down
2 changes: 1 addition & 1 deletion examples/kv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct TResultValue: public TCommandResponse {
char Data[0];
};

TMessageHolder<TMessage> TKv::Read(TMessageHolder<TCommandRequest> message) {
TMessageHolder<TMessage> TKv::Read(TMessageHolder<TCommandRequest> message, uint64_t index) {
auto readKv = message.Cast<TReadKv>();
std::string_view k(readKv->Data, readKv->KeySize);
auto it = H.find(std::string(k));
Expand Down
2 changes: 1 addition & 1 deletion examples/kv.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class TKv: public IRsm {
public:
TMessageHolder<TMessage> Read(TMessageHolder<TCommandRequest> message) override;
TMessageHolder<TMessage> Read(TMessageHolder<TCommandRequest> message, uint64_t index) override;
void Write(TMessageHolder<TLogEntry> message) override;
TMessageHolder<TLogEntry> Prepare(TMessageHolder<TCommandRequest> message, uint64_t term) override;

Expand Down
7 changes: 6 additions & 1 deletion src/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,15 @@ static_assert(sizeof(TAppendEntriesResponse) == sizeof(TMessageEx) + 16);

struct TCommandRequest: public TMessage {
static constexpr EMessageType MessageType = EMessageType::COMMAND_REQUEST;
enum EFlags {
ENone = 0,
EWrite = 1,
};
uint32_t Flags = ENone;
char Data[0];
};

static_assert(sizeof(TCommandRequest) == sizeof(TMessage));
static_assert(sizeof(TCommandRequest) == sizeof(TMessage) + 4);

struct TCommandResponse: public TMessage {
static constexpr EMessageType MessageType = EMessageType::COMMAND_RESPONSE;
Expand Down
21 changes: 14 additions & 7 deletions src/raft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ static uint32_t rand_(uint32_t* seed) {

} // namespace

TMessageHolder<TMessage> TDummyRsm::Read(TMessageHolder<TCommandRequest> message)
TMessageHolder<TMessage> TDummyRsm::Read(TMessageHolder<TCommandRequest> message, uint64_t index)
{
return {};
return NewHoldedMessage<TCommandResponse>(TCommandResponse {.Index = index});
}

void TDummyRsm::Write(TMessageHolder<TLogEntry> message)
Expand Down Expand Up @@ -253,12 +253,13 @@ void TRaft::OnAppendEntries(TMessageHolder<TAppendEntriesResponse> message) {

void TRaft::OnCommandRequest(TMessageHolder<TCommandRequest> command, const std::shared_ptr<INode>& replyTo) {
auto& log = State->Log;
auto entry = Rsm->Prepare(std::move(command), State->CurrentTerm);
log.emplace_back(std::move(entry));
if (command->Flags & TCommandRequest::EWrite) {
auto entry = Rsm->Prepare(std::move(command), State->CurrentTerm);
log.emplace_back(std::move(entry));
}
auto index = log.size();
if (replyTo) {
auto mes = NewHoldedMessage(TCommandResponse {.Index = index});
waiting.emplace(TWaiting{mes->Index, mes, replyTo});
waiting.emplace(TWaiting{index, std::move(command), replyTo});
}
}

Expand Down Expand Up @@ -378,7 +379,13 @@ void TRaft::ProcessWaiting() {
auto lastApplied = VolatileState->LastApplied;
while (!waiting.empty() && waiting.top().Index <= lastApplied) {
auto w = waiting.top(); waiting.pop();
w.ReplyTo->Send(std::move(w.Message));
TMessageHolder<TMessage> reply;
if (w.Command->Flags & TCommandRequest::EWrite) {
reply = NewHoldedMessage(TCommandResponse {.Index = w.Index});
} else {
reply = Rsm->Read(std::move(w.Command), w.Index);
}
w.ReplyTo->Send(std::move(reply));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/raft.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ struct INode {
// CommandRequest -> Read? -> CurrentIndex (fixate) >= CommittedIndex -> CommandResponse
struct IRsm {
virtual ~IRsm() = default;
virtual TMessageHolder<TMessage> Read(TMessageHolder<TCommandRequest> message) = 0;
virtual TMessageHolder<TMessage> Read(TMessageHolder<TCommandRequest> message, uint64_t index) = 0;
virtual void Write(TMessageHolder<TLogEntry> message) = 0;
virtual TMessageHolder<TLogEntry> Prepare(TMessageHolder<TCommandRequest> message, uint64_t term) = 0;
};

struct TDummyRsm: public IRsm {
TMessageHolder<TMessage> Read(TMessageHolder<TCommandRequest> message) override;
TMessageHolder<TMessage> Read(TMessageHolder<TCommandRequest> message, uint64_t index) override;
void Write(TMessageHolder<TLogEntry> message) override;
TMessageHolder<TLogEntry> Prepare(TMessageHolder<TCommandRequest> message, uint64_t term) override;
};
Expand Down Expand Up @@ -158,7 +158,7 @@ class TRaft {

struct TWaiting {
uint64_t Index;
TMessageHolder<TMessage> Message;
TMessageHolder<TCommandRequest> Command;
std::shared_ptr<INode> ReplyTo;
bool operator< (const TWaiting& other) const {
return Index > other.Index;
Expand Down
Loading