[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
maxbogue | 4776274 | 2016-03-01 00:56:18 | [diff] [blame] | 5 | #include "sync/engine/worker_entity_tracker.h" |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 6 | |
avi | 4856ced0 | 2015-12-22 03:14:10 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 10 | #include "sync/internal_api/public/base/model_type.h" |
[email protected] | 3e659774 | 2014-07-28 21:59:54 | [diff] [blame] | 11 | #include "sync/internal_api/public/non_blocking_sync_common.h" |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 12 | #include "sync/syncable/syncable_util.h" |
| 13 | #include "sync/util/time.h" |
| 14 | |
stanisc | 96665bd | 2015-08-14 18:59:14 | [diff] [blame] | 15 | namespace syncer_v2 { |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 16 | |
maxbogue | 4b212e6 | 2016-03-21 22:09:08 | [diff] [blame] | 17 | WorkerEntityTracker::WorkerEntityTracker(const std::string& id, |
| 18 | const std::string& client_tag_hash) |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 19 | : id_(id), |
| 20 | client_tag_hash_(client_tag_hash), |
maxbogue | 4b212e6 | 2016-03-21 22:09:08 | [diff] [blame] | 21 | highest_commit_response_version_(0), |
| 22 | highest_gu_response_version_(0), |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 23 | sequence_number_(0), |
maxbogue | 4b212e6 | 2016-03-21 22:09:08 | [diff] [blame] | 24 | base_version_(kUncommittedVersion) { |
| 25 | DCHECK(!client_tag_hash_.empty()); |
| 26 | } |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 27 | |
maxbogue | 4776274 | 2016-03-01 00:56:18 | [diff] [blame] | 28 | WorkerEntityTracker::~WorkerEntityTracker() {} |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 29 | |
maxbogue | 4776274 | 2016-03-01 00:56:18 | [diff] [blame] | 30 | bool WorkerEntityTracker::HasPendingCommit() const { |
maxbogue | 21e465ee3 | 2015-09-16 20:50:33 | [diff] [blame] | 31 | return !!pending_commit_; |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 32 | } |
| 33 | |
pavely | 4c454e2 | 2016-04-18 18:58:32 | [diff] [blame] | 34 | void WorkerEntityTracker::PopulateCommitProto( |
| 35 | sync_pb::SyncEntity* commit_entity, |
| 36 | int64_t* sequence_number) const { |
maxbogue | 21e465ee3 | 2015-09-16 20:50:33 | [diff] [blame] | 37 | DCHECK(HasPendingCommit()); |
stanisc | f3e705f | 2015-11-18 02:18:58 | [diff] [blame] | 38 | DCHECK(!client_tag_hash_.empty()); |
maxbogue | 21e465ee3 | 2015-09-16 20:50:33 | [diff] [blame] | 39 | |
stanisc | f3e705f | 2015-11-18 02:18:58 | [diff] [blame] | 40 | if (!id_.empty()) { |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 41 | commit_entity->set_id_string(id_); |
| 42 | } |
| 43 | |
stanisc | f3e705f | 2015-11-18 02:18:58 | [diff] [blame] | 44 | const EntityData& entity = pending_commit_->entity.value(); |
| 45 | DCHECK_EQ(client_tag_hash_, entity.client_tag_hash); |
| 46 | |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 47 | commit_entity->set_client_defined_unique_tag(client_tag_hash_); |
| 48 | commit_entity->set_version(base_version_); |
stanisc | f3e705f | 2015-11-18 02:18:58 | [diff] [blame] | 49 | commit_entity->set_deleted(entity.is_deleted()); |
| 50 | |
| 51 | // TODO(stanisc): This doesn't support bookmarks yet. |
| 52 | DCHECK(entity.parent_id.empty()); |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 53 | commit_entity->set_folder(false); |
stanisc | f3e705f | 2015-11-18 02:18:58 | [diff] [blame] | 54 | |
| 55 | commit_entity->set_name(entity.non_unique_name); |
| 56 | if (!entity.is_deleted()) { |
| 57 | commit_entity->set_ctime(syncer::TimeToProtoTime(entity.creation_time)); |
| 58 | commit_entity->set_mtime(syncer::TimeToProtoTime(entity.modification_time)); |
| 59 | commit_entity->mutable_specifics()->CopyFrom(entity.specifics); |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | *sequence_number = sequence_number_; |
| 63 | } |
| 64 | |
maxbogue | 4776274 | 2016-03-01 00:56:18 | [diff] [blame] | 65 | void WorkerEntityTracker::RequestCommit(const CommitRequestData& data) { |
maxbogue | 21e465ee3 | 2015-09-16 20:50:33 | [diff] [blame] | 66 | DCHECK_GE(data.base_version, base_version_) |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 67 | << "Base version should never decrease"; |
| 68 | |
maxbogue | 21e465ee3 | 2015-09-16 20:50:33 | [diff] [blame] | 69 | DCHECK_GE(data.sequence_number, sequence_number_) |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 70 | << "Sequence number should never decrease"; |
| 71 | |
| 72 | // Update our book-keeping counters. |
maxbogue | 21e465ee3 | 2015-09-16 20:50:33 | [diff] [blame] | 73 | base_version_ = data.base_version; |
| 74 | sequence_number_ = data.sequence_number; |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 75 | |
maxbogue | 21e465ee3 | 2015-09-16 20:50:33 | [diff] [blame] | 76 | // Don't commit deletions of server-unknown items. |
stanisc | f3e705f | 2015-11-18 02:18:58 | [diff] [blame] | 77 | if (data.entity->is_deleted() && !IsServerKnown()) { |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 78 | ClearPendingCommit(); |
| 79 | return; |
| 80 | } |
| 81 | |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 82 | // We intentionally don't update the id_ here. Good ID values come from the |
| 83 | // server and always pass through the sync thread first. There's no way the |
| 84 | // model thread could have a better ID value than we do. |
| 85 | |
| 86 | // This entity is identified by its client tag. That value can never change. |
stanisc | f3e705f | 2015-11-18 02:18:58 | [diff] [blame] | 87 | DCHECK_EQ(client_tag_hash_, data.entity->client_tag_hash); |
stanisc | 29a9029 | 2015-12-01 19:29:00 | [diff] [blame] | 88 | // TODO(stanisc): consider simply copying CommitRequestData instead of |
| 89 | // allocating one dynamically. |
maxbogue | 21e465ee3 | 2015-09-16 20:50:33 | [diff] [blame] | 90 | pending_commit_.reset(new CommitRequestData(data)); |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 91 | |
maxbogue | 21e465ee3 | 2015-09-16 20:50:33 | [diff] [blame] | 92 | // Do our counter values indicate a conflict? If so, don't commit. |
| 93 | // |
| 94 | // There's no need to inform the model thread of the conflict. The |
| 95 | // conflicting update has already been posted to its task runner; it will |
| 96 | // figure it out as soon as it runs that task. |
| 97 | // |
| 98 | // Note that this check must be after pending_commit_ is set. |
| 99 | if (IsInConflict()) { |
| 100 | ClearPendingCommit(); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | // Otherwise, keep the data associated with this pending commit |
| 105 | // so it can be committed at the next possible opportunity. |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 106 | } |
| 107 | |
maxbogue | 4776274 | 2016-03-01 00:56:18 | [diff] [blame] | 108 | void WorkerEntityTracker::ReceiveCommitResponse(const std::string& response_id, |
| 109 | int64_t response_version, |
| 110 | int64_t sequence_number) { |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 111 | // Commit responses, especially after the first commit, can update our ID. |
| 112 | id_ = response_id; |
| 113 | |
| 114 | DCHECK_GT(response_version, highest_commit_response_version_) |
| 115 | << "Had expected higher response version." |
| 116 | << " id: " << id_; |
| 117 | |
| 118 | // Commits are synchronous, so there's no reason why the sequence numbers |
| 119 | // wouldn't match. |
| 120 | DCHECK_EQ(sequence_number_, sequence_number) |
| 121 | << "Unexpected sequence number mismatch." |
| 122 | << " id: " << id_; |
| 123 | |
| 124 | highest_commit_response_version_ = response_version; |
| 125 | |
| 126 | // Because an in-progress commit blocks the sync thread, we can assume that |
| 127 | // the item we just committed successfully is exactly the one we have now. |
| 128 | // Nothing changed it while the commit was happening. Since we're now in |
| 129 | // sync with the server, we can clear the pending commit. |
| 130 | ClearPendingCommit(); |
| 131 | } |
| 132 | |
maxbogue | 4776274 | 2016-03-01 00:56:18 | [diff] [blame] | 133 | void WorkerEntityTracker::ReceiveUpdate(int64_t version) { |
[email protected] | 4237bc17 | 2014-08-06 20:46:11 | [diff] [blame] | 134 | if (version <= highest_gu_response_version_) |
| 135 | return; |
| 136 | |
| 137 | highest_gu_response_version_ = version; |
| 138 | |
| 139 | // Got an applicable update newer than any pending updates. It must be safe |
maxbogue | 8cd3afc | 2016-03-24 00:45:47 | [diff] [blame] | 140 | // to discard the old encrypted update, if there was one. |
| 141 | ClearEncryptedUpdate(); |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 142 | |
| 143 | if (IsInConflict()) { |
| 144 | // Incoming update clobbers the pending commit on the sync thread. |
| 145 | // The model thread can re-request this commit later if it wants to. |
| 146 | ClearPendingCommit(); |
| 147 | } |
| 148 | } |
| 149 | |
maxbogue | 8cd3afc | 2016-03-24 00:45:47 | [diff] [blame] | 150 | bool WorkerEntityTracker::ReceiveEncryptedUpdate( |
| 151 | const UpdateResponseData& data) { |
[email protected] | 4237bc17 | 2014-08-06 20:46:11 | [diff] [blame] | 152 | if (data.response_version < highest_gu_response_version_) |
| 153 | return false; |
| 154 | |
| 155 | highest_gu_response_version_ = data.response_version; |
maxbogue | 8cd3afc | 2016-03-24 00:45:47 | [diff] [blame] | 156 | encrypted_update_.reset(new UpdateResponseData(data)); |
[email protected] | 4237bc17 | 2014-08-06 20:46:11 | [diff] [blame] | 157 | ClearPendingCommit(); |
| 158 | return true; |
| 159 | } |
| 160 | |
maxbogue | 8cd3afc | 2016-03-24 00:45:47 | [diff] [blame] | 161 | bool WorkerEntityTracker::HasEncryptedUpdate() const { |
| 162 | return !!encrypted_update_; |
[email protected] | 4237bc17 | 2014-08-06 20:46:11 | [diff] [blame] | 163 | } |
| 164 | |
maxbogue | 8cd3afc | 2016-03-24 00:45:47 | [diff] [blame] | 165 | UpdateResponseData WorkerEntityTracker::GetEncryptedUpdate() const { |
| 166 | return *encrypted_update_; |
[email protected] | 4237bc17 | 2014-08-06 20:46:11 | [diff] [blame] | 167 | } |
| 168 | |
maxbogue | 8cd3afc | 2016-03-24 00:45:47 | [diff] [blame] | 169 | void WorkerEntityTracker::ClearEncryptedUpdate() { |
| 170 | encrypted_update_.reset(); |
[email protected] | 4237bc17 | 2014-08-06 20:46:11 | [diff] [blame] | 171 | } |
| 172 | |
maxbogue | 4776274 | 2016-03-01 00:56:18 | [diff] [blame] | 173 | bool WorkerEntityTracker::IsInConflict() const { |
maxbogue | 21e465ee3 | 2015-09-16 20:50:33 | [diff] [blame] | 174 | if (!HasPendingCommit()) |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 175 | return false; |
| 176 | |
maxbogue | 8cd3afc | 2016-03-24 00:45:47 | [diff] [blame] | 177 | if (HasEncryptedUpdate()) |
[email protected] | 4237bc17 | 2014-08-06 20:46:11 | [diff] [blame] | 178 | return true; |
| 179 | |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 180 | if (highest_gu_response_version_ <= highest_commit_response_version_) { |
| 181 | // The most recent server state was created in a commit made by this |
| 182 | // client. We're fully up to date, and therefore not in conflict. |
| 183 | return false; |
| 184 | } else { |
| 185 | // The most recent server state was written by someone else. |
| 186 | // Did the model thread have the most up to date version when it issued the |
| 187 | // commit request? |
| 188 | if (base_version_ >= highest_gu_response_version_) { |
| 189 | return false; // Yes. |
| 190 | } else { |
| 191 | return true; // No. |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
maxbogue | 4776274 | 2016-03-01 00:56:18 | [diff] [blame] | 196 | bool WorkerEntityTracker::IsServerKnown() const { |
stanisc | 96665bd | 2015-08-14 18:59:14 | [diff] [blame] | 197 | return base_version_ != kUncommittedVersion; |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 198 | } |
| 199 | |
maxbogue | 4776274 | 2016-03-01 00:56:18 | [diff] [blame] | 200 | void WorkerEntityTracker::ClearPendingCommit() { |
maxbogue | 21e465ee3 | 2015-09-16 20:50:33 | [diff] [blame] | 201 | pending_commit_.reset(); |
[email protected] | f233b14 | 2014-06-05 18:05:33 | [diff] [blame] | 202 | } |
| 203 | |
stanisc | 29a9029 | 2015-12-01 19:29:00 | [diff] [blame] | 204 | } // namespace syncer_v2 |