OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync/engine/syncer_thread.h" | 5 #include "chrome/browser/sync/engine/syncer_thread.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 #include <queue> | 9 #include <queue> |
10 #include <string> | |
11 #include <vector> | |
10 | 12 |
11 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
12 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 14 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
13 #include "build/build_config.h" | 15 #include "build/build_config.h" |
14 #include "chrome/browser/sync/engine/model_safe_worker.h" | 16 #include "chrome/browser/sync/engine/model_safe_worker.h" |
15 #include "chrome/browser/sync/engine/net/server_connection_manager.h" | 17 #include "chrome/browser/sync/engine/net/server_connection_manager.h" |
16 #include "chrome/browser/sync/engine/syncer.h" | 18 #include "chrome/browser/sync/engine/syncer.h" |
17 #include "chrome/browser/sync/sessions/sync_session.h" | 19 #include "chrome/browser/sync/sessions/sync_session.h" |
18 #include "jingle/notifier/listener/notification_constants.h" | 20 #include "jingle/notifier/listener/notification_constants.h" |
19 | 21 |
(...skipping 27 matching lines...) Expand all Loading... | |
47 // we want and allows stronger control over the poll rate from the server. We | 49 // we want and allows stronger control over the poll rate from the server. We |
48 // should probably re-visit this code later and figure out if user idle time | 50 // should probably re-visit this code later and figure out if user idle time |
49 // is really something we want and make sure it works, if it is. | 51 // is really something we want and make sure it works, if it is. |
50 const int SyncerThread::kDefaultMaxPollIntervalMs = 30 * 60 * 1000; | 52 const int SyncerThread::kDefaultMaxPollIntervalMs = 30 * 60 * 1000; |
51 | 53 |
52 // Backoff interval randomization factor. | 54 // Backoff interval randomization factor. |
53 static const int kBackoffRandomizationFactor = 2; | 55 static const int kBackoffRandomizationFactor = 2; |
54 | 56 |
55 const int SyncerThread::kMaxBackoffSeconds = 60 * 60 * 4; // 4 hours. | 57 const int SyncerThread::kMaxBackoffSeconds = 60 * 60 * 4; // 4 hours. |
56 | 58 |
59 void SyncerThread::NudgeSyncerWithPayloads( | |
60 int milliseconds_from_now, | |
61 NudgeSource source, | |
62 const std::vector<std::string>& payloads) { | |
63 base::AutoLock lock(lock_); | |
64 if (vault_.syncer_ == NULL) { | |
65 return; | |
66 } | |
67 | |
68 syncable::ModelTypeBitSet model_types; // All false by default. | |
69 NudgeSyncImpl(milliseconds_from_now, source, model_types, payloads); | |
70 } | |
71 | |
57 void SyncerThread::NudgeSyncerWithDataTypes( | 72 void SyncerThread::NudgeSyncerWithDataTypes( |
58 int milliseconds_from_now, | 73 int milliseconds_from_now, |
59 NudgeSource source, | 74 NudgeSource source, |
60 const syncable::ModelTypeBitSet& model_types) { | 75 const syncable::ModelTypeBitSet& model_types) { |
61 base::AutoLock lock(lock_); | 76 base::AutoLock lock(lock_); |
62 if (vault_.syncer_ == NULL) { | 77 if (vault_.syncer_ == NULL) { |
63 return; | 78 return; |
64 } | 79 } |
65 | 80 |
66 NudgeSyncImpl(milliseconds_from_now, source, model_types); | 81 std::vector<std::string> empty_payload; |
82 NudgeSyncImpl(milliseconds_from_now, source, model_types, empty_payload); | |
67 } | 83 } |
68 | 84 |
69 void SyncerThread::NudgeSyncer( | 85 void SyncerThread::NudgeSyncer( |
70 int milliseconds_from_now, | 86 int milliseconds_from_now, |
71 NudgeSource source) { | 87 NudgeSource source) { |
72 base::AutoLock lock(lock_); | 88 base::AutoLock lock(lock_); |
73 if (vault_.syncer_ == NULL) { | 89 if (vault_.syncer_ == NULL) { |
74 return; | 90 return; |
75 } | 91 } |
76 | 92 |
77 syncable::ModelTypeBitSet model_types; // All false by default. | 93 syncable::ModelTypeBitSet model_types; |
78 NudgeSyncImpl(milliseconds_from_now, source, model_types); | 94 model_types.set(); // The default nudge acts on all datatypes. |
95 std::vector<std::string> empty_payload; // No payloads. | |
96 NudgeSyncImpl(milliseconds_from_now, source, model_types, empty_payload); | |
79 } | 97 } |
80 | 98 |
81 SyncerThread::SyncerThread(sessions::SyncSessionContext* context) | 99 SyncerThread::SyncerThread(sessions::SyncSessionContext* context) |
82 : thread_main_started_(false, false), | 100 : thread_main_started_(false, false), |
83 thread_("SyncEngine_SyncerThread"), | 101 thread_("SyncEngine_SyncerThread"), |
84 vault_field_changed_(&lock_), | 102 vault_field_changed_(&lock_), |
85 conn_mgr_hookup_(NULL), | 103 conn_mgr_hookup_(NULL), |
86 syncer_short_poll_interval_seconds_(kDefaultShortPollIntervalSeconds), | 104 syncer_short_poll_interval_seconds_(kDefaultShortPollIntervalSeconds), |
87 syncer_long_poll_interval_seconds_(kDefaultLongPollIntervalSeconds), | 105 syncer_long_poll_interval_seconds_(kDefaultLongPollIntervalSeconds), |
88 syncer_polling_interval_(kDefaultShortPollIntervalSeconds), | 106 syncer_polling_interval_(kDefaultShortPollIntervalSeconds), |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
291 } | 309 } |
292 | 310 |
293 // Check if we should be paused or if a pause was requested. Note | 311 // Check if we should be paused or if a pause was requested. Note |
294 // that we don't check initial_sync_for_thread here since we want | 312 // that we don't check initial_sync_for_thread here since we want |
295 // the pause to happen regardless if it is the initial sync or not. | 313 // the pause to happen regardless if it is the initial sync or not. |
296 if (vault_.pause_requested_ || vault_.paused_) { | 314 if (vault_.pause_requested_ || vault_.paused_) { |
297 PauseUntilResumedOrQuit(); | 315 PauseUntilResumedOrQuit(); |
298 continue; | 316 continue; |
299 } | 317 } |
300 | 318 |
301 const TimeTicks next_poll = last_sync_time + | 319 const TimeTicks next_poll = last_sync_time + |
akalin
2011/01/12 09:55:19
can't last_sync_time be null here to? If this is
Nicolas Zea
2011/01/13 19:17:30
Filed bug 69558 regarding last_sync_time and PostT
| |
302 vault_.current_wait_interval_.poll_delta; | 320 vault_.current_wait_interval_.poll_delta; |
303 bool throttled = vault_.current_wait_interval_.mode == | 321 bool throttled = vault_.current_wait_interval_.mode == |
304 WaitInterval::THROTTLED; | 322 WaitInterval::THROTTLED; |
305 // If we are throttled, we must wait. Otherwise, wait until either the next | 323 // If we are throttled, we must wait. Otherwise, wait until either the next |
306 // nudge (if one exists) or the poll interval. | 324 // nudge (if one exists) or the poll interval. |
307 TimeTicks end_wait = next_poll; | 325 TimeTicks end_wait = next_poll; |
308 if (!throttled && !vault_.pending_nudge_time_.is_null()) { | 326 if (!throttled && !vault_.pending_nudge_time_.is_null()) { |
309 end_wait = std::min(end_wait, vault_.pending_nudge_time_); | 327 end_wait = std::min(end_wait, vault_.pending_nudge_time_); |
310 } | 328 } |
311 VLOG(1) << "end_wait is " << end_wait.ToInternalValue() | 329 VLOG(1) << "end_wait is " << end_wait.ToInternalValue() |
(...skipping 23 matching lines...) Expand all Loading... | |
335 // event. This will also update the source of the following SyncMain call. | 353 // event. This will also update the source of the following SyncMain call. |
336 VLOG(1) << "Calling Sync Main at time " << Time::Now().ToInternalValue(); | 354 VLOG(1) << "Calling Sync Main at time " << Time::Now().ToInternalValue(); |
337 bool nudged = false; | 355 bool nudged = false; |
338 scoped_ptr<SyncSession> session; | 356 scoped_ptr<SyncSession> session; |
339 session.reset(SyncMain(vault_.syncer_, | 357 session.reset(SyncMain(vault_.syncer_, |
340 throttled, continue_sync_cycle, &initial_sync_for_thread, &nudged)); | 358 throttled, continue_sync_cycle, &initial_sync_for_thread, &nudged)); |
341 | 359 |
342 // Update timing information for how often these datatypes are triggering | 360 // Update timing information for how often these datatypes are triggering |
343 // nudges. | 361 // nudges. |
344 base::TimeTicks now = TimeTicks::Now(); | 362 base::TimeTicks now = TimeTicks::Now(); |
345 for (size_t i = syncable::FIRST_REAL_MODEL_TYPE; | 363 if (!last_sync_time.is_null()) { |
346 i < session->source().second.size(); | 364 for (size_t i = syncable::FIRST_REAL_MODEL_TYPE; |
347 ++i) { | 365 i < session->source().types().size(); |
348 if (session->source().second[i]) { | 366 ++i) { |
349 syncable::PostTimeToTypeHistogram(syncable::ModelType(i), | 367 if (session->source().types()[i]) { |
350 now - last_sync_time); | 368 syncable::PostTimeToTypeHistogram(syncable::ModelType(i), |
369 now - last_sync_time); | |
370 } | |
351 } | 371 } |
352 } | 372 } |
353 | 373 |
354 last_sync_time = now; | 374 last_sync_time = now; |
355 | 375 |
356 VLOG(1) << "Updating the next polling time after SyncMain"; | 376 VLOG(1) << "Updating the next polling time after SyncMain"; |
357 vault_.current_wait_interval_ = CalculatePollingWaitTime( | 377 vault_.current_wait_interval_ = CalculatePollingWaitTime( |
358 static_cast<int>(vault_.current_wait_interval_.poll_delta.InSeconds()), | 378 static_cast<int>(vault_.current_wait_interval_.poll_delta.InSeconds()), |
359 &user_idle_milliseconds, &continue_sync_cycle, nudged); | 379 &user_idle_milliseconds, &continue_sync_cycle, nudged); |
360 } | 380 } |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
568 return session.release(); | 588 return session.release(); |
569 } | 589 } |
570 | 590 |
571 SyncSourceInfo SyncerThread::GetAndResetNudgeSource(bool was_throttled, | 591 SyncSourceInfo SyncerThread::GetAndResetNudgeSource(bool was_throttled, |
572 bool continue_sync_cycle, | 592 bool continue_sync_cycle, |
573 bool* initial_sync, | 593 bool* initial_sync, |
574 bool* was_nudged) { | 594 bool* was_nudged) { |
575 bool nudged = false; | 595 bool nudged = false; |
576 NudgeSource nudge_source = kUnknown; | 596 NudgeSource nudge_source = kUnknown; |
577 syncable::ModelTypeBitSet model_types; | 597 syncable::ModelTypeBitSet model_types; |
598 std::vector<std::string> payloads; | |
578 // Has the previous sync cycle completed? | 599 // Has the previous sync cycle completed? |
579 if (continue_sync_cycle) | 600 if (continue_sync_cycle) |
580 nudge_source = kContinuation; | 601 nudge_source = kContinuation; |
581 // Update the nudge source if a new nudge has come through during the | 602 // Update the nudge source if a new nudge has come through during the |
582 // previous sync cycle. | 603 // previous sync cycle. |
583 if (!vault_.pending_nudge_time_.is_null()) { | 604 if (!vault_.pending_nudge_time_.is_null()) { |
584 if (!was_throttled) { | 605 if (!was_throttled) { |
585 nudge_source = vault_.pending_nudge_source_; | 606 nudge_source = vault_.pending_nudge_source_; |
586 model_types = vault_.pending_nudge_types_; | 607 model_types = vault_.pending_nudge_types_; |
608 payloads = vault_.datatype_payloads_; | |
587 nudged = true; | 609 nudged = true; |
588 } | 610 } |
589 VLOG(1) << "Clearing pending nudge from " << vault_.pending_nudge_source_ | 611 VLOG(1) << "Clearing pending nudge from " << vault_.pending_nudge_source_ |
590 << " at tick " << vault_.pending_nudge_time_.ToInternalValue(); | 612 << " at tick " << vault_.pending_nudge_time_.ToInternalValue(); |
591 vault_.pending_nudge_source_ = kUnknown; | 613 vault_.pending_nudge_source_ = kUnknown; |
592 vault_.pending_nudge_types_.reset(); | 614 vault_.pending_nudge_types_.reset(); |
615 vault_.datatype_payloads_.clear(); | |
593 vault_.pending_nudge_time_ = base::TimeTicks(); | 616 vault_.pending_nudge_time_ = base::TimeTicks(); |
594 } | 617 } |
595 | 618 |
596 *was_nudged = nudged; | 619 *was_nudged = nudged; |
597 | 620 |
598 // TODO(tim): Hack for bug 64136 to correctly tag continuations that result | 621 // TODO(tim): Hack for bug 64136 to correctly tag continuations that result |
599 // from syncer having more work to do. This will be handled properly with | 622 // from syncer having more work to do. This will be handled properly with |
600 // the message loop based syncer thread, bug 26339. | 623 // the message loop based syncer thread, bug 26339. |
601 return MakeSyncSourceInfo(nudged || nudge_source == kContinuation, | 624 return MakeSyncSourceInfo(nudged || nudge_source == kContinuation, |
602 nudge_source, model_types, initial_sync); | 625 nudge_source, model_types, payloads, initial_sync); |
603 } | 626 } |
604 | 627 |
605 SyncSourceInfo SyncerThread::MakeSyncSourceInfo(bool nudged, | 628 SyncSourceInfo SyncerThread::MakeSyncSourceInfo(bool nudged, |
606 NudgeSource nudge_source, const syncable::ModelTypeBitSet& nudge_types, | 629 NudgeSource nudge_source, const syncable::ModelTypeBitSet& nudge_types, |
607 bool* initial_sync) { | 630 const std::vector<std::string>& payloads, bool* initial_sync) { |
608 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource updates_source = | 631 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource updates_source = |
609 sync_pb::GetUpdatesCallerInfo::UNKNOWN; | 632 sync_pb::GetUpdatesCallerInfo::UNKNOWN; |
610 if (*initial_sync) { | 633 if (*initial_sync) { |
611 updates_source = sync_pb::GetUpdatesCallerInfo::FIRST_UPDATE; | 634 updates_source = sync_pb::GetUpdatesCallerInfo::FIRST_UPDATE; |
612 *initial_sync = false; | 635 *initial_sync = false; |
613 } else if (!nudged) { | 636 } else if (!nudged) { |
614 updates_source = sync_pb::GetUpdatesCallerInfo::PERIODIC; | 637 updates_source = sync_pb::GetUpdatesCallerInfo::PERIODIC; |
615 } else { | 638 } else { |
616 switch (nudge_source) { | 639 switch (nudge_source) { |
617 case kNotification: | 640 case kNotification: |
618 updates_source = sync_pb::GetUpdatesCallerInfo::NOTIFICATION; | 641 updates_source = sync_pb::GetUpdatesCallerInfo::NOTIFICATION; |
619 break; | 642 break; |
620 case kLocal: | 643 case kLocal: |
621 updates_source = sync_pb::GetUpdatesCallerInfo::LOCAL; | 644 updates_source = sync_pb::GetUpdatesCallerInfo::LOCAL; |
622 break; | 645 break; |
623 case kContinuation: | 646 case kContinuation: |
624 updates_source = sync_pb::GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION; | 647 updates_source = sync_pb::GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION; |
625 break; | 648 break; |
626 case kClearPrivateData: | 649 case kClearPrivateData: |
627 updates_source = sync_pb::GetUpdatesCallerInfo::CLEAR_PRIVATE_DATA; | 650 updates_source = sync_pb::GetUpdatesCallerInfo::CLEAR_PRIVATE_DATA; |
628 break; | 651 break; |
629 case kUnknown: | 652 case kUnknown: |
630 default: | 653 default: |
631 updates_source = sync_pb::GetUpdatesCallerInfo::UNKNOWN; | 654 updates_source = sync_pb::GetUpdatesCallerInfo::UNKNOWN; |
632 break; | 655 break; |
633 } | 656 } |
634 } | 657 } |
635 return SyncSourceInfo(updates_source, nudge_types); | 658 |
659 syncable::ModelTypeBitSet sync_source_types; | |
660 if (nudge_types.none()) { | |
661 // No datatypes requested. This must be a poll so set all enabled datatypes. | |
662 DCHECK(payloads.empty()); // There should be no payloads | |
663 ModelSafeRoutingInfo routes; | |
664 session_context_->registrar()->GetModelSafeRoutingInfo(&routes); | |
665 for (ModelSafeRoutingInfo::const_iterator i = routes.begin(); | |
666 i != routes.end(); ++i) { | |
667 sync_source_types.set(i->first); | |
668 } | |
669 | |
670 } else { | |
671 sync_source_types = nudge_types; | |
672 } | |
673 | |
674 return SyncSourceInfo(updates_source, sync_source_types, payloads); | |
636 } | 675 } |
637 | 676 |
638 void SyncerThread::CreateSyncer(const std::string& dirname) { | 677 void SyncerThread::CreateSyncer(const std::string& dirname) { |
639 base::AutoLock lock(lock_); | 678 base::AutoLock lock(lock_); |
640 VLOG(1) << "Creating syncer up for: " << dirname; | 679 VLOG(1) << "Creating syncer up for: " << dirname; |
641 // The underlying database structure is ready, and we should create | 680 // The underlying database structure is ready, and we should create |
642 // the syncer. | 681 // the syncer. |
643 CHECK(vault_.syncer_ == NULL); | 682 CHECK(vault_.syncer_ == NULL); |
644 session_context_->set_account_name(dirname); | 683 session_context_->set_account_name(dirname); |
645 vault_.syncer_ = new Syncer(); | 684 vault_.syncer_ = new Syncer(); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
729 next_wait = std::min(GetRecommendedDelaySeconds( | 768 next_wait = std::min(GetRecommendedDelaySeconds( |
730 last_interval / 1000), syncer_max_interval_ / 1000) * 1000; | 769 last_interval / 1000), syncer_max_interval_ / 1000) * 1000; |
731 } | 770 } |
732 | 771 |
733 return next_wait; | 772 return next_wait; |
734 } | 773 } |
735 | 774 |
736 // Called with mutex_ already locked. | 775 // Called with mutex_ already locked. |
737 void SyncerThread::NudgeSyncImpl(int milliseconds_from_now, | 776 void SyncerThread::NudgeSyncImpl(int milliseconds_from_now, |
738 NudgeSource source, | 777 NudgeSource source, |
739 const syncable::ModelTypeBitSet& model_types) { | 778 const syncable::ModelTypeBitSet& model_types, |
779 const std::vector<std::string>& payloads) { | |
740 // TODO(sync): Add the option to reset the backoff state machine. | 780 // TODO(sync): Add the option to reset the backoff state machine. |
741 // This is needed so nudges that are a result of the user's desire | 781 // This is needed so nudges that are a result of the user's desire |
742 // to download updates for a new data type can be satisfied quickly. | 782 // to download updates for a new data type can be satisfied quickly. |
743 if (vault_.current_wait_interval_.mode == WaitInterval::THROTTLED || | 783 if (vault_.current_wait_interval_.mode == WaitInterval::THROTTLED || |
744 vault_.current_wait_interval_.had_nudge_during_backoff) { | 784 vault_.current_wait_interval_.had_nudge_during_backoff) { |
745 // Drop nudges on the floor if we've already had one since starting this | 785 // Drop nudges on the floor if we've already had one since starting this |
746 // stage of exponential backoff or we are throttled. | 786 // stage of exponential backoff or we are throttled. |
747 return; | 787 return; |
748 } | 788 } |
749 | 789 |
750 // Union the current bitset with any from nudges that may have already | 790 // Union the current bitset with any from nudges that may have already |
751 // posted (coalesce the nudge datatype information). | 791 // posted (coalesce the nudge datatype information). |
752 // TODO(tim): It seems weird to do this if the sources don't match up (e.g. | 792 // TODO(tim): It seems weird to do this if the sources don't match up (e.g. |
753 // if pending_source is kLocal and |source| is kClearPrivateData). | 793 // if pending_source is kLocal and |source| is kClearPrivateData). |
754 vault_.pending_nudge_types_ |= model_types; | 794 vault_.pending_nudge_types_ |= model_types; |
755 | 795 |
796 // Any nudge results in the payloads vector at least having an empty string | |
797 // for each datatype. Any datatype that does actually have a payload signifies | |
798 // that we are requesting that datatype (and hence the corresponding | |
799 // |pending_nudge_types_| slot should be set). | |
800 vault_.datatype_payloads_.resize(syncable::MODEL_TYPE_COUNT); | |
801 for (size_t i = syncable::FIRST_REAL_MODEL_TYPE; | |
802 i < payloads.size(); | |
803 ++i) { | |
804 if (!payloads[i].empty()) { | |
805 // This datatype has a payload we should hold on to. | |
806 vault_.pending_nudge_types_.set(i); | |
807 vault_.datatype_payloads_[i] = payloads[i]; // Overwrite old payloads. | |
akalin
2011/01/12 09:55:19
why can we get away with overwriting? Don't we ne
Nicolas Zea
2011/01/13 19:17:30
We merge across datatypes, but if we receive a pay
akalin
2011/01/13 19:47:41
Okay, if that's the case, maybe expand the comment
Nicolas Zea
2011/01/18 06:07:23
Done.
| |
808 } | |
809 } | |
810 | |
756 const TimeTicks nudge_time = TimeTicks::Now() + | 811 const TimeTicks nudge_time = TimeTicks::Now() + |
757 TimeDelta::FromMilliseconds(milliseconds_from_now); | 812 TimeDelta::FromMilliseconds(milliseconds_from_now); |
758 if (nudge_time <= vault_.pending_nudge_time_) { | 813 if (nudge_time <= vault_.pending_nudge_time_) { |
759 VLOG(1) << "Nudge for source " << source | 814 VLOG(1) << "Nudge for source " << source |
760 << " dropped due to existing later pending nudge"; | 815 << " dropped due to existing later pending nudge"; |
761 return; | 816 return; |
762 } | 817 } |
763 | 818 |
764 VLOG(1) << "Replacing pending nudge for source " << source | 819 VLOG(1) << "Replacing pending nudge for source " << source |
765 << " at " << nudge_time.ToInternalValue(); | 820 << " at " << nudge_time.ToInternalValue(); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
842 was_logged = true; | 897 was_logged = true; |
843 VLOG(1) << "UserIdleTime unimplemented on this platform, synchronization " | 898 VLOG(1) << "UserIdleTime unimplemented on this platform, synchronization " |
844 "will not throttle when user idle"; | 899 "will not throttle when user idle"; |
845 } | 900 } |
846 #endif | 901 #endif |
847 | 902 |
848 return 0; | 903 return 0; |
849 } | 904 } |
850 | 905 |
851 } // namespace browser_sync | 906 } // namespace browser_sync |
OLD | NEW |