Compare commits

..

75 Commits

Author SHA1 Message Date
vazmin 4adf6b3227 gh actions: upgrade to 1.2.11-1 2025-11-23 10:48:22 +00:00
vazmin f4a799402e gh actions: upgrade to 1.2.11-1 2025-11-23 10:00:56 +00:00
vazmin 27510e9641 gh actions: upgrade to 1.2.11-1 2025-11-23 09:06:43 +00:00
YuQing 848077797b upgrade version to 1.2.11 2025-11-16 17:01:06 +08:00
YuQing d22f9da49c bugfixed: MUST call sf_hold_task in sf_nio_notify for rare case 2025-11-16 15:29:38 +08:00
YuQing 5495455fa7 do NOT call task->finish_callback 2025-11-15 11:14:14 +08:00
YuQing 4da0ff251c upgrade version to 1.2.10 2025-11-11 09:57:18 +08:00
YuQing 2444eac6ce declare use_send_zc field anyway 2025-11-11 09:52:42 +08:00
YuQing a52cc2d5d4 check sf_context->use_io_uring more 2025-11-05 09:58:37 +08:00
YuQing c4af33a497 declare use_io_uring correctly 2025-11-04 15:55:33 +08:00
YuQing fa32972052 move use_io_uring and use_send_zc to struct sf_context 2025-11-04 15:40:00 +08:00
YuQing 688211fbcd correct compile error 2025-11-03 15:22:45 +08:00
YuQing 1b2f521b99 uring cancel callback release task correctly 2025-11-03 14:56:29 +08:00
YuQing ddc528d69d restore function sf_client_sock_in_read_stage 2025-11-02 15:02:54 +08:00
YuQing 32d443b497 MUST call set_read_event anyway after socket send done 2025-10-27 12:28:24 +08:00
YuQing 53dd39500f upgrade version to 1.2.9 2025-10-26 12:27:49 +08:00
YuQing 772a9a6895 Merge remote-tracking branch 'origin/use_iouring' 2025-10-26 12:26:53 +08:00
YuQing 932751d392 send zc done notify callback for recycling buffer 2025-10-20 10:34:47 +08:00
YuQing 817ff547da set alloc_conn_once and max_entries gracefully 2025-10-12 12:28:11 +08:00
YuQing 926cd40114 ioevent_init: set max entries for io_uring gracefully 2025-10-12 10:24:35 +08:00
YuQing b688973cf9 change use_send_zc's default value to true 2025-10-09 14:01:32 +08:00
YuQing b16526e8f7 bug fixed: check_task process correctly 2025-10-06 20:55:29 +08:00
YuQing 68079fc468 IOEventCallback: change event type from short to int 2025-10-05 16:53:21 +08:00
YuQing 3dcc1c570d call sf_proto_init_task_magic when task->shrinked 2025-10-03 21:06:58 +08:00
YuQing cf0950ea62 sf_set_read_event just skipped when use_io_uring is true 2025-10-03 11:33:26 +08:00
YuQing 263171c4fe async_connect use io_uring 2025-09-30 11:26:11 +08:00
YuQing a2ab8a0c01 adapt Linux io_uring OK 2025-09-27 15:41:56 +08:00
YuQing 0f75b039f6 sf_logger_set_schedule_entry change to sf_logger_set_schedule_entries 2025-09-26 19:57:03 +08:00
YuQing ecee21f289 socket send and recv adapt to io_uring 2025-09-25 15:54:38 +08:00
YuQing bc5af8a58b struct sf_network_handler add field use_iouring 2025-09-24 15:59:27 +08:00
YuQing f0ee6ce73f struct sf_context remove field: remove_from_ready_list 2025-09-21 15:08:08 +08:00
vazmin aef9d803d1 gh actions: upgrade to 1.2.8-1 2025-08-16 16:32:03 +00:00
YuQing 6d0465acc4 upgrade version to 1.2.8 2025-08-14 09:44:39 +08:00
YuQing 2e5258045d change SF_BINLOG_FILE_PREFIX to SF_BINLOG_FILE_PREFIX_STR 2025-08-10 12:03:25 +08:00
YuQing fc689a02ba rename fc_combine_two_string to fc_combine_two_strings 2025-08-09 15:22:04 +08:00
YuQing 7b3f6d620a use fc_safe_strcpy and fc_combine_two_string 2025-08-08 21:52:08 +08:00
YuQing 03f64998ce performance opt.: replace snprintf 2025-08-07 19:53:38 +08:00
YuQing 0b4936bd8f performance opt.: replace sprintf again 2025-08-05 18:05:56 +08:00
YuQing ba87f8e1ea performance opt.: replace sprintf 2025-08-04 16:57:35 +08:00
YuQing da2ddc7441 sf_log_config for client correctly 2025-04-23 15:17:23 +08:00
vazmin b83189f021 gh actions: upgrade to 1.2.7-1 2025-04-06 16:56:40 +00:00
YuQing 4ad53f7ee3 correct libserverframe release version 2025-04-01 17:21:01 +08:00
YuQing 003cc74b96 upgrade version to 1.2.7 2025-04-01 16:49:38 +08:00
YuQing 3815c0b2ce export function sf_file_writer_direct_write 2025-03-15 10:10:01 +08:00
YuQing 7ee7e7b535 add function sf_file_writer_get_last_line 2025-03-10 17:01:03 +08:00
YuQing e095ce45c2 add function sf_file_writer_flush_buffer 2025-03-05 20:05:56 +08:00
YuQing f9d8c3d070 add function write_to_binlog_index_file 2025-02-18 15:49:00 +08:00
YuQing fc9c23fb95 call flush_writer_files before rotate file 2025-02-17 10:54:07 +08:00
YuQing 755296bb68 add function sf_proto_send_buf2 2025-02-12 21:59:04 +08:00
YuQing 14a783fd6f sf_binlog_writer_rotate_file can skip empty file 2025-02-10 18:34:04 +08:00
YuQing e9e33883bf Merge branch 'master' of gitee.com:fastdfs100/libserverframe 2025-02-09 09:30:42 +08:00
YuQing 318640572f sf_file_writer.[hc]: support write done callback 2025-02-09 09:26:34 +08:00
YuQing f6e2de5668 upgrade version to 1.2.6 2025-01-27 20:50:18 +08:00
YuQing b6e24d0548 sf_connection_manager.[hc]: get connection functions add parameter shared 2025-01-27 10:59:57 +08:00
YuQing 03974ff302 explicit cast for fast_mblock_alloc_object 2024-12-08 09:29:16 +08:00
YuQing a3e1956128 change macro sf_log_network_error_for_update_ex 2024-10-29 09:52:45 +08:00
YuQing 75e8aacfd9 sf_binlog_writer.[hc] add parameter write_interval_ms for high performance 2024-10-07 09:21:19 +08:00
vazmin be4bad4ee1 gh actions: upgrade to 1.2.5-1 2024-09-29 15:24:39 +00:00
YuQing 3993b79a81 sf_connection_manager_init: set htable_capacity gracefully 2024-09-22 12:21:06 +08:00
YuQing 42c6e2c6b7 struct sf_context add field is_client for RDMA callback alloc_pd 2024-09-20 14:16:59 +08:00
YuQing 6a60a49c32 upgrade version to 1.2.5 2024-09-17 11:24:01 +08:00
YuQing 14d0a1c014 task init callback support extra argument 2024-09-15 12:06:25 +08:00
vazmin a01ccf66dc gh actions: upgrade to 1.2.4-1 2024-06-15 14:45:44 +00:00
YuQing 63d43fc9cc upgrade version to 1.2.4 2024-06-11 09:39:08 +08:00
YuQing e83be7356d change default values of connect timeout and network timeout 2024-04-28 16:20:59 +08:00
vazmin 3dfdb8ace6 gh actions: upgrade to 1.2.3-1 2024-03-17 15:11:04 +00:00
YuQing 35d9be16ee upgrade version to V1.2.3 2024-03-11 11:18:42 +08:00
YuQing 231e2610e5 log square quoted IPv6 address 2024-03-05 18:07:34 +08:00
YuQing 1c796ab819 sf_file_writer_init add parameter: max_record_size 2024-02-29 11:58:05 +08:00
YuQing 7f6ad6dcba sf_load_context_from_config_ex support max_pkg_size_min_value 2024-02-26 16:17:41 +08:00
YuQing 78d65ba2c6 net buffer config for each sf_context instance 2024-02-20 09:53:52 +08:00
YuQing d5a9f40a66 correct macros for struct SFBinlogBuffer 2024-02-15 15:13:40 +08:00
YuQing 9d3a92d7da fixed macro SF_BINLOG_BUFFER_LENGTH 2024-02-05 17:05:39 +08:00
YuQing cab9ce4c4f set flags of protocol header 2024-02-04 09:22:56 +08:00
vazmin 8ea4e28f73 gh actions: upgrade to 1.2.2-1 2024-01-31 12:00:10 +00:00
33 changed files with 1687 additions and 635 deletions

54
debian/changelog vendored
View File

@ -1,3 +1,57 @@
libserverframe (1.2.11-1) unstable; urgency=medium
* upgrade to 1.2.11-1
-- YuQing <384681@qq.com> Sun, 23 Nov 2025 10:48:22 +0000
libserverframe (1.2.11-1) unstable; urgency=medium
* upgrade to 1.2.11-1
-- YuQing <384681@qq.com> Sun, 23 Nov 2025 10:00:56 +0000
libserverframe (1.2.11-1) unstable; urgency=medium
* upgrade to 1.2.11-1
-- YuQing <384681@qq.com> Sun, 23 Nov 2025 09:06:43 +0000
libserverframe (1.2.8-1) unstable; urgency=medium
* upgrade to 1.2.8-1
-- YuQing <384681@qq.com> Sat, 16 Aug 2025 16:32:03 +0000
libserverframe (1.2.7-1) unstable; urgency=medium
* upgrade to 1.2.7-1
-- YuQing <384681@qq.com> Sun, 06 Apr 2025 16:56:40 +0000
libserverframe (1.2.5-1) unstable; urgency=medium
* upgrade to 1.2.5-1
-- YuQing <384681@qq.com> Sun, 29 Sep 2024 15:24:39 +0000
libserverframe (1.2.4-1) unstable; urgency=medium
* upgrade to 1.2.4-1
-- YuQing <384681@qq.com> Sat, 15 Jun 2024 14:45:44 +0000
libserverframe (1.2.3-1) unstable; urgency=medium
* upgrade to 1.2.3-1
-- YuQing <384681@qq.com> Sun, 17 Mar 2024 15:11:04 +0000
libserverframe (1.2.2-1) unstable; urgency=medium
* upgrade to 1.2.2-1
-- YuQing <384681@qq.com> Wed, 31 Jan 2024 12:00:10 +0000
libserverframe (1.2.1-1) unstable; urgency=medium
* upgrade to 1.2.1-1

2
debian/substvars vendored
View File

@ -1 +1 @@
libfastcommon:Version=1.0.71
libfastcommon:Version=1.0.83

View File

@ -2,7 +2,7 @@
%define CommitVersion %(echo $COMMIT_VERSION)
Name: libserverframe
Version: 1.2.2
Version: 1.2.11
Release: 1%{?dist}
Summary: network framework library
License: AGPL v3.0
@ -12,9 +12,9 @@ Source: http://github.com/happyfish100/libserverframe/%{name}-%{version}.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
BuildRequires: libfastcommon-devel >= 1.0.72
BuildRequires: libfastcommon-devel >= 1.0.83
Requires: %__cp %__mv %__chmod %__grep %__mkdir %__install %__id
Requires: libfastcommon >= 1.0.72
Requires: libfastcommon >= 1.0.83
%description
common framework library

View File

@ -9,10 +9,13 @@ DEBUG_FLAG=0
if [ -f /usr/include/fastcommon/_os_define.h ]; then
OS_BITS=$(grep -F OS_BITS /usr/include/fastcommon/_os_define.h | awk '{print $NF;}')
USE_URING=$(grep -F IOEVENT_USE_URING /usr/include/fastcommon/_os_define.h | awk '{print $NF;}')
elif [ -f /usr/local/include/fastcommon/_os_define.h ]; then
OS_BITS=$(grep -F OS_BITS /usr/local/include/fastcommon/_os_define.h | awk '{print $NF;}')
USE_URING=$(grep -F IOEVENT_USE_URING /usr/local/include/fastcommon/_os_define.h | awk '{print $NF;}')
else
OS_BITS=64
USE_URING=''
fi
uname=$(uname)
@ -49,6 +52,9 @@ LIBS=''
uname=$(uname)
if [ "$uname" = "Linux" ]; then
CFLAGS="$CFLAGS"
if [ -n "$USE_URING" ]; then
LIBS="$LIBS -luring"
fi
elif [ "$uname" = "FreeBSD" ] || [ "$uname" = "Darwin" ]; then
CFLAGS="$CFLAGS"
if [ "$uname" = "Darwin" ]; then
@ -132,6 +138,7 @@ fi
cd ..
cp Makefile.in Makefile
sed_replace "s#\\\$(CC)#gcc#g" Makefile
sed_replace "s#\\\$(CFLAGS)#$CFLAGS#g" Makefile
sed_replace "s#\\\$(LIBS)#$LIBS#g" Makefile
sed_replace "s#\\\$(TARGET_PREFIX)#$TARGET_PREFIX#g" Makefile

View File

@ -194,7 +194,7 @@ static struct fast_task_info *alloc_channel_task(IdempotencyClientChannel
return NULL;
}
snprintf(task->server_ip, sizeof(task->server_ip), "%s", server_ip);
fc_safe_strcpy(task->server_ip, server_ip);
task->port = port;
task->arg = channel;
task->thread_data = g_sf_context.thread_data +
@ -203,7 +203,8 @@ static struct fast_task_info *alloc_channel_task(IdempotencyClientChannel
channel->last_connect_time = g_current_time;
if ((*err_no=sf_nio_notify(task, SF_NIO_STAGE_CONNECT)) != 0) {
channel->in_ioevent = 0; //rollback
sf_release_task(task);
__sync_sub_and_fetch(&task->reffer_count, 1);
free_queue_push(task);
return NULL;
}
return task;
@ -213,6 +214,15 @@ int idempotency_client_channel_check_reconnect(
IdempotencyClientChannel *channel)
{
int result;
char formatted_ip[FORMATTED_IP_SIZE];
#if IOEVENT_USE_URING
struct fast_task_info *task;
task = channel->task;
if (SF_CTX->use_io_uring && FC_ATOMIC_GET(task->reffer_count) > 1) {
return 0;
}
#endif
if (!__sync_bool_compare_and_swap(&channel->in_ioevent, 0, 1)) {
return 0;
@ -223,11 +233,16 @@ int idempotency_client_channel_check_reconnect(
channel->last_connect_time = g_current_time;
}
logDebug("file: "__FILE__", line: %d, "
"trigger connect to server %s:%u",
__LINE__, channel->task->server_ip,
channel->task->port);
if (FC_LOG_BY_LEVEL(LOG_DEBUG)) {
format_ip_address(channel->task->server_ip, formatted_ip);
logDebug("file: "__FILE__", line: %d, "
"trigger connect to server %s:%u", __LINE__,
formatted_ip, channel->task->port);
}
if (channel->task->event.fd >= 0) {
channel->task->handler->close_connection(channel->task);
}
__sync_bool_compare_and_swap(&channel->task->canceled, 1, 0);
if ((result=sf_nio_notify(channel->task, SF_NIO_STAGE_CONNECT)) == 0) {
channel->last_connect_time = g_current_time;
@ -252,7 +267,10 @@ struct idempotency_client_channel *idempotency_client_channel_get(
IdempotencyClientChannel *current;
IdempotencyClientChannel *channel;
key_len = snprintf(key, sizeof(key), "%s-%u", server_ip, server_port);
key_len = strlen(server_ip);
memcpy(key, server_ip, key_len);
*(key + key_len++) = '-';
key_len += fc_itoa(server_port, key + key_len);
hash_code = fc_simple_hash(key, key_len);
bucket = channel_context.htable.buckets +
hash_code % channel_context.htable.capacity;
@ -336,8 +354,8 @@ int idempotency_client_channel_push(struct idempotency_client_channel *channel,
receipt->req_id = req_id;
fc_queue_push_ex(&channel->queue, receipt, &notify);
if (notify) {
if (__sync_add_and_fetch(&channel->in_ioevent, 0)) {
if (__sync_add_and_fetch(&channel->established, 0)) {
if (FC_ATOMIC_GET(channel->in_ioevent)) {
if (FC_ATOMIC_GET(channel->established)) {
sf_nio_notify(channel->task, SF_NIO_STAGE_CONTINUE);
}
} else {

View File

@ -76,20 +76,22 @@ static inline void idempotency_client_channel_set_id_key(
static inline int idempotency_client_channel_check_wait_ex(
struct idempotency_client_channel *channel, const int timeout)
{
if (__sync_add_and_fetch(&channel->established, 0)) {
if (FC_ATOMIC_GET(channel->established)) {
return 0;
}
idempotency_client_channel_check_reconnect(channel);
lcp_timedwait_sec(&channel->lcp, timeout);
if (__sync_add_and_fetch(&channel->established, 0)) {
if (FC_ATOMIC_GET(channel->established)) {
return 0;
} else {
/*
char formatted_ip[FORMATTED_IP_SIZE];
format_ip_address(channel->task->server_ip, formatted_ip);
logInfo("file: "__FILE__", line: %d, "
"channel_check fail, server %s:%u, in_ioevent: %d, "
"canceled: %d, req count: %"PRId64, __LINE__,
channel->task->server_ip, channel->task->port,
formatted_ip, channel->task->port,
__sync_add_and_fetch(&channel->in_ioevent, 0),
__sync_add_and_fetch(&channel->task->canceled, 0),
channel->task->req_count);

View File

@ -67,7 +67,6 @@ typedef struct idempotency_receipt_global_vars {
struct {
int task_padding_size;
sf_init_connection_callback init_connection;
struct ibv_pd *pd;
} rdma;
IdempotencyReceiptThreadContext *thread_contexts;
} IdempotencyReceiptGlobalVars;

View File

@ -46,14 +46,15 @@ static IdempotencyReceiptGlobalVars receipt_global_vars;
#define RECEIPT_THREAD_CONTEXTS receipt_global_vars.thread_contexts
#define TASK_PADDING_SIZE receipt_global_vars.rdma.task_padding_size
#define RDMA_INIT_CONNECTION receipt_global_vars.rdma.init_connection
#define RDMA_PD receipt_global_vars.rdma.pd
static int receipt_init_task(struct fast_task_info *task)
static int receipt_init_task(struct fast_task_info *task, void *arg)
{
task->connect_timeout = SF_G_CONNECT_TIMEOUT; //for client side
task->network_timeout = SF_G_NETWORK_TIMEOUT;
#if IOEVENT_USE_URING
FC_URING_IS_CLIENT(task) = true;
#endif
if (RDMA_INIT_CONNECTION != NULL) {
return RDMA_INIT_CONNECTION(task, RDMA_PD);
return RDMA_INIT_CONNECTION(task, arg);
} else {
return 0;
}
@ -62,11 +63,13 @@ static int receipt_init_task(struct fast_task_info *task)
static int receipt_recv_timeout_callback(struct fast_task_info *task)
{
IdempotencyClientChannel *channel;
char formatted_ip[FORMATTED_IP_SIZE];
format_ip_address(task->server_ip, formatted_ip);
if (SF_NIO_TASK_STAGE_FETCH(task) == SF_NIO_STAGE_CONNECT) {
logError("file: "__FILE__", line: %d, "
"connect to server %s:%u timeout",
__LINE__, task->server_ip, task->port);
__LINE__, formatted_ip, task->port);
return ETIMEDOUT;
}
@ -74,12 +77,12 @@ static int receipt_recv_timeout_callback(struct fast_task_info *task)
if (channel->waiting_resp_qinfo.head != NULL) {
logError("file: "__FILE__", line: %d, "
"waiting receipt response from server %s:%u timeout",
__LINE__, task->server_ip, task->port);
__LINE__, formatted_ip, task->port);
} else {
logError("file: "__FILE__", line: %d, "
"%s server %s:%u timeout, channel established: %d",
__LINE__, task->nio_stages.current == SF_NIO_STAGE_SEND ?
"send to" : "recv from", task->server_ip, task->port,
"send to" : "recv from", formatted_ip, task->port,
FC_ATOMIC_GET(channel->established));
}
@ -89,10 +92,10 @@ static int receipt_recv_timeout_callback(struct fast_task_info *task)
static void receipt_task_finish_cleanup(struct fast_task_info *task)
{
IdempotencyClientChannel *channel;
char formatted_ip[FORMATTED_IP_SIZE];
if (task->event.fd >= 0) {
sf_task_detach_thread(task);
task->handler->close_connection(task);
}
sf_nio_reset_task_length(task);
@ -104,9 +107,12 @@ static void receipt_task_finish_cleanup(struct fast_task_info *task)
__sync_bool_compare_and_swap(&channel->established, 1, 0);
__sync_bool_compare_and_swap(&channel->in_ioevent, 1, 0);
logDebug("file: "__FILE__", line: %d, "
"receipt task for server %s:%u exit",
__LINE__, task->server_ip, task->port);
if (FC_LOG_BY_LEVEL(LOG_DEBUG)) {
format_ip_address(task->server_ip, formatted_ip);
logDebug("file: "__FILE__", line: %d, "
"receipt task for server %s:%u exit",
__LINE__, formatted_ip, task->port);
}
}
static void setup_channel_request(struct fast_task_info *task)
@ -247,11 +253,14 @@ static inline int receipt_expect_body_length(struct fast_task_info *task,
const int expect_body_len)
{
int body_len;
char formatted_ip[FORMATTED_IP_SIZE];
body_len = task->recv.ptr->length - sizeof(SFCommonProtoHeader);
if (body_len != expect_body_len) {
format_ip_address(task->server_ip, formatted_ip);
logError("file: "__FILE__", line: %d, "
"server %s:%u, response body length: %d != %d", __LINE__,
task->server_ip, task->port, body_len, expect_body_len);
formatted_ip, task->port, body_len, expect_body_len);
return EINVAL;
}
@ -264,6 +273,7 @@ static int deal_setup_channel_response(struct fast_task_info *task)
IdempotencyReceiptThreadContext *thread_ctx;
SFProtoSetupChannelResp *resp;
IdempotencyClientChannel *channel;
char formatted_ip[FORMATTED_IP_SIZE];
int channel_id;
int channel_key;
int buffer_size;
@ -275,11 +285,12 @@ static int deal_setup_channel_response(struct fast_task_info *task)
}
channel = (IdempotencyClientChannel *)task->arg;
if (__sync_add_and_fetch(&channel->established, 0)) {
if (FC_ATOMIC_GET(channel->established)) {
format_ip_address(task->server_ip, formatted_ip);
logWarning("file: "__FILE__", line: %d, "
"response from server %s:%u, unexpected cmd: "
"SETUP_CHANNEL_RESP, ignore it!",
__LINE__, task->server_ip, task->port);
__LINE__, formatted_ip, task->port);
return 0;
}
@ -316,6 +327,7 @@ static inline int deal_report_req_receipt_response(struct fast_task_info *task)
IdempotencyClientChannel *channel;
IdempotencyClientReceipt *current;
IdempotencyClientReceipt *deleted;
char formatted_ip[FORMATTED_IP_SIZE];
if ((result=receipt_expect_body_length(task, 0)) != 0) {
return result;
@ -323,10 +335,11 @@ static inline int deal_report_req_receipt_response(struct fast_task_info *task)
channel = (IdempotencyClientChannel *)task->arg;
if (channel->waiting_resp_qinfo.head == NULL) {
format_ip_address(task->server_ip, formatted_ip);
logWarning("file: "__FILE__", line: %d, "
"response from server %s:%u, unexpect cmd: "
"REPORT_REQ_RECEIPT_RESP", __LINE__,
task->server_ip, task->port);
formatted_ip, task->port);
return EINVAL;
}
@ -348,6 +361,7 @@ static int receipt_deal_task(struct fast_task_info *task, const int stage)
{
int result;
SFCommonProtoHeader *header;
char formatted_ip[FORMATTED_IP_SIZE];
do {
if (stage == SF_NIO_STAGE_HANDSHAKE) {
@ -375,10 +389,11 @@ static int receipt_deal_task(struct fast_task_info *task, const int stage)
msg_len = SF_RECV_BODY_LENGTH(task);
message = SF_PROTO_RECV_BODY(task);
format_ip_address(task->server_ip, formatted_ip);
logError("file: "__FILE__", line: %d, "
"response from server %s:%u, cmd: %d (%s), "
"status: %d, error info: %.*s", __LINE__,
task->server_ip, task->port, header->cmd,
formatted_ip, task->port, header->cmd,
sf_get_cmd_caption(header->cmd),
result, msg_len, message);
break;
@ -397,14 +412,18 @@ static int receipt_deal_task(struct fast_task_info *task, const int stage)
break;
case SF_SERVICE_PROTO_CLOSE_CHANNEL_RESP:
result = ECONNRESET; //force to close socket
logDebug("file: "__FILE__", line: %d, "
"close channel to server %s:%u !!!",
__LINE__, task->server_ip, task->port);
if (FC_LOG_BY_LEVEL(LOG_DEBUG)) {
format_ip_address(task->server_ip, formatted_ip);
logDebug("file: "__FILE__", line: %d, "
"close channel to server %s:%u !!!",
__LINE__, formatted_ip, task->port);
}
break;
default:
format_ip_address(task->server_ip, formatted_ip);
logError("file: "__FILE__", line: %d, "
"response from server %s:%u, unexpect cmd: %d (%s)",
__LINE__, task->server_ip, task->port, header->cmd,
__LINE__, formatted_ip, task->port, header->cmd,
sf_get_cmd_caption(header->cmd));
result = EINVAL;
break;
@ -449,6 +468,7 @@ static void receipt_thread_close_idle_channel(
{
IdempotencyClientChannel *channel;
IdempotencyClientChannel *tmp;
char formatted_ip[FORMATTED_IP_SIZE];
fc_list_for_each_entry_safe(channel, tmp, &thread_ctx->head, dlink) {
if (channel->task->pending_send_count > 0) {
@ -458,9 +478,12 @@ static void receipt_thread_close_idle_channel(
if (g_current_time - channel->last_report_time >
g_idempotency_client_cfg.channel_max_idle_time)
{
logDebug("file: "__FILE__", line: %d, "
"close channel to server %s:%u because idle too long",
__LINE__, channel->task->server_ip, channel->task->port);
if (FC_LOG_BY_LEVEL(LOG_DEBUG)) {
format_ip_address(channel->task->server_ip, formatted_ip);
logDebug("file: "__FILE__", line: %d, "
"close channel to server %s:%u because idle too long",
__LINE__, formatted_ip, channel->task->port);
}
close_channel_request(channel->task);
}
}
@ -500,10 +523,12 @@ static int do_init(FCAddressPtrArray *address_array)
{
const int task_arg_size = 0;
const bool double_buffers = false;
const bool need_shrink_task_buffer = false;
const bool explicit_post_recv = false;
int result;
int bytes;
SFNetworkHandler *rdma_handler;
struct ibv_pd *pd;
bytes = sizeof(IdempotencyReceiptThreadContext) * SF_G_WORK_THREADS;
RECEIPT_THREAD_CONTEXTS = (IdempotencyReceiptThreadContext *)
@ -520,19 +545,19 @@ static int do_init(FCAddressPtrArray *address_array)
TASK_PADDING_SIZE = rdma_handler->get_connection_size();
RDMA_INIT_CONNECTION = rdma_handler->init_connection;
RDMA_PD = rdma_handler->pd;
pd = rdma_handler->pd;
} else {
TASK_PADDING_SIZE = 0;
RDMA_INIT_CONNECTION = NULL;
RDMA_PD = NULL;
pd = NULL;
}
return sf_service_init_ex2(&g_sf_context, "idemp-receipt",
receipt_alloc_thread_extra_data, receipt_thread_loop_callback,
NULL, sf_proto_set_body_length, NULL, NULL, receipt_deal_task,
receipt_task_finish_cleanup, receipt_recv_timeout_callback,
1000, sizeof(SFCommonProtoHeader), TASK_PADDING_SIZE,
task_arg_size, double_buffers, explicit_post_recv,
receipt_init_task, NULL);
task_arg_size, double_buffers, need_shrink_task_buffer,
explicit_post_recv, receipt_init_task, pd, NULL);
}
int receipt_handler_init(FCAddressPtrArray *address_array)
@ -544,7 +569,6 @@ int receipt_handler_init(FCAddressPtrArray *address_array)
}
sf_enable_thread_notify(true);
sf_set_remove_from_ready_list(false);
fc_sleep_ms(100);
return 0;

View File

@ -186,9 +186,11 @@ static int save(SFBinlogIndexContext *ctx, const char *filename)
result = 0;
p = buff;
bend = buff + sizeof(buff);
p += sprintf(p, "%d %"PRId64"\n",
ctx->index_array.count,
ctx->last_version);
p += fc_itoa(ctx->index_array.count, p);
*p++ = ' ';
p += fc_itoa(ctx->last_version, p);
*p++ = '\n';
index = ctx->index_array.indexes;
for (i=0; i<ctx->index_array.count; i++) {
@ -227,7 +229,7 @@ int sf_binlog_index_save(SFBinlogIndexContext *ctx)
int result;
char tmp_filename[PATH_MAX];
snprintf(tmp_filename, sizeof(tmp_filename), "%s.tmp", ctx->filename);
fc_combine_two_strings(ctx->filename, "tmp", '.', tmp_filename);
if ((result=save(ctx, tmp_filename)) != 0) {
return result;
}

View File

@ -168,6 +168,7 @@ static int deal_binlog_records(SFBinlogWriterThread *thread,
SFBinlogWriterBuffer *wb_head, uint32_t *last_timestamp)
{
int result;
bool skip_empty_file;
SFBinlogWriterBuffer *wbuffer;
SFBinlogWriterBuffer *current;
@ -196,11 +197,15 @@ static int deal_binlog_records(SFBinlogWriterThread *thread,
thread->mblock, current);
break;
case SF_BINLOG_BUFFER_TYPE_ROTATE_FILE:
if ((result=sf_file_writer_set_binlog_write_index(&current->
writer->fw, current->writer->fw.binlog.
last_index + 1)) != 0)
{
return result;
flush_writer_files(thread);
skip_empty_file = current->version.first;
if (!(skip_empty_file && current->writer->fw.file.size == 0)) {
if ((result=sf_file_writer_set_binlog_write_index(&current->
writer->fw, current->writer->fw.binlog.
last_index + 1)) != 0)
{
return result;
}
}
fast_mblock_free_object(&current->writer->
thread->mblock, current);
@ -300,7 +305,7 @@ void sf_binlog_writer_finish(SFBinlogWriterInfo *writer)
uint32_t last_timestamp;
int count;
if (writer->fw.file.name != NULL) {
if (writer->fw.file.name.str != NULL) {
while (writer->thread->running && !fc_queue_empty(
&writer->thread->queue))
{
@ -328,8 +333,8 @@ void sf_binlog_writer_finish(SFBinlogWriterInfo *writer)
deal_binlog_records(writer->thread, wb_head, &last_timestamp);
}
free(writer->fw.file.name);
writer->fw.file.name = NULL;
free(writer->fw.file.name.str);
writer->fw.file.name.str = NULL;
}
if (writer->fw.file.fd >= 0) {
@ -342,6 +347,7 @@ static void *binlog_writer_func(void *arg)
{
SFBinlogWriterThread *thread;
SFBinlogWriterBuffer *wb_head;
uint32_t last_record_time;
uint32_t current_timestamp;
uint32_t last_timestamp;
int result;
@ -351,13 +357,12 @@ static void *binlog_writer_func(void *arg)
#ifdef OS_LINUX
{
char thread_name[64];
snprintf(thread_name, sizeof(thread_name),
"%s-writer", thread->name);
fc_combine_two_strings(thread->name, "writer", '-', thread_name);
prctl(PR_SET_NAME, thread_name);
}
#endif
current_timestamp = last_timestamp = 0;
last_record_time = current_timestamp = last_timestamp = 0;
thread->running = true;
while (SF_G_CONTINUE_FLAG) {
wb_head = (SFBinlogWriterBuffer *)fc_queue_pop_all(&thread->queue);
@ -366,7 +371,7 @@ static void *binlog_writer_func(void *arg)
}
if ((result=deal_binlog_records(thread, wb_head,
&current_timestamp)) != 0)
&last_record_time)) != 0)
{
if (result != ERRNO_THREAD_EXIT) {
logCrit("file: "__FILE__", line: %d, "
@ -379,6 +384,8 @@ static void *binlog_writer_func(void *arg)
if (fc_queue_empty(&thread->queue)) {
current_timestamp = 0;
} else {
current_timestamp = last_record_time;
}
if ((current_timestamp == 0 && last_timestamp != 0) ||
(current_timestamp > last_timestamp))
@ -393,6 +400,12 @@ static void *binlog_writer_func(void *arg)
}
PTHREAD_MUTEX_UNLOCK(&thread->flow_ctrol.lcp.lock);
}
if (thread->write_interval_ms > 0 &&
last_record_time == g_current_time)
{
fc_sleep_ms(thread->write_interval_ms);
}
}
thread->running = false;
@ -430,20 +443,23 @@ static void binlog_wbuffer_destroy_func(void *element, void *args)
int sf_binlog_writer_init_normal_ex(SFBinlogWriterInfo *writer,
const char *data_path, const char *subdir_name,
const char *file_prefix, const int buffer_size,
const int64_t file_rotate_size, const bool call_fsync)
const char *file_prefix, const int max_record_size,
const int buffer_size, const int64_t file_rotate_size,
const bool call_fsync)
{
memset(writer, 0, sizeof(*writer));
writer->order_by = SF_BINLOG_WRITER_TYPE_ORDER_BY_NONE;
return sf_file_writer_init(&writer->fw, data_path, subdir_name,
file_prefix, buffer_size, file_rotate_size, call_fsync);
file_prefix, max_record_size, buffer_size,
file_rotate_size, call_fsync);
}
int sf_binlog_writer_init_by_version_ex(SFBinlogWriterInfo *writer,
const char *data_path, const char *subdir_name,
const char *file_prefix, const uint64_t next_version,
const int buffer_size, const int ring_size,
const int64_t file_rotate_size, const bool call_fsync)
const char *file_prefix, const int max_record_size,
const uint64_t next_version, const int buffer_size,
const int ring_size, const int64_t file_rotate_size,
const bool call_fsync)
{
int bytes;
@ -462,13 +478,15 @@ int sf_binlog_writer_init_by_version_ex(SFBinlogWriterInfo *writer,
binlog_writer_set_next_version(writer, next_version);
writer->flush.in_queue = false;
return sf_file_writer_init(&writer->fw, data_path, subdir_name,
file_prefix, buffer_size, file_rotate_size, call_fsync);
file_prefix, max_record_size, buffer_size,
file_rotate_size, call_fsync);
}
int sf_binlog_writer_init_thread_ex(SFBinlogWriterThread *thread,
const char *name, SFBinlogWriterInfo *writer, const short order_mode,
const int max_delay, const int max_record_size, const bool
use_fixed_buffer_size, const bool passive_write)
const int write_interval_ms, const int max_delay,
const int max_record_size, const bool use_fixed_buffer_size,
const bool passive_write)
{
const int alloc_elements_once = 1024;
const int64_t alloc_elements_limit = 0;
@ -478,10 +496,11 @@ int sf_binlog_writer_init_thread_ex(SFBinlogWriterThread *thread,
pthread_t tid;
struct fast_mblock_object_callbacks callbacks;
snprintf(thread->name, sizeof(thread->name), "%s", name);
fc_safe_strcpy(thread->name, name);
thread->order_mode = order_mode;
thread->use_fixed_buffer_size = use_fixed_buffer_size;
thread->passive_write = passive_write;
thread->write_interval_ms = write_interval_ms;
thread->flow_ctrol.max_delay = max_delay;
writer->fw.cfg.max_record_size = max_record_size;
writer->thread = thread;
@ -608,10 +627,12 @@ int sf_binlog_writer_change_write_index(SFBinlogWriterInfo *writer,
SF_BINLOG_BUFFER_TYPE_SET_WRITE_INDEX, write_index);
}
int sf_binlog_writer_rotate_file(SFBinlogWriterInfo *writer)
int sf_binlog_writer_rotate_file_ex(SFBinlogWriterInfo *writer,
const bool skip_empty_file)
{
return sf_binlog_writer_push_directive(writer,
SF_BINLOG_BUFFER_TYPE_ROTATE_FILE, 0);
SF_BINLOG_BUFFER_TYPE_ROTATE_FILE,
skip_empty_file ? 1 : 0);
}
int sf_binlog_writer_flush_file(SFBinlogWriterInfo *writer)

View File

@ -72,6 +72,7 @@ typedef struct binlog_writer_thread {
bool use_fixed_buffer_size;
bool passive_write;
char order_mode;
int write_interval_ms;
struct {
int max_delay; //in seconds
volatile uint32_t last_timestamp;
@ -112,61 +113,64 @@ extern "C" {
int sf_binlog_writer_init_normal_ex(SFBinlogWriterInfo *writer,
const char *data_path, const char *subdir_name,
const char *file_prefix, const int buffer_size,
const int64_t file_rotate_size, const bool call_fsync);
const char *file_prefix, const int max_record_size,
const int buffer_size, const int64_t file_rotate_size,
const bool call_fsync);
int sf_binlog_writer_init_by_version_ex(SFBinlogWriterInfo *writer,
const char *data_path, const char *subdir_name,
const char *file_prefix, const uint64_t next_version,
const int buffer_size, const int ring_size,
const int64_t file_rotate_size, const bool call_fsync);
const char *file_prefix, const int max_record_size,
const uint64_t next_version, const int buffer_size,
const int ring_size, const int64_t file_rotate_size,
const bool call_fsync);
int sf_binlog_writer_init_thread_ex(SFBinlogWriterThread *thread,
const char *name, SFBinlogWriterInfo *writer, const short order_mode,
const int max_delay, const int max_record_size, const bool
use_fixed_buffer_size, const bool passive_write);
const int write_interval_ms, const int max_delay,
const int max_record_size, const bool use_fixed_buffer_size,
const bool passive_write);
#define sf_binlog_writer_init_normal(writer, \
data_path, subdir_name, buffer_size) \
sf_binlog_writer_init_normal_ex(writer, data_path, subdir_name, \
SF_BINLOG_FILE_PREFIX, buffer_size, \
#define sf_binlog_writer_init_normal(writer, data_path, \
subdir_name, max_record_size, buffer_size) \
sf_binlog_writer_init_normal_ex(writer, data_path, subdir_name, \
SF_BINLOG_FILE_PREFIX_STR, max_record_size, buffer_size, \
SF_BINLOG_DEFAULT_ROTATE_SIZE, true)
#define sf_binlog_writer_init_by_version(writer, data_path, \
subdir_name, next_version, buffer_size, ring_size) \
sf_binlog_writer_init_by_version_ex(writer, data_path, subdir_name, \
SF_BINLOG_FILE_PREFIX, next_version, buffer_size, \
ring_size, SF_BINLOG_DEFAULT_ROTATE_SIZE, true)
#define sf_binlog_writer_init_by_version(writer, data_path, subdir_name, \
max_record_size, next_version, buffer_size, ring_size) \
sf_binlog_writer_init_by_version_ex(writer, data_path, subdir_name, \
SF_BINLOG_FILE_PREFIX_STR, max_record_size, next_version, \
buffer_size, ring_size, SF_BINLOG_DEFAULT_ROTATE_SIZE, true)
#define sf_binlog_writer_init_thread(thread, name, \
writer, max_delay, max_record_size) \
sf_binlog_writer_init_thread_ex(thread, name, writer, \
SF_BINLOG_THREAD_ORDER_MODE_FIXED, max_delay, \
max_record_size, true, false)
writer, write_interval_ms, max_delay, max_record_size) \
sf_binlog_writer_init_thread_ex(thread, name, writer, \
SF_BINLOG_THREAD_ORDER_MODE_FIXED, write_interval_ms, \
max_delay, max_record_size, true, false)
static inline int sf_binlog_writer_init_ex(SFBinlogWriterContext *context,
const char *data_path, const char *subdir_name,
const char *file_prefix, const int buffer_size,
const int max_delay, const int max_record_size,
const bool call_fsync)
const int write_interval_ms, const int max_delay,
const int max_record_size, const bool call_fsync)
{
int result;
if ((result=sf_binlog_writer_init_normal_ex(&context->writer,
data_path, subdir_name, file_prefix, buffer_size,
if ((result=sf_binlog_writer_init_normal_ex(&context->writer, data_path,
subdir_name, file_prefix, max_record_size, buffer_size,
SF_BINLOG_DEFAULT_ROTATE_SIZE, call_fsync)) != 0)
{
return result;
}
return sf_binlog_writer_init_thread(&context->thread, subdir_name,
&context->writer, max_delay, max_record_size);
&context->writer, write_interval_ms, max_delay, max_record_size);
}
#define sf_binlog_writer_init(context, data_path, subdir_name, \
buffer_size, max_delay, max_record_size) \
sf_binlog_writer_init_ex(context, data_path, subdir_name, \
SF_BINLOG_FILE_PREFIX, buffer_size, max_delay, \
max_record_size, true)
#define sf_binlog_writer_init(context, data_path, subdir_name, \
buffer_size, write_interval_ms, max_delay, max_record_size) \
sf_binlog_writer_init_ex(context, data_path, subdir_name, \
SF_BINLOG_FILE_PREFIX_STR, buffer_size, write_interval_ms, \
max_delay, max_record_size, true)
void sf_binlog_writer_finish(SFBinlogWriterInfo *writer);
@ -231,7 +235,14 @@ static inline int sf_binlog_writer_get_thread_waiting_count(
return waiting_count;
}
int sf_binlog_writer_rotate_file(SFBinlogWriterInfo *writer);
int sf_binlog_writer_rotate_file_ex(SFBinlogWriterInfo *writer,
const bool skip_empty_file);
static inline int sf_binlog_writer_rotate_file(SFBinlogWriterInfo *writer)
{
const bool skip_empty_file = false;
return sf_binlog_writer_rotate_file_ex(writer, skip_empty_file);
}
int sf_binlog_writer_flush_file(SFBinlogWriterInfo *writer);
@ -243,6 +254,9 @@ int sf_binlog_writer_notify_exit(SFBinlogWriterInfo *writer);
#define sf_binlog_writer_set_flags(writer, flags) \
sf_file_writer_set_flags(&(writer)->fw, flags)
#define sf_binlog_writer_set_write_done_callback(writer, callback, args) \
sf_file_writer_set_write_done_callback(&(writer)->fw, callback, args)
#define sf_binlog_writer_get_last_version_ex(writer, log_level) \
sf_file_writer_get_last_version_ex(&(writer)->fw, log_level)
@ -272,7 +286,9 @@ static inline SFBinlogWriterBuffer *sf_binlog_writer_alloc_buffer(
{
SFBinlogWriterBuffer *buffer;
if ((buffer=fast_mblock_alloc_object(&thread->mblock)) != NULL) {
if ((buffer=(SFBinlogWriterBuffer *)fast_mblock_alloc_object(
&thread->mblock)) != NULL)
{
buffer->type = SF_BINLOG_BUFFER_TYPE_WRITE_TO_FILE;
}
return buffer;

View File

@ -30,8 +30,11 @@ typedef struct {
#define sf_buffered_writer_init(writer, filename) \
sf_buffered_writer_init_ex(writer, filename, 1024 * 1024)
#define SF_BUFFERED_WRITER_LENGTH(bw) ((bw).buffer.current - (bw).buffer.buff)
#define SF_BUFFERED_WRITER_REMAIN(bw) ((bw).buffer.end - (bw).buffer.current)
#define SF_BUFFERED_WRITER_LENGTH(bw) \
SF_BINLOG_BUFFER_PRODUCER_DATA_LENGTH((bw).buffer)
#define SF_BUFFERED_WRITER_REMAIN(bw) \
SF_BINLOG_BUFFER_PRODUCER_BUFF_REMAIN((bw).buffer)
#ifdef __cplusplus
extern "C" {
@ -56,7 +59,6 @@ extern "C" {
if ((result=sf_binlog_buffer_init(&writer->buffer, buffer_size)) != 0) {
return result;
}
writer->buffer.end = writer->buffer.buff + writer->buffer.size;
return 0;
}
@ -65,7 +67,7 @@ extern "C" {
int result;
int length;
length = writer->buffer.current - writer->buffer.buff;
length = writer->buffer.data_end - writer->buffer.buff;
if (fc_safe_write(writer->fd, writer->buffer.buff, length) != length) {
result = errno != 0 ? errno : EIO;
logError("file: "__FILE__", line: %d, "
@ -74,17 +76,27 @@ extern "C" {
return result;
}
writer->buffer.current = writer->buffer.buff;
writer->buffer.data_end = writer->buffer.buff;
return 0;
}
static inline void sf_buffered_writer_destroy(SFBufferedWriter *writer)
static inline int sf_buffered_writer_destroy(SFBufferedWriter *writer)
{
int result;
if (writer->fd >= 0) {
if (fsync(writer->fd) != 0) {
result = errno != 0 ? errno : EIO;
logError("file: "__FILE__", line: %d, "
"fsync to file %s fail, errno: %d, error info: %s",
__LINE__, writer->filename, result, STRERROR(result));
return result;
}
close(writer->fd);
writer->fd = -1;
}
sf_binlog_buffer_destroy(&writer->buffer);
return 0;
}
#ifdef __cplusplus

View File

@ -23,7 +23,7 @@ static int calc_cluster_config_sign(SFClusterConfig *cluster)
FastBuffer buffer;
int result;
if ((result=fast_buffer_init_ex(&buffer, 1024)) != 0) {
if ((result=fast_buffer_init1(&buffer, 1024)) != 0) {
return result;
}
fc_server_to_config_string(&cluster->server_cfg, &buffer);

View File

@ -35,14 +35,14 @@ static int get_group_servers(SFConnectionManager *cm,
SFCMConnGroupEntry *group);
static ConnectionInfo *get_spec_connection(SFConnectionManager *cm,
const ConnectionInfo *target, int *err_no)
const ConnectionInfo *target, const bool shared, int *err_no)
{
return conn_pool_get_connection_ex(&cm->cpool,
target, cm->module_name, err_no);
target, cm->module_name, shared, err_no);
}
static ConnectionInfo *make_connection(SFConnectionManager *cm,
FCAddressPtrArray *addr_array, int *err_no)
FCAddressPtrArray *addr_array, const bool shared, int *err_no)
{
FCAddressInfo **current;
FCAddressInfo **addr;
@ -56,7 +56,7 @@ static ConnectionInfo *make_connection(SFConnectionManager *cm,
current = addr_array->addrs + addr_array->index;
if ((conn=get_spec_connection(cm, &(*current)->conn,
err_no)) != NULL)
shared, err_no)) != NULL)
{
return conn;
}
@ -72,7 +72,7 @@ static ConnectionInfo *make_connection(SFConnectionManager *cm,
}
if ((conn=get_spec_connection(cm, &(*addr)->conn,
err_no)) != NULL)
shared, err_no)) != NULL)
{
addr_array->index = addr - addr_array->addrs;
return conn;
@ -83,13 +83,13 @@ static ConnectionInfo *make_connection(SFConnectionManager *cm,
}
static ConnectionInfo *get_server_connection(SFConnectionManager *cm,
FCServerInfo *server, int *err_no)
FCServerInfo *server, const bool shared, int *err_no)
{
FCAddressPtrArray *addr_array;
ConnectionInfo *conn;
addr_array = &server->group_addrs[cm->server_group_index].address_array;
if ((conn=make_connection(cm, addr_array, err_no)) == NULL) {
if ((conn=make_connection(cm, addr_array, shared, err_no)) == NULL) {
logError("file: "__FILE__", line: %d, "
"%s server id: %d, address count: %d, get_server_connection fail",
__LINE__, cm->module_name, server->id, addr_array->count);
@ -98,7 +98,7 @@ static ConnectionInfo *get_server_connection(SFConnectionManager *cm,
}
static ConnectionInfo *get_connection(SFConnectionManager *cm,
const int group_index, int *err_no)
const int group_index, const bool shared, int *err_no)
{
SFCMServerArray *server_array;
ConnectionInfo *conn;
@ -110,7 +110,7 @@ static ConnectionInfo *get_connection(SFConnectionManager *cm,
server_hash_code = rand();
server_index = server_hash_code % server_array->count;
if ((conn=make_connection(cm, server_array->servers[server_index].
addr_array, err_no)) != NULL)
addr_array, shared, err_no)) != NULL)
{
return conn;
}
@ -122,7 +122,7 @@ static ConnectionInfo *get_connection(SFConnectionManager *cm,
}
if ((conn=make_connection(cm, server_array->servers[i].
addr_array, err_no)) != NULL)
addr_array, shared, err_no)) != NULL)
{
return conn;
}
@ -176,7 +176,7 @@ static inline bool alive_array_cas(SFConnectionManager *cm,
push_to_detect_queue(cm, group, new_alives);
fast_mblock_delay_free_object(&cm->sptr_array_allocator, old_alives,
(cm->common_cfg->connect_timeout + cm->common_cfg->
network_timeout) * group->all.count);
network_timeout) * group->all.count * 4);
return true;
} else {
fast_mblock_free_object(&cm->sptr_array_allocator, new_alives);
@ -225,7 +225,7 @@ static int remove_from_alives(SFConnectionManager *cm,
}
static inline ConnectionInfo *make_master_connection(SFConnectionManager *cm,
SFCMConnGroupEntry *group, int *err_no)
SFCMConnGroupEntry *group, const bool shared, int *err_no)
{
SFCMServerEntry *master;
ConnectionInfo *conn;
@ -234,7 +234,7 @@ static inline ConnectionInfo *make_master_connection(SFConnectionManager *cm,
master = (SFCMServerEntry *)FC_ATOMIC_GET(group->master);
if (master != NULL) {
if ((conn=make_connection(cm, master->addr_array,
err_no)) != NULL)
shared, err_no)) != NULL)
{
alives = (SFCMServerPtrArray *)FC_ATOMIC_GET(group->alives);
set_connection_params(conn, master, alives);
@ -254,12 +254,12 @@ static inline ConnectionInfo *make_master_connection(SFConnectionManager *cm,
static inline ConnectionInfo *make_readable_connection(SFConnectionManager *cm,
SFCMConnGroupEntry *group, SFCMServerPtrArray *alives,
const int index, int *err_no)
const int index, const bool shared, int *err_no)
{
ConnectionInfo *conn;
if ((conn=make_connection(cm, alives->servers[index]->
addr_array, err_no)) == NULL)
addr_array, shared, err_no)) == NULL)
{
remove_from_alives(cm, group, alives, alives->servers[index]);
} else {
@ -270,7 +270,7 @@ static inline ConnectionInfo *make_readable_connection(SFConnectionManager *cm,
}
static ConnectionInfo *get_master_connection(SFConnectionManager *cm,
const int group_index, int *err_no)
const int group_index, const bool shared, int *err_no)
{
SFCMConnGroupEntry *group;
ConnectionInfo *conn;
@ -283,7 +283,7 @@ static ConnectionInfo *get_master_connection(SFConnectionManager *cm,
&cm->common_cfg->net_retry_cfg.connect);
retry_count = 0;
while (1) {
if ((conn=make_master_connection(cm, group, err_no)) != NULL) {
if ((conn=make_master_connection(cm, group, shared, err_no)) != NULL) {
return conn;
}
@ -313,7 +313,7 @@ static ConnectionInfo *get_master_connection(SFConnectionManager *cm,
}
static ConnectionInfo *get_readable_connection(SFConnectionManager *cm,
const int group_index, int *err_no)
const int group_index, const bool shared, int *err_no)
{
SFCMConnGroupEntry *group;
SFCMServerPtrArray *alives;
@ -326,7 +326,7 @@ static ConnectionInfo *get_readable_connection(SFConnectionManager *cm,
if ((cm->common_cfg->read_rule == sf_data_read_rule_master_only) ||
(group->all.count == 1))
{
return get_master_connection(cm, group_index, err_no);
return get_master_connection(cm, group_index, shared, err_no);
}
sf_init_net_retry_interval_context(&net_retry_ctx,
@ -338,14 +338,14 @@ static ConnectionInfo *get_readable_connection(SFConnectionManager *cm,
if (alives->count > 0) {
index = rand() % alives->count;
if ((conn=make_readable_connection(cm, group, alives,
index, err_no)) != NULL)
index, shared, err_no)) != NULL)
{
return conn;
}
}
if (cm->common_cfg->read_rule == sf_data_read_rule_slave_first) {
if ((conn=make_master_connection(cm, group, err_no)) != NULL) {
if ((conn=make_master_connection(cm, group, shared, err_no)) != NULL) {
return conn;
}
}
@ -400,11 +400,12 @@ static void close_connection(SFConnectionManager *cm, ConnectionInfo *conn)
}
static ConnectionInfo *get_leader_connection(SFConnectionManager *cm,
FCServerInfo *server, int *err_no)
FCServerInfo *server, const bool shared, int *err_no)
{
ConnectionInfo *conn;
SFClientServerEntry leader;
SFNetRetryIntervalContext net_retry_ctx;
char formatted_ip[FORMATTED_IP_SIZE];
int i;
int connect_fails;
@ -415,7 +416,7 @@ static ConnectionInfo *get_leader_connection(SFConnectionManager *cm,
while (1) {
do {
if ((conn=get_server_connection(cm, server,
err_no)) == NULL)
shared, err_no)) == NULL)
{
connect_fails++;
break;
@ -432,8 +433,9 @@ static ConnectionInfo *get_leader_connection(SFConnectionManager *cm,
return conn;
}
release_connection(cm, conn);
if ((conn=get_spec_connection(cm, &leader.conn,
err_no)) == NULL)
shared, err_no)) == NULL)
{
if (cm->server_cfg != NULL) {
FCServerInfo *ls;
@ -444,7 +446,7 @@ static ConnectionInfo *get_leader_connection(SFConnectionManager *cm,
address_array.count > 1)
{
if ((conn=get_server_connection(cm, ls,
err_no)) != NULL)
shared, err_no)) != NULL)
{
return conn;
}
@ -467,10 +469,11 @@ static ConnectionInfo *get_leader_connection(SFConnectionManager *cm,
connect.times, ++i, *err_no);
}
format_ip_address(server->group_addrs[cm->server_group_index].
address_array.addrs[0]->conn.ip_addr, formatted_ip);
logWarning("file: "__FILE__", line: %d, "
"%s get_leader_connection fail, server id: %d, %s:%u, errno: %d",
__LINE__, cm->module_name, server->id, server->group_addrs
[cm->server_group_index]. address_array.addrs[0]->conn.ip_addr,
__LINE__, cm->module_name, server->id, formatted_ip,
server->group_addrs[cm->server_group_index].address_array.
addrs[0]->conn.port, *err_no);
return NULL;
@ -527,12 +530,19 @@ int sf_connection_manager_init_ex(SFConnectionManager *cm,
} extra_params;
FCServerGroupInfo *server_group;
int htable_init_capacity;
int htable_capacity;
int result;
htable_init_capacity = 4 * server_count;
if (htable_init_capacity < 256) {
htable_init_capacity = 256;
if (server_count <= 4) {
htable_capacity = 16;
} else if (server_count <= 16) {
htable_capacity = 64;
} else if (server_count <= 32) {
htable_capacity = 128;
} else if (server_count < 64) {
htable_capacity = 256;
} else {
htable_capacity = 4 * server_count;
}
if ((server_group=fc_server_get_group_by_index(server_cfg,
@ -552,7 +562,7 @@ int sf_connection_manager_init_ex(SFConnectionManager *cm,
extra_params.ptr = &extra_params.holder;
}
if ((result=conn_pool_init_ex1(&cm->cpool, common_cfg->connect_timeout,
max_count_per_entry, max_idle_time, htable_init_capacity,
max_count_per_entry, max_idle_time, htable_capacity,
connect_done_callback, args,
sf_cm_validate_connection_callback, cm,
sizeof(SFConnectionParameters),
@ -768,6 +778,7 @@ static int do_get_group_servers(SFConnectionManager *cm,
static int get_group_servers_by_active(SFConnectionManager *cm,
SFCMConnGroupEntry *group)
{
const bool shared = true;
SFCMServerPtrArray *alives;
SFCMServerEntry **server;
SFCMServerEntry **end;
@ -783,7 +794,7 @@ static int get_group_servers_by_active(SFConnectionManager *cm,
end = alives->servers + alives->count;
for (server=alives->servers; server<end; server++) {
if ((conn=make_connection(cm, (*server)->addr_array,
&result)) == NULL)
shared, &result)) == NULL)
{
continue;
}
@ -801,6 +812,7 @@ static int get_group_servers_by_active(SFConnectionManager *cm,
static int get_group_servers_by_all(SFConnectionManager *cm,
SFCMConnGroupEntry *group)
{
const bool shared = true;
SFCMServerEntry *server;
SFCMServerEntry *end;
ConnectionInfo *conn;
@ -818,7 +830,7 @@ static int get_group_servers_by_all(SFConnectionManager *cm,
}
if ((conn=make_connection(cm, server->addr_array,
&result)) == NULL)
shared, &result)) == NULL)
{
continue;
}

View File

@ -26,16 +26,17 @@
struct sf_connection_manager;
typedef ConnectionInfo *(*sf_get_connection_func)(
struct sf_connection_manager *cm,
const int group_index, int *err_no);
struct sf_connection_manager *cm, const int group_index,
const bool shared, int *err_no);
typedef ConnectionInfo *(*sf_get_server_connection_func)(
struct sf_connection_manager *cm,
FCServerInfo *server, int *err_no);
FCServerInfo *server, const bool shared, int *err_no);
typedef ConnectionInfo *(*sf_get_spec_connection_func)(
struct sf_connection_manager *cm,
const ConnectionInfo *target, int *err_no);
const ConnectionInfo *target,
const bool shared, int *err_no);
typedef void (*sf_release_connection_func)(
struct sf_connection_manager *cm, ConnectionInfo *conn);

View File

@ -20,8 +20,8 @@
#include "fastcommon/sockopt.h"
#define SF_DEFAULT_CONNECT_TIMEOUT 2
#define SF_DEFAULT_NETWORK_TIMEOUT 10
#define SF_DEFAULT_CONNECT_TIMEOUT 10
#define SF_DEFAULT_NETWORK_TIMEOUT 60
#define SF_DEF_THREAD_STACK_SIZE (256 * 1024)
#define SF_MIN_THREAD_STACK_SIZE (64 * 1024)
@ -96,6 +96,8 @@
#define SF_BINLOG_SOURCE_USER 'U' //by user call
#define SF_BINLOG_SOURCE_REPLAY 'R' //by binlog replay
#define SF_LOG_SCHEDULE_ENTRIES_COUNT 3
#ifdef __cplusplus
extern "C" {
#endif

View File

@ -34,49 +34,108 @@
#include "sf_func.h"
#include "sf_file_writer.h"
#define BINLOG_INDEX_ITEM_START_INDEX "start_index"
#define BINLOG_INDEX_ITEM_CURRENT_WRITE "current_write"
#define BINLOG_INDEX_ITEM_CURRENT_COMPRESS "current_compress"
#define BINLOG_INDEX_ITEM_START_INDEX_STR "start_index"
#define BINLOG_INDEX_ITEM_START_INDEX_LEN \
(sizeof(BINLOG_INDEX_ITEM_START_INDEX_STR) - 1)
#define GET_BINLOG_FILENAME(writer) \
sprintf(writer->file.name, "%s/%s/%s"SF_BINLOG_FILE_EXT_FMT, \
writer->cfg.data_path, writer->cfg.subdir_name, \
writer->cfg.file_prefix, writer->binlog.last_index)
#define BINLOG_INDEX_ITEM_CURRENT_WRITE_STR "current_write"
#define BINLOG_INDEX_ITEM_CURRENT_WRITE_LEN \
(sizeof(BINLOG_INDEX_ITEM_CURRENT_WRITE_STR) - 1)
#define GET_BINLOG_INDEX_FILENAME_EX(data_path, \
subdir_name, file_prefix, filename, size) \
snprintf(filename, size, "%s/%s/%s_index.dat", \
data_path, subdir_name, file_prefix)
#define BINLOG_INDEX_ITEM_CURRENT_COMPRESS_STR "current_compress"
#define BINLOG_INDEX_ITEM_CURRENT_COMPRESS_LEN \
(sizeof(BINLOG_INDEX_ITEM_CURRENT_COMPRESS_STR) - 1)
#define GET_BINLOG_INDEX_FILENAME(writer, filename, size) \
GET_BINLOG_INDEX_FILENAME_EX(writer->cfg.data_path, \
writer->cfg.subdir_name, writer->cfg.file_prefix, filename, size)
static inline void sf_file_writer_get_binlog_filename(SFFileWriterInfo *writer)
{
sf_file_writer_get_filename_ex(
writer->cfg.data_path, writer->cfg.subdir_name,
writer->cfg.file_prefix, writer->binlog.last_index,
writer->file.name.str, writer->file.name.size);
}
static inline void sf_file_writer_get_index_filename_ex(
const char *data_path, const char *subdir_name,
const char *file_prefix, const int file_prefix_len,
char *filename, const int size)
{
#define INDEX_FILENAME_AFFIX_STR "_index.dat"
#define INDEX_FILENAME_AFFIX_LEN (sizeof(INDEX_FILENAME_AFFIX_STR) - 1)
char *p;
int data_path_len;
int subdir_name_len;
data_path_len = strlen(data_path);
subdir_name_len = strlen(subdir_name);
if (data_path_len + 1 + subdir_name_len + 1 + file_prefix_len +
INDEX_FILENAME_AFFIX_LEN >= size)
{
*filename = '\0';
return;
}
memcpy(filename, data_path, data_path_len);
p = filename + data_path_len;
*p++ = '/';
memcpy(p, subdir_name, subdir_name_len);
p += subdir_name_len;
*p++ = '/';
memcpy(p, file_prefix, file_prefix_len);
p += file_prefix_len;
memcpy(p, INDEX_FILENAME_AFFIX_STR, INDEX_FILENAME_AFFIX_LEN);
p += INDEX_FILENAME_AFFIX_LEN;
*p = '\0';
}
const char *sf_file_writer_get_index_filename(const char *data_path,
const char *subdir_name, char *filename, const int size)
{
GET_BINLOG_INDEX_FILENAME_EX(data_path, subdir_name,
SF_BINLOG_FILE_PREFIX, filename, size);
sf_file_writer_get_index_filename_ex(data_path, subdir_name,
SF_BINLOG_FILE_PREFIX_STR, SF_BINLOG_FILE_PREFIX_LEN,
filename, size);
return filename;
}
static int write_to_binlog_index_file(SFFileWriterInfo *writer)
int sf_file_writer_write_to_binlog_index_file_ex(const char *data_path,
const char *subdir_name, const char *file_prefix,
const int start_index, const int last_index,
const int compress_index)
{
char filename[PATH_MAX];
char buff[256];
char *p;
int result;
int len;
GET_BINLOG_INDEX_FILENAME(writer, filename, sizeof(filename));
len = sprintf(buff, "%s=%d\n"
"%s=%d\n"
"%s=%d\n",
BINLOG_INDEX_ITEM_START_INDEX,
writer->binlog.start_index,
BINLOG_INDEX_ITEM_CURRENT_WRITE,
writer->binlog.last_index,
BINLOG_INDEX_ITEM_CURRENT_COMPRESS,
writer->binlog.compress_index);
sf_file_writer_get_index_filename_ex(data_path, subdir_name, file_prefix,
strlen(file_prefix), filename, sizeof(filename));
p = buff;
memcpy(p, BINLOG_INDEX_ITEM_START_INDEX_STR,
BINLOG_INDEX_ITEM_START_INDEX_LEN);
p += BINLOG_INDEX_ITEM_START_INDEX_LEN;
*p++ = '=';
p += fc_itoa(start_index, p);
*p++ = '\n';
memcpy(p, BINLOG_INDEX_ITEM_CURRENT_WRITE_STR,
BINLOG_INDEX_ITEM_CURRENT_WRITE_LEN);
p += BINLOG_INDEX_ITEM_CURRENT_WRITE_LEN;
*p++ = '=';
p += fc_itoa(last_index, p);
*p++ = '\n';
memcpy(p, BINLOG_INDEX_ITEM_CURRENT_COMPRESS_STR,
BINLOG_INDEX_ITEM_CURRENT_COMPRESS_LEN);
p += BINLOG_INDEX_ITEM_CURRENT_COMPRESS_LEN;
*p++ = '=';
p += fc_itoa(compress_index, p);
*p++ = '\n';
len = p - buff;
if ((result=safeWriteToFile(filename, buff, len)) != 0) {
logError("file: "__FILE__", line: %d, "
"write to file \"%s\" fail, errno: %d, error info: %s",
@ -86,6 +145,14 @@ static int write_to_binlog_index_file(SFFileWriterInfo *writer)
return result;
}
static inline int write_to_binlog_index_file(SFFileWriterInfo *writer)
{
return sf_file_writer_write_to_binlog_index_file_ex(
writer->cfg.data_path, writer->cfg.subdir_name,
writer->cfg.file_prefix, writer->binlog.start_index,
writer->binlog.last_index, writer->binlog.compress_index);
}
static int get_binlog_info_from_file(const char *data_path,
const char *subdir_name, int *start_index,
int *last_index, int *compress_index)
@ -94,8 +161,8 @@ static int get_binlog_info_from_file(const char *data_path,
IniContext ini_context;
int result;
GET_BINLOG_INDEX_FILENAME_EX(data_path,
subdir_name, SF_BINLOG_FILE_PREFIX,
sf_file_writer_get_index_filename_ex(data_path, subdir_name,
SF_BINLOG_FILE_PREFIX_STR, SF_BINLOG_FILE_PREFIX_LEN,
full_filename, sizeof(full_filename));
if (access(full_filename, F_OK) != 0) {
return errno != 0 ? errno : EPERM;
@ -109,13 +176,13 @@ static int get_binlog_info_from_file(const char *data_path,
}
*start_index = iniGetIntValue(NULL,
BINLOG_INDEX_ITEM_START_INDEX,
BINLOG_INDEX_ITEM_START_INDEX_STR,
&ini_context, 0);
*last_index = iniGetIntValue(NULL,
BINLOG_INDEX_ITEM_CURRENT_WRITE,
BINLOG_INDEX_ITEM_CURRENT_WRITE_STR,
&ini_context, 0);
*compress_index = iniGetIntValue(NULL,
BINLOG_INDEX_ITEM_CURRENT_COMPRESS,
BINLOG_INDEX_ITEM_CURRENT_COMPRESS_STR,
&ini_context, 0);
iniFreeContext(&ini_context);
@ -165,14 +232,14 @@ static int open_writable_binlog(SFFileWriterInfo *writer)
close(writer->file.fd);
}
GET_BINLOG_FILENAME(writer);
writer->file.fd = open(writer->file.name, O_WRONLY |
sf_file_writer_get_binlog_filename(writer);
writer->file.fd = open(writer->file.name.str, O_WRONLY |
O_CREAT | O_APPEND | O_CLOEXEC, 0644);
if (writer->file.fd < 0) {
logError("file: "__FILE__", line: %d, "
"open file \"%s\" fail, "
"errno: %d, error info: %s",
__LINE__, writer->file.name,
__LINE__, writer->file.name.str,
errno, STRERROR(errno));
return errno != 0 ? errno : EACCES;
}
@ -182,7 +249,7 @@ static int open_writable_binlog(SFFileWriterInfo *writer)
logError("file: "__FILE__", line: %d, "
"lseek file \"%s\" fail, "
"errno: %d, error info: %s",
__LINE__, writer->file.name,
__LINE__, writer->file.name.str,
errno, STRERROR(errno));
return errno != 0 ? errno : EIO;
}
@ -192,23 +259,24 @@ static int open_writable_binlog(SFFileWriterInfo *writer)
static int open_next_binlog(SFFileWriterInfo *writer)
{
GET_BINLOG_FILENAME(writer);
if (access(writer->file.name, F_OK) == 0) {
sf_file_writer_get_binlog_filename(writer);
if (access(writer->file.name.str, F_OK) == 0) {
char bak_filename[PATH_MAX];
char date_str[32];
snprintf(bak_filename, sizeof(bak_filename), "%s.%s",
writer->file.name, formatDatetime(g_current_time,
"%Y%m%d%H%M%S", date_str, sizeof(date_str)));
if (rename(writer->file.name, bak_filename) == 0) {
formatDatetime(g_current_time, "%Y%m%d%H%M%S",
date_str, sizeof(date_str));
fc_combine_two_strings(writer->file.name.str,
date_str, '.', bak_filename);
if (rename(writer->file.name.str, bak_filename) == 0) {
logWarning("file: "__FILE__", line: %d, "
"binlog file %s exist, rename to %s",
__LINE__, writer->file.name, bak_filename);
__LINE__, writer->file.name.str, bak_filename);
} else {
logError("file: "__FILE__", line: %d, "
"rename binlog %s to backup %s fail, "
"errno: %d, error info: %s",
__LINE__, writer->file.name, bak_filename,
__LINE__, writer->file.name.str, bak_filename,
errno, STRERROR(errno));
return errno != 0 ? errno : EPERM;
}
@ -227,7 +295,7 @@ static int do_write_to_file(SFFileWriterInfo *writer,
logError("file: "__FILE__", line: %d, "
"write to binlog file \"%s\" fail, "
"errno: %d, error info: %s",
__LINE__, writer->file.name,
__LINE__, writer->file.name.str,
result, STRERROR(result));
return result;
}
@ -236,19 +304,23 @@ static int do_write_to_file(SFFileWriterInfo *writer,
if (fsync(writer->file.fd) != 0) {
result = errno != 0 ? errno : EIO;
logError("file: "__FILE__", line: %d, "
"fsync to binlog file \"%s\" fail, "
"errno: %d, error info: %s",
__LINE__, writer->file.name,
"fsync to binlog file \"%s\" fail, errno: %d, "
"error info: %s", __LINE__, writer->file.name.str,
result, STRERROR(result));
return result;
}
}
writer->file.size += len;
if (writer->write_done_callback.func != NULL) {
writer->write_done_callback.func(writer,
writer->write_done_callback.args);
}
return 0;
}
static int check_write_to_file(SFFileWriterInfo *writer,
int sf_file_writer_direct_write(SFFileWriterInfo *writer,
char *buff, const int len)
{
int result;
@ -267,7 +339,7 @@ static int check_write_to_file(SFFileWriterInfo *writer,
if (result != 0) {
logError("file: "__FILE__", line: %d, "
"open binlog file \"%s\" fail",
__LINE__, writer->file.name);
__LINE__, writer->file.name.str);
return result;
}
@ -279,12 +351,12 @@ int sf_file_writer_flush(SFFileWriterInfo *writer)
int result;
int len;
len = SF_BINLOG_BUFFER_LENGTH(writer->binlog_buffer);
len = SF_BINLOG_BUFFER_PRODUCER_DATA_LENGTH(writer->binlog_buffer);
if (len == 0) {
return 0;
}
if ((result=check_write_to_file(writer, writer->
if ((result=sf_file_writer_direct_write(writer, writer->
binlog_buffer.buff, len)) == 0)
{
if (writer->flags & SF_FILE_WRITER_FLAGS_WANT_DONE_VERSION) {
@ -292,10 +364,30 @@ int sf_file_writer_flush(SFFileWriterInfo *writer)
}
}
writer->binlog_buffer.end = writer->binlog_buffer.buff;
writer->binlog_buffer.data_end = writer->binlog_buffer.buff;
return result;
}
int sf_file_writer_fsync(SFFileWriterInfo *writer)
{
int result;
if ((result=sf_file_writer_flush(writer)) != 0) {
return result;
}
if (fsync(writer->file.fd) == 0) {
return 0;
} else {
result = errno != 0 ? errno : EIO;
logError("file: "__FILE__", line: %d, "
"fsync to binlog file \"%s\" fail, errno: %d, "
"error info: %s", __LINE__, writer->file.name.str,
result, STRERROR(result));
return result;
}
}
int sf_file_writer_get_indexes(SFFileWriterInfo *writer,
int *start_index, int *last_index)
{
@ -324,14 +416,14 @@ int sf_file_writer_deal_versioned_buffer(SFFileWriterInfo *writer,
int result;
if (buffer->length >= writer->binlog_buffer.size / 4) {
if (SF_BINLOG_BUFFER_LENGTH(writer->binlog_buffer) > 0) {
if (SF_BINLOG_BUFFER_PRODUCER_DATA_LENGTH(writer->binlog_buffer) > 0) {
if ((result=sf_file_writer_flush(writer)) != 0) {
return result;
}
}
if ((result=check_write_to_file(writer, buffer->buff,
buffer->length)) == 0)
if ((result=sf_file_writer_direct_write(writer, buffer->
buff, buffer->length)) == 0)
{
if (writer->flags & SF_FILE_WRITER_FLAGS_WANT_DONE_VERSION) {
writer->last_versions.pending = version;
@ -342,13 +434,13 @@ int sf_file_writer_deal_versioned_buffer(SFFileWriterInfo *writer,
}
if (writer->cfg.file_rotate_size > 0 && writer->file.size +
SF_BINLOG_BUFFER_LENGTH(writer->binlog_buffer) +
SF_BINLOG_BUFFER_PRODUCER_DATA_LENGTH(writer->binlog_buffer) +
buffer->length > writer->cfg.file_rotate_size)
{
if ((result=sf_file_writer_flush(writer)) != 0) {
return result;
}
} else if (writer->binlog_buffer.size - SF_BINLOG_BUFFER_LENGTH(
} else if (SF_BINLOG_BUFFER_PRODUCER_BUFF_REMAIN(
writer->binlog_buffer) < buffer->length)
{
if ((result=sf_file_writer_flush(writer)) != 0) {
@ -359,16 +451,40 @@ int sf_file_writer_deal_versioned_buffer(SFFileWriterInfo *writer,
if (writer->flags & SF_FILE_WRITER_FLAGS_WANT_DONE_VERSION) {
writer->last_versions.pending = version;
}
memcpy(writer->binlog_buffer.end, buffer->buff, buffer->length);
writer->binlog_buffer.end += buffer->length;
memcpy(writer->binlog_buffer.data_end, buffer->buff, buffer->length);
writer->binlog_buffer.data_end += buffer->length;
return 0;
}
int sf_file_writer_save_buffer_ex(SFFileWriterInfo *writer,
const int length, const bool flush)
{
int result;
if (writer->cfg.file_rotate_size > 0 && writer->file.size +
SF_BINLOG_BUFFER_PRODUCER_DATA_LENGTH(writer->binlog_buffer) +
length > writer->cfg.file_rotate_size)
{
if ((result=sf_file_writer_flush(writer)) != 0) {
return result;
}
}
writer->binlog_buffer.data_end += length;
if (flush || SF_BINLOG_BUFFER_PRODUCER_BUFF_REMAIN(writer->
binlog_buffer) < writer->cfg.max_record_size)
{
return sf_file_writer_flush(writer);
} else {
return 0;
}
}
int sf_file_writer_init(SFFileWriterInfo *writer, const char *data_path,
const char *subdir_name, const char *file_prefix,
const int buffer_size, const int64_t file_rotate_size,
const bool call_fsync)
const int max_record_size, const int buffer_size,
const int64_t file_rotate_size, const bool call_fsync)
{
int result;
int path_len;
@ -379,17 +495,18 @@ int sf_file_writer_init(SFFileWriterInfo *writer, const char *data_path,
writer->last_versions.pending = 0;
writer->last_versions.done = 0;
writer->flags = 0;
sf_file_writer_set_write_done_callback(writer, NULL, NULL);
if ((result=sf_binlog_buffer_init(&writer->
binlog_buffer, buffer_size)) != 0)
{
return result;
}
writer->cfg.max_record_size = max_record_size;
writer->cfg.call_fsync = call_fsync;
writer->cfg.file_rotate_size = file_rotate_size;
writer->cfg.data_path = data_path;
path_len = snprintf(filepath, sizeof(filepath),
"%s/%s", data_path, subdir_name);
path_len = fc_combine_full_filepath(data_path, subdir_name, filepath);
if ((result=fc_check_mkdir_ex(filepath, 0775, &create)) != 0) {
return result;
}
@ -398,14 +515,11 @@ int sf_file_writer_init(SFFileWriterInfo *writer, const char *data_path,
}
writer->file.fd = -1;
snprintf(writer->cfg.subdir_name,
sizeof(writer->cfg.subdir_name),
"%s", subdir_name);
snprintf(writer->cfg.file_prefix,
sizeof(writer->cfg.file_prefix),
"%s", file_prefix);
writer->file.name = (char *)fc_malloc(path_len + 32);
if (writer->file.name == NULL) {
fc_safe_strcpy(writer->cfg.subdir_name, subdir_name);
fc_safe_strcpy(writer->cfg.file_prefix, file_prefix);
writer->file.name.size = path_len + 32;
writer->file.name.str = (char *)fc_malloc(writer->file.name.size);
if (writer->file.name.str == NULL) {
return ENOMEM;
}
@ -426,9 +540,9 @@ void sf_file_writer_destroy(SFFileWriterInfo *writer)
close(writer->file.fd);
writer->file.fd = -1;
}
if (writer->file.name != NULL) {
free(writer->file.name);
writer->file.name = NULL;
if (writer->file.name.str != NULL) {
free(writer->file.name.str);
writer->file.name.str = NULL;
}
sf_binlog_buffer_destroy(&writer->binlog_buffer);
}
@ -572,3 +686,22 @@ int sf_file_writer_get_last_lines(const char *data_path,
return 0;
}
int sf_file_writer_get_last_line(const char *data_path,
const char *subdir_name, char *buff,
const int buff_size, int *length)
{
int result;
int last_index;
int count = 1;
if ((result=sf_file_writer_get_binlog_last_index(data_path,
subdir_name, &last_index)) != 0)
{
*length = 0;
return result;
}
return sf_file_writer_get_last_lines(data_path, subdir_name,
last_index, buff, buff_size, &count, length);
}

View File

@ -24,20 +24,24 @@
#define SF_FILE_WRITER_FLAGS_WANT_DONE_VERSION 1
#define SF_BINLOG_SUBDIR_NAME_SIZE 128
#define SF_BINLOG_FILE_PREFIX_SIZE 64
#define SF_BINLOG_FILE_PREFIX_STR_SIZE 64
#define SF_BINLOG_DEFAULT_ROTATE_SIZE (1024 * 1024 * 1024)
#define SF_BINLOG_NEVER_ROTATE_FILE 0
#define SF_BINLOG_FILE_PREFIX "binlog"
#define SF_BINLOG_FILE_EXT_FMT ".%06d"
#define SF_BINLOG_FILE_PREFIX_STR "binlog"
#define SF_BINLOG_FILE_PREFIX_LEN (sizeof(SF_BINLOG_FILE_PREFIX_STR) - 1)
#define SF_BINLOG_FILE_EXT_LEN 6
#define SF_BINLOG_FILE_EXT_FMT ".%0"FC_MACRO_TOSTRING(SF_BINLOG_FILE_EXT_LEN)"d"
#define SF_BINLOG_BUFFER_LENGTH(buffer) ((buffer).end - (buffer).buff)
#define SF_BINLOG_BUFFER_REMAIN(buffer) ((buffer).end - (buffer).current)
struct sf_file_writer_info;
typedef void (*sf_file_write_done_callback)(
struct sf_file_writer_info *writer, void *args);
typedef struct sf_file_writer_info {
struct {
const char *data_path;
char subdir_name[SF_BINLOG_SUBDIR_NAME_SIZE];
char file_prefix[SF_BINLOG_FILE_PREFIX_SIZE];
char file_prefix[SF_BINLOG_FILE_PREFIX_STR_SIZE];
int64_t file_rotate_size;
int max_record_size;
bool call_fsync;
@ -51,8 +55,11 @@ typedef struct sf_file_writer_info {
struct {
int fd;
int64_t size;
char *name;
int64_t size; //file size
struct {
char *str;
int size;
} name;
} file;
int64_t total_count;
@ -63,6 +70,12 @@ typedef struct sf_file_writer_info {
int64_t pending;
volatile int64_t done;
} last_versions;
struct {
sf_file_write_done_callback func;
void *args;
} write_done_callback;
} SFFileWriterInfo;
#ifdef __cplusplus
@ -71,11 +84,14 @@ extern "C" {
int sf_file_writer_init(SFFileWriterInfo *writer, const char *data_path,
const char *subdir_name, const char *file_prefix,
const int buffer_size, const int64_t file_rotate_size,
const bool call_fsync);
const int max_record_size, const int buffer_size,
const int64_t file_rotate_size, const bool call_fsync);
void sf_file_writer_destroy(SFFileWriterInfo *writer);
int sf_file_writer_direct_write(SFFileWriterInfo *writer,
char *buff, const int len);
int sf_file_writer_deal_versioned_buffer(SFFileWriterInfo *writer,
BufferInfo *buffer, const int64_t version);
@ -84,12 +100,51 @@ int sf_file_writer_deal_versioned_buffer(SFFileWriterInfo *writer,
int sf_file_writer_flush(SFFileWriterInfo *writer);
int sf_file_writer_fsync(SFFileWriterInfo *writer);
#define SF_FILE_WRITER_DATA_END_BUFF(writer) (writer)->binlog_buffer.data_end
#define SF_FILE_WRITER_CURRENT_DATA_VERSION(writer) \
(writer)->last_versions.pending
#define SF_FILE_WRITER_NEXT_DATA_VERSION(writer) \
++((writer)->last_versions.pending)
int sf_file_writer_save_buffer_ex(SFFileWriterInfo *writer,
const int length, const bool flush);
static inline int sf_file_writer_save_buffer(
SFFileWriterInfo *writer, const int length)
{
const bool flush = false;
return sf_file_writer_save_buffer_ex(writer, length, flush);
}
static inline int sf_file_writer_flush_buffer(
SFFileWriterInfo *writer, const int length)
{
const bool flush = true;
return sf_file_writer_save_buffer_ex(writer, length, flush);
}
static inline void sf_file_writer_set_flags(
SFFileWriterInfo *writer, const short flags)
{
writer->flags = flags;
}
static inline void sf_file_writer_set_call_fsync(
SFFileWriterInfo *writer, const bool call_fsync)
{
writer->cfg.call_fsync = call_fsync;
}
static inline void sf_file_writer_set_write_done_callback (
SFFileWriterInfo *writer, sf_file_write_done_callback callback,
void *args)
{
writer->write_done_callback.func = callback;
writer->write_done_callback.args = args;
}
static inline int64_t sf_file_writer_get_last_version_ex(
SFFileWriterInfo *writer, const int log_level)
{
@ -172,7 +227,8 @@ static inline const char *sf_file_writer_get_filepath(
const char *data_path, const char *subdir_name,
char *filepath, const int size)
{
snprintf(filepath, size, "%s/%s", data_path, subdir_name);
fc_get_full_filepath_ex(data_path, strlen(data_path),
subdir_name, strlen(subdir_name), filepath, size);
return filepath;
}
@ -181,15 +237,41 @@ static inline const char *sf_file_writer_get_filename_ex(
const char *file_prefix, const int binlog_index,
char *filename, const int size)
{
snprintf(filename, size, "%s/%s/%s"SF_BINLOG_FILE_EXT_FMT,
data_path, subdir_name, file_prefix, binlog_index);
char *p;
int data_path_len;
int subdir_name_len;
int file_prefix_len;
data_path_len = strlen(data_path);
subdir_name_len = strlen(subdir_name);
file_prefix_len = strlen(file_prefix);
if (data_path_len + subdir_name_len + file_prefix_len +
4 + SF_BINLOG_FILE_EXT_LEN >= size)
{
snprintf(filename, size, "%s/%s/%s"SF_BINLOG_FILE_EXT_FMT,
data_path, subdir_name, file_prefix, binlog_index);
return filename;
}
p = filename;
memcpy(p, data_path, data_path_len);
p += data_path_len;
*p++ = '/';
memcpy(p, subdir_name, subdir_name_len);
p += subdir_name_len;
*p++ = '/';
memcpy(p, file_prefix, file_prefix_len);
p += file_prefix_len;
*p++ = '.';
fc_ltostr_ex(binlog_index, p, SF_BINLOG_FILE_EXT_LEN);
return filename;
}
#define sf_file_writer_get_filename(data_path, subdir_name, \
binlog_index, filename, size) \
sf_file_writer_get_filename_ex(data_path, subdir_name, \
SF_BINLOG_FILE_PREFIX, binlog_index, filename, size)
SF_BINLOG_FILE_PREFIX_STR, binlog_index, filename, size)
const char *sf_file_writer_get_index_filename(const char *data_path,
const char *subdir_name, char *filename, const int size);
@ -200,10 +282,40 @@ int sf_file_writer_set_binlog_start_index(SFFileWriterInfo *writer,
int sf_file_writer_set_binlog_write_index(SFFileWriterInfo *writer,
const int last_index);
static inline int sf_file_writer_rotate_file(SFFileWriterInfo *writer)
{
int last_index;
last_index = sf_file_writer_get_current_write_index(writer);
return sf_file_writer_set_binlog_write_index(writer, last_index + 1);
}
int sf_file_writer_get_last_lines(const char *data_path,
const char *subdir_name, const int current_write_index,
char *buff, const int buff_size, int *count, int *length);
static inline int sf_file_writer_get_last_line_ex(const char *data_path,
const char *subdir_name, const int current_write_index,
char *buff, const int buff_size, int *length)
{
int count = 1;
return sf_file_writer_get_last_lines(data_path, subdir_name,
current_write_index, buff, buff_size, &count, length);
}
int sf_file_writer_get_last_line(const char *data_path,
const char *subdir_name, char *buff,
const int buff_size, int *length);
int sf_file_writer_write_to_binlog_index_file_ex(const char *data_path,
const char *subdir_name, const char *file_prefix,
const int start_index, const int last_index,
const int compress_index);
#define sf_file_writer_write_to_binlog_index_file(data_path, \
subdir_name, start_index, last_index) \
sf_file_writer_write_to_binlog_index_file_ex(data_path, subdir_name, \
SF_BINLOG_FILE_PREFIX_STR, start_index, last_index, 0)
#ifdef __cplusplus
}
#endif

View File

@ -38,7 +38,7 @@ int sf_connect_to_server(const char *ip_addr, const int port, int *sock)
if(*sock < 0) {
return errno != 0 ? errno : ENOMEM;
}
tcpsetserveropt(*sock, g_sf_global_vars.network_timeout);
tcpsetserveropt(*sock, g_sf_global_vars.net_buffer_cfg.network_timeout);
if ((result=tcpsetnonblockopt(*sock)) != 0) {
close(*sock);
@ -47,8 +47,8 @@ int sf_connect_to_server(const char *ip_addr, const int port, int *sock)
}
FC_SET_CLOEXEC(*sock);
if ((result=connectserverbyip_nb(*sock, ip_addr, port,
g_sf_global_vars.connect_timeout)) != 0)
if ((result=connectserverbyip_nb(*sock, ip_addr, port, g_sf_global_vars.
net_buffer_cfg.connect_timeout)) != 0)
{
close(*sock);
*sock = -1;

View File

@ -56,7 +56,8 @@ static inline int sf_binlog_buffer_init(SFBinlogBuffer *buffer, const int size)
return ENOMEM;
}
buffer->current = buffer->end = buffer->buff;
buffer->current = buffer->data_end = buffer->buff;
buffer->buff_end = buffer->buff + size;
buffer->size = size;
return 0;
}
@ -65,7 +66,8 @@ static inline void sf_binlog_buffer_destroy(SFBinlogBuffer *buffer)
{
if (buffer->buff != NULL) {
free(buffer->buff);
buffer->current = buffer->end = buffer->buff = NULL;
buffer->current = buffer->buff = NULL;
buffer->data_end = buffer->buff_end = NULL;
buffer->size = 0;
}
}

View File

@ -37,102 +37,106 @@
#include "sf_global.h"
SFGlobalVariables g_sf_global_vars = {
SF_DEFAULT_CONNECT_TIMEOUT, SF_DEFAULT_NETWORK_TIMEOUT,
{{'/', 't', 'm', 'p', '\0'}, false},
true, true, false, DEFAULT_MAX_CONNECTONS,
true, true, false, { SF_DEFAULT_CONNECT_TIMEOUT,
SF_DEFAULT_NETWORK_TIMEOUT, DEFAULT_MAX_CONNECTONS,
SF_DEF_MAX_PACKAGE_SIZE, SF_DEF_MIN_BUFF_SIZE,
SF_DEF_MAX_BUFF_SIZE, 0, SF_DEF_THREAD_STACK_SIZE, 0,
SF_DEF_MAX_BUFF_SIZE}, 0, SF_DEF_THREAD_STACK_SIZE, 0,
{false, 0, 0, {'\0'}, {'\0'}},
{SF_DEF_SYNC_LOG_BUFF_INTERVAL, false},
{0, 0}, NULL, {NULL, 0}
};
SFContext g_sf_context = {{'\0'}, NULL, 0, sf_address_family_auto,
{{AF_UNSPEC, {{true, fc_comm_type_sock}, {false, fc_comm_type_rdma}}},
{AF_UNSPEC, {{true, fc_comm_type_sock}, {false, fc_comm_type_rdma}}}},
1, DEFAULT_WORK_THREADS, 0, true, true, true, {false, 0, 0},
{sf_task_finish_clean_up}
SFContext g_sf_context = {{'\0'}, NULL, 0, false, false, false,
sf_address_family_auto, {{AF_UNSPEC, {{true, fc_comm_type_sock},
{false, fc_comm_type_rdma}}},
{AF_UNSPEC, {{true, fc_comm_type_sock}, {false, fc_comm_type_rdma}}}},
{DEFAULT_MAX_CONNECTONS, SF_DEF_MAX_PACKAGE_SIZE, SF_DEF_MIN_BUFF_SIZE,
SF_DEF_MAX_BUFF_SIZE}, 1, DEFAULT_WORK_THREADS, 0, true, true,
{false, 0, 0}, {sf_task_finish_clean_up}
};
static int load_network_parameters(IniFullContext *ini_ctx,
const char *max_pkg_size_item_nm, const int fixed_buff_size,
const int task_buffer_extra_size)
const char *max_pkg_size_item_nm, const int max_pkg_size_min_value,
const int fixed_buff_size, const int task_buffer_extra_size,
SFNetBufferConfig *net_buffer_cfg)
{
int result;
int padding_buff_size;
char *pMinBuffSize;
char *pMaxBuffSize;
g_sf_global_vars.connect_timeout = iniGetIntValueEx(ini_ctx->
net_buffer_cfg->connect_timeout = iniGetIntValueEx(ini_ctx->
section_name, "connect_timeout", ini_ctx->context,
SF_DEFAULT_CONNECT_TIMEOUT, true);
if (g_sf_global_vars.connect_timeout <= 0) {
g_sf_global_vars.connect_timeout = SF_DEFAULT_CONNECT_TIMEOUT;
if (net_buffer_cfg->connect_timeout <= 0) {
net_buffer_cfg->connect_timeout = SF_DEFAULT_CONNECT_TIMEOUT;
}
g_sf_global_vars.network_timeout = iniGetIntValueEx(ini_ctx->
net_buffer_cfg->network_timeout = iniGetIntValueEx(ini_ctx->
section_name, "network_timeout", ini_ctx->context,
SF_DEFAULT_NETWORK_TIMEOUT, true);
if (g_sf_global_vars.network_timeout <= 0) {
g_sf_global_vars.network_timeout = SF_DEFAULT_NETWORK_TIMEOUT;
if (net_buffer_cfg->network_timeout <= 0) {
net_buffer_cfg->network_timeout = SF_DEFAULT_NETWORK_TIMEOUT;
}
g_sf_global_vars.max_connections = iniGetIntValueEx(ini_ctx->section_name,
net_buffer_cfg->max_connections = iniGetIntValueEx(ini_ctx->section_name,
"max_connections", ini_ctx->context, DEFAULT_MAX_CONNECTONS, true);
if (g_sf_global_vars.max_connections <= 0) {
g_sf_global_vars.max_connections = DEFAULT_MAX_CONNECTONS;
}
if ((result=set_rlimit(RLIMIT_NOFILE, g_sf_global_vars.
max_connections)) != 0)
{
return result;
if (net_buffer_cfg->max_connections <= 0) {
net_buffer_cfg->max_connections = DEFAULT_MAX_CONNECTONS;
}
if (fixed_buff_size > 0) {
padding_buff_size = fixed_buff_size + task_buffer_extra_size;
g_sf_global_vars.min_buff_size = padding_buff_size;
g_sf_global_vars.max_buff_size = padding_buff_size;
g_sf_global_vars.max_pkg_size = padding_buff_size;
padding_buff_size = FC_MAX(fixed_buff_size, max_pkg_size_min_value) +
task_buffer_extra_size;
net_buffer_cfg->min_buff_size = padding_buff_size;
net_buffer_cfg->max_buff_size = padding_buff_size;
net_buffer_cfg->max_pkg_size = padding_buff_size;
return 0;
}
g_sf_global_vars.max_pkg_size = iniGetByteCorrectValueEx(ini_ctx,
net_buffer_cfg->max_pkg_size = iniGetByteCorrectValueEx(ini_ctx,
max_pkg_size_item_nm, SF_DEF_MAX_PACKAGE_SIZE, 1, 8192,
SF_MAX_NETWORK_BUFF_SIZE, true);
if (net_buffer_cfg->max_pkg_size < max_pkg_size_min_value) {
net_buffer_cfg->max_pkg_size = max_pkg_size_min_value;
}
if (task_buffer_extra_size > 0) {
net_buffer_cfg->max_pkg_size += task_buffer_extra_size;
}
pMinBuffSize = iniGetStrValueEx(ini_ctx->section_name,
"min_buff_size", ini_ctx->context, true);
pMaxBuffSize = iniGetStrValueEx(ini_ctx->section_name,
"max_buff_size", ini_ctx->context, true);
if (pMinBuffSize == NULL || pMaxBuffSize == NULL) {
g_sf_global_vars.min_buff_size = g_sf_global_vars.max_pkg_size;
g_sf_global_vars.max_buff_size = g_sf_global_vars.max_pkg_size;
net_buffer_cfg->min_buff_size = net_buffer_cfg->max_pkg_size;
net_buffer_cfg->max_buff_size = net_buffer_cfg->max_pkg_size;
} else {
g_sf_global_vars.min_buff_size = iniGetByteCorrectValueEx(ini_ctx,
net_buffer_cfg->min_buff_size = iniGetByteCorrectValueEx(ini_ctx,
"min_buff_size", SF_DEF_MIN_BUFF_SIZE, 1, 4096,
SF_MAX_NETWORK_BUFF_SIZE, true);
g_sf_global_vars.max_buff_size = iniGetByteCorrectValueEx(ini_ctx,
net_buffer_cfg->max_buff_size = iniGetByteCorrectValueEx(ini_ctx,
"max_buff_size", SF_DEF_MAX_BUFF_SIZE, 1, 8192,
SF_MAX_NETWORK_BUFF_SIZE, true);
if (g_sf_global_vars.max_buff_size < g_sf_global_vars.max_pkg_size) {
g_sf_global_vars.max_buff_size = g_sf_global_vars.max_pkg_size;
if (task_buffer_extra_size > 0) {
net_buffer_cfg->min_buff_size += task_buffer_extra_size;
net_buffer_cfg->max_buff_size += task_buffer_extra_size;
}
if (g_sf_global_vars.max_buff_size < g_sf_global_vars.min_buff_size) {
if (net_buffer_cfg->max_buff_size < net_buffer_cfg->max_pkg_size) {
net_buffer_cfg->max_buff_size = net_buffer_cfg->max_pkg_size;
} else if (net_buffer_cfg->max_pkg_size <
net_buffer_cfg->max_buff_size)
{
net_buffer_cfg->max_pkg_size = net_buffer_cfg->max_buff_size;
}
if (net_buffer_cfg->max_buff_size < net_buffer_cfg->min_buff_size) {
logWarning("file: "__FILE__", line: %d, "
"max_buff_size: %d < min_buff_size: %d, "
"set max_buff_size to min_buff_size", __LINE__,
g_sf_global_vars.max_buff_size,
g_sf_global_vars.min_buff_size);
g_sf_global_vars.max_buff_size = g_sf_global_vars.min_buff_size;
}
}
if (task_buffer_extra_size > 0) {
g_sf_global_vars.min_buff_size += task_buffer_extra_size;
g_sf_global_vars.max_buff_size += task_buffer_extra_size;
if (g_sf_global_vars.max_pkg_size < g_sf_global_vars.max_buff_size) {
g_sf_global_vars.max_pkg_size = g_sf_global_vars.max_buff_size;
net_buffer_cfg->max_buff_size,
net_buffer_cfg->min_buff_size);
net_buffer_cfg->max_buff_size = net_buffer_cfg->min_buff_size;
}
}
@ -217,9 +221,7 @@ int sf_load_slow_log_config_ex(IniFullContext *ini_ctx, LogContext *log_ctx,
if (filename_prefix == NULL || *filename_prefix == '\0') {
strcpy(slow_log_cfg->filename_prefix, "slow");
} else {
snprintf(slow_log_cfg->filename_prefix,
sizeof(slow_log_cfg->filename_prefix),
"%s", filename_prefix);
fc_safe_strcpy(slow_log_cfg->filename_prefix, filename_prefix);
}
return 0;
@ -247,6 +249,7 @@ int sf_get_base_path_from_conf_file(const char *config_filename)
}
}
SF_G_BASE_PATH_LEN = strlen(SF_G_BASE_PATH_STR);
SF_G_BASE_PATH_INITED = true;
return 0;
}
@ -268,6 +271,7 @@ int sf_load_global_base_path(IniFullContext *ini_ctx)
}
chopPath(SF_G_BASE_PATH_STR);
SF_G_BASE_PATH_LEN = strlen(SF_G_BASE_PATH_STR);
if (!fileExists(SF_G_BASE_PATH_STR)) {
if ((result=fc_check_mkdir_ex(SF_G_BASE_PATH_STR, 0775,
&SF_G_BASE_PATH_CREATED)) != 0)
@ -290,6 +294,7 @@ int sf_load_global_config_ex(const char *log_filename_prefix,
const char *max_pkg_size_item_nm, const int fixed_buff_size,
const int task_buffer_extra_size, const bool need_set_run_by)
{
const int max_pkg_size_min_value = 0;
int result;
const char *old_section_name;
char *pRunByGroup;
@ -305,7 +310,15 @@ int sf_load_global_config_ex(const char *log_filename_prefix,
tcp_set_quick_ack(g_sf_global_vars.tcp_quick_ack);
if (load_network_params) {
if ((result=load_network_parameters(ini_ctx, max_pkg_size_item_nm,
fixed_buff_size, task_buffer_extra_size)) != 0)
max_pkg_size_min_value, fixed_buff_size,
task_buffer_extra_size, &g_sf_global_vars.
net_buffer_cfg)) != 0)
{
return result;
}
if ((result=set_rlimit(RLIMIT_NOFILE, g_sf_global_vars.
net_buffer_cfg.max_connections)) != 0)
{
return result;
}
@ -317,9 +330,7 @@ int sf_load_global_config_ex(const char *log_filename_prefix,
*g_sf_global_vars.run_by.group = '\0';
}
else {
snprintf(g_sf_global_vars.run_by.group,
sizeof(g_sf_global_vars.run_by.group),
"%s", pRunByGroup);
fc_safe_strcpy(g_sf_global_vars.run_by.group, pRunByGroup);
}
if (*(g_sf_global_vars.run_by.group) == '\0') {
g_sf_global_vars.run_by.gid = getegid();
@ -344,9 +355,7 @@ int sf_load_global_config_ex(const char *log_filename_prefix,
*g_sf_global_vars.run_by.user = '\0';
}
else {
snprintf(g_sf_global_vars.run_by.user,
sizeof(g_sf_global_vars.run_by.user),
"%s", pRunByUser);
fc_safe_strcpy(g_sf_global_vars.run_by.user, pRunByUser);
}
if (*(g_sf_global_vars.run_by.user) == '\0') {
g_sf_global_vars.run_by.uid = geteuid();
@ -416,7 +425,8 @@ int sf_load_config_ex(const char *log_filename_prefix,
{
return result;
}
return sf_load_context_from_config_ex(&g_sf_context, config);
return sf_load_context_from_config_ex(&g_sf_context, config,
fixed_buff_size, task_buffer_extra_size);
}
#define API_PREFIX_NAME "fast_rdma_"
@ -434,7 +444,7 @@ int sf_load_config_ex(const char *log_filename_prefix,
#define LOAD_API(handler, fname) LOAD_API_EX(handler, server_, fname)
static int load_rdma_apis(SFNetworkHandler *handler)
static int load_rdma_apis(SFContext *sf_context, SFNetworkHandler *handler)
{
const char *library = "libfastrdma.so";
void *dlhandle;
@ -449,7 +459,11 @@ static int load_rdma_apis(SFNetworkHandler *handler)
LOAD_API(handler, get_connection_size);
LOAD_API(handler, init_connection);
LOAD_API(handler, alloc_pd);
if (sf_context->is_client) {
LOAD_API_EX(handler, client_, alloc_pd);
} else {
LOAD_API(handler, alloc_pd);
}
LOAD_API_EX(handler, , create_server);
LOAD_API_EX(handler, , close_server);
LOAD_API(handler, accept_connection);
@ -463,8 +477,8 @@ static int load_rdma_apis(SFNetworkHandler *handler)
return 0;
}
static int init_network_handler(SFNetworkHandler *handler,
SFAddressFamilyHandler *fh)
static int init_network_handler(SFContext *sf_context,
SFNetworkHandler *handler, SFAddressFamilyHandler *fh)
{
handler->fh = fh;
handler->inner.handler = handler;
@ -489,7 +503,7 @@ static int init_network_handler(SFNetworkHandler *handler,
} else {
handler->inner.id = NULL;
handler->outer.id = NULL;
return load_rdma_apis(handler);
return load_rdma_apis(sf_context, handler);
}
}
@ -508,7 +522,7 @@ static void set_bind_address(const char *bind_addr, char *ipv4_bind_addr,
return;
}
snprintf(new_bind_addr, sizeof(new_bind_addr), "%s", bind_addr);
fc_safe_strcpy(new_bind_addr, bind_addr);
count = splitEx(new_bind_addr, ',', cols, 2);
for (i=0; i<count; i++) {
ip_addr = cols[i];
@ -518,9 +532,13 @@ static void set_bind_address(const char *bind_addr, char *ipv4_bind_addr,
++ip_addr;
len -= 2;
}
snprintf(ipv6_bind_addr, addr_size, "%.*s", len, ip_addr);
if (len >= addr_size) {
len = addr_size - 1;
}
memcpy(ipv6_bind_addr, ip_addr, len);
*(ipv6_bind_addr + len) = '\0';
} else {
snprintf(ipv4_bind_addr, addr_size, "%s", ip_addr);
fc_strlcpy(ipv4_bind_addr, ip_addr, addr_size);
}
}
}
@ -639,7 +657,8 @@ static int load_address_family(SFContext *sf_context,
}
int sf_load_context_from_config_ex(SFContext *sf_context,
SFContextIniConfig *config)
SFContextIniConfig *config, const int fixed_buff_size,
const int task_buffer_extra_size)
{
SFAddressFamilyHandler *fh;
SFNetworkHandler *sock_handler;
@ -651,6 +670,10 @@ int sf_load_context_from_config_ex(SFContext *sf_context,
int inner_port;
int outer_port;
int port;
#if IOEVENT_USE_URING
bool global_use_send_zc;
bool use_send_zc;
#endif
int i;
int result;
@ -687,6 +710,17 @@ int sf_load_context_from_config_ex(SFContext *sf_context,
outer_port = config->default_outer_port;
}
#if IOEVENT_USE_URING
global_use_send_zc = iniGetBoolValue(NULL, "use_send_zc",
config->ini_ctx.context, true);
if (config->ini_ctx.section_name == NULL) {
use_send_zc = global_use_send_zc;
} else {
use_send_zc = iniGetBoolValue(config->ini_ctx.section_name,
"use_send_zc", config->ini_ctx.context, global_use_send_zc);
}
#endif
for (i=0; i<SF_ADDRESS_FAMILY_COUNT; i++) {
fh = sf_context->handlers + i;
fh->ctx = sf_context;
@ -710,7 +744,7 @@ int sf_load_context_from_config_ex(SFContext *sf_context,
if (!handler->enabled) {
continue;
}
if ((result=init_network_handler(handler, fh)) != 0) {
if ((result=init_network_handler(sf_context, handler, fh)) != 0) {
return result;
}
}
@ -729,9 +763,15 @@ int sf_load_context_from_config_ex(SFContext *sf_context,
rdma_handler->inner.enabled = sock_handler->inner.enabled;
rdma_handler->outer.port = sock_handler->outer.port;
rdma_handler->outer.enabled = sock_handler->outer.enabled;
}
#if IOEVENT_USE_URING
sf_context->use_io_uring = (config->comm_type == fc_comm_type_sock);
sf_context->use_send_zc = sf_context->use_io_uring ? use_send_zc : false;
#else
sf_context->use_io_uring = false;
#endif
sf_context->accept_threads = iniGetIntValue(
config->ini_ctx.section_name,
"accept_threads", config->ini_ctx.context, 1);
@ -764,6 +804,14 @@ int sf_load_context_from_config_ex(SFContext *sf_context,
return result;
}
if ((result=load_network_parameters(&config->ini_ctx, config->
max_pkg_size_item_name, config->max_pkg_size_min_value,
fixed_buff_size, task_buffer_extra_size,
&sf_context->net_buffer_cfg)) != 0)
{
return result;
}
return 0;
}
@ -839,8 +887,7 @@ static void combine_bind_addr(char *bind_addr, const char *ip_addr)
p = bind_addr + strlen(bind_addr);
*p++ = ',';
}
sprintf(p, "%s", ip_addr);
strcpy(p, ip_addr);
}
static const char *get_address_family_caption(
@ -909,6 +956,12 @@ void sf_context_config_to_string(const SFContext *sf_context,
", address_family=%s, accept_threads=%d, work_threads=%d",
get_address_family_caption(sf_context->address_family),
sf_context->accept_threads, sf_context->work_threads);
#if IOEVENT_USE_URING
len += snprintf(output + len, size - len, ", use_io_uring=%d, "
"use_send_zc=%d", sf_context->use_io_uring,
sf_context->use_send_zc);
#endif
}
void sf_log_config_to_string_ex(SFLogConfig *log_cfg, const char *caption,
@ -960,11 +1013,11 @@ void sf_global_config_to_string_ex(const char *max_pkg_size_item_nm,
int max_buff_size;
char pkg_buff[256];
max_pkg_size = g_sf_global_vars.max_pkg_size -
max_pkg_size = g_sf_global_vars.net_buffer_cfg.max_pkg_size -
g_sf_global_vars.task_buffer_extra_size;
min_buff_size = g_sf_global_vars.min_buff_size -
min_buff_size = g_sf_global_vars.net_buffer_cfg.min_buff_size -
g_sf_global_vars.task_buffer_extra_size;
max_buff_size = g_sf_global_vars.max_buff_size -
max_buff_size = g_sf_global_vars.net_buffer_cfg.max_buff_size -
g_sf_global_vars.task_buffer_extra_size;
if (min_buff_size == max_buff_size && max_pkg_size == max_buff_size) {
@ -979,18 +1032,27 @@ void sf_global_config_to_string_ex(const char *max_pkg_size_item_nm,
len = snprintf(output, size,
"base_path=%s, max_connections=%d, connect_timeout=%d, "
"network_timeout=%d, thread_stack_size=%d KB, "
"%s, tcp_quick_ack=%d, log_level=%s, "
"run_by_group=%s, run_by_user=%s, ", SF_G_BASE_PATH_STR,
g_sf_global_vars.max_connections,
g_sf_global_vars.connect_timeout,
g_sf_global_vars.network_timeout,
g_sf_global_vars.thread_stack_size / 1024,
pkg_buff, g_sf_global_vars.tcp_quick_ack,
"network_timeout=%d, thread_stack_size=%d KB, %s, ",
SF_G_BASE_PATH_STR,
g_sf_global_vars.net_buffer_cfg.max_connections,
g_sf_global_vars.net_buffer_cfg.connect_timeout,
g_sf_global_vars.net_buffer_cfg.network_timeout,
g_sf_global_vars.thread_stack_size / 1024, pkg_buff);
#if IOEVENT_USE_URING
len += snprintf(output + len, size - len, "use_io_uring=%d, "
"use_send_zc=%d, ", g_sf_context.use_io_uring,
g_sf_context.use_send_zc);
#endif
len += snprintf(output + len, size - len,
"tcp_quick_ack=%d, "
"log_level=%s, "
"run_by_group=%s, run_by_user=%s, ",
g_sf_global_vars.tcp_quick_ack,
log_get_level_caption(),
g_sf_global_vars.run_by.group,
g_sf_global_vars.run_by.user
);
g_sf_global_vars.run_by.user);
sf_log_config_to_string(&g_sf_global_vars.error_log,
"error-log", output + len, size - len);
@ -1002,11 +1064,16 @@ void sf_log_config_ex(const char *other_config)
char sz_context_config[128];
sf_global_config_to_string(sz_global_config, sizeof(sz_global_config));
sf_context_config_to_string(&g_sf_context,
sz_context_config, sizeof(sz_context_config));
logInfo("%s, %s%s%s",
sz_global_config, sz_context_config,
if (!g_sf_context.is_client) {
sf_context_config_to_string(&g_sf_context, sz_context_config,
sizeof(sz_context_config));
} else {
*sz_context_config = '\0';
}
logInfo("%s%s%s%s%s", sz_global_config, (*sz_context_config != '\0') ?
", " : "", sz_context_config,
(other_config != NULL && *other_config != '\0') ? ", " : "",
(other_config != NULL) ? other_config : ""
);
@ -1016,6 +1083,8 @@ int sf_load_data_path_config_ex(IniFullContext *ini_ctx,
const char *item_name, const char *default_value, string_t *path)
{
const char *data_path;
int data_path_len;
int path_size;
data_path = iniGetStrValue(ini_ctx->section_name,
item_name, ini_ctx->context);
@ -1031,20 +1100,23 @@ int sf_load_data_path_config_ex(IniFullContext *ini_ctx,
return EINVAL;
}
data_path_len = strlen(data_path);
if (*data_path == '/') {
path->len = strlen(data_path);
path->len = data_path_len;
path->str = fc_strdup1(data_path, path->len);
if (path->str == NULL) {
return ENOMEM;
}
} else {
path->len = strlen(SF_G_BASE_PATH_STR) + strlen(data_path) + 1;
path->str = (char *)fc_malloc(path->len + 1);
path_size = SF_G_BASE_PATH_LEN + data_path_len + 2;
path->str = (char *)fc_malloc(path_size);
if (path->str == NULL) {
return ENOMEM;
}
path->len = sprintf(path->str, "%s/%s",
SF_G_BASE_PATH_STR, data_path);
path->len = fc_get_full_filepath_ex(SF_G_BASE_PATH_STR,
SF_G_BASE_PATH_LEN, data_path, data_path_len,
path->str, path_size);
}
chopPath(path->str);
path->len = strlen(path->str);

View File

@ -30,10 +30,9 @@ typedef struct sf_connection_stat {
} SFConnectionStat;
typedef struct sf_global_variables {
int connect_timeout;
int network_timeout;
struct {
char str[MAX_PATH_SIZE];
int len;
bool inited;
bool created;
} base_path;
@ -41,10 +40,9 @@ typedef struct sf_global_variables {
volatile bool continue_flag;
bool tcp_quick_ack;
bool epoll_edge_trigger;
int max_connections;
int max_pkg_size;
int min_buff_size;
int max_buff_size;
SFNetBufferConfig net_buffer_cfg;
int task_buffer_extra_size;
int thread_stack_size;
@ -69,6 +67,7 @@ typedef struct sf_context_ini_config {
int default_inner_port;
int default_outer_port;
int default_work_threads;
int max_pkg_size_min_value;
FCCommunicationType comm_type;
const char *max_pkg_size_item_name;
} SFContextIniConfig;
@ -81,12 +80,13 @@ extern SFGlobalVariables g_sf_global_vars;
extern SFContext g_sf_context;
#define SF_G_BASE_PATH_STR g_sf_global_vars.base_path.str
#define SF_G_BASE_PATH_LEN g_sf_global_vars.base_path.len
#define SF_G_BASE_PATH_INITED g_sf_global_vars.base_path.inited
#define SF_G_BASE_PATH_CREATED g_sf_global_vars.base_path.created
#define SF_G_CONTINUE_FLAG g_sf_global_vars.continue_flag
#define SF_G_CONNECT_TIMEOUT g_sf_global_vars.connect_timeout
#define SF_G_NETWORK_TIMEOUT g_sf_global_vars.network_timeout
#define SF_G_MAX_CONNECTIONS g_sf_global_vars.max_connections
#define SF_G_CONNECT_TIMEOUT g_sf_global_vars.net_buffer_cfg.connect_timeout
#define SF_G_NETWORK_TIMEOUT g_sf_global_vars.net_buffer_cfg.network_timeout
#define SF_G_MAX_CONNECTIONS g_sf_global_vars.net_buffer_cfg.max_connections
#define SF_G_THREAD_STACK_SIZE g_sf_global_vars.thread_stack_size
#define SF_G_UP_TIME g_sf_global_vars.up_time
@ -175,9 +175,9 @@ extern SFContext g_sf_context;
#define SF_FCHOWN_TO_RUNBY_RETURN_ON_ERROR(fd, path) \
SF_FCHOWN_RETURN_ON_ERROR(fd, path, geteuid(), getegid())
#define SF_SET_CONTEXT_INI_CONFIG_EX(config, the_comm_type, filename, \
pIniContext, section_name, def_inner_port, def_outer_port, \
def_work_threads, max_pkg_size_item_nm) \
#define SF_SET_CONTEXT_INI_CONFIG_EX(config, the_comm_type, filename, \
pIniContext, section_name, def_inner_port, def_outer_port, \
def_work_threads, max_pkg_size_item_nm, max_pkg_size_min_val) \
do { \
FAST_INI_SET_FULL_CTX_EX(config.ini_ctx, filename, \
section_name, pIniContext); \
@ -186,6 +186,7 @@ extern SFContext g_sf_context;
config.default_outer_port = def_outer_port; \
config.default_work_threads = def_work_threads; \
config.max_pkg_size_item_name = max_pkg_size_item_nm; \
config.max_pkg_size_min_value = max_pkg_size_min_val; \
} while (0)
#define SF_SET_CONTEXT_INI_CONFIG(config, the_comm_type, \
@ -193,7 +194,7 @@ extern SFContext g_sf_context;
def_outer_port, def_work_threads) \
SF_SET_CONTEXT_INI_CONFIG_EX(config, the_comm_type, filename, \
pIniContext, section_name, def_inner_port, def_outer_port, \
def_work_threads, "max_pkg_size")
def_work_threads, "max_pkg_size", 0)
int sf_load_global_config_ex(const char *log_filename_prefix,
IniFullContext *ini_ctx, const bool load_network_params,
@ -236,20 +237,23 @@ static inline int sf_load_config(const char *log_filename_prefix,
}
int sf_load_context_from_config_ex(SFContext *sf_context,
SFContextIniConfig *config);
SFContextIniConfig *config, const int fixed_buff_size,
const int task_buffer_extra_size);
static inline int sf_load_context_from_config(SFContext *sf_context,
const FCCommunicationType comm_type,
const char *filename, IniContext *pIniContext,
const char *section_name, const int default_inner_port,
const int default_outer_port)
const int default_outer_port, const int fixed_buff_size,
const int task_buffer_extra_size)
{
SFContextIniConfig config;
SF_SET_CONTEXT_INI_CONFIG(config, comm_type, filename, pIniContext,
section_name, default_inner_port, default_outer_port,
DEFAULT_WORK_THREADS);
return sf_load_context_from_config_ex(sf_context, &config);
return sf_load_context_from_config_ex(sf_context, &config,
fixed_buff_size, task_buffer_extra_size);
}
int sf_alloc_rdma_pd(SFContext *sf_context,
@ -315,8 +319,8 @@ static inline void sf_set_global_base_path(const char *base_path)
string_t path_string;
FC_SET_STRING(path_string, (char *)base_path);
normalize_path(NULL, &path_string, SF_G_BASE_PATH_STR,
sizeof(SF_G_BASE_PATH_STR));
SF_G_BASE_PATH_LEN = normalize_path(NULL, &path_string,
SF_G_BASE_PATH_STR, sizeof(SF_G_BASE_PATH_STR));
SF_G_BASE_PATH_INITED = true;
}

File diff suppressed because it is too large Load Diff

View File

@ -27,7 +27,8 @@
#include "sf_types.h"
#include "sf_global.h"
#define SF_CTX (task->handler->fh->ctx)
#define SF_CTX (task->handler->fh->ctx)
#define SF_NET_BUFFER_CFG SF_CTX->net_buffer_cfg
#ifdef __cplusplus
extern "C" {
@ -67,15 +68,6 @@ static inline void sf_set_connect_done_callback_ex(SFContext *sf_context,
sf_set_connect_done_callback_ex(&g_sf_context, done_callback)
static inline void sf_set_remove_from_ready_list_ex(
SFContext *sf_context, const bool enabled)
{
sf_context->remove_from_ready_list = enabled;
}
#define sf_set_remove_from_ready_list(enabled) \
sf_set_remove_from_ready_list_ex(&g_sf_context, enabled);
static inline TaskCleanUpCallback sf_get_task_cleanup_callback_ex(
SFContext *sf_context)
{
@ -98,10 +90,9 @@ static inline void sf_nio_reset_task_length(struct fast_task_info *task)
}
}
void sf_recv_notify_read(int sock, short event, void *arg);
void sf_socket_close_connection(struct fast_task_info *task);
void sf_recv_notify_read(int sock, const int event, void *arg);
int sf_send_add_event(struct fast_task_info *task);
int sf_client_sock_write(int sock, short event, void *arg);
int sf_client_sock_read(int sock, short event, void *arg);
void sf_task_finish_clean_up(struct fast_task_info *task);
@ -128,12 +119,12 @@ static inline int sf_set_body_length(struct fast_task_info *task)
}
task->recv.ptr->length += SF_CTX->header_size;
if (task->recv.ptr->length > g_sf_global_vars.max_pkg_size) {
if (task->recv.ptr->length > SF_NET_BUFFER_CFG.max_pkg_size) {
logError("file: "__FILE__", line: %d, "
"client ip: %s, pkg length: %d > "
"max pkg size: %d", __LINE__,
task->client_ip, task->recv.ptr->length,
g_sf_global_vars.max_pkg_size);
SF_NET_BUFFER_CFG.max_pkg_size);
return -1;
}
@ -157,11 +148,6 @@ static inline int sf_nio_forward_request(struct fast_task_info *task,
return sf_nio_notify(task, SF_NIO_STAGE_FORWARDED);
}
static inline bool sf_client_sock_in_read_stage(struct fast_task_info *task)
{
return (task->event.callback == (IOEventCallback)sf_client_sock_read);
}
static inline void sf_nio_add_to_deleted_list(struct nio_thread_data
*thread_data, struct fast_task_info *task)
{
@ -172,6 +158,8 @@ static inline void sf_nio_add_to_deleted_list(struct nio_thread_data
}
}
bool sf_client_sock_in_read_stage(struct fast_task_info *task);
#ifdef __cplusplus
}
#endif

View File

@ -106,7 +106,7 @@ void sf_ordered_writer_finish(SFOrderedWriterContext *ctx)
{
int count;
if (ctx->writer.fw.file.name != NULL) {
if (ctx->writer.fw.file.name.str != NULL) {
fc_queue_terminate(&ctx->thread.queues.version);
count = 0;
@ -120,8 +120,8 @@ void sf_ordered_writer_finish(SFOrderedWriterContext *ctx)
__LINE__, ctx->writer.fw.cfg.subdir_name);
}
free(ctx->writer.fw.file.name);
ctx->writer.fw.file.name = NULL;
free(ctx->writer.fw.file.name.str);
ctx->writer.fw.file.name.str = NULL;
}
if (ctx->writer.fw.file.fd >= 0) {
@ -142,8 +142,7 @@ static void *binlog_writer_func(void *arg)
#ifdef OS_LINUX
{
char thread_name[64];
snprintf(thread_name, sizeof(thread_name),
"%s-writer", thread->name);
fc_combine_two_strings(thread->name, "writer", '-', thread_name);
prctl(PR_SET_NAME, thread_name);
}
#endif
@ -203,7 +202,7 @@ static int sf_ordered_writer_init_thread(SFOrderedWriterContext *context,
thread = &context->thread;
writer = &context->writer;
snprintf(thread->name, sizeof(thread->name), "%s", name);
fc_safe_strcpy(thread->name, name);
writer->fw.cfg.max_record_size = max_record_size;
writer->thread = thread;
@ -250,8 +249,8 @@ int sf_ordered_writer_init_ex(SFOrderedWriterContext *context,
{
int result;
if ((result=sf_file_writer_init(&context->writer.fw, data_path,
subdir_name, file_prefix, buffer_size,
file_rotate_size, call_fsync)) != 0)
subdir_name, file_prefix, max_record_size,
buffer_size, file_rotate_size, call_fsync)) != 0)
{
return result;
}

View File

@ -71,7 +71,7 @@ int sf_ordered_writer_init_ex(SFOrderedWriterContext *context,
#define sf_ordered_writer_init(context, data_path, \
subdir_name, buffer_size, max_record_size) \
sf_ordered_writer_init_ex(context, data_path, subdir_name, \
SF_BINLOG_FILE_PREFIX, buffer_size, max_record_size, \
SF_BINLOG_FILE_PREFIX_STR, buffer_size, max_record_size, \
SF_BINLOG_DEFAULT_ROTATE_SIZE, true)
#define sf_ordered_writer_set_flags(ctx, flags) \

View File

@ -14,8 +14,6 @@
*/
#include <errno.h>
#include "fastcommon/shared_func.h"
#include "sf_util.h"
#include "sf_nio.h"
#include "sf_proto.h"
@ -29,14 +27,16 @@ static int64_t log_slower_than_us = 0;
int sf_proto_set_body_length(struct fast_task_info *task)
{
SFCommonProtoHeader *header;
char formatted_ip[FORMATTED_IP_SIZE];
header = (SFCommonProtoHeader *)task->recv.ptr->data;
if (!SF_PROTO_CHECK_MAGIC(header->magic)) {
format_ip_address(task->client_ip, formatted_ip);
logError("file: "__FILE__", line: %d, "
"%s peer %s:%u, magic "SF_PROTO_MAGIC_FORMAT" is invalid, "
"expect: "SF_PROTO_MAGIC_FORMAT", cmd: %d, body length: %d",
__LINE__, (task->handler != NULL ? task->handler->fh->ctx->
name : ""), task->client_ip, task->port,
name : ""), formatted_ip, task->port,
SF_PROTO_MAGIC_PARAMS(header->magic),
SF_PROTO_MAGIC_EXPECT_PARAMS, header->cmd,
buff2int(header->body_len));
@ -456,14 +456,17 @@ const char *sf_get_cmd_caption(const int cmd)
int sf_proto_deal_ack(struct fast_task_info *task,
SFRequestInfo *request, SFResponseInfo *response)
{
char formatted_ip[FORMATTED_IP_SIZE];
if (request->header.status != 0) {
if (request->header.body_len > 0) {
int remain_size;
int len;
format_ip_address(task->client_ip, formatted_ip);
response->error.length = sprintf(response->error.message,
"message from peer %s:%u => ",
task->client_ip, task->port);
formatted_ip, task->port);
remain_size = sizeof(response->error.message) -
response->error.length;
if (request->header.body_len >= remain_size) {
@ -525,6 +528,7 @@ int sf_proto_get_group_servers(ConnectionInfo *conn,
char out_buff[sizeof(SFCommonProtoHeader) +
sizeof(SFProtoGetGroupServersReq)];
char in_buff[1024];
char formatted_ip[FORMATTED_IP_SIZE];
SFCommonProtoHeader *header;
SFProtoGetGroupServersReq *req;
SFProtoGetGroupServersRespBodyHeader *body_header;
@ -552,9 +556,10 @@ int sf_proto_get_group_servers(ConnectionInfo *conn,
}
if (body_len < sizeof(SFProtoGetGroupServersRespBodyHeader)) {
format_ip_address(conn->ip_addr, formatted_ip);
logError("file: "__FILE__", line: %d, "
"server %s:%d response body length: %d < %d",
__LINE__, conn->ip_addr, conn->port, body_len,
"server %s:%u response body length: %d < %d",
__LINE__, formatted_ip, conn->port, body_len,
(int)sizeof(SFProtoGetGroupServersRespBodyHeader));
return EINVAL;
}
@ -562,15 +567,17 @@ int sf_proto_get_group_servers(ConnectionInfo *conn,
body_header = (SFProtoGetGroupServersRespBodyHeader *)in_buff;
count = buff2int(body_header->count);
if (count <= 0) {
format_ip_address(conn->ip_addr, formatted_ip);
logError("file: "__FILE__", line: %d, "
"server %s:%d response server count: %d <= 0",
__LINE__, conn->ip_addr, conn->port, count);
"server %s:%u response server count: %d <= 0",
__LINE__, formatted_ip, conn->port, count);
return EINVAL;
}
if (count > sarray->alloc) {
format_ip_address(conn->ip_addr, formatted_ip);
logError("file: "__FILE__", line: %d, "
"server %s:%d response server count: %d is too large, "
"exceeds %d", __LINE__, conn->ip_addr, conn->port,
"server %s:%u response server count: %d is too large, "
"exceeds %d", __LINE__, formatted_ip, conn->port,
count, sarray->alloc);
return EOVERFLOW;
}
@ -630,6 +637,7 @@ int sf_proto_deal_task_done(struct fast_task_info *task,
int r;
int64_t time_used;
int log_level;
char formatted_ip[FORMATTED_IP_SIZE];
char time_buff[32];
if (ctx->log_level != LOG_NOTHING && ctx->response.error.length > 0) {
@ -637,7 +645,8 @@ int sf_proto_deal_task_done(struct fast_task_info *task,
"file: "__FILE__", line: %d, %s "
"peer %s:%u, cmd: %d (%s), req body length: %d, "
"resp status: %d, %s", __LINE__, service_name,
task->client_ip, task->port, ctx->request.header.cmd,
format_ip_address(task->client_ip, formatted_ip),
task->port, ctx->request.header.cmd,
GET_CMD_CAPTION(ctx->request.header.cmd),
ctx->request.header.body_len, ctx->response.header.status,
ctx->response.error.message);
@ -650,7 +659,8 @@ int sf_proto_deal_task_done(struct fast_task_info *task,
log_it_ex(&g_log_context, log_level, "file: "__FILE__", line: %d, "
"%s client %s:%u, req cmd: %d (%s), req body_len: %d, "
"resp status: %d, time used: %s us", __LINE__, service_name,
task->client_ip, task->port, ctx->request.header.cmd,
format_ip_address(task->client_ip, formatted_ip),
task->port, ctx->request.header.cmd,
GET_CMD_CAPTION(ctx->request.header.cmd),
ctx->request.header.body_len, ctx->response.header.status,
long_to_comma_str(time_used, time_buff));
@ -674,6 +684,7 @@ int sf_proto_deal_task_done(struct fast_task_info *task,
status = sf_unify_errno(FC_ABS(ctx->response.header.status));
short2buff(status, proto_header->status);
short2buff(ctx->response.header.flags, proto_header->flags);
proto_header->cmd = ctx->response.header.cmd;
int2buff(ctx->response.header.body_len, proto_header->body_len);
task->send.ptr->length = sizeof(SFCommonProtoHeader) +
@ -690,8 +701,9 @@ int sf_proto_deal_task_done(struct fast_task_info *task,
blen = sprintf(buff, "timed used: %s us, %s client %s:%u, "
"req cmd: %d (%s), req body len: %d, resp cmd: %d (%s), "
"status: %d, resp body len: %d", long_to_comma_str(time_used,
time_buff), service_name, task->client_ip, task->port, ctx->
request.header.cmd, GET_CMD_CAPTION(ctx->request.header.cmd),
time_buff), service_name, format_ip_address(task->
client_ip, formatted_ip), task->port, ctx->request.
header.cmd, GET_CMD_CAPTION(ctx->request.header.cmd),
ctx->request.header.body_len, ctx->response.header.cmd,
GET_CMD_CAPTION(ctx->response.header.cmd),
ctx->response.header.status, ctx->response.header.body_len);
@ -704,7 +716,8 @@ int sf_proto_deal_task_done(struct fast_task_info *task,
"%s client %s:%u, req cmd: %d (%s), req body_len: %d, "
"resp cmd: %d (%s), status: %d, resp body_len: %d, "
"time used: %s us", __LINE__, service_name,
task->client_ip, task->port, ctx->request.header.cmd,
format_ip_address(task->client_ip, formatted_ip),
task->port, ctx->request.header.cmd,
GET_CMD_CAPTION(ctx->request.header.cmd),
ctx->request.header.body_len, ctx->response.header.cmd,
GET_CMD_CAPTION(ctx->response.header.cmd),

View File

@ -307,6 +307,7 @@ static inline void sf_proto_init_task_context(struct fast_task_info *task,
ctx->response.header.cmd = SF_PROTO_ACK;
ctx->response.header.body_len = 0;
ctx->response.header.status = 0;
ctx->response.header.flags = 0;
ctx->response.error.length = 0;
ctx->response.error.message[0] = '\0';
ctx->log_level = LOG_ERR;
@ -457,15 +458,16 @@ static inline void sf_log_network_error_ex1(SFResponseInfo *response,
sf_log_network_error_ex1(response, conn, service_name, result, \
LOG_ERR, __FILE__, __LINE__)
#define sf_log_network_error_for_update_ex(response, \
conn, service_name, result, file, line) \
#define sf_log_network_error_for_update_ex(response, conn, \
service_name, result, enoent_log_level, file, line) \
sf_log_network_error_ex1(response, conn, service_name, result, \
(result == SF_RETRIABLE_ERROR_CHANNEL_INVALID) ? \
LOG_DEBUG : LOG_ERR, file, line)
(result == SF_RETRIABLE_ERROR_CHANNEL_INVALID) ? \
LOG_DEBUG : ((result == ENOENT || result == ENODATA) ? \
enoent_log_level : LOG_ERR), file, line)
#define sf_log_network_error_for_update(response, conn, service_name, result) \
sf_log_network_error_for_update_ex(response, conn, \
service_name, result, __FILE__, __LINE__)
sf_log_network_error_for_update_ex(response, conn, service_name, \
result, LOG_ERR, __FILE__, __LINE__)
#define sf_log_network_error_for_delete_ex(response, conn, \
service_name, result, enoent_log_level, file, line) \
@ -560,6 +562,16 @@ int sf_recv_response(ConnectionInfo *conn, SFResponseInfo *response,
const int network_timeout, const unsigned char expect_cmd,
char *recv_data, const int expect_body_len);
static inline int sf_recv_none_body_response(ConnectionInfo *conn,
SFResponseInfo *response, const int network_timeout,
const unsigned char expect_cmd)
{
char *recv_data = NULL;
const int expect_body_len = 0;
return sf_recv_response(conn, response, network_timeout,
expect_cmd, recv_data, expect_body_len);
}
int sf_recv_vary_response(ConnectionInfo *conn, SFResponseInfo *response,
const int network_timeout, const unsigned char expect_cmd,
SFProtoRecvBuffer *buffer, const int min_body_len);
@ -615,6 +627,35 @@ static inline int sf_proto_send_buf1(ConnectionInfo *conn, char *data,
return result;
}
static inline int sf_proto_send_buf2(ConnectionInfo *conn, char *buff1,
const int length1, char *buff2, const int length2,
SFResponseInfo *response, const int network_timeout)
{
int result;
if (conn->comm_type == fc_comm_type_rdma) {
result = G_RDMA_CONNECTION_CALLBACKS.request_by_buf2(
conn, buff1, length1, buff2, length2,
network_timeout * 1000);
} else {
if ((result=tcpsenddata_nb(conn->sock, buff1, length1,
network_timeout)) == 0)
{
result = tcpsenddata_nb(conn->sock, buff2, length2,
network_timeout);
}
}
if (result != 0) {
response->error.length = snprintf(response->error.message,
sizeof(response->error.message),
"send data fail, errno: %d, error info: %s",
result, STRERROR(result));
}
return result;
}
int sf_send_and_recv_response_header(ConnectionInfo *conn, char *data,
const int len, SFResponseInfo *response, const int network_timeout);

View File

@ -33,9 +33,8 @@
#include "fastcommon/sched_thread.h"
#include "fastcommon/ioevent_loop.h"
#include "fastcommon/fc_memory.h"
#include "sf_nio.h"
#include "sf_proto.h"
#include "sf_util.h"
#include "sf_global.h"
#include "sf_service.h"
#if defined(OS_LINUX)
@ -58,22 +57,17 @@ struct worker_thread_context {
struct nio_thread_data *thread_data;
};
int sf_init_task(struct fast_task_info *task)
{
task->connect_timeout = SF_G_CONNECT_TIMEOUT; //for client side
task->network_timeout = SF_G_NETWORK_TIMEOUT;
return 0;
}
static void *worker_thread_entrance(void *arg);
static int sf_init_free_queue(struct fast_task_queue *free_queue,
const char *name, const bool double_buffers,
static int sf_init_free_queue(SFContext *sf_context, const char *name,
const bool double_buffers, const bool need_shrink_task_buffer,
const int task_padding_size, const int task_arg_size,
TaskInitCallback init_callback)
TaskInitCallback init_callback, void *init_arg)
{
int result;
int buffer_size;
int m;
int max_m;
int alloc_conn_once;
if ((result=set_rand_seed()) != 0) {
@ -82,19 +76,27 @@ static int sf_init_free_queue(struct fast_task_queue *free_queue,
return result;
}
m = g_sf_global_vars.min_buff_size / (64 * 1024);
if (strcmp(name, "cluster") == 0 || strcmp(name, "replica") == 0) {
buffer_size = FC_MAX(4 * 1024 * 1024, sf_context->
net_buffer_cfg.max_buff_size);
max_m = 64;
} else {
buffer_size = sf_context->net_buffer_cfg.min_buff_size;
max_m = 16;
}
m = buffer_size / (64 * 1024);
if (m == 0) {
m = 1;
} else if (m > 16) {
m = 16;
} else if (m > max_m) {
m = max_m;
}
alloc_conn_once = 256 / m;
return free_queue_init_ex2(free_queue, name, double_buffers,
g_sf_global_vars.max_connections, alloc_conn_once,
g_sf_global_vars.min_buff_size, g_sf_global_vars.
max_buff_size, task_padding_size, task_arg_size,
(init_callback != NULL ? init_callback :
sf_init_task));
return free_queue_init_ex2(&sf_context->free_queue, name, double_buffers,
need_shrink_task_buffer, sf_context->net_buffer_cfg.max_connections,
alloc_conn_once, sf_context->net_buffer_cfg.min_buff_size,
sf_context->net_buffer_cfg.max_buff_size, task_padding_size,
task_arg_size, init_callback, init_arg);
}
int sf_service_init_ex2(SFContext *sf_context, const char *name,
@ -109,12 +111,14 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name,
sf_recv_timeout_callback timeout_callback, const int net_timeout_ms,
const int proto_header_size, const int task_padding_size,
const int task_arg_size, const bool double_buffers,
const bool explicit_post_recv, TaskInitCallback init_callback,
const bool need_shrink_task_buffer, const bool explicit_post_recv,
TaskInitCallback init_callback, void *init_arg,
sf_release_buffer_callback release_buffer_callback)
{
int result;
int bytes;
int extra_events;
int max_entries;
int i;
struct worker_thread_context *thread_contexts;
struct worker_thread_context *thread_ctx;
@ -123,10 +127,10 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name,
pthread_t tid;
pthread_attr_t thread_attr;
snprintf(sf_context->name, sizeof(sf_context->name), "%s", name);
fc_safe_strcpy(sf_context->name, name);
sf_context->connect_need_log = true;
sf_context->realloc_task_buffer = g_sf_global_vars.
min_buff_size < g_sf_global_vars.max_buff_size;
sf_context->realloc_task_buffer = sf_context->net_buffer_cfg.
min_buff_size < sf_context->net_buffer_cfg.max_buff_size;
sf_context->callbacks.accept_done = accept_done_callback;
sf_set_parameters_ex(sf_context, proto_header_size,
set_body_length_func, alloc_recv_buffer_func,
@ -139,9 +143,9 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name,
}
}
if ((result=sf_init_free_queue(&sf_context->free_queue,
name, double_buffers, task_padding_size,
task_arg_size, init_callback)) != 0)
if ((result=sf_init_free_queue(sf_context, name, double_buffers,
need_shrink_task_buffer, task_padding_size,
task_arg_size, init_callback, init_arg)) != 0)
{
return result;
}
@ -169,7 +173,11 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name,
if (SF_G_EPOLL_EDGE_TRIGGER) {
#ifdef OS_LINUX
#if IOEVENT_USE_EPOLL
extra_events = EPOLLET;
#else
extra_events = 0;
#endif
#elif defined(OS_FREEBSD)
extra_events = EV_CLEAR;
#else
@ -179,6 +187,40 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name,
extra_events = 0;
}
max_entries = (sf_context->net_buffer_cfg.max_connections +
sf_context->work_threads - 1) / sf_context->work_threads;
if (strcmp(sf_context->name, "cluster") == 0 ||
strcmp(sf_context->name, "replica") == 0)
{
if (max_entries < 1024) {
max_entries += 8;
} else {
max_entries = 1024;
}
} else {
if (max_entries < 4 * 1024) {
max_entries = max_entries * 2;
} else if (max_entries < 8 * 1024) {
max_entries = (max_entries * 3) / 2;
} else if (max_entries < 16 * 1024) {
max_entries = (max_entries * 5) / 4;
} else if (max_entries < 32 * 1024) {
max_entries = (max_entries * 6) / 5;
} else if (max_entries < 64 * 1024) {
max_entries = (max_entries * 11) / 10;
} else if (max_entries < 128 * 1024) {
max_entries = (max_entries * 21) / 20;
}
#if IOEVENT_USE_URING
if (sf_context->use_io_uring) {
if (max_entries > 32 * 1024) {
max_entries = 32 * 1024;
}
}
#endif
}
g_current_time = time(NULL);
sf_context->thread_count = 0;
data_end = sf_context->thread_data + sf_context->work_threads;
@ -203,19 +245,45 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name,
thread_data->arg = NULL;
}
if (ioevent_init(&thread_data->ev_puller, 2 + g_sf_global_vars.
max_connections, net_timeout_ms, extra_events) != 0)
if ((result=ioevent_init(&thread_data->ev_puller, sf_context->
name, sf_context->use_io_uring, max_entries,
net_timeout_ms, extra_events)) != 0)
{
result = errno != 0 ? errno : ENOMEM;
char prompt[256];
#if IOEVENT_USE_URING
if (sf_context->use_io_uring) {
if (result == EPERM) {
strcpy(prompt, " make sure kernel."
"io_uring_disabled set to 0");
} else if (result == EINVAL) {
sprintf(prompt, " maybe max_connections: %d is too large"
" or [%s]'s work_threads: %d is too small",
sf_context->net_buffer_cfg.max_connections,
sf_context->name, sf_context->work_threads);
} else {
*prompt = '\0';
}
} else {
#endif
*prompt = '\0';
#if IOEVENT_USE_URING
}
#endif
logError("file: "__FILE__", line: %d, "
"ioevent_init fail, "
"errno: %d, error info: %s",
__LINE__, result, strerror(result));
"ioevent_init fail, errno: %d, error info: %s.%s"
, __LINE__, result, strerror(result), prompt);
return result;
}
result = fast_timer_init(&thread_data->timer,
2 * g_sf_global_vars.network_timeout, g_current_time);
#if IOEVENT_USE_URING
if (sf_context->use_io_uring && send_done_callback != NULL) {
ioevent_set_send_zc_done_notify(&thread_data->ev_puller, true);
}
#endif
result = fast_timer_init(&thread_data->timer, 2 * sf_context->
net_buffer_cfg.network_timeout, g_current_time);
if (result != 0) {
logError("file: "__FILE__", line: %d, "
"fast_timer_init fail, errno: %d, error info: %s",
@ -359,7 +427,9 @@ int sf_socket_create_server(SFListener *listener,
return result;
}
if ((result=tcpsetserveropt(listener->sock, SF_G_NETWORK_TIMEOUT)) != 0) {
if ((result=tcpsetserveropt(listener->sock, listener->handler->
fh->ctx->net_buffer_cfg.network_timeout)) != 0)
{
return result;
}
@ -492,7 +562,9 @@ struct fast_task_info *sf_socket_accept_connection(SFListener *listener)
}
FC_SET_CLOEXEC(incomesock);
if ((task=sf_alloc_init_task(listener->handler, incomesock)) == NULL) {
if ((task=sf_alloc_init_server_task(listener->handler,
incomesock)) == NULL)
{
close(incomesock);
return NULL;
}
@ -503,12 +575,6 @@ struct fast_task_info *sf_socket_accept_connection(SFListener *listener)
return task;
}
void sf_socket_close_connection(struct fast_task_info *task)
{
close(task->event.fd);
task->event.fd = -1;
}
void sf_socket_close_ex(SFContext *sf_context)
{
int i;
@ -787,15 +853,13 @@ int sf_setup_signal_handler()
return 0;
}
#define LOG_SCHEDULE_ENTRIES_COUNT 3
int sf_startup_schedule(pthread_t *schedule_tid)
{
ScheduleArray scheduleArray;
ScheduleEntry scheduleEntries[LOG_SCHEDULE_ENTRIES_COUNT];
ScheduleEntry scheduleEntries[SF_LOG_SCHEDULE_ENTRIES_COUNT];
scheduleArray.entries = scheduleEntries;
sf_setup_schedule(&g_log_context, &g_sf_global_vars.error_log,
sf_logger_setup_schedule(&g_log_context, &g_sf_global_vars.error_log,
&scheduleArray);
return sched_start(&scheduleArray, schedule_tid,
g_sf_global_vars.thread_stack_size, (bool * volatile)
@ -806,7 +870,7 @@ int sf_add_slow_log_schedule(SFSlowLogContext *slowlog_ctx)
{
int result;
ScheduleArray scheduleArray;
ScheduleEntry scheduleEntries[LOG_SCHEDULE_ENTRIES_COUNT];
ScheduleEntry scheduleEntries[SF_LOG_SCHEDULE_ENTRIES_COUNT];
if (!slowlog_ctx->cfg.enabled) {
return 0;
@ -819,8 +883,8 @@ int sf_add_slow_log_schedule(SFSlowLogContext *slowlog_ctx)
}
scheduleArray.entries = scheduleEntries;
sf_setup_schedule(&slowlog_ctx->ctx, &slowlog_ctx->cfg.log_cfg,
&scheduleArray);
sf_logger_setup_schedule(&slowlog_ctx->ctx, &slowlog_ctx->
cfg.log_cfg, &scheduleArray);
return sched_add_entries(&scheduleArray);
}

View File

@ -25,6 +25,9 @@
#include "fastcommon/ioevent.h"
#include "fastcommon/fast_task_queue.h"
#include "sf_types.h"
#include "sf_proto.h"
#include "sf_global.h"
#include "sf_nio.h"
typedef void* (*sf_alloc_thread_extra_data_callback)(const int thread_index);
typedef void (*sf_sig_quit_handler)(int sig);
@ -45,7 +48,8 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name,
sf_recv_timeout_callback timeout_callback, const int net_timeout_ms,
const int proto_header_size, const int task_padding_size,
const int task_arg_size, const bool double_buffers,
const bool explicit_post_recv, TaskInitCallback init_callback,
const bool need_shrink_task_buffer, const bool explicit_post_recv,
TaskInitCallback init_callback, void *init_arg,
sf_release_buffer_callback release_buffer_callback);
#define sf_service_init_ex(sf_context, name, alloc_thread_extra_data_callback,\
@ -56,7 +60,7 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name,
thread_loop_callback, accept_done_callback, set_body_length_func, \
NULL, send_done_callback, deal_func, task_cleanup_func, \
timeout_callback, net_timeout_ms, proto_header_size, \
0, task_arg_size, false, false, NULL, NULL)
0, task_arg_size, false, true, false, NULL, NULL, NULL)
#define sf_service_init(name, alloc_thread_extra_data_callback, \
thread_loop_callback, accept_done_callback, set_body_length_func, \
@ -65,8 +69,8 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name,
sf_service_init_ex2(&g_sf_context, name, alloc_thread_extra_data_callback, \
thread_loop_callback, accept_done_callback, set_body_length_func, NULL,\
send_done_callback, deal_func, task_cleanup_func, timeout_callback, \
net_timeout_ms, proto_header_size, 0, task_arg_size, false, false, \
NULL, NULL)
net_timeout_ms, proto_header_size, 0, task_arg_size, false, true, \
false, NULL, NULL, NULL)
int sf_service_destroy_ex(SFContext *sf_context);
@ -108,8 +112,6 @@ int sf_socket_create_server(SFListener *listener,
void sf_socket_close_server(SFListener *listener);
struct fast_task_info *sf_socket_accept_connection(SFListener *listener);
void sf_socket_close_connection(struct fast_task_info *task);
int sf_socket_server_ex(SFContext *sf_context);
#define sf_socket_server() sf_socket_server_ex(&g_sf_context)
@ -148,8 +150,6 @@ void sf_notify_all_threads_ex(SFContext *sf_context);
void sf_set_sig_quit_handler(sf_sig_quit_handler quit_handler);
int sf_init_task(struct fast_task_info *task);
static inline struct fast_task_info *sf_alloc_init_task_ex(
SFNetworkHandler *handler, const int fd,
const int reffer_count)
@ -165,6 +165,11 @@ static inline struct fast_task_info *sf_alloc_init_task_ex(
return NULL;
}
if (task->shrinked) {
task->shrinked = false;
sf_proto_init_task_magic(task);
}
__sync_add_and_fetch(&task->reffer_count, reffer_count);
__sync_bool_compare_and_swap(&task->canceled, 1, 0);
task->handler = handler;
@ -172,9 +177,42 @@ static inline struct fast_task_info *sf_alloc_init_task_ex(
return task;
}
#define sf_hold_task(task) __sync_add_and_fetch(&task->reffer_count, 1)
#define sf_hold_task_ex(task, inc_count) fc_hold_task_ex(task, inc_count)
#define sf_hold_task(task) fc_hold_task(task)
#define sf_alloc_init_task(handler, fd) sf_alloc_init_task_ex(handler, fd, 1)
static inline struct fast_task_info *sf_alloc_init_server_task(
SFNetworkHandler *handler, const int fd)
{
const int reffer_count = 1;
struct fast_task_info *task;
if ((task=sf_alloc_init_task_ex(handler, fd, reffer_count)) != NULL) {
#if IOEVENT_USE_URING
FC_URING_IS_CLIENT(task) = false;
#endif
}
return task;
}
static inline struct fast_task_info *sf_alloc_init_client_task(
SFNetworkHandler *handler)
{
const int fd = -1;
const int reffer_count = 1;
struct fast_task_info *task;
if ((task=sf_alloc_init_task_ex(handler, fd, reffer_count)) != NULL) {
#if IOEVENT_USE_URING
FC_URING_IS_CLIENT(task) = true;
#endif
}
return task;
}
static inline void sf_release_task(struct fast_task_info *task)
{
if (__sync_sub_and_fetch(&task->reffer_count, 1) == 0) {
@ -186,6 +224,15 @@ static inline void sf_release_task(struct fast_task_info *task)
"used: %d, freed: %d", __LINE__, task,
alloc_count, alloc_count - free_count, free_count);
*/
#if IOEVENT_USE_URING
if (SF_CTX->use_io_uring) {
task->handler->close_connection(task);
__sync_fetch_and_sub(&g_sf_global_vars.
connection_stat.current_count, 1);
}
#endif
free_queue_push(task);
}
}
@ -261,6 +308,19 @@ static inline SFNetworkHandler *sf_get_rdma_network_handler3(
return sf_get_rdma_network_handler(sf_context3);
}
static inline bool sf_get_double_buffers_flag(FCServerGroupInfo *server_group)
{
if (server_group->comm_type == fc_comm_type_sock) {
#if IOEVENT_USE_URING
return true;
#else
return false;
#endif
} else { //RDMA
return true;
}
}
#ifdef __cplusplus
}
#endif

View File

@ -42,6 +42,11 @@
#define SF_SOCKET_NETWORK_HANDLER_INDEX 0
#define SF_RDMACM_NETWORK_HANDLER_INDEX 1
#define SF_BINLOG_BUFFER_PRODUCER_DATA_LENGTH(bf) ((bf).data_end - (bf).buff)
#define SF_BINLOG_BUFFER_PRODUCER_BUFF_REMAIN(bf) ((bf).buff_end - (bf).data_end)
#define SF_BINLOG_BUFFER_CONSUMER_DATA_LENGTH(bf) ((bf).current - (bf).buff)
#define SF_BINLOG_BUFFER_CONSUMER_DATA_REMAIN(bf) ((bf).data_end - (bf).current)
typedef int (*sf_accept_done_callback)(struct fast_task_info *task,
const in_addr_64_t client_addr, const bool bInnerPort);
typedef int (*sf_set_body_length_callback)(struct fast_task_info *task);
@ -110,6 +115,7 @@ typedef struct sf_listener {
struct sf_context;
struct sf_address_family_handler;
typedef struct sf_network_handler {
bool enabled;
bool explicit_post_recv;
@ -160,20 +166,32 @@ typedef struct sf_address_family_handler {
struct sf_context *ctx;
} SFAddressFamilyHandler;
typedef struct sf_net_buffer_config {
int connect_timeout;
int network_timeout;
int max_connections;
int max_pkg_size;
int min_buff_size;
int max_buff_size;
} SFNetBufferConfig;
typedef struct sf_context {
char name[64];
struct nio_thread_data *thread_data;
volatile int thread_count;
//int rdma_port_offset;
bool is_client; //since v1.2.5
bool use_io_uring; //since v1.2.9
bool use_send_zc; //since v1.2.9
SFAddressFamily address_family;
SFAddressFamilyHandler handlers[SF_ADDRESS_FAMILY_COUNT];
SFNetBufferConfig net_buffer_cfg;
int accept_threads;
int work_threads;
int header_size;
bool remove_from_ready_list;
bool realloc_task_buffer;
bool connect_need_log; //for client connect
FCSmartPollingConfig smart_polling;
@ -219,10 +237,11 @@ typedef struct sf_binlog_file_position {
} SFBinlogFilePosition;
typedef struct server_binlog_buffer {
char *buff; //the buffer pointer
char *current; //for the consumer
char *end; //data end ptr
int size; //the buffer size (capacity)
char *buff; //the buffer pointer
char *current; //for the consumer
char *data_end; //data end ptr
char *buff_end; //buffer end ptr
int size; //the buffer size (capacity)
} SFBinlogBuffer;
typedef struct sf_space_stat {
@ -351,6 +370,7 @@ typedef struct sf_synchronize_context {
int result;
union {
bool finished;
bool ready;
int waiting_count;
};
} SFSynchronizeContext;

View File

@ -281,7 +281,7 @@ int sf_logger_init(LogContext *pContext, const char *filename_prefix)
return 0;
}
ScheduleEntry *sf_logger_set_schedule_entry(struct log_context *pContext,
ScheduleEntry *sf_logger_set_schedule_entries(struct log_context *pContext,
SFLogConfig *log_cfg, ScheduleEntry *pScheduleEntry)
{
INIT_SCHEDULE_ENTRY(*pScheduleEntry, sched_generate_next_id(),

View File

@ -96,14 +96,14 @@ void sf_parse_cmd_option_bool(int argc, char *argv[],
int sf_logger_init(LogContext *pContext, const char *filename_prefix);
ScheduleEntry *sf_logger_set_schedule_entry(struct log_context *pContext,
ScheduleEntry *sf_logger_set_schedule_entries(struct log_context *pContext,
SFLogConfig *log_cfg, ScheduleEntry *pScheduleEntry);
static inline void sf_setup_schedule(struct log_context *pContext,
static inline void sf_logger_setup_schedule(struct log_context *pContext,
SFLogConfig *log_cfg, ScheduleArray *scheduleArray)
{
ScheduleEntry *scheduleEntry;
scheduleEntry = sf_logger_set_schedule_entry(pContext,
scheduleEntry = sf_logger_set_schedule_entries(pContext,
log_cfg, scheduleArray->entries);
scheduleArray->count = scheduleEntry - scheduleArray->entries;
}