Skip to content

Commit 03d365f

Browse files
committed
[#3666] kea-admin lease-upload: write SQL statements to file
Avoids "Argument list too long". Also considerably speeds up the lease upload. The slowdown was noticeable for large number of leases, where for each lease, the whole set of statements added up to that point had to be printed in order to append another SQL statement. This is no longer the case since the SQL statement is appended to a file.
1 parent 1569148 commit 03d365f

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/bin/admin/kea-admin.in

+24-18
Original file line numberDiff line numberDiff line change
@@ -709,10 +709,22 @@ lease_upload() {
709709
exit 1
710710
fi
711711

712-
# Invoke LFC on the input file.
713-
log_info "Looking at ${input_file_line_length} lines of CSV in ${input_file}..."
712+
if test "${backend}" = 'mysql'; then
713+
function_call="CALL lease${dhcp_version}"
714+
elif test "${backend}" = 'pgsql'; then
715+
function_call="SELECT lease${dhcp_version}"
716+
else
717+
log_error "lease-upload not implemented for ${backend}"
718+
exit 1
719+
fi
720+
714721
cleaned_up_csv="/tmp/$(basename "${input_file}").tmp"
715722
check_file_overwrite "${cleaned_up_csv}"
723+
sql_statement_file="/tmp/$(basename "${input_file}").sql.tmp"
724+
check_file_overwrite "${sql_statement_file}"
725+
726+
# Invoke LFC on the input file.
727+
log_info "Looking at ${input_file_line_length} lines of CSV in ${input_file}..."
716728
cp "${input_file}" "${cleaned_up_csv}"
717729
"${KEA_LFC}" "-${dhcp_version}" -x "${cleaned_up_csv}" \
718730
-i "${cleaned_up_csv}.1" -o "${cleaned_up_csv}.output" \
@@ -738,37 +750,31 @@ lease_upload() {
738750

739751
# Construct the SQL insert statements.
740752
header_parsed=false
741-
sql_statement='START TRANSACTION;'
753+
echo 'START TRANSACTION;' > "${sql_statement_file}"
742754
while read -r line; do
743-
if "${header_parsed}"; then
744-
line=$(stringify_positions_in_line "${string_positions}" "${line}")
745-
if test "${backend}" = 'mysql'; then
746-
sql_statement="${sql_statement} CALL lease${dhcp_version}Upload(${line}); "
747-
elif test "${backend}" = 'pgsql'; then
748-
sql_statement="${sql_statement} SELECT lease${dhcp_version}Upload(${line}); "
749-
else
750-
log_error "lease-upload not implemented for ${backend}"
751-
exit 1
752-
fi
753-
else
755+
if ! "${header_parsed}"; then
754756
header_parsed=true
757+
continue
755758
fi
759+
line=$(stringify_positions_in_line "${string_positions}" "${line}")
760+
echo "${function_call}Upload(${line});" >> "${sql_statement_file}"
756761
done < "${cleaned_up_csv}"
757-
sql_statement="${sql_statement} COMMIT;"
762+
echo 'COMMIT;' >> "${sql_statement_file}"
758763

759764
# Execute the SQL insert statements.
760765
if test "${backend}" = 'mysql'; then
761-
output="$(mysql_execute "${sql_statement}")"
766+
output="$(mysql_execute_script "${sql_statement_file}")"
762767
elif test "${backend}" = 'pgsql'; then
763-
output="$(pgsql_execute "${sql_statement}")"
768+
output="$(pgsql_execute_script "${sql_statement_file}")"
764769
else
765770
log_error "lease-upload not implemented for ${backend}"
766771
exit 1
767772
fi
768773

769774
# Clean up the temporary CSV.
770775
rm -f "${cleaned_up_csv}"
771-
log_info "Removed temporary file ${cleaned_up_csv}."
776+
rm -f "${sql_statement_file}"
777+
log_info "Removed temporary files: ${cleaned_up_csv}, ${sql_statement_file}."
772778

773779
# Print a confirmation message.
774780
log_info "Successfully updated table lease${dhcp_version}."

0 commit comments

Comments
 (0)