| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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/directory_commit_contribution.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <algorithm> | |
| 11 #include <set> | |
| 12 | |
| 13 #include "sync/engine/commit_util.h" | |
| 14 #include "sync/engine/get_commit_ids.h" | |
| 15 #include "sync/engine/syncer_util.h" | |
| 16 #include "sync/internal_api/public/sessions/commit_counters.h" | |
| 17 #include "sync/syncable/model_neutral_mutable_entry.h" | |
| 18 #include "sync/syncable/syncable_model_neutral_write_transaction.h" | |
| 19 | |
| 20 namespace syncer { | |
| 21 | |
| 22 using syncable::GET_BY_HANDLE; | |
| 23 using syncable::SYNCER; | |
| 24 | |
| 25 DirectoryCommitContribution::~DirectoryCommitContribution() { | |
| 26 DCHECK(!syncing_bits_set_); | |
| 27 } | |
| 28 | |
| 29 // static. | |
| 30 std::unique_ptr<DirectoryCommitContribution> DirectoryCommitContribution::Build( | |
| 31 syncable::Directory* dir, | |
| 32 ModelType type, | |
| 33 size_t max_entries, | |
| 34 DirectoryTypeDebugInfoEmitter* debug_info_emitter) { | |
| 35 DCHECK(debug_info_emitter); | |
| 36 | |
| 37 std::vector<int64_t> metahandles; | |
| 38 | |
| 39 syncable::ModelNeutralWriteTransaction trans(FROM_HERE, SYNCER, dir); | |
| 40 GetCommitIdsForType(&trans, type, max_entries, &metahandles); | |
| 41 | |
| 42 if (metahandles.empty()) | |
| 43 return std::unique_ptr<DirectoryCommitContribution>(); | |
| 44 | |
| 45 google::protobuf::RepeatedPtrField<sync_pb::SyncEntity> entities; | |
| 46 for (std::vector<int64_t>::iterator it = metahandles.begin(); | |
| 47 it != metahandles.end(); ++it) { | |
| 48 sync_pb::SyncEntity* entity = entities.Add(); | |
| 49 syncable::ModelNeutralMutableEntry entry(&trans, GET_BY_HANDLE, *it); | |
| 50 commit_util::BuildCommitItem(entry, entity); | |
| 51 entry.PutSyncing(true); | |
| 52 } | |
| 53 | |
| 54 sync_pb::DataTypeContext context; | |
| 55 dir->GetDataTypeContext(&trans, type, &context); | |
| 56 | |
| 57 return std::unique_ptr<DirectoryCommitContribution>( | |
| 58 new DirectoryCommitContribution(metahandles, entities, context, dir, | |
| 59 debug_info_emitter)); | |
| 60 } | |
| 61 | |
| 62 void DirectoryCommitContribution::AddToCommitMessage( | |
| 63 sync_pb::ClientToServerMessage* msg) { | |
| 64 DCHECK(syncing_bits_set_); | |
| 65 sync_pb::CommitMessage* commit_message = msg->mutable_commit(); | |
| 66 entries_start_index_ = commit_message->entries_size(); | |
| 67 std::copy(entities_.begin(), | |
| 68 entities_.end(), | |
| 69 RepeatedPtrFieldBackInserter(commit_message->mutable_entries())); | |
| 70 if (!context_.context().empty()) | |
| 71 commit_message->add_client_contexts()->Swap(&context_); | |
| 72 | |
| 73 CommitCounters* counters = debug_info_emitter_->GetMutableCommitCounters(); | |
| 74 counters->num_commits_attempted += entities_.size(); | |
| 75 } | |
| 76 | |
| 77 SyncerError DirectoryCommitContribution::ProcessCommitResponse( | |
| 78 const sync_pb::ClientToServerResponse& response, | |
| 79 sessions::StatusController* status) { | |
| 80 DCHECK(syncing_bits_set_); | |
| 81 const sync_pb::CommitResponse& commit_response = response.commit(); | |
| 82 | |
| 83 int transient_error_commits = 0; | |
| 84 int conflicting_commits = 0; | |
| 85 int error_commits = 0; | |
| 86 int successes = 0; | |
| 87 | |
| 88 std::set<syncable::Id> deleted_folders; | |
| 89 { | |
| 90 syncable::ModelNeutralWriteTransaction trans(FROM_HERE, SYNCER, dir_); | |
| 91 for (size_t i = 0; i < metahandles_.size(); ++i) { | |
| 92 sync_pb::CommitResponse::ResponseType response_type = | |
| 93 commit_util::ProcessSingleCommitResponse( | |
| 94 &trans, | |
| 95 commit_response.entryresponse(entries_start_index_ + i), | |
| 96 entities_.Get(i), | |
| 97 metahandles_[i], | |
| 98 &deleted_folders); | |
| 99 switch (response_type) { | |
| 100 case sync_pb::CommitResponse::INVALID_MESSAGE: | |
| 101 ++error_commits; | |
| 102 break; | |
| 103 case sync_pb::CommitResponse::CONFLICT: | |
| 104 ++conflicting_commits; | |
| 105 status->increment_num_server_conflicts(); | |
| 106 break; | |
| 107 case sync_pb::CommitResponse::SUCCESS: | |
| 108 ++successes; | |
| 109 { | |
| 110 syncable::Entry e(&trans, GET_BY_HANDLE, metahandles_[i]); | |
| 111 if (e.GetModelType() == BOOKMARKS) | |
| 112 status->increment_num_successful_bookmark_commits(); | |
| 113 } | |
| 114 status->increment_num_successful_commits(); | |
| 115 break; | |
| 116 case sync_pb::CommitResponse::OVER_QUOTA: | |
| 117 // We handle over quota like a retry, which is same as transient. | |
| 118 case sync_pb::CommitResponse::RETRY: | |
| 119 case sync_pb::CommitResponse::TRANSIENT_ERROR: | |
| 120 ++transient_error_commits; | |
| 121 break; | |
| 122 default: | |
| 123 LOG(FATAL) << "Bad return from ProcessSingleCommitResponse"; | |
| 124 } | |
| 125 } | |
| 126 MarkDeletedChildrenSynced(dir_, &trans, &deleted_folders); | |
| 127 } | |
| 128 | |
| 129 CommitCounters* counters = debug_info_emitter_->GetMutableCommitCounters(); | |
| 130 counters->num_commits_success += successes; | |
| 131 counters->num_commits_conflict += transient_error_commits; | |
| 132 counters->num_commits_error += transient_error_commits; | |
| 133 | |
| 134 int commit_count = static_cast<int>(metahandles_.size()); | |
| 135 if (commit_count == successes) { | |
| 136 return SYNCER_OK; | |
| 137 } else if (error_commits > 0) { | |
| 138 return SERVER_RETURN_UNKNOWN_ERROR; | |
| 139 } else if (transient_error_commits > 0) { | |
| 140 return SERVER_RETURN_TRANSIENT_ERROR; | |
| 141 } else if (conflicting_commits > 0) { | |
| 142 // This means that the server already has an item with this version, but | |
| 143 // we haven't seen that update yet. | |
| 144 // | |
| 145 // A well-behaved client should respond to this by proceeding to the | |
| 146 // download updates phase, fetching the conflicting items, then attempting | |
| 147 // to resolve the conflict. That's not what this client does. | |
| 148 // | |
| 149 // We don't currently have any code to support that exceptional control | |
| 150 // flow. Instead, we abort the current sync cycle and start a new one. The | |
| 151 // end result is the same. | |
| 152 return SERVER_RETURN_CONFLICT; | |
| 153 } else { | |
| 154 LOG(FATAL) << "Inconsistent counts when processing commit response"; | |
| 155 return SYNCER_OK; | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 void DirectoryCommitContribution::CleanUp() { | |
| 160 DCHECK(syncing_bits_set_); | |
| 161 UnsetSyncingBits(); | |
| 162 debug_info_emitter_->EmitCommitCountersUpdate(); | |
| 163 debug_info_emitter_->EmitStatusCountersUpdate(); | |
| 164 } | |
| 165 | |
| 166 size_t DirectoryCommitContribution::GetNumEntries() const { | |
| 167 return metahandles_.size(); | |
| 168 } | |
| 169 | |
| 170 DirectoryCommitContribution::DirectoryCommitContribution( | |
| 171 const std::vector<int64_t>& metahandles, | |
| 172 const google::protobuf::RepeatedPtrField<sync_pb::SyncEntity>& entities, | |
| 173 const sync_pb::DataTypeContext& context, | |
| 174 syncable::Directory* dir, | |
| 175 DirectoryTypeDebugInfoEmitter* debug_info_emitter) | |
| 176 : dir_(dir), | |
| 177 metahandles_(metahandles), | |
| 178 entities_(entities), | |
| 179 context_(context), | |
| 180 entries_start_index_(0xDEADBEEF), | |
| 181 syncing_bits_set_(true), | |
| 182 debug_info_emitter_(debug_info_emitter) {} | |
| 183 | |
| 184 void DirectoryCommitContribution::UnsetSyncingBits() { | |
| 185 syncable::ModelNeutralWriteTransaction trans(FROM_HERE, SYNCER, dir_); | |
| 186 for (std::vector<int64_t>::const_iterator it = metahandles_.begin(); | |
| 187 it != metahandles_.end(); ++it) { | |
| 188 syncable::ModelNeutralMutableEntry entry(&trans, GET_BY_HANDLE, *it); | |
| 189 // TODO(sync): this seems like it could be harmful if a sync cycle doesn't | |
| 190 // complete but the Cleanup method is called anyways. It appears these are | |
| 191 // unset on the assumption that the sync cycle must have finished properly, | |
| 192 // although that's actually up to the commit response handling logic. | |
| 193 entry.PutDirtySync(false); | |
| 194 entry.PutSyncing(false); | |
| 195 } | |
| 196 syncing_bits_set_ = false; | |
| 197 } | |
| 198 | |
| 199 } // namespace syncer | |
| OLD | NEW |