Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(905)

Side by Side Diff: sync/engine/process_updates_command.cc

Issue 11091009: sync: Merge {Verify,Process}UpdatesCommand (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Amend comment Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sync/engine/process_updates_command.h ('k') | sync/engine/process_updates_command_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/process_updates_command.h" 5 #include "sync/engine/process_updates_command.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/location.h" 10 #include "base/location.h"
(...skipping 14 matching lines...) Expand all
25 #include "sync/internal_api/public/base/node_ordinal.h" 25 #include "sync/internal_api/public/base/node_ordinal.h"
26 26
27 using std::vector; 27 using std::vector;
28 28
29 namespace syncer { 29 namespace syncer {
30 30
31 using sessions::SyncSession; 31 using sessions::SyncSession;
32 using sessions::StatusController; 32 using sessions::StatusController;
33 using sessions::UpdateProgress; 33 using sessions::UpdateProgress;
34 34
35 using syncable::GET_BY_ID;
36
35 ProcessUpdatesCommand::ProcessUpdatesCommand() {} 37 ProcessUpdatesCommand::ProcessUpdatesCommand() {}
36 ProcessUpdatesCommand::~ProcessUpdatesCommand() {} 38 ProcessUpdatesCommand::~ProcessUpdatesCommand() {}
37 39
38 std::set<ModelSafeGroup> ProcessUpdatesCommand::GetGroupsToChange( 40 std::set<ModelSafeGroup> ProcessUpdatesCommand::GetGroupsToChange(
39 const sessions::SyncSession& session) const { 41 const sessions::SyncSession& session) const {
40 return session.GetEnabledGroupsWithVerifiedUpdates(); 42 std::set<ModelSafeGroup> groups_with_updates;
41 } 43
44 const sync_pb::GetUpdatesResponse& updates =
45 session.status_controller().updates_response().get_updates();
46 for (int i = 0; i < updates.entries().size(); i++) {
47 groups_with_updates.insert(
48 GetGroupForModelType(GetModelType(updates.entries(i)),
49 session.routing_info()));
50 }
51
52 return groups_with_updates;
53 }
54
55 namespace {
56
57 // This function attempts to determine whether or not this update is genuinely
58 // new, or if it is a reflection of one of our own commits.
59 //
60 // There is a known inaccuracy in its implementation. If this update ends up
61 // being applied to a local item with a different ID, we will count the change
62 // as being a non-reflection update. Fortunately, the server usually updates
63 // our IDs correctly in its commit response, so a new ID during GetUpdate should
64 // be rare.
65 //
66 // The only secnarios I can think of where this might happen are:
67 // - We commit a new item to the server, but we don't persist the
68 // server-returned new ID to the database before we shut down. On the GetUpdate
69 // following the next restart, we will receive an update from the server that
70 // updates its local ID.
71 // - When two attempts to create an item with identical UNIQUE_CLIENT_TAG values
72 // collide at the server. I have seen this in testing. When it happens, the
73 // test server will send one of the clients a response to upate its local ID so
74 // that both clients will refer to the item using the same ID going forward. In
75 // this case, we're right to assume that the update is not a reflection.
76 //
77 // For more information, see FindLocalIdToUpdate().
78 bool UpdateContainsNewVersion(syncable::BaseTransaction *trans,
79 const sync_pb::SyncEntity &update) {
80 int64 existing_version = -1; // The server always sends positive versions.
81 syncable::Entry existing_entry(trans, GET_BY_ID,
82 SyncableIdFromProto(update.id_string()));
83 if (existing_entry.good())
84 existing_version = existing_entry.Get(syncable::BASE_VERSION);
85
86 if (!existing_entry.good() && update.deleted()) {
87 // There are several possible explanations for this. The most common cases
88 // will be first time sync and the redelivery of deletions we've already
89 // synced, accepted, and purged from our database. In either case, the
90 // update is useless to us. Let's count them all as "not new", even though
91 // that may not always be entirely accurate.
92 return false;
93 }
94
95 if (existing_entry.good() &&
96 !existing_entry.Get(syncable::UNIQUE_CLIENT_TAG).empty() &&
97 existing_entry.Get(syncable::IS_DEL) &&
98 update.deleted()) {
99 // Unique client tags will have their version set to zero when they're
100 // deleted. The usual version comparison logic won't be able to detect
101 // reflections of these items. Instead, we assume any received tombstones
102 // are reflections. That should be correct most of the time.
103 return false;
104 }
105
106 return existing_version < update.version();
107 }
108
109 } // namespace
42 110
43 SyncerError ProcessUpdatesCommand::ModelChangingExecuteImpl( 111 SyncerError ProcessUpdatesCommand::ModelChangingExecuteImpl(
44 SyncSession* session) { 112 SyncSession* session) {
45 syncable::Directory* dir = session->context()->directory(); 113 syncable::Directory* dir = session->context()->directory();
46 114
47 const sessions::UpdateProgress* progress =
48 session->status_controller().update_progress();
49 if (!progress)
50 return SYNCER_OK; // Nothing to do.
51
52 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir); 115 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir);
53 vector<sessions::VerifiedUpdate>::const_iterator it; 116
54 for (it = progress->VerifiedUpdatesBegin(); 117 sessions::StatusController* status = session->mutable_status_controller();
55 it != progress->VerifiedUpdatesEnd(); 118 const sync_pb::GetUpdatesResponse& updates =
56 ++it) { 119 status->updates_response().get_updates();
57 const sync_pb::SyncEntity& update = it->second; 120 int update_count = updates.entries().size();
58 121
59 if (it->first != VERIFY_SUCCESS && it->first != VERIFY_UNDELETE) 122 ModelTypeSet requested_types = GetRoutingInfoTypes(
123 session->routing_info());
124
125 DVLOG(1) << update_count << " entries to verify";
126 for (int i = 0; i < update_count; i++) {
127 const sync_pb::SyncEntity& update = updates.entries(i);
128
129 // The current function gets executed on several different threads, but
130 // every call iterates over the same list of items that the server returned
131 // to us. We're not allowed to process items unless we're on the right
132 // thread for that type. This check will ensure we only touch the items
133 // that live on our current thread.
134 // TODO(tim): Don't allow access to objects in other ModelSafeGroups.
135 // See crbug.com/121521 .
136 ModelSafeGroup g = GetGroupForModelType(GetModelType(update),
137 session->routing_info());
138 if (g != status->group_restriction())
60 continue; 139 continue;
61 switch (ProcessUpdate(update, 140
62 dir->GetCryptographer(&trans), 141 VerifyResult verify_result = VerifyUpdate(&trans, update,
63 &trans)) { 142 requested_types,
64 case SUCCESS_PROCESSED: 143 session->routing_info());
65 case SUCCESS_STORED: 144 status->mutable_update_progress()->AddVerifyResult(verify_result, update);
66 break; 145 status->increment_num_updates_downloaded_by(1);
67 default: 146 if (!UpdateContainsNewVersion(&trans, update))
68 NOTREACHED(); 147 status->increment_num_reflected_updates_downloaded_by(1);
69 break; 148 if (update.deleted())
70 } 149 status->increment_num_tombstone_updates_downloaded_by(1);
71 } 150
72 151 if (verify_result != VERIFY_SUCCESS && verify_result != VERIFY_UNDELETE)
73 StatusController* status = session->mutable_status_controller(); 152 continue;
153
154 ServerUpdateProcessingResult process_result =
155 ProcessUpdate(update, dir->GetCryptographer(&trans), &trans);
156
157 DCHECK(process_result == SUCCESS_PROCESSED ||
158 process_result == SUCCESS_STORED);
159 }
160
74 status->mutable_update_progress()->ClearVerifiedUpdates(); 161 status->mutable_update_progress()->ClearVerifiedUpdates();
75 return SYNCER_OK; 162 return SYNCER_OK;
76 } 163 }
77 164
78 namespace { 165 namespace {
166
167 // In the event that IDs match, but tags differ AttemptReuniteClient tag
168 // will have refused to unify the update.
169 // We should not attempt to apply it at all since it violates consistency
170 // rules.
171 VerifyResult VerifyTagConsistency(const sync_pb::SyncEntity& entry,
172 const syncable::MutableEntry& same_id) {
173 if (entry.has_client_defined_unique_tag() &&
174 entry.client_defined_unique_tag() !=
175 same_id.Get(syncable::UNIQUE_CLIENT_TAG)) {
176 return VERIFY_FAIL;
177 }
178 return VERIFY_UNDECIDED;
179 }
180
181 } // namespace
182
183 VerifyResult ProcessUpdatesCommand::VerifyUpdate(
184 syncable::WriteTransaction* trans, const sync_pb::SyncEntity& entry,
185 ModelTypeSet requested_types,
186 const ModelSafeRoutingInfo& routes) {
187 syncable::Id id = SyncableIdFromProto(entry.id_string());
188 VerifyResult result = VERIFY_FAIL;
189
190 const bool deleted = entry.has_deleted() && entry.deleted();
191 const bool is_directory = IsFolder(entry);
192 const ModelType model_type = GetModelType(entry);
193
194 if (!id.ServerKnows()) {
195 LOG(ERROR) << "Illegal negative id in received updates";
196 return result;
197 }
198 {
199 const std::string name = SyncerProtoUtil::NameFromSyncEntity(entry);
200 if (name.empty() && !deleted) {
201 LOG(ERROR) << "Zero length name in non-deleted update";
202 return result;
203 }
204 }
205
206 syncable::MutableEntry same_id(trans, GET_BY_ID, id);
207 result = VerifyNewEntry(entry, &same_id, deleted);
208
209 ModelType placement_type = !deleted ? GetModelType(entry)
210 : same_id.good() ? same_id.GetModelType() : UNSPECIFIED;
211
212 if (VERIFY_UNDECIDED == result) {
213 result = VerifyTagConsistency(entry, same_id);
214 }
215
216 if (VERIFY_UNDECIDED == result) {
217 if (deleted) {
218 // For deletes the server could send tombostones for items that
219 // the client did not request. If so ignore those items.
220 if (IsRealDataType(placement_type) &&
221 !requested_types.Has(placement_type)) {
222 result = VERIFY_SKIP;
223 } else {
224 result = VERIFY_SUCCESS;
225 }
226 }
227 }
228
229 // If we have an existing entry, we check here for updates that break
230 // consistency rules.
231 if (VERIFY_UNDECIDED == result) {
232 result = VerifyUpdateConsistency(trans, entry, &same_id,
233 deleted, is_directory, model_type);
234 }
235
236 if (VERIFY_UNDECIDED == result)
237 result = VERIFY_SUCCESS; // No news is good news.
238
239 return result; // This might be VERIFY_SUCCESS as well
240 }
241
242 namespace {
79 // Returns true if the entry is still ok to process. 243 // Returns true if the entry is still ok to process.
80 bool ReverifyEntry(syncable::WriteTransaction* trans, 244 bool ReverifyEntry(syncable::WriteTransaction* trans,
81 const sync_pb::SyncEntity& entry, 245 const sync_pb::SyncEntity& entry,
82 syncable::MutableEntry* same_id) { 246 syncable::MutableEntry* same_id) {
83 247
84 const bool deleted = entry.has_deleted() && entry.deleted(); 248 const bool deleted = entry.has_deleted() && entry.deleted();
85 const bool is_directory = IsFolder(entry); 249 const bool is_directory = IsFolder(entry);
86 const ModelType model_type = GetModelType(entry); 250 const ModelType model_type = GetModelType(entry);
87 251
88 return VERIFY_SUCCESS == VerifyUpdateConsistency(trans, 252 return VERIFY_SUCCESS == VerifyUpdateConsistency(trans,
(...skipping 19 matching lines...) Expand all
108 272
109 // FindLocalEntryToUpdate has veto power. 273 // FindLocalEntryToUpdate has veto power.
110 if (local_id.IsNull()) { 274 if (local_id.IsNull()) {
111 return SUCCESS_PROCESSED; // The entry has become irrelevant. 275 return SUCCESS_PROCESSED; // The entry has become irrelevant.
112 } 276 }
113 277
114 CreateNewEntry(trans, local_id); 278 CreateNewEntry(trans, local_id);
115 279
116 // We take a two step approach. First we store the entries data in the 280 // We take a two step approach. First we store the entries data in the
117 // server fields of a local entry and then move the data to the local fields 281 // server fields of a local entry and then move the data to the local fields
118 syncable::MutableEntry target_entry(trans, syncable::GET_BY_ID, local_id); 282 syncable::MutableEntry target_entry(trans, GET_BY_ID, local_id);
119 283
120 // We need to run the Verify checks again; the world could have changed 284 // We need to run the Verify checks again; the world could have changed
121 // since VerifyUpdatesCommand. 285 // since we last verified.
122 if (!ReverifyEntry(trans, update, &target_entry)) { 286 if (!ReverifyEntry(trans, update, &target_entry)) {
123 return SUCCESS_PROCESSED; // The entry has become irrelevant. 287 return SUCCESS_PROCESSED; // The entry has become irrelevant.
124 } 288 }
125 289
126 // If we're repurposing an existing local entry with a new server ID, 290 // If we're repurposing an existing local entry with a new server ID,
127 // change the ID now, after we're sure that the update can succeed. 291 // change the ID now, after we're sure that the update can succeed.
128 if (local_id != server_id) { 292 if (local_id != server_id) {
129 DCHECK(!update.deleted()); 293 DCHECK(!update.deleted());
130 ChangeEntryIDAndUpdateChildren(trans, &target_entry, server_id); 294 ChangeEntryIDAndUpdateChildren(trans, &target_entry, server_id);
131 // When IDs change, versions become irrelevant. Forcing BASE_VERSION 295 // When IDs change, versions become irrelevant. Forcing BASE_VERSION
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 target_entry.Put(syncable::BASE_SERVER_SPECIFICS, 346 target_entry.Put(syncable::BASE_SERVER_SPECIFICS,
183 sync_pb::EntitySpecifics()); 347 sync_pb::EntitySpecifics());
184 } 348 }
185 349
186 UpdateServerFieldsFromUpdate(&target_entry, update, name); 350 UpdateServerFieldsFromUpdate(&target_entry, update, name);
187 351
188 return SUCCESS_PROCESSED; 352 return SUCCESS_PROCESSED;
189 } 353 }
190 354
191 } // namespace syncer 355 } // namespace syncer
OLDNEW
« no previous file with comments | « sync/engine/process_updates_command.h ('k') | sync/engine/process_updates_command_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698