Skip to content

Commit

Permalink
Improve memory after restart (#7952)
Browse files Browse the repository at this point in the history
ref #7564
  • Loading branch information
CalvinNeo committed Aug 23, 2023
1 parent 09a0a20 commit 18b486f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
5 changes: 2 additions & 3 deletions dbms/src/Common/TiFlashMetrics.h
Expand Up @@ -393,9 +393,8 @@ namespace DB
M(tiflash_raft_raft_log_lag_count, \
"Bucketed histogram raft index lag", \
Histogram, \
F(type_compact_index, {{"type", "compact_index"}}, EqualWidthBuckets{0, 200, 5}), \
F(type_applied_index, {{"type", "applied_index"}}, EqualWidthBuckets{0, 200, 5}), \
F(type_unflushed_applied_index, {{"type", "unflushed_applied_index"}}, EqualWidthBuckets{0, 200, 5})) \
F(type_applied_index, {{"type", "applied_index"}}, EqualWidthBuckets{0, 100, 10}), \
F(type_unflushed_applied_index, {{"type", "unflushed_applied_index"}}, EqualWidthBuckets{0, 100, 10})) \
M(tiflash_raft_raft_events_count, \
"Raft event counter", \
Counter, \
Expand Down
6 changes: 5 additions & 1 deletion dbms/src/Storages/Transaction/KVStore.cpp
Expand Up @@ -491,7 +491,11 @@ bool KVStore::canFlushRegionDataImpl(
can_flush = true;
}
auto gap_threshold = region_compact_log_gap.load();

auto last_restart_log_applied = curr_region.lastRestartLogApplied();
if (last_restart_log_applied + gap_threshold > index)
{
gap_threshold = std::max(gap_threshold / 2, 1);
}
auto last_compact_log_applied = curr_region.lastCompactLogApplied();
auto current_applied_gap = index > last_compact_log_applied ? index - last_compact_log_applied : 0;

Expand Down
11 changes: 11 additions & 0 deletions dbms/src/Storages/Transaction/Region.cpp
Expand Up @@ -426,6 +426,7 @@ RegionPtr Region::deserialize(ReadBuffer & buf, const TiFlashRaftProxyHelper * p

RegionData::deserialize(buf, region->data);
region->setLastCompactLogApplied(region->appliedIndex());
region->setLastRestartLogApplied(region->appliedIndex());
return region;
}

Expand Down Expand Up @@ -524,11 +525,21 @@ UInt64 Region::lastCompactLogApplied() const
return last_compact_log_applied;
}

UInt64 Region::lastRestartLogApplied() const
{
return last_restart_log_applied;
}

void Region::setLastCompactLogApplied(UInt64 new_value) const
{
last_compact_log_applied = new_value;
}

void Region::setLastRestartLogApplied(UInt64 new_value) const
{
last_restart_log_applied = new_value;
}

void Region::updateLastCompactLogApplied() const
{
uint64_t current_applied_index = appliedIndex();
Expand Down
5 changes: 5 additions & 0 deletions dbms/src/Storages/Transaction/Region.h
Expand Up @@ -152,7 +152,9 @@ class Region : public std::enable_shared_from_this<Region>
void markCompactLog();
Timepoint lastCompactLogTime() const;
UInt64 lastCompactLogApplied() const;
UInt64 lastRestartLogApplied() const;
void setLastCompactLogApplied(UInt64 new_value) const;
void setLastRestartLogApplied(UInt64 new_value) const;
// Must hold region lock.
void updateLastCompactLogApplied() const;

Expand Down Expand Up @@ -266,7 +268,10 @@ class Region : public std::enable_shared_from_this<Region>
std::atomic<UInt64> snapshot_event_flag{1};
const TiFlashRaftProxyHelper * proxy_helper{nullptr};
mutable std::atomic<Timepoint> last_compact_log_time{Timepoint::min()};
// Applied index since last persistence. Including all admin cmd.
mutable std::atomic<uint64_t> last_compact_log_applied{0};
// Applied index since last restart. Should only be set after restart.
mutable uint64_t last_restart_log_applied{0};
mutable std::atomic<size_t> approx_mem_cache_rows{0};
mutable std::atomic<size_t> approx_mem_cache_bytes{0};
};
Expand Down

0 comments on commit 18b486f

Please sign in to comment.