| 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/conflict_resolver.h" | 5 #include "sync/engine/conflict_resolver.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "sync/engine/conflict_util.h" | 12 #include "sync/engine/conflict_util.h" |
| 13 #include "sync/engine/syncer_util.h" | 13 #include "sync/engine/syncer_util.h" |
| 14 #include "sync/internal_api/public/sessions/update_counters.h" |
| 14 #include "sync/sessions/status_controller.h" | 15 #include "sync/sessions/status_controller.h" |
| 15 #include "sync/syncable/directory.h" | 16 #include "sync/syncable/directory.h" |
| 16 #include "sync/syncable/mutable_entry.h" | 17 #include "sync/syncable/mutable_entry.h" |
| 17 #include "sync/syncable/syncable_write_transaction.h" | 18 #include "sync/syncable/syncable_write_transaction.h" |
| 18 #include "sync/util/cryptographer.h" | 19 #include "sync/util/cryptographer.h" |
| 19 | 20 |
| 20 using std::list; | 21 using std::list; |
| 21 using std::set; | 22 using std::set; |
| 22 | 23 |
| 23 namespace syncer { | 24 namespace syncer { |
| 24 | 25 |
| 25 using sessions::StatusController; | 26 using sessions::StatusController; |
| 26 using syncable::Directory; | 27 using syncable::Directory; |
| 27 using syncable::Entry; | 28 using syncable::Entry; |
| 28 using syncable::Id; | 29 using syncable::Id; |
| 29 using syncable::MutableEntry; | 30 using syncable::MutableEntry; |
| 30 using syncable::WriteTransaction; | 31 using syncable::WriteTransaction; |
| 31 | 32 |
| 32 ConflictResolver::ConflictResolver() { | 33 ConflictResolver::ConflictResolver() { |
| 33 } | 34 } |
| 34 | 35 |
| 35 ConflictResolver::~ConflictResolver() { | 36 ConflictResolver::~ConflictResolver() { |
| 36 } | 37 } |
| 37 | 38 |
| 38 void ConflictResolver::ProcessSimpleConflict(WriteTransaction* trans, | 39 void ConflictResolver::ProcessSimpleConflict(WriteTransaction* trans, |
| 39 const Id& id, | 40 const Id& id, |
| 40 const Cryptographer* cryptographer, | 41 const Cryptographer* cryptographer, |
| 41 StatusController* status) { | 42 StatusController* status, |
| 43 UpdateCounters* counters) { |
| 42 MutableEntry entry(trans, syncable::GET_BY_ID, id); | 44 MutableEntry entry(trans, syncable::GET_BY_ID, id); |
| 43 // Must be good as the entry won't have been cleaned up. | 45 // Must be good as the entry won't have been cleaned up. |
| 44 CHECK(entry.good()); | 46 CHECK(entry.good()); |
| 45 | 47 |
| 46 // This function can only resolve simple conflicts. Simple conflicts have | 48 // This function can only resolve simple conflicts. Simple conflicts have |
| 47 // both IS_UNSYNCED and IS_UNAPPLIED_UDPATE set. | 49 // both IS_UNSYNCED and IS_UNAPPLIED_UDPATE set. |
| 48 if (!entry.GetIsUnappliedUpdate() || !entry.GetIsUnsynced()) { | 50 if (!entry.GetIsUnappliedUpdate() || !entry.GetIsUnsynced()) { |
| 49 // This is very unusual, but it can happen in tests. We may be able to | 51 // This is very unusual, but it can happen in tests. We may be able to |
| 50 // assert NOTREACHED() here when those tests are updated. | 52 // assert NOTREACHED() here when those tests are updated. |
| 51 return; | 53 return; |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 DVLOG(1) << "Resolving simple conflict, everything matches, ignoring " | 153 DVLOG(1) << "Resolving simple conflict, everything matches, ignoring " |
| 152 << "changes for: " << entry; | 154 << "changes for: " << entry; |
| 153 conflict_util::IgnoreConflict(&entry); | 155 conflict_util::IgnoreConflict(&entry); |
| 154 UMA_HISTOGRAM_ENUMERATION("Sync.ResolveSimpleConflict", | 156 UMA_HISTOGRAM_ENUMERATION("Sync.ResolveSimpleConflict", |
| 155 CHANGES_MATCH, | 157 CHANGES_MATCH, |
| 156 CONFLICT_RESOLUTION_SIZE); | 158 CONFLICT_RESOLUTION_SIZE); |
| 157 } else if (base_server_specifics_match) { | 159 } else if (base_server_specifics_match) { |
| 158 DVLOG(1) << "Resolving simple conflict, ignoring server encryption " | 160 DVLOG(1) << "Resolving simple conflict, ignoring server encryption " |
| 159 << " changes for: " << entry; | 161 << " changes for: " << entry; |
| 160 status->increment_num_server_overwrites(); | 162 status->increment_num_server_overwrites(); |
| 163 counters->num_server_overwrites++; |
| 161 conflict_util::OverwriteServerChanges(&entry); | 164 conflict_util::OverwriteServerChanges(&entry); |
| 162 UMA_HISTOGRAM_ENUMERATION("Sync.ResolveSimpleConflict", | 165 UMA_HISTOGRAM_ENUMERATION("Sync.ResolveSimpleConflict", |
| 163 IGNORE_ENCRYPTION, | 166 IGNORE_ENCRYPTION, |
| 164 CONFLICT_RESOLUTION_SIZE); | 167 CONFLICT_RESOLUTION_SIZE); |
| 165 } else if (entry_deleted || !name_matches || !parent_matches) { | 168 } else if (entry_deleted || !name_matches || !parent_matches) { |
| 166 // NOTE: The update application logic assumes that conflict resolution | 169 // NOTE: The update application logic assumes that conflict resolution |
| 167 // will never result in changes to the local hierarchy. The entry_deleted | 170 // will never result in changes to the local hierarchy. The entry_deleted |
| 168 // and !parent_matches cases here are critical to maintaining that | 171 // and !parent_matches cases here are critical to maintaining that |
| 169 // assumption. | 172 // assumption. |
| 170 conflict_util::OverwriteServerChanges(&entry); | 173 conflict_util::OverwriteServerChanges(&entry); |
| 171 status->increment_num_server_overwrites(); | 174 status->increment_num_server_overwrites(); |
| 175 counters->num_server_overwrites++; |
| 172 DVLOG(1) << "Resolving simple conflict, overwriting server changes " | 176 DVLOG(1) << "Resolving simple conflict, overwriting server changes " |
| 173 << "for: " << entry; | 177 << "for: " << entry; |
| 174 UMA_HISTOGRAM_ENUMERATION("Sync.ResolveSimpleConflict", | 178 UMA_HISTOGRAM_ENUMERATION("Sync.ResolveSimpleConflict", |
| 175 OVERWRITE_SERVER, | 179 OVERWRITE_SERVER, |
| 176 CONFLICT_RESOLUTION_SIZE); | 180 CONFLICT_RESOLUTION_SIZE); |
| 177 } else { | 181 } else { |
| 178 DVLOG(1) << "Resolving simple conflict, ignoring local changes for: " | 182 DVLOG(1) << "Resolving simple conflict, ignoring local changes for: " |
| 179 << entry; | 183 << entry; |
| 180 conflict_util::IgnoreLocalChanges(&entry); | 184 conflict_util::IgnoreLocalChanges(&entry); |
| 181 status->increment_num_local_overwrites(); | 185 status->increment_num_local_overwrites(); |
| 186 counters->num_local_overwrites++; |
| 182 UMA_HISTOGRAM_ENUMERATION("Sync.ResolveSimpleConflict", | 187 UMA_HISTOGRAM_ENUMERATION("Sync.ResolveSimpleConflict", |
| 183 OVERWRITE_LOCAL, | 188 OVERWRITE_LOCAL, |
| 184 CONFLICT_RESOLUTION_SIZE); | 189 CONFLICT_RESOLUTION_SIZE); |
| 185 } | 190 } |
| 186 // Now that we've resolved the conflict, clear the prev server | 191 // Now that we've resolved the conflict, clear the prev server |
| 187 // specifics. | 192 // specifics. |
| 188 entry.PutBaseServerSpecifics(sync_pb::EntitySpecifics()); | 193 entry.PutBaseServerSpecifics(sync_pb::EntitySpecifics()); |
| 189 } else { // SERVER_IS_DEL is true | 194 } else { // SERVER_IS_DEL is true |
| 190 if (entry.GetIsDir()) { | 195 if (entry.GetIsDir()) { |
| 191 Directory::Metahandles children; | 196 Directory::Metahandles children; |
| 192 trans->directory()->GetChildHandlesById(trans, | 197 trans->directory()->GetChildHandlesById(trans, |
| 193 entry.GetId(), | 198 entry.GetId(), |
| 194 &children); | 199 &children); |
| 195 // If a server deleted folder has local contents it should be a hierarchy | 200 // If a server deleted folder has local contents it should be a hierarchy |
| 196 // conflict. Hierarchy conflicts should not be processed by this | 201 // conflict. Hierarchy conflicts should not be processed by this |
| 197 // function. | 202 // function. |
| 198 DCHECK(children.empty()); | 203 DCHECK(children.empty()); |
| 199 } | 204 } |
| 200 | 205 |
| 201 // The entry is deleted on the server but still exists locally. | 206 // The entry is deleted on the server but still exists locally. |
| 202 // We undelete it by overwriting the server's tombstone with the local | 207 // We undelete it by overwriting the server's tombstone with the local |
| 203 // data. | 208 // data. |
| 204 conflict_util::OverwriteServerChanges(&entry); | 209 conflict_util::OverwriteServerChanges(&entry); |
| 205 status->increment_num_server_overwrites(); | 210 status->increment_num_server_overwrites(); |
| 211 counters->num_server_overwrites++; |
| 206 DVLOG(1) << "Resolving simple conflict, undeleting server entry: " | 212 DVLOG(1) << "Resolving simple conflict, undeleting server entry: " |
| 207 << entry; | 213 << entry; |
| 208 UMA_HISTOGRAM_ENUMERATION("Sync.ResolveSimpleConflict", | 214 UMA_HISTOGRAM_ENUMERATION("Sync.ResolveSimpleConflict", |
| 209 UNDELETE, | 215 UNDELETE, |
| 210 CONFLICT_RESOLUTION_SIZE); | 216 CONFLICT_RESOLUTION_SIZE); |
| 211 } | 217 } |
| 212 } | 218 } |
| 213 | 219 |
| 214 void ConflictResolver::ResolveConflicts( | 220 void ConflictResolver::ResolveConflicts( |
| 215 syncable::WriteTransaction* trans, | 221 syncable::WriteTransaction* trans, |
| 216 const Cryptographer* cryptographer, | 222 const Cryptographer* cryptographer, |
| 217 const std::set<syncable::Id>& simple_conflict_ids, | 223 const std::set<syncable::Id>& simple_conflict_ids, |
| 218 sessions::StatusController* status) { | 224 sessions::StatusController* status, |
| 225 UpdateCounters* counters) { |
| 219 // Iterate over simple conflict items. | 226 // Iterate over simple conflict items. |
| 220 set<Id>::const_iterator it; | 227 set<Id>::const_iterator it; |
| 221 for (it = simple_conflict_ids.begin(); | 228 for (it = simple_conflict_ids.begin(); |
| 222 it != simple_conflict_ids.end(); | 229 it != simple_conflict_ids.end(); |
| 223 ++it) { | 230 ++it) { |
| 224 // We don't resolve conflicts for control types here. | 231 // We don't resolve conflicts for control types here. |
| 225 Entry conflicting_node(trans, syncable::GET_BY_ID, *it); | 232 Entry conflicting_node(trans, syncable::GET_BY_ID, *it); |
| 226 CHECK(conflicting_node.good()); | 233 CHECK(conflicting_node.good()); |
| 227 if (IsControlType( | 234 if (IsControlType( |
| 228 GetModelTypeFromSpecifics(conflicting_node.GetSpecifics()))) { | 235 GetModelTypeFromSpecifics(conflicting_node.GetSpecifics()))) { |
| 229 continue; | 236 continue; |
| 230 } | 237 } |
| 231 | 238 |
| 232 ProcessSimpleConflict(trans, *it, cryptographer, status); | 239 ProcessSimpleConflict(trans, *it, cryptographer, status, counters); |
| 233 } | 240 } |
| 234 return; | 241 return; |
| 235 } | 242 } |
| 236 | 243 |
| 237 } // namespace syncer | 244 } // namespace syncer |
| OLD | NEW |