Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the client code for supporting experiments #5034

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@
<Filter>Source Files\Common</Filter>
</ClCompile>
<ClCompile Include="Experiment.cpp">
<Filter>Source Files\Repository</Filter>
<Filter>Source Files\Common</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/AppInstallerCLITests/Experiment.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
Expand Down
9 changes: 4 additions & 5 deletions src/AppInstallerCommonCore/AppInstallerTelemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#define AICLI_TraceLoggingStringView(_sv_,_name_) TraceLoggingCountedUtf8String(_sv_.data(), static_cast<ULONG>(_sv_.size()), _name_)
#define AICLI_TraceLoggingWStringView(_sv_,_name_) TraceLoggingCountedWideString(_sv_.data(), static_cast<ULONG>(_sv_.size()), _name_)
#define AICLI_TraceLoggingJsonWString(_sv_, _name_) TraceLoggingPackedFieldEx(_sv_.c_str(), static_cast<ULONG>((_sv_.size() + 1) * sizeof(wchar_t)), TlgInUNICODESTRING, TlgOutJSON, _name_)
#define AICLI_TraceLoggingJsonString(_sv_, _name_) TraceLoggingPackedFieldEx(_sv_.c_str(), static_cast<ULONG>((_sv_.size() + 1) * sizeof(char)), TlgInUNICODESTRING, TlgOutJSON, _name_)

#define AICLI_TraceLoggingWriteActivity(_eventName_,...) TraceLoggingWriteActivity(\
g_hTraceProvider,\
Expand Down Expand Up @@ -81,7 +80,7 @@ namespace AppInstaller::Logging
}
}

std::string GetExperimentsJson(const std::map<Experiment::Key, ExperimentState>& experiments)
std::wstring GetExperimentsJson(const std::map<Experiment::Key, ExperimentState>& experiments)
{
Json::Value root;
for (const auto& experiment : experiments)
Expand All @@ -91,7 +90,7 @@ namespace AppInstaller::Logging
}

Json::StreamWriterBuilder builder;
return Json::writeString(builder, root);
return Utility::ConvertToUTF16(Json::writeString(builder, root));
}
}

Expand Down Expand Up @@ -870,8 +869,8 @@ namespace AppInstaller::Logging
AICLI_TraceLoggingStringView(m_summary.RepairExecutionType, "RepairExecutionType"),
TraceLoggingUInt32(m_summary.RepairErrorCode, "RepairErrorCode"),
TelemetryPrivacyDataTag(PDT_ProductAndServicePerformance | PDT_ProductAndServiceUsage | PDT_SoftwareSetupAndInventory),
TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES),
AICLI_TraceLoggingJsonString(experimentsJson, "ExperimentsJson"));
AICLI_TraceLoggingJsonWString(experimentsJson, "ExperimentsJson"),
TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES));
}
}
}
Expand Down
31 changes: 13 additions & 18 deletions src/AppInstallerCommonCore/Experiment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,30 @@ namespace AppInstaller::Settings
{
ExperimentState GetExperimentStateInternal(Experiment::Key key, const UserSettings& userSettings)
{
if (key == Experiment::Key::None)
{
return { true, ExperimentToggleSource::Default };
}

if (!GroupPolicies().IsEnabled(TogglePolicy::Policy::Experiments))
{
AICLI_LOG(Core, Info, <<
"Experiment " << Experiment::GetExperiment(key).Name() <<
AICLI_LOG(Core, Info, << "Experiment " << Experiment::GetExperiment(key).Name() <<
" is disabled due to group policy: " << TogglePolicy::GetPolicy(TogglePolicy::Policy::Experiments).RegValueName());
return { false, ExperimentToggleSource::Policy };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you're intending for all experiments to have a default value of "disabled". I think the API we'll use allows us to have the control group be "enabled" and the experiment "disabled". We should at least add a comment somewhere we'll see while adding a new experiment to remind us to not add experiments like that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please provide more details about this comment? I’m not entirely clear on what the comment should mention.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should consider the meaning of the boolean associated with an experiment not "enabled" or "disabled", but rather, "In the experimental group?" That makes the default state of "false" mean "Not in the experimental group", and thus more directly maps to group policy that disables running experiments. A comment on ExperimentState::IsEnabled indicating this semantic and counter-indicating use of the "inverted" experiment would be sufficient.

}

auto experiments = userSettings.Get<Setting::Experiments>();
if (key == Experiment::Key::None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this back to first. While I'm not sure that we need a None, if we have one then checks against it should not end up logging about group policy being disabled.

{
return { false, ExperimentToggleSource::Default };
}

auto experiment = Experiment::GetExperiment(key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please call Experiment::GetExperiment(key) at most once in this function. All of the logs call it again rather than using this local.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you did this, but only to uses after the existing experiment local was created. Move the local earlier so that it can be truly constructed once.

auto userSettingsExperiments = userSettings.Get<Setting::Experiments>();
auto jsonName = std::string(experiment.JsonName());
if (experiments.find(jsonName) != experiments.end())
if (userSettingsExperiments.find(jsonName) != userSettingsExperiments.end())
{
auto isEnabled = experiments[jsonName];
AICLI_LOG(Core, Info, <<
"Experiment " << Experiment::GetExperiment(key).Name() <<
" is set to " << isEnabled << " in user settings");
auto isEnabled = userSettingsExperiments[jsonName];
AICLI_LOG(Core, Info, << "Experiment " << experiment.Name() << " is set to " << (isEnabled ? "true" : "false") << " in user settings");
return { isEnabled, ExperimentToggleSource::UserSetting };
}

auto isEnabled = AppInstaller::Experiment::IsEnabled(experiment.GetKey());
AICLI_LOG(Core, Info, <<
"Experiment " << Experiment::GetExperiment(key).Name() <<
" is set to " << isEnabled);
AICLI_LOG(Core, Info, << "Experiment " << experiment.Name() << " is set to " << (isEnabled ? "true" : "false"));
return { isEnabled, ExperimentToggleSource::Default };
}

Expand Down Expand Up @@ -85,7 +80,7 @@ namespace AppInstaller::Settings
switch (key)
{
case Key::CDN:
return Experiment{ "CDN experiment", "CDN", "https://aka.ms/winget-settings", "CDN"};
return Experiment{ "winget source CDN experiment", "CDN", "https://aka.ms/winget-settings", "CDN"};
#ifndef AICLI_DISABLE_TEST_HOOKS
case Key::TestExperiment:
return Experiment{ "Test experiment", "TestExperiment", "https://aka.ms/winget-settings", "TestExperiment" };
Expand All @@ -99,7 +94,7 @@ namespace AppInstaller::Settings
{
std::vector<Experiment> result;

for (Key_t i = 0x1; i < static_cast<Key_t>(Key::Max); i = i << 1)
for (Key_t i = 0x1; i < static_cast<Key_t>(Key::Max); ++i)
{
result.emplace_back(GetExperiment(static_cast<Key>(i)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ namespace AppInstaller::Logging
std::string DOUrl;
HRESULT DOHResult = S_OK;

// LogExperiment
std::map<Settings::Experiment::Key, Settings::ExperimentState> Experiments;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::map<Settings::Experiment::Key, Settings::ExperimentState> Experiments;
Settings::ExperimentStateCache ExperimentCache;

Per my other comment (and previous discussion) on creating this type.

};

Expand Down
20 changes: 10 additions & 10 deletions src/AppInstallerCommonCore/Public/winget/Experiment.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace AppInstaller::Settings
enum ExperimentToggleSource
{
Default = 0,
Policy = 1,
UserSetting = 2,
Policy,
UserSetting,
};

struct ExperimentState
Expand All @@ -30,8 +30,8 @@ namespace AppInstaller::Settings
{
enum class Key : unsigned
{
None = 0x0,
CDN = 0x1,
None = 0,
CDN,
Max,

#ifndef AICLI_DISABLE_TEST_HOOKS
Expand All @@ -41,23 +41,23 @@ namespace AppInstaller::Settings

using Key_t = std::underlying_type_t<Key>;

Experiment(std::string_view name, std::string_view jsonName, std::string_view link, std::string key) :
m_name(name), m_jsonName(jsonName), m_link(link), m_key(key) {}
Experiment(std::string name, std::string jsonName, std::string link, std::string key) :
m_name(std::move(name)), m_jsonName(jsonName), m_link(std::move(link)), m_key(std::move((key))) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
m_name(std::move(name)), m_jsonName(jsonName), m_link(std::move(link)), m_key(std::move((key))) {}
m_name(std::move(name)), m_jsonName(std::move(jsonName)), m_link(std::move(link)), m_key(std::move((key))) {}

Accompanied by making m_jsonName not a view.


static ExperimentState GetState(Key feature);
static ExperimentState GetStateInternal(Key feature);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What makes it internal if it is public?

static Experiment GetExperiment(Key key);
static std::vector<Experiment> GetAllExperiments();

std::string_view Name() const { return m_name; }
std::string Name() const { return m_name; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::string Name() const { return m_name; }
const std::string& Name() const { return m_name; }

And the rest as const&.

Utility::LocIndView JsonName() const { return m_jsonName; }
std::string_view Link() const { return m_link; }
std::string Link() const { return m_link; }
std::string GetKey() const { return m_key; }

private:
std::string_view m_name;
std::string m_name;
Utility::LocIndView m_jsonName;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not need to be a localized value, does it?

std::string_view m_link;
std::string m_link;
std::string m_key;
};
}
5 changes: 3 additions & 2 deletions src/AppInstallerSharedLib/JsonUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ namespace AppInstaller::JSON
{
for (const auto& entry : node.getMemberNames())
{
if (node[entry].isBool())
auto& value = node[entry];
if (value.isBool())
{
result[entry] = node[entry].asBool();
result[entry] = value.asBool();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/AppInstallerSharedLib/Public/winget/JsonUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace AppInstaller::JSON
std::optional<std::vector<std::string>> GetValue<std::vector<std::string>>(const Json::Value& node);

template<>
std::optional<std::map<std::string, bool>> GetValue(const Json::Value& node);
std::optional<std::map<std::string, bool>> GetValue<std::map<std::string, bool>>(const Json::Value& node);

#ifndef WINGET_DISABLE_FOR_FUZZING
// For cpprestsdk JSON
Expand Down
Loading