Commit 0c6a100b authored by Michael van Ouwerkerk's avatar Michael van Ouwerkerk Committed by Commit Bot

Parameterize the device info pulse interval.

Bug: 1030266
Change-Id: I4bc3751593aeab88d33c9c5b051023daedb5cecc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2020950
Commit-Queue: Michael van Ouwerkerk <mvanouwerkerk@chromium.org>
Reviewed-by: default avatarMikel Astiz <mastiz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#735416}
parent 81546805
...@@ -599,7 +599,7 @@ void DeviceInfoSyncBridge::SendLocalDataWithBatch( ...@@ -599,7 +599,7 @@ void DeviceInfoSyncBridge::SendLocalDataWithBatch(
StoreSpecifics(std::move(specifics), batch.get()); StoreSpecifics(std::move(specifics), batch.get());
CommitAndNotify(std::move(batch), /*should_notify=*/true); CommitAndNotify(std::move(batch), /*should_notify=*/true);
pulse_timer_.Start(FROM_HERE, DeviceInfoUtil::kPulseInterval, pulse_timer_.Start(FROM_HERE, DeviceInfoUtil::GetPulseInterval(),
base::BindOnce(&DeviceInfoSyncBridge::SendLocalData, base::BindOnce(&DeviceInfoSyncBridge::SendLocalData,
base::Unretained(this))); base::Unretained(this)));
} }
......
...@@ -6,21 +6,28 @@ ...@@ -6,21 +6,28 @@
#include <algorithm> #include <algorithm>
#include "base/feature_list.h"
#include "base/metrics/field_trial_params.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "components/sync/protocol/sync.pb.h" #include "components/sync/protocol/sync.pb.h"
namespace syncer { namespace syncer {
using base::Time;
using base::TimeDelta;
using sync_pb::DeviceInfoSpecifics;
const char DeviceInfoUtil::kClientTagPrefix[] = "DeviceInfo_"; const char DeviceInfoUtil::kClientTagPrefix[] = "DeviceInfo_";
const TimeDelta DeviceInfoUtil::kPulseInterval = TimeDelta::FromDays(1); const base::TimeDelta DeviceInfoUtil::kActiveThreshold =
const TimeDelta DeviceInfoUtil::kActiveThreshold = TimeDelta::FromDays(14); base::TimeDelta::FromDays(14);
namespace { namespace {
// Feature flag for configuring the pulse interval.
// TODO(crbug.com/1045940): Remove this when the experiment concludes.
const base::Feature kPulseInterval{"PulseInterval",
base::FEATURE_DISABLED_BY_DEFAULT};
// The delay between periodic updates to the entry corresponding to this device.
const base::FeatureParam<int> kPulseIntervalHours = {&kPulseInterval,
"PulseIntervalHours", 24};
base::TimeDelta Age(const base::Time last_update, const base::Time now) { base::TimeDelta Age(const base::Time last_update, const base::Time now) {
// Don't allow negative age for things somehow updated in the future. // Don't allow negative age for things somehow updated in the future.
return std::max(base::TimeDelta(), now - last_update); return std::max(base::TimeDelta(), now - last_update);
...@@ -28,16 +35,23 @@ base::TimeDelta Age(const base::Time last_update, const base::Time now) { ...@@ -28,16 +35,23 @@ base::TimeDelta Age(const base::Time last_update, const base::Time now) {
} // namespace } // namespace
// static
base::TimeDelta DeviceInfoUtil::GetPulseInterval() {
return base::TimeDelta::FromHours(kPulseIntervalHours.Get());
}
// static // static
base::TimeDelta DeviceInfoUtil::CalculatePulseDelay( base::TimeDelta DeviceInfoUtil::CalculatePulseDelay(
const base::Time last_update, const base::Time last_update,
const base::Time now) { const base::Time now) {
// Don't allow negative delays for very stale data, use delay of 0. // Don't allow negative delays for very stale data, use delay of 0.
return std::max(base::TimeDelta(), kPulseInterval - Age(last_update, now)); return std::max(base::TimeDelta(),
GetPulseInterval() - Age(last_update, now));
} }
// static // static
bool DeviceInfoUtil::IsActive(const Time last_update, const Time now) { bool DeviceInfoUtil::IsActive(const base::Time last_update,
const base::Time now) {
return Age(last_update, now) < kActiveThreshold; return Age(last_update, now) < kActiveThreshold;
} }
......
...@@ -25,24 +25,24 @@ class DeviceInfoUtil { ...@@ -25,24 +25,24 @@ class DeviceInfoUtil {
// can independently hash the tag to the same value. // can independently hash the tag to the same value.
static const char kClientTagPrefix[]; static const char kClientTagPrefix[];
// The delay between periodic updates to the entry corresponding to this
// device.
static const base::TimeDelta kPulseInterval;
// The amount of time a device can go without an updates before we consider it // The amount of time a device can go without an updates before we consider it
// stale/inactive, and start ignoring it for active device counts. // stale/inactive, and start ignoring it for active device counts.
static const base::TimeDelta kActiveThreshold; static const base::TimeDelta kActiveThreshold;
// The delay between periodic updates to the entry corresponding to the local
// device.
static base::TimeDelta GetPulseInterval();
// Determines the amount of time to wait before pulsing something with the // Determines the amount of time to wait before pulsing something with the
// given |last_update| timestamp. This uses the current time from |now| along // given |last_update| timestamp. This uses the current time from |now| along
// with |kDeviceInfoPulseInterval|, and will never return a negative delay. // with |GetPulseInterval()|, and will never return a negative delay.
// The smallest delay this function will return, even for something extremely // The smallest delay this function will return, even for something extremely
// old will be a delay of 0 time units. // old will be a delay of 0 time units.
static base::TimeDelta CalculatePulseDelay(const base::Time last_update, static base::TimeDelta CalculatePulseDelay(const base::Time last_update,
const base::Time now); const base::Time now);
// Determines if the given |last_update| timestamp should be considerend // Determines if the given |last_update| timestamp should be considered
// active based on |kStaleDeviceInfoThreshold|, given the current time. // active based on |kActiveThreshold|, given the current time.
static bool IsActive(const base::Time last_update, const base::Time now); static bool IsActive(const base::Time last_update, const base::Time now);
// Formats ClientTag from DeviceInfoSpecifics. // Formats ClientTag from DeviceInfoSpecifics.
......
...@@ -22,9 +22,9 @@ class DeviceInfoUtilTest : public testing::Test { ...@@ -22,9 +22,9 @@ class DeviceInfoUtilTest : public testing::Test {
// respectively, // respectively,
// than both constants. // than both constants.
EXPECT_LT(small_, DeviceInfoUtil::kActiveThreshold); EXPECT_LT(small_, DeviceInfoUtil::kActiveThreshold);
EXPECT_LT(small_, DeviceInfoUtil::kPulseInterval); EXPECT_LT(small_, DeviceInfoUtil::GetPulseInterval());
EXPECT_GT(big_, DeviceInfoUtil::kActiveThreshold); EXPECT_GT(big_, DeviceInfoUtil::kActiveThreshold);
EXPECT_GT(big_, DeviceInfoUtil::kPulseInterval); EXPECT_GT(big_, DeviceInfoUtil::GetPulseInterval());
} }
const Time now_ = Time::Now(); const Time now_ = Time::Now();
...@@ -35,44 +35,45 @@ class DeviceInfoUtilTest : public testing::Test { ...@@ -35,44 +35,45 @@ class DeviceInfoUtilTest : public testing::Test {
} // namespace } // namespace
TEST_F(DeviceInfoUtilTest, CalculatePulseDelaySame) { TEST_F(DeviceInfoUtilTest, CalculatePulseDelaySame) {
EXPECT_EQ(DeviceInfoUtil::kPulseInterval, EXPECT_EQ(DeviceInfoUtil::GetPulseInterval(),
DeviceInfoUtil::CalculatePulseDelay(Time(), Time())); DeviceInfoUtil::CalculatePulseDelay(Time(), Time()));
EXPECT_EQ(DeviceInfoUtil::kPulseInterval, EXPECT_EQ(DeviceInfoUtil::GetPulseInterval(),
DeviceInfoUtil::CalculatePulseDelay(now_, now_)); DeviceInfoUtil::CalculatePulseDelay(now_, now_));
EXPECT_EQ(DeviceInfoUtil::kPulseInterval, EXPECT_EQ(DeviceInfoUtil::GetPulseInterval(),
DeviceInfoUtil::CalculatePulseDelay(now_ + big_, now_ + big_)); DeviceInfoUtil::CalculatePulseDelay(now_ + big_, now_ + big_));
} }
TEST_F(DeviceInfoUtilTest, CalculatePulseDelayMiddle) { TEST_F(DeviceInfoUtilTest, CalculatePulseDelayMiddle) {
EXPECT_EQ(DeviceInfoUtil::kPulseInterval - small_, EXPECT_EQ(DeviceInfoUtil::GetPulseInterval() - small_,
DeviceInfoUtil::CalculatePulseDelay(Time(), Time() + small_)); DeviceInfoUtil::CalculatePulseDelay(Time(), Time() + small_));
EXPECT_EQ(small_, EXPECT_EQ(small_,
DeviceInfoUtil::CalculatePulseDelay( DeviceInfoUtil::CalculatePulseDelay(
Time(), Time() + DeviceInfoUtil::kPulseInterval - small_)); Time(), Time() + DeviceInfoUtil::GetPulseInterval() - small_));
} }
TEST_F(DeviceInfoUtilTest, CalculatePulseDelayStale) { TEST_F(DeviceInfoUtilTest, CalculatePulseDelayStale) {
EXPECT_EQ(TimeDelta(), DeviceInfoUtil::CalculatePulseDelay(
Time(), Time() + DeviceInfoUtil::kPulseInterval));
EXPECT_EQ(TimeDelta(), EXPECT_EQ(TimeDelta(),
DeviceInfoUtil::CalculatePulseDelay( DeviceInfoUtil::CalculatePulseDelay(
Time(), Time() + DeviceInfoUtil::kPulseInterval + small_)); Time(), Time() + DeviceInfoUtil::GetPulseInterval()));
EXPECT_EQ(TimeDelta(),
DeviceInfoUtil::CalculatePulseDelay(
Time(), Time() + DeviceInfoUtil::GetPulseInterval() + small_));
EXPECT_EQ(TimeDelta(), EXPECT_EQ(TimeDelta(),
DeviceInfoUtil::CalculatePulseDelay( DeviceInfoUtil::CalculatePulseDelay(
Time(), Time() + DeviceInfoUtil::kPulseInterval + small_)); Time(), Time() + DeviceInfoUtil::GetPulseInterval() + small_));
EXPECT_EQ(TimeDelta(), DeviceInfoUtil::CalculatePulseDelay( EXPECT_EQ(TimeDelta(), DeviceInfoUtil::CalculatePulseDelay(
now_, now_ + DeviceInfoUtil::kPulseInterval)); now_, now_ + DeviceInfoUtil::GetPulseInterval()));
} }
TEST_F(DeviceInfoUtilTest, CalculatePulseDelayFuture) { TEST_F(DeviceInfoUtilTest, CalculatePulseDelayFuture) {
EXPECT_EQ(DeviceInfoUtil::kPulseInterval, EXPECT_EQ(DeviceInfoUtil::GetPulseInterval(),
DeviceInfoUtil::CalculatePulseDelay(Time() + small_, Time())); DeviceInfoUtil::CalculatePulseDelay(Time() + small_, Time()));
EXPECT_EQ(DeviceInfoUtil::kPulseInterval, EXPECT_EQ(DeviceInfoUtil::GetPulseInterval(),
DeviceInfoUtil::CalculatePulseDelay( DeviceInfoUtil::CalculatePulseDelay(
Time() + DeviceInfoUtil::kPulseInterval, Time())); Time() + DeviceInfoUtil::GetPulseInterval(), Time()));
EXPECT_EQ(DeviceInfoUtil::kPulseInterval, EXPECT_EQ(DeviceInfoUtil::GetPulseInterval(),
DeviceInfoUtil::CalculatePulseDelay(Time() + big_, Time())); DeviceInfoUtil::CalculatePulseDelay(Time() + big_, Time()));
EXPECT_EQ(DeviceInfoUtil::kPulseInterval, EXPECT_EQ(DeviceInfoUtil::GetPulseInterval(),
DeviceInfoUtil::CalculatePulseDelay(now_ + big_, now_)); DeviceInfoUtil::CalculatePulseDelay(now_ + big_, now_));
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment