fix(mysql): fix the problem of data loss and bad match when mysql is disconnected

This commit is contained in:
firest 2022-12-30 00:13:03 +08:00
parent e1ae06a132
commit bc37556963
1 changed files with 21 additions and 7 deletions

View File

@ -408,20 +408,34 @@ on_sql_query(
LogMeta = #{connector => InstId, sql => SQLOrKey, state => State}, LogMeta = #{connector => InstId, sql => SQLOrKey, state => State},
?TRACE("QUERY", "mysql_connector_received", LogMeta), ?TRACE("QUERY", "mysql_connector_received", LogMeta),
Worker = ecpool:get_client(PoolName), Worker = ecpool:get_client(PoolName),
{ok, Conn} = ecpool_worker:client(Worker), case ecpool_worker:client(Worker) of
{ok, Conn} ->
?tp( ?tp(
mysql_connector_send_query, mysql_connector_send_query,
#{sql_func => SQLFunc, sql_or_key => SQLOrKey, data => Data} #{sql_func => SQLFunc, sql_or_key => SQLOrKey, data => Data}
), ),
do_sql_query(SQLFunc, Conn, SQLOrKey, Data, Timeout, LogMeta);
{error, disconnected} ->
?SLOG(
error,
LogMeta#{
msg => "mysql_connector_do_sql_query_failed",
reason => worker_is_disconnected
}
),
{error, {recoverable_error, disconnected}}
end.
do_sql_query(SQLFunc, Conn, SQLOrKey, Data, Timeout, LogMeta) ->
try mysql:SQLFunc(Conn, SQLOrKey, Data, Timeout) of try mysql:SQLFunc(Conn, SQLOrKey, Data, Timeout) of
{error, disconnected} = Result -> {error, disconnected} ->
?SLOG( ?SLOG(
error, error,
LogMeta#{msg => "mysql_connector_do_sql_query_failed", reason => disconnected} LogMeta#{msg => "mysql_connector_do_sql_query_failed", reason => disconnected}
), ),
%% kill the poll worker to trigger reconnection %% kill the poll worker to trigger reconnection
_ = exit(Conn, restart), _ = exit(Conn, restart),
Result; {error, {recoverable_error, disconnected}};
{error, not_prepared} = Error -> {error, not_prepared} = Error ->
?tp( ?tp(
mysql_connector_prepare_query_failed, mysql_connector_prepare_query_failed,