From c6cbc2b57afa118ea06403164c503be99c30b11e Mon Sep 17 00:00:00 2001 From: baotiao Date: Wed, 12 Jul 2017 03:45:57 +0800 Subject: [PATCH 01/17] change advance commit index --- floyd/example/simple/read_floyd.cc | 8 +- floyd/example/simple/t.cc | 12 +- floyd/proto/floyd.proto | 30 +-- floyd/src/floyd.pb.cc | 317 ++++++++++++++++++-------- floyd/src/floyd.pb.h | 347 +++++++++++++++++++++-------- floyd/src/floyd_apply.cc | 30 +-- floyd/src/floyd_client_pool.cc | 32 +-- floyd/src/floyd_command.cc | 52 ++--- floyd/src/floyd_context.cc | 48 ++-- floyd/src/floyd_peer_thread.cc | 6 +- floyd/src/floyd_worker.cc | 14 +- floyd/src/raft_log.cc | 2 +- floyd/third/rocksdb | 2 +- floyd/tools/log_parse/floyd.pb.cc | 317 ++++++++++++++++++-------- floyd/tools/log_parse/floyd.pb.h | 347 +++++++++++++++++++++-------- 15 files changed, 1061 insertions(+), 503 deletions(-) diff --git a/floyd/example/simple/read_floyd.cc b/floyd/example/simple/read_floyd.cc index 2d37471..07b7645 100644 --- a/floyd/example/simple/read_floyd.cc +++ b/floyd/example/simple/read_floyd.cc @@ -34,14 +34,8 @@ int main(int argc, char** argv) continue; } entry.ParseFromString(iter->value().ToString()); - floyd::CmdRequest cmd_request; - cmd_request.ParseFromString(entry.cmd()); uint64_t num = BitStrToUint(iter->key().ToString()); - std::string text_format; - google::protobuf::TextFormat::PrintToString(cmd_request, &text_format); - printf("cmd_request :\n%s \n", text_format.c_str()); - - printf("key %lu entry term: %lu key %s value %s\n", num, entry.term(), cmd_request.kv().key().c_str(), cmd_request.kv().value().c_str()); + printf("key %lu entry term: %lu key %s value %s\n", num, entry.term(), entry.key().c_str(), entry.value().c_str()); // std::cout << "res " << iter->key().ToString() << ": " << iter->value().ToString() << std::endl; } printf("cnt %d\n", cnt); diff --git a/floyd/example/simple/t.cc b/floyd/example/simple/t.cc index a7a648c..9ae1290 100644 --- a/floyd/example/simple/t.cc +++ b/floyd/example/simple/t.cc @@ -55,12 +55,12 @@ int main() } while (i--) { - std::string mystr[100100]; - for (int i = 0; i < 100000; i++) { - mystr[i] = slash::RandomString(100); - } - // f1->GetServerStatus(msg); - // printf("%s\n", msg.c_str()); + std::string mystr[100100]; + for (int i = 0; i < 100000; i++) { + mystr[i] = slash::RandomString(100); + } + f1->GetServerStatus(msg); + printf("%s\n", msg.c_str()); st = NowMicros(); for (int j = 0; j < 100000; j++) { f1->Write(mystr[j], mystr[j]); diff --git a/floyd/proto/floyd.proto b/floyd/proto/floyd.proto index 5d48bbf..4a943b5 100644 --- a/floyd/proto/floyd.proto +++ b/floyd/proto/floyd.proto @@ -2,24 +2,28 @@ package floyd; enum Type { // User cmd - Read = 0; - ReadAll = 1; - DirtyWrite = 2; - Write = 3; - Delete = 4; - TryLock = 5; - UnLock = 6; - DeleteUser = 7; + kRead = 0; + kWrite = 1; + kDirtyWrite = 2; + kDelete = 3; // Raft RPC - RequestVote = 8; - AppendEntries = 9; - ServerStatus = 10; + kRequestVote = 8; + kAppendEntries = 9; + kServerStatus = 10; } + message Entry { + enum OpType { + kRead = 0; + kWrite = 1; + kDelete = 2; + } required uint64 term = 1; - optional bytes cmd = 2; + required string key = 2; + optional bytes value = 3; + required OpType optype = 4; } message CmdRequest { @@ -52,7 +56,7 @@ message CmdRequest { required uint64 term = 3; required uint64 prev_log_index = 4; required uint64 prev_log_term = 5; - required uint64 commit_index = 6; + required uint64 leader_commit = 6; repeated Entry entries = 7; } optional AppendEntries append_entries = 5; diff --git a/floyd/src/floyd.pb.cc b/floyd/src/floyd.pb.cc index 100fc50..9360af1 100644 --- a/floyd/src/floyd.pb.cc +++ b/floyd/src/floyd.pb.cc @@ -23,6 +23,7 @@ namespace { const ::google::protobuf::Descriptor* Entry_descriptor_ = NULL; const ::google::protobuf::internal::GeneratedMessageReflection* Entry_reflection_ = NULL; +const ::google::protobuf::EnumDescriptor* Entry_OpType_descriptor_ = NULL; const ::google::protobuf::Descriptor* CmdRequest_descriptor_ = NULL; const ::google::protobuf::internal::GeneratedMessageReflection* CmdRequest_reflection_ = NULL; @@ -72,9 +73,11 @@ void protobuf_AssignDesc_floyd_2eproto() { "floyd.proto"); GOOGLE_CHECK(file != NULL); Entry_descriptor_ = file->message_type(0); - static const int Entry_offsets_[2] = { + static const int Entry_offsets_[4] = { GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Entry, term_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Entry, cmd_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Entry, key_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Entry, value_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Entry, optype_), }; Entry_reflection_ = new ::google::protobuf::internal::GeneratedMessageReflection( @@ -87,6 +90,7 @@ void protobuf_AssignDesc_floyd_2eproto() { ::google::protobuf::DescriptorPool::generated_pool(), ::google::protobuf::MessageFactory::generated_factory(), sizeof(Entry)); + Entry_OpType_descriptor_ = Entry_descriptor_->enum_type(0); CmdRequest_descriptor_ = file->message_type(1); static const int CmdRequest_offsets_[6] = { GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest, type_), @@ -165,7 +169,7 @@ void protobuf_AssignDesc_floyd_2eproto() { GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, term_), GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, prev_log_index_), GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, prev_log_term_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, commit_index_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, leader_commit_), GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, entries_), }; CmdRequest_AppendEntries_reflection_ = @@ -386,50 +390,51 @@ void protobuf_AddDesc_floyd_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; ::google::protobuf::DescriptorPool::InternalAddGeneratedFile( - "\n\013floyd.proto\022\005floyd\"\"\n\005Entry\022\014\n\004term\030\001 " - "\002(\004\022\013\n\003cmd\030\002 \001(\014\"\252\005\n\nCmdRequest\022\031\n\004type\030" - "\001 \002(\0162\013.floyd.Type\022 \n\002kv\030\002 \001(\0132\024.floyd.C" - "mdRequest.Kv\022$\n\004user\030\003 \001(\0132\026.floyd.CmdRe" - "quest.User\0223\n\014request_vote\030\004 \001(\0132\035.floyd" - ".CmdRequest.RequestVote\0227\n\016append_entrie" - "s\030\005 \001(\0132\037.floyd.CmdRequest.AppendEntries" - "\0225\n\rserver_status\030\006 \001(\0132\036.floyd.CmdReque" - "st.ServerStatus\032 \n\002Kv\022\013\n\003key\030\001 \002(\014\022\r\n\005va" - "lue\030\002 \001(\014\032 \n\004User\022\n\n\002ip\030\001 \002(\014\022\014\n\004port\030\002 " - "\002(\005\032d\n\013RequestVote\022\n\n\002ip\030\001 \002(\014\022\014\n\004port\030\002" - " \002(\005\022\014\n\004term\030\003 \002(\004\022\025\n\rlast_log_term\030\004 \002(" - "\004\022\026\n\016last_log_index\030\005 \002(\004\032\233\001\n\rAppendEntr" - "ies\022\n\n\002ip\030\001 \002(\014\022\014\n\004port\030\002 \002(\005\022\014\n\004term\030\003 " - "\002(\004\022\026\n\016prev_log_index\030\004 \002(\004\022\025\n\rprev_log_" - "term\030\005 \002(\004\022\024\n\014commit_index\030\006 \002(\004\022\035\n\007entr" - "ies\030\007 \003(\0132\014.floyd.Entry\032L\n\014ServerStatus\022" - "\014\n\004term\030\001 \002(\003\022\024\n\014commit_index\030\002 \002(\003\022\n\n\002i" - "p\030\003 \001(\014\022\014\n\004port\030\004 \001(\005\"\206\006\n\013CmdResponse\022\031\n" - "\004type\030\001 \002(\0162\013.floyd.Type\022\037\n\004code\030\002 \001(\0162\021" - ".floyd.StatusCode\022\013\n\003msg\030\003 \001(\014\022!\n\002kv\030\004 \001" - "(\0132\025.floyd.CmdResponse.Kv\022#\n\003kvs\030\005 \001(\0132\026" - ".floyd.CmdResponse.Kvs\022@\n\020request_vote_r" - "es\030\006 \001(\0132&.floyd.CmdResponse.RequestVote" - "Response\022D\n\022append_entries_res\030\007 \001(\0132(.f" - "loyd.CmdResponse.AppendEntriesResponse\0226" - "\n\rserver_status\030\010 \001(\0132\037.floyd.CmdRespons" - "e.ServerStatus\032\023\n\002Kv\022\r\n\005value\030\001 \001(\014\032(\n\003K" - "vs\022!\n\002kv\030\001 \003(\0132\025.floyd.CmdResponse.Kv\0329\n" - "\023RequestVoteResponse\022\014\n\004term\030\001 \002(\004\022\024\n\014vo" - "te_granted\030\002 \002(\010\032N\n\025AppendEntriesRespons" - "e\022\014\n\004term\030\001 \002(\004\022\017\n\007success\030\002 \002(\010\022\026\n\016last" - "_log_index\030\003 \001(\004\032\333\001\n\014ServerStatus\022\014\n\004ter" - "m\030\001 \002(\004\022\024\n\014commit_index\030\002 \002(\004\022\014\n\004role\030\003 " - "\002(\014\022\021\n\tleader_ip\030\004 \001(\014\022\023\n\013leader_port\030\005 " - "\001(\005\022\024\n\014voted_for_ip\030\006 \001(\014\022\026\n\016voted_for_p" - "ort\030\007 \001(\005\022\025\n\rlast_log_term\030\010 \001(\004\022\026\n\016last" - "_log_index\030\t \001(\004\022\024\n\014last_applied\030\n \001(\004*\243" - "\001\n\004Type\022\010\n\004Read\020\000\022\013\n\007ReadAll\020\001\022\016\n\nDirtyW" - "rite\020\002\022\t\n\005Write\020\003\022\n\n\006Delete\020\004\022\013\n\007TryLock" - "\020\005\022\n\n\006UnLock\020\006\022\016\n\nDeleteUser\020\007\022\017\n\013Reques" - "tVote\020\010\022\021\n\rAppendEntries\020\t\022\020\n\014ServerStat" - "us\020\n*0\n\nStatusCode\022\007\n\003kOk\020\000\022\r\n\tkNotFound" - "\020\001\022\n\n\006kError\020\002", 1734); + "\n\013floyd.proto\022\005floyd\"\204\001\n\005Entry\022\014\n\004term\030\001" + " \002(\004\022\013\n\003key\030\002 \002(\t\022\r\n\005value\030\003 \001(\014\022#\n\006opty" + "pe\030\004 \002(\0162\023.floyd.Entry.OpType\",\n\006OpType\022" + "\t\n\005kRead\020\000\022\n\n\006kWrite\020\001\022\013\n\007kDelete\020\002\"\253\005\n\n" + "CmdRequest\022\031\n\004type\030\001 \002(\0162\013.floyd.Type\022 \n" + "\002kv\030\002 \001(\0132\024.floyd.CmdRequest.Kv\022$\n\004user\030" + "\003 \001(\0132\026.floyd.CmdRequest.User\0223\n\014request" + "_vote\030\004 \001(\0132\035.floyd.CmdRequest.RequestVo" + "te\0227\n\016append_entries\030\005 \001(\0132\037.floyd.CmdRe" + "quest.AppendEntries\0225\n\rserver_status\030\006 \001" + "(\0132\036.floyd.CmdRequest.ServerStatus\032 \n\002Kv" + "\022\013\n\003key\030\001 \002(\014\022\r\n\005value\030\002 \001(\014\032 \n\004User\022\n\n\002" + "ip\030\001 \002(\014\022\014\n\004port\030\002 \002(\005\032d\n\013RequestVote\022\n\n" + "\002ip\030\001 \002(\014\022\014\n\004port\030\002 \002(\005\022\014\n\004term\030\003 \002(\004\022\025\n" + "\rlast_log_term\030\004 \002(\004\022\026\n\016last_log_index\030\005" + " \002(\004\032\234\001\n\rAppendEntries\022\n\n\002ip\030\001 \002(\014\022\014\n\004po" + "rt\030\002 \002(\005\022\014\n\004term\030\003 \002(\004\022\026\n\016prev_log_index" + "\030\004 \002(\004\022\025\n\rprev_log_term\030\005 \002(\004\022\025\n\rleader_" + "commit\030\006 \002(\004\022\035\n\007entries\030\007 \003(\0132\014.floyd.En" + "try\032L\n\014ServerStatus\022\014\n\004term\030\001 \002(\003\022\024\n\014com" + "mit_index\030\002 \002(\003\022\n\n\002ip\030\003 \001(\014\022\014\n\004port\030\004 \001(" + "\005\"\206\006\n\013CmdResponse\022\031\n\004type\030\001 \002(\0162\013.floyd." + "Type\022\037\n\004code\030\002 \001(\0162\021.floyd.StatusCode\022\013\n" + "\003msg\030\003 \001(\014\022!\n\002kv\030\004 \001(\0132\025.floyd.CmdRespon" + "se.Kv\022#\n\003kvs\030\005 \001(\0132\026.floyd.CmdResponse.K" + "vs\022@\n\020request_vote_res\030\006 \001(\0132&.floyd.Cmd" + "Response.RequestVoteResponse\022D\n\022append_e" + "ntries_res\030\007 \001(\0132(.floyd.CmdResponse.App" + "endEntriesResponse\0226\n\rserver_status\030\010 \001(" + "\0132\037.floyd.CmdResponse.ServerStatus\032\023\n\002Kv" + "\022\r\n\005value\030\001 \001(\014\032(\n\003Kvs\022!\n\002kv\030\001 \003(\0132\025.flo" + "yd.CmdResponse.Kv\0329\n\023RequestVoteResponse" + "\022\014\n\004term\030\001 \002(\004\022\024\n\014vote_granted\030\002 \002(\010\032N\n\025" + "AppendEntriesResponse\022\014\n\004term\030\001 \002(\004\022\017\n\007s" + "uccess\030\002 \002(\010\022\026\n\016last_log_index\030\003 \001(\004\032\333\001\n" + "\014ServerStatus\022\014\n\004term\030\001 \002(\004\022\024\n\014commit_in" + "dex\030\002 \002(\004\022\014\n\004role\030\003 \002(\014\022\021\n\tleader_ip\030\004 \001" + "(\014\022\023\n\013leader_port\030\005 \001(\005\022\024\n\014voted_for_ip\030" + "\006 \001(\014\022\026\n\016voted_for_port\030\007 \001(\005\022\025\n\rlast_lo" + "g_term\030\010 \001(\004\022\026\n\016last_log_index\030\t \001(\004\022\024\n\014" + "last_applied\030\n \001(\004*t\n\004Type\022\t\n\005kRead\020\000\022\n\n" + "\006kWrite\020\001\022\017\n\013kDirtyWrite\020\002\022\013\n\007kDelete\020\003\022" + "\020\n\014kRequestVote\020\010\022\022\n\016kAppendEntries\020\t\022\021\n" + "\rkServerStatus\020\n*0\n\nStatusCode\022\007\n\003kOk\020\000\022" + "\r\n\tkNotFound\020\001\022\n\n\006kError\020\002", 1786); ::google::protobuf::MessageFactory::InternalRegisterGeneratedFile( "floyd.proto", &protobuf_RegisterTypes); Entry::default_instance_ = new Entry(); @@ -477,10 +482,6 @@ bool Type_IsValid(int value) { case 1: case 2: case 3: - case 4: - case 5: - case 6: - case 7: case 8: case 9: case 10: @@ -508,9 +509,34 @@ bool StatusCode_IsValid(int value) { // =================================================================== +const ::google::protobuf::EnumDescriptor* Entry_OpType_descriptor() { + protobuf_AssignDescriptorsOnce(); + return Entry_OpType_descriptor_; +} +bool Entry_OpType_IsValid(int value) { + switch(value) { + case 0: + case 1: + case 2: + return true; + default: + return false; + } +} + +#ifndef _MSC_VER +const Entry_OpType Entry::kRead; +const Entry_OpType Entry::kWrite; +const Entry_OpType Entry::kDelete; +const Entry_OpType Entry::OpType_MIN; +const Entry_OpType Entry::OpType_MAX; +const int Entry::OpType_ARRAYSIZE; +#endif // _MSC_VER #ifndef _MSC_VER const int Entry::kTermFieldNumber; -const int Entry::kCmdFieldNumber; +const int Entry::kKeyFieldNumber; +const int Entry::kValueFieldNumber; +const int Entry::kOptypeFieldNumber; #endif // !_MSC_VER Entry::Entry() @@ -530,7 +556,9 @@ Entry::Entry(const Entry& from) void Entry::SharedCtor() { _cached_size_ = 0; term_ = GOOGLE_ULONGLONG(0); - cmd_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + key_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + value_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + optype_ = 0; ::memset(_has_bits_, 0, sizeof(_has_bits_)); } @@ -539,8 +567,11 @@ Entry::~Entry() { } void Entry::SharedDtor() { - if (cmd_ != &::google::protobuf::internal::kEmptyString) { - delete cmd_; + if (key_ != &::google::protobuf::internal::kEmptyString) { + delete key_; + } + if (value_ != &::google::protobuf::internal::kEmptyString) { + delete value_; } if (this != default_instance_) { } @@ -570,11 +601,17 @@ Entry* Entry::New() const { void Entry::Clear() { if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { term_ = GOOGLE_ULONGLONG(0); - if (has_cmd()) { - if (cmd_ != &::google::protobuf::internal::kEmptyString) { - cmd_->clear(); + if (has_key()) { + if (key_ != &::google::protobuf::internal::kEmptyString) { + key_->clear(); } } + if (has_value()) { + if (value_ != &::google::protobuf::internal::kEmptyString) { + value_->clear(); + } + } + optype_ = 0; } ::memset(_has_bits_, 0, sizeof(_has_bits_)); mutable_unknown_fields()->Clear(); @@ -597,17 +634,55 @@ bool Entry::MergePartialFromCodedStream( } else { goto handle_uninterpreted; } - if (input->ExpectTag(18)) goto parse_cmd; + if (input->ExpectTag(18)) goto parse_key; break; } - // optional bytes cmd = 2; + // required string key = 2; case 2: { if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_cmd: + parse_key: + DO_(::google::protobuf::internal::WireFormatLite::ReadString( + input, this->mutable_key())); + ::google::protobuf::internal::WireFormat::VerifyUTF8String( + this->key().data(), this->key().length(), + ::google::protobuf::internal::WireFormat::PARSE); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(26)) goto parse_value; + break; + } + + // optional bytes value = 3; + case 3: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_value: DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->mutable_cmd())); + input, this->mutable_value())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(32)) goto parse_optype; + break; + } + + // required .floyd.Entry.OpType optype = 4; + case 4: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_optype: + int value; + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( + input, &value))); + if (::floyd::Entry_OpType_IsValid(value)) { + set_optype(static_cast< ::floyd::Entry_OpType >(value)); + } else { + mutable_unknown_fields()->AddVarint(4, value); + } } else { goto handle_uninterpreted; } @@ -638,10 +713,25 @@ void Entry::SerializeWithCachedSizes( ::google::protobuf::internal::WireFormatLite::WriteUInt64(1, this->term(), output); } - // optional bytes cmd = 2; - if (has_cmd()) { + // required string key = 2; + if (has_key()) { + ::google::protobuf::internal::WireFormat::VerifyUTF8String( + this->key().data(), this->key().length(), + ::google::protobuf::internal::WireFormat::SERIALIZE); + ::google::protobuf::internal::WireFormatLite::WriteString( + 2, this->key(), output); + } + + // optional bytes value = 3; + if (has_value()) { ::google::protobuf::internal::WireFormatLite::WriteBytes( - 2, this->cmd(), output); + 3, this->value(), output); + } + + // required .floyd.Entry.OpType optype = 4; + if (has_optype()) { + ::google::protobuf::internal::WireFormatLite::WriteEnum( + 4, this->optype(), output); } if (!unknown_fields().empty()) { @@ -657,11 +747,27 @@ ::google::protobuf::uint8* Entry::SerializeWithCachedSizesToArray( target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(1, this->term(), target); } - // optional bytes cmd = 2; - if (has_cmd()) { + // required string key = 2; + if (has_key()) { + ::google::protobuf::internal::WireFormat::VerifyUTF8String( + this->key().data(), this->key().length(), + ::google::protobuf::internal::WireFormat::SERIALIZE); + target = + ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + 2, this->key(), target); + } + + // optional bytes value = 3; + if (has_value()) { target = ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( - 2, this->cmd(), target); + 3, this->value(), target); + } + + // required .floyd.Entry.OpType optype = 4; + if (has_optype()) { + target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray( + 4, this->optype(), target); } if (!unknown_fields().empty()) { @@ -682,11 +788,24 @@ int Entry::ByteSize() const { this->term()); } - // optional bytes cmd = 2; - if (has_cmd()) { + // required string key = 2; + if (has_key()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::StringSize( + this->key()); + } + + // optional bytes value = 3; + if (has_value()) { total_size += 1 + ::google::protobuf::internal::WireFormatLite::BytesSize( - this->cmd()); + this->value()); + } + + // required .floyd.Entry.OpType optype = 4; + if (has_optype()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::EnumSize(this->optype()); } } @@ -719,8 +838,14 @@ void Entry::MergeFrom(const Entry& from) { if (from.has_term()) { set_term(from.term()); } - if (from.has_cmd()) { - set_cmd(from.cmd()); + if (from.has_key()) { + set_key(from.key()); + } + if (from.has_value()) { + set_value(from.value()); + } + if (from.has_optype()) { + set_optype(from.optype()); } } mutable_unknown_fields()->MergeFrom(from.unknown_fields()); @@ -739,7 +864,7 @@ void Entry::CopyFrom(const Entry& from) { } bool Entry::IsInitialized() const { - if ((_has_bits_[0] & 0x00000001) != 0x00000001) return false; + if ((_has_bits_[0] & 0x0000000b) != 0x0000000b) return false; return true; } @@ -747,7 +872,9 @@ bool Entry::IsInitialized() const { void Entry::Swap(Entry* other) { if (other != this) { std::swap(term_, other->term_); - std::swap(cmd_, other->cmd_); + std::swap(key_, other->key_); + std::swap(value_, other->value_); + std::swap(optype_, other->optype_); std::swap(_has_bits_[0], other->_has_bits_[0]); _unknown_fields_.Swap(&other->_unknown_fields_); std::swap(_cached_size_, other->_cached_size_); @@ -1670,7 +1797,7 @@ const int CmdRequest_AppendEntries::kPortFieldNumber; const int CmdRequest_AppendEntries::kTermFieldNumber; const int CmdRequest_AppendEntries::kPrevLogIndexFieldNumber; const int CmdRequest_AppendEntries::kPrevLogTermFieldNumber; -const int CmdRequest_AppendEntries::kCommitIndexFieldNumber; +const int CmdRequest_AppendEntries::kLeaderCommitFieldNumber; const int CmdRequest_AppendEntries::kEntriesFieldNumber; #endif // !_MSC_VER @@ -1695,7 +1822,7 @@ void CmdRequest_AppendEntries::SharedCtor() { term_ = GOOGLE_ULONGLONG(0); prev_log_index_ = GOOGLE_ULONGLONG(0); prev_log_term_ = GOOGLE_ULONGLONG(0); - commit_index_ = GOOGLE_ULONGLONG(0); + leader_commit_ = GOOGLE_ULONGLONG(0); ::memset(_has_bits_, 0, sizeof(_has_bits_)); } @@ -1743,7 +1870,7 @@ void CmdRequest_AppendEntries::Clear() { term_ = GOOGLE_ULONGLONG(0); prev_log_index_ = GOOGLE_ULONGLONG(0); prev_log_term_ = GOOGLE_ULONGLONG(0); - commit_index_ = GOOGLE_ULONGLONG(0); + leader_commit_ = GOOGLE_ULONGLONG(0); } entries_.Clear(); ::memset(_has_bits_, 0, sizeof(_has_bits_)); @@ -1829,19 +1956,19 @@ bool CmdRequest_AppendEntries::MergePartialFromCodedStream( } else { goto handle_uninterpreted; } - if (input->ExpectTag(48)) goto parse_commit_index; + if (input->ExpectTag(48)) goto parse_leader_commit; break; } - // required uint64 commit_index = 6; + // required uint64 leader_commit = 6; case 6: { if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { - parse_commit_index: + parse_leader_commit: DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( - input, &commit_index_))); - set_has_commit_index(); + input, &leader_commit_))); + set_has_leader_commit(); } else { goto handle_uninterpreted; } @@ -1908,9 +2035,9 @@ void CmdRequest_AppendEntries::SerializeWithCachedSizes( ::google::protobuf::internal::WireFormatLite::WriteUInt64(5, this->prev_log_term(), output); } - // required uint64 commit_index = 6; - if (has_commit_index()) { - ::google::protobuf::internal::WireFormatLite::WriteUInt64(6, this->commit_index(), output); + // required uint64 leader_commit = 6; + if (has_leader_commit()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(6, this->leader_commit(), output); } // repeated .floyd.Entry entries = 7; @@ -1954,9 +2081,9 @@ ::google::protobuf::uint8* CmdRequest_AppendEntries::SerializeWithCachedSizesToA target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(5, this->prev_log_term(), target); } - // required uint64 commit_index = 6; - if (has_commit_index()) { - target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(6, this->commit_index(), target); + // required uint64 leader_commit = 6; + if (has_leader_commit()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(6, this->leader_commit(), target); } // repeated .floyd.Entry entries = 7; @@ -2012,11 +2139,11 @@ int CmdRequest_AppendEntries::ByteSize() const { this->prev_log_term()); } - // required uint64 commit_index = 6; - if (has_commit_index()) { + // required uint64 leader_commit = 6; + if (has_leader_commit()) { total_size += 1 + ::google::protobuf::internal::WireFormatLite::UInt64Size( - this->commit_index()); + this->leader_commit()); } } @@ -2070,8 +2197,8 @@ void CmdRequest_AppendEntries::MergeFrom(const CmdRequest_AppendEntries& from) { if (from.has_prev_log_term()) { set_prev_log_term(from.prev_log_term()); } - if (from.has_commit_index()) { - set_commit_index(from.commit_index()); + if (from.has_leader_commit()) { + set_leader_commit(from.leader_commit()); } } mutable_unknown_fields()->MergeFrom(from.unknown_fields()); @@ -2105,7 +2232,7 @@ void CmdRequest_AppendEntries::Swap(CmdRequest_AppendEntries* other) { std::swap(term_, other->term_); std::swap(prev_log_index_, other->prev_log_index_); std::swap(prev_log_term_, other->prev_log_term_); - std::swap(commit_index_, other->commit_index_); + std::swap(leader_commit_, other->leader_commit_); entries_.Swap(&other->entries_); std::swap(_has_bits_[0], other->_has_bits_[0]); _unknown_fields_.Swap(&other->_unknown_fields_); diff --git a/floyd/src/floyd.pb.h b/floyd/src/floyd.pb.h index a278822..852d052 100644 --- a/floyd/src/floyd.pb.h +++ b/floyd/src/floyd.pb.h @@ -48,22 +48,38 @@ class CmdResponse_RequestVoteResponse; class CmdResponse_AppendEntriesResponse; class CmdResponse_ServerStatus; +enum Entry_OpType { + Entry_OpType_kRead = 0, + Entry_OpType_kWrite = 1, + Entry_OpType_kDelete = 2 +}; +bool Entry_OpType_IsValid(int value); +const Entry_OpType Entry_OpType_OpType_MIN = Entry_OpType_kRead; +const Entry_OpType Entry_OpType_OpType_MAX = Entry_OpType_kDelete; +const int Entry_OpType_OpType_ARRAYSIZE = Entry_OpType_OpType_MAX + 1; + +const ::google::protobuf::EnumDescriptor* Entry_OpType_descriptor(); +inline const ::std::string& Entry_OpType_Name(Entry_OpType value) { + return ::google::protobuf::internal::NameOfEnum( + Entry_OpType_descriptor(), value); +} +inline bool Entry_OpType_Parse( + const ::std::string& name, Entry_OpType* value) { + return ::google::protobuf::internal::ParseNamedEnum( + Entry_OpType_descriptor(), name, value); +} enum Type { - Read = 0, - ReadAll = 1, - DirtyWrite = 2, - Write = 3, - Delete = 4, - TryLock = 5, - UnLock = 6, - DeleteUser = 7, - RequestVote = 8, - AppendEntries = 9, - ServerStatus = 10 + kRead = 0, + kWrite = 1, + kDirtyWrite = 2, + kDelete = 3, + kRequestVote = 8, + kAppendEntries = 9, + kServerStatus = 10 }; bool Type_IsValid(int value); -const Type Type_MIN = Read; -const Type Type_MAX = ServerStatus; +const Type Type_MIN = kRead; +const Type Type_MAX = kServerStatus; const int Type_ARRAYSIZE = Type_MAX + 1; const ::google::protobuf::EnumDescriptor* Type_descriptor(); @@ -150,6 +166,31 @@ class Entry : public ::google::protobuf::Message { // nested types ---------------------------------------------------- + typedef Entry_OpType OpType; + static const OpType kRead = Entry_OpType_kRead; + static const OpType kWrite = Entry_OpType_kWrite; + static const OpType kDelete = Entry_OpType_kDelete; + static inline bool OpType_IsValid(int value) { + return Entry_OpType_IsValid(value); + } + static const OpType OpType_MIN = + Entry_OpType_OpType_MIN; + static const OpType OpType_MAX = + Entry_OpType_OpType_MAX; + static const int OpType_ARRAYSIZE = + Entry_OpType_OpType_ARRAYSIZE; + static inline const ::google::protobuf::EnumDescriptor* + OpType_descriptor() { + return Entry_OpType_descriptor(); + } + static inline const ::std::string& OpType_Name(OpType value) { + return Entry_OpType_Name(value); + } + static inline bool OpType_Parse(const ::std::string& name, + OpType* value) { + return Entry_OpType_Parse(name, value); + } + // accessors ------------------------------------------------------- // required uint64 term = 1; @@ -159,32 +200,57 @@ class Entry : public ::google::protobuf::Message { inline ::google::protobuf::uint64 term() const; inline void set_term(::google::protobuf::uint64 value); - // optional bytes cmd = 2; - inline bool has_cmd() const; - inline void clear_cmd(); - static const int kCmdFieldNumber = 2; - inline const ::std::string& cmd() const; - inline void set_cmd(const ::std::string& value); - inline void set_cmd(const char* value); - inline void set_cmd(const void* value, size_t size); - inline ::std::string* mutable_cmd(); - inline ::std::string* release_cmd(); - inline void set_allocated_cmd(::std::string* cmd); + // required string key = 2; + inline bool has_key() const; + inline void clear_key(); + static const int kKeyFieldNumber = 2; + inline const ::std::string& key() const; + inline void set_key(const ::std::string& value); + inline void set_key(const char* value); + inline void set_key(const char* value, size_t size); + inline ::std::string* mutable_key(); + inline ::std::string* release_key(); + inline void set_allocated_key(::std::string* key); + + // optional bytes value = 3; + inline bool has_value() const; + inline void clear_value(); + static const int kValueFieldNumber = 3; + inline const ::std::string& value() const; + inline void set_value(const ::std::string& value); + inline void set_value(const char* value); + inline void set_value(const void* value, size_t size); + inline ::std::string* mutable_value(); + inline ::std::string* release_value(); + inline void set_allocated_value(::std::string* value); + + // required .floyd.Entry.OpType optype = 4; + inline bool has_optype() const; + inline void clear_optype(); + static const int kOptypeFieldNumber = 4; + inline ::floyd::Entry_OpType optype() const; + inline void set_optype(::floyd::Entry_OpType value); // @@protoc_insertion_point(class_scope:floyd.Entry) private: inline void set_has_term(); inline void clear_has_term(); - inline void set_has_cmd(); - inline void clear_has_cmd(); + inline void set_has_key(); + inline void clear_has_key(); + inline void set_has_value(); + inline void clear_has_value(); + inline void set_has_optype(); + inline void clear_has_optype(); ::google::protobuf::UnknownFieldSet _unknown_fields_; ::google::protobuf::uint64 term_; - ::std::string* cmd_; + ::std::string* key_; + ::std::string* value_; + int optype_; mutable int _cached_size_; - ::google::protobuf::uint32 _has_bits_[(2 + 31) / 32]; + ::google::protobuf::uint32 _has_bits_[(4 + 31) / 32]; friend void protobuf_AddDesc_floyd_2eproto(); friend void protobuf_AssignDesc_floyd_2eproto(); @@ -615,12 +681,12 @@ class CmdRequest_AppendEntries : public ::google::protobuf::Message { inline ::google::protobuf::uint64 prev_log_term() const; inline void set_prev_log_term(::google::protobuf::uint64 value); - // required uint64 commit_index = 6; - inline bool has_commit_index() const; - inline void clear_commit_index(); - static const int kCommitIndexFieldNumber = 6; - inline ::google::protobuf::uint64 commit_index() const; - inline void set_commit_index(::google::protobuf::uint64 value); + // required uint64 leader_commit = 6; + inline bool has_leader_commit() const; + inline void clear_leader_commit(); + static const int kLeaderCommitFieldNumber = 6; + inline ::google::protobuf::uint64 leader_commit() const; + inline void set_leader_commit(::google::protobuf::uint64 value); // repeated .floyd.Entry entries = 7; inline int entries_size() const; @@ -646,8 +712,8 @@ class CmdRequest_AppendEntries : public ::google::protobuf::Message { inline void clear_has_prev_log_index(); inline void set_has_prev_log_term(); inline void clear_has_prev_log_term(); - inline void set_has_commit_index(); - inline void clear_has_commit_index(); + inline void set_has_leader_commit(); + inline void clear_has_leader_commit(); ::google::protobuf::UnknownFieldSet _unknown_fields_; @@ -655,7 +721,7 @@ class CmdRequest_AppendEntries : public ::google::protobuf::Message { ::google::protobuf::uint64 term_; ::google::protobuf::uint64 prev_log_index_; ::google::protobuf::uint64 prev_log_term_; - ::google::protobuf::uint64 commit_index_; + ::google::protobuf::uint64 leader_commit_; ::google::protobuf::RepeatedPtrField< ::floyd::Entry > entries_; ::google::protobuf::int32 port_; @@ -1689,76 +1755,169 @@ inline void Entry::set_term(::google::protobuf::uint64 value) { term_ = value; } -// optional bytes cmd = 2; -inline bool Entry::has_cmd() const { +// required string key = 2; +inline bool Entry::has_key() const { return (_has_bits_[0] & 0x00000002u) != 0; } -inline void Entry::set_has_cmd() { +inline void Entry::set_has_key() { _has_bits_[0] |= 0x00000002u; } -inline void Entry::clear_has_cmd() { +inline void Entry::clear_has_key() { _has_bits_[0] &= ~0x00000002u; } -inline void Entry::clear_cmd() { - if (cmd_ != &::google::protobuf::internal::kEmptyString) { - cmd_->clear(); +inline void Entry::clear_key() { + if (key_ != &::google::protobuf::internal::kEmptyString) { + key_->clear(); + } + clear_has_key(); +} +inline const ::std::string& Entry::key() const { + return *key_; +} +inline void Entry::set_key(const ::std::string& value) { + set_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + key_ = new ::std::string; + } + key_->assign(value); +} +inline void Entry::set_key(const char* value) { + set_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + key_ = new ::std::string; + } + key_->assign(value); +} +inline void Entry::set_key(const char* value, size_t size) { + set_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + key_ = new ::std::string; + } + key_->assign(reinterpret_cast(value), size); +} +inline ::std::string* Entry::mutable_key() { + set_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + key_ = new ::std::string; + } + return key_; +} +inline ::std::string* Entry::release_key() { + clear_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + return NULL; + } else { + ::std::string* temp = key_; + key_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + return temp; } - clear_has_cmd(); } -inline const ::std::string& Entry::cmd() const { - return *cmd_; +inline void Entry::set_allocated_key(::std::string* key) { + if (key_ != &::google::protobuf::internal::kEmptyString) { + delete key_; + } + if (key) { + set_has_key(); + key_ = key; + } else { + clear_has_key(); + key_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + } +} + +// optional bytes value = 3; +inline bool Entry::has_value() const { + return (_has_bits_[0] & 0x00000004u) != 0; +} +inline void Entry::set_has_value() { + _has_bits_[0] |= 0x00000004u; +} +inline void Entry::clear_has_value() { + _has_bits_[0] &= ~0x00000004u; } -inline void Entry::set_cmd(const ::std::string& value) { - set_has_cmd(); - if (cmd_ == &::google::protobuf::internal::kEmptyString) { - cmd_ = new ::std::string; +inline void Entry::clear_value() { + if (value_ != &::google::protobuf::internal::kEmptyString) { + value_->clear(); } - cmd_->assign(value); + clear_has_value(); +} +inline const ::std::string& Entry::value() const { + return *value_; } -inline void Entry::set_cmd(const char* value) { - set_has_cmd(); - if (cmd_ == &::google::protobuf::internal::kEmptyString) { - cmd_ = new ::std::string; +inline void Entry::set_value(const ::std::string& value) { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; } - cmd_->assign(value); + value_->assign(value); } -inline void Entry::set_cmd(const void* value, size_t size) { - set_has_cmd(); - if (cmd_ == &::google::protobuf::internal::kEmptyString) { - cmd_ = new ::std::string; +inline void Entry::set_value(const char* value) { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; } - cmd_->assign(reinterpret_cast(value), size); + value_->assign(value); } -inline ::std::string* Entry::mutable_cmd() { - set_has_cmd(); - if (cmd_ == &::google::protobuf::internal::kEmptyString) { - cmd_ = new ::std::string; +inline void Entry::set_value(const void* value, size_t size) { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; + } + value_->assign(reinterpret_cast(value), size); +} +inline ::std::string* Entry::mutable_value() { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; } - return cmd_; + return value_; } -inline ::std::string* Entry::release_cmd() { - clear_has_cmd(); - if (cmd_ == &::google::protobuf::internal::kEmptyString) { +inline ::std::string* Entry::release_value() { + clear_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { return NULL; } else { - ::std::string* temp = cmd_; - cmd_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + ::std::string* temp = value_; + value_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); return temp; } } -inline void Entry::set_allocated_cmd(::std::string* cmd) { - if (cmd_ != &::google::protobuf::internal::kEmptyString) { - delete cmd_; +inline void Entry::set_allocated_value(::std::string* value) { + if (value_ != &::google::protobuf::internal::kEmptyString) { + delete value_; } - if (cmd) { - set_has_cmd(); - cmd_ = cmd; + if (value) { + set_has_value(); + value_ = value; } else { - clear_has_cmd(); - cmd_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + clear_has_value(); + value_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); } } +// required .floyd.Entry.OpType optype = 4; +inline bool Entry::has_optype() const { + return (_has_bits_[0] & 0x00000008u) != 0; +} +inline void Entry::set_has_optype() { + _has_bits_[0] |= 0x00000008u; +} +inline void Entry::clear_has_optype() { + _has_bits_[0] &= ~0x00000008u; +} +inline void Entry::clear_optype() { + optype_ = 0; + clear_has_optype(); +} +inline ::floyd::Entry_OpType Entry::optype() const { + return static_cast< ::floyd::Entry_OpType >(optype_); +} +inline void Entry::set_optype(::floyd::Entry_OpType value) { + assert(::floyd::Entry_OpType_IsValid(value)); + set_has_optype(); + optype_ = value; +} + // ------------------------------------------------------------------- // CmdRequest_Kv @@ -2323,26 +2482,26 @@ inline void CmdRequest_AppendEntries::set_prev_log_term(::google::protobuf::uint prev_log_term_ = value; } -// required uint64 commit_index = 6; -inline bool CmdRequest_AppendEntries::has_commit_index() const { +// required uint64 leader_commit = 6; +inline bool CmdRequest_AppendEntries::has_leader_commit() const { return (_has_bits_[0] & 0x00000020u) != 0; } -inline void CmdRequest_AppendEntries::set_has_commit_index() { +inline void CmdRequest_AppendEntries::set_has_leader_commit() { _has_bits_[0] |= 0x00000020u; } -inline void CmdRequest_AppendEntries::clear_has_commit_index() { +inline void CmdRequest_AppendEntries::clear_has_leader_commit() { _has_bits_[0] &= ~0x00000020u; } -inline void CmdRequest_AppendEntries::clear_commit_index() { - commit_index_ = GOOGLE_ULONGLONG(0); - clear_has_commit_index(); +inline void CmdRequest_AppendEntries::clear_leader_commit() { + leader_commit_ = GOOGLE_ULONGLONG(0); + clear_has_leader_commit(); } -inline ::google::protobuf::uint64 CmdRequest_AppendEntries::commit_index() const { - return commit_index_; +inline ::google::protobuf::uint64 CmdRequest_AppendEntries::leader_commit() const { + return leader_commit_; } -inline void CmdRequest_AppendEntries::set_commit_index(::google::protobuf::uint64 value) { - set_has_commit_index(); - commit_index_ = value; +inline void CmdRequest_AppendEntries::set_leader_commit(::google::protobuf::uint64 value) { + set_has_leader_commit(); + leader_commit_ = value; } // repeated .floyd.Entry entries = 7; @@ -3635,6 +3794,10 @@ inline void CmdResponse::set_allocated_server_status(::floyd::CmdResponse_Server namespace google { namespace protobuf { +template <> +inline const EnumDescriptor* GetEnumDescriptor< ::floyd::Entry_OpType>() { + return ::floyd::Entry_OpType_descriptor(); +} template <> inline const EnumDescriptor* GetEnumDescriptor< ::floyd::Type>() { return ::floyd::Type_descriptor(); diff --git a/floyd/src/floyd_apply.cc b/floyd/src/floyd_apply.cc index b9e652b..97a884e 100644 --- a/floyd/src/floyd_apply.cc +++ b/floyd/src/floyd_apply.cc @@ -65,32 +65,22 @@ void FloydApply::ApplyStateMachine(void* arg) { fapply->raft_log_->UpdateLastApplied(to_apply - 1); } -Status FloydApply::Apply(const Entry& log_entry) { - const std::string& data = log_entry.cmd(); - CmdRequest cmd; - if (!cmd.ParseFromArray(data.c_str(), data.length())) { - std::string text_format; - google::protobuf::TextFormat::PrintToString(cmd, &text_format); - LOGV(WARN_LEVEL, context_->info_log(), "FloydApply:Apply :\n%s \n", text_format.c_str()); - LOGV(WARN_LEVEL, context_->info_log(), "Parse log_entry failed"); - return Status::IOError("Parse error"); - } - +Status FloydApply::Apply(const Entry& entry) { rocksdb::Status ret; - switch (cmd.type()) { - case Type::Write: - ret = db_->Put(rocksdb::WriteOptions(), cmd.kv().key(), cmd.kv().value()); - LOGV(DEBUG_LEVEL, context_->info_log(), "Floyd Apply Write %s, key(%s) value(%s)", - ret.ToString().c_str(), cmd.kv().key().c_str(), cmd.kv().value().c_str()); + switch (entry.optype()) { + case Entry_OpType_kWrite: + ret = db_->Put(rocksdb::WriteOptions(), entry.key(), entry.value()); + LOGV(DEBUG_LEVEL, context_->info_log(), "FloydApply::Apply %s, key(%s) value(%s)", + ret.ToString().c_str(), entry.key().c_str(), entry.value().c_str()); break; - case Type::Delete: - ret = db_->Delete(rocksdb::WriteOptions(), cmd.kv().key()); + case Entry_OpType_kDelete: + ret = db_->Delete(rocksdb::WriteOptions(), entry.key()); break; - case Type::Read: + case Entry_OpType_kRead: ret = rocksdb::Status::OK(); break; default: - ret = rocksdb::Status::Corruption("Unknown cmd type"); + ret = rocksdb::Status::Corruption("Unknown entry type"); } if (!ret.ok()) { return Status::Corruption(ret.ToString()); diff --git a/floyd/src/floyd_client_pool.cc b/floyd/src/floyd_client_pool.cc index a04da7c..46ec685 100644 --- a/floyd/src/floyd_client_pool.cc +++ b/floyd/src/floyd_client_pool.cc @@ -115,47 +115,31 @@ Status ClientPool::UpHoldCli(Client *client) { static std::string CmdType(const CmdRequest& cmd) { std::string ret; switch (cmd.type()) { - case Type::Read: { + case Type::kRead: { ret = "Read"; break; } - case Type::ReadAll: { - ret = "ReadAll"; + case Type::kWrite: { + ret = "Write"; break; } - case Type::DirtyWrite: { + case Type::kDirtyWrite: { ret = "DirtyWrite"; break; } - case Type::Write: { - ret = "Write"; - break; - } - case Type::Delete: { + case Type::kDelete: { ret = "Delete"; break; } - case Type::TryLock: { - ret = "TryLock"; - break; - } - case Type::UnLock: { - ret = "UnLock"; - break; - } - case Type::DeleteUser: { - ret = "DeleteUser"; - break; - } - case Type::RequestVote: { + case Type::kRequestVote: { ret = "RequestVote"; break; } - case Type::AppendEntries: { + case Type::kAppendEntries: { ret = "AppendEntries"; break; } - case Type::ServerStatus: { + case Type::kServerStatus: { ret = "ServerStatus"; break; } diff --git a/floyd/src/floyd_command.cc b/floyd/src/floyd_command.cc index ba251b9..8597aae 100644 --- a/floyd/src/floyd_command.cc +++ b/floyd/src/floyd_command.cc @@ -23,14 +23,13 @@ namespace floyd { static void BuildReadRequest(const std::string& key, CmdRequest* cmd) { - cmd->set_type(Type::Read); + cmd->set_type(Type::kRead); CmdRequest_Kv* kv = cmd->mutable_kv(); kv->set_key(key); } static void BuildReadResponse(const std::string &key, const std::string &value, StatusCode code, CmdResponse* response) { - //response->set_type(Type::Read); response->set_code(code); CmdResponse_Kv* kv = response->mutable_kv(); if (code == StatusCode::kOk) { @@ -40,7 +39,7 @@ static void BuildReadResponse(const std::string &key, const std::string &value, static void BuildWriteRequest(const std::string& key, const std::string& value, CmdRequest* cmd) { - cmd->set_type(Type::Write); + cmd->set_type(Type::kWrite); CmdRequest_Kv* kv = cmd->mutable_kv(); kv->set_key(key); kv->set_value(value); @@ -48,21 +47,21 @@ static void BuildWriteRequest(const std::string& key, static void BuildDirtyWriteRequest(const std::string& key, const std::string& value, CmdRequest* cmd) { - cmd->set_type(Type::DirtyWrite); + cmd->set_type(Type::kDirtyWrite); CmdRequest_Kv* kv = cmd->mutable_kv(); kv->set_key(key); kv->set_value(value); } static void BuildDeleteRequest(const std::string& key, CmdRequest* cmd) { - cmd->set_type(Type::Delete); + cmd->set_type(Type::kDelete); CmdRequest_Kv* kv = cmd->mutable_kv(); kv->set_key(key); } static void BuildRequestVoteResponse(uint64_t term, bool granted, CmdResponse* response) { - response->set_type(Type::RequestVote); + response->set_type(Type::kRequestVote); CmdResponse_RequestVoteResponse* request_vote_res = response->mutable_request_vote_res(); request_vote_res->set_term(term); request_vote_res->set_vote_granted(granted); @@ -71,21 +70,24 @@ static void BuildRequestVoteResponse(uint64_t term, bool granted, static void BuildAppendEntriesResponse(bool succ, uint64_t term, uint64_t log_index, CmdResponse* response) { - response->set_type(Type::AppendEntries); + response->set_type(Type::kAppendEntries); CmdResponse_AppendEntriesResponse* append_entries_res = response->mutable_append_entries_res(); append_entries_res->set_term(term); append_entries_res->set_last_log_index(log_index); append_entries_res->set_success(succ); } -static void BuildLogEntry(const CmdRequest& cmd, uint64_t current_term, - Entry* entry) { - uint64_t len = cmd.ByteSize(); - char* data = new char[len + 1]; - cmd.SerializeToArray(data, len); - entry->set_term(current_term); - entry->set_cmd(data, len); - delete data; +static void BuildLogEntry(const CmdRequest& cmd, Entry* entry) { + entry->set_term(cmd.append_entries().term()); + entry->set_key(cmd.kv().key()); + entry->set_value(cmd.kv().value()); + if (cmd.type() == Type::kRead) { + entry->set_optype(Entry_OpType_kRead); + } else if (cmd.type() == Type::kWrite || cmd.type() == Type::kDirtyWrite) { + entry->set_optype(Entry_OpType_kWrite); + } else if (cmd.type() == Type::kDelete) { + entry->set_optype(Entry_OpType_kDelete); + } } Status FloydImpl::Write(const std::string& key, const std::string& value) { @@ -199,7 +201,7 @@ bool FloydImpl::GetServerStatus(std::string& msg) { msg.append(str); CmdRequest cmd; - cmd.set_type(Type::ServerStatus); + cmd.set_type(Type::kServerStatus); CmdResponse response; std::string local_server = slash::IpPortString(options_.local_ip, options_.local_port); for (auto& iter : options_.members) { @@ -248,11 +250,11 @@ Status FloydImpl::ReplyExecuteDirtyCommand(const CmdRequest& cmd, std::string value; rocksdb::Status rs; switch (cmd.type()) { - case Type::DirtyWrite: { + case Type::kDirtyWrite: { rs = db_->Put(rocksdb::WriteOptions(), cmd.kv().key(), cmd.kv().value()); //TODO(anan) add response type or reorganize proto //response->set_type(CmdResponse::DirtyWrite); - response->set_type(Type::Write); + response->set_type(Type::kWrite); CmdResponse_Kv* kv = response->mutable_kv(); if (rs.ok()) { response->set_code(StatusCode::kOk); @@ -269,8 +271,8 @@ Status FloydImpl::ReplyExecuteDirtyCommand(const CmdRequest& cmd, #endif break; } - case Type::ServerStatus: { - response->set_type(Type::ServerStatus); + case Type::kServerStatus: { + response->set_type(Type::kServerStatus); response->set_code(StatusCode::kOk); CmdResponse_ServerStatus* server_status = response->mutable_server_status(); DoGetServerStatus(server_status); @@ -335,7 +337,7 @@ Status FloydImpl::ExecuteCommand(const CmdRequest& cmd, // Append entry local std::vector entries; Entry entry; - BuildLogEntry(cmd, context_->current_term(), &entry); + BuildLogEntry(cmd, &entry); entries.push_back(&entry); uint64_t last_log_index = raft_log_->Append(entries); @@ -362,15 +364,15 @@ Status FloydImpl::ExecuteCommand(const CmdRequest& cmd, std::string value; rocksdb::Status rs; switch (cmd.type()) { - case Type::Write: { + case Type::kWrite: { response->set_code(StatusCode::kOk); break; } - case Type::Delete: { + case Type::kDelete: { response->set_code(StatusCode::kOk); break; } - case Type::Read: { + case Type::kRead: { rs = db_->Get(rocksdb::ReadOptions(), cmd.kv().key(), &value); if (rs.ok()) { BuildReadResponse(cmd.kv().key(), value, StatusCode::kOk, response); @@ -458,7 +460,7 @@ void FloydImpl::ReplyAppendEntries(CmdRequest& cmd, CmdResponse* response) { entries, &my_term); // Update log commit index - if (context_->AdvanceCommitIndex(append_entries.commit_index())) { + if (context_->AdvanceCommitIndex(append_entries.leader_commit())) { LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries after AdvanceCommitIndex %lu", context_->commit_index()); apply_->ScheduleApply(); diff --git a/floyd/src/floyd_context.cc b/floyd/src/floyd_context.cc index 13ff39a..4c267b2 100644 --- a/floyd/src/floyd_context.cc +++ b/floyd/src/floyd_context.cc @@ -139,26 +139,30 @@ void FloydContext::BecomeLeader() { } bool FloydContext::AdvanceCommitIndex(uint64_t new_commit_index) { - if (new_commit_index == 0) { - return false; - } - slash::MutexLock l(&commit_mu_); - LOGV(DEBUG_LEVEL, info_log_, "FloydContext::AdvanceCommitIndex commit_index=%lu, new commit_index=%lu", - commit_index_, new_commit_index); - if (commit_index_ >= new_commit_index) { - return false; - } - - uint64_t last_log_index = raft_log_->GetLastLogIndex(); - new_commit_index = std::min(last_log_index, new_commit_index); - Entry entry; - raft_log_->GetEntry(new_commit_index, &entry); - if (entry.term() == current_term_) { - commit_index_ = new_commit_index; - LOGV(DEBUG_LEVEL, info_log_, "FloydContext::AdvanceCommitIndex advance commit_index to %ld", new_commit_index); - return true; - } - return false; + commit_index_ = new_commit_index; + return true; +/* + * if (new_commit_index == 0) { + * return false; + * } + * slash::MutexLock l(&commit_mu_); + * LOGV(DEBUG_LEVEL, info_log_, "FloydContext::AdvanceCommitIndex commit_index=%lu, new commit_index=%lu", + * commit_index_, new_commit_index); + * if (commit_index_ >= new_commit_index) { + * return false; + * } + * + * uint64_t last_log_index = raft_log_->GetLastLogIndex(); + * new_commit_index = std::min(last_log_index, new_commit_index); + * Entry entry; + * raft_log_->GetEntry(new_commit_index, &entry); + * if (entry.term() == current_term_) { + * commit_index_ = new_commit_index; + * LOGV(DEBUG_LEVEL, info_log_, "FloydContext::AdvanceCommitIndex advance commit_index to %ld", new_commit_index); + * return true; + * } + * return false; + */ } uint64_t FloydContext::NextApplyIndex(uint64_t* len) { @@ -207,7 +211,7 @@ bool FloydContext::ReceiverDoRequestVote(uint64_t term, const std::string ip, int port, uint64_t log_term, uint64_t log_index, uint64_t *my_term) { slash::RWLock l(&stat_rw_, true); - // TODO we don't need judge here, since before coming here, we have make sure + // TODO(ba0tiao) we don't need judge here, since before coming here, we have make sure // that term > current_term_ // if there is some call between the judge and here, the problem is floyd not // thread safe, add judge can't solve the problem. the right way to solve the @@ -263,7 +267,7 @@ bool FloydContext::ReceiverDoAppendEntries(uint64_t term, uint64_t my_log_term = 0; Entry entry; - LOGV(DEBUG_LEVEL, info_log_, "FloydContext::ReceiverDoAppendEntries %llu pre_log_index: \n", pre_log_index); + LOGV(DEBUG_LEVEL, info_log_, "FloydContext::ReceiverDoAppendEntries pre_log_index: %llu\n", pre_log_index); if (raft_log_->GetEntry(pre_log_index, &entry) == 0) { my_log_term = entry.term(); } else { diff --git a/floyd/src/floyd_peer_thread.cc b/floyd/src/floyd_peer_thread.cc index 28bcf88..9a7f667 100644 --- a/floyd/src/floyd_peer_thread.cc +++ b/floyd/src/floyd_peer_thread.cc @@ -81,7 +81,7 @@ Status Peer::RequestVoteRPC() { uint64_t current_term = context_->current_term(); CmdRequest req; - req.set_type(Type::RequestVote); + req.set_type(Type::kRequestVote); CmdRequest_RequestVote* request_vote = req.mutable_request_vote(); request_vote->set_ip(context_->local_ip()); request_vote->set_port(context_->local_port()); @@ -200,7 +200,7 @@ Status Peer::AppendEntriesRPC() { CmdRequest req; CmdRequest_AppendEntries* append_entries = req.mutable_append_entries(); - req.set_type(Type::AppendEntries); + req.set_type(Type::kAppendEntries); append_entries->set_ip(context_->local_ip()); append_entries->set_port(context_->local_port()); append_entries->set_term(context_->current_term()); @@ -231,7 +231,7 @@ Status Peer::AppendEntriesRPC() { * commit_index should be min of follower log and leader's commit_index * if follower's commit index larger than follower log, it conflict */ - append_entries->set_commit_index( + append_entries->set_leader_commit( std::min(context_->commit_index(), prev_log_index + num_entries)); CmdResponse res; diff --git a/floyd/src/floyd_worker.cc b/floyd/src/floyd_worker.cc index cf68e7a..e42686e 100644 --- a/floyd/src/floyd_worker.cc +++ b/floyd/src/floyd_worker.cc @@ -39,25 +39,25 @@ int FloydWorkerConn::DealMessage() { set_is_reply(true); switch (request_.type()) { - case Type::Write: - case Type::Delete: - case Type::Read: { + case Type::kWrite: + case Type::kDelete: + case Type::kRead: { LOGV(DEBUG_LEVEL, floyd_->info_log_, "WorkerConn::DealMessage Write/Delete/Read"); floyd_->DoCommand(request_, &response_); break; } - case Type::DirtyWrite: - case Type::ServerStatus: { + case Type::kDirtyWrite: + case Type::kServerStatus: { LOGV(DEBUG_LEVEL, floyd_->info_log_, "WorkerConn::DealMessage DirtyWrite/ServerStatus"); floyd_->ReplyExecuteDirtyCommand(request_, &response_); break; } - case Type::RequestVote: { + case Type::kRequestVote: { LOGV(DEBUG_LEVEL, floyd_->info_log_, "WorkerConn::DealMessage RequestVote"); floyd_->ReplyRequestVote(request_, &response_); break; } - case Type::AppendEntries: { + case Type::kAppendEntries: { LOGV(DEBUG_LEVEL, floyd_->info_log_, "WorkerConn::DealMessage AppendEntries"); floyd_->ReplyAppendEntries(request_, &response_); break; diff --git a/floyd/src/raft_log.cc b/floyd/src/raft_log.cc index 8bb6d2d..2f59bb9 100644 --- a/floyd/src/raft_log.cc +++ b/floyd/src/raft_log.cc @@ -68,7 +68,7 @@ uint64_t RaftLog::Append(const std::vector &entries) { slash::MutexLock l(&lli_mutex_); std::string buf; rocksdb::Status s; - LOGV(DEBUG_LEVEL, info_log_, "entries.size %lld", entries.size()); + LOGV(DEBUG_LEVEL, info_log_, "RaftLog::Append: entries.size %lld", entries.size()); for (size_t i = 0; i < entries.size(); i++) { entries[i]->SerializeToString(&buf); last_log_index_++; diff --git a/floyd/third/rocksdb b/floyd/third/rocksdb index e14eaa3..8e6345d 160000 --- a/floyd/third/rocksdb +++ b/floyd/third/rocksdb @@ -1 +1 @@ -Subproject commit e14eaa31fd2c211ecb1b0e5036afb539d89f7522 +Subproject commit 8e6345d2df40756c5ef62f3190cb152058fec890 diff --git a/floyd/tools/log_parse/floyd.pb.cc b/floyd/tools/log_parse/floyd.pb.cc index 100fc50..9360af1 100644 --- a/floyd/tools/log_parse/floyd.pb.cc +++ b/floyd/tools/log_parse/floyd.pb.cc @@ -23,6 +23,7 @@ namespace { const ::google::protobuf::Descriptor* Entry_descriptor_ = NULL; const ::google::protobuf::internal::GeneratedMessageReflection* Entry_reflection_ = NULL; +const ::google::protobuf::EnumDescriptor* Entry_OpType_descriptor_ = NULL; const ::google::protobuf::Descriptor* CmdRequest_descriptor_ = NULL; const ::google::protobuf::internal::GeneratedMessageReflection* CmdRequest_reflection_ = NULL; @@ -72,9 +73,11 @@ void protobuf_AssignDesc_floyd_2eproto() { "floyd.proto"); GOOGLE_CHECK(file != NULL); Entry_descriptor_ = file->message_type(0); - static const int Entry_offsets_[2] = { + static const int Entry_offsets_[4] = { GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Entry, term_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Entry, cmd_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Entry, key_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Entry, value_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Entry, optype_), }; Entry_reflection_ = new ::google::protobuf::internal::GeneratedMessageReflection( @@ -87,6 +90,7 @@ void protobuf_AssignDesc_floyd_2eproto() { ::google::protobuf::DescriptorPool::generated_pool(), ::google::protobuf::MessageFactory::generated_factory(), sizeof(Entry)); + Entry_OpType_descriptor_ = Entry_descriptor_->enum_type(0); CmdRequest_descriptor_ = file->message_type(1); static const int CmdRequest_offsets_[6] = { GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest, type_), @@ -165,7 +169,7 @@ void protobuf_AssignDesc_floyd_2eproto() { GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, term_), GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, prev_log_index_), GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, prev_log_term_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, commit_index_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, leader_commit_), GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, entries_), }; CmdRequest_AppendEntries_reflection_ = @@ -386,50 +390,51 @@ void protobuf_AddDesc_floyd_2eproto() { GOOGLE_PROTOBUF_VERIFY_VERSION; ::google::protobuf::DescriptorPool::InternalAddGeneratedFile( - "\n\013floyd.proto\022\005floyd\"\"\n\005Entry\022\014\n\004term\030\001 " - "\002(\004\022\013\n\003cmd\030\002 \001(\014\"\252\005\n\nCmdRequest\022\031\n\004type\030" - "\001 \002(\0162\013.floyd.Type\022 \n\002kv\030\002 \001(\0132\024.floyd.C" - "mdRequest.Kv\022$\n\004user\030\003 \001(\0132\026.floyd.CmdRe" - "quest.User\0223\n\014request_vote\030\004 \001(\0132\035.floyd" - ".CmdRequest.RequestVote\0227\n\016append_entrie" - "s\030\005 \001(\0132\037.floyd.CmdRequest.AppendEntries" - "\0225\n\rserver_status\030\006 \001(\0132\036.floyd.CmdReque" - "st.ServerStatus\032 \n\002Kv\022\013\n\003key\030\001 \002(\014\022\r\n\005va" - "lue\030\002 \001(\014\032 \n\004User\022\n\n\002ip\030\001 \002(\014\022\014\n\004port\030\002 " - "\002(\005\032d\n\013RequestVote\022\n\n\002ip\030\001 \002(\014\022\014\n\004port\030\002" - " \002(\005\022\014\n\004term\030\003 \002(\004\022\025\n\rlast_log_term\030\004 \002(" - "\004\022\026\n\016last_log_index\030\005 \002(\004\032\233\001\n\rAppendEntr" - "ies\022\n\n\002ip\030\001 \002(\014\022\014\n\004port\030\002 \002(\005\022\014\n\004term\030\003 " - "\002(\004\022\026\n\016prev_log_index\030\004 \002(\004\022\025\n\rprev_log_" - "term\030\005 \002(\004\022\024\n\014commit_index\030\006 \002(\004\022\035\n\007entr" - "ies\030\007 \003(\0132\014.floyd.Entry\032L\n\014ServerStatus\022" - "\014\n\004term\030\001 \002(\003\022\024\n\014commit_index\030\002 \002(\003\022\n\n\002i" - "p\030\003 \001(\014\022\014\n\004port\030\004 \001(\005\"\206\006\n\013CmdResponse\022\031\n" - "\004type\030\001 \002(\0162\013.floyd.Type\022\037\n\004code\030\002 \001(\0162\021" - ".floyd.StatusCode\022\013\n\003msg\030\003 \001(\014\022!\n\002kv\030\004 \001" - "(\0132\025.floyd.CmdResponse.Kv\022#\n\003kvs\030\005 \001(\0132\026" - ".floyd.CmdResponse.Kvs\022@\n\020request_vote_r" - "es\030\006 \001(\0132&.floyd.CmdResponse.RequestVote" - "Response\022D\n\022append_entries_res\030\007 \001(\0132(.f" - "loyd.CmdResponse.AppendEntriesResponse\0226" - "\n\rserver_status\030\010 \001(\0132\037.floyd.CmdRespons" - "e.ServerStatus\032\023\n\002Kv\022\r\n\005value\030\001 \001(\014\032(\n\003K" - "vs\022!\n\002kv\030\001 \003(\0132\025.floyd.CmdResponse.Kv\0329\n" - "\023RequestVoteResponse\022\014\n\004term\030\001 \002(\004\022\024\n\014vo" - "te_granted\030\002 \002(\010\032N\n\025AppendEntriesRespons" - "e\022\014\n\004term\030\001 \002(\004\022\017\n\007success\030\002 \002(\010\022\026\n\016last" - "_log_index\030\003 \001(\004\032\333\001\n\014ServerStatus\022\014\n\004ter" - "m\030\001 \002(\004\022\024\n\014commit_index\030\002 \002(\004\022\014\n\004role\030\003 " - "\002(\014\022\021\n\tleader_ip\030\004 \001(\014\022\023\n\013leader_port\030\005 " - "\001(\005\022\024\n\014voted_for_ip\030\006 \001(\014\022\026\n\016voted_for_p" - "ort\030\007 \001(\005\022\025\n\rlast_log_term\030\010 \001(\004\022\026\n\016last" - "_log_index\030\t \001(\004\022\024\n\014last_applied\030\n \001(\004*\243" - "\001\n\004Type\022\010\n\004Read\020\000\022\013\n\007ReadAll\020\001\022\016\n\nDirtyW" - "rite\020\002\022\t\n\005Write\020\003\022\n\n\006Delete\020\004\022\013\n\007TryLock" - "\020\005\022\n\n\006UnLock\020\006\022\016\n\nDeleteUser\020\007\022\017\n\013Reques" - "tVote\020\010\022\021\n\rAppendEntries\020\t\022\020\n\014ServerStat" - "us\020\n*0\n\nStatusCode\022\007\n\003kOk\020\000\022\r\n\tkNotFound" - "\020\001\022\n\n\006kError\020\002", 1734); + "\n\013floyd.proto\022\005floyd\"\204\001\n\005Entry\022\014\n\004term\030\001" + " \002(\004\022\013\n\003key\030\002 \002(\t\022\r\n\005value\030\003 \001(\014\022#\n\006opty" + "pe\030\004 \002(\0162\023.floyd.Entry.OpType\",\n\006OpType\022" + "\t\n\005kRead\020\000\022\n\n\006kWrite\020\001\022\013\n\007kDelete\020\002\"\253\005\n\n" + "CmdRequest\022\031\n\004type\030\001 \002(\0162\013.floyd.Type\022 \n" + "\002kv\030\002 \001(\0132\024.floyd.CmdRequest.Kv\022$\n\004user\030" + "\003 \001(\0132\026.floyd.CmdRequest.User\0223\n\014request" + "_vote\030\004 \001(\0132\035.floyd.CmdRequest.RequestVo" + "te\0227\n\016append_entries\030\005 \001(\0132\037.floyd.CmdRe" + "quest.AppendEntries\0225\n\rserver_status\030\006 \001" + "(\0132\036.floyd.CmdRequest.ServerStatus\032 \n\002Kv" + "\022\013\n\003key\030\001 \002(\014\022\r\n\005value\030\002 \001(\014\032 \n\004User\022\n\n\002" + "ip\030\001 \002(\014\022\014\n\004port\030\002 \002(\005\032d\n\013RequestVote\022\n\n" + "\002ip\030\001 \002(\014\022\014\n\004port\030\002 \002(\005\022\014\n\004term\030\003 \002(\004\022\025\n" + "\rlast_log_term\030\004 \002(\004\022\026\n\016last_log_index\030\005" + " \002(\004\032\234\001\n\rAppendEntries\022\n\n\002ip\030\001 \002(\014\022\014\n\004po" + "rt\030\002 \002(\005\022\014\n\004term\030\003 \002(\004\022\026\n\016prev_log_index" + "\030\004 \002(\004\022\025\n\rprev_log_term\030\005 \002(\004\022\025\n\rleader_" + "commit\030\006 \002(\004\022\035\n\007entries\030\007 \003(\0132\014.floyd.En" + "try\032L\n\014ServerStatus\022\014\n\004term\030\001 \002(\003\022\024\n\014com" + "mit_index\030\002 \002(\003\022\n\n\002ip\030\003 \001(\014\022\014\n\004port\030\004 \001(" + "\005\"\206\006\n\013CmdResponse\022\031\n\004type\030\001 \002(\0162\013.floyd." + "Type\022\037\n\004code\030\002 \001(\0162\021.floyd.StatusCode\022\013\n" + "\003msg\030\003 \001(\014\022!\n\002kv\030\004 \001(\0132\025.floyd.CmdRespon" + "se.Kv\022#\n\003kvs\030\005 \001(\0132\026.floyd.CmdResponse.K" + "vs\022@\n\020request_vote_res\030\006 \001(\0132&.floyd.Cmd" + "Response.RequestVoteResponse\022D\n\022append_e" + "ntries_res\030\007 \001(\0132(.floyd.CmdResponse.App" + "endEntriesResponse\0226\n\rserver_status\030\010 \001(" + "\0132\037.floyd.CmdResponse.ServerStatus\032\023\n\002Kv" + "\022\r\n\005value\030\001 \001(\014\032(\n\003Kvs\022!\n\002kv\030\001 \003(\0132\025.flo" + "yd.CmdResponse.Kv\0329\n\023RequestVoteResponse" + "\022\014\n\004term\030\001 \002(\004\022\024\n\014vote_granted\030\002 \002(\010\032N\n\025" + "AppendEntriesResponse\022\014\n\004term\030\001 \002(\004\022\017\n\007s" + "uccess\030\002 \002(\010\022\026\n\016last_log_index\030\003 \001(\004\032\333\001\n" + "\014ServerStatus\022\014\n\004term\030\001 \002(\004\022\024\n\014commit_in" + "dex\030\002 \002(\004\022\014\n\004role\030\003 \002(\014\022\021\n\tleader_ip\030\004 \001" + "(\014\022\023\n\013leader_port\030\005 \001(\005\022\024\n\014voted_for_ip\030" + "\006 \001(\014\022\026\n\016voted_for_port\030\007 \001(\005\022\025\n\rlast_lo" + "g_term\030\010 \001(\004\022\026\n\016last_log_index\030\t \001(\004\022\024\n\014" + "last_applied\030\n \001(\004*t\n\004Type\022\t\n\005kRead\020\000\022\n\n" + "\006kWrite\020\001\022\017\n\013kDirtyWrite\020\002\022\013\n\007kDelete\020\003\022" + "\020\n\014kRequestVote\020\010\022\022\n\016kAppendEntries\020\t\022\021\n" + "\rkServerStatus\020\n*0\n\nStatusCode\022\007\n\003kOk\020\000\022" + "\r\n\tkNotFound\020\001\022\n\n\006kError\020\002", 1786); ::google::protobuf::MessageFactory::InternalRegisterGeneratedFile( "floyd.proto", &protobuf_RegisterTypes); Entry::default_instance_ = new Entry(); @@ -477,10 +482,6 @@ bool Type_IsValid(int value) { case 1: case 2: case 3: - case 4: - case 5: - case 6: - case 7: case 8: case 9: case 10: @@ -508,9 +509,34 @@ bool StatusCode_IsValid(int value) { // =================================================================== +const ::google::protobuf::EnumDescriptor* Entry_OpType_descriptor() { + protobuf_AssignDescriptorsOnce(); + return Entry_OpType_descriptor_; +} +bool Entry_OpType_IsValid(int value) { + switch(value) { + case 0: + case 1: + case 2: + return true; + default: + return false; + } +} + +#ifndef _MSC_VER +const Entry_OpType Entry::kRead; +const Entry_OpType Entry::kWrite; +const Entry_OpType Entry::kDelete; +const Entry_OpType Entry::OpType_MIN; +const Entry_OpType Entry::OpType_MAX; +const int Entry::OpType_ARRAYSIZE; +#endif // _MSC_VER #ifndef _MSC_VER const int Entry::kTermFieldNumber; -const int Entry::kCmdFieldNumber; +const int Entry::kKeyFieldNumber; +const int Entry::kValueFieldNumber; +const int Entry::kOptypeFieldNumber; #endif // !_MSC_VER Entry::Entry() @@ -530,7 +556,9 @@ Entry::Entry(const Entry& from) void Entry::SharedCtor() { _cached_size_ = 0; term_ = GOOGLE_ULONGLONG(0); - cmd_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + key_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + value_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + optype_ = 0; ::memset(_has_bits_, 0, sizeof(_has_bits_)); } @@ -539,8 +567,11 @@ Entry::~Entry() { } void Entry::SharedDtor() { - if (cmd_ != &::google::protobuf::internal::kEmptyString) { - delete cmd_; + if (key_ != &::google::protobuf::internal::kEmptyString) { + delete key_; + } + if (value_ != &::google::protobuf::internal::kEmptyString) { + delete value_; } if (this != default_instance_) { } @@ -570,11 +601,17 @@ Entry* Entry::New() const { void Entry::Clear() { if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { term_ = GOOGLE_ULONGLONG(0); - if (has_cmd()) { - if (cmd_ != &::google::protobuf::internal::kEmptyString) { - cmd_->clear(); + if (has_key()) { + if (key_ != &::google::protobuf::internal::kEmptyString) { + key_->clear(); } } + if (has_value()) { + if (value_ != &::google::protobuf::internal::kEmptyString) { + value_->clear(); + } + } + optype_ = 0; } ::memset(_has_bits_, 0, sizeof(_has_bits_)); mutable_unknown_fields()->Clear(); @@ -597,17 +634,55 @@ bool Entry::MergePartialFromCodedStream( } else { goto handle_uninterpreted; } - if (input->ExpectTag(18)) goto parse_cmd; + if (input->ExpectTag(18)) goto parse_key; break; } - // optional bytes cmd = 2; + // required string key = 2; case 2: { if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_cmd: + parse_key: + DO_(::google::protobuf::internal::WireFormatLite::ReadString( + input, this->mutable_key())); + ::google::protobuf::internal::WireFormat::VerifyUTF8String( + this->key().data(), this->key().length(), + ::google::protobuf::internal::WireFormat::PARSE); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(26)) goto parse_value; + break; + } + + // optional bytes value = 3; + case 3: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_value: DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->mutable_cmd())); + input, this->mutable_value())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(32)) goto parse_optype; + break; + } + + // required .floyd.Entry.OpType optype = 4; + case 4: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_optype: + int value; + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( + input, &value))); + if (::floyd::Entry_OpType_IsValid(value)) { + set_optype(static_cast< ::floyd::Entry_OpType >(value)); + } else { + mutable_unknown_fields()->AddVarint(4, value); + } } else { goto handle_uninterpreted; } @@ -638,10 +713,25 @@ void Entry::SerializeWithCachedSizes( ::google::protobuf::internal::WireFormatLite::WriteUInt64(1, this->term(), output); } - // optional bytes cmd = 2; - if (has_cmd()) { + // required string key = 2; + if (has_key()) { + ::google::protobuf::internal::WireFormat::VerifyUTF8String( + this->key().data(), this->key().length(), + ::google::protobuf::internal::WireFormat::SERIALIZE); + ::google::protobuf::internal::WireFormatLite::WriteString( + 2, this->key(), output); + } + + // optional bytes value = 3; + if (has_value()) { ::google::protobuf::internal::WireFormatLite::WriteBytes( - 2, this->cmd(), output); + 3, this->value(), output); + } + + // required .floyd.Entry.OpType optype = 4; + if (has_optype()) { + ::google::protobuf::internal::WireFormatLite::WriteEnum( + 4, this->optype(), output); } if (!unknown_fields().empty()) { @@ -657,11 +747,27 @@ ::google::protobuf::uint8* Entry::SerializeWithCachedSizesToArray( target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(1, this->term(), target); } - // optional bytes cmd = 2; - if (has_cmd()) { + // required string key = 2; + if (has_key()) { + ::google::protobuf::internal::WireFormat::VerifyUTF8String( + this->key().data(), this->key().length(), + ::google::protobuf::internal::WireFormat::SERIALIZE); + target = + ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + 2, this->key(), target); + } + + // optional bytes value = 3; + if (has_value()) { target = ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( - 2, this->cmd(), target); + 3, this->value(), target); + } + + // required .floyd.Entry.OpType optype = 4; + if (has_optype()) { + target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray( + 4, this->optype(), target); } if (!unknown_fields().empty()) { @@ -682,11 +788,24 @@ int Entry::ByteSize() const { this->term()); } - // optional bytes cmd = 2; - if (has_cmd()) { + // required string key = 2; + if (has_key()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::StringSize( + this->key()); + } + + // optional bytes value = 3; + if (has_value()) { total_size += 1 + ::google::protobuf::internal::WireFormatLite::BytesSize( - this->cmd()); + this->value()); + } + + // required .floyd.Entry.OpType optype = 4; + if (has_optype()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::EnumSize(this->optype()); } } @@ -719,8 +838,14 @@ void Entry::MergeFrom(const Entry& from) { if (from.has_term()) { set_term(from.term()); } - if (from.has_cmd()) { - set_cmd(from.cmd()); + if (from.has_key()) { + set_key(from.key()); + } + if (from.has_value()) { + set_value(from.value()); + } + if (from.has_optype()) { + set_optype(from.optype()); } } mutable_unknown_fields()->MergeFrom(from.unknown_fields()); @@ -739,7 +864,7 @@ void Entry::CopyFrom(const Entry& from) { } bool Entry::IsInitialized() const { - if ((_has_bits_[0] & 0x00000001) != 0x00000001) return false; + if ((_has_bits_[0] & 0x0000000b) != 0x0000000b) return false; return true; } @@ -747,7 +872,9 @@ bool Entry::IsInitialized() const { void Entry::Swap(Entry* other) { if (other != this) { std::swap(term_, other->term_); - std::swap(cmd_, other->cmd_); + std::swap(key_, other->key_); + std::swap(value_, other->value_); + std::swap(optype_, other->optype_); std::swap(_has_bits_[0], other->_has_bits_[0]); _unknown_fields_.Swap(&other->_unknown_fields_); std::swap(_cached_size_, other->_cached_size_); @@ -1670,7 +1797,7 @@ const int CmdRequest_AppendEntries::kPortFieldNumber; const int CmdRequest_AppendEntries::kTermFieldNumber; const int CmdRequest_AppendEntries::kPrevLogIndexFieldNumber; const int CmdRequest_AppendEntries::kPrevLogTermFieldNumber; -const int CmdRequest_AppendEntries::kCommitIndexFieldNumber; +const int CmdRequest_AppendEntries::kLeaderCommitFieldNumber; const int CmdRequest_AppendEntries::kEntriesFieldNumber; #endif // !_MSC_VER @@ -1695,7 +1822,7 @@ void CmdRequest_AppendEntries::SharedCtor() { term_ = GOOGLE_ULONGLONG(0); prev_log_index_ = GOOGLE_ULONGLONG(0); prev_log_term_ = GOOGLE_ULONGLONG(0); - commit_index_ = GOOGLE_ULONGLONG(0); + leader_commit_ = GOOGLE_ULONGLONG(0); ::memset(_has_bits_, 0, sizeof(_has_bits_)); } @@ -1743,7 +1870,7 @@ void CmdRequest_AppendEntries::Clear() { term_ = GOOGLE_ULONGLONG(0); prev_log_index_ = GOOGLE_ULONGLONG(0); prev_log_term_ = GOOGLE_ULONGLONG(0); - commit_index_ = GOOGLE_ULONGLONG(0); + leader_commit_ = GOOGLE_ULONGLONG(0); } entries_.Clear(); ::memset(_has_bits_, 0, sizeof(_has_bits_)); @@ -1829,19 +1956,19 @@ bool CmdRequest_AppendEntries::MergePartialFromCodedStream( } else { goto handle_uninterpreted; } - if (input->ExpectTag(48)) goto parse_commit_index; + if (input->ExpectTag(48)) goto parse_leader_commit; break; } - // required uint64 commit_index = 6; + // required uint64 leader_commit = 6; case 6: { if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { - parse_commit_index: + parse_leader_commit: DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( - input, &commit_index_))); - set_has_commit_index(); + input, &leader_commit_))); + set_has_leader_commit(); } else { goto handle_uninterpreted; } @@ -1908,9 +2035,9 @@ void CmdRequest_AppendEntries::SerializeWithCachedSizes( ::google::protobuf::internal::WireFormatLite::WriteUInt64(5, this->prev_log_term(), output); } - // required uint64 commit_index = 6; - if (has_commit_index()) { - ::google::protobuf::internal::WireFormatLite::WriteUInt64(6, this->commit_index(), output); + // required uint64 leader_commit = 6; + if (has_leader_commit()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(6, this->leader_commit(), output); } // repeated .floyd.Entry entries = 7; @@ -1954,9 +2081,9 @@ ::google::protobuf::uint8* CmdRequest_AppendEntries::SerializeWithCachedSizesToA target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(5, this->prev_log_term(), target); } - // required uint64 commit_index = 6; - if (has_commit_index()) { - target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(6, this->commit_index(), target); + // required uint64 leader_commit = 6; + if (has_leader_commit()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(6, this->leader_commit(), target); } // repeated .floyd.Entry entries = 7; @@ -2012,11 +2139,11 @@ int CmdRequest_AppendEntries::ByteSize() const { this->prev_log_term()); } - // required uint64 commit_index = 6; - if (has_commit_index()) { + // required uint64 leader_commit = 6; + if (has_leader_commit()) { total_size += 1 + ::google::protobuf::internal::WireFormatLite::UInt64Size( - this->commit_index()); + this->leader_commit()); } } @@ -2070,8 +2197,8 @@ void CmdRequest_AppendEntries::MergeFrom(const CmdRequest_AppendEntries& from) { if (from.has_prev_log_term()) { set_prev_log_term(from.prev_log_term()); } - if (from.has_commit_index()) { - set_commit_index(from.commit_index()); + if (from.has_leader_commit()) { + set_leader_commit(from.leader_commit()); } } mutable_unknown_fields()->MergeFrom(from.unknown_fields()); @@ -2105,7 +2232,7 @@ void CmdRequest_AppendEntries::Swap(CmdRequest_AppendEntries* other) { std::swap(term_, other->term_); std::swap(prev_log_index_, other->prev_log_index_); std::swap(prev_log_term_, other->prev_log_term_); - std::swap(commit_index_, other->commit_index_); + std::swap(leader_commit_, other->leader_commit_); entries_.Swap(&other->entries_); std::swap(_has_bits_[0], other->_has_bits_[0]); _unknown_fields_.Swap(&other->_unknown_fields_); diff --git a/floyd/tools/log_parse/floyd.pb.h b/floyd/tools/log_parse/floyd.pb.h index a278822..852d052 100644 --- a/floyd/tools/log_parse/floyd.pb.h +++ b/floyd/tools/log_parse/floyd.pb.h @@ -48,22 +48,38 @@ class CmdResponse_RequestVoteResponse; class CmdResponse_AppendEntriesResponse; class CmdResponse_ServerStatus; +enum Entry_OpType { + Entry_OpType_kRead = 0, + Entry_OpType_kWrite = 1, + Entry_OpType_kDelete = 2 +}; +bool Entry_OpType_IsValid(int value); +const Entry_OpType Entry_OpType_OpType_MIN = Entry_OpType_kRead; +const Entry_OpType Entry_OpType_OpType_MAX = Entry_OpType_kDelete; +const int Entry_OpType_OpType_ARRAYSIZE = Entry_OpType_OpType_MAX + 1; + +const ::google::protobuf::EnumDescriptor* Entry_OpType_descriptor(); +inline const ::std::string& Entry_OpType_Name(Entry_OpType value) { + return ::google::protobuf::internal::NameOfEnum( + Entry_OpType_descriptor(), value); +} +inline bool Entry_OpType_Parse( + const ::std::string& name, Entry_OpType* value) { + return ::google::protobuf::internal::ParseNamedEnum( + Entry_OpType_descriptor(), name, value); +} enum Type { - Read = 0, - ReadAll = 1, - DirtyWrite = 2, - Write = 3, - Delete = 4, - TryLock = 5, - UnLock = 6, - DeleteUser = 7, - RequestVote = 8, - AppendEntries = 9, - ServerStatus = 10 + kRead = 0, + kWrite = 1, + kDirtyWrite = 2, + kDelete = 3, + kRequestVote = 8, + kAppendEntries = 9, + kServerStatus = 10 }; bool Type_IsValid(int value); -const Type Type_MIN = Read; -const Type Type_MAX = ServerStatus; +const Type Type_MIN = kRead; +const Type Type_MAX = kServerStatus; const int Type_ARRAYSIZE = Type_MAX + 1; const ::google::protobuf::EnumDescriptor* Type_descriptor(); @@ -150,6 +166,31 @@ class Entry : public ::google::protobuf::Message { // nested types ---------------------------------------------------- + typedef Entry_OpType OpType; + static const OpType kRead = Entry_OpType_kRead; + static const OpType kWrite = Entry_OpType_kWrite; + static const OpType kDelete = Entry_OpType_kDelete; + static inline bool OpType_IsValid(int value) { + return Entry_OpType_IsValid(value); + } + static const OpType OpType_MIN = + Entry_OpType_OpType_MIN; + static const OpType OpType_MAX = + Entry_OpType_OpType_MAX; + static const int OpType_ARRAYSIZE = + Entry_OpType_OpType_ARRAYSIZE; + static inline const ::google::protobuf::EnumDescriptor* + OpType_descriptor() { + return Entry_OpType_descriptor(); + } + static inline const ::std::string& OpType_Name(OpType value) { + return Entry_OpType_Name(value); + } + static inline bool OpType_Parse(const ::std::string& name, + OpType* value) { + return Entry_OpType_Parse(name, value); + } + // accessors ------------------------------------------------------- // required uint64 term = 1; @@ -159,32 +200,57 @@ class Entry : public ::google::protobuf::Message { inline ::google::protobuf::uint64 term() const; inline void set_term(::google::protobuf::uint64 value); - // optional bytes cmd = 2; - inline bool has_cmd() const; - inline void clear_cmd(); - static const int kCmdFieldNumber = 2; - inline const ::std::string& cmd() const; - inline void set_cmd(const ::std::string& value); - inline void set_cmd(const char* value); - inline void set_cmd(const void* value, size_t size); - inline ::std::string* mutable_cmd(); - inline ::std::string* release_cmd(); - inline void set_allocated_cmd(::std::string* cmd); + // required string key = 2; + inline bool has_key() const; + inline void clear_key(); + static const int kKeyFieldNumber = 2; + inline const ::std::string& key() const; + inline void set_key(const ::std::string& value); + inline void set_key(const char* value); + inline void set_key(const char* value, size_t size); + inline ::std::string* mutable_key(); + inline ::std::string* release_key(); + inline void set_allocated_key(::std::string* key); + + // optional bytes value = 3; + inline bool has_value() const; + inline void clear_value(); + static const int kValueFieldNumber = 3; + inline const ::std::string& value() const; + inline void set_value(const ::std::string& value); + inline void set_value(const char* value); + inline void set_value(const void* value, size_t size); + inline ::std::string* mutable_value(); + inline ::std::string* release_value(); + inline void set_allocated_value(::std::string* value); + + // required .floyd.Entry.OpType optype = 4; + inline bool has_optype() const; + inline void clear_optype(); + static const int kOptypeFieldNumber = 4; + inline ::floyd::Entry_OpType optype() const; + inline void set_optype(::floyd::Entry_OpType value); // @@protoc_insertion_point(class_scope:floyd.Entry) private: inline void set_has_term(); inline void clear_has_term(); - inline void set_has_cmd(); - inline void clear_has_cmd(); + inline void set_has_key(); + inline void clear_has_key(); + inline void set_has_value(); + inline void clear_has_value(); + inline void set_has_optype(); + inline void clear_has_optype(); ::google::protobuf::UnknownFieldSet _unknown_fields_; ::google::protobuf::uint64 term_; - ::std::string* cmd_; + ::std::string* key_; + ::std::string* value_; + int optype_; mutable int _cached_size_; - ::google::protobuf::uint32 _has_bits_[(2 + 31) / 32]; + ::google::protobuf::uint32 _has_bits_[(4 + 31) / 32]; friend void protobuf_AddDesc_floyd_2eproto(); friend void protobuf_AssignDesc_floyd_2eproto(); @@ -615,12 +681,12 @@ class CmdRequest_AppendEntries : public ::google::protobuf::Message { inline ::google::protobuf::uint64 prev_log_term() const; inline void set_prev_log_term(::google::protobuf::uint64 value); - // required uint64 commit_index = 6; - inline bool has_commit_index() const; - inline void clear_commit_index(); - static const int kCommitIndexFieldNumber = 6; - inline ::google::protobuf::uint64 commit_index() const; - inline void set_commit_index(::google::protobuf::uint64 value); + // required uint64 leader_commit = 6; + inline bool has_leader_commit() const; + inline void clear_leader_commit(); + static const int kLeaderCommitFieldNumber = 6; + inline ::google::protobuf::uint64 leader_commit() const; + inline void set_leader_commit(::google::protobuf::uint64 value); // repeated .floyd.Entry entries = 7; inline int entries_size() const; @@ -646,8 +712,8 @@ class CmdRequest_AppendEntries : public ::google::protobuf::Message { inline void clear_has_prev_log_index(); inline void set_has_prev_log_term(); inline void clear_has_prev_log_term(); - inline void set_has_commit_index(); - inline void clear_has_commit_index(); + inline void set_has_leader_commit(); + inline void clear_has_leader_commit(); ::google::protobuf::UnknownFieldSet _unknown_fields_; @@ -655,7 +721,7 @@ class CmdRequest_AppendEntries : public ::google::protobuf::Message { ::google::protobuf::uint64 term_; ::google::protobuf::uint64 prev_log_index_; ::google::protobuf::uint64 prev_log_term_; - ::google::protobuf::uint64 commit_index_; + ::google::protobuf::uint64 leader_commit_; ::google::protobuf::RepeatedPtrField< ::floyd::Entry > entries_; ::google::protobuf::int32 port_; @@ -1689,76 +1755,169 @@ inline void Entry::set_term(::google::protobuf::uint64 value) { term_ = value; } -// optional bytes cmd = 2; -inline bool Entry::has_cmd() const { +// required string key = 2; +inline bool Entry::has_key() const { return (_has_bits_[0] & 0x00000002u) != 0; } -inline void Entry::set_has_cmd() { +inline void Entry::set_has_key() { _has_bits_[0] |= 0x00000002u; } -inline void Entry::clear_has_cmd() { +inline void Entry::clear_has_key() { _has_bits_[0] &= ~0x00000002u; } -inline void Entry::clear_cmd() { - if (cmd_ != &::google::protobuf::internal::kEmptyString) { - cmd_->clear(); +inline void Entry::clear_key() { + if (key_ != &::google::protobuf::internal::kEmptyString) { + key_->clear(); + } + clear_has_key(); +} +inline const ::std::string& Entry::key() const { + return *key_; +} +inline void Entry::set_key(const ::std::string& value) { + set_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + key_ = new ::std::string; + } + key_->assign(value); +} +inline void Entry::set_key(const char* value) { + set_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + key_ = new ::std::string; + } + key_->assign(value); +} +inline void Entry::set_key(const char* value, size_t size) { + set_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + key_ = new ::std::string; + } + key_->assign(reinterpret_cast(value), size); +} +inline ::std::string* Entry::mutable_key() { + set_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + key_ = new ::std::string; + } + return key_; +} +inline ::std::string* Entry::release_key() { + clear_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + return NULL; + } else { + ::std::string* temp = key_; + key_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + return temp; } - clear_has_cmd(); } -inline const ::std::string& Entry::cmd() const { - return *cmd_; +inline void Entry::set_allocated_key(::std::string* key) { + if (key_ != &::google::protobuf::internal::kEmptyString) { + delete key_; + } + if (key) { + set_has_key(); + key_ = key; + } else { + clear_has_key(); + key_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + } +} + +// optional bytes value = 3; +inline bool Entry::has_value() const { + return (_has_bits_[0] & 0x00000004u) != 0; +} +inline void Entry::set_has_value() { + _has_bits_[0] |= 0x00000004u; +} +inline void Entry::clear_has_value() { + _has_bits_[0] &= ~0x00000004u; } -inline void Entry::set_cmd(const ::std::string& value) { - set_has_cmd(); - if (cmd_ == &::google::protobuf::internal::kEmptyString) { - cmd_ = new ::std::string; +inline void Entry::clear_value() { + if (value_ != &::google::protobuf::internal::kEmptyString) { + value_->clear(); } - cmd_->assign(value); + clear_has_value(); +} +inline const ::std::string& Entry::value() const { + return *value_; } -inline void Entry::set_cmd(const char* value) { - set_has_cmd(); - if (cmd_ == &::google::protobuf::internal::kEmptyString) { - cmd_ = new ::std::string; +inline void Entry::set_value(const ::std::string& value) { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; } - cmd_->assign(value); + value_->assign(value); } -inline void Entry::set_cmd(const void* value, size_t size) { - set_has_cmd(); - if (cmd_ == &::google::protobuf::internal::kEmptyString) { - cmd_ = new ::std::string; +inline void Entry::set_value(const char* value) { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; } - cmd_->assign(reinterpret_cast(value), size); + value_->assign(value); } -inline ::std::string* Entry::mutable_cmd() { - set_has_cmd(); - if (cmd_ == &::google::protobuf::internal::kEmptyString) { - cmd_ = new ::std::string; +inline void Entry::set_value(const void* value, size_t size) { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; + } + value_->assign(reinterpret_cast(value), size); +} +inline ::std::string* Entry::mutable_value() { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; } - return cmd_; + return value_; } -inline ::std::string* Entry::release_cmd() { - clear_has_cmd(); - if (cmd_ == &::google::protobuf::internal::kEmptyString) { +inline ::std::string* Entry::release_value() { + clear_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { return NULL; } else { - ::std::string* temp = cmd_; - cmd_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + ::std::string* temp = value_; + value_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); return temp; } } -inline void Entry::set_allocated_cmd(::std::string* cmd) { - if (cmd_ != &::google::protobuf::internal::kEmptyString) { - delete cmd_; +inline void Entry::set_allocated_value(::std::string* value) { + if (value_ != &::google::protobuf::internal::kEmptyString) { + delete value_; } - if (cmd) { - set_has_cmd(); - cmd_ = cmd; + if (value) { + set_has_value(); + value_ = value; } else { - clear_has_cmd(); - cmd_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + clear_has_value(); + value_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); } } +// required .floyd.Entry.OpType optype = 4; +inline bool Entry::has_optype() const { + return (_has_bits_[0] & 0x00000008u) != 0; +} +inline void Entry::set_has_optype() { + _has_bits_[0] |= 0x00000008u; +} +inline void Entry::clear_has_optype() { + _has_bits_[0] &= ~0x00000008u; +} +inline void Entry::clear_optype() { + optype_ = 0; + clear_has_optype(); +} +inline ::floyd::Entry_OpType Entry::optype() const { + return static_cast< ::floyd::Entry_OpType >(optype_); +} +inline void Entry::set_optype(::floyd::Entry_OpType value) { + assert(::floyd::Entry_OpType_IsValid(value)); + set_has_optype(); + optype_ = value; +} + // ------------------------------------------------------------------- // CmdRequest_Kv @@ -2323,26 +2482,26 @@ inline void CmdRequest_AppendEntries::set_prev_log_term(::google::protobuf::uint prev_log_term_ = value; } -// required uint64 commit_index = 6; -inline bool CmdRequest_AppendEntries::has_commit_index() const { +// required uint64 leader_commit = 6; +inline bool CmdRequest_AppendEntries::has_leader_commit() const { return (_has_bits_[0] & 0x00000020u) != 0; } -inline void CmdRequest_AppendEntries::set_has_commit_index() { +inline void CmdRequest_AppendEntries::set_has_leader_commit() { _has_bits_[0] |= 0x00000020u; } -inline void CmdRequest_AppendEntries::clear_has_commit_index() { +inline void CmdRequest_AppendEntries::clear_has_leader_commit() { _has_bits_[0] &= ~0x00000020u; } -inline void CmdRequest_AppendEntries::clear_commit_index() { - commit_index_ = GOOGLE_ULONGLONG(0); - clear_has_commit_index(); +inline void CmdRequest_AppendEntries::clear_leader_commit() { + leader_commit_ = GOOGLE_ULONGLONG(0); + clear_has_leader_commit(); } -inline ::google::protobuf::uint64 CmdRequest_AppendEntries::commit_index() const { - return commit_index_; +inline ::google::protobuf::uint64 CmdRequest_AppendEntries::leader_commit() const { + return leader_commit_; } -inline void CmdRequest_AppendEntries::set_commit_index(::google::protobuf::uint64 value) { - set_has_commit_index(); - commit_index_ = value; +inline void CmdRequest_AppendEntries::set_leader_commit(::google::protobuf::uint64 value) { + set_has_leader_commit(); + leader_commit_ = value; } // repeated .floyd.Entry entries = 7; @@ -3635,6 +3794,10 @@ inline void CmdResponse::set_allocated_server_status(::floyd::CmdResponse_Server namespace google { namespace protobuf { +template <> +inline const EnumDescriptor* GetEnumDescriptor< ::floyd::Entry_OpType>() { + return ::floyd::Entry_OpType_descriptor(); +} template <> inline const EnumDescriptor* GetEnumDescriptor< ::floyd::Type>() { return ::floyd::Type_descriptor(); From 3720f43fbb6a62bbfd04408bc84c14a89a4541cc Mon Sep 17 00:00:00 2001 From: baotiao Date: Mon, 17 Jul 2017 17:55:35 +0800 Subject: [PATCH 02/17] before change floyd_context --- floyd/example/simple/Makefile | 4 +- floyd/example/simple/README | 4 - floyd/proto/floyd.proto | 3 +- floyd/src/floyd_apply.cc | 4 +- floyd/src/floyd_command.cc | 475 ----------------- floyd/src/floyd_context.cc | 59 ++- floyd/src/floyd_context.h | 7 +- floyd/src/floyd_impl.cc | 482 +++++++++++++++++- floyd/src/floyd_primary_thread.cc | 2 +- floyd/src/floyd_primary_thread.h | 4 - floyd/src/raft_log.cc | 26 +- floyd/src/raft_log.h | 14 +- floyd/third/pink | 2 +- floyd/tools/Makefile | 66 +++ floyd/tools/README | 3 + floyd/tools/construct_log.cc | 19 + floyd/{example/simple => tools}/cpt.cc | 0 floyd/{example/simple => tools}/read_floyd.cc | 0 floyd/{example/simple => tools}/read_rock.cc | 0 19 files changed, 644 insertions(+), 530 deletions(-) delete mode 100644 floyd/src/floyd_command.cc create mode 100644 floyd/tools/Makefile create mode 100644 floyd/tools/README create mode 100644 floyd/tools/construct_log.cc rename floyd/{example/simple => tools}/cpt.cc (100%) rename floyd/{example/simple => tools}/read_floyd.cc (100%) rename floyd/{example/simple => tools}/read_rock.cc (100%) diff --git a/floyd/example/simple/Makefile b/floyd/example/simple/Makefile index 968608b..02424f0 100644 --- a/floyd/example/simple/Makefile +++ b/floyd/example/simple/Makefile @@ -5,7 +5,7 @@ else CXXFLAGS = -pg -O2 -ggdb3 -pipe -fPIC -W -Wwrite-strings -Wpointer-arith -Wreorder -Wswitch -Wsign-promo -Wredundant-decls -Wformat -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -std=c++11 -gdwarf-2 -Wno-redundant-decls -Wno-unused-variable -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DOS_LINUX endif -OBJECT = t read_rock read_floyd cpt t1 t2 +OBJECT = t read_rock read_floyd cpt t1 t2 t3 SRC_DIR = ./ THIRD_PATH = ../../third OUTPUT = ./output @@ -66,6 +66,8 @@ t1: t1.cc t2: t2.cc $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) +t3: t3.cc + $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) $(OBJS): %.o : %.cc $(CXX) $(CXXFLAGS) -c $< -o $@ $(INCLUDE_PATH) diff --git a/floyd/example/simple/README b/floyd/example/simple/README index cf7c380..825b116 100644 --- a/floyd/example/simple/README +++ b/floyd/example/simple/README @@ -3,7 +3,3 @@ the simple example will run the Floyd:write the get performance of floyd in a si t is a single thread wirte tool to get performance t1 is multi thread program to get performance - -read_floyd is the tool to read floyd log -read_rock is the tool to read rocksdb data -cpt is a tool to compare two rocksdb, if they have the same data return true, else return false diff --git a/floyd/proto/floyd.proto b/floyd/proto/floyd.proto index 4a943b5..79f57fa 100644 --- a/floyd/proto/floyd.proto +++ b/floyd/proto/floyd.proto @@ -44,9 +44,8 @@ message CmdRequest { message RequestVote { required bytes ip = 1; required int32 port = 2; - required uint64 term = 3; + required uint64 last_log_index = 3; required uint64 last_log_term = 4; - required uint64 last_log_index = 5; } optional RequestVote request_vote = 4; diff --git a/floyd/src/floyd_apply.cc b/floyd/src/floyd_apply.cc index 97a884e..b816c14 100644 --- a/floyd/src/floyd_apply.cc +++ b/floyd/src/floyd_apply.cc @@ -46,14 +46,14 @@ void FloydApply::ApplyStateMachine(void* arg) { uint64_t len = 0, to_apply = 0; to_apply = context->NextApplyIndex(&len); - LOGV(DEBUG_LEVEL, context->info_log(), "ApplyStateMachine with %lu entries to apply from to_apply(%lu)", + LOGV(DEBUG_LEVEL, context->info_log(), "FloydApply::ApplyStateMachine: %lu entries to apply from to_apply(%lu)", len, to_apply); while (len-- > 0) { Entry log_entry; fapply->raft_log_->GetEntry(to_apply, &log_entry); Status s = fapply->Apply(log_entry); if (!s.ok()) { - LOGV(WARN_LEVEL, context->info_log(), "Apply log entry failed, at: %d, error: %s", + LOGV(WARN_LEVEL, context->info_log(), "FloydApply::ApplyStateMachine: Apply log entry failed, at: %d, error: %s", to_apply, s.ToString().c_str()); fapply->ScheduleApply(); // try once more usleep(1000000); diff --git a/floyd/src/floyd_command.cc b/floyd/src/floyd_command.cc deleted file mode 100644 index 8597aae..0000000 --- a/floyd/src/floyd_command.cc +++ /dev/null @@ -1,475 +0,0 @@ -// Copyright (c) 2015-present, Qihoo, Inc. All rights reserved. -// This source code is licensed under the BSD-style license found in the -// LICENSE file in the root directory of this source tree. An additional grant -// of patent rights can be found in the PATENTS file in the same directory. - -#include "floyd/src/floyd_impl.h" - -#include - -#include "floyd/src/floyd_context.h" -#include "floyd/src/floyd_apply.h" -#include "floyd/src/floyd_client_pool.h" -#include "floyd/src/floyd_primary_thread.h" -#include "floyd/src/floyd_peer_thread.h" -#include "floyd/src/raft_log.h" -#include "floyd/src/logger.h" - -#include "floyd/src/floyd.pb.h" - -#include "slash/include/slash_string.h" -#include "pink/include/bg_thread.h" - -namespace floyd { - -static void BuildReadRequest(const std::string& key, CmdRequest* cmd) { - cmd->set_type(Type::kRead); - CmdRequest_Kv* kv = cmd->mutable_kv(); - kv->set_key(key); -} - -static void BuildReadResponse(const std::string &key, const std::string &value, - StatusCode code, CmdResponse* response) { - response->set_code(code); - CmdResponse_Kv* kv = response->mutable_kv(); - if (code == StatusCode::kOk) { - kv->set_value(value); - } -} - -static void BuildWriteRequest(const std::string& key, - const std::string& value, CmdRequest* cmd) { - cmd->set_type(Type::kWrite); - CmdRequest_Kv* kv = cmd->mutable_kv(); - kv->set_key(key); - kv->set_value(value); -} - -static void BuildDirtyWriteRequest(const std::string& key, - const std::string& value, CmdRequest* cmd) { - cmd->set_type(Type::kDirtyWrite); - CmdRequest_Kv* kv = cmd->mutable_kv(); - kv->set_key(key); - kv->set_value(value); -} - -static void BuildDeleteRequest(const std::string& key, CmdRequest* cmd) { - cmd->set_type(Type::kDelete); - CmdRequest_Kv* kv = cmd->mutable_kv(); - kv->set_key(key); -} - -static void BuildRequestVoteResponse(uint64_t term, bool granted, - CmdResponse* response) { - response->set_type(Type::kRequestVote); - CmdResponse_RequestVoteResponse* request_vote_res = response->mutable_request_vote_res(); - request_vote_res->set_term(term); - request_vote_res->set_vote_granted(granted); -} - -static void BuildAppendEntriesResponse(bool succ, uint64_t term, - uint64_t log_index, - CmdResponse* response) { - response->set_type(Type::kAppendEntries); - CmdResponse_AppendEntriesResponse* append_entries_res = response->mutable_append_entries_res(); - append_entries_res->set_term(term); - append_entries_res->set_last_log_index(log_index); - append_entries_res->set_success(succ); -} - -static void BuildLogEntry(const CmdRequest& cmd, Entry* entry) { - entry->set_term(cmd.append_entries().term()); - entry->set_key(cmd.kv().key()); - entry->set_value(cmd.kv().value()); - if (cmd.type() == Type::kRead) { - entry->set_optype(Entry_OpType_kRead); - } else if (cmd.type() == Type::kWrite || cmd.type() == Type::kDirtyWrite) { - entry->set_optype(Entry_OpType_kWrite); - } else if (cmd.type() == Type::kDelete) { - entry->set_optype(Entry_OpType_kDelete); - } -} - -Status FloydImpl::Write(const std::string& key, const std::string& value) { - if (!HasLeader()) { - return Status::Incomplete("no leader node!"); - } - CmdRequest cmd; - BuildWriteRequest(key, value, &cmd); - CmdResponse response; - Status s = DoCommand(cmd, &response); - if (!s.ok()) { - return s; - } - if (response.code() == StatusCode::kOk) { - return Status::OK(); - } - return Status::Corruption("Write Error"); -} - -Status FloydImpl::DirtyWrite(const std::string& key, const std::string& value) { - // Write myself first - rocksdb::Status rs = db_->Put(rocksdb::WriteOptions(), key, value); - if (!rs.ok()) { - return Status::IOError("DirtyWrite failed, " + rs.ToString()); - } - - // Sync to other nodes without response - CmdRequest cmd; - BuildDirtyWriteRequest(key, value, &cmd); - - CmdResponse response; - std::string local_server = slash::IpPortString(options_.local_ip, options_.local_port); - for (auto& iter : options_.members) { - if (iter != local_server) { - Status s = worker_client_pool_->SendAndRecv(iter, cmd, &response); - LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::DirtyWrite Send to %s return %s, key(%s) value(%s)", - iter.c_str(), s.ToString().c_str(), cmd.kv().key().c_str(), cmd.kv().value().c_str()); - } - } - return Status::OK(); -} - -Status FloydImpl::Delete(const std::string& key) { - if (!HasLeader()) { - return Status::Incomplete("no leader node!"); - } - CmdRequest cmd; - BuildDeleteRequest(key, &cmd); - - CmdResponse response; - Status s = DoCommand(cmd, &response); - if (!s.ok()) { - return s; - } - if (response.code() == StatusCode::kOk) { - return Status::OK(); - } - return Status::Corruption("Delete Error"); -} - -Status FloydImpl::Read(const std::string& key, std::string& value) { - if (!HasLeader()) { - return Status::Incomplete("no leader node!"); - } - CmdRequest cmd; - BuildReadRequest(key, &cmd); - CmdResponse response; - Status s = DoCommand(cmd, &response); - if (!s.ok()) { - return s; - } - if (response.code() == StatusCode::kOk) { - value = response.kv().value(); - return Status::OK(); - } else if (response.code() == StatusCode::kNotFound) { - return Status::NotFound(""); - } else { - return Status::Corruption("Read Error"); - } -} - -Status FloydImpl::DirtyRead(const std::string& key, std::string& value) { - rocksdb::Status s = db_->Get(rocksdb::ReadOptions(), key, &value); - if (s.ok()) { - return Status::OK(); - } else if (s.IsNotFound()) { - return Status::NotFound(""); - } - return Status::Corruption(s.ToString()); -} - -bool FloydImpl::GetServerStatus(std::string& msg) { - LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::GetServerStatus start"); - - CmdResponse_ServerStatus server_status; - DoGetServerStatus(&server_status); - - char str[512]; - snprintf (str, 512, - " Node | Role | Term | CommitIdx | Leader | VoteFor | LastLogTerm | LastLogIdx | LastApplyIdx |\n" - "%15s:%-6d %9s %10lu %10lu %15s:%-6d %15s:%-6d %10lu %10lu %10lu\n", - options_.local_ip.c_str(), options_.local_port, - server_status.role().c_str(), - server_status.term(), server_status.commit_index(), - server_status.leader_ip().c_str(), server_status.leader_port(), - server_status.voted_for_ip().c_str(), server_status.voted_for_port(), - server_status.last_log_term(), server_status.last_log_index(), - server_status.last_applied()); - - msg.clear(); - msg.append(str); - - CmdRequest cmd; - cmd.set_type(Type::kServerStatus); - CmdResponse response; - std::string local_server = slash::IpPortString(options_.local_ip, options_.local_port); - for (auto& iter : options_.members) { - if (iter != local_server) { - Status s = worker_client_pool_->SendAndRecv(iter, cmd, &response); - LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::GetServerStatus Send to %s return %s", - iter.c_str(), s.ToString().c_str()); - if (s.ok()) { - std::string ip; - int port; - slash::ParseIpPortString(iter, ip, port); - CmdResponse_ServerStatus server_status = response.server_status(); - snprintf (str, 512, - "%15s:%-6d %9s %10lu %10lu %15s:%-6d %15s:%-6d %10lu %10lu %10lu\n", - ip.c_str(), port, - server_status.role().c_str(), - server_status.term(), server_status.commit_index(), - server_status.leader_ip().c_str(), server_status.leader_port(), - server_status.voted_for_ip().c_str(), server_status.voted_for_port(), - server_status.last_log_term(), server_status.last_log_index(), - server_status.last_applied()); - msg.append(str); - LOGV(DEBUG_LEVEL, info_log_, "GetServerStatus msg(%s)", str); - } - } - } - return true; -} - -Status FloydImpl::DoCommand(const CmdRequest& cmd, CmdResponse *response) { - // Execute if is leader - std::string leader_ip; - int leader_port; - context_->leader_node(&leader_ip, &leader_port); - if (options_.local_ip == leader_ip && options_.local_port == leader_port) { - return ExecuteCommand(cmd, response); - } - // Redirect to leader - return worker_client_pool_->SendAndRecv( - slash::IpPortString(leader_ip, leader_port), - cmd, response); -} - -Status FloydImpl::ReplyExecuteDirtyCommand(const CmdRequest& cmd, - CmdResponse *response) { - std::string value; - rocksdb::Status rs; - switch (cmd.type()) { - case Type::kDirtyWrite: { - rs = db_->Put(rocksdb::WriteOptions(), cmd.kv().key(), cmd.kv().value()); - //TODO(anan) add response type or reorganize proto - //response->set_type(CmdResponse::DirtyWrite); - response->set_type(Type::kWrite); - CmdResponse_Kv* kv = response->mutable_kv(); - if (rs.ok()) { - response->set_code(StatusCode::kOk); - } else { - response->set_code(StatusCode::kError); - } - - LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ExecuteDirtyCommand DirtyWrite %s, key(%s) value(%s)", - rs.ToString().c_str(), cmd.kv().key().c_str(), cmd.kv().value().c_str()); -#ifndef NDEBUG - std::string text_format; - google::protobuf::TextFormat::PrintToString(*response, &text_format); - LOGV(DEBUG_LEVEL, info_log_, "DirtyWrite Response :\n%s", text_format.c_str()); -#endif - break; - } - case Type::kServerStatus: { - response->set_type(Type::kServerStatus); - response->set_code(StatusCode::kOk); - CmdResponse_ServerStatus* server_status = response->mutable_server_status(); - DoGetServerStatus(server_status); - LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ExecuteDirtyCommand GetServerStatus"); - break; - } - default: { - return Status::Corruption("Unknown cmd type"); - } - } - return Status::OK(); -} - -bool FloydImpl::DoGetServerStatus(CmdResponse_ServerStatus* res) { - std::string role_msg; - switch (context_->role()) { - case Role::kFollower: - role_msg = "follower"; - break; - case Role::kCandidate: - role_msg = "candidate"; - break; - case Role::kLeader: - role_msg = "leader"; - break; - } - - res->set_term(context_->current_term()); - res->set_commit_index(context_->commit_index()); - res->set_role(role_msg); - - std::string ip; - int port; - context_->leader_node(&ip, &port); - if (ip.empty()) { - res->set_leader_ip("null"); - } else { - res->set_leader_ip(ip); - } - res->set_leader_port(port); - - context_->voted_for_node(&ip, &port); - if (ip.empty()) { - res->set_voted_for_ip("null"); - } else { - res->set_voted_for_ip(ip); - } - res->set_voted_for_port(port); - - uint64_t last_log_index; - uint64_t last_log_term; - raft_log_->GetLastLogTermAndIndex(&last_log_term, &last_log_index); - - res->set_last_log_term(last_log_term); - res->set_last_log_index(last_log_index); - res->set_last_applied(context_->last_applied()); - return true; -} - -Status FloydImpl::ExecuteCommand(const CmdRequest& cmd, - CmdResponse *response) { - // Append entry local - std::vector entries; - Entry entry; - BuildLogEntry(cmd, &entry); - entries.push_back(&entry); - - uint64_t last_log_index = raft_log_->Append(entries); - if (last_log_index <= 0) { - return Status::IOError("Append Entry failed"); - } - - // Notify primary then wait for apply - if (options_.single_mode) { - primary_->AddTask(kAdvanceCommitIndex); - } else { - primary_->AddTask(kNewCommand); - } - - response->set_type(cmd.type()); - response->set_code(StatusCode::kError); - - Status res = context_->WaitApply(last_log_index, 1000); - if (!res.ok()) { - return res; - } - - // Complete CmdRequest if needed - std::string value; - rocksdb::Status rs; - switch (cmd.type()) { - case Type::kWrite: { - response->set_code(StatusCode::kOk); - break; - } - case Type::kDelete: { - response->set_code(StatusCode::kOk); - break; - } - case Type::kRead: { - rs = db_->Get(rocksdb::ReadOptions(), cmd.kv().key(), &value); - if (rs.ok()) { - BuildReadResponse(cmd.kv().key(), value, StatusCode::kOk, response); - } else if (rs.IsNotFound()) { - BuildReadResponse(cmd.kv().key(), value, StatusCode::kNotFound, response); - } else { - BuildReadResponse(cmd.kv().key(), value, StatusCode::kError, response); - } - LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ExecuteCommand Read %s, key(%s) value(%s)", - rs.ToString().c_str(), cmd.kv().key().c_str(), value.c_str()); -#ifndef NDEBUG - std::string text_format; - google::protobuf::TextFormat::PrintToString(*response, &text_format); - LOGV(DEBUG_LEVEL, info_log_, "ReadResponse :\n%s", text_format.c_str()); -#endif - break; - } - default: { - return Status::Corruption("Unknown cmd type"); - } - } - return Status::OK(); -} - -void FloydImpl::ReplyRequestVote(const CmdRequest& cmd, CmdResponse* response) { - bool granted = false; - uint64_t my_term = context_->current_term(); - - CmdRequest_RequestVote request_vote = cmd.request_vote(); - LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::DoRequestVote my_term=%lu rqv.term=%lu", - my_term, request_vote.term()); - // if caller's term smaller than my term, then I will notice him - if (request_vote.term() < my_term) { - BuildRequestVoteResponse(my_term, granted, response); - return; - } - - // Step down by larger term - if (request_vote.term() > my_term) { - context_->BecomeFollower(request_vote.term()); - primary_->ResetElectLeaderTimer(); - } - - // Try to get my vote - granted = context_->ReceiverDoRequestVote( - request_vote.term(), request_vote.ip(), request_vote.port(), - request_vote.last_log_term(), request_vote.last_log_index(), - &my_term); - - BuildRequestVoteResponse(my_term, granted, response); -} - -void FloydImpl::ReplyAppendEntries(CmdRequest& cmd, CmdResponse* response) { - // Ignore stale term - bool status = false; - uint64_t my_term = context_->current_term(); - CmdRequest_AppendEntries append_entries = cmd.append_entries(); - // if the append entries term is smaller then my_term, then the caller must an older leader - if (append_entries.term() < my_term) { - BuildAppendEntriesResponse(status, my_term, raft_log_->GetLastLogIndex(), response); - return; - } - // TODO(ba0tiao) why we need become follower, maybe we have been follower before - context_->BecomeFollower(append_entries.term(), - append_entries.ip(), append_entries.port()); - - std::vector entries; - for (auto& it : *(cmd.mutable_append_entries()->mutable_entries())) { - entries.push_back(&it); - } - // TODO(ba0tiao) do consistency check here - - /* - * std::string text_format; - * google::protobuf::TextFormat::PrintToString(cmd, &text_format); - * LOGV(DEBUG_LEVEL, context_->info_log(), "FloydImpl::ReplyAppendEntries with %llu " - * "entries, message :\n%s", - * entries.size(), text_format.c_str()); - */ - - // Append entries - status = context_->ReceiverDoAppendEntries(append_entries.term(), - append_entries.prev_log_term(), - append_entries.prev_log_index(), - entries, &my_term); - - // Update log commit index - if (context_->AdvanceCommitIndex(append_entries.leader_commit())) { - LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries after AdvanceCommitIndex %lu", - context_->commit_index()); - apply_->ScheduleApply(); - } - - // TODO(anan) ElectLeader timer may timeout because of slow AppendEntries - // we delay reset timer. - primary_->ResetElectLeaderTimer(); - BuildAppendEntriesResponse(status, my_term, raft_log_->GetLastLogIndex(), response); -} - -} // namespace floyd diff --git a/floyd/src/floyd_context.cc b/floyd/src/floyd_context.cc index 4c267b2..6e67c71 100644 --- a/floyd/src/floyd_context.cc +++ b/floyd/src/floyd_context.cc @@ -35,6 +35,7 @@ FloydContext::FloydContext(const floyd::Options& opt, uint64_t seed = slash::NowMicros() % 100000; LOGV(INFO_LEVEL, info_log_, "ElectLeader srandom with seed %lu", seed); srandom(seed); + commit_index_ = raft_log_->commit_index(); //srandom(slash::NowMicros()); } @@ -138,31 +139,30 @@ void FloydContext::BecomeLeader() { LOGV(INFO_LEVEL, info_log_, "FloydContext::BecomeLeader I am become Leader!!"); } -bool FloydContext::AdvanceCommitIndex(uint64_t new_commit_index) { +// only leader will call AdvanceCommitIndex +// follower only need set commit as leader's +bool FloydContext::AdvanceLeaderCommitIndex(uint64_t new_commit_index) { + Entry entry; + raft_log_->GetEntry(new_commit_index, &entry); + slash::MutexLock l(&commit_mu_); + LOGV(DEBUG_LEVEL, info_log_, "FloydContext::AdvanceCommitIndex" + "commit_index=%lu, new_commit_index=%lu, entry.term() = %lu, current_term_ = %lu", + commit_index_, new_commit_index, entry.term(), current_term_); + if (entry.term() == current_term_) { + commit_index_ = new_commit_index; + LOGV(DEBUG_LEVEL, info_log_, "FloydContext::AdvanceCommitIndex advance commit_index to %ld", new_commit_index); + raft_log_->UpdateCommitIndex(commit_index_); + return true; + } + return false; +} + +bool FloydContext::AdvanceFollowerCommitIndex(uint64_t new_commit_index) { + // Update log commit index + slash::MutexLock l(&commit_mu_); commit_index_ = new_commit_index; + raft_log_->UpdateCommitIndex(commit_index_); return true; -/* - * if (new_commit_index == 0) { - * return false; - * } - * slash::MutexLock l(&commit_mu_); - * LOGV(DEBUG_LEVEL, info_log_, "FloydContext::AdvanceCommitIndex commit_index=%lu, new commit_index=%lu", - * commit_index_, new_commit_index); - * if (commit_index_ >= new_commit_index) { - * return false; - * } - * - * uint64_t last_log_index = raft_log_->GetLastLogIndex(); - * new_commit_index = std::min(last_log_index, new_commit_index); - * Entry entry; - * raft_log_->GetEntry(new_commit_index, &entry); - * if (entry.term() == current_term_) { - * commit_index_ = new_commit_index; - * LOGV(DEBUG_LEVEL, info_log_, "FloydContext::AdvanceCommitIndex advance commit_index to %ld", new_commit_index); - * return true; - * } - * return false; - */ } uint64_t FloydContext::NextApplyIndex(uint64_t* len) { @@ -183,7 +183,7 @@ void FloydContext::ApplyDone(uint64_t index) { } void FloydContext::MetaApply() { - raft_log_->UpdateMetadata(current_term_, voted_for_ip_, voted_for_port_, last_applied()); + raft_log_->UpdateMetadata(current_term_, voted_for_ip_, voted_for_port_); } bool FloydContext::VoteAndCheck(uint64_t vote_term) { @@ -260,18 +260,21 @@ bool FloydContext::ReceiverDoAppendEntries(uint64_t term, // Check pre_log match local log entry uint64_t last_log_index = raft_log_->GetLastLogIndex(); if (pre_log_index > last_log_index) { - LOGV(DEBUG_LEVEL, info_log_, "FloydContext::ReceiverDoAppendEntries: pre_log(%lu, %lu) > last_log_index(%lu)", - pre_log_term, pre_log_index, last_log_index); + LOGV(DEBUG_LEVEL, info_log_, "FloydContext::ReceiverDoAppendEntries:" + "pre_log(%lu, %lu) > last_log_index(%lu)", pre_log_term, pre_log_index, + last_log_index); return false; } uint64_t my_log_term = 0; Entry entry; - LOGV(DEBUG_LEVEL, info_log_, "FloydContext::ReceiverDoAppendEntries pre_log_index: %llu\n", pre_log_index); + LOGV(DEBUG_LEVEL, info_log_, "FloydContext::ReceiverDoAppendEntries" + "pre_log_index: %llu\n", pre_log_index); if (raft_log_->GetEntry(pre_log_index, &entry) == 0) { my_log_term = entry.term(); } else { - LOGV(WARN_LEVEL, info_log_, "FloydContext::ReceiverDoAppendEntries: can't get Entry from raft_log pre_log_index %llu", pre_log_index); + LOGV(WARN_LEVEL, info_log_, "FloydContext::ReceiverDoAppendEntries: can't" + "get Entry from raft_log pre_log_index %llu", pre_log_index); } if (pre_log_term != my_log_term) { diff --git a/floyd/src/floyd_context.h b/floyd/src/floyd_context.h index 21ddfe0..7c5308d 100644 --- a/floyd/src/floyd_context.h +++ b/floyd/src/floyd_context.h @@ -101,7 +101,11 @@ class FloydContext { slash::MutexLock l(&commit_mu_); return commit_index_; } - bool AdvanceCommitIndex(uint64_t commit_index); + void set_commit_index(uint64_t commit_index) { + commit_index_ = commit_index; + } + bool AdvanceLeaderCommitIndex(uint64_t leader_commit); + bool AdvanceFollowerCommitIndex(uint64_t leader_commit); /* Apply related */ // Return false if timeout @@ -128,6 +132,7 @@ class FloydContext { // Role related pthread_rwlock_t stat_rw_; uint64_t current_term_; + Role role_; std::string voted_for_ip_; int voted_for_port_; diff --git a/floyd/src/floyd_impl.cc b/floyd/src/floyd_impl.cc index d1d5df7..2cf62c3 100644 --- a/floyd/src/floyd_impl.cc +++ b/floyd/src/floyd_impl.cc @@ -5,12 +5,16 @@ #include "floyd/src/floyd_impl.h" +#include + #include #include #include "slash/include/env.h" - #include "slash/include/slash_string.h" +#include "pink/include/bg_thread.h" +#include "slash/include/slash_string.h" + #include "floyd/src/floyd_context.h" #include "floyd/src/floyd_apply.h" #include "floyd/src/floyd_worker.h" @@ -19,6 +23,7 @@ #include "floyd/src/floyd_primary_thread.h" #include "floyd/src/floyd_client_pool.h" #include "floyd/src/logger.h" +#include "floyd/src/floyd.pb.h" namespace floyd { @@ -170,4 +175,479 @@ Status Floyd::Open(const Options& options, Floyd** floyd) { Floyd::~Floyd() { } +static void BuildReadRequest(const std::string& key, CmdRequest* cmd) { + cmd->set_type(Type::kRead); + CmdRequest_Kv* kv = cmd->mutable_kv(); + kv->set_key(key); +} + +static void BuildReadResponse(const std::string &key, const std::string &value, + StatusCode code, CmdResponse* response) { + response->set_code(code); + CmdResponse_Kv* kv = response->mutable_kv(); + if (code == StatusCode::kOk) { + kv->set_value(value); + } +} + +static void BuildWriteRequest(const std::string& key, + const std::string& value, CmdRequest* cmd) { + cmd->set_type(Type::kWrite); + CmdRequest_Kv* kv = cmd->mutable_kv(); + kv->set_key(key); + kv->set_value(value); +} + +static void BuildDirtyWriteRequest(const std::string& key, + const std::string& value, CmdRequest* cmd) { + cmd->set_type(Type::kDirtyWrite); + CmdRequest_Kv* kv = cmd->mutable_kv(); + kv->set_key(key); + kv->set_value(value); +} + +static void BuildDeleteRequest(const std::string& key, CmdRequest* cmd) { + cmd->set_type(Type::kDelete); + CmdRequest_Kv* kv = cmd->mutable_kv(); + kv->set_key(key); +} + +static void BuildRequestVoteResponse(uint64_t term, bool granted, + CmdResponse* response) { + response->set_type(Type::kRequestVote); + CmdResponse_RequestVoteResponse* request_vote_res = response->mutable_request_vote_res(); + request_vote_res->set_term(term); + request_vote_res->set_vote_granted(granted); +} + +static void BuildAppendEntriesResponse(bool succ, uint64_t term, + uint64_t log_index, + CmdResponse* response) { + response->set_type(Type::kAppendEntries); + CmdResponse_AppendEntriesResponse* append_entries_res = response->mutable_append_entries_res(); + append_entries_res->set_term(term); + append_entries_res->set_last_log_index(log_index); + append_entries_res->set_success(succ); +} + +static void BuildLogEntry(const CmdRequest& cmd, uint64_t current_term, Entry* entry) { + entry->set_term(current_term); + entry->set_key(cmd.kv().key()); + entry->set_value(cmd.kv().value()); + if (cmd.type() == Type::kRead) { + entry->set_optype(Entry_OpType_kRead); + } else if (cmd.type() == Type::kWrite || cmd.type() == Type::kDirtyWrite) { + entry->set_optype(Entry_OpType_kWrite); + } else if (cmd.type() == Type::kDelete) { + entry->set_optype(Entry_OpType_kDelete); + } +} + +Status FloydImpl::Write(const std::string& key, const std::string& value) { + if (!HasLeader()) { + return Status::Incomplete("no leader node!"); + } + CmdRequest cmd; + BuildWriteRequest(key, value, &cmd); + CmdResponse response; + Status s = DoCommand(cmd, &response); + if (!s.ok()) { + return s; + } + if (response.code() == StatusCode::kOk) { + return Status::OK(); + } + return Status::Corruption("Write Error"); +} + +Status FloydImpl::DirtyWrite(const std::string& key, const std::string& value) { + // Write myself first + rocksdb::Status rs = db_->Put(rocksdb::WriteOptions(), key, value); + if (!rs.ok()) { + return Status::IOError("DirtyWrite failed, " + rs.ToString()); + } + + // Sync to other nodes without response + CmdRequest cmd; + BuildDirtyWriteRequest(key, value, &cmd); + + CmdResponse response; + std::string local_server = slash::IpPortString(options_.local_ip, options_.local_port); + for (auto& iter : options_.members) { + if (iter != local_server) { + Status s = worker_client_pool_->SendAndRecv(iter, cmd, &response); + LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::DirtyWrite Send to %s return %s, key(%s) value(%s)", + iter.c_str(), s.ToString().c_str(), cmd.kv().key().c_str(), cmd.kv().value().c_str()); + } + } + return Status::OK(); +} + +Status FloydImpl::Delete(const std::string& key) { + if (!HasLeader()) { + return Status::Incomplete("no leader node!"); + } + CmdRequest cmd; + BuildDeleteRequest(key, &cmd); + + CmdResponse response; + Status s = DoCommand(cmd, &response); + if (!s.ok()) { + return s; + } + if (response.code() == StatusCode::kOk) { + return Status::OK(); + } + return Status::Corruption("Delete Error"); +} + +Status FloydImpl::Read(const std::string& key, std::string& value) { + if (!HasLeader()) { + return Status::Incomplete("no leader node!"); + } + CmdRequest cmd; + BuildReadRequest(key, &cmd); + CmdResponse response; + Status s = DoCommand(cmd, &response); + if (!s.ok()) { + return s; + } + if (response.code() == StatusCode::kOk) { + value = response.kv().value(); + return Status::OK(); + } else if (response.code() == StatusCode::kNotFound) { + return Status::NotFound(""); + } else { + return Status::Corruption("Read Error"); + } +} + +Status FloydImpl::DirtyRead(const std::string& key, std::string& value) { + rocksdb::Status s = db_->Get(rocksdb::ReadOptions(), key, &value); + if (s.ok()) { + return Status::OK(); + } else if (s.IsNotFound()) { + return Status::NotFound(""); + } + return Status::Corruption(s.ToString()); +} + +bool FloydImpl::GetServerStatus(std::string& msg) { + LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::GetServerStatus start"); + + CmdResponse_ServerStatus server_status; + DoGetServerStatus(&server_status); + + char str[512]; + snprintf (str, 512, + " Node | Role | Term | CommitIdx | Leader | VoteFor | LastLogTerm | LastLogIdx | LastApplyIdx |\n" + "%15s:%-6d %9s %10lu %10lu %15s:%-6d %15s:%-6d %10lu %10lu %10lu\n", + options_.local_ip.c_str(), options_.local_port, + server_status.role().c_str(), + server_status.term(), server_status.commit_index(), + server_status.leader_ip().c_str(), server_status.leader_port(), + server_status.voted_for_ip().c_str(), server_status.voted_for_port(), + server_status.last_log_term(), server_status.last_log_index(), + server_status.last_applied()); + + msg.clear(); + msg.append(str); + + CmdRequest cmd; + cmd.set_type(Type::kServerStatus); + CmdResponse response; + std::string local_server = slash::IpPortString(options_.local_ip, options_.local_port); + for (auto& iter : options_.members) { + if (iter != local_server) { + Status s = worker_client_pool_->SendAndRecv(iter, cmd, &response); + LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::GetServerStatus Send to %s return %s", + iter.c_str(), s.ToString().c_str()); + if (s.ok()) { + std::string ip; + int port; + slash::ParseIpPortString(iter, ip, port); + CmdResponse_ServerStatus server_status = response.server_status(); + snprintf (str, 512, + "%15s:%-6d %9s %10lu %10lu %15s:%-6d %15s:%-6d %10lu %10lu %10lu\n", + ip.c_str(), port, + server_status.role().c_str(), + server_status.term(), server_status.commit_index(), + server_status.leader_ip().c_str(), server_status.leader_port(), + server_status.voted_for_ip().c_str(), server_status.voted_for_port(), + server_status.last_log_term(), server_status.last_log_index(), + server_status.last_applied()); + msg.append(str); + LOGV(DEBUG_LEVEL, info_log_, "GetServerStatus msg(%s)", str); + } + } + } + return true; +} + +Status FloydImpl::DoCommand(const CmdRequest& cmd, CmdResponse *response) { + // Execute if is leader + std::string leader_ip; + int leader_port; + context_->leader_node(&leader_ip, &leader_port); + if (options_.local_ip == leader_ip && options_.local_port == leader_port) { + return ExecuteCommand(cmd, response); + } + // Redirect to leader + return worker_client_pool_->SendAndRecv( + slash::IpPortString(leader_ip, leader_port), + cmd, response); +} + +Status FloydImpl::ReplyExecuteDirtyCommand(const CmdRequest& cmd, + CmdResponse *response) { + std::string value; + rocksdb::Status rs; + switch (cmd.type()) { + case Type::kDirtyWrite: { + rs = db_->Put(rocksdb::WriteOptions(), cmd.kv().key(), cmd.kv().value()); + //TODO(anan) add response type or reorganize proto + //response->set_type(CmdResponse::DirtyWrite); + response->set_type(Type::kWrite); + CmdResponse_Kv* kv = response->mutable_kv(); + if (rs.ok()) { + response->set_code(StatusCode::kOk); + } else { + response->set_code(StatusCode::kError); + } + + LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ExecuteDirtyCommand DirtyWrite %s, key(%s) value(%s)", + rs.ToString().c_str(), cmd.kv().key().c_str(), cmd.kv().value().c_str()); +#ifndef NDEBUG + std::string text_format; + google::protobuf::TextFormat::PrintToString(*response, &text_format); + LOGV(DEBUG_LEVEL, info_log_, "DirtyWrite Response :\n%s", text_format.c_str()); +#endif + break; + } + case Type::kServerStatus: { + response->set_type(Type::kServerStatus); + response->set_code(StatusCode::kOk); + CmdResponse_ServerStatus* server_status = response->mutable_server_status(); + DoGetServerStatus(server_status); + LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ExecuteDirtyCommand GetServerStatus"); + break; + } + default: { + return Status::Corruption("Unknown cmd type"); + } + } + return Status::OK(); +} + +bool FloydImpl::DoGetServerStatus(CmdResponse_ServerStatus* res) { + std::string role_msg; + switch (context_->role()) { + case Role::kFollower: + role_msg = "follower"; + break; + case Role::kCandidate: + role_msg = "candidate"; + break; + case Role::kLeader: + role_msg = "leader"; + break; + } + + res->set_term(context_->current_term()); + res->set_commit_index(context_->commit_index()); + res->set_role(role_msg); + + std::string ip; + int port; + context_->leader_node(&ip, &port); + if (ip.empty()) { + res->set_leader_ip("null"); + } else { + res->set_leader_ip(ip); + } + res->set_leader_port(port); + + context_->voted_for_node(&ip, &port); + if (ip.empty()) { + res->set_voted_for_ip("null"); + } else { + res->set_voted_for_ip(ip); + } + res->set_voted_for_port(port); + + uint64_t last_log_index; + uint64_t last_log_term; + raft_log_->GetLastLogTermAndIndex(&last_log_term, &last_log_index); + + res->set_last_log_term(last_log_term); + res->set_last_log_index(last_log_index); + res->set_last_applied(context_->last_applied()); + return true; +} + +Status FloydImpl::ExecuteCommand(const CmdRequest& cmd, + CmdResponse *response) { + // Append entry local + std::vector entries; + Entry entry; + BuildLogEntry(cmd, context_->current_term(), &entry); + entries.push_back(&entry); + + uint64_t last_log_index = raft_log_->Append(entries); + if (last_log_index <= 0) { + return Status::IOError("Append Entry failed"); + } + + // Notify primary then wait for apply + if (options_.single_mode) { + primary_->AddTask(kAdvanceCommitIndex); + } else { + primary_->AddTask(kNewCommand); + } + + response->set_type(cmd.type()); + response->set_code(StatusCode::kError); + + Status res = context_->WaitApply(last_log_index, 1000); + if (!res.ok()) { + return res; + } + + // Complete CmdRequest if needed + std::string value; + rocksdb::Status rs; + switch (cmd.type()) { + case Type::kWrite: { + response->set_code(StatusCode::kOk); + break; + } + case Type::kDelete: { + response->set_code(StatusCode::kOk); + break; + } + case Type::kRead: { + rs = db_->Get(rocksdb::ReadOptions(), cmd.kv().key(), &value); + if (rs.ok()) { + BuildReadResponse(cmd.kv().key(), value, StatusCode::kOk, response); + } else if (rs.IsNotFound()) { + BuildReadResponse(cmd.kv().key(), value, StatusCode::kNotFound, response); + } else { + BuildReadResponse(cmd.kv().key(), value, StatusCode::kError, response); + } + LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ExecuteCommand Read %s, key(%s) value(%s)", + rs.ToString().c_str(), cmd.kv().key().c_str(), value.c_str()); +#ifndef NDEBUG + std::string text_format; + google::protobuf::TextFormat::PrintToString(*response, &text_format); + LOGV(DEBUG_LEVEL, info_log_, "ReadResponse :\n%s", text_format.c_str()); +#endif + break; + } + default: { + return Status::Corruption("Unknown cmd type"); + } + } + return Status::OK(); +} + +void FloydImpl::ReplyRequestVote(const CmdRequest& cmd, CmdResponse* response) { + bool granted = false; + CmdRequest_RequestVote request_vote = cmd.request_vote(); + LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyRequestVote: my_term=%lu rqv.term=%lu", + my_term, request_vote.term()); + MutexLock l(context_->commit_mu_); + uint64_t my_current_term = context_->current_term; + uint64_t my_last_log_index = context_->last_log_index; + // if caller's term smaller than my term, then I will notice him + if (request_vote.last_log_term() < my_current_term) { + BuildRequestVoteResponse(my_current_term, granted, response); + return; + } + + // if votedfor is null or candidateId, and candidated's log is at least as up-to-date + // as receiver's log, grant vote + if (request_vote.last_log_term() > my_current_term || + (request_vote.last_log_term() == my_current_term && request_vote.last_log_index() >= my_last_log_index)) { + BecomeFollower(request_vote.term()); + primary_->ResetElectLeaderTimer(); + } + raft_log_->GetLastLogTermAndIndex(&my_log_term, &my_log_index); + LOGV(DEBUG_LEVEL, info_log_, "FloydContext::RequestVote: my last_log is %lu:%lu, caller is %lu:%lu", + my_log_term, my_log_index, log_term, log_index); + if (log_term < my_log_term + || (log_term == my_log_term && log_index < my_log_index)) { + LOGV(INFO_LEVEL, info_log_, "FloydContext::RequestVote: log index not up-to-date, my is %lu:%lu, caller is %lu:%lu", + my_log_term, my_log_index, log_term, log_index); + return false; // log index is not up-to-date as mine + } + + if (!voted_for_ip_.empty() + && (voted_for_ip_ != ip || voted_for_port_ != port)) { + LOGV(INFO_LEVEL, info_log_, "FloydContext::RequestVote: I have vote for (%s:%d) already.", + voted_for_ip_.c_str(), voted_for_port_); + return false; // I have vote someone else + } + + // Got my vote + voted_for_ip_ = ip; + voted_for_port_ = port; + *my_term = current_term_; + MetaApply(); + LOGV(INFO_LEVEL, info_log_, "FloydContext::RequestVote: grant vote for (%s:%d)," + " with my_term(%lu), my last_log(%lu:%lu), caller log(%lu,%lu).", + voted_for_ip_.c_str(), voted_for_port_, *my_term, + my_log_term, my_log_index, log_term, log_index); + return true; + BuildRequestVoteResponse(my_term, granted, response); +} + +void FloydImpl::ReplyAppendEntries(CmdRequest& cmd, CmdResponse* response) { + // Ignore stale term + MutexLock l(context_->commit_mu_); + bool status = false; + uint64_t my_term = context_->current_term; + CmdRequest_AppendEntries append_entries = cmd.append_entries(); + // if the append entries term is smaller then my_term, then the caller must an older leader + if (append_entries.term() < my_term) { + BuildAppendEntriesResponse(status, my_term, raft_log_->GetLastLogIndex(), response); + return; + } + // TODO(ba0tiao) why we need become follower, maybe we have been follower before + context_->BecomeFollower(append_entries.term(), + append_entries.ip(), append_entries.port()); + + std::vector entries; + for (auto& it : *(cmd.mutable_append_entries()->mutable_entries())) { + entries.push_back(&it); + } + // TODO(ba0tiao) do consistency check here + + /* + * std::string text_format; + * google::protobuf::TextFormat::PrintToString(cmd, &text_format); + * LOGV(DEBUG_LEVEL, context_->info_log(), "FloydImpl::ReplyAppendEntries with %llu " + * "entries, message :\n%s", + * entries.size(), text_format.c_str()); + */ + + // Append entries + status = context_->ReceiverDoAppendEntries(append_entries.term(), + append_entries.prev_log_term(), + append_entries.prev_log_index(), + entries, &my_term); + + // only when follower successfully do appendentries, we will update commit index + if (status) { + context_->AdvanceFollowerCommitIndex(append_entries.leader_commit()); + LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries after AdvanceCommitIndex %lu", + context_->commit_index()); + apply_->ScheduleApply(); + } + + // TODO(anan) ElectLeader timer may timeout because of slow AppendEntries + // we delay reset timer. + primary_->ResetElectLeaderTimer(); + BuildAppendEntriesResponse(status, my_term, raft_log_->GetLastLogIndex(), response); +} + } // namespace floyd diff --git a/floyd/src/floyd_primary_thread.cc b/floyd/src/floyd_primary_thread.cc index e751e42..8d2f641 100644 --- a/floyd/src/floyd_primary_thread.cc +++ b/floyd/src/floyd_primary_thread.cc @@ -184,7 +184,7 @@ void FloydPrimary::LaunchAdvanceCommitIndex(void *arg) { } LOGV(DEBUG_LEVEL, ptr->context_->info_log(), "FloydPrimary::AdvanceCommitIndex" " new_commit_index=%llu", new_commit_index); - if (ptr->context_->AdvanceCommitIndex(new_commit_index)) { + if (ptr->context_->AdvanceLeaderCommitIndex(new_commit_index)) { LOGV(DEBUG_LEVEL, ptr->context_->info_log(), "FloydPrimary::AdvanceCommitIndex ok, ScheduleApply"); ptr->apply_->ScheduleApply(); } diff --git a/floyd/src/floyd_primary_thread.h b/floyd/src/floyd_primary_thread.h index 8815d74..49dd6db 100644 --- a/floyd/src/floyd_primary_thread.h +++ b/floyd/src/floyd_primary_thread.h @@ -27,7 +27,6 @@ class FloydPrimary; class FloydContext; class FloydApply; -// TODO(anan) typedef twice instead of include ? class Peer; typedef std::map PeersSet; @@ -73,9 +72,6 @@ class FloydPrimary { static void LaunchLeaderHeartbeat(void *arg); static void LaunchCheckElectLeader(void *arg); - // void LeaderHeartbeat(); - // void CheckElectLeader(); - static void LaunchBecomeLeader(void *arg); static void LaunchNewCommand(void *arg); static void LaunchAdvanceCommitIndex(void *arg); diff --git a/floyd/src/raft_log.cc b/floyd/src/raft_log.cc index 2f59bb9..da5d705 100644 --- a/floyd/src/raft_log.cc +++ b/floyd/src/raft_log.cc @@ -17,7 +17,8 @@ namespace floyd { static const std::string kCurrentTerm = "CURRENTTERM"; static const std::string kVoteForIp = "VOTEFORIP"; static const std::string kVoteForPort = "VOTEFORPORT"; -static const std::string kApplyIndex = "APPLYINDEX"; +static const std::string kCommitIndex = "COMMITINDEX"; +static const std::string kLastApplied = "LASTAPPLIED"; extern std::string UintToBitStr(const uint64_t num) { char buf[8]; @@ -34,9 +35,10 @@ extern uint64_t BitStrToUint(const std::string &str) { RaftLog::RaftLog(const std::string &path, Logger *info_log) : + info_log_(info_log), last_log_index_(0), - last_applied_(0), - info_log_(info_log) { + commit_index_(0), + last_applied_(0) { rocksdb::Options options; options.create_if_missing = true; rocksdb::Status s = rocksdb::DB::Open(options, path, &log_db_); @@ -50,14 +52,19 @@ RaftLog::RaftLog(const std::string &path, Logger *info_log) : it->Prev(); it->Prev(); it->Prev(); + it->Prev(); last_log_index_ = BitStrToUint(it->key().ToString()); } std::string res; - s = log_db_->Get(rocksdb::ReadOptions(), kApplyIndex, &res); + s = log_db_->Get(rocksdb::ReadOptions(), kLastApplied, &res); if (s.ok()) { memcpy(&last_applied_, res.data(), sizeof(uint64_t)); } + s = log_db_->Get(rocksdb::ReadOptions(), kCommitIndex, &res); + if (s.ok()) { + memcpy(&commit_index_, res.data(), sizeof(uint64_t)); + } } RaftLog::~RaftLog() { @@ -151,7 +158,7 @@ bool RaftLog::GetLastLogTermAndIndex(uint64_t* last_log_term, uint64_t* last_log } void RaftLog::UpdateMetadata(uint64_t current_term, std::string voted_for_ip, - int32_t voted_for_port, uint64_t last_applied) { + int32_t voted_for_port) { char buf[8]; memcpy(buf, ¤t_term, sizeof(uint64_t)); log_db_->Put(rocksdb::WriteOptions(), kCurrentTerm, std::string(buf, 8)); @@ -164,7 +171,14 @@ void RaftLog::UpdateLastApplied(uint64_t last_applied) { last_applied_ = last_applied; char buf[8]; memcpy(buf, &last_applied, sizeof(uint64_t)); - log_db_->Put(rocksdb::WriteOptions(), kApplyIndex, std::string(buf, 8)); + log_db_->Put(rocksdb::WriteOptions(), kLastApplied, std::string(buf, 8)); +} + +void RaftLog::UpdateCommitIndex(uint64_t commit_index) { + commit_index_ = commit_index; + char buf[8]; + memcpy(buf, &commit_index_, sizeof(uint64_t)); + log_db_->Put(rocksdb::WriteOptions(), kCommitIndex, std::string(buf, 8)); } int RaftLog::TruncateSuffix(uint64_t index) { diff --git a/floyd/src/raft_log.h b/floyd/src/raft_log.h index 98e35b5..5b1ae78 100644 --- a/floyd/src/raft_log.h +++ b/floyd/src/raft_log.h @@ -31,7 +31,7 @@ class RaftLog { bool GetLastLogTermAndIndex(uint64_t* last_log_term, uint64_t* last_log_index); void UpdateMetadata(uint64_t current_term, std::string voted_for_ip, - int32_t voted_for_port, uint64_t last_applied); + int32_t voted_for_port); int TruncateSuffix(uint64_t index); // return persistent state from zeppelin @@ -47,6 +47,10 @@ class RaftLog { uint64_t last_log_index() { return last_log_index_; } + uint64_t commit_index() { + return commit_index_; + } + void UpdateCommitIndex(uint64_t commit_index); private: std::string path_; @@ -56,16 +60,18 @@ class RaftLog { */ slash::Mutex lli_mutex_; + rocksdb::DB* log_db_; + + Logger* info_log_; /* * we don't store last_log_index_ in rocksdb, since if we store it in rocksdb * we need update it every time I append an entry. * so we need update it when we open db */ uint64_t last_log_index_; - uint64_t last_applied_; - rocksdb::DB* log_db_; - Logger* info_log_; + uint64_t commit_index_; + uint64_t last_applied_; RaftLog(const RaftLog&); void operator=(const RaftLog&); }; // RaftLog diff --git a/floyd/third/pink b/floyd/third/pink index e3f7240..fdde43c 160000 --- a/floyd/third/pink +++ b/floyd/third/pink @@ -1 +1 @@ -Subproject commit e3f72400aab251313a7333097e6970d09aa96655 +Subproject commit fdde43cdca8c3cb7540f559a2a8ee830d7dd2483 diff --git a/floyd/tools/Makefile b/floyd/tools/Makefile new file mode 100644 index 0000000..72f27d4 --- /dev/null +++ b/floyd/tools/Makefile @@ -0,0 +1,66 @@ +CXX = g++ +ifeq ($(__PERF), 1) + CXXFLAGS = -O0 -g -pipe -fPIC -DNDEBUG -DLOG_LEVEL=LEVEL_INFO -W -Wwrite-strings -Wpointer-arith -Wreorder -Wswitch -Wsign-promo -Wredundant-decls -Wformat -Wall -D_GNU_SOURCE -std=c++11 -D__STDC_FORMAT_MACROS -std=c++11 -gdwarf-2 -Wno-redundant-decls -Wno-unused-variable -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DOS_LINUX +else + CXXFLAGS = -pg -O2 -ggdb3 -pipe -fPIC -W -Wwrite-strings -Wpointer-arith -Wreorder -Wswitch -Wsign-promo -Wredundant-decls -Wformat -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -std=c++11 -gdwarf-2 -Wno-redundant-decls -Wno-unused-variable -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DOS_LINUX +endif + +OBJECT = read_rock read_floyd cpt +SRC_DIR = ./ +THIRD_PATH = ../third +OUTPUT = ./output + +LIB_PATH = -L../lib/ \ + -L$(THIRD_PATH)/slash/slash/lib/ \ + -L$(THIRD_PATH)/rocksdb/ \ + -L$(THIRD_PATH)/pink/pink/lib/ + + +LIBS = -lpink \ + -lslash \ + -lrocksdb \ + -lsnappy \ + -lprotobuf \ + -lz \ + -lbz2 \ + -lrt \ + -lssl \ + -lcrypto \ + -lpthread + +INCLUDE_PATH = -I../../ \ + -I$(THIRD_PATH)/rocksdb/include \ + -I$(THIRD_PATH)/slash/ \ + -I$(THIRD_PATH)/pink/ + +.PHONY: all clean version + + +BASE_BOJS := $(wildcard $(SRC_DIR)/*.cc) +BASE_BOJS += $(wildcard $(SRC_DIR)/*.c) +BASE_BOJS += $(wildcard $(SRC_DIR)/*.cpp) +OBJS = $(patsubst %.cc,%.o,$(BASE_BOJS)) + +all: $(OBJECT) + rm -rf $(OUTPUT) + mkdir -p $(OUTPUT) + mkdir -p $(OUTPUT)/bin + cp $(OBJECT) $(OUTPUT)/bin/ + rm -rf $(OBJECT) + @echo "Success, go, go, go..." + +read_rock: read_rock.cc + $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) +cpt: cpt.cc + $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) +read_floyd: read_floyd.cc + $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) +$(OBJS): %.o : %.cc + $(CXX) $(CXXFLAGS) -c $< -o $@ $(INCLUDE_PATH) + +clean: + rm -rf $(OUTPUT) + rm -rf $(SRC_DIR)/log + rm -rf ./data* + rm -f $(SRC_DIR)/*.o + rm -rf $(OBJECT) diff --git a/floyd/tools/README b/floyd/tools/README new file mode 100644 index 0000000..5c873ba --- /dev/null +++ b/floyd/tools/README @@ -0,0 +1,3 @@ +read_floyd is the tool to read floyd log +read_rock is the tool to read rocksdb data +cpt is a tool to compare two rocksdb, if they have the same data return true, else return false diff --git a/floyd/tools/construct_log.cc b/floyd/tools/construct_log.cc new file mode 100644 index 0000000..eebce63 --- /dev/null +++ b/floyd/tools/construct_log.cc @@ -0,0 +1,19 @@ +#include + +#include "rocksdb/db.h" +#include "floyd/src/floyd.pb.h" +#include + +static const std::string kCurrentTerm = "CURRENTTERM"; +static const std::string kVoteForIp = "VOTEFORIP"; +static const std::string kVoteForPort = "VOTEFORPORT"; +static const std::string kApplyIndex = "APPLYINDEX"; + +int main() +{ + rocksdb::DB* db; + rocksdb::Options options; + rocksdb::Status s = rocksdb::DB::Open(options, "./data1/log", &db); + log_db_->Put(rocksdb::WriteOptions(), kVoteForPort, std::string(buf, 4)); + return 0; +} diff --git a/floyd/example/simple/cpt.cc b/floyd/tools/cpt.cc similarity index 100% rename from floyd/example/simple/cpt.cc rename to floyd/tools/cpt.cc diff --git a/floyd/example/simple/read_floyd.cc b/floyd/tools/read_floyd.cc similarity index 100% rename from floyd/example/simple/read_floyd.cc rename to floyd/tools/read_floyd.cc diff --git a/floyd/example/simple/read_rock.cc b/floyd/tools/read_rock.cc similarity index 100% rename from floyd/example/simple/read_rock.cc rename to floyd/tools/read_rock.cc From 1a245854c5fac919bd7e6ce4998532cacaca0587 Mon Sep 17 00:00:00 2001 From: baotiao Date: Tue, 18 Jul 2017 16:00:38 +0800 Subject: [PATCH 03/17] update --- floyd/src/floyd_context.cc | 45 ++++--------------------------- floyd/src/floyd_context.h | 10 ++++--- floyd/src/floyd_impl.cc | 31 ++++++++------------- floyd/src/floyd_peer_thread.cc | 22 --------------- floyd/src/floyd_primary_thread.cc | 5 ---- 5 files changed, 22 insertions(+), 91 deletions(-) diff --git a/floyd/src/floyd_context.cc b/floyd/src/floyd_context.cc index 6e67c71..b3d3e6c 100644 --- a/floyd/src/floyd_context.cc +++ b/floyd/src/floyd_context.cc @@ -79,44 +79,15 @@ uint64_t FloydContext::GetElectLeaderTimeout() { void FloydContext::BecomeFollower(uint64_t new_term, const std::string leader_ip, int leader_port) { - slash::RWLock l(&stat_rw_, true); - LOGV(DEBUG_LEVEL, info_log_, "BecomeFollower: with current_term_(%lu) and new_term(%lu)" - " commit_index(%lu) last_applied(%lu)", - current_term_, new_term, commit_index(), last_applied()); - //TODO(anan) BecameCandidate will conflict this assert - //assert(current_term_ <= new_term); - if (current_term_ > new_term) { - return; - } - if (current_term_ < new_term) { - current_term_ = new_term; - voted_for_ip_ = ""; - voted_for_port_ = 0; - MetaApply(); - } - if (!leader_ip.empty() && leader_port != 0) { - leader_ip_ = leader_ip; - leader_port_ = leader_port; - } + current_term_ = new_term; + voted_for_ip_ = ""; + voted_for_port_ = 0; + leader_ip_ = leader_ip; + leader_port_ = leader_port; role_ = Role::kFollower; } void FloydContext::BecomeCandidate() { - assert(role_ == Role::kFollower || role_ == Role::kCandidate); - slash::RWLock l(&stat_rw_, true); - switch(role_) { - case Role::kFollower: - LOGV(INFO_LEVEL, info_log_, "Become Candidate since prev leader timeout, prev term: %lu, prev leader is (%s:%d)", - current_term_, leader_ip_.c_str(), leader_port_); - break; - case Role::kCandidate: - LOGV(INFO_LEVEL, info_log_, "Become Candidate since prev election timeout, prev term: %lu", - current_term_, leader_ip_.c_str(), leader_port_); - break; - default: - LOGV(INFO_LEVEL, info_log_, "Become Candidate, should not be here, role: %d", role_); - } - current_term_++; role_ = Role::kCandidate; leader_ip_.clear(); @@ -124,15 +95,9 @@ void FloydContext::BecomeCandidate() { voted_for_ip_ = options_.local_ip; voted_for_port_ = options_.local_port; vote_quorum_ = 1; - MetaApply(); } void FloydContext::BecomeLeader() { - slash::RWLock l(&stat_rw_, true); - if (role_ == Role::kLeader) { - LOGV(INFO_LEVEL, info_log_, "FloydContext::BecomeLeader already Leader!!"); - return; - } role_ = Role::kLeader; leader_ip_ = options_.local_ip; leader_port_ = options_.local_port; diff --git a/floyd/src/floyd_context.h b/floyd/src/floyd_context.h index 7c5308d..5bc6498 100644 --- a/floyd/src/floyd_context.h +++ b/floyd/src/floyd_context.h @@ -121,7 +121,13 @@ class FloydContext { } void ApplyDone(uint64_t index); + void MetaApply(); + // Commit related + slash::Mutex commit_mu_; + + pthread_rwlock_t stat_rw_; + private: Options options_; RaftLog* raft_log_; @@ -130,7 +136,6 @@ class FloydContext { Logger* info_log_; // Role related - pthread_rwlock_t stat_rw_; uint64_t current_term_; Role role_; @@ -140,8 +145,6 @@ class FloydContext { int leader_port_; uint32_t vote_quorum_; - // Commit related - slash::Mutex commit_mu_; uint64_t commit_index_; // Apply related @@ -149,7 +152,6 @@ class FloydContext { slash::CondVar apply_cond_; uint64_t last_applied_; - void MetaApply(); }; } // namespace floyd diff --git a/floyd/src/floyd_impl.cc b/floyd/src/floyd_impl.cc index 2cf62c3..6f0921e 100644 --- a/floyd/src/floyd_impl.cc +++ b/floyd/src/floyd_impl.cc @@ -556,8 +556,9 @@ void FloydImpl::ReplyRequestVote(const CmdRequest& cmd, CmdResponse* response) { LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyRequestVote: my_term=%lu rqv.term=%lu", my_term, request_vote.term()); MutexLock l(context_->commit_mu_); - uint64_t my_current_term = context_->current_term; - uint64_t my_last_log_index = context_->last_log_index; + uint64_t my_current_term = context_->current_term(); + uint64_t my_last_log_index = context_->last_log_index(); + context_->raft_log_->GetLastLogTermAndIndex(&my_log_term, &my_log_index); // if caller's term smaller than my term, then I will notice him if (request_vote.last_log_term() < my_current_term) { BuildRequestVoteResponse(my_current_term, granted, response); @@ -566,26 +567,16 @@ void FloydImpl::ReplyRequestVote(const CmdRequest& cmd, CmdResponse* response) { // if votedfor is null or candidateId, and candidated's log is at least as up-to-date // as receiver's log, grant vote - if (request_vote.last_log_term() > my_current_term || + if (!voted_for_ip_.empty() && (voted_for_ip_ != ip || voted_for_port_ != port) (request_vote.last_log_term() == my_current_term && request_vote.last_log_index() >= my_last_log_index)) { - BecomeFollower(request_vote.term()); + + LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyRequestVote: BecomeFollower with current_term_(%lu) and new_term(%lu)" + " commit_index(%lu) last_applied(%lu)", + my_current_term, request_vote.last_log_term(), my_last_log_index, ()); + context_->BecomeFollower(request_vote.term()); primary_->ResetElectLeaderTimer(); - } - raft_log_->GetLastLogTermAndIndex(&my_log_term, &my_log_index); - LOGV(DEBUG_LEVEL, info_log_, "FloydContext::RequestVote: my last_log is %lu:%lu, caller is %lu:%lu", - my_log_term, my_log_index, log_term, log_index); - if (log_term < my_log_term - || (log_term == my_log_term && log_index < my_log_index)) { - LOGV(INFO_LEVEL, info_log_, "FloydContext::RequestVote: log index not up-to-date, my is %lu:%lu, caller is %lu:%lu", - my_log_term, my_log_index, log_term, log_index); - return false; // log index is not up-to-date as mine - } - - if (!voted_for_ip_.empty() - && (voted_for_ip_ != ip || voted_for_port_ != port)) { - LOGV(INFO_LEVEL, info_log_, "FloydContext::RequestVote: I have vote for (%s:%d) already.", - voted_for_ip_.c_str(), voted_for_port_); - return false; // I have vote someone else + BuildRequestVoteResponse(my_current_term, granted, response); + return ; } // Got my vote diff --git a/floyd/src/floyd_peer_thread.cc b/floyd/src/floyd_peer_thread.cc index 9a7f667..bf4dbf9 100644 --- a/floyd/src/floyd_peer_thread.cc +++ b/floyd/src/floyd_peer_thread.cc @@ -64,16 +64,6 @@ void Peer::RequestVoteRPCWrapper(void *arg) { } Status Peer::RequestVoteRPC() { - /* - * 这里为什么需要判断一下是否是 candidate, 如果保证调用requestvote 之前就肯定是candidate - * 就不需要这个保证了吧 - */ - if (context_->role() != Role::kCandidate) { - LOGV(DEBUG_LEVEL, context_->info_log(), "Peer(%s) not candidate," - "skip RequestVote", server_.c_str()); - return Status::OK(); - } - // TODO (anan) log->getEntry() need lock uint64_t last_log_term; uint64_t last_log_index; @@ -172,20 +162,8 @@ void Peer::AppendEntriesRPCWrapper(void *arg) { } Status Peer::AppendEntriesRPC() { - // 这里想的是如果我不是Leader 我就不应该走下面这个逻辑, 但是实际上及时 - // 这个判断完成以后, 接下来还是有可能last_log_index 获取的时候是leader - // - if (context_->role() != Role::kLeader) { - LOGV(WARN_LEVEL, context_->info_log(), "Peer(%s) not leader anymore," - "skip AppendEntries", server_.c_str()); - return Status::OK(); - } - uint64_t last_log_index = raft_log_->GetLastLogIndex(); uint64_t prev_log_index = next_index_ - 1; - if (prev_log_index > last_log_index) { - return Status::InvalidArgument("prev_Log_index > last_log_index"); - } uint64_t prev_log_term = 0; if (prev_log_index != 0) { diff --git a/floyd/src/floyd_primary_thread.cc b/floyd/src/floyd_primary_thread.cc index 8d2f641..862c96f 100644 --- a/floyd/src/floyd_primary_thread.cc +++ b/floyd/src/floyd_primary_thread.cc @@ -147,11 +147,6 @@ void FloydPrimary::LaunchCheckElectLeader(void *arg) { void FloydPrimary::LaunchBecomeLeader(void *arg) { FloydPrimary* ptr = static_cast(arg); - LOGV(DEBUG_LEVEL, ptr->context_->info_log(), "FloydPrimary::LaunchBecomeLeader"); - if (ptr->context_->role() == Role::kLeader) { - LOGV(WARN_LEVEL, ptr->context_->info_log(), "FloydPrimary::BecomeLeader already Leader"); - return; - } LOGV(DEBUG_LEVEL, ptr->context_->info_log(), "FloydPrimary::BecomeLeader"); ptr->context_->BecomeLeader(); ptr->NoticePeerTask(kBecomeLeader); From 9e3ebef888c227935bb260c09febced3ddde0a55 Mon Sep 17 00:00:00 2001 From: baotiao Date: Tue, 18 Jul 2017 19:17:36 +0800 Subject: [PATCH 04/17] stash --- floyd/src/floyd_client_pool.cc | 2 +- floyd/src/floyd_context.cc | 31 ------------------------------- floyd/src/floyd_context.h | 2 +- floyd/src/floyd_impl.cc | 9 +++++---- floyd/src/floyd_peer_thread.cc | 13 ++++++++----- floyd/src/floyd_primary_thread.cc | 3 ++- 6 files changed, 17 insertions(+), 43 deletions(-) diff --git a/floyd/src/floyd_client_pool.cc b/floyd/src/floyd_client_pool.cc index 46ec685..191271f 100644 --- a/floyd/src/floyd_client_pool.cc +++ b/floyd/src/floyd_client_pool.cc @@ -22,7 +22,7 @@ ClientPool::ClientPool(Logger* info_log, int timeout_ms, int retry) } Status ClientPool::SendAndRecv(const std::string& server, const CmdRequest& req, CmdResponse* res) { - // LOGV(DEBUG_LEVEL, info_log_, "Client::SendAndRecv %s cmd to %s", CmdType(req).c_str(), server.c_str()); + LOGV(DEBUG_LEVEL, info_log_, "Client::SendAndRecv %s cmd to %s", CmdType(req).c_str(), server.c_str()); Status ret; char stage = 0; Client *client = GetClient(server); diff --git a/floyd/src/floyd_context.cc b/floyd/src/floyd_context.cc index b3d3e6c..0e7b214 100644 --- a/floyd/src/floyd_context.cc +++ b/floyd/src/floyd_context.cc @@ -175,37 +175,6 @@ Status FloydContext::WaitApply(uint64_t last_applied, uint32_t timeout) { bool FloydContext::ReceiverDoRequestVote(uint64_t term, const std::string ip, int port, uint64_t log_term, uint64_t log_index, uint64_t *my_term) { - slash::RWLock l(&stat_rw_, true); - // TODO(ba0tiao) we don't need judge here, since before coming here, we have make sure - // that term > current_term_ - // if there is some call between the judge and here, the problem is floyd not - // thread safe, add judge can't solve the problem. the right way to solve the - // problem is make the library thread safe - /* - * if (term < current_term_) { - * return false; // stale term - * } - */ - - uint64_t my_log_index; - uint64_t my_log_term; - raft_log_->GetLastLogTermAndIndex(&my_log_term, &my_log_index); - LOGV(DEBUG_LEVEL, info_log_, "FloydContext::RequestVote: my last_log is %lu:%lu, caller is %lu:%lu", - my_log_term, my_log_index, log_term, log_index); - if (log_term < my_log_term - || (log_term == my_log_term && log_index < my_log_index)) { - LOGV(INFO_LEVEL, info_log_, "FloydContext::RequestVote: log index not up-to-date, my is %lu:%lu, caller is %lu:%lu", - my_log_term, my_log_index, log_term, log_index); - return false; // log index is not up-to-date as mine - } - - if (!voted_for_ip_.empty() - && (voted_for_ip_ != ip || voted_for_port_ != port)) { - LOGV(INFO_LEVEL, info_log_, "FloydContext::RequestVote: I have vote for (%s:%d) already.", - voted_for_ip_.c_str(), voted_for_port_); - return false; // I have vote someone else - } - // Got my vote voted_for_ip_ = ip; voted_for_port_ = port; diff --git a/floyd/src/floyd_context.h b/floyd/src/floyd_context.h index 5bc6498..b68dab1 100644 --- a/floyd/src/floyd_context.h +++ b/floyd/src/floyd_context.h @@ -125,6 +125,7 @@ class FloydContext { // Commit related slash::Mutex commit_mu_; + slash::Mutex apply_mu_; pthread_rwlock_t stat_rw_; @@ -148,7 +149,6 @@ class FloydContext { uint64_t commit_index_; // Apply related - slash::Mutex apply_mu_; slash::CondVar apply_cond_; uint64_t last_applied_; diff --git a/floyd/src/floyd_impl.cc b/floyd/src/floyd_impl.cc index 6f0921e..f755076 100644 --- a/floyd/src/floyd_impl.cc +++ b/floyd/src/floyd_impl.cc @@ -556,9 +556,10 @@ void FloydImpl::ReplyRequestVote(const CmdRequest& cmd, CmdResponse* response) { LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyRequestVote: my_term=%lu rqv.term=%lu", my_term, request_vote.term()); MutexLock l(context_->commit_mu_); - uint64_t my_current_term = context_->current_term(); - uint64_t my_last_log_index = context_->last_log_index(); - context_->raft_log_->GetLastLogTermAndIndex(&my_log_term, &my_log_index); + { + uint64_t my_current_term; + uint64_t my_last_log_index; + raft_log_->GetLastLogTermAndIndex(&my_current_term, &my_last_log_index); // if caller's term smaller than my term, then I will notice him if (request_vote.last_log_term() < my_current_term) { BuildRequestVoteResponse(my_current_term, granted, response); @@ -567,7 +568,7 @@ void FloydImpl::ReplyRequestVote(const CmdRequest& cmd, CmdResponse* response) { // if votedfor is null or candidateId, and candidated's log is at least as up-to-date // as receiver's log, grant vote - if (!voted_for_ip_.empty() && (voted_for_ip_ != ip || voted_for_port_ != port) + if (!context_->voted_for_ip_.empty() && (context_->voted_for_ip_ != request_vote.ip() || context_->voted_for_port_ != request_vote.port()) (request_vote.last_log_term() == my_current_term && request_vote.last_log_index() >= my_last_log_index)) { LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyRequestVote: BecomeFollower with current_term_(%lu) and new_term(%lu)" diff --git a/floyd/src/floyd_peer_thread.cc b/floyd/src/floyd_peer_thread.cc index bf4dbf9..25dc3cb 100644 --- a/floyd/src/floyd_peer_thread.cc +++ b/floyd/src/floyd_peer_thread.cc @@ -64,10 +64,11 @@ void Peer::RequestVoteRPCWrapper(void *arg) { } Status Peer::RequestVoteRPC() { - // TODO (anan) log->getEntry() need lock uint64_t last_log_term; uint64_t last_log_index; - context_->raft_log()->GetLastLogTermAndIndex(&last_log_term, &last_log_index); + { + MutexLock l(&context_->commit_mu_); + raft_log_->GetLastLogTermAndIndex(&last_log_term, &last_log_index); uint64_t current_term = context_->current_term(); CmdRequest req; @@ -78,6 +79,7 @@ Status Peer::RequestVoteRPC() { request_vote->set_term(current_term); request_vote->set_last_log_term(last_log_term); request_vote->set_last_log_index(last_log_index); + } #ifndef NDEBUG std::string text_format; @@ -102,6 +104,8 @@ Status Peer::RequestVoteRPC() { #endif // we get term from request vote + { + MutexLock l(&context_->state_mu_); uint64_t res_term = res.request_vote_res().term(); if (result.ok() && context_->role() == Role::kCandidate) { // kOk means RequestVote success, opposite vote for me @@ -115,19 +119,18 @@ Status Peer::RequestVoteRPC() { } } else { // opposite RequestVote fail, maybe opposite has larger term, or opposite has - // longer log. - // if opposite has larger term, this node will become follower + // longer log. if opposite has larger term, this node will become follower // otherwise we will do nothing LOGV(DEBUG_LEVEL, context_->info_log(), "Vote request denied by %s," " res_term=%lu, current_term=%lu", server_.c_str(), res_term, current_term); if (res_term > current_term) { - //TODO(anan) maybe combine these 2 steps context_->BecomeFollower(res_term); primary_->ResetElectLeaderTimer(); } } } + } return result; } diff --git a/floyd/src/floyd_primary_thread.cc b/floyd/src/floyd_primary_thread.cc index 862c96f..5edf7b8 100644 --- a/floyd/src/floyd_primary_thread.cc +++ b/floyd/src/floyd_primary_thread.cc @@ -146,8 +146,9 @@ void FloydPrimary::LaunchCheckElectLeader(void *arg) { } void FloydPrimary::LaunchBecomeLeader(void *arg) { + Mutex l(&ptr->context_->commit_mu_); FloydPrimary* ptr = static_cast(arg); - LOGV(DEBUG_LEVEL, ptr->context_->info_log(), "FloydPrimary::BecomeLeader"); + LOGV(DEBUG_LEVEL, ptr->context_->info_log(), "FloydPrimary::LaunchBecomeLeader"); ptr->context_->BecomeLeader(); ptr->NoticePeerTask(kBecomeLeader); ptr->AddTask(kLeaderHeartbeat); From b0faf6b9000290405db6c626e7390c21bbf444d2 Mon Sep 17 00:00:00 2001 From: baotiao Date: Tue, 18 Jul 2017 21:21:27 +0800 Subject: [PATCH 05/17] fix call pure virtual function in example --- floyd/example/simple/t.cc | 17 +++++++++++------ floyd/example/simple/t1.cc | 5 +++++ floyd/example/simple/t2.cc | 5 +++++ floyd/third/rocksdb | 2 +- floyd/tools/cpt.cc | 4 ++++ 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/floyd/example/simple/t.cc b/floyd/example/simple/t.cc index 9ae1290..ddebcb3 100644 --- a/floyd/example/simple/t.cc +++ b/floyd/example/simple/t.cc @@ -55,12 +55,12 @@ int main() } while (i--) { - std::string mystr[100100]; - for (int i = 0; i < 100000; i++) { - mystr[i] = slash::RandomString(100); - } - f1->GetServerStatus(msg); - printf("%s\n", msg.c_str()); + std::string mystr[100100]; + for (int i = 0; i < 100000; i++) { + mystr[i] = slash::RandomString(100); + } + // f1->GetServerStatus(msg); + // printf("%s\n", msg.c_str()); st = NowMicros(); for (int j = 0; j < 100000; j++) { f1->Write(mystr[j], mystr[j]); @@ -95,5 +95,10 @@ int main() } getchar(); + delete f2; + delete f3; + delete f4; + delete f5; + delete f1; return 0; } diff --git a/floyd/example/simple/t1.cc b/floyd/example/simple/t1.cc index 47c5ec4..0f3a839 100644 --- a/floyd/example/simple/t1.cc +++ b/floyd/example/simple/t1.cc @@ -84,5 +84,10 @@ int main() getchar(); + delete f2; + delete f3; + delete f4; + delete f5; + delete f1; return 0; } diff --git a/floyd/example/simple/t2.cc b/floyd/example/simple/t2.cc index 7d3a9f7..c6a9d47 100644 --- a/floyd/example/simple/t2.cc +++ b/floyd/example/simple/t2.cc @@ -122,5 +122,10 @@ int main() // at last, we will have 300 log, 100 db getchar(); + delete f2; + delete f3; + delete f4; + delete f5; + delete f1; return 0; } diff --git a/floyd/third/rocksdb b/floyd/third/rocksdb index 8e6345d..e14eaa3 160000 --- a/floyd/third/rocksdb +++ b/floyd/third/rocksdb @@ -1 +1 @@ -Subproject commit 8e6345d2df40756c5ef62f3190cb152058fec890 +Subproject commit e14eaa31fd2c211ecb1b0e5036afb539d89f7522 diff --git a/floyd/tools/cpt.cc b/floyd/tools/cpt.cc index 72cd46c..cd61907 100644 --- a/floyd/tools/cpt.cc +++ b/floyd/tools/cpt.cc @@ -42,5 +42,9 @@ int main(int argc, char** argv) return -1; } printf("success, two rocksdb data are same\n"); + delete iter2; + delete iter1; + delete db2; + delete db1; return 0; } From ef5e205e7f633f2a29c7fbd72df034f40df84d80 Mon Sep 17 00:00:00 2001 From: baotiao Date: Tue, 25 Jul 2017 22:04:24 +0800 Subject: [PATCH 06/17] first reconstruct compiler version. mark --- floyd/Makefile | 2 +- floyd/example/simple/Makefile | 10 +- floyd/include/floyd_options.h | 2 +- floyd/proto/floyd.proto | 9 +- floyd/src/floyd.pb.cc | 174 ++++++++++----------- floyd/src/floyd.pb.h | 148 +++++++++--------- floyd/src/floyd_apply.cc | 64 ++++---- floyd/src/floyd_apply.h | 15 +- floyd/src/floyd_context.cc | 241 +++++------------------------ floyd/src/floyd_context.h | 144 ++++------------- floyd/src/floyd_impl.cc | 249 ++++++++++++++++++------------ floyd/src/floyd_impl.h | 22 ++- floyd/src/floyd_options.cc | 20 ++- floyd/src/floyd_peer_thread.cc | 219 +++++++++++++------------- floyd/src/floyd_peer_thread.h | 48 ++++-- floyd/src/floyd_primary_thread.cc | 179 ++++++--------------- floyd/src/floyd_primary_thread.h | 41 ++--- floyd/src/raft_log.cc | 97 ++---------- floyd/src/raft_log.h | 36 +---- floyd/tools/log_parse/floyd.pb.cc | 174 ++++++++++----------- floyd/tools/log_parse/floyd.pb.h | 148 +++++++++--------- 21 files changed, 844 insertions(+), 1198 deletions(-) diff --git a/floyd/Makefile b/floyd/Makefile index 744c373..701504f 100755 --- a/floyd/Makefile +++ b/floyd/Makefile @@ -65,7 +65,7 @@ $(LIBPINK): make -C $(PINK_PATH)/pink __PERF=$(__PERF) SLASH_PATH=$(SLASH_PATH) $(LIBROCKSDB): - make -C $(ROCKSDB_PATH)/ static_lib + make -C $(ROCKSDB_PATH)/ static_lib -DDISABLE_JEMALLOC=1 $(LIBRARY): $(LIBSLASH) $(LIBPINK) $(LIBROCKSDB) $(LIBOBJS) $(OBJS) rm -rf $(LIB_OUTPUT) diff --git a/floyd/example/simple/Makefile b/floyd/example/simple/Makefile index 02424f0..9d91fde 100644 --- a/floyd/example/simple/Makefile +++ b/floyd/example/simple/Makefile @@ -5,7 +5,7 @@ else CXXFLAGS = -pg -O2 -ggdb3 -pipe -fPIC -W -Wwrite-strings -Wpointer-arith -Wreorder -Wswitch -Wsign-promo -Wredundant-decls -Wformat -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -std=c++11 -gdwarf-2 -Wno-redundant-decls -Wno-unused-variable -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DOS_LINUX endif -OBJECT = t read_rock read_floyd cpt t1 t2 t3 +OBJECT = t t1 t2 t3 SRC_DIR = ./ THIRD_PATH = ../../third OUTPUT = ./output @@ -53,14 +53,6 @@ all: $(OBJECT) t: t.o $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) -read_rock: read_rock.cc - $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) - -cpt: cpt.cc - $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) -read_floyd: read_floyd.cc - $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) - t1: t1.cc $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) diff --git a/floyd/include/floyd_options.h b/floyd/include/floyd_options.h index d2f82ca..afa6d78 100644 --- a/floyd/include/floyd_options.h +++ b/floyd/include/floyd_options.h @@ -29,7 +29,7 @@ struct Options { std::string local_ip; int local_port; std::string path; - uint64_t elect_timeout_ms; + uint64_t check_leader_us; uint64_t heartbeat_us; uint64_t append_entries_size_once; uint64_t append_entries_count_once; diff --git a/floyd/proto/floyd.proto b/floyd/proto/floyd.proto index 79f57fa..3e36569 100644 --- a/floyd/proto/floyd.proto +++ b/floyd/proto/floyd.proto @@ -42,10 +42,11 @@ message CmdRequest { optional User user = 3; message RequestVote { - required bytes ip = 1; - required int32 port = 2; - required uint64 last_log_index = 3; - required uint64 last_log_term = 4; + required uint64 term = 1; + required bytes ip = 2; + required int32 port = 3; + required uint64 last_log_index = 4; + required uint64 last_log_term = 5; } optional RequestVote request_vote = 4; diff --git a/floyd/src/floyd.pb.cc b/floyd/src/floyd.pb.cc index 9360af1..ddee480 100644 --- a/floyd/src/floyd.pb.cc +++ b/floyd/src/floyd.pb.cc @@ -145,11 +145,11 @@ void protobuf_AssignDesc_floyd_2eproto() { sizeof(CmdRequest_User)); CmdRequest_RequestVote_descriptor_ = CmdRequest_descriptor_->nested_type(2); static const int CmdRequest_RequestVote_offsets_[5] = { + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, term_), GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, ip_), GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, port_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, term_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, last_log_term_), GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, last_log_index_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, last_log_term_), }; CmdRequest_RequestVote_reflection_ = new ::google::protobuf::internal::GeneratedMessageReflection( @@ -402,9 +402,9 @@ void protobuf_AddDesc_floyd_2eproto() { "quest.AppendEntries\0225\n\rserver_status\030\006 \001" "(\0132\036.floyd.CmdRequest.ServerStatus\032 \n\002Kv" "\022\013\n\003key\030\001 \002(\014\022\r\n\005value\030\002 \001(\014\032 \n\004User\022\n\n\002" - "ip\030\001 \002(\014\022\014\n\004port\030\002 \002(\005\032d\n\013RequestVote\022\n\n" - "\002ip\030\001 \002(\014\022\014\n\004port\030\002 \002(\005\022\014\n\004term\030\003 \002(\004\022\025\n" - "\rlast_log_term\030\004 \002(\004\022\026\n\016last_log_index\030\005" + "ip\030\001 \002(\014\022\014\n\004port\030\002 \002(\005\032d\n\013RequestVote\022\014\n" + "\004term\030\001 \002(\004\022\n\n\002ip\030\002 \002(\014\022\014\n\004port\030\003 \002(\005\022\026\n" + "\016last_log_index\030\004 \002(\004\022\025\n\rlast_log_term\030\005" " \002(\004\032\234\001\n\rAppendEntries\022\n\n\002ip\030\001 \002(\014\022\014\n\004po" "rt\030\002 \002(\005\022\014\n\004term\030\003 \002(\004\022\026\n\016prev_log_index" "\030\004 \002(\004\022\025\n\rprev_log_term\030\005 \002(\004\022\025\n\rleader_" @@ -1415,11 +1415,11 @@ ::google::protobuf::Metadata CmdRequest_User::GetMetadata() const { // ------------------------------------------------------------------- #ifndef _MSC_VER +const int CmdRequest_RequestVote::kTermFieldNumber; const int CmdRequest_RequestVote::kIpFieldNumber; const int CmdRequest_RequestVote::kPortFieldNumber; -const int CmdRequest_RequestVote::kTermFieldNumber; -const int CmdRequest_RequestVote::kLastLogTermFieldNumber; const int CmdRequest_RequestVote::kLastLogIndexFieldNumber; +const int CmdRequest_RequestVote::kLastLogTermFieldNumber; #endif // !_MSC_VER CmdRequest_RequestVote::CmdRequest_RequestVote() @@ -1438,11 +1438,11 @@ CmdRequest_RequestVote::CmdRequest_RequestVote(const CmdRequest_RequestVote& fro void CmdRequest_RequestVote::SharedCtor() { _cached_size_ = 0; + term_ = GOOGLE_ULONGLONG(0); ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); port_ = 0; - term_ = GOOGLE_ULONGLONG(0); - last_log_term_ = GOOGLE_ULONGLONG(0); last_log_index_ = GOOGLE_ULONGLONG(0); + last_log_term_ = GOOGLE_ULONGLONG(0); ::memset(_has_bits_, 0, sizeof(_has_bits_)); } @@ -1481,15 +1481,15 @@ CmdRequest_RequestVote* CmdRequest_RequestVote::New() const { void CmdRequest_RequestVote::Clear() { if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + term_ = GOOGLE_ULONGLONG(0); if (has_ip()) { if (ip_ != &::google::protobuf::internal::kEmptyString) { ip_->clear(); } } port_ = 0; - term_ = GOOGLE_ULONGLONG(0); - last_log_term_ = GOOGLE_ULONGLONG(0); last_log_index_ = GOOGLE_ULONGLONG(0); + last_log_term_ = GOOGLE_ULONGLONG(0); } ::memset(_has_bits_, 0, sizeof(_has_bits_)); mutable_unknown_fields()->Clear(); @@ -1501,76 +1501,76 @@ bool CmdRequest_RequestVote::MergePartialFromCodedStream( ::google::protobuf::uint32 tag; while ((tag = input->ReadTag()) != 0) { switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { - // required bytes ip = 1; + // required uint64 term = 1; case 1: { if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->mutable_ip())); + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &term_))); + set_has_term(); } else { goto handle_uninterpreted; } - if (input->ExpectTag(16)) goto parse_port; + if (input->ExpectTag(18)) goto parse_ip; break; } - // required int32 port = 2; + // required bytes ip = 2; case 2: { if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { - parse_port: - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( - input, &port_))); - set_has_port(); + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_ip: + DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( + input, this->mutable_ip())); } else { goto handle_uninterpreted; } - if (input->ExpectTag(24)) goto parse_term; + if (input->ExpectTag(24)) goto parse_port; break; } - // required uint64 term = 3; + // required int32 port = 3; case 3: { if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { - parse_term: + parse_port: DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( - input, &term_))); - set_has_term(); + ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + input, &port_))); + set_has_port(); } else { goto handle_uninterpreted; } - if (input->ExpectTag(32)) goto parse_last_log_term; + if (input->ExpectTag(32)) goto parse_last_log_index; break; } - // required uint64 last_log_term = 4; + // required uint64 last_log_index = 4; case 4: { if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { - parse_last_log_term: + parse_last_log_index: DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( - input, &last_log_term_))); - set_has_last_log_term(); + input, &last_log_index_))); + set_has_last_log_index(); } else { goto handle_uninterpreted; } - if (input->ExpectTag(40)) goto parse_last_log_index; + if (input->ExpectTag(40)) goto parse_last_log_term; break; } - // required uint64 last_log_index = 5; + // required uint64 last_log_term = 5; case 5: { if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { - parse_last_log_index: + parse_last_log_term: DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( - input, &last_log_index_))); - set_has_last_log_index(); + input, &last_log_term_))); + set_has_last_log_term(); } else { goto handle_uninterpreted; } @@ -1596,30 +1596,30 @@ bool CmdRequest_RequestVote::MergePartialFromCodedStream( void CmdRequest_RequestVote::SerializeWithCachedSizes( ::google::protobuf::io::CodedOutputStream* output) const { - // required bytes ip = 1; + // required uint64 term = 1; + if (has_term()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(1, this->term(), output); + } + + // required bytes ip = 2; if (has_ip()) { ::google::protobuf::internal::WireFormatLite::WriteBytes( - 1, this->ip(), output); + 2, this->ip(), output); } - // required int32 port = 2; + // required int32 port = 3; if (has_port()) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(2, this->port(), output); + ::google::protobuf::internal::WireFormatLite::WriteInt32(3, this->port(), output); } - // required uint64 term = 3; - if (has_term()) { - ::google::protobuf::internal::WireFormatLite::WriteUInt64(3, this->term(), output); + // required uint64 last_log_index = 4; + if (has_last_log_index()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(4, this->last_log_index(), output); } - // required uint64 last_log_term = 4; + // required uint64 last_log_term = 5; if (has_last_log_term()) { - ::google::protobuf::internal::WireFormatLite::WriteUInt64(4, this->last_log_term(), output); - } - - // required uint64 last_log_index = 5; - if (has_last_log_index()) { - ::google::protobuf::internal::WireFormatLite::WriteUInt64(5, this->last_log_index(), output); + ::google::protobuf::internal::WireFormatLite::WriteUInt64(5, this->last_log_term(), output); } if (!unknown_fields().empty()) { @@ -1630,31 +1630,31 @@ void CmdRequest_RequestVote::SerializeWithCachedSizes( ::google::protobuf::uint8* CmdRequest_RequestVote::SerializeWithCachedSizesToArray( ::google::protobuf::uint8* target) const { - // required bytes ip = 1; + // required uint64 term = 1; + if (has_term()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(1, this->term(), target); + } + + // required bytes ip = 2; if (has_ip()) { target = ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( - 1, this->ip(), target); + 2, this->ip(), target); } - // required int32 port = 2; + // required int32 port = 3; if (has_port()) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(2, this->port(), target); + target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(3, this->port(), target); } - // required uint64 term = 3; - if (has_term()) { - target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(3, this->term(), target); + // required uint64 last_log_index = 4; + if (has_last_log_index()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(4, this->last_log_index(), target); } - // required uint64 last_log_term = 4; + // required uint64 last_log_term = 5; if (has_last_log_term()) { - target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(4, this->last_log_term(), target); - } - - // required uint64 last_log_index = 5; - if (has_last_log_index()) { - target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(5, this->last_log_index(), target); + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(5, this->last_log_term(), target); } if (!unknown_fields().empty()) { @@ -1668,41 +1668,41 @@ int CmdRequest_RequestVote::ByteSize() const { int total_size = 0; if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - // required bytes ip = 1; + // required uint64 term = 1; + if (has_term()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->term()); + } + + // required bytes ip = 2; if (has_ip()) { total_size += 1 + ::google::protobuf::internal::WireFormatLite::BytesSize( this->ip()); } - // required int32 port = 2; + // required int32 port = 3; if (has_port()) { total_size += 1 + ::google::protobuf::internal::WireFormatLite::Int32Size( this->port()); } - // required uint64 term = 3; - if (has_term()) { + // required uint64 last_log_index = 4; + if (has_last_log_index()) { total_size += 1 + ::google::protobuf::internal::WireFormatLite::UInt64Size( - this->term()); + this->last_log_index()); } - // required uint64 last_log_term = 4; + // required uint64 last_log_term = 5; if (has_last_log_term()) { total_size += 1 + ::google::protobuf::internal::WireFormatLite::UInt64Size( this->last_log_term()); } - // required uint64 last_log_index = 5; - if (has_last_log_index()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::UInt64Size( - this->last_log_index()); - } - } if (!unknown_fields().empty()) { total_size += @@ -1730,21 +1730,21 @@ void CmdRequest_RequestVote::MergeFrom(const ::google::protobuf::Message& from) void CmdRequest_RequestVote::MergeFrom(const CmdRequest_RequestVote& from) { GOOGLE_CHECK_NE(&from, this); if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (from.has_term()) { + set_term(from.term()); + } if (from.has_ip()) { set_ip(from.ip()); } if (from.has_port()) { set_port(from.port()); } - if (from.has_term()) { - set_term(from.term()); + if (from.has_last_log_index()) { + set_last_log_index(from.last_log_index()); } if (from.has_last_log_term()) { set_last_log_term(from.last_log_term()); } - if (from.has_last_log_index()) { - set_last_log_index(from.last_log_index()); - } } mutable_unknown_fields()->MergeFrom(from.unknown_fields()); } @@ -1769,11 +1769,11 @@ bool CmdRequest_RequestVote::IsInitialized() const { void CmdRequest_RequestVote::Swap(CmdRequest_RequestVote* other) { if (other != this) { + std::swap(term_, other->term_); std::swap(ip_, other->ip_); std::swap(port_, other->port_); - std::swap(term_, other->term_); - std::swap(last_log_term_, other->last_log_term_); std::swap(last_log_index_, other->last_log_index_); + std::swap(last_log_term_, other->last_log_term_); std::swap(_has_bits_[0], other->_has_bits_[0]); _unknown_fields_.Swap(&other->_unknown_fields_); std::swap(_cached_size_, other->_cached_size_); diff --git a/floyd/src/floyd.pb.h b/floyd/src/floyd.pb.h index 852d052..f89f5ff 100644 --- a/floyd/src/floyd.pb.h +++ b/floyd/src/floyd.pb.h @@ -514,10 +514,17 @@ class CmdRequest_RequestVote : public ::google::protobuf::Message { // accessors ------------------------------------------------------- - // required bytes ip = 1; + // required uint64 term = 1; + inline bool has_term() const; + inline void clear_term(); + static const int kTermFieldNumber = 1; + inline ::google::protobuf::uint64 term() const; + inline void set_term(::google::protobuf::uint64 value); + + // required bytes ip = 2; inline bool has_ip() const; inline void clear_ip(); - static const int kIpFieldNumber = 1; + static const int kIpFieldNumber = 2; inline const ::std::string& ip() const; inline void set_ip(const ::std::string& value); inline void set_ip(const char* value); @@ -526,53 +533,46 @@ class CmdRequest_RequestVote : public ::google::protobuf::Message { inline ::std::string* release_ip(); inline void set_allocated_ip(::std::string* ip); - // required int32 port = 2; + // required int32 port = 3; inline bool has_port() const; inline void clear_port(); - static const int kPortFieldNumber = 2; + static const int kPortFieldNumber = 3; inline ::google::protobuf::int32 port() const; inline void set_port(::google::protobuf::int32 value); - // required uint64 term = 3; - inline bool has_term() const; - inline void clear_term(); - static const int kTermFieldNumber = 3; - inline ::google::protobuf::uint64 term() const; - inline void set_term(::google::protobuf::uint64 value); + // required uint64 last_log_index = 4; + inline bool has_last_log_index() const; + inline void clear_last_log_index(); + static const int kLastLogIndexFieldNumber = 4; + inline ::google::protobuf::uint64 last_log_index() const; + inline void set_last_log_index(::google::protobuf::uint64 value); - // required uint64 last_log_term = 4; + // required uint64 last_log_term = 5; inline bool has_last_log_term() const; inline void clear_last_log_term(); - static const int kLastLogTermFieldNumber = 4; + static const int kLastLogTermFieldNumber = 5; inline ::google::protobuf::uint64 last_log_term() const; inline void set_last_log_term(::google::protobuf::uint64 value); - // required uint64 last_log_index = 5; - inline bool has_last_log_index() const; - inline void clear_last_log_index(); - static const int kLastLogIndexFieldNumber = 5; - inline ::google::protobuf::uint64 last_log_index() const; - inline void set_last_log_index(::google::protobuf::uint64 value); - // @@protoc_insertion_point(class_scope:floyd.CmdRequest.RequestVote) private: + inline void set_has_term(); + inline void clear_has_term(); inline void set_has_ip(); inline void clear_has_ip(); inline void set_has_port(); inline void clear_has_port(); - inline void set_has_term(); - inline void clear_has_term(); - inline void set_has_last_log_term(); - inline void clear_has_last_log_term(); inline void set_has_last_log_index(); inline void clear_has_last_log_index(); + inline void set_has_last_log_term(); + inline void clear_has_last_log_term(); ::google::protobuf::UnknownFieldSet _unknown_fields_; - ::std::string* ip_; ::google::protobuf::uint64 term_; - ::google::protobuf::uint64 last_log_term_; + ::std::string* ip_; ::google::protobuf::uint64 last_log_index_; + ::google::protobuf::uint64 last_log_term_; ::google::protobuf::int32 port_; mutable int _cached_size_; @@ -2162,16 +2162,38 @@ inline void CmdRequest_User::set_port(::google::protobuf::int32 value) { // CmdRequest_RequestVote -// required bytes ip = 1; -inline bool CmdRequest_RequestVote::has_ip() const { +// required uint64 term = 1; +inline bool CmdRequest_RequestVote::has_term() const { return (_has_bits_[0] & 0x00000001u) != 0; } -inline void CmdRequest_RequestVote::set_has_ip() { +inline void CmdRequest_RequestVote::set_has_term() { _has_bits_[0] |= 0x00000001u; } -inline void CmdRequest_RequestVote::clear_has_ip() { +inline void CmdRequest_RequestVote::clear_has_term() { _has_bits_[0] &= ~0x00000001u; } +inline void CmdRequest_RequestVote::clear_term() { + term_ = GOOGLE_ULONGLONG(0); + clear_has_term(); +} +inline ::google::protobuf::uint64 CmdRequest_RequestVote::term() const { + return term_; +} +inline void CmdRequest_RequestVote::set_term(::google::protobuf::uint64 value) { + set_has_term(); + term_ = value; +} + +// required bytes ip = 2; +inline bool CmdRequest_RequestVote::has_ip() const { + return (_has_bits_[0] & 0x00000002u) != 0; +} +inline void CmdRequest_RequestVote::set_has_ip() { + _has_bits_[0] |= 0x00000002u; +} +inline void CmdRequest_RequestVote::clear_has_ip() { + _has_bits_[0] &= ~0x00000002u; +} inline void CmdRequest_RequestVote::clear_ip() { if (ip_ != &::google::protobuf::internal::kEmptyString) { ip_->clear(); @@ -2232,15 +2254,15 @@ inline void CmdRequest_RequestVote::set_allocated_ip(::std::string* ip) { } } -// required int32 port = 2; +// required int32 port = 3; inline bool CmdRequest_RequestVote::has_port() const { - return (_has_bits_[0] & 0x00000002u) != 0; + return (_has_bits_[0] & 0x00000004u) != 0; } inline void CmdRequest_RequestVote::set_has_port() { - _has_bits_[0] |= 0x00000002u; + _has_bits_[0] |= 0x00000004u; } inline void CmdRequest_RequestVote::clear_has_port() { - _has_bits_[0] &= ~0x00000002u; + _has_bits_[0] &= ~0x00000004u; } inline void CmdRequest_RequestVote::clear_port() { port_ = 0; @@ -2254,37 +2276,37 @@ inline void CmdRequest_RequestVote::set_port(::google::protobuf::int32 value) { port_ = value; } -// required uint64 term = 3; -inline bool CmdRequest_RequestVote::has_term() const { - return (_has_bits_[0] & 0x00000004u) != 0; +// required uint64 last_log_index = 4; +inline bool CmdRequest_RequestVote::has_last_log_index() const { + return (_has_bits_[0] & 0x00000008u) != 0; } -inline void CmdRequest_RequestVote::set_has_term() { - _has_bits_[0] |= 0x00000004u; +inline void CmdRequest_RequestVote::set_has_last_log_index() { + _has_bits_[0] |= 0x00000008u; } -inline void CmdRequest_RequestVote::clear_has_term() { - _has_bits_[0] &= ~0x00000004u; +inline void CmdRequest_RequestVote::clear_has_last_log_index() { + _has_bits_[0] &= ~0x00000008u; } -inline void CmdRequest_RequestVote::clear_term() { - term_ = GOOGLE_ULONGLONG(0); - clear_has_term(); +inline void CmdRequest_RequestVote::clear_last_log_index() { + last_log_index_ = GOOGLE_ULONGLONG(0); + clear_has_last_log_index(); } -inline ::google::protobuf::uint64 CmdRequest_RequestVote::term() const { - return term_; +inline ::google::protobuf::uint64 CmdRequest_RequestVote::last_log_index() const { + return last_log_index_; } -inline void CmdRequest_RequestVote::set_term(::google::protobuf::uint64 value) { - set_has_term(); - term_ = value; +inline void CmdRequest_RequestVote::set_last_log_index(::google::protobuf::uint64 value) { + set_has_last_log_index(); + last_log_index_ = value; } -// required uint64 last_log_term = 4; +// required uint64 last_log_term = 5; inline bool CmdRequest_RequestVote::has_last_log_term() const { - return (_has_bits_[0] & 0x00000008u) != 0; + return (_has_bits_[0] & 0x00000010u) != 0; } inline void CmdRequest_RequestVote::set_has_last_log_term() { - _has_bits_[0] |= 0x00000008u; + _has_bits_[0] |= 0x00000010u; } inline void CmdRequest_RequestVote::clear_has_last_log_term() { - _has_bits_[0] &= ~0x00000008u; + _has_bits_[0] &= ~0x00000010u; } inline void CmdRequest_RequestVote::clear_last_log_term() { last_log_term_ = GOOGLE_ULONGLONG(0); @@ -2298,28 +2320,6 @@ inline void CmdRequest_RequestVote::set_last_log_term(::google::protobuf::uint64 last_log_term_ = value; } -// required uint64 last_log_index = 5; -inline bool CmdRequest_RequestVote::has_last_log_index() const { - return (_has_bits_[0] & 0x00000010u) != 0; -} -inline void CmdRequest_RequestVote::set_has_last_log_index() { - _has_bits_[0] |= 0x00000010u; -} -inline void CmdRequest_RequestVote::clear_has_last_log_index() { - _has_bits_[0] &= ~0x00000010u; -} -inline void CmdRequest_RequestVote::clear_last_log_index() { - last_log_index_ = GOOGLE_ULONGLONG(0); - clear_has_last_log_index(); -} -inline ::google::protobuf::uint64 CmdRequest_RequestVote::last_log_index() const { - return last_log_index_; -} -inline void CmdRequest_RequestVote::set_last_log_index(::google::protobuf::uint64 value) { - set_has_last_log_index(); - last_log_index_ = value; -} - // ------------------------------------------------------------------- // CmdRequest_AppendEntries diff --git a/floyd/src/floyd_apply.cc b/floyd/src/floyd_apply.cc index b816c14..c4a6745 100644 --- a/floyd/src/floyd_apply.cc +++ b/floyd/src/floyd_apply.cc @@ -14,55 +14,65 @@ #include "floyd/src/logger.h" #include "floyd/src/floyd.pb.h" +#include "floyd/src/raft_meta.h" +#include "floyd/src/raft_log.h" namespace floyd { -FloydApply::FloydApply(FloydContext* context, rocksdb::DB* db, RaftLog* raft_log) +FloydApply::FloydApply(FloydContext* context, rocksdb::DB* db, RaftMeta* raft_meta, + RaftLog* raft_log, Logger* info_log) : context_(context), db_(db), - raft_log_(raft_log) { - bg_thread_ = new pink::BGThread(); - bg_thread_->set_thread_name("FloydApply"); + raft_meta_(raft_meta), + raft_log_(raft_log), + info_log_(info_log) { } FloydApply::~FloydApply() { - delete bg_thread_; +} + +int FloydApply::Start() { + bg_thread_.set_thread_name("FloydApply"); + return bg_thread_.StartThread(); } Status FloydApply::ScheduleApply() { - if (bg_thread_->StartThread() != 0) { - return Status::Corruption("Failed to start apply thread"); - } - bg_thread_->Schedule(&ApplyStateMachine, static_cast(this)); - return Status::OK(); + bg_thread_.Schedule(&ApplyStateMachineWrapper, this); } -void FloydApply::ApplyStateMachine(void* arg) { - FloydApply* fapply = static_cast(arg); - FloydContext* context = fapply->context_; +void FloydApply::ApplyStateMachineWrapper(void* arg) { + reinterpret_cast(arg)->ApplyStateMachine(); +} +void FloydApply::ApplyStateMachine() { + uint64_t last_applied = raft_meta_->GetLastApplied(); // Apply as more entry as possible - uint64_t len = 0, to_apply = 0; - to_apply = context->NextApplyIndex(&len); + uint64_t commit_index; + context_->commit_index_mu.Lock(); + commit_index = context_->commit_index; + context_->commit_index_mu.Unlock(); - LOGV(DEBUG_LEVEL, context->info_log(), "FloydApply::ApplyStateMachine: %lu entries to apply from to_apply(%lu)", - len, to_apply); - while (len-- > 0) { + LOGV(DEBUG_LEVEL, info_log_, "FloydApply::ApplyStateMachine: last_applied: %lu, commit_index: %lu", + last_applied, commit_index); + while (++last_applied <= commit_index) { Entry log_entry; - fapply->raft_log_->GetEntry(to_apply, &log_entry); - Status s = fapply->Apply(log_entry); + raft_log_->GetEntry(last_applied, &log_entry); + Status s = Apply(log_entry); if (!s.ok()) { - LOGV(WARN_LEVEL, context->info_log(), "FloydApply::ApplyStateMachine: Apply log entry failed, at: %d, error: %s", - to_apply, s.ToString().c_str()); - fapply->ScheduleApply(); // try once more + LOGV(WARN_LEVEL, info_log_, "FloydApply::ApplyStateMachine: Apply log entry failed, at: %d, error: %s", + last_applied, s.ToString().c_str()); + ScheduleApply(); // try once more usleep(1000000); return; } - context->ApplyDone(to_apply); - to_apply++; + + raft_meta_->SetLastApplied(last_applied); } - fapply->raft_log_->UpdateLastApplied(to_apply - 1); + context_->apply_mu.Lock(); + context_->last_applied = last_applied; + context_->apply_mu.Unlock(); + context_->apply_cond.SignalAll(); } Status FloydApply::Apply(const Entry& entry) { @@ -70,7 +80,7 @@ Status FloydApply::Apply(const Entry& entry) { switch (entry.optype()) { case Entry_OpType_kWrite: ret = db_->Put(rocksdb::WriteOptions(), entry.key(), entry.value()); - LOGV(DEBUG_LEVEL, context_->info_log(), "FloydApply::Apply %s, key(%s) value(%s)", + LOGV(DEBUG_LEVEL, info_log_, "FloydApply::Apply %s, key(%s) value(%s)", ret.ToString().c_str(), entry.key().c_str(), entry.value().c_str()); break; case Entry_OpType_kDelete: diff --git a/floyd/src/floyd_apply.h b/floyd/src/floyd_apply.h index a829895..17764dc 100644 --- a/floyd/src/floyd_apply.h +++ b/floyd/src/floyd_apply.h @@ -12,20 +12,29 @@ #include "pink/include/bg_thread.h" namespace floyd { + using slash::Status; +class RaftMeta; +class RaftLog; +class Logger; + class FloydApply { public: - FloydApply(FloydContext* context, rocksdb::DB* db, RaftLog* raft_log); + FloydApply(FloydContext* context, rocksdb::DB* db, RaftMeta* raft_meta, RaftLog* raft_log, Logger* info_log); ~FloydApply(); + int Start(); Status ScheduleApply(); private: - pink::BGThread* bg_thread_; + pink::BGThread bg_thread_; FloydContext* context_; rocksdb::DB* db_; + RaftMeta* raft_meta_; RaftLog* raft_log_; - static void ApplyStateMachine(void* arg); + Logger* info_log_; + static void ApplyStateMachineWrapper(void* arg); + void ApplyStateMachine(); Status Apply(const Entry& log_entry); }; diff --git a/floyd/src/floyd_context.cc b/floyd/src/floyd_context.cc index 0e7b214..14ddcb9 100644 --- a/floyd/src/floyd_context.cc +++ b/floyd/src/floyd_context.cc @@ -6,242 +6,73 @@ #include "floyd/src/floyd_context.h" #include -#include "floyd/src/logger.h" - #include "slash/include/env.h" -#include "floyd/src/floyd.pb.h" #include "slash/include/xdebug.h" -namespace floyd { - -FloydContext::FloydContext(const floyd::Options& opt, - RaftLog* raft_log, Logger* info_log) - : options_(opt), - raft_log_(raft_log), - info_log_(info_log), - current_term_(0), - role_(Role::kFollower), - voted_for_port_(0), - leader_port_(0), - vote_quorum_(0), - commit_index_(0), - apply_cond_(&apply_mu_), - last_applied_(0) { - pthread_rwlockattr_t attr; - pthread_rwlockattr_init(&attr); - pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); - pthread_rwlock_init(&stat_rw_, &attr); - uint64_t seed = slash::NowMicros() % 100000; - LOGV(INFO_LEVEL, info_log_, "ElectLeader srandom with seed %lu", seed); - srandom(seed); - commit_index_ = raft_log_->commit_index(); - //srandom(slash::NowMicros()); -} +#include "floyd/src/floyd.pb.h" +#include "floyd/src/logger.h" +#include "floyd/src/raft_meta.h" -FloydContext::~FloydContext() { - pthread_rwlock_destroy(&stat_rw_); - LOGV(DEBUG_LEVEL, info_log_, "FloydConext dtor"); -} +namespace floyd { -void FloydContext::RecoverInit() { - assert(raft_log_ != NULL); - slash::RWLock l(&stat_rw_, true); - current_term_ = raft_log_->current_term(); - voted_for_ip_ = raft_log_->voted_for_ip(); - voted_for_port_ = raft_log_->voted_for_port(); - last_applied_ = raft_log_->last_applied(); - role_ = Role::kFollower; +void FloydContext::RecoverInit(RaftMeta *raft_meta) { + current_term = raft_meta->GetCurrentTerm(); + voted_for_ip = raft_meta->GetVotedForIp(); + voted_for_port = raft_meta->GetVotedForPort(); + commit_index = raft_meta->GetCommitIndex(); + role = Role::kFollower; } bool FloydContext::HasLeader() { - if (leader_ip_ == "" || leader_port_ == 0) { + if (leader_ip == "" || leader_port == 0) { return false; } return true; } void FloydContext::leader_node(std::string* ip, int* port) { - slash::RWLock l(&stat_rw_, false); - *ip = leader_ip_; - *port = leader_port_; + *ip = leader_ip; + *port = leader_port; } void FloydContext::voted_for_node(std::string* ip, int* port) { - slash::RWLock l(&stat_rw_, false); - *ip = voted_for_ip_; - *port = voted_for_port_; -} - -uint64_t FloydContext::GetElectLeaderTimeout() { - return rand() % (options_.elect_timeout_ms * 2) + options_.elect_timeout_ms; + *ip = voted_for_ip; + *port = voted_for_port; } void FloydContext::BecomeFollower(uint64_t new_term, - const std::string leader_ip, int leader_port) { - current_term_ = new_term; - voted_for_ip_ = ""; - voted_for_port_ = 0; - leader_ip_ = leader_ip; - leader_port_ = leader_port; - role_ = Role::kFollower; + const std::string _leader_ip, int _leader_port) { + current_term = new_term; + voted_for_ip = ""; + voted_for_port = 0; + leader_ip = _leader_ip; + leader_port = _leader_port; + role = Role::kFollower; } void FloydContext::BecomeCandidate() { - current_term_++; - role_ = Role::kCandidate; - leader_ip_.clear(); - leader_port_ = 0; - voted_for_ip_ = options_.local_ip; - voted_for_port_ = options_.local_port; - vote_quorum_ = 1; + current_term++; + role = Role::kCandidate; + leader_ip.clear(); + leader_port = 0; + voted_for_ip = options.local_ip; + voted_for_port = options.local_port; + vote_quorum = 1; } void FloydContext::BecomeLeader() { - role_ = Role::kLeader; - leader_ip_ = options_.local_ip; - leader_port_ = options_.local_port; - LOGV(INFO_LEVEL, info_log_, "FloydContext::BecomeLeader I am become Leader!!"); -} - -// only leader will call AdvanceCommitIndex -// follower only need set commit as leader's -bool FloydContext::AdvanceLeaderCommitIndex(uint64_t new_commit_index) { - Entry entry; - raft_log_->GetEntry(new_commit_index, &entry); - slash::MutexLock l(&commit_mu_); - LOGV(DEBUG_LEVEL, info_log_, "FloydContext::AdvanceCommitIndex" - "commit_index=%lu, new_commit_index=%lu, entry.term() = %lu, current_term_ = %lu", - commit_index_, new_commit_index, entry.term(), current_term_); - if (entry.term() == current_term_) { - commit_index_ = new_commit_index; - LOGV(DEBUG_LEVEL, info_log_, "FloydContext::AdvanceCommitIndex advance commit_index to %ld", new_commit_index); - raft_log_->UpdateCommitIndex(commit_index_); - return true; - } - return false; -} - -bool FloydContext::AdvanceFollowerCommitIndex(uint64_t new_commit_index) { - // Update log commit index - slash::MutexLock l(&commit_mu_); - commit_index_ = new_commit_index; - raft_log_->UpdateCommitIndex(commit_index_); - return true; -} - -uint64_t FloydContext::NextApplyIndex(uint64_t* len) { - //slash::MutexLock lcommit(&commit_mu_); - uint64_t tcommit_index = commit_index(); - *len = 0; - slash::MutexLock lapply(&apply_mu_); - if (tcommit_index > last_applied_) { - *len = tcommit_index - last_applied_; - } - return last_applied_ + 1; -} - -void FloydContext::ApplyDone(uint64_t index) { - slash::MutexLock lapply(&apply_mu_); - last_applied_ = index; - apply_cond_.SignalAll(); -} - -void FloydContext::MetaApply() { - raft_log_->UpdateMetadata(current_term_, voted_for_ip_, voted_for_port_); -} - -bool FloydContext::VoteAndCheck(uint64_t vote_term) { - slash::RWLock l(&stat_rw_, true); - LOGV(DEBUG_LEVEL, info_log_, "FloydContext::VoteAndCheck: current_term=%lu vote_term=%lu vote_quorum_=%d", - current_term_, vote_term, vote_quorum_); - if (current_term_ != vote_term) { - return false; - } - return (++vote_quorum_) > (options_.members.size() / 2); -} - -Status FloydContext::WaitApply(uint64_t last_applied, uint32_t timeout) { - slash::MutexLock lapply(&apply_mu_); - while (last_applied_ < last_applied) { - if (!apply_cond_.TimedWait(timeout)) { - return Status::Timeout("apply timeout"); - } - } - return Status::OK(); + role = Role::kLeader; + leader_ip = options.local_ip; + leader_port = options.local_port; } // Peer ask my vote with it's ip, port, log_term and log_index -bool FloydContext::ReceiverDoRequestVote(uint64_t term, const std::string ip, - int port, uint64_t log_term, uint64_t log_index, - uint64_t *my_term) { +void FloydContext::GrantVote(uint64_t term, const std::string ip, int port) { // Got my vote - voted_for_ip_ = ip; - voted_for_port_ = port; - *my_term = current_term_; - MetaApply(); - LOGV(INFO_LEVEL, info_log_, "FloydContext::RequestVote: grant vote for (%s:%d)," - " with my_term(%lu), my last_log(%lu:%lu), caller log(%lu,%lu).", - voted_for_ip_.c_str(), voted_for_port_, *my_term, - my_log_term, my_log_index, log_term, log_index); - return true; -} - -bool FloydContext::ReceiverDoAppendEntries(uint64_t term, - uint64_t pre_log_term, uint64_t pre_log_index, - std::vector& entries, uint64_t* my_term) { - slash::RWLock l(&stat_rw_, true); - // Check pre_log match local log entry - uint64_t last_log_index = raft_log_->GetLastLogIndex(); - if (pre_log_index > last_log_index) { - LOGV(DEBUG_LEVEL, info_log_, "FloydContext::ReceiverDoAppendEntries:" - "pre_log(%lu, %lu) > last_log_index(%lu)", pre_log_term, pre_log_index, - last_log_index); - return false; - } - - uint64_t my_log_term = 0; - Entry entry; - LOGV(DEBUG_LEVEL, info_log_, "FloydContext::ReceiverDoAppendEntries" - "pre_log_index: %llu\n", pre_log_index); - if (raft_log_->GetEntry(pre_log_index, &entry) == 0) { - my_log_term = entry.term(); - } else { - LOGV(WARN_LEVEL, info_log_, "FloydContext::ReceiverDoAppendEntries: can't" - "get Entry from raft_log pre_log_index %llu", pre_log_index); - } - - if (pre_log_term != my_log_term) { - LOGV(WARN_LEVEL, info_log_, "FloydContext::ReceiverDoAppendEntries: pre_log(%lu, %lu) don't match with" - " local log(%lu, %lu), truncate suffix from here", - pre_log_term, pre_log_index, my_log_term, last_log_index); - // TruncateSuffix [pre_log_index, last_log_index) - raft_log_->TruncateSuffix(pre_log_index); - return false; - } - - // Append entry - if (pre_log_index < last_log_index) { -#ifndef NDEBUG - uint64_t last_log_term; - raft_log_->GetLastLogTermAndIndex(&last_log_term, &last_log_index); - LOGV(DEBUG_LEVEL, info_log_, "FloydContext::ReceiverDoAppendEntries: truncate suffix from %lu, " - "pre_log(%lu,%lu), last_log(%lu,%lu)", - pre_log_index + 1, pre_log_term, pre_log_index, last_log_term, last_log_index); -#endif - // TruncateSuffix [pre_log_index + 1, last_log_index) - raft_log_->TruncateSuffix(pre_log_index + 1); - } - - *my_term = current_term_; - if (entries.size() > 0) { - LOGV(DEBUG_LEVEL, info_log_, "FloydContext::ReceiverDoAppendEntries: will append %u entries from " - " pre_log_index %lu", entries.size(), pre_log_index + 1); - if (raft_log_->Append(entries) <= 0) { - return false; - } - } - return true; + voted_for_ip = ip; + voted_for_port = port; + current_term = term; } } // namespace floyd diff --git a/floyd/src/floyd_context.h b/floyd/src/floyd_context.h index b68dab1..9bb7597 100644 --- a/floyd/src/floyd_context.h +++ b/floyd/src/floyd_context.h @@ -23,134 +23,54 @@ enum Role { kCandidate = 1, kLeader = 2, }; - -class Logger; -class RaftLog; - -class FloydContext { - public: - FloydContext(const Options& opt, RaftLog* raft_log, Logger* info_log); - ~FloydContext(); - - void RecoverInit(); - RaftLog* raft_log() { - return raft_log_; - } - - Logger* info_log() { - return info_log_; - } - +class RaftMeta; +/* + * we use FloydContext to avoid passing the floyd_impl's this point to other thread + */ +struct FloydContext { /* Role related */ + FloydContext(const Options& _options) + : options(_options), + apply_cond(&apply_mu) {}; void leader_node(std::string* ip, int* port); void voted_for_node(std::string* ip, int* port); bool HasLeader(); - uint64_t current_term() { - slash::RWLock l(&stat_rw_, false); - return current_term_; - } - - Role role() { - slash::RWLock l(&stat_rw_, false); - return role_; - } - - std::string local_ip() { - return options_.local_ip; - } - - int local_port() { - return options_.local_port; - } - - uint64_t heartbeat_us() { - return options_.heartbeat_us; - } - - uint64_t GetElectLeaderTimeout(); - - uint64_t append_entries_size_once() { - return options_.append_entries_size_once; - } - uint64_t append_entries_count_once() { - return options_.append_entries_count_once; - } - - bool single_mode() { - return options_.single_mode; - } - + void RecoverInit(RaftMeta *raft); void BecomeFollower(uint64_t new_iterm, const std::string leader_ip = "", int port = 0); void BecomeCandidate(); void BecomeLeader(); - bool VoteAndCheck(uint64_t vote_term); - bool ReceiverDoRequestVote(uint64_t term, - const std::string ip, int port, - uint64_t log_term, uint64_t log_index, - uint64_t* my_term); + void GrantVote(uint64_t term, const std::string ip, const int port); bool ReceiverDoAppendEntries(uint64_t term, uint64_t pre_log_term, uint64_t pre_log_index, std::vector& entries, uint64_t* my_term); - - /* Commit related */ - uint64_t commit_index() { - slash::MutexLock l(&commit_mu_); - return commit_index_; - } - void set_commit_index(uint64_t commit_index) { - commit_index_ = commit_index; - } - bool AdvanceLeaderCommitIndex(uint64_t leader_commit); - bool AdvanceFollowerCommitIndex(uint64_t leader_commit); - - /* Apply related */ - // Return false if timeout - Status WaitApply(uint64_t last_applied, uint32_t timeout); - - // commit index may be smaller than apply index, - // so we should check len first; - uint64_t NextApplyIndex(uint64_t* len); - - uint64_t last_applied() { - slash::MutexLock lapply(&apply_mu_); - return last_applied_; - } - - void ApplyDone(uint64_t index); - void MetaApply(); - - // Commit related - slash::Mutex commit_mu_; - slash::Mutex apply_mu_; - - pthread_rwlock_t stat_rw_; - - private: - Options options_; - RaftLog* raft_log_; - // used to debug - Logger* info_log_; - + Options options; // Role related - uint64_t current_term_; - - Role role_; - std::string voted_for_ip_; - int voted_for_port_; - std::string leader_ip_; - int leader_port_; - uint32_t vote_quorum_; - - uint64_t commit_index_; - - // Apply related - slash::CondVar apply_cond_; - uint64_t last_applied_; + uint64_t current_term; + + Role role; + std::string voted_for_ip; + int voted_for_port; + std::string leader_ip; + int leader_port; + uint32_t vote_quorum; + + uint64_t commit_index; + uint64_t last_applied; + uint64_t last_op_time; + + // mutex protect commit_index + // used in floyd_apply thread and floyd_peer thread + // floyd_apply thread will get the commit_index + // floyd_peer thread will update the commit_index + slash::Mutex commit_index_mu; + slash::Mutex commit_mu; + slash::Mutex apply_mu; + slash::CondVar apply_cond; }; diff --git a/floyd/src/floyd_impl.cc b/floyd/src/floyd_impl.cc index f755076..b523376 100644 --- a/floyd/src/floyd_impl.cc +++ b/floyd/src/floyd_impl.cc @@ -10,10 +10,10 @@ #include #include -#include "slash/include/env.h" -#include "slash/include/slash_string.h" #include "pink/include/bg_thread.h" +#include "slash/include/env.h" #include "slash/include/slash_string.h" +#include "slash/include/slash_mutex.h" #include "floyd/src/floyd_context.h" #include "floyd/src/floyd_apply.h" @@ -24,12 +24,13 @@ #include "floyd/src/floyd_client_pool.h" #include "floyd/src/logger.h" #include "floyd/src/floyd.pb.h" +#include "floyd/src/raft_meta.h" namespace floyd { FloydImpl::FloydImpl(const Options& options) - : options_(options), - db_(NULL), + : db_(NULL), + options_(options), info_log_(NULL) { } @@ -37,13 +38,12 @@ FloydImpl::~FloydImpl() { // worker will use floyd, delete worker first delete worker_; delete worker_client_pool_; - delete peer_client_pool_; delete primary_; delete apply_; - for (auto& pt : peers_) { + for (auto& pt : *peers_) { delete pt.second; } - + delete peers_; delete context_; delete db_; delete raft_log_; @@ -51,8 +51,7 @@ FloydImpl::~FloydImpl() { } bool FloydImpl::IsSelf(const std::string& ip_port) { - return (ip_port == - slash::IpPortString(options_.local_ip, options_.local_port)); + return (ip_port == slash::IpPortString(options_.local_ip, options_.local_port)); } bool FloydImpl::GetLeader(std::string *ip_port) { @@ -94,7 +93,6 @@ Status FloydImpl::Init() { } // TODO (anan) set timeout and retry - peer_client_pool_ = new ClientPool(info_log_); worker_client_pool_ = new ClientPool(info_log_); // Create DB @@ -106,37 +104,46 @@ Status FloydImpl::Init() { return Status::Corruption("Open DB failed, " + s.ToString()); } + s = rocksdb::DB::Open(options, options_.path + "/log/", &log_and_meta_); + if (!s.ok()) { + LOGV(ERROR_LEVEL, info_log_, "Open db failed! path: %s", options_.path.c_str()); + return Status::Corruption("Open DB failed, " + s.ToString()); + } + // Recover Context - raft_log_ = new RaftLog(options_.path + "/log/", info_log_); - context_ = new FloydContext(options_, raft_log_, info_log_); - context_->RecoverInit(); + raft_log_ = new RaftLog(log_and_meta_, info_log_); + raft_meta_ = new RaftMeta(log_and_meta_, info_log_); + context_ = new FloydContext(options_); + context_->RecoverInit(raft_meta_); // Create Apply threads - apply_ = new FloydApply(context_, db_, raft_log_); + apply_ = new FloydApply(context_, db_, raft_meta_, raft_log_, info_log_); - // TODO(annan) peers and primary refer to each other + // peers and primary refer to each other // Create PrimaryThread before Peers - primary_ = new FloydPrimary(context_, apply_); + primary_ = new FloydPrimary(context_, apply_, options_, info_log_); // Create peer threads + peers_ = new PeersSet(); for (auto iter = options_.members.begin(); iter != options_.members.end(); iter++) { if (!IsSelf(*iter)) { - Peer* pt = new Peer(*iter, context_, primary_, raft_log_, peer_client_pool_); - peers_.insert(std::pair(*iter, pt)); + Peer* pt = new Peer(*iter, context_, primary_, raft_log_, worker_client_pool_, options_, info_log_); + peers_->insert(std::pair(*iter, pt)); } } // Start peer thread int ret; - for (auto& pt : peers_) { + for (auto& pt : *peers_) { if ((ret = pt.second->StartThread()) != 0) { + pt.second->set_peers(peers_); LOGV(ERROR_LEVEL, info_log_, "FloydImpl peer thread to %s failed to " " start, ret is %d", pt.first.c_str(), ret); return Status::Corruption("failed to start peer thread to " + pt.first); } } - LOGV(INFO_LEVEL, info_log_, "Floyd start %d peer thread", peers_.size()); + LOGV(INFO_LEVEL, info_log_, "Floyd start %d peer thread", peers_->size()); // Start worker thread after Peers, because WorkerHandle will check peers worker_ = new FloydWorker(options_.local_port, 1000, this); @@ -146,12 +153,12 @@ Status FloydImpl::Init() { } // Set and Start PrimaryThread - primary_->set_peers(&peers_); + primary_->set_peers(peers_); if ((ret = primary_->Start()) != 0) { LOGV(ERROR_LEVEL, info_log_, "FloydImpl primary thread failed to start, ret is %d", ret); return Status::Corruption("failed to start primary thread, return " + std::to_string(ret)); } - primary_->AddTask(kCheckElectLeader); + primary_->AddTask(kCheckLeader); // test only // options_.Dump(); @@ -441,7 +448,7 @@ Status FloydImpl::ReplyExecuteDirtyCommand(const CmdRequest& cmd, bool FloydImpl::DoGetServerStatus(CmdResponse_ServerStatus* res) { std::string role_msg; - switch (context_->role()) { + switch (context_->role) { case Role::kFollower: role_msg = "follower"; break; @@ -453,8 +460,8 @@ bool FloydImpl::DoGetServerStatus(CmdResponse_ServerStatus* res) { break; } - res->set_term(context_->current_term()); - res->set_commit_index(context_->commit_index()); + res->set_term(context_->current_term); + res->set_commit_index(context_->commit_index); res->set_role(role_msg); std::string ip; @@ -481,16 +488,16 @@ bool FloydImpl::DoGetServerStatus(CmdResponse_ServerStatus* res) { res->set_last_log_term(last_log_term); res->set_last_log_index(last_log_index); - res->set_last_applied(context_->last_applied()); + res->set_last_applied(context_->last_applied); return true; } -Status FloydImpl::ExecuteCommand(const CmdRequest& cmd, +Status FloydImpl::ExecuteCommand(const CmdRequest& request, CmdResponse *response) { // Append entry local std::vector entries; Entry entry; - BuildLogEntry(cmd, context_->current_term(), &entry); + BuildLogEntry(request, context_->current_term, &entry); entries.push_back(&entry); uint64_t last_log_index = raft_log_->Append(entries); @@ -500,23 +507,25 @@ Status FloydImpl::ExecuteCommand(const CmdRequest& cmd, // Notify primary then wait for apply if (options_.single_mode) { - primary_->AddTask(kAdvanceCommitIndex); + primary_->AddTask(kNewCommand); } else { primary_->AddTask(kNewCommand); } - - response->set_type(cmd.type()); + response->set_type(request.type()); response->set_code(StatusCode::kError); - Status res = context_->WaitApply(last_log_index, 1000); - if (!res.ok()) { - return res; - } + context_->apply_mu.Lock(); + while (context_->last_applied < last_log_index) { + if (!context_->apply_cond.TimedWait(1000)) { + return Status::Timeout("FloydImpl::ExecuteCommand Timeout"); + } + } + context_->apply_mu.Unlock(); // Complete CmdRequest if needed std::string value; rocksdb::Status rs; - switch (cmd.type()) { + switch (request.type()) { case Type::kWrite: { response->set_code(StatusCode::kOk); break; @@ -526,16 +535,16 @@ Status FloydImpl::ExecuteCommand(const CmdRequest& cmd, break; } case Type::kRead: { - rs = db_->Get(rocksdb::ReadOptions(), cmd.kv().key(), &value); + rs = db_->Get(rocksdb::ReadOptions(), request.kv().key(), &value); if (rs.ok()) { - BuildReadResponse(cmd.kv().key(), value, StatusCode::kOk, response); + BuildReadResponse(request.kv().key(), value, StatusCode::kOk, response); } else if (rs.IsNotFound()) { - BuildReadResponse(cmd.kv().key(), value, StatusCode::kNotFound, response); + BuildReadResponse(request.kv().key(), value, StatusCode::kNotFound, response); } else { - BuildReadResponse(cmd.kv().key(), value, StatusCode::kError, response); + BuildReadResponse(request.kv().key(), value, StatusCode::kError, response); } LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ExecuteCommand Read %s, key(%s) value(%s)", - rs.ToString().c_str(), cmd.kv().key().c_str(), value.c_str()); + rs.ToString().c_str(), request.kv().key().c_str(), value.c_str()); #ifndef NDEBUG std::string text_format; google::protobuf::TextFormat::PrintToString(*response, &text_format); @@ -544,102 +553,136 @@ Status FloydImpl::ExecuteCommand(const CmdRequest& cmd, break; } default: { - return Status::Corruption("Unknown cmd type"); + return Status::Corruption("Unknown request type"); } } return Status::OK(); } -void FloydImpl::ReplyRequestVote(const CmdRequest& cmd, CmdResponse* response) { + +void FloydImpl::ReplyRequestVote(const CmdRequest& request, CmdResponse* response) { + slash::MutexLock l(&context_->commit_mu); bool granted = false; - CmdRequest_RequestVote request_vote = cmd.request_vote(); + CmdRequest_RequestVote request_vote = request.request_vote(); LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyRequestVote: my_term=%lu rqv.term=%lu", - my_term, request_vote.term()); - MutexLock l(context_->commit_mu_); - { - uint64_t my_current_term; - uint64_t my_last_log_index; - raft_log_->GetLastLogTermAndIndex(&my_current_term, &my_last_log_index); + context_->current_term, request_vote.term()); + uint64_t current_term; // if caller's term smaller than my term, then I will notice him - if (request_vote.last_log_term() < my_current_term) { - BuildRequestVoteResponse(my_current_term, granted, response); + if (request_vote.term() < context_->current_term) { + BuildRequestVoteResponse(context_->current_term, granted, response); return; } - + uint64_t my_last_log_term; + uint64_t my_last_log_index; + raft_log_->GetLastLogTermAndIndex(&my_last_log_term, &my_last_log_index); // if votedfor is null or candidateId, and candidated's log is at least as up-to-date // as receiver's log, grant vote - if (!context_->voted_for_ip_.empty() && (context_->voted_for_ip_ != request_vote.ip() || context_->voted_for_port_ != request_vote.port()) - (request_vote.last_log_term() == my_current_term && request_vote.last_log_index() >= my_last_log_index)) { - + if ((request_vote.last_log_term() < my_last_log_term) || + (request_vote.last_log_term() == my_last_log_term) && (request_vote.last_log_index() < my_last_log_index)) { + BuildRequestVoteResponse(context_->current_term, granted, response); + return; + } + + if (vote_for_.find(request_vote.term()) != vote_for_.end() && vote_for_[request_vote.term()] != std::make_pair(request_vote.ip(), request_vote.port())) { LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyRequestVote: BecomeFollower with current_term_(%lu) and new_term(%lu)" - " commit_index(%lu) last_applied(%lu)", - my_current_term, request_vote.last_log_term(), my_last_log_index, ()); - context_->BecomeFollower(request_vote.term()); - primary_->ResetElectLeaderTimer(); - BuildRequestVoteResponse(my_current_term, granted, response); + " commit_index(%lu) last_applied(%lu)", + context_->current_term, request_vote.last_log_term(), my_last_log_index, context_->last_applied); + BuildRequestVoteResponse(context_->current_term, granted, response); return ; } - + vote_for_[request_vote.term()] = std::make_pair(request_vote.ip(), request_vote.port()); + context_->BecomeFollower(request_vote.term()); + raft_meta_->SetCurrentTerm(context_->current_term); + raft_meta_->SetVotedForIp(context_->voted_for_ip); + raft_meta_->SetVotedForPort(context_->voted_for_port); // Got my vote - voted_for_ip_ = ip; - voted_for_port_ = port; - *my_term = current_term_; - MetaApply(); - LOGV(INFO_LEVEL, info_log_, "FloydContext::RequestVote: grant vote for (%s:%d)," - " with my_term(%lu), my last_log(%lu:%lu), caller log(%lu,%lu).", - voted_for_ip_.c_str(), voted_for_port_, *my_term, - my_log_term, my_log_index, log_term, log_index); + context_->GrantVote(request_vote.term(), request_vote.ip(), request_vote.port()); + granted = true; + BuildRequestVoteResponse(context_->current_term, granted, response); +} + +bool FloydImpl::AdvanceFollowerCommitIndex(uint64_t new_commit_index) { + // Update log commit index + context_->commit_index_mu.Lock(); + context_->commit_index = new_commit_index; + raft_meta_->SetCommitIndex(new_commit_index); + context_->commit_index_mu.Unlock(); return true; - BuildRequestVoteResponse(my_term, granted, response); } -void FloydImpl::ReplyAppendEntries(CmdRequest& cmd, CmdResponse* response) { +void FloydImpl::ReplyAppendEntries(CmdRequest& request, CmdResponse* response) { + bool success = false; + CmdRequest_AppendEntries append_entries = request.append_entries(); + slash::MutexLock l(&context_->commit_mu); // Ignore stale term - MutexLock l(context_->commit_mu_); - bool status = false; - uint64_t my_term = context_->current_term; - CmdRequest_AppendEntries append_entries = cmd.append_entries(); - // if the append entries term is smaller then my_term, then the caller must an older leader - if (append_entries.term() < my_term) { - BuildAppendEntriesResponse(status, my_term, raft_log_->GetLastLogIndex(), response); + // if the append entries term is smaller then my current term, then the caller must an older leader + uint64_t last_log_index = raft_log_->GetLastLogIndex(); + if (append_entries.term() < context_->current_term) { + BuildAppendEntriesResponse(success, context_->current_term, last_log_index, response); return; + } else if (append_entries.term() > context_->current_term) { + context_->BecomeFollower(append_entries.term(), + append_entries.ip(), append_entries.port()); + raft_meta_->SetCurrentTerm(context_->current_term); + raft_meta_->SetVotedForIp(context_->voted_for_ip); + raft_meta_->SetVotedForPort(context_->voted_for_port); + } + + if (append_entries.prev_log_index() > last_log_index) { + LOGV(INFO_LEVEL, info_log_, "RaftMeta::ReceiverDoAppendEntries:" + "pre_log(%lu, %lu) > last_log_index(%lu)", append_entries.prev_log_term(), append_entries.prev_log_index(), + last_log_index); + BuildAppendEntriesResponse(success, context_->current_term, last_log_index, response); + return ; } - // TODO(ba0tiao) why we need become follower, maybe we have been follower before - context_->BecomeFollower(append_entries.term(), - append_entries.ip(), append_entries.port()); std::vector entries; - for (auto& it : *(cmd.mutable_append_entries()->mutable_entries())) { + for (auto& it : *(request.mutable_append_entries()->mutable_entries())) { entries.push_back(&it); } - // TODO(ba0tiao) do consistency check here + uint64_t my_log_term = 0; + Entry entry; + LOGV(DEBUG_LEVEL, info_log_, "RaftMeta::ReceiverDoAppendEntries" + "prev_log_index: %llu\n", append_entries.prev_log_index()); + if (raft_log_->GetEntry(append_entries.prev_log_index(), &entry) == 0) { + my_log_term = entry.term(); + } else { + LOGV(WARN_LEVEL, info_log_, "FloydImple::ReplyAppentries: can't" + "get Entry from raft_log prev_log_index %llu", append_entries.prev_log_index()); + BuildAppendEntriesResponse(success, context_->current_term, last_log_index, response); + return; + } - /* - * std::string text_format; - * google::protobuf::TextFormat::PrintToString(cmd, &text_format); - * LOGV(DEBUG_LEVEL, context_->info_log(), "FloydImpl::ReplyAppendEntries with %llu " - * "entries, message :\n%s", - * entries.size(), text_format.c_str()); - */ + if (append_entries.prev_log_term() != my_log_term) { + LOGV(WARN_LEVEL, info_log_, "RaftMeta::ReceiverDoAppendEntries: pre_log(%lu, %lu) don't match with" + " local log(%lu, %lu), truncate suffix from here", + append_entries.prev_log_term(), append_entries.prev_log_index(), my_log_term, last_log_index); + // TruncateSuffix [prev_log_index, last_log_index) + raft_log_->TruncateSuffix(append_entries.prev_log_index()); + } - // Append entries - status = context_->ReceiverDoAppendEntries(append_entries.term(), - append_entries.prev_log_term(), - append_entries.prev_log_index(), - entries, &my_term); + // Append entry + if (append_entries.prev_log_index() < last_log_index) { + // TruncateSuffix [prev_log_index + 1, last_log_index) + raft_log_->TruncateSuffix(append_entries.prev_log_index() + 1); + } + if (entries.size() > 0) { + LOGV(DEBUG_LEVEL, info_log_, "RaftMeta::ReceiverDoAppendEntries: will append %u entries from " + " prev_log_index %lu", entries.size(), append_entries.prev_log_index() + 1); + if (raft_log_->Append(entries) <= 0) { + return ; + } + } // only when follower successfully do appendentries, we will update commit index - if (status) { - context_->AdvanceFollowerCommitIndex(append_entries.leader_commit()); + if (success) { + AdvanceFollowerCommitIndex(append_entries.leader_commit()); LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries after AdvanceCommitIndex %lu", - context_->commit_index()); + context_->commit_index); apply_->ScheduleApply(); } - // TODO(anan) ElectLeader timer may timeout because of slow AppendEntries - // we delay reset timer. - primary_->ResetElectLeaderTimer(); - BuildAppendEntriesResponse(status, my_term, raft_log_->GetLastLogIndex(), response); + BuildAppendEntriesResponse(success, context_->current_term, raft_log_->GetLastLogIndex(), response); } } // namespace floyd diff --git a/floyd/src/floyd_impl.h b/floyd/src/floyd_impl.h index 10bd3a1..8a5b129 100644 --- a/floyd/src/floyd_impl.h +++ b/floyd/src/floyd_impl.h @@ -23,12 +23,13 @@ using slash::Status; class Log; class ClientPool; -class FloydContext; +class RaftMeta; class Peer; class FloydPrimary; class FloydApply; class FloydWorker; class FloydWorkerConn; +class FloydContext; class Logger; class CmdRequest; class CmdResponse; @@ -65,21 +66,27 @@ class FloydImpl : public Floyd { friend class FloydWorkerHandle; friend class Peer; + // debug log used for ouput to file + Logger* info_log_; + Options options_; + // state machine db point rocksdb::DB* db_; // raft log RaftLog* raft_log_; - // debug log used for ouput to file - Logger* info_log_; + RaftMeta* raft_meta_; + rocksdb::DB* log_and_meta_; // used to store logs and meta data + FloydContext* context_; FloydWorker* worker_; FloydApply* apply_; FloydPrimary* primary_; - PeersSet peers_; - ClientPool* peer_client_pool_; + PeersSet* peers_; ClientPool* worker_client_pool_; + std::map > vote_for_; + bool IsSelf(const std::string& ip_port); Status DoCommand(const CmdRequest& cmd, CmdResponse *cmd_res); @@ -88,10 +95,13 @@ class FloydImpl : public Floyd { bool DoGetServerStatus(CmdResponse_ServerStatus* res); /* - * these two are the response to the request and appendentries + * these two are the response to the request vote and appendentries */ void ReplyRequestVote(const CmdRequest& cmd, CmdResponse* cmd_res); void ReplyAppendEntries(CmdRequest& cmd, CmdResponse* cmd_res); + + + bool AdvanceFollowerCommitIndex(uint64_t new_commit_index); // No coping allowed FloydImpl(const FloydImpl&); diff --git a/floyd/src/floyd_options.cc b/floyd/src/floyd_options.cc index 250ef5f..64c025e 100644 --- a/floyd/src/floyd_options.cc +++ b/floyd/src/floyd_options.cc @@ -5,6 +5,9 @@ #include "floyd/include/floyd_options.h" +#include +#include + namespace floyd { void split(const std::string &str, char delim, @@ -36,7 +39,7 @@ void Options::Dump() { printf (" local_ip : %s\n" " local_port : %d\n" " path : %s\n" - " elect_timeout_ms : %ld\n" + " check_leader_us : %ld\n" " heartbeat_us : %ld\n" " append_entries_size_once : %ld\n" "append_entries_count_once : %lu\n" @@ -44,7 +47,7 @@ void Options::Dump() { local_ip.c_str(), local_port, path.c_str(), - elect_timeout_ms, + check_leader_us, heartbeat_us, append_entries_size_once, append_entries_count_once, @@ -60,7 +63,7 @@ std::string Options::ToString() { sprintf (str + len, " local_ip : %s\n" " local_port : %d\n" " path : %s\n" - " elect_timeout_ms : %ld\n" + " check_leader_us : %ld\n" " heartbeat_us : %ld\n" " append_entries_size_once : %ld\n" "append_entries_count_once : %lu\n" @@ -68,7 +71,7 @@ std::string Options::ToString() { local_ip.c_str(), local_port, path.c_str(), - elect_timeout_ms, + check_leader_us, heartbeat_us, append_entries_size_once, append_entries_count_once, @@ -80,7 +83,7 @@ Options::Options() : local_ip("127.0.0.1"), local_port(10086), path("/data/floyd"), - elect_timeout_ms(5000), + check_leader_us(3000000), heartbeat_us(1000000), append_entries_size_once(1024), append_entries_count_once(24), @@ -93,11 +96,16 @@ Options::Options(const std::string& cluster_string, : local_ip(_local_ip), local_port(_local_port), path(_path), - elect_timeout_ms(5000), + check_leader_us(3000000), heartbeat_us(1000000), append_entries_size_once(1024), append_entries_count_once(24), single_mode(false) { + std::srand(std::time(0)); + // the default check_leader is [3s, 5s) + // the default heartbeat time is 1s + // we can promise 1s + 2 * rpc < 3s, since rpc time is approximately 10ms + check_leader_us = rand() % 2000000 + check_leader_us; split(cluster_string, ',', members); if (members.size() == 1) { single_mode = true; diff --git a/floyd/src/floyd_peer_thread.cc b/floyd/src/floyd_peer_thread.cc index 25dc3cb..21f7c65 100644 --- a/floyd/src/floyd_peer_thread.cc +++ b/floyd/src/floyd_peer_thread.cc @@ -7,28 +7,31 @@ #include #include + +#include "slash/include/env.h" +#include "slash/include/slash_mutex.h" +#include "slash/include/xdebug.h" + #include "floyd/src/floyd_primary_thread.h" #include "floyd/src/floyd_context.h" #include "floyd/src/floyd_client_pool.h" #include "floyd/src/raft_log.h" #include "floyd/src/floyd.pb.h" #include "floyd/src/logger.h" - -#include "slash/include/env.h" -#include "slash/include/slash_mutex.h" -#include "slash/include/xdebug.h" +#include "floyd/src/raft_meta.h" namespace floyd { -Peer::Peer(std::string server, FloydContext* context, FloydPrimary* primary, - RaftLog* raft_log, ClientPool* pool) +Peer::Peer(std::string server, FloydContext* context, FloydPrimary* primary, RaftLog* raft_log, + ClientPool* pool, const Options& options, Logger* info_log) : server_(server), context_(context), primary_(primary), + options_(options), raft_log_(raft_log), pool_(pool), next_index_(1) { - next_index_ = raft_log_->last_log_index() + 1; + next_index_ = raft_log_->GetLastLogIndex() + 1; } int Peer::StartThread() { @@ -37,173 +40,153 @@ int Peer::StartThread() { } Peer::~Peer() { - LOGV(INFO_LEVEL, context_->info_log(), "Peer(%s) exit!!!", server_.c_str()); -} - -void Peer::set_next_index(uint64_t next_index) { - next_index_ = next_index; -} - -uint64_t Peer::get_next_index() { - return next_index_; + LOGV(INFO_LEVEL, info_log_, "Peer(%s) exit!!!", server_.c_str()); } void Peer::AddRequestVoteTask() { - bg_thread_.Schedule(RequestVoteRPCWrapper, this); + bg_thread_.Schedule(&RequestVoteRPCWrapper, this); } void Peer::RequestVoteRPCWrapper(void *arg) { - Peer *peer = static_cast(arg); - LOGV(DEBUG_LEVEL, peer->context_->info_log(), "Peer(%s)::DoRequestVote", - peer->server_.c_str()); - Status result = peer->RequestVoteRPC(); - if (!result.ok()) { - LOGV(ERROR_LEVEL, peer->context_->info_log(), "Peer(%s) failed to " - "RequestVote caz %s.", peer->server_.c_str(), result.ToString().c_str()); + reinterpret_cast(arg)->RequestVoteRPC(); +} + +bool Peer::VoteAndCheck(uint64_t vote_term) { + if (context_->current_term != vote_term) { + return false; } + return (++context_->vote_quorum) > (options_.members.size() / 2); } Status Peer::RequestVoteRPC() { uint64_t last_log_term; uint64_t last_log_index; + CmdRequest req; { - MutexLock l(&context_->commit_mu_); + slash::MutexLock l(&context_->commit_mu); raft_log_->GetLastLogTermAndIndex(&last_log_term, &last_log_index); - uint64_t current_term = context_->current_term(); - CmdRequest req; req.set_type(Type::kRequestVote); CmdRequest_RequestVote* request_vote = req.mutable_request_vote(); - request_vote->set_ip(context_->local_ip()); - request_vote->set_port(context_->local_port()); - request_vote->set_term(current_term); + request_vote->set_ip(options_.local_ip); + request_vote->set_port(options_.local_port); + request_vote->set_term(context_->current_term); request_vote->set_last_log_term(last_log_term); request_vote->set_last_log_index(last_log_index); } -#ifndef NDEBUG - std::string text_format; - google::protobuf::TextFormat::PrintToString(req, &text_format); - LOGV(DEBUG_LEVEL, context_->info_log(), "Send RequestVote to %s, message :" - "\n%s", server_.c_str(), text_format.c_str()); -#endif - CmdResponse res; Status result = pool_->SendAndRecv(server_, req, &res); if (!result.ok()) { - LOGV(DEBUG_LEVEL, context_->info_log(), "RequestVote to %s failed %s", + LOGV(DEBUG_LEVEL, info_log_, "RequestVote to %s failed %s", server_.c_str(), result.ToString().c_str()); return result; } -#ifndef NDEBUG - google::protobuf::TextFormat::PrintToString(res, &text_format); - LOGV(DEBUG_LEVEL, context_->info_log(), "Recv RequestVote from %s, message :" - "\n%s", server_.c_str(), text_format.c_str()); -#endif - - // we get term from request vote { - MutexLock l(&context_->state_mu_); - uint64_t res_term = res.request_vote_res().term(); - if (result.ok() && context_->role() == Role::kCandidate) { + slash::MutexLock l(&context_->commit_mu); + if (context_->role == Role::kCandidate) { // kOk means RequestVote success, opposite vote for me if (res.request_vote_res().vote_granted() == true) { // granted - LOGV(INFO_LEVEL, context_->info_log(), "Peer(%s)::RequestVote granted" - " will Vote and check", server_.c_str()); + LOGV(INFO_LEVEL, info_log_, "Peer(%s)::RequestVote granted" + " will Vote and check", server_.c_str()); // However, we need check whether this vote is vote for old term // we need igore these type of vote - if (context_->VoteAndCheck(res_term)) { - primary_->AddTask(kBecomeLeader); + if (VoteAndCheck(res.request_vote_res().term())) { + context_->BecomeLeader(); + primary_->AddTask(kHeartBeat); } } else { + if (res.request_vote_res().term() > context_->current_term) { + context_->BecomeFollower(res.request_vote_res().term()); + } // opposite RequestVote fail, maybe opposite has larger term, or opposite has // longer log. if opposite has larger term, this node will become follower // otherwise we will do nothing - LOGV(DEBUG_LEVEL, context_->info_log(), "Vote request denied by %s," - " res_term=%lu, current_term=%lu", - server_.c_str(), res_term, current_term); - if (res_term > current_term) { - context_->BecomeFollower(res_term); - primary_->ResetElectLeaderTimer(); - } + LOGV(DEBUG_LEVEL, info_log_, "Vote request denied by %s," + " res_term=%lu, current_term=%lu", + server_.c_str(), res.request_vote_res().term(), context_->current_term); } + } else { + // TODO(ba0tiao) if i am not longer candidate } } - return result; } -void Peer::AddHeartBeatTask() { - AddAppendEntriesTask(); -} - -void Peer::AddBecomeLeaderTask() { - next_index_ = raft_log_->GetLastLogIndex() + 1; - AddAppendEntriesTask(); +void Peer::AddAppendEntriesTask() { + bg_thread_.Schedule(&AppendEntriesRPCWrapper, this); } -void Peer::AddAppendEntriesTask() { - bg_thread_.Schedule(AppendEntriesRPCWrapper, this); +void Peer::AppendEntriesRPCWrapper(void *arg) { + reinterpret_cast(arg)->AppendEntriesRPC(); } -uint64_t Peer::GetMatchIndex() { - return (next_index_ - 1); +uint64_t Peer::QuorumMatchIndex() { + std::vector values; + for (auto& iter : *peers_) { + values.push_back(iter.second->match_index()); + } + std::sort(values.begin(), values.end()); + return values.at(values.size() / 2); } -void Peer::AppendEntriesRPCWrapper(void *arg) { - Peer* peer = static_cast(arg); - LOGV(DEBUG_LEVEL, peer->context_->info_log(), "Peer(%s) DoAppendEntries", - peer->server_.c_str()); - Status result = peer->AppendEntriesRPC(); - if (!result.ok()) { - LOGV(ERROR_LEVEL, peer->context_->info_log(), "Peer(%s) failed to " - "AppendEntries caz %s.", - peer->server_.c_str(), result.ToString().c_str()); +// only leader will call AdvanceCommitIndex +// follower only need set commit as leader's +void Peer::AdvanceLeaderCommitIndex() { + Entry entry; + uint64_t new_commit_index = QuorumMatchIndex(); + if (context_->commit_index != new_commit_index) { + context_->commit_index = new_commit_index; + raft_meta_->SetCommitIndex(context_->commit_index); } + return; } Status Peer::AppendEntriesRPC() { - uint64_t last_log_index = raft_log_->GetLastLogIndex(); uint64_t prev_log_index = next_index_ - 1; - + uint64_t num_entries = 0; uint64_t prev_log_term = 0; + uint64_t last_log_index = 0; + CmdRequest req; + CmdRequest_AppendEntries* append_entries = req.mutable_append_entries(); + { + slash::MutexLock l(&context_->commit_mu); + if (prev_log_index != 0) { Entry entry; if (raft_log_->GetEntry(prev_log_index, &entry) != 0) { - LOGV(WARN_LEVEL, context_->info_log(), "Peer::AppendEntriesRPC:GetEntry index %llu " + LOGV(WARN_LEVEL, info_log_, "Peer::AppendEntriesRPC:GetEntry index %llu " "not found", prev_log_index); } else { prev_log_term = entry.term(); } } - CmdRequest req; - CmdRequest_AppendEntries* append_entries = req.mutable_append_entries(); req.set_type(Type::kAppendEntries); - append_entries->set_ip(context_->local_ip()); - append_entries->set_port(context_->local_port()); - append_entries->set_term(context_->current_term()); + append_entries->set_ip(options_.local_ip); + append_entries->set_port(options_.local_port); + append_entries->set_term(context_->current_term); append_entries->set_prev_log_index(prev_log_index); append_entries->set_prev_log_term(prev_log_term); - uint64_t num_entries = 0; + last_log_index = raft_log_->GetLastLogIndex(); Entry *tmp_entry = new Entry(); - LOGV(DEBUG_LEVEL, context_->info_log(), "next_index_ %llu, last_log_index %llu", next_index_.load(), last_log_index); + LOGV(DEBUG_LEVEL, info_log_, "next_index_ %llu, last_log_index %llu", next_index_.load(), last_log_index); for (uint64_t index = next_index_; index <= last_log_index; index++) { if (raft_log_->GetEntry(index, tmp_entry) == 0) { // TODO(ba0tiao) how to avoid memory copy here Entry *entry = append_entries->add_entries(); *entry = *tmp_entry; } else { - LOGV(WARN_LEVEL, context_->info_log(), "FloydPeerThread::AppendEntries: can't get Entry from raft_log, index %lld", index); + LOGV(WARN_LEVEL, info_log_, "FloydPeerThread::AppendEntries: can't get Entry from raft_log, index %lld", index); break; } num_entries++; - if (num_entries >= context_->append_entries_count_once() - || (uint64_t)append_entries->ByteSize() >= context_->append_entries_size_once()) { + if (num_entries >= options_.append_entries_count_once + || (uint64_t)append_entries->ByteSize() >= options_.append_entries_size_once) { break; } } @@ -213,42 +196,46 @@ Status Peer::AppendEntriesRPC() { * if follower's commit index larger than follower log, it conflict */ append_entries->set_leader_commit( - std::min(context_->commit_index(), prev_log_index + num_entries)); + std::min(context_->commit_index, prev_log_index + num_entries)); + } CmdResponse res; Status result = pool_->SendAndRecv(server_, req, &res); + { + slash::MutexLock l(&context_->commit_mu); if (!result.ok()) { - LOGV(WARN_LEVEL, context_->info_log(), "FloydPeerThread::AppendEntries: AppendEntry to %s failed %s", + LOGV(WARN_LEVEL, info_log_, "FloydPeerThread::AppendEntries: AppendEntry to %s failed %s", server_.c_str(), result.ToString().c_str()); return result; } - uint64_t res_term = res.append_entries_res().term(); - /* - * receiver has higer term than myself, so turn from candidate to follower - */ - if (result.ok() && res_term > context_->current_term()) { - // TODO(anan) maybe combine these 2 steps - context_->BecomeFollower(res_term); - primary_->ResetElectLeaderTimer(); - } - // TODO(ba0tiao) 这里是否需要判断Leader, 如果担心在执行sendandrecv 过程中角色 - // 发生了变化, 那么这个时候该如何处理 - if (result.ok() && context_->role() == Role::kLeader) { + // here we may get a larger term, and transfer to follower + // so we need to judge the role here + if (context_->role == Role::kLeader) { + /* + * receiver has higer term than myself, so turn from candidate to follower + */ + if (res.append_entries_res().term() > context_->current_term) { + context_->BecomeFollower(res.append_entries_res().term()); + } if (res.append_entries_res().success() == true) { + context_->last_op_time = slash::NowMicros(); + + match_index_ = prev_log_index + num_entries; + // only log entries from the leader's current term are committed + // by counting replicas + if (append_entries->entries(num_entries - 1).term() == context_->current_term) { + AdvanceLeaderCommitIndex(); + } next_index_ = prev_log_index + num_entries + 1; - LOGV(DEBUG_LEVEL, context_->info_log(), "next_index_ %lld prev_log_index \ - %lld num_entries %lld", next_index_.load(), prev_log_index, num_entries); - primary_->AddTask(kAdvanceCommitIndex); - // If this follower is far behind leader, and there is no more // AppendEntryTask, we should add one - if (next_index_ + context_->append_entries_count_once() < last_log_index) { + if (next_index_ + options_.append_entries_count_once < last_log_index) { int pri_size, qu_size; bg_thread_.QueueSize(&pri_size, &qu_size); if (qu_size < 1) { - LOGV(DEBUG_LEVEL, context_->info_log(), "AppendEntry again " + LOGV(DEBUG_LEVEL, info_log_, "AppendEntry again " "to catch up next_index(%llu) last_log_index(%llu)", next_index_.load(), last_log_index); AddAppendEntriesTask(); @@ -261,10 +248,14 @@ Status Peer::AppendEntriesRPC() { // Prev log don't match, so we retry with more prev one according to // response next_index_ = adjust_index; - LOGV(INFO_LEVEL, context_->info_log(), "update next_index_ %lld", next_index_.load()); + LOGV(INFO_LEVEL, info_log_, "update next_index_ %lld", next_index_.load()); AddAppendEntriesTask(); } } + } else if (context_->role == Role::kFollower) { + // TODO(ba0tiao) if I am not longer a leader + } else if (context_->role == Role::kCandidate) { + } } return result; } diff --git a/floyd/src/floyd_peer_thread.h b/floyd/src/floyd_peer_thread.h index 6df0dc7..7cf0785 100644 --- a/floyd/src/floyd_peer_thread.h +++ b/floyd/src/floyd_peer_thread.h @@ -19,33 +19,31 @@ namespace floyd { using slash::Status; -class Peer; - -class FloydContext; +class RaftMeta; class FloydPrimary; class RaftLog; class ClientPool; + class Peer { public: - Peer(std::string server, FloydContext* context, FloydPrimary* primary, - RaftLog* raft_log, ClientPool* pool); + Peer(std::string server, FloydContext* context, FloydPrimary* primary, RaftLog* raft_log, + ClientPool* pool, const Options& options, Logger* info_log); ~Peer(); int StartThread(); + // Apend Entries + // call by other thread, put job to peer_thread's bg_thread_ + void AddAppendEntriesTask(); + void AddRequestVoteTask(); + /* * the two main RPC call in raft consensus protocol is here * AppendEntriesRPC * RequestVoteRPC * the response to these two RPC at floyd_impl.h */ - // Apend Entries - void AddAppendEntriesTask(); - void AddHeartBeatTask(); - void AddBecomeLeaderTask(); - void AddRequestVoteTask(); - static void AppendEntriesRPCWrapper(void *arg); Status AppendEntriesRPC(); // Request Vote @@ -53,18 +51,42 @@ class Peer { Status RequestVoteRPC(); uint64_t GetMatchIndex(); - void set_next_index(uint64_t next_index); - uint64_t get_next_index(); + + void set_next_index(const uint64_t next_index) { + next_index_ = next_index; + } + uint64_t next_index() { + return next_index_; + } + + uint64_t match_index() { + return match_index_; + } + typedef std::map PeersSet; + + void set_peers(PeersSet* peers) { + peers_ = peers; + } private: + bool VoteAndCheck(uint64_t vote_term); + uint64_t QuorumMatchIndex(); + void AdvanceLeaderCommitIndex(); + + Logger* info_log_; std::string server_; FloydContext* context_; FloydPrimary* primary_; + RaftMeta* raft_meta_; RaftLog* raft_log_; ClientPool* pool_; + PeersSet* peers_; + + Options options_; std::atomic next_index_; + std::atomic match_index_; pink::BGThread bg_thread_; diff --git a/floyd/src/floyd_primary_thread.cc b/floyd/src/floyd_primary_thread.cc index 5edf7b8..a19b707 100644 --- a/floyd/src/floyd_primary_thread.cc +++ b/floyd/src/floyd_primary_thread.cc @@ -23,15 +23,15 @@ #include "floyd/src/raft_log.h" #include "floyd/src/floyd.pb.h" #include "floyd/src/logger.h" +#include "floyd/include/floyd_options.h" namespace floyd { -FloydPrimary::FloydPrimary(FloydContext* context, FloydApply* apply) +FloydPrimary::FloydPrimary(FloydContext* context, FloydApply* apply, const Options& options, Logger* info_log) : context_(context), apply_(apply), - reset_elect_leader_time_(0), - reset_leader_heartbeat_time_(0) { - srand(time(NULL)); + options_(options), + info_log_(info_log) { } int FloydPrimary::Start() { @@ -40,11 +40,11 @@ int FloydPrimary::Start() { } FloydPrimary::~FloydPrimary() { - LOGV(INFO_LEVEL, context_->info_log(), "FloydPrimary exit!!!"); + LOGV(INFO_LEVEL, info_log_, "FloydPrimary exit!!!"); } void FloydPrimary::set_peers(PeersSet* peers) { - LOGV(DEBUG_LEVEL, context_->info_log(), "FloydPrimary::set_peers peers " + LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::set_peers peers " "has %d pairs", peers->size()); peers_ = peers; } @@ -53,166 +53,87 @@ void FloydPrimary::set_peers(PeersSet* peers) { // 1. one short live Cron for LeaderHeartbeat, which is available as a leader; // 2. another long live Cron for ElectLeaderCheck, which is started when // creating Primary; -void FloydPrimary::AddTask(TaskType type, void* arg) { +void FloydPrimary::AddTask(TaskType type, bool is_delay, void* arg) { switch (type) { - case kLeaderHeartbeat: { - uint64_t timeout = context_->heartbeat_us(); - if (reset_leader_heartbeat_time_) { - uint64_t delta = (slash::NowMicros() - reset_leader_heartbeat_time_); - LOGV(DEBUG_LEVEL, context_->info_log(), "FloydPrimary::AddTask " - "kLeaderHeartbeat reset_leader_heartbeat_timer old timeout" - " %lu us, delta is %lu us", timeout, delta); - timeout = (delta < timeout) ? (timeout - delta) : 0; - reset_leader_heartbeat_time_ = 0; - } - LOGV(DEBUG_LEVEL, context_->info_log(), "FloydPrimary::AddTask " - "kLeaderHeartbeat will in %dms", timeout / 1000LL); - bg_thread_.DelaySchedule(timeout / 1000LL, LaunchLeaderHeartbeat, this); - break; - } - case kCheckElectLeader: { - uint64_t timeout = context_->GetElectLeaderTimeout() * 1000LL; - // Here we enlarge the wait time instead of an accurate one, - // to make the latter node more steady - if (reset_elect_leader_time_) { - reset_elect_leader_time_ = 0; - } - LOGV(DEBUG_LEVEL, context_->info_log(), "FloydPrimary::AddTask " - "kCheckElectLeader will in %dms", timeout / 1000LL); - bg_thread_.DelaySchedule(timeout / 1000LL, LaunchCheckElectLeader, this); + case kHeartBeat: { + LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::AddTask HeartBeat"); + uint64_t timeout = options_.heartbeat_us; + bg_thread_.DelaySchedule(timeout / 1000LL, LaunchHeartBeatWrapper, this); break; } - case kBecomeLeader: { - LOGV(DEBUG_LEVEL, context_->info_log(), "FloydPrimary::AddTask BecomeLeader"); - bg_thread_.Schedule(LaunchBecomeLeader, this); + case kCheckLeader: { + LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::AddTask CheckLeader"); + uint64_t timeout = options_.check_leader_us; + bg_thread_.DelaySchedule(timeout / 1000LL, LaunchCheckLeaderWrapper, this); break; } case kNewCommand: { - LOGV(DEBUG_LEVEL, context_->info_log(), "FloydPrimary::AddTask NewCommand"); - bg_thread_.Schedule(LaunchNewCommand, this); - break; - } - case kAdvanceCommitIndex: { - LOGV(DEBUG_LEVEL, context_->info_log(), "FloydPrimary::AddTask AddvanceCommitIndex"); - bg_thread_.Schedule(LaunchAdvanceCommitIndex, this); + LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::AddTask NewCommand"); + bg_thread_.Schedule(LaunchNewCommandWrapper, this); break; } default: { - LOGV(WARN_LEVEL, context_->info_log(), "FloydPrimary:: unknown task type %d", type); + LOGV(WARN_LEVEL, info_log_, "FloydPrimary:: unknown task type %d", type); + break; } } } -void FloydPrimary::LaunchLeaderHeartbeat(void *arg) { - FloydPrimary* ptr = static_cast(arg); - if (ptr->context_->role() == Role::kLeader) { - if (ptr->reset_leader_heartbeat_time_ == 0) { - LOGV(DEBUG_LEVEL, ptr->context_->info_log(), "FloydPrimary::LaunchTimingTask" - " Start LeaderHeartbeat"); - // ptr->LeaderHeartbeat(); - ptr->NoticePeerTask(kLeaderHeartbeat); - } - ptr->AddTask(kLeaderHeartbeat); - } +void FloydPrimary::LaunchHeartBeatWrapper(void *arg) { + reinterpret_cast(arg)->LaunchHeartBeat(); } -void FloydPrimary::LaunchCheckElectLeader(void *arg) { - FloydPrimary* ptr = reinterpret_cast(arg); - - if (ptr->context_->role() == Role::kLeader) { - LOGV(DEBUG_LEVEL, ptr->context_->info_log(), "FloydPrimary::LaunchCheckElectLeader" - " already Leader skip check"); - ptr->AddTask(kCheckElectLeader); - return; - } - - if (ptr->context_->single_mode()) { - ptr->AddTask(kBecomeLeader); - LOGV(INFO_LEVEL, ptr->context_->info_log(), "FloydPrimary::LaunchCheckElectLeader" - " single mode just become leader"); - return; +void FloydPrimary::LaunchHeartBeat() { + slash::MutexLock l(&context_->commit_mu); + if (context_->role == Role::kLeader) { + NoticePeerTask(kHeartBeat); + AddTask(kHeartBeat); } - - if (ptr->context_->role() == Role::kFollower && ptr->reset_elect_leader_time_) { - LOGV(DEBUG_LEVEL, ptr->context_->info_log(), "FloydPrimary::LaunchCheckElectLeader" - " still live"); - } else { - LOGV(DEBUG_LEVEL, ptr->context_->info_log(), "FloydPrimary::LaunchCheckElectLeader" - " start Elect leader after timeout"); - ptr->context_->BecomeCandidate(); - ptr->NoticePeerTask(kCheckElectLeader); - } - ptr->AddTask(kCheckElectLeader); } -void FloydPrimary::LaunchBecomeLeader(void *arg) { - Mutex l(&ptr->context_->commit_mu_); - FloydPrimary* ptr = static_cast(arg); - LOGV(DEBUG_LEVEL, ptr->context_->info_log(), "FloydPrimary::LaunchBecomeLeader"); - ptr->context_->BecomeLeader(); - ptr->NoticePeerTask(kBecomeLeader); - ptr->AddTask(kLeaderHeartbeat); +void FloydPrimary::LaunchCheckLeaderWrapper(void *arg) { + reinterpret_cast(arg)->LaunchCheckLeader(); } - -void FloydPrimary::LaunchNewCommand(void *arg) { - FloydPrimary* ptr = static_cast(arg); - LOGV(DEBUG_LEVEL, ptr->context_->info_log(), "FloydPrimary::LaunchNewCommand"); - if (ptr->context_->role() != Role::kLeader) { - LOGV(WARN_LEVEL, ptr->context_->info_log(), "FloydPrimary::LaunchNewCommand, Not leader yet"); - return; +void FloydPrimary::LaunchCheckLeader() { + slash::MutexLock l(&context_->commit_mu); + if (context_->role == Role::kFollower || context_->role == Role::kCandidate) { + if (options_.single_mode) { + context_->BecomeLeader(); + } else if (context_->last_op_time + options_.check_leader_us < slash::NowMicros()) { + context_->BecomeCandidate(); + NoticePeerTask(kHeartBeat); + } } - ptr->NoticePeerTask(kNewCommand); - ptr->ResetLeaderHeartbeatTimer(); + AddTask(kCheckLeader); } -void FloydPrimary::LaunchAdvanceCommitIndex(void *arg) { - FloydPrimary* ptr = static_cast(arg); - if (ptr->context_->role() != Role::kLeader) { - LOGV(WARN_LEVEL, ptr->context_->info_log(), "FloydPrimary::AdvanceCommitIndex not leader"); +void FloydPrimary::LaunchNewCommandWrapper(void *arg) { + reinterpret_cast(arg)->LaunchNewCommand(); +} +void FloydPrimary::LaunchNewCommand() { + LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::LaunchNewCommand"); + if (context_->role != Role::kLeader) { + LOGV(WARN_LEVEL, info_log_, "FloydPrimary::LaunchNewCommand, Not leader yet"); return; } - - uint64_t new_commit_index; - if (ptr->context_->single_mode()) { - new_commit_index = ptr->context_->raft_log()->GetLastLogIndex(); - } else { - new_commit_index = ptr->QuorumMatchIndex(); - } - LOGV(DEBUG_LEVEL, ptr->context_->info_log(), "FloydPrimary::AdvanceCommitIndex" - " new_commit_index=%llu", new_commit_index); - if (ptr->context_->AdvanceLeaderCommitIndex(new_commit_index)) { - LOGV(DEBUG_LEVEL, ptr->context_->info_log(), "FloydPrimary::AdvanceCommitIndex ok, ScheduleApply"); - ptr->apply_->ScheduleApply(); - } + NoticePeerTask(kNewCommand); } +// when adding task to peer thread, we can consider that this job have been in the network +// even it is still in the peer thread's queue void FloydPrimary::NoticePeerTask(TaskType type) { for (auto& peer : *peers_) { switch (type) { - case kLeaderHeartbeat: - peer.second->AddHeartBeatTask(); - break; - case kCheckElectLeader: + case kHeartBeat: peer.second->AddRequestVoteTask(); break; case kNewCommand: peer.second->AddAppendEntriesTask(); break; - case kBecomeLeader: - peer.second->AddBecomeLeaderTask(); - break; default: - LOGV(WARN_LEVEL, context_->info_log(), "Error TaskType to notice peer"); + LOGV(WARN_LEVEL, info_log_, "Error TaskType to notice peer"); } } } -uint64_t FloydPrimary::QuorumMatchIndex() { - std::vector values; - for (auto& iter : *peers_) { - values.push_back(iter.second->GetMatchIndex()); - } - std::sort(values.begin(), values.end()); - return values.at(values.size() / 2); -} } // namespace floyd diff --git a/floyd/src/floyd_primary_thread.h b/floyd/src/floyd_primary_thread.h index 49dd6db..d0c514a 100644 --- a/floyd/src/floyd_primary_thread.h +++ b/floyd/src/floyd_primary_thread.h @@ -26,42 +26,35 @@ class FloydPrimary; class FloydContext; class FloydApply; - class Peer; +class Options; + typedef std::map PeersSet; enum TaskType { - kCheckElectLeader = 0, - kLeaderHeartbeat, - kBecomeLeader, - kBecomeFollower, - kNewCommand, - kAdvanceCommitIndex + kHeartBeat = 0, + kCheckLeader = 1, + kNewCommand = 2 }; class FloydPrimary { public: - FloydPrimary(FloydContext* context, FloydApply* apply); + FloydPrimary(FloydContext* context, FloydApply* apply, const Options& options, Logger* info_log); ~FloydPrimary(); int Start(); - void AddTask(TaskType type, void *arg = NULL); - - void ResetElectLeaderTimer() { - reset_elect_leader_time_ = slash::NowMicros(); - } - void ResetLeaderHeartbeatTimer() { - reset_leader_heartbeat_time_ = slash::NowMicros(); - } + void AddTask(TaskType type, bool is_delay = true, void *arg = NULL); void set_peers(PeersSet* peers); private: FloydContext* context_; - PeersSet* peers_; FloydApply* apply_; + PeersSet* peers_; + Options options_; + Logger* info_log_; std::atomic reset_elect_leader_time_; std::atomic reset_leader_heartbeat_time_; @@ -69,12 +62,12 @@ class FloydPrimary { // The Launch* work is done by floyd_peer_thread // Cron task - static void LaunchLeaderHeartbeat(void *arg); - static void LaunchCheckElectLeader(void *arg); - - static void LaunchBecomeLeader(void *arg); - static void LaunchNewCommand(void *arg); - static void LaunchAdvanceCommitIndex(void *arg); + static void LaunchHeartBeatWrapper(void *arg); + void LaunchHeartBeat(); + static void LaunchCheckLeaderWrapper(void *arg); + void LaunchCheckLeader(); + static void LaunchNewCommandWrapper(void *arg); + void LaunchNewCommand(); void NoticePeerTask(TaskType type); uint64_t QuorumMatchIndex(); @@ -84,5 +77,5 @@ class FloydPrimary { void operator=(const FloydPrimary&); }; -} // namespace floyd +} // namespace floyd #endif // FLOYD_SRC_FLOYD_PRIMARY_THREAD_H_ diff --git a/floyd/src/raft_log.cc b/floyd/src/raft_log.cc index da5d705..de5fda2 100644 --- a/floyd/src/raft_log.cc +++ b/floyd/src/raft_log.cc @@ -13,13 +13,6 @@ #include "floyd/include/floyd_options.h" namespace floyd { - -static const std::string kCurrentTerm = "CURRENTTERM"; -static const std::string kVoteForIp = "VOTEFORIP"; -static const std::string kVoteForPort = "VOTEFORPORT"; -static const std::string kCommitIndex = "COMMITINDEX"; -static const std::string kLastApplied = "LASTAPPLIED"; - extern std::string UintToBitStr(const uint64_t num) { char buf[8]; uint64_t num1 = htobe64(num); @@ -34,18 +27,11 @@ extern uint64_t BitStrToUint(const std::string &str) { } -RaftLog::RaftLog(const std::string &path, Logger *info_log) : +RaftLog::RaftLog(rocksdb::DB *db, Logger *info_log) : + db_(db), info_log_(info_log), - last_log_index_(0), - commit_index_(0), - last_applied_(0) { - rocksdb::Options options; - options.create_if_missing = true; - rocksdb::Status s = rocksdb::DB::Open(options, path, &log_db_); - assert(s.ok()); - - rocksdb::Iterator *it = log_db_->NewIterator(rocksdb::ReadOptions()); - // skip currentterm, voteforip, voteforport, applyindex + last_log_index_(0) { + rocksdb::Iterator *it = db_->NewIterator(rocksdb::ReadOptions()); it->SeekToLast(); if (it->Valid()) { it->Prev(); @@ -55,20 +41,10 @@ RaftLog::RaftLog(const std::string &path, Logger *info_log) : it->Prev(); last_log_index_ = BitStrToUint(it->key().ToString()); } - - std::string res; - s = log_db_->Get(rocksdb::ReadOptions(), kLastApplied, &res); - if (s.ok()) { - memcpy(&last_applied_, res.data(), sizeof(uint64_t)); - } - s = log_db_->Get(rocksdb::ReadOptions(), kCommitIndex, &res); - if (s.ok()) { - memcpy(&commit_index_, res.data(), sizeof(uint64_t)); - } } RaftLog::~RaftLog() { - delete log_db_; + delete db_; } uint64_t RaftLog::Append(const std::vector &entries) { @@ -79,7 +55,7 @@ uint64_t RaftLog::Append(const std::vector &entries) { for (size_t i = 0; i < entries.size(); i++) { entries[i]->SerializeToString(&buf); last_log_index_++; - s = log_db_->Put(rocksdb::WriteOptions(), UintToBitStr(last_log_index_), buf); + s = db_->Put(rocksdb::WriteOptions(), UintToBitStr(last_log_index_), buf); if (!s.ok()) { LOGV(ERROR_LEVEL, info_log_, "RaftLog::Append false\n"); } @@ -95,7 +71,7 @@ int RaftLog::GetEntry(const uint64_t index, Entry *entry) { slash::MutexLock l(&lli_mutex_); std::string buf = UintToBitStr(index); std::string res; - rocksdb::Status s = log_db_->Get(rocksdb::ReadOptions(), buf, &res); + rocksdb::Status s = db_->Get(rocksdb::ReadOptions(), buf, &res); if (s.IsNotFound()) { LOGV(ERROR_LEVEL, info_log_, "RaftLog::GetEntry: GetEntry not found %lld\n", index); entry = NULL; @@ -105,37 +81,6 @@ int RaftLog::GetEntry(const uint64_t index, Entry *entry) { return 0; } -uint64_t RaftLog::current_term() { - std::string buf; - uint64_t ans; - rocksdb::Status s = log_db_->Get(rocksdb::ReadOptions(), kCurrentTerm, &buf); - if (s.IsNotFound()) { - return 0; - } - memcpy(&ans, buf.data(), sizeof(uint64_t)); - return ans; -} - -std::string RaftLog::voted_for_ip() { - std::string buf; - rocksdb::Status s = log_db_->Get(rocksdb::ReadOptions(), kVoteForIp, &buf); - if (s.IsNotFound()) { - return std::string(""); - } - return buf; -} - -int RaftLog::voted_for_port() { - std::string buf; - int ans; - rocksdb::Status s = log_db_->Get(rocksdb::ReadOptions(), kVoteForPort, &buf); - if (s.IsNotFound()) { - return 0; - } - memcpy(&ans, buf.data(), sizeof(int)); - return ans; -} - bool RaftLog::GetLastLogTermAndIndex(uint64_t* last_log_term, uint64_t* last_log_index) { slash::MutexLock l(&lli_mutex_); if (last_log_index_ == 0) { @@ -144,7 +89,7 @@ bool RaftLog::GetLastLogTermAndIndex(uint64_t* last_log_term, uint64_t* last_log return true; } std::string buf; - rocksdb::Status s = log_db_->Get(rocksdb::ReadOptions(), UintToBitStr(last_log_index_), &buf); + rocksdb::Status s = db_->Get(rocksdb::ReadOptions(), UintToBitStr(last_log_index_), &buf); if (!s.ok() || s.IsNotFound()) { *last_log_index = 0; *last_log_term = 0; @@ -157,35 +102,11 @@ bool RaftLog::GetLastLogTermAndIndex(uint64_t* last_log_term, uint64_t* last_log return true; } -void RaftLog::UpdateMetadata(uint64_t current_term, std::string voted_for_ip, - int32_t voted_for_port) { - char buf[8]; - memcpy(buf, ¤t_term, sizeof(uint64_t)); - log_db_->Put(rocksdb::WriteOptions(), kCurrentTerm, std::string(buf, 8)); - log_db_->Put(rocksdb::WriteOptions(), kVoteForIp, voted_for_ip); - memcpy(buf, &voted_for_port, sizeof(uint32_t)); - log_db_->Put(rocksdb::WriteOptions(), kVoteForPort, std::string(buf, 4)); -} - -void RaftLog::UpdateLastApplied(uint64_t last_applied) { - last_applied_ = last_applied; - char buf[8]; - memcpy(buf, &last_applied, sizeof(uint64_t)); - log_db_->Put(rocksdb::WriteOptions(), kLastApplied, std::string(buf, 8)); -} - -void RaftLog::UpdateCommitIndex(uint64_t commit_index) { - commit_index_ = commit_index; - char buf[8]; - memcpy(buf, &commit_index_, sizeof(uint64_t)); - log_db_->Put(rocksdb::WriteOptions(), kCommitIndex, std::string(buf, 8)); -} - int RaftLog::TruncateSuffix(uint64_t index) { // here we need to delete the unnecessary entry, since we don't store // last_log_index in rocksdb for (uint64_t i = index; i <= last_log_index_; i++) { - log_db_->Delete(rocksdb::WriteOptions(), UintToBitStr(i)); + db_->Delete(rocksdb::WriteOptions(), UintToBitStr(i)); } last_log_index_ = index; return 0; diff --git a/floyd/src/raft_log.h b/floyd/src/raft_log.h index 5b1ae78..60bc151 100644 --- a/floyd/src/raft_log.h +++ b/floyd/src/raft_log.h @@ -20,47 +20,25 @@ class Entry; class RaftLog { public: - RaftLog(const std::string &path, Logger* info_log); + RaftLog(rocksdb::DB* db, Logger* info_log); ~RaftLog(); - uint64_t Append(const std::vector &entries); - uint64_t GetLastLogIndex(); + uint64_t Append(const std::vector &entries); int GetEntry(uint64_t index, Entry *entry); + uint64_t GetLastLogIndex(); bool GetLastLogTermAndIndex(uint64_t* last_log_term, uint64_t* last_log_index); - - void UpdateMetadata(uint64_t current_term, std::string voted_for_ip, - int32_t voted_for_port); int TruncateSuffix(uint64_t index); - // return persistent state from zeppelin - uint64_t current_term(); - std::string voted_for_ip(); - int voted_for_port(); - - uint64_t last_applied() { - return last_applied_; - } - void UpdateLastApplied(uint64_t last_applied); - - uint64_t last_log_index() { - return last_log_index_; - } - uint64_t commit_index() { - return commit_index_; - } - void UpdateCommitIndex(uint64_t commit_index); - private: - std::string path_; - /* * mutex for last_log_index_ */ slash::Mutex lli_mutex_; + uint64_t last_log_index_; - rocksdb::DB* log_db_; + rocksdb::DB* const db_; Logger* info_log_; /* @@ -68,10 +46,6 @@ class RaftLog { * we need update it every time I append an entry. * so we need update it when we open db */ - uint64_t last_log_index_; - - uint64_t commit_index_; - uint64_t last_applied_; RaftLog(const RaftLog&); void operator=(const RaftLog&); }; // RaftLog diff --git a/floyd/tools/log_parse/floyd.pb.cc b/floyd/tools/log_parse/floyd.pb.cc index 9360af1..ddee480 100644 --- a/floyd/tools/log_parse/floyd.pb.cc +++ b/floyd/tools/log_parse/floyd.pb.cc @@ -145,11 +145,11 @@ void protobuf_AssignDesc_floyd_2eproto() { sizeof(CmdRequest_User)); CmdRequest_RequestVote_descriptor_ = CmdRequest_descriptor_->nested_type(2); static const int CmdRequest_RequestVote_offsets_[5] = { + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, term_), GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, ip_), GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, port_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, term_), - GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, last_log_term_), GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, last_log_index_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, last_log_term_), }; CmdRequest_RequestVote_reflection_ = new ::google::protobuf::internal::GeneratedMessageReflection( @@ -402,9 +402,9 @@ void protobuf_AddDesc_floyd_2eproto() { "quest.AppendEntries\0225\n\rserver_status\030\006 \001" "(\0132\036.floyd.CmdRequest.ServerStatus\032 \n\002Kv" "\022\013\n\003key\030\001 \002(\014\022\r\n\005value\030\002 \001(\014\032 \n\004User\022\n\n\002" - "ip\030\001 \002(\014\022\014\n\004port\030\002 \002(\005\032d\n\013RequestVote\022\n\n" - "\002ip\030\001 \002(\014\022\014\n\004port\030\002 \002(\005\022\014\n\004term\030\003 \002(\004\022\025\n" - "\rlast_log_term\030\004 \002(\004\022\026\n\016last_log_index\030\005" + "ip\030\001 \002(\014\022\014\n\004port\030\002 \002(\005\032d\n\013RequestVote\022\014\n" + "\004term\030\001 \002(\004\022\n\n\002ip\030\002 \002(\014\022\014\n\004port\030\003 \002(\005\022\026\n" + "\016last_log_index\030\004 \002(\004\022\025\n\rlast_log_term\030\005" " \002(\004\032\234\001\n\rAppendEntries\022\n\n\002ip\030\001 \002(\014\022\014\n\004po" "rt\030\002 \002(\005\022\014\n\004term\030\003 \002(\004\022\026\n\016prev_log_index" "\030\004 \002(\004\022\025\n\rprev_log_term\030\005 \002(\004\022\025\n\rleader_" @@ -1415,11 +1415,11 @@ ::google::protobuf::Metadata CmdRequest_User::GetMetadata() const { // ------------------------------------------------------------------- #ifndef _MSC_VER +const int CmdRequest_RequestVote::kTermFieldNumber; const int CmdRequest_RequestVote::kIpFieldNumber; const int CmdRequest_RequestVote::kPortFieldNumber; -const int CmdRequest_RequestVote::kTermFieldNumber; -const int CmdRequest_RequestVote::kLastLogTermFieldNumber; const int CmdRequest_RequestVote::kLastLogIndexFieldNumber; +const int CmdRequest_RequestVote::kLastLogTermFieldNumber; #endif // !_MSC_VER CmdRequest_RequestVote::CmdRequest_RequestVote() @@ -1438,11 +1438,11 @@ CmdRequest_RequestVote::CmdRequest_RequestVote(const CmdRequest_RequestVote& fro void CmdRequest_RequestVote::SharedCtor() { _cached_size_ = 0; + term_ = GOOGLE_ULONGLONG(0); ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); port_ = 0; - term_ = GOOGLE_ULONGLONG(0); - last_log_term_ = GOOGLE_ULONGLONG(0); last_log_index_ = GOOGLE_ULONGLONG(0); + last_log_term_ = GOOGLE_ULONGLONG(0); ::memset(_has_bits_, 0, sizeof(_has_bits_)); } @@ -1481,15 +1481,15 @@ CmdRequest_RequestVote* CmdRequest_RequestVote::New() const { void CmdRequest_RequestVote::Clear() { if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + term_ = GOOGLE_ULONGLONG(0); if (has_ip()) { if (ip_ != &::google::protobuf::internal::kEmptyString) { ip_->clear(); } } port_ = 0; - term_ = GOOGLE_ULONGLONG(0); - last_log_term_ = GOOGLE_ULONGLONG(0); last_log_index_ = GOOGLE_ULONGLONG(0); + last_log_term_ = GOOGLE_ULONGLONG(0); } ::memset(_has_bits_, 0, sizeof(_has_bits_)); mutable_unknown_fields()->Clear(); @@ -1501,76 +1501,76 @@ bool CmdRequest_RequestVote::MergePartialFromCodedStream( ::google::protobuf::uint32 tag; while ((tag = input->ReadTag()) != 0) { switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { - // required bytes ip = 1; + // required uint64 term = 1; case 1: { if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->mutable_ip())); + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &term_))); + set_has_term(); } else { goto handle_uninterpreted; } - if (input->ExpectTag(16)) goto parse_port; + if (input->ExpectTag(18)) goto parse_ip; break; } - // required int32 port = 2; + // required bytes ip = 2; case 2: { if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { - parse_port: - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( - input, &port_))); - set_has_port(); + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_ip: + DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( + input, this->mutable_ip())); } else { goto handle_uninterpreted; } - if (input->ExpectTag(24)) goto parse_term; + if (input->ExpectTag(24)) goto parse_port; break; } - // required uint64 term = 3; + // required int32 port = 3; case 3: { if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { - parse_term: + parse_port: DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( - input, &term_))); - set_has_term(); + ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + input, &port_))); + set_has_port(); } else { goto handle_uninterpreted; } - if (input->ExpectTag(32)) goto parse_last_log_term; + if (input->ExpectTag(32)) goto parse_last_log_index; break; } - // required uint64 last_log_term = 4; + // required uint64 last_log_index = 4; case 4: { if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { - parse_last_log_term: + parse_last_log_index: DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( - input, &last_log_term_))); - set_has_last_log_term(); + input, &last_log_index_))); + set_has_last_log_index(); } else { goto handle_uninterpreted; } - if (input->ExpectTag(40)) goto parse_last_log_index; + if (input->ExpectTag(40)) goto parse_last_log_term; break; } - // required uint64 last_log_index = 5; + // required uint64 last_log_term = 5; case 5: { if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { - parse_last_log_index: + parse_last_log_term: DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( - input, &last_log_index_))); - set_has_last_log_index(); + input, &last_log_term_))); + set_has_last_log_term(); } else { goto handle_uninterpreted; } @@ -1596,30 +1596,30 @@ bool CmdRequest_RequestVote::MergePartialFromCodedStream( void CmdRequest_RequestVote::SerializeWithCachedSizes( ::google::protobuf::io::CodedOutputStream* output) const { - // required bytes ip = 1; + // required uint64 term = 1; + if (has_term()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(1, this->term(), output); + } + + // required bytes ip = 2; if (has_ip()) { ::google::protobuf::internal::WireFormatLite::WriteBytes( - 1, this->ip(), output); + 2, this->ip(), output); } - // required int32 port = 2; + // required int32 port = 3; if (has_port()) { - ::google::protobuf::internal::WireFormatLite::WriteInt32(2, this->port(), output); + ::google::protobuf::internal::WireFormatLite::WriteInt32(3, this->port(), output); } - // required uint64 term = 3; - if (has_term()) { - ::google::protobuf::internal::WireFormatLite::WriteUInt64(3, this->term(), output); + // required uint64 last_log_index = 4; + if (has_last_log_index()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(4, this->last_log_index(), output); } - // required uint64 last_log_term = 4; + // required uint64 last_log_term = 5; if (has_last_log_term()) { - ::google::protobuf::internal::WireFormatLite::WriteUInt64(4, this->last_log_term(), output); - } - - // required uint64 last_log_index = 5; - if (has_last_log_index()) { - ::google::protobuf::internal::WireFormatLite::WriteUInt64(5, this->last_log_index(), output); + ::google::protobuf::internal::WireFormatLite::WriteUInt64(5, this->last_log_term(), output); } if (!unknown_fields().empty()) { @@ -1630,31 +1630,31 @@ void CmdRequest_RequestVote::SerializeWithCachedSizes( ::google::protobuf::uint8* CmdRequest_RequestVote::SerializeWithCachedSizesToArray( ::google::protobuf::uint8* target) const { - // required bytes ip = 1; + // required uint64 term = 1; + if (has_term()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(1, this->term(), target); + } + + // required bytes ip = 2; if (has_ip()) { target = ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( - 1, this->ip(), target); + 2, this->ip(), target); } - // required int32 port = 2; + // required int32 port = 3; if (has_port()) { - target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(2, this->port(), target); + target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(3, this->port(), target); } - // required uint64 term = 3; - if (has_term()) { - target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(3, this->term(), target); + // required uint64 last_log_index = 4; + if (has_last_log_index()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(4, this->last_log_index(), target); } - // required uint64 last_log_term = 4; + // required uint64 last_log_term = 5; if (has_last_log_term()) { - target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(4, this->last_log_term(), target); - } - - // required uint64 last_log_index = 5; - if (has_last_log_index()) { - target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(5, this->last_log_index(), target); + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(5, this->last_log_term(), target); } if (!unknown_fields().empty()) { @@ -1668,41 +1668,41 @@ int CmdRequest_RequestVote::ByteSize() const { int total_size = 0; if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - // required bytes ip = 1; + // required uint64 term = 1; + if (has_term()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->term()); + } + + // required bytes ip = 2; if (has_ip()) { total_size += 1 + ::google::protobuf::internal::WireFormatLite::BytesSize( this->ip()); } - // required int32 port = 2; + // required int32 port = 3; if (has_port()) { total_size += 1 + ::google::protobuf::internal::WireFormatLite::Int32Size( this->port()); } - // required uint64 term = 3; - if (has_term()) { + // required uint64 last_log_index = 4; + if (has_last_log_index()) { total_size += 1 + ::google::protobuf::internal::WireFormatLite::UInt64Size( - this->term()); + this->last_log_index()); } - // required uint64 last_log_term = 4; + // required uint64 last_log_term = 5; if (has_last_log_term()) { total_size += 1 + ::google::protobuf::internal::WireFormatLite::UInt64Size( this->last_log_term()); } - // required uint64 last_log_index = 5; - if (has_last_log_index()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::UInt64Size( - this->last_log_index()); - } - } if (!unknown_fields().empty()) { total_size += @@ -1730,21 +1730,21 @@ void CmdRequest_RequestVote::MergeFrom(const ::google::protobuf::Message& from) void CmdRequest_RequestVote::MergeFrom(const CmdRequest_RequestVote& from) { GOOGLE_CHECK_NE(&from, this); if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (from.has_term()) { + set_term(from.term()); + } if (from.has_ip()) { set_ip(from.ip()); } if (from.has_port()) { set_port(from.port()); } - if (from.has_term()) { - set_term(from.term()); + if (from.has_last_log_index()) { + set_last_log_index(from.last_log_index()); } if (from.has_last_log_term()) { set_last_log_term(from.last_log_term()); } - if (from.has_last_log_index()) { - set_last_log_index(from.last_log_index()); - } } mutable_unknown_fields()->MergeFrom(from.unknown_fields()); } @@ -1769,11 +1769,11 @@ bool CmdRequest_RequestVote::IsInitialized() const { void CmdRequest_RequestVote::Swap(CmdRequest_RequestVote* other) { if (other != this) { + std::swap(term_, other->term_); std::swap(ip_, other->ip_); std::swap(port_, other->port_); - std::swap(term_, other->term_); - std::swap(last_log_term_, other->last_log_term_); std::swap(last_log_index_, other->last_log_index_); + std::swap(last_log_term_, other->last_log_term_); std::swap(_has_bits_[0], other->_has_bits_[0]); _unknown_fields_.Swap(&other->_unknown_fields_); std::swap(_cached_size_, other->_cached_size_); diff --git a/floyd/tools/log_parse/floyd.pb.h b/floyd/tools/log_parse/floyd.pb.h index 852d052..f89f5ff 100644 --- a/floyd/tools/log_parse/floyd.pb.h +++ b/floyd/tools/log_parse/floyd.pb.h @@ -514,10 +514,17 @@ class CmdRequest_RequestVote : public ::google::protobuf::Message { // accessors ------------------------------------------------------- - // required bytes ip = 1; + // required uint64 term = 1; + inline bool has_term() const; + inline void clear_term(); + static const int kTermFieldNumber = 1; + inline ::google::protobuf::uint64 term() const; + inline void set_term(::google::protobuf::uint64 value); + + // required bytes ip = 2; inline bool has_ip() const; inline void clear_ip(); - static const int kIpFieldNumber = 1; + static const int kIpFieldNumber = 2; inline const ::std::string& ip() const; inline void set_ip(const ::std::string& value); inline void set_ip(const char* value); @@ -526,53 +533,46 @@ class CmdRequest_RequestVote : public ::google::protobuf::Message { inline ::std::string* release_ip(); inline void set_allocated_ip(::std::string* ip); - // required int32 port = 2; + // required int32 port = 3; inline bool has_port() const; inline void clear_port(); - static const int kPortFieldNumber = 2; + static const int kPortFieldNumber = 3; inline ::google::protobuf::int32 port() const; inline void set_port(::google::protobuf::int32 value); - // required uint64 term = 3; - inline bool has_term() const; - inline void clear_term(); - static const int kTermFieldNumber = 3; - inline ::google::protobuf::uint64 term() const; - inline void set_term(::google::protobuf::uint64 value); + // required uint64 last_log_index = 4; + inline bool has_last_log_index() const; + inline void clear_last_log_index(); + static const int kLastLogIndexFieldNumber = 4; + inline ::google::protobuf::uint64 last_log_index() const; + inline void set_last_log_index(::google::protobuf::uint64 value); - // required uint64 last_log_term = 4; + // required uint64 last_log_term = 5; inline bool has_last_log_term() const; inline void clear_last_log_term(); - static const int kLastLogTermFieldNumber = 4; + static const int kLastLogTermFieldNumber = 5; inline ::google::protobuf::uint64 last_log_term() const; inline void set_last_log_term(::google::protobuf::uint64 value); - // required uint64 last_log_index = 5; - inline bool has_last_log_index() const; - inline void clear_last_log_index(); - static const int kLastLogIndexFieldNumber = 5; - inline ::google::protobuf::uint64 last_log_index() const; - inline void set_last_log_index(::google::protobuf::uint64 value); - // @@protoc_insertion_point(class_scope:floyd.CmdRequest.RequestVote) private: + inline void set_has_term(); + inline void clear_has_term(); inline void set_has_ip(); inline void clear_has_ip(); inline void set_has_port(); inline void clear_has_port(); - inline void set_has_term(); - inline void clear_has_term(); - inline void set_has_last_log_term(); - inline void clear_has_last_log_term(); inline void set_has_last_log_index(); inline void clear_has_last_log_index(); + inline void set_has_last_log_term(); + inline void clear_has_last_log_term(); ::google::protobuf::UnknownFieldSet _unknown_fields_; - ::std::string* ip_; ::google::protobuf::uint64 term_; - ::google::protobuf::uint64 last_log_term_; + ::std::string* ip_; ::google::protobuf::uint64 last_log_index_; + ::google::protobuf::uint64 last_log_term_; ::google::protobuf::int32 port_; mutable int _cached_size_; @@ -2162,16 +2162,38 @@ inline void CmdRequest_User::set_port(::google::protobuf::int32 value) { // CmdRequest_RequestVote -// required bytes ip = 1; -inline bool CmdRequest_RequestVote::has_ip() const { +// required uint64 term = 1; +inline bool CmdRequest_RequestVote::has_term() const { return (_has_bits_[0] & 0x00000001u) != 0; } -inline void CmdRequest_RequestVote::set_has_ip() { +inline void CmdRequest_RequestVote::set_has_term() { _has_bits_[0] |= 0x00000001u; } -inline void CmdRequest_RequestVote::clear_has_ip() { +inline void CmdRequest_RequestVote::clear_has_term() { _has_bits_[0] &= ~0x00000001u; } +inline void CmdRequest_RequestVote::clear_term() { + term_ = GOOGLE_ULONGLONG(0); + clear_has_term(); +} +inline ::google::protobuf::uint64 CmdRequest_RequestVote::term() const { + return term_; +} +inline void CmdRequest_RequestVote::set_term(::google::protobuf::uint64 value) { + set_has_term(); + term_ = value; +} + +// required bytes ip = 2; +inline bool CmdRequest_RequestVote::has_ip() const { + return (_has_bits_[0] & 0x00000002u) != 0; +} +inline void CmdRequest_RequestVote::set_has_ip() { + _has_bits_[0] |= 0x00000002u; +} +inline void CmdRequest_RequestVote::clear_has_ip() { + _has_bits_[0] &= ~0x00000002u; +} inline void CmdRequest_RequestVote::clear_ip() { if (ip_ != &::google::protobuf::internal::kEmptyString) { ip_->clear(); @@ -2232,15 +2254,15 @@ inline void CmdRequest_RequestVote::set_allocated_ip(::std::string* ip) { } } -// required int32 port = 2; +// required int32 port = 3; inline bool CmdRequest_RequestVote::has_port() const { - return (_has_bits_[0] & 0x00000002u) != 0; + return (_has_bits_[0] & 0x00000004u) != 0; } inline void CmdRequest_RequestVote::set_has_port() { - _has_bits_[0] |= 0x00000002u; + _has_bits_[0] |= 0x00000004u; } inline void CmdRequest_RequestVote::clear_has_port() { - _has_bits_[0] &= ~0x00000002u; + _has_bits_[0] &= ~0x00000004u; } inline void CmdRequest_RequestVote::clear_port() { port_ = 0; @@ -2254,37 +2276,37 @@ inline void CmdRequest_RequestVote::set_port(::google::protobuf::int32 value) { port_ = value; } -// required uint64 term = 3; -inline bool CmdRequest_RequestVote::has_term() const { - return (_has_bits_[0] & 0x00000004u) != 0; +// required uint64 last_log_index = 4; +inline bool CmdRequest_RequestVote::has_last_log_index() const { + return (_has_bits_[0] & 0x00000008u) != 0; } -inline void CmdRequest_RequestVote::set_has_term() { - _has_bits_[0] |= 0x00000004u; +inline void CmdRequest_RequestVote::set_has_last_log_index() { + _has_bits_[0] |= 0x00000008u; } -inline void CmdRequest_RequestVote::clear_has_term() { - _has_bits_[0] &= ~0x00000004u; +inline void CmdRequest_RequestVote::clear_has_last_log_index() { + _has_bits_[0] &= ~0x00000008u; } -inline void CmdRequest_RequestVote::clear_term() { - term_ = GOOGLE_ULONGLONG(0); - clear_has_term(); +inline void CmdRequest_RequestVote::clear_last_log_index() { + last_log_index_ = GOOGLE_ULONGLONG(0); + clear_has_last_log_index(); } -inline ::google::protobuf::uint64 CmdRequest_RequestVote::term() const { - return term_; +inline ::google::protobuf::uint64 CmdRequest_RequestVote::last_log_index() const { + return last_log_index_; } -inline void CmdRequest_RequestVote::set_term(::google::protobuf::uint64 value) { - set_has_term(); - term_ = value; +inline void CmdRequest_RequestVote::set_last_log_index(::google::protobuf::uint64 value) { + set_has_last_log_index(); + last_log_index_ = value; } -// required uint64 last_log_term = 4; +// required uint64 last_log_term = 5; inline bool CmdRequest_RequestVote::has_last_log_term() const { - return (_has_bits_[0] & 0x00000008u) != 0; + return (_has_bits_[0] & 0x00000010u) != 0; } inline void CmdRequest_RequestVote::set_has_last_log_term() { - _has_bits_[0] |= 0x00000008u; + _has_bits_[0] |= 0x00000010u; } inline void CmdRequest_RequestVote::clear_has_last_log_term() { - _has_bits_[0] &= ~0x00000008u; + _has_bits_[0] &= ~0x00000010u; } inline void CmdRequest_RequestVote::clear_last_log_term() { last_log_term_ = GOOGLE_ULONGLONG(0); @@ -2298,28 +2320,6 @@ inline void CmdRequest_RequestVote::set_last_log_term(::google::protobuf::uint64 last_log_term_ = value; } -// required uint64 last_log_index = 5; -inline bool CmdRequest_RequestVote::has_last_log_index() const { - return (_has_bits_[0] & 0x00000010u) != 0; -} -inline void CmdRequest_RequestVote::set_has_last_log_index() { - _has_bits_[0] |= 0x00000010u; -} -inline void CmdRequest_RequestVote::clear_has_last_log_index() { - _has_bits_[0] &= ~0x00000010u; -} -inline void CmdRequest_RequestVote::clear_last_log_index() { - last_log_index_ = GOOGLE_ULONGLONG(0); - clear_has_last_log_index(); -} -inline ::google::protobuf::uint64 CmdRequest_RequestVote::last_log_index() const { - return last_log_index_; -} -inline void CmdRequest_RequestVote::set_last_log_index(::google::protobuf::uint64 value) { - set_has_last_log_index(); - last_log_index_ = value; -} - // ------------------------------------------------------------------- // CmdRequest_AppendEntries From 5dd928b3d7f78353714b40bff4132c2d95174c22 Mon Sep 17 00:00:00 2001 From: baotiao Date: Thu, 27 Jul 2017 00:53:55 +0800 Subject: [PATCH 07/17] first runnable version. mark --- floyd/example/simple/t.cc | 12 +- floyd/example/simple/t1.cc | 4 +- floyd/example/simple/t2.cc | 172 +- floyd/src/floyd_apply.cc | 7 +- floyd/src/floyd_apply.h | 2 +- floyd/src/floyd_context.cc | 25 +- floyd/src/floyd_context.h | 9 +- floyd/src/floyd_impl.cc | 79 +- floyd/src/floyd_impl.h | 9 +- floyd/src/floyd_options.cc | 11 +- floyd/src/floyd_peer_thread.cc | 90 +- floyd/src/floyd_peer_thread.h | 20 +- floyd/src/floyd_primary_thread.cc | 37 +- floyd/src/floyd_primary_thread.h | 19 +- floyd/src/raft_log.cc | 12 +- floyd/src/raft_log.h | 5 +- floyd/src/raft_meta.cc | 136 + floyd/src/raft_meta.h | 57 + floyd/tools/Makefile | 2 +- floyd/tools/floyd.pb.cc | 5143 +++++++++++++++++++++++++++++ floyd/tools/floyd.pb.h | 3816 +++++++++++++++++++++ floyd/tools/read_floyd.cc | 19 +- 22 files changed, 9458 insertions(+), 228 deletions(-) create mode 100644 floyd/src/raft_meta.cc create mode 100644 floyd/src/raft_meta.h create mode 100644 floyd/tools/floyd.pb.cc create mode 100644 floyd/tools/floyd.pb.h diff --git a/floyd/example/simple/t.cc b/floyd/example/simple/t.cc index ddebcb3..4e4bb06 100644 --- a/floyd/example/simple/t.cc +++ b/floyd/example/simple/t.cc @@ -55,12 +55,12 @@ int main() } while (i--) { - std::string mystr[100100]; - for (int i = 0; i < 100000; i++) { - mystr[i] = slash::RandomString(100); - } - // f1->GetServerStatus(msg); - // printf("%s\n", msg.c_str()); + std::string mystr[100100]; + for (int i = 0; i < 100000; i++) { + mystr[i] = slash::RandomString(10); + } + f1->GetServerStatus(msg); + printf("%s\n", msg.c_str()); st = NowMicros(); for (int j = 0; j < 100000; j++) { f1->Write(mystr[j], mystr[j]); diff --git a/floyd/example/simple/t1.cc b/floyd/example/simple/t1.cc index 0f3a839..f532d38 100644 --- a/floyd/example/simple/t1.cc +++ b/floyd/example/simple/t1.cc @@ -26,7 +26,7 @@ std::string mystr[100100]; void *fun(void *arg) { int i = 1; while (i--) { - for (int j = 0; j < 10000; j++) { + for (int j = 0; j < 100000; j++) { f1->Write(mystr[j], mystr[j]); } } @@ -80,7 +80,7 @@ int main() pthread_join(pid[i], NULL); } ed = NowMicros(); - printf("write 10000 cost time microsecond(us) %ld, qps %llu\n", ed - st, 10000 * 8 * 1000000LL / (ed - st)); + printf("write 100000 cost time microsecond(us) %ld, qps %llu\n", ed - st, 100000 * 8 * 1000000LL / (ed - st)); getchar(); diff --git a/floyd/example/simple/t2.cc b/floyd/example/simple/t2.cc index c6a9d47..bd80c37 100644 --- a/floyd/example/simple/t2.cc +++ b/floyd/example/simple/t2.cc @@ -26,106 +26,128 @@ int main() s = Floyd::Open(op, &f1); printf("%s\n", s.ToString().c_str()); + usleep(100000); Options op2("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8902, "./data2/"); s = Floyd::Open(op2, &f2); printf("%s\n", s.ToString().c_str()); + usleep(200000); Options op3("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8903, "./data3/"); s = Floyd::Open(op3, &f3); printf("%s\n", s.ToString().c_str()); + usleep(300000); Options op4("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8904, "./data4/"); s = Floyd::Open(op4, &f4); printf("%s\n", s.ToString().c_str()); + usleep(400000); Options op5("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8905, "./data5/"); s = Floyd::Open(op5, &f5); printf("%s\n", s.ToString().c_str()); std::string msg; - int i = 100; - uint64_t st = NowMicros(), ed; - std::string mystr[10010]; - for (int i = 0; i < 10000; i++) { - mystr[i] = slash::RandomString(10); - } - - sleep(10); - while (1) { - if (f1->HasLeader()) { - break; - } - sleep(2); - printf("electing leader, waitting...\n"); - } - - // ran at the begining - printf("run 5 times, every time write 100 item. at the beginning state\n"); - i = 5; + int i = 8; while (i--) { - f2->GetServerStatus(msg); - for (int j = 0; j < 100; j++) { - f2->Write(mystr[j], mystr[j]); - } + f1->GetServerStatus(msg); printf("%s\n", msg.c_str()); + sleep(2); } - - // delete two node - delete f1; + delete f2; delete f3; - while (1) { - if (f2->HasLeader()) { - break; - } - sleep(2); - printf("electing leader, waitting...\n"); - } - // ran with three node - printf("delete two node, run 5 times, every time write 100 item with three node\n"); - i = 5; - while (i--) { - f2->GetServerStatus(msg); - for (int j = 0; j < 100; j++) { - f2->Write(mystr[j], mystr[j]); - } + f1->GetServerStatus(msg); printf("%s\n", msg.c_str()); - } - - s = Floyd::Open(op, &f1); - if (!s.ok()) { - printf("floyd reoptn failed\n"); - } - s = Floyd::Open(op3, &f3); - if (!s.ok()) { - printf("floyd reoptn failed\n"); - } - - while (1) { - if (f1->HasLeader()) { - break; - } sleep(2); - printf("electing leader, waitting...\n"); } - // ran with node recovery - printf("recovery the two node, run 5 time, every time write 100 item\n"); - i = 5; - while (i--) { - f2->GetServerStatus(msg); - for (int j = 0; j < 100; j++) { - f1->Write(mystr[j], mystr[j]); - } - printf("%s\n", msg.c_str()); - } - - // at last, we will have 300 log, 100 db - getchar(); - delete f2; - delete f3; - delete f4; - delete f5; - delete f1; +/* + * std::string msg; + * int i = 100; + * uint64_t st = NowMicros(), ed; + * std::string mystr[10010]; + * for (int i = 0; i < 10000; i++) { + * mystr[i] = slash::RandomString(10); + * } + * + * sleep(10); + * while (1) { + * if (f1->HasLeader()) { + * break; + * } + * sleep(2); + * printf("electing leader, waitting...\n"); + * } + * + * // ran at the begining + * printf("run 5 times, every time write 100 item. at the beginning state\n"); + * i = 5; + * while (i--) { + * f2->GetServerStatus(msg); + * for (int j = 0; j < 100; j++) { + * f2->Write(mystr[j], mystr[j]); + * } + * printf("%s\n", msg.c_str()); + * } + * + * + * // delete two node + * delete f1; + * delete f3; + * + * while (1) { + * if (f2->HasLeader()) { + * break; + * } + * sleep(2); + * printf("electing leader, waitting...\n"); + * } + * // ran with three node + * printf("delete two node, run 5 times, every time write 100 item with three node\n"); + * i = 5; + * while (i--) { + * f2->GetServerStatus(msg); + * for (int j = 0; j < 100; j++) { + * f2->Write(mystr[j], mystr[j]); + * } + * printf("%s\n", msg.c_str()); + * } + * + * s = Floyd::Open(op, &f1); + * if (!s.ok()) { + * printf("floyd reoptn failed\n"); + * } + * s = Floyd::Open(op3, &f3); + * if (!s.ok()) { + * printf("floyd reoptn failed\n"); + * } + * + * while (1) { + * if (f1->HasLeader()) { + * break; + * } + * sleep(2); + * printf("electing leader, waitting...\n"); + * } + * // ran with node recovery + * printf("recovery the two node, run 5 time, every time write 100 item\n"); + * i = 5; + * while (i--) { + * f2->GetServerStatus(msg); + * for (int j = 0; j < 100; j++) { + * f1->Write(mystr[j], mystr[j]); + * } + * printf("%s\n", msg.c_str()); + * } + * + * // at last, we will have 300 log, 100 db + */ + getchar(); + delete f2; + delete f3; + delete f4; + delete f5; + delete f1; + return 0; } diff --git a/floyd/src/floyd_apply.cc b/floyd/src/floyd_apply.cc index c4a6745..fe64780 100644 --- a/floyd/src/floyd_apply.cc +++ b/floyd/src/floyd_apply.cc @@ -37,7 +37,7 @@ int FloydApply::Start() { return bg_thread_.StartThread(); } -Status FloydApply::ScheduleApply() { +void FloydApply::ScheduleApply() { bg_thread_.Schedule(&ApplyStateMachineWrapper, this); } @@ -55,7 +55,8 @@ void FloydApply::ApplyStateMachine() { LOGV(DEBUG_LEVEL, info_log_, "FloydApply::ApplyStateMachine: last_applied: %lu, commit_index: %lu", last_applied, commit_index); - while (++last_applied <= commit_index) { + while (last_applied < commit_index) { + last_applied++; Entry log_entry; raft_log_->GetEntry(last_applied, &log_entry); Status s = Apply(log_entry); @@ -66,11 +67,11 @@ void FloydApply::ApplyStateMachine() { usleep(1000000); return; } - raft_meta_->SetLastApplied(last_applied); } context_->apply_mu.Lock(); context_->last_applied = last_applied; + raft_meta_->SetLastApplied(last_applied); context_->apply_mu.Unlock(); context_->apply_cond.SignalAll(); } diff --git a/floyd/src/floyd_apply.h b/floyd/src/floyd_apply.h index 17764dc..876e5e0 100644 --- a/floyd/src/floyd_apply.h +++ b/floyd/src/floyd_apply.h @@ -24,7 +24,7 @@ class FloydApply { FloydApply(FloydContext* context, rocksdb::DB* db, RaftMeta* raft_meta, RaftLog* raft_log, Logger* info_log); ~FloydApply(); int Start(); - Status ScheduleApply(); + void ScheduleApply(); private: pink::BGThread bg_thread_; diff --git a/floyd/src/floyd_context.cc b/floyd/src/floyd_context.cc index 14ddcb9..a9549e6 100644 --- a/floyd/src/floyd_context.cc +++ b/floyd/src/floyd_context.cc @@ -21,16 +21,10 @@ void FloydContext::RecoverInit(RaftMeta *raft_meta) { voted_for_ip = raft_meta->GetVotedForIp(); voted_for_port = raft_meta->GetVotedForPort(); commit_index = raft_meta->GetCommitIndex(); + last_applied = raft_meta->GetLastApplied(); role = Role::kFollower; } -bool FloydContext::HasLeader() { - if (leader_ip == "" || leader_port == 0) { - return false; - } - return true; -} - void FloydContext::leader_node(std::string* ip, int* port) { *ip = leader_ip; *port = leader_port; @@ -43,9 +37,13 @@ void FloydContext::voted_for_node(std::string* ip, int* port) { void FloydContext::BecomeFollower(uint64_t new_term, const std::string _leader_ip, int _leader_port) { + // when requestvote receive a large term, then we transfer from candidate to follower + // then we should set voted_for_ip to the leader_ip + // if (current_term < new_term) { + voted_for_ip = _leader_ip; + voted_for_port = _leader_port; + // } current_term = new_term; - voted_for_ip = ""; - voted_for_port = 0; leader_ip = _leader_ip; leader_port = _leader_port; role = Role::kFollower; @@ -66,13 +64,4 @@ void FloydContext::BecomeLeader() { leader_ip = options.local_ip; leader_port = options.local_port; } - -// Peer ask my vote with it's ip, port, log_term and log_index -void FloydContext::GrantVote(uint64_t term, const std::string ip, int port) { - // Got my vote - voted_for_ip = ip; - voted_for_port = port; - current_term = term; -} - } // namespace floyd diff --git a/floyd/src/floyd_context.h b/floyd/src/floyd_context.h index 9bb7597..326ebc1 100644 --- a/floyd/src/floyd_context.h +++ b/floyd/src/floyd_context.h @@ -31,19 +31,21 @@ struct FloydContext { /* Role related */ FloydContext(const Options& _options) : options(_options), + voted_for_ip(""), + voted_for_port(0), + leader_ip(""), + leader_port(0), + vote_quorum(0), apply_cond(&apply_mu) {}; void leader_node(std::string* ip, int* port); void voted_for_node(std::string* ip, int* port); - bool HasLeader(); - void RecoverInit(RaftMeta *raft); void BecomeFollower(uint64_t new_iterm, const std::string leader_ip = "", int port = 0); void BecomeCandidate(); void BecomeLeader(); - void GrantVote(uint64_t term, const std::string ip, const int port); bool ReceiverDoAppendEntries(uint64_t term, uint64_t pre_log_term, uint64_t pre_log_index, std::vector& entries, uint64_t* my_term); @@ -71,7 +73,6 @@ struct FloydContext { slash::Mutex commit_mu; slash::Mutex apply_mu; slash::CondVar apply_cond; - }; } // namespace floyd diff --git a/floyd/src/floyd_impl.cc b/floyd/src/floyd_impl.cc index b523376..d8f7afa 100644 --- a/floyd/src/floyd_impl.cc +++ b/floyd/src/floyd_impl.cc @@ -40,14 +40,15 @@ FloydImpl::~FloydImpl() { delete worker_client_pool_; delete primary_; delete apply_; - for (auto& pt : *peers_) { + for (auto& pt : peers_) { delete pt.second; } - delete peers_; delete context_; - delete db_; + delete raft_meta_; delete raft_log_; + delete log_and_meta_; delete info_log_; + delete db_; } bool FloydImpl::IsSelf(const std::string& ip_port) { @@ -72,7 +73,10 @@ bool FloydImpl::GetLeader(std::string* ip, int* port) { } bool FloydImpl::HasLeader() { - return context_->HasLeader(); + if (context_->leader_ip == "" || context_->leader_port == 0) { + return false; + } + return true; } bool FloydImpl::GetAllNodes(std::vector& nodes) { @@ -113,37 +117,40 @@ Status FloydImpl::Init() { // Recover Context raft_log_ = new RaftLog(log_and_meta_, info_log_); raft_meta_ = new RaftMeta(log_and_meta_, info_log_); + raft_meta_->Init(); context_ = new FloydContext(options_); context_->RecoverInit(raft_meta_); // Create Apply threads apply_ = new FloydApply(context_, db_, raft_meta_, raft_log_, info_log_); + apply_->Start(); // peers and primary refer to each other // Create PrimaryThread before Peers - primary_ = new FloydPrimary(context_, apply_, options_, info_log_); + primary_ = new FloydPrimary(context_, raft_meta_, options_, info_log_); // Create peer threads - peers_ = new PeersSet(); + // peers_.clear(); for (auto iter = options_.members.begin(); iter != options_.members.end(); iter++) { if (!IsSelf(*iter)) { - Peer* pt = new Peer(*iter, context_, primary_, raft_log_, worker_client_pool_, options_, info_log_); - peers_->insert(std::pair(*iter, pt)); + Peer* pt = new Peer(*iter, context_, primary_, raft_meta_, raft_log_, + worker_client_pool_, apply_, options_, info_log_); + peers_.insert(std::pair(*iter, pt)); } } // Start peer thread int ret; - for (auto& pt : *peers_) { + for (auto& pt : peers_) { + pt.second->set_peers(peers_); if ((ret = pt.second->StartThread()) != 0) { - pt.second->set_peers(peers_); LOGV(ERROR_LEVEL, info_log_, "FloydImpl peer thread to %s failed to " " start, ret is %d", pt.first.c_str(), ret); return Status::Corruption("failed to start peer thread to " + pt.first); } } - LOGV(INFO_LEVEL, info_log_, "Floyd start %d peer thread", peers_->size()); + LOGV(INFO_LEVEL, info_log_, "Floyd start %d peer thread", peers_.size()); // Start worker thread after Peers, because WorkerHandle will check peers worker_ = new FloydWorker(options_.local_port, 1000, this); @@ -347,8 +354,8 @@ bool FloydImpl::GetServerStatus(std::string& msg) { char str[512]; snprintf (str, 512, - " Node | Role | Term | CommitIdx | Leader | VoteFor | LastLogTerm | LastLogIdx | LastApplyIdx |\n" - "%15s:%-6d %9s %10lu %10lu %15s:%-6d %15s:%-6d %10lu %10lu %10lu\n", + " Node | Role | Term | CommitIdx | Leader | VoteFor | LastLogTerm | LastLogIdx | CommitIndex | LastApplied |\n" + "%15s:%-6d %9s %10lu %10lu %15s:%-6d %15s:%-6d %10lu %10lu %10lu %10lu\n", options_.local_ip.c_str(), options_.local_port, server_status.role().c_str(), server_status.term(), server_status.commit_index(), @@ -375,13 +382,13 @@ bool FloydImpl::GetServerStatus(std::string& msg) { slash::ParseIpPortString(iter, ip, port); CmdResponse_ServerStatus server_status = response.server_status(); snprintf (str, 512, - "%15s:%-6d %9s %10lu %10lu %15s:%-6d %15s:%-6d %10lu %10lu %10lu\n", + "%15s:%-6d %9s %10lu %10lu %15s:%-6d %15s:%-6d %10lu %10lu %10lu %10lu\n", ip.c_str(), port, server_status.role().c_str(), server_status.term(), server_status.commit_index(), server_status.leader_ip().c_str(), server_status.leader_port(), server_status.voted_for_ip().c_str(), server_status.voted_for_port(), - server_status.last_log_term(), server_status.last_log_index(), + server_status.last_log_term(), server_status.last_log_index(), server_status.commit_index(), server_status.last_applied()); msg.append(str); LOGV(DEBUG_LEVEL, info_log_, "GetServerStatus msg(%s)", str); @@ -560,11 +567,19 @@ Status FloydImpl::ExecuteCommand(const CmdRequest& request, } +// Peer ask my vote with it's ip, port, log_term and log_index +void FloydImpl::GrantVote(uint64_t term, const std::string ip, int port) { + // Got my vote + context_->voted_for_ip = ip; + context_->voted_for_port = port; + context_->current_term = term; +} + void FloydImpl::ReplyRequestVote(const CmdRequest& request, CmdResponse* response) { slash::MutexLock l(&context_->commit_mu); bool granted = false; CmdRequest_RequestVote request_vote = request.request_vote(); - LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyRequestVote: my_term=%lu rqv.term=%lu", + LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyRequestVote: my_term=%lu request.term=%lu", context_->current_term, request_vote.term()); uint64_t current_term; // if caller's term smaller than my term, then I will notice him @@ -583,7 +598,8 @@ void FloydImpl::ReplyRequestVote(const CmdRequest& request, CmdResponse* respons return; } - if (vote_for_.find(request_vote.term()) != vote_for_.end() && vote_for_[request_vote.term()] != std::make_pair(request_vote.ip(), request_vote.port())) { + if (vote_for_.find(request_vote.term()) != vote_for_.end() + && vote_for_[request_vote.term()] != std::make_pair(request_vote.ip(), request_vote.port())) { LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyRequestVote: BecomeFollower with current_term_(%lu) and new_term(%lu)" " commit_index(%lu) last_applied(%lu)", context_->current_term, request_vote.last_log_term(), my_last_log_index, context_->last_applied); @@ -596,8 +612,11 @@ void FloydImpl::ReplyRequestVote(const CmdRequest& request, CmdResponse* respons raft_meta_->SetVotedForIp(context_->voted_for_ip); raft_meta_->SetVotedForPort(context_->voted_for_port); // Got my vote - context_->GrantVote(request_vote.term(), request_vote.ip(), request_vote.port()); + GrantVote(request_vote.term(), request_vote.ip(), request_vote.port()); granted = true; + LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyRequestVote: Grant my vote to %s:%d at term %lu", + context_->voted_for_ip.c_str(), context_->voted_for_port, context_->current_term); + context_->last_op_time = slash::NowMicros(); BuildRequestVoteResponse(context_->current_term, granted, response); } @@ -620,7 +639,7 @@ void FloydImpl::ReplyAppendEntries(CmdRequest& request, CmdResponse* response) { if (append_entries.term() < context_->current_term) { BuildAppendEntriesResponse(success, context_->current_term, last_log_index, response); return; - } else if (append_entries.term() > context_->current_term) { + } else if (append_entries.term() >= context_->current_term) { context_->BecomeFollower(append_entries.term(), append_entries.ip(), append_entries.port()); raft_meta_->SetCurrentTerm(context_->current_term); @@ -642,12 +661,14 @@ void FloydImpl::ReplyAppendEntries(CmdRequest& request, CmdResponse* response) { } uint64_t my_log_term = 0; Entry entry; - LOGV(DEBUG_LEVEL, info_log_, "RaftMeta::ReceiverDoAppendEntries" + LOGV(DEBUG_LEVEL, info_log_, "RaftMeta::ReceiverDoAppendEntries " "prev_log_index: %llu\n", append_entries.prev_log_index()); - if (raft_log_->GetEntry(append_entries.prev_log_index(), &entry) == 0) { + if (append_entries.prev_log_index() == 0) { + my_log_term = 0; + } else if (raft_log_->GetEntry(append_entries.prev_log_index(), &entry) == 0) { my_log_term = entry.term(); } else { - LOGV(WARN_LEVEL, info_log_, "FloydImple::ReplyAppentries: can't" + LOGV(WARN_LEVEL, info_log_, "FloydImple::ReplyAppentries: can't " "get Entry from raft_log prev_log_index %llu", append_entries.prev_log_index()); BuildAppendEntriesResponse(success, context_->current_term, last_log_index, response); return; @@ -671,17 +692,19 @@ void FloydImpl::ReplyAppendEntries(CmdRequest& request, CmdResponse* response) { LOGV(DEBUG_LEVEL, info_log_, "RaftMeta::ReceiverDoAppendEntries: will append %u entries from " " prev_log_index %lu", entries.size(), append_entries.prev_log_index() + 1); if (raft_log_->Append(entries) <= 0) { + context_->last_op_time = slash::NowMicros(); + BuildAppendEntriesResponse(success, context_->current_term, raft_log_->GetLastLogIndex(), response); return ; } } + success = true; // only when follower successfully do appendentries, we will update commit index - if (success) { - AdvanceFollowerCommitIndex(append_entries.leader_commit()); - LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries after AdvanceCommitIndex %lu", - context_->commit_index); - apply_->ScheduleApply(); - } + AdvanceFollowerCommitIndex(append_entries.leader_commit()); + LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries after AdvanceCommitIndex %lu", + context_->commit_index); + apply_->ScheduleApply(); + context_->last_op_time = slash::NowMicros(); BuildAppendEntriesResponse(success, context_->current_term, raft_log_->GetLastLogIndex(), response); } diff --git a/floyd/src/floyd_impl.h b/floyd/src/floyd_impl.h index 8a5b129..baa6eaa 100644 --- a/floyd/src/floyd_impl.h +++ b/floyd/src/floyd_impl.h @@ -66,23 +66,23 @@ class FloydImpl : public Floyd { friend class FloydWorkerHandle; friend class Peer; + rocksdb::DB* db_; + Options options_; // debug log used for ouput to file Logger* info_log_; - Options options_; // state machine db point - rocksdb::DB* db_; // raft log + rocksdb::DB* log_and_meta_; // used to store logs and meta data RaftLog* raft_log_; RaftMeta* raft_meta_; - rocksdb::DB* log_and_meta_; // used to store logs and meta data FloydContext* context_; FloydWorker* worker_; FloydApply* apply_; FloydPrimary* primary_; - PeersSet* peers_; + PeersSet peers_; ClientPool* worker_client_pool_; std::map > vote_for_; @@ -93,6 +93,7 @@ class FloydImpl : public Floyd { Status ExecuteCommand(const CmdRequest& cmd, CmdResponse *cmd_res); Status ReplyExecuteDirtyCommand(const CmdRequest& cmd, CmdResponse *cmd_res); bool DoGetServerStatus(CmdResponse_ServerStatus* res); + void GrantVote(uint64_t term, const std::string ip, int port); /* * these two are the response to the request vote and appendentries diff --git a/floyd/src/floyd_options.cc b/floyd/src/floyd_options.cc index 64c025e..2e3dec1 100644 --- a/floyd/src/floyd_options.cc +++ b/floyd/src/floyd_options.cc @@ -8,6 +8,8 @@ #include #include +#include "slash/include/env.h" + namespace floyd { void split(const std::string &str, char delim, @@ -83,7 +85,7 @@ Options::Options() : local_ip("127.0.0.1"), local_port(10086), path("/data/floyd"), - check_leader_us(3000000), + check_leader_us(5000000), heartbeat_us(1000000), append_entries_size_once(1024), append_entries_count_once(24), @@ -96,16 +98,17 @@ Options::Options(const std::string& cluster_string, : local_ip(_local_ip), local_port(_local_port), path(_path), - check_leader_us(3000000), + check_leader_us(5000000), heartbeat_us(1000000), append_entries_size_once(1024), append_entries_count_once(24), single_mode(false) { - std::srand(std::time(0)); + std::srand(slash::NowMicros()); // the default check_leader is [3s, 5s) // the default heartbeat time is 1s // we can promise 1s + 2 * rpc < 3s, since rpc time is approximately 10ms - check_leader_us = rand() % 2000000 + check_leader_us; + check_leader_us = std::rand() % 2000000 + check_leader_us; + printf("check_leader_us %lu\n", check_leader_us); split(cluster_string, ',', members); if (members.size() == 1) { single_mode = true; diff --git a/floyd/src/floyd_peer_thread.cc b/floyd/src/floyd_peer_thread.cc index 21f7c65..a038ca9 100644 --- a/floyd/src/floyd_peer_thread.cc +++ b/floyd/src/floyd_peer_thread.cc @@ -19,19 +19,25 @@ #include "floyd/src/floyd.pb.h" #include "floyd/src/logger.h" #include "floyd/src/raft_meta.h" +#include "floyd/src/floyd_apply.h" namespace floyd { -Peer::Peer(std::string server, FloydContext* context, FloydPrimary* primary, RaftLog* raft_log, - ClientPool* pool, const Options& options, Logger* info_log) +Peer::Peer(std::string server, FloydContext* context, FloydPrimary* primary, RaftMeta* raft_meta, + RaftLog* raft_log, ClientPool* pool, FloydApply* apply, const Options& options, Logger* info_log) : server_(server), context_(context), primary_(primary), - options_(options), + raft_meta_(raft_meta), raft_log_(raft_log), pool_(pool), - next_index_(1) { + apply_(apply), + options_(options), + info_log_(info_log), + next_index_(1), + match_index_(0) { next_index_ = raft_log_->GetLastLogIndex() + 1; + match_index_ = raft_meta_->GetLastApplied(); } int Peer::StartThread() { @@ -43,6 +49,13 @@ Peer::~Peer() { LOGV(INFO_LEVEL, info_log_, "Peer(%s) exit!!!", server_.c_str()); } +bool Peer::CheckAndVote(uint64_t vote_term) { + if (context_->current_term != vote_term) { + return false; + } + return (++context_->vote_quorum) > (options_.members.size() / 2); +} + void Peer::AddRequestVoteTask() { bg_thread_.Schedule(&RequestVoteRPCWrapper, this); } @@ -51,13 +64,6 @@ void Peer::RequestVoteRPCWrapper(void *arg) { reinterpret_cast(arg)->RequestVoteRPC(); } -bool Peer::VoteAndCheck(uint64_t vote_term) { - if (context_->current_term != vote_term) { - return false; - } - return (++context_->vote_quorum) > (options_.members.size() / 2); -} - Status Peer::RequestVoteRPC() { uint64_t last_log_term; uint64_t last_log_index; @@ -89,17 +95,19 @@ Status Peer::RequestVoteRPC() { if (context_->role == Role::kCandidate) { // kOk means RequestVote success, opposite vote for me if (res.request_vote_res().vote_granted() == true) { // granted - LOGV(INFO_LEVEL, info_log_, "Peer(%s)::RequestVote granted" - " will Vote and check", server_.c_str()); + LOGV(INFO_LEVEL, info_log_, "Peer(%s)::RequestVote granted will Vote and check", server_.c_str()); // However, we need check whether this vote is vote for old term // we need igore these type of vote - if (VoteAndCheck(res.request_vote_res().term())) { + if (CheckAndVote(res.request_vote_res().term())) { context_->BecomeLeader(); - primary_->AddTask(kHeartBeat); + primary_->AddTask(kHeartBeat, false); } } else { if (res.request_vote_res().term() > context_->current_term) { context_->BecomeFollower(res.request_vote_res().term()); + raft_meta_->SetCurrentTerm(context_->current_term); + raft_meta_->SetVotedForIp(context_->voted_for_ip); + raft_meta_->SetVotedForPort(context_->voted_for_port); } // opposite RequestVote fail, maybe opposite has larger term, or opposite has // longer log. if opposite has larger term, this node will become follower @@ -125,8 +133,14 @@ void Peer::AppendEntriesRPCWrapper(void *arg) { uint64_t Peer::QuorumMatchIndex() { std::vector values; - for (auto& iter : *peers_) { - values.push_back(iter.second->match_index()); + std::map::iterator iter; + LOGV(WARN_LEVEL, info_log_, "Peer::QuorumMatchIndex: Peers size %d", + peers_.size()); + for (iter = peers_.begin(); iter != peers_.end(); iter++) { + if (iter->first == server_) { + continue; + } + values.push_back(iter->second->match_index()); } std::sort(values.begin(), values.end()); return values.at(values.size() / 2); @@ -218,27 +232,31 @@ Status Peer::AppendEntriesRPC() { */ if (res.append_entries_res().term() > context_->current_term) { context_->BecomeFollower(res.append_entries_res().term()); + raft_meta_->SetCurrentTerm(context_->current_term); + raft_meta_->SetVotedForIp(context_->voted_for_ip); + raft_meta_->SetVotedForPort(context_->voted_for_port); } if (res.append_entries_res().success() == true) { - context_->last_op_time = slash::NowMicros(); - - match_index_ = prev_log_index + num_entries; - // only log entries from the leader's current term are committed - // by counting replicas - if (append_entries->entries(num_entries - 1).term() == context_->current_term) { - AdvanceLeaderCommitIndex(); - } - next_index_ = prev_log_index + num_entries + 1; - // If this follower is far behind leader, and there is no more - // AppendEntryTask, we should add one - if (next_index_ + options_.append_entries_count_once < last_log_index) { - int pri_size, qu_size; - bg_thread_.QueueSize(&pri_size, &qu_size); - if (qu_size < 1) { - LOGV(DEBUG_LEVEL, info_log_, "AppendEntry again " - "to catch up next_index(%llu) last_log_index(%llu)", - next_index_.load(), last_log_index); - AddAppendEntriesTask(); + if (num_entries > 0) { + match_index_ = prev_log_index + num_entries; + // only log entries from the leader's current term are committed + // by counting replicas + if (append_entries->entries(num_entries - 1).term() == context_->current_term) { + AdvanceLeaderCommitIndex(); + apply_->ScheduleApply(); + } + next_index_ = prev_log_index + num_entries + 1; + // If this follower is far behind leader, and there is no more + // AppendEntryTask, we should add one + if (next_index_ + options_.append_entries_count_once < last_log_index) { + int pri_size, qu_size; + bg_thread_.QueueSize(&pri_size, &qu_size); + if (qu_size < 1) { + LOGV(DEBUG_LEVEL, info_log_, "AppendEntry again " + "to catch up next_index(%llu) last_log_index(%llu)", + next_index_.load(), last_log_index); + AddAppendEntriesTask(); + } } } } else { diff --git a/floyd/src/floyd_peer_thread.h b/floyd/src/floyd_peer_thread.h index 7cf0785..a61b20e 100644 --- a/floyd/src/floyd_peer_thread.h +++ b/floyd/src/floyd_peer_thread.h @@ -23,12 +23,14 @@ class RaftMeta; class FloydPrimary; class RaftLog; class ClientPool; - +class FloydApply; +class Peer; +typedef std::map PeersSet; class Peer { public: - Peer(std::string server, FloydContext* context, FloydPrimary* primary, RaftLog* raft_log, - ClientPool* pool, const Options& options, Logger* info_log); + Peer(std::string server, FloydContext* context, FloydPrimary* primary, RaftMeta* raft_meta, + RaftLog* raft_log, ClientPool* pool, FloydApply* apply, const Options& options, Logger* info_log); ~Peer(); int StartThread(); @@ -62,28 +64,28 @@ class Peer { uint64_t match_index() { return match_index_; } - typedef std::map PeersSet; - void set_peers(PeersSet* peers) { + void set_peers(PeersSet &peers) { peers_ = peers; } private: - bool VoteAndCheck(uint64_t vote_term); + bool CheckAndVote(uint64_t vote_term); uint64_t QuorumMatchIndex(); void AdvanceLeaderCommitIndex(); - Logger* info_log_; std::string server_; FloydContext* context_; FloydPrimary* primary_; RaftMeta* raft_meta_; RaftLog* raft_log_; ClientPool* pool_; - PeersSet* peers_; - + FloydApply* apply_; Options options_; + Logger* info_log_; + + PeersSet peers_; std::atomic next_index_; std::atomic match_index_; diff --git a/floyd/src/floyd_primary_thread.cc b/floyd/src/floyd_primary_thread.cc index a19b707..945a535 100644 --- a/floyd/src/floyd_primary_thread.cc +++ b/floyd/src/floyd_primary_thread.cc @@ -20,16 +20,16 @@ #include "floyd/src/floyd_apply.h" #include "floyd/src/floyd_context.h" #include "floyd/src/floyd_client_pool.h" -#include "floyd/src/raft_log.h" +#include "floyd/src/raft_meta.h" #include "floyd/src/floyd.pb.h" #include "floyd/src/logger.h" #include "floyd/include/floyd_options.h" namespace floyd { -FloydPrimary::FloydPrimary(FloydContext* context, FloydApply* apply, const Options& options, Logger* info_log) +FloydPrimary::FloydPrimary(FloydContext* context, RaftMeta* raft_meta, const Options& options, Logger* info_log) : context_(context), - apply_(apply), + raft_meta_(raft_meta), options_(options), info_log_(info_log) { } @@ -43,28 +43,30 @@ FloydPrimary::~FloydPrimary() { LOGV(INFO_LEVEL, info_log_, "FloydPrimary exit!!!"); } -void FloydPrimary::set_peers(PeersSet* peers) { - LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::set_peers peers " - "has %d pairs", peers->size()); - peers_ = peers; -} - // TODO(anan) We keep 2 Primary Cron in total. // 1. one short live Cron for LeaderHeartbeat, which is available as a leader; // 2. another long live Cron for ElectLeaderCheck, which is started when // creating Primary; -void FloydPrimary::AddTask(TaskType type, bool is_delay, void* arg) { +void FloydPrimary::AddTask(TaskType type, bool is_delay) { switch (type) { case kHeartBeat: { LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::AddTask HeartBeat"); - uint64_t timeout = options_.heartbeat_us; - bg_thread_.DelaySchedule(timeout / 1000LL, LaunchHeartBeatWrapper, this); + if (is_delay) { + uint64_t timeout = options_.heartbeat_us; + bg_thread_.DelaySchedule(timeout / 1000LL, LaunchHeartBeatWrapper, this); + } else { + bg_thread_.Schedule(LaunchHeartBeatWrapper, this); + } break; } case kCheckLeader: { LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::AddTask CheckLeader"); - uint64_t timeout = options_.check_leader_us; - bg_thread_.DelaySchedule(timeout / 1000LL, LaunchCheckLeaderWrapper, this); + if (is_delay) { + uint64_t timeout = options_.check_leader_us; + bg_thread_.DelaySchedule(timeout / 1000LL, LaunchCheckLeaderWrapper, this); + } else { + bg_thread_.Schedule(LaunchHeartBeatWrapper, this); + } break; } case kNewCommand: { @@ -86,7 +88,7 @@ void FloydPrimary::LaunchHeartBeatWrapper(void *arg) { void FloydPrimary::LaunchHeartBeat() { slash::MutexLock l(&context_->commit_mu); if (context_->role == Role::kLeader) { - NoticePeerTask(kHeartBeat); + NoticePeerTask(kNewCommand); AddTask(kHeartBeat); } } @@ -101,6 +103,9 @@ void FloydPrimary::LaunchCheckLeader() { context_->BecomeLeader(); } else if (context_->last_op_time + options_.check_leader_us < slash::NowMicros()) { context_->BecomeCandidate(); + raft_meta_->SetCurrentTerm(context_->current_term); + raft_meta_->SetVotedForIp(context_->voted_for_ip); + raft_meta_->SetVotedForPort(context_->voted_for_port); NoticePeerTask(kHeartBeat); } } @@ -122,7 +127,7 @@ void FloydPrimary::LaunchNewCommand() { // when adding task to peer thread, we can consider that this job have been in the network // even it is still in the peer thread's queue void FloydPrimary::NoticePeerTask(TaskType type) { - for (auto& peer : *peers_) { + for (auto& peer : peers_) { switch (type) { case kHeartBeat: peer.second->AddRequestVoteTask(); diff --git a/floyd/src/floyd_primary_thread.h b/floyd/src/floyd_primary_thread.h index d0c514a..0dcd2e5 100644 --- a/floyd/src/floyd_primary_thread.h +++ b/floyd/src/floyd_primary_thread.h @@ -17,6 +17,7 @@ #include "pink/include/bg_thread.h" #include "floyd/src/floyd_context.h" +#include "floyd/src/floyd_peer_thread.h" namespace floyd { @@ -26,11 +27,10 @@ class FloydPrimary; class FloydContext; class FloydApply; +class RaftMeta; class Peer; class Options; -typedef std::map PeersSet; - enum TaskType { kHeartBeat = 0, kCheckLeader = 1, @@ -39,20 +39,19 @@ enum TaskType { class FloydPrimary { public: - FloydPrimary(FloydContext* context, FloydApply* apply, const Options& options, Logger* info_log); + FloydPrimary(FloydContext* context, RaftMeta* raft_meta, const Options& options, Logger* info_log); ~FloydPrimary(); int Start(); - - void AddTask(TaskType type, bool is_delay = true, void *arg = NULL); - - void set_peers(PeersSet* peers); - + void AddTask(TaskType type, bool is_delay = true); + void set_peers(PeersSet peers) { + peers_ = peers; + } private: FloydContext* context_; - FloydApply* apply_; - PeersSet* peers_; + RaftMeta* raft_meta_; + PeersSet peers_; Options options_; Logger* info_log_; diff --git a/floyd/src/raft_log.cc b/floyd/src/raft_log.cc index de5fda2..6cf3781 100644 --- a/floyd/src/raft_log.cc +++ b/floyd/src/raft_log.cc @@ -1,11 +1,17 @@ +// Copyright (c) 2015-present, Qihoo, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. + #include "floyd/src/raft_log.h" +#include + #include #include #include "rocksdb/db.h" #include "rocksdb/iterator.h" -#include #include "slash/include/xdebug.h" #include "floyd/src/floyd.pb.h" @@ -39,7 +45,9 @@ RaftLog::RaftLog(rocksdb::DB *db, Logger *info_log) : it->Prev(); it->Prev(); it->Prev(); - last_log_index_ = BitStrToUint(it->key().ToString()); + if (it->Valid()) { + last_log_index_ = BitStrToUint(it->key().ToString()); + } } } diff --git a/floyd/src/raft_log.h b/floyd/src/raft_log.h index 60bc151..65ec42b 100644 --- a/floyd/src/raft_log.h +++ b/floyd/src/raft_log.h @@ -32,15 +32,14 @@ class RaftLog { int TruncateSuffix(uint64_t index); private: + rocksdb::DB* const db_; + Logger* info_log_; /* * mutex for last_log_index_ */ slash::Mutex lli_mutex_; uint64_t last_log_index_; - rocksdb::DB* const db_; - - Logger* info_log_; /* * we don't store last_log_index_ in rocksdb, since if we store it in rocksdb * we need update it every time I append an entry. diff --git a/floyd/src/raft_meta.cc b/floyd/src/raft_meta.cc new file mode 100644 index 0000000..15baec7 --- /dev/null +++ b/floyd/src/raft_meta.cc @@ -0,0 +1,136 @@ +// Copyright (c) 2015-present, Qihoo, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. + +#include "floyd/src/raft_meta.h" + +#include + +#include "rocksdb/status.h" + +#include "floyd/src/logger.h" +#include "floyd/src/floyd.pb.h" +#include "slash/include/env.h" +#include "slash/include/xdebug.h" + +namespace floyd { + +static const std::string kCurrentTerm = "CURRENTTERM"; +static const std::string kVoteForIp = "VOTEFORIP"; +static const std::string kVoteForPort = "VOTEFORPORT"; +static const std::string kCommitIndex = "COMMITINDEX"; +static const std::string kLastApplied = "APPLYINDEX"; + +RaftMeta::RaftMeta(rocksdb::DB* db, Logger* info_log) + : db_(db), + info_log_(info_log) { +} + +RaftMeta::~RaftMeta() { +} + +void RaftMeta::Init() { + if (GetCurrentTerm() == 0) { + SetCurrentTerm(0); + } + if (GetVotedForIp() == "") { + SetVotedForIp(""); + } + if (GetVotedForPort() == 0) { + SetVotedForPort(0); + } + if (GetCommitIndex() == 0) { + SetCommitIndex(0); + } + if (GetLastApplied() == 0) { + SetLastApplied(0); + } +} + +uint64_t RaftMeta::GetCurrentTerm() { + std::string buf; + uint64_t ans; + rocksdb::Status s = db_->Get(rocksdb::ReadOptions(), kCurrentTerm, &buf); + if (s.IsNotFound()) { + return 0; + } + memcpy(&ans, buf.data(), sizeof(uint64_t)); + return ans; +} + +void RaftMeta::SetCurrentTerm(const uint64_t current_term) { + char buf[8]; + memcpy(buf, ¤t_term, sizeof(uint64_t)); + db_->Put(rocksdb::WriteOptions(), kCurrentTerm, std::string(buf, 8)); + return; +} + +std::string RaftMeta::GetVotedForIp() { + std::string buf; + rocksdb::Status s = db_->Get(rocksdb::ReadOptions(), kVoteForIp, &buf); + if (s.IsNotFound()) { + return std::string(""); + } + return buf; +} + +void RaftMeta::SetVotedForIp(const std::string ip) { + char buf[8]; + db_->Put(rocksdb::WriteOptions(), kVoteForIp, ip); + return; +} + +int RaftMeta::GetVotedForPort() { + std::string buf; + int ans; + rocksdb::Status s = db_->Get(rocksdb::ReadOptions(), kVoteForPort, &buf); + if (s.IsNotFound()) { + return 0; + } + memcpy(&ans, buf.data(), sizeof(int)); + return ans; +} + +void RaftMeta::SetVotedForPort(const int port) { + char buf[4]; + memcpy(buf, &port, sizeof(int)); + db_->Put(rocksdb::WriteOptions(), kVoteForPort, std::string(buf, sizeof(int))); + return; +} + +uint64_t RaftMeta::GetCommitIndex() { + std::string buf; + uint64_t ans; + rocksdb::Status s = db_->Get(rocksdb::ReadOptions(), kCommitIndex, &buf); + if (s.IsNotFound()) { + return 0; + } + memcpy(&ans, buf.data(), sizeof(uint64_t)); + return ans; +} + +void RaftMeta::SetCommitIndex(uint64_t commit_index) { + char buf[8]; + memcpy(buf, &commit_index, sizeof(uint64_t)); + db_->Put(rocksdb::WriteOptions(), kCommitIndex, std::string(buf, 8)); +} + +uint64_t RaftMeta::GetLastApplied() { + std::string buf; + uint64_t ans; + rocksdb::Status s = db_->Get(rocksdb::ReadOptions(), kLastApplied, &buf); + if (s.IsNotFound()) { + return 0; + } + memcpy(&ans, buf.data(), sizeof(uint64_t)); + return ans; +} + +void RaftMeta::SetLastApplied(uint64_t last_applied) { + char buf[8]; + memcpy(buf, &last_applied, sizeof(uint64_t)); + db_->Put(rocksdb::WriteOptions(), kLastApplied, std::string(buf, 8)); +} + +} // namespace floyd diff --git a/floyd/src/raft_meta.h b/floyd/src/raft_meta.h new file mode 100644 index 0000000..17c36ea --- /dev/null +++ b/floyd/src/raft_meta.h @@ -0,0 +1,57 @@ +// Copyright (c) 2015-present, Qihoo, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. + +#ifndef FLOYD_SRC_RAFT_META_H_ +#define FLOYD_SRC_RAFT_META_H_ + +#include + +#include "rocksdb/db.h" + +#include "floyd/include/floyd_options.h" +#include "slash/include/slash_status.h" +#include "slash/include/slash_mutex.h" + +namespace floyd { + +using slash::Status; + +class Logger; + +/* + * we use RaftMeta to avoid passing the floyd_impl's this point to other thread + */ +class RaftMeta { + public: + RaftMeta(rocksdb::DB *db, Logger* info_log); + ~RaftMeta(); + + void Init(); + + // return persistent state from zeppelin + uint64_t GetCurrentTerm(); + void SetCurrentTerm(const uint64_t current_term); + + std::string GetVotedForIp(); + int GetVotedForPort(); + void SetVotedForIp(const std::string ip); + void SetVotedForPort(const int port); + + uint64_t GetCommitIndex(); + void SetCommitIndex(const uint64_t commit_index); + + uint64_t GetLastApplied(); + void SetLastApplied(uint64_t last_applied); + + private: + // db used to data that need to be persistent + rocksdb::DB * const db_; + // used to debug + Logger* info_log_; + +}; + +} // namespace floyd +#endif // FLOYD_SRC_RAFT_META_H_ diff --git a/floyd/tools/Makefile b/floyd/tools/Makefile index 72f27d4..1a6fca8 100644 --- a/floyd/tools/Makefile +++ b/floyd/tools/Makefile @@ -53,7 +53,7 @@ read_rock: read_rock.cc $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) cpt: cpt.cc $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) -read_floyd: read_floyd.cc +read_floyd: read_floyd.cc floyd.pb.cc $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) $(OBJS): %.o : %.cc $(CXX) $(CXXFLAGS) -c $< -o $@ $(INCLUDE_PATH) diff --git a/floyd/tools/floyd.pb.cc b/floyd/tools/floyd.pb.cc new file mode 100644 index 0000000..ddee480 --- /dev/null +++ b/floyd/tools/floyd.pb.cc @@ -0,0 +1,5143 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: floyd.proto + +#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION +#include "floyd.pb.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +// @@protoc_insertion_point(includes) + +namespace floyd { + +namespace { + +const ::google::protobuf::Descriptor* Entry_descriptor_ = NULL; +const ::google::protobuf::internal::GeneratedMessageReflection* + Entry_reflection_ = NULL; +const ::google::protobuf::EnumDescriptor* Entry_OpType_descriptor_ = NULL; +const ::google::protobuf::Descriptor* CmdRequest_descriptor_ = NULL; +const ::google::protobuf::internal::GeneratedMessageReflection* + CmdRequest_reflection_ = NULL; +const ::google::protobuf::Descriptor* CmdRequest_Kv_descriptor_ = NULL; +const ::google::protobuf::internal::GeneratedMessageReflection* + CmdRequest_Kv_reflection_ = NULL; +const ::google::protobuf::Descriptor* CmdRequest_User_descriptor_ = NULL; +const ::google::protobuf::internal::GeneratedMessageReflection* + CmdRequest_User_reflection_ = NULL; +const ::google::protobuf::Descriptor* CmdRequest_RequestVote_descriptor_ = NULL; +const ::google::protobuf::internal::GeneratedMessageReflection* + CmdRequest_RequestVote_reflection_ = NULL; +const ::google::protobuf::Descriptor* CmdRequest_AppendEntries_descriptor_ = NULL; +const ::google::protobuf::internal::GeneratedMessageReflection* + CmdRequest_AppendEntries_reflection_ = NULL; +const ::google::protobuf::Descriptor* CmdRequest_ServerStatus_descriptor_ = NULL; +const ::google::protobuf::internal::GeneratedMessageReflection* + CmdRequest_ServerStatus_reflection_ = NULL; +const ::google::protobuf::Descriptor* CmdResponse_descriptor_ = NULL; +const ::google::protobuf::internal::GeneratedMessageReflection* + CmdResponse_reflection_ = NULL; +const ::google::protobuf::Descriptor* CmdResponse_Kv_descriptor_ = NULL; +const ::google::protobuf::internal::GeneratedMessageReflection* + CmdResponse_Kv_reflection_ = NULL; +const ::google::protobuf::Descriptor* CmdResponse_Kvs_descriptor_ = NULL; +const ::google::protobuf::internal::GeneratedMessageReflection* + CmdResponse_Kvs_reflection_ = NULL; +const ::google::protobuf::Descriptor* CmdResponse_RequestVoteResponse_descriptor_ = NULL; +const ::google::protobuf::internal::GeneratedMessageReflection* + CmdResponse_RequestVoteResponse_reflection_ = NULL; +const ::google::protobuf::Descriptor* CmdResponse_AppendEntriesResponse_descriptor_ = NULL; +const ::google::protobuf::internal::GeneratedMessageReflection* + CmdResponse_AppendEntriesResponse_reflection_ = NULL; +const ::google::protobuf::Descriptor* CmdResponse_ServerStatus_descriptor_ = NULL; +const ::google::protobuf::internal::GeneratedMessageReflection* + CmdResponse_ServerStatus_reflection_ = NULL; +const ::google::protobuf::EnumDescriptor* Type_descriptor_ = NULL; +const ::google::protobuf::EnumDescriptor* StatusCode_descriptor_ = NULL; + +} // namespace + + +void protobuf_AssignDesc_floyd_2eproto() { + protobuf_AddDesc_floyd_2eproto(); + const ::google::protobuf::FileDescriptor* file = + ::google::protobuf::DescriptorPool::generated_pool()->FindFileByName( + "floyd.proto"); + GOOGLE_CHECK(file != NULL); + Entry_descriptor_ = file->message_type(0); + static const int Entry_offsets_[4] = { + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Entry, term_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Entry, key_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Entry, value_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Entry, optype_), + }; + Entry_reflection_ = + new ::google::protobuf::internal::GeneratedMessageReflection( + Entry_descriptor_, + Entry::default_instance_, + Entry_offsets_, + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Entry, _has_bits_[0]), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Entry, _unknown_fields_), + -1, + ::google::protobuf::DescriptorPool::generated_pool(), + ::google::protobuf::MessageFactory::generated_factory(), + sizeof(Entry)); + Entry_OpType_descriptor_ = Entry_descriptor_->enum_type(0); + CmdRequest_descriptor_ = file->message_type(1); + static const int CmdRequest_offsets_[6] = { + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest, type_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest, kv_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest, user_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest, request_vote_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest, append_entries_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest, server_status_), + }; + CmdRequest_reflection_ = + new ::google::protobuf::internal::GeneratedMessageReflection( + CmdRequest_descriptor_, + CmdRequest::default_instance_, + CmdRequest_offsets_, + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest, _has_bits_[0]), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest, _unknown_fields_), + -1, + ::google::protobuf::DescriptorPool::generated_pool(), + ::google::protobuf::MessageFactory::generated_factory(), + sizeof(CmdRequest)); + CmdRequest_Kv_descriptor_ = CmdRequest_descriptor_->nested_type(0); + static const int CmdRequest_Kv_offsets_[2] = { + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_Kv, key_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_Kv, value_), + }; + CmdRequest_Kv_reflection_ = + new ::google::protobuf::internal::GeneratedMessageReflection( + CmdRequest_Kv_descriptor_, + CmdRequest_Kv::default_instance_, + CmdRequest_Kv_offsets_, + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_Kv, _has_bits_[0]), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_Kv, _unknown_fields_), + -1, + ::google::protobuf::DescriptorPool::generated_pool(), + ::google::protobuf::MessageFactory::generated_factory(), + sizeof(CmdRequest_Kv)); + CmdRequest_User_descriptor_ = CmdRequest_descriptor_->nested_type(1); + static const int CmdRequest_User_offsets_[2] = { + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_User, ip_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_User, port_), + }; + CmdRequest_User_reflection_ = + new ::google::protobuf::internal::GeneratedMessageReflection( + CmdRequest_User_descriptor_, + CmdRequest_User::default_instance_, + CmdRequest_User_offsets_, + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_User, _has_bits_[0]), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_User, _unknown_fields_), + -1, + ::google::protobuf::DescriptorPool::generated_pool(), + ::google::protobuf::MessageFactory::generated_factory(), + sizeof(CmdRequest_User)); + CmdRequest_RequestVote_descriptor_ = CmdRequest_descriptor_->nested_type(2); + static const int CmdRequest_RequestVote_offsets_[5] = { + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, term_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, ip_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, port_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, last_log_index_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, last_log_term_), + }; + CmdRequest_RequestVote_reflection_ = + new ::google::protobuf::internal::GeneratedMessageReflection( + CmdRequest_RequestVote_descriptor_, + CmdRequest_RequestVote::default_instance_, + CmdRequest_RequestVote_offsets_, + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, _has_bits_[0]), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_RequestVote, _unknown_fields_), + -1, + ::google::protobuf::DescriptorPool::generated_pool(), + ::google::protobuf::MessageFactory::generated_factory(), + sizeof(CmdRequest_RequestVote)); + CmdRequest_AppendEntries_descriptor_ = CmdRequest_descriptor_->nested_type(3); + static const int CmdRequest_AppendEntries_offsets_[7] = { + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, ip_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, port_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, term_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, prev_log_index_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, prev_log_term_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, leader_commit_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, entries_), + }; + CmdRequest_AppendEntries_reflection_ = + new ::google::protobuf::internal::GeneratedMessageReflection( + CmdRequest_AppendEntries_descriptor_, + CmdRequest_AppendEntries::default_instance_, + CmdRequest_AppendEntries_offsets_, + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, _has_bits_[0]), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_AppendEntries, _unknown_fields_), + -1, + ::google::protobuf::DescriptorPool::generated_pool(), + ::google::protobuf::MessageFactory::generated_factory(), + sizeof(CmdRequest_AppendEntries)); + CmdRequest_ServerStatus_descriptor_ = CmdRequest_descriptor_->nested_type(4); + static const int CmdRequest_ServerStatus_offsets_[4] = { + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_ServerStatus, term_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_ServerStatus, commit_index_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_ServerStatus, ip_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_ServerStatus, port_), + }; + CmdRequest_ServerStatus_reflection_ = + new ::google::protobuf::internal::GeneratedMessageReflection( + CmdRequest_ServerStatus_descriptor_, + CmdRequest_ServerStatus::default_instance_, + CmdRequest_ServerStatus_offsets_, + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_ServerStatus, _has_bits_[0]), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdRequest_ServerStatus, _unknown_fields_), + -1, + ::google::protobuf::DescriptorPool::generated_pool(), + ::google::protobuf::MessageFactory::generated_factory(), + sizeof(CmdRequest_ServerStatus)); + CmdResponse_descriptor_ = file->message_type(2); + static const int CmdResponse_offsets_[8] = { + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse, type_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse, code_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse, msg_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse, kv_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse, kvs_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse, request_vote_res_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse, append_entries_res_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse, server_status_), + }; + CmdResponse_reflection_ = + new ::google::protobuf::internal::GeneratedMessageReflection( + CmdResponse_descriptor_, + CmdResponse::default_instance_, + CmdResponse_offsets_, + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse, _has_bits_[0]), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse, _unknown_fields_), + -1, + ::google::protobuf::DescriptorPool::generated_pool(), + ::google::protobuf::MessageFactory::generated_factory(), + sizeof(CmdResponse)); + CmdResponse_Kv_descriptor_ = CmdResponse_descriptor_->nested_type(0); + static const int CmdResponse_Kv_offsets_[1] = { + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_Kv, value_), + }; + CmdResponse_Kv_reflection_ = + new ::google::protobuf::internal::GeneratedMessageReflection( + CmdResponse_Kv_descriptor_, + CmdResponse_Kv::default_instance_, + CmdResponse_Kv_offsets_, + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_Kv, _has_bits_[0]), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_Kv, _unknown_fields_), + -1, + ::google::protobuf::DescriptorPool::generated_pool(), + ::google::protobuf::MessageFactory::generated_factory(), + sizeof(CmdResponse_Kv)); + CmdResponse_Kvs_descriptor_ = CmdResponse_descriptor_->nested_type(1); + static const int CmdResponse_Kvs_offsets_[1] = { + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_Kvs, kv_), + }; + CmdResponse_Kvs_reflection_ = + new ::google::protobuf::internal::GeneratedMessageReflection( + CmdResponse_Kvs_descriptor_, + CmdResponse_Kvs::default_instance_, + CmdResponse_Kvs_offsets_, + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_Kvs, _has_bits_[0]), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_Kvs, _unknown_fields_), + -1, + ::google::protobuf::DescriptorPool::generated_pool(), + ::google::protobuf::MessageFactory::generated_factory(), + sizeof(CmdResponse_Kvs)); + CmdResponse_RequestVoteResponse_descriptor_ = CmdResponse_descriptor_->nested_type(2); + static const int CmdResponse_RequestVoteResponse_offsets_[2] = { + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_RequestVoteResponse, term_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_RequestVoteResponse, vote_granted_), + }; + CmdResponse_RequestVoteResponse_reflection_ = + new ::google::protobuf::internal::GeneratedMessageReflection( + CmdResponse_RequestVoteResponse_descriptor_, + CmdResponse_RequestVoteResponse::default_instance_, + CmdResponse_RequestVoteResponse_offsets_, + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_RequestVoteResponse, _has_bits_[0]), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_RequestVoteResponse, _unknown_fields_), + -1, + ::google::protobuf::DescriptorPool::generated_pool(), + ::google::protobuf::MessageFactory::generated_factory(), + sizeof(CmdResponse_RequestVoteResponse)); + CmdResponse_AppendEntriesResponse_descriptor_ = CmdResponse_descriptor_->nested_type(3); + static const int CmdResponse_AppendEntriesResponse_offsets_[3] = { + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_AppendEntriesResponse, term_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_AppendEntriesResponse, success_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_AppendEntriesResponse, last_log_index_), + }; + CmdResponse_AppendEntriesResponse_reflection_ = + new ::google::protobuf::internal::GeneratedMessageReflection( + CmdResponse_AppendEntriesResponse_descriptor_, + CmdResponse_AppendEntriesResponse::default_instance_, + CmdResponse_AppendEntriesResponse_offsets_, + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_AppendEntriesResponse, _has_bits_[0]), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_AppendEntriesResponse, _unknown_fields_), + -1, + ::google::protobuf::DescriptorPool::generated_pool(), + ::google::protobuf::MessageFactory::generated_factory(), + sizeof(CmdResponse_AppendEntriesResponse)); + CmdResponse_ServerStatus_descriptor_ = CmdResponse_descriptor_->nested_type(4); + static const int CmdResponse_ServerStatus_offsets_[10] = { + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_ServerStatus, term_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_ServerStatus, commit_index_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_ServerStatus, role_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_ServerStatus, leader_ip_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_ServerStatus, leader_port_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_ServerStatus, voted_for_ip_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_ServerStatus, voted_for_port_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_ServerStatus, last_log_term_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_ServerStatus, last_log_index_), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_ServerStatus, last_applied_), + }; + CmdResponse_ServerStatus_reflection_ = + new ::google::protobuf::internal::GeneratedMessageReflection( + CmdResponse_ServerStatus_descriptor_, + CmdResponse_ServerStatus::default_instance_, + CmdResponse_ServerStatus_offsets_, + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_ServerStatus, _has_bits_[0]), + GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CmdResponse_ServerStatus, _unknown_fields_), + -1, + ::google::protobuf::DescriptorPool::generated_pool(), + ::google::protobuf::MessageFactory::generated_factory(), + sizeof(CmdResponse_ServerStatus)); + Type_descriptor_ = file->enum_type(0); + StatusCode_descriptor_ = file->enum_type(1); +} + +namespace { + +GOOGLE_PROTOBUF_DECLARE_ONCE(protobuf_AssignDescriptors_once_); +inline void protobuf_AssignDescriptorsOnce() { + ::google::protobuf::GoogleOnceInit(&protobuf_AssignDescriptors_once_, + &protobuf_AssignDesc_floyd_2eproto); +} + +void protobuf_RegisterTypes(const ::std::string&) { + protobuf_AssignDescriptorsOnce(); + ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( + Entry_descriptor_, &Entry::default_instance()); + ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( + CmdRequest_descriptor_, &CmdRequest::default_instance()); + ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( + CmdRequest_Kv_descriptor_, &CmdRequest_Kv::default_instance()); + ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( + CmdRequest_User_descriptor_, &CmdRequest_User::default_instance()); + ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( + CmdRequest_RequestVote_descriptor_, &CmdRequest_RequestVote::default_instance()); + ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( + CmdRequest_AppendEntries_descriptor_, &CmdRequest_AppendEntries::default_instance()); + ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( + CmdRequest_ServerStatus_descriptor_, &CmdRequest_ServerStatus::default_instance()); + ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( + CmdResponse_descriptor_, &CmdResponse::default_instance()); + ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( + CmdResponse_Kv_descriptor_, &CmdResponse_Kv::default_instance()); + ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( + CmdResponse_Kvs_descriptor_, &CmdResponse_Kvs::default_instance()); + ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( + CmdResponse_RequestVoteResponse_descriptor_, &CmdResponse_RequestVoteResponse::default_instance()); + ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( + CmdResponse_AppendEntriesResponse_descriptor_, &CmdResponse_AppendEntriesResponse::default_instance()); + ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( + CmdResponse_ServerStatus_descriptor_, &CmdResponse_ServerStatus::default_instance()); +} + +} // namespace + +void protobuf_ShutdownFile_floyd_2eproto() { + delete Entry::default_instance_; + delete Entry_reflection_; + delete CmdRequest::default_instance_; + delete CmdRequest_reflection_; + delete CmdRequest_Kv::default_instance_; + delete CmdRequest_Kv_reflection_; + delete CmdRequest_User::default_instance_; + delete CmdRequest_User_reflection_; + delete CmdRequest_RequestVote::default_instance_; + delete CmdRequest_RequestVote_reflection_; + delete CmdRequest_AppendEntries::default_instance_; + delete CmdRequest_AppendEntries_reflection_; + delete CmdRequest_ServerStatus::default_instance_; + delete CmdRequest_ServerStatus_reflection_; + delete CmdResponse::default_instance_; + delete CmdResponse_reflection_; + delete CmdResponse_Kv::default_instance_; + delete CmdResponse_Kv_reflection_; + delete CmdResponse_Kvs::default_instance_; + delete CmdResponse_Kvs_reflection_; + delete CmdResponse_RequestVoteResponse::default_instance_; + delete CmdResponse_RequestVoteResponse_reflection_; + delete CmdResponse_AppendEntriesResponse::default_instance_; + delete CmdResponse_AppendEntriesResponse_reflection_; + delete CmdResponse_ServerStatus::default_instance_; + delete CmdResponse_ServerStatus_reflection_; +} + +void protobuf_AddDesc_floyd_2eproto() { + static bool already_here = false; + if (already_here) return; + already_here = true; + GOOGLE_PROTOBUF_VERIFY_VERSION; + + ::google::protobuf::DescriptorPool::InternalAddGeneratedFile( + "\n\013floyd.proto\022\005floyd\"\204\001\n\005Entry\022\014\n\004term\030\001" + " \002(\004\022\013\n\003key\030\002 \002(\t\022\r\n\005value\030\003 \001(\014\022#\n\006opty" + "pe\030\004 \002(\0162\023.floyd.Entry.OpType\",\n\006OpType\022" + "\t\n\005kRead\020\000\022\n\n\006kWrite\020\001\022\013\n\007kDelete\020\002\"\253\005\n\n" + "CmdRequest\022\031\n\004type\030\001 \002(\0162\013.floyd.Type\022 \n" + "\002kv\030\002 \001(\0132\024.floyd.CmdRequest.Kv\022$\n\004user\030" + "\003 \001(\0132\026.floyd.CmdRequest.User\0223\n\014request" + "_vote\030\004 \001(\0132\035.floyd.CmdRequest.RequestVo" + "te\0227\n\016append_entries\030\005 \001(\0132\037.floyd.CmdRe" + "quest.AppendEntries\0225\n\rserver_status\030\006 \001" + "(\0132\036.floyd.CmdRequest.ServerStatus\032 \n\002Kv" + "\022\013\n\003key\030\001 \002(\014\022\r\n\005value\030\002 \001(\014\032 \n\004User\022\n\n\002" + "ip\030\001 \002(\014\022\014\n\004port\030\002 \002(\005\032d\n\013RequestVote\022\014\n" + "\004term\030\001 \002(\004\022\n\n\002ip\030\002 \002(\014\022\014\n\004port\030\003 \002(\005\022\026\n" + "\016last_log_index\030\004 \002(\004\022\025\n\rlast_log_term\030\005" + " \002(\004\032\234\001\n\rAppendEntries\022\n\n\002ip\030\001 \002(\014\022\014\n\004po" + "rt\030\002 \002(\005\022\014\n\004term\030\003 \002(\004\022\026\n\016prev_log_index" + "\030\004 \002(\004\022\025\n\rprev_log_term\030\005 \002(\004\022\025\n\rleader_" + "commit\030\006 \002(\004\022\035\n\007entries\030\007 \003(\0132\014.floyd.En" + "try\032L\n\014ServerStatus\022\014\n\004term\030\001 \002(\003\022\024\n\014com" + "mit_index\030\002 \002(\003\022\n\n\002ip\030\003 \001(\014\022\014\n\004port\030\004 \001(" + "\005\"\206\006\n\013CmdResponse\022\031\n\004type\030\001 \002(\0162\013.floyd." + "Type\022\037\n\004code\030\002 \001(\0162\021.floyd.StatusCode\022\013\n" + "\003msg\030\003 \001(\014\022!\n\002kv\030\004 \001(\0132\025.floyd.CmdRespon" + "se.Kv\022#\n\003kvs\030\005 \001(\0132\026.floyd.CmdResponse.K" + "vs\022@\n\020request_vote_res\030\006 \001(\0132&.floyd.Cmd" + "Response.RequestVoteResponse\022D\n\022append_e" + "ntries_res\030\007 \001(\0132(.floyd.CmdResponse.App" + "endEntriesResponse\0226\n\rserver_status\030\010 \001(" + "\0132\037.floyd.CmdResponse.ServerStatus\032\023\n\002Kv" + "\022\r\n\005value\030\001 \001(\014\032(\n\003Kvs\022!\n\002kv\030\001 \003(\0132\025.flo" + "yd.CmdResponse.Kv\0329\n\023RequestVoteResponse" + "\022\014\n\004term\030\001 \002(\004\022\024\n\014vote_granted\030\002 \002(\010\032N\n\025" + "AppendEntriesResponse\022\014\n\004term\030\001 \002(\004\022\017\n\007s" + "uccess\030\002 \002(\010\022\026\n\016last_log_index\030\003 \001(\004\032\333\001\n" + "\014ServerStatus\022\014\n\004term\030\001 \002(\004\022\024\n\014commit_in" + "dex\030\002 \002(\004\022\014\n\004role\030\003 \002(\014\022\021\n\tleader_ip\030\004 \001" + "(\014\022\023\n\013leader_port\030\005 \001(\005\022\024\n\014voted_for_ip\030" + "\006 \001(\014\022\026\n\016voted_for_port\030\007 \001(\005\022\025\n\rlast_lo" + "g_term\030\010 \001(\004\022\026\n\016last_log_index\030\t \001(\004\022\024\n\014" + "last_applied\030\n \001(\004*t\n\004Type\022\t\n\005kRead\020\000\022\n\n" + "\006kWrite\020\001\022\017\n\013kDirtyWrite\020\002\022\013\n\007kDelete\020\003\022" + "\020\n\014kRequestVote\020\010\022\022\n\016kAppendEntries\020\t\022\021\n" + "\rkServerStatus\020\n*0\n\nStatusCode\022\007\n\003kOk\020\000\022" + "\r\n\tkNotFound\020\001\022\n\n\006kError\020\002", 1786); + ::google::protobuf::MessageFactory::InternalRegisterGeneratedFile( + "floyd.proto", &protobuf_RegisterTypes); + Entry::default_instance_ = new Entry(); + CmdRequest::default_instance_ = new CmdRequest(); + CmdRequest_Kv::default_instance_ = new CmdRequest_Kv(); + CmdRequest_User::default_instance_ = new CmdRequest_User(); + CmdRequest_RequestVote::default_instance_ = new CmdRequest_RequestVote(); + CmdRequest_AppendEntries::default_instance_ = new CmdRequest_AppendEntries(); + CmdRequest_ServerStatus::default_instance_ = new CmdRequest_ServerStatus(); + CmdResponse::default_instance_ = new CmdResponse(); + CmdResponse_Kv::default_instance_ = new CmdResponse_Kv(); + CmdResponse_Kvs::default_instance_ = new CmdResponse_Kvs(); + CmdResponse_RequestVoteResponse::default_instance_ = new CmdResponse_RequestVoteResponse(); + CmdResponse_AppendEntriesResponse::default_instance_ = new CmdResponse_AppendEntriesResponse(); + CmdResponse_ServerStatus::default_instance_ = new CmdResponse_ServerStatus(); + Entry::default_instance_->InitAsDefaultInstance(); + CmdRequest::default_instance_->InitAsDefaultInstance(); + CmdRequest_Kv::default_instance_->InitAsDefaultInstance(); + CmdRequest_User::default_instance_->InitAsDefaultInstance(); + CmdRequest_RequestVote::default_instance_->InitAsDefaultInstance(); + CmdRequest_AppendEntries::default_instance_->InitAsDefaultInstance(); + CmdRequest_ServerStatus::default_instance_->InitAsDefaultInstance(); + CmdResponse::default_instance_->InitAsDefaultInstance(); + CmdResponse_Kv::default_instance_->InitAsDefaultInstance(); + CmdResponse_Kvs::default_instance_->InitAsDefaultInstance(); + CmdResponse_RequestVoteResponse::default_instance_->InitAsDefaultInstance(); + CmdResponse_AppendEntriesResponse::default_instance_->InitAsDefaultInstance(); + CmdResponse_ServerStatus::default_instance_->InitAsDefaultInstance(); + ::google::protobuf::internal::OnShutdown(&protobuf_ShutdownFile_floyd_2eproto); +} + +// Force AddDescriptors() to be called at static initialization time. +struct StaticDescriptorInitializer_floyd_2eproto { + StaticDescriptorInitializer_floyd_2eproto() { + protobuf_AddDesc_floyd_2eproto(); + } +} static_descriptor_initializer_floyd_2eproto_; +const ::google::protobuf::EnumDescriptor* Type_descriptor() { + protobuf_AssignDescriptorsOnce(); + return Type_descriptor_; +} +bool Type_IsValid(int value) { + switch(value) { + case 0: + case 1: + case 2: + case 3: + case 8: + case 9: + case 10: + return true; + default: + return false; + } +} + +const ::google::protobuf::EnumDescriptor* StatusCode_descriptor() { + protobuf_AssignDescriptorsOnce(); + return StatusCode_descriptor_; +} +bool StatusCode_IsValid(int value) { + switch(value) { + case 0: + case 1: + case 2: + return true; + default: + return false; + } +} + + +// =================================================================== + +const ::google::protobuf::EnumDescriptor* Entry_OpType_descriptor() { + protobuf_AssignDescriptorsOnce(); + return Entry_OpType_descriptor_; +} +bool Entry_OpType_IsValid(int value) { + switch(value) { + case 0: + case 1: + case 2: + return true; + default: + return false; + } +} + +#ifndef _MSC_VER +const Entry_OpType Entry::kRead; +const Entry_OpType Entry::kWrite; +const Entry_OpType Entry::kDelete; +const Entry_OpType Entry::OpType_MIN; +const Entry_OpType Entry::OpType_MAX; +const int Entry::OpType_ARRAYSIZE; +#endif // _MSC_VER +#ifndef _MSC_VER +const int Entry::kTermFieldNumber; +const int Entry::kKeyFieldNumber; +const int Entry::kValueFieldNumber; +const int Entry::kOptypeFieldNumber; +#endif // !_MSC_VER + +Entry::Entry() + : ::google::protobuf::Message() { + SharedCtor(); +} + +void Entry::InitAsDefaultInstance() { +} + +Entry::Entry(const Entry& from) + : ::google::protobuf::Message() { + SharedCtor(); + MergeFrom(from); +} + +void Entry::SharedCtor() { + _cached_size_ = 0; + term_ = GOOGLE_ULONGLONG(0); + key_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + value_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + optype_ = 0; + ::memset(_has_bits_, 0, sizeof(_has_bits_)); +} + +Entry::~Entry() { + SharedDtor(); +} + +void Entry::SharedDtor() { + if (key_ != &::google::protobuf::internal::kEmptyString) { + delete key_; + } + if (value_ != &::google::protobuf::internal::kEmptyString) { + delete value_; + } + if (this != default_instance_) { + } +} + +void Entry::SetCachedSize(int size) const { + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); +} +const ::google::protobuf::Descriptor* Entry::descriptor() { + protobuf_AssignDescriptorsOnce(); + return Entry_descriptor_; +} + +const Entry& Entry::default_instance() { + if (default_instance_ == NULL) protobuf_AddDesc_floyd_2eproto(); + return *default_instance_; +} + +Entry* Entry::default_instance_ = NULL; + +Entry* Entry::New() const { + return new Entry; +} + +void Entry::Clear() { + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + term_ = GOOGLE_ULONGLONG(0); + if (has_key()) { + if (key_ != &::google::protobuf::internal::kEmptyString) { + key_->clear(); + } + } + if (has_value()) { + if (value_ != &::google::protobuf::internal::kEmptyString) { + value_->clear(); + } + } + optype_ = 0; + } + ::memset(_has_bits_, 0, sizeof(_has_bits_)); + mutable_unknown_fields()->Clear(); +} + +bool Entry::MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!(EXPRESSION)) return false + ::google::protobuf::uint32 tag; + while ((tag = input->ReadTag()) != 0) { + switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + // required uint64 term = 1; + case 1: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &term_))); + set_has_term(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(18)) goto parse_key; + break; + } + + // required string key = 2; + case 2: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_key: + DO_(::google::protobuf::internal::WireFormatLite::ReadString( + input, this->mutable_key())); + ::google::protobuf::internal::WireFormat::VerifyUTF8String( + this->key().data(), this->key().length(), + ::google::protobuf::internal::WireFormat::PARSE); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(26)) goto parse_value; + break; + } + + // optional bytes value = 3; + case 3: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_value: + DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( + input, this->mutable_value())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(32)) goto parse_optype; + break; + } + + // required .floyd.Entry.OpType optype = 4; + case 4: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_optype: + int value; + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( + input, &value))); + if (::floyd::Entry_OpType_IsValid(value)) { + set_optype(static_cast< ::floyd::Entry_OpType >(value)); + } else { + mutable_unknown_fields()->AddVarint(4, value); + } + } else { + goto handle_uninterpreted; + } + if (input->ExpectAtEnd()) return true; + break; + } + + default: { + handle_uninterpreted: + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { + return true; + } + DO_(::google::protobuf::internal::WireFormat::SkipField( + input, tag, mutable_unknown_fields())); + break; + } + } + } + return true; +#undef DO_ +} + +void Entry::SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const { + // required uint64 term = 1; + if (has_term()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(1, this->term(), output); + } + + // required string key = 2; + if (has_key()) { + ::google::protobuf::internal::WireFormat::VerifyUTF8String( + this->key().data(), this->key().length(), + ::google::protobuf::internal::WireFormat::SERIALIZE); + ::google::protobuf::internal::WireFormatLite::WriteString( + 2, this->key(), output); + } + + // optional bytes value = 3; + if (has_value()) { + ::google::protobuf::internal::WireFormatLite::WriteBytes( + 3, this->value(), output); + } + + // required .floyd.Entry.OpType optype = 4; + if (has_optype()) { + ::google::protobuf::internal::WireFormatLite::WriteEnum( + 4, this->optype(), output); + } + + if (!unknown_fields().empty()) { + ::google::protobuf::internal::WireFormat::SerializeUnknownFields( + unknown_fields(), output); + } +} + +::google::protobuf::uint8* Entry::SerializeWithCachedSizesToArray( + ::google::protobuf::uint8* target) const { + // required uint64 term = 1; + if (has_term()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(1, this->term(), target); + } + + // required string key = 2; + if (has_key()) { + ::google::protobuf::internal::WireFormat::VerifyUTF8String( + this->key().data(), this->key().length(), + ::google::protobuf::internal::WireFormat::SERIALIZE); + target = + ::google::protobuf::internal::WireFormatLite::WriteStringToArray( + 2, this->key(), target); + } + + // optional bytes value = 3; + if (has_value()) { + target = + ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( + 3, this->value(), target); + } + + // required .floyd.Entry.OpType optype = 4; + if (has_optype()) { + target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray( + 4, this->optype(), target); + } + + if (!unknown_fields().empty()) { + target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( + unknown_fields(), target); + } + return target; +} + +int Entry::ByteSize() const { + int total_size = 0; + + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + // required uint64 term = 1; + if (has_term()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->term()); + } + + // required string key = 2; + if (has_key()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::StringSize( + this->key()); + } + + // optional bytes value = 3; + if (has_value()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::BytesSize( + this->value()); + } + + // required .floyd.Entry.OpType optype = 4; + if (has_optype()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::EnumSize(this->optype()); + } + + } + if (!unknown_fields().empty()) { + total_size += + ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( + unknown_fields()); + } + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = total_size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); + return total_size; +} + +void Entry::MergeFrom(const ::google::protobuf::Message& from) { + GOOGLE_CHECK_NE(&from, this); + const Entry* source = + ::google::protobuf::internal::dynamic_cast_if_available( + &from); + if (source == NULL) { + ::google::protobuf::internal::ReflectionOps::Merge(from, this); + } else { + MergeFrom(*source); + } +} + +void Entry::MergeFrom(const Entry& from) { + GOOGLE_CHECK_NE(&from, this); + if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (from.has_term()) { + set_term(from.term()); + } + if (from.has_key()) { + set_key(from.key()); + } + if (from.has_value()) { + set_value(from.value()); + } + if (from.has_optype()) { + set_optype(from.optype()); + } + } + mutable_unknown_fields()->MergeFrom(from.unknown_fields()); +} + +void Entry::CopyFrom(const ::google::protobuf::Message& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +void Entry::CopyFrom(const Entry& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +bool Entry::IsInitialized() const { + if ((_has_bits_[0] & 0x0000000b) != 0x0000000b) return false; + + return true; +} + +void Entry::Swap(Entry* other) { + if (other != this) { + std::swap(term_, other->term_); + std::swap(key_, other->key_); + std::swap(value_, other->value_); + std::swap(optype_, other->optype_); + std::swap(_has_bits_[0], other->_has_bits_[0]); + _unknown_fields_.Swap(&other->_unknown_fields_); + std::swap(_cached_size_, other->_cached_size_); + } +} + +::google::protobuf::Metadata Entry::GetMetadata() const { + protobuf_AssignDescriptorsOnce(); + ::google::protobuf::Metadata metadata; + metadata.descriptor = Entry_descriptor_; + metadata.reflection = Entry_reflection_; + return metadata; +} + + +// =================================================================== + +#ifndef _MSC_VER +const int CmdRequest_Kv::kKeyFieldNumber; +const int CmdRequest_Kv::kValueFieldNumber; +#endif // !_MSC_VER + +CmdRequest_Kv::CmdRequest_Kv() + : ::google::protobuf::Message() { + SharedCtor(); +} + +void CmdRequest_Kv::InitAsDefaultInstance() { +} + +CmdRequest_Kv::CmdRequest_Kv(const CmdRequest_Kv& from) + : ::google::protobuf::Message() { + SharedCtor(); + MergeFrom(from); +} + +void CmdRequest_Kv::SharedCtor() { + _cached_size_ = 0; + key_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + value_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + ::memset(_has_bits_, 0, sizeof(_has_bits_)); +} + +CmdRequest_Kv::~CmdRequest_Kv() { + SharedDtor(); +} + +void CmdRequest_Kv::SharedDtor() { + if (key_ != &::google::protobuf::internal::kEmptyString) { + delete key_; + } + if (value_ != &::google::protobuf::internal::kEmptyString) { + delete value_; + } + if (this != default_instance_) { + } +} + +void CmdRequest_Kv::SetCachedSize(int size) const { + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); +} +const ::google::protobuf::Descriptor* CmdRequest_Kv::descriptor() { + protobuf_AssignDescriptorsOnce(); + return CmdRequest_Kv_descriptor_; +} + +const CmdRequest_Kv& CmdRequest_Kv::default_instance() { + if (default_instance_ == NULL) protobuf_AddDesc_floyd_2eproto(); + return *default_instance_; +} + +CmdRequest_Kv* CmdRequest_Kv::default_instance_ = NULL; + +CmdRequest_Kv* CmdRequest_Kv::New() const { + return new CmdRequest_Kv; +} + +void CmdRequest_Kv::Clear() { + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (has_key()) { + if (key_ != &::google::protobuf::internal::kEmptyString) { + key_->clear(); + } + } + if (has_value()) { + if (value_ != &::google::protobuf::internal::kEmptyString) { + value_->clear(); + } + } + } + ::memset(_has_bits_, 0, sizeof(_has_bits_)); + mutable_unknown_fields()->Clear(); +} + +bool CmdRequest_Kv::MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!(EXPRESSION)) return false + ::google::protobuf::uint32 tag; + while ((tag = input->ReadTag()) != 0) { + switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + // required bytes key = 1; + case 1: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( + input, this->mutable_key())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(18)) goto parse_value; + break; + } + + // optional bytes value = 2; + case 2: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_value: + DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( + input, this->mutable_value())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectAtEnd()) return true; + break; + } + + default: { + handle_uninterpreted: + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { + return true; + } + DO_(::google::protobuf::internal::WireFormat::SkipField( + input, tag, mutable_unknown_fields())); + break; + } + } + } + return true; +#undef DO_ +} + +void CmdRequest_Kv::SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const { + // required bytes key = 1; + if (has_key()) { + ::google::protobuf::internal::WireFormatLite::WriteBytes( + 1, this->key(), output); + } + + // optional bytes value = 2; + if (has_value()) { + ::google::protobuf::internal::WireFormatLite::WriteBytes( + 2, this->value(), output); + } + + if (!unknown_fields().empty()) { + ::google::protobuf::internal::WireFormat::SerializeUnknownFields( + unknown_fields(), output); + } +} + +::google::protobuf::uint8* CmdRequest_Kv::SerializeWithCachedSizesToArray( + ::google::protobuf::uint8* target) const { + // required bytes key = 1; + if (has_key()) { + target = + ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( + 1, this->key(), target); + } + + // optional bytes value = 2; + if (has_value()) { + target = + ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( + 2, this->value(), target); + } + + if (!unknown_fields().empty()) { + target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( + unknown_fields(), target); + } + return target; +} + +int CmdRequest_Kv::ByteSize() const { + int total_size = 0; + + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + // required bytes key = 1; + if (has_key()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::BytesSize( + this->key()); + } + + // optional bytes value = 2; + if (has_value()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::BytesSize( + this->value()); + } + + } + if (!unknown_fields().empty()) { + total_size += + ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( + unknown_fields()); + } + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = total_size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); + return total_size; +} + +void CmdRequest_Kv::MergeFrom(const ::google::protobuf::Message& from) { + GOOGLE_CHECK_NE(&from, this); + const CmdRequest_Kv* source = + ::google::protobuf::internal::dynamic_cast_if_available( + &from); + if (source == NULL) { + ::google::protobuf::internal::ReflectionOps::Merge(from, this); + } else { + MergeFrom(*source); + } +} + +void CmdRequest_Kv::MergeFrom(const CmdRequest_Kv& from) { + GOOGLE_CHECK_NE(&from, this); + if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (from.has_key()) { + set_key(from.key()); + } + if (from.has_value()) { + set_value(from.value()); + } + } + mutable_unknown_fields()->MergeFrom(from.unknown_fields()); +} + +void CmdRequest_Kv::CopyFrom(const ::google::protobuf::Message& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +void CmdRequest_Kv::CopyFrom(const CmdRequest_Kv& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +bool CmdRequest_Kv::IsInitialized() const { + if ((_has_bits_[0] & 0x00000001) != 0x00000001) return false; + + return true; +} + +void CmdRequest_Kv::Swap(CmdRequest_Kv* other) { + if (other != this) { + std::swap(key_, other->key_); + std::swap(value_, other->value_); + std::swap(_has_bits_[0], other->_has_bits_[0]); + _unknown_fields_.Swap(&other->_unknown_fields_); + std::swap(_cached_size_, other->_cached_size_); + } +} + +::google::protobuf::Metadata CmdRequest_Kv::GetMetadata() const { + protobuf_AssignDescriptorsOnce(); + ::google::protobuf::Metadata metadata; + metadata.descriptor = CmdRequest_Kv_descriptor_; + metadata.reflection = CmdRequest_Kv_reflection_; + return metadata; +} + + +// ------------------------------------------------------------------- + +#ifndef _MSC_VER +const int CmdRequest_User::kIpFieldNumber; +const int CmdRequest_User::kPortFieldNumber; +#endif // !_MSC_VER + +CmdRequest_User::CmdRequest_User() + : ::google::protobuf::Message() { + SharedCtor(); +} + +void CmdRequest_User::InitAsDefaultInstance() { +} + +CmdRequest_User::CmdRequest_User(const CmdRequest_User& from) + : ::google::protobuf::Message() { + SharedCtor(); + MergeFrom(from); +} + +void CmdRequest_User::SharedCtor() { + _cached_size_ = 0; + ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + port_ = 0; + ::memset(_has_bits_, 0, sizeof(_has_bits_)); +} + +CmdRequest_User::~CmdRequest_User() { + SharedDtor(); +} + +void CmdRequest_User::SharedDtor() { + if (ip_ != &::google::protobuf::internal::kEmptyString) { + delete ip_; + } + if (this != default_instance_) { + } +} + +void CmdRequest_User::SetCachedSize(int size) const { + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); +} +const ::google::protobuf::Descriptor* CmdRequest_User::descriptor() { + protobuf_AssignDescriptorsOnce(); + return CmdRequest_User_descriptor_; +} + +const CmdRequest_User& CmdRequest_User::default_instance() { + if (default_instance_ == NULL) protobuf_AddDesc_floyd_2eproto(); + return *default_instance_; +} + +CmdRequest_User* CmdRequest_User::default_instance_ = NULL; + +CmdRequest_User* CmdRequest_User::New() const { + return new CmdRequest_User; +} + +void CmdRequest_User::Clear() { + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (has_ip()) { + if (ip_ != &::google::protobuf::internal::kEmptyString) { + ip_->clear(); + } + } + port_ = 0; + } + ::memset(_has_bits_, 0, sizeof(_has_bits_)); + mutable_unknown_fields()->Clear(); +} + +bool CmdRequest_User::MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!(EXPRESSION)) return false + ::google::protobuf::uint32 tag; + while ((tag = input->ReadTag()) != 0) { + switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + // required bytes ip = 1; + case 1: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( + input, this->mutable_ip())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(16)) goto parse_port; + break; + } + + // required int32 port = 2; + case 2: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_port: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + input, &port_))); + set_has_port(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectAtEnd()) return true; + break; + } + + default: { + handle_uninterpreted: + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { + return true; + } + DO_(::google::protobuf::internal::WireFormat::SkipField( + input, tag, mutable_unknown_fields())); + break; + } + } + } + return true; +#undef DO_ +} + +void CmdRequest_User::SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const { + // required bytes ip = 1; + if (has_ip()) { + ::google::protobuf::internal::WireFormatLite::WriteBytes( + 1, this->ip(), output); + } + + // required int32 port = 2; + if (has_port()) { + ::google::protobuf::internal::WireFormatLite::WriteInt32(2, this->port(), output); + } + + if (!unknown_fields().empty()) { + ::google::protobuf::internal::WireFormat::SerializeUnknownFields( + unknown_fields(), output); + } +} + +::google::protobuf::uint8* CmdRequest_User::SerializeWithCachedSizesToArray( + ::google::protobuf::uint8* target) const { + // required bytes ip = 1; + if (has_ip()) { + target = + ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( + 1, this->ip(), target); + } + + // required int32 port = 2; + if (has_port()) { + target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(2, this->port(), target); + } + + if (!unknown_fields().empty()) { + target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( + unknown_fields(), target); + } + return target; +} + +int CmdRequest_User::ByteSize() const { + int total_size = 0; + + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + // required bytes ip = 1; + if (has_ip()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::BytesSize( + this->ip()); + } + + // required int32 port = 2; + if (has_port()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::Int32Size( + this->port()); + } + + } + if (!unknown_fields().empty()) { + total_size += + ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( + unknown_fields()); + } + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = total_size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); + return total_size; +} + +void CmdRequest_User::MergeFrom(const ::google::protobuf::Message& from) { + GOOGLE_CHECK_NE(&from, this); + const CmdRequest_User* source = + ::google::protobuf::internal::dynamic_cast_if_available( + &from); + if (source == NULL) { + ::google::protobuf::internal::ReflectionOps::Merge(from, this); + } else { + MergeFrom(*source); + } +} + +void CmdRequest_User::MergeFrom(const CmdRequest_User& from) { + GOOGLE_CHECK_NE(&from, this); + if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (from.has_ip()) { + set_ip(from.ip()); + } + if (from.has_port()) { + set_port(from.port()); + } + } + mutable_unknown_fields()->MergeFrom(from.unknown_fields()); +} + +void CmdRequest_User::CopyFrom(const ::google::protobuf::Message& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +void CmdRequest_User::CopyFrom(const CmdRequest_User& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +bool CmdRequest_User::IsInitialized() const { + if ((_has_bits_[0] & 0x00000003) != 0x00000003) return false; + + return true; +} + +void CmdRequest_User::Swap(CmdRequest_User* other) { + if (other != this) { + std::swap(ip_, other->ip_); + std::swap(port_, other->port_); + std::swap(_has_bits_[0], other->_has_bits_[0]); + _unknown_fields_.Swap(&other->_unknown_fields_); + std::swap(_cached_size_, other->_cached_size_); + } +} + +::google::protobuf::Metadata CmdRequest_User::GetMetadata() const { + protobuf_AssignDescriptorsOnce(); + ::google::protobuf::Metadata metadata; + metadata.descriptor = CmdRequest_User_descriptor_; + metadata.reflection = CmdRequest_User_reflection_; + return metadata; +} + + +// ------------------------------------------------------------------- + +#ifndef _MSC_VER +const int CmdRequest_RequestVote::kTermFieldNumber; +const int CmdRequest_RequestVote::kIpFieldNumber; +const int CmdRequest_RequestVote::kPortFieldNumber; +const int CmdRequest_RequestVote::kLastLogIndexFieldNumber; +const int CmdRequest_RequestVote::kLastLogTermFieldNumber; +#endif // !_MSC_VER + +CmdRequest_RequestVote::CmdRequest_RequestVote() + : ::google::protobuf::Message() { + SharedCtor(); +} + +void CmdRequest_RequestVote::InitAsDefaultInstance() { +} + +CmdRequest_RequestVote::CmdRequest_RequestVote(const CmdRequest_RequestVote& from) + : ::google::protobuf::Message() { + SharedCtor(); + MergeFrom(from); +} + +void CmdRequest_RequestVote::SharedCtor() { + _cached_size_ = 0; + term_ = GOOGLE_ULONGLONG(0); + ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + port_ = 0; + last_log_index_ = GOOGLE_ULONGLONG(0); + last_log_term_ = GOOGLE_ULONGLONG(0); + ::memset(_has_bits_, 0, sizeof(_has_bits_)); +} + +CmdRequest_RequestVote::~CmdRequest_RequestVote() { + SharedDtor(); +} + +void CmdRequest_RequestVote::SharedDtor() { + if (ip_ != &::google::protobuf::internal::kEmptyString) { + delete ip_; + } + if (this != default_instance_) { + } +} + +void CmdRequest_RequestVote::SetCachedSize(int size) const { + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); +} +const ::google::protobuf::Descriptor* CmdRequest_RequestVote::descriptor() { + protobuf_AssignDescriptorsOnce(); + return CmdRequest_RequestVote_descriptor_; +} + +const CmdRequest_RequestVote& CmdRequest_RequestVote::default_instance() { + if (default_instance_ == NULL) protobuf_AddDesc_floyd_2eproto(); + return *default_instance_; +} + +CmdRequest_RequestVote* CmdRequest_RequestVote::default_instance_ = NULL; + +CmdRequest_RequestVote* CmdRequest_RequestVote::New() const { + return new CmdRequest_RequestVote; +} + +void CmdRequest_RequestVote::Clear() { + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + term_ = GOOGLE_ULONGLONG(0); + if (has_ip()) { + if (ip_ != &::google::protobuf::internal::kEmptyString) { + ip_->clear(); + } + } + port_ = 0; + last_log_index_ = GOOGLE_ULONGLONG(0); + last_log_term_ = GOOGLE_ULONGLONG(0); + } + ::memset(_has_bits_, 0, sizeof(_has_bits_)); + mutable_unknown_fields()->Clear(); +} + +bool CmdRequest_RequestVote::MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!(EXPRESSION)) return false + ::google::protobuf::uint32 tag; + while ((tag = input->ReadTag()) != 0) { + switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + // required uint64 term = 1; + case 1: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &term_))); + set_has_term(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(18)) goto parse_ip; + break; + } + + // required bytes ip = 2; + case 2: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_ip: + DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( + input, this->mutable_ip())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(24)) goto parse_port; + break; + } + + // required int32 port = 3; + case 3: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_port: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + input, &port_))); + set_has_port(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(32)) goto parse_last_log_index; + break; + } + + // required uint64 last_log_index = 4; + case 4: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_last_log_index: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &last_log_index_))); + set_has_last_log_index(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(40)) goto parse_last_log_term; + break; + } + + // required uint64 last_log_term = 5; + case 5: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_last_log_term: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &last_log_term_))); + set_has_last_log_term(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectAtEnd()) return true; + break; + } + + default: { + handle_uninterpreted: + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { + return true; + } + DO_(::google::protobuf::internal::WireFormat::SkipField( + input, tag, mutable_unknown_fields())); + break; + } + } + } + return true; +#undef DO_ +} + +void CmdRequest_RequestVote::SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const { + // required uint64 term = 1; + if (has_term()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(1, this->term(), output); + } + + // required bytes ip = 2; + if (has_ip()) { + ::google::protobuf::internal::WireFormatLite::WriteBytes( + 2, this->ip(), output); + } + + // required int32 port = 3; + if (has_port()) { + ::google::protobuf::internal::WireFormatLite::WriteInt32(3, this->port(), output); + } + + // required uint64 last_log_index = 4; + if (has_last_log_index()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(4, this->last_log_index(), output); + } + + // required uint64 last_log_term = 5; + if (has_last_log_term()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(5, this->last_log_term(), output); + } + + if (!unknown_fields().empty()) { + ::google::protobuf::internal::WireFormat::SerializeUnknownFields( + unknown_fields(), output); + } +} + +::google::protobuf::uint8* CmdRequest_RequestVote::SerializeWithCachedSizesToArray( + ::google::protobuf::uint8* target) const { + // required uint64 term = 1; + if (has_term()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(1, this->term(), target); + } + + // required bytes ip = 2; + if (has_ip()) { + target = + ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( + 2, this->ip(), target); + } + + // required int32 port = 3; + if (has_port()) { + target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(3, this->port(), target); + } + + // required uint64 last_log_index = 4; + if (has_last_log_index()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(4, this->last_log_index(), target); + } + + // required uint64 last_log_term = 5; + if (has_last_log_term()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(5, this->last_log_term(), target); + } + + if (!unknown_fields().empty()) { + target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( + unknown_fields(), target); + } + return target; +} + +int CmdRequest_RequestVote::ByteSize() const { + int total_size = 0; + + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + // required uint64 term = 1; + if (has_term()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->term()); + } + + // required bytes ip = 2; + if (has_ip()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::BytesSize( + this->ip()); + } + + // required int32 port = 3; + if (has_port()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::Int32Size( + this->port()); + } + + // required uint64 last_log_index = 4; + if (has_last_log_index()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->last_log_index()); + } + + // required uint64 last_log_term = 5; + if (has_last_log_term()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->last_log_term()); + } + + } + if (!unknown_fields().empty()) { + total_size += + ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( + unknown_fields()); + } + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = total_size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); + return total_size; +} + +void CmdRequest_RequestVote::MergeFrom(const ::google::protobuf::Message& from) { + GOOGLE_CHECK_NE(&from, this); + const CmdRequest_RequestVote* source = + ::google::protobuf::internal::dynamic_cast_if_available( + &from); + if (source == NULL) { + ::google::protobuf::internal::ReflectionOps::Merge(from, this); + } else { + MergeFrom(*source); + } +} + +void CmdRequest_RequestVote::MergeFrom(const CmdRequest_RequestVote& from) { + GOOGLE_CHECK_NE(&from, this); + if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (from.has_term()) { + set_term(from.term()); + } + if (from.has_ip()) { + set_ip(from.ip()); + } + if (from.has_port()) { + set_port(from.port()); + } + if (from.has_last_log_index()) { + set_last_log_index(from.last_log_index()); + } + if (from.has_last_log_term()) { + set_last_log_term(from.last_log_term()); + } + } + mutable_unknown_fields()->MergeFrom(from.unknown_fields()); +} + +void CmdRequest_RequestVote::CopyFrom(const ::google::protobuf::Message& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +void CmdRequest_RequestVote::CopyFrom(const CmdRequest_RequestVote& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +bool CmdRequest_RequestVote::IsInitialized() const { + if ((_has_bits_[0] & 0x0000001f) != 0x0000001f) return false; + + return true; +} + +void CmdRequest_RequestVote::Swap(CmdRequest_RequestVote* other) { + if (other != this) { + std::swap(term_, other->term_); + std::swap(ip_, other->ip_); + std::swap(port_, other->port_); + std::swap(last_log_index_, other->last_log_index_); + std::swap(last_log_term_, other->last_log_term_); + std::swap(_has_bits_[0], other->_has_bits_[0]); + _unknown_fields_.Swap(&other->_unknown_fields_); + std::swap(_cached_size_, other->_cached_size_); + } +} + +::google::protobuf::Metadata CmdRequest_RequestVote::GetMetadata() const { + protobuf_AssignDescriptorsOnce(); + ::google::protobuf::Metadata metadata; + metadata.descriptor = CmdRequest_RequestVote_descriptor_; + metadata.reflection = CmdRequest_RequestVote_reflection_; + return metadata; +} + + +// ------------------------------------------------------------------- + +#ifndef _MSC_VER +const int CmdRequest_AppendEntries::kIpFieldNumber; +const int CmdRequest_AppendEntries::kPortFieldNumber; +const int CmdRequest_AppendEntries::kTermFieldNumber; +const int CmdRequest_AppendEntries::kPrevLogIndexFieldNumber; +const int CmdRequest_AppendEntries::kPrevLogTermFieldNumber; +const int CmdRequest_AppendEntries::kLeaderCommitFieldNumber; +const int CmdRequest_AppendEntries::kEntriesFieldNumber; +#endif // !_MSC_VER + +CmdRequest_AppendEntries::CmdRequest_AppendEntries() + : ::google::protobuf::Message() { + SharedCtor(); +} + +void CmdRequest_AppendEntries::InitAsDefaultInstance() { +} + +CmdRequest_AppendEntries::CmdRequest_AppendEntries(const CmdRequest_AppendEntries& from) + : ::google::protobuf::Message() { + SharedCtor(); + MergeFrom(from); +} + +void CmdRequest_AppendEntries::SharedCtor() { + _cached_size_ = 0; + ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + port_ = 0; + term_ = GOOGLE_ULONGLONG(0); + prev_log_index_ = GOOGLE_ULONGLONG(0); + prev_log_term_ = GOOGLE_ULONGLONG(0); + leader_commit_ = GOOGLE_ULONGLONG(0); + ::memset(_has_bits_, 0, sizeof(_has_bits_)); +} + +CmdRequest_AppendEntries::~CmdRequest_AppendEntries() { + SharedDtor(); +} + +void CmdRequest_AppendEntries::SharedDtor() { + if (ip_ != &::google::protobuf::internal::kEmptyString) { + delete ip_; + } + if (this != default_instance_) { + } +} + +void CmdRequest_AppendEntries::SetCachedSize(int size) const { + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); +} +const ::google::protobuf::Descriptor* CmdRequest_AppendEntries::descriptor() { + protobuf_AssignDescriptorsOnce(); + return CmdRequest_AppendEntries_descriptor_; +} + +const CmdRequest_AppendEntries& CmdRequest_AppendEntries::default_instance() { + if (default_instance_ == NULL) protobuf_AddDesc_floyd_2eproto(); + return *default_instance_; +} + +CmdRequest_AppendEntries* CmdRequest_AppendEntries::default_instance_ = NULL; + +CmdRequest_AppendEntries* CmdRequest_AppendEntries::New() const { + return new CmdRequest_AppendEntries; +} + +void CmdRequest_AppendEntries::Clear() { + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (has_ip()) { + if (ip_ != &::google::protobuf::internal::kEmptyString) { + ip_->clear(); + } + } + port_ = 0; + term_ = GOOGLE_ULONGLONG(0); + prev_log_index_ = GOOGLE_ULONGLONG(0); + prev_log_term_ = GOOGLE_ULONGLONG(0); + leader_commit_ = GOOGLE_ULONGLONG(0); + } + entries_.Clear(); + ::memset(_has_bits_, 0, sizeof(_has_bits_)); + mutable_unknown_fields()->Clear(); +} + +bool CmdRequest_AppendEntries::MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!(EXPRESSION)) return false + ::google::protobuf::uint32 tag; + while ((tag = input->ReadTag()) != 0) { + switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + // required bytes ip = 1; + case 1: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( + input, this->mutable_ip())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(16)) goto parse_port; + break; + } + + // required int32 port = 2; + case 2: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_port: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + input, &port_))); + set_has_port(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(24)) goto parse_term; + break; + } + + // required uint64 term = 3; + case 3: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_term: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &term_))); + set_has_term(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(32)) goto parse_prev_log_index; + break; + } + + // required uint64 prev_log_index = 4; + case 4: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_prev_log_index: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &prev_log_index_))); + set_has_prev_log_index(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(40)) goto parse_prev_log_term; + break; + } + + // required uint64 prev_log_term = 5; + case 5: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_prev_log_term: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &prev_log_term_))); + set_has_prev_log_term(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(48)) goto parse_leader_commit; + break; + } + + // required uint64 leader_commit = 6; + case 6: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_leader_commit: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &leader_commit_))); + set_has_leader_commit(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(58)) goto parse_entries; + break; + } + + // repeated .floyd.Entry entries = 7; + case 7: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_entries: + DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( + input, add_entries())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(58)) goto parse_entries; + if (input->ExpectAtEnd()) return true; + break; + } + + default: { + handle_uninterpreted: + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { + return true; + } + DO_(::google::protobuf::internal::WireFormat::SkipField( + input, tag, mutable_unknown_fields())); + break; + } + } + } + return true; +#undef DO_ +} + +void CmdRequest_AppendEntries::SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const { + // required bytes ip = 1; + if (has_ip()) { + ::google::protobuf::internal::WireFormatLite::WriteBytes( + 1, this->ip(), output); + } + + // required int32 port = 2; + if (has_port()) { + ::google::protobuf::internal::WireFormatLite::WriteInt32(2, this->port(), output); + } + + // required uint64 term = 3; + if (has_term()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(3, this->term(), output); + } + + // required uint64 prev_log_index = 4; + if (has_prev_log_index()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(4, this->prev_log_index(), output); + } + + // required uint64 prev_log_term = 5; + if (has_prev_log_term()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(5, this->prev_log_term(), output); + } + + // required uint64 leader_commit = 6; + if (has_leader_commit()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(6, this->leader_commit(), output); + } + + // repeated .floyd.Entry entries = 7; + for (int i = 0; i < this->entries_size(); i++) { + ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( + 7, this->entries(i), output); + } + + if (!unknown_fields().empty()) { + ::google::protobuf::internal::WireFormat::SerializeUnknownFields( + unknown_fields(), output); + } +} + +::google::protobuf::uint8* CmdRequest_AppendEntries::SerializeWithCachedSizesToArray( + ::google::protobuf::uint8* target) const { + // required bytes ip = 1; + if (has_ip()) { + target = + ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( + 1, this->ip(), target); + } + + // required int32 port = 2; + if (has_port()) { + target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(2, this->port(), target); + } + + // required uint64 term = 3; + if (has_term()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(3, this->term(), target); + } + + // required uint64 prev_log_index = 4; + if (has_prev_log_index()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(4, this->prev_log_index(), target); + } + + // required uint64 prev_log_term = 5; + if (has_prev_log_term()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(5, this->prev_log_term(), target); + } + + // required uint64 leader_commit = 6; + if (has_leader_commit()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(6, this->leader_commit(), target); + } + + // repeated .floyd.Entry entries = 7; + for (int i = 0; i < this->entries_size(); i++) { + target = ::google::protobuf::internal::WireFormatLite:: + WriteMessageNoVirtualToArray( + 7, this->entries(i), target); + } + + if (!unknown_fields().empty()) { + target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( + unknown_fields(), target); + } + return target; +} + +int CmdRequest_AppendEntries::ByteSize() const { + int total_size = 0; + + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + // required bytes ip = 1; + if (has_ip()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::BytesSize( + this->ip()); + } + + // required int32 port = 2; + if (has_port()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::Int32Size( + this->port()); + } + + // required uint64 term = 3; + if (has_term()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->term()); + } + + // required uint64 prev_log_index = 4; + if (has_prev_log_index()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->prev_log_index()); + } + + // required uint64 prev_log_term = 5; + if (has_prev_log_term()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->prev_log_term()); + } + + // required uint64 leader_commit = 6; + if (has_leader_commit()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->leader_commit()); + } + + } + // repeated .floyd.Entry entries = 7; + total_size += 1 * this->entries_size(); + for (int i = 0; i < this->entries_size(); i++) { + total_size += + ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( + this->entries(i)); + } + + if (!unknown_fields().empty()) { + total_size += + ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( + unknown_fields()); + } + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = total_size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); + return total_size; +} + +void CmdRequest_AppendEntries::MergeFrom(const ::google::protobuf::Message& from) { + GOOGLE_CHECK_NE(&from, this); + const CmdRequest_AppendEntries* source = + ::google::protobuf::internal::dynamic_cast_if_available( + &from); + if (source == NULL) { + ::google::protobuf::internal::ReflectionOps::Merge(from, this); + } else { + MergeFrom(*source); + } +} + +void CmdRequest_AppendEntries::MergeFrom(const CmdRequest_AppendEntries& from) { + GOOGLE_CHECK_NE(&from, this); + entries_.MergeFrom(from.entries_); + if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (from.has_ip()) { + set_ip(from.ip()); + } + if (from.has_port()) { + set_port(from.port()); + } + if (from.has_term()) { + set_term(from.term()); + } + if (from.has_prev_log_index()) { + set_prev_log_index(from.prev_log_index()); + } + if (from.has_prev_log_term()) { + set_prev_log_term(from.prev_log_term()); + } + if (from.has_leader_commit()) { + set_leader_commit(from.leader_commit()); + } + } + mutable_unknown_fields()->MergeFrom(from.unknown_fields()); +} + +void CmdRequest_AppendEntries::CopyFrom(const ::google::protobuf::Message& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +void CmdRequest_AppendEntries::CopyFrom(const CmdRequest_AppendEntries& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +bool CmdRequest_AppendEntries::IsInitialized() const { + if ((_has_bits_[0] & 0x0000003f) != 0x0000003f) return false; + + for (int i = 0; i < entries_size(); i++) { + if (!this->entries(i).IsInitialized()) return false; + } + return true; +} + +void CmdRequest_AppendEntries::Swap(CmdRequest_AppendEntries* other) { + if (other != this) { + std::swap(ip_, other->ip_); + std::swap(port_, other->port_); + std::swap(term_, other->term_); + std::swap(prev_log_index_, other->prev_log_index_); + std::swap(prev_log_term_, other->prev_log_term_); + std::swap(leader_commit_, other->leader_commit_); + entries_.Swap(&other->entries_); + std::swap(_has_bits_[0], other->_has_bits_[0]); + _unknown_fields_.Swap(&other->_unknown_fields_); + std::swap(_cached_size_, other->_cached_size_); + } +} + +::google::protobuf::Metadata CmdRequest_AppendEntries::GetMetadata() const { + protobuf_AssignDescriptorsOnce(); + ::google::protobuf::Metadata metadata; + metadata.descriptor = CmdRequest_AppendEntries_descriptor_; + metadata.reflection = CmdRequest_AppendEntries_reflection_; + return metadata; +} + + +// ------------------------------------------------------------------- + +#ifndef _MSC_VER +const int CmdRequest_ServerStatus::kTermFieldNumber; +const int CmdRequest_ServerStatus::kCommitIndexFieldNumber; +const int CmdRequest_ServerStatus::kIpFieldNumber; +const int CmdRequest_ServerStatus::kPortFieldNumber; +#endif // !_MSC_VER + +CmdRequest_ServerStatus::CmdRequest_ServerStatus() + : ::google::protobuf::Message() { + SharedCtor(); +} + +void CmdRequest_ServerStatus::InitAsDefaultInstance() { +} + +CmdRequest_ServerStatus::CmdRequest_ServerStatus(const CmdRequest_ServerStatus& from) + : ::google::protobuf::Message() { + SharedCtor(); + MergeFrom(from); +} + +void CmdRequest_ServerStatus::SharedCtor() { + _cached_size_ = 0; + term_ = GOOGLE_LONGLONG(0); + commit_index_ = GOOGLE_LONGLONG(0); + ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + port_ = 0; + ::memset(_has_bits_, 0, sizeof(_has_bits_)); +} + +CmdRequest_ServerStatus::~CmdRequest_ServerStatus() { + SharedDtor(); +} + +void CmdRequest_ServerStatus::SharedDtor() { + if (ip_ != &::google::protobuf::internal::kEmptyString) { + delete ip_; + } + if (this != default_instance_) { + } +} + +void CmdRequest_ServerStatus::SetCachedSize(int size) const { + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); +} +const ::google::protobuf::Descriptor* CmdRequest_ServerStatus::descriptor() { + protobuf_AssignDescriptorsOnce(); + return CmdRequest_ServerStatus_descriptor_; +} + +const CmdRequest_ServerStatus& CmdRequest_ServerStatus::default_instance() { + if (default_instance_ == NULL) protobuf_AddDesc_floyd_2eproto(); + return *default_instance_; +} + +CmdRequest_ServerStatus* CmdRequest_ServerStatus::default_instance_ = NULL; + +CmdRequest_ServerStatus* CmdRequest_ServerStatus::New() const { + return new CmdRequest_ServerStatus; +} + +void CmdRequest_ServerStatus::Clear() { + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + term_ = GOOGLE_LONGLONG(0); + commit_index_ = GOOGLE_LONGLONG(0); + if (has_ip()) { + if (ip_ != &::google::protobuf::internal::kEmptyString) { + ip_->clear(); + } + } + port_ = 0; + } + ::memset(_has_bits_, 0, sizeof(_has_bits_)); + mutable_unknown_fields()->Clear(); +} + +bool CmdRequest_ServerStatus::MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!(EXPRESSION)) return false + ::google::protobuf::uint32 tag; + while ((tag = input->ReadTag()) != 0) { + switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + // required int64 term = 1; + case 1: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::int64, ::google::protobuf::internal::WireFormatLite::TYPE_INT64>( + input, &term_))); + set_has_term(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(16)) goto parse_commit_index; + break; + } + + // required int64 commit_index = 2; + case 2: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_commit_index: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::int64, ::google::protobuf::internal::WireFormatLite::TYPE_INT64>( + input, &commit_index_))); + set_has_commit_index(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(26)) goto parse_ip; + break; + } + + // optional bytes ip = 3; + case 3: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_ip: + DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( + input, this->mutable_ip())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(32)) goto parse_port; + break; + } + + // optional int32 port = 4; + case 4: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_port: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + input, &port_))); + set_has_port(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectAtEnd()) return true; + break; + } + + default: { + handle_uninterpreted: + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { + return true; + } + DO_(::google::protobuf::internal::WireFormat::SkipField( + input, tag, mutable_unknown_fields())); + break; + } + } + } + return true; +#undef DO_ +} + +void CmdRequest_ServerStatus::SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const { + // required int64 term = 1; + if (has_term()) { + ::google::protobuf::internal::WireFormatLite::WriteInt64(1, this->term(), output); + } + + // required int64 commit_index = 2; + if (has_commit_index()) { + ::google::protobuf::internal::WireFormatLite::WriteInt64(2, this->commit_index(), output); + } + + // optional bytes ip = 3; + if (has_ip()) { + ::google::protobuf::internal::WireFormatLite::WriteBytes( + 3, this->ip(), output); + } + + // optional int32 port = 4; + if (has_port()) { + ::google::protobuf::internal::WireFormatLite::WriteInt32(4, this->port(), output); + } + + if (!unknown_fields().empty()) { + ::google::protobuf::internal::WireFormat::SerializeUnknownFields( + unknown_fields(), output); + } +} + +::google::protobuf::uint8* CmdRequest_ServerStatus::SerializeWithCachedSizesToArray( + ::google::protobuf::uint8* target) const { + // required int64 term = 1; + if (has_term()) { + target = ::google::protobuf::internal::WireFormatLite::WriteInt64ToArray(1, this->term(), target); + } + + // required int64 commit_index = 2; + if (has_commit_index()) { + target = ::google::protobuf::internal::WireFormatLite::WriteInt64ToArray(2, this->commit_index(), target); + } + + // optional bytes ip = 3; + if (has_ip()) { + target = + ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( + 3, this->ip(), target); + } + + // optional int32 port = 4; + if (has_port()) { + target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(4, this->port(), target); + } + + if (!unknown_fields().empty()) { + target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( + unknown_fields(), target); + } + return target; +} + +int CmdRequest_ServerStatus::ByteSize() const { + int total_size = 0; + + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + // required int64 term = 1; + if (has_term()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::Int64Size( + this->term()); + } + + // required int64 commit_index = 2; + if (has_commit_index()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::Int64Size( + this->commit_index()); + } + + // optional bytes ip = 3; + if (has_ip()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::BytesSize( + this->ip()); + } + + // optional int32 port = 4; + if (has_port()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::Int32Size( + this->port()); + } + + } + if (!unknown_fields().empty()) { + total_size += + ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( + unknown_fields()); + } + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = total_size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); + return total_size; +} + +void CmdRequest_ServerStatus::MergeFrom(const ::google::protobuf::Message& from) { + GOOGLE_CHECK_NE(&from, this); + const CmdRequest_ServerStatus* source = + ::google::protobuf::internal::dynamic_cast_if_available( + &from); + if (source == NULL) { + ::google::protobuf::internal::ReflectionOps::Merge(from, this); + } else { + MergeFrom(*source); + } +} + +void CmdRequest_ServerStatus::MergeFrom(const CmdRequest_ServerStatus& from) { + GOOGLE_CHECK_NE(&from, this); + if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (from.has_term()) { + set_term(from.term()); + } + if (from.has_commit_index()) { + set_commit_index(from.commit_index()); + } + if (from.has_ip()) { + set_ip(from.ip()); + } + if (from.has_port()) { + set_port(from.port()); + } + } + mutable_unknown_fields()->MergeFrom(from.unknown_fields()); +} + +void CmdRequest_ServerStatus::CopyFrom(const ::google::protobuf::Message& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +void CmdRequest_ServerStatus::CopyFrom(const CmdRequest_ServerStatus& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +bool CmdRequest_ServerStatus::IsInitialized() const { + if ((_has_bits_[0] & 0x00000003) != 0x00000003) return false; + + return true; +} + +void CmdRequest_ServerStatus::Swap(CmdRequest_ServerStatus* other) { + if (other != this) { + std::swap(term_, other->term_); + std::swap(commit_index_, other->commit_index_); + std::swap(ip_, other->ip_); + std::swap(port_, other->port_); + std::swap(_has_bits_[0], other->_has_bits_[0]); + _unknown_fields_.Swap(&other->_unknown_fields_); + std::swap(_cached_size_, other->_cached_size_); + } +} + +::google::protobuf::Metadata CmdRequest_ServerStatus::GetMetadata() const { + protobuf_AssignDescriptorsOnce(); + ::google::protobuf::Metadata metadata; + metadata.descriptor = CmdRequest_ServerStatus_descriptor_; + metadata.reflection = CmdRequest_ServerStatus_reflection_; + return metadata; +} + + +// ------------------------------------------------------------------- + +#ifndef _MSC_VER +const int CmdRequest::kTypeFieldNumber; +const int CmdRequest::kKvFieldNumber; +const int CmdRequest::kUserFieldNumber; +const int CmdRequest::kRequestVoteFieldNumber; +const int CmdRequest::kAppendEntriesFieldNumber; +const int CmdRequest::kServerStatusFieldNumber; +#endif // !_MSC_VER + +CmdRequest::CmdRequest() + : ::google::protobuf::Message() { + SharedCtor(); +} + +void CmdRequest::InitAsDefaultInstance() { + kv_ = const_cast< ::floyd::CmdRequest_Kv*>(&::floyd::CmdRequest_Kv::default_instance()); + user_ = const_cast< ::floyd::CmdRequest_User*>(&::floyd::CmdRequest_User::default_instance()); + request_vote_ = const_cast< ::floyd::CmdRequest_RequestVote*>(&::floyd::CmdRequest_RequestVote::default_instance()); + append_entries_ = const_cast< ::floyd::CmdRequest_AppendEntries*>(&::floyd::CmdRequest_AppendEntries::default_instance()); + server_status_ = const_cast< ::floyd::CmdRequest_ServerStatus*>(&::floyd::CmdRequest_ServerStatus::default_instance()); +} + +CmdRequest::CmdRequest(const CmdRequest& from) + : ::google::protobuf::Message() { + SharedCtor(); + MergeFrom(from); +} + +void CmdRequest::SharedCtor() { + _cached_size_ = 0; + type_ = 0; + kv_ = NULL; + user_ = NULL; + request_vote_ = NULL; + append_entries_ = NULL; + server_status_ = NULL; + ::memset(_has_bits_, 0, sizeof(_has_bits_)); +} + +CmdRequest::~CmdRequest() { + SharedDtor(); +} + +void CmdRequest::SharedDtor() { + if (this != default_instance_) { + delete kv_; + delete user_; + delete request_vote_; + delete append_entries_; + delete server_status_; + } +} + +void CmdRequest::SetCachedSize(int size) const { + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); +} +const ::google::protobuf::Descriptor* CmdRequest::descriptor() { + protobuf_AssignDescriptorsOnce(); + return CmdRequest_descriptor_; +} + +const CmdRequest& CmdRequest::default_instance() { + if (default_instance_ == NULL) protobuf_AddDesc_floyd_2eproto(); + return *default_instance_; +} + +CmdRequest* CmdRequest::default_instance_ = NULL; + +CmdRequest* CmdRequest::New() const { + return new CmdRequest; +} + +void CmdRequest::Clear() { + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + type_ = 0; + if (has_kv()) { + if (kv_ != NULL) kv_->::floyd::CmdRequest_Kv::Clear(); + } + if (has_user()) { + if (user_ != NULL) user_->::floyd::CmdRequest_User::Clear(); + } + if (has_request_vote()) { + if (request_vote_ != NULL) request_vote_->::floyd::CmdRequest_RequestVote::Clear(); + } + if (has_append_entries()) { + if (append_entries_ != NULL) append_entries_->::floyd::CmdRequest_AppendEntries::Clear(); + } + if (has_server_status()) { + if (server_status_ != NULL) server_status_->::floyd::CmdRequest_ServerStatus::Clear(); + } + } + ::memset(_has_bits_, 0, sizeof(_has_bits_)); + mutable_unknown_fields()->Clear(); +} + +bool CmdRequest::MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!(EXPRESSION)) return false + ::google::protobuf::uint32 tag; + while ((tag = input->ReadTag()) != 0) { + switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + // required .floyd.Type type = 1; + case 1: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + int value; + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( + input, &value))); + if (::floyd::Type_IsValid(value)) { + set_type(static_cast< ::floyd::Type >(value)); + } else { + mutable_unknown_fields()->AddVarint(1, value); + } + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(18)) goto parse_kv; + break; + } + + // optional .floyd.CmdRequest.Kv kv = 2; + case 2: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_kv: + DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( + input, mutable_kv())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(26)) goto parse_user; + break; + } + + // optional .floyd.CmdRequest.User user = 3; + case 3: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_user: + DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( + input, mutable_user())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(34)) goto parse_request_vote; + break; + } + + // optional .floyd.CmdRequest.RequestVote request_vote = 4; + case 4: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_request_vote: + DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( + input, mutable_request_vote())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(42)) goto parse_append_entries; + break; + } + + // optional .floyd.CmdRequest.AppendEntries append_entries = 5; + case 5: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_append_entries: + DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( + input, mutable_append_entries())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(50)) goto parse_server_status; + break; + } + + // optional .floyd.CmdRequest.ServerStatus server_status = 6; + case 6: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_server_status: + DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( + input, mutable_server_status())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectAtEnd()) return true; + break; + } + + default: { + handle_uninterpreted: + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { + return true; + } + DO_(::google::protobuf::internal::WireFormat::SkipField( + input, tag, mutable_unknown_fields())); + break; + } + } + } + return true; +#undef DO_ +} + +void CmdRequest::SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const { + // required .floyd.Type type = 1; + if (has_type()) { + ::google::protobuf::internal::WireFormatLite::WriteEnum( + 1, this->type(), output); + } + + // optional .floyd.CmdRequest.Kv kv = 2; + if (has_kv()) { + ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( + 2, this->kv(), output); + } + + // optional .floyd.CmdRequest.User user = 3; + if (has_user()) { + ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( + 3, this->user(), output); + } + + // optional .floyd.CmdRequest.RequestVote request_vote = 4; + if (has_request_vote()) { + ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( + 4, this->request_vote(), output); + } + + // optional .floyd.CmdRequest.AppendEntries append_entries = 5; + if (has_append_entries()) { + ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( + 5, this->append_entries(), output); + } + + // optional .floyd.CmdRequest.ServerStatus server_status = 6; + if (has_server_status()) { + ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( + 6, this->server_status(), output); + } + + if (!unknown_fields().empty()) { + ::google::protobuf::internal::WireFormat::SerializeUnknownFields( + unknown_fields(), output); + } +} + +::google::protobuf::uint8* CmdRequest::SerializeWithCachedSizesToArray( + ::google::protobuf::uint8* target) const { + // required .floyd.Type type = 1; + if (has_type()) { + target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray( + 1, this->type(), target); + } + + // optional .floyd.CmdRequest.Kv kv = 2; + if (has_kv()) { + target = ::google::protobuf::internal::WireFormatLite:: + WriteMessageNoVirtualToArray( + 2, this->kv(), target); + } + + // optional .floyd.CmdRequest.User user = 3; + if (has_user()) { + target = ::google::protobuf::internal::WireFormatLite:: + WriteMessageNoVirtualToArray( + 3, this->user(), target); + } + + // optional .floyd.CmdRequest.RequestVote request_vote = 4; + if (has_request_vote()) { + target = ::google::protobuf::internal::WireFormatLite:: + WriteMessageNoVirtualToArray( + 4, this->request_vote(), target); + } + + // optional .floyd.CmdRequest.AppendEntries append_entries = 5; + if (has_append_entries()) { + target = ::google::protobuf::internal::WireFormatLite:: + WriteMessageNoVirtualToArray( + 5, this->append_entries(), target); + } + + // optional .floyd.CmdRequest.ServerStatus server_status = 6; + if (has_server_status()) { + target = ::google::protobuf::internal::WireFormatLite:: + WriteMessageNoVirtualToArray( + 6, this->server_status(), target); + } + + if (!unknown_fields().empty()) { + target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( + unknown_fields(), target); + } + return target; +} + +int CmdRequest::ByteSize() const { + int total_size = 0; + + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + // required .floyd.Type type = 1; + if (has_type()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::EnumSize(this->type()); + } + + // optional .floyd.CmdRequest.Kv kv = 2; + if (has_kv()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( + this->kv()); + } + + // optional .floyd.CmdRequest.User user = 3; + if (has_user()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( + this->user()); + } + + // optional .floyd.CmdRequest.RequestVote request_vote = 4; + if (has_request_vote()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( + this->request_vote()); + } + + // optional .floyd.CmdRequest.AppendEntries append_entries = 5; + if (has_append_entries()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( + this->append_entries()); + } + + // optional .floyd.CmdRequest.ServerStatus server_status = 6; + if (has_server_status()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( + this->server_status()); + } + + } + if (!unknown_fields().empty()) { + total_size += + ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( + unknown_fields()); + } + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = total_size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); + return total_size; +} + +void CmdRequest::MergeFrom(const ::google::protobuf::Message& from) { + GOOGLE_CHECK_NE(&from, this); + const CmdRequest* source = + ::google::protobuf::internal::dynamic_cast_if_available( + &from); + if (source == NULL) { + ::google::protobuf::internal::ReflectionOps::Merge(from, this); + } else { + MergeFrom(*source); + } +} + +void CmdRequest::MergeFrom(const CmdRequest& from) { + GOOGLE_CHECK_NE(&from, this); + if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (from.has_type()) { + set_type(from.type()); + } + if (from.has_kv()) { + mutable_kv()->::floyd::CmdRequest_Kv::MergeFrom(from.kv()); + } + if (from.has_user()) { + mutable_user()->::floyd::CmdRequest_User::MergeFrom(from.user()); + } + if (from.has_request_vote()) { + mutable_request_vote()->::floyd::CmdRequest_RequestVote::MergeFrom(from.request_vote()); + } + if (from.has_append_entries()) { + mutable_append_entries()->::floyd::CmdRequest_AppendEntries::MergeFrom(from.append_entries()); + } + if (from.has_server_status()) { + mutable_server_status()->::floyd::CmdRequest_ServerStatus::MergeFrom(from.server_status()); + } + } + mutable_unknown_fields()->MergeFrom(from.unknown_fields()); +} + +void CmdRequest::CopyFrom(const ::google::protobuf::Message& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +void CmdRequest::CopyFrom(const CmdRequest& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +bool CmdRequest::IsInitialized() const { + if ((_has_bits_[0] & 0x00000001) != 0x00000001) return false; + + if (has_kv()) { + if (!this->kv().IsInitialized()) return false; + } + if (has_user()) { + if (!this->user().IsInitialized()) return false; + } + if (has_request_vote()) { + if (!this->request_vote().IsInitialized()) return false; + } + if (has_append_entries()) { + if (!this->append_entries().IsInitialized()) return false; + } + if (has_server_status()) { + if (!this->server_status().IsInitialized()) return false; + } + return true; +} + +void CmdRequest::Swap(CmdRequest* other) { + if (other != this) { + std::swap(type_, other->type_); + std::swap(kv_, other->kv_); + std::swap(user_, other->user_); + std::swap(request_vote_, other->request_vote_); + std::swap(append_entries_, other->append_entries_); + std::swap(server_status_, other->server_status_); + std::swap(_has_bits_[0], other->_has_bits_[0]); + _unknown_fields_.Swap(&other->_unknown_fields_); + std::swap(_cached_size_, other->_cached_size_); + } +} + +::google::protobuf::Metadata CmdRequest::GetMetadata() const { + protobuf_AssignDescriptorsOnce(); + ::google::protobuf::Metadata metadata; + metadata.descriptor = CmdRequest_descriptor_; + metadata.reflection = CmdRequest_reflection_; + return metadata; +} + + +// =================================================================== + +#ifndef _MSC_VER +const int CmdResponse_Kv::kValueFieldNumber; +#endif // !_MSC_VER + +CmdResponse_Kv::CmdResponse_Kv() + : ::google::protobuf::Message() { + SharedCtor(); +} + +void CmdResponse_Kv::InitAsDefaultInstance() { +} + +CmdResponse_Kv::CmdResponse_Kv(const CmdResponse_Kv& from) + : ::google::protobuf::Message() { + SharedCtor(); + MergeFrom(from); +} + +void CmdResponse_Kv::SharedCtor() { + _cached_size_ = 0; + value_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + ::memset(_has_bits_, 0, sizeof(_has_bits_)); +} + +CmdResponse_Kv::~CmdResponse_Kv() { + SharedDtor(); +} + +void CmdResponse_Kv::SharedDtor() { + if (value_ != &::google::protobuf::internal::kEmptyString) { + delete value_; + } + if (this != default_instance_) { + } +} + +void CmdResponse_Kv::SetCachedSize(int size) const { + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); +} +const ::google::protobuf::Descriptor* CmdResponse_Kv::descriptor() { + protobuf_AssignDescriptorsOnce(); + return CmdResponse_Kv_descriptor_; +} + +const CmdResponse_Kv& CmdResponse_Kv::default_instance() { + if (default_instance_ == NULL) protobuf_AddDesc_floyd_2eproto(); + return *default_instance_; +} + +CmdResponse_Kv* CmdResponse_Kv::default_instance_ = NULL; + +CmdResponse_Kv* CmdResponse_Kv::New() const { + return new CmdResponse_Kv; +} + +void CmdResponse_Kv::Clear() { + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (has_value()) { + if (value_ != &::google::protobuf::internal::kEmptyString) { + value_->clear(); + } + } + } + ::memset(_has_bits_, 0, sizeof(_has_bits_)); + mutable_unknown_fields()->Clear(); +} + +bool CmdResponse_Kv::MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!(EXPRESSION)) return false + ::google::protobuf::uint32 tag; + while ((tag = input->ReadTag()) != 0) { + switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + // optional bytes value = 1; + case 1: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( + input, this->mutable_value())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectAtEnd()) return true; + break; + } + + default: { + handle_uninterpreted: + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { + return true; + } + DO_(::google::protobuf::internal::WireFormat::SkipField( + input, tag, mutable_unknown_fields())); + break; + } + } + } + return true; +#undef DO_ +} + +void CmdResponse_Kv::SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const { + // optional bytes value = 1; + if (has_value()) { + ::google::protobuf::internal::WireFormatLite::WriteBytes( + 1, this->value(), output); + } + + if (!unknown_fields().empty()) { + ::google::protobuf::internal::WireFormat::SerializeUnknownFields( + unknown_fields(), output); + } +} + +::google::protobuf::uint8* CmdResponse_Kv::SerializeWithCachedSizesToArray( + ::google::protobuf::uint8* target) const { + // optional bytes value = 1; + if (has_value()) { + target = + ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( + 1, this->value(), target); + } + + if (!unknown_fields().empty()) { + target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( + unknown_fields(), target); + } + return target; +} + +int CmdResponse_Kv::ByteSize() const { + int total_size = 0; + + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + // optional bytes value = 1; + if (has_value()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::BytesSize( + this->value()); + } + + } + if (!unknown_fields().empty()) { + total_size += + ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( + unknown_fields()); + } + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = total_size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); + return total_size; +} + +void CmdResponse_Kv::MergeFrom(const ::google::protobuf::Message& from) { + GOOGLE_CHECK_NE(&from, this); + const CmdResponse_Kv* source = + ::google::protobuf::internal::dynamic_cast_if_available( + &from); + if (source == NULL) { + ::google::protobuf::internal::ReflectionOps::Merge(from, this); + } else { + MergeFrom(*source); + } +} + +void CmdResponse_Kv::MergeFrom(const CmdResponse_Kv& from) { + GOOGLE_CHECK_NE(&from, this); + if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (from.has_value()) { + set_value(from.value()); + } + } + mutable_unknown_fields()->MergeFrom(from.unknown_fields()); +} + +void CmdResponse_Kv::CopyFrom(const ::google::protobuf::Message& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +void CmdResponse_Kv::CopyFrom(const CmdResponse_Kv& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +bool CmdResponse_Kv::IsInitialized() const { + + return true; +} + +void CmdResponse_Kv::Swap(CmdResponse_Kv* other) { + if (other != this) { + std::swap(value_, other->value_); + std::swap(_has_bits_[0], other->_has_bits_[0]); + _unknown_fields_.Swap(&other->_unknown_fields_); + std::swap(_cached_size_, other->_cached_size_); + } +} + +::google::protobuf::Metadata CmdResponse_Kv::GetMetadata() const { + protobuf_AssignDescriptorsOnce(); + ::google::protobuf::Metadata metadata; + metadata.descriptor = CmdResponse_Kv_descriptor_; + metadata.reflection = CmdResponse_Kv_reflection_; + return metadata; +} + + +// ------------------------------------------------------------------- + +#ifndef _MSC_VER +const int CmdResponse_Kvs::kKvFieldNumber; +#endif // !_MSC_VER + +CmdResponse_Kvs::CmdResponse_Kvs() + : ::google::protobuf::Message() { + SharedCtor(); +} + +void CmdResponse_Kvs::InitAsDefaultInstance() { +} + +CmdResponse_Kvs::CmdResponse_Kvs(const CmdResponse_Kvs& from) + : ::google::protobuf::Message() { + SharedCtor(); + MergeFrom(from); +} + +void CmdResponse_Kvs::SharedCtor() { + _cached_size_ = 0; + ::memset(_has_bits_, 0, sizeof(_has_bits_)); +} + +CmdResponse_Kvs::~CmdResponse_Kvs() { + SharedDtor(); +} + +void CmdResponse_Kvs::SharedDtor() { + if (this != default_instance_) { + } +} + +void CmdResponse_Kvs::SetCachedSize(int size) const { + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); +} +const ::google::protobuf::Descriptor* CmdResponse_Kvs::descriptor() { + protobuf_AssignDescriptorsOnce(); + return CmdResponse_Kvs_descriptor_; +} + +const CmdResponse_Kvs& CmdResponse_Kvs::default_instance() { + if (default_instance_ == NULL) protobuf_AddDesc_floyd_2eproto(); + return *default_instance_; +} + +CmdResponse_Kvs* CmdResponse_Kvs::default_instance_ = NULL; + +CmdResponse_Kvs* CmdResponse_Kvs::New() const { + return new CmdResponse_Kvs; +} + +void CmdResponse_Kvs::Clear() { + kv_.Clear(); + ::memset(_has_bits_, 0, sizeof(_has_bits_)); + mutable_unknown_fields()->Clear(); +} + +bool CmdResponse_Kvs::MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!(EXPRESSION)) return false + ::google::protobuf::uint32 tag; + while ((tag = input->ReadTag()) != 0) { + switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + // repeated .floyd.CmdResponse.Kv kv = 1; + case 1: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_kv: + DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( + input, add_kv())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(10)) goto parse_kv; + if (input->ExpectAtEnd()) return true; + break; + } + + default: { + handle_uninterpreted: + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { + return true; + } + DO_(::google::protobuf::internal::WireFormat::SkipField( + input, tag, mutable_unknown_fields())); + break; + } + } + } + return true; +#undef DO_ +} + +void CmdResponse_Kvs::SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const { + // repeated .floyd.CmdResponse.Kv kv = 1; + for (int i = 0; i < this->kv_size(); i++) { + ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( + 1, this->kv(i), output); + } + + if (!unknown_fields().empty()) { + ::google::protobuf::internal::WireFormat::SerializeUnknownFields( + unknown_fields(), output); + } +} + +::google::protobuf::uint8* CmdResponse_Kvs::SerializeWithCachedSizesToArray( + ::google::protobuf::uint8* target) const { + // repeated .floyd.CmdResponse.Kv kv = 1; + for (int i = 0; i < this->kv_size(); i++) { + target = ::google::protobuf::internal::WireFormatLite:: + WriteMessageNoVirtualToArray( + 1, this->kv(i), target); + } + + if (!unknown_fields().empty()) { + target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( + unknown_fields(), target); + } + return target; +} + +int CmdResponse_Kvs::ByteSize() const { + int total_size = 0; + + // repeated .floyd.CmdResponse.Kv kv = 1; + total_size += 1 * this->kv_size(); + for (int i = 0; i < this->kv_size(); i++) { + total_size += + ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( + this->kv(i)); + } + + if (!unknown_fields().empty()) { + total_size += + ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( + unknown_fields()); + } + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = total_size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); + return total_size; +} + +void CmdResponse_Kvs::MergeFrom(const ::google::protobuf::Message& from) { + GOOGLE_CHECK_NE(&from, this); + const CmdResponse_Kvs* source = + ::google::protobuf::internal::dynamic_cast_if_available( + &from); + if (source == NULL) { + ::google::protobuf::internal::ReflectionOps::Merge(from, this); + } else { + MergeFrom(*source); + } +} + +void CmdResponse_Kvs::MergeFrom(const CmdResponse_Kvs& from) { + GOOGLE_CHECK_NE(&from, this); + kv_.MergeFrom(from.kv_); + mutable_unknown_fields()->MergeFrom(from.unknown_fields()); +} + +void CmdResponse_Kvs::CopyFrom(const ::google::protobuf::Message& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +void CmdResponse_Kvs::CopyFrom(const CmdResponse_Kvs& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +bool CmdResponse_Kvs::IsInitialized() const { + + return true; +} + +void CmdResponse_Kvs::Swap(CmdResponse_Kvs* other) { + if (other != this) { + kv_.Swap(&other->kv_); + std::swap(_has_bits_[0], other->_has_bits_[0]); + _unknown_fields_.Swap(&other->_unknown_fields_); + std::swap(_cached_size_, other->_cached_size_); + } +} + +::google::protobuf::Metadata CmdResponse_Kvs::GetMetadata() const { + protobuf_AssignDescriptorsOnce(); + ::google::protobuf::Metadata metadata; + metadata.descriptor = CmdResponse_Kvs_descriptor_; + metadata.reflection = CmdResponse_Kvs_reflection_; + return metadata; +} + + +// ------------------------------------------------------------------- + +#ifndef _MSC_VER +const int CmdResponse_RequestVoteResponse::kTermFieldNumber; +const int CmdResponse_RequestVoteResponse::kVoteGrantedFieldNumber; +#endif // !_MSC_VER + +CmdResponse_RequestVoteResponse::CmdResponse_RequestVoteResponse() + : ::google::protobuf::Message() { + SharedCtor(); +} + +void CmdResponse_RequestVoteResponse::InitAsDefaultInstance() { +} + +CmdResponse_RequestVoteResponse::CmdResponse_RequestVoteResponse(const CmdResponse_RequestVoteResponse& from) + : ::google::protobuf::Message() { + SharedCtor(); + MergeFrom(from); +} + +void CmdResponse_RequestVoteResponse::SharedCtor() { + _cached_size_ = 0; + term_ = GOOGLE_ULONGLONG(0); + vote_granted_ = false; + ::memset(_has_bits_, 0, sizeof(_has_bits_)); +} + +CmdResponse_RequestVoteResponse::~CmdResponse_RequestVoteResponse() { + SharedDtor(); +} + +void CmdResponse_RequestVoteResponse::SharedDtor() { + if (this != default_instance_) { + } +} + +void CmdResponse_RequestVoteResponse::SetCachedSize(int size) const { + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); +} +const ::google::protobuf::Descriptor* CmdResponse_RequestVoteResponse::descriptor() { + protobuf_AssignDescriptorsOnce(); + return CmdResponse_RequestVoteResponse_descriptor_; +} + +const CmdResponse_RequestVoteResponse& CmdResponse_RequestVoteResponse::default_instance() { + if (default_instance_ == NULL) protobuf_AddDesc_floyd_2eproto(); + return *default_instance_; +} + +CmdResponse_RequestVoteResponse* CmdResponse_RequestVoteResponse::default_instance_ = NULL; + +CmdResponse_RequestVoteResponse* CmdResponse_RequestVoteResponse::New() const { + return new CmdResponse_RequestVoteResponse; +} + +void CmdResponse_RequestVoteResponse::Clear() { + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + term_ = GOOGLE_ULONGLONG(0); + vote_granted_ = false; + } + ::memset(_has_bits_, 0, sizeof(_has_bits_)); + mutable_unknown_fields()->Clear(); +} + +bool CmdResponse_RequestVoteResponse::MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!(EXPRESSION)) return false + ::google::protobuf::uint32 tag; + while ((tag = input->ReadTag()) != 0) { + switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + // required uint64 term = 1; + case 1: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &term_))); + set_has_term(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(16)) goto parse_vote_granted; + break; + } + + // required bool vote_granted = 2; + case 2: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_vote_granted: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + input, &vote_granted_))); + set_has_vote_granted(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectAtEnd()) return true; + break; + } + + default: { + handle_uninterpreted: + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { + return true; + } + DO_(::google::protobuf::internal::WireFormat::SkipField( + input, tag, mutable_unknown_fields())); + break; + } + } + } + return true; +#undef DO_ +} + +void CmdResponse_RequestVoteResponse::SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const { + // required uint64 term = 1; + if (has_term()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(1, this->term(), output); + } + + // required bool vote_granted = 2; + if (has_vote_granted()) { + ::google::protobuf::internal::WireFormatLite::WriteBool(2, this->vote_granted(), output); + } + + if (!unknown_fields().empty()) { + ::google::protobuf::internal::WireFormat::SerializeUnknownFields( + unknown_fields(), output); + } +} + +::google::protobuf::uint8* CmdResponse_RequestVoteResponse::SerializeWithCachedSizesToArray( + ::google::protobuf::uint8* target) const { + // required uint64 term = 1; + if (has_term()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(1, this->term(), target); + } + + // required bool vote_granted = 2; + if (has_vote_granted()) { + target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(2, this->vote_granted(), target); + } + + if (!unknown_fields().empty()) { + target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( + unknown_fields(), target); + } + return target; +} + +int CmdResponse_RequestVoteResponse::ByteSize() const { + int total_size = 0; + + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + // required uint64 term = 1; + if (has_term()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->term()); + } + + // required bool vote_granted = 2; + if (has_vote_granted()) { + total_size += 1 + 1; + } + + } + if (!unknown_fields().empty()) { + total_size += + ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( + unknown_fields()); + } + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = total_size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); + return total_size; +} + +void CmdResponse_RequestVoteResponse::MergeFrom(const ::google::protobuf::Message& from) { + GOOGLE_CHECK_NE(&from, this); + const CmdResponse_RequestVoteResponse* source = + ::google::protobuf::internal::dynamic_cast_if_available( + &from); + if (source == NULL) { + ::google::protobuf::internal::ReflectionOps::Merge(from, this); + } else { + MergeFrom(*source); + } +} + +void CmdResponse_RequestVoteResponse::MergeFrom(const CmdResponse_RequestVoteResponse& from) { + GOOGLE_CHECK_NE(&from, this); + if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (from.has_term()) { + set_term(from.term()); + } + if (from.has_vote_granted()) { + set_vote_granted(from.vote_granted()); + } + } + mutable_unknown_fields()->MergeFrom(from.unknown_fields()); +} + +void CmdResponse_RequestVoteResponse::CopyFrom(const ::google::protobuf::Message& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +void CmdResponse_RequestVoteResponse::CopyFrom(const CmdResponse_RequestVoteResponse& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +bool CmdResponse_RequestVoteResponse::IsInitialized() const { + if ((_has_bits_[0] & 0x00000003) != 0x00000003) return false; + + return true; +} + +void CmdResponse_RequestVoteResponse::Swap(CmdResponse_RequestVoteResponse* other) { + if (other != this) { + std::swap(term_, other->term_); + std::swap(vote_granted_, other->vote_granted_); + std::swap(_has_bits_[0], other->_has_bits_[0]); + _unknown_fields_.Swap(&other->_unknown_fields_); + std::swap(_cached_size_, other->_cached_size_); + } +} + +::google::protobuf::Metadata CmdResponse_RequestVoteResponse::GetMetadata() const { + protobuf_AssignDescriptorsOnce(); + ::google::protobuf::Metadata metadata; + metadata.descriptor = CmdResponse_RequestVoteResponse_descriptor_; + metadata.reflection = CmdResponse_RequestVoteResponse_reflection_; + return metadata; +} + + +// ------------------------------------------------------------------- + +#ifndef _MSC_VER +const int CmdResponse_AppendEntriesResponse::kTermFieldNumber; +const int CmdResponse_AppendEntriesResponse::kSuccessFieldNumber; +const int CmdResponse_AppendEntriesResponse::kLastLogIndexFieldNumber; +#endif // !_MSC_VER + +CmdResponse_AppendEntriesResponse::CmdResponse_AppendEntriesResponse() + : ::google::protobuf::Message() { + SharedCtor(); +} + +void CmdResponse_AppendEntriesResponse::InitAsDefaultInstance() { +} + +CmdResponse_AppendEntriesResponse::CmdResponse_AppendEntriesResponse(const CmdResponse_AppendEntriesResponse& from) + : ::google::protobuf::Message() { + SharedCtor(); + MergeFrom(from); +} + +void CmdResponse_AppendEntriesResponse::SharedCtor() { + _cached_size_ = 0; + term_ = GOOGLE_ULONGLONG(0); + success_ = false; + last_log_index_ = GOOGLE_ULONGLONG(0); + ::memset(_has_bits_, 0, sizeof(_has_bits_)); +} + +CmdResponse_AppendEntriesResponse::~CmdResponse_AppendEntriesResponse() { + SharedDtor(); +} + +void CmdResponse_AppendEntriesResponse::SharedDtor() { + if (this != default_instance_) { + } +} + +void CmdResponse_AppendEntriesResponse::SetCachedSize(int size) const { + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); +} +const ::google::protobuf::Descriptor* CmdResponse_AppendEntriesResponse::descriptor() { + protobuf_AssignDescriptorsOnce(); + return CmdResponse_AppendEntriesResponse_descriptor_; +} + +const CmdResponse_AppendEntriesResponse& CmdResponse_AppendEntriesResponse::default_instance() { + if (default_instance_ == NULL) protobuf_AddDesc_floyd_2eproto(); + return *default_instance_; +} + +CmdResponse_AppendEntriesResponse* CmdResponse_AppendEntriesResponse::default_instance_ = NULL; + +CmdResponse_AppendEntriesResponse* CmdResponse_AppendEntriesResponse::New() const { + return new CmdResponse_AppendEntriesResponse; +} + +void CmdResponse_AppendEntriesResponse::Clear() { + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + term_ = GOOGLE_ULONGLONG(0); + success_ = false; + last_log_index_ = GOOGLE_ULONGLONG(0); + } + ::memset(_has_bits_, 0, sizeof(_has_bits_)); + mutable_unknown_fields()->Clear(); +} + +bool CmdResponse_AppendEntriesResponse::MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!(EXPRESSION)) return false + ::google::protobuf::uint32 tag; + while ((tag = input->ReadTag()) != 0) { + switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + // required uint64 term = 1; + case 1: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &term_))); + set_has_term(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(16)) goto parse_success; + break; + } + + // required bool success = 2; + case 2: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_success: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( + input, &success_))); + set_has_success(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(24)) goto parse_last_log_index; + break; + } + + // optional uint64 last_log_index = 3; + case 3: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_last_log_index: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &last_log_index_))); + set_has_last_log_index(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectAtEnd()) return true; + break; + } + + default: { + handle_uninterpreted: + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { + return true; + } + DO_(::google::protobuf::internal::WireFormat::SkipField( + input, tag, mutable_unknown_fields())); + break; + } + } + } + return true; +#undef DO_ +} + +void CmdResponse_AppendEntriesResponse::SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const { + // required uint64 term = 1; + if (has_term()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(1, this->term(), output); + } + + // required bool success = 2; + if (has_success()) { + ::google::protobuf::internal::WireFormatLite::WriteBool(2, this->success(), output); + } + + // optional uint64 last_log_index = 3; + if (has_last_log_index()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(3, this->last_log_index(), output); + } + + if (!unknown_fields().empty()) { + ::google::protobuf::internal::WireFormat::SerializeUnknownFields( + unknown_fields(), output); + } +} + +::google::protobuf::uint8* CmdResponse_AppendEntriesResponse::SerializeWithCachedSizesToArray( + ::google::protobuf::uint8* target) const { + // required uint64 term = 1; + if (has_term()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(1, this->term(), target); + } + + // required bool success = 2; + if (has_success()) { + target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(2, this->success(), target); + } + + // optional uint64 last_log_index = 3; + if (has_last_log_index()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(3, this->last_log_index(), target); + } + + if (!unknown_fields().empty()) { + target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( + unknown_fields(), target); + } + return target; +} + +int CmdResponse_AppendEntriesResponse::ByteSize() const { + int total_size = 0; + + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + // required uint64 term = 1; + if (has_term()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->term()); + } + + // required bool success = 2; + if (has_success()) { + total_size += 1 + 1; + } + + // optional uint64 last_log_index = 3; + if (has_last_log_index()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->last_log_index()); + } + + } + if (!unknown_fields().empty()) { + total_size += + ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( + unknown_fields()); + } + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = total_size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); + return total_size; +} + +void CmdResponse_AppendEntriesResponse::MergeFrom(const ::google::protobuf::Message& from) { + GOOGLE_CHECK_NE(&from, this); + const CmdResponse_AppendEntriesResponse* source = + ::google::protobuf::internal::dynamic_cast_if_available( + &from); + if (source == NULL) { + ::google::protobuf::internal::ReflectionOps::Merge(from, this); + } else { + MergeFrom(*source); + } +} + +void CmdResponse_AppendEntriesResponse::MergeFrom(const CmdResponse_AppendEntriesResponse& from) { + GOOGLE_CHECK_NE(&from, this); + if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (from.has_term()) { + set_term(from.term()); + } + if (from.has_success()) { + set_success(from.success()); + } + if (from.has_last_log_index()) { + set_last_log_index(from.last_log_index()); + } + } + mutable_unknown_fields()->MergeFrom(from.unknown_fields()); +} + +void CmdResponse_AppendEntriesResponse::CopyFrom(const ::google::protobuf::Message& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +void CmdResponse_AppendEntriesResponse::CopyFrom(const CmdResponse_AppendEntriesResponse& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +bool CmdResponse_AppendEntriesResponse::IsInitialized() const { + if ((_has_bits_[0] & 0x00000003) != 0x00000003) return false; + + return true; +} + +void CmdResponse_AppendEntriesResponse::Swap(CmdResponse_AppendEntriesResponse* other) { + if (other != this) { + std::swap(term_, other->term_); + std::swap(success_, other->success_); + std::swap(last_log_index_, other->last_log_index_); + std::swap(_has_bits_[0], other->_has_bits_[0]); + _unknown_fields_.Swap(&other->_unknown_fields_); + std::swap(_cached_size_, other->_cached_size_); + } +} + +::google::protobuf::Metadata CmdResponse_AppendEntriesResponse::GetMetadata() const { + protobuf_AssignDescriptorsOnce(); + ::google::protobuf::Metadata metadata; + metadata.descriptor = CmdResponse_AppendEntriesResponse_descriptor_; + metadata.reflection = CmdResponse_AppendEntriesResponse_reflection_; + return metadata; +} + + +// ------------------------------------------------------------------- + +#ifndef _MSC_VER +const int CmdResponse_ServerStatus::kTermFieldNumber; +const int CmdResponse_ServerStatus::kCommitIndexFieldNumber; +const int CmdResponse_ServerStatus::kRoleFieldNumber; +const int CmdResponse_ServerStatus::kLeaderIpFieldNumber; +const int CmdResponse_ServerStatus::kLeaderPortFieldNumber; +const int CmdResponse_ServerStatus::kVotedForIpFieldNumber; +const int CmdResponse_ServerStatus::kVotedForPortFieldNumber; +const int CmdResponse_ServerStatus::kLastLogTermFieldNumber; +const int CmdResponse_ServerStatus::kLastLogIndexFieldNumber; +const int CmdResponse_ServerStatus::kLastAppliedFieldNumber; +#endif // !_MSC_VER + +CmdResponse_ServerStatus::CmdResponse_ServerStatus() + : ::google::protobuf::Message() { + SharedCtor(); +} + +void CmdResponse_ServerStatus::InitAsDefaultInstance() { +} + +CmdResponse_ServerStatus::CmdResponse_ServerStatus(const CmdResponse_ServerStatus& from) + : ::google::protobuf::Message() { + SharedCtor(); + MergeFrom(from); +} + +void CmdResponse_ServerStatus::SharedCtor() { + _cached_size_ = 0; + term_ = GOOGLE_ULONGLONG(0); + commit_index_ = GOOGLE_ULONGLONG(0); + role_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + leader_ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + leader_port_ = 0; + voted_for_ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + voted_for_port_ = 0; + last_log_term_ = GOOGLE_ULONGLONG(0); + last_log_index_ = GOOGLE_ULONGLONG(0); + last_applied_ = GOOGLE_ULONGLONG(0); + ::memset(_has_bits_, 0, sizeof(_has_bits_)); +} + +CmdResponse_ServerStatus::~CmdResponse_ServerStatus() { + SharedDtor(); +} + +void CmdResponse_ServerStatus::SharedDtor() { + if (role_ != &::google::protobuf::internal::kEmptyString) { + delete role_; + } + if (leader_ip_ != &::google::protobuf::internal::kEmptyString) { + delete leader_ip_; + } + if (voted_for_ip_ != &::google::protobuf::internal::kEmptyString) { + delete voted_for_ip_; + } + if (this != default_instance_) { + } +} + +void CmdResponse_ServerStatus::SetCachedSize(int size) const { + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); +} +const ::google::protobuf::Descriptor* CmdResponse_ServerStatus::descriptor() { + protobuf_AssignDescriptorsOnce(); + return CmdResponse_ServerStatus_descriptor_; +} + +const CmdResponse_ServerStatus& CmdResponse_ServerStatus::default_instance() { + if (default_instance_ == NULL) protobuf_AddDesc_floyd_2eproto(); + return *default_instance_; +} + +CmdResponse_ServerStatus* CmdResponse_ServerStatus::default_instance_ = NULL; + +CmdResponse_ServerStatus* CmdResponse_ServerStatus::New() const { + return new CmdResponse_ServerStatus; +} + +void CmdResponse_ServerStatus::Clear() { + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + term_ = GOOGLE_ULONGLONG(0); + commit_index_ = GOOGLE_ULONGLONG(0); + if (has_role()) { + if (role_ != &::google::protobuf::internal::kEmptyString) { + role_->clear(); + } + } + if (has_leader_ip()) { + if (leader_ip_ != &::google::protobuf::internal::kEmptyString) { + leader_ip_->clear(); + } + } + leader_port_ = 0; + if (has_voted_for_ip()) { + if (voted_for_ip_ != &::google::protobuf::internal::kEmptyString) { + voted_for_ip_->clear(); + } + } + voted_for_port_ = 0; + last_log_term_ = GOOGLE_ULONGLONG(0); + } + if (_has_bits_[8 / 32] & (0xffu << (8 % 32))) { + last_log_index_ = GOOGLE_ULONGLONG(0); + last_applied_ = GOOGLE_ULONGLONG(0); + } + ::memset(_has_bits_, 0, sizeof(_has_bits_)); + mutable_unknown_fields()->Clear(); +} + +bool CmdResponse_ServerStatus::MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!(EXPRESSION)) return false + ::google::protobuf::uint32 tag; + while ((tag = input->ReadTag()) != 0) { + switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + // required uint64 term = 1; + case 1: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &term_))); + set_has_term(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(16)) goto parse_commit_index; + break; + } + + // required uint64 commit_index = 2; + case 2: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_commit_index: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &commit_index_))); + set_has_commit_index(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(26)) goto parse_role; + break; + } + + // required bytes role = 3; + case 3: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_role: + DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( + input, this->mutable_role())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(34)) goto parse_leader_ip; + break; + } + + // optional bytes leader_ip = 4; + case 4: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_leader_ip: + DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( + input, this->mutable_leader_ip())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(40)) goto parse_leader_port; + break; + } + + // optional int32 leader_port = 5; + case 5: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_leader_port: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + input, &leader_port_))); + set_has_leader_port(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(50)) goto parse_voted_for_ip; + break; + } + + // optional bytes voted_for_ip = 6; + case 6: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_voted_for_ip: + DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( + input, this->mutable_voted_for_ip())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(56)) goto parse_voted_for_port; + break; + } + + // optional int32 voted_for_port = 7; + case 7: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_voted_for_port: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( + input, &voted_for_port_))); + set_has_voted_for_port(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(64)) goto parse_last_log_term; + break; + } + + // optional uint64 last_log_term = 8; + case 8: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_last_log_term: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &last_log_term_))); + set_has_last_log_term(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(72)) goto parse_last_log_index; + break; + } + + // optional uint64 last_log_index = 9; + case 9: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_last_log_index: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &last_log_index_))); + set_has_last_log_index(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(80)) goto parse_last_applied; + break; + } + + // optional uint64 last_applied = 10; + case 10: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_last_applied: + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_UINT64>( + input, &last_applied_))); + set_has_last_applied(); + } else { + goto handle_uninterpreted; + } + if (input->ExpectAtEnd()) return true; + break; + } + + default: { + handle_uninterpreted: + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { + return true; + } + DO_(::google::protobuf::internal::WireFormat::SkipField( + input, tag, mutable_unknown_fields())); + break; + } + } + } + return true; +#undef DO_ +} + +void CmdResponse_ServerStatus::SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const { + // required uint64 term = 1; + if (has_term()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(1, this->term(), output); + } + + // required uint64 commit_index = 2; + if (has_commit_index()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(2, this->commit_index(), output); + } + + // required bytes role = 3; + if (has_role()) { + ::google::protobuf::internal::WireFormatLite::WriteBytes( + 3, this->role(), output); + } + + // optional bytes leader_ip = 4; + if (has_leader_ip()) { + ::google::protobuf::internal::WireFormatLite::WriteBytes( + 4, this->leader_ip(), output); + } + + // optional int32 leader_port = 5; + if (has_leader_port()) { + ::google::protobuf::internal::WireFormatLite::WriteInt32(5, this->leader_port(), output); + } + + // optional bytes voted_for_ip = 6; + if (has_voted_for_ip()) { + ::google::protobuf::internal::WireFormatLite::WriteBytes( + 6, this->voted_for_ip(), output); + } + + // optional int32 voted_for_port = 7; + if (has_voted_for_port()) { + ::google::protobuf::internal::WireFormatLite::WriteInt32(7, this->voted_for_port(), output); + } + + // optional uint64 last_log_term = 8; + if (has_last_log_term()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(8, this->last_log_term(), output); + } + + // optional uint64 last_log_index = 9; + if (has_last_log_index()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(9, this->last_log_index(), output); + } + + // optional uint64 last_applied = 10; + if (has_last_applied()) { + ::google::protobuf::internal::WireFormatLite::WriteUInt64(10, this->last_applied(), output); + } + + if (!unknown_fields().empty()) { + ::google::protobuf::internal::WireFormat::SerializeUnknownFields( + unknown_fields(), output); + } +} + +::google::protobuf::uint8* CmdResponse_ServerStatus::SerializeWithCachedSizesToArray( + ::google::protobuf::uint8* target) const { + // required uint64 term = 1; + if (has_term()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(1, this->term(), target); + } + + // required uint64 commit_index = 2; + if (has_commit_index()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(2, this->commit_index(), target); + } + + // required bytes role = 3; + if (has_role()) { + target = + ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( + 3, this->role(), target); + } + + // optional bytes leader_ip = 4; + if (has_leader_ip()) { + target = + ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( + 4, this->leader_ip(), target); + } + + // optional int32 leader_port = 5; + if (has_leader_port()) { + target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(5, this->leader_port(), target); + } + + // optional bytes voted_for_ip = 6; + if (has_voted_for_ip()) { + target = + ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( + 6, this->voted_for_ip(), target); + } + + // optional int32 voted_for_port = 7; + if (has_voted_for_port()) { + target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(7, this->voted_for_port(), target); + } + + // optional uint64 last_log_term = 8; + if (has_last_log_term()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(8, this->last_log_term(), target); + } + + // optional uint64 last_log_index = 9; + if (has_last_log_index()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(9, this->last_log_index(), target); + } + + // optional uint64 last_applied = 10; + if (has_last_applied()) { + target = ::google::protobuf::internal::WireFormatLite::WriteUInt64ToArray(10, this->last_applied(), target); + } + + if (!unknown_fields().empty()) { + target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( + unknown_fields(), target); + } + return target; +} + +int CmdResponse_ServerStatus::ByteSize() const { + int total_size = 0; + + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + // required uint64 term = 1; + if (has_term()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->term()); + } + + // required uint64 commit_index = 2; + if (has_commit_index()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->commit_index()); + } + + // required bytes role = 3; + if (has_role()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::BytesSize( + this->role()); + } + + // optional bytes leader_ip = 4; + if (has_leader_ip()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::BytesSize( + this->leader_ip()); + } + + // optional int32 leader_port = 5; + if (has_leader_port()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::Int32Size( + this->leader_port()); + } + + // optional bytes voted_for_ip = 6; + if (has_voted_for_ip()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::BytesSize( + this->voted_for_ip()); + } + + // optional int32 voted_for_port = 7; + if (has_voted_for_port()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::Int32Size( + this->voted_for_port()); + } + + // optional uint64 last_log_term = 8; + if (has_last_log_term()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->last_log_term()); + } + + } + if (_has_bits_[8 / 32] & (0xffu << (8 % 32))) { + // optional uint64 last_log_index = 9; + if (has_last_log_index()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->last_log_index()); + } + + // optional uint64 last_applied = 10; + if (has_last_applied()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::UInt64Size( + this->last_applied()); + } + + } + if (!unknown_fields().empty()) { + total_size += + ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( + unknown_fields()); + } + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = total_size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); + return total_size; +} + +void CmdResponse_ServerStatus::MergeFrom(const ::google::protobuf::Message& from) { + GOOGLE_CHECK_NE(&from, this); + const CmdResponse_ServerStatus* source = + ::google::protobuf::internal::dynamic_cast_if_available( + &from); + if (source == NULL) { + ::google::protobuf::internal::ReflectionOps::Merge(from, this); + } else { + MergeFrom(*source); + } +} + +void CmdResponse_ServerStatus::MergeFrom(const CmdResponse_ServerStatus& from) { + GOOGLE_CHECK_NE(&from, this); + if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (from.has_term()) { + set_term(from.term()); + } + if (from.has_commit_index()) { + set_commit_index(from.commit_index()); + } + if (from.has_role()) { + set_role(from.role()); + } + if (from.has_leader_ip()) { + set_leader_ip(from.leader_ip()); + } + if (from.has_leader_port()) { + set_leader_port(from.leader_port()); + } + if (from.has_voted_for_ip()) { + set_voted_for_ip(from.voted_for_ip()); + } + if (from.has_voted_for_port()) { + set_voted_for_port(from.voted_for_port()); + } + if (from.has_last_log_term()) { + set_last_log_term(from.last_log_term()); + } + } + if (from._has_bits_[8 / 32] & (0xffu << (8 % 32))) { + if (from.has_last_log_index()) { + set_last_log_index(from.last_log_index()); + } + if (from.has_last_applied()) { + set_last_applied(from.last_applied()); + } + } + mutable_unknown_fields()->MergeFrom(from.unknown_fields()); +} + +void CmdResponse_ServerStatus::CopyFrom(const ::google::protobuf::Message& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +void CmdResponse_ServerStatus::CopyFrom(const CmdResponse_ServerStatus& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +bool CmdResponse_ServerStatus::IsInitialized() const { + if ((_has_bits_[0] & 0x00000007) != 0x00000007) return false; + + return true; +} + +void CmdResponse_ServerStatus::Swap(CmdResponse_ServerStatus* other) { + if (other != this) { + std::swap(term_, other->term_); + std::swap(commit_index_, other->commit_index_); + std::swap(role_, other->role_); + std::swap(leader_ip_, other->leader_ip_); + std::swap(leader_port_, other->leader_port_); + std::swap(voted_for_ip_, other->voted_for_ip_); + std::swap(voted_for_port_, other->voted_for_port_); + std::swap(last_log_term_, other->last_log_term_); + std::swap(last_log_index_, other->last_log_index_); + std::swap(last_applied_, other->last_applied_); + std::swap(_has_bits_[0], other->_has_bits_[0]); + _unknown_fields_.Swap(&other->_unknown_fields_); + std::swap(_cached_size_, other->_cached_size_); + } +} + +::google::protobuf::Metadata CmdResponse_ServerStatus::GetMetadata() const { + protobuf_AssignDescriptorsOnce(); + ::google::protobuf::Metadata metadata; + metadata.descriptor = CmdResponse_ServerStatus_descriptor_; + metadata.reflection = CmdResponse_ServerStatus_reflection_; + return metadata; +} + + +// ------------------------------------------------------------------- + +#ifndef _MSC_VER +const int CmdResponse::kTypeFieldNumber; +const int CmdResponse::kCodeFieldNumber; +const int CmdResponse::kMsgFieldNumber; +const int CmdResponse::kKvFieldNumber; +const int CmdResponse::kKvsFieldNumber; +const int CmdResponse::kRequestVoteResFieldNumber; +const int CmdResponse::kAppendEntriesResFieldNumber; +const int CmdResponse::kServerStatusFieldNumber; +#endif // !_MSC_VER + +CmdResponse::CmdResponse() + : ::google::protobuf::Message() { + SharedCtor(); +} + +void CmdResponse::InitAsDefaultInstance() { + kv_ = const_cast< ::floyd::CmdResponse_Kv*>(&::floyd::CmdResponse_Kv::default_instance()); + kvs_ = const_cast< ::floyd::CmdResponse_Kvs*>(&::floyd::CmdResponse_Kvs::default_instance()); + request_vote_res_ = const_cast< ::floyd::CmdResponse_RequestVoteResponse*>(&::floyd::CmdResponse_RequestVoteResponse::default_instance()); + append_entries_res_ = const_cast< ::floyd::CmdResponse_AppendEntriesResponse*>(&::floyd::CmdResponse_AppendEntriesResponse::default_instance()); + server_status_ = const_cast< ::floyd::CmdResponse_ServerStatus*>(&::floyd::CmdResponse_ServerStatus::default_instance()); +} + +CmdResponse::CmdResponse(const CmdResponse& from) + : ::google::protobuf::Message() { + SharedCtor(); + MergeFrom(from); +} + +void CmdResponse::SharedCtor() { + _cached_size_ = 0; + type_ = 0; + code_ = 0; + msg_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + kv_ = NULL; + kvs_ = NULL; + request_vote_res_ = NULL; + append_entries_res_ = NULL; + server_status_ = NULL; + ::memset(_has_bits_, 0, sizeof(_has_bits_)); +} + +CmdResponse::~CmdResponse() { + SharedDtor(); +} + +void CmdResponse::SharedDtor() { + if (msg_ != &::google::protobuf::internal::kEmptyString) { + delete msg_; + } + if (this != default_instance_) { + delete kv_; + delete kvs_; + delete request_vote_res_; + delete append_entries_res_; + delete server_status_; + } +} + +void CmdResponse::SetCachedSize(int size) const { + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); +} +const ::google::protobuf::Descriptor* CmdResponse::descriptor() { + protobuf_AssignDescriptorsOnce(); + return CmdResponse_descriptor_; +} + +const CmdResponse& CmdResponse::default_instance() { + if (default_instance_ == NULL) protobuf_AddDesc_floyd_2eproto(); + return *default_instance_; +} + +CmdResponse* CmdResponse::default_instance_ = NULL; + +CmdResponse* CmdResponse::New() const { + return new CmdResponse; +} + +void CmdResponse::Clear() { + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + type_ = 0; + code_ = 0; + if (has_msg()) { + if (msg_ != &::google::protobuf::internal::kEmptyString) { + msg_->clear(); + } + } + if (has_kv()) { + if (kv_ != NULL) kv_->::floyd::CmdResponse_Kv::Clear(); + } + if (has_kvs()) { + if (kvs_ != NULL) kvs_->::floyd::CmdResponse_Kvs::Clear(); + } + if (has_request_vote_res()) { + if (request_vote_res_ != NULL) request_vote_res_->::floyd::CmdResponse_RequestVoteResponse::Clear(); + } + if (has_append_entries_res()) { + if (append_entries_res_ != NULL) append_entries_res_->::floyd::CmdResponse_AppendEntriesResponse::Clear(); + } + if (has_server_status()) { + if (server_status_ != NULL) server_status_->::floyd::CmdResponse_ServerStatus::Clear(); + } + } + ::memset(_has_bits_, 0, sizeof(_has_bits_)); + mutable_unknown_fields()->Clear(); +} + +bool CmdResponse::MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input) { +#define DO_(EXPRESSION) if (!(EXPRESSION)) return false + ::google::protobuf::uint32 tag; + while ((tag = input->ReadTag()) != 0) { + switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { + // required .floyd.Type type = 1; + case 1: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + int value; + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( + input, &value))); + if (::floyd::Type_IsValid(value)) { + set_type(static_cast< ::floyd::Type >(value)); + } else { + mutable_unknown_fields()->AddVarint(1, value); + } + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(16)) goto parse_code; + break; + } + + // optional .floyd.StatusCode code = 2; + case 2: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { + parse_code: + int value; + DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< + int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( + input, &value))); + if (::floyd::StatusCode_IsValid(value)) { + set_code(static_cast< ::floyd::StatusCode >(value)); + } else { + mutable_unknown_fields()->AddVarint(2, value); + } + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(26)) goto parse_msg; + break; + } + + // optional bytes msg = 3; + case 3: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_msg: + DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( + input, this->mutable_msg())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(34)) goto parse_kv; + break; + } + + // optional .floyd.CmdResponse.Kv kv = 4; + case 4: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_kv: + DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( + input, mutable_kv())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(42)) goto parse_kvs; + break; + } + + // optional .floyd.CmdResponse.Kvs kvs = 5; + case 5: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_kvs: + DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( + input, mutable_kvs())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(50)) goto parse_request_vote_res; + break; + } + + // optional .floyd.CmdResponse.RequestVoteResponse request_vote_res = 6; + case 6: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_request_vote_res: + DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( + input, mutable_request_vote_res())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(58)) goto parse_append_entries_res; + break; + } + + // optional .floyd.CmdResponse.AppendEntriesResponse append_entries_res = 7; + case 7: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_append_entries_res: + DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( + input, mutable_append_entries_res())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectTag(66)) goto parse_server_status; + break; + } + + // optional .floyd.CmdResponse.ServerStatus server_status = 8; + case 8: { + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { + parse_server_status: + DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( + input, mutable_server_status())); + } else { + goto handle_uninterpreted; + } + if (input->ExpectAtEnd()) return true; + break; + } + + default: { + handle_uninterpreted: + if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == + ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { + return true; + } + DO_(::google::protobuf::internal::WireFormat::SkipField( + input, tag, mutable_unknown_fields())); + break; + } + } + } + return true; +#undef DO_ +} + +void CmdResponse::SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const { + // required .floyd.Type type = 1; + if (has_type()) { + ::google::protobuf::internal::WireFormatLite::WriteEnum( + 1, this->type(), output); + } + + // optional .floyd.StatusCode code = 2; + if (has_code()) { + ::google::protobuf::internal::WireFormatLite::WriteEnum( + 2, this->code(), output); + } + + // optional bytes msg = 3; + if (has_msg()) { + ::google::protobuf::internal::WireFormatLite::WriteBytes( + 3, this->msg(), output); + } + + // optional .floyd.CmdResponse.Kv kv = 4; + if (has_kv()) { + ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( + 4, this->kv(), output); + } + + // optional .floyd.CmdResponse.Kvs kvs = 5; + if (has_kvs()) { + ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( + 5, this->kvs(), output); + } + + // optional .floyd.CmdResponse.RequestVoteResponse request_vote_res = 6; + if (has_request_vote_res()) { + ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( + 6, this->request_vote_res(), output); + } + + // optional .floyd.CmdResponse.AppendEntriesResponse append_entries_res = 7; + if (has_append_entries_res()) { + ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( + 7, this->append_entries_res(), output); + } + + // optional .floyd.CmdResponse.ServerStatus server_status = 8; + if (has_server_status()) { + ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray( + 8, this->server_status(), output); + } + + if (!unknown_fields().empty()) { + ::google::protobuf::internal::WireFormat::SerializeUnknownFields( + unknown_fields(), output); + } +} + +::google::protobuf::uint8* CmdResponse::SerializeWithCachedSizesToArray( + ::google::protobuf::uint8* target) const { + // required .floyd.Type type = 1; + if (has_type()) { + target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray( + 1, this->type(), target); + } + + // optional .floyd.StatusCode code = 2; + if (has_code()) { + target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray( + 2, this->code(), target); + } + + // optional bytes msg = 3; + if (has_msg()) { + target = + ::google::protobuf::internal::WireFormatLite::WriteBytesToArray( + 3, this->msg(), target); + } + + // optional .floyd.CmdResponse.Kv kv = 4; + if (has_kv()) { + target = ::google::protobuf::internal::WireFormatLite:: + WriteMessageNoVirtualToArray( + 4, this->kv(), target); + } + + // optional .floyd.CmdResponse.Kvs kvs = 5; + if (has_kvs()) { + target = ::google::protobuf::internal::WireFormatLite:: + WriteMessageNoVirtualToArray( + 5, this->kvs(), target); + } + + // optional .floyd.CmdResponse.RequestVoteResponse request_vote_res = 6; + if (has_request_vote_res()) { + target = ::google::protobuf::internal::WireFormatLite:: + WriteMessageNoVirtualToArray( + 6, this->request_vote_res(), target); + } + + // optional .floyd.CmdResponse.AppendEntriesResponse append_entries_res = 7; + if (has_append_entries_res()) { + target = ::google::protobuf::internal::WireFormatLite:: + WriteMessageNoVirtualToArray( + 7, this->append_entries_res(), target); + } + + // optional .floyd.CmdResponse.ServerStatus server_status = 8; + if (has_server_status()) { + target = ::google::protobuf::internal::WireFormatLite:: + WriteMessageNoVirtualToArray( + 8, this->server_status(), target); + } + + if (!unknown_fields().empty()) { + target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( + unknown_fields(), target); + } + return target; +} + +int CmdResponse::ByteSize() const { + int total_size = 0; + + if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { + // required .floyd.Type type = 1; + if (has_type()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::EnumSize(this->type()); + } + + // optional .floyd.StatusCode code = 2; + if (has_code()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::EnumSize(this->code()); + } + + // optional bytes msg = 3; + if (has_msg()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::BytesSize( + this->msg()); + } + + // optional .floyd.CmdResponse.Kv kv = 4; + if (has_kv()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( + this->kv()); + } + + // optional .floyd.CmdResponse.Kvs kvs = 5; + if (has_kvs()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( + this->kvs()); + } + + // optional .floyd.CmdResponse.RequestVoteResponse request_vote_res = 6; + if (has_request_vote_res()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( + this->request_vote_res()); + } + + // optional .floyd.CmdResponse.AppendEntriesResponse append_entries_res = 7; + if (has_append_entries_res()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( + this->append_entries_res()); + } + + // optional .floyd.CmdResponse.ServerStatus server_status = 8; + if (has_server_status()) { + total_size += 1 + + ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( + this->server_status()); + } + + } + if (!unknown_fields().empty()) { + total_size += + ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( + unknown_fields()); + } + GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); + _cached_size_ = total_size; + GOOGLE_SAFE_CONCURRENT_WRITES_END(); + return total_size; +} + +void CmdResponse::MergeFrom(const ::google::protobuf::Message& from) { + GOOGLE_CHECK_NE(&from, this); + const CmdResponse* source = + ::google::protobuf::internal::dynamic_cast_if_available( + &from); + if (source == NULL) { + ::google::protobuf::internal::ReflectionOps::Merge(from, this); + } else { + MergeFrom(*source); + } +} + +void CmdResponse::MergeFrom(const CmdResponse& from) { + GOOGLE_CHECK_NE(&from, this); + if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { + if (from.has_type()) { + set_type(from.type()); + } + if (from.has_code()) { + set_code(from.code()); + } + if (from.has_msg()) { + set_msg(from.msg()); + } + if (from.has_kv()) { + mutable_kv()->::floyd::CmdResponse_Kv::MergeFrom(from.kv()); + } + if (from.has_kvs()) { + mutable_kvs()->::floyd::CmdResponse_Kvs::MergeFrom(from.kvs()); + } + if (from.has_request_vote_res()) { + mutable_request_vote_res()->::floyd::CmdResponse_RequestVoteResponse::MergeFrom(from.request_vote_res()); + } + if (from.has_append_entries_res()) { + mutable_append_entries_res()->::floyd::CmdResponse_AppendEntriesResponse::MergeFrom(from.append_entries_res()); + } + if (from.has_server_status()) { + mutable_server_status()->::floyd::CmdResponse_ServerStatus::MergeFrom(from.server_status()); + } + } + mutable_unknown_fields()->MergeFrom(from.unknown_fields()); +} + +void CmdResponse::CopyFrom(const ::google::protobuf::Message& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +void CmdResponse::CopyFrom(const CmdResponse& from) { + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +bool CmdResponse::IsInitialized() const { + if ((_has_bits_[0] & 0x00000001) != 0x00000001) return false; + + if (has_request_vote_res()) { + if (!this->request_vote_res().IsInitialized()) return false; + } + if (has_append_entries_res()) { + if (!this->append_entries_res().IsInitialized()) return false; + } + if (has_server_status()) { + if (!this->server_status().IsInitialized()) return false; + } + return true; +} + +void CmdResponse::Swap(CmdResponse* other) { + if (other != this) { + std::swap(type_, other->type_); + std::swap(code_, other->code_); + std::swap(msg_, other->msg_); + std::swap(kv_, other->kv_); + std::swap(kvs_, other->kvs_); + std::swap(request_vote_res_, other->request_vote_res_); + std::swap(append_entries_res_, other->append_entries_res_); + std::swap(server_status_, other->server_status_); + std::swap(_has_bits_[0], other->_has_bits_[0]); + _unknown_fields_.Swap(&other->_unknown_fields_); + std::swap(_cached_size_, other->_cached_size_); + } +} + +::google::protobuf::Metadata CmdResponse::GetMetadata() const { + protobuf_AssignDescriptorsOnce(); + ::google::protobuf::Metadata metadata; + metadata.descriptor = CmdResponse_descriptor_; + metadata.reflection = CmdResponse_reflection_; + return metadata; +} + + +// @@protoc_insertion_point(namespace_scope) + +} // namespace floyd + +// @@protoc_insertion_point(global_scope) diff --git a/floyd/tools/floyd.pb.h b/floyd/tools/floyd.pb.h new file mode 100644 index 0000000..f89f5ff --- /dev/null +++ b/floyd/tools/floyd.pb.h @@ -0,0 +1,3816 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: floyd.proto + +#ifndef PROTOBUF_floyd_2eproto__INCLUDED +#define PROTOBUF_floyd_2eproto__INCLUDED + +#include + +#include + +#if GOOGLE_PROTOBUF_VERSION < 2005000 +#error This file was generated by a newer version of protoc which is +#error incompatible with your Protocol Buffer headers. Please update +#error your headers. +#endif +#if 2005000 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION +#error This file was generated by an older version of protoc which is +#error incompatible with your Protocol Buffer headers. Please +#error regenerate this file with a newer version of protoc. +#endif + +#include +#include +#include +#include +#include +#include +// @@protoc_insertion_point(includes) + +namespace floyd { + +// Internal implementation detail -- do not call these. +void protobuf_AddDesc_floyd_2eproto(); +void protobuf_AssignDesc_floyd_2eproto(); +void protobuf_ShutdownFile_floyd_2eproto(); + +class Entry; +class CmdRequest; +class CmdRequest_Kv; +class CmdRequest_User; +class CmdRequest_RequestVote; +class CmdRequest_AppendEntries; +class CmdRequest_ServerStatus; +class CmdResponse; +class CmdResponse_Kv; +class CmdResponse_Kvs; +class CmdResponse_RequestVoteResponse; +class CmdResponse_AppendEntriesResponse; +class CmdResponse_ServerStatus; + +enum Entry_OpType { + Entry_OpType_kRead = 0, + Entry_OpType_kWrite = 1, + Entry_OpType_kDelete = 2 +}; +bool Entry_OpType_IsValid(int value); +const Entry_OpType Entry_OpType_OpType_MIN = Entry_OpType_kRead; +const Entry_OpType Entry_OpType_OpType_MAX = Entry_OpType_kDelete; +const int Entry_OpType_OpType_ARRAYSIZE = Entry_OpType_OpType_MAX + 1; + +const ::google::protobuf::EnumDescriptor* Entry_OpType_descriptor(); +inline const ::std::string& Entry_OpType_Name(Entry_OpType value) { + return ::google::protobuf::internal::NameOfEnum( + Entry_OpType_descriptor(), value); +} +inline bool Entry_OpType_Parse( + const ::std::string& name, Entry_OpType* value) { + return ::google::protobuf::internal::ParseNamedEnum( + Entry_OpType_descriptor(), name, value); +} +enum Type { + kRead = 0, + kWrite = 1, + kDirtyWrite = 2, + kDelete = 3, + kRequestVote = 8, + kAppendEntries = 9, + kServerStatus = 10 +}; +bool Type_IsValid(int value); +const Type Type_MIN = kRead; +const Type Type_MAX = kServerStatus; +const int Type_ARRAYSIZE = Type_MAX + 1; + +const ::google::protobuf::EnumDescriptor* Type_descriptor(); +inline const ::std::string& Type_Name(Type value) { + return ::google::protobuf::internal::NameOfEnum( + Type_descriptor(), value); +} +inline bool Type_Parse( + const ::std::string& name, Type* value) { + return ::google::protobuf::internal::ParseNamedEnum( + Type_descriptor(), name, value); +} +enum StatusCode { + kOk = 0, + kNotFound = 1, + kError = 2 +}; +bool StatusCode_IsValid(int value); +const StatusCode StatusCode_MIN = kOk; +const StatusCode StatusCode_MAX = kError; +const int StatusCode_ARRAYSIZE = StatusCode_MAX + 1; + +const ::google::protobuf::EnumDescriptor* StatusCode_descriptor(); +inline const ::std::string& StatusCode_Name(StatusCode value) { + return ::google::protobuf::internal::NameOfEnum( + StatusCode_descriptor(), value); +} +inline bool StatusCode_Parse( + const ::std::string& name, StatusCode* value) { + return ::google::protobuf::internal::ParseNamedEnum( + StatusCode_descriptor(), name, value); +} +// =================================================================== + +class Entry : public ::google::protobuf::Message { + public: + Entry(); + virtual ~Entry(); + + Entry(const Entry& from); + + inline Entry& operator=(const Entry& from) { + CopyFrom(from); + return *this; + } + + inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + return _unknown_fields_; + } + + inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + return &_unknown_fields_; + } + + static const ::google::protobuf::Descriptor* descriptor(); + static const Entry& default_instance(); + + void Swap(Entry* other); + + // implements Message ---------------------------------------------- + + Entry* New() const; + void CopyFrom(const ::google::protobuf::Message& from); + void MergeFrom(const ::google::protobuf::Message& from); + void CopyFrom(const Entry& from); + void MergeFrom(const Entry& from); + void Clear(); + bool IsInitialized() const; + + int ByteSize() const; + bool MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input); + void SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const; + ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const; + int GetCachedSize() const { return _cached_size_; } + private: + void SharedCtor(); + void SharedDtor(); + void SetCachedSize(int size) const; + public: + + ::google::protobuf::Metadata GetMetadata() const; + + // nested types ---------------------------------------------------- + + typedef Entry_OpType OpType; + static const OpType kRead = Entry_OpType_kRead; + static const OpType kWrite = Entry_OpType_kWrite; + static const OpType kDelete = Entry_OpType_kDelete; + static inline bool OpType_IsValid(int value) { + return Entry_OpType_IsValid(value); + } + static const OpType OpType_MIN = + Entry_OpType_OpType_MIN; + static const OpType OpType_MAX = + Entry_OpType_OpType_MAX; + static const int OpType_ARRAYSIZE = + Entry_OpType_OpType_ARRAYSIZE; + static inline const ::google::protobuf::EnumDescriptor* + OpType_descriptor() { + return Entry_OpType_descriptor(); + } + static inline const ::std::string& OpType_Name(OpType value) { + return Entry_OpType_Name(value); + } + static inline bool OpType_Parse(const ::std::string& name, + OpType* value) { + return Entry_OpType_Parse(name, value); + } + + // accessors ------------------------------------------------------- + + // required uint64 term = 1; + inline bool has_term() const; + inline void clear_term(); + static const int kTermFieldNumber = 1; + inline ::google::protobuf::uint64 term() const; + inline void set_term(::google::protobuf::uint64 value); + + // required string key = 2; + inline bool has_key() const; + inline void clear_key(); + static const int kKeyFieldNumber = 2; + inline const ::std::string& key() const; + inline void set_key(const ::std::string& value); + inline void set_key(const char* value); + inline void set_key(const char* value, size_t size); + inline ::std::string* mutable_key(); + inline ::std::string* release_key(); + inline void set_allocated_key(::std::string* key); + + // optional bytes value = 3; + inline bool has_value() const; + inline void clear_value(); + static const int kValueFieldNumber = 3; + inline const ::std::string& value() const; + inline void set_value(const ::std::string& value); + inline void set_value(const char* value); + inline void set_value(const void* value, size_t size); + inline ::std::string* mutable_value(); + inline ::std::string* release_value(); + inline void set_allocated_value(::std::string* value); + + // required .floyd.Entry.OpType optype = 4; + inline bool has_optype() const; + inline void clear_optype(); + static const int kOptypeFieldNumber = 4; + inline ::floyd::Entry_OpType optype() const; + inline void set_optype(::floyd::Entry_OpType value); + + // @@protoc_insertion_point(class_scope:floyd.Entry) + private: + inline void set_has_term(); + inline void clear_has_term(); + inline void set_has_key(); + inline void clear_has_key(); + inline void set_has_value(); + inline void clear_has_value(); + inline void set_has_optype(); + inline void clear_has_optype(); + + ::google::protobuf::UnknownFieldSet _unknown_fields_; + + ::google::protobuf::uint64 term_; + ::std::string* key_; + ::std::string* value_; + int optype_; + + mutable int _cached_size_; + ::google::protobuf::uint32 _has_bits_[(4 + 31) / 32]; + + friend void protobuf_AddDesc_floyd_2eproto(); + friend void protobuf_AssignDesc_floyd_2eproto(); + friend void protobuf_ShutdownFile_floyd_2eproto(); + + void InitAsDefaultInstance(); + static Entry* default_instance_; +}; +// ------------------------------------------------------------------- + +class CmdRequest_Kv : public ::google::protobuf::Message { + public: + CmdRequest_Kv(); + virtual ~CmdRequest_Kv(); + + CmdRequest_Kv(const CmdRequest_Kv& from); + + inline CmdRequest_Kv& operator=(const CmdRequest_Kv& from) { + CopyFrom(from); + return *this; + } + + inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + return _unknown_fields_; + } + + inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + return &_unknown_fields_; + } + + static const ::google::protobuf::Descriptor* descriptor(); + static const CmdRequest_Kv& default_instance(); + + void Swap(CmdRequest_Kv* other); + + // implements Message ---------------------------------------------- + + CmdRequest_Kv* New() const; + void CopyFrom(const ::google::protobuf::Message& from); + void MergeFrom(const ::google::protobuf::Message& from); + void CopyFrom(const CmdRequest_Kv& from); + void MergeFrom(const CmdRequest_Kv& from); + void Clear(); + bool IsInitialized() const; + + int ByteSize() const; + bool MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input); + void SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const; + ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const; + int GetCachedSize() const { return _cached_size_; } + private: + void SharedCtor(); + void SharedDtor(); + void SetCachedSize(int size) const; + public: + + ::google::protobuf::Metadata GetMetadata() const; + + // nested types ---------------------------------------------------- + + // accessors ------------------------------------------------------- + + // required bytes key = 1; + inline bool has_key() const; + inline void clear_key(); + static const int kKeyFieldNumber = 1; + inline const ::std::string& key() const; + inline void set_key(const ::std::string& value); + inline void set_key(const char* value); + inline void set_key(const void* value, size_t size); + inline ::std::string* mutable_key(); + inline ::std::string* release_key(); + inline void set_allocated_key(::std::string* key); + + // optional bytes value = 2; + inline bool has_value() const; + inline void clear_value(); + static const int kValueFieldNumber = 2; + inline const ::std::string& value() const; + inline void set_value(const ::std::string& value); + inline void set_value(const char* value); + inline void set_value(const void* value, size_t size); + inline ::std::string* mutable_value(); + inline ::std::string* release_value(); + inline void set_allocated_value(::std::string* value); + + // @@protoc_insertion_point(class_scope:floyd.CmdRequest.Kv) + private: + inline void set_has_key(); + inline void clear_has_key(); + inline void set_has_value(); + inline void clear_has_value(); + + ::google::protobuf::UnknownFieldSet _unknown_fields_; + + ::std::string* key_; + ::std::string* value_; + + mutable int _cached_size_; + ::google::protobuf::uint32 _has_bits_[(2 + 31) / 32]; + + friend void protobuf_AddDesc_floyd_2eproto(); + friend void protobuf_AssignDesc_floyd_2eproto(); + friend void protobuf_ShutdownFile_floyd_2eproto(); + + void InitAsDefaultInstance(); + static CmdRequest_Kv* default_instance_; +}; +// ------------------------------------------------------------------- + +class CmdRequest_User : public ::google::protobuf::Message { + public: + CmdRequest_User(); + virtual ~CmdRequest_User(); + + CmdRequest_User(const CmdRequest_User& from); + + inline CmdRequest_User& operator=(const CmdRequest_User& from) { + CopyFrom(from); + return *this; + } + + inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + return _unknown_fields_; + } + + inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + return &_unknown_fields_; + } + + static const ::google::protobuf::Descriptor* descriptor(); + static const CmdRequest_User& default_instance(); + + void Swap(CmdRequest_User* other); + + // implements Message ---------------------------------------------- + + CmdRequest_User* New() const; + void CopyFrom(const ::google::protobuf::Message& from); + void MergeFrom(const ::google::protobuf::Message& from); + void CopyFrom(const CmdRequest_User& from); + void MergeFrom(const CmdRequest_User& from); + void Clear(); + bool IsInitialized() const; + + int ByteSize() const; + bool MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input); + void SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const; + ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const; + int GetCachedSize() const { return _cached_size_; } + private: + void SharedCtor(); + void SharedDtor(); + void SetCachedSize(int size) const; + public: + + ::google::protobuf::Metadata GetMetadata() const; + + // nested types ---------------------------------------------------- + + // accessors ------------------------------------------------------- + + // required bytes ip = 1; + inline bool has_ip() const; + inline void clear_ip(); + static const int kIpFieldNumber = 1; + inline const ::std::string& ip() const; + inline void set_ip(const ::std::string& value); + inline void set_ip(const char* value); + inline void set_ip(const void* value, size_t size); + inline ::std::string* mutable_ip(); + inline ::std::string* release_ip(); + inline void set_allocated_ip(::std::string* ip); + + // required int32 port = 2; + inline bool has_port() const; + inline void clear_port(); + static const int kPortFieldNumber = 2; + inline ::google::protobuf::int32 port() const; + inline void set_port(::google::protobuf::int32 value); + + // @@protoc_insertion_point(class_scope:floyd.CmdRequest.User) + private: + inline void set_has_ip(); + inline void clear_has_ip(); + inline void set_has_port(); + inline void clear_has_port(); + + ::google::protobuf::UnknownFieldSet _unknown_fields_; + + ::std::string* ip_; + ::google::protobuf::int32 port_; + + mutable int _cached_size_; + ::google::protobuf::uint32 _has_bits_[(2 + 31) / 32]; + + friend void protobuf_AddDesc_floyd_2eproto(); + friend void protobuf_AssignDesc_floyd_2eproto(); + friend void protobuf_ShutdownFile_floyd_2eproto(); + + void InitAsDefaultInstance(); + static CmdRequest_User* default_instance_; +}; +// ------------------------------------------------------------------- + +class CmdRequest_RequestVote : public ::google::protobuf::Message { + public: + CmdRequest_RequestVote(); + virtual ~CmdRequest_RequestVote(); + + CmdRequest_RequestVote(const CmdRequest_RequestVote& from); + + inline CmdRequest_RequestVote& operator=(const CmdRequest_RequestVote& from) { + CopyFrom(from); + return *this; + } + + inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + return _unknown_fields_; + } + + inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + return &_unknown_fields_; + } + + static const ::google::protobuf::Descriptor* descriptor(); + static const CmdRequest_RequestVote& default_instance(); + + void Swap(CmdRequest_RequestVote* other); + + // implements Message ---------------------------------------------- + + CmdRequest_RequestVote* New() const; + void CopyFrom(const ::google::protobuf::Message& from); + void MergeFrom(const ::google::protobuf::Message& from); + void CopyFrom(const CmdRequest_RequestVote& from); + void MergeFrom(const CmdRequest_RequestVote& from); + void Clear(); + bool IsInitialized() const; + + int ByteSize() const; + bool MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input); + void SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const; + ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const; + int GetCachedSize() const { return _cached_size_; } + private: + void SharedCtor(); + void SharedDtor(); + void SetCachedSize(int size) const; + public: + + ::google::protobuf::Metadata GetMetadata() const; + + // nested types ---------------------------------------------------- + + // accessors ------------------------------------------------------- + + // required uint64 term = 1; + inline bool has_term() const; + inline void clear_term(); + static const int kTermFieldNumber = 1; + inline ::google::protobuf::uint64 term() const; + inline void set_term(::google::protobuf::uint64 value); + + // required bytes ip = 2; + inline bool has_ip() const; + inline void clear_ip(); + static const int kIpFieldNumber = 2; + inline const ::std::string& ip() const; + inline void set_ip(const ::std::string& value); + inline void set_ip(const char* value); + inline void set_ip(const void* value, size_t size); + inline ::std::string* mutable_ip(); + inline ::std::string* release_ip(); + inline void set_allocated_ip(::std::string* ip); + + // required int32 port = 3; + inline bool has_port() const; + inline void clear_port(); + static const int kPortFieldNumber = 3; + inline ::google::protobuf::int32 port() const; + inline void set_port(::google::protobuf::int32 value); + + // required uint64 last_log_index = 4; + inline bool has_last_log_index() const; + inline void clear_last_log_index(); + static const int kLastLogIndexFieldNumber = 4; + inline ::google::protobuf::uint64 last_log_index() const; + inline void set_last_log_index(::google::protobuf::uint64 value); + + // required uint64 last_log_term = 5; + inline bool has_last_log_term() const; + inline void clear_last_log_term(); + static const int kLastLogTermFieldNumber = 5; + inline ::google::protobuf::uint64 last_log_term() const; + inline void set_last_log_term(::google::protobuf::uint64 value); + + // @@protoc_insertion_point(class_scope:floyd.CmdRequest.RequestVote) + private: + inline void set_has_term(); + inline void clear_has_term(); + inline void set_has_ip(); + inline void clear_has_ip(); + inline void set_has_port(); + inline void clear_has_port(); + inline void set_has_last_log_index(); + inline void clear_has_last_log_index(); + inline void set_has_last_log_term(); + inline void clear_has_last_log_term(); + + ::google::protobuf::UnknownFieldSet _unknown_fields_; + + ::google::protobuf::uint64 term_; + ::std::string* ip_; + ::google::protobuf::uint64 last_log_index_; + ::google::protobuf::uint64 last_log_term_; + ::google::protobuf::int32 port_; + + mutable int _cached_size_; + ::google::protobuf::uint32 _has_bits_[(5 + 31) / 32]; + + friend void protobuf_AddDesc_floyd_2eproto(); + friend void protobuf_AssignDesc_floyd_2eproto(); + friend void protobuf_ShutdownFile_floyd_2eproto(); + + void InitAsDefaultInstance(); + static CmdRequest_RequestVote* default_instance_; +}; +// ------------------------------------------------------------------- + +class CmdRequest_AppendEntries : public ::google::protobuf::Message { + public: + CmdRequest_AppendEntries(); + virtual ~CmdRequest_AppendEntries(); + + CmdRequest_AppendEntries(const CmdRequest_AppendEntries& from); + + inline CmdRequest_AppendEntries& operator=(const CmdRequest_AppendEntries& from) { + CopyFrom(from); + return *this; + } + + inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + return _unknown_fields_; + } + + inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + return &_unknown_fields_; + } + + static const ::google::protobuf::Descriptor* descriptor(); + static const CmdRequest_AppendEntries& default_instance(); + + void Swap(CmdRequest_AppendEntries* other); + + // implements Message ---------------------------------------------- + + CmdRequest_AppendEntries* New() const; + void CopyFrom(const ::google::protobuf::Message& from); + void MergeFrom(const ::google::protobuf::Message& from); + void CopyFrom(const CmdRequest_AppendEntries& from); + void MergeFrom(const CmdRequest_AppendEntries& from); + void Clear(); + bool IsInitialized() const; + + int ByteSize() const; + bool MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input); + void SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const; + ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const; + int GetCachedSize() const { return _cached_size_; } + private: + void SharedCtor(); + void SharedDtor(); + void SetCachedSize(int size) const; + public: + + ::google::protobuf::Metadata GetMetadata() const; + + // nested types ---------------------------------------------------- + + // accessors ------------------------------------------------------- + + // required bytes ip = 1; + inline bool has_ip() const; + inline void clear_ip(); + static const int kIpFieldNumber = 1; + inline const ::std::string& ip() const; + inline void set_ip(const ::std::string& value); + inline void set_ip(const char* value); + inline void set_ip(const void* value, size_t size); + inline ::std::string* mutable_ip(); + inline ::std::string* release_ip(); + inline void set_allocated_ip(::std::string* ip); + + // required int32 port = 2; + inline bool has_port() const; + inline void clear_port(); + static const int kPortFieldNumber = 2; + inline ::google::protobuf::int32 port() const; + inline void set_port(::google::protobuf::int32 value); + + // required uint64 term = 3; + inline bool has_term() const; + inline void clear_term(); + static const int kTermFieldNumber = 3; + inline ::google::protobuf::uint64 term() const; + inline void set_term(::google::protobuf::uint64 value); + + // required uint64 prev_log_index = 4; + inline bool has_prev_log_index() const; + inline void clear_prev_log_index(); + static const int kPrevLogIndexFieldNumber = 4; + inline ::google::protobuf::uint64 prev_log_index() const; + inline void set_prev_log_index(::google::protobuf::uint64 value); + + // required uint64 prev_log_term = 5; + inline bool has_prev_log_term() const; + inline void clear_prev_log_term(); + static const int kPrevLogTermFieldNumber = 5; + inline ::google::protobuf::uint64 prev_log_term() const; + inline void set_prev_log_term(::google::protobuf::uint64 value); + + // required uint64 leader_commit = 6; + inline bool has_leader_commit() const; + inline void clear_leader_commit(); + static const int kLeaderCommitFieldNumber = 6; + inline ::google::protobuf::uint64 leader_commit() const; + inline void set_leader_commit(::google::protobuf::uint64 value); + + // repeated .floyd.Entry entries = 7; + inline int entries_size() const; + inline void clear_entries(); + static const int kEntriesFieldNumber = 7; + inline const ::floyd::Entry& entries(int index) const; + inline ::floyd::Entry* mutable_entries(int index); + inline ::floyd::Entry* add_entries(); + inline const ::google::protobuf::RepeatedPtrField< ::floyd::Entry >& + entries() const; + inline ::google::protobuf::RepeatedPtrField< ::floyd::Entry >* + mutable_entries(); + + // @@protoc_insertion_point(class_scope:floyd.CmdRequest.AppendEntries) + private: + inline void set_has_ip(); + inline void clear_has_ip(); + inline void set_has_port(); + inline void clear_has_port(); + inline void set_has_term(); + inline void clear_has_term(); + inline void set_has_prev_log_index(); + inline void clear_has_prev_log_index(); + inline void set_has_prev_log_term(); + inline void clear_has_prev_log_term(); + inline void set_has_leader_commit(); + inline void clear_has_leader_commit(); + + ::google::protobuf::UnknownFieldSet _unknown_fields_; + + ::std::string* ip_; + ::google::protobuf::uint64 term_; + ::google::protobuf::uint64 prev_log_index_; + ::google::protobuf::uint64 prev_log_term_; + ::google::protobuf::uint64 leader_commit_; + ::google::protobuf::RepeatedPtrField< ::floyd::Entry > entries_; + ::google::protobuf::int32 port_; + + mutable int _cached_size_; + ::google::protobuf::uint32 _has_bits_[(7 + 31) / 32]; + + friend void protobuf_AddDesc_floyd_2eproto(); + friend void protobuf_AssignDesc_floyd_2eproto(); + friend void protobuf_ShutdownFile_floyd_2eproto(); + + void InitAsDefaultInstance(); + static CmdRequest_AppendEntries* default_instance_; +}; +// ------------------------------------------------------------------- + +class CmdRequest_ServerStatus : public ::google::protobuf::Message { + public: + CmdRequest_ServerStatus(); + virtual ~CmdRequest_ServerStatus(); + + CmdRequest_ServerStatus(const CmdRequest_ServerStatus& from); + + inline CmdRequest_ServerStatus& operator=(const CmdRequest_ServerStatus& from) { + CopyFrom(from); + return *this; + } + + inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + return _unknown_fields_; + } + + inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + return &_unknown_fields_; + } + + static const ::google::protobuf::Descriptor* descriptor(); + static const CmdRequest_ServerStatus& default_instance(); + + void Swap(CmdRequest_ServerStatus* other); + + // implements Message ---------------------------------------------- + + CmdRequest_ServerStatus* New() const; + void CopyFrom(const ::google::protobuf::Message& from); + void MergeFrom(const ::google::protobuf::Message& from); + void CopyFrom(const CmdRequest_ServerStatus& from); + void MergeFrom(const CmdRequest_ServerStatus& from); + void Clear(); + bool IsInitialized() const; + + int ByteSize() const; + bool MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input); + void SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const; + ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const; + int GetCachedSize() const { return _cached_size_; } + private: + void SharedCtor(); + void SharedDtor(); + void SetCachedSize(int size) const; + public: + + ::google::protobuf::Metadata GetMetadata() const; + + // nested types ---------------------------------------------------- + + // accessors ------------------------------------------------------- + + // required int64 term = 1; + inline bool has_term() const; + inline void clear_term(); + static const int kTermFieldNumber = 1; + inline ::google::protobuf::int64 term() const; + inline void set_term(::google::protobuf::int64 value); + + // required int64 commit_index = 2; + inline bool has_commit_index() const; + inline void clear_commit_index(); + static const int kCommitIndexFieldNumber = 2; + inline ::google::protobuf::int64 commit_index() const; + inline void set_commit_index(::google::protobuf::int64 value); + + // optional bytes ip = 3; + inline bool has_ip() const; + inline void clear_ip(); + static const int kIpFieldNumber = 3; + inline const ::std::string& ip() const; + inline void set_ip(const ::std::string& value); + inline void set_ip(const char* value); + inline void set_ip(const void* value, size_t size); + inline ::std::string* mutable_ip(); + inline ::std::string* release_ip(); + inline void set_allocated_ip(::std::string* ip); + + // optional int32 port = 4; + inline bool has_port() const; + inline void clear_port(); + static const int kPortFieldNumber = 4; + inline ::google::protobuf::int32 port() const; + inline void set_port(::google::protobuf::int32 value); + + // @@protoc_insertion_point(class_scope:floyd.CmdRequest.ServerStatus) + private: + inline void set_has_term(); + inline void clear_has_term(); + inline void set_has_commit_index(); + inline void clear_has_commit_index(); + inline void set_has_ip(); + inline void clear_has_ip(); + inline void set_has_port(); + inline void clear_has_port(); + + ::google::protobuf::UnknownFieldSet _unknown_fields_; + + ::google::protobuf::int64 term_; + ::google::protobuf::int64 commit_index_; + ::std::string* ip_; + ::google::protobuf::int32 port_; + + mutable int _cached_size_; + ::google::protobuf::uint32 _has_bits_[(4 + 31) / 32]; + + friend void protobuf_AddDesc_floyd_2eproto(); + friend void protobuf_AssignDesc_floyd_2eproto(); + friend void protobuf_ShutdownFile_floyd_2eproto(); + + void InitAsDefaultInstance(); + static CmdRequest_ServerStatus* default_instance_; +}; +// ------------------------------------------------------------------- + +class CmdRequest : public ::google::protobuf::Message { + public: + CmdRequest(); + virtual ~CmdRequest(); + + CmdRequest(const CmdRequest& from); + + inline CmdRequest& operator=(const CmdRequest& from) { + CopyFrom(from); + return *this; + } + + inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + return _unknown_fields_; + } + + inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + return &_unknown_fields_; + } + + static const ::google::protobuf::Descriptor* descriptor(); + static const CmdRequest& default_instance(); + + void Swap(CmdRequest* other); + + // implements Message ---------------------------------------------- + + CmdRequest* New() const; + void CopyFrom(const ::google::protobuf::Message& from); + void MergeFrom(const ::google::protobuf::Message& from); + void CopyFrom(const CmdRequest& from); + void MergeFrom(const CmdRequest& from); + void Clear(); + bool IsInitialized() const; + + int ByteSize() const; + bool MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input); + void SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const; + ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const; + int GetCachedSize() const { return _cached_size_; } + private: + void SharedCtor(); + void SharedDtor(); + void SetCachedSize(int size) const; + public: + + ::google::protobuf::Metadata GetMetadata() const; + + // nested types ---------------------------------------------------- + + typedef CmdRequest_Kv Kv; + typedef CmdRequest_User User; + typedef CmdRequest_RequestVote RequestVote; + typedef CmdRequest_AppendEntries AppendEntries; + typedef CmdRequest_ServerStatus ServerStatus; + + // accessors ------------------------------------------------------- + + // required .floyd.Type type = 1; + inline bool has_type() const; + inline void clear_type(); + static const int kTypeFieldNumber = 1; + inline ::floyd::Type type() const; + inline void set_type(::floyd::Type value); + + // optional .floyd.CmdRequest.Kv kv = 2; + inline bool has_kv() const; + inline void clear_kv(); + static const int kKvFieldNumber = 2; + inline const ::floyd::CmdRequest_Kv& kv() const; + inline ::floyd::CmdRequest_Kv* mutable_kv(); + inline ::floyd::CmdRequest_Kv* release_kv(); + inline void set_allocated_kv(::floyd::CmdRequest_Kv* kv); + + // optional .floyd.CmdRequest.User user = 3; + inline bool has_user() const; + inline void clear_user(); + static const int kUserFieldNumber = 3; + inline const ::floyd::CmdRequest_User& user() const; + inline ::floyd::CmdRequest_User* mutable_user(); + inline ::floyd::CmdRequest_User* release_user(); + inline void set_allocated_user(::floyd::CmdRequest_User* user); + + // optional .floyd.CmdRequest.RequestVote request_vote = 4; + inline bool has_request_vote() const; + inline void clear_request_vote(); + static const int kRequestVoteFieldNumber = 4; + inline const ::floyd::CmdRequest_RequestVote& request_vote() const; + inline ::floyd::CmdRequest_RequestVote* mutable_request_vote(); + inline ::floyd::CmdRequest_RequestVote* release_request_vote(); + inline void set_allocated_request_vote(::floyd::CmdRequest_RequestVote* request_vote); + + // optional .floyd.CmdRequest.AppendEntries append_entries = 5; + inline bool has_append_entries() const; + inline void clear_append_entries(); + static const int kAppendEntriesFieldNumber = 5; + inline const ::floyd::CmdRequest_AppendEntries& append_entries() const; + inline ::floyd::CmdRequest_AppendEntries* mutable_append_entries(); + inline ::floyd::CmdRequest_AppendEntries* release_append_entries(); + inline void set_allocated_append_entries(::floyd::CmdRequest_AppendEntries* append_entries); + + // optional .floyd.CmdRequest.ServerStatus server_status = 6; + inline bool has_server_status() const; + inline void clear_server_status(); + static const int kServerStatusFieldNumber = 6; + inline const ::floyd::CmdRequest_ServerStatus& server_status() const; + inline ::floyd::CmdRequest_ServerStatus* mutable_server_status(); + inline ::floyd::CmdRequest_ServerStatus* release_server_status(); + inline void set_allocated_server_status(::floyd::CmdRequest_ServerStatus* server_status); + + // @@protoc_insertion_point(class_scope:floyd.CmdRequest) + private: + inline void set_has_type(); + inline void clear_has_type(); + inline void set_has_kv(); + inline void clear_has_kv(); + inline void set_has_user(); + inline void clear_has_user(); + inline void set_has_request_vote(); + inline void clear_has_request_vote(); + inline void set_has_append_entries(); + inline void clear_has_append_entries(); + inline void set_has_server_status(); + inline void clear_has_server_status(); + + ::google::protobuf::UnknownFieldSet _unknown_fields_; + + ::floyd::CmdRequest_Kv* kv_; + ::floyd::CmdRequest_User* user_; + ::floyd::CmdRequest_RequestVote* request_vote_; + ::floyd::CmdRequest_AppendEntries* append_entries_; + ::floyd::CmdRequest_ServerStatus* server_status_; + int type_; + + mutable int _cached_size_; + ::google::protobuf::uint32 _has_bits_[(6 + 31) / 32]; + + friend void protobuf_AddDesc_floyd_2eproto(); + friend void protobuf_AssignDesc_floyd_2eproto(); + friend void protobuf_ShutdownFile_floyd_2eproto(); + + void InitAsDefaultInstance(); + static CmdRequest* default_instance_; +}; +// ------------------------------------------------------------------- + +class CmdResponse_Kv : public ::google::protobuf::Message { + public: + CmdResponse_Kv(); + virtual ~CmdResponse_Kv(); + + CmdResponse_Kv(const CmdResponse_Kv& from); + + inline CmdResponse_Kv& operator=(const CmdResponse_Kv& from) { + CopyFrom(from); + return *this; + } + + inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + return _unknown_fields_; + } + + inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + return &_unknown_fields_; + } + + static const ::google::protobuf::Descriptor* descriptor(); + static const CmdResponse_Kv& default_instance(); + + void Swap(CmdResponse_Kv* other); + + // implements Message ---------------------------------------------- + + CmdResponse_Kv* New() const; + void CopyFrom(const ::google::protobuf::Message& from); + void MergeFrom(const ::google::protobuf::Message& from); + void CopyFrom(const CmdResponse_Kv& from); + void MergeFrom(const CmdResponse_Kv& from); + void Clear(); + bool IsInitialized() const; + + int ByteSize() const; + bool MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input); + void SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const; + ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const; + int GetCachedSize() const { return _cached_size_; } + private: + void SharedCtor(); + void SharedDtor(); + void SetCachedSize(int size) const; + public: + + ::google::protobuf::Metadata GetMetadata() const; + + // nested types ---------------------------------------------------- + + // accessors ------------------------------------------------------- + + // optional bytes value = 1; + inline bool has_value() const; + inline void clear_value(); + static const int kValueFieldNumber = 1; + inline const ::std::string& value() const; + inline void set_value(const ::std::string& value); + inline void set_value(const char* value); + inline void set_value(const void* value, size_t size); + inline ::std::string* mutable_value(); + inline ::std::string* release_value(); + inline void set_allocated_value(::std::string* value); + + // @@protoc_insertion_point(class_scope:floyd.CmdResponse.Kv) + private: + inline void set_has_value(); + inline void clear_has_value(); + + ::google::protobuf::UnknownFieldSet _unknown_fields_; + + ::std::string* value_; + + mutable int _cached_size_; + ::google::protobuf::uint32 _has_bits_[(1 + 31) / 32]; + + friend void protobuf_AddDesc_floyd_2eproto(); + friend void protobuf_AssignDesc_floyd_2eproto(); + friend void protobuf_ShutdownFile_floyd_2eproto(); + + void InitAsDefaultInstance(); + static CmdResponse_Kv* default_instance_; +}; +// ------------------------------------------------------------------- + +class CmdResponse_Kvs : public ::google::protobuf::Message { + public: + CmdResponse_Kvs(); + virtual ~CmdResponse_Kvs(); + + CmdResponse_Kvs(const CmdResponse_Kvs& from); + + inline CmdResponse_Kvs& operator=(const CmdResponse_Kvs& from) { + CopyFrom(from); + return *this; + } + + inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + return _unknown_fields_; + } + + inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + return &_unknown_fields_; + } + + static const ::google::protobuf::Descriptor* descriptor(); + static const CmdResponse_Kvs& default_instance(); + + void Swap(CmdResponse_Kvs* other); + + // implements Message ---------------------------------------------- + + CmdResponse_Kvs* New() const; + void CopyFrom(const ::google::protobuf::Message& from); + void MergeFrom(const ::google::protobuf::Message& from); + void CopyFrom(const CmdResponse_Kvs& from); + void MergeFrom(const CmdResponse_Kvs& from); + void Clear(); + bool IsInitialized() const; + + int ByteSize() const; + bool MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input); + void SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const; + ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const; + int GetCachedSize() const { return _cached_size_; } + private: + void SharedCtor(); + void SharedDtor(); + void SetCachedSize(int size) const; + public: + + ::google::protobuf::Metadata GetMetadata() const; + + // nested types ---------------------------------------------------- + + // accessors ------------------------------------------------------- + + // repeated .floyd.CmdResponse.Kv kv = 1; + inline int kv_size() const; + inline void clear_kv(); + static const int kKvFieldNumber = 1; + inline const ::floyd::CmdResponse_Kv& kv(int index) const; + inline ::floyd::CmdResponse_Kv* mutable_kv(int index); + inline ::floyd::CmdResponse_Kv* add_kv(); + inline const ::google::protobuf::RepeatedPtrField< ::floyd::CmdResponse_Kv >& + kv() const; + inline ::google::protobuf::RepeatedPtrField< ::floyd::CmdResponse_Kv >* + mutable_kv(); + + // @@protoc_insertion_point(class_scope:floyd.CmdResponse.Kvs) + private: + + ::google::protobuf::UnknownFieldSet _unknown_fields_; + + ::google::protobuf::RepeatedPtrField< ::floyd::CmdResponse_Kv > kv_; + + mutable int _cached_size_; + ::google::protobuf::uint32 _has_bits_[(1 + 31) / 32]; + + friend void protobuf_AddDesc_floyd_2eproto(); + friend void protobuf_AssignDesc_floyd_2eproto(); + friend void protobuf_ShutdownFile_floyd_2eproto(); + + void InitAsDefaultInstance(); + static CmdResponse_Kvs* default_instance_; +}; +// ------------------------------------------------------------------- + +class CmdResponse_RequestVoteResponse : public ::google::protobuf::Message { + public: + CmdResponse_RequestVoteResponse(); + virtual ~CmdResponse_RequestVoteResponse(); + + CmdResponse_RequestVoteResponse(const CmdResponse_RequestVoteResponse& from); + + inline CmdResponse_RequestVoteResponse& operator=(const CmdResponse_RequestVoteResponse& from) { + CopyFrom(from); + return *this; + } + + inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + return _unknown_fields_; + } + + inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + return &_unknown_fields_; + } + + static const ::google::protobuf::Descriptor* descriptor(); + static const CmdResponse_RequestVoteResponse& default_instance(); + + void Swap(CmdResponse_RequestVoteResponse* other); + + // implements Message ---------------------------------------------- + + CmdResponse_RequestVoteResponse* New() const; + void CopyFrom(const ::google::protobuf::Message& from); + void MergeFrom(const ::google::protobuf::Message& from); + void CopyFrom(const CmdResponse_RequestVoteResponse& from); + void MergeFrom(const CmdResponse_RequestVoteResponse& from); + void Clear(); + bool IsInitialized() const; + + int ByteSize() const; + bool MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input); + void SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const; + ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const; + int GetCachedSize() const { return _cached_size_; } + private: + void SharedCtor(); + void SharedDtor(); + void SetCachedSize(int size) const; + public: + + ::google::protobuf::Metadata GetMetadata() const; + + // nested types ---------------------------------------------------- + + // accessors ------------------------------------------------------- + + // required uint64 term = 1; + inline bool has_term() const; + inline void clear_term(); + static const int kTermFieldNumber = 1; + inline ::google::protobuf::uint64 term() const; + inline void set_term(::google::protobuf::uint64 value); + + // required bool vote_granted = 2; + inline bool has_vote_granted() const; + inline void clear_vote_granted(); + static const int kVoteGrantedFieldNumber = 2; + inline bool vote_granted() const; + inline void set_vote_granted(bool value); + + // @@protoc_insertion_point(class_scope:floyd.CmdResponse.RequestVoteResponse) + private: + inline void set_has_term(); + inline void clear_has_term(); + inline void set_has_vote_granted(); + inline void clear_has_vote_granted(); + + ::google::protobuf::UnknownFieldSet _unknown_fields_; + + ::google::protobuf::uint64 term_; + bool vote_granted_; + + mutable int _cached_size_; + ::google::protobuf::uint32 _has_bits_[(2 + 31) / 32]; + + friend void protobuf_AddDesc_floyd_2eproto(); + friend void protobuf_AssignDesc_floyd_2eproto(); + friend void protobuf_ShutdownFile_floyd_2eproto(); + + void InitAsDefaultInstance(); + static CmdResponse_RequestVoteResponse* default_instance_; +}; +// ------------------------------------------------------------------- + +class CmdResponse_AppendEntriesResponse : public ::google::protobuf::Message { + public: + CmdResponse_AppendEntriesResponse(); + virtual ~CmdResponse_AppendEntriesResponse(); + + CmdResponse_AppendEntriesResponse(const CmdResponse_AppendEntriesResponse& from); + + inline CmdResponse_AppendEntriesResponse& operator=(const CmdResponse_AppendEntriesResponse& from) { + CopyFrom(from); + return *this; + } + + inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + return _unknown_fields_; + } + + inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + return &_unknown_fields_; + } + + static const ::google::protobuf::Descriptor* descriptor(); + static const CmdResponse_AppendEntriesResponse& default_instance(); + + void Swap(CmdResponse_AppendEntriesResponse* other); + + // implements Message ---------------------------------------------- + + CmdResponse_AppendEntriesResponse* New() const; + void CopyFrom(const ::google::protobuf::Message& from); + void MergeFrom(const ::google::protobuf::Message& from); + void CopyFrom(const CmdResponse_AppendEntriesResponse& from); + void MergeFrom(const CmdResponse_AppendEntriesResponse& from); + void Clear(); + bool IsInitialized() const; + + int ByteSize() const; + bool MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input); + void SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const; + ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const; + int GetCachedSize() const { return _cached_size_; } + private: + void SharedCtor(); + void SharedDtor(); + void SetCachedSize(int size) const; + public: + + ::google::protobuf::Metadata GetMetadata() const; + + // nested types ---------------------------------------------------- + + // accessors ------------------------------------------------------- + + // required uint64 term = 1; + inline bool has_term() const; + inline void clear_term(); + static const int kTermFieldNumber = 1; + inline ::google::protobuf::uint64 term() const; + inline void set_term(::google::protobuf::uint64 value); + + // required bool success = 2; + inline bool has_success() const; + inline void clear_success(); + static const int kSuccessFieldNumber = 2; + inline bool success() const; + inline void set_success(bool value); + + // optional uint64 last_log_index = 3; + inline bool has_last_log_index() const; + inline void clear_last_log_index(); + static const int kLastLogIndexFieldNumber = 3; + inline ::google::protobuf::uint64 last_log_index() const; + inline void set_last_log_index(::google::protobuf::uint64 value); + + // @@protoc_insertion_point(class_scope:floyd.CmdResponse.AppendEntriesResponse) + private: + inline void set_has_term(); + inline void clear_has_term(); + inline void set_has_success(); + inline void clear_has_success(); + inline void set_has_last_log_index(); + inline void clear_has_last_log_index(); + + ::google::protobuf::UnknownFieldSet _unknown_fields_; + + ::google::protobuf::uint64 term_; + ::google::protobuf::uint64 last_log_index_; + bool success_; + + mutable int _cached_size_; + ::google::protobuf::uint32 _has_bits_[(3 + 31) / 32]; + + friend void protobuf_AddDesc_floyd_2eproto(); + friend void protobuf_AssignDesc_floyd_2eproto(); + friend void protobuf_ShutdownFile_floyd_2eproto(); + + void InitAsDefaultInstance(); + static CmdResponse_AppendEntriesResponse* default_instance_; +}; +// ------------------------------------------------------------------- + +class CmdResponse_ServerStatus : public ::google::protobuf::Message { + public: + CmdResponse_ServerStatus(); + virtual ~CmdResponse_ServerStatus(); + + CmdResponse_ServerStatus(const CmdResponse_ServerStatus& from); + + inline CmdResponse_ServerStatus& operator=(const CmdResponse_ServerStatus& from) { + CopyFrom(from); + return *this; + } + + inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + return _unknown_fields_; + } + + inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + return &_unknown_fields_; + } + + static const ::google::protobuf::Descriptor* descriptor(); + static const CmdResponse_ServerStatus& default_instance(); + + void Swap(CmdResponse_ServerStatus* other); + + // implements Message ---------------------------------------------- + + CmdResponse_ServerStatus* New() const; + void CopyFrom(const ::google::protobuf::Message& from); + void MergeFrom(const ::google::protobuf::Message& from); + void CopyFrom(const CmdResponse_ServerStatus& from); + void MergeFrom(const CmdResponse_ServerStatus& from); + void Clear(); + bool IsInitialized() const; + + int ByteSize() const; + bool MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input); + void SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const; + ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const; + int GetCachedSize() const { return _cached_size_; } + private: + void SharedCtor(); + void SharedDtor(); + void SetCachedSize(int size) const; + public: + + ::google::protobuf::Metadata GetMetadata() const; + + // nested types ---------------------------------------------------- + + // accessors ------------------------------------------------------- + + // required uint64 term = 1; + inline bool has_term() const; + inline void clear_term(); + static const int kTermFieldNumber = 1; + inline ::google::protobuf::uint64 term() const; + inline void set_term(::google::protobuf::uint64 value); + + // required uint64 commit_index = 2; + inline bool has_commit_index() const; + inline void clear_commit_index(); + static const int kCommitIndexFieldNumber = 2; + inline ::google::protobuf::uint64 commit_index() const; + inline void set_commit_index(::google::protobuf::uint64 value); + + // required bytes role = 3; + inline bool has_role() const; + inline void clear_role(); + static const int kRoleFieldNumber = 3; + inline const ::std::string& role() const; + inline void set_role(const ::std::string& value); + inline void set_role(const char* value); + inline void set_role(const void* value, size_t size); + inline ::std::string* mutable_role(); + inline ::std::string* release_role(); + inline void set_allocated_role(::std::string* role); + + // optional bytes leader_ip = 4; + inline bool has_leader_ip() const; + inline void clear_leader_ip(); + static const int kLeaderIpFieldNumber = 4; + inline const ::std::string& leader_ip() const; + inline void set_leader_ip(const ::std::string& value); + inline void set_leader_ip(const char* value); + inline void set_leader_ip(const void* value, size_t size); + inline ::std::string* mutable_leader_ip(); + inline ::std::string* release_leader_ip(); + inline void set_allocated_leader_ip(::std::string* leader_ip); + + // optional int32 leader_port = 5; + inline bool has_leader_port() const; + inline void clear_leader_port(); + static const int kLeaderPortFieldNumber = 5; + inline ::google::protobuf::int32 leader_port() const; + inline void set_leader_port(::google::protobuf::int32 value); + + // optional bytes voted_for_ip = 6; + inline bool has_voted_for_ip() const; + inline void clear_voted_for_ip(); + static const int kVotedForIpFieldNumber = 6; + inline const ::std::string& voted_for_ip() const; + inline void set_voted_for_ip(const ::std::string& value); + inline void set_voted_for_ip(const char* value); + inline void set_voted_for_ip(const void* value, size_t size); + inline ::std::string* mutable_voted_for_ip(); + inline ::std::string* release_voted_for_ip(); + inline void set_allocated_voted_for_ip(::std::string* voted_for_ip); + + // optional int32 voted_for_port = 7; + inline bool has_voted_for_port() const; + inline void clear_voted_for_port(); + static const int kVotedForPortFieldNumber = 7; + inline ::google::protobuf::int32 voted_for_port() const; + inline void set_voted_for_port(::google::protobuf::int32 value); + + // optional uint64 last_log_term = 8; + inline bool has_last_log_term() const; + inline void clear_last_log_term(); + static const int kLastLogTermFieldNumber = 8; + inline ::google::protobuf::uint64 last_log_term() const; + inline void set_last_log_term(::google::protobuf::uint64 value); + + // optional uint64 last_log_index = 9; + inline bool has_last_log_index() const; + inline void clear_last_log_index(); + static const int kLastLogIndexFieldNumber = 9; + inline ::google::protobuf::uint64 last_log_index() const; + inline void set_last_log_index(::google::protobuf::uint64 value); + + // optional uint64 last_applied = 10; + inline bool has_last_applied() const; + inline void clear_last_applied(); + static const int kLastAppliedFieldNumber = 10; + inline ::google::protobuf::uint64 last_applied() const; + inline void set_last_applied(::google::protobuf::uint64 value); + + // @@protoc_insertion_point(class_scope:floyd.CmdResponse.ServerStatus) + private: + inline void set_has_term(); + inline void clear_has_term(); + inline void set_has_commit_index(); + inline void clear_has_commit_index(); + inline void set_has_role(); + inline void clear_has_role(); + inline void set_has_leader_ip(); + inline void clear_has_leader_ip(); + inline void set_has_leader_port(); + inline void clear_has_leader_port(); + inline void set_has_voted_for_ip(); + inline void clear_has_voted_for_ip(); + inline void set_has_voted_for_port(); + inline void clear_has_voted_for_port(); + inline void set_has_last_log_term(); + inline void clear_has_last_log_term(); + inline void set_has_last_log_index(); + inline void clear_has_last_log_index(); + inline void set_has_last_applied(); + inline void clear_has_last_applied(); + + ::google::protobuf::UnknownFieldSet _unknown_fields_; + + ::google::protobuf::uint64 term_; + ::google::protobuf::uint64 commit_index_; + ::std::string* role_; + ::std::string* leader_ip_; + ::std::string* voted_for_ip_; + ::google::protobuf::int32 leader_port_; + ::google::protobuf::int32 voted_for_port_; + ::google::protobuf::uint64 last_log_term_; + ::google::protobuf::uint64 last_log_index_; + ::google::protobuf::uint64 last_applied_; + + mutable int _cached_size_; + ::google::protobuf::uint32 _has_bits_[(10 + 31) / 32]; + + friend void protobuf_AddDesc_floyd_2eproto(); + friend void protobuf_AssignDesc_floyd_2eproto(); + friend void protobuf_ShutdownFile_floyd_2eproto(); + + void InitAsDefaultInstance(); + static CmdResponse_ServerStatus* default_instance_; +}; +// ------------------------------------------------------------------- + +class CmdResponse : public ::google::protobuf::Message { + public: + CmdResponse(); + virtual ~CmdResponse(); + + CmdResponse(const CmdResponse& from); + + inline CmdResponse& operator=(const CmdResponse& from) { + CopyFrom(from); + return *this; + } + + inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { + return _unknown_fields_; + } + + inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { + return &_unknown_fields_; + } + + static const ::google::protobuf::Descriptor* descriptor(); + static const CmdResponse& default_instance(); + + void Swap(CmdResponse* other); + + // implements Message ---------------------------------------------- + + CmdResponse* New() const; + void CopyFrom(const ::google::protobuf::Message& from); + void MergeFrom(const ::google::protobuf::Message& from); + void CopyFrom(const CmdResponse& from); + void MergeFrom(const CmdResponse& from); + void Clear(); + bool IsInitialized() const; + + int ByteSize() const; + bool MergePartialFromCodedStream( + ::google::protobuf::io::CodedInputStream* input); + void SerializeWithCachedSizes( + ::google::protobuf::io::CodedOutputStream* output) const; + ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const; + int GetCachedSize() const { return _cached_size_; } + private: + void SharedCtor(); + void SharedDtor(); + void SetCachedSize(int size) const; + public: + + ::google::protobuf::Metadata GetMetadata() const; + + // nested types ---------------------------------------------------- + + typedef CmdResponse_Kv Kv; + typedef CmdResponse_Kvs Kvs; + typedef CmdResponse_RequestVoteResponse RequestVoteResponse; + typedef CmdResponse_AppendEntriesResponse AppendEntriesResponse; + typedef CmdResponse_ServerStatus ServerStatus; + + // accessors ------------------------------------------------------- + + // required .floyd.Type type = 1; + inline bool has_type() const; + inline void clear_type(); + static const int kTypeFieldNumber = 1; + inline ::floyd::Type type() const; + inline void set_type(::floyd::Type value); + + // optional .floyd.StatusCode code = 2; + inline bool has_code() const; + inline void clear_code(); + static const int kCodeFieldNumber = 2; + inline ::floyd::StatusCode code() const; + inline void set_code(::floyd::StatusCode value); + + // optional bytes msg = 3; + inline bool has_msg() const; + inline void clear_msg(); + static const int kMsgFieldNumber = 3; + inline const ::std::string& msg() const; + inline void set_msg(const ::std::string& value); + inline void set_msg(const char* value); + inline void set_msg(const void* value, size_t size); + inline ::std::string* mutable_msg(); + inline ::std::string* release_msg(); + inline void set_allocated_msg(::std::string* msg); + + // optional .floyd.CmdResponse.Kv kv = 4; + inline bool has_kv() const; + inline void clear_kv(); + static const int kKvFieldNumber = 4; + inline const ::floyd::CmdResponse_Kv& kv() const; + inline ::floyd::CmdResponse_Kv* mutable_kv(); + inline ::floyd::CmdResponse_Kv* release_kv(); + inline void set_allocated_kv(::floyd::CmdResponse_Kv* kv); + + // optional .floyd.CmdResponse.Kvs kvs = 5; + inline bool has_kvs() const; + inline void clear_kvs(); + static const int kKvsFieldNumber = 5; + inline const ::floyd::CmdResponse_Kvs& kvs() const; + inline ::floyd::CmdResponse_Kvs* mutable_kvs(); + inline ::floyd::CmdResponse_Kvs* release_kvs(); + inline void set_allocated_kvs(::floyd::CmdResponse_Kvs* kvs); + + // optional .floyd.CmdResponse.RequestVoteResponse request_vote_res = 6; + inline bool has_request_vote_res() const; + inline void clear_request_vote_res(); + static const int kRequestVoteResFieldNumber = 6; + inline const ::floyd::CmdResponse_RequestVoteResponse& request_vote_res() const; + inline ::floyd::CmdResponse_RequestVoteResponse* mutable_request_vote_res(); + inline ::floyd::CmdResponse_RequestVoteResponse* release_request_vote_res(); + inline void set_allocated_request_vote_res(::floyd::CmdResponse_RequestVoteResponse* request_vote_res); + + // optional .floyd.CmdResponse.AppendEntriesResponse append_entries_res = 7; + inline bool has_append_entries_res() const; + inline void clear_append_entries_res(); + static const int kAppendEntriesResFieldNumber = 7; + inline const ::floyd::CmdResponse_AppendEntriesResponse& append_entries_res() const; + inline ::floyd::CmdResponse_AppendEntriesResponse* mutable_append_entries_res(); + inline ::floyd::CmdResponse_AppendEntriesResponse* release_append_entries_res(); + inline void set_allocated_append_entries_res(::floyd::CmdResponse_AppendEntriesResponse* append_entries_res); + + // optional .floyd.CmdResponse.ServerStatus server_status = 8; + inline bool has_server_status() const; + inline void clear_server_status(); + static const int kServerStatusFieldNumber = 8; + inline const ::floyd::CmdResponse_ServerStatus& server_status() const; + inline ::floyd::CmdResponse_ServerStatus* mutable_server_status(); + inline ::floyd::CmdResponse_ServerStatus* release_server_status(); + inline void set_allocated_server_status(::floyd::CmdResponse_ServerStatus* server_status); + + // @@protoc_insertion_point(class_scope:floyd.CmdResponse) + private: + inline void set_has_type(); + inline void clear_has_type(); + inline void set_has_code(); + inline void clear_has_code(); + inline void set_has_msg(); + inline void clear_has_msg(); + inline void set_has_kv(); + inline void clear_has_kv(); + inline void set_has_kvs(); + inline void clear_has_kvs(); + inline void set_has_request_vote_res(); + inline void clear_has_request_vote_res(); + inline void set_has_append_entries_res(); + inline void clear_has_append_entries_res(); + inline void set_has_server_status(); + inline void clear_has_server_status(); + + ::google::protobuf::UnknownFieldSet _unknown_fields_; + + int type_; + int code_; + ::std::string* msg_; + ::floyd::CmdResponse_Kv* kv_; + ::floyd::CmdResponse_Kvs* kvs_; + ::floyd::CmdResponse_RequestVoteResponse* request_vote_res_; + ::floyd::CmdResponse_AppendEntriesResponse* append_entries_res_; + ::floyd::CmdResponse_ServerStatus* server_status_; + + mutable int _cached_size_; + ::google::protobuf::uint32 _has_bits_[(8 + 31) / 32]; + + friend void protobuf_AddDesc_floyd_2eproto(); + friend void protobuf_AssignDesc_floyd_2eproto(); + friend void protobuf_ShutdownFile_floyd_2eproto(); + + void InitAsDefaultInstance(); + static CmdResponse* default_instance_; +}; +// =================================================================== + + +// =================================================================== + +// Entry + +// required uint64 term = 1; +inline bool Entry::has_term() const { + return (_has_bits_[0] & 0x00000001u) != 0; +} +inline void Entry::set_has_term() { + _has_bits_[0] |= 0x00000001u; +} +inline void Entry::clear_has_term() { + _has_bits_[0] &= ~0x00000001u; +} +inline void Entry::clear_term() { + term_ = GOOGLE_ULONGLONG(0); + clear_has_term(); +} +inline ::google::protobuf::uint64 Entry::term() const { + return term_; +} +inline void Entry::set_term(::google::protobuf::uint64 value) { + set_has_term(); + term_ = value; +} + +// required string key = 2; +inline bool Entry::has_key() const { + return (_has_bits_[0] & 0x00000002u) != 0; +} +inline void Entry::set_has_key() { + _has_bits_[0] |= 0x00000002u; +} +inline void Entry::clear_has_key() { + _has_bits_[0] &= ~0x00000002u; +} +inline void Entry::clear_key() { + if (key_ != &::google::protobuf::internal::kEmptyString) { + key_->clear(); + } + clear_has_key(); +} +inline const ::std::string& Entry::key() const { + return *key_; +} +inline void Entry::set_key(const ::std::string& value) { + set_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + key_ = new ::std::string; + } + key_->assign(value); +} +inline void Entry::set_key(const char* value) { + set_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + key_ = new ::std::string; + } + key_->assign(value); +} +inline void Entry::set_key(const char* value, size_t size) { + set_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + key_ = new ::std::string; + } + key_->assign(reinterpret_cast(value), size); +} +inline ::std::string* Entry::mutable_key() { + set_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + key_ = new ::std::string; + } + return key_; +} +inline ::std::string* Entry::release_key() { + clear_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + return NULL; + } else { + ::std::string* temp = key_; + key_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + return temp; + } +} +inline void Entry::set_allocated_key(::std::string* key) { + if (key_ != &::google::protobuf::internal::kEmptyString) { + delete key_; + } + if (key) { + set_has_key(); + key_ = key; + } else { + clear_has_key(); + key_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + } +} + +// optional bytes value = 3; +inline bool Entry::has_value() const { + return (_has_bits_[0] & 0x00000004u) != 0; +} +inline void Entry::set_has_value() { + _has_bits_[0] |= 0x00000004u; +} +inline void Entry::clear_has_value() { + _has_bits_[0] &= ~0x00000004u; +} +inline void Entry::clear_value() { + if (value_ != &::google::protobuf::internal::kEmptyString) { + value_->clear(); + } + clear_has_value(); +} +inline const ::std::string& Entry::value() const { + return *value_; +} +inline void Entry::set_value(const ::std::string& value) { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; + } + value_->assign(value); +} +inline void Entry::set_value(const char* value) { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; + } + value_->assign(value); +} +inline void Entry::set_value(const void* value, size_t size) { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; + } + value_->assign(reinterpret_cast(value), size); +} +inline ::std::string* Entry::mutable_value() { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; + } + return value_; +} +inline ::std::string* Entry::release_value() { + clear_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + return NULL; + } else { + ::std::string* temp = value_; + value_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + return temp; + } +} +inline void Entry::set_allocated_value(::std::string* value) { + if (value_ != &::google::protobuf::internal::kEmptyString) { + delete value_; + } + if (value) { + set_has_value(); + value_ = value; + } else { + clear_has_value(); + value_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + } +} + +// required .floyd.Entry.OpType optype = 4; +inline bool Entry::has_optype() const { + return (_has_bits_[0] & 0x00000008u) != 0; +} +inline void Entry::set_has_optype() { + _has_bits_[0] |= 0x00000008u; +} +inline void Entry::clear_has_optype() { + _has_bits_[0] &= ~0x00000008u; +} +inline void Entry::clear_optype() { + optype_ = 0; + clear_has_optype(); +} +inline ::floyd::Entry_OpType Entry::optype() const { + return static_cast< ::floyd::Entry_OpType >(optype_); +} +inline void Entry::set_optype(::floyd::Entry_OpType value) { + assert(::floyd::Entry_OpType_IsValid(value)); + set_has_optype(); + optype_ = value; +} + +// ------------------------------------------------------------------- + +// CmdRequest_Kv + +// required bytes key = 1; +inline bool CmdRequest_Kv::has_key() const { + return (_has_bits_[0] & 0x00000001u) != 0; +} +inline void CmdRequest_Kv::set_has_key() { + _has_bits_[0] |= 0x00000001u; +} +inline void CmdRequest_Kv::clear_has_key() { + _has_bits_[0] &= ~0x00000001u; +} +inline void CmdRequest_Kv::clear_key() { + if (key_ != &::google::protobuf::internal::kEmptyString) { + key_->clear(); + } + clear_has_key(); +} +inline const ::std::string& CmdRequest_Kv::key() const { + return *key_; +} +inline void CmdRequest_Kv::set_key(const ::std::string& value) { + set_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + key_ = new ::std::string; + } + key_->assign(value); +} +inline void CmdRequest_Kv::set_key(const char* value) { + set_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + key_ = new ::std::string; + } + key_->assign(value); +} +inline void CmdRequest_Kv::set_key(const void* value, size_t size) { + set_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + key_ = new ::std::string; + } + key_->assign(reinterpret_cast(value), size); +} +inline ::std::string* CmdRequest_Kv::mutable_key() { + set_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + key_ = new ::std::string; + } + return key_; +} +inline ::std::string* CmdRequest_Kv::release_key() { + clear_has_key(); + if (key_ == &::google::protobuf::internal::kEmptyString) { + return NULL; + } else { + ::std::string* temp = key_; + key_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + return temp; + } +} +inline void CmdRequest_Kv::set_allocated_key(::std::string* key) { + if (key_ != &::google::protobuf::internal::kEmptyString) { + delete key_; + } + if (key) { + set_has_key(); + key_ = key; + } else { + clear_has_key(); + key_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + } +} + +// optional bytes value = 2; +inline bool CmdRequest_Kv::has_value() const { + return (_has_bits_[0] & 0x00000002u) != 0; +} +inline void CmdRequest_Kv::set_has_value() { + _has_bits_[0] |= 0x00000002u; +} +inline void CmdRequest_Kv::clear_has_value() { + _has_bits_[0] &= ~0x00000002u; +} +inline void CmdRequest_Kv::clear_value() { + if (value_ != &::google::protobuf::internal::kEmptyString) { + value_->clear(); + } + clear_has_value(); +} +inline const ::std::string& CmdRequest_Kv::value() const { + return *value_; +} +inline void CmdRequest_Kv::set_value(const ::std::string& value) { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; + } + value_->assign(value); +} +inline void CmdRequest_Kv::set_value(const char* value) { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; + } + value_->assign(value); +} +inline void CmdRequest_Kv::set_value(const void* value, size_t size) { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; + } + value_->assign(reinterpret_cast(value), size); +} +inline ::std::string* CmdRequest_Kv::mutable_value() { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; + } + return value_; +} +inline ::std::string* CmdRequest_Kv::release_value() { + clear_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + return NULL; + } else { + ::std::string* temp = value_; + value_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + return temp; + } +} +inline void CmdRequest_Kv::set_allocated_value(::std::string* value) { + if (value_ != &::google::protobuf::internal::kEmptyString) { + delete value_; + } + if (value) { + set_has_value(); + value_ = value; + } else { + clear_has_value(); + value_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + } +} + +// ------------------------------------------------------------------- + +// CmdRequest_User + +// required bytes ip = 1; +inline bool CmdRequest_User::has_ip() const { + return (_has_bits_[0] & 0x00000001u) != 0; +} +inline void CmdRequest_User::set_has_ip() { + _has_bits_[0] |= 0x00000001u; +} +inline void CmdRequest_User::clear_has_ip() { + _has_bits_[0] &= ~0x00000001u; +} +inline void CmdRequest_User::clear_ip() { + if (ip_ != &::google::protobuf::internal::kEmptyString) { + ip_->clear(); + } + clear_has_ip(); +} +inline const ::std::string& CmdRequest_User::ip() const { + return *ip_; +} +inline void CmdRequest_User::set_ip(const ::std::string& value) { + set_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + ip_ = new ::std::string; + } + ip_->assign(value); +} +inline void CmdRequest_User::set_ip(const char* value) { + set_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + ip_ = new ::std::string; + } + ip_->assign(value); +} +inline void CmdRequest_User::set_ip(const void* value, size_t size) { + set_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + ip_ = new ::std::string; + } + ip_->assign(reinterpret_cast(value), size); +} +inline ::std::string* CmdRequest_User::mutable_ip() { + set_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + ip_ = new ::std::string; + } + return ip_; +} +inline ::std::string* CmdRequest_User::release_ip() { + clear_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + return NULL; + } else { + ::std::string* temp = ip_; + ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + return temp; + } +} +inline void CmdRequest_User::set_allocated_ip(::std::string* ip) { + if (ip_ != &::google::protobuf::internal::kEmptyString) { + delete ip_; + } + if (ip) { + set_has_ip(); + ip_ = ip; + } else { + clear_has_ip(); + ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + } +} + +// required int32 port = 2; +inline bool CmdRequest_User::has_port() const { + return (_has_bits_[0] & 0x00000002u) != 0; +} +inline void CmdRequest_User::set_has_port() { + _has_bits_[0] |= 0x00000002u; +} +inline void CmdRequest_User::clear_has_port() { + _has_bits_[0] &= ~0x00000002u; +} +inline void CmdRequest_User::clear_port() { + port_ = 0; + clear_has_port(); +} +inline ::google::protobuf::int32 CmdRequest_User::port() const { + return port_; +} +inline void CmdRequest_User::set_port(::google::protobuf::int32 value) { + set_has_port(); + port_ = value; +} + +// ------------------------------------------------------------------- + +// CmdRequest_RequestVote + +// required uint64 term = 1; +inline bool CmdRequest_RequestVote::has_term() const { + return (_has_bits_[0] & 0x00000001u) != 0; +} +inline void CmdRequest_RequestVote::set_has_term() { + _has_bits_[0] |= 0x00000001u; +} +inline void CmdRequest_RequestVote::clear_has_term() { + _has_bits_[0] &= ~0x00000001u; +} +inline void CmdRequest_RequestVote::clear_term() { + term_ = GOOGLE_ULONGLONG(0); + clear_has_term(); +} +inline ::google::protobuf::uint64 CmdRequest_RequestVote::term() const { + return term_; +} +inline void CmdRequest_RequestVote::set_term(::google::protobuf::uint64 value) { + set_has_term(); + term_ = value; +} + +// required bytes ip = 2; +inline bool CmdRequest_RequestVote::has_ip() const { + return (_has_bits_[0] & 0x00000002u) != 0; +} +inline void CmdRequest_RequestVote::set_has_ip() { + _has_bits_[0] |= 0x00000002u; +} +inline void CmdRequest_RequestVote::clear_has_ip() { + _has_bits_[0] &= ~0x00000002u; +} +inline void CmdRequest_RequestVote::clear_ip() { + if (ip_ != &::google::protobuf::internal::kEmptyString) { + ip_->clear(); + } + clear_has_ip(); +} +inline const ::std::string& CmdRequest_RequestVote::ip() const { + return *ip_; +} +inline void CmdRequest_RequestVote::set_ip(const ::std::string& value) { + set_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + ip_ = new ::std::string; + } + ip_->assign(value); +} +inline void CmdRequest_RequestVote::set_ip(const char* value) { + set_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + ip_ = new ::std::string; + } + ip_->assign(value); +} +inline void CmdRequest_RequestVote::set_ip(const void* value, size_t size) { + set_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + ip_ = new ::std::string; + } + ip_->assign(reinterpret_cast(value), size); +} +inline ::std::string* CmdRequest_RequestVote::mutable_ip() { + set_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + ip_ = new ::std::string; + } + return ip_; +} +inline ::std::string* CmdRequest_RequestVote::release_ip() { + clear_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + return NULL; + } else { + ::std::string* temp = ip_; + ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + return temp; + } +} +inline void CmdRequest_RequestVote::set_allocated_ip(::std::string* ip) { + if (ip_ != &::google::protobuf::internal::kEmptyString) { + delete ip_; + } + if (ip) { + set_has_ip(); + ip_ = ip; + } else { + clear_has_ip(); + ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + } +} + +// required int32 port = 3; +inline bool CmdRequest_RequestVote::has_port() const { + return (_has_bits_[0] & 0x00000004u) != 0; +} +inline void CmdRequest_RequestVote::set_has_port() { + _has_bits_[0] |= 0x00000004u; +} +inline void CmdRequest_RequestVote::clear_has_port() { + _has_bits_[0] &= ~0x00000004u; +} +inline void CmdRequest_RequestVote::clear_port() { + port_ = 0; + clear_has_port(); +} +inline ::google::protobuf::int32 CmdRequest_RequestVote::port() const { + return port_; +} +inline void CmdRequest_RequestVote::set_port(::google::protobuf::int32 value) { + set_has_port(); + port_ = value; +} + +// required uint64 last_log_index = 4; +inline bool CmdRequest_RequestVote::has_last_log_index() const { + return (_has_bits_[0] & 0x00000008u) != 0; +} +inline void CmdRequest_RequestVote::set_has_last_log_index() { + _has_bits_[0] |= 0x00000008u; +} +inline void CmdRequest_RequestVote::clear_has_last_log_index() { + _has_bits_[0] &= ~0x00000008u; +} +inline void CmdRequest_RequestVote::clear_last_log_index() { + last_log_index_ = GOOGLE_ULONGLONG(0); + clear_has_last_log_index(); +} +inline ::google::protobuf::uint64 CmdRequest_RequestVote::last_log_index() const { + return last_log_index_; +} +inline void CmdRequest_RequestVote::set_last_log_index(::google::protobuf::uint64 value) { + set_has_last_log_index(); + last_log_index_ = value; +} + +// required uint64 last_log_term = 5; +inline bool CmdRequest_RequestVote::has_last_log_term() const { + return (_has_bits_[0] & 0x00000010u) != 0; +} +inline void CmdRequest_RequestVote::set_has_last_log_term() { + _has_bits_[0] |= 0x00000010u; +} +inline void CmdRequest_RequestVote::clear_has_last_log_term() { + _has_bits_[0] &= ~0x00000010u; +} +inline void CmdRequest_RequestVote::clear_last_log_term() { + last_log_term_ = GOOGLE_ULONGLONG(0); + clear_has_last_log_term(); +} +inline ::google::protobuf::uint64 CmdRequest_RequestVote::last_log_term() const { + return last_log_term_; +} +inline void CmdRequest_RequestVote::set_last_log_term(::google::protobuf::uint64 value) { + set_has_last_log_term(); + last_log_term_ = value; +} + +// ------------------------------------------------------------------- + +// CmdRequest_AppendEntries + +// required bytes ip = 1; +inline bool CmdRequest_AppendEntries::has_ip() const { + return (_has_bits_[0] & 0x00000001u) != 0; +} +inline void CmdRequest_AppendEntries::set_has_ip() { + _has_bits_[0] |= 0x00000001u; +} +inline void CmdRequest_AppendEntries::clear_has_ip() { + _has_bits_[0] &= ~0x00000001u; +} +inline void CmdRequest_AppendEntries::clear_ip() { + if (ip_ != &::google::protobuf::internal::kEmptyString) { + ip_->clear(); + } + clear_has_ip(); +} +inline const ::std::string& CmdRequest_AppendEntries::ip() const { + return *ip_; +} +inline void CmdRequest_AppendEntries::set_ip(const ::std::string& value) { + set_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + ip_ = new ::std::string; + } + ip_->assign(value); +} +inline void CmdRequest_AppendEntries::set_ip(const char* value) { + set_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + ip_ = new ::std::string; + } + ip_->assign(value); +} +inline void CmdRequest_AppendEntries::set_ip(const void* value, size_t size) { + set_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + ip_ = new ::std::string; + } + ip_->assign(reinterpret_cast(value), size); +} +inline ::std::string* CmdRequest_AppendEntries::mutable_ip() { + set_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + ip_ = new ::std::string; + } + return ip_; +} +inline ::std::string* CmdRequest_AppendEntries::release_ip() { + clear_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + return NULL; + } else { + ::std::string* temp = ip_; + ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + return temp; + } +} +inline void CmdRequest_AppendEntries::set_allocated_ip(::std::string* ip) { + if (ip_ != &::google::protobuf::internal::kEmptyString) { + delete ip_; + } + if (ip) { + set_has_ip(); + ip_ = ip; + } else { + clear_has_ip(); + ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + } +} + +// required int32 port = 2; +inline bool CmdRequest_AppendEntries::has_port() const { + return (_has_bits_[0] & 0x00000002u) != 0; +} +inline void CmdRequest_AppendEntries::set_has_port() { + _has_bits_[0] |= 0x00000002u; +} +inline void CmdRequest_AppendEntries::clear_has_port() { + _has_bits_[0] &= ~0x00000002u; +} +inline void CmdRequest_AppendEntries::clear_port() { + port_ = 0; + clear_has_port(); +} +inline ::google::protobuf::int32 CmdRequest_AppendEntries::port() const { + return port_; +} +inline void CmdRequest_AppendEntries::set_port(::google::protobuf::int32 value) { + set_has_port(); + port_ = value; +} + +// required uint64 term = 3; +inline bool CmdRequest_AppendEntries::has_term() const { + return (_has_bits_[0] & 0x00000004u) != 0; +} +inline void CmdRequest_AppendEntries::set_has_term() { + _has_bits_[0] |= 0x00000004u; +} +inline void CmdRequest_AppendEntries::clear_has_term() { + _has_bits_[0] &= ~0x00000004u; +} +inline void CmdRequest_AppendEntries::clear_term() { + term_ = GOOGLE_ULONGLONG(0); + clear_has_term(); +} +inline ::google::protobuf::uint64 CmdRequest_AppendEntries::term() const { + return term_; +} +inline void CmdRequest_AppendEntries::set_term(::google::protobuf::uint64 value) { + set_has_term(); + term_ = value; +} + +// required uint64 prev_log_index = 4; +inline bool CmdRequest_AppendEntries::has_prev_log_index() const { + return (_has_bits_[0] & 0x00000008u) != 0; +} +inline void CmdRequest_AppendEntries::set_has_prev_log_index() { + _has_bits_[0] |= 0x00000008u; +} +inline void CmdRequest_AppendEntries::clear_has_prev_log_index() { + _has_bits_[0] &= ~0x00000008u; +} +inline void CmdRequest_AppendEntries::clear_prev_log_index() { + prev_log_index_ = GOOGLE_ULONGLONG(0); + clear_has_prev_log_index(); +} +inline ::google::protobuf::uint64 CmdRequest_AppendEntries::prev_log_index() const { + return prev_log_index_; +} +inline void CmdRequest_AppendEntries::set_prev_log_index(::google::protobuf::uint64 value) { + set_has_prev_log_index(); + prev_log_index_ = value; +} + +// required uint64 prev_log_term = 5; +inline bool CmdRequest_AppendEntries::has_prev_log_term() const { + return (_has_bits_[0] & 0x00000010u) != 0; +} +inline void CmdRequest_AppendEntries::set_has_prev_log_term() { + _has_bits_[0] |= 0x00000010u; +} +inline void CmdRequest_AppendEntries::clear_has_prev_log_term() { + _has_bits_[0] &= ~0x00000010u; +} +inline void CmdRequest_AppendEntries::clear_prev_log_term() { + prev_log_term_ = GOOGLE_ULONGLONG(0); + clear_has_prev_log_term(); +} +inline ::google::protobuf::uint64 CmdRequest_AppendEntries::prev_log_term() const { + return prev_log_term_; +} +inline void CmdRequest_AppendEntries::set_prev_log_term(::google::protobuf::uint64 value) { + set_has_prev_log_term(); + prev_log_term_ = value; +} + +// required uint64 leader_commit = 6; +inline bool CmdRequest_AppendEntries::has_leader_commit() const { + return (_has_bits_[0] & 0x00000020u) != 0; +} +inline void CmdRequest_AppendEntries::set_has_leader_commit() { + _has_bits_[0] |= 0x00000020u; +} +inline void CmdRequest_AppendEntries::clear_has_leader_commit() { + _has_bits_[0] &= ~0x00000020u; +} +inline void CmdRequest_AppendEntries::clear_leader_commit() { + leader_commit_ = GOOGLE_ULONGLONG(0); + clear_has_leader_commit(); +} +inline ::google::protobuf::uint64 CmdRequest_AppendEntries::leader_commit() const { + return leader_commit_; +} +inline void CmdRequest_AppendEntries::set_leader_commit(::google::protobuf::uint64 value) { + set_has_leader_commit(); + leader_commit_ = value; +} + +// repeated .floyd.Entry entries = 7; +inline int CmdRequest_AppendEntries::entries_size() const { + return entries_.size(); +} +inline void CmdRequest_AppendEntries::clear_entries() { + entries_.Clear(); +} +inline const ::floyd::Entry& CmdRequest_AppendEntries::entries(int index) const { + return entries_.Get(index); +} +inline ::floyd::Entry* CmdRequest_AppendEntries::mutable_entries(int index) { + return entries_.Mutable(index); +} +inline ::floyd::Entry* CmdRequest_AppendEntries::add_entries() { + return entries_.Add(); +} +inline const ::google::protobuf::RepeatedPtrField< ::floyd::Entry >& +CmdRequest_AppendEntries::entries() const { + return entries_; +} +inline ::google::protobuf::RepeatedPtrField< ::floyd::Entry >* +CmdRequest_AppendEntries::mutable_entries() { + return &entries_; +} + +// ------------------------------------------------------------------- + +// CmdRequest_ServerStatus + +// required int64 term = 1; +inline bool CmdRequest_ServerStatus::has_term() const { + return (_has_bits_[0] & 0x00000001u) != 0; +} +inline void CmdRequest_ServerStatus::set_has_term() { + _has_bits_[0] |= 0x00000001u; +} +inline void CmdRequest_ServerStatus::clear_has_term() { + _has_bits_[0] &= ~0x00000001u; +} +inline void CmdRequest_ServerStatus::clear_term() { + term_ = GOOGLE_LONGLONG(0); + clear_has_term(); +} +inline ::google::protobuf::int64 CmdRequest_ServerStatus::term() const { + return term_; +} +inline void CmdRequest_ServerStatus::set_term(::google::protobuf::int64 value) { + set_has_term(); + term_ = value; +} + +// required int64 commit_index = 2; +inline bool CmdRequest_ServerStatus::has_commit_index() const { + return (_has_bits_[0] & 0x00000002u) != 0; +} +inline void CmdRequest_ServerStatus::set_has_commit_index() { + _has_bits_[0] |= 0x00000002u; +} +inline void CmdRequest_ServerStatus::clear_has_commit_index() { + _has_bits_[0] &= ~0x00000002u; +} +inline void CmdRequest_ServerStatus::clear_commit_index() { + commit_index_ = GOOGLE_LONGLONG(0); + clear_has_commit_index(); +} +inline ::google::protobuf::int64 CmdRequest_ServerStatus::commit_index() const { + return commit_index_; +} +inline void CmdRequest_ServerStatus::set_commit_index(::google::protobuf::int64 value) { + set_has_commit_index(); + commit_index_ = value; +} + +// optional bytes ip = 3; +inline bool CmdRequest_ServerStatus::has_ip() const { + return (_has_bits_[0] & 0x00000004u) != 0; +} +inline void CmdRequest_ServerStatus::set_has_ip() { + _has_bits_[0] |= 0x00000004u; +} +inline void CmdRequest_ServerStatus::clear_has_ip() { + _has_bits_[0] &= ~0x00000004u; +} +inline void CmdRequest_ServerStatus::clear_ip() { + if (ip_ != &::google::protobuf::internal::kEmptyString) { + ip_->clear(); + } + clear_has_ip(); +} +inline const ::std::string& CmdRequest_ServerStatus::ip() const { + return *ip_; +} +inline void CmdRequest_ServerStatus::set_ip(const ::std::string& value) { + set_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + ip_ = new ::std::string; + } + ip_->assign(value); +} +inline void CmdRequest_ServerStatus::set_ip(const char* value) { + set_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + ip_ = new ::std::string; + } + ip_->assign(value); +} +inline void CmdRequest_ServerStatus::set_ip(const void* value, size_t size) { + set_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + ip_ = new ::std::string; + } + ip_->assign(reinterpret_cast(value), size); +} +inline ::std::string* CmdRequest_ServerStatus::mutable_ip() { + set_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + ip_ = new ::std::string; + } + return ip_; +} +inline ::std::string* CmdRequest_ServerStatus::release_ip() { + clear_has_ip(); + if (ip_ == &::google::protobuf::internal::kEmptyString) { + return NULL; + } else { + ::std::string* temp = ip_; + ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + return temp; + } +} +inline void CmdRequest_ServerStatus::set_allocated_ip(::std::string* ip) { + if (ip_ != &::google::protobuf::internal::kEmptyString) { + delete ip_; + } + if (ip) { + set_has_ip(); + ip_ = ip; + } else { + clear_has_ip(); + ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + } +} + +// optional int32 port = 4; +inline bool CmdRequest_ServerStatus::has_port() const { + return (_has_bits_[0] & 0x00000008u) != 0; +} +inline void CmdRequest_ServerStatus::set_has_port() { + _has_bits_[0] |= 0x00000008u; +} +inline void CmdRequest_ServerStatus::clear_has_port() { + _has_bits_[0] &= ~0x00000008u; +} +inline void CmdRequest_ServerStatus::clear_port() { + port_ = 0; + clear_has_port(); +} +inline ::google::protobuf::int32 CmdRequest_ServerStatus::port() const { + return port_; +} +inline void CmdRequest_ServerStatus::set_port(::google::protobuf::int32 value) { + set_has_port(); + port_ = value; +} + +// ------------------------------------------------------------------- + +// CmdRequest + +// required .floyd.Type type = 1; +inline bool CmdRequest::has_type() const { + return (_has_bits_[0] & 0x00000001u) != 0; +} +inline void CmdRequest::set_has_type() { + _has_bits_[0] |= 0x00000001u; +} +inline void CmdRequest::clear_has_type() { + _has_bits_[0] &= ~0x00000001u; +} +inline void CmdRequest::clear_type() { + type_ = 0; + clear_has_type(); +} +inline ::floyd::Type CmdRequest::type() const { + return static_cast< ::floyd::Type >(type_); +} +inline void CmdRequest::set_type(::floyd::Type value) { + assert(::floyd::Type_IsValid(value)); + set_has_type(); + type_ = value; +} + +// optional .floyd.CmdRequest.Kv kv = 2; +inline bool CmdRequest::has_kv() const { + return (_has_bits_[0] & 0x00000002u) != 0; +} +inline void CmdRequest::set_has_kv() { + _has_bits_[0] |= 0x00000002u; +} +inline void CmdRequest::clear_has_kv() { + _has_bits_[0] &= ~0x00000002u; +} +inline void CmdRequest::clear_kv() { + if (kv_ != NULL) kv_->::floyd::CmdRequest_Kv::Clear(); + clear_has_kv(); +} +inline const ::floyd::CmdRequest_Kv& CmdRequest::kv() const { + return kv_ != NULL ? *kv_ : *default_instance_->kv_; +} +inline ::floyd::CmdRequest_Kv* CmdRequest::mutable_kv() { + set_has_kv(); + if (kv_ == NULL) kv_ = new ::floyd::CmdRequest_Kv; + return kv_; +} +inline ::floyd::CmdRequest_Kv* CmdRequest::release_kv() { + clear_has_kv(); + ::floyd::CmdRequest_Kv* temp = kv_; + kv_ = NULL; + return temp; +} +inline void CmdRequest::set_allocated_kv(::floyd::CmdRequest_Kv* kv) { + delete kv_; + kv_ = kv; + if (kv) { + set_has_kv(); + } else { + clear_has_kv(); + } +} + +// optional .floyd.CmdRequest.User user = 3; +inline bool CmdRequest::has_user() const { + return (_has_bits_[0] & 0x00000004u) != 0; +} +inline void CmdRequest::set_has_user() { + _has_bits_[0] |= 0x00000004u; +} +inline void CmdRequest::clear_has_user() { + _has_bits_[0] &= ~0x00000004u; +} +inline void CmdRequest::clear_user() { + if (user_ != NULL) user_->::floyd::CmdRequest_User::Clear(); + clear_has_user(); +} +inline const ::floyd::CmdRequest_User& CmdRequest::user() const { + return user_ != NULL ? *user_ : *default_instance_->user_; +} +inline ::floyd::CmdRequest_User* CmdRequest::mutable_user() { + set_has_user(); + if (user_ == NULL) user_ = new ::floyd::CmdRequest_User; + return user_; +} +inline ::floyd::CmdRequest_User* CmdRequest::release_user() { + clear_has_user(); + ::floyd::CmdRequest_User* temp = user_; + user_ = NULL; + return temp; +} +inline void CmdRequest::set_allocated_user(::floyd::CmdRequest_User* user) { + delete user_; + user_ = user; + if (user) { + set_has_user(); + } else { + clear_has_user(); + } +} + +// optional .floyd.CmdRequest.RequestVote request_vote = 4; +inline bool CmdRequest::has_request_vote() const { + return (_has_bits_[0] & 0x00000008u) != 0; +} +inline void CmdRequest::set_has_request_vote() { + _has_bits_[0] |= 0x00000008u; +} +inline void CmdRequest::clear_has_request_vote() { + _has_bits_[0] &= ~0x00000008u; +} +inline void CmdRequest::clear_request_vote() { + if (request_vote_ != NULL) request_vote_->::floyd::CmdRequest_RequestVote::Clear(); + clear_has_request_vote(); +} +inline const ::floyd::CmdRequest_RequestVote& CmdRequest::request_vote() const { + return request_vote_ != NULL ? *request_vote_ : *default_instance_->request_vote_; +} +inline ::floyd::CmdRequest_RequestVote* CmdRequest::mutable_request_vote() { + set_has_request_vote(); + if (request_vote_ == NULL) request_vote_ = new ::floyd::CmdRequest_RequestVote; + return request_vote_; +} +inline ::floyd::CmdRequest_RequestVote* CmdRequest::release_request_vote() { + clear_has_request_vote(); + ::floyd::CmdRequest_RequestVote* temp = request_vote_; + request_vote_ = NULL; + return temp; +} +inline void CmdRequest::set_allocated_request_vote(::floyd::CmdRequest_RequestVote* request_vote) { + delete request_vote_; + request_vote_ = request_vote; + if (request_vote) { + set_has_request_vote(); + } else { + clear_has_request_vote(); + } +} + +// optional .floyd.CmdRequest.AppendEntries append_entries = 5; +inline bool CmdRequest::has_append_entries() const { + return (_has_bits_[0] & 0x00000010u) != 0; +} +inline void CmdRequest::set_has_append_entries() { + _has_bits_[0] |= 0x00000010u; +} +inline void CmdRequest::clear_has_append_entries() { + _has_bits_[0] &= ~0x00000010u; +} +inline void CmdRequest::clear_append_entries() { + if (append_entries_ != NULL) append_entries_->::floyd::CmdRequest_AppendEntries::Clear(); + clear_has_append_entries(); +} +inline const ::floyd::CmdRequest_AppendEntries& CmdRequest::append_entries() const { + return append_entries_ != NULL ? *append_entries_ : *default_instance_->append_entries_; +} +inline ::floyd::CmdRequest_AppendEntries* CmdRequest::mutable_append_entries() { + set_has_append_entries(); + if (append_entries_ == NULL) append_entries_ = new ::floyd::CmdRequest_AppendEntries; + return append_entries_; +} +inline ::floyd::CmdRequest_AppendEntries* CmdRequest::release_append_entries() { + clear_has_append_entries(); + ::floyd::CmdRequest_AppendEntries* temp = append_entries_; + append_entries_ = NULL; + return temp; +} +inline void CmdRequest::set_allocated_append_entries(::floyd::CmdRequest_AppendEntries* append_entries) { + delete append_entries_; + append_entries_ = append_entries; + if (append_entries) { + set_has_append_entries(); + } else { + clear_has_append_entries(); + } +} + +// optional .floyd.CmdRequest.ServerStatus server_status = 6; +inline bool CmdRequest::has_server_status() const { + return (_has_bits_[0] & 0x00000020u) != 0; +} +inline void CmdRequest::set_has_server_status() { + _has_bits_[0] |= 0x00000020u; +} +inline void CmdRequest::clear_has_server_status() { + _has_bits_[0] &= ~0x00000020u; +} +inline void CmdRequest::clear_server_status() { + if (server_status_ != NULL) server_status_->::floyd::CmdRequest_ServerStatus::Clear(); + clear_has_server_status(); +} +inline const ::floyd::CmdRequest_ServerStatus& CmdRequest::server_status() const { + return server_status_ != NULL ? *server_status_ : *default_instance_->server_status_; +} +inline ::floyd::CmdRequest_ServerStatus* CmdRequest::mutable_server_status() { + set_has_server_status(); + if (server_status_ == NULL) server_status_ = new ::floyd::CmdRequest_ServerStatus; + return server_status_; +} +inline ::floyd::CmdRequest_ServerStatus* CmdRequest::release_server_status() { + clear_has_server_status(); + ::floyd::CmdRequest_ServerStatus* temp = server_status_; + server_status_ = NULL; + return temp; +} +inline void CmdRequest::set_allocated_server_status(::floyd::CmdRequest_ServerStatus* server_status) { + delete server_status_; + server_status_ = server_status; + if (server_status) { + set_has_server_status(); + } else { + clear_has_server_status(); + } +} + +// ------------------------------------------------------------------- + +// CmdResponse_Kv + +// optional bytes value = 1; +inline bool CmdResponse_Kv::has_value() const { + return (_has_bits_[0] & 0x00000001u) != 0; +} +inline void CmdResponse_Kv::set_has_value() { + _has_bits_[0] |= 0x00000001u; +} +inline void CmdResponse_Kv::clear_has_value() { + _has_bits_[0] &= ~0x00000001u; +} +inline void CmdResponse_Kv::clear_value() { + if (value_ != &::google::protobuf::internal::kEmptyString) { + value_->clear(); + } + clear_has_value(); +} +inline const ::std::string& CmdResponse_Kv::value() const { + return *value_; +} +inline void CmdResponse_Kv::set_value(const ::std::string& value) { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; + } + value_->assign(value); +} +inline void CmdResponse_Kv::set_value(const char* value) { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; + } + value_->assign(value); +} +inline void CmdResponse_Kv::set_value(const void* value, size_t size) { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; + } + value_->assign(reinterpret_cast(value), size); +} +inline ::std::string* CmdResponse_Kv::mutable_value() { + set_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + value_ = new ::std::string; + } + return value_; +} +inline ::std::string* CmdResponse_Kv::release_value() { + clear_has_value(); + if (value_ == &::google::protobuf::internal::kEmptyString) { + return NULL; + } else { + ::std::string* temp = value_; + value_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + return temp; + } +} +inline void CmdResponse_Kv::set_allocated_value(::std::string* value) { + if (value_ != &::google::protobuf::internal::kEmptyString) { + delete value_; + } + if (value) { + set_has_value(); + value_ = value; + } else { + clear_has_value(); + value_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + } +} + +// ------------------------------------------------------------------- + +// CmdResponse_Kvs + +// repeated .floyd.CmdResponse.Kv kv = 1; +inline int CmdResponse_Kvs::kv_size() const { + return kv_.size(); +} +inline void CmdResponse_Kvs::clear_kv() { + kv_.Clear(); +} +inline const ::floyd::CmdResponse_Kv& CmdResponse_Kvs::kv(int index) const { + return kv_.Get(index); +} +inline ::floyd::CmdResponse_Kv* CmdResponse_Kvs::mutable_kv(int index) { + return kv_.Mutable(index); +} +inline ::floyd::CmdResponse_Kv* CmdResponse_Kvs::add_kv() { + return kv_.Add(); +} +inline const ::google::protobuf::RepeatedPtrField< ::floyd::CmdResponse_Kv >& +CmdResponse_Kvs::kv() const { + return kv_; +} +inline ::google::protobuf::RepeatedPtrField< ::floyd::CmdResponse_Kv >* +CmdResponse_Kvs::mutable_kv() { + return &kv_; +} + +// ------------------------------------------------------------------- + +// CmdResponse_RequestVoteResponse + +// required uint64 term = 1; +inline bool CmdResponse_RequestVoteResponse::has_term() const { + return (_has_bits_[0] & 0x00000001u) != 0; +} +inline void CmdResponse_RequestVoteResponse::set_has_term() { + _has_bits_[0] |= 0x00000001u; +} +inline void CmdResponse_RequestVoteResponse::clear_has_term() { + _has_bits_[0] &= ~0x00000001u; +} +inline void CmdResponse_RequestVoteResponse::clear_term() { + term_ = GOOGLE_ULONGLONG(0); + clear_has_term(); +} +inline ::google::protobuf::uint64 CmdResponse_RequestVoteResponse::term() const { + return term_; +} +inline void CmdResponse_RequestVoteResponse::set_term(::google::protobuf::uint64 value) { + set_has_term(); + term_ = value; +} + +// required bool vote_granted = 2; +inline bool CmdResponse_RequestVoteResponse::has_vote_granted() const { + return (_has_bits_[0] & 0x00000002u) != 0; +} +inline void CmdResponse_RequestVoteResponse::set_has_vote_granted() { + _has_bits_[0] |= 0x00000002u; +} +inline void CmdResponse_RequestVoteResponse::clear_has_vote_granted() { + _has_bits_[0] &= ~0x00000002u; +} +inline void CmdResponse_RequestVoteResponse::clear_vote_granted() { + vote_granted_ = false; + clear_has_vote_granted(); +} +inline bool CmdResponse_RequestVoteResponse::vote_granted() const { + return vote_granted_; +} +inline void CmdResponse_RequestVoteResponse::set_vote_granted(bool value) { + set_has_vote_granted(); + vote_granted_ = value; +} + +// ------------------------------------------------------------------- + +// CmdResponse_AppendEntriesResponse + +// required uint64 term = 1; +inline bool CmdResponse_AppendEntriesResponse::has_term() const { + return (_has_bits_[0] & 0x00000001u) != 0; +} +inline void CmdResponse_AppendEntriesResponse::set_has_term() { + _has_bits_[0] |= 0x00000001u; +} +inline void CmdResponse_AppendEntriesResponse::clear_has_term() { + _has_bits_[0] &= ~0x00000001u; +} +inline void CmdResponse_AppendEntriesResponse::clear_term() { + term_ = GOOGLE_ULONGLONG(0); + clear_has_term(); +} +inline ::google::protobuf::uint64 CmdResponse_AppendEntriesResponse::term() const { + return term_; +} +inline void CmdResponse_AppendEntriesResponse::set_term(::google::protobuf::uint64 value) { + set_has_term(); + term_ = value; +} + +// required bool success = 2; +inline bool CmdResponse_AppendEntriesResponse::has_success() const { + return (_has_bits_[0] & 0x00000002u) != 0; +} +inline void CmdResponse_AppendEntriesResponse::set_has_success() { + _has_bits_[0] |= 0x00000002u; +} +inline void CmdResponse_AppendEntriesResponse::clear_has_success() { + _has_bits_[0] &= ~0x00000002u; +} +inline void CmdResponse_AppendEntriesResponse::clear_success() { + success_ = false; + clear_has_success(); +} +inline bool CmdResponse_AppendEntriesResponse::success() const { + return success_; +} +inline void CmdResponse_AppendEntriesResponse::set_success(bool value) { + set_has_success(); + success_ = value; +} + +// optional uint64 last_log_index = 3; +inline bool CmdResponse_AppendEntriesResponse::has_last_log_index() const { + return (_has_bits_[0] & 0x00000004u) != 0; +} +inline void CmdResponse_AppendEntriesResponse::set_has_last_log_index() { + _has_bits_[0] |= 0x00000004u; +} +inline void CmdResponse_AppendEntriesResponse::clear_has_last_log_index() { + _has_bits_[0] &= ~0x00000004u; +} +inline void CmdResponse_AppendEntriesResponse::clear_last_log_index() { + last_log_index_ = GOOGLE_ULONGLONG(0); + clear_has_last_log_index(); +} +inline ::google::protobuf::uint64 CmdResponse_AppendEntriesResponse::last_log_index() const { + return last_log_index_; +} +inline void CmdResponse_AppendEntriesResponse::set_last_log_index(::google::protobuf::uint64 value) { + set_has_last_log_index(); + last_log_index_ = value; +} + +// ------------------------------------------------------------------- + +// CmdResponse_ServerStatus + +// required uint64 term = 1; +inline bool CmdResponse_ServerStatus::has_term() const { + return (_has_bits_[0] & 0x00000001u) != 0; +} +inline void CmdResponse_ServerStatus::set_has_term() { + _has_bits_[0] |= 0x00000001u; +} +inline void CmdResponse_ServerStatus::clear_has_term() { + _has_bits_[0] &= ~0x00000001u; +} +inline void CmdResponse_ServerStatus::clear_term() { + term_ = GOOGLE_ULONGLONG(0); + clear_has_term(); +} +inline ::google::protobuf::uint64 CmdResponse_ServerStatus::term() const { + return term_; +} +inline void CmdResponse_ServerStatus::set_term(::google::protobuf::uint64 value) { + set_has_term(); + term_ = value; +} + +// required uint64 commit_index = 2; +inline bool CmdResponse_ServerStatus::has_commit_index() const { + return (_has_bits_[0] & 0x00000002u) != 0; +} +inline void CmdResponse_ServerStatus::set_has_commit_index() { + _has_bits_[0] |= 0x00000002u; +} +inline void CmdResponse_ServerStatus::clear_has_commit_index() { + _has_bits_[0] &= ~0x00000002u; +} +inline void CmdResponse_ServerStatus::clear_commit_index() { + commit_index_ = GOOGLE_ULONGLONG(0); + clear_has_commit_index(); +} +inline ::google::protobuf::uint64 CmdResponse_ServerStatus::commit_index() const { + return commit_index_; +} +inline void CmdResponse_ServerStatus::set_commit_index(::google::protobuf::uint64 value) { + set_has_commit_index(); + commit_index_ = value; +} + +// required bytes role = 3; +inline bool CmdResponse_ServerStatus::has_role() const { + return (_has_bits_[0] & 0x00000004u) != 0; +} +inline void CmdResponse_ServerStatus::set_has_role() { + _has_bits_[0] |= 0x00000004u; +} +inline void CmdResponse_ServerStatus::clear_has_role() { + _has_bits_[0] &= ~0x00000004u; +} +inline void CmdResponse_ServerStatus::clear_role() { + if (role_ != &::google::protobuf::internal::kEmptyString) { + role_->clear(); + } + clear_has_role(); +} +inline const ::std::string& CmdResponse_ServerStatus::role() const { + return *role_; +} +inline void CmdResponse_ServerStatus::set_role(const ::std::string& value) { + set_has_role(); + if (role_ == &::google::protobuf::internal::kEmptyString) { + role_ = new ::std::string; + } + role_->assign(value); +} +inline void CmdResponse_ServerStatus::set_role(const char* value) { + set_has_role(); + if (role_ == &::google::protobuf::internal::kEmptyString) { + role_ = new ::std::string; + } + role_->assign(value); +} +inline void CmdResponse_ServerStatus::set_role(const void* value, size_t size) { + set_has_role(); + if (role_ == &::google::protobuf::internal::kEmptyString) { + role_ = new ::std::string; + } + role_->assign(reinterpret_cast(value), size); +} +inline ::std::string* CmdResponse_ServerStatus::mutable_role() { + set_has_role(); + if (role_ == &::google::protobuf::internal::kEmptyString) { + role_ = new ::std::string; + } + return role_; +} +inline ::std::string* CmdResponse_ServerStatus::release_role() { + clear_has_role(); + if (role_ == &::google::protobuf::internal::kEmptyString) { + return NULL; + } else { + ::std::string* temp = role_; + role_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + return temp; + } +} +inline void CmdResponse_ServerStatus::set_allocated_role(::std::string* role) { + if (role_ != &::google::protobuf::internal::kEmptyString) { + delete role_; + } + if (role) { + set_has_role(); + role_ = role; + } else { + clear_has_role(); + role_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + } +} + +// optional bytes leader_ip = 4; +inline bool CmdResponse_ServerStatus::has_leader_ip() const { + return (_has_bits_[0] & 0x00000008u) != 0; +} +inline void CmdResponse_ServerStatus::set_has_leader_ip() { + _has_bits_[0] |= 0x00000008u; +} +inline void CmdResponse_ServerStatus::clear_has_leader_ip() { + _has_bits_[0] &= ~0x00000008u; +} +inline void CmdResponse_ServerStatus::clear_leader_ip() { + if (leader_ip_ != &::google::protobuf::internal::kEmptyString) { + leader_ip_->clear(); + } + clear_has_leader_ip(); +} +inline const ::std::string& CmdResponse_ServerStatus::leader_ip() const { + return *leader_ip_; +} +inline void CmdResponse_ServerStatus::set_leader_ip(const ::std::string& value) { + set_has_leader_ip(); + if (leader_ip_ == &::google::protobuf::internal::kEmptyString) { + leader_ip_ = new ::std::string; + } + leader_ip_->assign(value); +} +inline void CmdResponse_ServerStatus::set_leader_ip(const char* value) { + set_has_leader_ip(); + if (leader_ip_ == &::google::protobuf::internal::kEmptyString) { + leader_ip_ = new ::std::string; + } + leader_ip_->assign(value); +} +inline void CmdResponse_ServerStatus::set_leader_ip(const void* value, size_t size) { + set_has_leader_ip(); + if (leader_ip_ == &::google::protobuf::internal::kEmptyString) { + leader_ip_ = new ::std::string; + } + leader_ip_->assign(reinterpret_cast(value), size); +} +inline ::std::string* CmdResponse_ServerStatus::mutable_leader_ip() { + set_has_leader_ip(); + if (leader_ip_ == &::google::protobuf::internal::kEmptyString) { + leader_ip_ = new ::std::string; + } + return leader_ip_; +} +inline ::std::string* CmdResponse_ServerStatus::release_leader_ip() { + clear_has_leader_ip(); + if (leader_ip_ == &::google::protobuf::internal::kEmptyString) { + return NULL; + } else { + ::std::string* temp = leader_ip_; + leader_ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + return temp; + } +} +inline void CmdResponse_ServerStatus::set_allocated_leader_ip(::std::string* leader_ip) { + if (leader_ip_ != &::google::protobuf::internal::kEmptyString) { + delete leader_ip_; + } + if (leader_ip) { + set_has_leader_ip(); + leader_ip_ = leader_ip; + } else { + clear_has_leader_ip(); + leader_ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + } +} + +// optional int32 leader_port = 5; +inline bool CmdResponse_ServerStatus::has_leader_port() const { + return (_has_bits_[0] & 0x00000010u) != 0; +} +inline void CmdResponse_ServerStatus::set_has_leader_port() { + _has_bits_[0] |= 0x00000010u; +} +inline void CmdResponse_ServerStatus::clear_has_leader_port() { + _has_bits_[0] &= ~0x00000010u; +} +inline void CmdResponse_ServerStatus::clear_leader_port() { + leader_port_ = 0; + clear_has_leader_port(); +} +inline ::google::protobuf::int32 CmdResponse_ServerStatus::leader_port() const { + return leader_port_; +} +inline void CmdResponse_ServerStatus::set_leader_port(::google::protobuf::int32 value) { + set_has_leader_port(); + leader_port_ = value; +} + +// optional bytes voted_for_ip = 6; +inline bool CmdResponse_ServerStatus::has_voted_for_ip() const { + return (_has_bits_[0] & 0x00000020u) != 0; +} +inline void CmdResponse_ServerStatus::set_has_voted_for_ip() { + _has_bits_[0] |= 0x00000020u; +} +inline void CmdResponse_ServerStatus::clear_has_voted_for_ip() { + _has_bits_[0] &= ~0x00000020u; +} +inline void CmdResponse_ServerStatus::clear_voted_for_ip() { + if (voted_for_ip_ != &::google::protobuf::internal::kEmptyString) { + voted_for_ip_->clear(); + } + clear_has_voted_for_ip(); +} +inline const ::std::string& CmdResponse_ServerStatus::voted_for_ip() const { + return *voted_for_ip_; +} +inline void CmdResponse_ServerStatus::set_voted_for_ip(const ::std::string& value) { + set_has_voted_for_ip(); + if (voted_for_ip_ == &::google::protobuf::internal::kEmptyString) { + voted_for_ip_ = new ::std::string; + } + voted_for_ip_->assign(value); +} +inline void CmdResponse_ServerStatus::set_voted_for_ip(const char* value) { + set_has_voted_for_ip(); + if (voted_for_ip_ == &::google::protobuf::internal::kEmptyString) { + voted_for_ip_ = new ::std::string; + } + voted_for_ip_->assign(value); +} +inline void CmdResponse_ServerStatus::set_voted_for_ip(const void* value, size_t size) { + set_has_voted_for_ip(); + if (voted_for_ip_ == &::google::protobuf::internal::kEmptyString) { + voted_for_ip_ = new ::std::string; + } + voted_for_ip_->assign(reinterpret_cast(value), size); +} +inline ::std::string* CmdResponse_ServerStatus::mutable_voted_for_ip() { + set_has_voted_for_ip(); + if (voted_for_ip_ == &::google::protobuf::internal::kEmptyString) { + voted_for_ip_ = new ::std::string; + } + return voted_for_ip_; +} +inline ::std::string* CmdResponse_ServerStatus::release_voted_for_ip() { + clear_has_voted_for_ip(); + if (voted_for_ip_ == &::google::protobuf::internal::kEmptyString) { + return NULL; + } else { + ::std::string* temp = voted_for_ip_; + voted_for_ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + return temp; + } +} +inline void CmdResponse_ServerStatus::set_allocated_voted_for_ip(::std::string* voted_for_ip) { + if (voted_for_ip_ != &::google::protobuf::internal::kEmptyString) { + delete voted_for_ip_; + } + if (voted_for_ip) { + set_has_voted_for_ip(); + voted_for_ip_ = voted_for_ip; + } else { + clear_has_voted_for_ip(); + voted_for_ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + } +} + +// optional int32 voted_for_port = 7; +inline bool CmdResponse_ServerStatus::has_voted_for_port() const { + return (_has_bits_[0] & 0x00000040u) != 0; +} +inline void CmdResponse_ServerStatus::set_has_voted_for_port() { + _has_bits_[0] |= 0x00000040u; +} +inline void CmdResponse_ServerStatus::clear_has_voted_for_port() { + _has_bits_[0] &= ~0x00000040u; +} +inline void CmdResponse_ServerStatus::clear_voted_for_port() { + voted_for_port_ = 0; + clear_has_voted_for_port(); +} +inline ::google::protobuf::int32 CmdResponse_ServerStatus::voted_for_port() const { + return voted_for_port_; +} +inline void CmdResponse_ServerStatus::set_voted_for_port(::google::protobuf::int32 value) { + set_has_voted_for_port(); + voted_for_port_ = value; +} + +// optional uint64 last_log_term = 8; +inline bool CmdResponse_ServerStatus::has_last_log_term() const { + return (_has_bits_[0] & 0x00000080u) != 0; +} +inline void CmdResponse_ServerStatus::set_has_last_log_term() { + _has_bits_[0] |= 0x00000080u; +} +inline void CmdResponse_ServerStatus::clear_has_last_log_term() { + _has_bits_[0] &= ~0x00000080u; +} +inline void CmdResponse_ServerStatus::clear_last_log_term() { + last_log_term_ = GOOGLE_ULONGLONG(0); + clear_has_last_log_term(); +} +inline ::google::protobuf::uint64 CmdResponse_ServerStatus::last_log_term() const { + return last_log_term_; +} +inline void CmdResponse_ServerStatus::set_last_log_term(::google::protobuf::uint64 value) { + set_has_last_log_term(); + last_log_term_ = value; +} + +// optional uint64 last_log_index = 9; +inline bool CmdResponse_ServerStatus::has_last_log_index() const { + return (_has_bits_[0] & 0x00000100u) != 0; +} +inline void CmdResponse_ServerStatus::set_has_last_log_index() { + _has_bits_[0] |= 0x00000100u; +} +inline void CmdResponse_ServerStatus::clear_has_last_log_index() { + _has_bits_[0] &= ~0x00000100u; +} +inline void CmdResponse_ServerStatus::clear_last_log_index() { + last_log_index_ = GOOGLE_ULONGLONG(0); + clear_has_last_log_index(); +} +inline ::google::protobuf::uint64 CmdResponse_ServerStatus::last_log_index() const { + return last_log_index_; +} +inline void CmdResponse_ServerStatus::set_last_log_index(::google::protobuf::uint64 value) { + set_has_last_log_index(); + last_log_index_ = value; +} + +// optional uint64 last_applied = 10; +inline bool CmdResponse_ServerStatus::has_last_applied() const { + return (_has_bits_[0] & 0x00000200u) != 0; +} +inline void CmdResponse_ServerStatus::set_has_last_applied() { + _has_bits_[0] |= 0x00000200u; +} +inline void CmdResponse_ServerStatus::clear_has_last_applied() { + _has_bits_[0] &= ~0x00000200u; +} +inline void CmdResponse_ServerStatus::clear_last_applied() { + last_applied_ = GOOGLE_ULONGLONG(0); + clear_has_last_applied(); +} +inline ::google::protobuf::uint64 CmdResponse_ServerStatus::last_applied() const { + return last_applied_; +} +inline void CmdResponse_ServerStatus::set_last_applied(::google::protobuf::uint64 value) { + set_has_last_applied(); + last_applied_ = value; +} + +// ------------------------------------------------------------------- + +// CmdResponse + +// required .floyd.Type type = 1; +inline bool CmdResponse::has_type() const { + return (_has_bits_[0] & 0x00000001u) != 0; +} +inline void CmdResponse::set_has_type() { + _has_bits_[0] |= 0x00000001u; +} +inline void CmdResponse::clear_has_type() { + _has_bits_[0] &= ~0x00000001u; +} +inline void CmdResponse::clear_type() { + type_ = 0; + clear_has_type(); +} +inline ::floyd::Type CmdResponse::type() const { + return static_cast< ::floyd::Type >(type_); +} +inline void CmdResponse::set_type(::floyd::Type value) { + assert(::floyd::Type_IsValid(value)); + set_has_type(); + type_ = value; +} + +// optional .floyd.StatusCode code = 2; +inline bool CmdResponse::has_code() const { + return (_has_bits_[0] & 0x00000002u) != 0; +} +inline void CmdResponse::set_has_code() { + _has_bits_[0] |= 0x00000002u; +} +inline void CmdResponse::clear_has_code() { + _has_bits_[0] &= ~0x00000002u; +} +inline void CmdResponse::clear_code() { + code_ = 0; + clear_has_code(); +} +inline ::floyd::StatusCode CmdResponse::code() const { + return static_cast< ::floyd::StatusCode >(code_); +} +inline void CmdResponse::set_code(::floyd::StatusCode value) { + assert(::floyd::StatusCode_IsValid(value)); + set_has_code(); + code_ = value; +} + +// optional bytes msg = 3; +inline bool CmdResponse::has_msg() const { + return (_has_bits_[0] & 0x00000004u) != 0; +} +inline void CmdResponse::set_has_msg() { + _has_bits_[0] |= 0x00000004u; +} +inline void CmdResponse::clear_has_msg() { + _has_bits_[0] &= ~0x00000004u; +} +inline void CmdResponse::clear_msg() { + if (msg_ != &::google::protobuf::internal::kEmptyString) { + msg_->clear(); + } + clear_has_msg(); +} +inline const ::std::string& CmdResponse::msg() const { + return *msg_; +} +inline void CmdResponse::set_msg(const ::std::string& value) { + set_has_msg(); + if (msg_ == &::google::protobuf::internal::kEmptyString) { + msg_ = new ::std::string; + } + msg_->assign(value); +} +inline void CmdResponse::set_msg(const char* value) { + set_has_msg(); + if (msg_ == &::google::protobuf::internal::kEmptyString) { + msg_ = new ::std::string; + } + msg_->assign(value); +} +inline void CmdResponse::set_msg(const void* value, size_t size) { + set_has_msg(); + if (msg_ == &::google::protobuf::internal::kEmptyString) { + msg_ = new ::std::string; + } + msg_->assign(reinterpret_cast(value), size); +} +inline ::std::string* CmdResponse::mutable_msg() { + set_has_msg(); + if (msg_ == &::google::protobuf::internal::kEmptyString) { + msg_ = new ::std::string; + } + return msg_; +} +inline ::std::string* CmdResponse::release_msg() { + clear_has_msg(); + if (msg_ == &::google::protobuf::internal::kEmptyString) { + return NULL; + } else { + ::std::string* temp = msg_; + msg_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + return temp; + } +} +inline void CmdResponse::set_allocated_msg(::std::string* msg) { + if (msg_ != &::google::protobuf::internal::kEmptyString) { + delete msg_; + } + if (msg) { + set_has_msg(); + msg_ = msg; + } else { + clear_has_msg(); + msg_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); + } +} + +// optional .floyd.CmdResponse.Kv kv = 4; +inline bool CmdResponse::has_kv() const { + return (_has_bits_[0] & 0x00000008u) != 0; +} +inline void CmdResponse::set_has_kv() { + _has_bits_[0] |= 0x00000008u; +} +inline void CmdResponse::clear_has_kv() { + _has_bits_[0] &= ~0x00000008u; +} +inline void CmdResponse::clear_kv() { + if (kv_ != NULL) kv_->::floyd::CmdResponse_Kv::Clear(); + clear_has_kv(); +} +inline const ::floyd::CmdResponse_Kv& CmdResponse::kv() const { + return kv_ != NULL ? *kv_ : *default_instance_->kv_; +} +inline ::floyd::CmdResponse_Kv* CmdResponse::mutable_kv() { + set_has_kv(); + if (kv_ == NULL) kv_ = new ::floyd::CmdResponse_Kv; + return kv_; +} +inline ::floyd::CmdResponse_Kv* CmdResponse::release_kv() { + clear_has_kv(); + ::floyd::CmdResponse_Kv* temp = kv_; + kv_ = NULL; + return temp; +} +inline void CmdResponse::set_allocated_kv(::floyd::CmdResponse_Kv* kv) { + delete kv_; + kv_ = kv; + if (kv) { + set_has_kv(); + } else { + clear_has_kv(); + } +} + +// optional .floyd.CmdResponse.Kvs kvs = 5; +inline bool CmdResponse::has_kvs() const { + return (_has_bits_[0] & 0x00000010u) != 0; +} +inline void CmdResponse::set_has_kvs() { + _has_bits_[0] |= 0x00000010u; +} +inline void CmdResponse::clear_has_kvs() { + _has_bits_[0] &= ~0x00000010u; +} +inline void CmdResponse::clear_kvs() { + if (kvs_ != NULL) kvs_->::floyd::CmdResponse_Kvs::Clear(); + clear_has_kvs(); +} +inline const ::floyd::CmdResponse_Kvs& CmdResponse::kvs() const { + return kvs_ != NULL ? *kvs_ : *default_instance_->kvs_; +} +inline ::floyd::CmdResponse_Kvs* CmdResponse::mutable_kvs() { + set_has_kvs(); + if (kvs_ == NULL) kvs_ = new ::floyd::CmdResponse_Kvs; + return kvs_; +} +inline ::floyd::CmdResponse_Kvs* CmdResponse::release_kvs() { + clear_has_kvs(); + ::floyd::CmdResponse_Kvs* temp = kvs_; + kvs_ = NULL; + return temp; +} +inline void CmdResponse::set_allocated_kvs(::floyd::CmdResponse_Kvs* kvs) { + delete kvs_; + kvs_ = kvs; + if (kvs) { + set_has_kvs(); + } else { + clear_has_kvs(); + } +} + +// optional .floyd.CmdResponse.RequestVoteResponse request_vote_res = 6; +inline bool CmdResponse::has_request_vote_res() const { + return (_has_bits_[0] & 0x00000020u) != 0; +} +inline void CmdResponse::set_has_request_vote_res() { + _has_bits_[0] |= 0x00000020u; +} +inline void CmdResponse::clear_has_request_vote_res() { + _has_bits_[0] &= ~0x00000020u; +} +inline void CmdResponse::clear_request_vote_res() { + if (request_vote_res_ != NULL) request_vote_res_->::floyd::CmdResponse_RequestVoteResponse::Clear(); + clear_has_request_vote_res(); +} +inline const ::floyd::CmdResponse_RequestVoteResponse& CmdResponse::request_vote_res() const { + return request_vote_res_ != NULL ? *request_vote_res_ : *default_instance_->request_vote_res_; +} +inline ::floyd::CmdResponse_RequestVoteResponse* CmdResponse::mutable_request_vote_res() { + set_has_request_vote_res(); + if (request_vote_res_ == NULL) request_vote_res_ = new ::floyd::CmdResponse_RequestVoteResponse; + return request_vote_res_; +} +inline ::floyd::CmdResponse_RequestVoteResponse* CmdResponse::release_request_vote_res() { + clear_has_request_vote_res(); + ::floyd::CmdResponse_RequestVoteResponse* temp = request_vote_res_; + request_vote_res_ = NULL; + return temp; +} +inline void CmdResponse::set_allocated_request_vote_res(::floyd::CmdResponse_RequestVoteResponse* request_vote_res) { + delete request_vote_res_; + request_vote_res_ = request_vote_res; + if (request_vote_res) { + set_has_request_vote_res(); + } else { + clear_has_request_vote_res(); + } +} + +// optional .floyd.CmdResponse.AppendEntriesResponse append_entries_res = 7; +inline bool CmdResponse::has_append_entries_res() const { + return (_has_bits_[0] & 0x00000040u) != 0; +} +inline void CmdResponse::set_has_append_entries_res() { + _has_bits_[0] |= 0x00000040u; +} +inline void CmdResponse::clear_has_append_entries_res() { + _has_bits_[0] &= ~0x00000040u; +} +inline void CmdResponse::clear_append_entries_res() { + if (append_entries_res_ != NULL) append_entries_res_->::floyd::CmdResponse_AppendEntriesResponse::Clear(); + clear_has_append_entries_res(); +} +inline const ::floyd::CmdResponse_AppendEntriesResponse& CmdResponse::append_entries_res() const { + return append_entries_res_ != NULL ? *append_entries_res_ : *default_instance_->append_entries_res_; +} +inline ::floyd::CmdResponse_AppendEntriesResponse* CmdResponse::mutable_append_entries_res() { + set_has_append_entries_res(); + if (append_entries_res_ == NULL) append_entries_res_ = new ::floyd::CmdResponse_AppendEntriesResponse; + return append_entries_res_; +} +inline ::floyd::CmdResponse_AppendEntriesResponse* CmdResponse::release_append_entries_res() { + clear_has_append_entries_res(); + ::floyd::CmdResponse_AppendEntriesResponse* temp = append_entries_res_; + append_entries_res_ = NULL; + return temp; +} +inline void CmdResponse::set_allocated_append_entries_res(::floyd::CmdResponse_AppendEntriesResponse* append_entries_res) { + delete append_entries_res_; + append_entries_res_ = append_entries_res; + if (append_entries_res) { + set_has_append_entries_res(); + } else { + clear_has_append_entries_res(); + } +} + +// optional .floyd.CmdResponse.ServerStatus server_status = 8; +inline bool CmdResponse::has_server_status() const { + return (_has_bits_[0] & 0x00000080u) != 0; +} +inline void CmdResponse::set_has_server_status() { + _has_bits_[0] |= 0x00000080u; +} +inline void CmdResponse::clear_has_server_status() { + _has_bits_[0] &= ~0x00000080u; +} +inline void CmdResponse::clear_server_status() { + if (server_status_ != NULL) server_status_->::floyd::CmdResponse_ServerStatus::Clear(); + clear_has_server_status(); +} +inline const ::floyd::CmdResponse_ServerStatus& CmdResponse::server_status() const { + return server_status_ != NULL ? *server_status_ : *default_instance_->server_status_; +} +inline ::floyd::CmdResponse_ServerStatus* CmdResponse::mutable_server_status() { + set_has_server_status(); + if (server_status_ == NULL) server_status_ = new ::floyd::CmdResponse_ServerStatus; + return server_status_; +} +inline ::floyd::CmdResponse_ServerStatus* CmdResponse::release_server_status() { + clear_has_server_status(); + ::floyd::CmdResponse_ServerStatus* temp = server_status_; + server_status_ = NULL; + return temp; +} +inline void CmdResponse::set_allocated_server_status(::floyd::CmdResponse_ServerStatus* server_status) { + delete server_status_; + server_status_ = server_status; + if (server_status) { + set_has_server_status(); + } else { + clear_has_server_status(); + } +} + + +// @@protoc_insertion_point(namespace_scope) + +} // namespace floyd + +#ifndef SWIG +namespace google { +namespace protobuf { + +template <> +inline const EnumDescriptor* GetEnumDescriptor< ::floyd::Entry_OpType>() { + return ::floyd::Entry_OpType_descriptor(); +} +template <> +inline const EnumDescriptor* GetEnumDescriptor< ::floyd::Type>() { + return ::floyd::Type_descriptor(); +} +template <> +inline const EnumDescriptor* GetEnumDescriptor< ::floyd::StatusCode>() { + return ::floyd::StatusCode_descriptor(); +} + +} // namespace google +} // namespace protobuf +#endif // SWIG + +// @@protoc_insertion_point(global_scope) + +#endif // PROTOBUF_floyd_2eproto__INCLUDED diff --git a/floyd/tools/read_floyd.cc b/floyd/tools/read_floyd.cc index 07b7645..13a840e 100644 --- a/floyd/tools/read_floyd.cc +++ b/floyd/tools/read_floyd.cc @@ -19,7 +19,7 @@ extern uint64_t BitStrToUint(const std::string &str) { return be64toh(num); } -int main(int argc, char** argv) +int main(int argc, char** argv) { rocksdb::DB* db; rocksdb::Options options; @@ -30,12 +30,19 @@ int main(int argc, char** argv) for (iter->SeekToFirst(); iter->Valid(); iter->Next()) { cnt++; floyd::Entry entry; - if (iter->key().ToString() == "CURRENTTERM" || iter->key().ToString() == "VOTEFORIP" || iter->key().ToString() == "VOTEFORPORT" || iter->key().ToString() == "APPLYINDEX") { - continue; + if (iter->key().ToString() == "CURRENTTERM" || iter->key().ToString() == "VOTEFORIP" || iter->key().ToString() == "VOTEFORPORT" || iter->key().ToString() == "APPLYINDEX" || iter->key().ToString() == "COMMITINDEX") { + if (iter->key().ToString() == "VOTEFORIP") { + printf("key %s, value %s\n", iter->key().ToString().c_str(), iter->value().ToString().c_str()); + } else { + uint64_t ans; + memcpy(&ans, iter->value().data(), sizeof(uint64_t)); + printf("key %s, value %lu\n", iter->key().ToString().c_str(), ans); + } + } else { + entry.ParseFromString(iter->value().ToString()); + uint64_t num = BitStrToUint(iter->key().ToString()); + printf("key %lu entry term: %lu key %s value %s\n", num, entry.term(), entry.key().c_str(), entry.value().c_str()); } - entry.ParseFromString(iter->value().ToString()); - uint64_t num = BitStrToUint(iter->key().ToString()); - printf("key %lu entry term: %lu key %s value %s\n", num, entry.term(), entry.key().c_str(), entry.value().c_str()); // std::cout << "res " << iter->key().ToString() << ": " << iter->value().ToString() << std::endl; } printf("cnt %d\n", cnt); From 32bcd9c28615dc703d248b0e380e8f7ede2858a5 Mon Sep 17 00:00:00 2001 From: baotiao Date: Thu, 27 Jul 2017 18:51:37 +0800 Subject: [PATCH 08/17] stable version --- floyd/example/simple/README | 1 + floyd/example/simple/t.cc | 12 +-- floyd/example/simple/t1.cc | 6 +- floyd/example/simple/t2.cc | 135 ++++++++++++--------------------- floyd/src/floyd_apply.cc | 3 +- floyd/src/floyd_context.cc | 10 --- floyd/src/floyd_context.h | 8 +- floyd/src/floyd_impl.cc | 43 ++++++----- floyd/src/floyd_options.cc | 1 - floyd/src/floyd_peer_thread.cc | 10 +-- floyd/src/raft_log.cc | 7 +- 11 files changed, 91 insertions(+), 145 deletions(-) diff --git a/floyd/example/simple/README b/floyd/example/simple/README index 825b116..267d81c 100644 --- a/floyd/example/simple/README +++ b/floyd/example/simple/README @@ -3,3 +3,4 @@ the simple example will run the Floyd:write the get performance of floyd in a si t is a single thread wirte tool to get performance t1 is multi thread program to get performance +t2 is an example test node join and leave diff --git a/floyd/example/simple/t.cc b/floyd/example/simple/t.cc index 4e4bb06..f123f4d 100644 --- a/floyd/example/simple/t.cc +++ b/floyd/example/simple/t.cc @@ -43,7 +43,7 @@ int main() printf("%s\n", s.ToString().c_str()); std::string msg; - int i = 100; + int cnt = 100; uint64_t st = NowMicros(), ed; sleep(10); @@ -54,7 +54,7 @@ int main() sleep(2); } - while (i--) { + while (cnt--) { std::string mystr[100100]; for (int i = 0; i < 100000; i++) { mystr[i] = slash::RandomString(10); @@ -72,8 +72,8 @@ int main() delete f1; - i = 5; - while (i--) { + cnt = 5; + while (cnt--) { f2->GetServerStatus(msg); for (int j = 0; j < 100; j++) { f2->Write("zz2" + char(j), "value2" + char(j)); @@ -85,8 +85,8 @@ int main() if (!s.ok()) { printf("floyd reoptn failed\n"); } - i = 5; - while (i--) { + cnt = 5; + while (cnt--) { f2->GetServerStatus(msg); for (int j = 0; j < 100; j++) { f1->Write("zz3" + char(j), "value3" + char(j)); diff --git a/floyd/example/simple/t1.cc b/floyd/example/simple/t1.cc index f532d38..f4dd7d5 100644 --- a/floyd/example/simple/t1.cc +++ b/floyd/example/simple/t1.cc @@ -26,7 +26,7 @@ std::string mystr[100100]; void *fun(void *arg) { int i = 1; while (i--) { - for (int j = 0; j < 100000; j++) { + for (int j = 0; j < 10000; j++) { f1->Write(mystr[j], mystr[j]); } } @@ -61,7 +61,7 @@ int main() std::string msg; int i = 10; uint64_t st = NowMicros(), ed; - for (int i = 0; i < 100000; i++) { + for (int i = 0; i < 10000; i++) { mystr[i] = slash::RandomString(10); } while (1) { @@ -80,7 +80,7 @@ int main() pthread_join(pid[i], NULL); } ed = NowMicros(); - printf("write 100000 cost time microsecond(us) %ld, qps %llu\n", ed - st, 100000 * 8 * 1000000LL / (ed - st)); + printf("write 10000 cost time microsecond(us) %ld, qps %llu\n", ed - st, 10000 * 8 * 1000000LL / (ed - st)); getchar(); diff --git a/floyd/example/simple/t2.cc b/floyd/example/simple/t2.cc index bd80c37..7da285f 100644 --- a/floyd/example/simple/t2.cc +++ b/floyd/example/simple/t2.cc @@ -56,98 +56,57 @@ int main() delete f2; delete f3; - while (1) { + i = 8; + while (i--) { + f1->GetServerStatus(msg); + printf("%s\n", msg.c_str()); + sleep(2); + } + + s = Floyd::Open(op2, &f2); + s = Floyd::Open(op3, &f3); + delete f4; + i = 8; + while (i--) { f1->GetServerStatus(msg); printf("%s\n", msg.c_str()); sleep(2); } -/* - * std::string msg; - * int i = 100; - * uint64_t st = NowMicros(), ed; - * std::string mystr[10010]; - * for (int i = 0; i < 10000; i++) { - * mystr[i] = slash::RandomString(10); - * } - * - * sleep(10); - * while (1) { - * if (f1->HasLeader()) { - * break; - * } - * sleep(2); - * printf("electing leader, waitting...\n"); - * } - * - * // ran at the begining - * printf("run 5 times, every time write 100 item. at the beginning state\n"); - * i = 5; - * while (i--) { - * f2->GetServerStatus(msg); - * for (int j = 0; j < 100; j++) { - * f2->Write(mystr[j], mystr[j]); - * } - * printf("%s\n", msg.c_str()); - * } - * - * - * // delete two node - * delete f1; - * delete f3; - * - * while (1) { - * if (f2->HasLeader()) { - * break; - * } - * sleep(2); - * printf("electing leader, waitting...\n"); - * } - * // ran with three node - * printf("delete two node, run 5 times, every time write 100 item with three node\n"); - * i = 5; - * while (i--) { - * f2->GetServerStatus(msg); - * for (int j = 0; j < 100; j++) { - * f2->Write(mystr[j], mystr[j]); - * } - * printf("%s\n", msg.c_str()); - * } - * - * s = Floyd::Open(op, &f1); - * if (!s.ok()) { - * printf("floyd reoptn failed\n"); - * } - * s = Floyd::Open(op3, &f3); - * if (!s.ok()) { - * printf("floyd reoptn failed\n"); - * } - * - * while (1) { - * if (f1->HasLeader()) { - * break; - * } - * sleep(2); - * printf("electing leader, waitting...\n"); - * } - * // ran with node recovery - * printf("recovery the two node, run 5 time, every time write 100 item\n"); - * i = 5; - * while (i--) { - * f2->GetServerStatus(msg); - * for (int j = 0; j < 100; j++) { - * f1->Write(mystr[j], mystr[j]); - * } - * printf("%s\n", msg.c_str()); - * } - * - * // at last, we will have 300 log, 100 db - */ - getchar(); - delete f2; - delete f3; - delete f4; - delete f5; - delete f1; + + s = Floyd::Open(op4, &f4); + delete f1; + delete f5; + i = 8; + while (i--) { + f3->GetServerStatus(msg); + printf("%s\n", msg.c_str()); + sleep(2); + } + + delete f2; + i = 8; + while (i--) { + f3->GetServerStatus(msg); + printf("%s\n", msg.c_str()); + sleep(2); + } + + s = Floyd::Open(op, &f1); + s = Floyd::Open(op2, &f2); + s = Floyd::Open(op5, &f5); + i = 8; + while (i--) { + f1->GetServerStatus(msg); + printf("%s\n", msg.c_str()); + sleep(2); + } + + getchar(); + delete f2; + delete f3; + delete f4; + delete f5; + delete f1; return 0; } diff --git a/floyd/src/floyd_apply.cc b/floyd/src/floyd_apply.cc index fe64780..53270b0 100644 --- a/floyd/src/floyd_apply.cc +++ b/floyd/src/floyd_apply.cc @@ -46,7 +46,7 @@ void FloydApply::ApplyStateMachineWrapper(void* arg) { } void FloydApply::ApplyStateMachine() { - uint64_t last_applied = raft_meta_->GetLastApplied(); + uint64_t last_applied = context_->last_applied; // Apply as more entry as possible uint64_t commit_index; context_->commit_index_mu.Lock(); @@ -67,7 +67,6 @@ void FloydApply::ApplyStateMachine() { usleep(1000000); return; } - raft_meta_->SetLastApplied(last_applied); } context_->apply_mu.Lock(); context_->last_applied = last_applied; diff --git a/floyd/src/floyd_context.cc b/floyd/src/floyd_context.cc index a9549e6..aa3511f 100644 --- a/floyd/src/floyd_context.cc +++ b/floyd/src/floyd_context.cc @@ -25,16 +25,6 @@ void FloydContext::RecoverInit(RaftMeta *raft_meta) { role = Role::kFollower; } -void FloydContext::leader_node(std::string* ip, int* port) { - *ip = leader_ip; - *port = leader_port; -} - -void FloydContext::voted_for_node(std::string* ip, int* port) { - *ip = voted_for_ip; - *port = voted_for_port; -} - void FloydContext::BecomeFollower(uint64_t new_term, const std::string _leader_ip, int _leader_port) { // when requestvote receive a large term, then we transfer from candidate to follower diff --git a/floyd/src/floyd_context.h b/floyd/src/floyd_context.h index 326ebc1..5091478 100644 --- a/floyd/src/floyd_context.h +++ b/floyd/src/floyd_context.h @@ -37,8 +37,6 @@ struct FloydContext { leader_port(0), vote_quorum(0), apply_cond(&apply_mu) {}; - void leader_node(std::string* ip, int* port); - void voted_for_node(std::string* ip, int* port); void RecoverInit(RaftMeta *raft); void BecomeFollower(uint64_t new_iterm, @@ -46,10 +44,6 @@ struct FloydContext { void BecomeCandidate(); void BecomeLeader(); - bool ReceiverDoAppendEntries(uint64_t term, - uint64_t pre_log_term, uint64_t pre_log_index, - std::vector& entries, uint64_t* my_term); - Options options; // Role related uint64_t current_term; @@ -62,7 +56,7 @@ struct FloydContext { uint32_t vote_quorum; uint64_t commit_index; - uint64_t last_applied; + std::atomic last_applied; uint64_t last_op_time; // mutex protect commit_index diff --git a/floyd/src/floyd_impl.cc b/floyd/src/floyd_impl.cc index d8f7afa..aa67ea9 100644 --- a/floyd/src/floyd_impl.cc +++ b/floyd/src/floyd_impl.cc @@ -30,6 +30,7 @@ namespace floyd { FloydImpl::FloydImpl(const Options& options) : db_(NULL), + log_and_meta_(NULL), options_(options), info_log_(NULL) { } @@ -46,7 +47,7 @@ FloydImpl::~FloydImpl() { delete context_; delete raft_meta_; delete raft_log_; - delete log_and_meta_; + // delete log_and_meta_; delete info_log_; delete db_; } @@ -56,19 +57,16 @@ bool FloydImpl::IsSelf(const std::string& ip_port) { } bool FloydImpl::GetLeader(std::string *ip_port) { - std::string ip; - int port; - context_->leader_node(&ip, &port); - if (ip.empty() || port == 0) { + if (context_->leader_ip.empty() || context_->leader_port == 0) { return false; } - *ip_port = slash::IpPortString(ip, port); + *ip_port = slash::IpPortString(context_->leader_ip, context_->leader_port); return true; } -// TODO (baotiao): this function is wrong bool FloydImpl::GetLeader(std::string* ip, int* port) { - context_->leader_node(ip, port); + *ip = context_->leader_ip; + *port = context_->leader_port; return (!ip->empty() && *port != 0); } @@ -361,7 +359,7 @@ bool FloydImpl::GetServerStatus(std::string& msg) { server_status.term(), server_status.commit_index(), server_status.leader_ip().c_str(), server_status.leader_port(), server_status.voted_for_ip().c_str(), server_status.voted_for_port(), - server_status.last_log_term(), server_status.last_log_index(), + server_status.last_log_term(), server_status.last_log_index(), server_status.commit_index(), server_status.last_applied()); msg.clear(); @@ -402,7 +400,11 @@ Status FloydImpl::DoCommand(const CmdRequest& cmd, CmdResponse *response) { // Execute if is leader std::string leader_ip; int leader_port; - context_->leader_node(&leader_ip, &leader_port); + { + slash::MutexLock l(&context_->commit_mu); + leader_ip = context_->leader_ip; + leader_port = context_->leader_port; + } if (options_.local_ip == leader_ip && options_.local_port == leader_port) { return ExecuteCommand(cmd, response); } @@ -473,7 +475,11 @@ bool FloydImpl::DoGetServerStatus(CmdResponse_ServerStatus* res) { std::string ip; int port; - context_->leader_node(&ip, &port); + { + slash::MutexLock l(&context_->commit_mu); + ip = context_->leader_ip; + port = context_->leader_port; + } if (ip.empty()) { res->set_leader_ip("null"); } else { @@ -481,7 +487,11 @@ bool FloydImpl::DoGetServerStatus(CmdResponse_ServerStatus* res) { } res->set_leader_port(port); - context_->voted_for_node(&ip, &port); + { + slash::MutexLock l(&context_->commit_mu); + ip = context_->voted_for_ip; + port = context_->voted_for_port; + } if (ip.empty()) { res->set_voted_for_ip("null"); } else { @@ -495,7 +505,7 @@ bool FloydImpl::DoGetServerStatus(CmdResponse_ServerStatus* res) { res->set_last_log_term(last_log_term); res->set_last_log_index(last_log_index); - res->set_last_applied(context_->last_applied); + res->set_last_applied(raft_meta_->GetLastApplied()); return true; } @@ -552,11 +562,6 @@ Status FloydImpl::ExecuteCommand(const CmdRequest& request, } LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ExecuteCommand Read %s, key(%s) value(%s)", rs.ToString().c_str(), request.kv().key().c_str(), value.c_str()); -#ifndef NDEBUG - std::string text_format; - google::protobuf::TextFormat::PrintToString(*response, &text_format); - LOGV(DEBUG_LEVEL, info_log_, "ReadResponse :\n%s", text_format.c_str()); -#endif break; } default: { @@ -602,7 +607,7 @@ void FloydImpl::ReplyRequestVote(const CmdRequest& request, CmdResponse* respons && vote_for_[request_vote.term()] != std::make_pair(request_vote.ip(), request_vote.port())) { LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyRequestVote: BecomeFollower with current_term_(%lu) and new_term(%lu)" " commit_index(%lu) last_applied(%lu)", - context_->current_term, request_vote.last_log_term(), my_last_log_index, context_->last_applied); + context_->current_term, request_vote.last_log_term(), my_last_log_index, context_->last_applied.load()); BuildRequestVoteResponse(context_->current_term, granted, response); return ; } diff --git a/floyd/src/floyd_options.cc b/floyd/src/floyd_options.cc index 2e3dec1..15aa8dd 100644 --- a/floyd/src/floyd_options.cc +++ b/floyd/src/floyd_options.cc @@ -108,7 +108,6 @@ Options::Options(const std::string& cluster_string, // the default heartbeat time is 1s // we can promise 1s + 2 * rpc < 3s, since rpc time is approximately 10ms check_leader_us = std::rand() % 2000000 + check_leader_us; - printf("check_leader_us %lu\n", check_leader_us); split(cluster_string, ',', members); if (members.size() == 1) { single_mode = true; diff --git a/floyd/src/floyd_peer_thread.cc b/floyd/src/floyd_peer_thread.cc index a038ca9..c2137a6 100644 --- a/floyd/src/floyd_peer_thread.cc +++ b/floyd/src/floyd_peer_thread.cc @@ -127,15 +127,9 @@ void Peer::AddAppendEntriesTask() { bg_thread_.Schedule(&AppendEntriesRPCWrapper, this); } -void Peer::AppendEntriesRPCWrapper(void *arg) { - reinterpret_cast(arg)->AppendEntriesRPC(); -} - uint64_t Peer::QuorumMatchIndex() { std::vector values; std::map::iterator iter; - LOGV(WARN_LEVEL, info_log_, "Peer::QuorumMatchIndex: Peers size %d", - peers_.size()); for (iter = peers_.begin(); iter != peers_.end(); iter++) { if (iter->first == server_) { continue; @@ -158,6 +152,10 @@ void Peer::AdvanceLeaderCommitIndex() { return; } +void Peer::AppendEntriesRPCWrapper(void *arg) { + reinterpret_cast(arg)->AppendEntriesRPC(); +} + Status Peer::AppendEntriesRPC() { uint64_t prev_log_index = next_index_ - 1; uint64_t num_entries = 0; diff --git a/floyd/src/raft_log.cc b/floyd/src/raft_log.cc index 6cf3781..fd25674 100644 --- a/floyd/src/raft_log.cc +++ b/floyd/src/raft_log.cc @@ -49,6 +49,7 @@ RaftLog::RaftLog(rocksdb::DB *db, Logger *info_log) : last_log_index_ = BitStrToUint(it->key().ToString()); } } + delete it; } RaftLog::~RaftLog() { @@ -103,10 +104,10 @@ bool RaftLog::GetLastLogTermAndIndex(uint64_t* last_log_term, uint64_t* last_log *last_log_term = 0; return true; } - Entry *entry = new Entry(); - bool is = entry->ParseFromString(buf); + Entry entry; + bool is = entry.ParseFromString(buf); *last_log_index = last_log_index_; - *last_log_term = entry->term(); + *last_log_term = entry.term(); return true; } From 85c365539933a0af34d8d7ab830931feedb01537 Mon Sep 17 00:00:00 2001 From: baotiao Date: Fri, 28 Jul 2017 16:05:43 +0800 Subject: [PATCH 09/17] update stop thread interface --- floyd/Makefile | 2 +- floyd/example/simple/Makefile | 3 ++- floyd/example/simple/t.cc | 2 +- floyd/src/floyd_apply.cc | 4 ++++ floyd/src/floyd_apply.h | 1 + floyd/src/floyd_context.h | 2 +- floyd/src/floyd_impl.cc | 24 +++++++++++++++--------- floyd/src/floyd_impl.h | 8 ++++---- floyd/src/floyd_peer_thread.cc | 14 +++++++++----- floyd/src/floyd_peer_thread.h | 3 ++- floyd/src/floyd_primary_thread.cc | 8 ++++++-- floyd/src/floyd_primary_thread.h | 1 + floyd/src/floyd_worker.h | 6 +++++- 13 files changed, 52 insertions(+), 26 deletions(-) diff --git a/floyd/Makefile b/floyd/Makefile index 701504f..91514ce 100755 --- a/floyd/Makefile +++ b/floyd/Makefile @@ -2,7 +2,7 @@ CXX = g++ ifeq ($(__PERF), 1) CXXFLAGS = -O0 -ggdb3 -pg -pipe -fPIC -D__XDEBUG__ -DLOG_LEVEL=LEVEL_DEBUG -W -Wwrite-strings -Wpointer-arith -Wreorder -Wswitch -Wsign-promo -Wredundant-decls -Wformat -D_GNU_SOURCE -std=c++11 -D__STDC_FORMAT_MACROS -std=c++11 -Wno-redundant-decls -Wno-unused-variable -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DOS_LINUX else - CXXFLAGS = -O2 -g -pipe -fPIC -DNDEBUG -W -DLOG_LEVEL=LEVEL_INFO -Wwrite-strings -Wpointer-arith -Wreorder -Wswitch -Wsign-promo -Wredundant-decls -Wformat -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -std=c++11 -gdwarf-2 -Wno-redundant-decls -Wno-unused-variable -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DOS_LINUX + CXXFLAGS = -O2 -g -pg -pipe -fPIC -DNDEBUG -W -DLOG_LEVEL=LEVEL_INFO -Wwrite-strings -Wpointer-arith -Wreorder -Wswitch -Wsign-promo -Wredundant-decls -Wformat -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -std=c++11 -gdwarf-2 -Wno-redundant-decls -Wno-unused-variable -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DOS_LINUX endif ifndef SLASH_PATH diff --git a/floyd/example/simple/Makefile b/floyd/example/simple/Makefile index 9d91fde..ba403d5 100644 --- a/floyd/example/simple/Makefile +++ b/floyd/example/simple/Makefile @@ -2,7 +2,7 @@ CXX = g++ ifeq ($(__PERF), 1) CXXFLAGS = -O0 -g -pipe -fPIC -DNDEBUG -DLOG_LEVEL=LEVEL_INFO -W -Wwrite-strings -Wpointer-arith -Wreorder -Wswitch -Wsign-promo -Wredundant-decls -Wformat -Wall -D_GNU_SOURCE -std=c++11 -D__STDC_FORMAT_MACROS -std=c++11 -gdwarf-2 -Wno-redundant-decls -Wno-unused-variable -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DOS_LINUX else - CXXFLAGS = -pg -O2 -ggdb3 -pipe -fPIC -W -Wwrite-strings -Wpointer-arith -Wreorder -Wswitch -Wsign-promo -Wredundant-decls -Wformat -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -std=c++11 -gdwarf-2 -Wno-redundant-decls -Wno-unused-variable -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DOS_LINUX + CXXFLAGS = -pg -g -O2 -ggdb3 -pipe -fPIC -W -Wwrite-strings -Wpointer-arith -Wreorder -Wswitch -Wsign-promo -Wredundant-decls -Wformat -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -std=c++11 -gdwarf-2 -Wno-redundant-decls -Wno-unused-variable -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DOS_LINUX endif OBJECT = t t1 t2 t3 @@ -27,6 +27,7 @@ LIBS = -lfloyd \ -lrt \ -lssl \ -lcrypto \ + -lprofiler \ -lpthread INCLUDE_PATH = -I../../../ \ diff --git a/floyd/example/simple/t.cc b/floyd/example/simple/t.cc index f123f4d..4a4122b 100644 --- a/floyd/example/simple/t.cc +++ b/floyd/example/simple/t.cc @@ -67,7 +67,7 @@ int main() // f1->Write("zz", "zz"); } ed = NowMicros(); - printf("write 10000 cost time microsecond(us) %ld, qps %llu\n", ed - st, 100000 * 1000000LL / (ed - st)); + printf("write 100000 cost time microsecond(us) %ld, qps %llu\n", ed - st, 100000 * 1000000LL / (ed - st)); } delete f1; diff --git a/floyd/src/floyd_apply.cc b/floyd/src/floyd_apply.cc index 53270b0..8d04d6b 100644 --- a/floyd/src/floyd_apply.cc +++ b/floyd/src/floyd_apply.cc @@ -37,6 +37,10 @@ int FloydApply::Start() { return bg_thread_.StartThread(); } +int FloydApply::Stop() { + return bg_thread_.StopThread(); +} + void FloydApply::ScheduleApply() { bg_thread_.Schedule(&ApplyStateMachineWrapper, this); } diff --git a/floyd/src/floyd_apply.h b/floyd/src/floyd_apply.h index 876e5e0..b9f17a4 100644 --- a/floyd/src/floyd_apply.h +++ b/floyd/src/floyd_apply.h @@ -24,6 +24,7 @@ class FloydApply { FloydApply(FloydContext* context, rocksdb::DB* db, RaftMeta* raft_meta, RaftLog* raft_log, Logger* info_log); ~FloydApply(); int Start(); + int Stop(); void ScheduleApply(); private: diff --git a/floyd/src/floyd_context.h b/floyd/src/floyd_context.h index 5091478..f9cc862 100644 --- a/floyd/src/floyd_context.h +++ b/floyd/src/floyd_context.h @@ -64,7 +64,7 @@ struct FloydContext { // floyd_apply thread will get the commit_index // floyd_peer thread will update the commit_index slash::Mutex commit_index_mu; - slash::Mutex commit_mu; + slash::Mutex global_mu; slash::Mutex apply_mu; slash::CondVar apply_cond; }; diff --git a/floyd/src/floyd_impl.cc b/floyd/src/floyd_impl.cc index aa67ea9..716e5a3 100644 --- a/floyd/src/floyd_impl.cc +++ b/floyd/src/floyd_impl.cc @@ -37,19 +37,25 @@ FloydImpl::FloydImpl(const Options& options) FloydImpl::~FloydImpl() { // worker will use floyd, delete worker first + context_->global_mu.Lock(); + worker_->Stop(); + primary_->Stop(); + apply_->Stop(); delete worker_; delete worker_client_pool_; delete primary_; delete apply_; for (auto& pt : peers_) { + pt.second->Stop(); delete pt.second; } + context_->global_mu.Unlock(); delete context_; delete raft_meta_; delete raft_log_; - // delete log_and_meta_; delete info_log_; delete db_; + // delete log_and_meta_; } bool FloydImpl::IsSelf(const std::string& ip_port) { @@ -108,8 +114,8 @@ Status FloydImpl::Init() { s = rocksdb::DB::Open(options, options_.path + "/log/", &log_and_meta_); if (!s.ok()) { - LOGV(ERROR_LEVEL, info_log_, "Open db failed! path: %s", options_.path.c_str()); - return Status::Corruption("Open DB failed, " + s.ToString()); + LOGV(ERROR_LEVEL, info_log_, "Open DB log_and_meta failed! path: %s", options_.path.c_str()); + return Status::Corruption("Open DB log_and_meta failed, " + s.ToString()); } // Recover Context @@ -142,7 +148,7 @@ Status FloydImpl::Init() { int ret; for (auto& pt : peers_) { pt.second->set_peers(peers_); - if ((ret = pt.second->StartThread()) != 0) { + if ((ret = pt.second->Start()) != 0) { LOGV(ERROR_LEVEL, info_log_, "FloydImpl peer thread to %s failed to " " start, ret is %d", pt.first.c_str(), ret); return Status::Corruption("failed to start peer thread to " + pt.first); @@ -401,7 +407,7 @@ Status FloydImpl::DoCommand(const CmdRequest& cmd, CmdResponse *response) { std::string leader_ip; int leader_port; { - slash::MutexLock l(&context_->commit_mu); + slash::MutexLock l(&context_->global_mu); leader_ip = context_->leader_ip; leader_port = context_->leader_port; } @@ -476,7 +482,7 @@ bool FloydImpl::DoGetServerStatus(CmdResponse_ServerStatus* res) { std::string ip; int port; { - slash::MutexLock l(&context_->commit_mu); + slash::MutexLock l(&context_->global_mu); ip = context_->leader_ip; port = context_->leader_port; } @@ -488,7 +494,7 @@ bool FloydImpl::DoGetServerStatus(CmdResponse_ServerStatus* res) { res->set_leader_port(port); { - slash::MutexLock l(&context_->commit_mu); + slash::MutexLock l(&context_->global_mu); ip = context_->voted_for_ip; port = context_->voted_for_port; } @@ -581,7 +587,7 @@ void FloydImpl::GrantVote(uint64_t term, const std::string ip, int port) { } void FloydImpl::ReplyRequestVote(const CmdRequest& request, CmdResponse* response) { - slash::MutexLock l(&context_->commit_mu); + slash::MutexLock l(&context_->global_mu); bool granted = false; CmdRequest_RequestVote request_vote = request.request_vote(); LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyRequestVote: my_term=%lu request.term=%lu", @@ -637,7 +643,7 @@ bool FloydImpl::AdvanceFollowerCommitIndex(uint64_t new_commit_index) { void FloydImpl::ReplyAppendEntries(CmdRequest& request, CmdResponse* response) { bool success = false; CmdRequest_AppendEntries append_entries = request.append_entries(); - slash::MutexLock l(&context_->commit_mu); + slash::MutexLock l(&context_->global_mu); // Ignore stale term // if the append entries term is smaller then my current term, then the caller must an older leader uint64_t last_log_index = raft_log_->GetLastLogIndex(); diff --git a/floyd/src/floyd_impl.h b/floyd/src/floyd_impl.h index baa6eaa..0bf6a4b 100644 --- a/floyd/src/floyd_impl.h +++ b/floyd/src/floyd_impl.h @@ -67,16 +67,16 @@ class FloydImpl : public Floyd { friend class Peer; rocksdb::DB* db_; - Options options_; - // debug log used for ouput to file - Logger* info_log_; - // state machine db point // raft log rocksdb::DB* log_and_meta_; // used to store logs and meta data RaftLog* raft_log_; RaftMeta* raft_meta_; + Options options_; + // debug log used for ouput to file + Logger* info_log_; + FloydContext* context_; FloydWorker* worker_; diff --git a/floyd/src/floyd_peer_thread.cc b/floyd/src/floyd_peer_thread.cc index c2137a6..b04c975 100644 --- a/floyd/src/floyd_peer_thread.cc +++ b/floyd/src/floyd_peer_thread.cc @@ -40,7 +40,7 @@ Peer::Peer(std::string server, FloydContext* context, FloydPrimary* primary, Raf match_index_ = raft_meta_->GetLastApplied(); } -int Peer::StartThread() { +int Peer::Start() { bg_thread_.set_thread_name("FloydPeer" + server_.substr(server_.find(':'))); return bg_thread_.StartThread(); } @@ -48,6 +48,10 @@ int Peer::StartThread() { Peer::~Peer() { LOGV(INFO_LEVEL, info_log_, "Peer(%s) exit!!!", server_.c_str()); } +int Peer::Stop() { + return bg_thread_.StopThread(); +} + bool Peer::CheckAndVote(uint64_t vote_term) { if (context_->current_term != vote_term) { @@ -69,7 +73,7 @@ Status Peer::RequestVoteRPC() { uint64_t last_log_index; CmdRequest req; { - slash::MutexLock l(&context_->commit_mu); + slash::MutexLock l(&context_->global_mu); raft_log_->GetLastLogTermAndIndex(&last_log_term, &last_log_index); req.set_type(Type::kRequestVote); @@ -91,7 +95,7 @@ Status Peer::RequestVoteRPC() { } { - slash::MutexLock l(&context_->commit_mu); + slash::MutexLock l(&context_->global_mu); if (context_->role == Role::kCandidate) { // kOk means RequestVote success, opposite vote for me if (res.request_vote_res().vote_granted() == true) { // granted @@ -164,7 +168,7 @@ Status Peer::AppendEntriesRPC() { CmdRequest req; CmdRequest_AppendEntries* append_entries = req.mutable_append_entries(); { - slash::MutexLock l(&context_->commit_mu); + slash::MutexLock l(&context_->global_mu); if (prev_log_index != 0) { Entry entry; @@ -215,7 +219,7 @@ Status Peer::AppendEntriesRPC() { Status result = pool_->SendAndRecv(server_, req, &res); { - slash::MutexLock l(&context_->commit_mu); + slash::MutexLock l(&context_->global_mu); if (!result.ok()) { LOGV(WARN_LEVEL, info_log_, "FloydPeerThread::AppendEntries: AppendEntry to %s failed %s", server_.c_str(), result.ToString().c_str()); diff --git a/floyd/src/floyd_peer_thread.h b/floyd/src/floyd_peer_thread.h index a61b20e..6f09873 100644 --- a/floyd/src/floyd_peer_thread.h +++ b/floyd/src/floyd_peer_thread.h @@ -33,7 +33,8 @@ class Peer { RaftLog* raft_log, ClientPool* pool, FloydApply* apply, const Options& options, Logger* info_log); ~Peer(); - int StartThread(); + int Start(); + int Stop(); // Apend Entries // call by other thread, put job to peer_thread's bg_thread_ diff --git a/floyd/src/floyd_primary_thread.cc b/floyd/src/floyd_primary_thread.cc index 945a535..8503c8b 100644 --- a/floyd/src/floyd_primary_thread.cc +++ b/floyd/src/floyd_primary_thread.cc @@ -43,6 +43,10 @@ FloydPrimary::~FloydPrimary() { LOGV(INFO_LEVEL, info_log_, "FloydPrimary exit!!!"); } +int FloydPrimary::Stop() { + return bg_thread_.StopThread(); +} + // TODO(anan) We keep 2 Primary Cron in total. // 1. one short live Cron for LeaderHeartbeat, which is available as a leader; // 2. another long live Cron for ElectLeaderCheck, which is started when @@ -86,7 +90,7 @@ void FloydPrimary::LaunchHeartBeatWrapper(void *arg) { } void FloydPrimary::LaunchHeartBeat() { - slash::MutexLock l(&context_->commit_mu); + slash::MutexLock l(&context_->global_mu); if (context_->role == Role::kLeader) { NoticePeerTask(kNewCommand); AddTask(kHeartBeat); @@ -97,7 +101,7 @@ void FloydPrimary::LaunchCheckLeaderWrapper(void *arg) { reinterpret_cast(arg)->LaunchCheckLeader(); } void FloydPrimary::LaunchCheckLeader() { - slash::MutexLock l(&context_->commit_mu); + slash::MutexLock l(&context_->global_mu); if (context_->role == Role::kFollower || context_->role == Role::kCandidate) { if (options_.single_mode) { context_->BecomeLeader(); diff --git a/floyd/src/floyd_primary_thread.h b/floyd/src/floyd_primary_thread.h index 0dcd2e5..39aea0a 100644 --- a/floyd/src/floyd_primary_thread.h +++ b/floyd/src/floyd_primary_thread.h @@ -43,6 +43,7 @@ class FloydPrimary { ~FloydPrimary(); int Start(); + int Stop(); void AddTask(TaskType type, bool is_delay = true); void set_peers(PeersSet peers) { peers_ = peers; diff --git a/floyd/src/floyd_worker.h b/floyd/src/floyd_worker.h index 0366de8..0822425 100644 --- a/floyd/src/floyd_worker.h +++ b/floyd/src/floyd_worker.h @@ -62,7 +62,7 @@ class FloydWorker { FloydWorker(int port, int cron_interval, FloydImpl* floyd); ~FloydWorker() { - thread_->StopThread(); + // thread_->StopThread(); delete thread_; } @@ -71,6 +71,10 @@ class FloydWorker { return thread_->StartThread(); } + int Stop() { + return thread_->StopThread(); + } + private: FloydWorkerConnFactory conn_factory_; FloydWorkerHandle handle_; From 5275e170a5f44d2f1f075ef6a909865192f1613e Mon Sep 17 00:00:00 2001 From: baotiao Date: Mon, 31 Jul 2017 18:25:00 +0800 Subject: [PATCH 10/17] fix bugs 1. add request vote log 2. update example --- floyd/Makefile | 2 +- floyd/example/simple/t.cc | 1 + floyd/example/simple/t1.cc | 18 +++++++++++------- floyd/src/floyd_impl.cc | 2 -- floyd/src/floyd_primary_thread.cc | 2 ++ 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/floyd/Makefile b/floyd/Makefile index 91514ce..d9ce641 100755 --- a/floyd/Makefile +++ b/floyd/Makefile @@ -1,6 +1,6 @@ CXX = g++ ifeq ($(__PERF), 1) - CXXFLAGS = -O0 -ggdb3 -pg -pipe -fPIC -D__XDEBUG__ -DLOG_LEVEL=LEVEL_DEBUG -W -Wwrite-strings -Wpointer-arith -Wreorder -Wswitch -Wsign-promo -Wredundant-decls -Wformat -D_GNU_SOURCE -std=c++11 -D__STDC_FORMAT_MACROS -std=c++11 -Wno-redundant-decls -Wno-unused-variable -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DOS_LINUX + CXXFLAGS = -O0 -ggdb3 -g -pg -pipe -fPIC -D__XDEBUG__ -DLOG_LEVEL=LEVEL_DEBUG -W -Wwrite-strings -Wpointer-arith -Wreorder -Wswitch -Wsign-promo -Wredundant-decls -Wformat -D_GNU_SOURCE -std=c++11 -D__STDC_FORMAT_MACROS -std=c++11 -Wno-redundant-decls -Wno-unused-variable -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DOS_LINUX else CXXFLAGS = -O2 -g -pg -pipe -fPIC -DNDEBUG -W -DLOG_LEVEL=LEVEL_INFO -Wwrite-strings -Wpointer-arith -Wreorder -Wswitch -Wsign-promo -Wredundant-decls -Wformat -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -std=c++11 -gdwarf-2 -Wno-redundant-decls -Wno-unused-variable -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DOS_LINUX endif diff --git a/floyd/example/simple/t.cc b/floyd/example/simple/t.cc index 4a4122b..568b56f 100644 --- a/floyd/example/simple/t.cc +++ b/floyd/example/simple/t.cc @@ -51,6 +51,7 @@ int main() if (f1->HasLeader()) { break; } + printf("electing leader... sleep 2s\n"); sleep(2); } diff --git a/floyd/example/simple/t1.cc b/floyd/example/simple/t1.cc index f4dd7d5..ac6a9e9 100644 --- a/floyd/example/simple/t1.cc +++ b/floyd/example/simple/t1.cc @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -22,11 +23,11 @@ uint64_t NowMicros() { } Floyd *f1; -std::string mystr[100100]; +std::string mystr[1001000]; void *fun(void *arg) { int i = 1; while (i--) { - for (int j = 0; j < 10000; j++) { + for (int j = 0; j < 100000; j++) { f1->Write(mystr[j], mystr[j]); } } @@ -34,6 +35,8 @@ void *fun(void *arg) { int main() { + // sigaction(27, NULL, NULL); + // signal(27, NULL); // Options op("127.0.0.1:8907", "127.0.0.1", 8907, "./data1/"); Options op("127.0.0.1:8907,127.0.0.1:8908,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8907, "./data1/"); Floyd *f2, *f3, *f4, *f5; @@ -61,27 +64,28 @@ int main() std::string msg; int i = 10; uint64_t st = NowMicros(), ed; - for (int i = 0; i < 10000; i++) { + for (int i = 0; i < 1000000; i++) { mystr[i] = slash::RandomString(10); } while (1) { if (f1->HasLeader()) { break; } + printf("electing leader... sleep 2s\n"); sleep(2); } pthread_t pid[24]; st = NowMicros(); - for (int i = 0; i < 8; i++) { + int thread_num = 4; + for (int i = 0; i < thread_num; i++) { pthread_create(&pid[i], NULL, fun, NULL); } - for (int i = 0; i < 8; i++) { + for (int i = 0; i < thread_num; i++) { pthread_join(pid[i], NULL); } ed = NowMicros(); - printf("write 10000 cost time microsecond(us) %ld, qps %llu\n", ed - st, 10000 * 8 * 1000000LL / (ed - st)); - + printf("write 100000 cost time microsecond(us) %ld, qps %llu\n", ed - st, 100000 * thread_num * 1000000LL / (ed - st)); getchar(); delete f2; diff --git a/floyd/src/floyd_impl.cc b/floyd/src/floyd_impl.cc index 716e5a3..3b7d276 100644 --- a/floyd/src/floyd_impl.cc +++ b/floyd/src/floyd_impl.cc @@ -37,7 +37,6 @@ FloydImpl::FloydImpl(const Options& options) FloydImpl::~FloydImpl() { // worker will use floyd, delete worker first - context_->global_mu.Lock(); worker_->Stop(); primary_->Stop(); apply_->Stop(); @@ -49,7 +48,6 @@ FloydImpl::~FloydImpl() { pt.second->Stop(); delete pt.second; } - context_->global_mu.Unlock(); delete context_; delete raft_meta_; delete raft_log_; diff --git a/floyd/src/floyd_primary_thread.cc b/floyd/src/floyd_primary_thread.cc index 8503c8b..5cbb461 100644 --- a/floyd/src/floyd_primary_thread.cc +++ b/floyd/src/floyd_primary_thread.cc @@ -134,9 +134,11 @@ void FloydPrimary::NoticePeerTask(TaskType type) { for (auto& peer : peers_) { switch (type) { case kHeartBeat: + LOGV(INFO_LEVEL, info_log_, "FloydPrimary::NoticePeerTask send request vote message"); peer.second->AddRequestVoteTask(); break; case kNewCommand: + LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::NoticePeerTask send appendEntries message"); peer.second->AddAppendEntriesTask(); break; default: From acd3656d35496df9c88dfe97527512d520ae414b Mon Sep 17 00:00:00 2001 From: baotiao Date: Wed, 2 Aug 2017 17:37:21 +0800 Subject: [PATCH 11/17] fix single mode bugs --- floyd/example/simple/Makefile | 6 +++++- floyd/example/simple/README | 3 +++ floyd/example/simple/t1.cc | 27 +++++++++++++++++++-------- floyd/src/floyd_apply.cc | 2 -- floyd/src/floyd_context.cc | 4 ++-- floyd/src/floyd_context.h | 3 --- floyd/src/floyd_impl.cc | 28 ++++++++++++++++------------ floyd/src/floyd_options.cc | 8 ++++---- floyd/src/floyd_peer_thread.cc | 26 ++++++++++++++++---------- floyd/src/floyd_peer_thread.h | 2 +- floyd/src/floyd_primary_thread.cc | 14 ++++++++++---- 11 files changed, 76 insertions(+), 47 deletions(-) diff --git a/floyd/example/simple/Makefile b/floyd/example/simple/Makefile index ba403d5..c38bb47 100644 --- a/floyd/example/simple/Makefile +++ b/floyd/example/simple/Makefile @@ -5,7 +5,7 @@ else CXXFLAGS = -pg -g -O2 -ggdb3 -pipe -fPIC -W -Wwrite-strings -Wpointer-arith -Wreorder -Wswitch -Wsign-promo -Wredundant-decls -Wformat -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -std=c++11 -gdwarf-2 -Wno-redundant-decls -Wno-unused-variable -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DOS_LINUX endif -OBJECT = t t1 t2 t3 +OBJECT = t t1 t2 t3 t4 t5 SRC_DIR = ./ THIRD_PATH = ../../third OUTPUT = ./output @@ -61,6 +61,10 @@ t2: t2.cc $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) t3: t3.cc $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) +t4: t4.cc + $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) +t5: t5.cc + $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) $(OBJS): %.o : %.cc $(CXX) $(CXXFLAGS) -c $< -o $@ $(INCLUDE_PATH) diff --git a/floyd/example/simple/README b/floyd/example/simple/README index 267d81c..af4ef13 100644 --- a/floyd/example/simple/README +++ b/floyd/example/simple/README @@ -4,3 +4,6 @@ the simple example will run the Floyd:write the get performance of floyd in a si t is a single thread wirte tool to get performance t1 is multi thread program to get performance t2 is an example test node join and leave + +t4 is an example used to see the message passing by each node in a stable situation +t5 used to test single mode floyd, including starting a node and writing data diff --git a/floyd/example/simple/t1.cc b/floyd/example/simple/t1.cc index ac6a9e9..bc4280e 100644 --- a/floyd/example/simple/t1.cc +++ b/floyd/example/simple/t1.cc @@ -23,21 +23,30 @@ uint64_t NowMicros() { } Floyd *f1; -std::string mystr[1001000]; +std::string keystr[1001000]; +std::string valstr[1001000]; void *fun(void *arg) { int i = 1; while (i--) { for (int j = 0; j < 100000; j++) { - f1->Write(mystr[j], mystr[j]); + f1->Write(keystr[j], valstr[j]); } } } -int main() +int main(int argc, char * argv[]) { - // sigaction(27, NULL, NULL); - // signal(27, NULL); - // Options op("127.0.0.1:8907", "127.0.0.1", 8907, "./data1/"); + int val_size = 128; + int thread_num = 4; + if (argc > 1) { + val_size = atoi(argv[1]); + } + if (argc > 2) { + thread_num = atoi(argv[2]); + } + + printf("multi threads test to get performance thread num %d key size %d\n", thread_num, val_size); + Options op("127.0.0.1:8907,127.0.0.1:8908,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8907, "./data1/"); Floyd *f2, *f3, *f4, *f5; op.Dump(); @@ -65,7 +74,10 @@ int main() int i = 10; uint64_t st = NowMicros(), ed; for (int i = 0; i < 1000000; i++) { - mystr[i] = slash::RandomString(10); + keystr[i] = slash::RandomString(32); + } + for (int i = 0; i < 1000000; i++) { + valstr[i] = slash::RandomString(val_size); } while (1) { if (f1->HasLeader()) { @@ -77,7 +89,6 @@ int main() pthread_t pid[24]; st = NowMicros(); - int thread_num = 4; for (int i = 0; i < thread_num; i++) { pthread_create(&pid[i], NULL, fun, NULL); } diff --git a/floyd/src/floyd_apply.cc b/floyd/src/floyd_apply.cc index 8d04d6b..d30d42c 100644 --- a/floyd/src/floyd_apply.cc +++ b/floyd/src/floyd_apply.cc @@ -53,9 +53,7 @@ void FloydApply::ApplyStateMachine() { uint64_t last_applied = context_->last_applied; // Apply as more entry as possible uint64_t commit_index; - context_->commit_index_mu.Lock(); commit_index = context_->commit_index; - context_->commit_index_mu.Unlock(); LOGV(DEBUG_LEVEL, info_log_, "FloydApply::ApplyStateMachine: last_applied: %lu, commit_index: %lu", last_applied, commit_index); diff --git a/floyd/src/floyd_context.cc b/floyd/src/floyd_context.cc index aa3511f..641e274 100644 --- a/floyd/src/floyd_context.cc +++ b/floyd/src/floyd_context.cc @@ -30,8 +30,8 @@ void FloydContext::BecomeFollower(uint64_t new_term, // when requestvote receive a large term, then we transfer from candidate to follower // then we should set voted_for_ip to the leader_ip // if (current_term < new_term) { - voted_for_ip = _leader_ip; - voted_for_port = _leader_port; + voted_for_ip = _leader_ip; + voted_for_port = _leader_port; // } current_term = new_term; leader_ip = _leader_ip; diff --git a/floyd/src/floyd_context.h b/floyd/src/floyd_context.h index f9cc862..7d01fbc 100644 --- a/floyd/src/floyd_context.h +++ b/floyd/src/floyd_context.h @@ -61,9 +61,6 @@ struct FloydContext { // mutex protect commit_index // used in floyd_apply thread and floyd_peer thread - // floyd_apply thread will get the commit_index - // floyd_peer thread will update the commit_index - slash::Mutex commit_index_mu; slash::Mutex global_mu; slash::Mutex apply_mu; slash::CondVar apply_cond; diff --git a/floyd/src/floyd_impl.cc b/floyd/src/floyd_impl.cc index 3b7d276..d3493ef 100644 --- a/floyd/src/floyd_impl.cc +++ b/floyd/src/floyd_impl.cc @@ -528,20 +528,23 @@ Status FloydImpl::ExecuteCommand(const CmdRequest& request, // Notify primary then wait for apply if (options_.single_mode) { - primary_->AddTask(kNewCommand); + context_->commit_index = last_log_index; + raft_meta_->SetCommitIndex(context_->commit_index); + apply_->ScheduleApply(); } else { primary_->AddTask(kNewCommand); } response->set_type(request.type()); response->set_code(StatusCode::kError); - context_->apply_mu.Lock(); + { + slash::MutexLock l(&context_->apply_mu); while (context_->last_applied < last_log_index) { if (!context_->apply_cond.TimedWait(1000)) { return Status::Timeout("FloydImpl::ExecuteCommand Timeout"); } } - context_->apply_mu.Unlock(); + } // Complete CmdRequest if needed std::string value; @@ -586,6 +589,7 @@ void FloydImpl::GrantVote(uint64_t term, const std::string ip, int port) { void FloydImpl::ReplyRequestVote(const CmdRequest& request, CmdResponse* response) { slash::MutexLock l(&context_->global_mu); + context_->last_op_time = slash::NowMicros(); bool granted = false; CmdRequest_RequestVote request_vote = request.request_vote(); LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyRequestVote: my_term=%lu request.term=%lu", @@ -625,16 +629,13 @@ void FloydImpl::ReplyRequestVote(const CmdRequest& request, CmdResponse* respons granted = true; LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyRequestVote: Grant my vote to %s:%d at term %lu", context_->voted_for_ip.c_str(), context_->voted_for_port, context_->current_term); - context_->last_op_time = slash::NowMicros(); BuildRequestVoteResponse(context_->current_term, granted, response); } bool FloydImpl::AdvanceFollowerCommitIndex(uint64_t new_commit_index) { // Update log commit index - context_->commit_index_mu.Lock(); context_->commit_index = new_commit_index; raft_meta_->SetCommitIndex(new_commit_index); - context_->commit_index_mu.Unlock(); return true; } @@ -642,6 +643,7 @@ void FloydImpl::ReplyAppendEntries(CmdRequest& request, CmdResponse* response) { bool success = false; CmdRequest_AppendEntries append_entries = request.append_entries(); slash::MutexLock l(&context_->global_mu); + context_->last_op_time = slash::NowMicros(); // Ignore stale term // if the append entries term is smaller then my current term, then the caller must an older leader uint64_t last_log_index = raft_log_->GetLastLogIndex(); @@ -697,23 +699,25 @@ void FloydImpl::ReplyAppendEntries(CmdRequest& request, CmdResponse* response) { raft_log_->TruncateSuffix(append_entries.prev_log_index() + 1); } - if (entries.size() > 0) { + if (append_entries.entries().size() > 0) { LOGV(DEBUG_LEVEL, info_log_, "RaftMeta::ReceiverDoAppendEntries: will append %u entries from " - " prev_log_index %lu", entries.size(), append_entries.prev_log_index() + 1); + " prev_log_index %lu", append_entries.entries().size(), append_entries.prev_log_index() + 1); if (raft_log_->Append(entries) <= 0) { context_->last_op_time = slash::NowMicros(); BuildAppendEntriesResponse(success, context_->current_term, raft_log_->GetLastLogIndex(), response); return ; } + AdvanceFollowerCommitIndex(append_entries.leader_commit()); + apply_->ScheduleApply(); + } else { + LOGV(INFO_LEVEL, info_log_, "FloydImpl::ReceiverDoAppendEntries: Receive PingPong AppendEntries from %s:%d", + append_entries.ip().c_str(), append_entries.port()); } success = true; // only when follower successfully do appendentries, we will update commit index - AdvanceFollowerCommitIndex(append_entries.leader_commit()); LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries after AdvanceCommitIndex %lu", context_->commit_index); - apply_->ScheduleApply(); - - context_->last_op_time = slash::NowMicros(); + // update last_op_time to avoid another leader election BuildAppendEntriesResponse(success, context_->current_term, raft_log_->GetLastLogIndex(), response); } diff --git a/floyd/src/floyd_options.cc b/floyd/src/floyd_options.cc index 15aa8dd..e3d3082 100644 --- a/floyd/src/floyd_options.cc +++ b/floyd/src/floyd_options.cc @@ -85,8 +85,8 @@ Options::Options() : local_ip("127.0.0.1"), local_port(10086), path("/data/floyd"), - check_leader_us(5000000), - heartbeat_us(1000000), + check_leader_us(6000000), + heartbeat_us(3000000), append_entries_size_once(1024), append_entries_count_once(24), single_mode(false) { @@ -98,8 +98,8 @@ Options::Options(const std::string& cluster_string, : local_ip(_local_ip), local_port(_local_port), path(_path), - check_leader_us(5000000), - heartbeat_us(1000000), + check_leader_us(6000000), + heartbeat_us(3000000), append_entries_size_once(1024), append_entries_count_once(24), single_mode(false) { diff --git a/floyd/src/floyd_peer_thread.cc b/floyd/src/floyd_peer_thread.cc index b04c975..3b337ae 100644 --- a/floyd/src/floyd_peer_thread.cc +++ b/floyd/src/floyd_peer_thread.cc @@ -25,7 +25,7 @@ namespace floyd { Peer::Peer(std::string server, FloydContext* context, FloydPrimary* primary, RaftMeta* raft_meta, RaftLog* raft_log, ClientPool* pool, FloydApply* apply, const Options& options, Logger* info_log) - : server_(server), + : peer_addr_(server), context_(context), primary_(primary), raft_meta_(raft_meta), @@ -41,12 +41,12 @@ Peer::Peer(std::string server, FloydContext* context, FloydPrimary* primary, Raf } int Peer::Start() { - bg_thread_.set_thread_name("FloydPeer" + server_.substr(server_.find(':'))); + bg_thread_.set_thread_name("FloydPeer" + peer_addr_.substr(peer_addr_.find(':'))); return bg_thread_.StartThread(); } Peer::~Peer() { - LOGV(INFO_LEVEL, info_log_, "Peer(%s) exit!!!", server_.c_str()); + LOGV(INFO_LEVEL, info_log_, "Peer(%s) exit!!!", peer_addr_.c_str()); } int Peer::Stop() { return bg_thread_.StopThread(); @@ -86,11 +86,11 @@ Status Peer::RequestVoteRPC() { } CmdResponse res; - Status result = pool_->SendAndRecv(server_, req, &res); + Status result = pool_->SendAndRecv(peer_addr_, req, &res); if (!result.ok()) { LOGV(DEBUG_LEVEL, info_log_, "RequestVote to %s failed %s", - server_.c_str(), result.ToString().c_str()); + peer_addr_.c_str(), result.ToString().c_str()); return result; } @@ -99,10 +99,13 @@ Status Peer::RequestVoteRPC() { if (context_->role == Role::kCandidate) { // kOk means RequestVote success, opposite vote for me if (res.request_vote_res().vote_granted() == true) { // granted - LOGV(INFO_LEVEL, info_log_, "Peer(%s)::RequestVote granted will Vote and check", server_.c_str()); + LOGV(INFO_LEVEL, info_log_, "Peer::RequestVoteRpc: Candidate %s:%d get vote from node %s", + options_.local_ip.c_str(), options_.local_port, peer_addr_.c_str()); // However, we need check whether this vote is vote for old term // we need igore these type of vote if (CheckAndVote(res.request_vote_res().term())) { + LOGV(INFO_LEVEL, info_log_, "Peer::RequestVoteRPC: Become leader %s:%d", + options_.local_ip.c_str(), options_.local_port, peer_addr_.c_str()); context_->BecomeLeader(); primary_->AddTask(kHeartBeat, false); } @@ -118,7 +121,7 @@ Status Peer::RequestVoteRPC() { // otherwise we will do nothing LOGV(DEBUG_LEVEL, info_log_, "Vote request denied by %s," " res_term=%lu, current_term=%lu", - server_.c_str(), res.request_vote_res().term(), context_->current_term); + peer_addr_.c_str(), res.request_vote_res().term(), context_->current_term); } } else { // TODO(ba0tiao) if i am not longer candidate @@ -135,7 +138,7 @@ uint64_t Peer::QuorumMatchIndex() { std::vector values; std::map::iterator iter; for (iter = peers_.begin(); iter != peers_.end(); iter++) { - if (iter->first == server_) { + if (iter->first == peer_addr_) { continue; } values.push_back(iter->second->match_index()); @@ -216,13 +219,13 @@ Status Peer::AppendEntriesRPC() { } CmdResponse res; - Status result = pool_->SendAndRecv(server_, req, &res); + Status result = pool_->SendAndRecv(peer_addr_, req, &res); { slash::MutexLock l(&context_->global_mu); if (!result.ok()) { LOGV(WARN_LEVEL, info_log_, "FloydPeerThread::AppendEntries: AppendEntry to %s failed %s", - server_.c_str(), result.ToString().c_str()); + peer_addr_.c_str(), result.ToString().c_str()); return result; } @@ -233,6 +236,9 @@ Status Peer::AppendEntriesRPC() { * receiver has higer term than myself, so turn from candidate to follower */ if (res.append_entries_res().term() > context_->current_term) { + LOGV(INFO_LEVEL, info_log_, "Peer::AppendEntriesRPC: %s:%d Transfer from Leader to Follower since get A larger term" + "from peer %s, local term is %d, peer term is %d", options_.local_ip.c_str(), options_.local_port, + peer_addr_.c_str(), context_->current_term, res.append_entries_res().term()); context_->BecomeFollower(res.append_entries_res().term()); raft_meta_->SetCurrentTerm(context_->current_term); raft_meta_->SetVotedForIp(context_->voted_for_ip); diff --git a/floyd/src/floyd_peer_thread.h b/floyd/src/floyd_peer_thread.h index 6f09873..856cf86 100644 --- a/floyd/src/floyd_peer_thread.h +++ b/floyd/src/floyd_peer_thread.h @@ -76,7 +76,7 @@ class Peer { uint64_t QuorumMatchIndex(); void AdvanceLeaderCommitIndex(); - std::string server_; + std::string peer_addr_; FloydContext* context_; FloydPrimary* primary_; RaftMeta* raft_meta_; diff --git a/floyd/src/floyd_primary_thread.cc b/floyd/src/floyd_primary_thread.cc index 5cbb461..94e07e0 100644 --- a/floyd/src/floyd_primary_thread.cc +++ b/floyd/src/floyd_primary_thread.cc @@ -27,7 +27,8 @@ namespace floyd { -FloydPrimary::FloydPrimary(FloydContext* context, RaftMeta* raft_meta, const Options& options, Logger* info_log) +FloydPrimary::FloydPrimary(FloydContext* context, RaftMeta* raft_meta, + const Options& options, Logger* info_log) : context_(context), raft_meta_(raft_meta), options_(options), @@ -54,7 +55,6 @@ int FloydPrimary::Stop() { void FloydPrimary::AddTask(TaskType type, bool is_delay) { switch (type) { case kHeartBeat: { - LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::AddTask HeartBeat"); if (is_delay) { uint64_t timeout = options_.heartbeat_us; bg_thread_.DelaySchedule(timeout / 1000LL, LaunchHeartBeatWrapper, this); @@ -64,7 +64,6 @@ void FloydPrimary::AddTask(TaskType type, bool is_delay) { break; } case kCheckLeader: { - LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::AddTask CheckLeader"); if (is_delay) { uint64_t timeout = options_.check_leader_us; bg_thread_.DelaySchedule(timeout / 1000LL, LaunchCheckLeaderWrapper, this); @@ -74,7 +73,6 @@ void FloydPrimary::AddTask(TaskType type, bool is_delay) { break; } case kNewCommand: { - LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::AddTask NewCommand"); bg_thread_.Schedule(LaunchNewCommandWrapper, this); break; } @@ -105,8 +103,16 @@ void FloydPrimary::LaunchCheckLeader() { if (context_->role == Role::kFollower || context_->role == Role::kCandidate) { if (options_.single_mode) { context_->BecomeLeader(); + context_->voted_for_ip = options_.local_ip; + context_->voted_for_port = options_.local_port; + raft_meta_->SetCurrentTerm(context_->current_term); + raft_meta_->SetVotedForIp(context_->voted_for_ip); + raft_meta_->SetVotedForPort(context_->voted_for_port); } else if (context_->last_op_time + options_.check_leader_us < slash::NowMicros()) { context_->BecomeCandidate(); + LOGV(INFO_LEVEL, info_log_, "FloydPrimary::LaunchCheckLeader: %s:%d Become Candidate because of timeout, new term is %d" + "voted for %s:%d", options_.local_ip.c_str(), options_.local_port, context_->current_term, + context_->voted_for_ip.c_str(), context_->voted_for_port); raft_meta_->SetCurrentTerm(context_->current_term); raft_meta_->SetVotedForIp(context_->voted_for_ip); raft_meta_->SetVotedForPort(context_->voted_for_port); From 4756bdb9e11d513afbe3deb68ccb4a5a50576b1e Mon Sep 17 00:00:00 2001 From: baotiao Date: Thu, 3 Aug 2017 11:34:56 +0800 Subject: [PATCH 12/17] update read_floyd, read_rock tool. support only read meta data, read the precise index value. upgrade floyd's log message, make the log more readably --- floyd/example/simple/t1.cc | 10 +++---- floyd/src/floyd_apply.cc | 4 +-- floyd/src/floyd_peer_thread.cc | 42 +++++++++++++++-------------- floyd/src/floyd_peer_thread.h | 3 +++ floyd/src/floyd_primary_thread.cc | 7 +++-- floyd/src/raft_log.cc | 4 +-- floyd/tools/read_floyd.cc | 44 +++++++++++++++++++++++++++++-- floyd/tools/read_rock.cc | 23 ++++++++++++++++ 8 files changed, 104 insertions(+), 33 deletions(-) diff --git a/floyd/example/simple/t1.cc b/floyd/example/simple/t1.cc index bc4280e..125f132 100644 --- a/floyd/example/simple/t1.cc +++ b/floyd/example/simple/t1.cc @@ -47,26 +47,26 @@ int main(int argc, char * argv[]) printf("multi threads test to get performance thread num %d key size %d\n", thread_num, val_size); - Options op("127.0.0.1:8907,127.0.0.1:8908,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8907, "./data1/"); + Options op("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8901, "./data1/"); Floyd *f2, *f3, *f4, *f5; op.Dump(); slash::Status s = Floyd::Open(op, &f1); printf("%s\n", s.ToString().c_str()); - Options op2("127.0.0.1:8907,127.0.0.1:8908,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8908, "./data2/"); + Options op2("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8902, "./data2/"); s = Floyd::Open(op2, &f2); printf("%s\n", s.ToString().c_str()); - Options op3("127.0.0.1:8907,127.0.0.1:8908,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8903, "./data3/"); + Options op3("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8903, "./data3/"); s = Floyd::Open(op3, &f3); printf("%s\n", s.ToString().c_str()); - Options op4("127.0.0.1:8907,127.0.0.1:8908,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8904, "./data4/"); + Options op4("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8904, "./data4/"); s = Floyd::Open(op4, &f4); printf("%s\n", s.ToString().c_str()); - Options op5("127.0.0.1:8907,127.0.0.1:8908,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8905, "./data5/"); + Options op5("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8905, "./data5/"); s = Floyd::Open(op5, &f5); printf("%s\n", s.ToString().c_str()); diff --git a/floyd/src/floyd_apply.cc b/floyd/src/floyd_apply.cc index d30d42c..571e100 100644 --- a/floyd/src/floyd_apply.cc +++ b/floyd/src/floyd_apply.cc @@ -57,16 +57,16 @@ void FloydApply::ApplyStateMachine() { LOGV(DEBUG_LEVEL, info_log_, "FloydApply::ApplyStateMachine: last_applied: %lu, commit_index: %lu", last_applied, commit_index); + Entry log_entry; while (last_applied < commit_index) { last_applied++; - Entry log_entry; raft_log_->GetEntry(last_applied, &log_entry); Status s = Apply(log_entry); if (!s.ok()) { LOGV(WARN_LEVEL, info_log_, "FloydApply::ApplyStateMachine: Apply log entry failed, at: %d, error: %s", last_applied, s.ToString().c_str()); - ScheduleApply(); // try once more usleep(1000000); + ScheduleApply(); // try once more return; } } diff --git a/floyd/src/floyd_peer_thread.cc b/floyd/src/floyd_peer_thread.cc index 3b337ae..58fdc69 100644 --- a/floyd/src/floyd_peer_thread.cc +++ b/floyd/src/floyd_peer_thread.cc @@ -89,7 +89,7 @@ Status Peer::RequestVoteRPC() { Status result = pool_->SendAndRecv(peer_addr_, req, &res); if (!result.ok()) { - LOGV(DEBUG_LEVEL, info_log_, "RequestVote to %s failed %s", + LOGV(DEBUG_LEVEL, info_log_, "Peer::RequestVoteRPC: RequestVote to %s failed %s", peer_addr_.c_str(), result.ToString().c_str()); return result; } @@ -99,29 +99,29 @@ Status Peer::RequestVoteRPC() { if (context_->role == Role::kCandidate) { // kOk means RequestVote success, opposite vote for me if (res.request_vote_res().vote_granted() == true) { // granted - LOGV(INFO_LEVEL, info_log_, "Peer::RequestVoteRpc: Candidate %s:%d get vote from node %s", - options_.local_ip.c_str(), options_.local_port, peer_addr_.c_str()); + LOGV(INFO_LEVEL, info_log_, "Peer::RequestVoteRpc: Candidate %s:%d get vote from node %s at term %d", + options_.local_ip.c_str(), options_.local_port, peer_addr_.c_str(), context_->current_term); // However, we need check whether this vote is vote for old term // we need igore these type of vote if (CheckAndVote(res.request_vote_res().term())) { - LOGV(INFO_LEVEL, info_log_, "Peer::RequestVoteRPC: Become leader %s:%d", - options_.local_ip.c_str(), options_.local_port, peer_addr_.c_str()); context_->BecomeLeader(); + LOGV(INFO_LEVEL, info_log_, "Peer::RequestVoteRPC: %s:%d become leader at term %d", + options_.local_ip.c_str(), options_.local_port, context_->current_term); primary_->AddTask(kHeartBeat, false); } } else { if (res.request_vote_res().term() > context_->current_term) { + // opposite RequestVote fail, maybe opposite has larger term, or opposite has + // longer log. if opposite has larger term, this node will become follower + // otherwise we will do nothing + LOGV(INFO_LEVEL, info_log_, "Peer::RequestVoteRPC: Candidate %s:%d vote request denied by %s," + " res_term=%lu, current_term=%lu", options_.local_ip.c_str(), options_.local_port, + peer_addr_.c_str(), res.request_vote_res().term(), context_->current_term); context_->BecomeFollower(res.request_vote_res().term()); raft_meta_->SetCurrentTerm(context_->current_term); raft_meta_->SetVotedForIp(context_->voted_for_ip); raft_meta_->SetVotedForPort(context_->voted_for_port); } - // opposite RequestVote fail, maybe opposite has larger term, or opposite has - // longer log. if opposite has larger term, this node will become follower - // otherwise we will do nothing - LOGV(DEBUG_LEVEL, info_log_, "Vote request denied by %s," - " res_term=%lu, current_term=%lu", - peer_addr_.c_str(), res.request_vote_res().term(), context_->current_term); } } else { // TODO(ba0tiao) if i am not longer candidate @@ -176,8 +176,8 @@ Status Peer::AppendEntriesRPC() { if (prev_log_index != 0) { Entry entry; if (raft_log_->GetEntry(prev_log_index, &entry) != 0) { - LOGV(WARN_LEVEL, info_log_, "Peer::AppendEntriesRPC:GetEntry index %llu " - "not found", prev_log_index); + LOGV(WARN_LEVEL, info_log_, "Peer::AppendEntriesRPC: Get my(%s:%d) Entry index %llu " + "not found", options_.local_ip.c_str(), options_.local_port, prev_log_index); } else { prev_log_term = entry.term(); } @@ -192,14 +192,16 @@ Status Peer::AppendEntriesRPC() { last_log_index = raft_log_->GetLastLogIndex(); Entry *tmp_entry = new Entry(); - LOGV(DEBUG_LEVEL, info_log_, "next_index_ %llu, last_log_index %llu", next_index_.load(), last_log_index); + LOGV(DEBUG_LEVEL, info_log_, "Peer::AppendEntriesRPC: peer_addr(%s)'s next_index_ %llu, my last_log_index %llu", + peer_addr_.c_str(), next_index_.load(), last_log_index); for (uint64_t index = next_index_; index <= last_log_index; index++) { if (raft_log_->GetEntry(index, tmp_entry) == 0) { // TODO(ba0tiao) how to avoid memory copy here Entry *entry = append_entries->add_entries(); *entry = *tmp_entry; } else { - LOGV(WARN_LEVEL, info_log_, "FloydPeerThread::AppendEntries: can't get Entry from raft_log, index %lld", index); + LOGV(WARN_LEVEL, info_log_, "Peer::AppendEntriesRPC: peer_addr %s can't get Entry ", + "from raft_log, index %lld", peer_addr_.c_str(), index); break; } @@ -224,8 +226,8 @@ Status Peer::AppendEntriesRPC() { { slash::MutexLock l(&context_->global_mu); if (!result.ok()) { - LOGV(WARN_LEVEL, info_log_, "FloydPeerThread::AppendEntries: AppendEntry to %s failed %s", - peer_addr_.c_str(), result.ToString().c_str()); + LOGV(WARN_LEVEL, info_log_, "Peer::AppendEntries: Candidate %s:%d SendAndRecv to %s failed %s", + options_.local_ip.c_str(), options_.local_port, peer_addr_.c_str(), result.ToString().c_str()); return result; } @@ -260,9 +262,9 @@ Status Peer::AppendEntriesRPC() { int pri_size, qu_size; bg_thread_.QueueSize(&pri_size, &qu_size); if (qu_size < 1) { - LOGV(DEBUG_LEVEL, info_log_, "AppendEntry again " + LOGV(DEBUG_LEVEL, info_log_, "Peer::AppendEntriesRPC: peer_addr %s AppendEntry again " "to catch up next_index(%llu) last_log_index(%llu)", - next_index_.load(), last_log_index); + peer_addr_.c_str(), next_index_.load(), last_log_index); AddAppendEntriesTask(); } } @@ -274,7 +276,7 @@ Status Peer::AppendEntriesRPC() { // Prev log don't match, so we retry with more prev one according to // response next_index_ = adjust_index; - LOGV(INFO_LEVEL, info_log_, "update next_index_ %lld", next_index_.load()); + LOGV(INFO_LEVEL, info_log_, "Peer::AppendEntriesRPC: peer_addr %s Update next_index_ %lld", peer_addr_.c_str(), next_index_.load()); AddAppendEntriesTask(); } } diff --git a/floyd/src/floyd_peer_thread.h b/floyd/src/floyd_peer_thread.h index 856cf86..487ab10 100644 --- a/floyd/src/floyd_peer_thread.h +++ b/floyd/src/floyd_peer_thread.h @@ -69,6 +69,9 @@ class Peer { void set_peers(PeersSet &peers) { peers_ = peers; } + std::string peer_addr() const { + return peer_addr_; + } private: diff --git a/floyd/src/floyd_primary_thread.cc b/floyd/src/floyd_primary_thread.cc index 94e07e0..f32ea2b 100644 --- a/floyd/src/floyd_primary_thread.cc +++ b/floyd/src/floyd_primary_thread.cc @@ -140,11 +140,14 @@ void FloydPrimary::NoticePeerTask(TaskType type) { for (auto& peer : peers_) { switch (type) { case kHeartBeat: - LOGV(INFO_LEVEL, info_log_, "FloydPrimary::NoticePeerTask send request vote message"); + LOGV(INFO_LEVEL, info_log_, "FloydPrimary::NoticePeerTask server %s:%d send request vote message to %s", + options_.local_ip.c_str(), options_.local_port, peer.second->peer_addr().c_str()); peer.second->AddRequestVoteTask(); break; case kNewCommand: - LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::NoticePeerTask send appendEntries message"); + LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::NoticePeerTask server %s:%d send appendEntries message to %s", + options_.local_ip.c_str(), options_.local_port, peer.second->peer_addr().c_str()); + peer.second->AddAppendEntriesTask(); break; default: diff --git a/floyd/src/raft_log.cc b/floyd/src/raft_log.cc index fd25674..a119b5b 100644 --- a/floyd/src/raft_log.cc +++ b/floyd/src/raft_log.cc @@ -66,7 +66,7 @@ uint64_t RaftLog::Append(const std::vector &entries) { last_log_index_++; s = db_->Put(rocksdb::WriteOptions(), UintToBitStr(last_log_index_), buf); if (!s.ok()) { - LOGV(ERROR_LEVEL, info_log_, "RaftLog::Append false\n"); + LOGV(ERROR_LEVEL, info_log_, "RaftLog::Append %lu false\n", last_log_index_); } } return last_log_index_; @@ -112,7 +112,7 @@ bool RaftLog::GetLastLogTermAndIndex(uint64_t* last_log_term, uint64_t* last_log } int RaftLog::TruncateSuffix(uint64_t index) { - // here we need to delete the unnecessary entry, since we don't store + // we need to delete the unnecessary entry, since we don't store // last_log_index in rocksdb for (uint64_t i = index; i <= last_log_index_; i++) { db_->Delete(rocksdb::WriteOptions(), UintToBitStr(i)); diff --git a/floyd/tools/read_floyd.cc b/floyd/tools/read_floyd.cc index 13a840e..9978915 100644 --- a/floyd/tools/read_floyd.cc +++ b/floyd/tools/read_floyd.cc @@ -1,4 +1,7 @@ #include +#include +#include +#include #include "rocksdb/db.h" #include "floyd/src/floyd.pb.h" @@ -26,10 +29,47 @@ int main(int argc, char** argv) std::cout << argv[1] << std::endl; rocksdb::Status s = rocksdb::DB::Open(options, argv[1], &db); rocksdb::Iterator* iter = db->NewIterator(rocksdb::ReadOptions()); + char c; + bool is_meta = false; + int index = 0; + floyd::Entry entry; + while ((c = getopt(argc, argv, "mi:")) != EOF) { + switch (c) { + case 'm': + is_meta = true; + break; + case 'i': + index = atoi(optarg); + } + } + if (is_meta) { + iter->SeekToLast(); + for (int i = 0; i < 5; i++) { + if (iter->key().ToString() == "VOTEFORIP") { + printf("key %s, value %s\n", iter->key().ToString().c_str(), iter->value().ToString().c_str()); + } else { + uint64_t ans; + memcpy(&ans, iter->value().data(), sizeof(uint64_t)); + printf("key %s, value %lu\n", iter->key().ToString().c_str(), ans); + } + iter->Prev(); + } + return 0; + } + if (index) { + std::string val; + rocksdb::Status s = db->Get(rocksdb::ReadOptions(), UintToBitStr(uint64_t(index)), &val); + if (s.IsNotFound()) { + printf("key %d not found\n", index); + } else { + entry.ParseFromString(val); + printf("index %d entry term: %lu key %s value %s\n", index, entry.term(), entry.key().c_str(), entry.value().c_str()); + } + return 0; + } int cnt = 0; for (iter->SeekToFirst(); iter->Valid(); iter->Next()) { cnt++; - floyd::Entry entry; if (iter->key().ToString() == "CURRENTTERM" || iter->key().ToString() == "VOTEFORIP" || iter->key().ToString() == "VOTEFORPORT" || iter->key().ToString() == "APPLYINDEX" || iter->key().ToString() == "COMMITINDEX") { if (iter->key().ToString() == "VOTEFORIP") { printf("key %s, value %s\n", iter->key().ToString().c_str(), iter->value().ToString().c_str()); @@ -41,7 +81,7 @@ int main(int argc, char** argv) } else { entry.ParseFromString(iter->value().ToString()); uint64_t num = BitStrToUint(iter->key().ToString()); - printf("key %lu entry term: %lu key %s value %s\n", num, entry.term(), entry.key().c_str(), entry.value().c_str()); + printf("index %lu entry term: %lu key %s value %s\n", num, entry.term(), entry.key().c_str(), entry.value().c_str()); } // std::cout << "res " << iter->key().ToString() << ": " << iter->value().ToString() << std::endl; } diff --git a/floyd/tools/read_rock.cc b/floyd/tools/read_rock.cc index ff9ff93..9f39f31 100644 --- a/floyd/tools/read_rock.cc +++ b/floyd/tools/read_rock.cc @@ -1,4 +1,7 @@ #include +#include +#include +#include #include "rocksdb/db.h" using namespace rocksdb; @@ -10,6 +13,26 @@ int main(int argc, char** argv) std::cout << argv[1] << std::endl; rocksdb::Status s = rocksdb::DB::Open(options, argv[1], &db); rocksdb::Iterator* iter = db->NewIterator(rocksdb::ReadOptions()); + char c; + int index = 0; + std::string key = ""; + while ((c = getopt(argc, argv, "i:")) != EOF) { + switch (c) { + case 'i': + key = std::string(optarg, strlen(optarg)); + break; + } + } + if (key != "") { + std::string val; + rocksdb::Status s = db->Get(rocksdb::ReadOptions(), key, &val); + if (s.IsNotFound()) { + printf("key %s not found\n", key.c_str()); + } else { + printf("key %s, val %s\n", key.c_str(), val.c_str()); + } + return 0; + } int cnt = 0; for (iter->SeekToFirst(); iter->Valid(); iter->Next()) { cnt++; From ac03ff62e4cf059a4ad03ce50a6b79885e113514 Mon Sep 17 00:00:00 2001 From: baotiao Date: Thu, 3 Aug 2017 11:36:35 +0800 Subject: [PATCH 13/17] add more examples --- floyd/example/simple/t3.cc | 79 ++++++++++++++++++++++++++++++++++++++ floyd/example/simple/t4.cc | 63 ++++++++++++++++++++++++++++++ floyd/example/simple/t5.cc | 59 ++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+) create mode 100644 floyd/example/simple/t3.cc create mode 100644 floyd/example/simple/t4.cc create mode 100644 floyd/example/simple/t5.cc diff --git a/floyd/example/simple/t3.cc b/floyd/example/simple/t3.cc new file mode 100644 index 0000000..9b54b89 --- /dev/null +++ b/floyd/example/simple/t3.cc @@ -0,0 +1,79 @@ +#include +#include +#include + +#include +#include + +#include "floyd/include/floyd.h" +#include "slash/include/testutil.h" + +using namespace floyd; +uint64_t NowMicros() { + struct timeval tv; + gettimeofday(&tv, NULL); + return static_cast(tv.tv_sec) * 1000000 + tv.tv_usec; +} +std::string mystr[100100]; + +int main() +{ + Options op("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8901, "./data1/"); + op.Dump(); + + Floyd *f1, *f2, *f3, *f4, *f5; + + slash::Status s; + s = Floyd::Open(op, &f1); + printf("%s\n", s.ToString().c_str()); + + Options op2("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8902, "./data2/"); + s = Floyd::Open(op2, &f2); + printf("%s\n", s.ToString().c_str()); + + Options op3("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8903, "./data3/"); + s = Floyd::Open(op3, &f3); + printf("%s\n", s.ToString().c_str()); + + Options op4("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8904, "./data4/"); + s = Floyd::Open(op4, &f4); + printf("%s\n", s.ToString().c_str()); + + Options op5("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8905, "./data5/"); + s = Floyd::Open(op5, &f5); + printf("%s\n", s.ToString().c_str()); + + std::string msg; + int i = 100; + uint64_t st = NowMicros(), ed; + for (int i = 0; i < 100000; i++) { + mystr[i] = slash::RandomString(10); + } + + while (1) { + if (f1->HasLeader()) { + break; + } + sleep(2); + } + + // ran at the begining + printf("run 5 times, every time write 100 item. at the beginning state\n"); + i = 5; + std::string val; + while (i--) { + f3->GetServerStatus(msg); + for (int j = 0; j < 100000; j++) { + f3->Write(mystr[j], mystr[j]); + s = f3->Read(mystr[j], val); + printf("status %s val %s\n", s.ToString().c_str(), val.c_str()); + } + printf("%s\n", msg.c_str()); + } + + // sleep(1); + + + getchar(); + return 0; +} diff --git a/floyd/example/simple/t4.cc b/floyd/example/simple/t4.cc new file mode 100644 index 0000000..2bf1b9f --- /dev/null +++ b/floyd/example/simple/t4.cc @@ -0,0 +1,63 @@ +#include +#include +#include + +#include +#include + +#include "floyd/include/floyd.h" +#include "slash/include/testutil.h" + +using namespace floyd; +uint64_t NowMicros() { + struct timeval tv; + gettimeofday(&tv, NULL); + return static_cast(tv.tv_sec) * 1000000 + tv.tv_usec; +} + +int main() +{ + Options op("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8901, "./data1/"); + op.Dump(); + + Floyd *f1, *f2, *f3, *f4, *f5; + + slash::Status s; + s = Floyd::Open(op, &f1); + printf("%s\n", s.ToString().c_str()); + + usleep(100000); + Options op2("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8902, "./data2/"); + s = Floyd::Open(op2, &f2); + printf("%s\n", s.ToString().c_str()); + + usleep(200000); + Options op3("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8903, "./data3/"); + s = Floyd::Open(op3, &f3); + printf("%s\n", s.ToString().c_str()); + + usleep(300000); + Options op4("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8904, "./data4/"); + s = Floyd::Open(op4, &f4); + printf("%s\n", s.ToString().c_str()); + + usleep(400000); + Options op5("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8905, "./data5/"); + s = Floyd::Open(op5, &f5); + printf("%s\n", s.ToString().c_str()); + + std::string msg; + while (1) { + f1->GetServerStatus(msg); + printf("%s\n", msg.c_str()); + sleep(2); + } + getchar(); + delete f2; + delete f3; + delete f4; + delete f5; + delete f1; + + return 0; +} diff --git a/floyd/example/simple/t5.cc b/floyd/example/simple/t5.cc new file mode 100644 index 0000000..6711bc0 --- /dev/null +++ b/floyd/example/simple/t5.cc @@ -0,0 +1,59 @@ +#include +#include +#include + +#include +#include + +#include "floyd/include/floyd.h" +#include "slash/include/testutil.h" + +using namespace floyd; +uint64_t NowMicros() { + struct timeval tv; + gettimeofday(&tv, NULL); + return static_cast(tv.tv_sec) * 1000000 + tv.tv_usec; +} + +int main() +{ + printf("testing single mode floyd, including starting a node and writing data\n"); + Options op("127.0.0.1:8901", "127.0.0.1", 8901, "./data1/"); + op.Dump(); + + Floyd *f1; + + op.single_mode = true; + slash::Status s; + s = Floyd::Open(op, &f1); + printf("%s\n", s.ToString().c_str()); + + std::string msg; + int cnt = 10; + while (cnt--) { + f1->GetServerStatus(msg); + printf("%s\n", msg.c_str()); + sleep(2); + } + uint64_t st, ed; + std::string mystr[100100]; + for (int i = 0; i < 100000; i++) { + mystr[i] = slash::RandomString(10); + } + f1->GetServerStatus(msg); + printf("%s\n", msg.c_str()); + st = NowMicros(); + for (int j = 0; j < 100000; j++) { + slash::Status ws = f1->Write(mystr[j], mystr[j]); + if (!ws.ok()) { + printf("floyd write error\n"); + } + // f1->Write("zz", "zz"); + } + ed = NowMicros(); + printf("write 100000 cost time microsecond(us) %ld, qps %llu\n", ed - st, 100000 * 1000000LL / (ed - st)); + getchar(); + delete f1; + + return 0; +} From f27442453e3ac9b59abc57d9346719ebd012d553 Mon Sep 17 00:00:00 2001 From: baotiao Date: Mon, 7 Aug 2017 03:39:44 +0800 Subject: [PATCH 14/17] add another construct log tool --- floyd/Makefile | 2 +- floyd/example/simple/t4.cc | 10 +++ floyd/src/floyd_apply.cc | 2 + floyd/src/floyd_client_pool.cc | 2 +- floyd/src/floyd_impl.cc | 55 ++++++++-------- floyd/src/floyd_peer_thread.cc | 27 ++++---- floyd/src/floyd_peer_thread.h | 4 +- floyd/src/floyd_primary_thread.cc | 10 +-- floyd/src/raft_log.cc | 14 +++-- floyd/src/raft_meta.cc | 1 - floyd/tools/Makefile | 6 +- floyd/tools/cl.cc | 100 ++++++++++++++++++++++++++++++ floyd/tools/construct_log.cc | 19 ------ floyd/tools/read_floyd.cc | 1 - 14 files changed, 177 insertions(+), 76 deletions(-) create mode 100644 floyd/tools/cl.cc delete mode 100644 floyd/tools/construct_log.cc diff --git a/floyd/Makefile b/floyd/Makefile index d9ce641..f487801 100755 --- a/floyd/Makefile +++ b/floyd/Makefile @@ -77,7 +77,7 @@ $(LIBRARY): $(LIBSLASH) $(LIBPINK) $(LIBROCKSDB) $(LIBOBJS) $(OBJS) $(OBJS): %.o : %.cc $(CXX) $(CXXFLAGS) -c $< -o $@ $(INCLUDE_PATH) -clean: +clean: rm -rf $(SRC_DIR)/*.o rm -rf $(LIB_OUTPUT)/* diff --git a/floyd/example/simple/t4.cc b/floyd/example/simple/t4.cc index 2bf1b9f..ddd660d 100644 --- a/floyd/example/simple/t4.cc +++ b/floyd/example/simple/t4.cc @@ -46,9 +46,19 @@ int main() s = Floyd::Open(op5, &f5); printf("%s\n", s.ToString().c_str()); + bool is_writed = false; std::string msg; while (1) { f1->GetServerStatus(msg); + /* + * if (is_writed == false) { + * // writing one record to update the meta data + * slash::Status s = f1->Write("lastdata", "lastdata"); + * if (s.ok()) { + * is_writed = true; + * } + * } + */ printf("%s\n", msg.c_str()); sleep(2); } diff --git a/floyd/src/floyd_apply.cc b/floyd/src/floyd_apply.cc index 571e100..2b0b920 100644 --- a/floyd/src/floyd_apply.cc +++ b/floyd/src/floyd_apply.cc @@ -34,6 +34,7 @@ FloydApply::~FloydApply() { int FloydApply::Start() { bg_thread_.set_thread_name("FloydApply"); + // bg_thread_.DelaySchedule(3000, ApplyStateMachineWrapper, this); return bg_thread_.StartThread(); } @@ -75,6 +76,7 @@ void FloydApply::ApplyStateMachine() { raft_meta_->SetLastApplied(last_applied); context_->apply_mu.Unlock(); context_->apply_cond.SignalAll(); + // bg_thread_.DelaySchedule(3000, ApplyStateMachineWrapper, this); } Status FloydApply::Apply(const Entry& entry) { diff --git a/floyd/src/floyd_client_pool.cc b/floyd/src/floyd_client_pool.cc index 191271f..0649057 100644 --- a/floyd/src/floyd_client_pool.cc +++ b/floyd/src/floyd_client_pool.cc @@ -22,7 +22,7 @@ ClientPool::ClientPool(Logger* info_log, int timeout_ms, int retry) } Status ClientPool::SendAndRecv(const std::string& server, const CmdRequest& req, CmdResponse* res) { - LOGV(DEBUG_LEVEL, info_log_, "Client::SendAndRecv %s cmd to %s", CmdType(req).c_str(), server.c_str()); + LOGV(DEBUG_LEVEL, info_log_, "ClientPool::SendAndRecv Send %s command to server %s", CmdType(req).c_str(), server.c_str()); Status ret; char stage = 0; Client *client = GetClient(server); diff --git a/floyd/src/floyd_impl.cc b/floyd/src/floyd_impl.cc index d3493ef..4b4e0e3 100644 --- a/floyd/src/floyd_impl.cc +++ b/floyd/src/floyd_impl.cc @@ -9,6 +9,7 @@ #include #include +#include #include "pink/include/bg_thread.h" #include "slash/include/env.h" @@ -133,8 +134,7 @@ Status FloydImpl::Init() { // Create peer threads // peers_.clear(); - for (auto iter = options_.members.begin(); - iter != options_.members.end(); iter++) { + for (auto iter = options_.members.begin(); iter != options_.members.end(); iter++) { if (!IsSelf(*iter)) { Peer* pt = new Peer(*iter, context_, primary_, raft_meta_, raft_log_, worker_client_pool_, apply_, options_, info_log_); @@ -356,11 +356,9 @@ bool FloydImpl::GetServerStatus(std::string& msg) { char str[512]; snprintf (str, 512, - " Node | Role | Term | CommitIdx | Leader | VoteFor | LastLogTerm | LastLogIdx | CommitIndex | LastApplied |\n" - "%15s:%-6d %9s %10lu %10lu %15s:%-6d %15s:%-6d %10lu %10lu %10lu %10lu\n", - options_.local_ip.c_str(), options_.local_port, - server_status.role().c_str(), - server_status.term(), server_status.commit_index(), + " Node | Role | Term | Leader | VoteFor | LastLogTerm | LastLogIdx | CommitIndex | LastApplied |\n" + "%15s:%-6d%10s%7lu%14s:%-6d%14s:%-d%10lu%13lu%14lu%13lu\n", + options_.local_ip.c_str(), options_.local_port, server_status.role().c_str(), server_status.term(), server_status.leader_ip().c_str(), server_status.leader_port(), server_status.voted_for_ip().c_str(), server_status.voted_for_port(), server_status.last_log_term(), server_status.last_log_index(), server_status.commit_index(), @@ -384,16 +382,13 @@ bool FloydImpl::GetServerStatus(std::string& msg) { slash::ParseIpPortString(iter, ip, port); CmdResponse_ServerStatus server_status = response.server_status(); snprintf (str, 512, - "%15s:%-6d %9s %10lu %10lu %15s:%-6d %15s:%-6d %10lu %10lu %10lu %10lu\n", - ip.c_str(), port, - server_status.role().c_str(), - server_status.term(), server_status.commit_index(), + "%15s:%-6d%10s%7lu%14s:%-6d%14s:%-d%10lu%13lu%14lu%13lu\n", + ip.c_str(), port, server_status.role().c_str(), server_status.term(), server_status.leader_ip().c_str(), server_status.leader_port(), server_status.voted_for_ip().c_str(), server_status.voted_for_port(), server_status.last_log_term(), server_status.last_log_index(), server_status.commit_index(), server_status.last_applied()); msg.append(str); - LOGV(DEBUG_LEVEL, info_log_, "GetServerStatus msg(%s)", str); } } } @@ -632,10 +627,14 @@ void FloydImpl::ReplyRequestVote(const CmdRequest& request, CmdResponse* respons BuildRequestVoteResponse(context_->current_term, granted, response); } -bool FloydImpl::AdvanceFollowerCommitIndex(uint64_t new_commit_index) { +bool FloydImpl::AdvanceFollowerCommitIndex(uint64_t leader_commit) { // Update log commit index - context_->commit_index = new_commit_index; - raft_meta_->SetCommitIndex(new_commit_index); + /* + * If leaderCommit > commitIndex, set commitIndex = + * min(leaderCommit, index of last new entry) + */ + context_->commit_index = std::min(leader_commit, raft_log_->GetLastLogIndex()); + raft_meta_->SetCommitIndex(context_->commit_index); return true; } @@ -643,6 +642,7 @@ void FloydImpl::ReplyAppendEntries(CmdRequest& request, CmdResponse* response) { bool success = false; CmdRequest_AppendEntries append_entries = request.append_entries(); slash::MutexLock l(&context_->global_mu); + // update last_op_time to avoid another leader election context_->last_op_time = slash::NowMicros(); // Ignore stale term // if the append entries term is smaller then my current term, then the caller must an older leader @@ -659,7 +659,7 @@ void FloydImpl::ReplyAppendEntries(CmdRequest& request, CmdResponse* response) { } if (append_entries.prev_log_index() > last_log_index) { - LOGV(INFO_LEVEL, info_log_, "RaftMeta::ReceiverDoAppendEntries:" + LOGV(INFO_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries:" "pre_log(%lu, %lu) > last_log_index(%lu)", append_entries.prev_log_term(), append_entries.prev_log_index(), last_log_index); BuildAppendEntriesResponse(success, context_->current_term, last_log_index, response); @@ -672,7 +672,7 @@ void FloydImpl::ReplyAppendEntries(CmdRequest& request, CmdResponse* response) { } uint64_t my_log_term = 0; Entry entry; - LOGV(DEBUG_LEVEL, info_log_, "RaftMeta::ReceiverDoAppendEntries " + LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries " "prev_log_index: %llu\n", append_entries.prev_log_index()); if (append_entries.prev_log_index() == 0) { my_log_term = 0; @@ -686,38 +686,41 @@ void FloydImpl::ReplyAppendEntries(CmdRequest& request, CmdResponse* response) { } if (append_entries.prev_log_term() != my_log_term) { - LOGV(WARN_LEVEL, info_log_, "RaftMeta::ReceiverDoAppendEntries: pre_log(%lu, %lu) don't match with" - " local log(%lu, %lu), truncate suffix from here", - append_entries.prev_log_term(), append_entries.prev_log_index(), my_log_term, last_log_index); + LOGV(WARN_LEVEL, info_log_, "FloydImpl::ReplyAppentries: leader %s:%d pre_log(%lu, %lu)'s term don't match with" + " my log(%lu, %lu) term, truncate my log from here %lu",append_entries.ip().c_str(), append_entries.port(), + append_entries.prev_log_term(), append_entries.prev_log_index(), my_log_term, last_log_index, + append_entries.prev_log_index()); // TruncateSuffix [prev_log_index, last_log_index) raft_log_->TruncateSuffix(append_entries.prev_log_index()); } // Append entry if (append_entries.prev_log_index() < last_log_index) { - // TruncateSuffix [prev_log_index + 1, last_log_index) + LOGV(WARN_LEVEL, info_log_, "FloydImpl::ReplyAppentries: leader %s:%d pre_log(%lu, %lu)'s index smaller than" + " my log(%lu, %lu) index, truncate suffix from here %lu", append_entries.ip().c_str(), append_entries.port(), + append_entries.prev_log_term(), append_entries.prev_log_index(), my_log_term, last_log_index, + append_entries.prev_log_index() + 1); raft_log_->TruncateSuffix(append_entries.prev_log_index() + 1); } if (append_entries.entries().size() > 0) { - LOGV(DEBUG_LEVEL, info_log_, "RaftMeta::ReceiverDoAppendEntries: will append %u entries from " - " prev_log_index %lu", append_entries.entries().size(), append_entries.prev_log_index() + 1); + LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries: leader %s:%d will append %u entries from " + " prev_log_index %lu", append_entries.ip().c_str(), append_entries.port(), + append_entries.entries().size(), append_entries.prev_log_index()); if (raft_log_->Append(entries) <= 0) { - context_->last_op_time = slash::NowMicros(); BuildAppendEntriesResponse(success, context_->current_term, raft_log_->GetLastLogIndex(), response); return ; } AdvanceFollowerCommitIndex(append_entries.leader_commit()); apply_->ScheduleApply(); } else { - LOGV(INFO_LEVEL, info_log_, "FloydImpl::ReceiverDoAppendEntries: Receive PingPong AppendEntries from %s:%d", + LOGV(INFO_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries: Receive PingPong AppendEntries from %s:%d", append_entries.ip().c_str(), append_entries.port()); } success = true; // only when follower successfully do appendentries, we will update commit index LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries after AdvanceCommitIndex %lu", context_->commit_index); - // update last_op_time to avoid another leader election BuildAppendEntriesResponse(success, context_->current_term, raft_log_->GetLastLogIndex(), response); } diff --git a/floyd/src/floyd_peer_thread.cc b/floyd/src/floyd_peer_thread.cc index 58fdc69..a21becd 100644 --- a/floyd/src/floyd_peer_thread.cc +++ b/floyd/src/floyd_peer_thread.cc @@ -68,7 +68,7 @@ void Peer::RequestVoteRPCWrapper(void *arg) { reinterpret_cast(arg)->RequestVoteRPC(); } -Status Peer::RequestVoteRPC() { +void Peer::RequestVoteRPC() { uint64_t last_log_term; uint64_t last_log_index; CmdRequest req; @@ -91,7 +91,7 @@ Status Peer::RequestVoteRPC() { if (!result.ok()) { LOGV(DEBUG_LEVEL, info_log_, "Peer::RequestVoteRPC: RequestVote to %s failed %s", peer_addr_.c_str(), result.ToString().c_str()); - return result; + return ; } { @@ -127,11 +127,7 @@ Status Peer::RequestVoteRPC() { // TODO(ba0tiao) if i am not longer candidate } } - return result; -} - -void Peer::AddAppendEntriesTask() { - bg_thread_.Schedule(&AppendEntriesRPCWrapper, this); + return ; } uint64_t Peer::QuorumMatchIndex() { @@ -158,12 +154,13 @@ void Peer::AdvanceLeaderCommitIndex() { } return; } - +void Peer::AddAppendEntriesTask() { + bg_thread_.Schedule(&AppendEntriesRPCWrapper, this); +} void Peer::AppendEntriesRPCWrapper(void *arg) { reinterpret_cast(arg)->AppendEntriesRPC(); } - -Status Peer::AppendEntriesRPC() { +void Peer::AppendEntriesRPC() { uint64_t prev_log_index = next_index_ - 1; uint64_t num_entries = 0; uint64_t prev_log_term = 0; @@ -228,7 +225,7 @@ Status Peer::AppendEntriesRPC() { if (!result.ok()) { LOGV(WARN_LEVEL, info_log_, "Peer::AppendEntries: Candidate %s:%d SendAndRecv to %s failed %s", options_.local_ip.c_str(), options_.local_port, peer_addr_.c_str(), result.ToString().c_str()); - return result; + return ; } // here we may get a larger term, and transfer to follower @@ -270,13 +267,17 @@ Status Peer::AppendEntriesRPC() { } } } else { + LOGV(INFO_LEVEL, info_log_, "Peer::AppEntriesRPC: peer_addr %s Send AppEntriesRPC failed," + "peer's last_log_index %lu, peer's next_index_ %lu", + peer_addr_.c_str(), res.append_entries_res().last_log_index(), next_index_.load()); uint64_t adjust_index = std::min(res.append_entries_res().last_log_index() + 1, next_index_ - 1); if (adjust_index > 0) { // Prev log don't match, so we retry with more prev one according to // response next_index_ = adjust_index; - LOGV(INFO_LEVEL, info_log_, "Peer::AppendEntriesRPC: peer_addr %s Update next_index_ %lld", peer_addr_.c_str(), next_index_.load()); + LOGV(INFO_LEVEL, info_log_, "Peer::AppEntriesRPC: peer_addr %s Adjust peer next_index_, Now next_index_ is %lu", + peer_addr_.c_str(), adjust_index); AddAppendEntriesTask(); } } @@ -285,7 +286,7 @@ Status Peer::AppendEntriesRPC() { } else if (context_->role == Role::kCandidate) { } } - return result; + return ; } } // namespace floyd diff --git a/floyd/src/floyd_peer_thread.h b/floyd/src/floyd_peer_thread.h index 487ab10..c475d07 100644 --- a/floyd/src/floyd_peer_thread.h +++ b/floyd/src/floyd_peer_thread.h @@ -48,10 +48,10 @@ class Peer { * the response to these two RPC at floyd_impl.h */ static void AppendEntriesRPCWrapper(void *arg); - Status AppendEntriesRPC(); + void AppendEntriesRPC(); // Request Vote static void RequestVoteRPCWrapper(void *arg); - Status RequestVoteRPC(); + void RequestVoteRPC(); uint64_t GetMatchIndex(); diff --git a/floyd/src/floyd_primary_thread.cc b/floyd/src/floyd_primary_thread.cc index f32ea2b..ba0663a 100644 --- a/floyd/src/floyd_primary_thread.cc +++ b/floyd/src/floyd_primary_thread.cc @@ -111,7 +111,7 @@ void FloydPrimary::LaunchCheckLeader() { } else if (context_->last_op_time + options_.check_leader_us < slash::NowMicros()) { context_->BecomeCandidate(); LOGV(INFO_LEVEL, info_log_, "FloydPrimary::LaunchCheckLeader: %s:%d Become Candidate because of timeout, new term is %d" - "voted for %s:%d", options_.local_ip.c_str(), options_.local_port, context_->current_term, + " voted for %s:%d", options_.local_ip.c_str(), options_.local_port, context_->current_term, context_->voted_for_ip.c_str(), context_->voted_for_port); raft_meta_->SetCurrentTerm(context_->current_term); raft_meta_->SetVotedForIp(context_->voted_for_ip); @@ -140,13 +140,13 @@ void FloydPrimary::NoticePeerTask(TaskType type) { for (auto& peer : peers_) { switch (type) { case kHeartBeat: - LOGV(INFO_LEVEL, info_log_, "FloydPrimary::NoticePeerTask server %s:%d send request vote message to %s", - options_.local_ip.c_str(), options_.local_port, peer.second->peer_addr().c_str()); + LOGV(INFO_LEVEL, info_log_, "FloydPrimary::NoticePeerTask server %s:%d send request vote message to %s at term %d", + options_.local_ip.c_str(), options_.local_port, peer.second->peer_addr().c_str(), context_->current_term); peer.second->AddRequestVoteTask(); break; case kNewCommand: - LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::NoticePeerTask server %s:%d send appendEntries message to %s", - options_.local_ip.c_str(), options_.local_port, peer.second->peer_addr().c_str()); + LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::NoticePeerTask server %s:%d send appendEntries message to %s at term %d", + options_.local_ip.c_str(), options_.local_port, peer.second->peer_addr().c_str(), context_->current_term); peer.second->AddAppendEntriesTask(); break; diff --git a/floyd/src/raft_log.cc b/floyd/src/raft_log.cc index a119b5b..1a64522 100644 --- a/floyd/src/raft_log.cc +++ b/floyd/src/raft_log.cc @@ -53,7 +53,6 @@ RaftLog::RaftLog(rocksdb::DB *db, Logger *info_log) : } RaftLog::~RaftLog() { - delete db_; } uint64_t RaftLog::Append(const std::vector &entries) { @@ -66,7 +65,8 @@ uint64_t RaftLog::Append(const std::vector &entries) { last_log_index_++; s = db_->Put(rocksdb::WriteOptions(), UintToBitStr(last_log_index_), buf); if (!s.ok()) { - LOGV(ERROR_LEVEL, info_log_, "RaftLog::Append %lu false\n", last_log_index_); + LOGV(ERROR_LEVEL, info_log_, "RaftLog::Append %lu string %s false\n", last_log_index_, UintToBitStr(last_log_index_).c_str()); + return --last_log_index_; } } return last_log_index_; @@ -82,7 +82,7 @@ int RaftLog::GetEntry(const uint64_t index, Entry *entry) { std::string res; rocksdb::Status s = db_->Get(rocksdb::ReadOptions(), buf, &res); if (s.IsNotFound()) { - LOGV(ERROR_LEVEL, info_log_, "RaftLog::GetEntry: GetEntry not found %lld\n", index); + LOGV(ERROR_LEVEL, info_log_, "RaftLog::GetEntry: GetEntry not found %lld string %s\n", index, UintToBitStr(index).c_str()); entry = NULL; return 1; } @@ -114,10 +114,12 @@ bool RaftLog::GetLastLogTermAndIndex(uint64_t* last_log_term, uint64_t* last_log int RaftLog::TruncateSuffix(uint64_t index) { // we need to delete the unnecessary entry, since we don't store // last_log_index in rocksdb - for (uint64_t i = index; i <= last_log_index_; i++) { - db_->Delete(rocksdb::WriteOptions(), UintToBitStr(i)); + for (; last_log_index_ >= index; last_log_index_--) { + rocksdb::Status s = db_->Delete(rocksdb::WriteOptions(), UintToBitStr(last_log_index_)); + if (!s.ok()) { + return -1; + } } - last_log_index_ = index; return 0; } diff --git a/floyd/src/raft_meta.cc b/floyd/src/raft_meta.cc index 15baec7..97ea375 100644 --- a/floyd/src/raft_meta.cc +++ b/floyd/src/raft_meta.cc @@ -76,7 +76,6 @@ std::string RaftMeta::GetVotedForIp() { } void RaftMeta::SetVotedForIp(const std::string ip) { - char buf[8]; db_->Put(rocksdb::WriteOptions(), kVoteForIp, ip); return; } diff --git a/floyd/tools/Makefile b/floyd/tools/Makefile index 1a6fca8..c31750e 100644 --- a/floyd/tools/Makefile +++ b/floyd/tools/Makefile @@ -5,7 +5,7 @@ else CXXFLAGS = -pg -O2 -ggdb3 -pipe -fPIC -W -Wwrite-strings -Wpointer-arith -Wreorder -Wswitch -Wsign-promo -Wredundant-decls -Wformat -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -std=c++11 -gdwarf-2 -Wno-redundant-decls -Wno-unused-variable -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DOS_LINUX endif -OBJECT = read_rock read_floyd cpt +OBJECT = read_rock read_floyd cpt cl cl1 SRC_DIR = ./ THIRD_PATH = ../third OUTPUT = ./output @@ -55,6 +55,10 @@ cpt: cpt.cc $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) read_floyd: read_floyd.cc floyd.pb.cc $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) +cl: cl.cc ../src/*.cc ../include/* + $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) +cl1: cl1.cc ../src/*.cc ../include/* + $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) $(OBJS): %.o : %.cc $(CXX) $(CXXFLAGS) -c $< -o $@ $(INCLUDE_PATH) diff --git a/floyd/tools/cl.cc b/floyd/tools/cl.cc new file mode 100644 index 0000000..38ed3c1 --- /dev/null +++ b/floyd/tools/cl.cc @@ -0,0 +1,100 @@ +#include +#include + +#include + +#include "rocksdb/db.h" +#include "slash/include/env.h" + +#include "floyd/src/raft_log.h" +#include "floyd/src/raft_meta.h" +#include "floyd/src/floyd.pb.h" +#include "floyd/src/logger.h" + +using namespace floyd; +void InitEntry(int term, const std::string &key, const std::string &val, Entry *entry) { + entry->set_term(term); + entry->set_key(key); + entry->set_value(val); + entry->set_optype(Entry_OpType_kWrite); +} +int cl(const std::string path) { + // construct data1 node + Logger *logger; + slash::CreatePath(path); + if (NewLogger(path + "/LOG", &logger) != 0) { + return -1; + } + rocksdb::DB* db; + rocksdb::Options options; + options.create_if_missing = true; + rocksdb::Status s = rocksdb::DB::Open(options, path + "/log/", &db); + RaftMeta *raft_meta = new RaftMeta(db, logger); + raft_meta->Init(); + RaftLog *raft_log = new RaftLog(db, logger); + std::vector entries; + char buff[10]; + for (int i = 0; i < 10; i++) { + Entry *entry = new Entry(); + snprintf(buff, sizeof(buff), "%d", i); + InitEntry(10, std::string(buff, sizeof(int)), std::string(buff, sizeof(int))); + entries.push_back(entry); + } + raft_log->Append(entries); + raft_meta->SetCurrentTerm(10); + raft_meta->SetCommitIndex(10); + raft_meta->SetLastApplied(0); + + delete raft_meta; + delete raft_log; + delete db; + delete logger; +} + +int cl_spec(const std::string path) { + // construct data1 node + Logger *logger; + slash::CreatePath(path); + if (NewLogger(path + "/LOG", &logger) != 0) { + return -1; + } + rocksdb::DB* db; + rocksdb::Options options; + options.create_if_missing = true; + rocksdb::Status s = rocksdb::DB::Open(options, path + "/log/", &db); + RaftMeta *raft_meta = new RaftMeta(db, logger); + raft_meta->Init(); + RaftLog *raft_log = new RaftLog(db, logger); + std::vector entries; + char buff[10]; + for (int i = 0; i < 21; i++) { + Entry *entry = new Entry(); + snprintf(buff, sizeof(buff), "%d", i); + entry->set_term(10); + entry->set_key(std::string(buff, sizeof(int))); + entry->set_value(std::string(buff, sizeof(int))); + entry->set_optype(Entry_OpType_kWrite); + entries.push_back(entry); + } + raft_log->Append(entries); + raft_meta->SetCurrentTerm(10); + raft_meta->SetCommitIndex(10); + raft_meta->SetLastApplied(0); + + delete raft_meta; + delete raft_log; + delete db; + delete logger; +} + +int main() +{ + + cl("./data1/"); + cl("./data2/"); + cl("./data3/"); + cl_spec("./data4/"); + cl("./data5/"); + + return 0; +} diff --git a/floyd/tools/construct_log.cc b/floyd/tools/construct_log.cc deleted file mode 100644 index eebce63..0000000 --- a/floyd/tools/construct_log.cc +++ /dev/null @@ -1,19 +0,0 @@ -#include - -#include "rocksdb/db.h" -#include "floyd/src/floyd.pb.h" -#include - -static const std::string kCurrentTerm = "CURRENTTERM"; -static const std::string kVoteForIp = "VOTEFORIP"; -static const std::string kVoteForPort = "VOTEFORPORT"; -static const std::string kApplyIndex = "APPLYINDEX"; - -int main() -{ - rocksdb::DB* db; - rocksdb::Options options; - rocksdb::Status s = rocksdb::DB::Open(options, "./data1/log", &db); - log_db_->Put(rocksdb::WriteOptions(), kVoteForPort, std::string(buf, 4)); - return 0; -} diff --git a/floyd/tools/read_floyd.cc b/floyd/tools/read_floyd.cc index 9978915..09d9b34 100644 --- a/floyd/tools/read_floyd.cc +++ b/floyd/tools/read_floyd.cc @@ -7,7 +7,6 @@ #include "floyd/src/floyd.pb.h" #include - using namespace rocksdb; extern std::string UintToBitStr(const uint64_t num) { char buf[8]; From 45abd944da870183ecd1b72b28f8a84cc063ed2e Mon Sep 17 00:00:00 2001 From: baotiao Date: Mon, 7 Aug 2017 18:29:24 +0800 Subject: [PATCH 15/17] fix various binlog length and data, after restart and write will merge all the data --- floyd/example/simple/t4.cc | 17 ++++++----- floyd/src/floyd_apply.cc | 4 +-- floyd/src/floyd_impl.cc | 52 ++++++++++++++++++---------------- floyd/src/floyd_options.cc | 8 +++--- floyd/src/floyd_peer_thread.cc | 2 +- floyd/src/raft_log.cc | 15 +++++++++- 6 files changed, 56 insertions(+), 42 deletions(-) diff --git a/floyd/example/simple/t4.cc b/floyd/example/simple/t4.cc index ddd660d..05fccae 100644 --- a/floyd/example/simple/t4.cc +++ b/floyd/example/simple/t4.cc @@ -50,15 +50,14 @@ int main() std::string msg; while (1) { f1->GetServerStatus(msg); - /* - * if (is_writed == false) { - * // writing one record to update the meta data - * slash::Status s = f1->Write("lastdata", "lastdata"); - * if (s.ok()) { - * is_writed = true; - * } - * } - */ + if (is_writed == false) { + // writing one record to update the meta data + slash::Status s = f1->Write("lastdata", "lastdata"); + if (s.ok()) { + is_writed = true; + printf("write lastdata success\n"); + } + } printf("%s\n", msg.c_str()); sleep(2); } diff --git a/floyd/src/floyd_apply.cc b/floyd/src/floyd_apply.cc index 2b0b920..48ac0c3 100644 --- a/floyd/src/floyd_apply.cc +++ b/floyd/src/floyd_apply.cc @@ -34,7 +34,7 @@ FloydApply::~FloydApply() { int FloydApply::Start() { bg_thread_.set_thread_name("FloydApply"); - // bg_thread_.DelaySchedule(3000, ApplyStateMachineWrapper, this); + bg_thread_.DelaySchedule(3000, ApplyStateMachineWrapper, this); return bg_thread_.StartThread(); } @@ -76,7 +76,7 @@ void FloydApply::ApplyStateMachine() { raft_meta_->SetLastApplied(last_applied); context_->apply_mu.Unlock(); context_->apply_cond.SignalAll(); - // bg_thread_.DelaySchedule(3000, ApplyStateMachineWrapper, this); + bg_thread_.DelaySchedule(3000, ApplyStateMachineWrapper, this); } Status FloydApply::Apply(const Entry& entry) { diff --git a/floyd/src/floyd_impl.cc b/floyd/src/floyd_impl.cc index 4b4e0e3..7cf3eb5 100644 --- a/floyd/src/floyd_impl.cc +++ b/floyd/src/floyd_impl.cc @@ -645,10 +645,9 @@ void FloydImpl::ReplyAppendEntries(CmdRequest& request, CmdResponse* response) { // update last_op_time to avoid another leader election context_->last_op_time = slash::NowMicros(); // Ignore stale term - // if the append entries term is smaller then my current term, then the caller must an older leader - uint64_t last_log_index = raft_log_->GetLastLogIndex(); + // if the append entries leader's term is smaller then my current term, then the caller must an older leader if (append_entries.term() < context_->current_term) { - BuildAppendEntriesResponse(success, context_->current_term, last_log_index, response); + BuildAppendEntriesResponse(success, context_->current_term, raft_log_->GetLastLogIndex(), response); return; } else if (append_entries.term() >= context_->current_term) { context_->BecomeFollower(append_entries.term(), @@ -658,51 +657,54 @@ void FloydImpl::ReplyAppendEntries(CmdRequest& request, CmdResponse* response) { raft_meta_->SetVotedForPort(context_->voted_for_port); } - if (append_entries.prev_log_index() > last_log_index) { - LOGV(INFO_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries:" - "pre_log(%lu, %lu) > last_log_index(%lu)", append_entries.prev_log_term(), append_entries.prev_log_index(), - last_log_index); - BuildAppendEntriesResponse(success, context_->current_term, last_log_index, response); + if (append_entries.prev_log_index() > raft_log_->GetLastLogIndex()) { + LOGV(INFO_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries: leader %s:%p " + " pre_log(%lu, %lu) > last_log_index(%lu)", options_.local_ip.c_str(), options_.local_port, append_entries.prev_log_term(), + append_entries.prev_log_index(), raft_log_->GetLastLogIndex()); + BuildAppendEntriesResponse(success, context_->current_term, raft_log_->GetLastLogIndex(), response); return ; } - std::vector entries; - for (auto& it : *(request.mutable_append_entries()->mutable_entries())) { - entries.push_back(&it); + // Append entry + if (append_entries.prev_log_index() < raft_log_->GetLastLogIndex()) { + LOGV(WARN_LEVEL, info_log_, "FloydImpl::ReplyAppentries: leader %s:%d pre_log(%lu, %lu)'s index smaller than" + " my log(%lu) index, truncate suffix from here %lu", append_entries.ip().c_str(), append_entries.port(), + append_entries.prev_log_term(), append_entries.prev_log_index(), raft_log_->GetLastLogIndex(), + append_entries.prev_log_index() + 1); + raft_log_->TruncateSuffix(append_entries.prev_log_index() + 1); } - uint64_t my_log_term = 0; + + // we compare peer's prev index and term with my last log index and term + uint64_t my_last_log_term = 0; Entry entry; LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries " "prev_log_index: %llu\n", append_entries.prev_log_index()); if (append_entries.prev_log_index() == 0) { - my_log_term = 0; + my_last_log_term = 0; } else if (raft_log_->GetEntry(append_entries.prev_log_index(), &entry) == 0) { - my_log_term = entry.term(); + my_last_log_term = entry.term(); } else { LOGV(WARN_LEVEL, info_log_, "FloydImple::ReplyAppentries: can't " "get Entry from raft_log prev_log_index %llu", append_entries.prev_log_index()); - BuildAppendEntriesResponse(success, context_->current_term, last_log_index, response); + BuildAppendEntriesResponse(success, context_->current_term, raft_log_->GetLastLogIndex(), response); return; } - if (append_entries.prev_log_term() != my_log_term) { + if (append_entries.prev_log_term() != my_last_log_term) { LOGV(WARN_LEVEL, info_log_, "FloydImpl::ReplyAppentries: leader %s:%d pre_log(%lu, %lu)'s term don't match with" " my log(%lu, %lu) term, truncate my log from here %lu",append_entries.ip().c_str(), append_entries.port(), - append_entries.prev_log_term(), append_entries.prev_log_index(), my_log_term, last_log_index, + append_entries.prev_log_term(), append_entries.prev_log_index(), my_last_log_term, raft_log_->GetLastLogIndex(), append_entries.prev_log_index()); // TruncateSuffix [prev_log_index, last_log_index) raft_log_->TruncateSuffix(append_entries.prev_log_index()); + BuildAppendEntriesResponse(success, context_->current_term, raft_log_->GetLastLogIndex(), response); + return ; } - // Append entry - if (append_entries.prev_log_index() < last_log_index) { - LOGV(WARN_LEVEL, info_log_, "FloydImpl::ReplyAppentries: leader %s:%d pre_log(%lu, %lu)'s index smaller than" - " my log(%lu, %lu) index, truncate suffix from here %lu", append_entries.ip().c_str(), append_entries.port(), - append_entries.prev_log_term(), append_entries.prev_log_index(), my_log_term, last_log_index, - append_entries.prev_log_index() + 1); - raft_log_->TruncateSuffix(append_entries.prev_log_index() + 1); + std::vector entries; + for (auto& it : *(request.mutable_append_entries()->mutable_entries())) { + entries.push_back(&it); } - if (append_entries.entries().size() > 0) { LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries: leader %s:%d will append %u entries from " " prev_log_index %lu", append_entries.ip().c_str(), append_entries.port(), diff --git a/floyd/src/floyd_options.cc b/floyd/src/floyd_options.cc index e3d3082..658b601 100644 --- a/floyd/src/floyd_options.cc +++ b/floyd/src/floyd_options.cc @@ -87,8 +87,8 @@ Options::Options() path("/data/floyd"), check_leader_us(6000000), heartbeat_us(3000000), - append_entries_size_once(1024), - append_entries_count_once(24), + append_entries_size_once(1024000), + append_entries_count_once(128), single_mode(false) { } @@ -100,8 +100,8 @@ Options::Options(const std::string& cluster_string, path(_path), check_leader_us(6000000), heartbeat_us(3000000), - append_entries_size_once(1024), - append_entries_count_once(24), + append_entries_size_once(1024000), + append_entries_count_once(128), single_mode(false) { std::srand(slash::NowMicros()); // the default check_leader is [3s, 5s) diff --git a/floyd/src/floyd_peer_thread.cc b/floyd/src/floyd_peer_thread.cc index a21becd..8a31b3d 100644 --- a/floyd/src/floyd_peer_thread.cc +++ b/floyd/src/floyd_peer_thread.cc @@ -197,7 +197,7 @@ void Peer::AppendEntriesRPC() { Entry *entry = append_entries->add_entries(); *entry = *tmp_entry; } else { - LOGV(WARN_LEVEL, info_log_, "Peer::AppendEntriesRPC: peer_addr %s can't get Entry ", + LOGV(WARN_LEVEL, info_log_, "Peer::AppendEntriesRPC: peer_addr %s can't get Entry " "from raft_log, index %lld", peer_addr_.c_str(), index); break; } diff --git a/floyd/src/raft_log.cc b/floyd/src/raft_log.cc index 1a64522..df4d1dd 100644 --- a/floyd/src/raft_log.cc +++ b/floyd/src/raft_log.cc @@ -59,16 +59,26 @@ uint64_t RaftLog::Append(const std::vector &entries) { slash::MutexLock l(&lli_mutex_); std::string buf; rocksdb::Status s; + rocksdb::WriteBatch wb; LOGV(DEBUG_LEVEL, info_log_, "RaftLog::Append: entries.size %lld", entries.size()); for (size_t i = 0; i < entries.size(); i++) { entries[i]->SerializeToString(&buf); last_log_index_++; + // wb.Put(UintToBitStr(last_log_index_), buf); s = db_->Put(rocksdb::WriteOptions(), UintToBitStr(last_log_index_), buf); if (!s.ok()) { LOGV(ERROR_LEVEL, info_log_, "RaftLog::Append %lu string %s false\n", last_log_index_, UintToBitStr(last_log_index_).c_str()); return --last_log_index_; } } + /* + * s = db_->Write(rocksdb::WriteOptions(), &wb); + * if (!s.ok()) { + * LOGV(ERROR_LEVEL, info_log_, "RaftLog::Append %lu false\n", last_log_index_); + * return last_log_index_; + * } + * last_log_index_ += entries.size(); + */ return last_log_index_; } @@ -82,7 +92,7 @@ int RaftLog::GetEntry(const uint64_t index, Entry *entry) { std::string res; rocksdb::Status s = db_->Get(rocksdb::ReadOptions(), buf, &res); if (s.IsNotFound()) { - LOGV(ERROR_LEVEL, info_log_, "RaftLog::GetEntry: GetEntry not found %lld string %s\n", index, UintToBitStr(index).c_str()); + LOGV(ERROR_LEVEL, info_log_, "RaftLog::GetEntry: GetEntry not found %lld \n", index); entry = NULL; return 1; } @@ -111,6 +121,9 @@ bool RaftLog::GetLastLogTermAndIndex(uint64_t* last_log_term, uint64_t* last_log return true; } +/* + * truncate suffix from index + */ int RaftLog::TruncateSuffix(uint64_t index) { // we need to delete the unnecessary entry, since we don't store // last_log_index in rocksdb From 803bcfb7affcded3937b1631e8d22c5de6b1a351 Mon Sep 17 00:00:00 2001 From: baotiao Date: Wed, 9 Aug 2017 11:13:48 +0800 Subject: [PATCH 16/17] 1. optimization when there is lots of empty appendEntries 2. add construct log tool cl.cc cl1.cc 3. add two simple example. the detail usage is in README file --- floyd/example/simple/Makefile | 6 +- floyd/example/simple/README | 3 +- floyd/example/simple/t1.cc | 43 ++-- floyd/example/simple/t2.cc | 5 + floyd/example/simple/t3.cc | 2 +- floyd/example/simple/t6.cc | 113 +++++++++ floyd/example/simple/t7.cc | 123 ++++++++++ floyd/include/floyd.h | 7 +- floyd/include/floyd_options.h | 11 +- floyd/src/floyd_apply.cc | 17 +- floyd/src/floyd_context.h | 3 + floyd/src/floyd_impl.cc | 30 ++- floyd/src/floyd_impl.h | 5 +- floyd/src/floyd_peer_thread.cc | 80 +++++-- floyd/src/floyd_peer_thread.h | 5 + floyd/src/floyd_primary_thread.cc | 11 +- floyd/src/raft_log.cc | 32 +-- floyd/tools/README | 2 + floyd/tools/cl.cc | 2 +- floyd/tools/cl1.cc | 369 ++++++++++++++++++++++++++++++ 20 files changed, 784 insertions(+), 85 deletions(-) create mode 100644 floyd/example/simple/t6.cc create mode 100644 floyd/example/simple/t7.cc create mode 100644 floyd/tools/cl1.cc diff --git a/floyd/example/simple/Makefile b/floyd/example/simple/Makefile index c38bb47..9f4f4d9 100644 --- a/floyd/example/simple/Makefile +++ b/floyd/example/simple/Makefile @@ -5,7 +5,7 @@ else CXXFLAGS = -pg -g -O2 -ggdb3 -pipe -fPIC -W -Wwrite-strings -Wpointer-arith -Wreorder -Wswitch -Wsign-promo -Wredundant-decls -Wformat -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -std=c++11 -gdwarf-2 -Wno-redundant-decls -Wno-unused-variable -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DOS_LINUX endif -OBJECT = t t1 t2 t3 t4 t5 +OBJECT = t t1 t2 t3 t4 t5 t6 t7 SRC_DIR = ./ THIRD_PATH = ../../third OUTPUT = ./output @@ -65,6 +65,10 @@ t4: t4.cc $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) t5: t5.cc $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) +t6: t6.cc + $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) +t7: t7.cc + $(CXX) $(CXXFLAGS) -o $@ $^ $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) $(OBJS): %.o : %.cc $(CXX) $(CXXFLAGS) -c $< -o $@ $(INCLUDE_PATH) diff --git a/floyd/example/simple/README b/floyd/example/simple/README index af4ef13..77e88b1 100644 --- a/floyd/example/simple/README +++ b/floyd/example/simple/README @@ -2,8 +2,9 @@ the simple example will run the Floyd:write the get performance of floyd in a si t is a single thread wirte tool to get performance -t1 is multi thread program to get performance +t1 is multi thread program to get performance, in this case, all the writes is from the leader node t2 is an example test node join and leave t4 is an example used to see the message passing by each node in a stable situation t5 used to test single mode floyd, including starting a node and writing data +t6 is the same as t1 except that all the writes is from the follower node diff --git a/floyd/example/simple/t1.cc b/floyd/example/simple/t1.cc index 125f132..b8ac02d 100644 --- a/floyd/example/simple/t1.cc +++ b/floyd/example/simple/t1.cc @@ -22,22 +22,40 @@ uint64_t NowMicros() { return static_cast(tv.tv_sec) * 1000000 + tv.tv_usec; } -Floyd *f1; +Floyd *f1, *f2, *f3, *f4, *f5; std::string keystr[1001000]; std::string valstr[1001000]; +int val_size = 128; +int thread_num = 4; +int item_num = 10000; + void *fun(void *arg) { int i = 1; + Floyd *p; + if (f1->IsLeader()) { + p = f1; + } + if (f2->IsLeader()) { + p = f2; + } + if (f3->IsLeader()) { + p = f3; + } + if (f4->IsLeader()) { + p = f4; + } + if (f5->IsLeader()) { + p = f5; + } while (i--) { - for (int j = 0; j < 100000; j++) { - f1->Write(keystr[j], valstr[j]); + for (int j = 0; j < item_num; j++) { + p->Write(keystr[j], valstr[j]); } } } int main(int argc, char * argv[]) { - int val_size = 128; - int thread_num = 4; if (argc > 1) { val_size = atoi(argv[1]); } @@ -47,11 +65,8 @@ int main(int argc, char * argv[]) printf("multi threads test to get performance thread num %d key size %d\n", thread_num, val_size); - Options op("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8901, "./data1/"); - Floyd *f2, *f3, *f4, *f5; - op.Dump(); - - slash::Status s = Floyd::Open(op, &f1); + Options op1("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8901, "./data1/"); + slash::Status s = Floyd::Open(op1, &f1); printf("%s\n", s.ToString().c_str()); Options op2("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8902, "./data2/"); @@ -73,14 +88,16 @@ int main(int argc, char * argv[]) std::string msg; int i = 10; uint64_t st = NowMicros(), ed; - for (int i = 0; i < 1000000; i++) { + for (int i = 0; i < item_num; i++) { keystr[i] = slash::RandomString(32); } - for (int i = 0; i < 1000000; i++) { + for (int i = 0; i < item_num; i++) { valstr[i] = slash::RandomString(val_size); } while (1) { if (f1->HasLeader()) { + f1->GetServerStatus(msg); + printf("%s\n", msg.c_str()); break; } printf("electing leader... sleep 2s\n"); @@ -96,7 +113,7 @@ int main(int argc, char * argv[]) pthread_join(pid[i], NULL); } ed = NowMicros(); - printf("write 100000 cost time microsecond(us) %ld, qps %llu\n", ed - st, 100000 * thread_num * 1000000LL / (ed - st)); + printf("write 100000 cost time microsecond(us) %ld, qps %llu\n", ed - st, item_num * thread_num * 1000000LL / (ed - st)); getchar(); delete f2; diff --git a/floyd/example/simple/t2.cc b/floyd/example/simple/t2.cc index 7da285f..f2a0cfe 100644 --- a/floyd/example/simple/t2.cc +++ b/floyd/example/simple/t2.cc @@ -54,6 +54,7 @@ int main() sleep(2); } + printf("delete node2, node3 to show 3 nodes status\n"); delete f2; delete f3; i = 8; @@ -63,6 +64,7 @@ int main() sleep(2); } + printf("restart node2, node3 and stop node4, show 4 nodes status\n"); s = Floyd::Open(op2, &f2); s = Floyd::Open(op3, &f3); delete f4; @@ -73,6 +75,7 @@ int main() sleep(2); } + printf("reopen node4, delete node1, node5 show 3 nodes status\n"); s = Floyd::Open(op4, &f4); delete f1; delete f5; @@ -83,6 +86,7 @@ int main() sleep(2); } + printf("delete node2, now only two nodes are alive\n"); delete f2; i = 8; while (i--) { @@ -91,6 +95,7 @@ int main() sleep(2); } + printf("reopen node1, node2, node5, the cluster recover now\n"); s = Floyd::Open(op, &f1); s = Floyd::Open(op2, &f2); s = Floyd::Open(op5, &f5); diff --git a/floyd/example/simple/t3.cc b/floyd/example/simple/t3.cc index 9b54b89..d6621e2 100644 --- a/floyd/example/simple/t3.cc +++ b/floyd/example/simple/t3.cc @@ -63,7 +63,7 @@ int main() std::string val; while (i--) { f3->GetServerStatus(msg); - for (int j = 0; j < 100000; j++) { + for (int j = 0; j < 100; j++) { f3->Write(mystr[j], mystr[j]); s = f3->Read(mystr[j], val); printf("status %s val %s\n", s.ToString().c_str(), val.c_str()); diff --git a/floyd/example/simple/t6.cc b/floyd/example/simple/t6.cc new file mode 100644 index 0000000..75a8d8b --- /dev/null +++ b/floyd/example/simple/t6.cc @@ -0,0 +1,113 @@ +// Copyright (c) 2015-present, Qihoo, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. + +#include +#include +#include +#include +#include + +#include +#include + +#include "floyd/include/floyd.h" +#include "slash/include/testutil.h" + +using namespace floyd; +uint64_t NowMicros() { + struct timeval tv; + gettimeofday(&tv, NULL); + return static_cast(tv.tv_sec) * 1000000 + tv.tv_usec; +} + +Floyd *f1, *f2, *f3, *f4, *f5; +std::string keystr[1001000]; +std::string valstr[1001000]; +int val_size = 128; +int thread_num = 4; +int item_num = 10000; + +void *fun(void *arg) { + int i = 1; + Floyd *p = f1; + if (f1->IsLeader()) { + p = f2; + } + while (i--) { + for (int j = 0; j < item_num; j++) { + p->Write(keystr[j], valstr[j]); + } + } +} + +int main(int argc, char * argv[]) +{ + if (argc > 1) { + val_size = atoi(argv[1]); + } + if (argc > 2) { + thread_num = atoi(argv[2]); + } + + printf("multi threads test to get performance thread num %d key size %d\n", thread_num, val_size); + + Options op1("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8901, "./data1/"); + slash::Status s = Floyd::Open(op1, &f1); + printf("%s\n", s.ToString().c_str()); + + Options op2("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8902, "./data2/"); + s = Floyd::Open(op2, &f2); + printf("%s\n", s.ToString().c_str()); + + Options op3("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8903, "./data3/"); + s = Floyd::Open(op3, &f3); + printf("%s\n", s.ToString().c_str()); + + Options op4("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8904, "./data4/"); + s = Floyd::Open(op4, &f4); + printf("%s\n", s.ToString().c_str()); + + Options op5("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8905, "./data5/"); + s = Floyd::Open(op5, &f5); + printf("%s\n", s.ToString().c_str()); + + std::string msg; + int i = 10; + uint64_t st = NowMicros(), ed; + for (int i = 0; i < item_num; i++) { + keystr[i] = slash::RandomString(32); + } + for (int i = 0; i < item_num; i++) { + valstr[i] = slash::RandomString(val_size); + } + while (1) { + if (f1->HasLeader()) { + f1->GetServerStatus(msg); + printf("%s\n", msg.c_str()); + break; + } + printf("electing leader... sleep 2s\n"); + sleep(2); + } + + pthread_t pid[24]; + st = NowMicros(); + for (int i = 0; i < thread_num; i++) { + pthread_create(&pid[i], NULL, fun, NULL); + } + for (int i = 0; i < thread_num; i++) { + pthread_join(pid[i], NULL); + } + ed = NowMicros(); + printf("write 100000 cost time microsecond(us) %ld, qps %llu\n", ed - st, item_num * thread_num * 1000000LL / (ed - st)); + + getchar(); + delete f2; + delete f3; + delete f4; + delete f5; + delete f1; + return 0; +} diff --git a/floyd/example/simple/t7.cc b/floyd/example/simple/t7.cc new file mode 100644 index 0000000..5c7f6f9 --- /dev/null +++ b/floyd/example/simple/t7.cc @@ -0,0 +1,123 @@ +#include +#include +#include + +#include +#include + +#include "floyd/include/floyd.h" +#include "slash/include/testutil.h" + +using namespace floyd; +uint64_t NowMicros() { + struct timeval tv; + gettimeofday(&tv, NULL); + return static_cast(tv.tv_sec) * 1000000 + tv.tv_usec; +} + +std::string keystr[1001000]; +std::string valstr[1001000]; +int val_size = 128; +int item_num = 1000; + +int main(int argc, char *argv[]) +{ + if (argc > 1) { + val_size = atoi(argv[1]); + } + + printf("test write 3 node and then join the other 2 node case, key size %d\n", val_size); + + Options op("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8901, "./data1/"); + op.Dump(); + + Floyd *f1, *f2, *f3, *f4, *f5; + + slash::Status s; + s = Floyd::Open(op, &f1); + printf("%s\n", s.ToString().c_str()); + + Options op2("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8902, "./data2/"); + s = Floyd::Open(op2, &f2); + printf("%s\n", s.ToString().c_str()); + + Options op3("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8903, "./data3/"); + s = Floyd::Open(op3, &f3); + printf("%s\n", s.ToString().c_str()); + + Options op4("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8904, "./data4/"); + s = Floyd::Open(op4, &f4); + printf("%s\n", s.ToString().c_str()); + + Options op5("127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:8905", "127.0.0.1", 8905, "./data5/"); + s = Floyd::Open(op5, &f5); + printf("%s\n", s.ToString().c_str()); + + std::string msg; + int cnt = 1; + uint64_t st = NowMicros(), ed; + + while (1) { + if (f1->HasLeader()) { + break; + } + printf("electing leader... sleep 2s\n"); + sleep(2); + } + + while (cnt--) { + for (int i = 0; i < item_num; i++) { + valstr[i] = slash::RandomString(10); + } + f1->GetServerStatus(msg); + printf("%s\n", msg.c_str()); + st = NowMicros(); + for (int j = 0; j < item_num; j++) { + f1->Write(keystr[j], valstr[j]); + } + ed = NowMicros(); + printf("write 100000 cost time microsecond(us) %ld, qps %llu\n", ed - st, item_num * 1000000LL / (ed - st)); + } + + delete f1; + delete f5; + + while (1) { + if (f2->HasLeader()) { + break; + } + printf("electing leader... sleep 2s\n"); + sleep(2); + } + cnt = 1; + while (cnt--) { + for (int i = 0; i < item_num; i++) { + valstr[i] = slash::RandomString(10); + } + f2->GetServerStatus(msg); + printf("%s\n", msg.c_str()); + st = NowMicros(); + for (int j = 0; j < item_num; j++) { + f2->Write(keystr[j], valstr[j]); + } + ed = NowMicros(); + printf("write 100000 cost time microsecond(us) %ld, qps %llu\n", ed - st, item_num * 1000000LL / (ed - st)); + } + + s = Floyd::Open(op, &f1); + s = Floyd::Open(op5, &f5); + + cnt = 10; + while (cnt--) { + f2->GetServerStatus(msg); + printf("%s\n", msg.c_str()); + } + + getchar(); + delete f2; + delete f3; + delete f4; + delete f5; + delete f1; + return 0; +} diff --git a/floyd/include/floyd.h b/floyd/include/floyd.h index db47630..4695d43 100644 --- a/floyd/include/floyd.h +++ b/floyd/include/floyd.h @@ -16,7 +16,7 @@ namespace floyd { using slash::Status; -class Floyd { +class Floyd { public: static Status Open(const Options& options, Floyd** floyd); @@ -31,13 +31,14 @@ class Floyd { // return true if leader has been elected virtual bool GetLeader(std::string* ip_port) = 0; - virtual bool HasLeader() = 0; virtual bool GetLeader(std::string* ip, int* port) = 0; + virtual bool HasLeader() = 0; virtual bool GetAllNodes(std::vector& nodes) = 0; + virtual bool IsLeader() = 0; // used for debug virtual bool GetServerStatus(std::string& msg) = 0; - + // log level can be modified virtual void set_log_level(const int log_level) = 0; diff --git a/floyd/include/floyd_options.h b/floyd/include/floyd_options.h index afa6d78..41f2aa1 100644 --- a/floyd/include/floyd_options.h +++ b/floyd/include/floyd_options.h @@ -3,8 +3,8 @@ // LICENSE file in the root directory of this source tree. An additional grant // of patent rights can be found in the PATENTS file in the same directory. -#ifndef FLOYD_SRC_FLOYD_OPTIONS_H_ -#define FLOYD_SRC_FLOYD_OPTIONS_H_ +#ifndef FLOYD_INCLUDE_FLOYD_OPTIONS_H_ +#define FLOYD_INCLUDE_FLOYD_OPTIONS_H_ #include #include @@ -24,8 +24,7 @@ enum { struct Options { // cluster members // parsed from comma separated ip1:port1,ip2:port2... - std::vector members; - + std::vector members; std::string local_ip; int local_port; std::string path; @@ -46,5 +45,5 @@ struct Options { const std::string& _path); }; -} // namespace floyd -#endif // FLOYD_INCLUDE_FLOYD_OPTIONS_ +} // namespace floyd +#endif // FLOYD_INCLUDE_FLOYD_OPTIONS_H_ diff --git a/floyd/src/floyd_apply.cc b/floyd/src/floyd_apply.cc index 48ac0c3..8b4d9b5 100644 --- a/floyd/src/floyd_apply.cc +++ b/floyd/src/floyd_apply.cc @@ -5,24 +5,24 @@ #include "floyd/src/floyd_apply.h" +#include + #include #include #include "slash/include/xdebug.h" -#include - #include "floyd/src/logger.h" #include "floyd/src/floyd.pb.h" #include "floyd/src/raft_meta.h" #include "floyd/src/raft_log.h" - namespace floyd { FloydApply::FloydApply(FloydContext* context, rocksdb::DB* db, RaftMeta* raft_meta, RaftLog* raft_log, Logger* info_log) - : context_(context), + : bg_thread_(1024 * 1024 * 1024), + context_(context), db_(db), raft_meta_(raft_meta), raft_log_(raft_log), @@ -34,7 +34,7 @@ FloydApply::~FloydApply() { int FloydApply::Start() { bg_thread_.set_thread_name("FloydApply"); - bg_thread_.DelaySchedule(3000, ApplyStateMachineWrapper, this); + bg_thread_.Schedule(ApplyStateMachineWrapper, this); return bg_thread_.StartThread(); } @@ -43,6 +43,12 @@ int FloydApply::Stop() { } void FloydApply::ScheduleApply() { + /* + * int timer_queue_size, queue_size; + * bg_thread_.QueueSize(&timer_queue_size, &queue_size); + * LOGV(INFO_LEVEL, info_log_, "Peer::AddRequestVoteTask timer_queue size %d queue_size %d", + * timer_queue_size, queue_size); + */ bg_thread_.Schedule(&ApplyStateMachineWrapper, this); } @@ -76,7 +82,6 @@ void FloydApply::ApplyStateMachine() { raft_meta_->SetLastApplied(last_applied); context_->apply_mu.Unlock(); context_->apply_cond.SignalAll(); - bg_thread_.DelaySchedule(3000, ApplyStateMachineWrapper, this); } Status FloydApply::Apply(const Entry& entry) { diff --git a/floyd/src/floyd_context.h b/floyd/src/floyd_context.h index 7d01fbc..2839d1d 100644 --- a/floyd/src/floyd_context.h +++ b/floyd/src/floyd_context.h @@ -36,6 +36,9 @@ struct FloydContext { leader_ip(""), leader_port(0), vote_quorum(0), + commit_index(0), + last_applied(0), + last_op_time(0), apply_cond(&apply_mu) {}; void RecoverInit(RaftMeta *raft); diff --git a/floyd/src/floyd_impl.cc b/floyd/src/floyd_impl.cc index 7cf3eb5..6b67137 100644 --- a/floyd/src/floyd_impl.cc +++ b/floyd/src/floyd_impl.cc @@ -54,7 +54,7 @@ FloydImpl::~FloydImpl() { delete raft_log_; delete info_log_; delete db_; - // delete log_and_meta_; + delete log_and_meta_; } bool FloydImpl::IsSelf(const std::string& ip_port) { @@ -69,6 +69,16 @@ bool FloydImpl::GetLeader(std::string *ip_port) { return true; } +bool FloydImpl::IsLeader() { + if (context_->leader_ip == "" || context_->leader_port == 0) { + return false; + } + if (context_->leader_ip == options_.local_ip && context_->leader_port == options_.local_port) { + return true; + } + return false; +} + bool FloydImpl::GetLeader(std::string* ip, int* port) { *ip = context_->leader_ip; *port = context_->leader_port; @@ -357,7 +367,7 @@ bool FloydImpl::GetServerStatus(std::string& msg) { char str[512]; snprintf (str, 512, " Node | Role | Term | Leader | VoteFor | LastLogTerm | LastLogIdx | CommitIndex | LastApplied |\n" - "%15s:%-6d%10s%7lu%14s:%-6d%14s:%-d%10lu%13lu%14lu%13lu\n", + "%15s:%-6d%10s%7lu%14s:%-6d%14s:%-6d%10lu%13lu%14lu%13lu\n", options_.local_ip.c_str(), options_.local_port, server_status.role().c_str(), server_status.term(), server_status.leader_ip().c_str(), server_status.leader_port(), server_status.voted_for_ip().c_str(), server_status.voted_for_port(), @@ -382,7 +392,7 @@ bool FloydImpl::GetServerStatus(std::string& msg) { slash::ParseIpPortString(iter, ip, port); CmdResponse_ServerStatus server_status = response.server_status(); snprintf (str, 512, - "%15s:%-6d%10s%7lu%14s:%-6d%14s:%-d%10lu%13lu%14lu%13lu\n", + "%15s:%-6d%10s%7lu%14s:%-6d%14s:%-6d%10lu%13lu%14lu%13lu\n", ip.c_str(), port, server_status.role().c_str(), server_status.term(), server_status.leader_ip().c_str(), server_status.leader_port(), server_status.voted_for_ip().c_str(), server_status.voted_for_port(), @@ -658,7 +668,7 @@ void FloydImpl::ReplyAppendEntries(CmdRequest& request, CmdResponse* response) { } if (append_entries.prev_log_index() > raft_log_->GetLastLogIndex()) { - LOGV(INFO_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries: leader %s:%p " + LOGV(INFO_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries: leader %s:%d " " pre_log(%lu, %lu) > last_log_index(%lu)", options_.local_ip.c_str(), options_.local_port, append_entries.prev_log_term(), append_entries.prev_log_index(), raft_log_->GetLastLogIndex()); BuildAppendEntriesResponse(success, context_->current_term, raft_log_->GetLastLogIndex(), response); @@ -667,8 +677,8 @@ void FloydImpl::ReplyAppendEntries(CmdRequest& request, CmdResponse* response) { // Append entry if (append_entries.prev_log_index() < raft_log_->GetLastLogIndex()) { - LOGV(WARN_LEVEL, info_log_, "FloydImpl::ReplyAppentries: leader %s:%d pre_log(%lu, %lu)'s index smaller than" - " my log(%lu) index, truncate suffix from here %lu", append_entries.ip().c_str(), append_entries.port(), + LOGV(WARN_LEVEL, info_log_, "FloydImpl::ReplyAppendEtries: leader %s:%d pre_log(%lu, %lu)'s index smaller than" + " my log index %lu, truncate suffix from %lu", append_entries.ip().c_str(), append_entries.port(), append_entries.prev_log_term(), append_entries.prev_log_index(), raft_log_->GetLastLogIndex(), append_entries.prev_log_index() + 1); raft_log_->TruncateSuffix(append_entries.prev_log_index() + 1); @@ -692,7 +702,7 @@ void FloydImpl::ReplyAppendEntries(CmdRequest& request, CmdResponse* response) { if (append_entries.prev_log_term() != my_last_log_term) { LOGV(WARN_LEVEL, info_log_, "FloydImpl::ReplyAppentries: leader %s:%d pre_log(%lu, %lu)'s term don't match with" - " my log(%lu, %lu) term, truncate my log from here %lu",append_entries.ip().c_str(), append_entries.port(), + " my log(%lu, %lu) term, truncate my log from %lu",append_entries.ip().c_str(), append_entries.port(), append_entries.prev_log_term(), append_entries.prev_log_index(), my_last_log_term, raft_log_->GetLastLogIndex(), append_entries.prev_log_index()); // TruncateSuffix [prev_log_index, last_log_index) @@ -713,12 +723,14 @@ void FloydImpl::ReplyAppendEntries(CmdRequest& request, CmdResponse* response) { BuildAppendEntriesResponse(success, context_->current_term, raft_log_->GetLastLogIndex(), response); return ; } - AdvanceFollowerCommitIndex(append_entries.leader_commit()); - apply_->ScheduleApply(); } else { LOGV(INFO_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries: Receive PingPong AppendEntries from %s:%d", append_entries.ip().c_str(), append_entries.port()); } + if (append_entries.leader_commit() != context_->commit_index) { + AdvanceFollowerCommitIndex(append_entries.leader_commit()); + apply_->ScheduleApply(); + } success = true; // only when follower successfully do appendentries, we will update commit index LOGV(DEBUG_LEVEL, info_log_, "FloydImpl::ReplyAppendEntries after AdvanceCommitIndex %lu", diff --git a/floyd/src/floyd_impl.h b/floyd/src/floyd_impl.h index 0bf6a4b..7607ea4 100644 --- a/floyd/src/floyd_impl.h +++ b/floyd/src/floyd_impl.h @@ -52,11 +52,12 @@ class FloydImpl : public Floyd { // return true if leader has been elected virtual bool GetLeader(std::string* ip_port); - virtual bool HasLeader(); virtual bool GetLeader(std::string* ip, int* port); + virtual bool HasLeader(); virtual bool GetAllNodes(std::vector& nodes); - virtual bool GetServerStatus(std::string& msg); + virtual bool IsLeader(); + virtual bool GetServerStatus(std::string& msg); // log level can be modified virtual void set_log_level(const int log_level); diff --git a/floyd/src/floyd_peer_thread.cc b/floyd/src/floyd_peer_thread.cc index 8a31b3d..43ce200 100644 --- a/floyd/src/floyd_peer_thread.cc +++ b/floyd/src/floyd_peer_thread.cc @@ -35,7 +35,9 @@ Peer::Peer(std::string server, FloydContext* context, FloydPrimary* primary, Raf options_(options), info_log_(info_log), next_index_(1), - match_index_(0) { + match_index_(0), + peer_last_op_time(0), + bg_thread_(1024 * 1024 * 256) { next_index_ = raft_log_->GetLastLogIndex() + 1; match_index_ = raft_meta_->GetLastApplied(); } @@ -60,7 +62,20 @@ bool Peer::CheckAndVote(uint64_t vote_term) { return (++context_->vote_quorum) > (options_.members.size() / 2); } +void Peer::UpdatePeerInfo() { + for (auto& pt : peers_) { + pt.second->set_next_index(raft_log_->GetLastLogIndex() + 1); + pt.second->set_match_index(0); + } +} + void Peer::AddRequestVoteTask() { + /* + * int timer_queue_size, queue_size; + * bg_thread_.QueueSize(&timer_queue_size, &queue_size); + * LOGV(INFO_LEVEL, info_log_, "Peer::AddRequestVoteTask timer_queue size %d queue_size %d", + * timer_queue_size, queue_size); + */ bg_thread_.Schedule(&RequestVoteRPCWrapper, this); } @@ -83,6 +98,8 @@ void Peer::RequestVoteRPC() { request_vote->set_term(context_->current_term); request_vote->set_last_log_term(last_log_term); request_vote->set_last_log_index(last_log_index); + LOGV(INFO_LEVEL, info_log_, "Peer::RequestVoteRPC server %s:%d Send RequestVoteRPC message to %s at term %d", + options_.local_ip.c_str(), options_.local_port, peer_addr_.c_str(), context_->current_term); } CmdResponse res; @@ -105,6 +122,7 @@ void Peer::RequestVoteRPC() { // we need igore these type of vote if (CheckAndVote(res.request_vote_res().term())) { context_->BecomeLeader(); + UpdatePeerInfo(); LOGV(INFO_LEVEL, info_log_, "Peer::RequestVoteRPC: %s:%d become leader at term %d", options_.local_ip.c_str(), options_.local_port, context_->current_term); primary_->AddTask(kHeartBeat, false); @@ -155,6 +173,12 @@ void Peer::AdvanceLeaderCommitIndex() { return; } void Peer::AddAppendEntriesTask() { + /* + * int timer_queue_size, queue_size; + * bg_thread_.QueueSize(&timer_queue_size, &queue_size); + * LOGV(INFO_LEVEL, info_log_, "Peer::AddAppendEntriesTask timer_queue size %d queue_size %d", + * timer_queue_size, queue_size); + */ bg_thread_.Schedule(&AppendEntriesRPCWrapper, this); } void Peer::AppendEntriesRPCWrapper(void *arg) { @@ -169,6 +193,15 @@ void Peer::AppendEntriesRPC() { CmdRequest_AppendEntries* append_entries = req.mutable_append_entries(); { slash::MutexLock l(&context_->global_mu); + last_log_index = raft_log_->GetLastLogIndex(); + /* + * LOGV(INFO_LEVEL, info_log_, "Peer::AppendEntriesRPC: next_index_ %d last_log_index %d peer_last_op_time %lu nowmicros %lu", + * next_index_.load(), last_log_index, peer_last_op_time, slash::NowMicros()); + */ + if (next_index_ > last_log_index && peer_last_op_time + options_.heartbeat_us > slash::NowMicros()) { + return; + } + peer_last_op_time = slash::NowMicros(); if (prev_log_index != 0) { Entry entry; @@ -186,11 +219,8 @@ void Peer::AppendEntriesRPC() { append_entries->set_term(context_->current_term); append_entries->set_prev_log_index(prev_log_index); append_entries->set_prev_log_term(prev_log_term); - - last_log_index = raft_log_->GetLastLogIndex(); + append_entries->set_leader_commit(context_->commit_index); Entry *tmp_entry = new Entry(); - LOGV(DEBUG_LEVEL, info_log_, "Peer::AppendEntriesRPC: peer_addr(%s)'s next_index_ %llu, my last_log_index %llu", - peer_addr_.c_str(), next_index_.load(), last_log_index); for (uint64_t index = next_index_; index <= last_log_index; index++) { if (raft_log_->GetEntry(index, tmp_entry) == 0) { // TODO(ba0tiao) how to avoid memory copy here @@ -209,12 +239,14 @@ void Peer::AppendEntriesRPC() { } } delete tmp_entry; - /* - * commit_index should be min of follower log and leader's commit_index - * if follower's commit index larger than follower log, it conflict - */ - append_entries->set_leader_commit( - std::min(context_->commit_index, prev_log_index + num_entries)); + LOGV(DEBUG_LEVEL, info_log_, "Peer::AppendEntriesRPC: peer_addr(%s)'s next_index_ %llu, my last_log_index %llu" + " AppendEntriesRPC will send %d iterm", peer_addr_.c_str(), next_index_.load(), last_log_index, num_entries); + // if the AppendEntries don't contain any log item + if (num_entries == 0) { + LOGV(INFO_LEVEL, info_log_, "Peer::AppendEntryRpc server %s:%d Send pingpong appendEntries message to %s at term %d", + options_.local_ip.c_str(), options_.local_port, peer_addr_.c_str(), context_->current_term); + } + } CmdResponse res; @@ -253,18 +285,20 @@ void Peer::AppendEntriesRPC() { apply_->ScheduleApply(); } next_index_ = prev_log_index + num_entries + 1; - // If this follower is far behind leader, and there is no more - // AppendEntryTask, we should add one - if (next_index_ + options_.append_entries_count_once < last_log_index) { - int pri_size, qu_size; - bg_thread_.QueueSize(&pri_size, &qu_size); - if (qu_size < 1) { - LOGV(DEBUG_LEVEL, info_log_, "Peer::AppendEntriesRPC: peer_addr %s AppendEntry again " - "to catch up next_index(%llu) last_log_index(%llu)", - peer_addr_.c_str(), next_index_.load(), last_log_index); - AddAppendEntriesTask(); - } - } + /* + * // If this follower is far behind leader, and there is no more + * // AppendEntryTask, we should add one + * if (next_index_ + options_.append_entries_count_once < last_log_index) { + * int pri_size, qu_size; + * bg_thread_.QueueSize(&pri_size, &qu_size); + * if (qu_size < 1) { + * LOGV(DEBUG_LEVEL, info_log_, "Peer::AppendEntriesRPC: peer_addr %s AppendEntry again " + * "to catch up next_index(%llu) last_log_index(%llu)", + * peer_addr_.c_str(), next_index_.load(), last_log_index); + * AddAppendEntriesTask(); + * } + * } + */ } } else { LOGV(INFO_LEVEL, info_log_, "Peer::AppEntriesRPC: peer_addr %s Send AppEntriesRPC failed," diff --git a/floyd/src/floyd_peer_thread.h b/floyd/src/floyd_peer_thread.h index c475d07..0f31f4c 100644 --- a/floyd/src/floyd_peer_thread.h +++ b/floyd/src/floyd_peer_thread.h @@ -62,6 +62,9 @@ class Peer { return next_index_; } + void set_match_index(const uint64_t match_index) { + match_index_ = match_index; + } uint64_t match_index() { return match_index_; } @@ -78,6 +81,7 @@ class Peer { bool CheckAndVote(uint64_t vote_term); uint64_t QuorumMatchIndex(); void AdvanceLeaderCommitIndex(); + void UpdatePeerInfo(); std::string peer_addr_; FloydContext* context_; @@ -93,6 +97,7 @@ class Peer { std::atomic next_index_; std::atomic match_index_; + uint64_t peer_last_op_time; pink::BGThread bg_thread_; diff --git a/floyd/src/floyd_primary_thread.cc b/floyd/src/floyd_primary_thread.cc index ba0663a..dd24c07 100644 --- a/floyd/src/floyd_primary_thread.cc +++ b/floyd/src/floyd_primary_thread.cc @@ -53,6 +53,12 @@ int FloydPrimary::Stop() { // 2. another long live Cron for ElectLeaderCheck, which is started when // creating Primary; void FloydPrimary::AddTask(TaskType type, bool is_delay) { + /* + * int timer_queue_size, queue_size; + * bg_thread_.QueueSize(&timer_queue_size, &queue_size); + * LOGV(INFO_LEVEL, info_log_, "FloydPrimary::AddTask timer_queue size %d queue_size %d tasktype %d is_delay %d", + * timer_queue_size, queue_size, type, is_delay); + */ switch (type) { case kHeartBeat: { if (is_delay) { @@ -140,14 +146,13 @@ void FloydPrimary::NoticePeerTask(TaskType type) { for (auto& peer : peers_) { switch (type) { case kHeartBeat: - LOGV(INFO_LEVEL, info_log_, "FloydPrimary::NoticePeerTask server %s:%d send request vote message to %s at term %d", + LOGV(INFO_LEVEL, info_log_, "FloydPrimary::NoticePeerTask server %s:%d Add request Task to queue to %s at term %d", options_.local_ip.c_str(), options_.local_port, peer.second->peer_addr().c_str(), context_->current_term); peer.second->AddRequestVoteTask(); break; case kNewCommand: - LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::NoticePeerTask server %s:%d send appendEntries message to %s at term %d", + LOGV(DEBUG_LEVEL, info_log_, "FloydPrimary::NoticePeerTask server %s:%d Add appendEntries Task to queue to %s at term %d", options_.local_ip.c_str(), options_.local_port, peer.second->peer_addr().c_str(), context_->current_term); - peer.second->AddAppendEntriesTask(); break; default: diff --git a/floyd/src/raft_log.cc b/floyd/src/raft_log.cc index df4d1dd..50167a0 100644 --- a/floyd/src/raft_log.cc +++ b/floyd/src/raft_log.cc @@ -57,28 +57,26 @@ RaftLog::~RaftLog() { uint64_t RaftLog::Append(const std::vector &entries) { slash::MutexLock l(&lli_mutex_); - std::string buf; - rocksdb::Status s; rocksdb::WriteBatch wb; LOGV(DEBUG_LEVEL, info_log_, "RaftLog::Append: entries.size %lld", entries.size()); for (size_t i = 0; i < entries.size(); i++) { + std::string buf; entries[i]->SerializeToString(&buf); last_log_index_++; - // wb.Put(UintToBitStr(last_log_index_), buf); - s = db_->Put(rocksdb::WriteOptions(), UintToBitStr(last_log_index_), buf); - if (!s.ok()) { - LOGV(ERROR_LEVEL, info_log_, "RaftLog::Append %lu string %s false\n", last_log_index_, UintToBitStr(last_log_index_).c_str()); - return --last_log_index_; - } + wb.Put(UintToBitStr(last_log_index_), buf); + // s = db_->Put(rocksdb::WriteOptions(), UintToBitStr(last_log_index_), buf); + // if (!s.ok()) { + // LOGV(ERROR_LEVEL, info_log_, "RaftLog::Append %lu string %s false\n", last_log_index_, UintToBitStr(last_log_index_).c_str()); + // return --last_log_index_; + // } + } + rocksdb::Status s; + s = db_->Write(rocksdb::WriteOptions(), &wb); + if (!s.ok()) { + LOGV(ERROR_LEVEL, info_log_, "RaftLog::Append %lu false\n", last_log_index_); + last_log_index_ -= entries.size(); + return last_log_index_; } - /* - * s = db_->Write(rocksdb::WriteOptions(), &wb); - * if (!s.ok()) { - * LOGV(ERROR_LEVEL, info_log_, "RaftLog::Append %lu false\n", last_log_index_); - * return last_log_index_; - * } - * last_log_index_ += entries.size(); - */ return last_log_index_; } @@ -130,6 +128,8 @@ int RaftLog::TruncateSuffix(uint64_t index) { for (; last_log_index_ >= index; last_log_index_--) { rocksdb::Status s = db_->Delete(rocksdb::WriteOptions(), UintToBitStr(last_log_index_)); if (!s.ok()) { + LOGV(ERROR_LEVEL, info_log_, "RaftLog::TruncateSuffix Error last_log_index %lu " + "truncate from %lu\n", last_log_index_, index); return -1; } } diff --git a/floyd/tools/README b/floyd/tools/README index 5c873ba..d1bb7ee 100644 --- a/floyd/tools/README +++ b/floyd/tools/README @@ -1,3 +1,5 @@ read_floyd is the tool to read floyd log read_rock is the tool to read rocksdb data cpt is a tool to compare two rocksdb, if they have the same data return true, else return false +cl construct 4 same log, ant one log with longer logger +cl1 construct the raft paper's Figure 7 logger, cl1 is the leader, cl2 is (a), cl3 is (c), cl4 is (e), cl5 is (f) diff --git a/floyd/tools/cl.cc b/floyd/tools/cl.cc index 38ed3c1..1ea2208 100644 --- a/floyd/tools/cl.cc +++ b/floyd/tools/cl.cc @@ -37,7 +37,7 @@ int cl(const std::string path) { for (int i = 0; i < 10; i++) { Entry *entry = new Entry(); snprintf(buff, sizeof(buff), "%d", i); - InitEntry(10, std::string(buff, sizeof(int)), std::string(buff, sizeof(int))); + InitEntry(10, std::string(buff, sizeof(int)), std::string(buff, sizeof(int)), entry); entries.push_back(entry); } raft_log->Append(entries); diff --git a/floyd/tools/cl1.cc b/floyd/tools/cl1.cc new file mode 100644 index 0000000..8da5b1c --- /dev/null +++ b/floyd/tools/cl1.cc @@ -0,0 +1,369 @@ +/** + * @file cl1.cc + * @brief construct raft log with different log entry + * @author chenzongzhi + * @version 1.0.0 + * @date 2017-08-06 + */ + + +#include +#include + +#include + +#include "rocksdb/db.h" +#include "slash/include/env.h" + +#include "floyd/src/raft_log.h" +#include "floyd/src/raft_meta.h" +#include "floyd/src/floyd.pb.h" +#include "floyd/src/logger.h" + +using namespace floyd; + +void InitEntry(int term, const std::string &key, const std::string &val, Entry *entry) { + entry->set_term(term); + entry->set_key(key); + entry->set_value(val); + entry->set_optype(Entry_OpType_kWrite); +} +int cl1(const std::string path) { + // construct data1 node + Logger *logger; + slash::CreatePath(path); + if (NewLogger(path + "/LOG", &logger) != 0) { + return -1; + } + rocksdb::DB* db; + rocksdb::Options options; + options.create_if_missing = true; + rocksdb::Status s = rocksdb::DB::Open(options, path + "/log/", &db); + RaftMeta *raft_meta = new RaftMeta(db, logger); + raft_meta->Init(); + RaftLog *raft_log = new RaftLog(db, logger); + + std::vector entries; + Entry *entry = new Entry(); + InitEntry(1, "1", "1", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(1, "2", "2", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(1, "3", "3", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(4, "4", "4", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(4, "5", "5", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(5, "6", "6", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(5, "7", "7", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(6, "8", "8", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(6, "9", "9", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(6, "10", "10", entry); + entries.push_back(entry); + + raft_log->Append(entries); + raft_meta->SetCurrentTerm(6); + raft_meta->SetCommitIndex(9); + raft_meta->SetLastApplied(0); + + delete raft_meta; + delete raft_log; + delete db; + delete logger; +} + +int cl2(const std::string path) { + // construct data1 node + Logger *logger; + slash::CreatePath(path); + if (NewLogger(path + "/LOG", &logger) != 0) { + return -1; + } + rocksdb::DB* db; + rocksdb::Options options; + options.create_if_missing = true; + rocksdb::Status s = rocksdb::DB::Open(options, path + "/log/", &db); + RaftMeta *raft_meta = new RaftMeta(db, logger); + raft_meta->Init(); + RaftLog *raft_log = new RaftLog(db, logger); + + std::vector entries; + Entry *entry = new Entry(); + InitEntry(1, "1", "1", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(1, "2", "2", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(1, "3", "3", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(4, "4", "4", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(4, "5", "5", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(5, "6", "6", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(5, "7", "7", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(6, "8", "8", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(6, "9", "9", entry); + entries.push_back(entry); + + raft_log->Append(entries); + raft_meta->SetCurrentTerm(6); + raft_meta->SetCommitIndex(9); + raft_meta->SetLastApplied(0); + + delete raft_meta; + delete raft_log; + delete db; + delete logger; +} + +int cl3(const std::string path) { + // construct data1 node + Logger *logger; + slash::CreatePath(path); + if (NewLogger(path + "/LOG", &logger) != 0) { + return -1; + } + rocksdb::DB* db; + rocksdb::Options options; + options.create_if_missing = true; + rocksdb::Status s = rocksdb::DB::Open(options, path + "/log/", &db); + RaftMeta *raft_meta = new RaftMeta(db, logger); + raft_meta->Init(); + RaftLog *raft_log = new RaftLog(db, logger); + + std::vector entries; + Entry *entry = new Entry(); + InitEntry(1, "1", "1", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(1, "2", "2", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(1, "3", "3", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(4, "4", "4", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(4, "5", "5", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(5, "6", "6", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(5, "7", "7", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(6, "8", "8", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(6, "9", "9", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(6, "10", "10", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(6, "11", "11", entry); + entries.push_back(entry); + + raft_log->Append(entries); + raft_meta->SetCurrentTerm(6); + raft_meta->SetCommitIndex(9); + raft_meta->SetLastApplied(0); + + delete raft_meta; + delete raft_log; + delete db; + delete logger; +} + + +int cl4(const std::string path) { + // construct data1 node + Logger *logger; + slash::CreatePath(path); + if (NewLogger(path + "/LOG", &logger) != 0) { + return -1; + } + rocksdb::DB* db; + rocksdb::Options options; + options.create_if_missing = true; + rocksdb::Status s = rocksdb::DB::Open(options, path + "/log/", &db); + RaftMeta *raft_meta = new RaftMeta(db, logger); + raft_meta->Init(); + RaftLog *raft_log = new RaftLog(db, logger); + + std::vector entries; + Entry *entry = new Entry(); + InitEntry(1, "1", "1", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(1, "2", "2", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(1, "3", "3", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(4, "4", "4", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(4, "5", "5", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(4, "6", "6", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(4, "7", "7", entry); + entries.push_back(entry); + + raft_log->Append(entries); + raft_meta->SetCurrentTerm(4); + raft_meta->SetCommitIndex(3); + raft_meta->SetLastApplied(0); + + delete raft_meta; + delete raft_log; + delete db; + delete logger; +} + +int cl5(const std::string path) { + // construct data1 node + Logger *logger; + slash::CreatePath(path); + if (NewLogger(path + "/LOG", &logger) != 0) { + return -1; + } + rocksdb::DB* db; + rocksdb::Options options; + options.create_if_missing = true; + rocksdb::Status s = rocksdb::DB::Open(options, path + "/log/", &db); + RaftMeta *raft_meta = new RaftMeta(db, logger); + raft_meta->Init(); + RaftLog *raft_log = new RaftLog(db, logger); + + std::vector entries; + Entry *entry = new Entry(); + InitEntry(1, "1", "1", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(1, "2", "2", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(1, "3", "3", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(2, "4", "4", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(2, "5", "5", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(2, "6", "6", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(3, "7", "7", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(3, "8", "8", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(3, "9", "9", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(3, "10", "10", entry); + entries.push_back(entry); + + entry = new Entry(); + InitEntry(3, "11", "11", entry); + entries.push_back(entry); + + raft_log->Append(entries); + raft_meta->SetCurrentTerm(3); + raft_meta->SetCommitIndex(3); + raft_meta->SetLastApplied(0); + + delete raft_meta; + delete raft_log; + delete db; + delete logger; +} + +int main() +{ + + cl1("./data1/"); + cl2("./data2/"); + cl3("./data3/"); + cl4("./data4/"); + cl5("./data5/"); + + return 0; +} From 00efc0529b9cdaa027dd30221df872f35ab3c533 Mon Sep 17 00:00:00 2001 From: baotiao Date: Wed, 9 Aug 2017 17:31:18 +0800 Subject: [PATCH 17/17] fix QuorumMatchIndex bug --- floyd/example/simple/t7.cc | 14 ++++++++++++-- floyd/src/floyd_peer_thread.cc | 5 ++++- floyd/src/floyd_primary_thread.h | 1 - 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/floyd/example/simple/t7.cc b/floyd/example/simple/t7.cc index 5c7f6f9..c7b34ff 100644 --- a/floyd/example/simple/t7.cc +++ b/floyd/example/simple/t7.cc @@ -56,6 +56,9 @@ int main(int argc, char *argv[]) std::string msg; int cnt = 1; uint64_t st = NowMicros(), ed; + for (int i = 0; i < item_num; i++) { + keystr[i] = slash::RandomString(32); + } while (1) { if (f1->HasLeader()) { @@ -82,6 +85,7 @@ int main(int argc, char *argv[]) delete f1; delete f5; + sleep(10); while (1) { if (f2->HasLeader()) { break; @@ -98,10 +102,15 @@ int main(int argc, char *argv[]) printf("%s\n", msg.c_str()); st = NowMicros(); for (int j = 0; j < item_num; j++) { - f2->Write(keystr[j], valstr[j]); + slash::Status s1 = f2->Write(keystr[j], valstr[j]); + if (s.ok()) { + printf("write key success %s %s\n", keystr[j].c_str(), valstr[j].c_str()); + } else { + printf("write error\n"); + } } ed = NowMicros(); - printf("write 100000 cost time microsecond(us) %ld, qps %llu\n", ed - st, item_num * 1000000LL / (ed - st)); + printf("write after delete two nodes cost time microsecond(us) %ld, qps %llu\n", ed - st, item_num * 1000000LL / (ed - st)); } s = Floyd::Open(op, &f1); @@ -111,6 +120,7 @@ int main(int argc, char *argv[]) while (cnt--) { f2->GetServerStatus(msg); printf("%s\n", msg.c_str()); + sleep(2); } getchar(); diff --git a/floyd/src/floyd_peer_thread.cc b/floyd/src/floyd_peer_thread.cc index 43ce200..4b645f5 100644 --- a/floyd/src/floyd_peer_thread.cc +++ b/floyd/src/floyd_peer_thread.cc @@ -153,10 +153,13 @@ uint64_t Peer::QuorumMatchIndex() { std::map::iterator iter; for (iter = peers_.begin(); iter != peers_.end(); iter++) { if (iter->first == peer_addr_) { + values.push_back(match_index_); continue; } values.push_back(iter->second->match_index()); } + LOGV(DEBUG_LEVEL, info_log_, "Peer::QuorumMatchIndex: Get peers match_index %d %d %d %d", + values[0], values[1], values[2], values[3]); std::sort(values.begin(), values.end()); return values.at(values.size() / 2); } @@ -166,7 +169,7 @@ uint64_t Peer::QuorumMatchIndex() { void Peer::AdvanceLeaderCommitIndex() { Entry entry; uint64_t new_commit_index = QuorumMatchIndex(); - if (context_->commit_index != new_commit_index) { + if (context_->commit_index < new_commit_index) { context_->commit_index = new_commit_index; raft_meta_->SetCommitIndex(context_->commit_index); } diff --git a/floyd/src/floyd_primary_thread.h b/floyd/src/floyd_primary_thread.h index 39aea0a..0f6f139 100644 --- a/floyd/src/floyd_primary_thread.h +++ b/floyd/src/floyd_primary_thread.h @@ -70,7 +70,6 @@ class FloydPrimary { void LaunchNewCommand(); void NoticePeerTask(TaskType type); - uint64_t QuorumMatchIndex(); // No copying allowed FloydPrimary(const FloydPrimary&);