From 13ba0963a357627f4bcaccf99fd0a611338a9830 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Fri, 20 Dec 2019 09:07:09 +0800 Subject: [PATCH] trunk_binlog_truncate delete trunk data file --- HISTORY | 2 +- storage/storage_service.c | 4 +- storage/trunk_mgr/trunk_mem.c | 28 +--------- storage/trunk_mgr/trunk_mem.h | 2 - storage/trunk_mgr/trunk_sync.c | 93 ++++++++++++++++++---------------- storage/trunk_mgr/trunk_sync.h | 4 +- 6 files changed, 56 insertions(+), 77 deletions(-) diff --git a/HISTORY b/HISTORY index b67e019..91e2317 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 6.05 2019-12-19 +Version 6.05 2019-12-20 * fdfs_trackerd and fdfs_storaged print the server version in usage. you can execute fdfs_trackerd or fdfs_storaged without parameters to show the server version diff --git a/storage/storage_service.c b/storage/storage_service.c index 6f2f45a..2613266 100644 --- a/storage/storage_service.c +++ b/storage/storage_service.c @@ -4059,12 +4059,12 @@ static int storage_server_trunk_delete_binlog_marks(struct fast_task_info *pTask } result = storage_delete_trunk_data_file(); - if (!(result == 0 || result == ENOENT)) + if (result != 0) { return result; } - return trunk_unlink_all_mark_files(false); + return trunk_unlink_all_mark_files(); } /** diff --git a/storage/trunk_mgr/trunk_mem.c b/storage/trunk_mgr/trunk_mem.c index 4443090..1e8de2e 100644 --- a/storage/trunk_mgr/trunk_mem.c +++ b/storage/trunk_mgr/trunk_mem.c @@ -591,7 +591,7 @@ int storage_trunk_binlog_compress_check_recovery() } case STORAGE_TRUNK_COMPRESS_STAGE_COMPRESS_SUCCESS: /* unlink all mark files because the binlog file be compressed */ - result = trunk_unlink_all_mark_files(true); + result = trunk_unlink_all_mark_files(); if (result == 0) { g_trunk_binlog_compress_stage = @@ -693,7 +693,7 @@ static int storage_trunk_compress() last_write_version = current_write_version; /* unlink all mark files because the binlog file be compressed */ - result = trunk_unlink_all_mark_files(true); + result = trunk_unlink_all_mark_files(); } while (0); __sync_sub_and_fetch(&trunk_binlog_compress_in_progress, 1); @@ -1164,30 +1164,6 @@ static int storage_trunk_restore(const int64_t restore_offset) return result; } -int storage_delete_trunk_data_file() -{ - char trunk_data_filename[MAX_PATH_SIZE]; - int result; - - storage_trunk_get_data_filename(trunk_data_filename); - if (unlink(trunk_data_filename) == 0) - { - return 0; - } - - result = errno != 0 ? errno : ENOENT; - if (result != ENOENT) - { - logError("file: "__FILE__", line: %d, " - "unlink trunk data file: %s fail, " - "errno: %d, error info: %s", - __LINE__, trunk_data_filename, - result, STRERROR(result)); - } - - return result; -} - static int storage_trunk_load() { #define TRUNK_DATA_NEW_FIELD_COUNT 8 // >= v5.01 diff --git a/storage/trunk_mgr/trunk_mem.h b/storage/trunk_mgr/trunk_mem.h index f8b2d75..293a603 100644 --- a/storage/trunk_mgr/trunk_mem.h +++ b/storage/trunk_mgr/trunk_mem.h @@ -107,8 +107,6 @@ int trunk_binlog_compress_func(void *args); int storage_trunk_binlog_compress_check_recovery(); -int storage_delete_trunk_data_file(); - char *storage_trunk_get_data_filename(char *full_filename); #define storage_check_reserved_space(pGroup) \ diff --git a/storage/trunk_mgr/trunk_sync.c b/storage/trunk_mgr/trunk_sync.c index 54ac1fb..a82a111 100644 --- a/storage/trunk_mgr/trunk_sync.c +++ b/storage/trunk_mgr/trunk_sync.c @@ -387,7 +387,7 @@ int trunk_sync_notify_thread_reset_offset() } } - logError("file: "__FILE__", line: %d, " + logWarning("file: "__FILE__", line: %d, " "%d trunk sync threads reset binlog offset timeout.", __LINE__, count); return EBUSY; @@ -516,13 +516,12 @@ static int trunk_binlog_delete_overflow_backups() strcpy(file_array.files[file_array.count]. filename, ent->d_name); file_array.count++; - break; } } closedir(dir); - over_count = file_array.count - g_trunk_binlog_max_backups; + over_count = (file_array.count - g_trunk_binlog_max_backups) + 1; if (result != 0 || over_count <= 0) { if (file_array.files != NULL) @@ -539,7 +538,6 @@ static int trunk_binlog_delete_overflow_backups() { sprintf(full_filename, "%s/%s", file_path, file_array.files[i].filename); - logInfo("unlink old file: %s", full_filename); unlink(full_filename); } @@ -592,6 +590,34 @@ static int trunk_binlog_backup_and_truncate() return (result == 0) ? open_res : result; } +int storage_delete_trunk_data_file() +{ + char trunk_data_filename[MAX_PATH_SIZE]; + int result; + + storage_trunk_get_data_filename(trunk_data_filename); + if (unlink(trunk_data_filename) == 0) + { + return 0; + } + + result = errno != 0 ? errno : ENOENT; + if (result == ENOENT) + { + result = 0; + } + else + { + logError("file: "__FILE__", line: %d, " + "unlink trunk data file: %s fail, " + "errno: %d, error info: %s", + __LINE__, trunk_data_filename, + result, STRERROR(result)); + } + + return result; +} + int trunk_binlog_truncate() { int result; @@ -626,6 +652,11 @@ int trunk_binlog_truncate() } while (0); pthread_mutex_unlock(&trunk_sync_thread_lock); + if (result == 0) + { + result = storage_delete_trunk_data_file(); + } + return result; } @@ -2453,11 +2484,10 @@ void trunk_waiting_sync_thread_exit() } } -int trunk_unlink_all_mark_files(const bool force_delete) +int trunk_unlink_all_mark_files() { char file_path[MAX_PATH_SIZE]; - char old_filename[MAX_PATH_SIZE]; - char new_filename[MAX_PATH_SIZE]; + char full_filename[MAX_PATH_SIZE]; DIR *dir; struct dirent *ent; int result; @@ -2496,49 +2526,22 @@ int trunk_unlink_all_mark_files(const bool force_delete) continue; } - snprintf(old_filename, sizeof(old_filename), "%s/%s", + snprintf(full_filename, sizeof(full_filename), "%s/%s", file_path, ent->d_name); - if (force_delete) + if (unlink(full_filename) != 0) { - if (unlink(old_filename) != 0) + result = errno != 0 ? errno : EPERM; + if (result == ENOENT) { - result = errno != 0 ? errno : EPERM; - if (result == ENOENT) - { - result = 0; - } - else - { - logError("file: "__FILE__", line: %d, " - "unlink %s fail, errno: %d, error info: %s", - __LINE__, old_filename, - result, STRERROR(result)); - break; - } + result = 0; } - } - else - { - snprintf(new_filename, sizeof(new_filename), - "%s.%04d%02d%02d%02d%02d%02d", old_filename, - tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, - tm.tm_hour, tm.tm_min, tm.tm_sec); - if (rename(old_filename, new_filename) != 0) + else { - result = errno != 0 ? errno : EPERM; - if (result == ENOENT) - { - result = 0; - } - else - { - logError("file: "__FILE__", line: %d, " - "rename file %s to %s fail, " - "errno: %d, error info: %s", - __LINE__, old_filename, new_filename, - result, STRERROR(result)); - break; - } + logError("file: "__FILE__", line: %d, " + "unlink %s fail, errno: %d, error info: %s", + __LINE__, full_filename, + result, STRERROR(result)); + break; } } } diff --git a/storage/trunk_mgr/trunk_sync.h b/storage/trunk_mgr/trunk_sync.h index c853e28..4292c8f 100644 --- a/storage/trunk_mgr/trunk_sync.h +++ b/storage/trunk_mgr/trunk_sync.h @@ -65,7 +65,7 @@ void trunk_waiting_sync_thread_exit(); char *get_trunk_binlog_filename(char *full_filename); char *trunk_mark_filename_by_reader(const void *pArg, char *full_filename); -int trunk_unlink_all_mark_files(const bool force_delete); +int trunk_unlink_all_mark_files(); int trunk_unlink_mark_file(const char *storage_id); int trunk_rename_mark_file(const char *old_ip_addr, const int old_port, \ const char *new_ip_addr, const int new_port); @@ -88,6 +88,8 @@ int trunk_binlog_compress_rollback(); int trunk_sync_notify_thread_reset_offset(); int trunk_binlog_get_write_version(); +int storage_delete_trunk_data_file(); + char *get_trunk_binlog_tmp_filename_ex(const char *binlog_filename, char *tmp_filename);