Commit b8cd3c98 authored by gejun's avatar gejun

Patch svn r34974 r34975

Change-Id: If1bd623fd133a0b7eeebacd396a80f4595c15d4c
parent 691b8a15
...@@ -530,7 +530,7 @@ void SerializeHttpRequest(base::IOBuf* request, ...@@ -530,7 +530,7 @@ void SerializeHttpRequest(base::IOBuf* request,
if (h->method() != HTTP_METHOD_GET) { if (h->method() != HTTP_METHOD_GET) {
h->RemoveHeader("Content-Length"); h->RemoveHeader("Content-Length");
// Never use "Content-Length" set by user. // Never use "Content-Length" set by user.
os << "Content-Length:" << (content ? content->length() : 0) os << "Content-Length: " << (content ? content->length() : 0)
<< BRPC_CRLF; << BRPC_CRLF;
} }
//rfc 7230#section-5.4: //rfc 7230#section-5.4:
...@@ -543,7 +543,7 @@ void SerializeHttpRequest(base::IOBuf* request, ...@@ -543,7 +543,7 @@ void SerializeHttpRequest(base::IOBuf* request,
//the tunnel destination, seperated by a colon. For example, //the tunnel destination, seperated by a colon. For example,
//Host: server.example.com:80 //Host: server.example.com:80
if (h->GetHeader("host") == NULL) { if (h->GetHeader("host") == NULL) {
os << "Host:"; os << "Host: ";
if (!uri.host().empty()) { if (!uri.host().empty()) {
os << uri.host(); os << uri.host();
if (uri.port() >= 0) { if (uri.port() >= 0) {
...@@ -555,19 +555,19 @@ void SerializeHttpRequest(base::IOBuf* request, ...@@ -555,19 +555,19 @@ void SerializeHttpRequest(base::IOBuf* request,
os << BRPC_CRLF; os << BRPC_CRLF;
} }
if (!h->content_type().empty()) { if (!h->content_type().empty()) {
os << "Content-Type:" << h->content_type() os << "Content-Type: " << h->content_type()
<< BRPC_CRLF; << BRPC_CRLF;
} }
for (HttpHeader::HeaderIterator it = h->HeaderBegin(); for (HttpHeader::HeaderIterator it = h->HeaderBegin();
it != h->HeaderEnd(); ++it) { it != h->HeaderEnd(); ++it) {
os << it->first << ':' << it->second << BRPC_CRLF; os << it->first << ": " << it->second << BRPC_CRLF;
} }
if (h->GetHeader("Accept") == NULL) { if (h->GetHeader("Accept") == NULL) {
os << "Accept:*/*" BRPC_CRLF; os << "Accept: */*" BRPC_CRLF;
} }
// The fake "curl" user-agent may let servers return plain-text results. // The fake "curl" user-agent may let servers return plain-text results.
if (h->GetHeader("User-Agent") == NULL) { if (h->GetHeader("User-Agent") == NULL) {
os << "User-Agent:baidu-rpc/1.0 curl/7.0" BRPC_CRLF; os << "User-Agent: baidu-rpc/1.0 curl/7.0" BRPC_CRLF;
} }
const std::string& user_info = h->uri().user_info(); const std::string& user_info = h->uri().user_info();
if (!user_info.empty() && h->GetHeader("Authorization") == NULL) { if (!user_info.empty() && h->GetHeader("Authorization") == NULL) {
...@@ -577,7 +577,7 @@ void SerializeHttpRequest(base::IOBuf* request, ...@@ -577,7 +577,7 @@ void SerializeHttpRequest(base::IOBuf* request,
// invalid and rejected by http_parser_parse_url(). // invalid and rejected by http_parser_parse_url().
std::string encoded_user_info; std::string encoded_user_info;
base::Base64Encode(user_info, &encoded_user_info); base::Base64Encode(user_info, &encoded_user_info);
os << "Authorization:Basic " << encoded_user_info << BRPC_CRLF; os << "Authorization: Basic " << encoded_user_info << BRPC_CRLF;
} }
os << BRPC_CRLF; // CRLF before content os << BRPC_CRLF; // CRLF before content
os.move_to(*request); os.move_to(*request);
...@@ -607,15 +607,15 @@ void SerializeHttpResponse(base::IOBuf* response, ...@@ -607,15 +607,15 @@ void SerializeHttpResponse(base::IOBuf* response,
// Never use "Content-Length" set by user. // Never use "Content-Length" set by user.
// Always set Content-Length since lighttpd requires the header to be // Always set Content-Length since lighttpd requires the header to be
// set to 0 for empty content. // set to 0 for empty content.
os << "Content-Length:" << content->length() << BRPC_CRLF; os << "Content-Length: " << content->length() << BRPC_CRLF;
} }
if (!h->content_type().empty()) { if (!h->content_type().empty()) {
os << "Content-Type:" << h->content_type() os << "Content-Type: " << h->content_type()
<< BRPC_CRLF; << BRPC_CRLF;
} }
for (HttpHeader::HeaderIterator it = h->HeaderBegin(); for (HttpHeader::HeaderIterator it = h->HeaderBegin();
it != h->HeaderEnd(); ++it) { it != h->HeaderEnd(); ++it) {
os << it->first << ':' << it->second << BRPC_CRLF; os << it->first << ": " << it->second << BRPC_CRLF;
} }
os << BRPC_CRLF; // CRLF before content os << BRPC_CRLF; // CRLF before content
os.move_to(*response); os.move_to(*response);
......
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
namespace bvar { namespace bvar {
namespace detail { namespace detail {
// set to true in UT. Not using gflags since users hardly need to change it.
bool FLAGS_show_sampler_usage = true;
const int WARN_NOSLEEP_THRESHOLD = 2; const int WARN_NOSLEEP_THRESHOLD = 2;
// Combine two circular linked list into one. // Combine two circular linked list into one.
...@@ -82,8 +84,10 @@ void SamplerCollector::run() { ...@@ -82,8 +84,10 @@ void SamplerCollector::run() {
base::LinkNode<Sampler> root; base::LinkNode<Sampler> root;
int consecutive_nosleep = 0; int consecutive_nosleep = 0;
PassiveStatus<double> cumulated_time(get_cumulated_time, this); PassiveStatus<double> cumulated_time(get_cumulated_time, this);
bvar::PerSecond<bvar::PassiveStatus<double> > usage( bvar::PerSecond<bvar::PassiveStatus<double> > usage(&cumulated_time, 10);
"bvar_sampler_collector_usage", &cumulated_time, 10); if (FLAGS_show_sampler_usage) {
usage.expose("bvar_sampler_collector_usage");
}
while (!_stop) { while (!_stop) {
int64_t abstime = base::gettimeofday_us(); int64_t abstime = base::gettimeofday_us();
Sampler* s = this->reset(); Sampler* s = this->reset();
...@@ -96,15 +100,15 @@ void SamplerCollector::run() { ...@@ -96,15 +100,15 @@ void SamplerCollector::run() {
// We may remove p from the list, save next first. // We may remove p from the list, save next first.
base::LinkNode<Sampler>* saved_next = p->next(); base::LinkNode<Sampler>* saved_next = p->next();
Sampler* s = p->value(); Sampler* s = p->value();
pthread_mutex_lock(&s->_mutex); s->_mutex.lock();
if (!s->_used) { if (!s->_used) {
pthread_mutex_unlock(&s->_mutex); s->_mutex.unlock();
p->RemoveFromList(); p->RemoveFromList();
delete s; delete s;
++nremoved; ++nremoved;
} else { } else {
s->take_sample(); s->take_sample();
pthread_mutex_unlock(&s->_mutex); s->_mutex.unlock();
++nsampled; ++nsampled;
} }
p = saved_next; p = saved_next;
...@@ -130,22 +134,18 @@ void SamplerCollector::run() { ...@@ -130,22 +134,18 @@ void SamplerCollector::run() {
} }
} }
Sampler::Sampler() : _used(true) { Sampler::Sampler() : _used(true) {}
pthread_mutex_init(&_mutex, NULL);
}
Sampler::~Sampler() { Sampler::~Sampler() {}
pthread_mutex_destroy(&_mutex);
}
void Sampler::schedule() { void Sampler::schedule() {
*base::get_leaky_singleton<SamplerCollector>() << this; *base::get_leaky_singleton<SamplerCollector>() << this;
} }
void Sampler::destroy() { void Sampler::destroy() {
pthread_mutex_lock(&_mutex); _mutex.lock();
_used = false; _used = false;
pthread_mutex_unlock(&_mutex); _mutex.unlock();
} }
} // namespace detail } // namespace detail
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#ifndef BVAR_DETAIL_SAMPLER_H #ifndef BVAR_DETAIL_SAMPLER_H
#define BVAR_DETAIL_SAMPLER_H #define BVAR_DETAIL_SAMPLER_H
#include <pthread.h> // pthread_mutex_t
#include "base/containers/linked_list.h"// LinkNode #include "base/containers/linked_list.h"// LinkNode
#include "base/scoped_lock.h" // BAIDU_SCOPED_LOCK #include "base/scoped_lock.h" // BAIDU_SCOPED_LOCK
#include "base/logging.h" // LOG() #include "base/logging.h" // LOG()
...@@ -49,7 +48,7 @@ protected: ...@@ -49,7 +48,7 @@ protected:
friend class SamplerCollector; friend class SamplerCollector;
bool _used; bool _used;
// Sync destroy() and take_sample(). // Sync destroy() and take_sample().
pthread_mutex_t _mutex; base::Mutex _mutex;
}; };
// Representing a non-existing operator so that we can test // Representing a non-existing operator so that we can test
......
...@@ -329,66 +329,16 @@ StaticLibrary('test', Sources(GLOB('*.pb.cc'))) ...@@ -329,66 +329,16 @@ StaticLibrary('test', Sources(GLOB('*.pb.cc')))
Application('test_base', Application('test_base',
Sources(' '.join(base_ut)) + Sources(' '.join(opt_base_ut), opt_cxx_flags), Sources(' '.join(base_ut)) + Sources(' '.join(opt_base_ut), opt_cxx_flags),
Libraries('libbase_debug.a')) Libraries('libbase_debug.a'))
Application('test_bvar', Sources(GLOB('bvar_*_unittest.cpp'), opt_cxx_flags), Application('test_bvar', Sources(GLOB('bvar_*_unittest.cpp'), opt_cxx_flags),
Libraries('libbvar_debug.a libbase_debug.a')) Libraries('libbvar_debug.a libbase_debug.a'))
bthread_ut = [ bthread_ut = GLOB('bthread_*unittest.cpp').split(' ')
"bthread_butex_unittest.cpp",
"bthread_execution_queue_unittest.cpp",
"bthread_key_unittest.cpp",
"bthread_rwlock_unittest.cpp",
"bthread_unittest.cpp",
"bthread_cond_unittest.cpp",
"bthread_fd_unittest.cpp",
"bthread_list_unittest.cpp",
"bthread_sched_yield_unittest.cpp",
"bthread_work_stealing_queue_unittest.cpp",
"bthread_countdown_event_unittest.cpp",
"bthread_futex_unittest.cpp",
"bthread_mutex_unittest.cpp",
"bthread_setconcurrency_unittest.cpp",
"bthread_dispatcher_unittest.cpp",
"bthread_id_unittest.cpp",
"bthread_ping_pong_unittest.cpp",
"bthread_timer_thread_unittest.cpp",
]
for ut in bthread_ut: for ut in bthread_ut:
Application(ut[:-4], Sources(ut, opt_cxx_flags), Application(ut[:-4], Sources(ut, opt_cxx_flags),
Libraries('libbthread_debug.a libbvar_debug.a libbase_debug.a')) Libraries('libbthread_debug.a libbvar_debug.a libbase_debug.a'))
brpc_ut = [ brpc_ut = GLOB('brpc_*unittest.cpp').split(' ')
"brpc_builtin_service_unittest.cpp",
"brpc_channel_unittest.cpp",
"brpc_controller_unittest.cpp",
"brpc_esp_protocol_unittest.cpp",
"brpc_event_dispatcher_unittest.cpp",
"brpc_extension_unittest.cpp",
"brpc_hpack_unittest.cpp",
"brpc_http_message_unittest.cpp",
"brpc_http_parser_unittest.cpp",
"brpc_http_rpc_protocol_unittest.cpp",
"brpc_http_status_code_unittest.cpp",
"brpc_hulu_pbrpc_protocol_unittest.cpp",
"brpc_input_messenger_unittest.cpp",
"brpc_load_balancer_unittest.cpp",
"brpc_memcache_unittest.cpp",
"brpc_mongo_protocol_unittest.cpp",
"brpc_naming_service_filter_unittest.cpp",
"brpc_naming_service_unittest.cpp",
"brpc_nova_pbrpc_protocol_unittest.cpp",
"brpc_proto_unittest.cpp",
"brpc_public_pbrpc_protocol_unittest.cpp",
"brpc_redis_unittest.cpp",
"brpc_rtmp_unittest.cpp",
"brpc_server_unittest.cpp",
"brpc_snappy_compress_unittest.cpp",
"brpc_socket_map_unittest.cpp",
"brpc_socket_unittest.cpp",
"brpc_sofa_pbrpc_protocol_unittest.cpp",
"brpc_streaming_rpc_unittest.cpp",
"brpc_uri_unittest.cpp",
]
for ut in brpc_ut: for ut in brpc_ut:
Application(ut[:-4], Sources(ut), Application(ut[:-4], Sources(ut),
Libraries('libtest.a libbrpc_debug.a libbthread_debug.a libbvar_debug.a libbase_debug.a libjson2pb_debug.a libmcpack2pb_debug.a')) Libraries('libtest.a libbrpc_debug.a libbthread_debug.a libbvar_debug.a libbase_debug.a libjson2pb_debug.a libmcpack2pb_debug.a'))
......
...@@ -247,7 +247,7 @@ TEST(HttpMessageTest, find_method_property_by_uri) { ...@@ -247,7 +247,7 @@ TEST(HttpMessageTest, find_method_property_by_uri) {
ASSERT_EQ("flags", mp->method->service()->name()); ASSERT_EQ("flags", mp->method->service()->name());
ASSERT_EQ("foo/bar", unknown_method); ASSERT_EQ("foo/bar", unknown_method);
mp = FindMethodPropertyByURI("/baidu.rpc.flags/$*", mp = FindMethodPropertyByURI("/brpc.flags/$*",
&server, &unknown_method); &server, &unknown_method);
ASSERT_TRUE(mp); ASSERT_TRUE(mp);
ASSERT_EQ("flags", mp->method->service()->name()); ASSERT_EQ("flags", mp->method->service()->name());
...@@ -335,37 +335,37 @@ TEST(HttpMessageTest, serialize_http_request) { ...@@ -335,37 +335,37 @@ TEST(HttpMessageTest, serialize_http_request) {
base::IOBuf content; base::IOBuf content;
content.append("data"); content.append("data");
SerializeHttpRequest(&request, &header, ep, &content); SerializeHttpRequest(&request, &header, ep, &content);
ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length:4\r\nHost:127.0.0.1:1234\r\nFoo:Bar\r\nAccept:*/*\r\nUser-Agent:baidu-rpc/1.0 curl/7.0\r\n\r\ndata", request); ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length: 4\r\nHost: 127.0.0.1:1234\r\nFoo: Bar\r\nAccept: */*\r\nUser-Agent: baidu-rpc/1.0 curl/7.0\r\n\r\ndata", request);
// user-set content-length is ignored. // user-set content-length is ignored.
header.SetHeader("Content-Length", "100"); header.SetHeader("Content-Length", "100");
SerializeHttpRequest(&request, &header, ep, &content); SerializeHttpRequest(&request, &header, ep, &content);
ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length:4\r\nHost:127.0.0.1:1234\r\nFoo:Bar\r\nAccept:*/*\r\nUser-Agent:baidu-rpc/1.0 curl/7.0\r\n\r\ndata", request); ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length: 4\r\nHost: 127.0.0.1:1234\r\nFoo: Bar\r\nAccept: */*\r\nUser-Agent: baidu-rpc/1.0 curl/7.0\r\n\r\ndata", request);
// user-host overwrites passed-in remote_side // user-host overwrites passed-in remote_side
header.SetHeader("Host", "MyHost:4321"); header.SetHeader("Host", "MyHost: 4321");
SerializeHttpRequest(&request, &header, ep, &content); SerializeHttpRequest(&request, &header, ep, &content);
ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length:4\r\nFoo:Bar\r\nHost:MyHost:4321\r\nAccept:*/*\r\nUser-Agent:baidu-rpc/1.0 curl/7.0\r\n\r\ndata", request); ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length: 4\r\nFoo: Bar\r\nHost: MyHost: 4321\r\nAccept: */*\r\nUser-Agent: baidu-rpc/1.0 curl/7.0\r\n\r\ndata", request);
// user-set accept // user-set accept
header.SetHeader("accePT"/*intended uppercase*/, "blahblah"); header.SetHeader("accePT"/*intended uppercase*/, "blahblah");
SerializeHttpRequest(&request, &header, ep, &content); SerializeHttpRequest(&request, &header, ep, &content);
ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length:4\r\naccePT:blahblah\r\nFoo:Bar\r\nHost:MyHost:4321\r\nUser-Agent:baidu-rpc/1.0 curl/7.0\r\n\r\ndata", request); ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length: 4\r\naccePT: blahblah\r\nFoo: Bar\r\nHost: MyHost: 4321\r\nUser-Agent: baidu-rpc/1.0 curl/7.0\r\n\r\ndata", request);
// user-set UA // user-set UA
header.SetHeader("user-AGENT", "myUA"); header.SetHeader("user-AGENT", "myUA");
SerializeHttpRequest(&request, &header, ep, &content); SerializeHttpRequest(&request, &header, ep, &content);
ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length:4\r\naccePT:blahblah\r\nuser-AGENT:myUA\r\nFoo:Bar\r\nHost:MyHost:4321\r\n\r\ndata", request); ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length: 4\r\naccePT: blahblah\r\nuser-AGENT: myUA\r\nFoo: Bar\r\nHost: MyHost: 4321\r\n\r\ndata", request);
// user-set Authorization // user-set Authorization
header.SetHeader("authorization", "myAuthString"); header.SetHeader("authorization", "myAuthString");
SerializeHttpRequest(&request, &header, ep, &content); SerializeHttpRequest(&request, &header, ep, &content);
ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length:4\r\naccePT:blahblah\r\nuser-AGENT:myUA\r\nauthorization:myAuthString\r\nFoo:Bar\r\nHost:MyHost:4321\r\n\r\ndata", request); ASSERT_EQ("POST / HTTP/1.1\r\nContent-Length: 4\r\naccePT: blahblah\r\nuser-AGENT: myUA\r\nauthorization: myAuthString\r\nFoo: Bar\r\nHost: MyHost: 4321\r\n\r\ndata", request);
// GET does not serialize content // GET does not serialize content
header.set_method(brpc::HTTP_METHOD_GET); header.set_method(brpc::HTTP_METHOD_GET);
SerializeHttpRequest(&request, &header, ep, &content); SerializeHttpRequest(&request, &header, ep, &content);
ASSERT_EQ("GET / HTTP/1.1\r\naccePT:blahblah\r\nuser-AGENT:myUA\r\nauthorization:myAuthString\r\nFoo:Bar\r\nHost:MyHost:4321\r\n\r\n", request); ASSERT_EQ("GET / HTTP/1.1\r\naccePT: blahblah\r\nuser-AGENT: myUA\r\nauthorization: myAuthString\r\nFoo: Bar\r\nHost: MyHost: 4321\r\n\r\n", request);
} }
TEST(HttpMessageTest, serialize_http_response) { TEST(HttpMessageTest, serialize_http_response) {
...@@ -376,7 +376,7 @@ TEST(HttpMessageTest, serialize_http_response) { ...@@ -376,7 +376,7 @@ TEST(HttpMessageTest, serialize_http_response) {
base::IOBuf content; base::IOBuf content;
content.append("data"); content.append("data");
SerializeHttpResponse(&response, &header, &content); SerializeHttpResponse(&response, &header, &content);
ASSERT_EQ("HTTP/1.1 200 OK\r\nContent-Length:4\r\nFoo:Bar\r\n\r\ndata", response); ASSERT_EQ("HTTP/1.1 200 OK\r\nContent-Length: 4\r\nFoo: Bar\r\n\r\ndata", response);
// content is cleared. // content is cleared.
CHECK(content.empty()); CHECK(content.empty());
...@@ -384,11 +384,11 @@ TEST(HttpMessageTest, serialize_http_response) { ...@@ -384,11 +384,11 @@ TEST(HttpMessageTest, serialize_http_response) {
content.append("data2"); content.append("data2");
header.SetHeader("Content-Length", "100"); header.SetHeader("Content-Length", "100");
SerializeHttpResponse(&response, &header, &content); SerializeHttpResponse(&response, &header, &content);
ASSERT_EQ("HTTP/1.1 200 OK\r\nContent-Length:5\r\nFoo:Bar\r\n\r\ndata2", response); ASSERT_EQ("HTTP/1.1 200 OK\r\nContent-Length: 5\r\nFoo: Bar\r\n\r\ndata2", response);
// null content // null content
SerializeHttpResponse(&response, &header, NULL); SerializeHttpResponse(&response, &header, NULL);
ASSERT_EQ("HTTP/1.1 200 OK\r\nFoo:Bar\r\n\r\n", response); ASSERT_EQ("HTTP/1.1 200 OK\r\nFoo: Bar\r\n\r\n", response);
} }
} //namespace } //namespace
...@@ -14,10 +14,18 @@ ...@@ -14,10 +14,18 @@
#include "bvar/bvar.h" #include "bvar/bvar.h"
#include <gtest/gtest.h> #include <gtest/gtest.h>
namespace bvar {
namespace detail {
extern bool FLAGS_show_sampler_usage;
}
}
namespace { namespace {
class StatusTest : public testing::Test { class StatusTest : public testing::Test {
protected: protected:
void SetUp() {} void SetUp() {
bvar::detail::FLAGS_show_sampler_usage = false;
}
void TearDown() { void TearDown() {
ASSERT_EQ(0UL, bvar::Variable::count_exposed()); ASSERT_EQ(0UL, bvar::Variable::count_exposed());
} }
......
...@@ -19,6 +19,9 @@ ...@@ -19,6 +19,9 @@
namespace bvar { namespace bvar {
DECLARE_bool(bvar_log_dumpped); DECLARE_bool(bvar_log_dumpped);
namespace detail {
extern bool FLAGS_show_sampler_usage;
}
} }
template <typename T> template <typename T>
...@@ -37,7 +40,9 @@ std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) { ...@@ -37,7 +40,9 @@ std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
namespace { namespace {
class VariableTest : public testing::Test { class VariableTest : public testing::Test {
protected: protected:
void SetUp() {} void SetUp() {
bvar::detail::FLAGS_show_sampler_usage = false;
}
void TearDown() { void TearDown() {
ASSERT_EQ(0UL, bvar::Variable::count_exposed()); ASSERT_EQ(0UL, bvar::Variable::count_exposed());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment