commit.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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
3// found in the LICENSE file.
4
5#include "sync/engine/commit.h"
6
7#include "base/debug/trace_event.h"
8#include "sync/engine/build_commit_command.h"
9#include "sync/engine/get_commit_ids_command.h"
10#include "sync/engine/process_commit_response_command.h"
11#include "sync/engine/syncer_proto_util.h"
12#include "sync/sessions/sync_session.h"
13#include "sync/syncable/mutable_entry.h"
14#include "sync/syncable/syncable_write_transaction.h"
15
16namespace syncer {
17
18using sessions::SyncSession;
19using sessions::StatusController;
20using syncable::SYNCER;
21using syncable::WriteTransaction;
22
23namespace {
24
25// Sets the SYNCING bits of all items in the commit set to value_to_set.
26void SetAllSyncingBitsToValue(WriteTransaction* trans,
27                              const sessions::OrderedCommitSet& commit_set,
28                              bool value_to_set) {
29  const std::vector<syncable::Id>& commit_ids = commit_set.GetAllCommitIds();
30  for (std::vector<syncable::Id>::const_iterator it = commit_ids.begin();
31       it != commit_ids.end(); ++it) {
32    syncable::MutableEntry entry(trans, syncable::GET_BY_ID, *it);
33    if (entry.good()) {
34      entry.Put(syncable::SYNCING, value_to_set);
35    }
36  }
37}
38
39// Sets the SYNCING bits for all items in the OrderedCommitSet.
40void SetSyncingBits(WriteTransaction* trans,
41                    const sessions::OrderedCommitSet& commit_set) {
42  SetAllSyncingBitsToValue(trans, commit_set, true);
43}
44
45// Clears the SYNCING bits for all items in the OrderedCommitSet.
46void ClearSyncingBits(syncable::Directory* dir,
47                      const sessions::OrderedCommitSet& commit_set) {
48  WriteTransaction trans(FROM_HERE, SYNCER, dir);
49  SetAllSyncingBitsToValue(&trans, commit_set, false);
50}
51
52// Helper function that finds sync items that are ready to be committed to the
53// server and serializes them into a commit message protobuf.  It will return
54// false iff there are no entries ready to be committed at this time.
55//
56// The OrderedCommitSet parameter is an output parameter which will contain
57// the set of all items which are to be committed.  The number of items in
58// the set shall not exceed the maximum batch size.  (The default batch size
59// is currently 25, though it can be overwritten by the server.)
60//
61// The ClientToServerMessage parameter is an output parameter which will contain
62// the commit message which should be sent to the server.  It is valid iff the
63// return value of this function is true.
64bool PrepareCommitMessage(
65    sessions::SyncSession* session,
66    ModelTypeSet requested_types,
67    sessions::OrderedCommitSet* commit_set,
68    sync_pb::ClientToServerMessage* commit_message,
69    ExtensionsActivityMonitor::Records* extensions_activity_buffer) {
70  TRACE_EVENT0("sync", "PrepareCommitMessage");
71
72  commit_set->Clear();
73  commit_message->Clear();
74
75  WriteTransaction trans(FROM_HERE, SYNCER, session->context()->directory());
76
77  // Fetch the items to commit.
78  const size_t batch_size = session->context()->max_commit_batch_size();
79  GetCommitIdsCommand get_commit_ids_command(&trans,
80                                             requested_types,
81                                             batch_size,
82                                             commit_set);
83  get_commit_ids_command.Execute(session);
84
85  DVLOG(1) << "Commit message will contain " << commit_set->Size() << " items.";
86  if (commit_set->Empty()) {
87    return false;
88  }
89
90  // Serialize the message.
91  BuildCommitCommand build_commit_command(&trans,
92                                          *commit_set,
93                                          commit_message,
94                                          extensions_activity_buffer);
95  build_commit_command.Execute(session);
96
97  SetSyncingBits(&trans, *commit_set);
98  return true;
99}
100
101SyncerError BuildAndPostCommitsImpl(ModelTypeSet requested_types,
102                                    Syncer* syncer,
103                                    sessions::SyncSession* session,
104                                    sessions::OrderedCommitSet* commit_set) {
105  while (!syncer->ExitRequested()) {
106    sync_pb::ClientToServerMessage commit_message;
107    ExtensionsActivityMonitor::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    sync_pb::ClientToServerResponse commit_response;
118
119    DVLOG(1) << "Sending commit message.";
120    TRACE_EVENT_BEGIN0("sync", "PostCommit");
121    const SyncerError post_result = SyncerProtoUtil::PostClientToServerMessage(
122        &commit_message, &commit_response, session);
123    TRACE_EVENT_END0("sync", "PostCommit");
124
125    // TODO(rlarocque): Put all the post-commit logic in one place.
126    // See crbug.com/196338.
127
128    if (post_result != SYNCER_OK) {
129      LOG(WARNING) << "Post commit failed";
130      return post_result;
131    }
132
133    if (!commit_response.has_commit()) {
134      LOG(WARNING) << "Commit response has no commit body!";
135      return SERVER_RESPONSE_VALIDATION_FAILED;
136    }
137
138    const size_t num_responses = commit_response.commit().entryresponse_size();
139    if (num_responses != commit_set->Size()) {
140      LOG(ERROR)
141         << "Commit response has wrong number of entries! "
142         << "Expected: " << commit_set->Size() << ", "
143         << "Got: " << num_responses;
144      return SERVER_RESPONSE_VALIDATION_FAILED;
145    }
146
147    TRACE_EVENT_BEGIN0("sync", "ProcessCommitResponse");
148    ProcessCommitResponseCommand process_response_command(
149        *commit_set, commit_message, commit_response);
150    const SyncerError processing_result =
151        process_response_command.Execute(session);
152    TRACE_EVENT_END0("sync", "ProcessCommitResponse");
153
154    // If the commit failed, return the data to the ExtensionsActivityMonitor.
155    if (session->status_controller().
156        model_neutral_state().num_successful_bookmark_commits == 0) {
157      ExtensionsActivityMonitor* extensions_activity_monitor =
158          session->context()->extensions_monitor();
159      extensions_activity_monitor->PutRecords(extensions_activity_buffer);
160    }
161
162    if (processing_result != SYNCER_OK) {
163      return processing_result;
164    }
165    session->SendEventNotification(SyncEngineEvent::STATUS_CHANGED);
166  }
167
168  return SYNCER_OK;
169}
170
171}  // namespace
172
173
174SyncerError BuildAndPostCommits(ModelTypeSet requested_types,
175                                Syncer* syncer,
176                                sessions::SyncSession* session) {
177  sessions::OrderedCommitSet commit_set(session->context()->routing_info());
178  SyncerError result =
179      BuildAndPostCommitsImpl(requested_types, syncer, session, &commit_set);
180  if (result != SYNCER_OK) {
181    ClearSyncingBits(session->context()->directory(), commit_set);
182  }
183  return result;
184}
185
186}  // namespace syncer
187