Compare commits
63 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
4adf6b3227 | |
|
|
f4a799402e | |
|
|
27510e9641 | |
|
|
848077797b | |
|
|
d22f9da49c | |
|
|
5495455fa7 | |
|
|
4da0ff251c | |
|
|
2444eac6ce | |
|
|
a52cc2d5d4 | |
|
|
c4af33a497 | |
|
|
fa32972052 | |
|
|
688211fbcd | |
|
|
1b2f521b99 | |
|
|
ddc528d69d | |
|
|
32d443b497 | |
|
|
53dd39500f | |
|
|
772a9a6895 | |
|
|
932751d392 | |
|
|
817ff547da | |
|
|
926cd40114 | |
|
|
b688973cf9 | |
|
|
b16526e8f7 | |
|
|
68079fc468 | |
|
|
3dcc1c570d | |
|
|
cf0950ea62 | |
|
|
263171c4fe | |
|
|
a2ab8a0c01 | |
|
|
0f75b039f6 | |
|
|
ecee21f289 | |
|
|
bc5af8a58b | |
|
|
f0ee6ce73f | |
|
|
aef9d803d1 | |
|
|
6d0465acc4 | |
|
|
2e5258045d | |
|
|
fc689a02ba | |
|
|
7b3f6d620a | |
|
|
03f64998ce | |
|
|
0b4936bd8f | |
|
|
ba87f8e1ea | |
|
|
da2ddc7441 | |
|
|
b83189f021 | |
|
|
4ad53f7ee3 | |
|
|
003cc74b96 | |
|
|
3815c0b2ce | |
|
|
7ee7e7b535 | |
|
|
e095ce45c2 | |
|
|
f9d8c3d070 | |
|
|
fc9c23fb95 | |
|
|
755296bb68 | |
|
|
14a783fd6f | |
|
|
e9e33883bf | |
|
|
318640572f | |
|
|
f6e2de5668 | |
|
|
b6e24d0548 | |
|
|
03974ff302 | |
|
|
a3e1956128 | |
|
|
75e8aacfd9 | |
|
|
be4bad4ee1 | |
|
|
3993b79a81 | |
|
|
42c6e2c6b7 | |
|
|
6a60a49c32 | |
|
|
14d0a1c014 | |
|
|
a01ccf66dc |
|
|
@ -1,3 +1,45 @@
|
||||||
|
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
|
libserverframe (1.2.3-1) unstable; urgency=medium
|
||||||
|
|
||||||
* upgrade to 1.2.3-1
|
* upgrade to 1.2.3-1
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
libfastcommon:Version=1.0.73
|
libfastcommon:Version=1.0.83
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
%define CommitVersion %(echo $COMMIT_VERSION)
|
%define CommitVersion %(echo $COMMIT_VERSION)
|
||||||
|
|
||||||
Name: libserverframe
|
Name: libserverframe
|
||||||
Version: 1.2.4
|
Version: 1.2.11
|
||||||
Release: 1%{?dist}
|
Release: 1%{?dist}
|
||||||
Summary: network framework library
|
Summary: network framework library
|
||||||
License: AGPL v3.0
|
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)
|
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
|
||||||
|
|
||||||
BuildRequires: libfastcommon-devel >= 1.0.74
|
BuildRequires: libfastcommon-devel >= 1.0.83
|
||||||
Requires: %__cp %__mv %__chmod %__grep %__mkdir %__install %__id
|
Requires: %__cp %__mv %__chmod %__grep %__mkdir %__install %__id
|
||||||
Requires: libfastcommon >= 1.0.74
|
Requires: libfastcommon >= 1.0.83
|
||||||
|
|
||||||
%description
|
%description
|
||||||
common framework library
|
common framework library
|
||||||
|
|
|
||||||
7
make.sh
7
make.sh
|
|
@ -9,10 +9,13 @@ DEBUG_FLAG=0
|
||||||
|
|
||||||
if [ -f /usr/include/fastcommon/_os_define.h ]; then
|
if [ -f /usr/include/fastcommon/_os_define.h ]; then
|
||||||
OS_BITS=$(grep -F OS_BITS /usr/include/fastcommon/_os_define.h | awk '{print $NF;}')
|
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
|
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;}')
|
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
|
else
|
||||||
OS_BITS=64
|
OS_BITS=64
|
||||||
|
USE_URING=''
|
||||||
fi
|
fi
|
||||||
|
|
||||||
uname=$(uname)
|
uname=$(uname)
|
||||||
|
|
@ -49,6 +52,9 @@ LIBS=''
|
||||||
uname=$(uname)
|
uname=$(uname)
|
||||||
if [ "$uname" = "Linux" ]; then
|
if [ "$uname" = "Linux" ]; then
|
||||||
CFLAGS="$CFLAGS"
|
CFLAGS="$CFLAGS"
|
||||||
|
if [ -n "$USE_URING" ]; then
|
||||||
|
LIBS="$LIBS -luring"
|
||||||
|
fi
|
||||||
elif [ "$uname" = "FreeBSD" ] || [ "$uname" = "Darwin" ]; then
|
elif [ "$uname" = "FreeBSD" ] || [ "$uname" = "Darwin" ]; then
|
||||||
CFLAGS="$CFLAGS"
|
CFLAGS="$CFLAGS"
|
||||||
if [ "$uname" = "Darwin" ]; then
|
if [ "$uname" = "Darwin" ]; then
|
||||||
|
|
@ -132,6 +138,7 @@ fi
|
||||||
|
|
||||||
cd ..
|
cd ..
|
||||||
cp Makefile.in Makefile
|
cp Makefile.in Makefile
|
||||||
|
sed_replace "s#\\\$(CC)#gcc#g" Makefile
|
||||||
sed_replace "s#\\\$(CFLAGS)#$CFLAGS#g" Makefile
|
sed_replace "s#\\\$(CFLAGS)#$CFLAGS#g" Makefile
|
||||||
sed_replace "s#\\\$(LIBS)#$LIBS#g" Makefile
|
sed_replace "s#\\\$(LIBS)#$LIBS#g" Makefile
|
||||||
sed_replace "s#\\\$(TARGET_PREFIX)#$TARGET_PREFIX#g" Makefile
|
sed_replace "s#\\\$(TARGET_PREFIX)#$TARGET_PREFIX#g" Makefile
|
||||||
|
|
|
||||||
|
|
@ -194,7 +194,7 @@ static struct fast_task_info *alloc_channel_task(IdempotencyClientChannel
|
||||||
return NULL;
|
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->port = port;
|
||||||
task->arg = channel;
|
task->arg = channel;
|
||||||
task->thread_data = g_sf_context.thread_data +
|
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;
|
channel->last_connect_time = g_current_time;
|
||||||
if ((*err_no=sf_nio_notify(task, SF_NIO_STAGE_CONNECT)) != 0) {
|
if ((*err_no=sf_nio_notify(task, SF_NIO_STAGE_CONNECT)) != 0) {
|
||||||
channel->in_ioevent = 0; //rollback
|
channel->in_ioevent = 0; //rollback
|
||||||
sf_release_task(task);
|
__sync_sub_and_fetch(&task->reffer_count, 1);
|
||||||
|
free_queue_push(task);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return task;
|
return task;
|
||||||
|
|
@ -215,6 +216,14 @@ int idempotency_client_channel_check_reconnect(
|
||||||
int result;
|
int result;
|
||||||
char formatted_ip[FORMATTED_IP_SIZE];
|
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)) {
|
if (!__sync_bool_compare_and_swap(&channel->in_ioevent, 0, 1)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -231,6 +240,9 @@ int idempotency_client_channel_check_reconnect(
|
||||||
formatted_ip, channel->task->port);
|
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);
|
__sync_bool_compare_and_swap(&channel->task->canceled, 1, 0);
|
||||||
if ((result=sf_nio_notify(channel->task, SF_NIO_STAGE_CONNECT)) == 0) {
|
if ((result=sf_nio_notify(channel->task, SF_NIO_STAGE_CONNECT)) == 0) {
|
||||||
channel->last_connect_time = g_current_time;
|
channel->last_connect_time = g_current_time;
|
||||||
|
|
@ -255,7 +267,10 @@ struct idempotency_client_channel *idempotency_client_channel_get(
|
||||||
IdempotencyClientChannel *current;
|
IdempotencyClientChannel *current;
|
||||||
IdempotencyClientChannel *channel;
|
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);
|
hash_code = fc_simple_hash(key, key_len);
|
||||||
bucket = channel_context.htable.buckets +
|
bucket = channel_context.htable.buckets +
|
||||||
hash_code % channel_context.htable.capacity;
|
hash_code % channel_context.htable.capacity;
|
||||||
|
|
@ -339,8 +354,8 @@ int idempotency_client_channel_push(struct idempotency_client_channel *channel,
|
||||||
receipt->req_id = req_id;
|
receipt->req_id = req_id;
|
||||||
fc_queue_push_ex(&channel->queue, receipt, ¬ify);
|
fc_queue_push_ex(&channel->queue, receipt, ¬ify);
|
||||||
if (notify) {
|
if (notify) {
|
||||||
if (__sync_add_and_fetch(&channel->in_ioevent, 0)) {
|
if (FC_ATOMIC_GET(channel->in_ioevent)) {
|
||||||
if (__sync_add_and_fetch(&channel->established, 0)) {
|
if (FC_ATOMIC_GET(channel->established)) {
|
||||||
sf_nio_notify(channel->task, SF_NIO_STAGE_CONTINUE);
|
sf_nio_notify(channel->task, SF_NIO_STAGE_CONTINUE);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -76,13 +76,13 @@ static inline void idempotency_client_channel_set_id_key(
|
||||||
static inline int idempotency_client_channel_check_wait_ex(
|
static inline int idempotency_client_channel_check_wait_ex(
|
||||||
struct idempotency_client_channel *channel, const int timeout)
|
struct idempotency_client_channel *channel, const int timeout)
|
||||||
{
|
{
|
||||||
if (__sync_add_and_fetch(&channel->established, 0)) {
|
if (FC_ATOMIC_GET(channel->established)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
idempotency_client_channel_check_reconnect(channel);
|
idempotency_client_channel_check_reconnect(channel);
|
||||||
lcp_timedwait_sec(&channel->lcp, timeout);
|
lcp_timedwait_sec(&channel->lcp, timeout);
|
||||||
if (__sync_add_and_fetch(&channel->established, 0)) {
|
if (FC_ATOMIC_GET(channel->established)) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,6 @@ typedef struct idempotency_receipt_global_vars {
|
||||||
struct {
|
struct {
|
||||||
int task_padding_size;
|
int task_padding_size;
|
||||||
sf_init_connection_callback init_connection;
|
sf_init_connection_callback init_connection;
|
||||||
struct ibv_pd *pd;
|
|
||||||
} rdma;
|
} rdma;
|
||||||
IdempotencyReceiptThreadContext *thread_contexts;
|
IdempotencyReceiptThreadContext *thread_contexts;
|
||||||
} IdempotencyReceiptGlobalVars;
|
} IdempotencyReceiptGlobalVars;
|
||||||
|
|
|
||||||
|
|
@ -46,12 +46,15 @@ static IdempotencyReceiptGlobalVars receipt_global_vars;
|
||||||
#define RECEIPT_THREAD_CONTEXTS receipt_global_vars.thread_contexts
|
#define RECEIPT_THREAD_CONTEXTS receipt_global_vars.thread_contexts
|
||||||
#define TASK_PADDING_SIZE receipt_global_vars.rdma.task_padding_size
|
#define TASK_PADDING_SIZE receipt_global_vars.rdma.task_padding_size
|
||||||
#define RDMA_INIT_CONNECTION receipt_global_vars.rdma.init_connection
|
#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)
|
||||||
{
|
{
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
FC_URING_IS_CLIENT(task) = true;
|
||||||
|
#endif
|
||||||
|
|
||||||
if (RDMA_INIT_CONNECTION != NULL) {
|
if (RDMA_INIT_CONNECTION != NULL) {
|
||||||
return RDMA_INIT_CONNECTION(task, RDMA_PD);
|
return RDMA_INIT_CONNECTION(task, arg);
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -93,7 +96,6 @@ static void receipt_task_finish_cleanup(struct fast_task_info *task)
|
||||||
|
|
||||||
if (task->event.fd >= 0) {
|
if (task->event.fd >= 0) {
|
||||||
sf_task_detach_thread(task);
|
sf_task_detach_thread(task);
|
||||||
task->handler->close_connection(task);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sf_nio_reset_task_length(task);
|
sf_nio_reset_task_length(task);
|
||||||
|
|
@ -283,7 +285,7 @@ static int deal_setup_channel_response(struct fast_task_info *task)
|
||||||
}
|
}
|
||||||
|
|
||||||
channel = (IdempotencyClientChannel *)task->arg;
|
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);
|
format_ip_address(task->server_ip, formatted_ip);
|
||||||
logWarning("file: "__FILE__", line: %d, "
|
logWarning("file: "__FILE__", line: %d, "
|
||||||
"response from server %s:%u, unexpected cmd: "
|
"response from server %s:%u, unexpected cmd: "
|
||||||
|
|
@ -521,10 +523,12 @@ static int do_init(FCAddressPtrArray *address_array)
|
||||||
{
|
{
|
||||||
const int task_arg_size = 0;
|
const int task_arg_size = 0;
|
||||||
const bool double_buffers = false;
|
const bool double_buffers = false;
|
||||||
|
const bool need_shrink_task_buffer = false;
|
||||||
const bool explicit_post_recv = false;
|
const bool explicit_post_recv = false;
|
||||||
int result;
|
int result;
|
||||||
int bytes;
|
int bytes;
|
||||||
SFNetworkHandler *rdma_handler;
|
SFNetworkHandler *rdma_handler;
|
||||||
|
struct ibv_pd *pd;
|
||||||
|
|
||||||
bytes = sizeof(IdempotencyReceiptThreadContext) * SF_G_WORK_THREADS;
|
bytes = sizeof(IdempotencyReceiptThreadContext) * SF_G_WORK_THREADS;
|
||||||
RECEIPT_THREAD_CONTEXTS = (IdempotencyReceiptThreadContext *)
|
RECEIPT_THREAD_CONTEXTS = (IdempotencyReceiptThreadContext *)
|
||||||
|
|
@ -541,19 +545,19 @@ static int do_init(FCAddressPtrArray *address_array)
|
||||||
|
|
||||||
TASK_PADDING_SIZE = rdma_handler->get_connection_size();
|
TASK_PADDING_SIZE = rdma_handler->get_connection_size();
|
||||||
RDMA_INIT_CONNECTION = rdma_handler->init_connection;
|
RDMA_INIT_CONNECTION = rdma_handler->init_connection;
|
||||||
RDMA_PD = rdma_handler->pd;
|
pd = rdma_handler->pd;
|
||||||
} else {
|
} else {
|
||||||
TASK_PADDING_SIZE = 0;
|
TASK_PADDING_SIZE = 0;
|
||||||
RDMA_INIT_CONNECTION = NULL;
|
RDMA_INIT_CONNECTION = NULL;
|
||||||
RDMA_PD = NULL;
|
pd = NULL;
|
||||||
}
|
}
|
||||||
return sf_service_init_ex2(&g_sf_context, "idemp-receipt",
|
return sf_service_init_ex2(&g_sf_context, "idemp-receipt",
|
||||||
receipt_alloc_thread_extra_data, receipt_thread_loop_callback,
|
receipt_alloc_thread_extra_data, receipt_thread_loop_callback,
|
||||||
NULL, sf_proto_set_body_length, NULL, NULL, receipt_deal_task,
|
NULL, sf_proto_set_body_length, NULL, NULL, receipt_deal_task,
|
||||||
receipt_task_finish_cleanup, receipt_recv_timeout_callback,
|
receipt_task_finish_cleanup, receipt_recv_timeout_callback,
|
||||||
1000, sizeof(SFCommonProtoHeader), TASK_PADDING_SIZE,
|
1000, sizeof(SFCommonProtoHeader), TASK_PADDING_SIZE,
|
||||||
task_arg_size, double_buffers, explicit_post_recv,
|
task_arg_size, double_buffers, need_shrink_task_buffer,
|
||||||
receipt_init_task, NULL);
|
explicit_post_recv, receipt_init_task, pd, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
int receipt_handler_init(FCAddressPtrArray *address_array)
|
int receipt_handler_init(FCAddressPtrArray *address_array)
|
||||||
|
|
@ -565,7 +569,6 @@ int receipt_handler_init(FCAddressPtrArray *address_array)
|
||||||
}
|
}
|
||||||
|
|
||||||
sf_enable_thread_notify(true);
|
sf_enable_thread_notify(true);
|
||||||
sf_set_remove_from_ready_list(false);
|
|
||||||
fc_sleep_ms(100);
|
fc_sleep_ms(100);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
|
|
@ -186,9 +186,11 @@ static int save(SFBinlogIndexContext *ctx, const char *filename)
|
||||||
result = 0;
|
result = 0;
|
||||||
p = buff;
|
p = buff;
|
||||||
bend = buff + sizeof(buff);
|
bend = buff + sizeof(buff);
|
||||||
p += sprintf(p, "%d %"PRId64"\n",
|
|
||||||
ctx->index_array.count,
|
p += fc_itoa(ctx->index_array.count, p);
|
||||||
ctx->last_version);
|
*p++ = ' ';
|
||||||
|
p += fc_itoa(ctx->last_version, p);
|
||||||
|
*p++ = '\n';
|
||||||
|
|
||||||
index = ctx->index_array.indexes;
|
index = ctx->index_array.indexes;
|
||||||
for (i=0; i<ctx->index_array.count; i++) {
|
for (i=0; i<ctx->index_array.count; i++) {
|
||||||
|
|
@ -227,7 +229,7 @@ int sf_binlog_index_save(SFBinlogIndexContext *ctx)
|
||||||
int result;
|
int result;
|
||||||
char tmp_filename[PATH_MAX];
|
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) {
|
if ((result=save(ctx, tmp_filename)) != 0) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -168,6 +168,7 @@ static int deal_binlog_records(SFBinlogWriterThread *thread,
|
||||||
SFBinlogWriterBuffer *wb_head, uint32_t *last_timestamp)
|
SFBinlogWriterBuffer *wb_head, uint32_t *last_timestamp)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
bool skip_empty_file;
|
||||||
SFBinlogWriterBuffer *wbuffer;
|
SFBinlogWriterBuffer *wbuffer;
|
||||||
SFBinlogWriterBuffer *current;
|
SFBinlogWriterBuffer *current;
|
||||||
|
|
||||||
|
|
@ -196,11 +197,15 @@ static int deal_binlog_records(SFBinlogWriterThread *thread,
|
||||||
thread->mblock, current);
|
thread->mblock, current);
|
||||||
break;
|
break;
|
||||||
case SF_BINLOG_BUFFER_TYPE_ROTATE_FILE:
|
case SF_BINLOG_BUFFER_TYPE_ROTATE_FILE:
|
||||||
if ((result=sf_file_writer_set_binlog_write_index(¤t->
|
flush_writer_files(thread);
|
||||||
writer->fw, current->writer->fw.binlog.
|
skip_empty_file = current->version.first;
|
||||||
last_index + 1)) != 0)
|
if (!(skip_empty_file && current->writer->fw.file.size == 0)) {
|
||||||
{
|
if ((result=sf_file_writer_set_binlog_write_index(¤t->
|
||||||
return result;
|
writer->fw, current->writer->fw.binlog.
|
||||||
|
last_index + 1)) != 0)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fast_mblock_free_object(¤t->writer->
|
fast_mblock_free_object(¤t->writer->
|
||||||
thread->mblock, current);
|
thread->mblock, current);
|
||||||
|
|
@ -300,7 +305,7 @@ void sf_binlog_writer_finish(SFBinlogWriterInfo *writer)
|
||||||
uint32_t last_timestamp;
|
uint32_t last_timestamp;
|
||||||
int count;
|
int count;
|
||||||
|
|
||||||
if (writer->fw.file.name != NULL) {
|
if (writer->fw.file.name.str != NULL) {
|
||||||
while (writer->thread->running && !fc_queue_empty(
|
while (writer->thread->running && !fc_queue_empty(
|
||||||
&writer->thread->queue))
|
&writer->thread->queue))
|
||||||
{
|
{
|
||||||
|
|
@ -328,8 +333,8 @@ void sf_binlog_writer_finish(SFBinlogWriterInfo *writer)
|
||||||
deal_binlog_records(writer->thread, wb_head, &last_timestamp);
|
deal_binlog_records(writer->thread, wb_head, &last_timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(writer->fw.file.name);
|
free(writer->fw.file.name.str);
|
||||||
writer->fw.file.name = NULL;
|
writer->fw.file.name.str = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (writer->fw.file.fd >= 0) {
|
if (writer->fw.file.fd >= 0) {
|
||||||
|
|
@ -342,6 +347,7 @@ static void *binlog_writer_func(void *arg)
|
||||||
{
|
{
|
||||||
SFBinlogWriterThread *thread;
|
SFBinlogWriterThread *thread;
|
||||||
SFBinlogWriterBuffer *wb_head;
|
SFBinlogWriterBuffer *wb_head;
|
||||||
|
uint32_t last_record_time;
|
||||||
uint32_t current_timestamp;
|
uint32_t current_timestamp;
|
||||||
uint32_t last_timestamp;
|
uint32_t last_timestamp;
|
||||||
int result;
|
int result;
|
||||||
|
|
@ -351,13 +357,12 @@ static void *binlog_writer_func(void *arg)
|
||||||
#ifdef OS_LINUX
|
#ifdef OS_LINUX
|
||||||
{
|
{
|
||||||
char thread_name[64];
|
char thread_name[64];
|
||||||
snprintf(thread_name, sizeof(thread_name),
|
fc_combine_two_strings(thread->name, "writer", '-', thread_name);
|
||||||
"%s-writer", thread->name);
|
|
||||||
prctl(PR_SET_NAME, thread_name);
|
prctl(PR_SET_NAME, thread_name);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
current_timestamp = last_timestamp = 0;
|
last_record_time = current_timestamp = last_timestamp = 0;
|
||||||
thread->running = true;
|
thread->running = true;
|
||||||
while (SF_G_CONTINUE_FLAG) {
|
while (SF_G_CONTINUE_FLAG) {
|
||||||
wb_head = (SFBinlogWriterBuffer *)fc_queue_pop_all(&thread->queue);
|
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,
|
if ((result=deal_binlog_records(thread, wb_head,
|
||||||
¤t_timestamp)) != 0)
|
&last_record_time)) != 0)
|
||||||
{
|
{
|
||||||
if (result != ERRNO_THREAD_EXIT) {
|
if (result != ERRNO_THREAD_EXIT) {
|
||||||
logCrit("file: "__FILE__", line: %d, "
|
logCrit("file: "__FILE__", line: %d, "
|
||||||
|
|
@ -379,6 +384,8 @@ static void *binlog_writer_func(void *arg)
|
||||||
|
|
||||||
if (fc_queue_empty(&thread->queue)) {
|
if (fc_queue_empty(&thread->queue)) {
|
||||||
current_timestamp = 0;
|
current_timestamp = 0;
|
||||||
|
} else {
|
||||||
|
current_timestamp = last_record_time;
|
||||||
}
|
}
|
||||||
if ((current_timestamp == 0 && last_timestamp != 0) ||
|
if ((current_timestamp == 0 && last_timestamp != 0) ||
|
||||||
(current_timestamp > last_timestamp))
|
(current_timestamp > last_timestamp))
|
||||||
|
|
@ -393,6 +400,12 @@ static void *binlog_writer_func(void *arg)
|
||||||
}
|
}
|
||||||
PTHREAD_MUTEX_UNLOCK(&thread->flow_ctrol.lcp.lock);
|
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;
|
thread->running = false;
|
||||||
|
|
@ -471,8 +484,9 @@ int sf_binlog_writer_init_by_version_ex(SFBinlogWriterInfo *writer,
|
||||||
|
|
||||||
int sf_binlog_writer_init_thread_ex(SFBinlogWriterThread *thread,
|
int sf_binlog_writer_init_thread_ex(SFBinlogWriterThread *thread,
|
||||||
const char *name, SFBinlogWriterInfo *writer, const short order_mode,
|
const char *name, SFBinlogWriterInfo *writer, const short order_mode,
|
||||||
const int max_delay, const int max_record_size, const bool
|
const int write_interval_ms, const int max_delay,
|
||||||
use_fixed_buffer_size, const bool passive_write)
|
const int max_record_size, const bool use_fixed_buffer_size,
|
||||||
|
const bool passive_write)
|
||||||
{
|
{
|
||||||
const int alloc_elements_once = 1024;
|
const int alloc_elements_once = 1024;
|
||||||
const int64_t alloc_elements_limit = 0;
|
const int64_t alloc_elements_limit = 0;
|
||||||
|
|
@ -482,10 +496,11 @@ int sf_binlog_writer_init_thread_ex(SFBinlogWriterThread *thread,
|
||||||
pthread_t tid;
|
pthread_t tid;
|
||||||
struct fast_mblock_object_callbacks callbacks;
|
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->order_mode = order_mode;
|
||||||
thread->use_fixed_buffer_size = use_fixed_buffer_size;
|
thread->use_fixed_buffer_size = use_fixed_buffer_size;
|
||||||
thread->passive_write = passive_write;
|
thread->passive_write = passive_write;
|
||||||
|
thread->write_interval_ms = write_interval_ms;
|
||||||
thread->flow_ctrol.max_delay = max_delay;
|
thread->flow_ctrol.max_delay = max_delay;
|
||||||
writer->fw.cfg.max_record_size = max_record_size;
|
writer->fw.cfg.max_record_size = max_record_size;
|
||||||
writer->thread = thread;
|
writer->thread = thread;
|
||||||
|
|
@ -612,10 +627,12 @@ int sf_binlog_writer_change_write_index(SFBinlogWriterInfo *writer,
|
||||||
SF_BINLOG_BUFFER_TYPE_SET_WRITE_INDEX, write_index);
|
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,
|
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)
|
int sf_binlog_writer_flush_file(SFBinlogWriterInfo *writer)
|
||||||
|
|
|
||||||
|
|
@ -72,6 +72,7 @@ typedef struct binlog_writer_thread {
|
||||||
bool use_fixed_buffer_size;
|
bool use_fixed_buffer_size;
|
||||||
bool passive_write;
|
bool passive_write;
|
||||||
char order_mode;
|
char order_mode;
|
||||||
|
int write_interval_ms;
|
||||||
struct {
|
struct {
|
||||||
int max_delay; //in seconds
|
int max_delay; //in seconds
|
||||||
volatile uint32_t last_timestamp;
|
volatile uint32_t last_timestamp;
|
||||||
|
|
@ -125,32 +126,33 @@ int sf_binlog_writer_init_by_version_ex(SFBinlogWriterInfo *writer,
|
||||||
|
|
||||||
int sf_binlog_writer_init_thread_ex(SFBinlogWriterThread *thread,
|
int sf_binlog_writer_init_thread_ex(SFBinlogWriterThread *thread,
|
||||||
const char *name, SFBinlogWriterInfo *writer, const short order_mode,
|
const char *name, SFBinlogWriterInfo *writer, const short order_mode,
|
||||||
const int max_delay, const int max_record_size, const bool
|
const int write_interval_ms, const int max_delay,
|
||||||
use_fixed_buffer_size, const bool passive_write);
|
const int max_record_size, const bool use_fixed_buffer_size,
|
||||||
|
const bool passive_write);
|
||||||
|
|
||||||
#define sf_binlog_writer_init_normal(writer, data_path, \
|
#define sf_binlog_writer_init_normal(writer, data_path, \
|
||||||
subdir_name, max_record_size, buffer_size) \
|
subdir_name, max_record_size, buffer_size) \
|
||||||
sf_binlog_writer_init_normal_ex(writer, data_path, subdir_name, \
|
sf_binlog_writer_init_normal_ex(writer, data_path, subdir_name, \
|
||||||
SF_BINLOG_FILE_PREFIX, max_record_size, buffer_size, \
|
SF_BINLOG_FILE_PREFIX_STR, max_record_size, buffer_size, \
|
||||||
SF_BINLOG_DEFAULT_ROTATE_SIZE, true)
|
SF_BINLOG_DEFAULT_ROTATE_SIZE, true)
|
||||||
|
|
||||||
#define sf_binlog_writer_init_by_version(writer, data_path, subdir_name, \
|
#define sf_binlog_writer_init_by_version(writer, data_path, subdir_name, \
|
||||||
max_record_size, next_version, buffer_size, ring_size) \
|
max_record_size, next_version, buffer_size, ring_size) \
|
||||||
sf_binlog_writer_init_by_version_ex(writer, data_path, subdir_name, \
|
sf_binlog_writer_init_by_version_ex(writer, data_path, subdir_name, \
|
||||||
SF_BINLOG_FILE_PREFIX, max_record_size, next_version, \
|
SF_BINLOG_FILE_PREFIX_STR, max_record_size, next_version, \
|
||||||
buffer_size, ring_size, SF_BINLOG_DEFAULT_ROTATE_SIZE, true)
|
buffer_size, ring_size, SF_BINLOG_DEFAULT_ROTATE_SIZE, true)
|
||||||
|
|
||||||
#define sf_binlog_writer_init_thread(thread, name, \
|
#define sf_binlog_writer_init_thread(thread, name, \
|
||||||
writer, max_delay, max_record_size) \
|
writer, write_interval_ms, max_delay, max_record_size) \
|
||||||
sf_binlog_writer_init_thread_ex(thread, name, writer, \
|
sf_binlog_writer_init_thread_ex(thread, name, writer, \
|
||||||
SF_BINLOG_THREAD_ORDER_MODE_FIXED, max_delay, \
|
SF_BINLOG_THREAD_ORDER_MODE_FIXED, write_interval_ms, \
|
||||||
max_record_size, true, false)
|
max_delay, max_record_size, true, false)
|
||||||
|
|
||||||
static inline int sf_binlog_writer_init_ex(SFBinlogWriterContext *context,
|
static inline int sf_binlog_writer_init_ex(SFBinlogWriterContext *context,
|
||||||
const char *data_path, const char *subdir_name,
|
const char *data_path, const char *subdir_name,
|
||||||
const char *file_prefix, const int buffer_size,
|
const char *file_prefix, const int buffer_size,
|
||||||
const int max_delay, const int max_record_size,
|
const int write_interval_ms, const int max_delay,
|
||||||
const bool call_fsync)
|
const int max_record_size, const bool call_fsync)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
if ((result=sf_binlog_writer_init_normal_ex(&context->writer, data_path,
|
if ((result=sf_binlog_writer_init_normal_ex(&context->writer, data_path,
|
||||||
|
|
@ -161,14 +163,14 @@ static inline int sf_binlog_writer_init_ex(SFBinlogWriterContext *context,
|
||||||
}
|
}
|
||||||
|
|
||||||
return sf_binlog_writer_init_thread(&context->thread, subdir_name,
|
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, \
|
#define sf_binlog_writer_init(context, data_path, subdir_name, \
|
||||||
buffer_size, max_delay, max_record_size) \
|
buffer_size, write_interval_ms, max_delay, max_record_size) \
|
||||||
sf_binlog_writer_init_ex(context, data_path, subdir_name, \
|
sf_binlog_writer_init_ex(context, data_path, subdir_name, \
|
||||||
SF_BINLOG_FILE_PREFIX, buffer_size, max_delay, \
|
SF_BINLOG_FILE_PREFIX_STR, buffer_size, write_interval_ms, \
|
||||||
max_record_size, true)
|
max_delay, max_record_size, true)
|
||||||
|
|
||||||
void sf_binlog_writer_finish(SFBinlogWriterInfo *writer);
|
void sf_binlog_writer_finish(SFBinlogWriterInfo *writer);
|
||||||
|
|
||||||
|
|
@ -233,7 +235,14 @@ static inline int sf_binlog_writer_get_thread_waiting_count(
|
||||||
return 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);
|
int sf_binlog_writer_flush_file(SFBinlogWriterInfo *writer);
|
||||||
|
|
||||||
|
|
@ -245,6 +254,9 @@ int sf_binlog_writer_notify_exit(SFBinlogWriterInfo *writer);
|
||||||
#define sf_binlog_writer_set_flags(writer, flags) \
|
#define sf_binlog_writer_set_flags(writer, flags) \
|
||||||
sf_file_writer_set_flags(&(writer)->fw, 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) \
|
#define sf_binlog_writer_get_last_version_ex(writer, log_level) \
|
||||||
sf_file_writer_get_last_version_ex(&(writer)->fw, log_level)
|
sf_file_writer_get_last_version_ex(&(writer)->fw, log_level)
|
||||||
|
|
||||||
|
|
@ -274,7 +286,9 @@ static inline SFBinlogWriterBuffer *sf_binlog_writer_alloc_buffer(
|
||||||
{
|
{
|
||||||
SFBinlogWriterBuffer *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;
|
buffer->type = SF_BINLOG_BUFFER_TYPE_WRITE_TO_FILE;
|
||||||
}
|
}
|
||||||
return buffer;
|
return buffer;
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ static int calc_cluster_config_sign(SFClusterConfig *cluster)
|
||||||
FastBuffer buffer;
|
FastBuffer buffer;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
if ((result=fast_buffer_init_ex(&buffer, 1024)) != 0) {
|
if ((result=fast_buffer_init1(&buffer, 1024)) != 0) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
fc_server_to_config_string(&cluster->server_cfg, &buffer);
|
fc_server_to_config_string(&cluster->server_cfg, &buffer);
|
||||||
|
|
|
||||||
|
|
@ -35,14 +35,14 @@ static int get_group_servers(SFConnectionManager *cm,
|
||||||
SFCMConnGroupEntry *group);
|
SFCMConnGroupEntry *group);
|
||||||
|
|
||||||
static ConnectionInfo *get_spec_connection(SFConnectionManager *cm,
|
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,
|
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,
|
static ConnectionInfo *make_connection(SFConnectionManager *cm,
|
||||||
FCAddressPtrArray *addr_array, int *err_no)
|
FCAddressPtrArray *addr_array, const bool shared, int *err_no)
|
||||||
{
|
{
|
||||||
FCAddressInfo **current;
|
FCAddressInfo **current;
|
||||||
FCAddressInfo **addr;
|
FCAddressInfo **addr;
|
||||||
|
|
@ -56,7 +56,7 @@ static ConnectionInfo *make_connection(SFConnectionManager *cm,
|
||||||
|
|
||||||
current = addr_array->addrs + addr_array->index;
|
current = addr_array->addrs + addr_array->index;
|
||||||
if ((conn=get_spec_connection(cm, &(*current)->conn,
|
if ((conn=get_spec_connection(cm, &(*current)->conn,
|
||||||
err_no)) != NULL)
|
shared, err_no)) != NULL)
|
||||||
{
|
{
|
||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
@ -72,7 +72,7 @@ static ConnectionInfo *make_connection(SFConnectionManager *cm,
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((conn=get_spec_connection(cm, &(*addr)->conn,
|
if ((conn=get_spec_connection(cm, &(*addr)->conn,
|
||||||
err_no)) != NULL)
|
shared, err_no)) != NULL)
|
||||||
{
|
{
|
||||||
addr_array->index = addr - addr_array->addrs;
|
addr_array->index = addr - addr_array->addrs;
|
||||||
return conn;
|
return conn;
|
||||||
|
|
@ -83,13 +83,13 @@ static ConnectionInfo *make_connection(SFConnectionManager *cm,
|
||||||
}
|
}
|
||||||
|
|
||||||
static ConnectionInfo *get_server_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;
|
FCAddressPtrArray *addr_array;
|
||||||
ConnectionInfo *conn;
|
ConnectionInfo *conn;
|
||||||
|
|
||||||
addr_array = &server->group_addrs[cm->server_group_index].address_array;
|
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, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"%s server id: %d, address count: %d, get_server_connection fail",
|
"%s server id: %d, address count: %d, get_server_connection fail",
|
||||||
__LINE__, cm->module_name, server->id, addr_array->count);
|
__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,
|
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;
|
SFCMServerArray *server_array;
|
||||||
ConnectionInfo *conn;
|
ConnectionInfo *conn;
|
||||||
|
|
@ -110,7 +110,7 @@ static ConnectionInfo *get_connection(SFConnectionManager *cm,
|
||||||
server_hash_code = rand();
|
server_hash_code = rand();
|
||||||
server_index = server_hash_code % server_array->count;
|
server_index = server_hash_code % server_array->count;
|
||||||
if ((conn=make_connection(cm, server_array->servers[server_index].
|
if ((conn=make_connection(cm, server_array->servers[server_index].
|
||||||
addr_array, err_no)) != NULL)
|
addr_array, shared, err_no)) != NULL)
|
||||||
{
|
{
|
||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
@ -122,7 +122,7 @@ static ConnectionInfo *get_connection(SFConnectionManager *cm,
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((conn=make_connection(cm, server_array->servers[i].
|
if ((conn=make_connection(cm, server_array->servers[i].
|
||||||
addr_array, err_no)) != NULL)
|
addr_array, shared, err_no)) != NULL)
|
||||||
{
|
{
|
||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
@ -176,7 +176,7 @@ static inline bool alive_array_cas(SFConnectionManager *cm,
|
||||||
push_to_detect_queue(cm, group, new_alives);
|
push_to_detect_queue(cm, group, new_alives);
|
||||||
fast_mblock_delay_free_object(&cm->sptr_array_allocator, old_alives,
|
fast_mblock_delay_free_object(&cm->sptr_array_allocator, old_alives,
|
||||||
(cm->common_cfg->connect_timeout + cm->common_cfg->
|
(cm->common_cfg->connect_timeout + cm->common_cfg->
|
||||||
network_timeout) * group->all.count);
|
network_timeout) * group->all.count * 4);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
fast_mblock_free_object(&cm->sptr_array_allocator, new_alives);
|
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,
|
static inline ConnectionInfo *make_master_connection(SFConnectionManager *cm,
|
||||||
SFCMConnGroupEntry *group, int *err_no)
|
SFCMConnGroupEntry *group, const bool shared, int *err_no)
|
||||||
{
|
{
|
||||||
SFCMServerEntry *master;
|
SFCMServerEntry *master;
|
||||||
ConnectionInfo *conn;
|
ConnectionInfo *conn;
|
||||||
|
|
@ -234,7 +234,7 @@ static inline ConnectionInfo *make_master_connection(SFConnectionManager *cm,
|
||||||
master = (SFCMServerEntry *)FC_ATOMIC_GET(group->master);
|
master = (SFCMServerEntry *)FC_ATOMIC_GET(group->master);
|
||||||
if (master != NULL) {
|
if (master != NULL) {
|
||||||
if ((conn=make_connection(cm, master->addr_array,
|
if ((conn=make_connection(cm, master->addr_array,
|
||||||
err_no)) != NULL)
|
shared, err_no)) != NULL)
|
||||||
{
|
{
|
||||||
alives = (SFCMServerPtrArray *)FC_ATOMIC_GET(group->alives);
|
alives = (SFCMServerPtrArray *)FC_ATOMIC_GET(group->alives);
|
||||||
set_connection_params(conn, master, 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,
|
static inline ConnectionInfo *make_readable_connection(SFConnectionManager *cm,
|
||||||
SFCMConnGroupEntry *group, SFCMServerPtrArray *alives,
|
SFCMConnGroupEntry *group, SFCMServerPtrArray *alives,
|
||||||
const int index, int *err_no)
|
const int index, const bool shared, int *err_no)
|
||||||
{
|
{
|
||||||
ConnectionInfo *conn;
|
ConnectionInfo *conn;
|
||||||
|
|
||||||
if ((conn=make_connection(cm, alives->servers[index]->
|
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]);
|
remove_from_alives(cm, group, alives, alives->servers[index]);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -270,7 +270,7 @@ static inline ConnectionInfo *make_readable_connection(SFConnectionManager *cm,
|
||||||
}
|
}
|
||||||
|
|
||||||
static ConnectionInfo *get_master_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;
|
SFCMConnGroupEntry *group;
|
||||||
ConnectionInfo *conn;
|
ConnectionInfo *conn;
|
||||||
|
|
@ -283,7 +283,7 @@ static ConnectionInfo *get_master_connection(SFConnectionManager *cm,
|
||||||
&cm->common_cfg->net_retry_cfg.connect);
|
&cm->common_cfg->net_retry_cfg.connect);
|
||||||
retry_count = 0;
|
retry_count = 0;
|
||||||
while (1) {
|
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;
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -313,7 +313,7 @@ static ConnectionInfo *get_master_connection(SFConnectionManager *cm,
|
||||||
}
|
}
|
||||||
|
|
||||||
static ConnectionInfo *get_readable_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;
|
SFCMConnGroupEntry *group;
|
||||||
SFCMServerPtrArray *alives;
|
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) ||
|
if ((cm->common_cfg->read_rule == sf_data_read_rule_master_only) ||
|
||||||
(group->all.count == 1))
|
(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,
|
sf_init_net_retry_interval_context(&net_retry_ctx,
|
||||||
|
|
@ -338,14 +338,14 @@ static ConnectionInfo *get_readable_connection(SFConnectionManager *cm,
|
||||||
if (alives->count > 0) {
|
if (alives->count > 0) {
|
||||||
index = rand() % alives->count;
|
index = rand() % alives->count;
|
||||||
if ((conn=make_readable_connection(cm, group, alives,
|
if ((conn=make_readable_connection(cm, group, alives,
|
||||||
index, err_no)) != NULL)
|
index, shared, err_no)) != NULL)
|
||||||
{
|
{
|
||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cm->common_cfg->read_rule == sf_data_read_rule_slave_first) {
|
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;
|
return conn;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -400,7 +400,7 @@ static void close_connection(SFConnectionManager *cm, ConnectionInfo *conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
static ConnectionInfo *get_leader_connection(SFConnectionManager *cm,
|
static ConnectionInfo *get_leader_connection(SFConnectionManager *cm,
|
||||||
FCServerInfo *server, int *err_no)
|
FCServerInfo *server, const bool shared, int *err_no)
|
||||||
{
|
{
|
||||||
ConnectionInfo *conn;
|
ConnectionInfo *conn;
|
||||||
SFClientServerEntry leader;
|
SFClientServerEntry leader;
|
||||||
|
|
@ -416,7 +416,7 @@ static ConnectionInfo *get_leader_connection(SFConnectionManager *cm,
|
||||||
while (1) {
|
while (1) {
|
||||||
do {
|
do {
|
||||||
if ((conn=get_server_connection(cm, server,
|
if ((conn=get_server_connection(cm, server,
|
||||||
err_no)) == NULL)
|
shared, err_no)) == NULL)
|
||||||
{
|
{
|
||||||
connect_fails++;
|
connect_fails++;
|
||||||
break;
|
break;
|
||||||
|
|
@ -433,8 +433,9 @@ static ConnectionInfo *get_leader_connection(SFConnectionManager *cm,
|
||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
release_connection(cm, conn);
|
release_connection(cm, conn);
|
||||||
|
|
||||||
if ((conn=get_spec_connection(cm, &leader.conn,
|
if ((conn=get_spec_connection(cm, &leader.conn,
|
||||||
err_no)) == NULL)
|
shared, err_no)) == NULL)
|
||||||
{
|
{
|
||||||
if (cm->server_cfg != NULL) {
|
if (cm->server_cfg != NULL) {
|
||||||
FCServerInfo *ls;
|
FCServerInfo *ls;
|
||||||
|
|
@ -445,7 +446,7 @@ static ConnectionInfo *get_leader_connection(SFConnectionManager *cm,
|
||||||
address_array.count > 1)
|
address_array.count > 1)
|
||||||
{
|
{
|
||||||
if ((conn=get_server_connection(cm, ls,
|
if ((conn=get_server_connection(cm, ls,
|
||||||
err_no)) != NULL)
|
shared, err_no)) != NULL)
|
||||||
{
|
{
|
||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
@ -529,12 +530,19 @@ int sf_connection_manager_init_ex(SFConnectionManager *cm,
|
||||||
} extra_params;
|
} extra_params;
|
||||||
FCServerGroupInfo *server_group;
|
FCServerGroupInfo *server_group;
|
||||||
|
|
||||||
int htable_init_capacity;
|
int htable_capacity;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
htable_init_capacity = 4 * server_count;
|
if (server_count <= 4) {
|
||||||
if (htable_init_capacity < 256) {
|
htable_capacity = 16;
|
||||||
htable_init_capacity = 256;
|
} 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,
|
if ((server_group=fc_server_get_group_by_index(server_cfg,
|
||||||
|
|
@ -554,7 +562,7 @@ int sf_connection_manager_init_ex(SFConnectionManager *cm,
|
||||||
extra_params.ptr = &extra_params.holder;
|
extra_params.ptr = &extra_params.holder;
|
||||||
}
|
}
|
||||||
if ((result=conn_pool_init_ex1(&cm->cpool, common_cfg->connect_timeout,
|
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,
|
connect_done_callback, args,
|
||||||
sf_cm_validate_connection_callback, cm,
|
sf_cm_validate_connection_callback, cm,
|
||||||
sizeof(SFConnectionParameters),
|
sizeof(SFConnectionParameters),
|
||||||
|
|
@ -770,6 +778,7 @@ static int do_get_group_servers(SFConnectionManager *cm,
|
||||||
static int get_group_servers_by_active(SFConnectionManager *cm,
|
static int get_group_servers_by_active(SFConnectionManager *cm,
|
||||||
SFCMConnGroupEntry *group)
|
SFCMConnGroupEntry *group)
|
||||||
{
|
{
|
||||||
|
const bool shared = true;
|
||||||
SFCMServerPtrArray *alives;
|
SFCMServerPtrArray *alives;
|
||||||
SFCMServerEntry **server;
|
SFCMServerEntry **server;
|
||||||
SFCMServerEntry **end;
|
SFCMServerEntry **end;
|
||||||
|
|
@ -785,7 +794,7 @@ static int get_group_servers_by_active(SFConnectionManager *cm,
|
||||||
end = alives->servers + alives->count;
|
end = alives->servers + alives->count;
|
||||||
for (server=alives->servers; server<end; server++) {
|
for (server=alives->servers; server<end; server++) {
|
||||||
if ((conn=make_connection(cm, (*server)->addr_array,
|
if ((conn=make_connection(cm, (*server)->addr_array,
|
||||||
&result)) == NULL)
|
shared, &result)) == NULL)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -803,6 +812,7 @@ static int get_group_servers_by_active(SFConnectionManager *cm,
|
||||||
static int get_group_servers_by_all(SFConnectionManager *cm,
|
static int get_group_servers_by_all(SFConnectionManager *cm,
|
||||||
SFCMConnGroupEntry *group)
|
SFCMConnGroupEntry *group)
|
||||||
{
|
{
|
||||||
|
const bool shared = true;
|
||||||
SFCMServerEntry *server;
|
SFCMServerEntry *server;
|
||||||
SFCMServerEntry *end;
|
SFCMServerEntry *end;
|
||||||
ConnectionInfo *conn;
|
ConnectionInfo *conn;
|
||||||
|
|
@ -820,7 +830,7 @@ static int get_group_servers_by_all(SFConnectionManager *cm,
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((conn=make_connection(cm, server->addr_array,
|
if ((conn=make_connection(cm, server->addr_array,
|
||||||
&result)) == NULL)
|
shared, &result)) == NULL)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,16 +26,17 @@
|
||||||
struct sf_connection_manager;
|
struct sf_connection_manager;
|
||||||
|
|
||||||
typedef ConnectionInfo *(*sf_get_connection_func)(
|
typedef ConnectionInfo *(*sf_get_connection_func)(
|
||||||
struct sf_connection_manager *cm,
|
struct sf_connection_manager *cm, const int group_index,
|
||||||
const int group_index, int *err_no);
|
const bool shared, int *err_no);
|
||||||
|
|
||||||
typedef ConnectionInfo *(*sf_get_server_connection_func)(
|
typedef ConnectionInfo *(*sf_get_server_connection_func)(
|
||||||
struct sf_connection_manager *cm,
|
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)(
|
typedef ConnectionInfo *(*sf_get_spec_connection_func)(
|
||||||
struct sf_connection_manager *cm,
|
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)(
|
typedef void (*sf_release_connection_func)(
|
||||||
struct sf_connection_manager *cm, ConnectionInfo *conn);
|
struct sf_connection_manager *cm, ConnectionInfo *conn);
|
||||||
|
|
|
||||||
|
|
@ -96,6 +96,8 @@
|
||||||
#define SF_BINLOG_SOURCE_USER 'U' //by user call
|
#define SF_BINLOG_SOURCE_USER 'U' //by user call
|
||||||
#define SF_BINLOG_SOURCE_REPLAY 'R' //by binlog replay
|
#define SF_BINLOG_SOURCE_REPLAY 'R' //by binlog replay
|
||||||
|
|
||||||
|
#define SF_LOG_SCHEDULE_ENTRIES_COUNT 3
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -34,49 +34,108 @@
|
||||||
#include "sf_func.h"
|
#include "sf_func.h"
|
||||||
#include "sf_file_writer.h"
|
#include "sf_file_writer.h"
|
||||||
|
|
||||||
#define BINLOG_INDEX_ITEM_START_INDEX "start_index"
|
#define BINLOG_INDEX_ITEM_START_INDEX_STR "start_index"
|
||||||
#define BINLOG_INDEX_ITEM_CURRENT_WRITE "current_write"
|
#define BINLOG_INDEX_ITEM_START_INDEX_LEN \
|
||||||
#define BINLOG_INDEX_ITEM_CURRENT_COMPRESS "current_compress"
|
(sizeof(BINLOG_INDEX_ITEM_START_INDEX_STR) - 1)
|
||||||
|
|
||||||
#define GET_BINLOG_FILENAME(writer) \
|
#define BINLOG_INDEX_ITEM_CURRENT_WRITE_STR "current_write"
|
||||||
sprintf(writer->file.name, "%s/%s/%s"SF_BINLOG_FILE_EXT_FMT, \
|
#define BINLOG_INDEX_ITEM_CURRENT_WRITE_LEN \
|
||||||
writer->cfg.data_path, writer->cfg.subdir_name, \
|
(sizeof(BINLOG_INDEX_ITEM_CURRENT_WRITE_STR) - 1)
|
||||||
writer->cfg.file_prefix, writer->binlog.last_index)
|
|
||||||
|
|
||||||
#define GET_BINLOG_INDEX_FILENAME_EX(data_path, \
|
#define BINLOG_INDEX_ITEM_CURRENT_COMPRESS_STR "current_compress"
|
||||||
subdir_name, file_prefix, filename, size) \
|
#define BINLOG_INDEX_ITEM_CURRENT_COMPRESS_LEN \
|
||||||
snprintf(filename, size, "%s/%s/%s_index.dat", \
|
(sizeof(BINLOG_INDEX_ITEM_CURRENT_COMPRESS_STR) - 1)
|
||||||
data_path, subdir_name, file_prefix)
|
|
||||||
|
|
||||||
#define GET_BINLOG_INDEX_FILENAME(writer, filename, size) \
|
static inline void sf_file_writer_get_binlog_filename(SFFileWriterInfo *writer)
|
||||||
GET_BINLOG_INDEX_FILENAME_EX(writer->cfg.data_path, \
|
{
|
||||||
writer->cfg.subdir_name, writer->cfg.file_prefix, filename, size)
|
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 *sf_file_writer_get_index_filename(const char *data_path,
|
||||||
const char *subdir_name, char *filename, const int size)
|
const char *subdir_name, char *filename, const int size)
|
||||||
{
|
{
|
||||||
GET_BINLOG_INDEX_FILENAME_EX(data_path, subdir_name,
|
sf_file_writer_get_index_filename_ex(data_path, subdir_name,
|
||||||
SF_BINLOG_FILE_PREFIX, filename, size);
|
SF_BINLOG_FILE_PREFIX_STR, SF_BINLOG_FILE_PREFIX_LEN,
|
||||||
|
filename, size);
|
||||||
return filename;
|
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 filename[PATH_MAX];
|
||||||
char buff[256];
|
char buff[256];
|
||||||
|
char *p;
|
||||||
int result;
|
int result;
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
GET_BINLOG_INDEX_FILENAME(writer, filename, sizeof(filename));
|
sf_file_writer_get_index_filename_ex(data_path, subdir_name, file_prefix,
|
||||||
len = sprintf(buff, "%s=%d\n"
|
strlen(file_prefix), filename, sizeof(filename));
|
||||||
"%s=%d\n"
|
p = buff;
|
||||||
"%s=%d\n",
|
memcpy(p, BINLOG_INDEX_ITEM_START_INDEX_STR,
|
||||||
BINLOG_INDEX_ITEM_START_INDEX,
|
BINLOG_INDEX_ITEM_START_INDEX_LEN);
|
||||||
writer->binlog.start_index,
|
p += BINLOG_INDEX_ITEM_START_INDEX_LEN;
|
||||||
BINLOG_INDEX_ITEM_CURRENT_WRITE,
|
*p++ = '=';
|
||||||
writer->binlog.last_index,
|
p += fc_itoa(start_index, p);
|
||||||
BINLOG_INDEX_ITEM_CURRENT_COMPRESS,
|
*p++ = '\n';
|
||||||
writer->binlog.compress_index);
|
|
||||||
|
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) {
|
if ((result=safeWriteToFile(filename, buff, len)) != 0) {
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"write to file \"%s\" fail, errno: %d, error info: %s",
|
"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;
|
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,
|
static int get_binlog_info_from_file(const char *data_path,
|
||||||
const char *subdir_name, int *start_index,
|
const char *subdir_name, int *start_index,
|
||||||
int *last_index, int *compress_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;
|
IniContext ini_context;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
GET_BINLOG_INDEX_FILENAME_EX(data_path,
|
sf_file_writer_get_index_filename_ex(data_path, subdir_name,
|
||||||
subdir_name, SF_BINLOG_FILE_PREFIX,
|
SF_BINLOG_FILE_PREFIX_STR, SF_BINLOG_FILE_PREFIX_LEN,
|
||||||
full_filename, sizeof(full_filename));
|
full_filename, sizeof(full_filename));
|
||||||
if (access(full_filename, F_OK) != 0) {
|
if (access(full_filename, F_OK) != 0) {
|
||||||
return errno != 0 ? errno : EPERM;
|
return errno != 0 ? errno : EPERM;
|
||||||
|
|
@ -109,13 +176,13 @@ static int get_binlog_info_from_file(const char *data_path,
|
||||||
}
|
}
|
||||||
|
|
||||||
*start_index = iniGetIntValue(NULL,
|
*start_index = iniGetIntValue(NULL,
|
||||||
BINLOG_INDEX_ITEM_START_INDEX,
|
BINLOG_INDEX_ITEM_START_INDEX_STR,
|
||||||
&ini_context, 0);
|
&ini_context, 0);
|
||||||
*last_index = iniGetIntValue(NULL,
|
*last_index = iniGetIntValue(NULL,
|
||||||
BINLOG_INDEX_ITEM_CURRENT_WRITE,
|
BINLOG_INDEX_ITEM_CURRENT_WRITE_STR,
|
||||||
&ini_context, 0);
|
&ini_context, 0);
|
||||||
*compress_index = iniGetIntValue(NULL,
|
*compress_index = iniGetIntValue(NULL,
|
||||||
BINLOG_INDEX_ITEM_CURRENT_COMPRESS,
|
BINLOG_INDEX_ITEM_CURRENT_COMPRESS_STR,
|
||||||
&ini_context, 0);
|
&ini_context, 0);
|
||||||
|
|
||||||
iniFreeContext(&ini_context);
|
iniFreeContext(&ini_context);
|
||||||
|
|
@ -165,14 +232,14 @@ static int open_writable_binlog(SFFileWriterInfo *writer)
|
||||||
close(writer->file.fd);
|
close(writer->file.fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
GET_BINLOG_FILENAME(writer);
|
sf_file_writer_get_binlog_filename(writer);
|
||||||
writer->file.fd = open(writer->file.name, O_WRONLY |
|
writer->file.fd = open(writer->file.name.str, O_WRONLY |
|
||||||
O_CREAT | O_APPEND | O_CLOEXEC, 0644);
|
O_CREAT | O_APPEND | O_CLOEXEC, 0644);
|
||||||
if (writer->file.fd < 0) {
|
if (writer->file.fd < 0) {
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"open file \"%s\" fail, "
|
"open file \"%s\" fail, "
|
||||||
"errno: %d, error info: %s",
|
"errno: %d, error info: %s",
|
||||||
__LINE__, writer->file.name,
|
__LINE__, writer->file.name.str,
|
||||||
errno, STRERROR(errno));
|
errno, STRERROR(errno));
|
||||||
return errno != 0 ? errno : EACCES;
|
return errno != 0 ? errno : EACCES;
|
||||||
}
|
}
|
||||||
|
|
@ -182,7 +249,7 @@ static int open_writable_binlog(SFFileWriterInfo *writer)
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"lseek file \"%s\" fail, "
|
"lseek file \"%s\" fail, "
|
||||||
"errno: %d, error info: %s",
|
"errno: %d, error info: %s",
|
||||||
__LINE__, writer->file.name,
|
__LINE__, writer->file.name.str,
|
||||||
errno, STRERROR(errno));
|
errno, STRERROR(errno));
|
||||||
return errno != 0 ? errno : EIO;
|
return errno != 0 ? errno : EIO;
|
||||||
}
|
}
|
||||||
|
|
@ -192,23 +259,24 @@ static int open_writable_binlog(SFFileWriterInfo *writer)
|
||||||
|
|
||||||
static int open_next_binlog(SFFileWriterInfo *writer)
|
static int open_next_binlog(SFFileWriterInfo *writer)
|
||||||
{
|
{
|
||||||
GET_BINLOG_FILENAME(writer);
|
sf_file_writer_get_binlog_filename(writer);
|
||||||
if (access(writer->file.name, F_OK) == 0) {
|
if (access(writer->file.name.str, F_OK) == 0) {
|
||||||
char bak_filename[PATH_MAX];
|
char bak_filename[PATH_MAX];
|
||||||
char date_str[32];
|
char date_str[32];
|
||||||
|
|
||||||
snprintf(bak_filename, sizeof(bak_filename), "%s.%s",
|
formatDatetime(g_current_time, "%Y%m%d%H%M%S",
|
||||||
writer->file.name, formatDatetime(g_current_time,
|
date_str, sizeof(date_str));
|
||||||
"%Y%m%d%H%M%S", date_str, sizeof(date_str)));
|
fc_combine_two_strings(writer->file.name.str,
|
||||||
if (rename(writer->file.name, bak_filename) == 0) {
|
date_str, '.', bak_filename);
|
||||||
|
if (rename(writer->file.name.str, bak_filename) == 0) {
|
||||||
logWarning("file: "__FILE__", line: %d, "
|
logWarning("file: "__FILE__", line: %d, "
|
||||||
"binlog file %s exist, rename to %s",
|
"binlog file %s exist, rename to %s",
|
||||||
__LINE__, writer->file.name, bak_filename);
|
__LINE__, writer->file.name.str, bak_filename);
|
||||||
} else {
|
} else {
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"rename binlog %s to backup %s fail, "
|
"rename binlog %s to backup %s fail, "
|
||||||
"errno: %d, error info: %s",
|
"errno: %d, error info: %s",
|
||||||
__LINE__, writer->file.name, bak_filename,
|
__LINE__, writer->file.name.str, bak_filename,
|
||||||
errno, STRERROR(errno));
|
errno, STRERROR(errno));
|
||||||
return errno != 0 ? errno : EPERM;
|
return errno != 0 ? errno : EPERM;
|
||||||
}
|
}
|
||||||
|
|
@ -227,7 +295,7 @@ static int do_write_to_file(SFFileWriterInfo *writer,
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"write to binlog file \"%s\" fail, "
|
"write to binlog file \"%s\" fail, "
|
||||||
"errno: %d, error info: %s",
|
"errno: %d, error info: %s",
|
||||||
__LINE__, writer->file.name,
|
__LINE__, writer->file.name.str,
|
||||||
result, STRERROR(result));
|
result, STRERROR(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -237,17 +305,22 @@ static int do_write_to_file(SFFileWriterInfo *writer,
|
||||||
result = errno != 0 ? errno : EIO;
|
result = errno != 0 ? errno : EIO;
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"fsync to binlog file \"%s\" fail, errno: %d, "
|
"fsync to binlog file \"%s\" fail, errno: %d, "
|
||||||
"error info: %s", __LINE__, writer->file.name,
|
"error info: %s", __LINE__, writer->file.name.str,
|
||||||
result, STRERROR(result));
|
result, STRERROR(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
writer->file.size += len;
|
writer->file.size += len;
|
||||||
|
if (writer->write_done_callback.func != NULL) {
|
||||||
|
writer->write_done_callback.func(writer,
|
||||||
|
writer->write_done_callback.args);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int check_write_to_file(SFFileWriterInfo *writer,
|
int sf_file_writer_direct_write(SFFileWriterInfo *writer,
|
||||||
char *buff, const int len)
|
char *buff, const int len)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
|
@ -266,7 +339,7 @@ static int check_write_to_file(SFFileWriterInfo *writer,
|
||||||
if (result != 0) {
|
if (result != 0) {
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"open binlog file \"%s\" fail",
|
"open binlog file \"%s\" fail",
|
||||||
__LINE__, writer->file.name);
|
__LINE__, writer->file.name.str);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -283,7 +356,7 @@ int sf_file_writer_flush(SFFileWriterInfo *writer)
|
||||||
return 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)
|
binlog_buffer.buff, len)) == 0)
|
||||||
{
|
{
|
||||||
if (writer->flags & SF_FILE_WRITER_FLAGS_WANT_DONE_VERSION) {
|
if (writer->flags & SF_FILE_WRITER_FLAGS_WANT_DONE_VERSION) {
|
||||||
|
|
@ -309,7 +382,7 @@ int sf_file_writer_fsync(SFFileWriterInfo *writer)
|
||||||
result = errno != 0 ? errno : EIO;
|
result = errno != 0 ? errno : EIO;
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"fsync to binlog file \"%s\" fail, errno: %d, "
|
"fsync to binlog file \"%s\" fail, errno: %d, "
|
||||||
"error info: %s", __LINE__, writer->file.name,
|
"error info: %s", __LINE__, writer->file.name.str,
|
||||||
result, STRERROR(result));
|
result, STRERROR(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -349,8 +422,8 @@ int sf_file_writer_deal_versioned_buffer(SFFileWriterInfo *writer,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((result=check_write_to_file(writer, buffer->buff,
|
if ((result=sf_file_writer_direct_write(writer, buffer->
|
||||||
buffer->length)) == 0)
|
buff, buffer->length)) == 0)
|
||||||
{
|
{
|
||||||
if (writer->flags & SF_FILE_WRITER_FLAGS_WANT_DONE_VERSION) {
|
if (writer->flags & SF_FILE_WRITER_FLAGS_WANT_DONE_VERSION) {
|
||||||
writer->last_versions.pending = version;
|
writer->last_versions.pending = version;
|
||||||
|
|
@ -384,7 +457,8 @@ int sf_file_writer_deal_versioned_buffer(SFFileWriterInfo *writer,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sf_file_writer_save_buffer(SFFileWriterInfo *writer, const int length)
|
int sf_file_writer_save_buffer_ex(SFFileWriterInfo *writer,
|
||||||
|
const int length, const bool flush)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
|
|
@ -398,9 +472,8 @@ int sf_file_writer_save_buffer(SFFileWriterInfo *writer, const int length)
|
||||||
}
|
}
|
||||||
|
|
||||||
writer->binlog_buffer.data_end += length;
|
writer->binlog_buffer.data_end += length;
|
||||||
|
if (flush || SF_BINLOG_BUFFER_PRODUCER_BUFF_REMAIN(writer->
|
||||||
if (SF_BINLOG_BUFFER_PRODUCER_BUFF_REMAIN(writer->binlog_buffer) <
|
binlog_buffer) < writer->cfg.max_record_size)
|
||||||
writer->cfg.max_record_size)
|
|
||||||
{
|
{
|
||||||
return sf_file_writer_flush(writer);
|
return sf_file_writer_flush(writer);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -422,6 +495,7 @@ int sf_file_writer_init(SFFileWriterInfo *writer, const char *data_path,
|
||||||
writer->last_versions.pending = 0;
|
writer->last_versions.pending = 0;
|
||||||
writer->last_versions.done = 0;
|
writer->last_versions.done = 0;
|
||||||
writer->flags = 0;
|
writer->flags = 0;
|
||||||
|
sf_file_writer_set_write_done_callback(writer, NULL, NULL);
|
||||||
if ((result=sf_binlog_buffer_init(&writer->
|
if ((result=sf_binlog_buffer_init(&writer->
|
||||||
binlog_buffer, buffer_size)) != 0)
|
binlog_buffer, buffer_size)) != 0)
|
||||||
{
|
{
|
||||||
|
|
@ -432,8 +506,7 @@ int sf_file_writer_init(SFFileWriterInfo *writer, const char *data_path,
|
||||||
writer->cfg.call_fsync = call_fsync;
|
writer->cfg.call_fsync = call_fsync;
|
||||||
writer->cfg.file_rotate_size = file_rotate_size;
|
writer->cfg.file_rotate_size = file_rotate_size;
|
||||||
writer->cfg.data_path = data_path;
|
writer->cfg.data_path = data_path;
|
||||||
path_len = snprintf(filepath, sizeof(filepath),
|
path_len = fc_combine_full_filepath(data_path, subdir_name, filepath);
|
||||||
"%s/%s", data_path, subdir_name);
|
|
||||||
if ((result=fc_check_mkdir_ex(filepath, 0775, &create)) != 0) {
|
if ((result=fc_check_mkdir_ex(filepath, 0775, &create)) != 0) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -442,14 +515,11 @@ int sf_file_writer_init(SFFileWriterInfo *writer, const char *data_path,
|
||||||
}
|
}
|
||||||
|
|
||||||
writer->file.fd = -1;
|
writer->file.fd = -1;
|
||||||
snprintf(writer->cfg.subdir_name,
|
fc_safe_strcpy(writer->cfg.subdir_name, subdir_name);
|
||||||
sizeof(writer->cfg.subdir_name),
|
fc_safe_strcpy(writer->cfg.file_prefix, file_prefix);
|
||||||
"%s", subdir_name);
|
writer->file.name.size = path_len + 32;
|
||||||
snprintf(writer->cfg.file_prefix,
|
writer->file.name.str = (char *)fc_malloc(writer->file.name.size);
|
||||||
sizeof(writer->cfg.file_prefix),
|
if (writer->file.name.str == NULL) {
|
||||||
"%s", file_prefix);
|
|
||||||
writer->file.name = (char *)fc_malloc(path_len + 32);
|
|
||||||
if (writer->file.name == NULL) {
|
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -470,9 +540,9 @@ void sf_file_writer_destroy(SFFileWriterInfo *writer)
|
||||||
close(writer->file.fd);
|
close(writer->file.fd);
|
||||||
writer->file.fd = -1;
|
writer->file.fd = -1;
|
||||||
}
|
}
|
||||||
if (writer->file.name != NULL) {
|
if (writer->file.name.str != NULL) {
|
||||||
free(writer->file.name);
|
free(writer->file.name.str);
|
||||||
writer->file.name = NULL;
|
writer->file.name.str = NULL;
|
||||||
}
|
}
|
||||||
sf_binlog_buffer_destroy(&writer->binlog_buffer);
|
sf_binlog_buffer_destroy(&writer->binlog_buffer);
|
||||||
}
|
}
|
||||||
|
|
@ -616,3 +686,22 @@ int sf_file_writer_get_last_lines(const char *data_path,
|
||||||
|
|
||||||
return 0;
|
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);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,17 +24,24 @@
|
||||||
#define SF_FILE_WRITER_FLAGS_WANT_DONE_VERSION 1
|
#define SF_FILE_WRITER_FLAGS_WANT_DONE_VERSION 1
|
||||||
|
|
||||||
#define SF_BINLOG_SUBDIR_NAME_SIZE 128
|
#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_DEFAULT_ROTATE_SIZE (1024 * 1024 * 1024)
|
||||||
#define SF_BINLOG_NEVER_ROTATE_FILE 0
|
#define SF_BINLOG_NEVER_ROTATE_FILE 0
|
||||||
#define SF_BINLOG_FILE_PREFIX "binlog"
|
#define SF_BINLOG_FILE_PREFIX_STR "binlog"
|
||||||
#define SF_BINLOG_FILE_EXT_FMT ".%06d"
|
#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"
|
||||||
|
|
||||||
|
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 {
|
typedef struct sf_file_writer_info {
|
||||||
struct {
|
struct {
|
||||||
const char *data_path;
|
const char *data_path;
|
||||||
char subdir_name[SF_BINLOG_SUBDIR_NAME_SIZE];
|
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;
|
int64_t file_rotate_size;
|
||||||
int max_record_size;
|
int max_record_size;
|
||||||
bool call_fsync;
|
bool call_fsync;
|
||||||
|
|
@ -48,8 +55,11 @@ typedef struct sf_file_writer_info {
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
int fd;
|
int fd;
|
||||||
int64_t size;
|
int64_t size; //file size
|
||||||
char *name;
|
struct {
|
||||||
|
char *str;
|
||||||
|
int size;
|
||||||
|
} name;
|
||||||
} file;
|
} file;
|
||||||
|
|
||||||
int64_t total_count;
|
int64_t total_count;
|
||||||
|
|
@ -60,6 +70,12 @@ typedef struct sf_file_writer_info {
|
||||||
int64_t pending;
|
int64_t pending;
|
||||||
volatile int64_t done;
|
volatile int64_t done;
|
||||||
} last_versions;
|
} last_versions;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
sf_file_write_done_callback func;
|
||||||
|
void *args;
|
||||||
|
} write_done_callback;
|
||||||
|
|
||||||
} SFFileWriterInfo;
|
} SFFileWriterInfo;
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
@ -73,6 +89,9 @@ int sf_file_writer_init(SFFileWriterInfo *writer, const char *data_path,
|
||||||
|
|
||||||
void sf_file_writer_destroy(SFFileWriterInfo *writer);
|
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,
|
int sf_file_writer_deal_versioned_buffer(SFFileWriterInfo *writer,
|
||||||
BufferInfo *buffer, const int64_t version);
|
BufferInfo *buffer, const int64_t version);
|
||||||
|
|
||||||
|
|
@ -89,7 +108,22 @@ int sf_file_writer_fsync(SFFileWriterInfo *writer);
|
||||||
#define SF_FILE_WRITER_NEXT_DATA_VERSION(writer) \
|
#define SF_FILE_WRITER_NEXT_DATA_VERSION(writer) \
|
||||||
++((writer)->last_versions.pending)
|
++((writer)->last_versions.pending)
|
||||||
|
|
||||||
int sf_file_writer_save_buffer(SFFileWriterInfo *writer, const int length);
|
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(
|
static inline void sf_file_writer_set_flags(
|
||||||
SFFileWriterInfo *writer, const short flags)
|
SFFileWriterInfo *writer, const short flags)
|
||||||
|
|
@ -103,6 +137,14 @@ static inline void sf_file_writer_set_call_fsync(
|
||||||
writer->cfg.call_fsync = 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(
|
static inline int64_t sf_file_writer_get_last_version_ex(
|
||||||
SFFileWriterInfo *writer, const int log_level)
|
SFFileWriterInfo *writer, const int log_level)
|
||||||
{
|
{
|
||||||
|
|
@ -185,7 +227,8 @@ static inline const char *sf_file_writer_get_filepath(
|
||||||
const char *data_path, const char *subdir_name,
|
const char *data_path, const char *subdir_name,
|
||||||
char *filepath, const int size)
|
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;
|
return filepath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -194,15 +237,41 @@ static inline const char *sf_file_writer_get_filename_ex(
|
||||||
const char *file_prefix, const int binlog_index,
|
const char *file_prefix, const int binlog_index,
|
||||||
char *filename, const int size)
|
char *filename, const int size)
|
||||||
{
|
{
|
||||||
snprintf(filename, size, "%s/%s/%s"SF_BINLOG_FILE_EXT_FMT,
|
char *p;
|
||||||
data_path, subdir_name, file_prefix, binlog_index);
|
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;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define sf_file_writer_get_filename(data_path, subdir_name, \
|
#define sf_file_writer_get_filename(data_path, subdir_name, \
|
||||||
binlog_index, filename, size) \
|
binlog_index, filename, size) \
|
||||||
sf_file_writer_get_filename_ex(data_path, subdir_name, \
|
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 *sf_file_writer_get_index_filename(const char *data_path,
|
||||||
const char *subdir_name, char *filename, const int size);
|
const char *subdir_name, char *filename, const int size);
|
||||||
|
|
@ -213,10 +282,40 @@ int sf_file_writer_set_binlog_start_index(SFFileWriterInfo *writer,
|
||||||
int sf_file_writer_set_binlog_write_index(SFFileWriterInfo *writer,
|
int sf_file_writer_set_binlog_write_index(SFFileWriterInfo *writer,
|
||||||
const int last_index);
|
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,
|
int sf_file_writer_get_last_lines(const char *data_path,
|
||||||
const char *subdir_name, const int current_write_index,
|
const char *subdir_name, const int current_write_index,
|
||||||
char *buff, const int buff_size, int *count, int *length);
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
132
src/sf_global.c
132
src/sf_global.c
|
|
@ -47,11 +47,12 @@ SFGlobalVariables g_sf_global_vars = {
|
||||||
{0, 0}, NULL, {NULL, 0}
|
{0, 0}, NULL, {NULL, 0}
|
||||||
};
|
};
|
||||||
|
|
||||||
SFContext g_sf_context = {{'\0'}, NULL, 0, sf_address_family_auto,
|
SFContext g_sf_context = {{'\0'}, NULL, 0, false, false, false,
|
||||||
{{AF_UNSPEC, {{true, fc_comm_type_sock}, {false, fc_comm_type_rdma}}},
|
sf_address_family_auto, {{AF_UNSPEC, {{true, fc_comm_type_sock},
|
||||||
{AF_UNSPEC, {{true, fc_comm_type_sock}, {false, fc_comm_type_rdma}}}},
|
{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,
|
{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, true,
|
SF_DEF_MAX_BUFF_SIZE}, 1, DEFAULT_WORK_THREADS, 0, true, true,
|
||||||
{false, 0, 0}, {sf_task_finish_clean_up}
|
{false, 0, 0}, {sf_task_finish_clean_up}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -220,9 +221,7 @@ int sf_load_slow_log_config_ex(IniFullContext *ini_ctx, LogContext *log_ctx,
|
||||||
if (filename_prefix == NULL || *filename_prefix == '\0') {
|
if (filename_prefix == NULL || *filename_prefix == '\0') {
|
||||||
strcpy(slow_log_cfg->filename_prefix, "slow");
|
strcpy(slow_log_cfg->filename_prefix, "slow");
|
||||||
} else {
|
} else {
|
||||||
snprintf(slow_log_cfg->filename_prefix,
|
fc_safe_strcpy(slow_log_cfg->filename_prefix, filename_prefix);
|
||||||
sizeof(slow_log_cfg->filename_prefix),
|
|
||||||
"%s", filename_prefix);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -250,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;
|
SF_G_BASE_PATH_INITED = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -271,6 +271,7 @@ int sf_load_global_base_path(IniFullContext *ini_ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
chopPath(SF_G_BASE_PATH_STR);
|
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 (!fileExists(SF_G_BASE_PATH_STR)) {
|
||||||
if ((result=fc_check_mkdir_ex(SF_G_BASE_PATH_STR, 0775,
|
if ((result=fc_check_mkdir_ex(SF_G_BASE_PATH_STR, 0775,
|
||||||
&SF_G_BASE_PATH_CREATED)) != 0)
|
&SF_G_BASE_PATH_CREATED)) != 0)
|
||||||
|
|
@ -329,9 +330,7 @@ int sf_load_global_config_ex(const char *log_filename_prefix,
|
||||||
*g_sf_global_vars.run_by.group = '\0';
|
*g_sf_global_vars.run_by.group = '\0';
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
snprintf(g_sf_global_vars.run_by.group,
|
fc_safe_strcpy(g_sf_global_vars.run_by.group, pRunByGroup);
|
||||||
sizeof(g_sf_global_vars.run_by.group),
|
|
||||||
"%s", pRunByGroup);
|
|
||||||
}
|
}
|
||||||
if (*(g_sf_global_vars.run_by.group) == '\0') {
|
if (*(g_sf_global_vars.run_by.group) == '\0') {
|
||||||
g_sf_global_vars.run_by.gid = getegid();
|
g_sf_global_vars.run_by.gid = getegid();
|
||||||
|
|
@ -356,9 +355,7 @@ int sf_load_global_config_ex(const char *log_filename_prefix,
|
||||||
*g_sf_global_vars.run_by.user = '\0';
|
*g_sf_global_vars.run_by.user = '\0';
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
snprintf(g_sf_global_vars.run_by.user,
|
fc_safe_strcpy(g_sf_global_vars.run_by.user, pRunByUser);
|
||||||
sizeof(g_sf_global_vars.run_by.user),
|
|
||||||
"%s", pRunByUser);
|
|
||||||
}
|
}
|
||||||
if (*(g_sf_global_vars.run_by.user) == '\0') {
|
if (*(g_sf_global_vars.run_by.user) == '\0') {
|
||||||
g_sf_global_vars.run_by.uid = geteuid();
|
g_sf_global_vars.run_by.uid = geteuid();
|
||||||
|
|
@ -447,7 +444,7 @@ int sf_load_config_ex(const char *log_filename_prefix,
|
||||||
|
|
||||||
#define LOAD_API(handler, fname) LOAD_API_EX(handler, server_, fname)
|
#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";
|
const char *library = "libfastrdma.so";
|
||||||
void *dlhandle;
|
void *dlhandle;
|
||||||
|
|
@ -462,7 +459,11 @@ static int load_rdma_apis(SFNetworkHandler *handler)
|
||||||
|
|
||||||
LOAD_API(handler, get_connection_size);
|
LOAD_API(handler, get_connection_size);
|
||||||
LOAD_API(handler, init_connection);
|
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, , create_server);
|
||||||
LOAD_API_EX(handler, , close_server);
|
LOAD_API_EX(handler, , close_server);
|
||||||
LOAD_API(handler, accept_connection);
|
LOAD_API(handler, accept_connection);
|
||||||
|
|
@ -476,8 +477,8 @@ static int load_rdma_apis(SFNetworkHandler *handler)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int init_network_handler(SFNetworkHandler *handler,
|
static int init_network_handler(SFContext *sf_context,
|
||||||
SFAddressFamilyHandler *fh)
|
SFNetworkHandler *handler, SFAddressFamilyHandler *fh)
|
||||||
{
|
{
|
||||||
handler->fh = fh;
|
handler->fh = fh;
|
||||||
handler->inner.handler = handler;
|
handler->inner.handler = handler;
|
||||||
|
|
@ -502,7 +503,7 @@ static int init_network_handler(SFNetworkHandler *handler,
|
||||||
} else {
|
} else {
|
||||||
handler->inner.id = NULL;
|
handler->inner.id = NULL;
|
||||||
handler->outer.id = NULL;
|
handler->outer.id = NULL;
|
||||||
return load_rdma_apis(handler);
|
return load_rdma_apis(sf_context, handler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -521,7 +522,7 @@ static void set_bind_address(const char *bind_addr, char *ipv4_bind_addr,
|
||||||
return;
|
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);
|
count = splitEx(new_bind_addr, ',', cols, 2);
|
||||||
for (i=0; i<count; i++) {
|
for (i=0; i<count; i++) {
|
||||||
ip_addr = cols[i];
|
ip_addr = cols[i];
|
||||||
|
|
@ -531,9 +532,13 @@ static void set_bind_address(const char *bind_addr, char *ipv4_bind_addr,
|
||||||
++ip_addr;
|
++ip_addr;
|
||||||
len -= 2;
|
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 {
|
} else {
|
||||||
snprintf(ipv4_bind_addr, addr_size, "%s", ip_addr);
|
fc_strlcpy(ipv4_bind_addr, ip_addr, addr_size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -665,6 +670,10 @@ int sf_load_context_from_config_ex(SFContext *sf_context,
|
||||||
int inner_port;
|
int inner_port;
|
||||||
int outer_port;
|
int outer_port;
|
||||||
int port;
|
int port;
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
bool global_use_send_zc;
|
||||||
|
bool use_send_zc;
|
||||||
|
#endif
|
||||||
int i;
|
int i;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
|
|
@ -701,6 +710,17 @@ int sf_load_context_from_config_ex(SFContext *sf_context,
|
||||||
outer_port = config->default_outer_port;
|
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++) {
|
for (i=0; i<SF_ADDRESS_FAMILY_COUNT; i++) {
|
||||||
fh = sf_context->handlers + i;
|
fh = sf_context->handlers + i;
|
||||||
fh->ctx = sf_context;
|
fh->ctx = sf_context;
|
||||||
|
|
@ -724,7 +744,7 @@ int sf_load_context_from_config_ex(SFContext *sf_context,
|
||||||
if (!handler->enabled) {
|
if (!handler->enabled) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ((result=init_network_handler(handler, fh)) != 0) {
|
if ((result=init_network_handler(sf_context, handler, fh)) != 0) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -743,9 +763,15 @@ int sf_load_context_from_config_ex(SFContext *sf_context,
|
||||||
rdma_handler->inner.enabled = sock_handler->inner.enabled;
|
rdma_handler->inner.enabled = sock_handler->inner.enabled;
|
||||||
rdma_handler->outer.port = sock_handler->outer.port;
|
rdma_handler->outer.port = sock_handler->outer.port;
|
||||||
rdma_handler->outer.enabled = sock_handler->outer.enabled;
|
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(
|
sf_context->accept_threads = iniGetIntValue(
|
||||||
config->ini_ctx.section_name,
|
config->ini_ctx.section_name,
|
||||||
"accept_threads", config->ini_ctx.context, 1);
|
"accept_threads", config->ini_ctx.context, 1);
|
||||||
|
|
@ -861,8 +887,7 @@ static void combine_bind_addr(char *bind_addr, const char *ip_addr)
|
||||||
p = bind_addr + strlen(bind_addr);
|
p = bind_addr + strlen(bind_addr);
|
||||||
*p++ = ',';
|
*p++ = ',';
|
||||||
}
|
}
|
||||||
|
strcpy(p, ip_addr);
|
||||||
sprintf(p, "%s", ip_addr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *get_address_family_caption(
|
static const char *get_address_family_caption(
|
||||||
|
|
@ -931,6 +956,12 @@ void sf_context_config_to_string(const SFContext *sf_context,
|
||||||
", address_family=%s, accept_threads=%d, work_threads=%d",
|
", address_family=%s, accept_threads=%d, work_threads=%d",
|
||||||
get_address_family_caption(sf_context->address_family),
|
get_address_family_caption(sf_context->address_family),
|
||||||
sf_context->accept_threads, sf_context->work_threads);
|
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,
|
void sf_log_config_to_string_ex(SFLogConfig *log_cfg, const char *caption,
|
||||||
|
|
@ -1001,18 +1032,27 @@ void sf_global_config_to_string_ex(const char *max_pkg_size_item_nm,
|
||||||
|
|
||||||
len = snprintf(output, size,
|
len = snprintf(output, size,
|
||||||
"base_path=%s, max_connections=%d, connect_timeout=%d, "
|
"base_path=%s, max_connections=%d, connect_timeout=%d, "
|
||||||
"network_timeout=%d, thread_stack_size=%d KB, "
|
"network_timeout=%d, thread_stack_size=%d KB, %s, ",
|
||||||
"%s, tcp_quick_ack=%d, log_level=%s, "
|
SF_G_BASE_PATH_STR,
|
||||||
"run_by_group=%s, run_by_user=%s, ", SF_G_BASE_PATH_STR,
|
|
||||||
g_sf_global_vars.net_buffer_cfg.max_connections,
|
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.connect_timeout,
|
||||||
g_sf_global_vars.net_buffer_cfg.network_timeout,
|
g_sf_global_vars.net_buffer_cfg.network_timeout,
|
||||||
g_sf_global_vars.thread_stack_size / 1024,
|
g_sf_global_vars.thread_stack_size / 1024, pkg_buff);
|
||||||
pkg_buff, g_sf_global_vars.tcp_quick_ack,
|
|
||||||
|
#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(),
|
log_get_level_caption(),
|
||||||
g_sf_global_vars.run_by.group,
|
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,
|
sf_log_config_to_string(&g_sf_global_vars.error_log,
|
||||||
"error-log", output + len, size - len);
|
"error-log", output + len, size - len);
|
||||||
|
|
@ -1024,11 +1064,16 @@ void sf_log_config_ex(const char *other_config)
|
||||||
char sz_context_config[128];
|
char sz_context_config[128];
|
||||||
|
|
||||||
sf_global_config_to_string(sz_global_config, sizeof(sz_global_config));
|
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",
|
if (!g_sf_context.is_client) {
|
||||||
sz_global_config, sz_context_config,
|
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 != '\0') ? ", " : "",
|
||||||
(other_config != NULL) ? other_config : ""
|
(other_config != NULL) ? other_config : ""
|
||||||
);
|
);
|
||||||
|
|
@ -1038,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 *item_name, const char *default_value, string_t *path)
|
||||||
{
|
{
|
||||||
const char *data_path;
|
const char *data_path;
|
||||||
|
int data_path_len;
|
||||||
|
int path_size;
|
||||||
|
|
||||||
data_path = iniGetStrValue(ini_ctx->section_name,
|
data_path = iniGetStrValue(ini_ctx->section_name,
|
||||||
item_name, ini_ctx->context);
|
item_name, ini_ctx->context);
|
||||||
|
|
@ -1053,20 +1100,23 @@ int sf_load_data_path_config_ex(IniFullContext *ini_ctx,
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data_path_len = strlen(data_path);
|
||||||
if (*data_path == '/') {
|
if (*data_path == '/') {
|
||||||
path->len = strlen(data_path);
|
path->len = data_path_len;
|
||||||
path->str = fc_strdup1(data_path, path->len);
|
path->str = fc_strdup1(data_path, path->len);
|
||||||
if (path->str == NULL) {
|
if (path->str == NULL) {
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
path->len = strlen(SF_G_BASE_PATH_STR) + strlen(data_path) + 1;
|
path_size = SF_G_BASE_PATH_LEN + data_path_len + 2;
|
||||||
path->str = (char *)fc_malloc(path->len + 1);
|
path->str = (char *)fc_malloc(path_size);
|
||||||
if (path->str == NULL) {
|
if (path->str == NULL) {
|
||||||
return ENOMEM;
|
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);
|
chopPath(path->str);
|
||||||
path->len = strlen(path->str);
|
path->len = strlen(path->str);
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ typedef struct sf_connection_stat {
|
||||||
typedef struct sf_global_variables {
|
typedef struct sf_global_variables {
|
||||||
struct {
|
struct {
|
||||||
char str[MAX_PATH_SIZE];
|
char str[MAX_PATH_SIZE];
|
||||||
|
int len;
|
||||||
bool inited;
|
bool inited;
|
||||||
bool created;
|
bool created;
|
||||||
} base_path;
|
} base_path;
|
||||||
|
|
@ -79,6 +80,7 @@ extern SFGlobalVariables g_sf_global_vars;
|
||||||
extern SFContext g_sf_context;
|
extern SFContext g_sf_context;
|
||||||
|
|
||||||
#define SF_G_BASE_PATH_STR g_sf_global_vars.base_path.str
|
#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_INITED g_sf_global_vars.base_path.inited
|
||||||
#define SF_G_BASE_PATH_CREATED g_sf_global_vars.base_path.created
|
#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_CONTINUE_FLAG g_sf_global_vars.continue_flag
|
||||||
|
|
@ -317,8 +319,8 @@ static inline void sf_set_global_base_path(const char *base_path)
|
||||||
string_t path_string;
|
string_t path_string;
|
||||||
|
|
||||||
FC_SET_STRING(path_string, (char *)base_path);
|
FC_SET_STRING(path_string, (char *)base_path);
|
||||||
normalize_path(NULL, &path_string, SF_G_BASE_PATH_STR,
|
SF_G_BASE_PATH_LEN = normalize_path(NULL, &path_string,
|
||||||
sizeof(SF_G_BASE_PATH_STR));
|
SF_G_BASE_PATH_STR, sizeof(SF_G_BASE_PATH_STR));
|
||||||
SF_G_BASE_PATH_INITED = true;
|
SF_G_BASE_PATH_INITED = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
654
src/sf_nio.c
654
src/sf_nio.c
|
|
@ -41,6 +41,9 @@
|
||||||
#include "sf_service.h"
|
#include "sf_service.h"
|
||||||
#include "sf_nio.h"
|
#include "sf_nio.h"
|
||||||
|
|
||||||
|
static int sf_client_sock_write(int sock, const int event, void *arg);
|
||||||
|
static int sf_client_sock_read(int sock, const int event, void *arg);
|
||||||
|
|
||||||
void sf_set_parameters_ex(SFContext *sf_context, const int header_size,
|
void sf_set_parameters_ex(SFContext *sf_context, const int header_size,
|
||||||
sf_set_body_length_callback set_body_length_func,
|
sf_set_body_length_callback set_body_length_func,
|
||||||
sf_alloc_recv_buffer_callback alloc_recv_buffer_func,
|
sf_alloc_recv_buffer_callback alloc_recv_buffer_func,
|
||||||
|
|
@ -59,19 +62,43 @@ void sf_set_parameters_ex(SFContext *sf_context, const int header_size,
|
||||||
sf_context->callbacks.release_buffer = release_buffer_callback;
|
sf_context->callbacks.release_buffer = release_buffer_callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
#define CLEAR_OP_TYPE_AND_RELEASE_TASK(task) \
|
||||||
|
FC_URING_OP_TYPE(task) = IORING_OP_NOP; \
|
||||||
|
sf_release_task(task)
|
||||||
|
|
||||||
|
static int sf_uring_cancel_done(int sock, const int event, void *arg)
|
||||||
|
{
|
||||||
|
struct fast_task_info *task;
|
||||||
|
|
||||||
|
task = (struct fast_task_info *)arg;
|
||||||
|
if (event != IOEVENT_TIMEOUT) {
|
||||||
|
CLEAR_OP_TYPE_AND_RELEASE_TASK(task);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void sf_task_detach_thread(struct fast_task_info *task)
|
void sf_task_detach_thread(struct fast_task_info *task)
|
||||||
{
|
{
|
||||||
ioevent_detach(&task->thread_data->ev_puller, task->event.fd);
|
#if IOEVENT_USE_URING
|
||||||
|
if (SF_CTX->use_io_uring) {
|
||||||
|
if (FC_URING_OP_TYPE(task) != IORING_OP_NOP) {
|
||||||
|
task->event.callback = (IOEventCallback)sf_uring_cancel_done;
|
||||||
|
uring_prep_cancel(task);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
#endif
|
||||||
|
ioevent_detach(&task->thread_data->ev_puller, task->event.fd);
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (task->event.timer.expires > 0) {
|
if (task->event.timer.expires > 0) {
|
||||||
fast_timer_remove(&task->thread_data->timer,
|
fast_timer_remove(&task->thread_data->timer,
|
||||||
&task->event.timer);
|
&task->event.timer);
|
||||||
task->event.timer.expires = 0;
|
task->event.timer.expires = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SF_CTX->remove_from_ready_list) {
|
|
||||||
ioevent_remove(&task->thread_data->ev_puller, task);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sf_task_switch_thread(struct fast_task_info *task,
|
void sf_task_switch_thread(struct fast_task_info *task,
|
||||||
|
|
@ -92,28 +119,38 @@ static inline void release_iovec_buffer(struct fast_task_info *task)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void sf_socket_close_connection(struct fast_task_info *task)
|
||||||
|
{
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
if (SF_CTX->use_io_uring) {
|
||||||
|
if (uring_prep_close_fd(task) != 0) {
|
||||||
|
close(task->event.fd);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
#endif
|
||||||
|
close(task->event.fd);
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
task->event.fd = -1;
|
||||||
|
}
|
||||||
|
|
||||||
void sf_task_finish_clean_up(struct fast_task_info *task)
|
void sf_task_finish_clean_up(struct fast_task_info *task)
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
assert(task->event.fd >= 0);
|
|
||||||
if (task->event.fd < 0) {
|
|
||||||
logWarning("file: "__FILE__", line: %d, "
|
|
||||||
"task: %p already cleaned",
|
|
||||||
__LINE__, task);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (task->finish_callback != NULL) {
|
|
||||||
task->finish_callback(task);
|
|
||||||
task->finish_callback = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
release_iovec_buffer(task);
|
release_iovec_buffer(task);
|
||||||
sf_task_detach_thread(task);
|
sf_task_detach_thread(task);
|
||||||
task->handler->close_connection(task);
|
|
||||||
|
|
||||||
__sync_fetch_and_sub(&g_sf_global_vars.connection_stat.current_count, 1);
|
#if IOEVENT_USE_URING
|
||||||
|
if (!SF_CTX->use_io_uring) {
|
||||||
|
#endif
|
||||||
|
task->handler->close_connection(task);
|
||||||
|
__sync_fetch_and_sub(&g_sf_global_vars.
|
||||||
|
connection_stat.current_count, 1);
|
||||||
|
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
sf_release_task(task);
|
sf_release_task(task);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -139,46 +176,115 @@ static inline int set_write_event(struct fast_task_info *task)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
static inline int prepare_first_recv(struct fast_task_info *task)
|
||||||
|
{
|
||||||
|
if (SF_CTX->callbacks.alloc_recv_buffer != NULL) {
|
||||||
|
return uring_prep_recv_data(task, task->recv.ptr->data,
|
||||||
|
SF_CTX->header_size);
|
||||||
|
} else {
|
||||||
|
return uring_prep_first_recv(task);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int prepare_next_recv(struct fast_task_info *task)
|
||||||
|
{
|
||||||
|
int recv_bytes;
|
||||||
|
|
||||||
|
if (task->recv.ptr->length == 0) { //recv header
|
||||||
|
recv_bytes = SF_CTX->header_size - task->recv.ptr->offset;
|
||||||
|
return uring_prep_recv_data(task, task->recv.ptr->data +
|
||||||
|
task->recv.ptr->offset, recv_bytes);
|
||||||
|
} else {
|
||||||
|
recv_bytes = task->recv.ptr->length - task->recv.ptr->offset;
|
||||||
|
if (task->recv_body == NULL) {
|
||||||
|
return uring_prep_recv_data(task, task->recv.ptr->data +
|
||||||
|
task->recv.ptr->offset, recv_bytes);
|
||||||
|
} else {
|
||||||
|
return uring_prep_recv_data(task, task->recv_body +
|
||||||
|
(task->recv.ptr->offset - SF_CTX->
|
||||||
|
header_size), recv_bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static inline int set_read_event(struct fast_task_info *task)
|
static inline int set_read_event(struct fast_task_info *task)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
if (task->event.callback == (IOEventCallback)sf_client_sock_read) {
|
#if IOEVENT_USE_URING
|
||||||
return 0;
|
if (SF_CTX->use_io_uring) {
|
||||||
}
|
if (FC_URING_OP_TYPE(task) != IORING_OP_NOP) {
|
||||||
|
if (FC_URING_OP_TYPE(task) == IORING_OP_RECV) {
|
||||||
|
logWarning("file: "__FILE__", line: %d, "
|
||||||
|
"trigger recv again!", __LINE__);
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
logWarning("file: "__FILE__", line: %d, "
|
||||||
|
"another operation in progress, op_type: %d!",
|
||||||
|
__LINE__, FC_URING_OP_TYPE(task));
|
||||||
|
return EBUSY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
task->event.callback = (IOEventCallback)sf_client_sock_read;
|
if (task->event.callback != (IOEventCallback)sf_client_sock_read) {
|
||||||
if (ioevent_modify(&task->thread_data->ev_puller,
|
task->event.callback = (IOEventCallback)sf_client_sock_read;
|
||||||
task->event.fd, IOEVENT_READ, task) != 0)
|
}
|
||||||
{
|
if ((result=prepare_first_recv(task)) != 0) {
|
||||||
result = errno != 0 ? errno : ENOENT;
|
ioevent_add_to_deleted_list(task);
|
||||||
ioevent_add_to_deleted_list(task);
|
return result;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
#endif
|
||||||
|
|
||||||
logError("file: "__FILE__", line: %d, "
|
if (task->event.callback == (IOEventCallback)sf_client_sock_read) {
|
||||||
"ioevent_modify fail, "
|
return 0;
|
||||||
"errno: %d, error info: %s",
|
}
|
||||||
__LINE__, result, strerror(result));
|
task->event.callback = (IOEventCallback)sf_client_sock_read;
|
||||||
return result;
|
if (ioevent_modify(&task->thread_data->ev_puller,
|
||||||
|
task->event.fd, IOEVENT_READ, task) != 0)
|
||||||
|
{
|
||||||
|
result = errno != 0 ? errno : ENOENT;
|
||||||
|
ioevent_add_to_deleted_list(task);
|
||||||
|
|
||||||
|
logError("file: "__FILE__", line: %d, "
|
||||||
|
"ioevent_modify fail, "
|
||||||
|
"errno: %d, error info: %s",
|
||||||
|
__LINE__, result, strerror(result));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sf_set_read_event(struct fast_task_info *task)
|
int sf_set_read_event(struct fast_task_info *task)
|
||||||
{
|
{
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
if (SF_CTX->use_io_uring) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* reset recv offset and length */
|
||||||
task->recv.ptr->offset = 0;
|
task->recv.ptr->offset = 0;
|
||||||
task->recv.ptr->length = 0;
|
task->recv.ptr->length = 0;
|
||||||
|
|
||||||
task->nio_stages.current = SF_NIO_STAGE_RECV;
|
task->nio_stages.current = SF_NIO_STAGE_RECV;
|
||||||
return set_read_event(task);
|
return set_read_event(task);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int sf_ioevent_add(struct fast_task_info *task,
|
static inline int sf_ioevent_add(struct fast_task_info *task)
|
||||||
IOEventCallback callback, const int timeout)
|
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
result = ioevent_set(task, task->thread_data, task->event.fd,
|
result = ioevent_set(task, task->thread_data, task->event.fd,
|
||||||
IOEVENT_READ, callback, timeout);
|
IOEVENT_READ, (IOEventCallback)sf_client_sock_read,
|
||||||
|
SF_CTX->net_buffer_cfg.network_timeout);
|
||||||
return result > 0 ? -1 * result : result;
|
return result > 0 ? -1 * result : result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -196,8 +302,7 @@ static inline void inc_connection_current_count()
|
||||||
static inline int sf_nio_init(struct fast_task_info *task)
|
static inline int sf_nio_init(struct fast_task_info *task)
|
||||||
{
|
{
|
||||||
inc_connection_current_count();
|
inc_connection_current_count();
|
||||||
return sf_ioevent_add(task, (IOEventCallback)sf_client_sock_read,
|
return sf_ioevent_add(task);
|
||||||
SF_CTX->net_buffer_cfg.network_timeout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int sf_socket_async_connect_check(struct fast_task_info *task)
|
int sf_socket_async_connect_check(struct fast_task_info *task)
|
||||||
|
|
@ -212,7 +317,7 @@ int sf_socket_async_connect_check(struct fast_task_info *task)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int sf_client_connect_done(int sock, short event, void *arg)
|
static int sf_client_connect_done(int sock, const int event, void *arg)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
struct fast_task_info *task;
|
struct fast_task_info *task;
|
||||||
|
|
@ -220,16 +325,31 @@ static int sf_client_connect_done(int sock, short event, void *arg)
|
||||||
|
|
||||||
task = (struct fast_task_info *)arg;
|
task = (struct fast_task_info *)arg;
|
||||||
if (task->canceled) {
|
if (task->canceled) {
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
if (SF_CTX->use_io_uring && event != IOEVENT_TIMEOUT) {
|
||||||
|
CLEAR_OP_TYPE_AND_RELEASE_TASK(task);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
return ENOTCONN;
|
return ENOTCONN;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event & IOEVENT_TIMEOUT) {
|
if (event == IOEVENT_TIMEOUT) {
|
||||||
result = ETIMEDOUT;
|
result = ETIMEDOUT;
|
||||||
} else {
|
} else {
|
||||||
result = task->handler->async_connect_check(task);
|
#if IOEVENT_USE_URING
|
||||||
if (result == EINPROGRESS) {
|
if (SF_CTX->use_io_uring) {
|
||||||
return 0;
|
CLEAR_OP_TYPE_AND_RELEASE_TASK(task);
|
||||||
|
result = (task->event.res < 0 ? -1 * task->event.res :
|
||||||
|
task->event.res);
|
||||||
|
} else {
|
||||||
|
#endif
|
||||||
|
result = task->handler->async_connect_check(task);
|
||||||
|
if (result == EINPROGRESS) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SF_CTX->callbacks.connect_done != NULL) {
|
if (SF_CTX->callbacks.connect_done != NULL) {
|
||||||
|
|
@ -260,14 +380,26 @@ static int sf_client_connect_done(int sock, short event, void *arg)
|
||||||
int sf_socket_async_connect_server(struct fast_task_info *task)
|
int sf_socket_async_connect_server(struct fast_task_info *task)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
if ((task->event.fd=socketCreateEx2(AF_UNSPEC, task->server_ip,
|
|
||||||
O_NONBLOCK, NULL, &result)) < 0)
|
|
||||||
{
|
|
||||||
return result > 0 ? -1 * result : result;
|
|
||||||
}
|
|
||||||
|
|
||||||
return asyncconnectserverbyip(task->event.fd,
|
#if IOEVENT_USE_URING
|
||||||
task->server_ip, task->port);
|
if (SF_CTX->use_io_uring) {
|
||||||
|
if ((result=uring_prep_connect(task)) != 0) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return EINPROGRESS;
|
||||||
|
} else {
|
||||||
|
#endif
|
||||||
|
if ((task->event.fd=socketCreateEx2(AF_UNSPEC, task->server_ip,
|
||||||
|
O_NONBLOCK, NULL, &result)) < 0)
|
||||||
|
{
|
||||||
|
return result > 0 ? -1 * result : result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return asyncconnectserverbyip(task->event.fd,
|
||||||
|
task->server_ip, task->port);
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static int sf_async_connect_server(struct fast_task_info *task)
|
static int sf_async_connect_server(struct fast_task_info *task)
|
||||||
|
|
@ -287,10 +419,7 @@ static int sf_async_connect_server(struct fast_task_info *task)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == 0) {
|
if (result == 0) {
|
||||||
if ((result=sf_ioevent_add(task, (IOEventCallback)
|
if ((result=sf_ioevent_add(task)) != 0) {
|
||||||
sf_client_sock_read, SF_CTX->
|
|
||||||
net_buffer_cfg.network_timeout)) != 0)
|
|
||||||
{
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -331,10 +460,12 @@ static int sf_nio_deal_task(struct fast_task_info *task, const int stage)
|
||||||
case SF_NIO_STAGE_RECV:
|
case SF_NIO_STAGE_RECV:
|
||||||
task->nio_stages.current = SF_NIO_STAGE_RECV;
|
task->nio_stages.current = SF_NIO_STAGE_RECV;
|
||||||
if ((result=set_read_event(task)) == 0) {
|
if ((result=set_read_event(task)) == 0) {
|
||||||
if (sf_client_sock_read(task->event.fd,
|
if (!SF_CTX->use_io_uring) {
|
||||||
IOEVENT_READ, task) < 0)
|
if (sf_client_sock_read(task->event.fd,
|
||||||
{
|
IOEVENT_READ, task) < 0)
|
||||||
result = errno != 0 ? errno : EIO;
|
{
|
||||||
|
result = errno != 0 ? errno : EIO;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -345,10 +476,7 @@ static int sf_nio_deal_task(struct fast_task_info *task, const int stage)
|
||||||
result = SF_CTX->callbacks.deal_task(task, SF_NIO_STAGE_CONTINUE);
|
result = SF_CTX->callbacks.deal_task(task, SF_NIO_STAGE_CONTINUE);
|
||||||
break;
|
break;
|
||||||
case SF_NIO_STAGE_FORWARDED: //forward by other thread
|
case SF_NIO_STAGE_FORWARDED: //forward by other thread
|
||||||
if ((result=sf_ioevent_add(task, (IOEventCallback)
|
if ((result=sf_ioevent_add(task)) == 0) {
|
||||||
sf_client_sock_read,
|
|
||||||
SF_CTX->net_buffer_cfg.network_timeout)) == 0)
|
|
||||||
{
|
|
||||||
result = SF_CTX->callbacks.deal_task(task, SF_NIO_STAGE_SEND);
|
result = SF_CTX->callbacks.deal_task(task, SF_NIO_STAGE_SEND);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -416,6 +544,7 @@ int sf_nio_notify(struct fast_task_info *task, const int stage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sf_hold_task(task); //since 1.2.11
|
||||||
PTHREAD_MUTEX_LOCK(&task->thread_data->waiting_queue.lock);
|
PTHREAD_MUTEX_LOCK(&task->thread_data->waiting_queue.lock);
|
||||||
task->notify_next = NULL;
|
task->notify_next = NULL;
|
||||||
if (task->thread_data->waiting_queue.tail == NULL) {
|
if (task->thread_data->waiting_queue.tail == NULL) {
|
||||||
|
|
@ -435,7 +564,7 @@ int sf_nio_notify(struct fast_task_info *task, const int stage)
|
||||||
{
|
{
|
||||||
result = errno != 0 ? errno : EIO;
|
result = errno != 0 ? errno : EIO;
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"write eventfd %d fail, errno: %d, error info: %s",
|
"write to fd %d fail, errno: %d, error info: %s",
|
||||||
__LINE__, FC_NOTIFY_WRITE_FD(task->thread_data),
|
__LINE__, FC_NOTIFY_WRITE_FD(task->thread_data),
|
||||||
result, STRERROR(result));
|
result, STRERROR(result));
|
||||||
return result;
|
return result;
|
||||||
|
|
@ -463,26 +592,32 @@ static inline void deal_notified_task(struct fast_task_info *task,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sf_recv_notify_read(int sock, short event, void *arg)
|
void sf_recv_notify_read(int fd, const int event, void *arg)
|
||||||
{
|
{
|
||||||
int64_t n;
|
int64_t n;
|
||||||
int stage;
|
int stage;
|
||||||
struct nio_thread_data *thread_data;
|
struct ioevent_notify_entry *notify_entry;
|
||||||
struct fast_task_info *task;
|
struct fast_task_info *task;
|
||||||
struct fast_task_info *current;
|
struct fast_task_info *current;
|
||||||
|
|
||||||
thread_data = ((struct ioevent_notify_entry *)arg)->thread_data;
|
notify_entry = (struct ioevent_notify_entry *)arg;
|
||||||
if (read(sock, &n, sizeof(n)) < 0) {
|
if (read(fd, &n, sizeof(n)) < 0) {
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
if (errno == EAGAIN) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
logWarning("file: "__FILE__", line: %d, "
|
logWarning("file: "__FILE__", line: %d, "
|
||||||
"read from eventfd %d fail, errno: %d, error info: %s",
|
"read from fd %d fail, errno: %d, error info: %s",
|
||||||
__LINE__, sock, errno, STRERROR(errno));
|
__LINE__, fd, errno, STRERROR(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
PTHREAD_MUTEX_LOCK(&thread_data->waiting_queue.lock);
|
PTHREAD_MUTEX_LOCK(¬ify_entry->thread_data->waiting_queue.lock);
|
||||||
current = thread_data->waiting_queue.head;
|
current = notify_entry->thread_data->waiting_queue.head;
|
||||||
thread_data->waiting_queue.head = NULL;
|
notify_entry->thread_data->waiting_queue.head = NULL;
|
||||||
thread_data->waiting_queue.tail = NULL;
|
notify_entry->thread_data->waiting_queue.tail = NULL;
|
||||||
PTHREAD_MUTEX_UNLOCK(&thread_data->waiting_queue.lock);
|
PTHREAD_MUTEX_UNLOCK(¬ify_entry->thread_data->waiting_queue.lock);
|
||||||
|
|
||||||
while (current != NULL) {
|
while (current != NULL) {
|
||||||
task = current;
|
task = current;
|
||||||
|
|
@ -499,6 +634,7 @@ void sf_recv_notify_read(int sock, short event, void *arg)
|
||||||
__sync_bool_compare_and_swap(&task->nio_stages.notify,
|
__sync_bool_compare_and_swap(&task->nio_stages.notify,
|
||||||
stage, SF_NIO_STAGE_NONE);
|
stage, SF_NIO_STAGE_NONE);
|
||||||
}
|
}
|
||||||
|
sf_release_task(task); //since 1.2.11
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -508,16 +644,31 @@ int sf_send_add_event(struct fast_task_info *task)
|
||||||
if (task->send.ptr->length > 0) {
|
if (task->send.ptr->length > 0) {
|
||||||
/* direct send */
|
/* direct send */
|
||||||
task->nio_stages.current = SF_NIO_STAGE_SEND;
|
task->nio_stages.current = SF_NIO_STAGE_SEND;
|
||||||
if (sf_client_sock_write(task->event.fd, IOEVENT_WRITE, task) < 0) {
|
#if IOEVENT_USE_URING
|
||||||
return errno != 0 ? errno : EIO;
|
if (SF_CTX->use_io_uring) {
|
||||||
|
if (task->event.callback != (IOEventCallback)sf_client_sock_write) {
|
||||||
|
task->event.callback = (IOEventCallback)sf_client_sock_write;
|
||||||
|
}
|
||||||
|
if (SF_CTX->use_send_zc) {
|
||||||
|
return uring_prep_first_send_zc(task);
|
||||||
|
} else {
|
||||||
|
return uring_prep_first_send(task);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
#endif
|
||||||
|
if (sf_client_sock_write(task->event.fd, IOEVENT_WRITE, task) < 0) {
|
||||||
|
return errno != 0 ? errno : EIO;
|
||||||
|
}
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int check_task(struct fast_task_info *task,
|
static inline int check_task(struct fast_task_info *task,
|
||||||
const short event, const int expect_stage)
|
const int event, const int expect_stage)
|
||||||
{
|
{
|
||||||
if (task->canceled) {
|
if (task->canceled) {
|
||||||
return ENOTCONN;
|
return ENOTCONN;
|
||||||
|
|
@ -537,6 +688,18 @@ static inline int check_task(struct fast_task_info *task,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
if (SF_CTX->use_io_uring) {
|
||||||
|
logWarning("file: "__FILE__", line: %d, "
|
||||||
|
"client ip: %s, event: %d, expect stage: %d, "
|
||||||
|
"but current stage: %d, close connection",
|
||||||
|
__LINE__, task->client_ip, event,
|
||||||
|
expect_stage, task->nio_stages.current);
|
||||||
|
ioevent_add_to_deleted_list(task);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (task->handler->comm_type == fc_comm_type_sock) {
|
if (task->handler->comm_type == fc_comm_type_sock) {
|
||||||
if (tcp_socket_connected(task->event.fd)) {
|
if (tcp_socket_connected(task->event.fd)) {
|
||||||
return EAGAIN;
|
return EAGAIN;
|
||||||
|
|
@ -553,28 +716,69 @@ static inline int check_task(struct fast_task_info *task,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
static inline int prepare_next_send(struct fast_task_info *task)
|
||||||
|
{
|
||||||
|
if (SF_CTX->use_send_zc) {
|
||||||
|
return uring_prep_next_send_zc(task);
|
||||||
|
} else {
|
||||||
|
return uring_prep_next_send(task);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
ssize_t sf_socket_send_data(struct fast_task_info *task,
|
ssize_t sf_socket_send_data(struct fast_task_info *task,
|
||||||
SFCommAction *action, bool *send_done)
|
SFCommAction *action, bool *send_done)
|
||||||
{
|
{
|
||||||
int bytes;
|
int bytes;
|
||||||
|
int result;
|
||||||
|
|
||||||
if (task->iovec_array.iovs != NULL) {
|
#if IOEVENT_USE_URING
|
||||||
bytes = writev(task->event.fd, task->iovec_array.iovs,
|
if (SF_CTX->use_io_uring) {
|
||||||
FC_MIN(task->iovec_array.count, IOV_MAX));
|
bytes = task->event.res;
|
||||||
} else {
|
} else {
|
||||||
bytes = write(task->event.fd, task->send.ptr->data +
|
#endif
|
||||||
task->send.ptr->offset, task->send.ptr->length -
|
if (task->iovec_array.iovs != NULL) {
|
||||||
task->send.ptr->offset);
|
bytes = writev(task->event.fd, task->iovec_array.iovs,
|
||||||
|
FC_MIN(task->iovec_array.count, IOV_MAX));
|
||||||
|
} else {
|
||||||
|
bytes = write(task->event.fd, task->send.ptr->data +
|
||||||
|
task->send.ptr->offset, task->send.ptr->length -
|
||||||
|
task->send.ptr->offset);
|
||||||
|
}
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (bytes < 0) {
|
if (bytes < 0) {
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
#if IOEVENT_USE_URING
|
||||||
{
|
if (SF_CTX->use_io_uring) {
|
||||||
if (set_write_event(task) != 0) {
|
result = -bytes;
|
||||||
return -1;
|
} else {
|
||||||
|
#endif
|
||||||
|
result = errno;
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if (result == EAGAIN || result == EWOULDBLOCK) {
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
if (SF_CTX->use_io_uring) {
|
||||||
|
if (prepare_next_send(task) != 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
#endif
|
||||||
|
if (set_write_event(task) != 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
*action = sf_comm_action_break;
|
*action = sf_comm_action_break;
|
||||||
return 0;
|
return 0;
|
||||||
} else if (errno == EINTR) { //should retry
|
} else if (result == EINTR && !SF_CTX->use_io_uring) {
|
||||||
|
/* should try again */
|
||||||
logDebug("file: "__FILE__", line: %d, "
|
logDebug("file: "__FILE__", line: %d, "
|
||||||
"client ip: %s, ignore interupt signal",
|
"client ip: %s, ignore interupt signal",
|
||||||
__LINE__, task->client_ip);
|
__LINE__, task->client_ip);
|
||||||
|
|
@ -585,30 +789,40 @@ ssize_t sf_socket_send_data(struct fast_task_info *task,
|
||||||
"client ip: %s, send fail, task offset: %d, length: %d, "
|
"client ip: %s, send fail, task offset: %d, length: %d, "
|
||||||
"errno: %d, error info: %s", __LINE__, task->client_ip,
|
"errno: %d, error info: %s", __LINE__, task->client_ip,
|
||||||
task->send.ptr->offset, task->send.ptr->length,
|
task->send.ptr->offset, task->send.ptr->length,
|
||||||
errno, strerror(errno));
|
result, strerror(result));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
} else if (bytes == 0) {
|
} else if (bytes == 0) {
|
||||||
logWarning("file: "__FILE__", line: %d, "
|
logWarning("file: "__FILE__", line: %d, "
|
||||||
"client ip: %s, sock: %d, task length: %d, offset: %d, "
|
"client ip: %s, task length: %d, offset: %d, "
|
||||||
"send failed, connection disconnected", __LINE__,
|
"send failed, connection disconnected", __LINE__,
|
||||||
task->client_ip, task->event.fd, task->send.ptr->length,
|
task->client_ip, task->send.ptr->length,
|
||||||
task->send.ptr->offset);
|
task->send.ptr->offset);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
task->send.ptr->offset += bytes;
|
task->send.ptr->offset += bytes;
|
||||||
if (task->send.ptr->offset >= task->send.ptr->length) {
|
if (task->send.ptr->offset >= task->send.ptr->length) {
|
||||||
if (task->send.ptr != task->recv.ptr) { //double buffers
|
#if IOEVENT_USE_URING
|
||||||
task->send.ptr->offset = 0;
|
if (SF_CTX->use_io_uring && FC_URING_IS_SEND_ZC(task) &&
|
||||||
task->send.ptr->length = 0;
|
task->thread_data->ev_puller.send_zc_done_notify)
|
||||||
}
|
{
|
||||||
*action = sf_comm_action_finish;
|
*action = sf_comm_action_break;
|
||||||
*send_done = true;
|
*send_done = false;
|
||||||
} else {
|
} else {
|
||||||
*action = sf_comm_action_continue;
|
#endif
|
||||||
*send_done = false;
|
if (task->send.ptr != task->recv.ptr) { //double buffers
|
||||||
|
task->send.ptr->offset = 0;
|
||||||
|
task->send.ptr->length = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
*action = sf_comm_action_finish;
|
||||||
|
*send_done = true;
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} else {
|
||||||
/* set next writev iovec array */
|
/* set next writev iovec array */
|
||||||
if (task->iovec_array.iovs != NULL) {
|
if (task->iovec_array.iovs != NULL) {
|
||||||
struct iovec *iov;
|
struct iovec *iov;
|
||||||
|
|
@ -637,6 +851,25 @@ ssize_t sf_socket_send_data(struct fast_task_info *task,
|
||||||
task->iovec_array.iovs = iov;
|
task->iovec_array.iovs = iov;
|
||||||
task->iovec_array.count = end - iov;
|
task->iovec_array.count = end - iov;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
if (SF_CTX->use_io_uring) {
|
||||||
|
if (!(FC_URING_IS_SEND_ZC(task) && task->thread_data->
|
||||||
|
ev_puller.send_zc_done_notify))
|
||||||
|
{
|
||||||
|
if (prepare_next_send(task) != 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*action = sf_comm_action_break;
|
||||||
|
} else {
|
||||||
|
#endif
|
||||||
|
*action = sf_comm_action_continue;
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
*send_done = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return bytes;
|
return bytes;
|
||||||
|
|
@ -646,30 +879,56 @@ ssize_t sf_socket_recv_data(struct fast_task_info *task,
|
||||||
const bool call_post_recv, SFCommAction *action)
|
const bool call_post_recv, SFCommAction *action)
|
||||||
{
|
{
|
||||||
int bytes;
|
int bytes;
|
||||||
|
int result;
|
||||||
int recv_bytes;
|
int recv_bytes;
|
||||||
bool new_alloc;
|
bool new_alloc;
|
||||||
|
|
||||||
if (task->recv.ptr->length == 0) { //recv header
|
#if IOEVENT_USE_URING
|
||||||
recv_bytes = SF_CTX->header_size - task->recv.ptr->offset;
|
if (SF_CTX->use_io_uring) {
|
||||||
bytes = read(task->event.fd, task->recv.ptr->data +
|
bytes = task->event.res;
|
||||||
task->recv.ptr->offset, recv_bytes);
|
|
||||||
} else {
|
} else {
|
||||||
recv_bytes = task->recv.ptr->length - task->recv.ptr->offset;
|
#endif
|
||||||
if (task->recv_body == NULL) {
|
if (task->recv.ptr->length == 0) { //recv header
|
||||||
|
recv_bytes = SF_CTX->header_size - task->recv.ptr->offset;
|
||||||
bytes = read(task->event.fd, task->recv.ptr->data +
|
bytes = read(task->event.fd, task->recv.ptr->data +
|
||||||
task->recv.ptr->offset, recv_bytes);
|
task->recv.ptr->offset, recv_bytes);
|
||||||
} else {
|
} else {
|
||||||
bytes = read(task->event.fd, task->recv_body +
|
recv_bytes = task->recv.ptr->length - task->recv.ptr->offset;
|
||||||
(task->recv.ptr->offset - SF_CTX->
|
if (task->recv_body == NULL) {
|
||||||
header_size), recv_bytes);
|
bytes = read(task->event.fd, task->recv.ptr->data +
|
||||||
|
task->recv.ptr->offset, recv_bytes);
|
||||||
|
} else {
|
||||||
|
bytes = read(task->event.fd, task->recv_body +
|
||||||
|
(task->recv.ptr->offset - SF_CTX->
|
||||||
|
header_size), recv_bytes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (bytes < 0) {
|
if (bytes < 0) {
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
#if IOEVENT_USE_URING
|
||||||
|
if (SF_CTX->use_io_uring) {
|
||||||
|
result = -bytes;
|
||||||
|
} else {
|
||||||
|
#endif
|
||||||
|
result = errno;
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if (result == EAGAIN || result == EWOULDBLOCK) {
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
if (SF_CTX->use_io_uring) {
|
||||||
|
if (prepare_next_recv(task) != 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
*action = sf_comm_action_break;
|
*action = sf_comm_action_break;
|
||||||
return 0;
|
return 0;
|
||||||
} else if (errno == EINTR) { //should retry
|
} else if (result == EINTR && !SF_CTX->use_io_uring) {
|
||||||
|
/* should try again */
|
||||||
logDebug("file: "__FILE__", line: %d, "
|
logDebug("file: "__FILE__", line: %d, "
|
||||||
"client ip: %s, ignore interupt signal",
|
"client ip: %s, ignore interupt signal",
|
||||||
__LINE__, task->client_ip);
|
__LINE__, task->client_ip);
|
||||||
|
|
@ -680,7 +939,7 @@ ssize_t sf_socket_recv_data(struct fast_task_info *task,
|
||||||
"client ip: %s, recv fail, "
|
"client ip: %s, recv fail, "
|
||||||
"errno: %d, error info: %s",
|
"errno: %d, error info: %s",
|
||||||
__LINE__, task->client_ip,
|
__LINE__, task->client_ip,
|
||||||
errno, strerror(errno));
|
result, strerror(result));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
} else if (bytes == 0) {
|
} else if (bytes == 0) {
|
||||||
|
|
@ -712,7 +971,18 @@ ssize_t sf_socket_recv_data(struct fast_task_info *task,
|
||||||
task->recv.ptr->offset += bytes;
|
task->recv.ptr->offset += bytes;
|
||||||
if (task->recv.ptr->length == 0) { //pkg header
|
if (task->recv.ptr->length == 0) { //pkg header
|
||||||
if (task->recv.ptr->offset < SF_CTX->header_size) {
|
if (task->recv.ptr->offset < SF_CTX->header_size) {
|
||||||
*action = sf_comm_action_continue;
|
#if IOEVENT_USE_URING
|
||||||
|
if (SF_CTX->use_io_uring) {
|
||||||
|
if (prepare_next_recv(task) != 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*action = sf_comm_action_break;
|
||||||
|
} else {
|
||||||
|
#endif
|
||||||
|
*action = sf_comm_action_continue;
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
}
|
||||||
|
#endif
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -765,7 +1035,18 @@ ssize_t sf_socket_recv_data(struct fast_task_info *task,
|
||||||
if (task->recv.ptr->offset >= task->recv.ptr->length) { //recv done
|
if (task->recv.ptr->offset >= task->recv.ptr->length) { //recv done
|
||||||
*action = sf_comm_action_finish;
|
*action = sf_comm_action_finish;
|
||||||
} else {
|
} else {
|
||||||
*action = sf_comm_action_continue;
|
#if IOEVENT_USE_URING
|
||||||
|
if (SF_CTX->use_io_uring) {
|
||||||
|
if (prepare_next_recv(task) != 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*action = sf_comm_action_break;
|
||||||
|
} else {
|
||||||
|
#endif
|
||||||
|
*action = sf_comm_action_continue;
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
return bytes;
|
return bytes;
|
||||||
|
|
@ -841,8 +1122,7 @@ static int calc_iops_and_remove_polling(struct fast_task_info *task)
|
||||||
ioevent_set_timeout(&task->thread_data->ev_puller,
|
ioevent_set_timeout(&task->thread_data->ev_puller,
|
||||||
task->thread_data->timeout_ms);
|
task->thread_data->timeout_ms);
|
||||||
}
|
}
|
||||||
result = sf_ioevent_add(task, (IOEventCallback)
|
result = sf_ioevent_add(task);
|
||||||
sf_client_sock_read, SF_CTX->net_buffer_cfg.network_timeout);
|
|
||||||
|
|
||||||
format_ip_address(task->client_ip, formatted_ip);
|
format_ip_address(task->client_ip, formatted_ip);
|
||||||
logInfo("file: "__FILE__", line: %d, client: %s:%u, "
|
logInfo("file: "__FILE__", line: %d, client: %s:%u, "
|
||||||
|
|
@ -904,7 +1184,7 @@ int sf_rdma_busy_polling_callback(struct nio_thread_data *thread_data)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sf_client_sock_read(int sock, short event, void *arg)
|
static int sf_client_sock_read(int sock, const int event, void *arg)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
int bytes;
|
int bytes;
|
||||||
|
|
@ -914,10 +1194,15 @@ int sf_client_sock_read(int sock, short event, void *arg)
|
||||||
|
|
||||||
task = (struct fast_task_info *)arg;
|
task = (struct fast_task_info *)arg;
|
||||||
if ((result=check_task(task, event, SF_NIO_STAGE_RECV)) != 0) {
|
if ((result=check_task(task, event, SF_NIO_STAGE_RECV)) != 0) {
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
if (SF_CTX->use_io_uring && event != IOEVENT_TIMEOUT) {
|
||||||
|
CLEAR_OP_TYPE_AND_RELEASE_TASK(task);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
return result >= 0 ? 0 : -1;
|
return result >= 0 ? 0 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event & IOEVENT_TIMEOUT) {
|
if (event == IOEVENT_TIMEOUT) {
|
||||||
if (task->recv.ptr->offset == 0 && task->req_count > 0) {
|
if (task->recv.ptr->offset == 0 && task->req_count > 0) {
|
||||||
if (SF_CTX->callbacks.task_timeout != NULL) {
|
if (SF_CTX->callbacks.task_timeout != NULL) {
|
||||||
if (SF_CTX->callbacks.task_timeout(task) != 0) {
|
if (SF_CTX->callbacks.task_timeout(task) != 0) {
|
||||||
|
|
@ -929,7 +1214,8 @@ int sf_client_sock_read(int sock, short event, void *arg)
|
||||||
task->event.timer.expires = g_current_time +
|
task->event.timer.expires = g_current_time +
|
||||||
SF_CTX->net_buffer_cfg.network_timeout;
|
SF_CTX->net_buffer_cfg.network_timeout;
|
||||||
fast_timer_add(&task->thread_data->timer,
|
fast_timer_add(&task->thread_data->timer,
|
||||||
&task->event.timer);
|
&task->event.timer);
|
||||||
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
if (task->recv.ptr->length > 0) {
|
if (task->recv.ptr->length > 0) {
|
||||||
logWarning("file: "__FILE__", line: %d, "
|
logWarning("file: "__FILE__", line: %d, "
|
||||||
|
|
@ -946,10 +1232,14 @@ int sf_client_sock_read(int sock, short event, void *arg)
|
||||||
ioevent_add_to_deleted_list(task);
|
ioevent_add_to_deleted_list(task);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
if (SF_CTX->use_io_uring) {
|
||||||
|
CLEAR_OP_TYPE_AND_RELEASE_TASK(task);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
total_read = 0;
|
total_read = 0;
|
||||||
action = sf_comm_action_continue;
|
action = sf_comm_action_continue;
|
||||||
while (1) {
|
while (1) {
|
||||||
|
|
@ -996,23 +1286,72 @@ int sf_client_sock_read(int sock, short event, void *arg)
|
||||||
return total_read;
|
return total_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sf_client_sock_write(int sock, short event, void *arg)
|
static int sock_write_done(struct fast_task_info *task,
|
||||||
|
const int length, const bool send_done)
|
||||||
|
{
|
||||||
|
int next_stage;
|
||||||
|
|
||||||
|
release_iovec_buffer(task);
|
||||||
|
task->recv.ptr->offset = 0;
|
||||||
|
task->recv.ptr->length = 0;
|
||||||
|
|
||||||
|
if (SF_CTX->callbacks.send_done == NULL || !send_done) {
|
||||||
|
task->nio_stages.current = SF_NIO_STAGE_RECV;
|
||||||
|
} else {
|
||||||
|
if (SF_CTX->callbacks.send_done(task,
|
||||||
|
length, &next_stage) != 0)
|
||||||
|
{
|
||||||
|
ioevent_add_to_deleted_list(task);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (task->nio_stages.current != next_stage) {
|
||||||
|
task->nio_stages.current = next_stage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
if (!SF_CTX->use_io_uring || task->nio_stages.
|
||||||
|
current == SF_NIO_STAGE_RECV)
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (set_read_event(task) != 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int sf_client_sock_write(int sock, const int event, void *arg)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
int bytes;
|
int bytes;
|
||||||
int total_write;
|
int total_write;
|
||||||
int length;
|
int length;
|
||||||
int next_stage;
|
|
||||||
SFCommAction action;
|
SFCommAction action;
|
||||||
bool send_done;
|
bool send_done;
|
||||||
struct fast_task_info *task;
|
struct fast_task_info *task;
|
||||||
|
|
||||||
task = (struct fast_task_info *)arg;
|
task = (struct fast_task_info *)arg;
|
||||||
if ((result=check_task(task, event, SF_NIO_STAGE_SEND)) != 0) {
|
if ((result=check_task(task, event, SF_NIO_STAGE_SEND)) != 0) {
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
if (SF_CTX->use_io_uring && event != IOEVENT_TIMEOUT) {
|
||||||
|
if (event == IOEVENT_NOTIFY || !(FC_URING_IS_SEND_ZC(task) &&
|
||||||
|
task->thread_data->ev_puller.send_zc_done_notify))
|
||||||
|
{
|
||||||
|
CLEAR_OP_TYPE_AND_RELEASE_TASK(task);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
return result >= 0 ? 0 : -1;
|
return result >= 0 ? 0 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event & IOEVENT_TIMEOUT) {
|
if (event == IOEVENT_TIMEOUT) {
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"client ip: %s, send timeout. total length: %d, offset: %d, "
|
"client ip: %s, send timeout. total length: %d, offset: %d, "
|
||||||
"remain: %d", __LINE__, task->client_ip, task->send.ptr->length,
|
"remain: %d", __LINE__, task->client_ip, task->send.ptr->length,
|
||||||
|
|
@ -1023,6 +1362,42 @@ int sf_client_sock_write(int sock, short event, void *arg)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
if (event == IOEVENT_NOTIFY) {
|
||||||
|
if (!FC_URING_IS_SEND_ZC(task)) {
|
||||||
|
logWarning("file: "__FILE__", line: %d, "
|
||||||
|
"unexpected io_uring notify!", __LINE__);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
FC_URING_OP_TYPE(task) = IORING_OP_NOP;
|
||||||
|
if (!task->canceled) {
|
||||||
|
if (task->send.ptr->offset >= task->send.ptr->length) {
|
||||||
|
length = task->send.ptr->length;
|
||||||
|
if (task->send.ptr != task->recv.ptr) { //double buffers
|
||||||
|
task->send.ptr->offset = 0;
|
||||||
|
task->send.ptr->length = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = sock_write_done(task, length, true);
|
||||||
|
} else {
|
||||||
|
result = prepare_next_send(task);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sf_release_task(task);
|
||||||
|
return result == 0 ? 0 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (SF_CTX->use_io_uring) {
|
||||||
|
if (!(FC_URING_IS_SEND_ZC(task) && task->thread_data->
|
||||||
|
ev_puller.send_zc_done_notify))
|
||||||
|
{
|
||||||
|
CLEAR_OP_TYPE_AND_RELEASE_TASK(task);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
total_write = 0;
|
total_write = 0;
|
||||||
length = task->send.ptr->length;
|
length = task->send.ptr->length;
|
||||||
action = sf_comm_action_continue;
|
action = sf_comm_action_continue;
|
||||||
|
|
@ -1038,27 +1413,9 @@ int sf_client_sock_write(int sock, short event, void *arg)
|
||||||
|
|
||||||
total_write += bytes;
|
total_write += bytes;
|
||||||
if (action == sf_comm_action_finish) {
|
if (action == sf_comm_action_finish) {
|
||||||
release_iovec_buffer(task);
|
if (sock_write_done(task, length, send_done) != 0) {
|
||||||
task->recv.ptr->offset = 0;
|
|
||||||
task->recv.ptr->length = 0;
|
|
||||||
if (set_read_event(task) != 0) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SF_CTX->callbacks.send_done == NULL || !send_done) {
|
|
||||||
task->nio_stages.current = SF_NIO_STAGE_RECV;
|
|
||||||
} else {
|
|
||||||
if (SF_CTX->callbacks.send_done(task,
|
|
||||||
length, &next_stage) != 0)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (task->nio_stages.current != next_stage) {
|
|
||||||
task->nio_stages.current = next_stage;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
} else if (action == sf_comm_action_break) {
|
} else if (action == sf_comm_action_break) {
|
||||||
break;
|
break;
|
||||||
|
|
@ -1067,3 +1424,8 @@ int sf_client_sock_write(int sock, short event, void *arg)
|
||||||
|
|
||||||
return total_write;
|
return total_write;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool sf_client_sock_in_read_stage(struct fast_task_info *task)
|
||||||
|
{
|
||||||
|
return (task->event.callback == (IOEventCallback)sf_client_sock_read);
|
||||||
|
}
|
||||||
|
|
|
||||||
21
src/sf_nio.h
21
src/sf_nio.h
|
|
@ -68,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)
|
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(
|
static inline TaskCleanUpCallback sf_get_task_cleanup_callback_ex(
|
||||||
SFContext *sf_context)
|
SFContext *sf_context)
|
||||||
{
|
{
|
||||||
|
|
@ -99,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_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);
|
void sf_task_finish_clean_up(struct fast_task_info *task);
|
||||||
|
|
||||||
|
|
@ -158,11 +148,6 @@ static inline int sf_nio_forward_request(struct fast_task_info *task,
|
||||||
return sf_nio_notify(task, SF_NIO_STAGE_FORWARDED);
|
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
|
static inline void sf_nio_add_to_deleted_list(struct nio_thread_data
|
||||||
*thread_data, struct fast_task_info *task)
|
*thread_data, struct fast_task_info *task)
|
||||||
{
|
{
|
||||||
|
|
@ -173,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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -106,7 +106,7 @@ void sf_ordered_writer_finish(SFOrderedWriterContext *ctx)
|
||||||
{
|
{
|
||||||
int count;
|
int count;
|
||||||
|
|
||||||
if (ctx->writer.fw.file.name != NULL) {
|
if (ctx->writer.fw.file.name.str != NULL) {
|
||||||
fc_queue_terminate(&ctx->thread.queues.version);
|
fc_queue_terminate(&ctx->thread.queues.version);
|
||||||
|
|
||||||
count = 0;
|
count = 0;
|
||||||
|
|
@ -120,8 +120,8 @@ void sf_ordered_writer_finish(SFOrderedWriterContext *ctx)
|
||||||
__LINE__, ctx->writer.fw.cfg.subdir_name);
|
__LINE__, ctx->writer.fw.cfg.subdir_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(ctx->writer.fw.file.name);
|
free(ctx->writer.fw.file.name.str);
|
||||||
ctx->writer.fw.file.name = NULL;
|
ctx->writer.fw.file.name.str = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx->writer.fw.file.fd >= 0) {
|
if (ctx->writer.fw.file.fd >= 0) {
|
||||||
|
|
@ -142,8 +142,7 @@ static void *binlog_writer_func(void *arg)
|
||||||
#ifdef OS_LINUX
|
#ifdef OS_LINUX
|
||||||
{
|
{
|
||||||
char thread_name[64];
|
char thread_name[64];
|
||||||
snprintf(thread_name, sizeof(thread_name),
|
fc_combine_two_strings(thread->name, "writer", '-', thread_name);
|
||||||
"%s-writer", thread->name);
|
|
||||||
prctl(PR_SET_NAME, thread_name);
|
prctl(PR_SET_NAME, thread_name);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -203,7 +202,7 @@ static int sf_ordered_writer_init_thread(SFOrderedWriterContext *context,
|
||||||
|
|
||||||
thread = &context->thread;
|
thread = &context->thread;
|
||||||
writer = &context->writer;
|
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->fw.cfg.max_record_size = max_record_size;
|
||||||
writer->thread = thread;
|
writer->thread = thread;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,7 @@ int sf_ordered_writer_init_ex(SFOrderedWriterContext *context,
|
||||||
#define sf_ordered_writer_init(context, data_path, \
|
#define sf_ordered_writer_init(context, data_path, \
|
||||||
subdir_name, buffer_size, max_record_size) \
|
subdir_name, buffer_size, max_record_size) \
|
||||||
sf_ordered_writer_init_ex(context, data_path, subdir_name, \
|
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)
|
SF_BINLOG_DEFAULT_ROTATE_SIZE, true)
|
||||||
|
|
||||||
#define sf_ordered_writer_set_flags(ctx, flags) \
|
#define sf_ordered_writer_set_flags(ctx, flags) \
|
||||||
|
|
|
||||||
|
|
@ -458,15 +458,16 @@ static inline void sf_log_network_error_ex1(SFResponseInfo *response,
|
||||||
sf_log_network_error_ex1(response, conn, service_name, result, \
|
sf_log_network_error_ex1(response, conn, service_name, result, \
|
||||||
LOG_ERR, __FILE__, __LINE__)
|
LOG_ERR, __FILE__, __LINE__)
|
||||||
|
|
||||||
#define sf_log_network_error_for_update_ex(response, \
|
#define sf_log_network_error_for_update_ex(response, conn, \
|
||||||
conn, service_name, result, file, line) \
|
service_name, result, enoent_log_level, file, line) \
|
||||||
sf_log_network_error_ex1(response, conn, service_name, result, \
|
sf_log_network_error_ex1(response, conn, service_name, result, \
|
||||||
(result == SF_RETRIABLE_ERROR_CHANNEL_INVALID) ? \
|
(result == SF_RETRIABLE_ERROR_CHANNEL_INVALID) ? \
|
||||||
LOG_DEBUG : LOG_ERR, file, line)
|
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) \
|
#define sf_log_network_error_for_update(response, conn, service_name, result) \
|
||||||
sf_log_network_error_for_update_ex(response, conn, \
|
sf_log_network_error_for_update_ex(response, conn, service_name, \
|
||||||
service_name, result, __FILE__, __LINE__)
|
result, LOG_ERR, __FILE__, __LINE__)
|
||||||
|
|
||||||
#define sf_log_network_error_for_delete_ex(response, conn, \
|
#define sf_log_network_error_for_delete_ex(response, conn, \
|
||||||
service_name, result, enoent_log_level, file, line) \
|
service_name, result, enoent_log_level, file, line) \
|
||||||
|
|
@ -561,6 +562,16 @@ int sf_recv_response(ConnectionInfo *conn, SFResponseInfo *response,
|
||||||
const int network_timeout, const unsigned char expect_cmd,
|
const int network_timeout, const unsigned char expect_cmd,
|
||||||
char *recv_data, const int expect_body_len);
|
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,
|
int sf_recv_vary_response(ConnectionInfo *conn, SFResponseInfo *response,
|
||||||
const int network_timeout, const unsigned char expect_cmd,
|
const int network_timeout, const unsigned char expect_cmd,
|
||||||
SFProtoRecvBuffer *buffer, const int min_body_len);
|
SFProtoRecvBuffer *buffer, const int min_body_len);
|
||||||
|
|
@ -616,6 +627,35 @@ static inline int sf_proto_send_buf1(ConnectionInfo *conn, char *data,
|
||||||
return result;
|
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,
|
int sf_send_and_recv_response_header(ConnectionInfo *conn, char *data,
|
||||||
const int len, SFResponseInfo *response, const int network_timeout);
|
const int len, SFResponseInfo *response, const int network_timeout);
|
||||||
|
|
||||||
|
|
|
||||||
141
src/sf_service.c
141
src/sf_service.c
|
|
@ -33,9 +33,8 @@
|
||||||
#include "fastcommon/sched_thread.h"
|
#include "fastcommon/sched_thread.h"
|
||||||
#include "fastcommon/ioevent_loop.h"
|
#include "fastcommon/ioevent_loop.h"
|
||||||
#include "fastcommon/fc_memory.h"
|
#include "fastcommon/fc_memory.h"
|
||||||
#include "sf_nio.h"
|
#include "sf_proto.h"
|
||||||
#include "sf_util.h"
|
#include "sf_util.h"
|
||||||
#include "sf_global.h"
|
|
||||||
#include "sf_service.h"
|
#include "sf_service.h"
|
||||||
|
|
||||||
#if defined(OS_LINUX)
|
#if defined(OS_LINUX)
|
||||||
|
|
@ -61,11 +60,14 @@ struct worker_thread_context {
|
||||||
static void *worker_thread_entrance(void *arg);
|
static void *worker_thread_entrance(void *arg);
|
||||||
|
|
||||||
static int sf_init_free_queue(SFContext *sf_context, const char *name,
|
static int sf_init_free_queue(SFContext *sf_context, const char *name,
|
||||||
const bool double_buffers, const int task_padding_size,
|
const bool double_buffers, const bool need_shrink_task_buffer,
|
||||||
const int task_arg_size, TaskInitCallback init_callback)
|
const int task_padding_size, const int task_arg_size,
|
||||||
|
TaskInitCallback init_callback, void *init_arg)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
int buffer_size;
|
||||||
int m;
|
int m;
|
||||||
|
int max_m;
|
||||||
int alloc_conn_once;
|
int alloc_conn_once;
|
||||||
|
|
||||||
if ((result=set_rand_seed()) != 0) {
|
if ((result=set_rand_seed()) != 0) {
|
||||||
|
|
@ -74,18 +76,27 @@ static int sf_init_free_queue(SFContext *sf_context, const char *name,
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
m = sf_context->net_buffer_cfg.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) {
|
if (m == 0) {
|
||||||
m = 1;
|
m = 1;
|
||||||
} else if (m > 16) {
|
} else if (m > max_m) {
|
||||||
m = 16;
|
m = max_m;
|
||||||
}
|
}
|
||||||
alloc_conn_once = 256 / m;
|
alloc_conn_once = 256 / m;
|
||||||
|
|
||||||
return free_queue_init_ex2(&sf_context->free_queue, name, double_buffers,
|
return free_queue_init_ex2(&sf_context->free_queue, name, double_buffers,
|
||||||
sf_context->net_buffer_cfg.max_connections, alloc_conn_once,
|
need_shrink_task_buffer, sf_context->net_buffer_cfg.max_connections,
|
||||||
sf_context->net_buffer_cfg.min_buff_size, sf_context->
|
alloc_conn_once, sf_context->net_buffer_cfg.min_buff_size,
|
||||||
net_buffer_cfg.max_buff_size, task_padding_size,
|
sf_context->net_buffer_cfg.max_buff_size, task_padding_size,
|
||||||
task_arg_size, init_callback);
|
task_arg_size, init_callback, init_arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
int sf_service_init_ex2(SFContext *sf_context, const char *name,
|
int sf_service_init_ex2(SFContext *sf_context, const char *name,
|
||||||
|
|
@ -100,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,
|
sf_recv_timeout_callback timeout_callback, const int net_timeout_ms,
|
||||||
const int proto_header_size, const int task_padding_size,
|
const int proto_header_size, const int task_padding_size,
|
||||||
const int task_arg_size, const bool double_buffers,
|
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)
|
sf_release_buffer_callback release_buffer_callback)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
int bytes;
|
int bytes;
|
||||||
int extra_events;
|
int extra_events;
|
||||||
|
int max_entries;
|
||||||
int i;
|
int i;
|
||||||
struct worker_thread_context *thread_contexts;
|
struct worker_thread_context *thread_contexts;
|
||||||
struct worker_thread_context *thread_ctx;
|
struct worker_thread_context *thread_ctx;
|
||||||
|
|
@ -114,7 +127,7 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name,
|
||||||
pthread_t tid;
|
pthread_t tid;
|
||||||
pthread_attr_t thread_attr;
|
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->connect_need_log = true;
|
||||||
sf_context->realloc_task_buffer = sf_context->net_buffer_cfg.
|
sf_context->realloc_task_buffer = sf_context->net_buffer_cfg.
|
||||||
min_buff_size < sf_context->net_buffer_cfg.max_buff_size;
|
min_buff_size < sf_context->net_buffer_cfg.max_buff_size;
|
||||||
|
|
@ -131,7 +144,8 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name,
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((result=sf_init_free_queue(sf_context, name, double_buffers,
|
if ((result=sf_init_free_queue(sf_context, name, double_buffers,
|
||||||
task_padding_size, task_arg_size, init_callback)) != 0)
|
need_shrink_task_buffer, task_padding_size,
|
||||||
|
task_arg_size, init_callback, init_arg)) != 0)
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -159,7 +173,11 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name,
|
||||||
|
|
||||||
if (SF_G_EPOLL_EDGE_TRIGGER) {
|
if (SF_G_EPOLL_EDGE_TRIGGER) {
|
||||||
#ifdef OS_LINUX
|
#ifdef OS_LINUX
|
||||||
|
#if IOEVENT_USE_EPOLL
|
||||||
extra_events = EPOLLET;
|
extra_events = EPOLLET;
|
||||||
|
#else
|
||||||
|
extra_events = 0;
|
||||||
|
#endif
|
||||||
#elif defined(OS_FREEBSD)
|
#elif defined(OS_FREEBSD)
|
||||||
extra_events = EV_CLEAR;
|
extra_events = EV_CLEAR;
|
||||||
#else
|
#else
|
||||||
|
|
@ -169,6 +187,40 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name,
|
||||||
extra_events = 0;
|
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);
|
g_current_time = time(NULL);
|
||||||
sf_context->thread_count = 0;
|
sf_context->thread_count = 0;
|
||||||
data_end = sf_context->thread_data + sf_context->work_threads;
|
data_end = sf_context->thread_data + sf_context->work_threads;
|
||||||
|
|
@ -193,18 +245,43 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name,
|
||||||
thread_data->arg = NULL;
|
thread_data->arg = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ioevent_init(&thread_data->ev_puller, 2 + sf_context->
|
if ((result=ioevent_init(&thread_data->ev_puller, sf_context->
|
||||||
net_buffer_cfg.max_connections, net_timeout_ms,
|
name, sf_context->use_io_uring, max_entries,
|
||||||
extra_events) != 0)
|
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, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"ioevent_init fail, "
|
"ioevent_init fail, errno: %d, error info: %s.%s"
|
||||||
"errno: %d, error info: %s",
|
, __LINE__, result, strerror(result), prompt);
|
||||||
__LINE__, result, strerror(result));
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#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->
|
result = fast_timer_init(&thread_data->timer, 2 * sf_context->
|
||||||
net_buffer_cfg.network_timeout, g_current_time);
|
net_buffer_cfg.network_timeout, g_current_time);
|
||||||
if (result != 0) {
|
if (result != 0) {
|
||||||
|
|
@ -485,7 +562,9 @@ struct fast_task_info *sf_socket_accept_connection(SFListener *listener)
|
||||||
}
|
}
|
||||||
FC_SET_CLOEXEC(incomesock);
|
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);
|
close(incomesock);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
@ -496,12 +575,6 @@ struct fast_task_info *sf_socket_accept_connection(SFListener *listener)
|
||||||
return task;
|
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)
|
void sf_socket_close_ex(SFContext *sf_context)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
@ -780,15 +853,13 @@ int sf_setup_signal_handler()
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define LOG_SCHEDULE_ENTRIES_COUNT 3
|
|
||||||
|
|
||||||
int sf_startup_schedule(pthread_t *schedule_tid)
|
int sf_startup_schedule(pthread_t *schedule_tid)
|
||||||
{
|
{
|
||||||
ScheduleArray scheduleArray;
|
ScheduleArray scheduleArray;
|
||||||
ScheduleEntry scheduleEntries[LOG_SCHEDULE_ENTRIES_COUNT];
|
ScheduleEntry scheduleEntries[SF_LOG_SCHEDULE_ENTRIES_COUNT];
|
||||||
|
|
||||||
scheduleArray.entries = scheduleEntries;
|
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);
|
&scheduleArray);
|
||||||
return sched_start(&scheduleArray, schedule_tid,
|
return sched_start(&scheduleArray, schedule_tid,
|
||||||
g_sf_global_vars.thread_stack_size, (bool * volatile)
|
g_sf_global_vars.thread_stack_size, (bool * volatile)
|
||||||
|
|
@ -799,7 +870,7 @@ int sf_add_slow_log_schedule(SFSlowLogContext *slowlog_ctx)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
ScheduleArray scheduleArray;
|
ScheduleArray scheduleArray;
|
||||||
ScheduleEntry scheduleEntries[LOG_SCHEDULE_ENTRIES_COUNT];
|
ScheduleEntry scheduleEntries[SF_LOG_SCHEDULE_ENTRIES_COUNT];
|
||||||
|
|
||||||
if (!slowlog_ctx->cfg.enabled) {
|
if (!slowlog_ctx->cfg.enabled) {
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -812,8 +883,8 @@ int sf_add_slow_log_schedule(SFSlowLogContext *slowlog_ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
scheduleArray.entries = scheduleEntries;
|
scheduleArray.entries = scheduleEntries;
|
||||||
sf_setup_schedule(&slowlog_ctx->ctx, &slowlog_ctx->cfg.log_cfg,
|
sf_logger_setup_schedule(&slowlog_ctx->ctx, &slowlog_ctx->
|
||||||
&scheduleArray);
|
cfg.log_cfg, &scheduleArray);
|
||||||
return sched_add_entries(&scheduleArray);
|
return sched_add_entries(&scheduleArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,9 @@
|
||||||
#include "fastcommon/ioevent.h"
|
#include "fastcommon/ioevent.h"
|
||||||
#include "fastcommon/fast_task_queue.h"
|
#include "fastcommon/fast_task_queue.h"
|
||||||
#include "sf_types.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_alloc_thread_extra_data_callback)(const int thread_index);
|
||||||
typedef void (*sf_sig_quit_handler)(int sig);
|
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,
|
sf_recv_timeout_callback timeout_callback, const int net_timeout_ms,
|
||||||
const int proto_header_size, const int task_padding_size,
|
const int proto_header_size, const int task_padding_size,
|
||||||
const int task_arg_size, const bool double_buffers,
|
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);
|
sf_release_buffer_callback release_buffer_callback);
|
||||||
|
|
||||||
#define sf_service_init_ex(sf_context, name, alloc_thread_extra_data_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, \
|
thread_loop_callback, accept_done_callback, set_body_length_func, \
|
||||||
NULL, send_done_callback, deal_func, task_cleanup_func, \
|
NULL, send_done_callback, deal_func, task_cleanup_func, \
|
||||||
timeout_callback, net_timeout_ms, proto_header_size, \
|
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, \
|
#define sf_service_init(name, alloc_thread_extra_data_callback, \
|
||||||
thread_loop_callback, accept_done_callback, set_body_length_func, \
|
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, \
|
sf_service_init_ex2(&g_sf_context, name, alloc_thread_extra_data_callback, \
|
||||||
thread_loop_callback, accept_done_callback, set_body_length_func, NULL,\
|
thread_loop_callback, accept_done_callback, set_body_length_func, NULL,\
|
||||||
send_done_callback, deal_func, task_cleanup_func, timeout_callback, \
|
send_done_callback, deal_func, task_cleanup_func, timeout_callback, \
|
||||||
net_timeout_ms, proto_header_size, 0, task_arg_size, false, false, \
|
net_timeout_ms, proto_header_size, 0, task_arg_size, false, true, \
|
||||||
NULL, NULL)
|
false, NULL, NULL, NULL)
|
||||||
|
|
||||||
int sf_service_destroy_ex(SFContext *sf_context);
|
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);
|
void sf_socket_close_server(SFListener *listener);
|
||||||
struct fast_task_info *sf_socket_accept_connection(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);
|
int sf_socket_server_ex(SFContext *sf_context);
|
||||||
#define sf_socket_server() sf_socket_server_ex(&g_sf_context)
|
#define sf_socket_server() sf_socket_server_ex(&g_sf_context)
|
||||||
|
|
||||||
|
|
@ -163,6 +165,11 @@ static inline struct fast_task_info *sf_alloc_init_task_ex(
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (task->shrinked) {
|
||||||
|
task->shrinked = false;
|
||||||
|
sf_proto_init_task_magic(task);
|
||||||
|
}
|
||||||
|
|
||||||
__sync_add_and_fetch(&task->reffer_count, reffer_count);
|
__sync_add_and_fetch(&task->reffer_count, reffer_count);
|
||||||
__sync_bool_compare_and_swap(&task->canceled, 1, 0);
|
__sync_bool_compare_and_swap(&task->canceled, 1, 0);
|
||||||
task->handler = handler;
|
task->handler = handler;
|
||||||
|
|
@ -170,12 +177,42 @@ static inline struct fast_task_info *sf_alloc_init_task_ex(
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define sf_hold_task_ex(task, inc_count) __sync_add_and_fetch( \
|
#define sf_hold_task_ex(task, inc_count) fc_hold_task_ex(task, inc_count)
|
||||||
&task->reffer_count, inc_count)
|
#define sf_hold_task(task) fc_hold_task(task)
|
||||||
#define sf_hold_task(task) sf_hold_task_ex(task, 1)
|
|
||||||
|
|
||||||
#define sf_alloc_init_task(handler, fd) sf_alloc_init_task_ex(handler, fd, 1)
|
#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)
|
static inline void sf_release_task(struct fast_task_info *task)
|
||||||
{
|
{
|
||||||
if (__sync_sub_and_fetch(&task->reffer_count, 1) == 0) {
|
if (__sync_sub_and_fetch(&task->reffer_count, 1) == 0) {
|
||||||
|
|
@ -187,6 +224,15 @@ static inline void sf_release_task(struct fast_task_info *task)
|
||||||
"used: %d, freed: %d", __LINE__, task,
|
"used: %d, freed: %d", __LINE__, task,
|
||||||
alloc_count, alloc_count - free_count, free_count);
|
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);
|
free_queue_push(task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -262,6 +308,19 @@ static inline SFNetworkHandler *sf_get_rdma_network_handler3(
|
||||||
return sf_get_rdma_network_handler(sf_context3);
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,7 @@
|
||||||
|
|
||||||
#define SF_BINLOG_BUFFER_PRODUCER_DATA_LENGTH(bf) ((bf).data_end - (bf).buff)
|
#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_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)
|
#define SF_BINLOG_BUFFER_CONSUMER_DATA_REMAIN(bf) ((bf).data_end - (bf).current)
|
||||||
|
|
||||||
typedef int (*sf_accept_done_callback)(struct fast_task_info *task,
|
typedef int (*sf_accept_done_callback)(struct fast_task_info *task,
|
||||||
|
|
@ -114,6 +115,7 @@ typedef struct sf_listener {
|
||||||
|
|
||||||
struct sf_context;
|
struct sf_context;
|
||||||
struct sf_address_family_handler;
|
struct sf_address_family_handler;
|
||||||
|
|
||||||
typedef struct sf_network_handler {
|
typedef struct sf_network_handler {
|
||||||
bool enabled;
|
bool enabled;
|
||||||
bool explicit_post_recv;
|
bool explicit_post_recv;
|
||||||
|
|
@ -178,7 +180,9 @@ typedef struct sf_context {
|
||||||
struct nio_thread_data *thread_data;
|
struct nio_thread_data *thread_data;
|
||||||
volatile int thread_count;
|
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;
|
SFAddressFamily address_family;
|
||||||
SFAddressFamilyHandler handlers[SF_ADDRESS_FAMILY_COUNT];
|
SFAddressFamilyHandler handlers[SF_ADDRESS_FAMILY_COUNT];
|
||||||
|
|
||||||
|
|
@ -188,7 +192,6 @@ typedef struct sf_context {
|
||||||
int work_threads;
|
int work_threads;
|
||||||
|
|
||||||
int header_size;
|
int header_size;
|
||||||
bool remove_from_ready_list;
|
|
||||||
bool realloc_task_buffer;
|
bool realloc_task_buffer;
|
||||||
bool connect_need_log; //for client connect
|
bool connect_need_log; //for client connect
|
||||||
FCSmartPollingConfig smart_polling;
|
FCSmartPollingConfig smart_polling;
|
||||||
|
|
|
||||||
|
|
@ -281,7 +281,7 @@ int sf_logger_init(LogContext *pContext, const char *filename_prefix)
|
||||||
return 0;
|
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)
|
SFLogConfig *log_cfg, ScheduleEntry *pScheduleEntry)
|
||||||
{
|
{
|
||||||
INIT_SCHEDULE_ENTRY(*pScheduleEntry, sched_generate_next_id(),
|
INIT_SCHEDULE_ENTRY(*pScheduleEntry, sched_generate_next_id(),
|
||||||
|
|
|
||||||
|
|
@ -96,14 +96,14 @@ void sf_parse_cmd_option_bool(int argc, char *argv[],
|
||||||
|
|
||||||
int sf_logger_init(LogContext *pContext, const char *filename_prefix);
|
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);
|
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)
|
SFLogConfig *log_cfg, ScheduleArray *scheduleArray)
|
||||||
{
|
{
|
||||||
ScheduleEntry *scheduleEntry;
|
ScheduleEntry *scheduleEntry;
|
||||||
scheduleEntry = sf_logger_set_schedule_entry(pContext,
|
scheduleEntry = sf_logger_set_schedule_entries(pContext,
|
||||||
log_cfg, scheduleArray->entries);
|
log_cfg, scheduleArray->entries);
|
||||||
scheduleArray->count = scheduleEntry - scheduleArray->entries;
|
scheduleArray->count = scheduleEntry - scheduleArray->entries;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue