-
Notifications
You must be signed in to change notification settings - Fork 261
MemLens Database Specific Changes #170
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
Changes from 14 commits
3f963bb
804dcd1
b67fa88
4404af5
33c16f2
7c35fe8
ccade23
c625f78
e1ce45d
676a5d3
491b254
4adc82f
7eb9fff
d8cbe16
71975f2
35ed786
b9b3363
0d02a14
8348689
0a85b15
ccb268d
4cd5b88
a642ef8
08203e3
94d2a72
a759dda
a0a136f
32b5758
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| build --cxxopt='-std=c++17' --copt=-O3 --jobs=40 | ||
| build --cxxopt='-std=c++17' --copt="-pg" --linkopt="-pg" --strip=never --jobs=40 | ||
| #build --action_env=PYTHON_BIN_PATH="/usr/bin/python3.10" | ||
| #build --action_env=PYTHON_LIB_PATH="/usr/include/python3.10" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,3 +13,9 @@ sdk_validator/venv | |
| __pycache__ | ||
| MODULE.* | ||
| apache_release | ||
| *.out.* | ||
| *.data.* | ||
| *.pb.* | ||
| .cache/ | ||
| resdb/ | ||
| 100*_db/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,11 +47,14 @@ cc_library( | |
| name = "leveldb", | ||
| srcs = ["leveldb.cpp"], | ||
| hdrs = ["leveldb.h"], | ||
| linkopts = ["-pg","-g","-ggdb"], # Enable profiling during linking | ||
|
||
| deps = [ | ||
| ":storage", | ||
| "//chain/storage/proto:kv_cc_proto", | ||
| "//chain/storage/proto:leveldb_config_cc_proto", | ||
| "//common:comm", | ||
| "//common/lru:lru_cache", | ||
| "//platform/statistic:stats", | ||
| "//third_party:leveldb", | ||
| ], | ||
| ) | ||
|
|
@@ -64,4 +67,4 @@ cc_test( | |
| ":memory_db", | ||
| "//common/test:test_main", | ||
| ], | ||
| ) | ||
| ) | ||
cjcchen marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,8 +20,10 @@ | |
| #include "chain/storage/leveldb.h" | ||
|
|
||
| #include <glog/logging.h> | ||
| #include <unistd.h> | ||
|
|
||
| #include "chain/storage/proto/kv.pb.h" | ||
| #include "leveldb/options.h" | ||
|
|
||
| namespace resdb { | ||
| namespace storage { | ||
|
|
@@ -50,6 +52,14 @@ ResLevelDB::ResLevelDB(std::optional<LevelDBInfo> config) { | |
| path = (*config).path(); | ||
| } | ||
| } | ||
| if ((*config).enable_block_cache()) { | ||
| LRUCache<std::string, std::string>* block_cache = | ||
|
||
| new LRUCache<std::string, std::string>(1000); | ||
| block_cache_ = | ||
cjcchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| std::unique_ptr<LRUCache<std::string, std::string>>(block_cache); | ||
| LOG(ERROR) << "initialized block cache" << std::endl; | ||
| } | ||
| global_stats_ = Stats::GetGlobalStats(); | ||
| CreateDB(path); | ||
| } | ||
|
|
||
|
|
@@ -74,15 +84,22 @@ ResLevelDB::~ResLevelDB() { | |
| if (db_) { | ||
| db_.reset(); | ||
| } | ||
| if (block_cache_) { | ||
| block_cache_->Flush(); | ||
| } | ||
| } | ||
|
|
||
| int ResLevelDB::SetValue(const std::string& key, const std::string& value) { | ||
| if (block_cache_) { | ||
| block_cache_->Put(key, value); | ||
| } | ||
| batch_.Put(key, value); | ||
|
|
||
| if (batch_.ApproximateSize() >= write_batch_size_) { | ||
| leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch_); | ||
| if (status.ok()) { | ||
| batch_.Clear(); | ||
| GetMetrics(); | ||
| return 0; | ||
| } else { | ||
| LOG(ERROR) << "flush buffer fail:" << status.ToString(); | ||
|
|
@@ -94,7 +111,17 @@ int ResLevelDB::SetValue(const std::string& key, const std::string& value) { | |
|
|
||
| std::string ResLevelDB::GetValue(const std::string& key) { | ||
| std::string value = ""; | ||
| std::string cached_result = ""; | ||
|
||
| if (block_cache_) { | ||
| std::string cached_result = block_cache_->Get(key); | ||
| } | ||
| if (cached_result != "") { | ||
| LOG(ERROR) << "Cache Hit for key: " << key << cached_result; | ||
| GetMetrics(); | ||
| return cached_result; | ||
| } | ||
| leveldb::Status status = db_->Get(leveldb::ReadOptions(), key, &value); | ||
| GetMetrics(); | ||
| if (status.ok()) { | ||
| return value; | ||
| } else { | ||
|
|
@@ -134,6 +161,19 @@ std::string ResLevelDB::GetRange(const std::string& min_key, | |
| return values; | ||
| } | ||
|
|
||
| void ResLevelDB::GetMetrics() { | ||
| if (!block_cache_) { | ||
| return; | ||
| } | ||
| std::string stats; | ||
| std::string approximate_size; | ||
| db_->GetProperty("leveldb.stats", &stats); | ||
| db_->GetProperty("leveldb.approximate-memory-usage", &approximate_size); | ||
| LOG(ERROR) << "LevelDB Stats" << stats << " : " << approximate_size << "\n"; | ||
| global_stats_->SetStorageEngineMetrics(block_cache_->GetCacheHitRatio(), | ||
| stats, approximate_size); | ||
| } | ||
|
|
||
| bool ResLevelDB::Flush() { | ||
| leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch_); | ||
| if (status.ok()) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| # | ||
|
|
||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| cc_library( | ||
| name = "lru_cache", | ||
| srcs = ["lru_cache.cpp"], | ||
| hdrs = ["lru_cache.h"], | ||
| linkopts = ["-pg","-g","-ggdb"], # Enable profiling during linking | ||
| copts = ["-pg"], | ||
| ) |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,101 @@ | ||||||||||
| #include "lru_cache.h" | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add some UTs. |
||||||||||
|
|
||||||||||
| #include <algorithm> | ||||||||||
|
|
||||||||||
| namespace resdb { | ||||||||||
|
|
||||||||||
| template <typename KeyType, typename ValueType> | ||||||||||
| LRUCache<KeyType, ValueType>::LRUCache(int capacity) { | ||||||||||
| m_ = capacity; | ||||||||||
| cache_hits_ = 0; | ||||||||||
| cache_misses_ = 0; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| template <typename KeyType, typename ValueType> | ||||||||||
| LRUCache<KeyType, ValueType>::~LRUCache() { | ||||||||||
| um_.clear(); | ||||||||||
| dq_.clear(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| template <typename KeyType, typename ValueType> | ||||||||||
| ValueType LRUCache<KeyType, ValueType>::Get(KeyType key) { | ||||||||||
| if (!um_.count(key)) { | ||||||||||
| cache_misses_++; | ||||||||||
| return ValueType(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| auto it = std::find(dq_.begin(), dq_.end(), key); | ||||||||||
|
||||||||||
| dq_.erase(it); | ||||||||||
|
||||||||||
| dq_.erase(it); | |
| if ( it != dq_.end()){ | |
| dq_.erase(it); | |
| } |
cjcchen marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #include <deque> | ||
| #include <unordered_map> | ||
|
|
||
| using namespace std; | ||
|
|
||
| namespace resdb { | ||
|
|
||
| template <typename KeyType, typename ValueType> | ||
| class LRUCache { | ||
| public: | ||
| // Constructor to initialize the cache with a given capacity | ||
| LRUCache(int capacity); | ||
|
|
||
| // Destructor | ||
| ~LRUCache(); | ||
|
|
||
| // Get the value of the key if present in the cache | ||
| ValueType Get(KeyType key); | ||
|
|
||
| // Insert or update the key-value pair | ||
| void Put(KeyType key, ValueType value); | ||
|
|
||
| // Method to change the cache capacity | ||
| void SetCapacity(int new_capacity); | ||
|
|
||
| // Method to flush the cache (clear all entries) | ||
| void Flush(); | ||
|
|
||
| // Method to get the cache hit count | ||
| int GetCacheHits() const; | ||
|
|
||
| // Method to get the cache miss count | ||
| int GetCacheMisses() const; | ||
|
|
||
| // Method to get the cache Hit Ratio | ||
| double GetCacheHitRatio() const; | ||
|
|
||
| private: | ||
| int m_; // Cache capacity | ||
| int cache_hits_; // Cache hits count | ||
| int cache_misses_; // Cache misses count | ||
|
|
||
| deque<KeyType> dq_; // To maintain most and least recently used items | ||
|
||
| unordered_map<KeyType, ValueType> um_; // Key-value map | ||
| }; | ||
| } // namespace resdb | ||
|
Uh oh!
There was an error while loading. Please reload this page.