-
Notifications
You must be signed in to change notification settings - Fork 2k
/
storage_sync.c
3427 lines (3022 loc) · 87.4 KB
/
storage_sync.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** * 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.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 "fastcommon/logger.h"
#include "fastcommon/sockopt.h"
#include "fastcommon/shared_func.h"
#include "fastcommon/pthread_func.h"
#include "fastcommon/sched_thread.h"
#include "fastcommon/ini_file_reader.h"
#include "fastcommon/fc_atomic.h"
#include "fdfs_define.h"
#include "fdfs_global.h"
#include "tracker_types.h"
#include "tracker_proto.h"
#include "storage_global.h"
#include "storage_func.h"
#include "storage_ip_changed_dealer.h"
#include "tracker_client_thread.h"
#include "storage_client.h"
#include "trunk_mem.h"
#include "storage_sync_func.h"
#include "storage_sync.h"
#define SYNC_BINLOG_FILE_MAX_SIZE 1024 * 1024 * 1024
#define SYNC_BINLOG_FILE_PREFIX "binlog"
#define SYNC_BINLOG_INDEX_FILENAME_OLD SYNC_BINLOG_FILE_PREFIX".index"
#define SYNC_BINLOG_INDEX_FILENAME SYNC_BINLOG_FILE_PREFIX"_index.dat"
#define SYNC_MARK_FILE_EXT ".mark"
#define SYNC_BINLOG_FILE_EXT_FMT ".%03d"
#define SYNC_DIR_NAME "sync"
#define MARK_ITEM_BINLOG_FILE_INDEX "binlog_index"
#define MARK_ITEM_BINLOG_FILE_OFFSET "binlog_offset"
#define MARK_ITEM_NEED_SYNC_OLD "need_sync_old"
#define MARK_ITEM_SYNC_OLD_DONE "sync_old_done"
#define MARK_ITEM_UNTIL_TIMESTAMP "until_timestamp"
#define MARK_ITEM_SCAN_ROW_COUNT "scan_row_count"
#define MARK_ITEM_SYNC_ROW_COUNT "sync_row_count"
#define SYNC_BINLOG_WRITE_BUFF_SIZE (16 * 1024)
#define BINLOG_INDEX_ITEM_CURRENT_WRITE "current_write"
#define BINLOG_INDEX_ITEM_CURRENT_COMPRESS "current_compress"
int g_binlog_fd = -1;
int g_binlog_index = 0;
static int64_t binlog_file_size = 0;
static int binlog_compress_index = 0;
volatile int g_storage_sync_thread_count = 0;
static pthread_mutex_t sync_thread_lock;
static char *binlog_write_cache_buff = NULL;
static int binlog_write_cache_len = 0;
static int binlog_write_version = 1;
/* save sync thread ids */
static pthread_t *sync_tids = NULL;
static struct fc_list_head reader_head;
static int storage_write_to_mark_file(StorageBinLogReader *pReader);
static int storage_binlog_reader_skip(StorageBinLogReader *pReader);
static int storage_binlog_fsync(const bool bNeedLock);
static int storage_binlog_preread(StorageBinLogReader *pReader);
/**
8 bytes: filename bytes
8 bytes: file size
4 bytes: source op timestamp
FDFS_GROUP_NAME_MAX_LEN bytes: group_name
filename bytes : filename
file size bytes: file content
**/
static int storage_sync_copy_file(ConnectionInfo *pStorageServer, \
StorageBinLogReader *pReader, const StorageBinLogRecord *pRecord, \
char proto_cmd)
{
TrackerHeader *pHeader;
char *p;
char *pBuff;
char full_filename[MAX_PATH_SIZE];
char out_buff[sizeof(TrackerHeader)+FDFS_GROUP_NAME_MAX_LEN+256];
char formatted_ip[FORMATTED_IP_SIZE];
char in_buff[1];
struct stat stat_buf;
FDFSTrunkFullInfo trunkInfo;
FDFSTrunkHeader trunkHeader;
int64_t file_offset;
int64_t in_bytes;
int64_t total_send_bytes;
int result;
bool need_sync_file;
if ((result=trunk_file_stat(pRecord->store_path_index,
pRecord->true_filename, pRecord->true_filename_len,
&stat_buf, &trunkInfo, &trunkHeader)) != 0)
{
if (result == ENOENT)
{
if(pRecord->op_type==STORAGE_OP_TYPE_SOURCE_CREATE_FILE)
{
logDebug("file: "__FILE__", line: %d, " \
"sync data file, logic file: %s " \
"not exists, maybe deleted later?", \
__LINE__, pRecord->filename);
}
return 0;
}
else
{
logError("file: "__FILE__", line: %d, " \
"call stat fail, logic file: %s, "\
"error no: %d, error info: %s", \
__LINE__, pRecord->filename, \
result, STRERROR(result));
return result;
}
}
need_sync_file = true;
if (pReader->last_file_exist && proto_cmd == \
STORAGE_PROTO_CMD_SYNC_CREATE_FILE)
{
FDFSFileInfo file_info;
result = storage_query_file_info_ex(NULL, \
pStorageServer, g_group_name, \
pRecord->filename, &file_info, true);
if (result == 0)
{
if (file_info.file_size == stat_buf.st_size)
{
if (FC_LOG_BY_LEVEL(LOG_DEBUG)) {
format_ip_address(pStorageServer->ip_addr, formatted_ip);
logDebug("file: "__FILE__", line: %d, "
"sync data file, logic file: %s "
"on dest server %s:%u already exists, "
"and same as mine, ignore it", __LINE__,
pRecord->filename, formatted_ip,
pStorageServer->port);
}
need_sync_file = false;
}
else
{
format_ip_address(pStorageServer->ip_addr, formatted_ip);
logWarning("file: "__FILE__", line: %d, "
"sync data file, logic file: %s "
"on dest server %s:%u already exists, "
"but file size: %"PRId64" not same as mine: %"PRId64
", need re-sync it", __LINE__, pRecord->filename,
formatted_ip, pStorageServer->port, file_info.file_size,
(int64_t)stat_buf.st_size);
proto_cmd = STORAGE_PROTO_CMD_SYNC_UPDATE_FILE;
}
}
else if (result != ENOENT)
{
return result;
}
}
if (IS_TRUNK_FILE_BY_ID(trunkInfo))
{
file_offset = TRUNK_FILE_START_OFFSET(trunkInfo);
trunk_get_full_filename((&trunkInfo), full_filename, \
sizeof(full_filename));
}
else
{
file_offset = 0;
sprintf(full_filename, "%s/data/%s", \
g_fdfs_store_paths.paths[pRecord->store_path_index].path, \
pRecord->true_filename);
}
total_send_bytes = 0;
//printf("sync create file: %s\n", pRecord->filename);
do
{
int64_t body_len;
pHeader = (TrackerHeader *)out_buff;
memset(pHeader, 0, sizeof(TrackerHeader));
body_len = 2 * FDFS_PROTO_PKG_LEN_SIZE + \
4 + FDFS_GROUP_NAME_MAX_LEN + \
pRecord->filename_len;
if (need_sync_file)
{
body_len += stat_buf.st_size;
}
long2buff(body_len, pHeader->pkg_len);
pHeader->cmd = proto_cmd;
pHeader->status = need_sync_file ? 0 : EEXIST;
p = out_buff + sizeof(TrackerHeader);
long2buff(pRecord->filename_len, p);
p += FDFS_PROTO_PKG_LEN_SIZE;
long2buff(stat_buf.st_size, p);
p += FDFS_PROTO_PKG_LEN_SIZE;
int2buff(pRecord->timestamp, p);
p += 4;
sprintf(p, "%s", g_group_name);
p += FDFS_GROUP_NAME_MAX_LEN;
memcpy(p, pRecord->filename, pRecord->filename_len);
p += pRecord->filename_len;
if((result=tcpsenddata_nb(pStorageServer->sock, out_buff,
p - out_buff, SF_G_NETWORK_TIMEOUT)) != 0)
{
format_ip_address(pStorageServer->ip_addr, formatted_ip);
logError("file: "__FILE__", line: %d, "
"sync data to storage server %s:%u fail, errno: %d, "
"error info: %s", __LINE__, formatted_ip,
pStorageServer->port, result, STRERROR(result));
break;
}
if (need_sync_file && (stat_buf.st_size > 0) && \
((result=tcpsendfile_ex(pStorageServer->sock, \
full_filename, file_offset, stat_buf.st_size, \
SF_G_NETWORK_TIMEOUT, &total_send_bytes)) != 0))
{
format_ip_address(pStorageServer->ip_addr, formatted_ip);
logError("file: "__FILE__", line: %d, "
"sync data to storage server %s:%u fail, errno: %d, "
"error info: %s", __LINE__, formatted_ip,
pStorageServer->port, result, STRERROR(result));
break;
}
pBuff = in_buff;
if ((result=fdfs_recv_response(pStorageServer, \
&pBuff, 0, &in_bytes)) != 0)
{
logError("file: "__FILE__", line: %d, "
"fdfs_recv_response fail, result: %d",
__LINE__, result);
break;
}
} while (0);
__sync_add_and_fetch(&g_storage_stat.total_sync_out_bytes,
total_send_bytes);
if (result == 0)
{
__sync_add_and_fetch(&g_storage_stat.success_sync_out_bytes,
total_send_bytes);
}
if (result == EEXIST)
{
if (need_sync_file && pRecord->op_type ==
STORAGE_OP_TYPE_SOURCE_CREATE_FILE)
{
format_ip_address(pStorageServer->ip_addr, formatted_ip);
logWarning("file: "__FILE__", line: %d, "
"storage server ip: %s:%u, data file: %s already exists, "
"maybe some mistake?", __LINE__, formatted_ip,
pStorageServer->port, pRecord->filename);
}
pReader->last_file_exist = true;
return 0;
}
else if (result == 0)
{
pReader->last_file_exist = false;
return 0;
}
else
{
return result;
}
}
/**
8 bytes: filename bytes
8 bytes: start offset
8 bytes: append length
4 bytes: source op timestamp
FDFS_GROUP_NAME_MAX_LEN bytes: group_name
filename bytes : filename
file size bytes: file content
**/
static int storage_sync_modify_file(ConnectionInfo *pStorageServer, \
StorageBinLogReader *pReader, StorageBinLogRecord *pRecord, \
const char cmd)
{
#define SYNC_MODIFY_FIELD_COUNT 3
TrackerHeader *pHeader;
char *p;
char *pBuff;
char *fields[SYNC_MODIFY_FIELD_COUNT];
char full_filename[MAX_PATH_SIZE];
char out_buff[sizeof(TrackerHeader)+FDFS_GROUP_NAME_MAX_LEN+256];
char formatted_ip[FORMATTED_IP_SIZE];
char in_buff[1];
struct stat stat_buf;
int64_t in_bytes;
int64_t total_send_bytes;
int64_t start_offset;
int64_t modify_length;
int result;
int count;
if ((count=splitEx(pRecord->filename, ' ', fields, SYNC_MODIFY_FIELD_COUNT))
!= SYNC_MODIFY_FIELD_COUNT)
{
logError("file: "__FILE__", line: %d, " \
"the format of binlog not correct, filename: %s", \
__LINE__, pRecord->filename);
return EINVAL;
}
start_offset = strtoll((fields[1]), NULL, 10);
modify_length = strtoll((fields[2]), NULL, 10);
pRecord->filename_len = strlen(pRecord->filename);
pRecord->true_filename_len = pRecord->filename_len;
if ((result=storage_split_filename_ex(pRecord->filename, \
&pRecord->true_filename_len, pRecord->true_filename, \
&pRecord->store_path_index)) != 0)
{
return result;
}
snprintf(full_filename, sizeof(full_filename), \
"%s/data/%s", g_fdfs_store_paths.paths[pRecord->store_path_index].path, \
pRecord->true_filename);
if (lstat(full_filename, &stat_buf) != 0)
{
if (errno == ENOENT)
{
logDebug("file: "__FILE__", line: %d, " \
"sync appender file, file: %s not exists, "\
"maybe deleted later?", \
__LINE__, full_filename);
return 0;
}
else
{
result = errno != 0 ? errno : EPERM;
logError("file: "__FILE__", line: %d, " \
"call stat fail, appender file: %s, "\
"error no: %d, error info: %s", \
__LINE__, full_filename, \
result, STRERROR(result));
return result;
}
}
if (stat_buf.st_size < start_offset + modify_length)
{
logWarning("file: "__FILE__", line: %d, " \
"appender file: %s 'size: %"PRId64 \
" < %"PRId64", maybe some mistakes " \
"happened, skip sync this appender file", __LINE__, \
full_filename, stat_buf.st_size, \
start_offset + modify_length);
return 0;
}
total_send_bytes = 0;
//printf("sync create file: %s\n", pRecord->filename);
do
{
int64_t body_len;
pHeader = (TrackerHeader *)out_buff;
memset(pHeader, 0, sizeof(TrackerHeader));
body_len = 3 * FDFS_PROTO_PKG_LEN_SIZE + \
4 + FDFS_GROUP_NAME_MAX_LEN + \
pRecord->filename_len + modify_length;
long2buff(body_len, pHeader->pkg_len);
pHeader->cmd = cmd;
pHeader->status = 0;
p = out_buff + sizeof(TrackerHeader);
long2buff(pRecord->filename_len, p);
p += FDFS_PROTO_PKG_LEN_SIZE;
long2buff(start_offset, p);
p += FDFS_PROTO_PKG_LEN_SIZE;
long2buff(modify_length, p);
p += FDFS_PROTO_PKG_LEN_SIZE;
int2buff(pRecord->timestamp, p);
p += 4;
sprintf(p, "%s", g_group_name);
p += FDFS_GROUP_NAME_MAX_LEN;
memcpy(p, pRecord->filename, pRecord->filename_len);
p += pRecord->filename_len;
if((result=tcpsenddata_nb(pStorageServer->sock, out_buff, \
p - out_buff, SF_G_NETWORK_TIMEOUT)) != 0)
{
format_ip_address(pStorageServer->ip_addr, formatted_ip);
logError("file: "__FILE__", line: %d, "
"sync data to storage server %s:%u fail, errno: %d, "
"error info: %s", __LINE__, formatted_ip,
pStorageServer->port, result, STRERROR(result));
break;
}
if ((result=tcpsendfile_ex(pStorageServer->sock, \
full_filename, start_offset, modify_length, \
SF_G_NETWORK_TIMEOUT, &total_send_bytes)) != 0)
{
format_ip_address(pStorageServer->ip_addr, formatted_ip);
logError("file: "__FILE__", line: %d, "
"sync data to storage server %s:%u fail, errno: %d, "
"error info: %s", __LINE__, formatted_ip,
pStorageServer->port, result, STRERROR(result));
break;
}
pBuff = in_buff;
if ((result=fdfs_recv_response(pStorageServer, \
&pBuff, 0, &in_bytes)) != 0)
{
logError("file: "__FILE__", line: %d, "
"fdfs_recv_response fail, result: %d",
__LINE__, result);
break;
}
} while (0);
__sync_add_and_fetch(&g_storage_stat.total_sync_out_bytes,
total_send_bytes);
if (result == 0)
{
__sync_add_and_fetch(&g_storage_stat.success_sync_out_bytes,
total_send_bytes);
}
return result == EEXIST ? 0 : result;
}
/**
8 bytes: filename bytes
8 bytes: old file size
8 bytes: new file size
4 bytes: source op timestamp
FDFS_GROUP_NAME_MAX_LEN bytes: group_name
filename bytes : filename
**/
static int storage_sync_truncate_file(ConnectionInfo *pStorageServer, \
StorageBinLogReader *pReader, StorageBinLogRecord *pRecord)
{
#define SYNC_TRUNCATE_FIELD_COUNT 3
TrackerHeader *pHeader;
char *p;
char *pBuff;
char *fields[SYNC_TRUNCATE_FIELD_COUNT];
char full_filename[MAX_PATH_SIZE];
char out_buff[sizeof(TrackerHeader)+FDFS_GROUP_NAME_MAX_LEN+256];
char formatted_ip[FORMATTED_IP_SIZE];
char in_buff[1];
struct stat stat_buf;
int64_t in_bytes;
int64_t old_file_size;
int64_t new_file_size;
int result;
int count;
if ((count=splitEx(pRecord->filename, ' ', fields,
SYNC_TRUNCATE_FIELD_COUNT)) != SYNC_TRUNCATE_FIELD_COUNT)
{
logError("file: "__FILE__", line: %d, " \
"the format of binlog not correct, filename: %s", \
__LINE__, pRecord->filename);
return EINVAL;
}
old_file_size = strtoll((fields[1]), NULL, 10);
new_file_size = strtoll((fields[2]), NULL, 10);
pRecord->filename_len = strlen(pRecord->filename);
pRecord->true_filename_len = pRecord->filename_len;
if ((result=storage_split_filename_ex(pRecord->filename, \
&pRecord->true_filename_len, pRecord->true_filename, \
&pRecord->store_path_index)) != 0)
{
return result;
}
snprintf(full_filename, sizeof(full_filename), \
"%s/data/%s", g_fdfs_store_paths.paths[pRecord->store_path_index].path, \
pRecord->true_filename);
if (lstat(full_filename, &stat_buf) != 0)
{
if (errno == ENOENT)
{
logDebug("file: "__FILE__", line: %d, " \
"sync appender file, file: %s not exists, "\
"maybe deleted later?", \
__LINE__, full_filename);
return 0;
}
else
{
result = errno != 0 ? errno : EPERM;
logError("file: "__FILE__", line: %d, " \
"call stat fail, appender file: %s, "\
"error no: %d, error info: %s", \
__LINE__, full_filename, \
result, STRERROR(result));
return result;
}
}
if (stat_buf.st_size != new_file_size)
{
logDebug("file: "__FILE__", line: %d, " \
"appender file: %s 'size: %"PRId64 \
" != %"PRId64", maybe append/modify later",\
__LINE__, full_filename, stat_buf.st_size,
new_file_size);
}
do
{
int64_t body_len;
pHeader = (TrackerHeader *)out_buff;
memset(pHeader, 0, sizeof(TrackerHeader));
body_len = 3 * FDFS_PROTO_PKG_LEN_SIZE + \
4 + FDFS_GROUP_NAME_MAX_LEN + \
pRecord->filename_len;
long2buff(body_len, pHeader->pkg_len);
pHeader->cmd = STORAGE_PROTO_CMD_SYNC_TRUNCATE_FILE;
pHeader->status = 0;
p = out_buff + sizeof(TrackerHeader);
long2buff(pRecord->filename_len, p);
p += FDFS_PROTO_PKG_LEN_SIZE;
long2buff(old_file_size, p);
p += FDFS_PROTO_PKG_LEN_SIZE;
long2buff(new_file_size, p);
p += FDFS_PROTO_PKG_LEN_SIZE;
int2buff(pRecord->timestamp, p);
p += 4;
sprintf(p, "%s", g_group_name);
p += FDFS_GROUP_NAME_MAX_LEN;
memcpy(p, pRecord->filename, pRecord->filename_len);
p += pRecord->filename_len;
if((result=tcpsenddata_nb(pStorageServer->sock, out_buff,
p - out_buff, SF_G_NETWORK_TIMEOUT)) != 0)
{
format_ip_address(pStorageServer->ip_addr, formatted_ip);
logError("file: "__FILE__", line: %d, "
"sync data to storage server %s:%u fail, errno: %d, "
"error info: %s", __LINE__, formatted_ip,
pStorageServer->port, result, STRERROR(result));
break;
}
pBuff = in_buff;
if ((result=fdfs_recv_response(pStorageServer, \
&pBuff, 0, &in_bytes)) != 0)
{
logError("file: "__FILE__", line: %d, "
"fdfs_recv_response fail, result: %d",
__LINE__, result);
break;
}
} while (0);
return result == EEXIST ? 0 : result;
}
/**
send pkg format:
4 bytes: source delete timestamp
FDFS_GROUP_NAME_MAX_LEN bytes: group_name
remain bytes: filename
**/
static int storage_sync_delete_file(ConnectionInfo *pStorageServer, \
const StorageBinLogRecord *pRecord)
{
TrackerHeader *pHeader;
char out_buff[sizeof(TrackerHeader)+FDFS_GROUP_NAME_MAX_LEN+256];
char formatted_ip[FORMATTED_IP_SIZE];
struct stat stat_buf;
FDFSTrunkFullInfo trunkInfo;
FDFSTrunkHeader trunkHeader;
char in_buff[1];
char *pBuff;
int64_t in_bytes;
int result;
if ((result=trunk_file_stat(pRecord->store_path_index, \
pRecord->true_filename, pRecord->true_filename_len, \
&stat_buf, &trunkInfo, &trunkHeader)) == 0)
{
if (pRecord->op_type == STORAGE_OP_TYPE_SOURCE_DELETE_FILE)
{
logWarning("file: "__FILE__", line: %d, " \
"sync data file, logic file: %s exists, " \
"maybe created later?", \
__LINE__, pRecord->filename);
}
return 0;
}
memset(out_buff, 0, sizeof(out_buff));
int2buff(pRecord->timestamp, out_buff + sizeof(TrackerHeader));
memcpy(out_buff + sizeof(TrackerHeader) + 4, g_group_name, \
sizeof(g_group_name));
memcpy(out_buff + sizeof(TrackerHeader) + 4 + FDFS_GROUP_NAME_MAX_LEN, \
pRecord->filename, pRecord->filename_len);
pHeader = (TrackerHeader *)out_buff;
long2buff(4 + FDFS_GROUP_NAME_MAX_LEN + pRecord->filename_len, \
pHeader->pkg_len);
pHeader->cmd = STORAGE_PROTO_CMD_SYNC_DELETE_FILE;
if ((result=tcpsenddata_nb(pStorageServer->sock, out_buff, \
sizeof(TrackerHeader) + 4 + FDFS_GROUP_NAME_MAX_LEN + \
pRecord->filename_len, SF_G_NETWORK_TIMEOUT)) != 0)
{
format_ip_address(pStorageServer->ip_addr, formatted_ip);
logError("FILE: "__FILE__", line: %d, "
"send data to storage server %s:%u fail, errno: %d, "
"error info: %s", __LINE__, formatted_ip,
pStorageServer->port, result, STRERROR(result));
return result;
}
pBuff = in_buff;
result = fdfs_recv_response(pStorageServer, &pBuff, 0, &in_bytes);
if (result != 0)
{
if (result == ENOENT)
{
result = 0;
}
else
{
logError("file: "__FILE__", line: %d, "
"fdfs_recv_response fail, result: %d",
__LINE__, result);
}
}
return result;
}
/**
FDFS_STORAGE_ID_MAX_SIZE bytes: my server id
**/
static int storage_report_my_server_id(ConnectionInfo *pStorageServer)
{
int result;
TrackerHeader *pHeader;
char out_buff[sizeof(TrackerHeader) + FDFS_STORAGE_ID_MAX_SIZE];
char formatted_ip[FORMATTED_IP_SIZE];
char in_buff[1];
char *pBuff;
int64_t in_bytes;
pHeader = (TrackerHeader *)out_buff;
memset(out_buff, 0, sizeof(out_buff));
long2buff(FDFS_STORAGE_ID_MAX_SIZE, pHeader->pkg_len);
pHeader->cmd = STORAGE_PROTO_CMD_REPORT_SERVER_ID;
strcpy(out_buff + sizeof(TrackerHeader), g_my_server_id_str);
if ((result=tcpsenddata_nb(pStorageServer->sock, out_buff, \
sizeof(TrackerHeader) + FDFS_STORAGE_ID_MAX_SIZE, \
SF_G_NETWORK_TIMEOUT)) != 0)
{
format_ip_address(pStorageServer->ip_addr, formatted_ip);
logError("FILE: "__FILE__", line: %d, "
"send data to storage server %s:%u fail, errno: %d, "
"error info: %s", __LINE__, formatted_ip,
pStorageServer->port, result, STRERROR(result));
return result;
}
pBuff = in_buff;
result = fdfs_recv_response(pStorageServer, &pBuff, 0, &in_bytes);
if (result != 0)
{
logError("file: "__FILE__", line: %d, "
"fdfs_recv_response fail, result: %d",
__LINE__, result);
}
return result;
}
/**
8 bytes: dest(link) filename length
8 bytes: source filename length
4 bytes: source op timestamp
FDFS_GROUP_NAME_MAX_LEN bytes: group_name
dest filename length: dest filename
source filename length: source filename
**/
static int storage_sync_link_file(ConnectionInfo *pStorageServer, \
StorageBinLogRecord *pRecord)
{
TrackerHeader *pHeader;
int result;
char out_buff[sizeof(TrackerHeader) + 2 * FDFS_PROTO_PKG_LEN_SIZE + \
4 + FDFS_GROUP_NAME_MAX_LEN + 256];
char formatted_ip[FORMATTED_IP_SIZE];
char in_buff[1];
FDFSTrunkFullInfo trunkInfo;
FDFSTrunkHeader trunkHeader;
int out_body_len;
int64_t in_bytes;
char *pBuff;
struct stat stat_buf;
int fd;
fd = -1;
if ((result=trunk_file_lstat_ex(pRecord->store_path_index, \
pRecord->true_filename, pRecord->true_filename_len, \
&stat_buf, &trunkInfo, &trunkHeader, &fd)) != 0)
{
if (result == ENOENT)
{
if (pRecord->op_type == STORAGE_OP_TYPE_SOURCE_CREATE_LINK)
{
logDebug("file: "__FILE__", line: %d, " \
"sync data file, logic file: %s does not " \
"exist, maybe delete later?", \
__LINE__, pRecord->filename);
}
}
else
{
logError("file: "__FILE__", line: %d, " \
"call stat fail, logic file: %s, "\
"error no: %d, error info: %s", \
__LINE__, pRecord->filename, \
result, STRERROR(result));
}
return 0;
}
if (!S_ISLNK(stat_buf.st_mode))
{
if (fd >= 0)
{
close(fd);
}
if (pRecord->op_type == STORAGE_OP_TYPE_SOURCE_CREATE_LINK)
{
logWarning("file: "__FILE__", line: %d, " \
"sync data file, logic file %s is not " \
"a symbol link, maybe create later?", \
__LINE__, pRecord->filename);
}
return 0;
}
if (pRecord->src_filename_len > 0)
{
if (fd >= 0)
{
close(fd);
}
}
else if (IS_TRUNK_FILE_BY_ID(trunkInfo))
{
result = trunk_file_get_content(&trunkInfo, \
stat_buf.st_size, &fd, pRecord->src_filename, \
sizeof(pRecord->src_filename));
close(fd);
if (result != 0)
{
logWarning("file: "__FILE__", line: %d, " \
"logic file: %s, get file content fail, " \
"errno: %d, error info: %s", \
__LINE__, pRecord->filename, \
result, STRERROR(result));
return 0;
}
pRecord->src_filename_len = stat_buf.st_size;
*(pRecord->src_filename + pRecord->src_filename_len) = '\0';
}
else
{
char full_filename[MAX_PATH_SIZE];
char src_full_filename[MAX_PATH_SIZE];
char *p;
char *pSrcFilename;
int src_path_index;
int src_filename_len;
snprintf(full_filename, sizeof(full_filename), \
"%s/data/%s", g_fdfs_store_paths.paths[pRecord->store_path_index].path, \
pRecord->true_filename);
src_filename_len = readlink(full_filename, src_full_filename, \
sizeof(src_full_filename) - 1);
if (src_filename_len <= 0)
{
logWarning("file: "__FILE__", line: %d, " \
"data file: %s, readlink fail, "\
"errno: %d, error info: %s", \
__LINE__, src_full_filename, errno, STRERROR(errno));
return 0;
}
*(src_full_filename + src_filename_len) = '\0';
pSrcFilename = strstr(src_full_filename, "/data/");
if (pSrcFilename == NULL)
{
logError("file: "__FILE__", line: %d, " \
"source data file: %s is invalid", \
__LINE__, src_full_filename);
return EINVAL;
}
pSrcFilename += 6;
p = strstr(pSrcFilename, "/data/");
while (p != NULL)
{
pSrcFilename = p + 6;
p = strstr(pSrcFilename, "/data/");
}
if (g_fdfs_store_paths.count == 1)
{
src_path_index = 0;
}
else
{
*(pSrcFilename - 6) = '\0';
for (src_path_index=0; src_path_index<g_fdfs_store_paths.count; \
src_path_index++)
{
if (strcmp(src_full_filename, \
g_fdfs_store_paths.paths[src_path_index].path) == 0)
{
break;
}
}
if (src_path_index == g_fdfs_store_paths.count)
{
logError("file: "__FILE__", line: %d, " \
"source data file: %s is invalid", \
__LINE__, src_full_filename);
return EINVAL;
}
}
pRecord->src_filename_len = src_filename_len - (pSrcFilename - \
src_full_filename) + 4;
if (pRecord->src_filename_len >= sizeof(pRecord->src_filename))
{
logError("file: "__FILE__", line: %d, " \
"source data file: %s is invalid", \
__LINE__, src_full_filename);
return EINVAL;
}
sprintf(pRecord->src_filename, "%c"FDFS_STORAGE_DATA_DIR_FORMAT"/%s", \
FDFS_STORAGE_STORE_PATH_PREFIX_CHAR, \
src_path_index, pSrcFilename);
}
pHeader = (TrackerHeader *)out_buff;
memset(out_buff, 0, sizeof(out_buff));
long2buff(pRecord->filename_len, out_buff + sizeof(TrackerHeader));
long2buff(pRecord->src_filename_len, out_buff + sizeof(TrackerHeader) + \
FDFS_PROTO_PKG_LEN_SIZE);
int2buff(pRecord->timestamp, out_buff + sizeof(TrackerHeader) + \
2 * FDFS_PROTO_PKG_LEN_SIZE);
sprintf(out_buff + sizeof(TrackerHeader) + 2 * FDFS_PROTO_PKG_LEN_SIZE\
+ 4, "%s", g_group_name);
memcpy(out_buff + sizeof(TrackerHeader) + 2 * FDFS_PROTO_PKG_LEN_SIZE \
+ 4 + FDFS_GROUP_NAME_MAX_LEN, \
pRecord->filename, pRecord->filename_len);
memcpy(out_buff + sizeof(TrackerHeader) + 2 * FDFS_PROTO_PKG_LEN_SIZE \
+ 4 + FDFS_GROUP_NAME_MAX_LEN + pRecord->filename_len, \
pRecord->src_filename, pRecord->src_filename_len);
out_body_len = 2 * FDFS_PROTO_PKG_LEN_SIZE + 4 + \
FDFS_GROUP_NAME_MAX_LEN + pRecord->filename_len + \
pRecord->src_filename_len;
long2buff(out_body_len, pHeader->pkg_len);
pHeader->cmd = STORAGE_PROTO_CMD_SYNC_CREATE_LINK;
if ((result=tcpsenddata_nb(pStorageServer->sock, out_buff,
sizeof(TrackerHeader) + out_body_len,
SF_G_NETWORK_TIMEOUT)) != 0)
{
format_ip_address(pStorageServer->ip_addr, formatted_ip);
logError("FILE: "__FILE__", line: %d, "
"send data to storage server %s:%u fail, errno: %d, "
"error info: %s", __LINE__, formatted_ip,
pStorageServer->port, result, STRERROR(result));
return result;
}
pBuff = in_buff;
result = fdfs_recv_response(pStorageServer, &pBuff, 0, &in_bytes);
if (result != 0)
{
if (result == ENOENT)
{
result = 0;
}
else
{
logError("file: "__FILE__", line: %d, "
"fdfs_recv_response fail, result: %d",
__LINE__, result);
}
}
return result;
}
/**
8 bytes: dest filename length
8 bytes: source filename length
4 bytes: source op timestamp
FDFS_GROUP_NAME_MAX_LEN bytes: group_name
dest filename length: dest filename
source filename length: source filename
**/
static int storage_sync_rename_file(ConnectionInfo *pStorageServer,
StorageBinLogReader *pReader, StorageBinLogRecord *pRecord)
{
TrackerHeader *pHeader;
int result;
char out_buff[sizeof(TrackerHeader) + 2 * FDFS_PROTO_PKG_LEN_SIZE +
4 + FDFS_GROUP_NAME_MAX_LEN + 256];
char formatted_ip[FORMATTED_IP_SIZE];
char in_buff[1];
int out_body_len;
int64_t in_bytes;
char *pBuff;
char full_filename[MAX_PATH_SIZE];
struct stat stat_buf;
if (pRecord->op_type == STORAGE_OP_TYPE_REPLICA_RENAME_FILE)
{
return storage_sync_copy_file(pStorageServer,
pReader, pRecord,
STORAGE_PROTO_CMD_SYNC_CREATE_FILE);
}
snprintf(full_filename, sizeof(full_filename), "%s/data/%s",
g_fdfs_store_paths.paths[pRecord->store_path_index].path,
pRecord->true_filename);
if (lstat(full_filename, &stat_buf) != 0)
{
if (errno == ENOENT)