Skip to content

Commit 12e9196

Browse files
author
wuxianrong
committed
import braft
1 parent 7e12e9a commit 12e9196

24 files changed

+3040
-15
lines changed

CMakeLists.txt

Lines changed: 217 additions & 10 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
)
@@ -273,10 +274,10 @@ ExternalProject_Add(glog
273274
-DCMAKE_POLICY_VERSION_MINIMUM=3.5
274275
-DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX}
275276
-DCMAKE_BUILD_TYPE=${LIB_BUILD_TYPE}
276-
-DWITH_GFLAGS=ON
277+
-DWITH_GFLAGS=OFF
277278
-DBUILD_TESTING=OFF
278279
-DBUILD_SHARED_LIBS=OFF
279-
-DWITH_UNWIND=${LIBUNWIND_ON}
280+
-DWITH_UNWIND=OFF
280281
-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}
281282
BUILD_COMMAND
282283
make -j${CPU_CORE}
@@ -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
@@ -543,7 +544,7 @@ if(CMAKE_SYSTEM_NAME MATCHES "Linux")
543544
BUILD_ALWAYS
544545
1
545546
INSTALL_COMMAND
546-
make install
547+
make -j1 install
547548
)
548549

549550
set(JEMALLOC_LIBRARY ${INSTALL_LIBDIR}/libjemalloc.a)
@@ -595,9 +596,155 @@ else()
595596
endif()
596597

597598
set(PROTOBUF_INCLUDE_DIR ${INSTALL_INCLUDEDIR})
598-
set(PROTOBUF_LIBRARY ${INSTALL_LIBDIR}/${LIB_PROTOBUF})
599+
if(${OS_VERSION} MATCHES "Rocky" OR ${OS_VERSION} MATCHES "CentOS")
600+
set(PROTOBUF_LIBRARY ${INSTALL_LIBDIR_64}/${LIB_PROTOBUF})
601+
else()
602+
set(PROTOBUF_LIBRARY ${INSTALL_LIBDIR}/${LIB_PROTOBUF})
603+
endif()
599604
set(PROTOBUF_PROTOC ${STAGED_INSTALL_PREFIX}/bin/protoc)
600605

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

900+
# Generate protobuf files before compiling praft (which depends on them)
901+
set(PROTO_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/pika_inner_message.proto ${CMAKE_CURRENT_SOURCE_DIR}/src/rsync_service.proto)
902+
custom_protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_FILES})
903+
message("pika PROTO_SRCS = ${PROTO_SRCS}")
904+
message("pika PROTO_HDRS = ${PROTO_HDRS}")
905+
906+
# Create a custom target for generated proto files
907+
add_custom_target(pika_proto_gen
908+
DEPENDS ${PROTO_SRCS} ${PROTO_HDRS} protobuf
909+
COMMENT "Generating Pika protobuf files"
910+
)
911+
753912
add_subdirectory(src/pstd)
754913
add_subdirectory(src/net)
914+
add_subdirectory(src/praft) # praft 必须在 storage 之前,因为 storage 需要 binlog.pb.h
755915
add_subdirectory(src/storage)
756916
add_subdirectory(src/cache)
757917
if (USE_PIKA_TOOLS)
@@ -794,10 +954,7 @@ set(PIKA_BUILD_VERSION_CC ${CMAKE_BINARY_DIR}/pika_build_version.cc
794954
message("PIKA_BUILD_VERSION_CC : " ${PIKA_BUILD_VERSION_CC})
795955
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/build_version.cc.in ${PIKA_BUILD_VERSION_CC} @ONLY)
796956

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}")
957+
# PROTO_SRCS and PROTO_HDRS are already generated above
801958

802959
add_executable(${PROJECT_NAME}
803960
${DIR_SRCS}
@@ -821,6 +978,9 @@ add_dependencies(${PROJECT_NAME}
821978
zlib
822979
${LIBGPERF_NAME}
823980
${LIBJEMALLOC_NAME}
981+
leveldb
982+
brpc
983+
braft
824984
rocksdb
825985
protobuf
826986
pstd
@@ -839,10 +999,14 @@ target_include_directories(${PROJECT_NAME}
839999
target_link_libraries(${PROJECT_NAME}
8401000
cache
8411001
storage
1002+
praft
8421003
net
8431004
pstd
8441005
${GLOG_LIBRARY}
8451006
librocksdb.a
1007+
${BRAFT_LIBRARY}
1008+
${BRPC_LIBRARY}
1009+
${LEVELDB_LIBRARY}
8461010
${LIB_PROTOBUF}
8471011
${LIB_GFLAGS}
8481012
${LIB_FMT}
@@ -854,6 +1018,49 @@ target_link_libraries(${PROJECT_NAME}
8541018
${LIBUNWIND_LIBRARY}
8551019
${JEMALLOC_LIBRARY})
8561020

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