Chromium Code Reviews| 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/sync_directory_commit_contribution.h" | |
| 6 | |
| 7 #include "sync/engine/build_commit_command.h" | |
| 8 #include "sync/engine/get_commit_ids.h" | |
| 9 #include "sync/engine/process_commit_response_command.h" | |
| 10 #include "sync/engine/syncer_util.h" | |
| 11 #include "sync/syncable/model_neutral_mutable_entry.h" | |
| 12 #include "sync/syncable/syncable_model_neutral_write_transaction.h" | |
| 13 | |
| 14 namespace syncer { | |
| 15 | |
| 16 using syncable::SYNCER; | |
|
Nicolas Zea
2013/10/09 00:17:39
move below GET_BY_HANDLE
rlarocque
2013/10/09 20:00:19
Done.
| |
| 17 using syncable::GET_BY_HANDLE; | |
| 18 | |
| 19 SyncDirectoryCommitContribution::~SyncDirectoryCommitContribution() { | |
| 20 syncable::ModelNeutralWriteTransaction trans(FROM_HERE, SYNCER, dir_); | |
| 21 for (std::vector<int64>::const_iterator it = metahandles_.begin(); | |
| 22 it != metahandles_.end(); ++it) { | |
| 23 syncable::ModelNeutralMutableEntry entry(&trans, GET_BY_HANDLE, *it); | |
| 24 entry.PutSyncing(false); | |
| 25 } | |
| 26 } | |
| 27 | |
| 28 // static. | |
| 29 SyncDirectoryCommitContribution* SyncDirectoryCommitContribution::Build( | |
| 30 syncable::Directory* dir, | |
| 31 ModelType type, | |
| 32 size_t max_entries) { | |
| 33 std::vector<int64> metahandles; | |
| 34 | |
| 35 syncable::ModelNeutralWriteTransaction trans(FROM_HERE, SYNCER, dir); | |
| 36 GetCommitIdsForType(&trans, type, max_entries, &metahandles); | |
| 37 | |
| 38 if (metahandles.empty()) | |
| 39 return NULL; | |
| 40 | |
| 41 google::protobuf::RepeatedPtrField<sync_pb::SyncEntity> entities; | |
| 42 for (std::vector<int64>::iterator it = metahandles.begin(); | |
| 43 it != metahandles.end(); ++it) { | |
| 44 sync_pb::SyncEntity* entity = entities.Add(); | |
| 45 syncable::ModelNeutralMutableEntry entry(&trans, GET_BY_HANDLE, *it); | |
| 46 BuildCommitCommand::BuildCommitItem(entry, entity); | |
| 47 entry.PutSyncing(true); | |
| 48 } | |
| 49 | |
| 50 return new SyncDirectoryCommitContribution(metahandles, entities, dir); | |
| 51 } | |
| 52 | |
| 53 void SyncDirectoryCommitContribution::AddToCommitMessage( | |
| 54 sync_pb::ClientToServerMessage* msg) { | |
| 55 sync_pb::CommitMessage* commit_message = msg->mutable_commit(); | |
| 56 entries_start_index_ = commit_message->entries_size(); | |
| 57 std::copy(entities_.begin(), | |
| 58 entities_.end(), | |
| 59 RepeatedPtrFieldBackInserter(commit_message->mutable_entries())); | |
| 60 } | |
| 61 | |
| 62 SyncerError SyncDirectoryCommitContribution::ProcessCommitResponse( | |
| 63 const sync_pb::ClientToServerResponse& response, | |
| 64 sessions::StatusController* status) { | |
| 65 const sync_pb::CommitResponse& commit_response = response.commit(); | |
| 66 | |
| 67 int transient_error_commits = 0; | |
| 68 int conflicting_commits = 0; | |
| 69 int error_commits = 0; | |
| 70 int successes = 0; | |
| 71 | |
| 72 std::set<syncable::Id> deleted_folders; | |
| 73 { | |
| 74 syncable::ModelNeutralWriteTransaction trans(FROM_HERE, SYNCER, dir_); | |
| 75 for (size_t i = 0; i < metahandles_.size(); ++i) { | |
| 76 sync_pb::CommitResponse::ResponseType response_type = | |
| 77 ProcessCommitResponseCommand::ProcessSingleCommitResponse( | |
| 78 &trans, | |
| 79 commit_response.entryresponse(entries_start_index_ + i), | |
| 80 entities_.Get(i), | |
| 81 metahandles_[i], | |
| 82 &deleted_folders); | |
| 83 switch (response_type) { | |
| 84 case sync_pb::CommitResponse::INVALID_MESSAGE: | |
| 85 ++error_commits; | |
| 86 break; | |
| 87 case sync_pb::CommitResponse::CONFLICT: | |
| 88 ++conflicting_commits; | |
| 89 status->increment_num_server_conflicts(); | |
| 90 break; | |
| 91 case sync_pb::CommitResponse::SUCCESS: | |
| 92 ++successes; | |
| 93 { | |
| 94 syncable::Entry e(&trans, GET_BY_HANDLE, metahandles_[i]); | |
| 95 if (e.GetModelType() == BOOKMARKS) | |
| 96 status->increment_num_successful_bookmark_commits(); | |
| 97 } | |
| 98 status->increment_num_successful_commits(); | |
| 99 break; | |
| 100 case sync_pb::CommitResponse::OVER_QUOTA: | |
| 101 // We handle over quota like a retry, which is same as transient. | |
| 102 case sync_pb::CommitResponse::RETRY: | |
| 103 case sync_pb::CommitResponse::TRANSIENT_ERROR: | |
| 104 ++transient_error_commits; | |
| 105 break; | |
| 106 default: | |
| 107 LOG(FATAL) << "Bad return from ProcessSingleCommitResponse"; | |
| 108 } | |
| 109 } | |
| 110 MarkDeletedChildrenSynced(dir_, &trans, &deleted_folders); | |
| 111 } | |
| 112 | |
| 113 int commit_count = static_cast<int>(metahandles_.size()); | |
| 114 if (commit_count == successes) { | |
| 115 return SYNCER_OK; | |
| 116 } else if (error_commits > 0) { | |
| 117 return SERVER_RETURN_UNKNOWN_ERROR; | |
| 118 } else if (transient_error_commits > 0) { | |
| 119 return SERVER_RETURN_TRANSIENT_ERROR; | |
| 120 } else if (conflicting_commits > 0) { | |
| 121 // This means that the server already has an item with this version, but | |
| 122 // we haven't seen that update yet. | |
| 123 // | |
| 124 // A well-behaved client should respond to this by proceeding to the | |
| 125 // download updates phase, fetching the conflicting items, then attempting | |
| 126 // to resolve the conflict. That's not what this client does. | |
| 127 // | |
| 128 // We don't currently have any code to support that exceptional control | |
| 129 // flow. Instead, we abort the current sync cycle and start a new one. The | |
| 130 // end result is the same. | |
| 131 return SERVER_RETURN_CONFLICT; | |
| 132 } else { | |
| 133 LOG(FATAL) << "Inconsistent counts when processing commit response"; | |
| 134 return SYNCER_OK; | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 size_t SyncDirectoryCommitContribution::GetNumEntries() { | |
| 139 return metahandles_.size(); | |
| 140 } | |
| 141 | |
| 142 SyncDirectoryCommitContribution::SyncDirectoryCommitContribution( | |
| 143 const std::vector<int64>& metahandles, | |
| 144 const google::protobuf::RepeatedPtrField<sync_pb::SyncEntity>& entities, | |
| 145 syncable::Directory* dir) | |
| 146 : dir_(dir), | |
| 147 metahandles_(metahandles), | |
| 148 entities_(entities), | |
| 149 entries_start_index_(0xDEADBEEF) { | |
| 150 } | |
| 151 | |
| 152 } // namespace syncer | |
| OLD | NEW |