From 9b4a5ec9261a1d3e676b93ad8c2d52240814e163 Mon Sep 17 00:00:00 2001 From: Tej Singh Date: Thu, 25 Apr 2019 12:43:35 -0700 Subject: [PATCH] AML: update experiment id logic Update the experiment id logic for binary push and train info to reflect the new design. Bug: 131267440 Test: gts tests added in topic Test: gts-tradefed run gts-dev --module GtsStatsdHostTestCases Change-Id: I09ff331c9d4b91c2e13a51b832c33d11b34ec0d7 --- cmds/statsd/src/StatsService.cpp | 72 ++++++++++++++++++++++--------- cmds/statsd/src/StatsService.h | 7 ++- cmds/statsd/src/logd/LogEvent.cpp | 3 +- 3 files changed, 58 insertions(+), 24 deletions(-) diff --git a/cmds/statsd/src/StatsService.cpp b/cmds/statsd/src/StatsService.cpp index 14332528e992d..4c97c3454e829 100644 --- a/cmds/statsd/src/StatsService.cpp +++ b/cmds/statsd/src/StatsService.cpp @@ -1202,9 +1202,10 @@ Status StatsService::unregisterPullerCallback(int32_t atomTag, const String16& p return Status::ok(); } -Status StatsService::sendBinaryPushStateChangedAtom(const android::String16& trainName, - int64_t trainVersionCode, int options, - int32_t state, +Status StatsService::sendBinaryPushStateChangedAtom(const android::String16& trainNameIn, + const int64_t trainVersionCodeIn, + const int options, + const int32_t state, const std::vector& experimentIdsIn) { uid_t uid = IPCThreadState::self()->getCallingUid(); // For testing @@ -1224,34 +1225,64 @@ Status StatsService::sendBinaryPushStateChangedAtom(const android::String16& tra // TODO: add verifier permission bool readTrainInfoSuccess = false; - InstallTrainInfo trainInfo; - if (trainVersionCode == -1 || experimentIdsIn.empty() || trainName.size() == 0) { - readTrainInfoSuccess = StorageManager::readTrainInfo(trainInfo); - } + InstallTrainInfo trainInfoOnDisk; + readTrainInfoSuccess = StorageManager::readTrainInfo(trainInfoOnDisk); - if (trainVersionCode == -1 && readTrainInfoSuccess) { - trainVersionCode = trainInfo.trainVersionCode; + bool resetExperimentIds = false; + int64_t trainVersionCode = trainVersionCodeIn; + std::string trainNameUtf8 = std::string(String8(trainNameIn).string()); + if (readTrainInfoSuccess) { + // Keep the old train version if we received an empty version. + if (trainVersionCodeIn == -1) { + trainVersionCode = trainInfoOnDisk.trainVersionCode; + } else if (trainVersionCodeIn != trainInfoOnDisk.trainVersionCode) { + // Reset experiment ids if we receive a new non-empty train version. + resetExperimentIds = true; + } + + // Keep the old train name if we received an empty train name. + if (trainNameUtf8.size() == 0) { + trainNameUtf8 = trainInfoOnDisk.trainName; + } else if (trainNameUtf8 != trainInfoOnDisk.trainName) { + // Reset experiment ids if we received a new valid train name. + resetExperimentIds = true; + } + + // Reset if we received a different experiment id. + if (!experimentIdsIn.empty() && + (trainInfoOnDisk.experimentIds.empty() || + experimentIdsIn[0] != trainInfoOnDisk.experimentIds[0])) { + resetExperimentIds = true; + } } // Find the right experiment IDs std::vector experimentIds; - if (readTrainInfoSuccess && experimentIdsIn.empty()) { - experimentIds = trainInfo.experimentIds; - } else { + if (resetExperimentIds || !readTrainInfoSuccess) { experimentIds = experimentIdsIn; + } else { + experimentIds = trainInfoOnDisk.experimentIds; + } + + if (!experimentIds.empty()) { + int64_t firstId = experimentIds[0]; + switch (state) { + case android::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALL_SUCCESS: + experimentIds.push_back(firstId + 1); + break; + case android::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALLER_ROLLBACK_INITIATED: + experimentIds.push_back(firstId + 2); + break; + case android::util::BINARY_PUSH_STATE_CHANGED__STATE__INSTALLER_ROLLBACK_SUCCESS: + experimentIds.push_back(firstId + 3); + break; + } } // Flatten the experiment IDs to proto vector experimentIdsProtoBuffer; writeExperimentIdsToProto(experimentIds, &experimentIdsProtoBuffer); - - // Find the right train name - std::string trainNameUtf8; - if (readTrainInfoSuccess && trainName.size() == 0) { - trainNameUtf8 = trainInfo.trainName; - } else { - trainNameUtf8 = std::string(String8(trainName).string()); - } + StorageManager::writeTrainInfo(trainVersionCode, trainNameUtf8, state, experimentIds); userid_t userId = multiuser_get_user_id(uid); bool requiresStaging = options & IStatsManager::FLAG_REQUIRE_STAGING; @@ -1260,7 +1291,6 @@ Status StatsService::sendBinaryPushStateChangedAtom(const android::String16& tra LogEvent event(trainNameUtf8, trainVersionCode, requiresStaging, rollbackEnabled, requiresLowLatencyMonitor, state, experimentIdsProtoBuffer, userId); mProcessor->OnLogEvent(&event); - StorageManager::writeTrainInfo(trainVersionCode, trainNameUtf8, state, experimentIds); return Status::ok(); } diff --git a/cmds/statsd/src/StatsService.h b/cmds/statsd/src/StatsService.h index 929d260dcd95e..0b6df8b0997cf 100644 --- a/cmds/statsd/src/StatsService.h +++ b/cmds/statsd/src/StatsService.h @@ -189,8 +189,11 @@ public: * Binder call to log BinaryPushStateChanged atom. */ virtual Status sendBinaryPushStateChangedAtom( - const android::String16& trainName, int64_t trainVersionCode, int options, - int32_t state, const std::vector& experimentIds) override; + const android::String16& trainNameIn, + const int64_t trainVersionCodeIn, + const int options, + const int32_t state, + const std::vector& experimentIdsIn) override; /** * Binder call to get registered experiment IDs. diff --git a/cmds/statsd/src/logd/LogEvent.cpp b/cmds/statsd/src/logd/LogEvent.cpp index ca874b57170e8..0ade53118d77f 100644 --- a/cmds/statsd/src/logd/LogEvent.cpp +++ b/cmds/statsd/src/logd/LogEvent.cpp @@ -678,7 +678,8 @@ void LogEvent::ToProto(ProtoOutputStream& protoOutput) const { writeFieldValueTreeToStream(mTagId, getValues(), &protoOutput); } -void writeExperimentIdsToProto(const std::vector& experimentIds, std::vector* protoOut) { +void writeExperimentIdsToProto(const std::vector& experimentIds, + std::vector* protoOut) { ProtoOutputStream proto; for (const auto& expId : experimentIds) { proto.write(FIELD_TYPE_INT64 | FIELD_COUNT_REPEATED | FIELD_ID_EXPERIMENT_ID,