Chromium Code Reviews| 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" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 #include "sync/internal_api/public/base/node_ordinal.h" | 25 #include "sync/internal_api/public/base/node_ordinal.h" |
| 26 | 26 |
| 27 using std::vector; | 27 using std::vector; |
| 28 | 28 |
| 29 namespace syncer { | 29 namespace syncer { |
| 30 | 30 |
| 31 using sessions::SyncSession; | 31 using sessions::SyncSession; |
| 32 using sessions::StatusController; | 32 using sessions::StatusController; |
| 33 using sessions::UpdateProgress; | 33 using sessions::UpdateProgress; |
| 34 | 34 |
| 35 using syncable::GET_BY_ID; | |
| 36 | |
| 35 ProcessUpdatesCommand::ProcessUpdatesCommand() {} | 37 ProcessUpdatesCommand::ProcessUpdatesCommand() {} |
| 36 ProcessUpdatesCommand::~ProcessUpdatesCommand() {} | 38 ProcessUpdatesCommand::~ProcessUpdatesCommand() {} |
| 37 | 39 |
| 38 std::set<ModelSafeGroup> ProcessUpdatesCommand::GetGroupsToChange( | 40 std::set<ModelSafeGroup> ProcessUpdatesCommand::GetGroupsToChange( |
| 39 const sessions::SyncSession& session) const { | 41 const sessions::SyncSession& session) const { |
| 40 return session.GetEnabledGroupsWithVerifiedUpdates(); | 42 std::set<ModelSafeGroup> groups_with_updates; |
| 41 } | 43 |
| 44 const sync_pb::GetUpdatesResponse& updates = | |
| 45 session.status_controller().updates_response().get_updates(); | |
| 46 for (int i = 0; i < updates.entries().size(); i++) { | |
| 47 groups_with_updates.insert( | |
| 48 GetGroupForModelType(GetModelType(updates.entries(i)), | |
| 49 session.routing_info())); | |
| 50 } | |
| 51 | |
| 52 return groups_with_updates; | |
| 53 } | |
| 54 | |
| 55 namespace { | |
| 56 | |
| 57 // This function attempts to determine whether or not this update is genuinely | |
| 58 // new, or if it is a reflection of one of our own commits. | |
| 59 // | |
| 60 // There is a known inaccuracy in its implementation. If this update ends up | |
| 61 // being applied to a local item with a different ID, we will count the change | |
| 62 // as being a non-reflection update. Fortunately, the server usually updates | |
| 63 // our IDs correctly in its commit response, so a new ID during GetUpdate should | |
| 64 // be rare. | |
| 65 // | |
| 66 // The only secnarios I can think of where this might happen are: | |
| 67 // - We commit a new item to the server, but we don't persist the | |
| 68 // server-returned new ID to the database before we shut down. On the GetUpdate | |
| 69 // following the next restart, we will receive an update from the server that | |
| 70 // updates its local ID. | |
| 71 // - When two attempts to create an item with identical UNIQUE_CLIENT_TAG values | |
| 72 // collide at the server. I have seen this in testing. When it happens, the | |
| 73 // test server will send one of the clients a response to upate its local ID so | |
| 74 // that both clients will refer to the item using the same ID going forward. In | |
| 75 // this case, we're right to assume that the update is not a reflection. | |
| 76 // | |
| 77 // For more information, see FindLocalIdToUpdate(). | |
| 78 bool UpdateContainsNewVersion(syncable::BaseTransaction *trans, | |
| 79 const sync_pb::SyncEntity &update) { | |
| 80 int64 existing_version = -1; // The server always sends positive versions. | |
| 81 syncable::Entry existing_entry(trans, GET_BY_ID, | |
| 82 SyncableIdFromProto(update.id_string())); | |
| 83 if (existing_entry.good()) | |
| 84 existing_version = existing_entry.Get(syncable::BASE_VERSION); | |
| 85 | |
| 86 if (!existing_entry.good() && update.deleted()) { | |
| 87 // There are several possible explanations for this. The most common cases | |
| 88 // will be first time sync and the redelivery of deletions we've already | |
| 89 // synced, accepted, and purged from our database. In either case, the | |
| 90 // update is useless to us. Let's count them all as "not new", even though | |
| 91 // that may not always be entirely accurate. | |
| 92 return false; | |
| 93 } | |
| 94 | |
| 95 if (existing_entry.good() && | |
| 96 !existing_entry.Get(syncable::UNIQUE_CLIENT_TAG).empty() && | |
| 97 existing_entry.Get(syncable::IS_DEL) && | |
| 98 update.deleted()) { | |
| 99 // Unique client tags will have their version set to zero when they're | |
| 100 // deleted. The usual version comparison logic won't be able to detect | |
| 101 // reflections of these items. Instead, we assume any received tombstones | |
| 102 // are reflections. That should be correct most of the time. | |
| 103 return false; | |
| 104 } | |
| 105 | |
| 106 return existing_version < update.version(); | |
| 107 } | |
| 108 | |
| 109 } // namespace | |
| 42 | 110 |
| 43 SyncerError ProcessUpdatesCommand::ModelChangingExecuteImpl( | 111 SyncerError ProcessUpdatesCommand::ModelChangingExecuteImpl( |
| 44 SyncSession* session) { | 112 SyncSession* session) { |
| 45 syncable::Directory* dir = session->context()->directory(); | 113 syncable::Directory* dir = session->context()->directory(); |
| 46 | 114 |
| 47 const sessions::UpdateProgress* progress = | |
| 48 session->status_controller().update_progress(); | |
| 49 if (!progress) | |
| 50 return SYNCER_OK; // Nothing to do. | |
| 51 | |
| 52 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir); | 115 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir); |
| 53 vector<sessions::VerifiedUpdate>::const_iterator it; | 116 |
| 54 for (it = progress->VerifiedUpdatesBegin(); | 117 sessions::StatusController* status = session->mutable_status_controller(); |
| 55 it != progress->VerifiedUpdatesEnd(); | 118 const sync_pb::GetUpdatesResponse& updates = |
| 56 ++it) { | 119 status->updates_response().get_updates(); |
| 57 const sync_pb::SyncEntity& update = it->second; | 120 int update_count = updates.entries().size(); |
| 58 | 121 |
| 59 if (it->first != VERIFY_SUCCESS && it->first != VERIFY_UNDELETE) | 122 ModelTypeSet requested_types = GetRoutingInfoTypes( |
| 123 session->routing_info()); | |
| 124 | |
| 125 DVLOG(1) << update_count << " entries to verify"; | |
| 126 for (int i = 0; i < update_count; i++) { | |
| 127 const sync_pb::SyncEntity& update = updates.entries(i); | |
| 128 | |
| 129 // The current function gets executed on several different threads, but | |
| 130 // every call iterates over the same list of items that the server returned | |
| 131 // to us. We're not allowed to process items unless we're on the right | |
| 132 // thread for that type. This check will ensure we only touch the items | |
| 133 // that live on our current thread. | |
| 134 // TODO(tim): Hide these details. See crbug.com/121521 . | |
|
tim (not reviewing)
2012/10/15 23:22:32
"Don't allow access to objects in other ModelSafeG
| |
| 135 ModelSafeGroup g = GetGroupForModelType(GetModelType(update), | |
| 136 session->routing_info()); | |
| 137 if (g != status->group_restriction()) | |
| 60 continue; | 138 continue; |
| 61 switch (ProcessUpdate(update, | 139 |
| 62 dir->GetCryptographer(&trans), | 140 VerifyResult verify_result = VerifyUpdate(&trans, update, |
| 63 &trans)) { | 141 requested_types, |
| 64 case SUCCESS_PROCESSED: | 142 session->routing_info()); |
| 65 case SUCCESS_STORED: | 143 status->mutable_update_progress()->AddVerifyResult(verify_result, update); |
| 66 break; | 144 status->increment_num_updates_downloaded_by(1); |
| 67 default: | 145 if (!UpdateContainsNewVersion(&trans, update)) |
| 68 NOTREACHED(); | 146 status->increment_num_reflected_updates_downloaded_by(1); |
| 69 break; | 147 if (update.deleted()) |
| 70 } | 148 status->increment_num_tombstone_updates_downloaded_by(1); |
| 71 } | 149 |
| 72 | 150 if (verify_result != VERIFY_SUCCESS && verify_result != VERIFY_UNDELETE) |
| 73 StatusController* status = session->mutable_status_controller(); | 151 continue; |
| 152 | |
| 153 ServerUpdateProcessingResult process_result = | |
| 154 ProcessUpdate(update, dir->GetCryptographer(&trans), &trans); | |
| 155 | |
| 156 DCHECK(process_result == SUCCESS_PROCESSED || | |
| 157 process_result == SUCCESS_STORED); | |
| 158 } | |
| 159 | |
| 74 status->mutable_update_progress()->ClearVerifiedUpdates(); | 160 status->mutable_update_progress()->ClearVerifiedUpdates(); |
| 75 return SYNCER_OK; | 161 return SYNCER_OK; |
| 76 } | 162 } |
| 77 | 163 |
| 78 namespace { | 164 namespace { |
| 165 | |
| 166 // In the event that IDs match, but tags differ AttemptReuniteClient tag | |
| 167 // will have refused to unify the update. | |
| 168 // We should not attempt to apply it at all since it violates consistency | |
| 169 // rules. | |
| 170 VerifyResult VerifyTagConsistency(const sync_pb::SyncEntity& entry, | |
| 171 const syncable::MutableEntry& same_id) { | |
| 172 if (entry.has_client_defined_unique_tag() && | |
| 173 entry.client_defined_unique_tag() != | |
| 174 same_id.Get(syncable::UNIQUE_CLIENT_TAG)) { | |
| 175 return VERIFY_FAIL; | |
| 176 } | |
| 177 return VERIFY_UNDECIDED; | |
| 178 } | |
| 179 | |
| 180 } // namespace | |
| 181 | |
| 182 VerifyResult ProcessUpdatesCommand::VerifyUpdate( | |
| 183 syncable::WriteTransaction* trans, const sync_pb::SyncEntity& entry, | |
| 184 ModelTypeSet requested_types, | |
| 185 const ModelSafeRoutingInfo& routes) { | |
| 186 syncable::Id id = SyncableIdFromProto(entry.id_string()); | |
| 187 VerifyResult result = VERIFY_FAIL; | |
| 188 | |
| 189 const bool deleted = entry.has_deleted() && entry.deleted(); | |
| 190 const bool is_directory = IsFolder(entry); | |
| 191 const ModelType model_type = GetModelType(entry); | |
| 192 | |
| 193 if (!id.ServerKnows()) { | |
| 194 LOG(ERROR) << "Illegal negative id in received updates"; | |
| 195 return result; | |
| 196 } | |
| 197 { | |
| 198 const std::string name = SyncerProtoUtil::NameFromSyncEntity(entry); | |
| 199 if (name.empty() && !deleted) { | |
| 200 LOG(ERROR) << "Zero length name in non-deleted update"; | |
| 201 return result; | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 syncable::MutableEntry same_id(trans, GET_BY_ID, id); | |
| 206 result = VerifyNewEntry(entry, &same_id, deleted); | |
| 207 | |
| 208 ModelType placement_type = !deleted ? GetModelType(entry) | |
| 209 : same_id.good() ? same_id.GetModelType() : UNSPECIFIED; | |
| 210 | |
| 211 if (VERIFY_UNDECIDED == result) { | |
| 212 result = VerifyTagConsistency(entry, same_id); | |
| 213 } | |
| 214 | |
| 215 if (VERIFY_UNDECIDED == result) { | |
| 216 if (deleted) { | |
| 217 // For deletes the server could send tombostones for items that | |
| 218 // the client did not request. If so ignore those items. | |
| 219 if (IsRealDataType(placement_type) && | |
| 220 !requested_types.Has(placement_type)) { | |
| 221 result = VERIFY_SKIP; | |
| 222 } else { | |
| 223 result = VERIFY_SUCCESS; | |
| 224 } | |
| 225 } | |
| 226 } | |
| 227 | |
| 228 // If we have an existing entry, we check here for updates that break | |
| 229 // consistency rules. | |
| 230 if (VERIFY_UNDECIDED == result) { | |
| 231 result = VerifyUpdateConsistency(trans, entry, &same_id, | |
| 232 deleted, is_directory, model_type); | |
| 233 } | |
| 234 | |
| 235 if (VERIFY_UNDECIDED == result) | |
| 236 result = VERIFY_SUCCESS; // No news is good news. | |
| 237 | |
| 238 return result; // This might be VERIFY_SUCCESS as well | |
| 239 } | |
| 240 | |
| 241 namespace { | |
| 79 // Returns true if the entry is still ok to process. | 242 // Returns true if the entry is still ok to process. |
| 80 bool ReverifyEntry(syncable::WriteTransaction* trans, | 243 bool ReverifyEntry(syncable::WriteTransaction* trans, |
| 81 const sync_pb::SyncEntity& entry, | 244 const sync_pb::SyncEntity& entry, |
| 82 syncable::MutableEntry* same_id) { | 245 syncable::MutableEntry* same_id) { |
| 83 | 246 |
| 84 const bool deleted = entry.has_deleted() && entry.deleted(); | 247 const bool deleted = entry.has_deleted() && entry.deleted(); |
| 85 const bool is_directory = IsFolder(entry); | 248 const bool is_directory = IsFolder(entry); |
| 86 const ModelType model_type = GetModelType(entry); | 249 const ModelType model_type = GetModelType(entry); |
| 87 | 250 |
| 88 return VERIFY_SUCCESS == VerifyUpdateConsistency(trans, | 251 return VERIFY_SUCCESS == VerifyUpdateConsistency(trans, |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 108 | 271 |
| 109 // FindLocalEntryToUpdate has veto power. | 272 // FindLocalEntryToUpdate has veto power. |
| 110 if (local_id.IsNull()) { | 273 if (local_id.IsNull()) { |
| 111 return SUCCESS_PROCESSED; // The entry has become irrelevant. | 274 return SUCCESS_PROCESSED; // The entry has become irrelevant. |
| 112 } | 275 } |
| 113 | 276 |
| 114 CreateNewEntry(trans, local_id); | 277 CreateNewEntry(trans, local_id); |
| 115 | 278 |
| 116 // We take a two step approach. First we store the entries data in the | 279 // We take a two step approach. First we store the entries data in the |
| 117 // server fields of a local entry and then move the data to the local fields | 280 // server fields of a local entry and then move the data to the local fields |
| 118 syncable::MutableEntry target_entry(trans, syncable::GET_BY_ID, local_id); | 281 syncable::MutableEntry target_entry(trans, GET_BY_ID, local_id); |
| 119 | 282 |
| 120 // We need to run the Verify checks again; the world could have changed | 283 // We need to run the Verify checks again; the world could have changed |
| 121 // since VerifyUpdatesCommand. | 284 // since we last verified. |
| 122 if (!ReverifyEntry(trans, update, &target_entry)) { | 285 if (!ReverifyEntry(trans, update, &target_entry)) { |
| 123 return SUCCESS_PROCESSED; // The entry has become irrelevant. | 286 return SUCCESS_PROCESSED; // The entry has become irrelevant. |
| 124 } | 287 } |
| 125 | 288 |
| 126 // If we're repurposing an existing local entry with a new server ID, | 289 // If we're repurposing an existing local entry with a new server ID, |
| 127 // change the ID now, after we're sure that the update can succeed. | 290 // change the ID now, after we're sure that the update can succeed. |
| 128 if (local_id != server_id) { | 291 if (local_id != server_id) { |
| 129 DCHECK(!update.deleted()); | 292 DCHECK(!update.deleted()); |
| 130 ChangeEntryIDAndUpdateChildren(trans, &target_entry, server_id); | 293 ChangeEntryIDAndUpdateChildren(trans, &target_entry, server_id); |
| 131 // When IDs change, versions become irrelevant. Forcing BASE_VERSION | 294 // When IDs change, versions become irrelevant. Forcing BASE_VERSION |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 target_entry.Put(syncable::BASE_SERVER_SPECIFICS, | 345 target_entry.Put(syncable::BASE_SERVER_SPECIFICS, |
| 183 sync_pb::EntitySpecifics()); | 346 sync_pb::EntitySpecifics()); |
| 184 } | 347 } |
| 185 | 348 |
| 186 UpdateServerFieldsFromUpdate(&target_entry, update, name); | 349 UpdateServerFieldsFromUpdate(&target_entry, update, name); |
| 187 | 350 |
| 188 return SUCCESS_PROCESSED; | 351 return SUCCESS_PROCESSED; |
| 189 } | 352 } |
| 190 | 353 |
| 191 } // namespace syncer | 354 } // namespace syncer |
| OLD | NEW |