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,
if (h->method() != HTTP_METHOD_GET) {
h->RemoveHeader("Content-Length");
// Never use "Content-Length" set by user.
os << "Content-Length:" << (content ? content->length() : 0)
os << "Content-Length: " << (content ? content->length() : 0)
<< BRPC_CRLF;
}
//rfc 7230#section-5.4:
......@@ -543,7 +543,7 @@ void SerializeHttpRequest(base::IOBuf* request,
//the tunnel destination, seperated by a colon. For example,
//Host: server.example.com:80
if (h->GetHeader("host") == NULL) {
os << "Host:";
os << "Host: ";
if (!uri.host().empty()) {
os << uri.host();
if (uri.port() >= 0) {
......@@ -555,19 +555,19 @@ void SerializeHttpRequest(base::IOBuf* request,
os << BRPC_CRLF;
}
if (!h->content_type().empty()) {
os << "Content-Type:" << h->content_type()
os << "Content-Type: " << h->content_type()
<< BRPC_CRLF;
}
for (HttpHeader::HeaderIterator it = h->HeaderBegin();
it != h->HeaderEnd(); ++it) {
os << it->first << ':' << it->second << BRPC_CRLF;
os << it->first << ": " << it->second << BRPC_CRLF;
}
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.
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();
if (!user_info.empty() && h->GetHeader("Authorization") == NULL) {
......@@ -577,7 +577,7 @@ void SerializeHttpRequest(base::IOBuf* request,
// invalid and rejected by http_parser_parse_url().
std::string 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.move_to(*request);
......@@ -607,15 +607,15 @@ void SerializeHttpResponse(base::IOBuf* response,
// Never use "Content-Length" set by user.
// Always set Content-Length since lighttpd requires the header to be
// 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()) {
os << "Content-Type:" << h->content_type()
os << "Content-Type: " << h->content_type()
<< BRPC_CRLF;
}
for (HttpHeader::HeaderIterator it = h->HeaderBegin();
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.move_to(*response);
......
......@@ -12,6 +12,8 @@
namespace bvar {
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;
// Combine two circular linked list into one.
......@@ -82,8 +84,10 @@ void SamplerCollector::run() {
base::LinkNode<Sampler> root;
int consecutive_nosleep = 0;
PassiveStatus<double> cumulated_time(get_cumulated_time, this);
bvar::PerSecond<bvar::PassiveStatus<double> > usage(
"bvar_sampler_collector_usage", &cumulated_time, 10);
bvar::PerSecond<bvar::PassiveStatus<double> > usage(&cumulated_time, 10);
if (FLAGS_show_sampler_usage) {
usage.expose("bvar_sampler_collector_usage");
}
while (!_stop) {
int64_t abstime = base::gettimeofday_us();
Sampler* s = this->reset();
......@@ -96,15 +100,15 @@ void SamplerCollector::run() {
// We may remove p from the list, save next first.
base::LinkNode<Sampler>* saved_next = p->next();
Sampler* s = p->value();
pthread_mutex_lock(&s->_mutex);
s->_mutex.lock();
if (!s->_used) {
pthread_mutex_unlock(&s->_mutex);
s->_mutex.unlock();
p->RemoveFromList();
delete s;
++nremoved;
} else {
s->take_sample();
pthread_mutex_unlock(&s->_mutex);
s->_mutex.unlock();
++nsampled;
}
p = saved_next;
......@@ -130,22 +134,18 @@ void SamplerCollector::run() {
}
}
Sampler::Sampler() : _used(true) {
pthread_mutex_init(&_mutex, NULL);
}
Sampler::Sampler() : _used(true) {}
Sampler::~Sampler() {
pthread_mutex_destroy(&_mutex);
}
Sampler::~Sampler() {}
void Sampler::schedule() {
*base::get_leaky_singleton<SamplerCollector>() << this;
}
void Sampler::destroy() {
pthread_mutex_lock(&_mutex);
_mutex.lock();
_used = false;
pthread_mutex_unlock(&_mutex);
_mutex.unlock();
}
} // namespace detail
......
......@@ -5,7 +5,6 @@
#ifndef BVAR_DETAIL_SAMPLER_H
#define BVAR_DETAIL_SAMPLER_H
#include <pthread.h> // pthread_mutex_t
#include "base/containers/linked_list.h"// LinkNode
#include "base/scoped_lock.h" // BAIDU_SCOPED_LOCK
#include "base/logging.h" // LOG()
......@@ -49,7 +48,7 @@ protected:
friend class SamplerCollector;
bool _used;
// Sync destroy() and take_sample().
pthread_mutex_t _mutex;
base::Mutex _mutex;
};
// Representing a non-existing operator so that we can test
......
......@@ -329,66 +329,16 @@ StaticLibrary('test', Sources(GLOB('*.pb.cc')))
Application('test_base',
Sources(' '.join(base_ut)) + Sources(' '.join(opt_base_ut), opt_cxx_flags),
Libraries('libbase_debug.a'))
Application('test_bvar', Sources(GLOB('bvar_*_unittest.cpp'), opt_cxx_flags),
Libraries('libbvar_debug.a libbase_debug.a'))
bthread_ut = [
"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",
]
bthread_ut = GLOB('bthread_*unittest.cpp').split(' ')
for ut in bthread_ut:
Application(ut[:-4], Sources(ut, opt_cxx_flags),
Libraries('libbthread_debug.a libbvar_debug.a libbase_debug.a'))
brpc_ut = [
"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",
]
brpc_ut = GLOB('brpc_*unittest.cpp').split(' ')
for ut in brpc_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'))
......
......@@ -247,7 +247,7 @@ TEST(HttpMessageTest, find_method_property_by_uri) {
ASSERT_EQ("flags", mp->method->service()->name());
ASSERT_EQ("foo/bar", unknown_method);
mp = FindMethodPropertyByURI("/baidu.rpc.flags/$*",
mp = FindMethodPropertyByURI("/brpc.flags/$*",
&server, &unknown_method);
ASSERT_TRUE(mp);
ASSERT_EQ("flags", mp->method->service()->name());
......@@ -335,37 +335,37 @@ TEST(HttpMessageTest, serialize_http_request) {
base::IOBuf content;
content.append("data");
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.
header.SetHeader("Content-Length", "100");
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
header.SetHeader("Host", "MyHost:4321");
header.SetHeader("Host", "MyHost: 4321");
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
header.SetHeader("accePT"/*intended uppercase*/, "blahblah");
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
header.SetHeader("user-AGENT", "myUA");
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
header.SetHeader("authorization", "myAuthString");
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
header.set_method(brpc::HTTP_METHOD_GET);
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) {
......@@ -376,7 +376,7 @@ TEST(HttpMessageTest, serialize_http_response) {
base::IOBuf content;
content.append("data");
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.
CHECK(content.empty());
......@@ -384,11 +384,11 @@ TEST(HttpMessageTest, serialize_http_response) {
content.append("data2");
header.SetHeader("Content-Length", "100");
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
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
......@@ -14,10 +14,18 @@
#include "bvar/bvar.h"
#include <gtest/gtest.h>
namespace bvar {
namespace detail {
extern bool FLAGS_show_sampler_usage;
}
}
namespace {
class StatusTest : public testing::Test {
protected:
void SetUp() {}
void SetUp() {
bvar::detail::FLAGS_show_sampler_usage = false;
}
void TearDown() {
ASSERT_EQ(0UL, bvar::Variable::count_exposed());
}
......
......@@ -19,6 +19,9 @@
namespace bvar {
DECLARE_bool(bvar_log_dumpped);
namespace detail {
extern bool FLAGS_show_sampler_usage;
}
}
template <typename T>
......@@ -37,7 +40,9 @@ std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
namespace {
class VariableTest : public testing::Test {
protected:
void SetUp() {}
void SetUp() {
bvar::detail::FLAGS_show_sampler_usage = false;
}
void TearDown() {
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