Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: Implementing Redis-Raft commands to build a cluster. #136

Merged
merged 76 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
fc35d0f
feat: import braft
panlei-coder Jan 9, 2024
28fb07c
fix: fix braft.cmake
panlei-coder Jan 9, 2024
202879b
fix:fix import braft cmake
panlei-coder Jan 12, 2024
ff58235
fix: fix fetchcontent_populate
panlei-coder Jan 12, 2024
d03d09a
fix: fix find gflags brpc braft cmake
panlei-coder Jan 14, 2024
2f38634
add independent code and fix brpc
Centurybbx Jan 15, 2024
1a94613
Merge branch 'unstable' into import-braft
panlei-coder Jan 16, 2024
04e425c
fix: delete unnecessary comments
panlei-coder Jan 16, 2024
7dba371
fix: fix fetch content to ExternalProject_Add
panlei-coder Jan 18, 2024
bb59287
fix: add counter.pb.h and counter.pb.cc
panlei-coder Jan 18, 2024
665a992
Merge branch 'OpenAtomFoundation:import-braft' into import-braft
panlei-coder Jan 18, 2024
6e45317
feat:Import braft (#133)
panlei-coder Jan 18, 2024
0182848
fix: fixed a bug where the compiler could not find glog
panlei-coder Jan 19, 2024
c6f91e1
Merge branch 'import-braft' into import-braft
panlei-coder Jan 19, 2024
cfe3482
fix:fixed a bug where the compiler could not find glog (#134)
panlei-coder Jan 19, 2024
d5ddcbe
feat: add basic braft state_machine class.
KKorpse Jan 8, 2024
edfa444
feat: NODE.ADD and NODE.REMOVE cmd simply supported.
KKorpse Jan 10, 2024
617a146
feat: part impl of RAFT.CLUSTER.
KKorpse Jan 19, 2024
f7706ae
feat: RAFT.CLUSTER JOIN command can send request now.
KKorpse Jan 20, 2024
e78d687
fix: Remove cmake change of KKorpse.
KKorpse Jan 20, 2024
9d8296c
fix: reverse change of leveldb.cmake, fix a bug of PRaft::SendNodeAdd…
KKorpse Jan 21, 2024
ac0daa8
fix:fixed a bug that included braft
panlei-coder Jan 31, 2024
d6b8e55
Merge branch 'import-braft' of https://github.com/panlei-coder/pikiwi…
panlei-coder Jan 31, 2024
538041c
fix: modify the link script for braft
panlei-coder Feb 2, 2024
aea6aca
fix:fixed a bug that included braft (#163)
panlei-coder Feb 3, 2024
ea22b29
change rm command format
AlexStocks Feb 7, 2024
7977cb8
fix:improve basic functions and to be further debugging
panlei-coder Feb 20, 2024
8d03e8d
fix:fix compilation issues in debug mode
panlei-coder Feb 22, 2024
1dc6e51
fix:Node::AddPeer is faulty and needs further debugging
panlei-coder Feb 23, 2024
f239907
fix:the commissioning of the three-node cluster is complete
panlei-coder Feb 24, 2024
6f9afc3
fix:fix comment
panlei-coder Feb 24, 2024
2090c8c
fix:Synchronize the latest changes to the import-braft branch (#180)
panlei-coder Feb 27, 2024
2a49a98
feat: import braft
panlei-coder Jan 9, 2024
0f1eada
fix: fix braft.cmake
panlei-coder Jan 9, 2024
036bb94
fix:fix import braft cmake
panlei-coder Jan 12, 2024
8ec20df
fix: fix fetchcontent_populate
panlei-coder Jan 12, 2024
9cc9f53
fix: fix find gflags brpc braft cmake
panlei-coder Jan 14, 2024
37b7744
add independent code and fix brpc
Centurybbx Jan 15, 2024
9c366a5
fix: delete unnecessary comments
panlei-coder Jan 16, 2024
7dfb91d
fix: fix fetch content to ExternalProject_Add
panlei-coder Jan 18, 2024
921fe14
fix: add counter.pb.h and counter.pb.cc
panlei-coder Jan 18, 2024
1ca6b3b
fix: fixed a bug where the compiler could not find glog
panlei-coder Jan 19, 2024
7262dd0
fix:fixed a bug that included braft
panlei-coder Jan 31, 2024
bf54c66
fix:fix bug in debug mode
panlei-coder Feb 25, 2024
3a5d540
fix:fixed a bug where the compiler could not find glog (#134)
panlei-coder Jan 19, 2024
cabc2c0
fix:resolve code conflicts
panlei-coder Feb 26, 2024
311458a
fix:disable glog
panlei-coder Feb 27, 2024
bfd087f
fix:fix compilation issues on mac
panlei-coder Feb 28, 2024
3b50bf6
Merge branch 'import-braft' into import-braft
panlei-coder Feb 28, 2024
cef1beb
mac & linux
dingxiaoshuai123 Feb 29, 2024
2e048e4
fix:resolve fail of not found leveldb
panlei-coder Feb 29, 2024
a5cfb78
fix:uniformly capitalize the cmake keyword
panlei-coder Feb 29, 2024
7c78fce
fix:fix comment
panlei-coder Mar 3, 2024
9b8f50b
feat: add basic braft state_machine class.
KKorpse Jan 8, 2024
3c76d42
feat: NODE.ADD and NODE.REMOVE cmd simply supported.
KKorpse Jan 10, 2024
db2974a
feat: part impl of RAFT.CLUSTER.
KKorpse Jan 19, 2024
345eb04
feat: RAFT.CLUSTER JOIN command can send request now.
KKorpse Jan 20, 2024
5f0ebb5
fix: Remove cmake change of KKorpse.
KKorpse Jan 20, 2024
708975d
fix: reverse change of leveldb.cmake, fix a bug of PRaft::SendNodeAdd…
KKorpse Jan 21, 2024
fd07c2f
feat: import braft
panlei-coder Jan 9, 2024
12a2c45
fix:fix import braft cmake
panlei-coder Jan 12, 2024
9409b9d
fix: fix fetch content to ExternalProject_Add
panlei-coder Jan 18, 2024
269763d
fix:improve basic functions and to be further debugging
panlei-coder Feb 20, 2024
2bdc0e3
fix:Node::AddPeer is faulty and needs further debugging
panlei-coder Feb 23, 2024
600412b
fix:the commissioning of the three-node cluster is complete
panlei-coder Feb 24, 2024
5f0fa8e
fix:fix comment
panlei-coder Feb 24, 2024
b425ee3
fix:Add the encapsulation of the apply interface
panlei-coder Mar 3, 2024
ec995ca
fix:rebase to the import-braft branch
panlei-coder Mar 3, 2024
a87cb4e
fix:fix rebase errors
panlei-coder Mar 3, 2024
06827aa
fix:resolve merge conflicts
panlei-coder Mar 3, 2024
4d0995c
fix: remove counter and generate praft.proto src in cmake
longfar-ncy Mar 4, 2024
157c13a
fix: can not build debug version
longfar-ncy Mar 4, 2024
5502548
fix:fix comment
panlei-coder Mar 4, 2024
39ebb56
fix:resolve merge conflicts
panlei-coder Mar 4, 2024
cf8d9bf
Merge branch 'import-braft' into raft
panlei-coder Mar 6, 2024
07e9765
fix:fix rebase bug
panlei-coder Mar 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ ENABLE_TESTING()

ADD_SUBDIRECTORY(src/pstd)
ADD_SUBDIRECTORY(src/net)
ADD_SUBDIRECTORY(src/praft)
ADD_SUBDIRECTORY(src/storage)
ADD_SUBDIRECTORY(src)

Expand Down Expand Up @@ -200,4 +201,3 @@ ADD_CUSTOM_TARGET(cpplint echo '${LINT_FILES}' | xargs -n12 -P8
--linelength=120
--filter=-legal/copyright,-build/header_guard,-runtime/references
)

8 changes: 2 additions & 6 deletions cmake/gflags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ INCLUDE_GUARD()

INCLUDE(cmake/utils.cmake)

SET(MY_BUILD_TYPE ${CMAKE_BUILD_TYPE})
SET(CMAKE_BUILD_TYPE ${THIRD_PARTY_BUILD_TYPE})

FetchContent_DeclareGitHubWithMirror(gflags
gflags/gflags v2.2.2
SHA256=19713a36c9f32b33df59d1c79b4958434cb005b5b47dc5400a7a4b078111d9b5
Expand All @@ -22,6 +19,7 @@ FetchContent_MakeAvailableWithArgs(gflags
BUILD_gflags_LIB=ON
BUILD_gflags_nothreads_LIB=OFF
BUILD_TESTING=OFF
CMAKE_BUILD_TYPE=${THIRD_PARTY_BUILD_TYPE}
)

FIND_PACKAGE(Threads REQUIRED)
Expand All @@ -30,6 +28,4 @@ TARGET_LINK_LIBRARIES(gflags_static Threads::Threads)

SET(GFLAGS_INCLUDE_PATH ${CMAKE_CURRENT_BINARY_DIR}/_deps/gflags-build/include)
SET(GFLAGS_LIBRARY ${CMAKE_CURRENT_BINARY_DIR}/_deps/gflags-build/libgflags.a)
SET(GFLAGS_LIB ${CMAKE_CURRENT_BINARY_DIR}/_deps/gflags-build/libgflags.a)

SET(CMAKE_BUILD_TYPE ${MY_BUILD_TYPE})
SET(GFLAGS_LIB ${CMAKE_CURRENT_BINARY_DIR}/_deps/gflags-build/libgflags.a)
2 changes: 1 addition & 1 deletion cmake/glog.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ IF (CMAKE_BUILD_TYPE STREQUAL "Release")
SET(GLOG_LIB ${CMAKE_CURRENT_BINARY_DIR}/_deps/glog-build/libglog.a CACHE BOOL "" FORCE)
ELSEIF (CMAKE_BUILD_TYPE STREQUAL "Debug")
SET(GLOG_LIB ${CMAKE_CURRENT_BINARY_DIR}/_deps/glog-build/libglogd.a CACHE BOOL "" FORCE)
ENDIF()
ENDIF()
6 changes: 3 additions & 3 deletions cmake/leveldb.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
INCLUDE_GUARD()

FETCHCONTENT_DECLARE(
leveldb
GIT_REPOSITORY https://github.com/google/leveldb.git
GIT_TAG main
leveldb
GIT_REPOSITORY https://github.com/google/leveldb.git
GIT_TAG main
)
SET(LEVELDB_BUILD_TESTS OFF CACHE BOOL "" FORCE)
SET(LEVELDB_BUILD_BENCHMARKS OFF CACHE BOOL "" FORCE)
Expand Down
2 changes: 1 addition & 1 deletion cmake/modules/glog/Findgflags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ IF (gflags_SOURCE_DIR)

# add_library(gflags_static::gflags_static ALIAS gflags_static)
INSTALL(TARGETS gflags_static EXPORT glog-targets)
ENDIF ()
ENDIF ()
2 changes: 1 addition & 1 deletion cmake/protobuf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,4 @@ IF (NOT PROTOBUF_FOUND)
SET(PROTOBUF_PROTOC_EXECUTABLE ${extern_protobuf_PROTOC_EXECUTABLE}
CACHE FILEPATH "protobuf executable." FORCE)
PROMPT_PROTOBUF_LIB(extern_protobuf zlib)
ENDIF (NOT PROTOBUF_FOUND)
ENDIF (NOT PROTOBUF_FOUND)
2 changes: 1 addition & 1 deletion cmake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ FUNCTION(FetchContent_DeclareGitHubWithMirror dep repo tag hash)
https://github.com/${repo}/archive/${tag}.zip
${hash}
)
ENDFUNCTION()
ENDFUNCTION()
3 changes: 3 additions & 0 deletions pikiwidb.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ daemonize no
# port 0 is not permitted.
port 9221

# Braft relies on brpc to communicate via the default port number plus the port offset
raft-port-offset 10

# If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for incoming connections.
#
Expand Down
21 changes: 12 additions & 9 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ AUX_SOURCE_DIRECTORY(. PIKIWIDB_SRC)
ADD_EXECUTABLE(pikiwidb ${PIKIWIDB_SRC})
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

TARGET_INCLUDE_DIRECTORIES(pikiwidb PRIVATE
${PROJECT_SOURCE_DIR}/src
${PROJECT_SOURCE_DIR}/src/pstd
${PROJECT_SOURCE_DIR}/src/net
${PROJECT_SOURCE_DIR}/src/storage/include
${rocksdb_SOURCE_DIR}/
${rocksdb_SOURCE_DIR}/include
TARGET_INCLUDE_DIRECTORIES(pikiwidb
PRIVATE ${PROJECT_SOURCE_DIR}/src
PRIVATE ${PROJECT_SOURCE_DIR}/src/pstd
PRIVATE ${PROJECT_SOURCE_DIR}/src/net
PRIVATE ${PROJECT_SOURCE_DIR}/src/storage/include
PRIVATE ${rocksdb_SOURCE_DIR}/
PRIVATE ${rocksdb_SOURCE_DIR}/include
PRIVATE ${BRAFT_INCLUDE_DIR}
PRIVATE ${BRPC_INCLUDE_DIR}
PRIVATE ${GFLAGS_INCLUDE_PATH}
PRIVATE ${PROJECT_SOURCE_DIR}/src/praft
)

IF (CMAKE_SYSTEM_NAME MATCHES "Darwin")
Expand All @@ -34,5 +36,6 @@ ELSE ()
MESSAGE(FATAL_ERROR "only support linux or macOS")
ENDIF ()

TARGET_LINK_LIBRARIES(pikiwidb net; dl; fmt; storage; rocksdb; braft brpc ssl crypto zlib protobuf leveldb gflags "${MAC_LIBRARY}")
SET_TARGET_PROPERTIES(pikiwidb PROPERTIES LINKER_LANGUAGE CXX)
TARGET_LINK_LIBRARIES(pikiwidb net; dl; fmt; storage; rocksdb; pstd braft brpc ssl crypto zlib protobuf leveldb gflags rt crypto dl z praft "${MAC_LIBRARY}")

SET_TARGET_PROPERTIES(pikiwidb PROPERTIES LINKER_LANGUAGE CXX)
9 changes: 8 additions & 1 deletion src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const std::string kCmdNameDel = "del";
const std::string kCmdNameExists = "exists";
const std::string kCmdNamePExpire = "pexpire";

// raft cmd
const std::string kCmdNameRaftCluster = "raft.cluster";
const std::string kCmdNameRaftNode = "raft.node";

// string cmd
const std::string kCmdNameSet = "set";
const std::string kCmdNameGet = "get";
Expand Down Expand Up @@ -62,6 +66,7 @@ const std::string kCmdNameFlushdb = "flushdb";
const std::string kCmdNameFlushall = "flushall";
const std::string kCmdNameAuth = "auth";
const std::string kCmdNameSelect = "select";
const std::string kCmdNameInfo = "info";

// hash cmd
const std::string kCmdNameHSet = "hset";
Expand Down Expand Up @@ -119,6 +124,7 @@ enum CmdFlags {
kCmdFlagsProtected = (1 << 12), // Don't accept in scripts
kCmdFlagsModuleNoCluster = (1 << 13), // No cluster mode support
kCmdFlagsNoMulti = (1 << 14), // Cannot be pipelined
kCmdFlagsRaft = (1 << 15), // raft
};

enum AclCategory {
Expand All @@ -142,7 +148,8 @@ enum AclCategory {
kAclCategoryDangerous = (1 << 17),
kAclCategoryConnection = (1 << 18),
kAclCategoryTransaction = (1 << 19),
kAclCategoryScripting = (1 << 20)
kAclCategoryScripting = (1 << 20),
kAclCategoryRaft = (1 << 21),
};

/**
Expand Down
24 changes: 23 additions & 1 deletion src/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "pstd_string.h"
#include "slow_log.h"
#include "store.h"
#include "praft.h"

namespace pikiwidb {

Expand Down Expand Up @@ -130,6 +131,10 @@ void CmdRes::SetRes(CmdRes::CmdRet _ret, const std::string& content) {
case kInvalidCursor:
AppendStringRaw("-ERR invalid cursor");
break;
case kWrongLeader:
AppendStringRaw("-ERR wrong leader");
AppendStringRaw(content);
AppendStringRaw(CRLF);
default:
break;
}
Expand Down Expand Up @@ -268,6 +273,15 @@ int PClient::handlePacket(const char* start, int bytes) {
}
}

if (isJoinCmdTarget()) {
// Proccees the packet at one turn.
auto [len, is_disconnect] = PRAFT.ProcessClusterJoinCmdResponse(this, start, bytes);
if (is_disconnect) {
conn->ActiveClose();
}
return len;
}

auto parseRet = parser_.ParseRequest(ptr, end);
if (parseRet == PParseResult::kError) {
if (!parser_.IsInitialState()) {
Expand Down Expand Up @@ -410,7 +424,7 @@ PClient::PClient(TcpConnection* obj)

int PClient::HandlePackets(pikiwidb::TcpConnection* obj, const char* start, int size) {
int total = 0;

LOG(INFO) << start;
while (total < size) {
auto processed = handlePacket(start + total, size - total);
if (processed <= 0) {
Expand All @@ -437,6 +451,9 @@ void PClient::OnConnect() {
if (g_config.masterauth.empty()) {
SetAuth();
}
} else if (isJoinCmdTarget()) {
SetName("ClusterJoinCmdConnection");
PRAFT.SendNodeInfoRequest(this);
} else {
if (g_config.password.empty()) {
SetAuth();
Expand Down Expand Up @@ -509,6 +526,10 @@ bool PClient::isPeerMaster() const {
return repl_addr.GetIP() == PeerIP() && repl_addr.GetPort() == PeerPort();
}

bool PClient::isJoinCmdTarget() const {
return PRAFT.GetJoinCtx().GetPeerIp() == PeerIP() && PRAFT.GetJoinCtx().GetPort() == PeerPort();
}

int PClient::uniqueID() const {
if (auto c = getTcpConnection(); c) {
return c->GetUniqueId();
Expand Down Expand Up @@ -673,6 +694,7 @@ void PClient::FeedMonitors(const std::vector<std::string>& params) {
}
}
}

void PClient::SetKey(std::vector<std::string>& names) {
keys_ = std::move(names); // use std::move clear copy expense
}
Expand Down
3 changes: 3 additions & 0 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class CmdRes {
kErrOther,
KIncrByOverFlow,
kInvalidCursor,
kWrongLeader,
};

CmdRes() = default;
Expand Down Expand Up @@ -209,6 +210,8 @@ class PClient : public std::enable_shared_from_this<PClient>, public CmdRes {
bool isPeerMaster() const;
int uniqueID() const;

bool isJoinCmdTarget() const;

// TcpConnection's life is undetermined, so use weak ptr for safety.
std::weak_ptr<TcpConnection> tcp_connection_;

Expand Down
75 changes: 75 additions & 0 deletions src/cmd_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "cmd_admin.h"
#include "store.h"
#include "braft/raft.h"
#include "praft.h"

namespace pikiwidb {

Expand Down Expand Up @@ -78,4 +80,77 @@ void SelectCmd::DoCmd(PClient* client) {
client->SetRes(CmdRes::kOK);
}

InfoCmd::InfoCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsAdmin | kCmdFlagsReadonly, kAclCategoryAdmin) {}

bool InfoCmd::DoInitial(PClient* client) { return true; }

/*
* INFO raft
* Querying Node Information.
* Reply:
* raft_node_id:595100767
raft_state:up
raft_role:follower
raft_is_voting:yes
raft_leader_id:1733428433
raft_current_term:1
raft_num_nodes:2
raft_num_voting_nodes:2
raft_node1:id=1733428433,state=connected,voting=yes,addr=localhost,port=5001,last_conn_secs=5,conn_errors=0,conn_oks=1
*/
// @todo The info raft command is only supported for the time being
void InfoCmd::DoCmd(PClient* client) {
if (client->argv_.size() <= 1) {
return client->SetRes(CmdRes::kWrongNum, client->CmdName());
}

auto cmd = client->argv_[1];
if (!strcasecmp(cmd.c_str(), "RAFT")) {
if (client->argv_.size() != 2) {
return client->SetRes(CmdRes::kWrongNum, client->CmdName());
}

if (!PRAFT.IsInitialized()) {
return client->SetRes(CmdRes::kErrOther, "don't already cluster member");
}

auto node_status = PRAFT.GetNodeStatus();
if (node_status.state == braft::State::STATE_END) {
return client->SetRes(CmdRes::kErrOther, "Node is not initialized");
}

std::string message("");
message += "raft_group_id:" + PRAFT.GetGroupId() + "\r\n";
message += "raft_node_id:" + PRAFT.GetNodeId() + "\r\n";
if (braft::is_active_state(node_status.state)) {
message += "raft_state:up\r\n";
} else {
message += "raft_state:down\r\n";
}
message += "raft_role:" + std::string(braft::state2str(node_status.state)) + "\r\n";
// message += "raft_is_voting:" + node_status.is_voting + "\r\n";
message += "raft_leader_id:" + node_status.leader_id.to_string() + "\r\n";
message += "raft_current_term:" + std::to_string(node_status.term) + "\r\n";
// message += "raft_num_nodes:" + std::to_string(node_status.num_nodes) + "\r\n";
// message += "raft_num_voting_nodes:" + std::to_string(node_status.num_voting_nodes) + "\r\n";

if (PRAFT.IsLeader()) {
std::vector<braft::PeerId> peers;
auto status = PRAFT.GetListPeers(&peers);
if (!status.ok()) {
return client->SetRes(CmdRes::kErrOther, status.error_str());
}

for (int i = 0; i < peers.size(); i++) {
message += "raft_node" + std::to_string(i) + ":addr=" + butil::ip2str(peers[i].addr.ip).c_str() + ",port=" + std::to_string(peers[i].addr.port) + "\r\n";
}
}

client->AppendString(message);
} else {
client->SetRes(CmdRes::kErrOther, "ERR the cmd is not supported");
}
}

} // namespace pikiwidb
11 changes: 11 additions & 0 deletions src/cmd_admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,15 @@ class SelectCmd : public BaseCmd {
void DoCmd(PClient* client) override;
};

class InfoCmd : public BaseCmd {
public:
InfoCmd(const std::string& name, int16_t arity);

protected:
bool DoInitial(PClient* client) override;

private:
void DoCmd(PClient* client) override;
};

} // namespace pikiwidb
Loading
Loading