Skip to content

Commit 5697b32

Browse files
author
wuxianrong
committed
import braft
1 parent 7e12e9a commit 5697b32

File tree

23 files changed

+3014
-8
lines changed

23 files changed

+3014
-8
lines changed

CMakeLists.txt

Lines changed: 207 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ ExternalProject_Add(gflags
200200
-DGFLAGS_NAMESPACE=gflags
201201
-DBUILD_STATIC_LIBS=ON
202202
-DBUILD_SHARED_LIBS=OFF
203+
-DREGISTER_INSTALL_PREFIX=OFF
203204
BUILD_COMMAND
204205
make -j${CPU_CORE}
205206
)
@@ -457,9 +458,9 @@ set(LZ4_INCLUDE_DIR ${INSTALL_INCLUDEDIR})
457458
ExternalProject_Add(zlib
458459
DEPENDS
459460
URL
460-
https://github.com/madler/zlib/releases/download/v1.2.13/zlib-1.2.13.tar.gz
461+
https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz
461462
URL_HASH
462-
MD5=9b8aa094c4e5765dabf4da391f00d15c
463+
MD5=9855b6d802d7fe5b7bd5b196a2271655
463464
DOWNLOAD_NO_PROGRESS
464465
1
465466
UPDATE_COMMAND
@@ -598,6 +599,146 @@ set(PROTOBUF_INCLUDE_DIR ${INSTALL_INCLUDEDIR})
598599
set(PROTOBUF_LIBRARY ${INSTALL_LIBDIR}/${LIB_PROTOBUF})
599600
set(PROTOBUF_PROTOC ${STAGED_INSTALL_PREFIX}/bin/protoc)
600601

602+
ExternalProject_Add(leveldb
603+
DEPENDS
604+
snappy
605+
URL
606+
https://github.com/google/leveldb/archive/refs/tags/1.23.tar.gz
607+
URL_HASH
608+
MD5=afbde776fb8760312009963f09a586c7
609+
DOWNLOAD_NO_PROGRESS
610+
1
611+
UPDATE_COMMAND
612+
""
613+
LOG_CONFIGURE
614+
1
615+
LOG_BUILD
616+
1
617+
LOG_INSTALL
618+
1
619+
CMAKE_ARGS
620+
-DCMAKE_POLICY_VERSION_MINIMUM=3.5
621+
-DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX}
622+
-DCMAKE_BUILD_TYPE=${LIB_BUILD_TYPE}
623+
-DLEVELDB_BUILD_TESTS=OFF
624+
-DLEVELDB_BUILD_BENCHMARKS=OFF
625+
-DBUILD_SHARED_LIBS=OFF
626+
-DHAVE_SNAPPY=ON
627+
-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}
628+
-DCMAKE_CXX_FLAGS=-I${INSTALL_INCLUDEDIR}
629+
-DCMAKE_EXE_LINKER_FLAGS=-L${INSTALL_LIBDIR}
630+
BUILD_ALWAYS
631+
1
632+
BUILD_COMMAND
633+
make -j${CPU_CORE}
634+
)
635+
636+
if(${OS_VERSION} MATCHES "Rocky" OR ${OS_VERSION} MATCHES "CentOS")
637+
set(LEVELDB_LIBRARY ${INSTALL_LIBDIR_64}/libleveldb.a)
638+
else()
639+
set(LEVELDB_LIBRARY ${INSTALL_LIBDIR}/libleveldb.a)
640+
endif()
641+
set(LEVELDB_INCLUDE_DIR ${INSTALL_INCLUDEDIR})
642+
643+
ExternalProject_Add(brpc
644+
DEPENDS
645+
gflags
646+
protobuf
647+
leveldb
648+
glog
649+
snappy
650+
zlib
651+
URL
652+
https://github.com/apache/brpc/archive/refs/tags/1.6.0.tar.gz
653+
URL_HASH
654+
MD5=0d37cea25bd006e89806f461ef7e39ba
655+
DOWNLOAD_NO_PROGRESS
656+
1
657+
UPDATE_COMMAND
658+
""
659+
LOG_CONFIGURE
660+
1
661+
LOG_BUILD
662+
1
663+
LOG_INSTALL
664+
1
665+
CMAKE_ARGS
666+
-DCMAKE_POLICY_VERSION_MINIMUM=3.5
667+
-DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX}
668+
-DCMAKE_BUILD_TYPE=${LIB_BUILD_TYPE}
669+
-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}
670+
-DWITH_GLOG=ON
671+
-DWITH_SNAPPY=ON
672+
-DBUILD_SHARED_LIBS=OFF
673+
-DDOWNLOAD_GTEST=OFF
674+
BUILD_ALWAYS
675+
1
676+
BUILD_COMMAND
677+
make -j${CPU_CORE}
678+
)
679+
680+
if(${OS_VERSION} MATCHES "Rocky" OR ${OS_VERSION} MATCHES "CentOS")
681+
set(BRPC_LIBRARY ${INSTALL_LIBDIR_64}/libbrpc.a)
682+
else()
683+
set(BRPC_LIBRARY ${INSTALL_LIBDIR}/libbrpc.a)
684+
endif()
685+
set(BRPC_INCLUDE_DIR ${INSTALL_INCLUDEDIR})
686+
687+
ExternalProject_Add(braft
688+
DEPENDS
689+
gflags
690+
protobuf
691+
leveldb
692+
brpc
693+
glog
694+
snappy
695+
zlib
696+
URL
697+
https://github.com/baidu/braft/archive/refs/tags/v1.1.2.tar.gz
698+
URL_HASH
699+
MD5=f1d0307cf45449bbec9b64ca81b5f808
700+
DOWNLOAD_NO_PROGRESS
701+
1
702+
UPDATE_COMMAND
703+
""
704+
LOG_CONFIGURE
705+
1
706+
LOG_BUILD
707+
1
708+
LOG_INSTALL
709+
1
710+
CMAKE_ARGS
711+
-DCMAKE_POLICY_VERSION_MINIMUM=3.5
712+
-DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX}
713+
-DCMAKE_BUILD_TYPE=${LIB_BUILD_TYPE}
714+
-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}
715+
-DWITH_GLOG=ON
716+
-DBUILD_SHARED_LIBS=OFF
717+
-DBUILD_UNIT_TESTS=OFF
718+
-DDOWNLOAD_GTEST=OFF
719+
BUILD_ALWAYS
720+
1
721+
BUILD_COMMAND
722+
make -j${CPU_CORE} braft-static
723+
INSTALL_COMMAND
724+
${CMAKE_COMMAND} -E copy_directory <BINARY_DIR>/output/include/braft ${INSTALL_INCLUDEDIR}/braft
725+
COMMAND ${CMAKE_COMMAND} -E copy <BINARY_DIR>/output/lib/libbraft.a ${INSTALL_LIBDIR}/libbraft.a
726+
)
727+
728+
# For CentOS/Rocky, also copy to lib64
729+
if(${OS_VERSION} MATCHES "Rocky" OR ${OS_VERSION} MATCHES "CentOS")
730+
ExternalProject_Add_Step(braft copy_to_lib64
731+
COMMAND ${CMAKE_COMMAND} -E make_directory ${INSTALL_LIBDIR_64}
732+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${INSTALL_LIBDIR}/libbraft.a ${INSTALL_LIBDIR_64}/libbraft.a
733+
DEPENDEES install
734+
COMMENT "Copying braft to lib64 for CentOS/Rocky"
735+
)
736+
set(BRAFT_LIBRARY ${INSTALL_LIBDIR_64}/libbraft.a)
737+
else()
738+
set(BRAFT_LIBRARY ${INSTALL_LIBDIR}/libbraft.a)
739+
endif()
740+
set(BRAFT_INCLUDE_DIR ${INSTALL_INCLUDEDIR})
741+
601742
ExternalProject_Add(rocksdb
602743
DEPENDS
603744
gflags
@@ -750,8 +891,21 @@ endif()
750891
set(ROCKSDB_INCLUDE_DIR ${INSTALL_INCLUDEDIR})
751892
set(ROCKSDB_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/${EP_BASE_SUFFIX}/Source/rocksdb)
752893

894+
# Generate protobuf files before compiling praft (which depends on them)
895+
set(PROTO_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/pika_inner_message.proto ${CMAKE_CURRENT_SOURCE_DIR}/src/rsync_service.proto)
896+
custom_protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_FILES})
897+
message("pika PROTO_SRCS = ${PROTO_SRCS}")
898+
message("pika PROTO_HDRS = ${PROTO_HDRS}")
899+
900+
# Create a custom target for generated proto files
901+
add_custom_target(pika_proto_gen
902+
DEPENDS ${PROTO_SRCS} ${PROTO_HDRS}
903+
COMMENT "Generating Pika protobuf files"
904+
)
905+
753906
add_subdirectory(src/pstd)
754907
add_subdirectory(src/net)
908+
add_subdirectory(src/praft) # praft 必须在 storage 之前,因为 storage 需要 binlog.pb.h
755909
add_subdirectory(src/storage)
756910
add_subdirectory(src/cache)
757911
if (USE_PIKA_TOOLS)
@@ -794,10 +948,7 @@ set(PIKA_BUILD_VERSION_CC ${CMAKE_BINARY_DIR}/pika_build_version.cc
794948
message("PIKA_BUILD_VERSION_CC : " ${PIKA_BUILD_VERSION_CC})
795949
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/build_version.cc.in ${PIKA_BUILD_VERSION_CC} @ONLY)
796950

797-
set(PROTO_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/pika_inner_message.proto ${CMAKE_CURRENT_SOURCE_DIR}/src/rsync_service.proto)
798-
custom_protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_FILES})
799-
message("pika PROTO_SRCS = ${PROTO_SRCS}")
800-
message("pika PROTO_HDRS = ${PROTO_HDRS}")
951+
# PROTO_SRCS and PROTO_HDRS are already generated above
801952

802953
add_executable(${PROJECT_NAME}
803954
${DIR_SRCS}
@@ -821,6 +972,9 @@ add_dependencies(${PROJECT_NAME}
821972
zlib
822973
${LIBGPERF_NAME}
823974
${LIBJEMALLOC_NAME}
975+
leveldb
976+
brpc
977+
braft
824978
rocksdb
825979
protobuf
826980
pstd
@@ -839,10 +993,14 @@ target_include_directories(${PROJECT_NAME}
839993
target_link_libraries(${PROJECT_NAME}
840994
cache
841995
storage
996+
praft
842997
net
843998
pstd
844999
${GLOG_LIBRARY}
8451000
librocksdb.a
1001+
${BRAFT_LIBRARY}
1002+
${BRPC_LIBRARY}
1003+
${LEVELDB_LIBRARY}
8461004
${LIB_PROTOBUF}
8471005
${LIB_GFLAGS}
8481006
${LIB_FMT}
@@ -854,6 +1012,49 @@ target_link_libraries(${PROJECT_NAME}
8541012
${LIBUNWIND_LIBRARY}
8551013
${JEMALLOC_LIBRARY})
8561014

1015+
# Add platform-specific libraries for brpc and braft
1016+
if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
1017+
# macOS frameworks
1018+
target_link_libraries(${PROJECT_NAME}
1019+
"-framework CoreFoundation"
1020+
"-framework CoreGraphics"
1021+
"-framework CoreData"
1022+
"-framework CoreText"
1023+
"-framework Security"
1024+
"-framework Foundation"
1025+
"-framework ApplicationServices"
1026+
"-framework SystemConfiguration"
1027+
"-framework AppKit"
1028+
"-Wl,-undefined,dynamic_lookup" # Allow undefined symbols (gperftools optional)
1029+
)
1030+
find_library(OPENSSL_CRYPTO_LIBRARY NAMES crypto libcrypto)
1031+
find_library(OPENSSL_SSL_LIBRARY NAMES ssl libssl)
1032+
if(OPENSSL_CRYPTO_LIBRARY)
1033+
target_link_libraries(${PROJECT_NAME} ${OPENSSL_CRYPTO_LIBRARY})
1034+
endif()
1035+
if(OPENSSL_SSL_LIBRARY)
1036+
target_link_libraries(${PROJECT_NAME} ${OPENSSL_SSL_LIBRARY})
1037+
endif()
1038+
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
1039+
# Linux system libraries for brpc and braft
1040+
target_link_libraries(${PROJECT_NAME}
1041+
rt # Real-time extensions
1042+
dl # Dynamic linking
1043+
)
1044+
# Find and link OpenSSL for Linux
1045+
find_package(OpenSSL)
1046+
if(OPENSSL_FOUND)
1047+
target_link_libraries(${PROJECT_NAME} OpenSSL::SSL OpenSSL::Crypto)
1048+
else()
1049+
# Fallback: try to find libraries directly
1050+
find_library(OPENSSL_CRYPTO_LIBRARY NAMES crypto libcrypto)
1051+
find_library(OPENSSL_SSL_LIBRARY NAMES ssl libssl)
1052+
if(OPENSSL_CRYPTO_LIBRARY AND OPENSSL_SSL_LIBRARY)
1053+
target_link_libraries(${PROJECT_NAME} ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY})
1054+
endif()
1055+
endif()
1056+
endif()
1057+
8571058
option(USE_SSL "Enable SSL support" OFF)
8581059
add_custom_target(
8591060
clang-tidy

conf/pika.conf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,3 +632,35 @@ cache-lfu-decay-time: 1
632632
# which serves for the scenario of codis-pika cluster reelection
633633
# You'd better [DO NOT MODIFY IT UNLESS YOU KNOW WHAT YOU ARE DOING]
634634
internal-used-unfinished-full-sync :
635+
636+
###################
637+
## Raft Configuration
638+
###################
639+
640+
# Enable Raft consensus protocol for distributed consensus
641+
# When enabled, Pika will use Raft to ensure data consistency across nodes
642+
# Default value is no
643+
raft-enabled : no
644+
645+
# Raft group identifier
646+
# This is used to identify the Raft cluster
647+
# All nodes in the same Raft cluster should have the same group-id
648+
raft-group-id : pika_raft_group
649+
650+
# Initial Raft peers (comma separated, e.g., 127.0.0.1:12221,127.0.0.1:12222)
651+
# Format: ip:port,ip:port,ip:port
652+
# The port should be: pika_port + 3000 (e.g., if pika port is 9221, raft port is 12221)
653+
# Leave empty if initializing a single-node cluster
654+
raft-peers :
655+
656+
# Raft election timeout in milliseconds
657+
# This is the time to wait before starting a new election if no heartbeat is received
658+
# A larger value reduces the chance of unnecessary elections but increases failover time
659+
# Default value is 1000ms (1 second)
660+
raft-election-timeout-ms : 1000
661+
662+
# Raft snapshot interval in seconds
663+
# This determines how often Raft takes snapshots of the state machine
664+
# Snapshots are used to compact the log and speed up node recovery
665+
# Default value is 3600 seconds (1 hour)
666+
raft-snapshot-interval-s : 3600

include/pika_command.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ const std::string kCmdNameLastSave = "lastsave";
6767
const std::string kCmdNameCache = "cache";
6868
const std::string kCmdNameClearCache = "clearcache";
6969

70+
// Raft commands
71+
const std::string kCmdNameRaftCluster = "raft.cluster";
72+
const std::string kCmdNameRaftNode = "raft.node";
73+
const std::string kCmdNameRaftConfig = "raft.config";
74+
7075
// Migrate slot
7176
const std::string kCmdNameSlotsMgrtSlot = "slotsmgrtslot";
7277
const std::string kCmdNameSlotsMgrtTagSlot = "slotsmgrttagslot";

include/pika_conf.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,29 @@ class PikaConf : public pstd::BaseConf {
883883
int cache_maxmemory_policy() { return cache_maxmemory_policy_; }
884884
int cache_maxmemory_samples() { return cache_maxmemory_samples_; }
885885
int cache_lfu_decay_time() { return cache_lfu_decay_time_; }
886+
887+
// Raft configuration getters
888+
bool raft_enabled() {
889+
std::shared_lock l(rwlock_);
890+
return raft_enabled_;
891+
}
892+
std::string raft_group_id() {
893+
std::shared_lock l(rwlock_);
894+
return raft_group_id_;
895+
}
896+
std::string raft_peers() {
897+
std::shared_lock l(rwlock_);
898+
return raft_peers_;
899+
}
900+
int raft_election_timeout_ms() {
901+
std::shared_lock l(rwlock_);
902+
return raft_election_timeout_ms_;
903+
}
904+
int raft_snapshot_interval_s() {
905+
std::shared_lock l(rwlock_);
906+
return raft_snapshot_interval_s_;
907+
}
908+
886909
int Load();
887910
int ConfigRewrite();
888911
int ConfigRewriteSlaveOf();
@@ -1065,6 +1088,13 @@ class PikaConf : public pstd::BaseConf {
10651088

10661089
//Internal used metrics Persisted by pika.conf
10671090
std::unordered_set<std::string> internal_used_unfinished_full_sync_;
1091+
1092+
// Raft configuration
1093+
bool raft_enabled_ = false;
1094+
std::string raft_group_id_;
1095+
std::string raft_peers_;
1096+
int raft_election_timeout_ms_ = 1000;
1097+
int raft_snapshot_interval_s_ = 3600;
10681098
};
10691099

10701100
#endif

0 commit comments

Comments
 (0)