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;
remain_count = *count;
for (i=0; i<2; i++) {
current_count = remain_count;
sf_file_writer_get_filename(data_path, subdir_name, sf_file_writer_get_filename(data_path, subdir_name,
current_index, filename, sizeof(filename)); current_write_index, filename, sizeof(filename));
if (access(filename, F_OK) == 0) { if (access(filename, F_OK) == 0) {
result = fc_get_last_lines(filename, buff + *length, if ((result=fc_get_last_lines(filename, buff, buff_size,
buff_size - *length, &lines, &current_count); &lines, count)) != 0)
{
if (result != ENOENT) {
return result;
}
}
if (*count >= target_count || current_write_index == 0) {
memmove(buff, lines.str, lines.len);
*length = lines.len;
return 0;
}
} else { } else {
result = errno != 0 ? errno : EPERM; result = errno != 0 ? errno : EPERM;
if (result != ENOENT) { if (result == ENOENT) {
*count = 0;
*length = 0;
return 0;
} else {
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"stat file %s fail, errno: %d, error info: %s", "stat file %s fail, errno: %d, error info: %s",
__LINE__, filename, result, STRERROR(result)); __LINE__, filename, result, STRERROR(result));
*count = 0; *count = 0;
*length = 0;
return result; return result;
} }
} }
if (result == 0) { sf_file_writer_get_filename(data_path, subdir_name,
memmove(buff + *length, lines.str, lines.len); current_write_index - 1, filename, sizeof(filename));
*length += lines.len; if (access(filename, F_OK) != 0) {
remain_count -= current_count; result = errno != 0 ? errno : EPERM;
if (remain_count == 0) { if (result == ENOENT) {
break; memmove(buff, lines.str, lines.len);
} *length = lines.len;
} else if (result != ENOENT) { return 0;
} else {
logError("file: "__FILE__", line: %d, "
"stat file %s fail, errno: %d, error info: %s",
__LINE__, filename, result, STRERROR(result));
*count = 0; *count = 0;
*length = 0;
return result; return result;
} }
if (current_index == 0) {
break;
} }
--current_index; //try previous binlog file 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;
} }
*count -= remain_count;
return 0; return 0;
} }