Commit 592d24b4 authored by gejun's avatar gejun

Fix http_verbose related issues

parent 012ca4cc
......@@ -150,7 +150,7 @@ Notes on http header:
# 查看HTTP消息
打开[-http_verbose](http://brpc.baidu.com:8765/flags/http_verbose)即可在stderr看到所有的http request和response,注意这应该只用于线下调试,而不是线上程序。
打开[-http_verbose](http://brpc.baidu.com:8765/flags/http_verbose)即可看到所有的http request和response,注意这应该只用于线下调试,而不是线上程序。
# HTTP错误
......
......@@ -273,7 +273,7 @@ cntl->http_request().uri().SetQuery("time", "2015/1/2");
# 调试
打开[-http_verbose](http://brpc.baidu.com:8765/flags/http_verbose)即可在stderr看到所有的http request和response,注意这应该只用于线下调试,而不是线上程序。
打开[-http_verbose](http://brpc.baidu.com:8765/flags/http_verbose)即可看到所有的http request和response,注意这应该只用于线下调试,而不是线上程序。
# 压缩response body
......
......@@ -11,7 +11,7 @@
像http一样,brpc保证在最差情况下解析redis reply的时间复杂度也是O(N),N是reply的字节数,而不是O($N^2$)。当reply是个较大的数组时,这是比较重要的。
加上[-redis_verbose](#查看发出的请求和收到的回复)后会在stderr上打印出所有的redis request和response供调试。
加上[-redis_verbose](#查看发出的请求和收到的回复)后会打印出所有的redis request和response供调试。
# 访问单台redis
......@@ -151,13 +151,13 @@ response中的所有reply的ownership属于response。当response析构时,rep
# 查看发出的请求和收到的回复
打开[-redis_verbose](http://brpc.baidu.com:8765/flags/redis_verbose)可在stderr看到所有的redis request和response,注意这应该只用于线下调试,而不是线上程序。
打开[-redis_verbose](http://brpc.baidu.com:8765/flags/redis_verbose)即看到所有的redis request和response,注意这应该只用于线下调试,而不是线上程序。
打开[-redis_verbose_crlf2space](http://brpc.baidu.com:8765/flags/redis_verbose_crlf2space)可让打印内容中的CRLF (\r\n)变为空格,方便阅读。
| Name | Value | Description | Defined At |
| ------------------------ | ----- | ---------------------------------------- | ---------------------------------- |
| redis_verbose | false | [DEBUG] Print EVERY redis request/response to stderr | src/brpc/policy/redis_protocol.cpp |
| redis_verbose | false | [DEBUG] Print EVERY redis request/response | src/brpc/policy/redis_protocol.cpp |
| redis_verbose_crlf2space | false | [DEBUG] Show \r\n as a space | src/brpc/redis.cpp |
# 性能
......
......@@ -157,7 +157,7 @@ Notes on http header:
# Debug HTTP messages
Turn on [-http_verbose](http://brpc.baidu.com:8765/flags/http_verbose) so that the framework prints each http request and response in stderr. Note that this should only be used in tests or debuggings rather than online services.
Turn on [-http_verbose](http://brpc.baidu.com:8765/flags/http_verbose) so that the framework prints each http request and response. Note that this should only be used in tests or debuggings rather than online services.
# HTTP errors
......
......@@ -272,7 +272,7 @@ cntl->http_request().uri().SetQuery("time", "2015/1/2");
# Debugging
Turn on [-http_verbose](http://brpc.baidu.com:8765/flags/http_verbose) to print contents of all http requests and responses to stderr. Note that this should only be used for debugging rather than online services.
Turn on [-http_verbose](http://brpc.baidu.com:8765/flags/http_verbose) to print contents of all http requests and responses. Note that this should only be used for debugging rather than online services.
# Compress the response body
......
......@@ -11,7 +11,7 @@ Advantages compared to [hiredis](https://github.com/redis/hiredis) (the official
Similarly with http, brpc guarantees that the time complexity of parsing redis replies is O(N) in worst cases rather than O(N^2) , where N is the number of bytes of reply. This is important when the reply consists of large arrays.
Turn on [-redis_verbose](#Debug) to print contents of all redis requests and responses to stderr, which is for debugging only.
Turn on [-redis_verbose](#Debug) to print contents of all redis requests and responses, which is for debugging only.
# Request a redis server
......@@ -151,13 +151,13 @@ Another choice is to use the common [twemproxy](https://github.com/twitter/twemp
# Debug
Turn on [-redis_verbose](http://brpc.baidu.com:8765/flags/redis_verbose) to print contents of all redis requests and responses to stderr. Note that this should only be used for debugging rather than online services.
Turn on [-redis_verbose](http://brpc.baidu.com:8765/flags/redis_verbose) to print contents of all redis requests and responses. Note that this should only be used for debugging rather than online services.
Turn on [-redis_verbose_crlf2space](http://brpc.baidu.com:8765/flags/redis_verbose_crlf2space) to replace the `CRLF` (\r\n) with spaces in debugging logs for better readability.
| Name | Value | Description | Defined At |
| ------------------------ | ----- | ---------------------------------------- | ---------------------------------- |
| redis_verbose | false | [DEBUG] Print EVERY redis request/response to stderr | src/brpc/policy/redis_protocol.cpp |
| redis_verbose | false | [DEBUG] Print EVERY redis request/response | src/brpc/policy/redis_protocol.cpp |
| redis_verbose_crlf2space | false | [DEBUG] Show \r\n as a space | src/brpc/redis.cpp |
# Performance
......
......@@ -1392,7 +1392,7 @@ void Controller::set_mongo_session_data(MongoContext* data) {
bool Controller::is_ssl() const {
Socket* s = _current_call.sending_sock.get();
return s ? (s->ssl_state() == SSL_CONNECTED) : false;
return s != NULL && s->is_ssl();
}
x509_st* Controller::get_peer_certificate() const {
......
......@@ -33,7 +33,7 @@
namespace brpc {
DEFINE_bool(http_verbose, false,
"[DEBUG] Print EVERY http request/response to stderr");
"[DEBUG] Print EVERY http request/response");
DEFINE_int32(http_verbose_max_body_length, 512,
"[DEBUG] Max body length printed when -http_verbose is on");
DECLARE_int64(socket_max_unwritten_bytes);
......@@ -111,13 +111,13 @@ int HttpMessage::on_header_value(http_parser *parser,
vs = new butil::IOBufBuilder;
http_message->_vmsgbuilder = vs;
if (parser->type == HTTP_REQUEST) {
*vs << "[HTTP REQUEST @" << butil::my_ip() << "]\n< "
*vs << "[ HTTP REQUEST @" << butil::my_ip() << " ]\n< "
<< HttpMethod2Str((HttpMethod)parser->method) << ' '
<< http_message->_url << " HTTP/" << parser->http_major
<< '.' << parser->http_minor;
} else {
// NOTE: http_message->header().status_code() may not be set yet.
*vs << "[HTTP RESPONSE @" << butil::my_ip() << "]\n< HTTP/"
*vs << "[ HTTP RESPONSE @" << butil::my_ip() << " ]\n< HTTP/"
<< parser->http_major
<< '.' << parser->http_minor << ' ' << parser->status_code
<< ' ' << HttpReasonPhrase(parser->status_code);
......@@ -221,7 +221,7 @@ int HttpMessage::OnBody(const char *at, const size_t length) {
// description which is very helpful for debugging. Otherwise
// the body is probably streaming data which is too long to print.
header().status_code() == HTTP_STATUS_OK) {
std::cerr << _vmsgbuilder->buf() << std::endl;
LOG(INFO) << '\n' << _vmsgbuilder->buf();
delete _vmsgbuilder;
_vmsgbuilder = NULL;
} else {
......@@ -286,7 +286,7 @@ int HttpMessage::OnMessageComplete() {
*_vmsgbuilder << "\n<skipped " << _vbodylen
- (size_t)FLAGS_http_verbose_max_body_length << " bytes>";
}
std::cerr << _vmsgbuilder->buf() << std::endl;
LOG(INFO) << '\n' << _vmsgbuilder->buf();
delete _vmsgbuilder;
_vmsgbuilder = NULL;
}
......
......@@ -45,7 +45,7 @@ class HttpMessage {
public:
// If read_body_progressively is true, the body will be read progressively
// by using SetBodyReader().
explicit HttpMessage(bool read_body_progressively = false);
HttpMessage(bool read_body_progressively = false);
~HttpMessage();
const butil::IOBuf &body() const { return _body; }
......
......@@ -571,7 +571,8 @@ H2ParseResult H2Context::OnHeaders(
return MakeH2Error(H2_PROTOCOL_ERROR);
}
_last_server_stream_id = frame_head.stream_id;
sctx = new H2StreamContext(this, frame_head.stream_id);
sctx = new H2StreamContext(_socket->is_read_progressive());
sctx->Init(this, frame_head.stream_id);
const int rc = TryToInsertStream(frame_head.stream_id, sctx);
if (rc < 0) {
delete sctx;
......@@ -587,7 +588,8 @@ H2ParseResult H2Context::OnHeaders(
if (is_client_side()) {
RPC_VLOG << "Fail to find stream_id=" << frame_head.stream_id;
// Ignore the message without closing the socket.
H2StreamContext tmp_sctx(this, frame_head.stream_id);
H2StreamContext tmp_sctx(false);
tmp_sctx.Init(this, frame_head.stream_id);
tmp_sctx.OnHeaders(it, frame_head, frag_size, pad_length);
return MakeH2Message(NULL);
} else {
......@@ -643,7 +645,8 @@ H2ParseResult H2Context::OnContinuation(
if (is_client_side()) {
RPC_VLOG << "Fail to find stream_id=" << frame_head.stream_id;
// Ignore the message without closing the socket.
H2StreamContext tmp_sctx(this, frame_head.stream_id);
H2StreamContext tmp_sctx(false);
tmp_sctx.Init(this, frame_head.stream_id);
tmp_sctx.OnContinuation(it, frame_head);
return MakeH2Message(NULL);
} else {
......@@ -695,7 +698,8 @@ H2ParseResult H2Context::OnData(
if (is_client_side()) {
RPC_VLOG << "Fail to find stream_id=" << frame_head.stream_id;
// Ignore the message without closing the socket.
H2StreamContext tmp_sctx(this, frame_head.stream_id);
H2StreamContext tmp_sctx(false);
tmp_sctx.Init(this, frame_head.stream_id);
tmp_sctx.OnData(it, frame_head, frag_size, pad_length);
DeferWindowUpdate(tmp_sctx.ReleaseDeferredWindowUpdate());
return MakeH2Message(NULL);
......@@ -1117,8 +1121,9 @@ void H2Context::ClearAbandonedStreamsImpl() {
}
}
H2StreamContext::H2StreamContext()
: _conn_ctx(NULL)
H2StreamContext::H2StreamContext(bool read_body_progressively)
: HttpContext(read_body_progressively)
, _conn_ctx(NULL)
#if defined(BRPC_H2_STREAM_STATE)
, _state(H2_STREAM_IDLE)
#endif
......@@ -1140,22 +1145,6 @@ void H2StreamContext::Init(H2Context* conn_ctx, int stream_id) {
butil::memory_order_relaxed);
}
H2StreamContext::H2StreamContext(H2Context* conn_ctx, int stream_id)
: _conn_ctx(conn_ctx)
#if defined(BRPC_H2_STREAM_STATE)
, _state(H2_STREAM_IDLE)
#endif
, _stream_id(stream_id)
, _stream_ended(false)
, _remote_window_left(conn_ctx->remote_settings().stream_window_size)
, _deferred_window_update(0)
, _correlation_id(INVALID_BTHREAD_ID.value) {
header().set_version(2, 0);
#ifndef NDEBUG
get_http2_bvars()->h2_stream_context_count << 1;
#endif
}
H2StreamContext::~H2StreamContext() {
#ifndef NDEBUG
get_http2_bvars()->h2_stream_context_count << -1;
......@@ -1428,7 +1417,7 @@ H2UnsentRequest* H2UnsentRequest::New(Controller* c) {
val->append("Basic ");
val->append(encoded_user_info);
}
msg->_sctx.reset(new H2StreamContext);
msg->_sctx.reset(new H2StreamContext(c->is_response_read_progressively()));
return msg;
}
......@@ -1769,7 +1758,7 @@ void PackH2Request(butil::IOBuf*,
*user_message = h2_req;
if (FLAGS_http_verbose) {
std::cerr << *h2_req << std::endl;
LOG(INFO) << '\n' << *h2_req;
}
}
......
......@@ -135,7 +135,7 @@ friend void PackH2Request(butil::IOBuf*, SocketMessage**,
Controller*, const butil::IOBuf&, const Authenticator*);
public:
static H2UnsentRequest* New(Controller* c);
void Print(std::ostream&) const;
void Print(std::ostream& os) const;
int AddRefManually()
{ return _nref.fetch_add(1, butil::memory_order_relaxed); }
......@@ -196,7 +196,7 @@ class H2UnsentResponse : public SocketMessage {
public:
static H2UnsentResponse* New(Controller* c, int stream_id, bool is_grpc);
void Destroy();
void Print(std::ostream&) const;
void Print(std::ostream& os) const;
// @SocketMessage
butil::Status AppendAndDestroySelf(butil::IOBuf* out, Socket*) override;
size_t EstimatedByteSize() override;
......@@ -227,11 +227,10 @@ private:
// Used in http_rpc_protocol.cpp
class H2StreamContext : public HttpContext {
public:
H2StreamContext();
H2StreamContext(bool read_body_progressively);
~H2StreamContext();
void Init(H2Context* conn_ctx, int stream_id);
H2StreamContext(H2Context* conn_ctx, int stream_id);
// Decode headers in HPACK from *it and set into this->header(). The input
// does not need to complete.
// Returns 0 on success, -1 otherwise.
......
......@@ -198,9 +198,9 @@ static void PrintMessage(const butil::IOBuf& inbuf,
butil::IOBuf buf2;
char str[48];
if (request_or_response) {
snprintf(str, sizeof(str), "[HTTP REQUEST @%s]", butil::my_ip_cstr());
snprintf(str, sizeof(str), "[ HTTP REQUEST @%s ]", butil::my_ip_cstr());
} else {
snprintf(str, sizeof(str), "[HTTP RESPONSE @%s]", butil::my_ip_cstr());
snprintf(str, sizeof(str), "[ HTTP RESPONSE @%s ]", butil::my_ip_cstr());
}
buf2.append(str);
size_t last_size;
......@@ -212,11 +212,10 @@ static void PrintMessage(const butil::IOBuf& inbuf,
buf2.pop_back(2); // remove "> "
}
if (!has_content) {
buf2.append(buf1);
LOG(INFO) << '\n' << buf2 << buf1;
} else {
buf2.append(butil::ToPrintableString(buf1, FLAGS_http_verbose_max_body_length));
LOG(INFO) << '\n' << buf2 << butil::ToPrintableString(buf1, FLAGS_http_verbose_max_body_length);
}
std::cerr << buf2 << std::endl;
}
static void AddGrpcPrefix(butil::IOBuf* body, bool compressed) {
......@@ -863,7 +862,7 @@ HttpResponseSender::~HttpResponseSender() {
rc = -1;
} else {
if (FLAGS_http_verbose) {
std::cerr << *h2_response << std::endl;
LOG(INFO) << '\n' << *h2_response;
}
if (span) {
span->set_response_size(h2_response->EstimatedByteSize());
......@@ -1020,7 +1019,7 @@ FindMethodPropertyByURI(const std::string& uri_path, const Server* server,
return NULL;
}
ParseResult ParseHttpMessage(butil::IOBuf *source, Socket *socket,
ParseResult ParseHttpMessage(butil::IOBuf *source, Socket *socket,
bool read_eof, const void* /*arg*/) {
HttpContext* http_imsg =
static_cast<HttpContext*>(socket->parsing_context());
......@@ -1035,8 +1034,7 @@ ParseResult ParseHttpMessage(butil::IOBuf *source, Socket *socket,
// source is likely to be empty.
return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
}
http_imsg = new (std::nothrow) HttpContext(
socket->is_read_progressive());
http_imsg = new (std::nothrow) HttpContext(socket->is_read_progressive());
if (http_imsg == NULL) {
LOG(FATAL) << "Fail to new HttpContext";
return MakeParseError(PARSE_ERROR_NO_RESOURCE);
......
......@@ -80,7 +80,7 @@ class HttpContext : public ReadableProgressiveAttachment
, public InputMessageBase
, public HttpMessage {
public:
HttpContext(bool read_body_progressively = false)
HttpContext(bool read_body_progressively)
: InputMessageBase()
, HttpMessage(read_body_progressively)
, _is_stage2(false) {
......
......@@ -37,7 +37,7 @@ DECLARE_bool(enable_rpcz);
namespace policy {
DEFINE_bool(redis_verbose, false,
"[DEBUG] Print EVERY redis request/response to stderr");
"[DEBUG] Print EVERY redis request/response");
struct InputResponse : public InputMessageBase {
bthread_id_t id_wait;
......@@ -142,9 +142,8 @@ void ProcessRedisResponse(InputMessageBase* msg_base) {
}
((RedisResponse*)cntl->response())->Swap(&msg->response);
if (FLAGS_redis_verbose) {
std::cerr << "[REDIS RESPONSE] "
<< *((RedisResponse*)cntl->response())
<< std::endl;
LOG(INFO) << "\n[REDIS RESPONSE] "
<< *((RedisResponse*)cntl->response());
}
}
} // silently ignore the response.
......@@ -171,7 +170,7 @@ void SerializeRedisRequest(butil::IOBuf* buf,
}
ControllerPrivateAccessor(cntl).set_pipelined_count(rr->command_size());
if (FLAGS_redis_verbose) {
std::cerr << "[REDIS REQUEST] " << *rr << std::endl;
LOG(INFO) << "\n[REDIS REQUEST] " << *rr;
}
}
......
......@@ -87,8 +87,6 @@ DEFINE_int32(connect_timeout_as_unreachable, 3,
"times *continuously*, the error is changed to ENETUNREACH which "
"fails the main socket as well when this socket is pooled.");
DECLARE_bool(http_verbose);
static bool validate_connect_timeout_as_unreachable(const char*, int32_t v) {
return v >= 2 && v < 1000/*large enough*/;
}
......@@ -1822,9 +1820,6 @@ int Socket::SSLHandshake(int fd, bool server_mode) {
int rc = SSL_do_handshake(_ssl_session);
if (rc == 1) {
_ssl_state = SSL_CONNECTED;
if (FLAGS_http_verbose) {
std::cerr << _ssl_session << std::endl;
}
AddBIOBuffer(_ssl_session, fd, FLAGS_ssl_bio_buffer_size);
return 0;
}
......
......@@ -401,6 +401,7 @@ public:
void CheckEOF();
SSLState ssl_state() const { return _ssl_state; }
bool is_ssl() const { return ssl_state() == SSL_CONNECTED; }
X509* GetPeerCertificate() const;
// Print debugging inforamtion of `id' into the ostream.
......
......@@ -136,7 +136,7 @@ protected:
}
brpc::policy::HttpContext* MakePostRequestMessage(const std::string& path) {
brpc::policy::HttpContext* msg = new brpc::policy::HttpContext();
brpc::policy::HttpContext* msg = new brpc::policy::HttpContext(false);
msg->header().uri().set_path(path);
msg->header().set_content_type("application/json");
msg->header().set_method(brpc::HTTP_METHOD_POST);
......@@ -149,7 +149,7 @@ protected:
}
brpc::policy::HttpContext* MakeGetRequestMessage(const std::string& path) {
brpc::policy::HttpContext* msg = new brpc::policy::HttpContext();
brpc::policy::HttpContext* msg = new brpc::policy::HttpContext(false);
msg->header().uri().set_path(path);
msg->header().set_method(brpc::HTTP_METHOD_GET);
return msg;
......@@ -157,7 +157,7 @@ protected:
brpc::policy::HttpContext* MakeResponseMessage(int code) {
brpc::policy::HttpContext* msg = new brpc::policy::HttpContext();
brpc::policy::HttpContext* msg = new brpc::policy::HttpContext(false);
msg->header().set_status_code(code);
msg->header().set_content_type("application/json");
......
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