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:cache layer for pika #2133

Closed
wants to merge 84 commits into from
Closed
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
df37f11
add dependences
brother-jin Oct 19, 2023
924df2c
fix: add_ci (#229)
Mixficsol Oct 19, 2023
7c9bf66
formate code style && fix centos build bug
brother-jin Oct 19, 2023
d501b7c
fix centos build bug
brother-jin Oct 19, 2023
6cc9c43
format code style
brother-jin Oct 19, 2023
9069b22
format
brother-jin Oct 19, 2023
dd0f6ae
rename classname
brother-jin Oct 19, 2023
7bf9dd8
format code
brother-jin Oct 21, 2023
43a1f89
use cache define
brother-jin Oct 23, 2023
4ddc9a7
Merge pull request #234 from chejinge/feature/cache_string
AlexStocks Oct 23, 2023
c8e2463
upgrade rediscache version to v0.1.3
AlexStocks Oct 23, 2023
ddfd105
change file name
AlexStocks Oct 23, 2023
f2adc4e
using DEFER
AlexStocks Oct 24, 2023
e64921f
Merge pull request #235 from pikiwidb/redis-cache-0.1.3
AlexStocks Oct 24, 2023
d93061e
fix github ci failure
AlexStocks Oct 25, 2023
acdd7d3
Merge pull request #239 from pikiwidb/pika_cache_build_failure
AlexStocks Oct 25, 2023
4051d84
add_string_cache【暂时不修改格式及代码风格,会在最后一个PR统一做修改】 (#237)
chejinge Oct 30, 2023
c7fe09b
add_slave_cache (#243)
Mixficsol Oct 30, 2023
b4f6909
fix:cache string (#247)
chejinge Nov 1, 2023
771ac73
add_bitmap_cache (#255)
chejinge Nov 6, 2023
dd878c7
add string cache cmd (#252)
Mixficsol Nov 6, 2023
16477de
add list cache cmd (#250)
Mixficsol Nov 6, 2023
4fbb251
cache set command (#256)
chejinge Nov 6, 2023
b658ec9
add hash cache cmd (#251)
Mixficsol Nov 6, 2023
484391a
cache zset command (#257)
chejinge Nov 6, 2023
11ccccd
Update pika_zset.h
chejinge Nov 6, 2023
2d8696f
add Pika Cache Status (#259)
Mixficsol Nov 7, 2023
158a40a
cache index
brother-jin Nov 7, 2023
dcd3619
git revert
brother-jin Nov 7, 2023
9d82d2a
【fix】cache Asynchronous transmission (#265)
Mixficsol Nov 8, 2023
9cbb52f
add defer (#269)
Mixficsol Nov 9, 2023
fe658f8
cache index (#260)
chejinge Nov 9, 2023
576c259
Support for multiple databases (#275)
Mixficsol Nov 13, 2023
d8efef3
smart pointer and lock granularity && set don't update cache (#277)
chejinge Nov 13, 2023
2a42803
fix_lock (#278)
Mixficsol Nov 13, 2023
12e8139
fix_command_bug (#282)
chejinge Nov 13, 2023
e8adba4
Fix del mget (#291)
chejinge Nov 14, 2023
3ce3862
bugfix:ttl&&exits command (#300)
chejinge Nov 15, 2023
7f671fa
fix: hash/list/set/zset test (#285)
Mixficsol Nov 15, 2023
01f0315
fix bitpos strlen msetnx
brother-jin Nov 15, 2023
d858ba9
fix bitpos strlen msetnx
brother-jin Nov 15, 2023
0fe689e
Update pika_kv.cc
chejinge Nov 15, 2023
3c75d58
Update pika_command.cc
chejinge Nov 15, 2023
629001b
fix: hash/zset test cache (#302)
Mixficsol Nov 15, 2023
cdd9f44
Merge pull request #305 from chejinge/bug_fix_strlen
AlexStocks Nov 15, 2023
4f18bb7
fix Master-slave test
Nov 16, 2023
1871d09
Merge pull request #311 from Mixficsol/cache_test
Mixficsol Nov 16, 2023
3fbb4db
code format (#316)
Mixficsol Nov 17, 2023
5aca7b5
fix:cache code style
brother-jin Nov 19, 2023
612f184
fix:cache code style
brother-jin Nov 19, 2023
29307cf
fix
brother-jin Nov 21, 2023
bb32bc6
Supports multi-DB asynchronous data transfer (#317)
Mixficsol Nov 21, 2023
ca3fbbc
Merge branch 'pika_cache' into fix_cache_codestyle
chejinge Nov 21, 2023
f396d7f
Merge pull request #318 from chejinge/fix_cache_codestyle
AlexStocks Nov 21, 2023
7c1ec0b
Merge branch 'unstable' into pika_cache
chejinge Nov 21, 2023
a3bec4d
fix conflict and change name
brother-jin Nov 21, 2023
4741b14
fix conflict and change name
brother-jin Nov 21, 2023
d9d645e
fix conflict and change name
brother-jin Nov 21, 2023
9d25e7b
Update pika_admin.cc
chejinge Nov 21, 2023
62bdeed
Update pika_admin.cc
chejinge Nov 21, 2023
0c5fb1d
Merge pull request #319 from chejinge/fix_code_style_of_cache
AlexStocks Nov 21, 2023
91c7b00
Merge branch 'unstable' into pika_cache
chejinge Nov 21, 2023
23d281a
Initialization variable (#321)
Mixficsol Nov 22, 2023
5dbdda3
Merge branch 'unstable' into pika_cache
chejinge Nov 22, 2023
17d414c
fix_flushdb
Nov 23, 2023
812ae11
fix: Cache flushdb (#322)
Mixficsol Nov 23, 2023
a0618c5
Fix cache mget (#323)
chejinge Nov 23, 2023
e200d84
Merge branch 'pika_cache' of github.com:pikiwidb/pika into cache
Nov 23, 2023
7993074
fix_lock_and_multi_caches
brother-jin Nov 23, 2023
c38a740
Merge pull request #324 from chejinge/fix_lock_and_multi_caches
AlexStocks Nov 23, 2023
f6c7163
fix_mem_leak
brother-jin Nov 24, 2023
55edfd5
fix_mem_leak
brother-jin Nov 24, 2023
c1fcb13
fix
brother-jin Nov 27, 2023
853ba64
Merge pull request #326 from chejinge/fix_mem_leak
AlexStocks Nov 27, 2023
d3b580b
add cacheclean command
Nov 23, 2023
a0dfe9f
Merge pull request #325 from Mixficsol/cache
AlexStocks Nov 27, 2023
6991712
fix coreedump
brother-jin Nov 28, 2023
b1e49ab
Merge pull request #328 from chejinge/fix_mem_leak
AlexStocks Nov 28, 2023
5c28442
Fix:mem leak (#329)
chejinge Nov 29, 2023
5d5f685
Update pika_admin.cc
Mixficsol Nov 29, 2023
88dca95
Update pika.conf
Mixficsol Nov 29, 2023
0f9f3ce
Update pika.conf
Mixficsol Nov 29, 2023
33c102e
fix_license
brother-jin Dec 1, 2023
1bdd728
Merge pull request #333 from chejinge/license
AlexStocks Dec 1, 2023
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
4 changes: 2 additions & 2 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ name: "CodeQL"

on:
push:
branches: [ "unstable", "3.5.1" ]
branches: [ "unstable", "3.5.1", "*" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "unstable", "3.5.1" ]
branches: [ "unstable", "3.5.1", "*" ]
schedule:
- cron: '25 19 * * 6'

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/codis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ name: Codis

on:
push:
branches: [ "unstable", "3.5.1" ]
branches: [ "unstable", "3.5.1", "*" ]
pull_request:
branches: [ "unstable", "3.5.1" ]
branches: [ "unstable", "3.5.1", "*" ]

jobs:

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/operator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ name: Operator

on:
push:
branches: [ "unstable", "3.5.1" ]
branches: [ "unstable", "3.5.1", "*" ]
pull_request:
branches: [ "unstable", "3.5.1" ]
branches: [ "unstable", "3.5.1", "*" ]

jobs:

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pika.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Pika

on:
push:
branches: [ "unstable", "3.5.1" ]
branches: [ "unstable", "3.5.1", "*" ]
pull_request:
branches: [ "unstable", "3.5.1" ]
branches: [ "unstable", "3.5.1", "*" ]

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tools_go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ name: Tools_go_build

on:
push:
branches: [ "unstable", "3.5.1" ]
branches: [ "unstable", "3.5.1", "*" ]
paths:
- 'tools/**'
pull_request:
branches: [ "unstable", "3.5.1" ]
branches: [ "unstable", "3.5.1", "*" ]
paths:
- 'tools/**'

Expand Down
43 changes: 41 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ elseif(${BUILD_TYPE} STREQUAL RELWITHDEBINFO)
else()
set(LIB_BUILD_TYPE RELEASE)
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")


endif()

if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
Expand Down Expand Up @@ -626,6 +628,35 @@ ExternalProject_Add(rocksdb
make -j${CPU_CORE}
)

ExternalProject_Add(rediscache
URL
https://github.com/pikiwidb/rediscache/archive/refs/tags/v1.0.5.tar.gz
URL_HASH
MD5=99e4d0bde20811a6058a6aa482c18711
DOWNLOAD_NO_PROGRESS
1
UPDATE_COMMAND
""
LOG_BUILD
1
BUILD_IN_SOURCE
1
SOURCE_SUBDIR
""
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX}
-DCMAKE_INSTALL_INCLUDEDIR=${INSTALL_INCLUDEDIR}
-DCMAKE_INSTALL_LIBDIR=${INSTALL_LIBDIR}
-DCMAKE_BUILD_TYPE=${LIB_BUILD_TYPE}
BUILD_ALWAYS
1
BUILD_COMMAND
make -j${CPU_CORE}
)
set(REDISCACHE_INCLUDE_DIR ${INSTALL_INCLUDEDIR})
set(REDISCACHE_LIBRARY ${INSTALL_LIBDIR}/librediscache.a)


option(USE_PIKA_TOOLS "compile pika-tools" OFF)
if (USE_PIKA_TOOLS)
ExternalProject_Add(hiredis
Expand Down Expand Up @@ -697,6 +728,7 @@ set(ROCKSDB_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/${EP_BASE_SUFFIX}/Source/rock
add_subdirectory(src/pstd)
add_subdirectory(src/net)
add_subdirectory(src/storage)
add_subdirectory(src/cache)
if (USE_PIKA_TOOLS)
add_subdirectory(tools)
endif()
Expand Down Expand Up @@ -731,7 +763,9 @@ message("pika GIT_DATE = ${PIKA_GIT_DATE}")
message("pika GIT_TAG = ${PIKA_GIT_TAG}")
message("pika BUILD_DATE = ${PIKA_BUILD_DATE}")

set(PIKA_BUILD_VERSION_CC ${CMAKE_BINARY_DIR}/pika_build_version.cc)
set(PIKA_BUILD_VERSION_CC ${CMAKE_BINARY_DIR}/pika_build_version.cc
src/pika_cache_load_thread.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)

Expand Down Expand Up @@ -766,7 +800,10 @@ add_dependencies(${PROJECT_NAME}
protobuf
pstd
net
storage)
rediscache
storage
cache
)

target_include_directories(${PROJECT_NAME}
PUBLIC ${CMAKE_CURRENT_BINARY_DIR}
Expand All @@ -775,6 +812,7 @@ target_include_directories(${PROJECT_NAME}
)

target_link_libraries(${PROJECT_NAME}
cache
storage
net
pstd
Expand All @@ -787,6 +825,7 @@ target_link_libraries(${PROJECT_NAME}
libzstd.a
liblz4.a
libz.a
librediscache.a
${LIBUNWIND_LIBRARY}
${JEMALLOC_LIBRARY})

Expand Down
34 changes: 34 additions & 0 deletions conf/pika.conf
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,37 @@ max-rsync-parallel-num : 4
# The synchronization mode of Pika primary/secondary replication is determined by ReplicationID. ReplicationID in one replication_cluster are the same
# replication-id :

###################
## Cache Settings
###################
# cache-num
cache-num : 16

# cache-model 0:cache_none 1:cache_read
cache-model : 1
# cache-type: string, set, zset, list, hash, bit
cache-type: string, set, zset, list, hash
# cache-start-direction 0:cache first ${cache-items-per-key} items; -1:cache last ${cache-items-per-key} items
cache-start-direction : 0
cache-items-per-key : 512

# cache-maxmemory, unit bytes
cache-maxmemory : 10737418240

# cache-maxmemory-policy
# 0: volatile-lru -> Evict using approximated LRU among the keys with an expire set.
# 1: allkeys-lru -> Evict any key using approximated LRU.
# 2: volatile-lfu -> Evict using approximated LFU among the keys with an expire set.
# 3: allkeys-lfu -> Evict any key using approximated LFU.
# 4: volatile-random -> Remove a random key among the ones with an expire set.
# 5: allkeys-random -> Remove a random key, any key.
# 6: volatile-ttl -> Remove the key with the nearest expire time (minor TTL)
# 7: noeviction -> Don't evict anything, just return an error on write operations.
cache-maxmemory-policy : 1

# cache-maxmemory-samples
cache-maxmemory-samples: 5

# cache-lfu-decay-time
cache-lfu-decay-time: 1

32 changes: 30 additions & 2 deletions include/pika_admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ class FlushallCmd : public Cmd {
public:
FlushallCmd(const std::string& name, int arity, uint16_t flag) : Cmd(name, arity, flag) {}
void Do(std::shared_ptr<Slot> slot = nullptr) override;
void DoThroughDB(std::shared_ptr<Slot> slot = nullptr) override;
void DoUpdateCache(std::shared_ptr<Slot> slot = nullptr) override;
void Split(std::shared_ptr<Slot> slot, const HintKeys& hint_keys) override{};
void Merge() override{};
Cmd* Clone() override { return new FlushallCmd(*this); }
Expand All @@ -169,6 +171,8 @@ class FlushdbCmd : public Cmd {
public:
FlushdbCmd(const std::string& name, int arity, uint16_t flag) : Cmd(name, arity, flag) {}
void Do(std::shared_ptr<Slot> slot = nullptr) override;
void DoThroughDB(std::shared_ptr<Slot> slot = nullptr) override;
void DoUpdateCache(std::shared_ptr<Slot> slot = nullptr) override;
void Split(std::shared_ptr<Slot> slot, const HintKeys& hint_keys) override{};
void Merge() override{};
Cmd* Clone() override { return new FlushdbCmd(*this); }
Expand Down Expand Up @@ -211,7 +215,8 @@ class InfoCmd : public Cmd {
kInfo,
kInfoAll,
kInfoDebug,
kInfoCommandStats
kInfoCommandStats,
kInfoCache
};

InfoCmd(const std::string& name, int arity, uint16_t flag) : Cmd(name, arity, flag) {}
Expand Down Expand Up @@ -239,6 +244,7 @@ class InfoCmd : public Cmd {
const static std::string kRocksDBSection;
const static std::string kDebugSection;
const static std::string kCommandStatsSection;
const static std::string kCacheSection;

void DoInitial() override;
void Clear() override {
Expand All @@ -259,6 +265,9 @@ class InfoCmd : public Cmd {
void InfoRocksDB(std::string& info);
void InfoDebug(std::string& info);
void InfoCommandStats(std::string& info);
void InfoCache(std::string& info, std::shared_ptr<Slot> slot = nullptr);

std::string CacheStatusToString(int status);
};

class ShutdownCmd : public Cmd {
Expand All @@ -285,7 +294,7 @@ class ConfigCmd : public Cmd {
std::vector<std::string> config_args_v_;
void DoInitial() override;
void ConfigGet(std::string& ret);
void ConfigSet(std::string& ret);
void ConfigSet(std::string& ret, std::shared_ptr<Slot> slot);
void ConfigRewrite(std::string& ret);
void ConfigResetstat(std::string& ret);
void ConfigRewriteReplicationID(std::string& ret);
Expand Down Expand Up @@ -487,6 +496,25 @@ class DisableWalCmd : public Cmd {
void DoInitial() override;
};

class CacheCmd : public Cmd {
public:
enum CacheCondition {kCLEAR_DB, kCLEAR_HITRATIO, kDEL_KEYS, kRANDOM_KEY};
CacheCmd(const std::string& name, int arity, uint16_t flag) : Cmd(name, arity, flag) {}
void Do(std::shared_ptr<Slot> slot = nullptr) override;
void Split(std::shared_ptr<Slot> slot, const HintKeys& hint_keys) override{};
void Merge() override{};
Cmd* Clone() override { return new CacheCmd(*this); }

private:
CacheCondition condition_;
std::vector<std::string> keys_;
rocksdb::Status s_;
void DoInitial() override;
void Clear() override {
keys_.clear();
}
};

#ifdef WITH_COMMAND_DOCS
class CommandCmd : public Cmd {
public:
Expand Down
18 changes: 18 additions & 0 deletions include/pika_bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ class BitGetCmd : public Cmd {
return res;
}
void Do(std::shared_ptr<Slot> slot = nullptr) override;
void ReadCache(std::shared_ptr<Slot> slot = nullptr) override;
void DoUpdateCache(std::shared_ptr<Slot> slot = nullptr) override;
void DoThroughDB(std::shared_ptr<Slot> slot = nullptr) override;
void Split(std::shared_ptr<Slot> slot, const HintKeys& hint_keys) override {};
void Merge() override {};
Cmd* Clone() override { return new BitGetCmd(*this); }

private:
std::string key_;
int64_t bit_offset_ = -1;
rocksdb::Status s_;
void Clear() override {
key_ = "";
bit_offset_ = -1;
Expand All @@ -47,6 +51,8 @@ class BitSetCmd : public Cmd {
return res;
}
void Do(std::shared_ptr<Slot> slot = nullptr) override;
void DoUpdateCache(std::shared_ptr<Slot> slot = nullptr) override;
void DoThroughDB(std::shared_ptr<Slot> slot = nullptr) override;
void Split(std::shared_ptr<Slot> slot, const HintKeys& hint_keys) override {};
void Merge() override {};
Cmd* Clone() override { return new BitSetCmd(*this); }
Expand All @@ -55,6 +61,7 @@ class BitSetCmd : public Cmd {
std::string key_;
int64_t bit_offset_;
int64_t on_;
rocksdb::Status s_;
void Clear() override {
key_ = "";
bit_offset_ = -1;
Expand All @@ -72,6 +79,9 @@ class BitCountCmd : public Cmd {
return res;
}
void Do(std::shared_ptr<Slot> slot = nullptr) override;
void ReadCache(std::shared_ptr<Slot> slot = nullptr) override;
void DoUpdateCache(std::shared_ptr<Slot> slot = nullptr) override;
void DoThroughDB(std::shared_ptr<Slot> slot = nullptr) override;
void Split(std::shared_ptr<Slot> slot, const HintKeys& hint_keys) override {};
void Merge() override {};
Cmd* Clone() override { return new BitCountCmd(*this); }
Expand All @@ -81,6 +91,7 @@ class BitCountCmd : public Cmd {
bool count_all_;
int64_t start_offset_;
int64_t end_offset_;
rocksdb::Status s_;
void Clear() override {
key_ = "";
count_all_ = false;
Expand All @@ -99,6 +110,9 @@ class BitPosCmd : public Cmd {
return res;
}
void Do(std::shared_ptr<Slot> slot = nullptr) override;
void ReadCache(std::shared_ptr<Slot> slot = nullptr) override;
void DoUpdateCache(std::shared_ptr<Slot> slot = nullptr) override;
void DoThroughDB(std::shared_ptr<Slot> slot = nullptr) override;
void Split(std::shared_ptr<Slot> slot, const HintKeys& hint_keys) override {};
void Merge() override {};
Cmd* Clone() override { return new BitPosCmd(*this); }
Expand All @@ -110,6 +124,7 @@ class BitPosCmd : public Cmd {
int64_t bit_val_;
int64_t start_offset_;
int64_t end_offset_;
rocksdb::Status s_;
void Clear() override {
key_ = "";
pos_all_ = false;
Expand Down Expand Up @@ -139,13 +154,16 @@ class BitOpCmd : public Cmd {
return {dest_key_};
}
void Do(std::shared_ptr<Slot> slot = nullptr) override;
void DoUpdateCache(std::shared_ptr<Slot> slot = nullptr) override;
void DoThroughDB(std::shared_ptr<Slot> slot = nullptr) override;
void Split(std::shared_ptr<Slot> slot, const HintKeys& hint_keys) override{};
void Merge() override{};
Cmd* Clone() override { return new BitOpCmd(*this); }
void DoBinlog(const std::shared_ptr<SyncMasterSlot>& slot) override;

private:
std::string dest_key_;
rocksdb::Status s_;
std::vector<std::string> src_keys_;
storage::BitOpType op_;
void Clear() override {
Expand Down
Loading