OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 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_util.h" |
6 | 6 |
7 #include <vector> | |
8 | |
9 #include "base/basictypes.h" | |
10 #include "base/location.h" | 7 #include "base/location.h" |
11 #include "sync/engine/syncer.h" | |
12 #include "sync/engine/syncer_proto_util.h" | 8 #include "sync/engine/syncer_proto_util.h" |
13 #include "sync/engine/syncer_util.h" | 9 #include "sync/engine/syncer_util.h" |
14 #include "sync/sessions/sync_session.h" | |
15 #include "sync/syncable/directory.h" | 10 #include "sync/syncable/directory.h" |
16 #include "sync/syncable/model_neutral_mutable_entry.h" | 11 #include "sync/syncable/model_neutral_mutable_entry.h" |
17 #include "sync/syncable/syncable_model_neutral_write_transaction.h" | 12 #include "sync/syncable/syncable_model_neutral_write_transaction.h" |
18 #include "sync/syncable/syncable_proto_util.h" | 13 #include "sync/syncable/syncable_proto_util.h" |
19 #include "sync/syncable/syncable_util.h" | 14 #include "sync/syncable/syncable_util.h" |
20 #include "sync/util/cryptographer.h" | 15 #include "sync/util/cryptographer.h" |
21 | 16 |
22 using std::vector; | |
23 | |
24 namespace syncer { | 17 namespace syncer { |
25 | 18 |
26 using sessions::SyncSession; | |
27 using sessions::StatusController; | 19 using sessions::StatusController; |
28 | 20 |
29 using syncable::GET_BY_ID; | 21 using syncable::GET_BY_ID; |
30 | 22 |
31 ProcessUpdatesCommand::ProcessUpdatesCommand() {} | |
32 ProcessUpdatesCommand::~ProcessUpdatesCommand() {} | |
33 | |
34 namespace { | 23 namespace { |
35 | 24 |
36 // This function attempts to determine whether or not this update is genuinely | 25 // 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. | 26 // new, or if it is a reflection of one of our own commits. |
38 // | 27 // |
39 // There is a known inaccuracy in its implementation. If this update ends up | 28 // 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 | 29 // 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 | 30 // 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 | 31 // our IDs correctly in its commit response, so a new ID during GetUpdate should |
43 // be rare. | 32 // be rare. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 // reflections of these items. Instead, we assume any received tombstones | 69 // reflections of these items. Instead, we assume any received tombstones |
81 // are reflections. That should be correct most of the time. | 70 // are reflections. That should be correct most of the time. |
82 return false; | 71 return false; |
83 } | 72 } |
84 | 73 |
85 return existing_version < update.version(); | 74 return existing_version < update.version(); |
86 } | 75 } |
87 | 76 |
88 } // namespace | 77 } // namespace |
89 | 78 |
90 SyncerError ProcessUpdatesCommand::ExecuteImpl(SyncSession* session) { | 79 void PartitionUpdatesByType( |
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, | 80 const sync_pb::GetUpdatesResponse& updates, |
81 ModelTypeSet requested_types, | |
134 TypeSyncEntityMap* updates_by_type) { | 82 TypeSyncEntityMap* updates_by_type) { |
135 int update_count = updates.entries().size(); | 83 int update_count = updates.entries().size(); |
84 for (ModelTypeSet::Iterator it = requested_types.First(); | |
85 it.Good(); it.Inc()) { | |
86 updates_by_type->insert(std::make_pair(it.Get(), SyncEntityList())); | |
87 } | |
136 for (int i = 0; i < update_count; ++i) { | 88 for (int i = 0; i < update_count; ++i) { |
137 const sync_pb::SyncEntity& update = updates.entries(i); | 89 const sync_pb::SyncEntity& update = updates.entries(i); |
138 ModelType type = GetModelType(update); | 90 ModelType type = GetModelType(update); |
139 DCHECK(IsRealDataType(type)); | 91 if (!IsRealDataType(type)) { |
140 (*updates_by_type)[type].push_back(&update); | 92 NOTREACHED() << "Received update with invalid type."; |
93 continue; | |
94 } | |
95 | |
96 TypeSyncEntityMap::iterator it = updates_by_type->find(type); | |
97 if (it == updates_by_type->end()) { | |
98 DLOG(WARNING) << "Skipping update for unexpected type " | |
99 << ModelTypeToString(type); | |
Nicolas Zea
2013/10/25 21:04:41
nit: align <<
rlarocque
2013/10/25 22:29:47
Done.
| |
100 continue; | |
101 } | |
102 | |
103 it->second.push_back(&update); | |
Nicolas Zea
2013/10/25 21:04:41
Same comment applies here. I think you can get the
rlarocque
2013/10/25 22:29:47
The trade off here is not so clear. We'd need to
| |
141 } | 104 } |
142 } | 105 } |
143 | 106 |
107 void ProcessDownloadedUpdates( | |
108 syncable::Directory* dir, | |
109 syncable::ModelNeutralWriteTransaction* trans, | |
110 ModelType type, | |
111 const SyncEntityList& applicable_updates, | |
112 sessions::StatusController* status) { | |
113 for (SyncEntityList::const_iterator update_it = applicable_updates.begin(); | |
114 update_it != applicable_updates.end(); ++update_it) { | |
115 DCHECK_EQ(type, GetModelType(**update_it)); | |
116 if (!UpdateContainsNewVersion(trans, **update_it)) | |
117 status->increment_num_reflected_updates_downloaded_by(1); | |
118 if ((*update_it)->deleted()) | |
119 status->increment_num_tombstone_updates_downloaded_by(1); | |
120 VerifyResult verify_result = VerifyUpdate(trans, **update_it, type); | |
121 if (verify_result != VERIFY_SUCCESS && verify_result != VERIFY_UNDELETE) | |
122 continue; | |
123 ProcessUpdate(**update_it, dir->GetCryptographer(trans), trans); | |
124 } | |
125 } | |
126 | |
144 namespace { | 127 namespace { |
145 | 128 |
146 // In the event that IDs match, but tags differ AttemptReuniteClient tag | 129 // In the event that IDs match, but tags differ AttemptReuniteClient tag |
147 // will have refused to unify the update. | 130 // will have refused to unify the update. |
148 // We should not attempt to apply it at all since it violates consistency | 131 // We should not attempt to apply it at all since it violates consistency |
149 // rules. | 132 // rules. |
150 VerifyResult VerifyTagConsistency( | 133 VerifyResult VerifyTagConsistency( |
151 const sync_pb::SyncEntity& entry, | 134 const sync_pb::SyncEntity& entry, |
152 const syncable::ModelNeutralMutableEntry& same_id) { | 135 const syncable::ModelNeutralMutableEntry& same_id) { |
153 if (entry.has_client_defined_unique_tag() && | 136 if (entry.has_client_defined_unique_tag() && |
154 entry.client_defined_unique_tag() != | 137 entry.client_defined_unique_tag() != |
155 same_id.GetUniqueClientTag()) { | 138 same_id.GetUniqueClientTag()) { |
156 return VERIFY_FAIL; | 139 return VERIFY_FAIL; |
157 } | 140 } |
158 return VERIFY_UNDECIDED; | 141 return VERIFY_UNDECIDED; |
159 } | 142 } |
160 | 143 |
161 } // namespace | 144 } // namespace |
162 | 145 |
163 VerifyResult ProcessUpdatesCommand::VerifyUpdate( | 146 VerifyResult VerifyUpdate( |
164 syncable::ModelNeutralWriteTransaction* trans, | 147 syncable::ModelNeutralWriteTransaction* trans, |
165 const sync_pb::SyncEntity& entry, | 148 const sync_pb::SyncEntity& entry, |
166 ModelTypeSet requested_types, | 149 ModelType requested_type) { |
167 const ModelSafeRoutingInfo& routes) { | |
168 syncable::Id id = SyncableIdFromProto(entry.id_string()); | 150 syncable::Id id = SyncableIdFromProto(entry.id_string()); |
169 VerifyResult result = VERIFY_FAIL; | 151 VerifyResult result = VERIFY_FAIL; |
170 | 152 |
171 const bool deleted = entry.has_deleted() && entry.deleted(); | 153 const bool deleted = entry.has_deleted() && entry.deleted(); |
172 const bool is_directory = IsFolder(entry); | 154 const bool is_directory = IsFolder(entry); |
173 const ModelType model_type = GetModelType(entry); | 155 const ModelType model_type = GetModelType(entry); |
174 | 156 |
175 if (!id.ServerKnows()) { | 157 if (!id.ServerKnows()) { |
176 LOG(ERROR) << "Illegal negative id in received updates"; | 158 LOG(ERROR) << "Illegal negative id in received updates"; |
177 return result; | 159 return result; |
(...skipping 13 matching lines...) Expand all Loading... | |
191 : same_id.good() ? same_id.GetModelType() : UNSPECIFIED; | 173 : same_id.good() ? same_id.GetModelType() : UNSPECIFIED; |
192 | 174 |
193 if (VERIFY_UNDECIDED == result) { | 175 if (VERIFY_UNDECIDED == result) { |
194 result = VerifyTagConsistency(entry, same_id); | 176 result = VerifyTagConsistency(entry, same_id); |
195 } | 177 } |
196 | 178 |
197 if (VERIFY_UNDECIDED == result) { | 179 if (VERIFY_UNDECIDED == result) { |
198 if (deleted) { | 180 if (deleted) { |
199 // For deletes the server could send tombostones for items that | 181 // For deletes the server could send tombostones for items that |
200 // the client did not request. If so ignore those items. | 182 // the client did not request. If so ignore those items. |
201 if (IsRealDataType(placement_type) && | 183 if (IsRealDataType(placement_type) && requested_type != placement_type) { |
202 !requested_types.Has(placement_type)) { | |
203 result = VERIFY_SKIP; | 184 result = VERIFY_SKIP; |
204 } else { | 185 } else { |
205 result = VERIFY_SUCCESS; | 186 result = VERIFY_SUCCESS; |
206 } | 187 } |
207 } | 188 } |
208 } | 189 } |
209 | 190 |
210 // If we have an existing entry, we check here for updates that break | 191 // If we have an existing entry, we check here for updates that break |
211 // consistency rules. | 192 // consistency rules. |
212 if (VERIFY_UNDECIDED == result) { | 193 if (VERIFY_UNDECIDED == result) { |
(...skipping 20 matching lines...) Expand all Loading... | |
233 return VERIFY_SUCCESS == VerifyUpdateConsistency(trans, | 214 return VERIFY_SUCCESS == VerifyUpdateConsistency(trans, |
234 entry, | 215 entry, |
235 deleted, | 216 deleted, |
236 is_directory, | 217 is_directory, |
237 model_type, | 218 model_type, |
238 same_id); | 219 same_id); |
239 } | 220 } |
240 } // namespace | 221 } // namespace |
241 | 222 |
242 // Process a single update. Will avoid touching global state. | 223 // Process a single update. Will avoid touching global state. |
243 void ProcessUpdatesCommand::ProcessUpdate( | 224 void ProcessUpdate( |
244 const sync_pb::SyncEntity& update, | 225 const sync_pb::SyncEntity& update, |
245 const Cryptographer* cryptographer, | 226 const Cryptographer* cryptographer, |
246 syncable::ModelNeutralWriteTransaction* const trans) { | 227 syncable::ModelNeutralWriteTransaction* const trans) { |
247 const syncable::Id& server_id = SyncableIdFromProto(update.id_string()); | 228 const syncable::Id& server_id = SyncableIdFromProto(update.id_string()); |
248 const std::string name = SyncerProtoUtil::NameFromSyncEntity(update); | 229 const std::string name = SyncerProtoUtil::NameFromSyncEntity(update); |
249 | 230 |
250 // Look to see if there's a local item that should recieve this update, | 231 // 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. | 232 // maybe due to a duplicate client tag or a lost commit response. |
252 syncable::Id local_id = FindLocalIdToUpdate(trans, update); | 233 syncable::Id local_id = FindLocalIdToUpdate(trans, update); |
253 | 234 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
339 target_entry.PutBaseServerSpecifics( | 320 target_entry.PutBaseServerSpecifics( |
340 sync_pb::EntitySpecifics()); | 321 sync_pb::EntitySpecifics()); |
341 } | 322 } |
342 | 323 |
343 UpdateServerFieldsFromUpdate(&target_entry, update, name); | 324 UpdateServerFieldsFromUpdate(&target_entry, update, name); |
344 | 325 |
345 return; | 326 return; |
346 } | 327 } |
347 | 328 |
348 } // namespace syncer | 329 } // namespace syncer |
OLD | NEW |