| 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/commit.h" | 5 #include "sync/engine/commit.h" |
| 6 | 6 |
| 7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
| 8 #include "sync/engine/build_commit_command.h" | 8 #include "sync/engine/build_commit_command.h" |
| 9 #include "sync/engine/get_commit_ids.h" | |
| 10 #include "sync/engine/process_commit_response_command.h" | 9 #include "sync/engine/process_commit_response_command.h" |
| 10 #include "sync/engine/sync_directory_commit_contribution.h" |
| 11 #include "sync/engine/syncer.h" | 11 #include "sync/engine/syncer.h" |
| 12 #include "sync/engine/syncer_proto_util.h" | 12 #include "sync/engine/syncer_proto_util.h" |
| 13 #include "sync/sessions/sync_session.h" | 13 #include "sync/sessions/sync_session.h" |
| 14 #include "sync/syncable/mutable_entry.h" | |
| 15 #include "sync/syncable/syncable_model_neutral_write_transaction.h" | |
| 16 | 14 |
| 17 namespace syncer { | 15 namespace syncer { |
| 18 | 16 |
| 19 using sessions::SyncSession; | 17 Commit* Commit::Init( |
| 20 using sessions::StatusController; | 18 ModelTypeSet requested_types, |
| 21 using syncable::SYNCER; | 19 size_t max_entries, |
| 22 using syncable::ModelNeutralWriteTransaction; | 20 const std::string& account_name, |
| 23 | 21 const std::string& cache_guid, |
| 24 namespace { | 22 CommitContributorMap* contributor_map, |
| 25 | 23 ExtensionsActivity* extensions_activity) { |
| 26 // Sets the SYNCING bits of all items in the commit set to value_to_set. | 24 // Gather per-type contributions. |
| 27 void SetAllSyncingBitsToValue(ModelNeutralWriteTransaction* trans, | 25 ContributionMap contributions; |
| 28 const sessions::OrderedCommitSet& commit_set, | 26 size_t num_entries = 0; |
| 29 bool value_to_set) { | 27 for (ModelTypeSet::Iterator it = requested_types.First(); |
| 30 const std::vector<int64>& commit_handles = commit_set.GetAllCommitHandles(); | 28 it.Good(); it.Inc()) { |
| 31 for (std::vector<int64>::const_iterator it = commit_handles.begin(); | 29 CommitContributorMap::iterator cm_it = contributor_map->find(it.Get()); |
| 32 it != commit_handles.end(); ++it) { | 30 if (cm_it == contributor_map->end()) { |
| 33 syncable::ModelNeutralMutableEntry entry( | 31 NOTREACHED() |
| 34 trans, syncable::GET_BY_HANDLE, *it); | 32 << "Could not find requested type " << ModelTypeToString(it.Get()) |
| 35 if (entry.good()) { | 33 << " in contributor map."; |
| 36 entry.PutSyncing(value_to_set); | 34 continue; |
| 35 } |
| 36 size_t spaces_remaining = max_entries - num_entries; |
| 37 SyncDirectoryCommitContribution* contribution = |
| 38 cm_it->second->GetContribution(spaces_remaining); |
| 39 if (contribution) { |
| 40 num_entries += contribution->GetNumEntries(); |
| 41 contributions.insert(std::make_pair(it.Get(), contribution)); |
| 42 } |
| 43 if (num_entries == max_entries) { |
| 44 break; // No point in continuting to iterate in this case. |
| 37 } | 45 } |
| 38 } | 46 } |
| 47 |
| 48 // Give up if no one had anything to commit. |
| 49 if (contributions.empty()) |
| 50 return NULL; |
| 51 |
| 52 sync_pb::ClientToServerMessage message; |
| 53 message.set_message_contents(sync_pb::ClientToServerMessage::COMMIT); |
| 54 message.set_share(account_name); |
| 55 |
| 56 sync_pb::CommitMessage* commit_message = message.mutable_commit(); |
| 57 commit_message->set_cache_guid(cache_guid); |
| 58 |
| 59 // Set extensions activity if bookmark commits are present. |
| 60 ExtensionsActivity::Records extensions_activity_buffer; |
| 61 ContributionMap::iterator it = contributions.find(syncer::BOOKMARKS); |
| 62 if (it != contributions.end() && it->second->GetNumEntries() != 0) { |
| 63 BuildCommitCommand::AddExtensionsActivityToMessage( |
| 64 extensions_activity, |
| 65 &extensions_activity_buffer, |
| 66 commit_message); |
| 67 } |
| 68 |
| 69 // Set the client config params. |
| 70 ModelTypeSet enabled_types; |
| 71 for (CommitContributorMap::iterator it = contributor_map->begin(); |
| 72 it != contributor_map->end(); ++it) { |
| 73 enabled_types.Put(it->first); |
| 74 } |
| 75 BuildCommitCommand::AddClientConfigParamsToMessage(enabled_types, |
| 76 commit_message); |
| 77 |
| 78 // Finally, serialize all our contributions. |
| 79 for (std::map<ModelType, SyncDirectoryCommitContribution*>::iterator it = |
| 80 contributions.begin(); it != contributions.end(); ++it) { |
| 81 it->second->AddToCommitMessage(&message); |
| 82 } |
| 83 |
| 84 // If we made it this far, then we've successfully prepared a commit message. |
| 85 return new Commit(contributions, message, extensions_activity_buffer); |
| 39 } | 86 } |
| 40 | 87 |
| 41 // Sets the SYNCING bits for all items in the OrderedCommitSet. | 88 Commit::Commit( |
| 42 void SetSyncingBits(ModelNeutralWriteTransaction* trans, | 89 const std::map<ModelType, SyncDirectoryCommitContribution*>& contributions, |
| 43 const sessions::OrderedCommitSet& commit_set) { | 90 const sync_pb::ClientToServerMessage& message, |
| 44 SetAllSyncingBitsToValue(trans, commit_set, true); | 91 ExtensionsActivity::Records extensions_activity_buffer) |
| 92 : contributions_(contributions), |
| 93 deleter_(&contributions_), |
| 94 message_(message), |
| 95 extensions_activity_buffer_(extensions_activity_buffer) { |
| 45 } | 96 } |
| 46 | 97 |
| 47 // Clears the SYNCING bits for all items in the OrderedCommitSet. | 98 Commit::~Commit() { |
| 48 void ClearSyncingBits(syncable::Directory* dir, | |
| 49 const sessions::OrderedCommitSet& commit_set) { | |
| 50 ModelNeutralWriteTransaction trans(FROM_HERE, SYNCER, dir); | |
| 51 SetAllSyncingBitsToValue(&trans, commit_set, false); | |
| 52 } | 99 } |
| 53 | 100 |
| 54 // Helper function that finds sync items that are ready to be committed to the | 101 SyncerError Commit::PostAndProcessResponse( |
| 55 // server and serializes them into a commit message protobuf. It will return | |
| 56 // false iff there are no entries ready to be committed at this time. | |
| 57 // | |
| 58 // The OrderedCommitSet parameter is an output parameter which will contain | |
| 59 // the set of all items which are to be committed. The number of items in | |
| 60 // the set shall not exceed the maximum batch size. (The default batch size | |
| 61 // is currently 25, though it can be overwritten by the server.) | |
| 62 // | |
| 63 // The ClientToServerMessage parameter is an output parameter which will contain | |
| 64 // the commit message which should be sent to the server. It is valid iff the | |
| 65 // return value of this function is true. | |
| 66 bool PrepareCommitMessage( | |
| 67 sessions::SyncSession* session, | 102 sessions::SyncSession* session, |
| 68 ModelTypeSet requested_types, | 103 sessions::StatusController* status, |
| 69 sessions::OrderedCommitSet* commit_set, | 104 ExtensionsActivity* extensions_activity) { |
| 70 sync_pb::ClientToServerMessage* commit_message, | 105 ModelTypeSet request_types; |
| 71 ExtensionsActivity::Records* extensions_activity_buffer) { | 106 for (ContributionMap::const_iterator it = contributions_.begin(); |
| 72 TRACE_EVENT0("sync", "PrepareCommitMessage"); | 107 it != contributions_.end(); ++it) { |
| 108 request_types.Put(it->first); |
| 109 } |
| 110 session->mutable_status_controller()->set_commit_request_types(request_types); |
| 73 | 111 |
| 74 commit_set->Clear(); | 112 DVLOG(1) << "Sending commit message."; |
| 75 commit_message->Clear(); | 113 TRACE_EVENT_BEGIN0("sync", "PostCommit"); |
| 114 const SyncerError post_result = SyncerProtoUtil::PostClientToServerMessage( |
| 115 &message_, &response_, session); |
| 116 TRACE_EVENT_END0("sync", "PostCommit"); |
| 76 | 117 |
| 77 ModelNeutralWriteTransaction trans( | 118 if (post_result != SYNCER_OK) { |
| 78 FROM_HERE, SYNCER, session->context()->directory()); | 119 LOG(WARNING) << "Post commit failed"; |
| 79 | 120 return post_result; |
| 80 // Fetch the items to commit. | |
| 81 const size_t batch_size = session->context()->max_commit_batch_size(); | |
| 82 GetCommitIds(&trans, requested_types, batch_size, commit_set); | |
| 83 | |
| 84 DVLOG(1) << "Commit message will contain " << commit_set->Size() << " items."; | |
| 85 if (commit_set->Empty()) { | |
| 86 return false; | |
| 87 } | 121 } |
| 88 | 122 |
| 89 // Serialize the message. | 123 if (!response_.has_commit()) { |
| 90 BuildCommitCommand build_commit_command(&trans, | 124 LOG(WARNING) << "Commit response has no commit body!"; |
| 91 *commit_set, | 125 return SERVER_RESPONSE_VALIDATION_FAILED; |
| 92 commit_message, | |
| 93 extensions_activity_buffer); | |
| 94 build_commit_command.Execute(session); | |
| 95 | |
| 96 SetSyncingBits(&trans, *commit_set); | |
| 97 return true; | |
| 98 } | |
| 99 | |
| 100 SyncerError BuildAndPostCommitsImpl(ModelTypeSet requested_types, | |
| 101 Syncer* syncer, | |
| 102 sessions::SyncSession* session, | |
| 103 sessions::OrderedCommitSet* commit_set) { | |
| 104 ModelTypeSet commit_request_types; | |
| 105 while (!syncer->ExitRequested()) { | |
| 106 sync_pb::ClientToServerMessage commit_message; | |
| 107 ExtensionsActivity::Records extensions_activity_buffer; | |
| 108 | |
| 109 if (!PrepareCommitMessage(session, | |
| 110 requested_types, | |
| 111 commit_set, | |
| 112 &commit_message, | |
| 113 &extensions_activity_buffer)) { | |
| 114 break; | |
| 115 } | |
| 116 | |
| 117 commit_request_types.PutAll(commit_set->Types()); | |
| 118 session->mutable_status_controller()->set_commit_request_types( | |
| 119 commit_request_types); | |
| 120 | |
| 121 sync_pb::ClientToServerResponse commit_response; | |
| 122 | |
| 123 DVLOG(1) << "Sending commit message."; | |
| 124 TRACE_EVENT_BEGIN0("sync", "PostCommit"); | |
| 125 const SyncerError post_result = SyncerProtoUtil::PostClientToServerMessage( | |
| 126 &commit_message, &commit_response, session); | |
| 127 TRACE_EVENT_END0("sync", "PostCommit"); | |
| 128 | |
| 129 // TODO(rlarocque): Put all the post-commit logic in one place. | |
| 130 // See crbug.com/196338. | |
| 131 | |
| 132 if (post_result != SYNCER_OK) { | |
| 133 LOG(WARNING) << "Post commit failed"; | |
| 134 return post_result; | |
| 135 } | |
| 136 | |
| 137 if (!commit_response.has_commit()) { | |
| 138 LOG(WARNING) << "Commit response has no commit body!"; | |
| 139 return SERVER_RESPONSE_VALIDATION_FAILED; | |
| 140 } | |
| 141 | |
| 142 const size_t num_responses = commit_response.commit().entryresponse_size(); | |
| 143 if (num_responses != commit_set->Size()) { | |
| 144 LOG(ERROR) | |
| 145 << "Commit response has wrong number of entries! " | |
| 146 << "Expected: " << commit_set->Size() << ", " | |
| 147 << "Got: " << num_responses; | |
| 148 return SERVER_RESPONSE_VALIDATION_FAILED; | |
| 149 } | |
| 150 | |
| 151 TRACE_EVENT_BEGIN0("sync", "ProcessCommitResponse"); | |
| 152 ProcessCommitResponseCommand process_response_command( | |
| 153 *commit_set, commit_message, commit_response); | |
| 154 const SyncerError processing_result = | |
| 155 process_response_command.Execute(session); | |
| 156 TRACE_EVENT_END0("sync", "ProcessCommitResponse"); | |
| 157 | |
| 158 // If the commit failed, return the data to the ExtensionsActivityMonitor. | |
| 159 if (session->status_controller(). | |
| 160 model_neutral_state().num_successful_bookmark_commits == 0) { | |
| 161 ExtensionsActivity* extensions_activity = | |
| 162 session->context()->extensions_activity(); | |
| 163 extensions_activity->PutRecords(extensions_activity_buffer); | |
| 164 } | |
| 165 | |
| 166 if (processing_result != SYNCER_OK) { | |
| 167 return processing_result; | |
| 168 } | |
| 169 session->SendEventNotification(SyncEngineEvent::STATUS_CHANGED); | |
| 170 } | 126 } |
| 171 | 127 |
| 172 return SYNCER_OK; | 128 size_t message_entries = message_.commit().entries_size(); |
| 173 } | 129 size_t response_entries = response_.commit().entryresponse_size(); |
| 130 if (message_entries != response_entries) { |
| 131 LOG(ERROR) |
| 132 << "Commit response has wrong number of entries! " |
| 133 << "Expected: " << message_entries << ", " |
| 134 << "Got: " << response_entries; |
| 135 return SERVER_RESPONSE_VALIDATION_FAILED; |
| 136 } |
| 174 | 137 |
| 175 } // namespace | 138 // Let the contributors process the responses to each of their requests. |
| 139 SyncerError processing_result = SYNCER_OK; |
| 140 for (std::map<ModelType, SyncDirectoryCommitContribution*>::iterator it = |
| 141 contributions_.begin(); it != contributions_.end(); ++it) { |
| 142 TRACE_EVENT1("sync", "ProcessCommitResponse", |
| 143 "type", ModelTypeToString(it->first)); |
| 144 SyncerError type_result = |
| 145 it->second->ProcessCommitResponse(response_, status); |
| 146 if (processing_result == SYNCER_OK && type_result != SYNCER_OK) { |
| 147 processing_result = type_result; |
| 148 } |
| 149 } |
| 176 | 150 |
| 151 // Handle bookmarks' special extensions activity stats. |
| 152 if (session->status_controller(). |
| 153 model_neutral_state().num_successful_bookmark_commits == 0) { |
| 154 extensions_activity->PutRecords(extensions_activity_buffer_); |
| 155 } |
| 177 | 156 |
| 178 SyncerError BuildAndPostCommits(ModelTypeSet requested_types, | 157 return processing_result; |
| 179 Syncer* syncer, | |
| 180 sessions::SyncSession* session) { | |
| 181 sessions::OrderedCommitSet commit_set; | |
| 182 SyncerError result = | |
| 183 BuildAndPostCommitsImpl(requested_types, syncer, session, &commit_set); | |
| 184 if (result != SYNCER_OK) { | |
| 185 ClearSyncingBits(session->context()->directory(), commit_set); | |
| 186 } | |
| 187 return result; | |
| 188 } | 158 } |
| 189 | 159 |
| 190 } // namespace syncer | 160 } // namespace syncer |
| OLD | NEW |