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

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

Issue 38803003: sync: Implement per-type update processing (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix progress marker fetching Created 7 years, 1 month 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
OLDNEW
(Empty)
1 // Copyright 2012 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
5 #include "sync/engine/process_updates_command.h"
6
7 #include <vector>
8
9 #include "base/basictypes.h"
10 #include "base/location.h"
11 #include "sync/engine/syncer.h"
12 #include "sync/engine/syncer_proto_util.h"
13 #include "sync/engine/syncer_util.h"
14 #include "sync/sessions/sync_session.h"
15 #include "sync/syncable/directory.h"
16 #include "sync/syncable/model_neutral_mutable_entry.h"
17 #include "sync/syncable/syncable_model_neutral_write_transaction.h"
18 #include "sync/syncable/syncable_proto_util.h"
19 #include "sync/syncable/syncable_util.h"
20 #include "sync/util/cryptographer.h"
21
22 using std::vector;
23
24 namespace syncer {
25
26 using sessions::SyncSession;
27 using sessions::StatusController;
28
29 using syncable::GET_BY_ID;
30
31 ProcessUpdatesCommand::ProcessUpdatesCommand() {}
32 ProcessUpdatesCommand::~ProcessUpdatesCommand() {}
33
34 namespace {
35
36 // This function attempts to determine whether or not this update is genuinely
37 // new, or if it is a reflection of one of our own commits.
38 //
39 // There is a known inaccuracy in its implementation. If this update ends up
40 // being applied to a local item with a different ID, we will count the change
41 // as being a non-reflection update. Fortunately, the server usually updates
42 // our IDs correctly in its commit response, so a new ID during GetUpdate should
43 // be rare.
44 //
45 // The only secnarios I can think of where this might happen are:
46 // - We commit a new item to the server, but we don't persist the
47 // server-returned new ID to the database before we shut down. On the GetUpdate
48 // following the next restart, we will receive an update from the server that
49 // updates its local ID.
50 // - When two attempts to create an item with identical UNIQUE_CLIENT_TAG values
51 // collide at the server. I have seen this in testing. When it happens, the
52 // test server will send one of the clients a response to upate its local ID so
53 // that both clients will refer to the item using the same ID going forward. In
54 // this case, we're right to assume that the update is not a reflection.
55 //
56 // For more information, see FindLocalIdToUpdate().
57 bool UpdateContainsNewVersion(syncable::BaseTransaction *trans,
58 const sync_pb::SyncEntity &update) {
59 int64 existing_version = -1; // The server always sends positive versions.
60 syncable::Entry existing_entry(trans, GET_BY_ID,
61 SyncableIdFromProto(update.id_string()));
62 if (existing_entry.good())
63 existing_version = existing_entry.GetBaseVersion();
64
65 if (!existing_entry.good() && update.deleted()) {
66 // There are several possible explanations for this. The most common cases
67 // will be first time sync and the redelivery of deletions we've already
68 // synced, accepted, and purged from our database. In either case, the
69 // update is useless to us. Let's count them all as "not new", even though
70 // that may not always be entirely accurate.
71 return false;
72 }
73
74 if (existing_entry.good() &&
75 !existing_entry.GetUniqueClientTag().empty() &&
76 existing_entry.GetIsDel() &&
77 update.deleted()) {
78 // Unique client tags will have their version set to zero when they're
79 // deleted. The usual version comparison logic won't be able to detect
80 // reflections of these items. Instead, we assume any received tombstones
81 // are reflections. That should be correct most of the time.
82 return false;
83 }
84
85 return existing_version < update.version();
86 }
87
88 } // namespace
89
90 SyncerError ProcessUpdatesCommand::ExecuteImpl(SyncSession* session) {
91 syncable::Directory* dir = session->context()->directory();
92
93 syncable::ModelNeutralWriteTransaction trans(
94 FROM_HERE, syncable::SYNCER, dir);
95
96 sessions::StatusController* status = session->mutable_status_controller();
97 const sync_pb::GetUpdatesResponse& updates =
98 status->updates_response().get_updates();
99 ModelTypeSet requested_types = GetRoutingInfoTypes(
100 session->context()->routing_info());
101
102 TypeSyncEntityMap updates_by_type;
103
104 PartitionUpdatesByType(updates, &updates_by_type);
105
106 int update_count = updates.entries().size();
107 status->increment_num_updates_downloaded_by(update_count);
108
109 DVLOG(1) << update_count << " entries to verify";
110 for (TypeSyncEntityMap::iterator it = updates_by_type.begin();
111 it != updates_by_type.end(); ++it) {
112 for (SyncEntityList::iterator update_it = it->second.begin();
113 update_it != it->second.end(); ++update_it) {
114 if (!UpdateContainsNewVersion(&trans, **update_it))
115 status->increment_num_reflected_updates_downloaded_by(1);
116 if ((*update_it)->deleted())
117 status->increment_num_tombstone_updates_downloaded_by(1);
118 VerifyResult verify_result = VerifyUpdate(
119 &trans,
120 **update_it,
121 requested_types,
122 session->context()->routing_info());
123 if (verify_result != VERIFY_SUCCESS && verify_result != VERIFY_UNDELETE)
124 continue;
125 ProcessUpdate(**update_it, dir->GetCryptographer(&trans), &trans);
126 }
127 }
128
129 return SYNCER_OK;
130 }
131
132 void ProcessUpdatesCommand::PartitionUpdatesByType(
133 const sync_pb::GetUpdatesResponse& updates,
134 TypeSyncEntityMap* updates_by_type) {
135 int update_count = updates.entries().size();
136 for (int i = 0; i < update_count; ++i) {
137 const sync_pb::SyncEntity& update = updates.entries(i);
138 ModelType type = GetModelType(update);
139 DCHECK(IsRealDataType(type));
140 (*updates_by_type)[type].push_back(&update);
141 }
142 }
143
144 namespace {
145
146 // In the event that IDs match, but tags differ AttemptReuniteClient tag
147 // will have refused to unify the update.
148 // We should not attempt to apply it at all since it violates consistency
149 // rules.
150 VerifyResult VerifyTagConsistency(
151 const sync_pb::SyncEntity& entry,
152 const syncable::ModelNeutralMutableEntry& same_id) {
153 if (entry.has_client_defined_unique_tag() &&
154 entry.client_defined_unique_tag() !=
155 same_id.GetUniqueClientTag()) {
156 return VERIFY_FAIL;
157 }
158 return VERIFY_UNDECIDED;
159 }
160
161 } // namespace
162
163 VerifyResult ProcessUpdatesCommand::VerifyUpdate(
164 syncable::ModelNeutralWriteTransaction* trans,
165 const sync_pb::SyncEntity& entry,
166 ModelTypeSet requested_types,
167 const ModelSafeRoutingInfo& routes) {
168 syncable::Id id = SyncableIdFromProto(entry.id_string());
169 VerifyResult result = VERIFY_FAIL;
170
171 const bool deleted = entry.has_deleted() && entry.deleted();
172 const bool is_directory = IsFolder(entry);
173 const ModelType model_type = GetModelType(entry);
174
175 if (!id.ServerKnows()) {
176 LOG(ERROR) << "Illegal negative id in received updates";
177 return result;
178 }
179 {
180 const std::string name = SyncerProtoUtil::NameFromSyncEntity(entry);
181 if (name.empty() && !deleted) {
182 LOG(ERROR) << "Zero length name in non-deleted update";
183 return result;
184 }
185 }
186
187 syncable::ModelNeutralMutableEntry same_id(trans, GET_BY_ID, id);
188 result = VerifyNewEntry(entry, &same_id, deleted);
189
190 ModelType placement_type = !deleted ? GetModelType(entry)
191 : same_id.good() ? same_id.GetModelType() : UNSPECIFIED;
192
193 if (VERIFY_UNDECIDED == result) {
194 result = VerifyTagConsistency(entry, same_id);
195 }
196
197 if (VERIFY_UNDECIDED == result) {
198 if (deleted) {
199 // For deletes the server could send tombostones for items that
200 // the client did not request. If so ignore those items.
201 if (IsRealDataType(placement_type) &&
202 !requested_types.Has(placement_type)) {
203 result = VERIFY_SKIP;
204 } else {
205 result = VERIFY_SUCCESS;
206 }
207 }
208 }
209
210 // If we have an existing entry, we check here for updates that break
211 // consistency rules.
212 if (VERIFY_UNDECIDED == result) {
213 result = VerifyUpdateConsistency(trans, entry, deleted,
214 is_directory, model_type, &same_id);
215 }
216
217 if (VERIFY_UNDECIDED == result)
218 result = VERIFY_SUCCESS; // No news is good news.
219
220 return result; // This might be VERIFY_SUCCESS as well
221 }
222
223 namespace {
224 // Returns true if the entry is still ok to process.
225 bool ReverifyEntry(syncable::ModelNeutralWriteTransaction* trans,
226 const sync_pb::SyncEntity& entry,
227 syncable::ModelNeutralMutableEntry* same_id) {
228
229 const bool deleted = entry.has_deleted() && entry.deleted();
230 const bool is_directory = IsFolder(entry);
231 const ModelType model_type = GetModelType(entry);
232
233 return VERIFY_SUCCESS == VerifyUpdateConsistency(trans,
234 entry,
235 deleted,
236 is_directory,
237 model_type,
238 same_id);
239 }
240 } // namespace
241
242 // Process a single update. Will avoid touching global state.
243 void ProcessUpdatesCommand::ProcessUpdate(
244 const sync_pb::SyncEntity& update,
245 const Cryptographer* cryptographer,
246 syncable::ModelNeutralWriteTransaction* const trans) {
247 const syncable::Id& server_id = SyncableIdFromProto(update.id_string());
248 const std::string name = SyncerProtoUtil::NameFromSyncEntity(update);
249
250 // Look to see if there's a local item that should recieve this update,
251 // maybe due to a duplicate client tag or a lost commit response.
252 syncable::Id local_id = FindLocalIdToUpdate(trans, update);
253
254 // FindLocalEntryToUpdate has veto power.
255 if (local_id.IsNull()) {
256 return; // The entry has become irrelevant.
257 }
258
259 CreateNewEntry(trans, local_id);
260
261 // We take a two step approach. First we store the entries data in the
262 // server fields of a local entry and then move the data to the local fields
263 syncable::ModelNeutralMutableEntry target_entry(trans, GET_BY_ID, local_id);
264
265 // We need to run the Verify checks again; the world could have changed
266 // since we last verified.
267 if (!ReverifyEntry(trans, update, &target_entry)) {
268 return; // The entry has become irrelevant.
269 }
270
271 // If we're repurposing an existing local entry with a new server ID,
272 // change the ID now, after we're sure that the update can succeed.
273 if (local_id != server_id) {
274 DCHECK(!update.deleted());
275 ChangeEntryIDAndUpdateChildren(trans, &target_entry, server_id);
276 // When IDs change, versions become irrelevant. Forcing BASE_VERSION
277 // to zero would ensure that this update gets applied, but would indicate
278 // creation or undeletion if it were committed that way. Instead, prefer
279 // forcing BASE_VERSION to entry.version() while also forcing
280 // IS_UNAPPLIED_UPDATE to true. If the item is UNSYNCED, it's committable
281 // from the new state; it may commit before the conflict resolver gets
282 // a crack at it.
283 if (target_entry.GetIsUnsynced() || target_entry.GetBaseVersion() > 0) {
284 // If either of these conditions are met, then we can expect valid client
285 // fields for this entry. When BASE_VERSION is positive, consistency is
286 // enforced on the client fields at update-application time. Otherwise,
287 // we leave the BASE_VERSION field alone; it'll get updated the first time
288 // we successfully apply this update.
289 target_entry.PutBaseVersion(update.version());
290 }
291 // Force application of this update, no matter what.
292 target_entry.PutIsUnappliedUpdate(true);
293 }
294
295 // If this is a newly received undecryptable update, and the only thing that
296 // has changed are the specifics, store the original decryptable specifics,
297 // (on which any current or future local changes are based) before we
298 // overwrite SERVER_SPECIFICS.
299 // MTIME, CTIME, and NON_UNIQUE_NAME are not enforced.
300
301 bool position_matches = false;
302 if (target_entry.ShouldMaintainPosition() && !update.deleted()) {
303 std::string update_tag = GetUniqueBookmarkTagFromUpdate(update);
304 if (UniquePosition::IsValidSuffix(update_tag)) {
305 position_matches = GetUpdatePosition(update, update_tag).Equals(
306 target_entry.GetServerUniquePosition());
307 } else {
308 NOTREACHED();
309 }
310 } else {
311 // If this item doesn't care about positions, then set this flag to true.
312 position_matches = true;
313 }
314
315 if (!update.deleted() && !target_entry.GetServerIsDel() &&
316 (SyncableIdFromProto(update.parent_id_string()) ==
317 target_entry.GetServerParentId()) &&
318 position_matches &&
319 update.has_specifics() && update.specifics().has_encrypted() &&
320 !cryptographer->CanDecrypt(update.specifics().encrypted())) {
321 sync_pb::EntitySpecifics prev_specifics =
322 target_entry.GetServerSpecifics();
323 // We only store the old specifics if they were decryptable and applied and
324 // there is no BASE_SERVER_SPECIFICS already. Else do nothing.
325 if (!target_entry.GetIsUnappliedUpdate() &&
326 !IsRealDataType(GetModelTypeFromSpecifics(
327 target_entry.GetBaseServerSpecifics())) &&
328 (!prev_specifics.has_encrypted() ||
329 cryptographer->CanDecrypt(prev_specifics.encrypted()))) {
330 DVLOG(2) << "Storing previous server specifcs: "
331 << prev_specifics.SerializeAsString();
332 target_entry.PutBaseServerSpecifics(prev_specifics);
333 }
334 } else if (IsRealDataType(GetModelTypeFromSpecifics(
335 target_entry.GetBaseServerSpecifics()))) {
336 // We have a BASE_SERVER_SPECIFICS, but a subsequent non-specifics-only
337 // change arrived. As a result, we can't use the specifics alone to detect
338 // changes, so we clear BASE_SERVER_SPECIFICS.
339 target_entry.PutBaseServerSpecifics(
340 sync_pb::EntitySpecifics());
341 }
342
343 UpdateServerFieldsFromUpdate(&target_entry, update, name);
344
345 return;
346 }
347
348 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698