connect to storage server failover with multi IPs

pull/687/head
YuQing 2023-12-11 16:07:45 +08:00
parent 48fb05dbb2
commit fd77ababf5
5 changed files with 94 additions and 31 deletions

View File

@ -348,39 +348,62 @@ static int fdfs_client_do_init_ex(TrackerServerGroup *pTrackerGroup, \
return result; return result;
} }
load_fdfs_parameters_from_tracker = iniGetBoolValue(NULL, \ load_fdfs_parameters_from_tracker = iniGetBoolValue(NULL,
"load_fdfs_parameters_from_tracker", \ "load_fdfs_parameters_from_tracker",
iniContext, false); iniContext, false);
if (load_fdfs_parameters_from_tracker) if (load_fdfs_parameters_from_tracker)
{ {
fdfs_get_params_from_tracker(&use_storage_id); if ((result=fdfs_get_params_from_tracker(&use_storage_id)) != 0)
{
return result;
}
} }
else else
{ {
use_storage_id = iniGetBoolValue(NULL, "use_storage_id", \ use_storage_id = iniGetBoolValue(NULL, "use_storage_id",
iniContext, false); iniContext, false);
if (use_storage_id) if (use_storage_id)
{ {
result = fdfs_load_storage_ids_from_file( \ if ((result=fdfs_load_storage_ids_from_file(
conf_filename, iniContext); conf_filename, iniContext)) != 0)
} {
} return result;
}
}
}
if (use_storage_id)
{
FDFSStorageIdInfo *idInfo;
FDFSStorageIdInfo *end;
end = g_storage_ids_by_id.ids + g_storage_ids_by_id.count;
for (idInfo=g_storage_ids_by_id.ids; idInfo<end; idInfo++)
{
if (idInfo->ip_addrs.count > 1)
{
g_multi_storage_ips = true;
break;
}
}
}
#ifdef DEBUG_FLAG #ifdef DEBUG_FLAG
logDebug("base_path=%s, " \ logDebug("base_path=%s, "
"connect_timeout=%d, "\ "connect_timeout=%d, "
"network_timeout=%d, "\ "network_timeout=%d, "
"tracker_server_count=%d, " \ "tracker_server_count=%d, "
"anti_steal_token=%d, " \ "anti_steal_token=%d, "
"anti_steal_secret_key length=%d, " \ "anti_steal_secret_key length=%d, "
"use_connection_pool=%d, " \ "use_connection_pool=%d, "
"g_connection_pool_max_idle_time=%ds, " \ "g_connection_pool_max_idle_time=%ds, "
"use_storage_id=%d, storage server id count: %d\n", \ "use_storage_id=%d, storage server id count: %d, "
SF_G_BASE_PATH_STR, SF_G_CONNECT_TIMEOUT, \ "multi storage ips: %d\n",
SF_G_NETWORK_TIMEOUT, pTrackerGroup->server_count, \ SF_G_BASE_PATH_STR, SF_G_CONNECT_TIMEOUT,
g_anti_steal_token, g_anti_steal_secret_key.length, \ SF_G_NETWORK_TIMEOUT, pTrackerGroup->server_count,
g_use_connection_pool, g_connection_pool_max_idle_time, \ g_anti_steal_token, g_anti_steal_secret_key.length,
use_storage_id, g_storage_ids_by_id.count); g_use_connection_pool, g_connection_pool_max_idle_time,
use_storage_id, g_storage_ids_by_id.count, g_multi_storage_ips);
#endif #endif
return 0; return 0;

View File

@ -13,5 +13,6 @@
int g_tracker_server_http_port = 80; int g_tracker_server_http_port = 80;
TrackerServerGroup g_tracker_group = {0, 0, -1, NULL}; TrackerServerGroup g_tracker_group = {0, 0, -1, NULL};
bool g_multi_storage_ips = false;
bool g_anti_steal_token = false; bool g_anti_steal_token = false;
BufferInfo g_anti_steal_secret_key = {0}; BufferInfo g_anti_steal_secret_key = {0};

View File

@ -22,6 +22,7 @@ extern "C" {
extern int g_tracker_server_http_port; extern int g_tracker_server_http_port;
extern TrackerServerGroup g_tracker_group; extern TrackerServerGroup g_tracker_group;
extern bool g_multi_storage_ips;
extern bool g_anti_steal_token; extern bool g_anti_steal_token;
extern BufferInfo g_anti_steal_secret_key; extern BufferInfo g_anti_steal_secret_key;

View File

@ -96,7 +96,7 @@ int main(int argc, char *argv[])
} }
log_init(); log_init();
g_log_context.log_level = LOG_DEBUG; //g_log_context.log_level = LOG_DEBUG;
ignore_signal_pipe(); ignore_signal_pipe();
if ((result=fdfs_client_init(conf_filename)) != 0) if ((result=fdfs_client_init(conf_filename)) != 0)

View File

@ -64,6 +64,44 @@ static int g_base64_context_inited = 0;
ppStorageServer, TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE, \ ppStorageServer, TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE, \
group_name, filename, pNewStorage, new_connection) group_name, filename, pNewStorage, new_connection)
static ConnectionInfo *storage_make_connection(
ConnectionInfo *pStorageServer, int *err_no)
{
ConnectionInfo *conn;
FDFSStorageIdInfo *idInfo;
if ((conn=tracker_make_connection(pStorageServer, err_no)) != NULL)
{
return conn;
}
if (!g_multi_storage_ips)
{
return NULL;
}
if ((idInfo=fdfs_get_storage_id_by_ip_port(pStorageServer->ip_addr,
pStorageServer->port)) == NULL)
{
return NULL;
}
if (idInfo->ip_addrs.count < 2)
{
return NULL;
}
if (strcmp(pStorageServer->ip_addr, idInfo->ip_addrs.ips[0].address) == 0)
{
strcpy(pStorageServer->ip_addr, idInfo->ip_addrs.ips[1].address);
}
else
{
strcpy(pStorageServer->ip_addr, idInfo->ip_addrs.ips[0].address);
}
return tracker_make_connection(pStorageServer, err_no);
}
static int storage_get_connection(ConnectionInfo *pTrackerServer, \ static int storage_get_connection(ConnectionInfo *pTrackerServer, \
ConnectionInfo **ppStorageServer, const byte cmd, \ ConnectionInfo **ppStorageServer, const byte cmd, \
const char *group_name, const char *filename, \ const char *group_name, const char *filename, \
@ -97,7 +135,7 @@ static int storage_get_connection(ConnectionInfo *pTrackerServer, \
return result; return result;
} }
if ((*ppStorageServer=tracker_make_connection(pNewStorage, if ((*ppStorageServer=storage_make_connection(pNewStorage,
&result)) == NULL) &result)) == NULL)
{ {
return result; return result;
@ -113,7 +151,7 @@ static int storage_get_connection(ConnectionInfo *pTrackerServer, \
} }
else else
{ {
if ((*ppStorageServer=tracker_make_connection( if ((*ppStorageServer=storage_make_connection(
*ppStorageServer, &result)) == NULL) *ppStorageServer, &result)) == NULL)
{ {
return result; return result;
@ -162,7 +200,7 @@ static int storage_get_upload_connection(ConnectionInfo *pTrackerServer, \
return result; return result;
} }
if ((*ppStorageServer=tracker_make_connection(pNewStorage, if ((*ppStorageServer=storage_make_connection(pNewStorage,
&result)) == NULL) &result)) == NULL)
{ {
return result; return result;
@ -178,7 +216,7 @@ static int storage_get_upload_connection(ConnectionInfo *pTrackerServer, \
} }
else else
{ {
if ((*ppStorageServer=tracker_make_connection( if ((*ppStorageServer=storage_make_connection(
*ppStorageServer, &result)) == NULL) *ppStorageServer, &result)) == NULL)
{ {
return result; return result;