From ba41d958b2bfa478d076ca76a76e21f83719a6f2 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Mon, 11 Dec 2023 19:20:32 +0800 Subject: [PATCH] client.conf add config item: connect_first_by --- client/client_func.c | 19 +++++++++++-- client/client_global.c | 1 + client/client_global.h | 6 ++++ client/storage_client.c | 61 ++++++++++++++++++++++++++++++++++++++++- conf/client.conf | 7 +++++ 5 files changed, 91 insertions(+), 3 deletions(-) diff --git a/client/client_func.c b/client/client_func.c index ba8d62f..4508a6f 100644 --- a/client/client_func.c +++ b/client/client_func.c @@ -376,6 +376,7 @@ static int fdfs_client_do_init_ex(TrackerServerGroup *pTrackerGroup, \ { FDFSStorageIdInfo *idInfo; FDFSStorageIdInfo *end; + char *connect_first_by; end = g_storage_ids_by_id.ids + g_storage_ids_by_id.count; for (idInfo=g_storage_ids_by_id.ids; idInfoserver_count, g_anti_steal_token, g_anti_steal_secret_key.length, g_use_connection_pool, g_connection_pool_max_idle_time, - use_storage_id, g_storage_ids_by_id.count, g_multi_storage_ips); + use_storage_id, g_connect_first_by == fdfs_connect_first_by_tracker ? + "tracker" : "last-connected", g_storage_ids_by_id.count, + g_multi_storage_ips); #endif return 0; diff --git a/client/client_global.c b/client/client_global.c index 4db57cd..680584c 100644 --- a/client/client_global.c +++ b/client/client_global.c @@ -14,5 +14,6 @@ int g_tracker_server_http_port = 80; TrackerServerGroup g_tracker_group = {0, 0, -1, NULL}; bool g_multi_storage_ips = false; +FDFSConnectFirstBy g_connect_first_by = fdfs_connect_first_by_tracker; bool g_anti_steal_token = false; BufferInfo g_anti_steal_secret_key = {0}; diff --git a/client/client_global.h b/client/client_global.h index 45b5fea..175cf78 100644 --- a/client/client_global.h +++ b/client/client_global.h @@ -15,6 +15,11 @@ #include "tracker_types.h" #include "fdfs_shared_func.h" +typedef enum { + fdfs_connect_first_by_tracker, + fdfs_connect_first_by_last_connected +} FDFSConnectFirstBy; + #ifdef __cplusplus extern "C" { #endif @@ -23,6 +28,7 @@ extern int g_tracker_server_http_port; extern TrackerServerGroup g_tracker_group; extern bool g_multi_storage_ips; +extern FDFSConnectFirstBy g_connect_first_by; extern bool g_anti_steal_token; extern BufferInfo g_anti_steal_secret_key; diff --git a/client/storage_client.c b/client/storage_client.c index 92050dc..5acfa9b 100644 --- a/client/storage_client.c +++ b/client/storage_client.c @@ -64,7 +64,7 @@ static int g_base64_context_inited = 0; ppStorageServer, TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE, \ group_name, filename, pNewStorage, new_connection) -static ConnectionInfo *storage_make_connection( +static ConnectionInfo *make_connection_by_tracker( ConnectionInfo *pStorageServer, int *err_no) { ConnectionInfo *conn; @@ -102,6 +102,65 @@ static ConnectionInfo *storage_make_connection( return tracker_make_connection(pStorageServer, err_no); } +static ConnectionInfo *make_connection_by_last_connected( + ConnectionInfo *pStorageServer, int *err_no) +{ + ConnectionInfo *conn; + FDFSStorageIdInfo *idInfo; + int index; + + if (!g_multi_storage_ips) + { + return tracker_make_connection(pStorageServer, err_no); + } + + if ((idInfo=fdfs_get_storage_id_by_ip_port(pStorageServer->ip_addr, + pStorageServer->port)) == NULL) + { + return tracker_make_connection(pStorageServer, err_no); + } + if (idInfo->ip_addrs.count < 2) + { + return tracker_make_connection(pStorageServer, err_no); + } + + index = idInfo->ip_addrs.index; + if (strcmp(pStorageServer->ip_addr, idInfo->ip_addrs. + ips[index].address) != 0) + { + strcpy(pStorageServer->ip_addr, idInfo->ip_addrs. + ips[index].address); + } + if ((conn=tracker_make_connection(pStorageServer, err_no)) != NULL) + { + return conn; + } + + if (++index == idInfo->ip_addrs.count) + { + index = 0; + } + strcpy(pStorageServer->ip_addr, idInfo->ip_addrs.ips[index].address); + if ((conn=tracker_make_connection(pStorageServer, err_no)) != NULL) + { + idInfo->ip_addrs.index = index; + } + return conn; +} + +static inline ConnectionInfo *storage_make_connection( + ConnectionInfo *pStorageServer, int *err_no) +{ + if (g_connect_first_by == fdfs_connect_first_by_tracker) + { + return make_connection_by_tracker(pStorageServer, err_no); + } + else + { + return make_connection_by_last_connected(pStorageServer, err_no); + } +} + static int storage_get_connection(ConnectionInfo *pTrackerServer, \ ConnectionInfo **ppStorageServer, const byte cmd, \ const char *group_name, const char *filename, \ diff --git a/conf/client.conf b/conf/client.conf index 9b858ee..99e5f91 100644 --- a/conf/client.conf +++ b/conf/client.conf @@ -37,6 +37,13 @@ tracker_server = 192.168.0.197:22122 ### debug log_level = info +# connect which ip address first for multi IPs of a storage server, value list: +## tracker: connect to the ip address return by tracker server first +## last-connected: connect to the ip address last connected first +# default value is tracker +# since V6.11 +connect_first_by = tracker + # if use connection pool # default value is false # since V4.05