Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
231 changes: 222 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ ExternalProject_Add(gflags
-DGFLAGS_NAMESPACE=gflags
-DBUILD_STATIC_LIBS=ON
-DBUILD_SHARED_LIBS=OFF
-DREGISTER_INSTALL_PREFIX=OFF
BUILD_COMMAND
make -j${CPU_CORE}
)
Expand Down Expand Up @@ -273,10 +274,10 @@ ExternalProject_Add(glog
-DCMAKE_POLICY_VERSION_MINIMUM=3.5
-DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX}
-DCMAKE_BUILD_TYPE=${LIB_BUILD_TYPE}
-DWITH_GFLAGS=ON
-DWITH_GFLAGS=OFF
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The glog library is being configured with -DWITH_GFLAGS=OFF (changed from ON). Since gflags is a dependency and is already being built, this may cause compatibility issues. Consider verifying that glog without gflags integration is intentional and doesn't break existing logging configurations that may depend on gflags.

Suggested change
-DWITH_GFLAGS=OFF
-DWITH_GFLAGS=ON

Copilot uses AI. Check for mistakes.
-DBUILD_TESTING=OFF
-DBUILD_SHARED_LIBS=OFF
-DWITH_UNWIND=${LIBUNWIND_ON}
-DWITH_UNWIND=OFF
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The glog library is being configured with -DWITH_UNWIND=OFF (changed from ${LIBUNWIND_ON}). This disables stack trace support in glog, which can make debugging crashes more difficult. Consider verifying that disabling libunwind support is intentional and acceptable for production use.

Suggested change
-DWITH_UNWIND=OFF
-DWITH_UNWIND=ON

Copilot uses AI. Check for mistakes.
-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}
BUILD_COMMAND
make -j${CPU_CORE}
Expand Down Expand Up @@ -457,9 +458,9 @@ set(LZ4_INCLUDE_DIR ${INSTALL_INCLUDEDIR})
ExternalProject_Add(zlib
DEPENDS
URL
https://github.com/madler/zlib/releases/download/v1.2.13/zlib-1.2.13.tar.gz
https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The zlib version is being upgraded from 1.2.13 to 1.3.1. While version upgrades are generally good, ensure this has been tested thoroughly as zlib is a critical compression library used throughout the system. The corresponding MD5 hash has been updated, which is correct.

Copilot uses AI. Check for mistakes.
URL_HASH
MD5=9b8aa094c4e5765dabf4da391f00d15c
MD5=9855b6d802d7fe5b7bd5b196a2271655
DOWNLOAD_NO_PROGRESS
1
UPDATE_COMMAND
Expand Down Expand Up @@ -571,6 +572,8 @@ ExternalProject_Add(protobuf
1
LOG_INSTALL
1
LOG_OUTPUT_ON_FAILURE
ON
SOURCE_SUBDIR
cmake
CMAKE_ARGS
Expand All @@ -595,9 +598,159 @@ else()
endif()

set(PROTOBUF_INCLUDE_DIR ${INSTALL_INCLUDEDIR})
set(PROTOBUF_LIBRARY ${INSTALL_LIBDIR}/${LIB_PROTOBUF})
if(${OS_VERSION} MATCHES "Rocky" OR ${OS_VERSION} MATCHES "CentOS")
set(PROTOBUF_LIBRARY ${INSTALL_LIBDIR_64}/${LIB_PROTOBUF})
else()
set(PROTOBUF_LIBRARY ${INSTALL_LIBDIR}/${LIB_PROTOBUF})
endif()
set(PROTOBUF_PROTOC ${STAGED_INSTALL_PREFIX}/bin/protoc)

ExternalProject_Add(leveldb
DEPENDS
snappy
URL
https://github.com/google/leveldb/archive/refs/tags/1.23.tar.gz
URL_HASH
MD5=afbde776fb8760312009963f09a586c7
DOWNLOAD_NO_PROGRESS
1
UPDATE_COMMAND
""
LOG_CONFIGURE
1
LOG_BUILD
1
LOG_INSTALL
1
CMAKE_ARGS
-DCMAKE_POLICY_VERSION_MINIMUM=3.5
-DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX}
-DCMAKE_BUILD_TYPE=${LIB_BUILD_TYPE}
-DLEVELDB_BUILD_TESTS=OFF
-DLEVELDB_BUILD_BENCHMARKS=OFF
-DBUILD_SHARED_LIBS=OFF
-DHAVE_SNAPPY=ON
-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}
-DCMAKE_CXX_FLAGS=-I${INSTALL_INCLUDEDIR}
-DCMAKE_EXE_LINKER_FLAGS=-L${INSTALL_LIBDIR}\ -L${INSTALL_LIBDIR_64}
-DCMAKE_SHARED_LINKER_FLAGS=-L${INSTALL_LIBDIR}\ -L${INSTALL_LIBDIR_64}
-DCMAKE_MODULE_LINKER_FLAGS=-L${INSTALL_LIBDIR}\ -L${INSTALL_LIBDIR_64}
BUILD_ALWAYS
1
BUILD_COMMAND
make -j${CPU_CORE}
)

if(${OS_VERSION} MATCHES "Rocky" OR ${OS_VERSION} MATCHES "CentOS")
set(LEVELDB_LIBRARY ${INSTALL_LIBDIR_64}/libleveldb.a)
else()
set(LEVELDB_LIBRARY ${INSTALL_LIBDIR}/libleveldb.a)
endif()
set(LEVELDB_INCLUDE_DIR ${INSTALL_INCLUDEDIR})

ExternalProject_Add(brpc
DEPENDS
gflags
protobuf
leveldb
glog
snappy
zlib
URL
https://github.com/apache/brpc/archive/refs/tags/1.6.0.tar.gz
URL_HASH
MD5=0d37cea25bd006e89806f461ef7e39ba
DOWNLOAD_NO_PROGRESS
1
UPDATE_COMMAND
""
LOG_CONFIGURE
1
LOG_BUILD
1
LOG_INSTALL
1
LOG_OUTPUT_ON_FAILURE
ON
CMAKE_ARGS
-DCMAKE_POLICY_VERSION_MINIMUM=3.5
-DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX}
-DCMAKE_BUILD_TYPE=${LIB_BUILD_TYPE}
-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}
-DWITH_GLOG=ON
-DWITH_SNAPPY=ON
-DBUILD_SHARED_LIBS=OFF
-DDOWNLOAD_GTEST=OFF
BUILD_ALWAYS
1
BUILD_COMMAND
make -j${CPU_CORE}
)

if(${OS_VERSION} MATCHES "Rocky" OR ${OS_VERSION} MATCHES "CentOS")
set(BRPC_LIBRARY ${INSTALL_LIBDIR_64}/libbrpc.a)
else()
set(BRPC_LIBRARY ${INSTALL_LIBDIR}/libbrpc.a)
endif()
set(BRPC_INCLUDE_DIR ${INSTALL_INCLUDEDIR})

ExternalProject_Add(braft
DEPENDS
gflags
protobuf
leveldb
brpc
glog
snappy
zlib
URL
https://github.com/baidu/braft/archive/refs/tags/v1.1.2.tar.gz
URL_HASH
MD5=f1d0307cf45449bbec9b64ca81b5f808
DOWNLOAD_NO_PROGRESS
1
UPDATE_COMMAND
""
LOG_CONFIGURE
1
LOG_BUILD
1
LOG_INSTALL
1
LOG_OUTPUT_ON_FAILURE
ON
CMAKE_ARGS
-DCMAKE_POLICY_VERSION_MINIMUM=3.5
-DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX}
-DCMAKE_BUILD_TYPE=${LIB_BUILD_TYPE}
-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}
-DWITH_GLOG=ON
-DBUILD_SHARED_LIBS=OFF
-DBUILD_UNIT_TESTS=OFF
-DDOWNLOAD_GTEST=OFF
BUILD_ALWAYS
1
BUILD_COMMAND
make -j${CPU_CORE} braft-static
INSTALL_COMMAND
${CMAKE_COMMAND} -E copy_directory <BINARY_DIR>/output/include/braft ${INSTALL_INCLUDEDIR}/braft
COMMAND ${CMAKE_COMMAND} -E copy <BINARY_DIR>/output/lib/libbraft.a ${INSTALL_LIBDIR}/libbraft.a
)

# For CentOS/Rocky, also copy to lib64
if(${OS_VERSION} MATCHES "Rocky" OR ${OS_VERSION} MATCHES "CentOS")
ExternalProject_Add_Step(braft copy_to_lib64
COMMAND ${CMAKE_COMMAND} -E make_directory ${INSTALL_LIBDIR_64}
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${INSTALL_LIBDIR}/libbraft.a ${INSTALL_LIBDIR_64}/libbraft.a
DEPENDEES install
COMMENT "Copying braft to lib64 for CentOS/Rocky"
)
set(BRAFT_LIBRARY ${INSTALL_LIBDIR_64}/libbraft.a)
else()
set(BRAFT_LIBRARY ${INSTALL_LIBDIR}/libbraft.a)
endif()
set(BRAFT_INCLUDE_DIR ${INSTALL_INCLUDEDIR})

ExternalProject_Add(rocksdb
DEPENDS
gflags
Expand Down Expand Up @@ -750,8 +903,21 @@ endif()
set(ROCKSDB_INCLUDE_DIR ${INSTALL_INCLUDEDIR})
set(ROCKSDB_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/${EP_BASE_SUFFIX}/Source/rocksdb)

# Generate protobuf files before compiling praft (which depends on them)
set(PROTO_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/pika_inner_message.proto ${CMAKE_CURRENT_SOURCE_DIR}/src/rsync_service.proto)
custom_protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_FILES})
message("pika PROTO_SRCS = ${PROTO_SRCS}")
message("pika PROTO_HDRS = ${PROTO_HDRS}")

# Create a custom target for generated proto files
add_custom_target(pika_proto_gen
DEPENDS ${PROTO_SRCS} ${PROTO_HDRS} protobuf
COMMENT "Generating Pika protobuf files"
)

add_subdirectory(src/pstd)
add_subdirectory(src/net)
add_subdirectory(src/praft) # praft 必须在 storage 之前,因为 storage 需要 binlog.pb.h
add_subdirectory(src/storage)
add_subdirectory(src/cache)
if (USE_PIKA_TOOLS)
Expand Down Expand Up @@ -794,10 +960,7 @@ set(PIKA_BUILD_VERSION_CC ${CMAKE_BINARY_DIR}/pika_build_version.cc
message("PIKA_BUILD_VERSION_CC : " ${PIKA_BUILD_VERSION_CC})
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/build_version.cc.in ${PIKA_BUILD_VERSION_CC} @ONLY)

set(PROTO_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/pika_inner_message.proto ${CMAKE_CURRENT_SOURCE_DIR}/src/rsync_service.proto)
custom_protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_FILES})
message("pika PROTO_SRCS = ${PROTO_SRCS}")
message("pika PROTO_HDRS = ${PROTO_HDRS}")
# PROTO_SRCS and PROTO_HDRS are already generated above

add_executable(${PROJECT_NAME}
${DIR_SRCS}
Expand All @@ -821,6 +984,9 @@ add_dependencies(${PROJECT_NAME}
zlib
${LIBGPERF_NAME}
${LIBJEMALLOC_NAME}
leveldb
brpc
braft
rocksdb
protobuf
pstd
Expand All @@ -839,10 +1005,14 @@ target_include_directories(${PROJECT_NAME}
target_link_libraries(${PROJECT_NAME}
cache
storage
praft
net
pstd
${GLOG_LIBRARY}
librocksdb.a
${BRAFT_LIBRARY}
${BRPC_LIBRARY}
${LEVELDB_LIBRARY}
${LIB_PROTOBUF}
${LIB_GFLAGS}
${LIB_FMT}
Expand All @@ -854,6 +1024,49 @@ target_link_libraries(${PROJECT_NAME}
${LIBUNWIND_LIBRARY}
${JEMALLOC_LIBRARY})

# Add platform-specific libraries for brpc and braft
if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
# macOS frameworks
target_link_libraries(${PROJECT_NAME}
"-framework CoreFoundation"
"-framework CoreGraphics"
"-framework CoreData"
"-framework CoreText"
"-framework Security"
"-framework Foundation"
"-framework ApplicationServices"
"-framework SystemConfiguration"
"-framework AppKit"
"-Wl,-undefined,dynamic_lookup" # Allow undefined symbols (gperftools optional)
)
find_library(OPENSSL_CRYPTO_LIBRARY NAMES crypto libcrypto)
find_library(OPENSSL_SSL_LIBRARY NAMES ssl libssl)
if(OPENSSL_CRYPTO_LIBRARY)
target_link_libraries(${PROJECT_NAME} ${OPENSSL_CRYPTO_LIBRARY})
endif()
if(OPENSSL_SSL_LIBRARY)
target_link_libraries(${PROJECT_NAME} ${OPENSSL_SSL_LIBRARY})
endif()
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
# Linux system libraries for brpc and braft
target_link_libraries(${PROJECT_NAME}
rt # Real-time extensions
dl # Dynamic linking
)
# Find and link OpenSSL for Linux
find_package(OpenSSL)
if(OPENSSL_FOUND)
target_link_libraries(${PROJECT_NAME} OpenSSL::SSL OpenSSL::Crypto)
else()
# Fallback: try to find libraries directly
find_library(OPENSSL_CRYPTO_LIBRARY NAMES crypto libcrypto)
find_library(OPENSSL_SSL_LIBRARY NAMES ssl libssl)
if(OPENSSL_CRYPTO_LIBRARY AND OPENSSL_SSL_LIBRARY)
target_link_libraries(${PROJECT_NAME} ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY})
endif()
endif()
endif()

option(USE_SSL "Enable SSL support" OFF)
add_custom_target(
clang-tidy
Expand Down
26 changes: 26 additions & 0 deletions conf/pika.conf
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,29 @@ cache-lfu-decay-time: 1
# which serves for the scenario of codis-pika cluster reelection
# You'd better [DO NOT MODIFY IT UNLESS YOU KNOW WHAT YOU ARE DOING]
internal-used-unfinished-full-sync :

###################
## Raft Configuration
###################

# Enable Raft consensus protocol for distributed consensus
# When enabled, Pika will use Raft to ensure data consistency across nodes
# Default value is no
raft-enabled : no

# Raft group identifier
# This is used to identify the Raft cluster
# All nodes in the same Raft cluster should have the same group-id
raft-group-id : pika_raft_group

# Raft election timeout in milliseconds
# This is the time to wait before starting a new election if no heartbeat is received
# A larger value reduces the chance of unnecessary elections but increases failover time
# Default value is 1000ms (1 second)
raft-election-timeout-ms : 1000

# Raft snapshot interval in seconds
# This determines how often Raft takes snapshots of the state machine
# Snapshots are used to compact the log and speed up node recovery
# Default value is 3600 seconds (1 hour)
raft-snapshot-interval-s : 3600
2 changes: 2 additions & 0 deletions include/pika_bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class BitSetCmd : public Cmd {
std::string key_;
int64_t bit_offset_;
int64_t on_;
int32_t bit_val_ = 0; // For async mode
rocksdb::Status s_;
void Clear() override {
key_ = "";
Expand Down Expand Up @@ -169,6 +170,7 @@ class BitOpCmd : public Cmd {
rocksdb::Status s_;
std::vector<std::string> src_keys_;
storage::BitOpType op_;
int64_t result_length_ = 0; // For async mode
void Clear() override {
dest_key_ = "";
src_keys_.clear();
Expand Down
9 changes: 9 additions & 0 deletions include/pika_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ const std::string kCmdNameLastSave = "lastsave";
const std::string kCmdNameCache = "cache";
const std::string kCmdNameClearCache = "clearcache";

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

// Migrate slot
const std::string kCmdNameSlotsMgrtSlot = "slotsmgrtslot";
const std::string kCmdNameSlotsMgrtTagSlot = "slotsmgrttagslot";
Expand Down Expand Up @@ -617,6 +621,11 @@ class Cmd : public std::enable_shared_from_this<Cmd> {
uint32_t aclCategory_ = 0;
bool cache_missed_in_rtc_{false};

// Raft async mode helper functions
bool IsRaftLeader() const;
bool IsRaftEnabled() const;
bool ShouldUseAsyncMode() const;

private:
virtual void DoInitial() = 0;
virtual void Clear(){};
Expand Down
Loading
Loading