bugfixed: sf_file_writer_get_last_lines deal correctly when cross files

replication_quorum
YuQing 2022-07-21 18:34:11 +08:00
parent 5da65a172c
commit 94ee91d37d
1 changed files with 71 additions and 34 deletions

View File

@ -474,52 +474,89 @@ int sf_file_writer_get_last_lines(const char *data_path,
char *buff, const int buff_size, int *count, int *length) char *buff, const int buff_size, int *count, int *length)
{ {
int result; int result;
int remain_count; int target_count;
int current_count; int count1;
int current_index;
int i;
char filename[PATH_MAX]; char filename[PATH_MAX];
string_t lines; string_t lines;
current_index = current_write_index; target_count = *count;
*length = 0; sf_file_writer_get_filename(data_path, subdir_name,
remain_count = *count; current_write_index, filename, sizeof(filename));
for (i=0; i<2; i++) { if (access(filename, F_OK) == 0) {
current_count = remain_count; if ((result=fc_get_last_lines(filename, buff, buff_size,
sf_file_writer_get_filename(data_path, subdir_name, &lines, count)) != 0)
current_index, filename, sizeof(filename)); {
if (access(filename, F_OK) == 0) {
result = fc_get_last_lines(filename, buff + *length,
buff_size - *length, &lines, &current_count);
} else {
result = errno != 0 ? errno : EPERM;
if (result != ENOENT) { if (result != ENOENT) {
logError("file: "__FILE__", line: %d, "
"stat file %s fail, errno: %d, error info: %s",
__LINE__, filename, result, STRERROR(result));
*count = 0;
return result; return result;
} }
} }
if (result == 0) { if (*count >= target_count || current_write_index == 0) {
memmove(buff + *length, lines.str, lines.len); memmove(buff, lines.str, lines.len);
*length += lines.len; *length = lines.len;
remain_count -= current_count; return 0;
if (remain_count == 0) { }
break; } else {
} result = errno != 0 ? errno : EPERM;
} else if (result != ENOENT) { if (result == ENOENT) {
*count = 0; *count = 0;
*length = 0;
return 0;
} else {
logError("file: "__FILE__", line: %d, "
"stat file %s fail, errno: %d, error info: %s",
__LINE__, filename, result, STRERROR(result));
*count = 0;
*length = 0;
return result; return result;
} }
if (current_index == 0) {
break;
}
--current_index; //try previous binlog file
} }
*count -= remain_count; sf_file_writer_get_filename(data_path, subdir_name,
current_write_index - 1, filename, sizeof(filename));
if (access(filename, F_OK) != 0) {
result = errno != 0 ? errno : EPERM;
if (result == ENOENT) {
memmove(buff, lines.str, lines.len);
*length = lines.len;
return 0;
} else {
logError("file: "__FILE__", line: %d, "
"stat file %s fail, errno: %d, error info: %s",
__LINE__, filename, result, STRERROR(result));
*count = 0;
*length = 0;
return result;
}
}
count1 = target_count - *count;
if ((result=fc_get_last_lines(filename, buff,
buff_size, &lines, &count1)) != 0)
{
*count = 0;
*length = 0;
return result;
}
memmove(buff, lines.str, lines.len);
*length = lines.len;
if (*count == 0) {
*count = count1;
} else {
sf_file_writer_get_filename(data_path, subdir_name,
current_write_index, filename, sizeof(filename));
if ((result=fc_get_first_lines(filename, buff + (*length),
buff_size - (*length), &lines, count)) != 0)
{
*count = 0;
*length = 0;
return result;
}
*count += count1;
*length += lines.len;
}
return 0; return 0;
} }