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

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: Minor fixes 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"
11 #include "sync/engine/syncer.h" 11 #include "sync/engine/syncer.h"
12 #include "sync/engine/syncer_proto_util.h" 12 #include "sync/engine/syncer_proto_util.h"
13 #include "sync/engine/syncer_util.h" 13 #include "sync/engine/syncer_util.h"
14 #include "sync/sessions/sync_session.h" 14 #include "sync/sessions/sync_session.h"
15 #include "sync/syncable/directory.h" 15 #include "sync/syncable/directory.h"
16 #include "sync/syncable/mutable_entry.h" 16 #include "sync/syncable/mutable_entry.h"
17 #include "sync/syncable/syncable_proto_util.h" 17 #include "sync/syncable/syncable_proto_util.h"
18 #include "sync/syncable/syncable_util.h" 18 #include "sync/syncable/syncable_util.h"
19 #include "sync/syncable/write_transaction.h" 19 #include "sync/syncable/write_transaction.h"
20 #include "sync/util/cryptographer.h" 20 #include "sync/util/cryptographer.h"
21 21
22 using std::vector; 22 using std::vector;
23 23
24 namespace syncer { 24 namespace syncer {
25 25
26 using sessions::SyncSession; 26 using sessions::SyncSession;
27 using sessions::StatusController; 27 using sessions::StatusController;
28 using sessions::UpdateProgress; 28 using sessions::UpdateProgress;
29 29
30 using syncable::GET_BY_ID;
31
30 ProcessUpdatesCommand::ProcessUpdatesCommand() {} 32 ProcessUpdatesCommand::ProcessUpdatesCommand() {}
31 ProcessUpdatesCommand::~ProcessUpdatesCommand() {} 33 ProcessUpdatesCommand::~ProcessUpdatesCommand() {}
32 34
33 std::set<ModelSafeGroup> ProcessUpdatesCommand::GetGroupsToChange( 35 std::set<ModelSafeGroup> ProcessUpdatesCommand::GetGroupsToChange(
34 const sessions::SyncSession& session) const { 36 const sessions::SyncSession& session) const {
35 return session.GetEnabledGroupsWithVerifiedUpdates(); 37 std::set<ModelSafeGroup> groups_with_updates;
36 } 38
39 const sync_pb::GetUpdatesResponse& updates =
40 session.status_controller().updates_response().get_updates();
41 for (int i = 0; i < updates.entries().size(); i++) {
42 groups_with_updates.insert(
43 GetGroupForModelType(GetModelType(updates.entries(i)),
44 session.routing_info()));
45 }
46
47 return groups_with_updates;
48 }
49
50 namespace {
51
52 // This function attempts to determine whether or not this update is genuinely
53 // new, or if it is a reflection of one of our own commits.
54 //
55 // There is a known inaccuracy in its implementation. If this update ends up
56 // being applied to a local item with a different ID, we will count the change
57 // as being a non-reflection update. Fortunately, the server usually updates
58 // our IDs correctly in its commit response, so a new ID during GetUpdate should
59 // be rare.
60 //
61 // The only secnarios I can think of where this might happen are:
62 // - We commit a new item to the server, but we don't persist the
63 // server-returned new ID to the database before we shut down. On the GetUpdate
64 // following the next restart, we will receive an update from the server that
65 // updates its local ID.
66 // - When two attempts to create an item with identical UNIQUE_CLIENT_TAG values
67 // collide at the server. I have seen this in testing. When it happens, the
68 // test server will send one of the clients a response to upate its local ID so
69 // that both clients will refer to the item using the same ID going forward. In
70 // this case, we're right to assume that the update is not a reflection.
71 //
72 // For more information, see FindLocalIdToUpdate().
73 bool UpdateContainsNewVersion(syncable::BaseTransaction *trans,
74 const sync_pb::SyncEntity &update) {
75 int64 existing_version = -1; // The server always sends positive versions.
76 syncable::Entry existing_entry(trans, GET_BY_ID,
77 SyncableIdFromProto(update.id_string()));
78 if (existing_entry.good())
79 existing_version = existing_entry.Get(syncable::BASE_VERSION);
80
81 if (!existing_entry.good() && update.deleted()) {
82 // There are several possible explanations for this. The most common cases
83 // will be first time sync and the redelivery of deletions we've already
84 // synced, accepted, and purged from our database. In either case, the
85 // update is useless to us. Let's count them all as "not new", even though
86 // that may not always be entirely accurate.
87 return false;
88 }
89
90 if (existing_entry.good() &&
91 !existing_entry.Get(syncable::UNIQUE_CLIENT_TAG).empty() &&
92 existing_entry.Get(syncable::IS_DEL) &&
93 update.deleted()) {
94 // Unique client tags will have their version set to zero when they're
95 // deleted. The usual version comparison logic won't be able to detect
96 // reflections of these items. Instead, we assume any received tombstones
97 // are reflections. That should be correct most of the time.
98 return false;
99 }
100
101 return existing_version < update.version();
102 }
103
104 } // namespace
37 105
38 SyncerError ProcessUpdatesCommand::ModelChangingExecuteImpl( 106 SyncerError ProcessUpdatesCommand::ModelChangingExecuteImpl(
39 SyncSession* session) { 107 SyncSession* session) {
40 syncable::Directory* dir = session->context()->directory(); 108 syncable::Directory* dir = session->context()->directory();
41 109
42 const sessions::UpdateProgress* progress =
43 session->status_controller().update_progress();
44 if (!progress)
45 return SYNCER_OK; // Nothing to do.
46
47 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir); 110 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir);
48 vector<sessions::VerifiedUpdate>::const_iterator it; 111
49 for (it = progress->VerifiedUpdatesBegin(); 112 sessions::StatusController* status = session->mutable_status_controller();
50 it != progress->VerifiedUpdatesEnd(); 113 const sync_pb::GetUpdatesResponse& updates =
51 ++it) { 114 status->updates_response().get_updates();
52 const sync_pb::SyncEntity& update = it->second; 115 int update_count = updates.entries().size();
53 116
54 if (it->first != VERIFY_SUCCESS && it->first != VERIFY_UNDELETE) 117 ModelTypeSet requested_types = GetRoutingInfoTypes(
118 session->routing_info());
119
120 DVLOG(1) << update_count << " entries to verify";
121 for (int i = 0; i < update_count; i++) {
122 const sync_pb::SyncEntity& update = updates.entries(i);
123 ModelSafeGroup g = GetGroupForModelType(GetModelType(update),
124 session->routing_info());
125 if (g != status->group_restriction())
tim (not reviewing) 2012/10/15 21:06:52 Add a comment here that we have to filter by Model
rlarocque 2012/10/15 22:47:32 Done.
55 continue; 126 continue;
56 switch (ProcessUpdate(update, 127
57 dir->GetCryptographer(&trans), 128 VerifyResult verify_result = VerifyUpdate(&trans, update,
58 &trans)) { 129 requested_types,
59 case SUCCESS_PROCESSED: 130 session->routing_info());
60 case SUCCESS_STORED: 131 status->mutable_update_progress()->AddVerifyResult(verify_result, update);
61 break; 132 status->increment_num_updates_downloaded_by(1);
62 default: 133 if (!UpdateContainsNewVersion(&trans, update))
63 NOTREACHED(); 134 status->increment_num_reflected_updates_downloaded_by(1);
64 break; 135 if (update.deleted())
65 } 136 status->increment_num_tombstone_updates_downloaded_by(1);
66 } 137
67 138 if (verify_result != VERIFY_SUCCESS && verify_result != VERIFY_UNDELETE)
68 StatusController* status = session->mutable_status_controller(); 139 continue;
140
141 ServerUpdateProcessingResult process_result =
142 ProcessUpdate(update, dir->GetCryptographer(&trans), &trans);
143
144 DCHECK(process_result == SUCCESS_PROCESSED ||
145 process_result == SUCCESS_STORED);
146 }
147
69 status->mutable_update_progress()->ClearVerifiedUpdates(); 148 status->mutable_update_progress()->ClearVerifiedUpdates();
70 return SYNCER_OK; 149 return SYNCER_OK;
71 } 150 }
72 151
73 namespace { 152 namespace {
153
154 // In the event that IDs match, but tags differ AttemptReuniteClient tag
155 // will have refused to unify the update.
156 // We should not attempt to apply it at all since it violates consistency
157 // rules.
158 VerifyResult VerifyTagConsistency(const sync_pb::SyncEntity& entry,
159 const syncable::MutableEntry& same_id) {
160 if (entry.has_client_defined_unique_tag() &&
161 entry.client_defined_unique_tag() !=
162 same_id.Get(syncable::UNIQUE_CLIENT_TAG)) {
163 return VERIFY_FAIL;
164 }
165 return VERIFY_UNDECIDED;
166 }
167
168 } // namespace
169
170 VerifyResult ProcessUpdatesCommand::VerifyUpdate(
171 syncable::WriteTransaction* trans, const sync_pb::SyncEntity& entry,
172 ModelTypeSet requested_types,
173 const ModelSafeRoutingInfo& routes) {
174 syncable::Id id = SyncableIdFromProto(entry.id_string());
175 VerifyResult result = VERIFY_FAIL;
176
177 const bool deleted = entry.has_deleted() && entry.deleted();
178 const bool is_directory = IsFolder(entry);
179 const ModelType model_type = GetModelType(entry);
180
181 if (!id.ServerKnows()) {
182 LOG(ERROR) << "Illegal negative id in received updates";
183 return result;
184 }
185 {
186 const std::string name = SyncerProtoUtil::NameFromSyncEntity(entry);
187 if (name.empty() && !deleted) {
188 LOG(ERROR) << "Zero length name in non-deleted update";
189 return result;
190 }
191 }
192
193 syncable::MutableEntry same_id(trans, GET_BY_ID, id);
194 result = VerifyNewEntry(entry, &same_id, deleted);
195
196 ModelType placement_type = !deleted ? GetModelType(entry)
197 : same_id.good() ? same_id.GetModelType() : UNSPECIFIED;
198
199 if (VERIFY_UNDECIDED == result) {
200 result = VerifyTagConsistency(entry, same_id);
201 }
202
203 if (VERIFY_UNDECIDED == result) {
204 if (deleted) {
205 // For deletes the server could send tombostones for items that
206 // the client did not request. If so ignore those items.
207 if (IsRealDataType(placement_type) &&
208 !requested_types.Has(placement_type)) {
209 result = VERIFY_SKIP;
210 } else {
211 result = VERIFY_SUCCESS;
212 }
213 }
214 }
215
216 // If we have an existing entry, we check here for updates that break
217 // consistency rules.
218 if (VERIFY_UNDECIDED == result) {
219 result = VerifyUpdateConsistency(trans, entry, &same_id,
220 deleted, is_directory, model_type);
221 }
222
223 if (VERIFY_UNDECIDED == result)
224 result = VERIFY_SUCCESS; // No news is good news.
225
226 return result; // This might be VERIFY_SUCCESS as well
227 }
228
229 namespace {
74 // Returns true if the entry is still ok to process. 230 // Returns true if the entry is still ok to process.
75 bool ReverifyEntry(syncable::WriteTransaction* trans, 231 bool ReverifyEntry(syncable::WriteTransaction* trans,
76 const sync_pb::SyncEntity& entry, 232 const sync_pb::SyncEntity& entry,
77 syncable::MutableEntry* same_id) { 233 syncable::MutableEntry* same_id) {
78 234
79 const bool deleted = entry.has_deleted() && entry.deleted(); 235 const bool deleted = entry.has_deleted() && entry.deleted();
80 const bool is_directory = IsFolder(entry); 236 const bool is_directory = IsFolder(entry);
81 const ModelType model_type = GetModelType(entry); 237 const ModelType model_type = GetModelType(entry);
82 238
83 return VERIFY_SUCCESS == VerifyUpdateConsistency(trans, 239 return VERIFY_SUCCESS == VerifyUpdateConsistency(trans,
(...skipping 19 matching lines...) Expand all
103 259
104 // FindLocalEntryToUpdate has veto power. 260 // FindLocalEntryToUpdate has veto power.
105 if (local_id.IsNull()) { 261 if (local_id.IsNull()) {
106 return SUCCESS_PROCESSED; // The entry has become irrelevant. 262 return SUCCESS_PROCESSED; // The entry has become irrelevant.
107 } 263 }
108 264
109 CreateNewEntry(trans, local_id); 265 CreateNewEntry(trans, local_id);
110 266
111 // We take a two step approach. First we store the entries data in the 267 // We take a two step approach. First we store the entries data in the
112 // server fields of a local entry and then move the data to the local fields 268 // server fields of a local entry and then move the data to the local fields
113 syncable::MutableEntry target_entry(trans, syncable::GET_BY_ID, local_id); 269 syncable::MutableEntry target_entry(trans, GET_BY_ID, local_id);
114 270
115 // We need to run the Verify checks again; the world could have changed 271 // We need to run the Verify checks again; the world could have changed
116 // since VerifyUpdatesCommand. 272 // since we last verified.
117 if (!ReverifyEntry(trans, update, &target_entry)) { 273 if (!ReverifyEntry(trans, update, &target_entry)) {
118 return SUCCESS_PROCESSED; // The entry has become irrelevant. 274 return SUCCESS_PROCESSED; // The entry has become irrelevant.
119 } 275 }
120 276
121 // If we're repurposing an existing local entry with a new server ID, 277 // If we're repurposing an existing local entry with a new server ID,
122 // change the ID now, after we're sure that the update can succeed. 278 // change the ID now, after we're sure that the update can succeed.
123 if (local_id != server_id) { 279 if (local_id != server_id) {
124 DCHECK(!update.deleted()); 280 DCHECK(!update.deleted());
125 ChangeEntryIDAndUpdateChildren(trans, &target_entry, server_id); 281 ChangeEntryIDAndUpdateChildren(trans, &target_entry, server_id);
126 // When IDs change, versions become irrelevant. Forcing BASE_VERSION 282 // When IDs change, versions become irrelevant. Forcing BASE_VERSION
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 target_entry.Put(syncable::BASE_SERVER_SPECIFICS, 332 target_entry.Put(syncable::BASE_SERVER_SPECIFICS,
177 sync_pb::EntitySpecifics()); 333 sync_pb::EntitySpecifics());
178 } 334 }
179 335
180 UpdateServerFieldsFromUpdate(&target_entry, update, name); 336 UpdateServerFieldsFromUpdate(&target_entry, update, name);
181 337
182 return SUCCESS_PROCESSED; 338 return SUCCESS_PROCESSED;
183 } 339 }
184 340
185 } // namespace syncer 341 } // 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