-
Notifications
You must be signed in to change notification settings - Fork 2k
/
storage_sync_func.c
170 lines (157 loc) · 5.27 KB
/
storage_sync_func.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/** * Copyright (C) 2008 Happy Fish / YuQing
*
* FastDFS may be copied only under the terms of the GNU General
* Public License V3, which may be found in the FastDFS source kit.
* Please visit the FastDFS Home Page http://www.fastken.com/ for more detail.
**/
//storage_sync_func.c
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include "fdfs_define.h"
#include "fdfs_global.h"
#include "fastcommon/logger.h"
#include "fastcommon/sockopt.h"
#include "tracker_types.h"
#include "tracker_proto.h"
#include "storage_global.h"
#include "storage_func.h"
#include "storage_sync_func.h"
void storage_sync_connect_storage_server_ex(const FDFSStorageBrief *pStorage,
ConnectionInfo *conn, bool *check_flag)
{
int nContinuousFail;
int previousCodes[FDFS_MULTI_IP_MAX_COUNT];
int conn_results[FDFS_MULTI_IP_MAX_COUNT];
int result;
int i;
FDFSMultiIP ip_addrs;
FDFSMultiIP *multi_ip;
const char *bind_addr;
char formatted_ip[FORMATTED_IP_SIZE];
multi_ip = NULL;
if (g_use_storage_id)
{
FDFSStorageIdInfo *idInfo;
idInfo = fdfs_get_storage_by_id(pStorage->id);
if (idInfo == NULL)
{
logWarning("file: "__FILE__", line: %d, "
"storage server id: %s not exist "
"in storage_ids.conf from tracker server, "
"storage ip: %s", __LINE__, pStorage->id,
pStorage->ip_addr);
}
else
{
multi_ip = &idInfo->ip_addrs;
}
}
if (multi_ip != NULL)
{
ip_addrs = *multi_ip;
}
else
{
ip_addrs.count = 1;
ip_addrs.index = 0;
ip_addrs.ips[0].type = fdfs_get_ip_type(pStorage->ip_addr);
strcpy(ip_addrs.ips[0].address, pStorage->ip_addr);
}
conn->sock = -1;
nContinuousFail = 0;
memset(previousCodes, 0, sizeof(previousCodes));
memset(conn_results, 0, sizeof(conn_results));
while (SF_G_CONTINUE_FLAG && *check_flag &&
pStorage->status != FDFS_STORAGE_STATUS_DELETED &&
pStorage->status != FDFS_STORAGE_STATUS_IP_CHANGED &&
pStorage->status != FDFS_STORAGE_STATUS_NONE)
{
for (i=0; i<ip_addrs.count; i++)
{
strcpy(conn->ip_addr, ip_addrs.ips[i].address);
if (g_client_bind_addr)
{
bind_addr = is_ipv6_addr(conn->ip_addr) ?
SF_G_INNER_BIND_ADDR6 : SF_G_INNER_BIND_ADDR4;
}
else
{
bind_addr = NULL;
}
conn->sock = socketCreateExAuto(conn->ip_addr,
O_NONBLOCK, bind_addr, &result);
if (conn->sock < 0)
{
logCrit("file: "__FILE__", line: %d, "
"socket create fail, program exit!", __LINE__);
SF_G_CONTINUE_FLAG = false;
break;
}
if ((conn_results[i]=connectserverbyip_nb(conn->sock,
conn->ip_addr, SF_G_INNER_PORT,
SF_G_CONNECT_TIMEOUT)) == 0)
{
char szFailPrompt[64];
if (nContinuousFail == 0)
{
*szFailPrompt = '\0';
}
else
{
sprintf(szFailPrompt,
", continuous fail count: %d",
nContinuousFail);
}
format_ip_address(conn->ip_addr, formatted_ip);
logInfo("file: "__FILE__", line: %d, "
"successfully connect to "
"storage server %s:%u%s", __LINE__,
formatted_ip, SF_G_INNER_PORT, szFailPrompt);
nContinuousFail = 0;
break;
}
nContinuousFail++;
if (previousCodes[i] != conn_results[i])
{
format_ip_address(conn->ip_addr, formatted_ip);
logError("file: "__FILE__", line: %d, "
"connect to storage server %s:%u fail, "
"errno: %d, error info: %s",
__LINE__, formatted_ip, SF_G_INNER_PORT,
conn_results[i], STRERROR(conn_results[i]));
previousCodes[i] = conn_results[i];
}
close(conn->sock);
conn->sock = -1;
}
if (conn->sock >= 0 || !SF_G_CONTINUE_FLAG)
{
break;
}
sleep(1);
}
if (nContinuousFail > 0)
{
int avg_fails;
avg_fails = (nContinuousFail + ip_addrs.count - 1) / ip_addrs.count;
for (i=0; i<ip_addrs.count; i++)
{
format_ip_address(ip_addrs.ips[i].address, formatted_ip);
logError("file: "__FILE__", line: %d, "
"connect to storage server %s:%u fail, "
"try count: %d, errno: %d, error info: %s",
__LINE__, formatted_ip, SF_G_INNER_PORT, avg_fails,
conn_results[i], STRERROR(conn_results[i]));
}
}
}