| 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 // Sets the SYNCING bits of all items in the commit set to value_to_set. |
| 25 void SetAllSyncingBitsToValue(WriteTransaction* trans, |
| 26 const sessions::OrderedCommitSet& commit_set, |
| 27 bool value_to_set) { |
| 28 const std::vector<syncable::Id>& commit_ids = commit_set.GetAllCommitIds(); |
| 29 for (std::vector<syncable::Id>::const_iterator it = commit_ids.begin(); |
| 30 it != commit_ids.end(); ++it) { |
| 31 syncable::MutableEntry entry(trans, syncable::GET_BY_ID, *it); |
| 32 if (entry.good()) { |
| 33 entry.Put(syncable::SYNCING, value_to_set); |
| 34 } |
| 35 } |
| 36 } |
| 37 |
| 38 // Sets the SYNCING bits for all items in the OrderedCommitSet. |
| 39 void SetSyncingBits(WriteTransaction* trans, |
| 40 const sessions::OrderedCommitSet& commit_set) { |
| 41 SetAllSyncingBitsToValue(trans, commit_set, true); |
| 42 } |
| 43 |
| 44 // Clears the SYNCING bits for all items in the OrderedCommitSet. |
| 45 void ClearSyncingBits(syncable::Directory* dir, |
| 46 const sessions::OrderedCommitSet& commit_set) { |
| 47 WriteTransaction trans(FROM_HERE, SYNCER, dir); |
| 48 SetAllSyncingBitsToValue(&trans, commit_set, false); |
| 49 } |
| 50 |
| 22 // Helper function that finds sync items that are ready to be committed to the | 51 // 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 | 52 // 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. | 53 // false iff there are no entries ready to be committed at this time. |
| 25 // | 54 // |
| 26 // The OrderedCommitSet parameter is an output parameter which will contain | 55 // 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 | 56 // 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 | 57 // 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.) | 58 // is currently 25, though it can be overwritten by the server.) |
| 30 // | 59 // |
| 31 // The ClientToServerMessage parameter is an output parameter which will contain | 60 // The ClientToServerMessage parameter is an output parameter which will contain |
| 32 // the commit message which should be sent to the server. It is valid iff the | 61 // the commit message which should be sent to the server. It is valid iff the |
| 33 // return value of this function is true. | 62 // return value of this function is true. |
| 34 bool PrepareCommitMessage(sessions::SyncSession* session, | 63 bool PrepareCommitMessage(sessions::SyncSession* session, |
| 35 sessions::OrderedCommitSet* commit_set, | 64 sessions::OrderedCommitSet* commit_set, |
| 36 ClientToServerMessage* commit_message) { | 65 ClientToServerMessage* commit_message) { |
| 37 TRACE_EVENT0("sync", "PrepareCommitMessage"); | 66 TRACE_EVENT0("sync", "PrepareCommitMessage"); |
| 38 | 67 |
| 39 commit_set->Clear(); | 68 commit_set->Clear(); |
| 40 commit_message->Clear(); | 69 commit_message->Clear(); |
| 41 | 70 |
| 42 WriteTransaction trans(FROM_HERE, SYNCER, session->context()->directory()); | 71 WriteTransaction trans(FROM_HERE, SYNCER, session->context()->directory()); |
| 43 sessions::ScopedSetSessionWriteTransaction set_trans(session, &trans); | 72 sessions::ScopedSetSessionWriteTransaction set_trans(session, &trans); |
| 44 | 73 |
| 45 // Fetch the items to commit. | 74 // Fetch the items to commit. |
| 46 const size_t batch_size = session->context()->max_commit_batch_size(); | 75 const size_t batch_size = session->context()->max_commit_batch_size(); |
| 47 GetCommitIdsCommand get_commit_ids_command(batch_size, commit_set); | 76 GetCommitIdsCommand get_commit_ids_command(batch_size, commit_set); |
| 48 get_commit_ids_command.Execute(session); | 77 get_commit_ids_command.Execute(session); |
| 49 | 78 |
| 50 DVLOG(1) << "Commit message will contain " << commit_set->Size() << " items."; | 79 DVLOG(1) << "Commit message will contain " << commit_set->Size() << " items."; |
| 51 if (commit_set->Empty()) | 80 if (commit_set->Empty()) { |
| 52 return false; | 81 return false; |
| 82 } |
| 53 | 83 |
| 54 // Serialize the message. | 84 // Serialize the message. |
| 55 BuildCommitCommand build_commit_command(*commit_set, commit_message); | 85 BuildCommitCommand build_commit_command(*commit_set, commit_message); |
| 56 build_commit_command.Execute(session); | 86 build_commit_command.Execute(session); |
| 57 | 87 |
| 88 SetSyncingBits(session->write_transaction(), *commit_set); |
| 58 return true; | 89 return true; |
| 59 } | 90 } |
| 60 | 91 |
| 61 SyncerError BuildAndPostCommits(Syncer* syncer, | 92 SyncerError BuildAndPostCommitsImpl(Syncer* syncer, |
| 62 sessions::SyncSession* session) { | 93 sessions::SyncSession* session, |
| 63 StatusController* status_controller = session->mutable_status_controller(); | 94 sessions::OrderedCommitSet* commit_set) { |
| 64 | |
| 65 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 const 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 return post_result; |
| 109 } |
| 110 |
| 111 if (!commit_response.has_commit()) { |
| 112 LOG(WARNING) << "Commit response has no commit body!"; |
| 113 return SERVER_RESPONSE_VALIDATION_FAILED; |
| 114 } |
| 115 |
| 116 const size_t num_responses = commit_response.commit().entryresponse_size(); |
| 117 if (num_responses != commit_set->Size()) { |
| 118 LOG(ERROR) |
| 119 << "Commit response has wrong number of entries! " |
| 120 << "Expected: " << commit_set->Size() << ", " |
| 121 << "Got: " << num_responses; |
| 122 return SERVER_RESPONSE_VALIDATION_FAILED; |
| 123 } |
| 82 | 124 |
| 83 TRACE_EVENT_BEGIN0("sync", "ProcessCommitResponse"); | 125 TRACE_EVENT_BEGIN0("sync", "ProcessCommitResponse"); |
| 84 ProcessCommitResponseCommand process_response_command( | 126 ProcessCommitResponseCommand process_response_command( |
| 85 commit_set, commit_message, commit_response); | 127 *commit_set, commit_message, commit_response); |
| 86 status_controller->set_last_process_commit_response_result( | 128 const SyncerError processing_result = |
| 87 process_response_command.Execute(session)); | 129 process_response_command.Execute(session); |
| 88 TRACE_EVENT_END0("sync", "ProcessCommitResponse"); | 130 TRACE_EVENT_END0("sync", "ProcessCommitResponse"); |
| 89 | 131 |
| 90 // Exit early if either the commit or the response processing failed. | 132 if (processing_result != SYNCER_OK) { |
| 91 if (status_controller->last_post_commit_result() != SYNCER_OK) | 133 return processing_result; |
| 92 return status_controller->last_post_commit_result(); | 134 } |
| 93 if (status_controller->last_process_commit_response_result() != SYNCER_OK) | |
| 94 return status_controller->last_process_commit_response_result(); | |
| 95 } | 135 } |
| 96 | 136 |
| 97 return SYNCER_OK; | 137 return SYNCER_OK; |
| 98 } | 138 } |
| 99 | 139 |
| 140 } // namespace |
| 141 |
| 142 |
| 143 SyncerError BuildAndPostCommits(Syncer* syncer, |
| 144 sessions::SyncSession* session) { |
| 145 sessions::OrderedCommitSet commit_set(session->routing_info()); |
| 146 SyncerError result = BuildAndPostCommitsImpl(syncer, session, &commit_set); |
| 147 if (result != SYNCER_OK) { |
| 148 ClearSyncingBits(session->context()->directory(), commit_set); |
| 149 } |
| 150 return result; |
| 151 } |
| 152 |
| 100 } // namespace browser_sync | 153 } // namespace browser_sync |
| OLD | NEW |