Add API to let metrics directly drop data without writing to an output.

+ Metrics will do flushIfNeeded() to correctly move the clock and informing
  AnomalyTracker the past bucket info, and then clear past buckets.

+ We will still keep the current bucket data for the validity of the future metrics.

Bug: 70571383
Test: statsd_test
Change-Id: Ib13c45574974e7b4e82bd8f305091dc93bda76f5
diff --git a/cmds/statsd/src/metrics/MetricProducer.h b/cmds/statsd/src/metrics/MetricProducer.h
index 2bf6241..05b7f87 100644
--- a/cmds/statsd/src/metrics/MetricProducer.h
+++ b/cmds/statsd/src/metrics/MetricProducer.h
@@ -148,6 +148,15 @@
         return mMetricId;
     }
 
+    // Let MetricProducer drop in-memory data to save memory.
+    // We still need to keep future data valid and anomaly tracking work, which means we will
+    // have to flush old data, informing anomaly trackers then safely drop old data.
+    // We still keep current bucket data for future metrics' validity.
+    void dropData(const uint64_t dropTimeNs) {
+        std::lock_guard<std::mutex> lock(mMutex);
+        dropDataLocked(dropTimeNs);
+    }
+
 protected:
     virtual void onConditionChangedLocked(const bool condition, const uint64_t eventTime) = 0;
     virtual void onSlicedConditionMayChangeLocked(const uint64_t eventTime) = 0;
@@ -179,6 +188,8 @@
         return mStartTimeNs + (mCurrentBucketNum + 1) * mBucketSizeNs;
     }
 
+    virtual void dropDataLocked(const uint64_t dropTimeNs) = 0;
+
     const int64_t mMetricId;
 
     const ConfigKey mConfigKey;