In QUIC BBRv2, reset max_bytes_delivered_in_round when a new round starts.

Protected by FLAGS_quic_reloadable_flag_quic_bbr2_reset_max_bytes_delivered.

PiperOrigin-RevId: 350375957
Change-Id: I014bcb32d0740ad6433ef1cd9e7baa404e111a60
diff --git a/quic/core/congestion_control/bbr2_misc.cc b/quic/core/congestion_control/bbr2_misc.cc
index 2ff92d5..4413ba3 100644
--- a/quic/core/congestion_control/bbr2_misc.cc
+++ b/quic/core/congestion_control/bbr2_misc.cc
@@ -8,6 +8,7 @@
 #include "quic/core/quic_bandwidth.h"
 #include "quic/core/quic_time.h"
 #include "quic/core/quic_types.h"
+#include "quic/platform/api/quic_flag_utils.h"
 #include "quic/platform/api/quic_logging.h"
 
 namespace quic {
@@ -272,8 +273,13 @@
     QuicPacketNumber least_unacked_packet,
     const Bbr2CongestionEvent& congestion_event) {
   if (congestion_event.end_of_round_trip) {
-    bytes_lost_in_round_ = 0;
-    loss_events_in_round_ = 0;
+    if (!reset_max_bytes_delivered_) {
+      bytes_lost_in_round_ = 0;
+      loss_events_in_round_ = 0;
+    } else {
+      QUIC_RELOADABLE_FLAG_COUNT_N(quic_bbr2_reset_max_bytes_delivered, 1, 2);
+      OnNewRound();
+    }
   }
 
   bandwidth_sampler_.RemoveObsoletePackets(least_unacked_packet);
@@ -347,11 +353,23 @@
   return false;
 }
 
-void Bbr2NetworkModel::RestartRound() {
+void Bbr2NetworkModel::RestartRoundEarly() {
+  if (!reset_max_bytes_delivered_) {
+    bytes_lost_in_round_ = 0;
+    loss_events_in_round_ = 0;
+    max_bytes_delivered_in_round_ = 0;
+  } else {
+    QUIC_RELOADABLE_FLAG_COUNT_N(quic_bbr2_reset_max_bytes_delivered, 2, 2);
+    OnNewRound();
+  }
+  round_trip_counter_.RestartRound();
+}
+
+void Bbr2NetworkModel::OnNewRound() {
+  DCHECK(reset_max_bytes_delivered_);
   bytes_lost_in_round_ = 0;
   loss_events_in_round_ = 0;
   max_bytes_delivered_in_round_ = 0;
-  round_trip_counter_.RestartRound();
 }
 
 void Bbr2NetworkModel::cap_inflight_lo(QuicByteCount cap) {
diff --git a/quic/core/congestion_control/bbr2_misc.h b/quic/core/congestion_control/bbr2_misc.h
index 2cb8333..4bffb2b 100644
--- a/quic/core/congestion_control/bbr2_misc.h
+++ b/quic/core/congestion_control/bbr2_misc.h
@@ -359,7 +359,7 @@
   void AdaptLowerBounds(const Bbr2CongestionEvent& congestion_event);
 
   // Restart the current round trip as if it is starting now.
-  void RestartRound();
+  void RestartRoundEarly();
 
   void AdvanceMaxBandwidthFilter() { max_bandwidth_filter_.Advance(); }
 
@@ -509,6 +509,9 @@
   }
 
  private:
+  // Called when a new round trip starts.
+  void OnNewRound();
+
   const Bbr2Params& Params() const { return *params_; }
   const Bbr2Params* const params_;
   RoundTripCounter round_trip_counter_;
@@ -550,6 +553,8 @@
   bool full_bandwidth_reached_ = false;
   QuicBandwidth full_bandwidth_baseline_ = QuicBandwidth::Zero();
   QuicRoundTripCount rounds_without_bandwidth_growth_ = 0;
+  const bool reset_max_bytes_delivered_ =
+      GetQuicReloadableFlag(quic_bbr2_reset_max_bytes_delivered);
 };
 
 enum class Bbr2Mode : uint8_t {
diff --git a/quic/core/congestion_control/bbr2_probe_bw.cc b/quic/core/congestion_control/bbr2_probe_bw.cc
index 4054cd6..031e5a1 100644
--- a/quic/core/congestion_control/bbr2_probe_bw.cc
+++ b/quic/core/congestion_control/bbr2_probe_bw.cc
@@ -510,7 +510,7 @@
 
   cycle_.probe_up_bytes = std::numeric_limits<QuicByteCount>::max();
   cycle_.has_advanced_max_bw = false;
-  model_->RestartRound();
+  model_->RestartRoundEarly();
 }
 
 void Bbr2ProbeBwMode::EnterProbeCruise(QuicTime now) {
@@ -549,7 +549,7 @@
   model_->clear_inflight_lo();
   cycle_.probe_up_rounds = probe_up_rounds;
   cycle_.probe_up_acked = 0;
-  model_->RestartRound();
+  model_->RestartRoundEarly();
 }
 
 void Bbr2ProbeBwMode::EnterProbeUp(QuicTime now) {
@@ -564,7 +564,7 @@
   cycle_.is_sample_from_probing = true;
   RaiseInflightHighSlope();
 
-  model_->RestartRound();
+  model_->RestartRoundEarly();
 }
 
 void Bbr2ProbeBwMode::ExitProbeDown() {
diff --git a/quic/core/quic_flags_list.h b/quic/core/quic_flags_list.h
index 0c9dd37..41d2820 100644
--- a/quic/core/quic_flags_list.h
+++ b/quic/core/quic_flags_list.h
@@ -15,6 +15,7 @@
 QUIC_FLAG(FLAGS_quic_reloadable_flag_quic_bbr2_bw_startup, false)
 QUIC_FLAG(FLAGS_quic_reloadable_flag_quic_bbr2_disable_reno_coexistence, false)
 QUIC_FLAG(FLAGS_quic_reloadable_flag_quic_bbr2_fewer_startup_round_trips, false)
+QUIC_FLAG(FLAGS_quic_reloadable_flag_quic_bbr2_reset_max_bytes_delivered, false)
 QUIC_FLAG(FLAGS_quic_reloadable_flag_quic_bbr2_use_bytes_delivered, false)
 QUIC_FLAG(FLAGS_quic_reloadable_flag_quic_can_send_ack_frequency, true)
 QUIC_FLAG(FLAGS_quic_reloadable_flag_quic_close_connection_on_0rtt_packet_number_higher_than_1rtt, false)