Address comment at ag/18491259 and ag/18486388

Test: TH
Bug: 230289468
Change-Id: Id91fabb47b542d8526d6aa787b5947238c3934fb
Merged-In: Id91fabb47b542d8526d6aa787b5947238c3934fb
  (cherry-picked from ag/18579048)
This commit is contained in:
Junyu Lai
2022-05-25 00:08:56 +08:00
parent d451fdbb25
commit dbe8ddca71
2 changed files with 31 additions and 45 deletions

View File

@@ -628,30 +628,14 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
}
/**
* Create the persistent counter that counts total import legacy stats attempts.
* Create a persistent counter for given directory and name.
*/
// TODO: Refactor multiple create counter functions into one.
public PersistentInt createImportLegacyAttemptsCounter(@NonNull Path path)
public PersistentInt createPersistentCounter(@NonNull Path dir, @NonNull String name)
throws IOException {
// TODO: Modify PersistentInt to call setStartTime every time a write is made.
// Create and pass a real logger here.
return new PersistentInt(path.toString(), null /* logger */);
}
/**
* Create the persistent counter that counts total import legacy stats successes.
*/
public PersistentInt createImportLegacySuccessesCounter(@NonNull Path path)
throws IOException {
return new PersistentInt(path.toString(), null /* logger */);
}
/**
* Create the persistent counter that counts total import legacy stats fallbacks.
*/
public PersistentInt createImportLegacyFallbacksCounter(@NonNull Path path)
throws IOException {
return new PersistentInt(path.toString(), null /* logger */);
final String path = dir.resolve(name).toString();
return new PersistentInt(path, null /* logger */);
}
/**
@@ -929,12 +913,12 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
return;
}
try {
mImportLegacyAttemptsCounter = mDeps.createImportLegacyAttemptsCounter(
mStatsDir.toPath().resolve(NETSTATS_IMPORT_ATTEMPTS_COUNTER_NAME));
mImportLegacySuccessesCounter = mDeps.createImportLegacySuccessesCounter(
mStatsDir.toPath().resolve(NETSTATS_IMPORT_SUCCESSES_COUNTER_NAME));
mImportLegacyFallbacksCounter = mDeps.createImportLegacyFallbacksCounter(
mStatsDir.toPath().resolve(NETSTATS_IMPORT_FALLBACKS_COUNTER_NAME));
mImportLegacyAttemptsCounter = mDeps.createPersistentCounter(mStatsDir.toPath(),
NETSTATS_IMPORT_ATTEMPTS_COUNTER_NAME);
mImportLegacySuccessesCounter = mDeps.createPersistentCounter(mStatsDir.toPath(),
NETSTATS_IMPORT_SUCCESSES_COUNTER_NAME);
mImportLegacyFallbacksCounter = mDeps.createPersistentCounter(mStatsDir.toPath(),
NETSTATS_IMPORT_FALLBACKS_COUNTER_NAME);
} catch (IOException e) {
Log.wtf(TAG, "Failed to create persistent counters, skip.", e);
return;
@@ -951,12 +935,13 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
return;
}
// If fallbacks is not zero, proceed with reading only to give signals from dogfooders.
// TODO: Remove fallbacks counter check before T formal release.
// TODO(b/233752318): Remove fallbacks counter check before T formal release.
if (attempts >= targetAttempts && fallbacks == 0) return;
if (attempts >= targetAttempts) {
final boolean dryRunImportOnly = (attempts >= targetAttempts);
if (dryRunImportOnly) {
Log.i(TAG, "Starting import : only perform read");
} else{
} else {
Log.i(TAG, "Starting import : attempts " + attempts + "/" + targetAttempts);
}
@@ -1030,7 +1015,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
// For cases where the fallbacks is not zero but target attempts counts reached,
// only perform reads above and return here.
if (attempts >= targetAttempts) return;
if (dryRunImportOnly) return;
// Find the latest end time.
for (final MigrationInfo migration : migrations) {

View File

@@ -67,6 +67,9 @@ import static android.text.format.DateUtils.WEEK_IN_MILLIS;
import static com.android.net.module.util.NetworkStatsUtils.SUBSCRIBER_ID_MATCH_RULE_EXACT;
import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL;
import static com.android.server.net.NetworkStatsService.NETSTATS_IMPORT_ATTEMPTS_COUNTER_NAME;
import static com.android.server.net.NetworkStatsService.NETSTATS_IMPORT_FALLBACKS_COUNTER_NAME;
import static com.android.server.net.NetworkStatsService.NETSTATS_IMPORT_SUCCESSES_COUNTER_NAME;
import static com.android.testutils.DevSdkIgnoreRuleKt.SC_V2;
import static org.junit.Assert.assertEquals;
@@ -152,6 +155,7 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Clock;
@@ -383,21 +387,18 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
}
@Override
public PersistentInt createImportLegacyAttemptsCounter(
@androidx.annotation.NonNull Path path) {
public PersistentInt createPersistentCounter(@androidx.annotation.NonNull Path dir,
@androidx.annotation.NonNull String name) throws IOException {
switch (name) {
case NETSTATS_IMPORT_ATTEMPTS_COUNTER_NAME:
return mImportLegacyAttemptsCounter;
}
@Override
public PersistentInt createImportLegacySuccessesCounter(
@androidx.annotation.NonNull Path path) {
case NETSTATS_IMPORT_SUCCESSES_COUNTER_NAME:
return mImportLegacySuccessesCounter;
}
@Override
public PersistentInt createImportLegacyFallbacksCounter(
@androidx.annotation.NonNull Path path) {
case NETSTATS_IMPORT_FALLBACKS_COUNTER_NAME:
return mImportLegacyFallbacksCounter;
default:
throw new IllegalArgumentException("Unknown counter name: " + name);
}
}
@Override