OLD | NEW |
---|---|
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()) | |
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()) |
136 status->increment_num_tombstone_updates_downloaded_by(1); | |
137 | |
138 if (verify_result != VERIFY_SUCCESS && verify_result != VERIFY_UNDELETE) | |
rlarocque
2012/10/08 23:16:41
This function up until this point is mostly copied
| |
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 | |
148 return SYNCER_OK; | |
149 } | |
150 | |
151 namespace { | |
152 | |
153 // In the event that IDs match, but tags differ AttemptReuniteClient tag | |
154 // will have refused to unify the update. | |
155 // We should not attempt to apply it at all since it violates consistency | |
156 // rules. | |
157 VerifyResult VerifyTagConsistency(const sync_pb::SyncEntity& entry, | |
158 const syncable::MutableEntry& same_id) { | |
159 if (entry.has_client_defined_unique_tag() && | |
160 entry.client_defined_unique_tag() != | |
161 same_id.Get(syncable::UNIQUE_CLIENT_TAG)) { | |
162 return VERIFY_FAIL; | |
163 } | |
164 return VERIFY_UNDECIDED; | |
165 } | |
166 | |
167 } // namespace | |
168 | |
169 VerifyResult ProcessUpdatesCommand::VerifyUpdate( | |
170 syncable::WriteTransaction* trans, const sync_pb::SyncEntity& entry, | |
171 ModelTypeSet requested_types, | |
172 const ModelSafeRoutingInfo& routes) { | |
173 syncable::Id id = SyncableIdFromProto(entry.id_string()); | |
174 VerifyResult result = VERIFY_FAIL; | |
175 | |
176 const bool deleted = entry.has_deleted() && entry.deleted(); | |
177 const bool is_directory = IsFolder(entry); | |
178 const ModelType model_type = GetModelType(entry); | |
179 | |
180 if (!id.ServerKnows()) { | |
181 LOG(ERROR) << "Illegal negative id in received updates"; | |
182 return result; | |
183 } | |
184 { | |
185 const std::string name = SyncerProtoUtil::NameFromSyncEntity(entry); | |
186 if (name.empty() && !deleted) { | |
187 LOG(ERROR) << "Zero length name in non-deleted update"; | |
188 return result; | |
65 } | 189 } |
66 } | 190 } |
67 | 191 |
68 StatusController* status = session->mutable_status_controller(); | 192 syncable::MutableEntry same_id(trans, GET_BY_ID, id); |
69 status->mutable_update_progress()->ClearVerifiedUpdates(); | 193 result = VerifyNewEntry(entry, &same_id, deleted); |
70 return SYNCER_OK; | 194 |
195 ModelType placement_type = !deleted ? GetModelType(entry) | |
196 : same_id.good() ? same_id.GetModelType() : UNSPECIFIED; | |
197 | |
198 if (VERIFY_UNDECIDED == result) { | |
199 result = VerifyTagConsistency(entry, same_id); | |
200 } | |
201 | |
202 if (VERIFY_UNDECIDED == result) { | |
203 if (deleted) { | |
204 // For deletes the server could send tombostones for items that | |
205 // the client did not request. If so ignore those items. | |
206 if (IsRealDataType(placement_type) && | |
207 !requested_types.Has(placement_type)) { | |
208 result = VERIFY_SKIP; | |
209 } else { | |
210 result = VERIFY_SUCCESS; | |
211 } | |
212 } | |
213 } | |
214 | |
215 // If we have an existing entry, we check here for updates that break | |
216 // consistency rules. | |
217 if (VERIFY_UNDECIDED == result) { | |
218 result = VerifyUpdateConsistency(trans, entry, &same_id, | |
219 deleted, is_directory, model_type); | |
220 } | |
221 | |
222 if (VERIFY_UNDECIDED == result) | |
223 result = VERIFY_SUCCESS; // No news is good news. | |
224 | |
225 return result; // This might be VERIFY_SUCCESS as well | |
71 } | 226 } |
72 | 227 |
73 namespace { | 228 namespace { |
74 // Returns true if the entry is still ok to process. | 229 // Returns true if the entry is still ok to process. |
75 bool ReverifyEntry(syncable::WriteTransaction* trans, | 230 bool ReverifyEntry(syncable::WriteTransaction* trans, |
76 const sync_pb::SyncEntity& entry, | 231 const sync_pb::SyncEntity& entry, |
77 syncable::MutableEntry* same_id) { | 232 syncable::MutableEntry* same_id) { |
78 | 233 |
79 const bool deleted = entry.has_deleted() && entry.deleted(); | 234 const bool deleted = entry.has_deleted() && entry.deleted(); |
80 const bool is_directory = IsFolder(entry); | 235 const bool is_directory = IsFolder(entry); |
(...skipping 22 matching lines...) Expand all Loading... | |
103 | 258 |
104 // FindLocalEntryToUpdate has veto power. | 259 // FindLocalEntryToUpdate has veto power. |
105 if (local_id.IsNull()) { | 260 if (local_id.IsNull()) { |
106 return SUCCESS_PROCESSED; // The entry has become irrelevant. | 261 return SUCCESS_PROCESSED; // The entry has become irrelevant. |
107 } | 262 } |
108 | 263 |
109 CreateNewEntry(trans, local_id); | 264 CreateNewEntry(trans, local_id); |
110 | 265 |
111 // We take a two step approach. First we store the entries data in the | 266 // 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 | 267 // 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); | 268 syncable::MutableEntry target_entry(trans, GET_BY_ID, local_id); |
114 | 269 |
115 // We need to run the Verify checks again; the world could have changed | 270 // We need to run the Verify checks again; the world could have changed |
116 // since VerifyUpdatesCommand. | 271 // since we last verified. |
rlarocque
2012/10/08 23:16:41
This is at least somewhat redundant. I'll investi
| |
117 if (!ReverifyEntry(trans, update, &target_entry)) { | 272 if (!ReverifyEntry(trans, update, &target_entry)) { |
118 return SUCCESS_PROCESSED; // The entry has become irrelevant. | 273 return SUCCESS_PROCESSED; // The entry has become irrelevant. |
119 } | 274 } |
120 | 275 |
121 // If we're repurposing an existing local entry with a new server ID, | 276 // 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. | 277 // change the ID now, after we're sure that the update can succeed. |
123 if (local_id != server_id) { | 278 if (local_id != server_id) { |
124 DCHECK(!update.deleted()); | 279 DCHECK(!update.deleted()); |
125 ChangeEntryIDAndUpdateChildren(trans, &target_entry, server_id); | 280 ChangeEntryIDAndUpdateChildren(trans, &target_entry, server_id); |
126 // When IDs change, versions become irrelevant. Forcing BASE_VERSION | 281 // When IDs change, versions become irrelevant. Forcing BASE_VERSION |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
176 target_entry.Put(syncable::BASE_SERVER_SPECIFICS, | 331 target_entry.Put(syncable::BASE_SERVER_SPECIFICS, |
177 sync_pb::EntitySpecifics()); | 332 sync_pb::EntitySpecifics()); |
178 } | 333 } |
179 | 334 |
180 UpdateServerFieldsFromUpdate(&target_entry, update, name); | 335 UpdateServerFieldsFromUpdate(&target_entry, update, name); |
181 | 336 |
182 return SUCCESS_PROCESSED; | 337 return SUCCESS_PROCESSED; |
183 } | 338 } |
184 | 339 |
185 } // namespace syncer | 340 } // namespace syncer |
OLD | NEW |