Skip to content

Commit 1ad27c6

Browse files
committed
[Backport 2.4][#8037][DocDB] - Refactor memory management for tablets into a separate class
Summary: Original commit: D11320 / e67b207 Refactors block cache initialization/management, log cache garbage collection, and memstore limit management into its own class. In the process, creates log cache garbage collection and memstore management thread for the master tablet. T8037 Refactor Memstore background task T8037 Refactor all block cache initialization for ts_tablet_manager T8037 Memory flush refactor to Memory Manager, passing test T8037 Refactor sys_catalog to use memory manager T8037 Remove refactored functions from docdb_rocksdb_util T8037 adds comments and lint changes Depends on D11522 Test Plan: Jenkins: rebase: 2.4 Jenkins: hot Depends on D11522 ybd --cxx-test tserver_ts_tablet_manager-test ybd --cxx-test master_master-test ybd --cxx-test integration-tests_flush-test Reviewers: sergei, rsami, bogdan Reviewed By: bogdan Subscribers: ybase, jenkins-bot Differential Revision: https://phabricator.dev.yugabyte.com/D11577
1 parent f8686b4 commit 1ad27c6

11 files changed

+499
-331
lines changed

src/yb/docdb/docdb_rocksdb_util.cc

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -118,31 +118,6 @@ DEFINE_int32(priority_thread_pool_size, -1,
118118
"If -1 and max_background_compactions is specified - use max_background_compactions. "
119119
"If -1 and max_background_compactions is not specified - use sqrt(num_cpus).");
120120

121-
namespace {
122-
constexpr int kDbCacheSizeUsePercentage = -1;
123-
constexpr int kDbCacheSizeCacheDisabled = -2;
124-
constexpr int kDbCacheSizeUseDefault = -3;
125-
}
126-
127-
DEFINE_bool(enable_block_based_table_cache_gc, false,
128-
"Set to true to enable block based table garbage collector.");
129-
130-
DEFINE_int64(db_block_cache_size_bytes, kDbCacheSizeUsePercentage,
131-
"Size of RocksDB block cache (in bytes). "
132-
"This defaults to -1 for system auto-generated default, which would use "
133-
"FLAGS_db_block_cache_size_percentage to select a percentage of the total "
134-
"memory as the default size for the shared block cache. Value of -2 disables "
135-
"block cache.");
136-
137-
DEFINE_int32(db_block_cache_size_percentage, kDbCacheSizeUseDefault,
138-
"Default percentage of total available memory to use as block cache size, if not "
139-
"asking for a raw number, through FLAGS_db_block_cache_size_bytes. "
140-
"Defaults to -3 (use default percentage as defined by master or tserver).");
141-
142-
DEFINE_int32(db_block_cache_num_shard_bits, 4,
143-
"Number of bits to use for sharding the block cache (defaults to 4 bits)");
144-
TAG_FLAG(db_block_cache_num_shard_bits, advanced);
145-
146121
using std::shared_ptr;
147122
using std::string;
148123
using std::unique_ptr;
@@ -743,77 +718,5 @@ Status ForceRocksDBCompact(rocksdb::DB* db) {
743718
return Status::OK();
744719
}
745720

746-
namespace {
747-
748-
class LRUCacheGC : public GarbageCollector {
749-
public:
750-
explicit LRUCacheGC(std::shared_ptr<rocksdb::Cache> cache) : cache_(std::move(cache)) {}
751-
752-
void CollectGarbage(size_t required) {
753-
if (!FLAGS_enable_block_based_table_cache_gc) {
754-
return;
755-
}
756-
757-
auto evicted = cache_->Evict(required);
758-
LOG(INFO) << "Evicted from table cache: " << HumanReadableNumBytes::ToString(evicted)
759-
<< ", new usage: " << HumanReadableNumBytes::ToString(cache_->GetUsage())
760-
<< ", required: " << HumanReadableNumBytes::ToString(required);
761-
}
762-
763-
virtual ~LRUCacheGC() = default;
764-
765-
private:
766-
std::shared_ptr<rocksdb::Cache> cache_;
767-
};
768-
769-
int64_t GetTargetBlockCacheSize(const int32_t default_block_cache_size_percentage) {
770-
int32_t target_block_cache_size_percentage =
771-
(FLAGS_db_block_cache_size_percentage == kDbCacheSizeUseDefault) ?
772-
default_block_cache_size_percentage : FLAGS_db_block_cache_size_percentage;
773-
774-
int64_t target_block_cache_size_bytes = FLAGS_db_block_cache_size_bytes;
775-
// Auto-compute size of block cache if asked to.
776-
if (target_block_cache_size_bytes == kDbCacheSizeUsePercentage) {
777-
// Check some bounds.
778-
CHECK(target_block_cache_size_percentage > 0 && target_block_cache_size_percentage <= 100)
779-
<< Substitute(
780-
"Flag tablet_block_cache_size_percentage must be between 0 and 100. Current value: "
781-
"$0",
782-
target_block_cache_size_percentage);
783-
784-
const int64_t total_ram_avail = MemTracker::GetRootTracker()->limit();
785-
target_block_cache_size_bytes = total_ram_avail * target_block_cache_size_percentage / 100;
786-
}
787-
return target_block_cache_size_bytes;
788-
}
789-
790-
} // namespace
791-
792-
std::shared_ptr<MemTracker> InitBlockCacheMemTracker(
793-
const int32_t default_block_cache_size_percentage,
794-
const std::shared_ptr<MemTracker>& mem_tracker) {
795-
int64_t block_cache_size_bytes = GetTargetBlockCacheSize(default_block_cache_size_percentage);
796-
797-
return MemTracker::FindOrCreateTracker(block_cache_size_bytes, "BlockBasedTable", mem_tracker);
798-
}
799-
800-
std::shared_ptr<GarbageCollector> InitBlockCache(
801-
const scoped_refptr<MetricEntity>& metrics,
802-
const int32_t default_block_cache_size_percentage,
803-
MemTracker* block_based_table_mem_tracker,
804-
tablet::TabletOptions* options) {
805-
std::shared_ptr<GarbageCollector> block_based_table_gc;
806-
int64_t block_cache_size_bytes = GetTargetBlockCacheSize(default_block_cache_size_percentage);
807-
808-
if (block_cache_size_bytes != kDbCacheSizeCacheDisabled) {
809-
options->block_cache = rocksdb::NewLRUCache(block_cache_size_bytes,
810-
FLAGS_db_block_cache_num_shard_bits);
811-
options->block_cache->SetMetrics(metrics);
812-
block_based_table_gc = std::make_shared<LRUCacheGC>(options->block_cache);
813-
block_based_table_mem_tracker->AddGarbageCollector(block_based_table_gc);
814-
}
815-
return block_based_table_gc;
816-
}
817-
818721
} // namespace docdb
819722
} // namespace yb

src/yb/docdb/docdb_rocksdb_util.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,6 @@ std::unique_ptr<IntentAwareIterator> CreateIntentAwareIterator(
106106
// Request RocksDB compaction and wait until it completes.
107107
CHECKED_STATUS ForceRocksDBCompact(rocksdb::DB* db);
108108

109-
std::shared_ptr<MemTracker> InitBlockCacheMemTracker(
110-
const int32_t default_block_cache_size_percentage,
111-
const std::shared_ptr<MemTracker>& mem_tracker);
112-
113-
std::shared_ptr<GarbageCollector> InitBlockCache(
114-
const scoped_refptr<MetricEntity>& metrics,
115-
const int32_t default_block_cache_size_percentage,
116-
MemTracker* block_based_table_mem_tracker,
117-
tablet::TabletOptions* options);
118-
119109
// Initialize the RocksDB 'options'.
120110
// The 'statistics' object provided by the caller will be used by RocksDB to maintain the stats for
121111
// the tablet.

src/yb/integration-tests/flush-test.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include "yb/tserver/mini_tablet_server.h"
2828
#include "yb/tserver/tablet_server.h"
29+
#include "yb/tserver/tablet_memory_manager.h"
2930
#include "yb/tserver/ts_tablet_manager.h"
3031

3132
using namespace std::literals;
@@ -70,7 +71,7 @@ Result<TabletId> GetTabletIdFromSstFilename(const std::string& filename) {
7071
}
7172
}
7273

73-
class TabletManagerListener : public tserver::TsTabletManagerListener {
74+
class TabletManagerListener : public tserver::TabletMemoryManagerListenerIf {
7475
public:
7576
void StartedFlush(const TabletId& tablet_id) override {
7677
std::lock_guard<std::mutex> lock(mutex_);
@@ -126,7 +127,8 @@ class FlushITest : public YBTest {
126127
}
127128

128129
void SetupCluster() {
129-
cluster_->GetTabletManager(0)->TEST_listeners.push_back(tablet_manager_listener_);
130+
cluster_->GetTabletManager(0)->tablet_memory_manager()->TEST_listeners.push_back(
131+
tablet_manager_listener_);
130132
}
131133

132134
void SetupWorkload(

src/yb/master/sys_catalog.cc

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
#include "yb/consensus/quorum_util.h"
5555

5656
#include "yb/docdb/doc_rowwise_iterator.h"
57-
#include "yb/docdb/docdb_rocksdb_util.h"
5857

5958
#include "yb/fs/fs_manager.h"
6059
#include "yb/gutil/strings/numbers.h"
@@ -69,6 +68,7 @@
6968
#include "yb/tablet/tablet_options.h"
7069
#include "yb/tablet/operations/write_operation.h"
7170

71+
#include "yb/tserver/tablet_memory_manager.h"
7272
#include "yb/tserver/ts_tablet_manager.h"
7373

7474
#include "yb/util/flag_tags.h"
@@ -157,6 +157,9 @@ SysCatalogTable::~SysCatalogTable() {
157157
}
158158

159159
void SysCatalogTable::StartShutdown() {
160+
if (mem_manager_) {
161+
mem_manager_->Shutdown();
162+
}
160163
if (tablet_peer()) {
161164
CHECK(std::atomic_load(&tablet_peer_)->StartShutdown());
162165
}
@@ -520,21 +523,24 @@ Status SysCatalogTable::OpenTablet(const scoped_refptr<tablet::RaftGroupMetadata
520523
RETURN_NOT_OK(tablet_peer()->SetBootstrapping());
521524
tablet::TabletOptions tablet_options;
522525

523-
block_based_table_mem_tracker_ = docdb::InitBlockCacheMemTracker(
526+
// Returns a vector that includes the tablet peer associated with master.
527+
const auto get_peers_lambda = [this]() -> std::vector<tablet::TabletPeerPtr> {
528+
return { tablet_peer() };
529+
};
530+
531+
mem_manager_ = std::make_shared<tserver::TabletMemoryManager>(
532+
&tablet_options,
533+
master_->mem_tracker(),
524534
kDefaultMasterBlockCacheSizePercentage,
525-
master_->mem_tracker());
526-
block_based_table_gc_ = docdb::InitBlockCache(
527535
GetMetricEntity(),
528-
kDefaultMasterBlockCacheSizePercentage,
529-
block_based_table_mem_tracker_.get(),
530-
&tablet_options);
536+
get_peers_lambda);
531537

532538
tablet::TabletInitData tablet_init_data = {
533539
.metadata = metadata,
534540
.client_future = master_->async_client_initializer().get_client_future(),
535541
.clock = scoped_refptr<server::Clock>(master_->clock()),
536542
.parent_mem_tracker = master_->mem_tracker(),
537-
.block_based_table_mem_tracker = block_based_table_mem_tracker_,
543+
.block_based_table_mem_tracker = mem_manager_->block_based_table_mem_tracker(),
538544
.metric_registry = metric_registry_,
539545
.log_anchor_registry = tablet_peer()->log_anchor_registry(),
540546
.tablet_options = tablet_options,
@@ -587,6 +593,8 @@ Status SysCatalogTable::OpenTablet(const scoped_refptr<tablet::RaftGroupMetadata
587593
if (!tablet->schema()->Equals(schema_)) {
588594
return STATUS(Corruption, "Unexpected schema", tablet->schema()->ToString());
589595
}
596+
RETURN_NOT_OK(mem_manager_->Init());
597+
590598
return Status::OK();
591599
}
592600

src/yb/master/sys_catalog.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
#include "yb/tablet/snapshot_coordinator.h"
4545
#include "yb/tablet/tablet_peer.h"
4646

47+
#include "yb/tserver/tablet_memory_manager.h"
48+
4749
#include "yb/util/mem_tracker.h"
4850
#include "yb/util/pb_util.h"
4951
#include "yb/util/status.h"
@@ -258,9 +260,7 @@ class SysCatalogTable {
258260

259261
std::unordered_map<std::string, scoped_refptr<AtomicGauge<uint64>>> visitor_duration_metrics_;
260262

261-
std::shared_ptr<MemTracker> block_based_table_mem_tracker_;
262-
263-
std::shared_ptr<GarbageCollector> block_based_table_gc_;
263+
std::shared_ptr<tserver::TabletMemoryManager> mem_manager_;
264264

265265
DISALLOW_COPY_AND_ASSIGN(SysCatalogTable);
266266
};

src/yb/tserver/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ set(TSERVER_SRCS
163163
remote_bootstrap_session.cc
164164
remote_bootstrap_snapshots.cc
165165
service_util.cc
166+
tablet_memory_manager.cc
166167
tablet_server.cc
167168
tablet_server_options.cc
168169
tablet_service.cc

0 commit comments

Comments
 (0)