Skip to content
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

Prepared queries for MAM #2961

Merged
merged 53 commits into from
Dec 14, 2020
Merged

Conversation

arcusfelis
Copy link
Contributor

This PR is rebased version of #2958

Proposed changes include:

  • We use prepared queries in mod_mam_rdbms_arch

@codecov
Copy link

codecov bot commented Dec 1, 2020

Codecov Report

Merging #2961 (031e841) into mu-prepared-queries (0b736f6) will increase coverage by 0.28%.
The diff coverage is 84.92%.

Impacted file tree graph

@@                   Coverage Diff                   @@
##           mu-prepared-queries    #2961      +/-   ##
=======================================================
+ Coverage                79.10%   79.39%   +0.28%     
=======================================================
  Files                      377      382       +5     
  Lines                    32846    32810      -36     
=======================================================
+ Hits                     25982    26048      +66     
+ Misses                    6864     6762     -102     
Impacted Files Coverage Δ
src/mam/mod_mam.erl 88.88% <ø> (ø)
src/rdbms/mongoose_rdbms.erl 63.55% <40.00%> (-1.85%) ⬇️
src/mam/mod_mam_rdbms_arch.erl 48.45% <47.14%> (+9.18%) ⬆️
src/mam/mod_mam_utils.erl 88.92% <85.71%> (-0.08%) ⬇️
src/mam/mam_filter.erl 91.66% <91.66%> (ø)
src/mam/mod_mam_muc_rdbms_arch.erl 94.73% <92.75%> (+6.22%) ⬆️
src/mam/mam_lookup.erl 96.15% <96.15%> (ø)
src/mam/mam_lookup_sql.erl 98.43% <98.43%> (ø)
src/mam/mam_decoder.erl 100.00% <100.00%> (ø)
src/mam/mam_encoder.erl 100.00% <100.00%> (ø)
... and 17 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0b736f6...031e841. Read the comment docs.

Copy link
Member

@chrzaszcz chrzaszcz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the improvements.
I added some comments and I think a few of them should be addressed to make the code even better.

src/mam/mod_mam_rdbms_arch.erl Outdated Show resolved Hide resolved
@@ -28,8 +28,13 @@

-export([get_db_type/0,
begin_trans/0,
get_db_specific_limits/0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems unused, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_db_specific_limits/0 is used now...

src/mam/mod_mam_rdbms_arch.erl Outdated Show resolved Hide resolved
src/mam/mod_mam_rdbms_arch.erl Outdated Show resolved Hide resolved
src/mam/mod_mam_rdbms_arch.erl Outdated Show resolved Hide resolved
src/mam/mod_mam_rdbms_arch.erl Outdated Show resolved Hide resolved
src/mam/mod_mam_rdbms_arch.erl Outdated Show resolved Hide resolved
src/mam/mod_mam_rdbms_arch.erl Show resolved Hide resolved
@arcusfelis arcusfelis changed the title Prepared queries for one2one MAM Prepared queries for MAM Dec 2, 2020
Copy link
Member

@chrzaszcz chrzaszcz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some comments. There is a lot of good changes, thanks for that.

However, I think the PR itself became a bit too big, I hoped we could divide the work as agreed before.

Anyway, the only serious concerns are about the untested lines of code.

@@ -0,0 +1,2 @@
-record(db_mapping, {column :: atom(), param :: atom(), format :: atom()}).
-record(lookup_field, {op :: atom(), column :: atom(), param :: atom(), required :: atom(), value_maker :: atom()}).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line too long, just put each field in a separate line.

There are more lines longer than 100 characters in this PR. I am not adding any comments to the rest them, but I think it's good to change them stay within this limit - inaka/elvis recommends that and I think it's a reasonable value.

Btw, is required boolean()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

required :: true | undefined.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not true | false?

Comment on lines 9 to 37
decode_row({ExtMessID, ExtSrcJID, ExtData}, Env) ->
MessID = mongoose_rdbms:result_to_integer(ExtMessID),
SrcJID = decode_jid(ExtSrcJID, Env),
Packet = decode_packet(ExtData, Env),
{MessID, SrcJID, Packet}.

decode_muc_row({ExtMessID, Nick, ExtData}, Env = #{archive_jid := RoomJID}) ->
MessID = mongoose_rdbms:result_to_integer(ExtMessID),
SrcJID = jid:replace_resource(RoomJID, Nick),
Packet = decode_packet(ExtData, Env),
{MessID, SrcJID, Packet}.

decode_muc_gdpr_row({ExtMessID, ExtData}, Env) ->
Packet = decode_packet(ExtData, Env),
{ExtMessID, Packet}.

decode_retraction_info(_Env, []) -> skip;
decode_retraction_info(Env, [{ResMessID, Data}]) ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add type specs for exported functions? Or are you planning to do that later?

src/mam/mam_filter.erl Outdated Show resolved Hide resolved

%% Private logic below

%% There is no optimizations for these queries yet:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
%% There is no optimizations for these queries yet:
%% There are no optimizations for these queries yet:

Copy link
Contributor Author

@arcusfelis arcusfelis Dec 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, skipped this. Would add that after the final review ^^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

src/mam/mam_lookup.erl Show resolved Hide resolved
src/mam/mod_mam_rdbms_arch.erl Outdated Show resolved Hide resolved
@@ -28,8 +28,13 @@

-export([get_db_type/0,
begin_trans/0,
get_db_specific_limits/0,

This comment was marked as resolved.

src/mam/mod_mam_utils.erl Outdated Show resolved Hide resolved
src/mam/mod_mam_muc_rdbms_arch.erl Show resolved Hide resolved
src/mam/mod_mam_muc_rdbms_arch.erl Outdated Show resolved Hide resolved
@chrzaszcz
Copy link
Member

@arcusfelis I think we need to rebase this branch - it shows many more commits than it should which makes it hard to review.

Copy link
Member

@chrzaszcz chrzaszcz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the minio fix from the PR? The rest look really good, I only have a few concerns, once these few are addressed it can be merged.


-spec unescape_binary(binary(), env_vars()) -> binary().
unescape_binary(Bin, #{host := Host}) ->
%% Funny, rdbms ignores this Host variable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: It's good to have this remark, but maybe remove the word 'funny'? It just does not seem right for me.

Copy link
Contributor Author

@arcusfelis arcusfelis Dec 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the whole comment.

src/mam/mam_lookup.erl Show resolved Hide resolved
Comment on lines 68 to 78
What = #{what => mam_lookup_failed, statement => StmtName,
sql_query => lookup_sql_binary(QueryType, Table, Env, Filters, Order, OffsetLimit),
reason => Error, host => Host},
?LOG_ERROR(What),
error(What)
catch error:Error:Stacktrace ->
What = #{what => mam_lookup_failed, statement => StmtName,
sql_query => lookup_sql_binary(QueryType, Table, Env, Filters, Order, OffsetLimit),
stacktrace => Stacktrace, reason => Error, host => Host},
?LOG_ERROR(What),
erlang:raise(error, Error, Stacktrace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It worries me a bit that these lines are not tested, but I understand that we only check the happy path in tests. Did you check both error cases manually? I think it's necessary here, especially given the recent formatter crashes - we often have errors in error handling (sic!).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reason why it's here is that it's good to see an SQL query string when something fails.
But it's pretty generic code. And we also have mod_mam_utils:success_sql_query/?.

So, I've created mongoose_rdbms:execute_successfully/3 for all these cases (and we don't have error handling in MAM anymore, that's good)


filters_to_sql(Filters) ->
SQLs = [filter_to_sql(Filter) || Filter <- Filters],
case skip_undefined(SQLs) of
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need to do this? It looks like we do not have undefined in the list. Btw, I prefer code without temporary undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

So, we can more easily print it
Also, ets-lookup logic would run a bit faster for big iolists
Copy link
Member

@chrzaszcz chrzaszcz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now, great work 👍

@chrzaszcz chrzaszcz merged commit 3a5d1bc into mu-prepared-queries Dec 14, 2020
@chrzaszcz chrzaszcz deleted the mu-prepared-queries-mam branch December 14, 2020 15:19
arcusfelis added a commit that referenced this pull request Mar 1, 2021
…- final commit

So, we can more easily print it
Also, ets-lookup logic would run a bit faster for big iolists
arcusfelis added a commit that referenced this pull request Mar 1, 2021
Store statement as binary in prepared_statements ETS table
So, we can more easily print it
Also, ets-lookup logic would run a bit faster for big iolists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants