| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "sync/engine/worker_entity_tracker.h" | 5 #include "sync/engine/worker_entity_tracker.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "sync/internal_api/public/base/model_type.h" | 10 #include "sync/internal_api/public/base/model_type.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 DCHECK(!client_tag_hash_.empty()); | 25 DCHECK(!client_tag_hash_.empty()); |
| 26 } | 26 } |
| 27 | 27 |
| 28 WorkerEntityTracker::~WorkerEntityTracker() {} | 28 WorkerEntityTracker::~WorkerEntityTracker() {} |
| 29 | 29 |
| 30 bool WorkerEntityTracker::HasPendingCommit() const { | 30 bool WorkerEntityTracker::HasPendingCommit() const { |
| 31 return !!pending_commit_; | 31 return !!pending_commit_; |
| 32 } | 32 } |
| 33 | 33 |
| 34 void WorkerEntityTracker::PopulateCommitProto( | 34 void WorkerEntityTracker::PopulateCommitProto( |
| 35 sync_pb::SyncEntity* commit_entity, | 35 sync_pb::SyncEntity* commit_entity) const { |
| 36 int64_t* sequence_number) const { | |
| 37 DCHECK(HasPendingCommit()); | 36 DCHECK(HasPendingCommit()); |
| 38 DCHECK(!client_tag_hash_.empty()); | 37 DCHECK(!client_tag_hash_.empty()); |
| 39 | 38 |
| 40 if (!id_.empty()) { | 39 if (!id_.empty()) { |
| 41 commit_entity->set_id_string(id_); | 40 commit_entity->set_id_string(id_); |
| 42 } | 41 } |
| 43 | 42 |
| 44 const EntityData& entity = pending_commit_->entity.value(); | 43 const EntityData& entity = pending_commit_->entity.value(); |
| 45 DCHECK_EQ(client_tag_hash_, entity.client_tag_hash); | 44 DCHECK_EQ(client_tag_hash_, entity.client_tag_hash); |
| 46 | 45 |
| 47 commit_entity->set_client_defined_unique_tag(client_tag_hash_); | 46 commit_entity->set_client_defined_unique_tag(client_tag_hash_); |
| 48 commit_entity->set_version(base_version_); | 47 commit_entity->set_version(base_version_); |
| 49 commit_entity->set_deleted(entity.is_deleted()); | 48 commit_entity->set_deleted(entity.is_deleted()); |
| 50 | 49 |
| 51 // TODO(stanisc): This doesn't support bookmarks yet. | 50 // TODO(stanisc): This doesn't support bookmarks yet. |
| 52 DCHECK(entity.parent_id.empty()); | 51 DCHECK(entity.parent_id.empty()); |
| 53 commit_entity->set_folder(false); | 52 commit_entity->set_folder(false); |
| 54 | 53 |
| 55 commit_entity->set_name(entity.non_unique_name); | 54 commit_entity->set_name(entity.non_unique_name); |
| 56 if (!entity.is_deleted()) { | 55 if (!entity.is_deleted()) { |
| 57 commit_entity->set_ctime(syncer::TimeToProtoTime(entity.creation_time)); | 56 commit_entity->set_ctime(syncer::TimeToProtoTime(entity.creation_time)); |
| 58 commit_entity->set_mtime(syncer::TimeToProtoTime(entity.modification_time)); | 57 commit_entity->set_mtime(syncer::TimeToProtoTime(entity.modification_time)); |
| 59 commit_entity->mutable_specifics()->CopyFrom(entity.specifics); | 58 commit_entity->mutable_specifics()->CopyFrom(entity.specifics); |
| 60 } | 59 } |
| 61 | |
| 62 *sequence_number = sequence_number_; | |
| 63 } | 60 } |
| 64 | 61 |
| 65 void WorkerEntityTracker::RequestCommit(const CommitRequestData& data) { | 62 void WorkerEntityTracker::RequestCommit(const CommitRequestData& data) { |
| 66 DCHECK_GE(data.base_version, base_version_) | 63 DCHECK_GE(data.base_version, base_version_) |
| 67 << "Base version should never decrease"; | 64 << "Base version should never decrease"; |
| 68 | 65 |
| 69 DCHECK_GE(data.sequence_number, sequence_number_) | 66 DCHECK_GE(data.sequence_number, sequence_number_) |
| 70 << "Sequence number should never decrease"; | 67 << "Sequence number should never decrease"; |
| 71 | 68 |
| 72 // Update our book-keeping counters. | 69 // Update our book-keeping counters. |
| 73 base_version_ = data.base_version; | 70 base_version_ = data.base_version; |
| 74 sequence_number_ = data.sequence_number; | 71 sequence_number_ = data.sequence_number; |
| 72 pending_commit_specifics_hash_ = data.specifics_hash; |
| 75 | 73 |
| 76 // Don't commit deletions of server-unknown items. | 74 // Don't commit deletions of server-unknown items. |
| 77 if (data.entity->is_deleted() && !IsServerKnown()) { | 75 if (data.entity->is_deleted() && !IsServerKnown()) { |
| 78 ClearPendingCommit(); | 76 ClearPendingCommit(); |
| 79 return; | 77 return; |
| 80 } | 78 } |
| 81 | 79 |
| 82 // We intentionally don't update the id_ here. Good ID values come from the | 80 // 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 | 81 // 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. | 82 // model thread could have a better ID value than we do. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 98 // Note that this check must be after pending_commit_ is set. | 96 // Note that this check must be after pending_commit_ is set. |
| 99 if (IsInConflict()) { | 97 if (IsInConflict()) { |
| 100 ClearPendingCommit(); | 98 ClearPendingCommit(); |
| 101 return; | 99 return; |
| 102 } | 100 } |
| 103 | 101 |
| 104 // Otherwise, keep the data associated with this pending commit | 102 // Otherwise, keep the data associated with this pending commit |
| 105 // so it can be committed at the next possible opportunity. | 103 // so it can be committed at the next possible opportunity. |
| 106 } | 104 } |
| 107 | 105 |
| 108 void WorkerEntityTracker::ReceiveCommitResponse(const std::string& response_id, | 106 void WorkerEntityTracker::ReceiveCommitResponse(CommitResponseData* ack) { |
| 109 int64_t response_version, | 107 DCHECK_GT(ack->response_version, highest_commit_response_version_) |
| 110 int64_t sequence_number) { | |
| 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." | 108 << "Had expected higher response version." |
| 116 << " id: " << id_; | 109 << " id: " << id_; |
| 117 | 110 |
| 118 // Commits are synchronous, so there's no reason why the sequence numbers | 111 // Commit responses, especially after the first commit, can update our ID. |
| 119 // wouldn't match. | 112 id_ = ack->id; |
| 120 DCHECK_EQ(sequence_number_, sequence_number) | 113 highest_commit_response_version_ = ack->response_version; |
| 121 << "Unexpected sequence number mismatch." | |
| 122 << " id: " << id_; | |
| 123 | 114 |
| 124 highest_commit_response_version_ = response_version; | 115 // Fill in some cached info for the response data. Since commits happen |
| 116 // synchronously on the sync thread, our item's state is guaranteed to be |
| 117 // the same at the end of the commit as it was at the start. |
| 118 ack->sequence_number = sequence_number_; |
| 119 ack->specifics_hash = pending_commit_specifics_hash_; |
| 125 | 120 |
| 126 // Because an in-progress commit blocks the sync thread, we can assume that | 121 // 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. | 122 // 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 | 123 // Nothing changed it while the commit was happening. Since we're now in |
| 129 // sync with the server, we can clear the pending commit. | 124 // sync with the server, we can clear the pending commit. |
| 130 ClearPendingCommit(); | 125 ClearPendingCommit(); |
| 131 } | 126 } |
| 132 | 127 |
| 133 void WorkerEntityTracker::ReceiveUpdate(int64_t version) { | 128 void WorkerEntityTracker::ReceiveUpdate(int64_t version) { |
| 134 if (version <= highest_gu_response_version_) | 129 if (version <= highest_gu_response_version_) |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 } | 187 } |
| 193 } | 188 } |
| 194 } | 189 } |
| 195 | 190 |
| 196 bool WorkerEntityTracker::IsServerKnown() const { | 191 bool WorkerEntityTracker::IsServerKnown() const { |
| 197 return base_version_ != kUncommittedVersion; | 192 return base_version_ != kUncommittedVersion; |
| 198 } | 193 } |
| 199 | 194 |
| 200 void WorkerEntityTracker::ClearPendingCommit() { | 195 void WorkerEntityTracker::ClearPendingCommit() { |
| 201 pending_commit_.reset(); | 196 pending_commit_.reset(); |
| 197 pending_commit_specifics_hash_.clear(); |
| 202 } | 198 } |
| 203 | 199 |
| 204 } // namespace syncer_v2 | 200 } // namespace syncer_v2 |
| OLD | NEW |