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

Update devices each 1 second until chain is not created #1188

Merged
merged 2 commits into from
Jan 14, 2019

Conversation

AlexeyBarabash
Copy link
Contributor

@AlexeyBarabash AlexeyBarabash commented Dec 26, 2018

Fixes brave/brave-browser#2742 , brave/brave-browser#2782 .

Improves response time on sync creation.

Submitter Checklist:

  • Submitted a ticket for my issue if one did not already exist.
  • Used Github auto-closing keywords in the commit message.
  • Added/updated tests for this change (for new code or code which already has tests).
  • Verified that these changes build without errors on
    • Windows
    • macOS
    • Linux
  • Verified that these changes pass automated tests (npm test brave_unit_tests && npm test brave_browser_tests) on
    • Windows
    • macOS
    • Linux
  • Ran git rebase master (if needed).
  • Ran git rebase -i to squash commits (if needed).
  • Tagged reviewers and labelled the pull request as needed.
  • Request a security/privacy review as needed.
  • Add appropriate QA labels (QA/Yes or QA/No) to include the closed issue in milestone

Test Plan:

  1. On Device1 go to chrome://sync/
  2. Press "Start new chain" => "Computer" => "Copy code"
  3. On device 2 go to chrome://sync/
  4. "Enter sync chain code" button
  5. Paste the code
  6. "Confirm sync code" button
  7. Device1 gets created a sync chain not so late (<5 sec) after Device2

Reviewer Checklist:

  • New files have MPL-2.0 license header.
  • Request a security/privacy review as needed.
  • Adequate test coverage exists to prevent regressions
  • Verify test plan is specified in PR before merging to source

Copy link
Member

@darkdh darkdh left a comment

Choose a reason for hiding this comment

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

doesn't this cause two LoopProc running with different interval simultaneously?

components/brave_sync/brave_sync_service_impl.cc Outdated Show resolved Hide resolved
components/brave_sync/brave_sync_service_impl.cc Outdated Show resolved Hide resolved
components/brave_sync/brave_sync_service_impl.cc Outdated Show resolved Hide resolved
components/brave_sync/brave_sync_service_impl.cc Outdated Show resolved Hide resolved
Copy link
Member

@darkdh darkdh left a comment

Choose a reason for hiding this comment

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

@AlexeyBarabash
Copy link
Contributor Author

Pushed the fix. Will squash and rebase after approval.

@@ -463,6 +464,7 @@ void BraveSyncServiceImpl::OnResolvedPreferences(const RecordsList& records) {
bool contains_only_one_device = false;

auto sync_devices = sync_prefs_->GetSyncDevices();
bool waiting_for_second_device = sync_devices->size() < 2;
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you add a method for this? There are two checks that both check the same condition, but with different values <= 1 vs < 2 and it's confusing to read like that

@bridiver
Copy link
Collaborator

@AlexeyBarabash can you please add a test for this?

@AlexeyBarabash
Copy link
Contributor Author

@darkdh @bridiver
Thanks for the review.
Added a test and fixed lint issues.
Tried to run npm run lint but then understand brave/brave-browser#2743 is not merged yet. In any way looking forward to start use it.

@AlexeyBarabash
Copy link
Contributor Author

Pushed a fix for #1188 (comment) comment .

@AlexeyBarabash
Copy link
Contributor Author

Rebased and squashed commits.

@@ -484,7 +487,7 @@ void BraveSyncServiceImpl::OnResolvedPreferences(const RecordsList& records) {
(record->deviceId == this_device_id &&
record->action == jslib::SyncRecord::Action::A_DELETE &&
actually_merged);
contains_only_one_device = sync_devices->size() < 2 &&
contains_only_one_device = sync_devices->have_less_than_two() &&
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm confused about this, if there's only one device and it has been deleted, then there are no devices, not one device

@@ -496,6 +499,11 @@ void BraveSyncServiceImpl::OnResolvedPreferences(const RecordsList& records) {
ResetSyncInternal();
if (contains_only_one_device)
OnResetSync();

if (waiting_for_second_device && !sync_devices->have_less_than_two()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't understand why we need this second check and the extra var. Why do we care what the previous size of the sync_devices was? It's also possible that both this_device_deleted and contains_only_one_device can both be true. The logic here just doesn't make sense to me

Copy link
Collaborator

@bridiver bridiver Jan 2, 2019

Choose a reason for hiding this comment

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

couldn't we just do something like this?

diff --git a/components/brave_sync/brave_sync_service_impl.cc b/components/brave_sync/brave_sync_service_impl.cc
index cb76609e..09fb02e7 100644
--- a/components/brave_sync/brave_sync_service_impl.cc
+++ b/components/brave_sync/brave_sync_service_impl.cc
@@ -464,9 +464,7 @@ BraveSyncServiceImpl::PrepareResolvedPreferences(const RecordsList& records) {
 }
 
 void BraveSyncServiceImpl::OnResolvedPreferences(const RecordsList& records) {
-  const std::string this_device_id = sync_prefs_->GetThisDeviceId();
   bool this_device_deleted = false;
-  bool contains_only_one_device = false;
 
   auto sync_devices = sync_prefs_->GetSyncDevices();
   for (const auto &record : records) {
@@ -480,22 +478,21 @@ void BraveSyncServiceImpl::OnResolvedPreferences(const RecordsList& records) {
           record->syncTimestamp.ToJsTime()),
           record->action,
           &actually_merged);
-      this_device_deleted = this_device_deleted ||
-        (record->deviceId == this_device_id &&
+      this_device_deleted =
+          record->deviceId == sync_prefs_->GetThisDeviceId() &&
           record->action == jslib::SyncRecord::Action::A_DELETE &&
-          actually_merged);
-      contains_only_one_device = sync_devices->size() < 2 &&
-        record->action == jslib::SyncRecord::Action::A_DELETE &&
           actually_merged;
+      }
     }
   } // for each device
 
   sync_prefs_->SetSyncDevices(*sync_devices);
 
-  if (this_device_deleted)
+  if (this_device_deleted || sync_devices->size() == 0) {
     ResetSyncInternal();
-  if (contains_only_one_device)
-    OnResetSync();
+  } else if (sync_devices->size() == 1) {
+    StartLoop(true);
+  }
 }
 
 void BraveSyncServiceImpl::OnSyncPrefsChanged(const std::string& pref) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm confused about this, if there's only one device and it has been deleted, then there are no devices, not one device

Yes this is a naming issue.

I don't understand why we need this second check and the extra var. Why do we care what the previous size of the sync_devices was?

Because we want to set update interval from 1 to 60 sec if devices size was changed from (0|1) to (2|3|...); and we dont want to touch update interval when devices list was changed from in other cases, like (2|3|4 => 3|4|5) or (3|4|5 => 2|3|4).

The idea is: we cannot have less than two devices and if we have less than two devices we should fetch devices list 1 time/sec to improve the response time.

It's also possible that both this_device_deleted and contains_only_one_device can both be true. The logic here just doesn't make sense to me

There are three things which does BraveSyncServiceImpl::OnResolvedPreferences:

  1. We received this device is being deleted, someone on other device in chain pressed the cross near this device.
    We mark preferences to set sync state to not enabled.

  2. When devices list size gets to 1, this means sync chain is no longer exists about to be destroyed. We also mark preferences as sync not enabled.

ResetSyncInternal() and OnResetSync() do the same if (sync_devices->size() <= 1).

  1. set fetch sync records interval from 1 sec to 60 sec when sync chain created, devices size was changed from (0|1) to (2|3|...).

Modifying.


void BraveSyncServiceImpl::StartLoop() {
void BraveSyncServiceImpl::StartLoop(const bool waiting_for_second_device) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe use_initial_update_interval?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agree, sounds more self-explaining.

@AlexeyBarabash
Copy link
Contributor Author

Pushed fixes.

@AlexeyBarabash
Copy link
Contributor Author

Rebased to current master.

darkdh
darkdh previously approved these changes Jan 14, 2019
Copy link
Member

@darkdh darkdh left a comment

Choose a reason for hiding this comment

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

lgtm

@@ -43,6 +43,7 @@ class SyncDevices {
std::unique_ptr<base::Value> ToValueArrOnly() const;
std::string ToJson() const;
size_t size() const { return devices_.size(); }
bool have_less_than_two() const { return size() < 2; }
Copy link
Collaborator

Choose a reason for hiding this comment

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

just a nit, but can we make this has_second_device and reverse the logic? I think the purpose would be more intuitive and have_less_than_two just sounds weird to me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modified this.

@AlexeyBarabash
Copy link
Contributor Author

Put the fix according to the last notice, squashed fix commits and force pushed the branch.
So I need again your approval @bridiver @darkdh .

@AlexeyBarabash AlexeyBarabash merged commit f005c59 into master Jan 14, 2019
AlexeyBarabash added a commit that referenced this pull request Jan 14, 2019
Update devices each 1 second until chain is not created
AlexeyBarabash added a commit that referenced this pull request Jan 14, 2019
Update devices each 1 second until chain is not created
@bsclifton bsclifton deleted the sync_frequent_initial_update branch January 24, 2019 21:15
darkdh added a commit that referenced this pull request Feb 1, 2019
darkdh added a commit that referenced this pull request Feb 1, 2019
Revert "Merge pull request #1188 from brave/sync_frequent_initial_update"
darkdh added a commit that referenced this pull request Feb 1, 2019
Revert "Merge pull request #1188 from brave/sync_frequent_initial_update"
darkdh added a commit that referenced this pull request Feb 1, 2019
Revert "Merge pull request #1188 from brave/sync_frequent_initial_update"
darkdh added a commit that referenced this pull request Feb 1, 2019
Revert "Merge pull request #1188 from brave/sync_frequent_initial_update"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Speedup sync chain creation time
3 participants