[MS81] Support remove history before cutoff timestamp

This is needed to ensure corrupted data can be clean up if
the data migration process dones't go well.

Test: NetworkStatsCollectionTest
Bug: 197717846
Change-Id: Ic76ad6f3e96f03791b48988fb2622c9c647ffc7c
(cherry picked from commit 306a00316c)
Merged-In: Ic76ad6f3e96f03791b48988fb2622c9c647ffc7c
This commit is contained in:
Junyu Lai
2022-01-29 04:12:58 +00:00
committed by Cherrypicker Worker
parent d1ba6a4f53
commit 108d20f84a
5 changed files with 189 additions and 15 deletions

View File

@@ -694,6 +694,26 @@ public class NetworkStatsCollection implements FileRotator.Reader, FileRotator.W
}
}
/**
* Remove histories which contains or is before the cutoff timestamp.
* @hide
*/
public void removeHistoryBefore(long cutoffMillis) {
final ArrayList<Key> knownKeys = new ArrayList<>();
knownKeys.addAll(mStats.keySet());
for (Key key : knownKeys) {
final NetworkStatsHistory history = mStats.get(key);
if (history.getStart() > cutoffMillis) continue;
history.removeBucketsStartingBefore(cutoffMillis);
if (history.size() == 0) {
mStats.remove(key);
}
mDirty = true;
}
}
private void noteRecordedHistory(long startMillis, long endMillis, long totalBytes) {
if (startMillis < mStartMillis) mStartMillis = startMillis;
if (endMillis > mEndMillis) mEndMillis = endMillis;

View File

@@ -680,19 +680,21 @@ public final class NetworkStatsHistory implements Parcelable {
}
/**
* Remove buckets older than requested cutoff.
* Remove buckets that start older than requested cutoff.
*
* This method will remove any bucket that contains any data older than the requested
* cutoff, even if that same bucket includes some data from after the cutoff.
*
* @hide
*/
public void removeBucketsBefore(long cutoff) {
public void removeBucketsStartingBefore(final long cutoff) {
// TODO: Consider use getIndexBefore.
int i;
for (i = 0; i < bucketCount; i++) {
final long curStart = bucketStart[i];
final long curEnd = curStart + bucketDuration;
// cutoff happens before or during this bucket; everything before
// this bucket should be removed.
if (curEnd > cutoff) break;
// This bucket starts after or at the cutoff, so it should be kept.
if (curStart >= cutoff) break;
}
if (i > 0) {