OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/sync/engine/get_commit_ids_command.h" | |
6 | |
7 #include <set> | |
8 #include <utility> | |
9 #include <vector> | |
10 | |
11 #include "chrome/browser/sync/engine/nigori_util.h" | |
12 #include "chrome/browser/sync/engine/syncer_util.h" | |
13 #include "chrome/browser/sync/syncable/syncable.h" | |
14 #include "chrome/browser/sync/util/cryptographer.h" | |
15 | |
16 using std::set; | |
17 using std::vector; | |
18 | |
19 namespace browser_sync { | |
20 | |
21 using sessions::OrderedCommitSet; | |
22 using sessions::SyncSession; | |
23 using sessions::StatusController; | |
24 | |
25 GetCommitIdsCommand::GetCommitIdsCommand(int commit_batch_size) | |
26 : requested_commit_batch_size_(commit_batch_size) {} | |
27 | |
28 GetCommitIdsCommand::~GetCommitIdsCommand() {} | |
29 | |
30 SyncerError GetCommitIdsCommand::ExecuteImpl(SyncSession* session) { | |
31 // Gather the full set of unsynced items and store it in the session. They | |
32 // are not in the correct order for commit. | |
33 std::set<int64> ready_unsynced_set; | |
34 syncable::Directory::UnsyncedMetaHandles all_unsynced_handles; | |
35 SyncerUtil::GetUnsyncedEntries(session->write_transaction(), | |
36 &all_unsynced_handles); | |
37 | |
38 syncable::ModelTypeSet encrypted_types; | |
39 bool passphrase_missing = false; | |
40 Cryptographer* cryptographer = | |
41 session->context()-> | |
42 directory()->GetCryptographer(session->write_transaction()); | |
43 if (cryptographer) { | |
44 encrypted_types = cryptographer->GetEncryptedTypes(); | |
45 passphrase_missing = cryptographer->has_pending_keys(); | |
46 }; | |
47 | |
48 const syncable::ModelTypeSet throttled_types = | |
49 session->context()->GetThrottledTypes(); | |
50 // We filter out all unready entries from the set of unsynced handles. This | |
51 // new set of ready and unsynced items (which excludes throttled items as | |
52 // well) is then what we use to determine what is a candidate for commit. | |
53 FilterUnreadyEntries(session->write_transaction(), | |
54 throttled_types, | |
55 encrypted_types, | |
56 passphrase_missing, | |
57 all_unsynced_handles, | |
58 &ready_unsynced_set); | |
59 | |
60 BuildCommitIds(session->write_transaction(), | |
61 session->routing_info(), | |
62 ready_unsynced_set); | |
63 | |
64 StatusController* status = session->mutable_status_controller(); | |
65 syncable::Directory::UnsyncedMetaHandles ready_unsynced_vector( | |
66 ready_unsynced_set.begin(), ready_unsynced_set.end()); | |
67 status->set_unsynced_handles(ready_unsynced_vector); | |
68 const vector<syncable::Id>& verified_commit_ids = | |
69 ordered_commit_set_->GetAllCommitIds(); | |
70 | |
71 for (size_t i = 0; i < verified_commit_ids.size(); i++) | |
72 DVLOG(1) << "Debug commit batch result:" << verified_commit_ids[i]; | |
73 | |
74 status->set_commit_set(*ordered_commit_set_.get()); | |
75 return SYNCER_OK; | |
76 } | |
77 | |
78 namespace { | |
79 | |
80 bool IsEntryInConflict(const syncable::Entry& entry) { | |
81 if (entry.Get(syncable::IS_UNSYNCED) && | |
82 entry.Get(syncable::SERVER_VERSION) > 0 && | |
83 (entry.Get(syncable::SERVER_VERSION) > | |
84 entry.Get(syncable::BASE_VERSION))) { | |
85 // The local and server versions don't match. The item must be in | |
86 // conflict, so there's no point in attempting to commit. | |
87 DCHECK(entry.Get(syncable::IS_UNAPPLIED_UPDATE)); | |
88 DVLOG(1) << "Excluding entry from commit due to version mismatch " | |
89 << entry; | |
90 return true; | |
91 } | |
92 return false; | |
93 } | |
94 | |
95 // An entry is not considered ready for commit if any are true: | |
96 // 1. It's in conflict. | |
97 // 2. It requires encryption (either the type is encrypted but a passphrase | |
98 // is missing from the cryptographer, or the entry itself wasn't properly | |
99 // encrypted). | |
100 // 3. It's type is currently throttled. | |
101 // 4. It's a delete but has not been committed. | |
102 bool IsEntryReadyForCommit(syncable::ModelTypeSet throttled_types, | |
103 syncable::ModelTypeSet encrypted_types, | |
104 bool passphrase_missing, | |
105 const syncable::Entry& entry) { | |
106 DCHECK(entry.Get(syncable::IS_UNSYNCED)); | |
107 if (IsEntryInConflict(entry)) | |
108 return false; | |
109 | |
110 const syncable::ModelType type = entry.GetModelType(); | |
111 // We special case the nigori node because even though it is considered an | |
112 // "encrypted type", not all nigori node changes require valid encryption | |
113 // (ex: sync_tabs). | |
114 if ((type != syncable::NIGORI) && | |
115 encrypted_types.Has(type) && | |
116 (passphrase_missing || | |
117 syncable::EntryNeedsEncryption(encrypted_types, entry))) { | |
118 // This entry requires encryption but is not properly encrypted (possibly | |
119 // due to the cryptographer not being initialized or the user hasn't | |
120 // provided the most recent passphrase). | |
121 DVLOG(1) << "Excluding entry from commit due to lack of encryption " | |
122 << entry; | |
123 return false; | |
124 } | |
125 | |
126 // Look at the throttled types. | |
127 if (throttled_types.Has(type)) | |
128 return false; | |
129 | |
130 // Drop deleted uncommitted entries. | |
131 if (entry.Get(syncable::IS_DEL) && !entry.Get(syncable::ID).ServerKnows()) { | |
132 // TODO(zea): These will remain unsynced indefinitely. This is harmless, | |
133 // but we should clean them up somewhere. | |
134 DVLOG(1) << "Ignoring deleted and uncommitted item." << entry; | |
135 return false; | |
136 } | |
137 | |
138 // Extra validity checks. | |
139 syncable::Id id = entry.Get(syncable::ID); | |
140 if (id == entry.Get(syncable::PARENT_ID)) { | |
141 CHECK(id.IsRoot()) << "Non-root item is self parenting." << entry; | |
142 // If the root becomes unsynced it can cause us problems. | |
143 NOTREACHED() << "Root item became unsynced " << entry; | |
144 return false; | |
145 } | |
146 | |
147 if (entry.IsRoot()) { | |
148 NOTREACHED() << "Permanent item became unsynced " << entry; | |
149 return false; | |
150 } | |
151 | |
152 DVLOG(2) << "Entry is ready for commit: " << entry; | |
153 return true; | |
154 } | |
155 | |
156 } // namespace | |
157 | |
158 void GetCommitIdsCommand::FilterUnreadyEntries( | |
159 syncable::BaseTransaction* trans, | |
160 syncable::ModelTypeSet throttled_types, | |
161 syncable::ModelTypeSet encrypted_types, | |
162 bool passphrase_missing, | |
163 const syncable::Directory::UnsyncedMetaHandles& unsynced_handles, | |
164 std::set<int64>* ready_unsynced_set) { | |
165 for (syncable::Directory::UnsyncedMetaHandles::const_iterator iter = | |
166 unsynced_handles.begin(); iter != unsynced_handles.end(); ++iter) { | |
167 syncable::Entry entry(trans, syncable::GET_BY_HANDLE, *iter); | |
168 if (IsEntryReadyForCommit(throttled_types, | |
169 encrypted_types, | |
170 passphrase_missing, | |
171 entry)) { | |
172 ready_unsynced_set->insert(*iter); | |
173 } | |
174 } | |
175 } | |
176 | |
177 bool GetCommitIdsCommand::AddUncommittedParentsAndTheirPredecessors( | |
178 syncable::BaseTransaction* trans, | |
179 const ModelSafeRoutingInfo& routes, | |
180 const std::set<int64>& ready_unsynced_set, | |
181 const syncable::Entry& item, | |
182 sessions::OrderedCommitSet* result) const { | |
183 OrderedCommitSet item_dependencies(routes); | |
184 syncable::Id parent_id = item.Get(syncable::PARENT_ID); | |
185 | |
186 // Climb the tree adding entries leaf -> root. | |
187 while (!parent_id.ServerKnows()) { | |
188 syncable::Entry parent(trans, syncable::GET_BY_ID, parent_id); | |
189 CHECK(parent.good()) << "Bad user-only parent in item path."; | |
190 int64 handle = parent.Get(syncable::META_HANDLE); | |
191 if (ordered_commit_set_->HaveCommitItem(handle)) { | |
192 // We've already added this parent (and therefore all of its parents). | |
193 // We can return early. | |
194 break; | |
195 } | |
196 if (!AddItemThenPredecessors(trans, ready_unsynced_set, parent, | |
197 &item_dependencies)) { | |
198 // There was a parent/predecessor in conflict. We return without adding | |
199 // anything to |ordered_commit_set_|. | |
200 DVLOG(1) << "Parent or parent's predecessor was in conflict, omitting " | |
201 << item; | |
202 return false; | |
203 } | |
204 parent_id = parent.Get(syncable::PARENT_ID); | |
205 } | |
206 | |
207 // Reverse what we added to get the correct order. | |
208 result->AppendReverse(item_dependencies); | |
209 return true; | |
210 } | |
211 | |
212 bool GetCommitIdsCommand::AddItem(const std::set<int64>& ready_unsynced_set, | |
213 const syncable::Entry& item, | |
214 OrderedCommitSet* result) const { | |
215 DCHECK(item.Get(syncable::IS_UNSYNCED)); | |
216 // An item in conflict means that dependent items (successors and children) | |
217 // cannot be added either. | |
218 if (IsEntryInConflict(item)) | |
219 return false; | |
220 int64 item_handle = item.Get(syncable::META_HANDLE); | |
221 if (ready_unsynced_set.count(item_handle) == 0) { | |
222 // It's not in conflict, but not ready for commit. Just return true without | |
223 // adding it to the commit set. | |
224 return true; | |
225 } | |
226 result->AddCommitItem(item_handle, item.Get(syncable::ID), | |
227 item.GetModelType()); | |
228 return true; | |
229 } | |
230 | |
231 bool GetCommitIdsCommand::AddItemThenPredecessors( | |
232 syncable::BaseTransaction* trans, | |
233 const std::set<int64>& ready_unsynced_set, | |
234 const syncable::Entry& item, | |
235 OrderedCommitSet* result) const { | |
236 int64 item_handle = item.Get(syncable::META_HANDLE); | |
237 if (ordered_commit_set_->HaveCommitItem(item_handle)) { | |
238 // We've already added this item to the commit set, and so must have | |
239 // already added the predecessors as well. | |
240 return true; | |
241 } | |
242 if (!AddItem(ready_unsynced_set, item, result)) | |
243 return false; // Item is in conflict. | |
244 if (item.Get(syncable::IS_DEL)) | |
245 return true; // Deleted items have no predecessors. | |
246 | |
247 syncable::Id prev_id = item.Get(syncable::PREV_ID); | |
248 while (!prev_id.IsRoot()) { | |
249 syncable::Entry prev(trans, syncable::GET_BY_ID, prev_id); | |
250 CHECK(prev.good()) << "Bad id when walking predecessors."; | |
251 if (!prev.Get(syncable::IS_UNSYNCED)) | |
252 break; | |
253 int64 handle = prev.Get(syncable::META_HANDLE); | |
254 if (ordered_commit_set_->HaveCommitItem(handle)) { | |
255 // We've already added this item to the commit set, and so must have | |
256 // already added the predecessors as well. | |
257 return true; | |
258 } | |
259 if (!AddItem(ready_unsynced_set, prev, result)) | |
260 return false; // Item is in conflict. | |
261 prev_id = prev.Get(syncable::PREV_ID); | |
262 } | |
263 return true; | |
264 } | |
265 | |
266 bool GetCommitIdsCommand::AddPredecessorsThenItem( | |
267 syncable::BaseTransaction* trans, | |
268 const ModelSafeRoutingInfo& routes, | |
269 const std::set<int64>& ready_unsynced_set, | |
270 const syncable::Entry& item, | |
271 OrderedCommitSet* result) const { | |
272 OrderedCommitSet item_dependencies(routes); | |
273 if (!AddItemThenPredecessors(trans, ready_unsynced_set, item, | |
274 &item_dependencies)) { | |
275 // Either the item or its predecessors are in conflict, so don't add any | |
276 // items to the commit set. | |
277 DVLOG(1) << "Predecessor was in conflict, omitting " << item; | |
278 return false; | |
279 } | |
280 | |
281 // Reverse what we added to get the correct order. | |
282 result->AppendReverse(item_dependencies); | |
283 return true; | |
284 } | |
285 | |
286 bool GetCommitIdsCommand::IsCommitBatchFull() const { | |
287 return ordered_commit_set_->Size() >= requested_commit_batch_size_; | |
288 } | |
289 | |
290 void GetCommitIdsCommand::AddCreatesAndMoves( | |
291 syncable::WriteTransaction* write_transaction, | |
292 const ModelSafeRoutingInfo& routes, | |
293 const std::set<int64>& ready_unsynced_set) { | |
294 // Add moves and creates, and prepend their uncommitted parents. | |
295 for (std::set<int64>::const_iterator iter = ready_unsynced_set.begin(); | |
296 !IsCommitBatchFull() && iter != ready_unsynced_set.end(); ++iter) { | |
297 int64 metahandle = *iter; | |
298 if (ordered_commit_set_->HaveCommitItem(metahandle)) | |
299 continue; | |
300 | |
301 syncable::Entry entry(write_transaction, | |
302 syncable::GET_BY_HANDLE, | |
303 metahandle); | |
304 if (!entry.Get(syncable::IS_DEL)) { | |
305 // We only commit an item + its dependencies if it and all its | |
306 // dependencies are not in conflict. | |
307 OrderedCommitSet item_dependencies(routes); | |
308 if (AddUncommittedParentsAndTheirPredecessors( | |
309 write_transaction, | |
310 routes, | |
311 ready_unsynced_set, | |
312 entry, | |
313 &item_dependencies) && | |
314 AddPredecessorsThenItem(write_transaction, | |
315 routes, | |
316 ready_unsynced_set, | |
317 entry, | |
318 &item_dependencies)) { | |
319 ordered_commit_set_->Append(item_dependencies); | |
320 } | |
321 } | |
322 } | |
323 | |
324 // It's possible that we overcommitted while trying to expand dependent | |
325 // items. If so, truncate the set down to the allowed size. | |
326 ordered_commit_set_->Truncate(requested_commit_batch_size_); | |
327 } | |
328 | |
329 void GetCommitIdsCommand::AddDeletes( | |
330 syncable::WriteTransaction* write_transaction, | |
331 const std::set<int64>& ready_unsynced_set) { | |
332 set<syncable::Id> legal_delete_parents; | |
333 | |
334 for (std::set<int64>::const_iterator iter = ready_unsynced_set.begin(); | |
335 !IsCommitBatchFull() && iter != ready_unsynced_set.end(); ++iter) { | |
336 int64 metahandle = *iter; | |
337 if (ordered_commit_set_->HaveCommitItem(metahandle)) | |
338 continue; | |
339 | |
340 syncable::Entry entry(write_transaction, syncable::GET_BY_HANDLE, | |
341 metahandle); | |
342 | |
343 if (entry.Get(syncable::IS_DEL)) { | |
344 syncable::Entry parent(write_transaction, syncable::GET_BY_ID, | |
345 entry.Get(syncable::PARENT_ID)); | |
346 // If the parent is deleted and unsynced, then any children of that | |
347 // parent don't need to be added to the delete queue. | |
348 // | |
349 // Note: the parent could be synced if there was an update deleting a | |
350 // folder when we had a deleted all items in it. | |
351 // We may get more updates, or we may want to delete the entry. | |
352 if (parent.good() && | |
353 parent.Get(syncable::IS_DEL) && | |
354 parent.Get(syncable::IS_UNSYNCED)) { | |
355 // However, if an entry is moved, these rules can apply differently. | |
356 // | |
357 // If the entry was moved, then the destination parent was deleted, | |
358 // then we'll miss it in the roll up. We have to add it in manually. | |
359 // TODO(chron): Unit test for move / delete cases: | |
360 // Case 1: Locally moved, then parent deleted | |
361 // Case 2: Server moved, then locally issue recursive delete. | |
362 if (entry.Get(syncable::ID).ServerKnows() && | |
363 entry.Get(syncable::PARENT_ID) != | |
364 entry.Get(syncable::SERVER_PARENT_ID)) { | |
365 DVLOG(1) << "Inserting moved and deleted entry, will be missed by " | |
366 << "delete roll." << entry.Get(syncable::ID); | |
367 | |
368 ordered_commit_set_->AddCommitItem(metahandle, | |
369 entry.Get(syncable::ID), | |
370 entry.GetModelType()); | |
371 } | |
372 | |
373 // Skip this entry since it's a child of a parent that will be | |
374 // deleted. The server will unroll the delete and delete the | |
375 // child as well. | |
376 continue; | |
377 } | |
378 | |
379 legal_delete_parents.insert(entry.Get(syncable::PARENT_ID)); | |
380 } | |
381 } | |
382 | |
383 // We could store all the potential entries with a particular parent during | |
384 // the above scan, but instead we rescan here. This is less efficient, but | |
385 // we're dropping memory alloc/dealloc in favor of linear scans of recently | |
386 // examined entries. | |
387 // | |
388 // Scan through the UnsyncedMetaHandles again. If we have a deleted | |
389 // entry, then check if the parent is in legal_delete_parents. | |
390 // | |
391 // Parent being in legal_delete_parents means for the child: | |
392 // a recursive delete is not currently happening (no recent deletes in same | |
393 // folder) | |
394 // parent did expect at least one old deleted child | |
395 // parent was not deleted | |
396 for (std::set<int64>::const_iterator iter = ready_unsynced_set.begin(); | |
397 !IsCommitBatchFull() && iter != ready_unsynced_set.end(); ++iter) { | |
398 int64 metahandle = *iter; | |
399 if (ordered_commit_set_->HaveCommitItem(metahandle)) | |
400 continue; | |
401 syncable::MutableEntry entry(write_transaction, syncable::GET_BY_HANDLE, | |
402 metahandle); | |
403 if (entry.Get(syncable::IS_DEL)) { | |
404 syncable::Id parent_id = entry.Get(syncable::PARENT_ID); | |
405 if (legal_delete_parents.count(parent_id)) { | |
406 ordered_commit_set_->AddCommitItem(metahandle, entry.Get(syncable::ID), | |
407 entry.GetModelType()); | |
408 } | |
409 } | |
410 } | |
411 } | |
412 | |
413 void GetCommitIdsCommand::BuildCommitIds( | |
414 syncable::WriteTransaction* write_transaction, | |
415 const ModelSafeRoutingInfo& routes, | |
416 const std::set<int64>& ready_unsynced_set) { | |
417 ordered_commit_set_.reset(new OrderedCommitSet(routes)); | |
418 // Commits follow these rules: | |
419 // 1. Moves or creates are preceded by needed folder creates, from | |
420 // root to leaf. For folders whose contents are ordered, moves | |
421 // and creates appear in order. | |
422 // 2. Moves/Creates before deletes. | |
423 // 3. Deletes, collapsed. | |
424 // We commit deleted moves under deleted items as moves when collapsing | |
425 // delete trees. | |
426 | |
427 // Add moves and creates, and prepend their uncommitted parents. | |
428 AddCreatesAndMoves(write_transaction, routes, ready_unsynced_set); | |
429 | |
430 // Add all deletes. | |
431 AddDeletes(write_transaction, ready_unsynced_set); | |
432 } | |
433 | |
434 } // namespace browser_sync | |
OLD | NEW |