From d441031c6381c11164cd3e077dc16ae1900b9327 Mon Sep 17 00:00:00 2001 From: Sherman The Tank <5414276+sherman-the-tank@users.noreply.github.com> Date: Tue, 18 Sep 2018 16:59:48 +0800 Subject: [PATCH] Rename consensus directory to raftex (#17) --- CMakeLists.txt | 4 +- consensus/CMakeLists.txt | 8 - consensus/Replica.cpp | 17 -- consensus/Replica.h | 148 ------------------ raftex/CMakeLists.txt | 8 + {consensus => raftex}/FileBasedWal.cpp | 6 +- {consensus => raftex}/FileBasedWal.h | 12 +- {consensus => raftex}/LogIterator.h | 10 +- {consensus => raftex}/Wal.h | 12 +- {consensus => raftex}/test/CMakeLists.txt | 2 +- .../test/FileBasedWalTest.cpp | 4 +- 11 files changed, 33 insertions(+), 198 deletions(-) delete mode 100644 consensus/CMakeLists.txt delete mode 100644 consensus/Replica.cpp delete mode 100644 consensus/Replica.h create mode 100644 raftex/CMakeLists.txt rename {consensus => raftex}/FileBasedWal.cpp (99%) rename {consensus => raftex}/FileBasedWal.h (96%) rename {consensus => raftex}/LogIterator.h (80%) rename {consensus => raftex}/Wal.h (82%) rename {consensus => raftex}/test/CMakeLists.txt (91%) rename {consensus => raftex}/test/FileBasedWalTest.cpp (98%) diff --git a/CMakeLists.txt b/CMakeLists.txt index e92922ab774..050ad77027a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -105,10 +105,10 @@ add_subdirectory(interface) add_subdirectory(common) add_subdirectory(dataman) add_subdirectory(client) -#add_subdirectory(storage) add_subdirectory(server) add_subdirectory(console) -add_subdirectory(consensus) +add_subdirectory(raftex) +#add_subdirectory(storage) add_dependencies(common third-party) #add_dependencies(storage_engines common) diff --git a/consensus/CMakeLists.txt b/consensus/CMakeLists.txt deleted file mode 100644 index b162776683a..00000000000 --- a/consensus/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -add_library( - consensus_obj OBJECT - FileBasedWal.cpp -) -add_dependencies(consensus_obj common) - -add_subdirectory(test) - diff --git a/consensus/Replica.cpp b/consensus/Replica.cpp deleted file mode 100644 index bf01670c5b8..00000000000 --- a/consensus/Replica.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/* Copyright (c) 2018 - present, VE Software Inc. All rights reserved - * - * This source code is licensed under Apache 2.0 License - * (found in the LICENSE.Apache file in the root directory) - */ - -#include "consensus/Replica.h" - -namespace vesoft { -namespace vgraph { -namespace consensus { - - -} // namespace consensus -} // namespace vgraph -} // namespace vesoft - diff --git a/consensus/Replica.h b/consensus/Replica.h deleted file mode 100644 index cf259c06403..00000000000 --- a/consensus/Replica.h +++ /dev/null @@ -1,148 +0,0 @@ -/* Copyright (c) 2018 - present, VE Software Inc. All rights reserved - * - * This source code is licensed under Apache 2.0 License - * (found in the LICENSE.Apache file in the root directory) - */ - -#ifndef CONSENSUS_REPLICA_H_ -#define CONSENSUS_REPLICA_H_ - -#include "base/Base.h" - -namespace vesoft { -namespace vgraph { -namespace consensus { - -class Replica { -public: - Replica(cpp2::PartitionID partId, - cpp2::IPv4 localIp, - cpp2::Port localPort, - const folly::StringPiece walRoot); - virtual ~Replica(); - - void start(); - void stop(); - - void addReplica(const folly::StringPiece addr, cpp2::Port port); - - /***************************************************** - * - * Methods to process incoming consensus requests - * - ****************************************************/ - // Process the incoming leader selection request - void processAskForVoteRequest(std::unique_ptr req, - cpp2::AskForVoteResponse& resp); - - // Process appendLog request - void processAppendLogRequest(std::unique_ptr req, - cpp2::AppendLogResponse& resp); - - /******************************************************** - * - * Public Log Append API, used by the consensus caller - * - *******************************************************/ - // Asynchronously append a log. - // - // The method will take the ownership of the log and returns as soon as - // possible. Internally it will asynchronously try to send the log to - // all followers. It will keep trying until majority of followers accept - // the log, then the future will be fulfilled - folly::Future appendLogAsync(std::string& logMsg, - uint32_t timeoutInMs = 0, - bool sendToListenersToo = true); - -protected: - // This method is called when my leader term is finished, either by - // receiving a new leader election request, or a new leader heartbeat. - virtual void onLostLeadership(cpp2::TermID term) = 0; - - // This method is called when I'm elected as a new leader. - virtual void onElected(cpp2::TermID term) = 0; - - // The subclass implements this method to commit a batch of log messages - virtual bool commitLogs(int64_t firstLogId, - std::vector&& logMsgs) = 0; - -private: - enum class Role : uint64_t { - LEADER = 1, // I'm the leader - FOLLOWER, // I'm following a leader - CANDIDATE // I'm waiting to be elected - }; -#define GET_MY_ROLE() \ - static_cast((myRoleSig_.load() & 0x00000000FFFFFFFF)) -#define GET_ROLE(roleSig) static_cast((roleSig & 0x00000000FFFFFFFF)) -// SET_ROLE should always be protected by consensusLock_ -#define SET_MY_ROLE(oldRoleSig, setToRole) \ - myRoleSig_.compare_exchange_strong( \ - oldRoleSig, \ - ((oldRoleSig & 0xFFFFFFFF00000000) + 0x0000000100000000) | \ - static_cast(setToRole)) - - enum class Status { - STARTING = 0, // The part is starting, not ready for service - RUNNING, // The part is running - STOPPED // The part has been stopped - }; - - // My current role signature - // The higher 32 bits are ABA, the lower 32 bits are the current role - // - // NOTE: The change of the role signature has to be protected by the - // consensusLock_ - std::atomic roleSig_{0}; - - /******************************************************************* - * * - * Exclusive access Section * - * * - * Access to all members defined in this section should be guarded * - * by ##consensusLock_## * - * * - * */ - mutable std::mutex consensusLock_; - - // All my peers. - // TODO(sye) For now, we assume the peer list is unchangeable. But this - // could be changed when the cluster grows or shrinks. We will handle it - // in the future - std::unordered_map, - std::unique_ptr> - > peerHosts_; - using HostRefType = typename decltype(peerHosts_)::value_type; - - std::unordered_map> listeners_; - using ListenerRefType = typename decltype(listeners_)::value_type; - - // When I'm the leader, the leaderIp_ and leaderPort_ is same - // as localIp_ and localPort_ - cpp2::IPv4 leaderIp_{0}; - cpp2::Port leaderPort_{0}; - - // The current term id - // - // When I become a candidate, termId will be bumped up by 1 - // When I voted for someone, this will be set to the term id proposed - // by that candidate - cpp2::TermID term_{0}; - - // The id for the last received log - cpp2::LogID lastLogId_{0}; - // The id for the last globally committed log (synced from the leader) - cpp2::LogID committedLogId_{0}; - // The id for the last locally committed log - cpp2::LogID myLastCommittedLogId_{0}; - /* * - * * - * End of Exclusive access Section * - * * - ******************************************************************/ -}; - -} // namespace consensus -} // namespace vgraph -} // namespace vesoft -#endif // CONSENSUS_REPLICA_H_ diff --git a/raftex/CMakeLists.txt b/raftex/CMakeLists.txt new file mode 100644 index 00000000000..9401f1b2dc4 --- /dev/null +++ b/raftex/CMakeLists.txt @@ -0,0 +1,8 @@ +add_library( + raftex_obj OBJECT + FileBasedWal.cpp +) +add_dependencies(raftex_obj common) + +add_subdirectory(test) + diff --git a/consensus/FileBasedWal.cpp b/raftex/FileBasedWal.cpp similarity index 99% rename from consensus/FileBasedWal.cpp rename to raftex/FileBasedWal.cpp index cee9ebbda22..9a9be9211e9 100644 --- a/consensus/FileBasedWal.cpp +++ b/raftex/FileBasedWal.cpp @@ -4,12 +4,12 @@ * (found in the LICENSE.Apache file in the root directory) */ -#include "consensus/FileBasedWal.h" +#include "raftex/FileBasedWal.h" #include "fs/FileUtils.h" namespace vesoft { namespace vgraph { -namespace consensus { +namespace raftex { using WalFileInfoPair = FileBasedWal::WalFiles::value_type; using namespace vesoft::fs; @@ -721,7 +721,7 @@ std::unique_ptr FileBasedWal::iterator(LogID firstLogId) { new internal::FileBasedWalIterator(shared_from_this(), firstLogId)); } -} // namespace consensus +} // namespace raftex } // namespace vgraph } // namespace vesoft diff --git a/consensus/FileBasedWal.h b/raftex/FileBasedWal.h similarity index 96% rename from consensus/FileBasedWal.h rename to raftex/FileBasedWal.h index f619d599ffd..8ca6255f06e 100644 --- a/consensus/FileBasedWal.h +++ b/raftex/FileBasedWal.h @@ -4,16 +4,16 @@ * (found in the LICENSE.Apache file in the root directory) */ -#ifndef CONSENSUS_WAL_FILEBASEDWAL_H_ -#define CONSENSUS_WAL_FILEBASEDWAL_H_ +#ifndef RAFTEX_WAL_FILEBASEDWAL_H_ +#define RAFTEX_WAL_FILEBASEDWAL_H_ #include "base/Base.h" #include "base/Cord.h" -#include "consensus/Wal.h" +#include "raftex/Wal.h" namespace vesoft { namespace vgraph { -namespace consensus { +namespace raftex { struct FileBasedWalPolicy { // The life span of the log messages (number of seconds) @@ -179,8 +179,8 @@ class FileBasedWal final : public Wal, std::string msg); }; -} // namespace consensus +} // namespace raftex } // namespace vgraph } // namespace vesoft -#endif // CONSENSUS_WAL_FILEBASEDWAL_H_ +#endif // RAFTEX_WAL_FILEBASEDWAL_H_ diff --git a/consensus/LogIterator.h b/raftex/LogIterator.h similarity index 80% rename from consensus/LogIterator.h rename to raftex/LogIterator.h index 3e177391d0d..b50bd0c681a 100644 --- a/consensus/LogIterator.h +++ b/raftex/LogIterator.h @@ -4,14 +4,14 @@ * (found in the LICENSE.Apache file in the root directory) */ -#ifndef CONSENSUS_LOGITERATOR_H_ -#define CONSENSUS_LOGITERATOR_H_ +#ifndef RAFTEX_LOGITERATOR_H_ +#define RAFTEX_LOGITERATOR_H_ #include "base/Base.h" namespace vesoft { namespace vgraph { -namespace consensus { +namespace raftex { class LogIterator { public: @@ -28,8 +28,8 @@ class LogIterator { virtual std::string logMsg() const = 0; }; -} // namespace consensus +} // namespace raftex } // namespace vgraph } // namespace vesoft -#endif // CONSENSUS_LOGITERATOR_H_ +#endif // RAFTEX_LOGITERATOR_H_ diff --git a/consensus/Wal.h b/raftex/Wal.h similarity index 82% rename from consensus/Wal.h rename to raftex/Wal.h index fb9de0cde54..7fcb8a1ef09 100644 --- a/consensus/Wal.h +++ b/raftex/Wal.h @@ -4,15 +4,15 @@ * (found in the LICENSE.Apache file in the root directory) */ -#ifndef CONSENSUS_WAL_WAL_H_ -#define CONSENSUS_WAL_WAL_H_ +#ifndef RAFTEX_WAL_WAL_H_ +#define RAFTEX_WAL_WAL_H_ #include "base/Base.h" -#include "consensus/LogIterator.h" +#include "raftex/LogIterator.h" namespace vesoft { namespace vgraph { -namespace consensus { +namespace raftex { /** * Base class for all WAL implementations @@ -34,8 +34,8 @@ class Wal { virtual std::unique_ptr iterator(int64_t firstLogId) = 0; }; -} // namespace consensus +} // namespace raftex } // namespace vgraph } // namespace vesoft -#endif // CONSENSUS_WAL_WAL_H_ +#endif // RAFTEX_WAL_WAL_H_ diff --git a/consensus/test/CMakeLists.txt b/raftex/test/CMakeLists.txt similarity index 91% rename from consensus/test/CMakeLists.txt rename to raftex/test/CMakeLists.txt index 11408e499b8..599a5cd9287 100644 --- a/consensus/test/CMakeLists.txt +++ b/raftex/test/CMakeLists.txt @@ -1,7 +1,7 @@ add_executable( file_based_wal_test FileBasedWalTest.cpp - $ + $ $ $ $ diff --git a/consensus/test/FileBasedWalTest.cpp b/raftex/test/FileBasedWalTest.cpp similarity index 98% rename from consensus/test/FileBasedWalTest.cpp rename to raftex/test/FileBasedWalTest.cpp index cabae9765b9..24470833acb 100644 --- a/consensus/test/FileBasedWalTest.cpp +++ b/raftex/test/FileBasedWalTest.cpp @@ -6,12 +6,12 @@ #include "base/Base.h" #include -#include "consensus/FileBasedWal.h" +#include "raftex/FileBasedWal.h" #include "fs/TempDir.h" namespace vesoft { namespace vgraph { -namespace consensus { +namespace raftex { using namespace vesoft::fs;