Chromium Code Reviews| OLD | NEW |
|---|---|
| 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) { | |
| 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, | |
| 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 Loading... | |
| 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 |
| 90 } // namespace | |
| 91 | |
| 61 SyncerError BuildAndPostCommits(Syncer* syncer, | 92 SyncerError BuildAndPostCommits(Syncer* syncer, |
| 62 sessions::SyncSession* session) { | 93 sessions::SyncSession* session) { |
| 63 StatusController* status_controller = session->mutable_status_controller(); | |
| 64 | |
| 65 sessions::OrderedCommitSet commit_set(session->routing_info()); | 94 sessions::OrderedCommitSet commit_set(session->routing_info()); |
| 66 ClientToServerMessage commit_message; | 95 ClientToServerMessage commit_message; |
| 67 while (PrepareCommitMessage(session, &commit_set, &commit_message) | 96 while (!syncer->ExitRequested() && |
| 68 && !syncer->ExitRequested()) { | 97 PrepareCommitMessage(session, &commit_set, &commit_message)) { |
| 69 ClientToServerResponse commit_response; | 98 ClientToServerResponse commit_response; |
| 70 | 99 |
| 71 DVLOG(1) << "Sending commit message."; | 100 DVLOG(1) << "Sending commit message."; |
| 72 TRACE_EVENT_BEGIN0("sync", "PostCommit"); | 101 TRACE_EVENT_BEGIN0("sync", "PostCommit"); |
| 73 status_controller->set_last_post_commit_result( | 102 SyncerError post_result = SyncerProtoUtil::PostClientToServerMessage( |
| 74 SyncerProtoUtil::PostClientToServerMessage(commit_message, | 103 commit_message, &commit_response, session); |
| 75 &commit_response, | |
| 76 session)); | |
| 77 TRACE_EVENT_END0("sync", "PostCommit"); | 104 TRACE_EVENT_END0("sync", "PostCommit"); |
| 78 | 105 |
| 79 // ProcessCommitResponse includes some code that cleans up after a failure | 106 if (post_result != SYNCER_OK) { |
| 80 // to post a commit message, so we must run it regardless of whether or not | 107 LOG(WARNING) << "Post commit failed"; |
| 81 // the commit succeeds. | 108 ClearSyncingBits(session->context()->directory(), commit_set); |
|
tim (not reviewing)
2012/06/05 19:18:25
Now that you've grouped everything here, instead o
rlarocque
2012/06/05 20:51:14
I considered something like that. I was actually
tim (not reviewing)
2012/06/05 21:16:46
I see. I was imagining a class with a single "Coas
| |
| 109 return post_result; | |
| 110 } | |
| 111 | |
| 112 if (!commit_response.has_commit()) { | |
| 113 LOG(WARNING) << "Commit response has no commit body!"; | |
| 114 ClearSyncingBits(session->context()->directory(), commit_set); | |
| 115 return SERVER_RESPONSE_VALIDATION_FAILED; | |
| 116 } | |
| 117 | |
| 118 size_t num_responses = commit_response.commit().entryresponse_size(); | |
|
tim (not reviewing)
2012/06/05 19:18:25
nit - const
rlarocque
2012/06/05 20:51:14
Fixed here and in a few other places.
| |
| 119 if (num_responses != commit_set.Size()) { | |
| 120 LOG(ERROR) | |
| 121 << "Commit response has wrong number of entries! " | |
| 122 << "Expected: " << commit_set.Size() << ", " | |
| 123 << "Got: " << num_responses; | |
| 124 ClearSyncingBits(session->context()->directory(), commit_set); | |
| 125 return SERVER_RESPONSE_VALIDATION_FAILED; | |
| 126 } | |
| 82 | 127 |
| 83 TRACE_EVENT_BEGIN0("sync", "ProcessCommitResponse"); | 128 TRACE_EVENT_BEGIN0("sync", "ProcessCommitResponse"); |
| 84 ProcessCommitResponseCommand process_response_command( | 129 ProcessCommitResponseCommand process_response_command( |
| 85 commit_set, commit_message, commit_response); | 130 commit_set, commit_message, commit_response); |
| 86 status_controller->set_last_process_commit_response_result( | 131 SyncerError processing_result = process_response_command.Execute(session); |
| 87 process_response_command.Execute(session)); | |
| 88 TRACE_EVENT_END0("sync", "ProcessCommitResponse"); | 132 TRACE_EVENT_END0("sync", "ProcessCommitResponse"); |
| 89 | 133 |
| 90 // Exit early if either the commit or the response processing failed. | 134 if (processing_result != SYNCER_OK) { |
| 91 if (status_controller->last_post_commit_result() != SYNCER_OK) | 135 return processing_result; |
| 92 return status_controller->last_post_commit_result(); | 136 } |
| 93 if (status_controller->last_process_commit_response_result() != SYNCER_OK) | |
| 94 return status_controller->last_process_commit_response_result(); | |
| 95 } | 137 } |
| 96 | 138 |
| 97 return SYNCER_OK; | 139 return SYNCER_OK; |
| 98 } | 140 } |
| 99 | 141 |
| 100 } // namespace browser_sync | 142 } // namespace browser_sync |
| OLD | NEW |