New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(mysql): be explicit that batch queries are parameterless #10095
fix(mysql): be explicit that batch queries are parameterless #10095
Conversation
ab7841d
to
db2cfbc
Compare
@@ -160,7 +160,7 @@ status_result(_Status = false) -> connecting. | |||
%%======================================================================================== | |||
|
|||
do_batch_insert(InstanceId, BatchReqs, InsertPart, Tokens, State) -> | |||
SQL = emqx_plugin_libs_rule:proc_batch_sql(BatchReqs, InsertPart, Tokens), | |||
SQL = emqx_plugin_libs_rule:proc_batch_mysql(BatchReqs, InsertPart, Tokens), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: since this is also used in TDengine, maybe the function name could be something more generic (perhaps the old name)?
Either this or "fork" the old function so that TDengine uses the old version?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did that as part of 2544742. I tried to raise some awareness there about pgsql in a comment, would be glad if you take another look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still digesting the escaping logic, but don't want to block the fix.
@@ -28,7 +28,7 @@ | |||
preproc_sql/1, | |||
preproc_sql/2, | |||
proc_sql/2, | |||
proc_sql_param_str/2, | |||
proc_mysql_param_str/2, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think adding a new function with this name may be better than replacing the generic function, there are many SQL-likes backend.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did that as part of 2544742. I tried to raise some awareness there about pgsql
in a comment, would be glad if you take another look.
@keynslug |
Thanks, @lafirest.
This is actually a speculation if this will improve performance, but I suspect it will, slightly, because we now won't hit whole layer of caching prepared statements in the client.
Sure, will do shortly, along with other post-review changes. |
Replied too quickly. 😅 What I meant is that while trying to hit some hot codepath and pinpoint where does a performance regression come from, under my workload I actually observed message loss due to prepared statements table exhaustion on the server. So this PR is not actually addressing a root cause for the performance regression, but theoretically, might contribute to it. Also hit some concerning error from the server (about SQL syntax error) that forced me to reimplement escaping logic. |
37d1433
to
870f820
Compare
Pull Request Test Coverage Report for Build 4373849295
💛 - Coveralls |
Hi @lafirest, I've attached a changelog entry with DeepL-ed Chinese translation, much appreciate if you could take a quick read-through. |
changes/ee/fix-10095.zh.md
Outdated
@@ -0,0 +1,3 @@ | |||
阻止 mysql 客户端在每个批处理中用不必要的 `PREPARE` 查询反复轰炸mysql服务器,破坏服务器并耗尽其内部限制。当 mysql 桥处于批处理模式时,这种情况正在发生。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
优化 MySQL 桥接在批量模式下能更高效的使用预处理语句
,减少了对 MySQL 服务器的查询压力, 并确保对 SQL 语句进行更安全和谨慎的转义。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't translate it word for word, but the meaning is correct.
changes/ee/fix-10095.en.md
Outdated
@@ -0,0 +1,3 @@ | |||
Stop mysql client from bombarding mysql server repeatedly with unnecessary `PREPARE` queries on every batch, trashing the server and exhausting its internal limits. This was happening when the mysql bridge was in the batch mode. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: mysql -> MySQL
2757d46
to
ac2b4a5
Compare
Also make them look as recommended in the documentation.
So that mysql client won't attempt to prepare them automatically, thus trashing the server's prepared statements table and making interaction overall heavier.
Also hexencode non-utf8 binaries. This is essentially an heuristic. We don't know column types in runtime, and there's no simple way to find them out. Since we're already doing full binary scan during escaping it should be cheap to bail out on non-utf8 strings and hexencode them instead. Also introduce separate function to highlight that this escaping is MySQL-specific.
Bump `emqx_plugin_libs` app version to 4.3.7.
Similar to what we have in mysql and pgqsl testsuites.
ac2b4a5
to
f7c0d29
Compare
So that mysql client won't attempt to prepare them automatically, thus trashing the server's prepared statements table and making whole interaction overall heavier. Also add tests to verify that we don't allocate more prepared statements than needed.
This PR also fixes an issue where some strings might have been inserted inconsistently, since escaping logic missed some edge cases.
EEC-782
EMQX-9167