| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 "sync/engine/process_updates_util.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <string> | |
| 11 | |
| 12 #include "base/location.h" | |
| 13 #include "base/metrics/sparse_histogram.h" | |
| 14 #include "sync/engine/syncer_proto_util.h" | |
| 15 #include "sync/engine/syncer_types.h" | |
| 16 #include "sync/engine/syncer_util.h" | |
| 17 #include "sync/internal_api/public/sessions/update_counters.h" | |
| 18 #include "sync/syncable/directory.h" | |
| 19 #include "sync/syncable/model_neutral_mutable_entry.h" | |
| 20 #include "sync/syncable/syncable_model_neutral_write_transaction.h" | |
| 21 #include "sync/syncable/syncable_proto_util.h" | |
| 22 #include "sync/syncable/syncable_util.h" | |
| 23 #include "sync/util/cryptographer.h" | |
| 24 #include "sync/util/data_type_histogram.h" | |
| 25 | |
| 26 namespace syncer { | |
| 27 | |
| 28 using sessions::StatusController; | |
| 29 | |
| 30 using syncable::GET_BY_ID; | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 // This function attempts to determine whether or not this update is genuinely | |
| 35 // new, or if it is a reflection of one of our own commits. | |
| 36 // | |
| 37 // There is a known inaccuracy in its implementation. If this update ends up | |
| 38 // being applied to a local item with a different ID, we will count the change | |
| 39 // as being a non-reflection update. Fortunately, the server usually updates | |
| 40 // our IDs correctly in its commit response, so a new ID during GetUpdate should | |
| 41 // be rare. | |
| 42 // | |
| 43 // The only scenarios I can think of where this might happen are: | |
| 44 // - We commit a new item to the server, but we don't persist the | |
| 45 // server-returned new ID to the database before we shut down. On the GetUpdate | |
| 46 // following the next restart, we will receive an update from the server that | |
| 47 // updates its local ID. | |
| 48 // - When two attempts to create an item with identical UNIQUE_CLIENT_TAG values | |
| 49 // collide at the server. I have seen this in testing. When it happens, the | |
| 50 // test server will send one of the clients a response to upate its local ID so | |
| 51 // that both clients will refer to the item using the same ID going forward. In | |
| 52 // this case, we're right to assume that the update is not a reflection. | |
| 53 // | |
| 54 // For more information, see FindLocalIdToUpdate(). | |
| 55 bool UpdateContainsNewVersion(syncable::BaseTransaction *trans, | |
| 56 const sync_pb::SyncEntity &update) { | |
| 57 int64_t existing_version = -1; // The server always sends positive versions. | |
| 58 syncable::Entry existing_entry(trans, GET_BY_ID, | |
| 59 SyncableIdFromProto(update.id_string())); | |
| 60 if (existing_entry.good()) | |
| 61 existing_version = existing_entry.GetBaseVersion(); | |
| 62 | |
| 63 if (!existing_entry.good() && update.deleted()) { | |
| 64 // There are several possible explanations for this. The most common cases | |
| 65 // will be first time sync and the redelivery of deletions we've already | |
| 66 // synced, accepted, and purged from our database. In either case, the | |
| 67 // update is useless to us. Let's count them all as "not new", even though | |
| 68 // that may not always be entirely accurate. | |
| 69 return false; | |
| 70 } | |
| 71 | |
| 72 if (existing_entry.good() && | |
| 73 !existing_entry.GetUniqueClientTag().empty() && | |
| 74 existing_entry.GetIsDel() && | |
| 75 update.deleted()) { | |
| 76 // Unique client tags will have their version set to zero when they're | |
| 77 // deleted. The usual version comparison logic won't be able to detect | |
| 78 // reflections of these items. Instead, we assume any received tombstones | |
| 79 // are reflections. That should be correct most of the time. | |
| 80 return false; | |
| 81 } | |
| 82 | |
| 83 return existing_version < update.version(); | |
| 84 } | |
| 85 | |
| 86 // In the event that IDs match, but tags differ AttemptReuniteClient tag | |
| 87 // will have refused to unify the update. | |
| 88 // We should not attempt to apply it at all since it violates consistency | |
| 89 // rules. | |
| 90 VerifyResult VerifyTagConsistency( | |
| 91 const sync_pb::SyncEntity& entry, | |
| 92 const syncable::ModelNeutralMutableEntry& same_id) { | |
| 93 if (entry.has_client_defined_unique_tag() && | |
| 94 entry.client_defined_unique_tag() != | |
| 95 same_id.GetUniqueClientTag()) { | |
| 96 return VERIFY_FAIL; | |
| 97 } | |
| 98 return VERIFY_UNDECIDED; | |
| 99 } | |
| 100 | |
| 101 // Checks whether or not an update is fit for processing. | |
| 102 // | |
| 103 // The answer may be "no" if the update appears invalid, or it's not releveant | |
| 104 // (ie. a delete for an item we've never heard of), or other reasons. | |
| 105 VerifyResult VerifyUpdate( | |
| 106 syncable::ModelNeutralWriteTransaction* trans, | |
| 107 const sync_pb::SyncEntity& entry, | |
| 108 ModelType requested_type) { | |
| 109 syncable::Id id = SyncableIdFromProto(entry.id_string()); | |
| 110 VerifyResult result = VERIFY_FAIL; | |
| 111 | |
| 112 const bool deleted = entry.has_deleted() && entry.deleted(); | |
| 113 const bool is_directory = IsFolder(entry); | |
| 114 const ModelType model_type = GetModelType(entry); | |
| 115 | |
| 116 if (!id.ServerKnows()) { | |
| 117 LOG(ERROR) << "Illegal negative id in received updates"; | |
| 118 return result; | |
| 119 } | |
| 120 { | |
| 121 const std::string name = SyncerProtoUtil::NameFromSyncEntity(entry); | |
| 122 if (name.empty() && !deleted) { | |
| 123 LOG(ERROR) << "Zero length name in non-deleted update"; | |
| 124 return result; | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 syncable::ModelNeutralMutableEntry same_id(trans, GET_BY_ID, id); | |
| 129 result = VerifyNewEntry(entry, &same_id, deleted); | |
| 130 | |
| 131 ModelType placement_type = !deleted ? GetModelType(entry) | |
| 132 : same_id.good() ? same_id.GetModelType() : UNSPECIFIED; | |
| 133 | |
| 134 if (VERIFY_UNDECIDED == result) { | |
| 135 result = VerifyTagConsistency(entry, same_id); | |
| 136 } | |
| 137 | |
| 138 if (VERIFY_UNDECIDED == result) { | |
| 139 if (deleted) { | |
| 140 // For deletes the server could send tombostones for items that | |
| 141 // the client did not request. If so ignore those items. | |
| 142 if (IsRealDataType(placement_type) && requested_type != placement_type) { | |
| 143 result = VERIFY_SKIP; | |
| 144 } else { | |
| 145 result = VERIFY_SUCCESS; | |
| 146 } | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 // If we have an existing entry, we check here for updates that break | |
| 151 // consistency rules. | |
| 152 if (VERIFY_UNDECIDED == result) { | |
| 153 result = VerifyUpdateConsistency(trans, entry, deleted, | |
| 154 is_directory, model_type, &same_id); | |
| 155 } | |
| 156 | |
| 157 if (VERIFY_UNDECIDED == result) | |
| 158 result = VERIFY_SUCCESS; // No news is good news. | |
| 159 | |
| 160 return result; // This might be VERIFY_SUCCESS as well | |
| 161 } | |
| 162 | |
| 163 // Returns true if the entry is still ok to process. | |
| 164 bool ReverifyEntry(syncable::ModelNeutralWriteTransaction* trans, | |
| 165 const sync_pb::SyncEntity& entry, | |
| 166 syncable::ModelNeutralMutableEntry* same_id) { | |
| 167 const bool deleted = entry.has_deleted() && entry.deleted(); | |
| 168 const bool is_directory = IsFolder(entry); | |
| 169 const ModelType model_type = GetModelType(entry); | |
| 170 | |
| 171 return VERIFY_SUCCESS == VerifyUpdateConsistency(trans, | |
| 172 entry, | |
| 173 deleted, | |
| 174 is_directory, | |
| 175 model_type, | |
| 176 same_id); | |
| 177 } | |
| 178 | |
| 179 // Process a single update. Will avoid touching global state. | |
| 180 // | |
| 181 // If the update passes a series of checks, this function will copy | |
| 182 // the SyncEntity's data into the SERVER side of the syncable::Directory. | |
| 183 void ProcessUpdate( | |
| 184 const sync_pb::SyncEntity& update, | |
| 185 const Cryptographer* cryptographer, | |
| 186 syncable::ModelNeutralWriteTransaction* const trans) { | |
| 187 const syncable::Id& server_id = SyncableIdFromProto(update.id_string()); | |
| 188 const std::string name = SyncerProtoUtil::NameFromSyncEntity(update); | |
| 189 | |
| 190 // Look to see if there's a local item that should recieve this update, | |
| 191 // maybe due to a duplicate client tag or a lost commit response. | |
| 192 syncable::Id local_id = FindLocalIdToUpdate(trans, update); | |
| 193 | |
| 194 // FindLocalEntryToUpdate has veto power. | |
| 195 if (local_id.IsNull()) { | |
| 196 return; // The entry has become irrelevant. | |
| 197 } | |
| 198 | |
| 199 CreateNewEntry(trans, local_id); | |
| 200 | |
| 201 // We take a two step approach. First we store the entries data in the | |
| 202 // server fields of a local entry and then move the data to the local fields | |
| 203 syncable::ModelNeutralMutableEntry target_entry(trans, GET_BY_ID, local_id); | |
| 204 | |
| 205 // We need to run the Verify checks again; the world could have changed | |
| 206 // since we last verified. | |
| 207 if (!ReverifyEntry(trans, update, &target_entry)) { | |
| 208 return; // The entry has become irrelevant. | |
| 209 } | |
| 210 | |
| 211 // If we're repurposing an existing local entry with a new server ID, | |
| 212 // change the ID now, after we're sure that the update can succeed. | |
| 213 if (local_id != server_id) { | |
| 214 DCHECK(!update.deleted()); | |
| 215 ChangeEntryIDAndUpdateChildren(trans, &target_entry, server_id); | |
| 216 // When IDs change, versions become irrelevant. Forcing BASE_VERSION | |
| 217 // to zero would ensure that this update gets applied, but would indicate | |
| 218 // creation or undeletion if it were committed that way. Instead, prefer | |
| 219 // forcing BASE_VERSION to entry.version() while also forcing | |
| 220 // IS_UNAPPLIED_UPDATE to true. If the item is UNSYNCED, it's committable | |
| 221 // from the new state; it may commit before the conflict resolver gets | |
| 222 // a crack at it. | |
| 223 if (target_entry.GetIsUnsynced() || target_entry.GetBaseVersion() > 0) { | |
| 224 // If either of these conditions are met, then we can expect valid client | |
| 225 // fields for this entry. When BASE_VERSION is positive, consistency is | |
| 226 // enforced on the client fields at update-application time. Otherwise, | |
| 227 // we leave the BASE_VERSION field alone; it'll get updated the first time | |
| 228 // we successfully apply this update. | |
| 229 target_entry.PutBaseVersion(update.version()); | |
| 230 } | |
| 231 // Force application of this update, no matter what. | |
| 232 target_entry.PutIsUnappliedUpdate(true); | |
| 233 } | |
| 234 | |
| 235 // If this is a newly received undecryptable update, and the only thing that | |
| 236 // has changed are the specifics, store the original decryptable specifics, | |
| 237 // (on which any current or future local changes are based) before we | |
| 238 // overwrite SERVER_SPECIFICS. | |
| 239 // MTIME, CTIME, and NON_UNIQUE_NAME are not enforced. | |
| 240 | |
| 241 bool position_matches = false; | |
| 242 if (target_entry.ShouldMaintainPosition() && !update.deleted()) { | |
| 243 std::string update_tag = GetUniqueBookmarkTagFromUpdate(update); | |
| 244 if (UniquePosition::IsValidSuffix(update_tag)) { | |
| 245 position_matches = GetUpdatePosition(update, update_tag).Equals( | |
| 246 target_entry.GetServerUniquePosition()); | |
| 247 } else { | |
| 248 NOTREACHED(); | |
| 249 } | |
| 250 } else { | |
| 251 // If this item doesn't care about positions, then set this flag to true. | |
| 252 position_matches = true; | |
| 253 } | |
| 254 | |
| 255 if (!update.deleted() && !target_entry.GetServerIsDel() && | |
| 256 (SyncableIdFromProto(update.parent_id_string()) == | |
| 257 target_entry.GetServerParentId()) && | |
| 258 position_matches && | |
| 259 update.has_specifics() && update.specifics().has_encrypted() && | |
| 260 !cryptographer->CanDecrypt(update.specifics().encrypted())) { | |
| 261 sync_pb::EntitySpecifics prev_specifics = | |
| 262 target_entry.GetServerSpecifics(); | |
| 263 // We only store the old specifics if they were decryptable and applied and | |
| 264 // there is no BASE_SERVER_SPECIFICS already. Else do nothing. | |
| 265 if (!target_entry.GetIsUnappliedUpdate() && | |
| 266 !IsRealDataType(GetModelTypeFromSpecifics( | |
| 267 target_entry.GetBaseServerSpecifics())) && | |
| 268 (!prev_specifics.has_encrypted() || | |
| 269 cryptographer->CanDecrypt(prev_specifics.encrypted()))) { | |
| 270 DVLOG(2) << "Storing previous server specifcs: " | |
| 271 << prev_specifics.SerializeAsString(); | |
| 272 target_entry.PutBaseServerSpecifics(prev_specifics); | |
| 273 } | |
| 274 } else if (IsRealDataType(GetModelTypeFromSpecifics( | |
| 275 target_entry.GetBaseServerSpecifics()))) { | |
| 276 // We have a BASE_SERVER_SPECIFICS, but a subsequent non-specifics-only | |
| 277 // change arrived. As a result, we can't use the specifics alone to detect | |
| 278 // changes, so we clear BASE_SERVER_SPECIFICS. | |
| 279 target_entry.PutBaseServerSpecifics( | |
| 280 sync_pb::EntitySpecifics()); | |
| 281 } | |
| 282 | |
| 283 UpdateServerFieldsFromUpdate(&target_entry, update, name); | |
| 284 | |
| 285 return; | |
| 286 } | |
| 287 | |
| 288 } // namespace | |
| 289 | |
| 290 void ProcessDownloadedUpdates( | |
| 291 syncable::Directory* dir, | |
| 292 syncable::ModelNeutralWriteTransaction* trans, | |
| 293 ModelType type, | |
| 294 const SyncEntityList& applicable_updates, | |
| 295 sessions::StatusController* status, | |
| 296 UpdateCounters* counters) { | |
| 297 for (SyncEntityList::const_iterator update_it = applicable_updates.begin(); | |
| 298 update_it != applicable_updates.end(); ++update_it) { | |
| 299 DCHECK_EQ(type, GetModelType(**update_it)); | |
| 300 if (!UpdateContainsNewVersion(trans, **update_it)) { | |
| 301 status->increment_num_reflected_updates_downloaded_by(1); | |
| 302 counters->num_reflected_updates_received++; | |
| 303 } | |
| 304 if ((*update_it)->deleted()) { | |
| 305 status->increment_num_tombstone_updates_downloaded_by(1); | |
| 306 counters->num_tombstone_updates_received++; | |
| 307 } | |
| 308 VerifyResult verify_result = VerifyUpdate(trans, **update_it, type); | |
| 309 if (verify_result != VERIFY_SUCCESS && verify_result != VERIFY_UNDELETE) | |
| 310 continue; | |
| 311 ProcessUpdate(**update_it, dir->GetCryptographer(trans), trans); | |
| 312 if ((*update_it)->ByteSize() > 0) { | |
| 313 SyncRecordDatatypeBin("DataUse.Sync.Download.Bytes", | |
| 314 ModelTypeToHistogramInt(type), | |
| 315 (*update_it)->ByteSize()); | |
| 316 } | |
| 317 UMA_HISTOGRAM_SPARSE_SLOWLY("DataUse.Sync.Download.Count", | |
| 318 ModelTypeToHistogramInt(type)); | |
| 319 } | |
| 320 } | |
| 321 | |
| 322 void ExpireEntriesByVersion(syncable::Directory* dir, | |
| 323 syncable::ModelNeutralWriteTransaction* trans, | |
| 324 ModelType type, | |
| 325 int64_t version_watermark) { | |
| 326 syncable::Directory::Metahandles handles; | |
| 327 dir->GetMetaHandlesOfType(trans, type, &handles); | |
| 328 for (size_t i = 0; i < handles.size(); ++i) { | |
| 329 syncable::ModelNeutralMutableEntry entry(trans, syncable::GET_BY_HANDLE, | |
| 330 handles[i]); | |
| 331 if (!entry.good() || !entry.GetId().ServerKnows() || | |
| 332 entry.GetUniqueServerTag() == ModelTypeToRootTag(type) || | |
| 333 entry.GetIsUnappliedUpdate() || entry.GetIsUnsynced() || | |
| 334 entry.GetIsDel() || entry.GetServerIsDel() || | |
| 335 entry.GetBaseVersion() >= version_watermark) { | |
| 336 continue; | |
| 337 } | |
| 338 | |
| 339 // Mark entry as unapplied update first to ensure journaling the deletion. | |
| 340 entry.PutIsUnappliedUpdate(true); | |
| 341 // Mark entry as deleted by server. | |
| 342 entry.PutServerIsDel(true); | |
| 343 entry.PutServerVersion(version_watermark); | |
| 344 } | |
| 345 } | |
| 346 | |
| 347 } // namespace syncer | |
| OLD | NEW |