-
Notifications
You must be signed in to change notification settings - Fork 3
/
replicate.c
351 lines (311 loc) · 7.86 KB
/
replicate.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* $Id$
*/
#include "config.h"
#ifdef HAVE_PRIVATE_SRCS
/* replace replicate.c with replicate_private.c */
#include "replicate_private.c"
#else /* ! HAVE_PRIVATE_SRCS */
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <libgen.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/time.h>
/*
* fuse.h requres that _FILE_OFFSET_BITS is defined in any case, but
* AC_SYS_LARGEFILE does not define it on a 64bit platform like x86_64
* since it is not necessary. To avoid this problem we define it here.
*/
#ifndef _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 64
#endif
#define FUSE_USE_VERSION 25
#include <fuse.h>
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
#include <gfarm/gfarm.h>
#include <gfarm2fs.h>
#include "gfarm2fs_msg_enums.h"
#ifdef HAVE_GFS_REPLICATE_FILE_TO
#define gfs_replicate_to(file, dsthost, dstport) \
gfs_replicate_file_to(file, dsthost, 0)
#endif
static int replicate_ncopy;
static int replicate_max_concurrency;
volatile sig_atomic_t replicate_concurrency = 0;
static int replicate_enabled;
#define XATTR_NCOPY "gfarm.ncopy"
/*
* #define UNSAFE_DEBUG
*
* Since gflog_info() is not async-signal-safe,
* UNSAFE_DEBUG shouldn't be defined.
*/
static void
sigchld_handler(int sig)
{
int pid, status;
#ifdef UNSAFE_DEBUG
int no;
char *msg;
#endif
for (;;) {
pid = waitpid(-1, &status, WNOHANG);
if (pid == -1 || pid == 0)
break;
--replicate_concurrency;
#ifdef UNSAFE_DEBUG
if (WIFEXITED(status)) {
msg = "exit";
no = WEXITSTATUS(status);
} else if (WIFSIGNALED(status)) {
msg = "killed by signal";
no = WTERMSIG(status);
} else {
msg = "unknown status";
no = status;
}
gflog_info(GFARM_MSG_2000041, "replicate [%d]: %s %d",
pid, msg, no);
#endif
}
}
void
gfarm2fs_replicate_init(struct gfarm2fs_param *param)
{
struct sigaction sa;
if (param->ncopy < 1 || param->copy_limit < 0)
return;
replicate_ncopy = param->ncopy;
replicate_max_concurrency = param->copy_limit;
#if defined(HAVE_SYS_XATTR_H) && defined(ENABLE_XATTR) && \
defined(HAVE_GFARM_XATTR_CACHING) && \
defined(HAVE_GFARM_XATTR_CACHING_PATTERN_ADD)
if (!gfarm_xattr_caching(XATTR_NCOPY)) {
gfarm_xattr_caching_pattern_add(XATTR_NCOPY);
gfs_stat_cache_clear();
}
#endif
sa.sa_handler = sigchld_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_NOCLDSTOP;
sigaction(SIGCHLD, &sa, NULL);
replicate_enabled = 1;
}
void
gfarm2fs_replicate_final(void)
{
/* nothing to do for now */
}
static int
get_required_ncopy(const char *path)
{
int ncopy = replicate_ncopy;
#if defined(HAVE_SYS_XATTR_H) && defined(ENABLE_XATTR)
char *p, *n, *ep, s_ncopy[GFARM_INT32STRLEN];
size_t size_ncopy;
gfarm_error_t e;
int nc;
p = strdup(path);
if (p == NULL)
return (ncopy);
for (;;) {
size_ncopy = sizeof(s_ncopy);
e = gfs_getxattr_cached(p, XATTR_NCOPY, s_ncopy, &size_ncopy);
if (e == GFARM_ERR_NO_ERROR) {
s_ncopy[size_ncopy] = '\0';
nc = strtol(s_ncopy, &ep, 10);
if (*ep == '\0') {
ncopy = nc;
break;
}
}
n = gfarm_url_dir(p);
if (strcmp(n, p) == 0) {
free(n);
break;
}
free(p);
p = n;
}
free(p);
#endif
return (ncopy);
}
static int
stat_ncopy(const char *path)
{
struct gfs_stat st;
gfarm_error_t e;
int ncopy;
e = gfs_lstat_cached(path, &st);
if (e == GFARM_ERR_NO_ERROR) {
ncopy = st.st_ncopy;
gfs_stat_free(&st);
return (ncopy);
}
return (0);
}
struct selected_hosts {
int available_nhosts;
struct gfarm_host_sched_info *available_hosts;
int nhosts;
char **hosts;
int *ports;
};
static gfarm_error_t
select_nhosts(const char *path, int n, struct selected_hosts *selected)
{
char *domain = "";
gfarm_error_t e;
e = gfarm_schedule_hosts_domain_all(path, domain,
&selected->available_nhosts, &selected->available_hosts);
if (e == GFARM_ERR_NO_ERROR) {
GFARM_MALLOC_ARRAY(selected->hosts, n);
if (selected->hosts == NULL) {
e = GFARM_ERR_NO_MEMORY;
} else {
GFARM_MALLOC_ARRAY(selected->ports, n);
if (selected->ports == NULL) {
e = GFARM_ERR_NO_MEMORY;
} else {
selected->nhosts = n;
e = gfarm_schedule_hosts_acyclic_to_write(path,
selected->available_nhosts,
selected->available_hosts,
&selected->nhosts,
selected->hosts, selected->ports);
if (e == GFARM_ERR_NO_ERROR)
return (e);
free(selected->ports);
}
free(selected->hosts);
}
gfarm_host_sched_info_free(selected->available_nhosts,
selected->available_hosts);
}
return (e);
}
static void
free_selected_hosts(struct selected_hosts *selected)
{
free(selected->ports);
free(selected->hosts);
gfarm_host_sched_info_free(selected->available_nhosts,
selected->available_hosts);
}
static gfarm_error_t
replicate_file(const char *path, int ncopy, int ndsts, char **dsts, int *ports)
{
int i, n = 0;
gfarm_error_t e = GFARM_ERR_NO_ERROR;
for (i = 0; i < ndsts && n < ncopy; ++i) {
e = gfs_replicate_to(path, dsts[i], ports[i]);
if (e == GFARM_ERR_NO_ERROR ||
e == GFARM_ERR_OPERATION_NOW_IN_PROGRESS)
++n;
else if (e == GFARM_ERR_ALREADY_EXISTS)
/* skip */;
else {
gflog_error(GFARM_MSG_2000047,
"%s: replicataion to %s:%d fails: %s",
path, dsts[i], ports[i], gfarm_error_string(e));
break;
}
}
return (n == ncopy ? GFARM_ERR_NO_ERROR : e);
}
void
gfarm2fs_replicate(const char *path, struct fuse_file_info *fi)
{
struct gfarmized_path gfarmized;
int ncopy, cur_ncopy, pid;
int wait = 0, max_wait = 10;
struct selected_hosts selected;
gfarm_error_t e;
if (!replicate_enabled)
return;
e = gfarmize_path(path, &gfarmized);
if (e != GFARM_ERR_NO_ERROR) {
gflog_error(GFARM_MSG_2000088, "gfarmize_path(%s): %s",
path, gfarm_error_string(e));
return;
}
/* if necessary number of copies is less than 2, return */
ncopy = get_required_ncopy(gfarmized.path);
if (ncopy < 2) {
free_gfarmized_path(&gfarmized);
return;
}
/* if it has enough number of copies, return */
cur_ncopy = stat_ncopy(gfarmized.path);
if (ncopy <= cur_ncopy) {
free_gfarmized_path(&gfarmized);
return;
}
/* if enough number of replication processes are in process, wait */
while (replicate_max_concurrency > 0 &&
replicate_concurrency >= replicate_max_concurrency &&
wait++ < max_wait)
sleep(1);
if (wait >= max_wait) {
gflog_error(GFARM_MSG_2000045, "%s: too busy to replicate",
gfarmized.path);
free_gfarmized_path(&gfarmized);
return;
}
/* at most ncopy hosts required */
e = select_nhosts(gfarmized.path, ncopy, &selected);
if (e != GFARM_ERR_NO_ERROR) {
gflog_error(GFARM_MSG_2000048, "%s: failed to schedule hosts",
gfarmized.path);
free_gfarmized_path(&gfarmized);
return;
}
/* create 'ncopy - cur_ncopy' copies */
if (replicate_max_concurrency == 0) {
gflog_info(GFARM_MSG_2000049,
"replicate: %s ncopy %d (required %d)",
gfarmized.path, selected.nhosts, ncopy);
e = replicate_file(gfarmized.path, ncopy - cur_ncopy,
selected.nhosts, selected.hosts, selected.ports);
free_selected_hosts(&selected);
free_gfarmized_path(&gfarmized);
return;
}
switch ((pid = fork())) {
case 0:
e = gfarm_terminate();
if (e == GFARM_ERR_NO_ERROR)
e = gfarm_initialize(NULL, NULL);
if (e != GFARM_ERR_NO_ERROR) {
gflog_error(GFARM_MSG_2000050,
"%s: failed to initialize: %s",
gfarmized.path, gfarm_error_string(e));
_exit(1);
}
e = replicate_file(gfarmized.path, ncopy - cur_ncopy,
selected.nhosts, selected.hosts, selected.ports);
(void)gfarm_terminate();
_exit(e == GFARM_ERR_NO_ERROR ? 0 : 1);
case -1:
gflog_error_errno(GFARM_MSG_2000044, "fork");
break;
default:
gflog_info(GFARM_MSG_2000042,
"replicate [%d]: %s ncopy %d (required %d)",
pid, gfarmized.path, selected.nhosts, ncopy);
++replicate_concurrency;
break;
}
free_selected_hosts(&selected);
free_gfarmized_path(&gfarmized);
}
#endif /* HAVE_PRIVATE_SRCS */