@@ -690,17 +690,19 @@ void EngineShard::RetireExpiredAndEvict() {
690690 auto [evicted_items, evicted_bytes] =
691691 db_slice.FreeMemWithEvictionStepAtomic (i, starting_segment_id, eviction_goal);
692692
693- DVLOG (2 ) << " Heartbeat eviction: Expected to evict " << eviction_goal
694- << " bytes. Actually evicted " << evicted_items << " items, " << evicted_bytes
695- << " bytes. Max eviction per heartbeat: "
696- << GetFlag (FLAGS_max_eviction_per_heartbeat);
693+ VLOG (2 ) << " Heartbeat eviction: Expected to evict " << eviction_goal
694+ << " bytes. Actually evicted " << evicted_items << " items, " << evicted_bytes
695+ << " bytes. Max eviction per heartbeat: "
696+ << GetFlag (FLAGS_max_eviction_per_heartbeat);
697697
698698 deleted_bytes += evicted_bytes;
699699 eviction_goal -= std::min (eviction_goal, evicted_bytes);
700700 }
701701 }
702702
703- eviction_state_.deleted_bytes_before_rss_update += deleted_bytes;
703+ // Track deleted bytes only if we expect to lower memory
704+ if (eviction_state_.track_deleted_bytes )
705+ eviction_state_.deleted_bytes_before_rss_update += deleted_bytes;
704706}
705707
706708size_t EngineShard::CalculateEvictionBytes () {
@@ -718,9 +720,9 @@ size_t EngineShard::CalculateEvictionBytes() {
718720 size_t goal_bytes =
719721 CalculateHowManyBytesToEvictOnShard (limit, global_used_memory, shard_memory_budget_threshold);
720722
721- VLOG_IF_EVERY_N ( 1 , goal_bytes > 0 , 50 )
722- << " Used memory goal bytes: " << goal_bytes << " , used memory: " << global_used_memory
723- << " , memory limit: " << max_memory_limit;
723+ VLOG_IF ( 2 , goal_bytes > 0 ) << " Used memory goal bytes: " << goal_bytes
724+ << " , used memory: " << global_used_memory
725+ << " , memory limit: " << max_memory_limit;
724726
725727 // Check for `enable_heartbeat_rss_eviction` flag since it dynamic. And reset
726728 // state if flag has changed.
@@ -739,26 +741,45 @@ size_t EngineShard::CalculateEvictionBytes() {
739741 auto decrease_delete_bytes_before_rss_update =
740742 std::min (deleted_bytes_before_rss_update,
741743 (global_rss_memory_at_prev_eviction - global_used_rss_memory) / shards_count);
742- VLOG_EVERY_N ( 1 , 50 ) << " deleted_bytes_before_rss_update: " << deleted_bytes_before_rss_update
743- << " decrease_delete_bytes_before_rss_update: "
744- << decrease_delete_bytes_before_rss_update;
744+ VLOG ( 2 ) << " deleted_bytes_before_rss_update: " << deleted_bytes_before_rss_update
745+ << " decrease_delete_bytes_before_rss_update: "
746+ << decrease_delete_bytes_before_rss_update;
745747 deleted_bytes_before_rss_update -= decrease_delete_bytes_before_rss_update;
746748 }
747749
748750 global_rss_memory_at_prev_eviction = global_used_rss_memory;
749751
752+ LOG_IF (DFATAL, global_used_rss_memory < (deleted_bytes_before_rss_update * shards_count))
753+ << " RSS evicition underflow "
754+ << " global_used_rss_memory: " << global_used_rss_memory
755+ << " deleted_bytes_before_rss_update: " << deleted_bytes_before_rss_update;
756+
757+ // If we underflow use limit as used_memory
758+ size_t used_rss_memory_with_deleted_bytes =
759+ std::min (global_used_rss_memory - deleted_bytes_before_rss_update * shards_count, limit);
760+
750761 // Try to evict more bytes if we are close to the rss memory limit
751762 const size_t rss_goal_bytes = CalculateHowManyBytesToEvictOnShard (
752- limit, global_used_rss_memory - deleted_bytes_before_rss_update * shards_count,
753- shard_memory_budget_threshold);
763+ limit, used_rss_memory_with_deleted_bytes, shard_memory_budget_threshold);
764+
765+ // RSS evictions starts so we should start tracking deleted_bytes
766+ if (rss_goal_bytes) {
767+ eviction_state_.track_deleted_bytes = true ;
768+ } else {
769+ // There is no RSS eviction goal and we have cleared tracked deleted bytes
770+ if (!deleted_bytes_before_rss_update) {
771+ eviction_state_.track_deleted_bytes = false ;
772+ }
773+ }
754774
755- VLOG_IF_EVERY_N ( 1 , rss_goal_bytes > 0 , 50 )
775+ VLOG_IF ( 2 , rss_goal_bytes > 0 )
756776 << " Rss memory goal bytes: " << rss_goal_bytes
757777 << " , rss used memory: " << global_used_rss_memory << " , rss memory limit: " << limit
758778 << " , deleted_bytes_before_rss_update: " << deleted_bytes_before_rss_update;
759779
760780 goal_bytes = std::max (goal_bytes, rss_goal_bytes);
761781 }
782+
762783 return goal_bytes;
763784}
764785
0 commit comments