Description:
In function find_uniq_filename, the binlog dir was always scaned to find the biggest unique number. If there are too many files in the dir, this scan may be Inefficient (must hold LOCK_log while rotate the binlog file).
Since Variable log_bin_basename is read only . Actually I think we only need to scan the dir only once (for example , after the instance was started, or some operation such as purge binlog/reset master, etc...) and store the biggest number. This number can be added when binlog rotate is needed. We only need to check if the new filename was existed in the binlog dir.
related trace:
MYSQL_BIN_LOG::new_file_impl -> MYSQL_LOG::generate_new_name -> find_uniq_filename()
quoted code from find_uniq_filename()
file_info= dir_info->dir_entry;
for (i= dir_info->number_off_files ; i-- ; file_info++)
{
if (memcmp(file_info->name, start, length) == 0 &&
test_if_number(file_info->name+length, &number,0))
{
set_if_bigger(max_found,(ulong) number);
}
}
How to repeat:
read related code.
Suggested fix:
optimize it