Commit 74e853aa authored by wangxuefeng's avatar wangxuefeng

Sync from brpc office repo.

parent 4cd5a7fe
...@@ -63,15 +63,25 @@ ...@@ -63,15 +63,25 @@
#include "brpc/socket_map.h" // SocketMapList #include "brpc/socket_map.h" // SocketMapList
#include "brpc/server.h" #include "brpc/server.h"
#include "brpc/trackme.h" // TrackMe #include "brpc/trackme.h" // TrackMe
#include <malloc.h> // malloc_trim
#include "brpc/details/usercode_backup_pool.h" #include "brpc/details/usercode_backup_pool.h"
#include <malloc.h> // malloc_trim
#include "butil/fd_guard.h" #include "butil/fd_guard.h"
#include "butil/files/file_watcher.h" #include "butil/files/file_watcher.h"
extern "C" {
// defined in gperftools/malloc_extension_c.h
void BAIDU_WEAK MallocExtension_ReleaseFreeMemory(void);
}
namespace brpc { namespace brpc {
DECLARE_bool(usercode_in_pthread); DECLARE_bool(usercode_in_pthread);
DEFINE_int32(free_memory_to_system_interval, 0,
"Try to return free memory to system every so many seconds, "
"values <= 0 disables this feature");
BRPC_VALIDATE_GFLAG(free_memory_to_system_interval, PassValidate);
namespace policy { namespace policy {
// Defined in http_rpc_protocol.cpp // Defined in http_rpc_protocol.cpp
void InitCommonStrings(); void InitCommonStrings();
...@@ -178,7 +188,7 @@ static void* GlobalUpdate(void*) { ...@@ -178,7 +188,7 @@ static void* GlobalUpdate(void*) {
const int WARN_NOSLEEP_THRESHOLD = 2; const int WARN_NOSLEEP_THRESHOLD = 2;
int64_t last_time_us = start_time_us; int64_t last_time_us = start_time_us;
int consecutive_nosleep = 0; int consecutive_nosleep = 0;
//int64_t last_malloc_trim_time = start_time_us; int64_t last_return_free_memory_time = start_time_us;
while (1) { while (1) {
const int64_t sleep_us = 1000000L + last_time_us - butil::gettimeofday_us(); const int64_t sleep_us = 1000000L + last_time_us - butil::gettimeofday_us();
if (sleep_us > 0) { if (sleep_us > 0) {
...@@ -215,11 +225,24 @@ static void* GlobalUpdate(void*) { ...@@ -215,11 +225,24 @@ static void* GlobalUpdate(void*) {
} }
} }
// TODO: Add branch for tcmalloc. const int return_mem_interval =
// if (last_time_us > last_malloc_trim_time + 10*1000000L) { FLAGS_free_memory_to_system_interval/*reloadable*/;
// last_malloc_trim_time = last_time_us; if (return_mem_interval > 0 &&
// malloc_trim(10*1024*1024/*leave 10M pad*/); last_time_us >= last_return_free_memory_time +
// } return_mem_interval * 1000000L) {
last_return_free_memory_time = last_time_us;
// TODO: Calling MallocExtension::instance()->ReleaseFreeMemory may
// crash the program in later calls to malloc, verified on tcmalloc
// 1.7 and 2.5, which means making the static member function weak
// in details/tcmalloc_extension.cpp is probably not correct, however
// it does work for heap profilers.
if (MallocExtension_ReleaseFreeMemory != NULL) {
MallocExtension_ReleaseFreeMemory();
} else {
// GNU specific.
malloc_trim(10 * 1024 * 1024/*leave 10M pad*/);
}
}
} }
return NULL; return NULL;
} }
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Authors: Ge,Jun (gejun@baidu.com) // Authors: wangxuefeng (wangxuefeng@didichuxing.com)
#include <google/protobuf/descriptor.h> // MethodDescriptor #include <google/protobuf/descriptor.h> // MethodDescriptor
#include <google/protobuf/message.h> // Message #include <google/protobuf/message.h> // Message
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Authors: Ge,Jun (gejun@baidu.com) // Authors: wangxuefeng (wangxuefeng@didichuxing.com)
#ifndef BRPC_POLICY_THRIFT_PROTOCOL_H #ifndef BRPC_POLICY_THRIFT_PROTOCOL_H
#define BRPC_POLICY_THRIFT_PROTOCOL_H #define BRPC_POLICY_THRIFT_PROTOCOL_H
......
...@@ -167,10 +167,6 @@ const ConnectionType CONNECTION_TYPE_ALL = ...@@ -167,10 +167,6 @@ const ConnectionType CONNECTION_TYPE_ALL =
(int)CONNECTION_TYPE_POOLED | (int)CONNECTION_TYPE_POOLED |
(int)CONNECTION_TYPE_SHORT); (int)CONNECTION_TYPE_SHORT);
// DEPRECATED: old names.
const ProtocolType PROTOCOL_BAIDU_RPC = PROTOCOL_BAIDU_STD;
const ProtocolType PROTOCOL_MEMCACHE_BINARY = PROTOCOL_MEMCACHE;
// [thread-safe] // [thread-safe]
// Register `protocol' using key=`type'. // Register `protocol' using key=`type'.
// Returns 0 on success, -1 otherwise // Returns 0 on success, -1 otherwise
......
...@@ -210,30 +210,15 @@ static void PrintSupportedCompressions(std::ostream& os, void*) { ...@@ -210,30 +210,15 @@ static void PrintSupportedCompressions(std::ostream& os, void*) {
} }
} }
static bool check_TCMALLOC_SAMPLE_PARAMETER() {
char* str = getenv("TCMALLOC_SAMPLE_PARAMETER");
if (str == NULL) {
return false;
}
char* endptr;
int val = strtol(str, &endptr, 10);
return (*endptr == '\0' && val > 0);
}
static bool has_TCMALLOC_SAMPLE_PARAMETER() {
static bool val = check_TCMALLOC_SAMPLE_PARAMETER();
return val;
}
static void PrintEnabledProfilers(std::ostream& os, void*) { static void PrintEnabledProfilers(std::ostream& os, void*) {
if (cpu_profiler_enabled) { if (cpu_profiler_enabled) {
os << "cpu "; os << "cpu ";
} }
if (IsHeapProfilerEnabled) { if (IsHeapProfilerEnabled()) {
if (has_TCMALLOC_SAMPLE_PARAMETER()) { if (has_TCMALLOC_SAMPLE_PARAMETER()) {
os << "heap "; os << "heap ";
} else { } else {
os << "heap(lack of TCMALLOC_SAMPLE_PARAMETER) "; os << "heap(no TCMALLOC_SAMPLE_PARAMETER in env) ";
} }
} }
os << "contention"; os << "contention";
...@@ -670,6 +655,15 @@ struct RevertServerStatus { ...@@ -670,6 +655,15 @@ struct RevertServerStatus {
} }
}; };
static int get_port_from_fd(int fd) {
struct sockaddr_in addr;
socklen_t size = sizeof(addr);
if (getsockname(fd, (struct sockaddr*)&addr, &size) < 0) {
return -1;
}
return ntohs(addr.sin_port);
}
int Server::StartInternal(const butil::ip_t& ip, int Server::StartInternal(const butil::ip_t& ip,
const PortRange& port_range, const PortRange& port_range,
const ServerOptions *opt) { const ServerOptions *opt) {
...@@ -901,6 +895,15 @@ int Server::StartInternal(const butil::ip_t& ip, ...@@ -901,6 +895,15 @@ int Server::StartInternal(const butil::ip_t& ip,
} }
return -1; return -1;
} }
if (_listen_addr.port == 0) {
// port=0 makes kernel dynamically select a port from
// https://en.wikipedia.org/wiki/Ephemeral_port
_listen_addr.port = get_port_from_fd(sockfd);
if (_listen_addr.port <= 0) {
LOG(ERROR) << "Fail to get port from fd=" << sockfd;
return -1;
}
}
if (_am == NULL) { if (_am == NULL) {
_am = BuildAcceptor(); _am = BuildAcceptor();
if (NULL == _am) { if (NULL == _am) {
...@@ -930,6 +933,12 @@ int Server::StartInternal(const butil::ip_t& ip, ...@@ -930,6 +933,12 @@ int Server::StartInternal(const butil::ip_t& ip,
<< " is same with port=" << _listen_addr.port << " to Start()"; << " is same with port=" << _listen_addr.port << " to Start()";
return -1; return -1;
} }
if (_options.internal_port == 0) {
LOG(ERROR) << "ServerOptions.internal_port cannot be 0, which"
" allocates a dynamic and probabaly unfiltered port,"
" against the purpose of \"being internal\".";
return -1;
}
butil::EndPoint internal_point = _listen_addr; butil::EndPoint internal_point = _listen_addr;
internal_point.port = _options.internal_port; internal_point.port = _options.internal_port;
butil::fd_guard sockfd(tcp_listen(internal_point, FLAGS_reuse_addr)); butil::fd_guard sockfd(tcp_listen(internal_point, FLAGS_reuse_addr));
......
...@@ -247,7 +247,7 @@ struct ServerOptions { ...@@ -247,7 +247,7 @@ struct ServerOptions {
// Provide builtin services at this port rather than the port to Start(). // Provide builtin services at this port rather than the port to Start().
// When your server needs to be accessed from public (including traffic // When your server needs to be accessed from public (including traffic
// redirected by nginx or other http front-end servers), set this port // redirected by nginx or other http front-end servers), set this port
// to a port number that's ONLY accessible from Baidu's internal network // to a port number that's ONLY accessible from internal network
// so that you can check out the builtin services from this port while // so that you can check out the builtin services from this port while
// hiding them from public. Setting this option also enables security // hiding them from public. Setting this option also enables security
// protection code which we may add constantly. // protection code which we may add constantly.
...@@ -415,28 +415,25 @@ public: ...@@ -415,28 +415,25 @@ public:
Server(ProfilerLinker = ProfilerLinker()); Server(ProfilerLinker = ProfilerLinker());
~Server(); ~Server();
// Start this server. Use default options if `opt' is NULL. // A set of functions to start this server.
// This function can be called multiple times if the server is completely
// stopped by Stop() and Join().
// Returns 0 on success, -1 otherwise and errno is set appropriately. // Returns 0 on success, -1 otherwise and errno is set appropriately.
// Notes:
// * Default options are taken if `opt' is NULL.
// * A server can be started more than once if the server is completely
// stopped by Stop() and Join().
// * port can be 0, which makes kernel to choose a port dynamically.
// Start on a single address "0.0.0.0:8000". // Start on an address in form of "0.0.0.0:8000".
int Start(const char* ip_port_str, const ServerOptions* opt); int Start(const char* ip_port_str, const ServerOptions* opt);
int Start(const butil::EndPoint& ip_port, const ServerOptions* opt);
// Start on IP_ANY:port. // Start on IP_ANY:port.
int Start(int port, const ServerOptions* opt); int Start(int port, const ServerOptions* opt);
// Start on `ip_str' + any useable port in `range'
// Start on ip:port enclosed in butil::EndPoint which is defined in int Start(const char* ip_str, PortRange range, const ServerOptions *opt);
// src/butil/endpoint.h
int Start(const butil::EndPoint& ip_port, const ServerOptions* opt);
// Start on `ip_str' + any useable port in `port_range'
int Start(const char* ip_str, PortRange port_range,
const ServerOptions *opt);
// NOTE: Stop() is paired with Join() to stop a server with minimum lost // NOTE: Stop() is paired with Join() to stop a server without losing
// of requests. The point of separating them is that you can Stop() // requests. The point of separating them is that you can Stop() multiple
// multiple servers before Join()-ing them, the total time to Join is // servers before Join() them, in which case the total time to Join is
// time of the slowest Join(). Otherwise you have to Join() them one by // time of the slowest Join(). Otherwise you have to Join() them one by
// one, in which case the total time is sum of all Join(). // one, in which case the total time is sum of all Join().
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Authors: Ge,Jun (gejun@baidu.com) // Authors: wangxuefeng (wangxuefeng@didichuxing.com)
#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION #define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION
#include "brpc/thrift_binary_message.h" #include "brpc/thrift_binary_message.h"
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Authors: Ge,Jun (gejun@baidu.com) // Authors: wangxuefeng (wangxuefeng@didichuxing.com)
#ifndef BRPC_THRIFT_BINARY_MESSAGE_H #ifndef BRPC_THRIFT_BINARY_MESSAGE_H
#define BRPC_THRIFT_BINARY_MESSAGE_H #define BRPC_THRIFT_BINARY_MESSAGE_H
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Authors: Ge,Jun (gejun@baidu.com) // Authors: wangxuefeng (wangxuefeng@didichuxing.com)
#include "butil/class_name.h" #include "butil/class_name.h"
#include "brpc/thrift_service.h" #include "brpc/thrift_service.h"
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Authors: Ge,Jun (gejun@baidu.com) // Authors: wangxuefeng (wangxuefeng@didichuxing.com)
#ifndef BRPC_THRIFT_SERVICE_H #ifndef BRPC_THRIFT_SERVICE_H
#define BRPC_THRIFT_SERVICE_H #define BRPC_THRIFT_SERVICE_H
......
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