Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(446)

Side by Side Diff: sync/engine/commit.cc

Issue 25638003: sync: Implement per-type commit interface (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More review fixes Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/commit_util.h"
9 #include "sync/engine/get_commit_ids.h" 9 #include "sync/engine/sync_directory_commit_contribution.h"
10 #include "sync/engine/process_commit_response_command.h"
11 #include "sync/engine/syncer.h" 10 #include "sync/engine/syncer.h"
12 #include "sync/engine/syncer_proto_util.h" 11 #include "sync/engine/syncer_proto_util.h"
13 #include "sync/sessions/sync_session.h" 12 #include "sync/sessions/sync_session.h"
14 #include "sync/syncable/mutable_entry.h"
15 #include "sync/syncable/syncable_model_neutral_write_transaction.h"
16 13
17 namespace syncer { 14 namespace syncer {
18 15
19 using sessions::SyncSession; 16 Commit* Commit::Init(
20 using sessions::StatusController; 17 ModelTypeSet requested_types,
21 using syncable::SYNCER; 18 size_t max_entries,
22 using syncable::ModelNeutralWriteTransaction; 19 const std::string& account_name,
20 const std::string& cache_guid,
21 CommitContributorMap* contributor_map,
22 ExtensionsActivity* extensions_activity) {
23 // Gather per-type contributions.
24 ContributionMap contributions;
25 size_t num_entries = 0;
26 for (ModelTypeSet::Iterator it = requested_types.First();
27 it.Good(); it.Inc()) {
28 CommitContributorMap::iterator cm_it = contributor_map->find(it.Get());
29 if (cm_it == contributor_map->end()) {
30 NOTREACHED()
31 << "Could not find requested type " << ModelTypeToString(it.Get())
32 << " in contributor map.";
33 continue;
34 }
35 size_t spaces_remaining = max_entries - num_entries;
36 SyncDirectoryCommitContribution* contribution =
37 cm_it->second->GetContribution(spaces_remaining);
38 if (contribution) {
39 num_entries += contribution->GetNumEntries();
40 contributions.insert(std::make_pair(it.Get(), contribution));
41 }
42 if (num_entries == max_entries) {
43 break; // No point in continuting to iterate in this case.
44 }
45 }
23 46
24 namespace { 47 // Give up if no one had anything to commit.
48 if (contributions.empty())
49 return NULL;
25 50
26 // Sets the SYNCING bits of all items in the commit set to value_to_set. 51 sync_pb::ClientToServerMessage message;
27 void SetAllSyncingBitsToValue(ModelNeutralWriteTransaction* trans, 52 message.set_message_contents(sync_pb::ClientToServerMessage::COMMIT);
28 const sessions::OrderedCommitSet& commit_set, 53 message.set_share(account_name);
29 bool value_to_set) { 54
30 const std::vector<int64>& commit_handles = commit_set.GetAllCommitHandles(); 55 sync_pb::CommitMessage* commit_message = message.mutable_commit();
31 for (std::vector<int64>::const_iterator it = commit_handles.begin(); 56 commit_message->set_cache_guid(cache_guid);
32 it != commit_handles.end(); ++it) { 57
33 syncable::ModelNeutralMutableEntry entry( 58 // Set extensions activity if bookmark commits are present.
34 trans, syncable::GET_BY_HANDLE, *it); 59 ExtensionsActivity::Records extensions_activity_buffer;
35 if (entry.good()) { 60 ContributionMap::iterator it = contributions.find(syncer::BOOKMARKS);
36 entry.PutSyncing(value_to_set); 61 if (it != contributions.end() && it->second->GetNumEntries() != 0) {
37 } 62 commit_util::AddExtensionsActivityToMessage(
63 extensions_activity,
64 &extensions_activity_buffer,
65 commit_message);
66 }
67
68 // Set the client config params.
69 ModelTypeSet enabled_types;
70 for (CommitContributorMap::iterator it = contributor_map->begin();
71 it != contributor_map->end(); ++it) {
72 enabled_types.Put(it->first);
73 }
74 commit_util::AddClientConfigParamsToMessage(enabled_types,
75 commit_message);
76
77 // Finally, serialize all our contributions.
78 for (std::map<ModelType, SyncDirectoryCommitContribution*>::iterator it =
79 contributions.begin(); it != contributions.end(); ++it) {
80 it->second->AddToCommitMessage(&message);
81 }
82
83 // If we made it this far, then we've successfully prepared a commit message.
84 return new Commit(contributions, message, extensions_activity_buffer);
85 }
86
87 Commit::Commit(
88 const std::map<ModelType, SyncDirectoryCommitContribution*>& contributions,
89 const sync_pb::ClientToServerMessage& message,
90 ExtensionsActivity::Records extensions_activity_buffer)
91 : contributions_(contributions),
92 deleter_(&contributions_),
93 message_(message),
94 extensions_activity_buffer_(extensions_activity_buffer) {
95 }
96
97 Commit::~Commit() {
98 for (ContributionMap::iterator it = contributions_.begin();
Nicolas Zea 2013/10/14 21:45:35 This still has the issue of making it an implicit
rlarocque 2013/10/14 23:23:28 Done.
99 it != contributions_.end(); ++it) {
100 it->second->CleanUp();
38 } 101 }
39 } 102 }
40 103
41 // Sets the SYNCING bits for all items in the OrderedCommitSet. 104 SyncerError Commit::PostAndProcessResponse(
42 void SetSyncingBits(ModelNeutralWriteTransaction* trans, 105 sessions::SyncSession* session,
43 const sessions::OrderedCommitSet& commit_set) { 106 sessions::StatusController* status,
44 SetAllSyncingBitsToValue(trans, commit_set, true); 107 ExtensionsActivity* extensions_activity) {
45 } 108 ModelTypeSet request_types;
109 for (ContributionMap::const_iterator it = contributions_.begin();
110 it != contributions_.end(); ++it) {
111 request_types.Put(it->first);
112 }
113 session->mutable_status_controller()->set_commit_request_types(request_types);
46 114
47 // Clears the SYNCING bits for all items in the OrderedCommitSet. 115 DVLOG(1) << "Sending commit message.";
48 void ClearSyncingBits(syncable::Directory* dir, 116 TRACE_EVENT_BEGIN0("sync", "PostCommit");
49 const sessions::OrderedCommitSet& commit_set) { 117 const SyncerError post_result = SyncerProtoUtil::PostClientToServerMessage(
50 ModelNeutralWriteTransaction trans(FROM_HERE, SYNCER, dir); 118 &message_, &response_, session);
51 SetAllSyncingBitsToValue(&trans, commit_set, false); 119 TRACE_EVENT_END0("sync", "PostCommit");
52 }
53 120
54 // Helper function that finds sync items that are ready to be committed to the 121 if (post_result != SYNCER_OK) {
55 // server and serializes them into a commit message protobuf. It will return 122 LOG(WARNING) << "Post commit failed";
56 // false iff there are no entries ready to be committed at this time. 123 return post_result;
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,
68 ModelTypeSet requested_types,
69 sessions::OrderedCommitSet* commit_set,
70 sync_pb::ClientToServerMessage* commit_message,
71 ExtensionsActivity::Records* extensions_activity_buffer) {
72 TRACE_EVENT0("sync", "PrepareCommitMessage");
73
74 commit_set->Clear();
75 commit_message->Clear();
76
77 ModelNeutralWriteTransaction trans(
78 FROM_HERE, SYNCER, session->context()->directory());
79
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 } 124 }
88 125
89 // Serialize the message. 126 if (!response_.has_commit()) {
90 BuildCommitCommand build_commit_command(&trans, 127 LOG(WARNING) << "Commit response has no commit body!";
91 *commit_set, 128 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 } 129 }
171 130
172 return SYNCER_OK; 131 size_t message_entries = message_.commit().entries_size();
173 } 132 size_t response_entries = response_.commit().entryresponse_size();
133 if (message_entries != response_entries) {
134 LOG(ERROR)
135 << "Commit response has wrong number of entries! "
136 << "Expected: " << message_entries << ", "
137 << "Got: " << response_entries;
138 return SERVER_RESPONSE_VALIDATION_FAILED;
139 }
174 140
175 } // namespace 141 // Let the contributors process the responses to each of their requests.
142 SyncerError processing_result = SYNCER_OK;
143 for (std::map<ModelType, SyncDirectoryCommitContribution*>::iterator it =
144 contributions_.begin(); it != contributions_.end(); ++it) {
145 TRACE_EVENT1("sync", "ProcessCommitResponse",
146 "type", ModelTypeToString(it->first));
147 SyncerError type_result =
148 it->second->ProcessCommitResponse(response_, status);
149 if (processing_result == SYNCER_OK && type_result != SYNCER_OK) {
150 processing_result = type_result;
151 }
152 }
176 153
154 // Handle bookmarks' special extensions activity stats.
155 if (session->status_controller().
156 model_neutral_state().num_successful_bookmark_commits == 0) {
157 extensions_activity->PutRecords(extensions_activity_buffer_);
158 }
177 159
178 SyncerError BuildAndPostCommits(ModelTypeSet requested_types, 160 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 } 161 }
189 162
190 } // namespace syncer 163 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698