summaryrefslogtreecommitdiffstats
path: root/sync
diff options
context:
space:
mode:
authorlipalani@chromium.org <lipalani@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-31 00:03:39 +0000
committerlipalani@chromium.org <lipalani@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-31 00:03:39 +0000
commit96de5f3c1bcff53169190063d7a7352d4688a658 (patch)
tree82db8d3c0a1b003a2dd66956789bac65e73b6128 /sync
parent279904dc705263d7e40cfc30907247a2acd2e2d8 (diff)
downloadchromium_src-96de5f3c1bcff53169190063d7a7352d4688a658.zip
chromium_src-96de5f3c1bcff53169190063d7a7352d4688a658.tar.gz
chromium_src-96de5f3c1bcff53169190063d7a7352d4688a658.tar.bz2
We store the past 10 records of client server communication in a queue in memory.
The next patch would address the javascript side of exposing it in the UI. BUG= TEST= Review URL: http://codereview.chromium.org/9732008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@130001 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'sync')
-rw-r--r--sync/engine/syncer_proto_util.cc3
-rw-r--r--sync/engine/traffic_logger.cc4
-rw-r--r--sync/engine/traffic_recorder.cc81
-rw-r--r--sync/engine/traffic_recorder.h75
-rwxr-xr-xsync/engine/traffic_recorder_unittest.cc42
-rw-r--r--sync/sessions/sync_session_context.cc9
-rw-r--r--sync/sessions/sync_session_context.h8
-rw-r--r--sync/sync.gyp3
8 files changed, 219 insertions, 6 deletions
diff --git a/sync/engine/syncer_proto_util.cc b/sync/engine/syncer_proto_util.cc
index 5051b82..141c7e9 100644
--- a/sync/engine/syncer_proto_util.cc
+++ b/sync/engine/syncer_proto_util.cc
@@ -339,6 +339,7 @@ SyncerError SyncerProtoUtil::PostClientToServerMessage(
syncable::Directory* dir = session->context()->directory();
LogClientToServerMessage(msg);
+ session->context()->traffic_recorder()->RecordClientToServerMessage(msg);
if (!PostAndProcessHeaders(session->context()->connection_manager(), session,
msg, response)) {
// There was an error establishing communication with the server.
@@ -353,6 +354,8 @@ SyncerError SyncerProtoUtil::PostClientToServerMessage(
}
LogClientToServerResponse(*response);
+ session->context()->traffic_recorder()->RecordClientToServerResponse(
+ *response);
browser_sync::SyncProtocolError sync_protocol_error;
diff --git a/sync/engine/traffic_logger.cc b/sync/engine/traffic_logger.cc
index d849e9b..1f8ba1d 100644
--- a/sync/engine/traffic_logger.cc
+++ b/sync/engine/traffic_logger.cc
@@ -35,16 +35,12 @@ void LogData(const T& data,
void LogClientToServerMessage(const sync_pb::ClientToServerMessage& msg) {
LogData(msg, &ClientToServerMessageToValue,
"******Client To Server Message******");
- // TODO(lipalani) : Store the data (minus specifics)
- // in a circular buffer in memory.
}
void LogClientToServerResponse(
const sync_pb::ClientToServerResponse& response) {
LogData(response, &ClientToServerResponseToValue,
"******Server Response******");
- // TODO(lipalani) : Store the data (minus specifics)
- // in a circular buffer in memory.
}
} // namespace browser_sync
diff --git a/sync/engine/traffic_recorder.cc b/sync/engine/traffic_recorder.cc
new file mode 100644
index 0000000..e9ea716
--- /dev/null
+++ b/sync/engine/traffic_recorder.cc
@@ -0,0 +1,81 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "sync/engine/traffic_recorder.h"
+
+#include "base/json/json_writer.h"
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/values.h"
+#include "sync/protocol/proto_value_conversions.h"
+#include "sync/protocol/sync.pb.h"
+#include "sync/sessions/sync_session.h"
+
+namespace browser_sync {
+
+TrafficRecorder::TrafficRecord::TrafficRecord(const std::string& message,
+ TrafficMessageType message_type,
+ bool truncated) :
+ message(message),
+ message_type(message_type),
+ truncated(truncated) {
+}
+
+TrafficRecorder::TrafficRecord::TrafficRecord()
+ : message_type(UNKNOWN_MESSAGE_TYPE),
+ truncated(false) {
+}
+
+TrafficRecorder::TrafficRecord::~TrafficRecord() {
+}
+
+TrafficRecorder::TrafficRecorder(unsigned int max_messages,
+ unsigned int max_message_size)
+ : max_messages_(max_messages),
+ max_message_size_(max_message_size) {
+}
+
+TrafficRecorder::~TrafficRecorder() {
+}
+
+
+void TrafficRecorder::AddTrafficToQueue(TrafficRecord* record) {
+ records_.resize(records_.size() + 1);
+ std::swap(records_.back(), *record);
+
+ // We might have more records than our limit.
+ // Maintain the size invariant by deleting items.
+ while (records_.size() > max_messages_) {
+ records_.pop_front();
+ }
+}
+
+void TrafficRecorder::StoreProtoInQueue(
+ const ::google::protobuf::MessageLite& msg,
+ TrafficMessageType type) {
+ bool truncated = false;
+ std::string message;
+ if (static_cast<unsigned int>(msg.ByteSize()) >= max_message_size_) {
+ // TODO(lipalani): Trim the specifics to fit in size.
+ truncated = true;
+ } else {
+ msg.SerializeToString(&message);
+ }
+
+ TrafficRecord record(message, type, truncated);
+ AddTrafficToQueue(&record);
+}
+
+void TrafficRecorder::RecordClientToServerMessage(
+ const sync_pb::ClientToServerMessage& msg) {
+ StoreProtoInQueue(msg, CLIENT_TO_SERVER_MESSAGE);
+}
+
+void TrafficRecorder::RecordClientToServerResponse(
+ const sync_pb::ClientToServerResponse& response) {
+ StoreProtoInQueue(response, CLIENT_TO_SERVER_RESPONSE);
+}
+
+} // namespace browser_sync
+
diff --git a/sync/engine/traffic_recorder.h b/sync/engine/traffic_recorder.h
new file mode 100644
index 0000000..52bf8ce
--- /dev/null
+++ b/sync/engine/traffic_recorder.h
@@ -0,0 +1,75 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_SYNC_ENGINE_TRAFFIC_RECORDER_H_
+#define CHROME_BROWSER_SYNC_ENGINE_TRAFFIC_RECORDER_H_
+#pragma once
+
+#include <deque>
+#include <string>
+
+#include "base/basictypes.h"
+#include "base/gtest_prod_util.h"
+#include "sync/protocol/sync.pb.h"
+
+namespace sync_pb {
+class ClientToServerResponse;
+class ClientToServerMessage;
+}
+
+namespace browser_sync {
+
+class TrafficRecorder {
+ public:
+ enum TrafficMessageType {
+ CLIENT_TO_SERVER_MESSAGE,
+ CLIENT_TO_SERVER_RESPONSE,
+ UNKNOWN_MESSAGE_TYPE
+ };
+
+ struct TrafficRecord {
+ // The serialized message.
+ std::string message;
+ TrafficMessageType message_type;
+ // If the message is too big to be kept in memory then it should be
+ // truncated. For now the entire message is omitted if it is too big.
+ // TODO(lipalani): Truncate the specifics to fit within size.
+ bool truncated;
+
+ TrafficRecord(const std::string& message,
+ TrafficMessageType message_type,
+ bool truncated);
+ TrafficRecord();
+ ~TrafficRecord();
+ };
+
+ TrafficRecorder(unsigned int max_messages, unsigned int max_message_size);
+ ~TrafficRecorder();
+
+ void RecordClientToServerMessage(const sync_pb::ClientToServerMessage& msg);
+ void RecordClientToServerResponse(
+ const sync_pb::ClientToServerResponse& response);
+
+ const std::deque<TrafficRecord>& records() {
+ return records_;
+ }
+
+ private:
+ void AddTrafficToQueue(TrafficRecord* record);
+ void StoreProtoInQueue(const ::google::protobuf::MessageLite& msg,
+ TrafficMessageType type);
+
+ // Maximum number of messages stored in the queue.
+ unsigned int max_messages_;
+
+ // Maximum size of each message.
+ unsigned int max_message_size_;
+ std::deque<TrafficRecord> records_;
+ DISALLOW_COPY_AND_ASSIGN(TrafficRecorder);
+};
+
+} // namespace browser_sync
+
+#endif // CHROME_BROWSER_SYNC_ENGINE_TRAFFIC_RECORDER_H_
+
diff --git a/sync/engine/traffic_recorder_unittest.cc b/sync/engine/traffic_recorder_unittest.cc
new file mode 100755
index 0000000..bf3f345
--- /dev/null
+++ b/sync/engine/traffic_recorder_unittest.cc
@@ -0,0 +1,42 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "sync/engine/traffic_recorder.h"
+
+#include "sync/protocol/sync.pb.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace browser_sync {
+
+const unsigned int kMaxMessages = 10;
+const unsigned int kMaxMessageSize = 5 * 1024;
+
+// Ensure the number of records don't exceed |kMaxMessages|.
+TEST(TrafficRecorderTest, MaxRecordsTest) {
+ TrafficRecorder recorder(kMaxMessages, kMaxMessageSize);
+ sync_pb::ClientToServerResponse response;
+
+ for (unsigned int i = 0; i < 2*kMaxMessages; ++i)
+ recorder.RecordClientToServerResponse(response);
+
+ EXPECT_EQ(recorder.records().size(), kMaxMessages);
+}
+
+// Ensure records with size greater than |kMaxMessageSize| are truncated.
+TEST(TrafficRecorderTest, MaxMessageSizeTest) {
+ sync_pb::ClientToServerResponse response;
+
+ sync_pb::ClientToServerResponse::Error* error = response.mutable_error();
+ std::string error_description(kMaxMessageSize * 2, 'a');
+ error->set_error_description(error_description);
+
+ TrafficRecorder recorder(kMaxMessages, kMaxMessageSize);
+ recorder.RecordClientToServerResponse(response);
+
+ TrafficRecorder::TrafficRecord record = recorder.records().front();
+ EXPECT_TRUE(record.truncated);
+ EXPECT_TRUE(record.message.empty());
+}
+
+} //namespace browser_sync
diff --git a/sync/sessions/sync_session_context.cc b/sync/sessions/sync_session_context.cc
index 2001e7f..99b212f 100644
--- a/sync/sessions/sync_session_context.cc
+++ b/sync/sessions/sync_session_context.cc
@@ -11,6 +11,9 @@
namespace browser_sync {
namespace sessions {
+const unsigned int kMaxMessagesToRecord = 10;
+const unsigned int kMaxMessageSizeToRecord = 5 * 1024;
+
SyncSessionContext::SyncSessionContext(
ServerConnectionManager* connection_manager,
syncable::Directory* directory,
@@ -25,7 +28,8 @@ SyncSessionContext::SyncSessionContext(
extensions_activity_monitor_(extensions_activity_monitor),
notifications_enabled_(false),
max_commit_batch_size_(kDefaultMaxCommitBatchSize),
- debug_info_getter_(debug_info_getter) {
+ debug_info_getter_(debug_info_getter),
+ traffic_recorder_(kMaxMessagesToRecord, kMaxMessageSizeToRecord) {
std::vector<SyncEngineEventListener*>::const_iterator it;
for (it = listeners.begin(); it != listeners.end(); ++it)
listeners_.AddObserver(*it);
@@ -36,7 +40,8 @@ SyncSessionContext::SyncSessionContext()
directory_(NULL),
registrar_(NULL),
extensions_activity_monitor_(NULL),
- debug_info_getter_(NULL) {
+ debug_info_getter_(NULL),
+ traffic_recorder_(kMaxMessagesToRecord, kMaxMessageSizeToRecord) {
}
SyncSessionContext::~SyncSessionContext() {
diff --git a/sync/sessions/sync_session_context.h b/sync/sessions/sync_session_context.h
index 94129ce..60e3d59 100644
--- a/sync/sessions/sync_session_context.h
+++ b/sync/sessions/sync_session_context.h
@@ -27,6 +27,7 @@
#include "base/time.h"
#include "sync/engine/model_safe_worker.h"
#include "sync/engine/syncer_types.h"
+#include "sync/engine/traffic_recorder.h"
#include "sync/sessions/debug_info_getter.h"
namespace syncable {
@@ -123,6 +124,10 @@ class SyncSessionContext {
// new throttled types this will remain constant through out the sync cycle.
syncable::ModelTypeSet GetThrottledTypes() const;
+ browser_sync::TrafficRecorder* traffic_recorder() {
+ return &traffic_recorder_;
+ }
+
private:
typedef std::map<syncable::ModelType, base::TimeTicks> UnthrottleTimes;
@@ -177,6 +182,9 @@ class SyncSessionContext {
// unthrottled.
UnthrottleTimes unthrottle_times_;
+ // TODO(lipalani): Move the creation of this to |SyncManager|.
+ browser_sync::TrafficRecorder traffic_recorder_;
+
DISALLOW_COPY_AND_ASSIGN(SyncSessionContext);
};
diff --git a/sync/sync.gyp b/sync/sync.gyp
index 8a0eaa8..599f915 100644
--- a/sync/sync.gyp
+++ b/sync/sync.gyp
@@ -88,6 +88,8 @@
'engine/syncproto.h',
'engine/traffic_logger.cc',
'engine/traffic_logger.h',
+ 'engine/traffic_recorder.cc',
+ 'engine/traffic_recorder.h',
'engine/update_applicator.cc',
'engine/update_applicator.h',
'engine/verify_updates_command.cc',
@@ -290,6 +292,7 @@
'engine/sync_scheduler_whitebox_unittest.cc',
'engine/syncer_unittest.cc',
'engine/syncproto_unittest.cc',
+ 'engine/traffic_recorder_unittest.cc',
'engine/verify_updates_command_unittest.cc',
'js/js_arg_list_unittest.cc',
'js/js_event_details_unittest.cc',