Partial config update: various small things

- init new/replaced metrics
- guardrails for number of metrics/matchers/conditions/alerts
- report update to statsdstats
- update if the config is active

Test: atest statsd_test
Bug: 162323476
Bug: 176249205
Bug: 176248686
Merged-In: I751e2c7c8bbbd819f216821cbf4f0ec315c4563b
Change-Id: Ieaa2d18903de8020fcee5bb128124ab19f10a152
diff --git a/bin/src/StatsLogProcessor.cpp b/bin/src/StatsLogProcessor.cpp
index ecb93dd..8e919c1 100644
--- a/bin/src/StatsLogProcessor.cpp
+++ b/bin/src/StatsLogProcessor.cpp
@@ -549,9 +549,7 @@
         configValid = it->second->updateConfig(config, mTimeBaseNs, timestampNs,
                                                mAnomalyAlarmMonitor, mPeriodicAlarmMonitor);
         if (configValid) {
-            // TODO(b/162323476): refresh TTL, ensure init() is handled properly.
             mUidMap->OnConfigUpdated(key);
-
         }
     }
     if (!configValid) {
diff --git a/bin/src/metrics/MetricsManager.cpp b/bin/src/metrics/MetricsManager.cpp
index 3b6e10b..f9b0a10 100644
--- a/bin/src/metrics/MetricsManager.cpp
+++ b/bin/src/metrics/MetricsManager.cpp
@@ -97,40 +97,8 @@
     for (const auto& annotation : config.annotation()) {
         mAnnotations.emplace_back(annotation.field_int64(), annotation.field_int32());
     }
-
-    // Guardrail. Reject the config if it's too big.
-    if (mAllMetricProducers.size() > StatsdStats::kMaxMetricCountPerConfig ||
-        mAllConditionTrackers.size() > StatsdStats::kMaxConditionCountPerConfig ||
-        mAllAtomMatchingTrackers.size() > StatsdStats::kMaxMatcherCountPerConfig) {
-        ALOGE("This config is too big! Reject!");
-        mConfigValid = false;
-    }
-    if (mAllAnomalyTrackers.size() > StatsdStats::kMaxAlertCountPerConfig) {
-        ALOGE("This config has too many alerts! Reject!");
-        mConfigValid = false;
-    }
-
-    mIsAlwaysActive = (mMetricIndexesWithActivation.size() != mAllMetricProducers.size()) ||
-            (mAllMetricProducers.size() == 0);
-    bool isActive = mIsAlwaysActive;
-    for (int metric : mMetricIndexesWithActivation) {
-        isActive |= mAllMetricProducers[metric]->isActive();
-    }
-    mIsActive = isActive;
-    VLOG("mIsActive is initialized to %d", mIsActive)
-
-    // no matter whether this config is valid, log it in the stats.
-    StatsdStats::getInstance().noteConfigReceived(
-            key, mAllMetricProducers.size(), mAllConditionTrackers.size(),
-            mAllAtomMatchingTrackers.size(), mAllAnomalyTrackers.size(), mAnnotations,
-            mConfigValid);
-    // Check active
-    for (const auto& metric : mAllMetricProducers) {
-        if (metric->isActive()) {
-            mIsActive = true;
-            break;
-        }
-    }
+    verifyGuardrailsAndUpdateStatsdStats();
+    initializeConfigActiveStatus();
 }
 
 MetricsManager::~MetricsManager() {
@@ -211,6 +179,9 @@
     mPullAtomUids.clear();
     mPullAtomPackages.clear();
     createAllLogSourcesFromConfig(config);
+
+    verifyGuardrailsAndUpdateStatsdStats();
+    initializeConfigActiveStatus();
     return mConfigValid;
 }
 
@@ -272,6 +243,35 @@
     }
 }
 
+void MetricsManager::verifyGuardrailsAndUpdateStatsdStats() {
+    // Guardrail. Reject the config if it's too big.
+    if (mAllMetricProducers.size() > StatsdStats::kMaxMetricCountPerConfig ||
+        mAllConditionTrackers.size() > StatsdStats::kMaxConditionCountPerConfig ||
+        mAllAtomMatchingTrackers.size() > StatsdStats::kMaxMatcherCountPerConfig) {
+        ALOGE("This config is too big! Reject!");
+        mConfigValid = false;
+    }
+    if (mAllAnomalyTrackers.size() > StatsdStats::kMaxAlertCountPerConfig) {
+        ALOGE("This config has too many alerts! Reject!");
+        mConfigValid = false;
+    }
+    // no matter whether this config is valid, log it in the stats.
+    StatsdStats::getInstance().noteConfigReceived(
+            mConfigKey, mAllMetricProducers.size(), mAllConditionTrackers.size(),
+            mAllAtomMatchingTrackers.size(), mAllAnomalyTrackers.size(), mAnnotations,
+            mConfigValid);
+}
+
+void MetricsManager::initializeConfigActiveStatus() {
+    mIsAlwaysActive = (mMetricIndexesWithActivation.size() != mAllMetricProducers.size()) ||
+                      (mAllMetricProducers.size() == 0);
+    mIsActive = mIsAlwaysActive;
+    for (int metric : mMetricIndexesWithActivation) {
+        mIsActive |= mAllMetricProducers[metric]->isActive();
+    }
+    VLOG("mIsActive is initialized to %d", mIsActive);
+}
+
 void MetricsManager::initAllowedLogSources() {
     std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
     mAllowedLogSources.clear();
diff --git a/bin/src/metrics/MetricsManager.h b/bin/src/metrics/MetricsManager.h
index 5fc8f4e..16c57c6 100644
--- a/bin/src/metrics/MetricsManager.h
+++ b/bin/src/metrics/MetricsManager.h
@@ -292,6 +292,14 @@
     // Calls initAllowedLogSources and initPullAtomSources. Sets mConfigValid to false on error.
     void createAllLogSourcesFromConfig(const StatsdConfig& config);
 
+    // Verifies the config meets guardrails and updates statsdStats.
+    // Sets mConfigValid to false on error. Should be called on config creation/update
+    void verifyGuardrailsAndUpdateStatsdStats();
+
+    // Initializes mIsAlwaysActive and mIsActive.
+    // Should be called on config creation/update.
+    void initializeConfigActiveStatus();
+
     // The metrics that don't need to be uploaded or even reported.
     std::set<int64_t> mNoReportMetricIds;
 
diff --git a/bin/src/metrics/parsing_utils/config_update_utils.cpp b/bin/src/metrics/parsing_utils/config_update_utils.cpp
index af9606b..0699ec0 100644
--- a/bin/src/metrics/parsing_utils/config_update_utils.cpp
+++ b/bin/src/metrics/parsing_utils/config_update_utils.cpp
@@ -895,6 +895,12 @@
         }
     }
 
+    // Init new/replaced metrics.
+    for (size_t i = 0; i < newMetricProducers.size(); i++) {
+        if (metricsToUpdate[i] == UPDATE_REPLACE || metricsToUpdate[i] == UPDATE_NEW) {
+            newMetricProducers[i]->prepareFirstBucket();
+        }
+    }
     return true;
 }