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

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

Issue 10523003: Refactor following sync commit loop change (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Alternate SYNCING bits clear Created 8 years, 6 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
« no previous file with comments | « sync/engine/build_commit_command.cc ('k') | sync/engine/model_changing_syncer_command.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/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_command.h" 9 #include "sync/engine/get_commit_ids_command.h"
10 #include "sync/engine/process_commit_response_command.h" 10 #include "sync/engine/process_commit_response_command.h"
11 #include "sync/engine/syncer_proto_util.h" 11 #include "sync/engine/syncer_proto_util.h"
12 #include "sync/sessions/sync_session.h" 12 #include "sync/sessions/sync_session.h"
13 13
14 using syncable::SYNCER; 14 using syncable::SYNCER;
15 using syncable::WriteTransaction; 15 using syncable::WriteTransaction;
16 16
17 namespace browser_sync { 17 namespace browser_sync {
18 18
19 using sessions::SyncSession; 19 using sessions::SyncSession;
20 using sessions::StatusController; 20 using sessions::StatusController;
21 21
22 namespace {
23
24 void IterateOverSyncingBits(WriteTransaction* trans,
25 const sessions::OrderedCommitSet& commit_set,
26 bool value) {
tim (not reviewing) 2012/06/06 00:41:46 nit - consider renaming parameter value_to_set or
rlarocque 2012/06/06 01:01:09 How about SetAllSyncingBitsToValue()?
27 const std::vector<syncable::Id>& commit_ids = commit_set.GetAllCommitIds();
28 for (std::vector<syncable::Id>::const_iterator it = commit_ids.begin();
29 it != commit_ids.end(); ++it) {
30 syncable::MutableEntry entry(trans, syncable::GET_BY_ID, *it);
31 if (entry.good()) {
32 entry.Put(syncable::SYNCING, value);
33 }
34 }
35 }
36
37 // Sets the SYNCING bits for all items in the OrderedCommitSet.
38 void SetSyncingBits(WriteTransaction* trans,
39 const sessions::OrderedCommitSet& commit_set) {
40 IterateOverSyncingBits(trans, commit_set, true);
41 }
42
43 // Clears the SYNCING bits for all items in the OrderedCommitSet.
44 void ClearSyncingBits(syncable::Directory* dir,
tim (not reviewing) 2012/06/06 00:41:46 nit - We don't really need this function and could
rlarocque 2012/06/06 01:01:09 I like this function's name better than IterateOve
tim (not reviewing) 2012/06/08 22:30:51 Good point, although SetAllSyncingBitsToValue(fals
45 const sessions::OrderedCommitSet& commit_set) {
46 WriteTransaction trans(FROM_HERE, SYNCER, dir);
47 IterateOverSyncingBits(&trans, commit_set, false);
48 }
49
22 // Helper function that finds sync items that are ready to be committed to the 50 // Helper function that finds sync items that are ready to be committed to the
23 // server and serializes them into a commit message protobuf. It will return 51 // server and serializes them into a commit message protobuf. It will return
24 // false iff there are no entries ready to be committed at this time. 52 // false iff there are no entries ready to be committed at this time.
25 // 53 //
26 // The OrderedCommitSet parameter is an output parameter which will contain 54 // The OrderedCommitSet parameter is an output parameter which will contain
27 // the set of all items which are to be committed. The number of items in 55 // the set of all items which are to be committed. The number of items in
28 // the set shall not exceed the maximum batch size. (The default batch size 56 // the set shall not exceed the maximum batch size. (The default batch size
29 // is currently 25, though it can be overwritten by the server.) 57 // is currently 25, though it can be overwritten by the server.)
30 // 58 //
31 // The ClientToServerMessage parameter is an output parameter which will contain 59 // The ClientToServerMessage parameter is an output parameter which will contain
(...skipping 16 matching lines...) Expand all
48 get_commit_ids_command.Execute(session); 76 get_commit_ids_command.Execute(session);
49 77
50 DVLOG(1) << "Commit message will contain " << commit_set->Size() << " items."; 78 DVLOG(1) << "Commit message will contain " << commit_set->Size() << " items.";
51 if (commit_set->Empty()) 79 if (commit_set->Empty())
52 return false; 80 return false;
53 81
54 // Serialize the message. 82 // Serialize the message.
55 BuildCommitCommand build_commit_command(*commit_set, commit_message); 83 BuildCommitCommand build_commit_command(*commit_set, commit_message);
56 build_commit_command.Execute(session); 84 build_commit_command.Execute(session);
57 85
86 SetSyncingBits(session->write_transaction(), *commit_set);
58 return true; 87 return true;
59 } 88 }
60 89
61 SyncerError BuildAndPostCommits(Syncer* syncer, 90 SyncerError BuildAndPostCommitsImpl(Syncer* syncer,
62 sessions::SyncSession* session) { 91 sessions::SyncSession* session,
63 StatusController* status_controller = session->mutable_status_controller(); 92 sessions::OrderedCommitSet* commit_set) {
64
65 sessions::OrderedCommitSet commit_set(session->routing_info());
66 ClientToServerMessage commit_message; 93 ClientToServerMessage commit_message;
67 while (PrepareCommitMessage(session, &commit_set, &commit_message) 94 while (!syncer->ExitRequested() &&
68 && !syncer->ExitRequested()) { 95 PrepareCommitMessage(session, commit_set, &commit_message)) {
69 ClientToServerResponse commit_response; 96 ClientToServerResponse commit_response;
70 97
71 DVLOG(1) << "Sending commit message."; 98 DVLOG(1) << "Sending commit message.";
72 TRACE_EVENT_BEGIN0("sync", "PostCommit"); 99 TRACE_EVENT_BEGIN0("sync", "PostCommit");
73 status_controller->set_last_post_commit_result( 100 const SyncerError post_result = SyncerProtoUtil::PostClientToServerMessage(
74 SyncerProtoUtil::PostClientToServerMessage(commit_message, 101 commit_message, &commit_response, session);
75 &commit_response,
76 session));
77 TRACE_EVENT_END0("sync", "PostCommit"); 102 TRACE_EVENT_END0("sync", "PostCommit");
78 103
79 // ProcessCommitResponse includes some code that cleans up after a failure 104 if (post_result != SYNCER_OK) {
80 // to post a commit message, so we must run it regardless of whether or not 105 LOG(WARNING) << "Post commit failed";
81 // the commit succeeds. 106 return post_result;
107 }
108
109 if (!commit_response.has_commit()) {
110 LOG(WARNING) << "Commit response has no commit body!";
111 return SERVER_RESPONSE_VALIDATION_FAILED;
112 }
113
114 const size_t num_responses = commit_response.commit().entryresponse_size();
115 if (num_responses != commit_set->Size()) {
116 LOG(ERROR)
117 << "Commit response has wrong number of entries! "
118 << "Expected: " << commit_set->Size() << ", "
119 << "Got: " << num_responses;
120 return SERVER_RESPONSE_VALIDATION_FAILED;
121 }
82 122
83 TRACE_EVENT_BEGIN0("sync", "ProcessCommitResponse"); 123 TRACE_EVENT_BEGIN0("sync", "ProcessCommitResponse");
84 ProcessCommitResponseCommand process_response_command( 124 ProcessCommitResponseCommand process_response_command(
85 commit_set, commit_message, commit_response); 125 *commit_set, commit_message, commit_response);
86 status_controller->set_last_process_commit_response_result( 126 const SyncerError processing_result =
87 process_response_command.Execute(session)); 127 process_response_command.Execute(session);
88 TRACE_EVENT_END0("sync", "ProcessCommitResponse"); 128 TRACE_EVENT_END0("sync", "ProcessCommitResponse");
89 129
90 // Exit early if either the commit or the response processing failed. 130 if (processing_result != SYNCER_OK) {
91 if (status_controller->last_post_commit_result() != SYNCER_OK) 131 return processing_result;
tim (not reviewing) 2012/06/06 00:41:46 Note that elsewhere in this file we're using no-br
rlarocque 2012/06/06 01:01:09 I think 'with braces' style is more common. I'll
92 return status_controller->last_post_commit_result(); 132 }
93 if (status_controller->last_process_commit_response_result() != SYNCER_OK)
94 return status_controller->last_process_commit_response_result();
95 } 133 }
96 134
97 return SYNCER_OK; 135 return SYNCER_OK;
98 } 136 }
99 137
138 } // namespace
139
140
141 SyncerError BuildAndPostCommits(Syncer* syncer,
142 sessions::SyncSession* session) {
143 sessions::OrderedCommitSet commit_set(session->routing_info());
144 SyncerError result = BuildAndPostCommitsImpl(syncer, session, &commit_set);
145 if (result != SYNCER_OK) {
146 ClearSyncingBits(session->context()->directory(), commit_set);
147 }
148 return result;
149 }
150
100 } // namespace browser_sync 151 } // namespace browser_sync
OLDNEW
« no previous file with comments | « sync/engine/build_commit_command.cc ('k') | sync/engine/model_changing_syncer_command.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698