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

feat: add client mqueue/inflight messages API #12561

Conversation

SergeTupchiy
Copy link
Contributor

@SergeTupchiy SergeTupchiy commented Feb 21, 2024

Fixes EMQX-11861
Release version: v/e5.6.0

NOTE: session_mem only implementation, persistent_session is planned to be addressed in a separate PR.

Summary

PR Checklist

Please convert it to a draft if any of the following conditions are not met. Reviewers may skip over until all the items are checked:

  • Added tests for the changes
  • Added property-based tests for code which performs user input validation
  • Changed lines covered in coverage report
  • Change log has been added to changes/(ce|ee)/(feat|perf|fix|breaking)-<PR-id>.en.md files
  • For internal contributor: there is a jira ticket to track this change
  • Created PR to emqx-docs if documentation update is required, or link to a follow-up jira ticket
  • Schema changes are backward compatible

Checklist for CI (.github/workflows) changes

  • If changed package build workflow, pass this action (manual trigger)
  • Change log has been added to changes/ dir for user-facing artifacts update

@SergeTupchiy SergeTupchiy force-pushed the EMQX-11861-client-mqueue-inflight-API branch 2 times, most recently from 6a46eef to a870dc0 Compare February 22, 2024 17:37
from_clientid => emqx_utils_conv:bin(From),
from_username => maps:get(username, Headers, <<>>)
},
case erlang:byte_size(Payload) =< ?MAX_MSG_PAYLOAD_SIZE of
Copy link
Contributor Author

Choose a reason for hiding this comment

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

NOTE: message formatter/schema, payload size limit is copied from emqx_retainer_api.
Perhaps some extra info should be added when payload is omitted due to exceeding the limit?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe add an option like ?payload=none | base64 | plain to the API.
Return 400 if payload is not JSON serializable and plain is requested.

About size limit, maybe alongside the page limit, there can be a max_payload_bytes limit and each response try to accumulate not greater than this limit however ensure at least one message is returned even if the size is above limit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -268,6 +268,9 @@ info(inflight_cnt, #session{inflight = Inflight}) ->
emqx_inflight:size(Inflight);
info(inflight_max, #session{inflight = Inflight}) ->
emqx_inflight:max_size(Inflight);
info({inflight_msgs, PagerParams}, #session{inflight = Inflight}) ->
{InflightList, Meta} = emqx_inflight:query(Inflight, PagerParams),
{[I#inflight_data.message || {_, I} <- InflightList], Meta};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we enrich message with some inflight details (wait_ack | wait_comp, timestamp)?

Copy link
Member

Choose a reason for hiding this comment

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

maybe no need for now

@SergeTupchiy SergeTupchiy force-pushed the EMQX-11861-client-mqueue-inflight-API branch 2 times, most recently from 1a76e0f to 60000af Compare February 23, 2024 14:43
@SergeTupchiy SergeTupchiy marked this pull request as ready for review February 23, 2024 14:54
@SergeTupchiy SergeTupchiy requested review from lafirest and a team as code owners February 23, 2024 14:54
qos => Qos,
topic => Topic,
publish_at =>
emqx_utils_calendar:epoch_to_rfc3339(Timestamp),
Copy link
Member

Choose a reason for hiding this comment

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

maybe should not format it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I followed retainer messages formatting...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed time formatting

from_clientid => emqx_utils_conv:bin(From),
from_username => maps:get(username, Headers, <<>>)
},
case erlang:byte_size(Payload) =< ?MAX_MSG_PAYLOAD_SIZE of
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add an option like ?payload=none | base64 | plain to the API.
Return 400 if payload is not JSON serializable and plain is requested.

About size limit, maybe alongside the page limit, there can be a max_payload_bytes limit and each response try to accumulate not greater than this limit however ensure at least one message is returned even if the size is above limit.

@SergeTupchiy SergeTupchiy force-pushed the EMQX-11861-client-mqueue-inflight-API branch from 60000af to e0d13ed Compare March 5, 2024 19:45
<<"meta">> => #{
<<"count">> => 1,
<<"continuation">> => <<"AAYS53qRa0n07AAABFIACg==">>,
<<"limit">> => 50,
Copy link
Member

Choose a reason for hiding this comment

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

there is maybe no need to return limit in response ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed limit from meta

@@ -0,0 +1,3 @@
Implement HTTP API to get the list of client's inflight and mqueue messages:
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
Implement HTTP API to get the list of client's inflight and mqueue messages:
Implement HTTP APIs to get the list of client's inflight and mqueue messages:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@@ -0,0 +1,3 @@
Implement HTTP API to get the list of client's inflight and mqueue messages:
- GET /clients/{clientid}/mqueue_messages?page=1&limit=100
Copy link
Member

Choose a reason for hiding this comment

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

page is now continuation ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, thanks

@@ -0,0 +1,3 @@
Implement HTTP API to get the list of client's inflight and mqueue messages:
- GET /clients/{clientid}/mqueue_messages?page=1&limit=100
- GET /clients/{clientid}/inflight_messages?page=1&limit=100
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add:

  1. he ordering of the messages in each page?
  2. how to start the first page
  3. how to continue the next pages
  4. what happens if continuation is invalid

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@SergeTupchiy SergeTupchiy force-pushed the EMQX-11861-client-mqueue-inflight-API branch 2 times, most recently from a186ee3 to 5606257 Compare March 6, 2024 19:24
@SergeTupchiy SergeTupchiy changed the base branch from master to release-56 March 6, 2024 19:24
Resp = #{meta => Meta1, data => format_msgs(Msgs, PayloadFmt, MaxBytes)},
%% Make sure minirest won't set another content-type for self-encoded JSON response body
Headers = #{<<"content-type">> => <<"application/json">>},
case emqx_utils_json:safe_encode(Resp) of
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: this can actually serialize pretty much every payload, as it is not parsed and validated.
For example, if message.payload is invalid JSON string (missing enclosing }), it won't be detected:

emqx_utils_json:safe_encode(#{message => <<"{\"foo\": \"bar\"">>}).
{ok,<<"{\"message\":\"{\\\"foo\\\": \\\"bar\\\"\"}">>}
```

@SergeTupchiy SergeTupchiy force-pushed the EMQX-11861-client-mqueue-inflight-API branch from 5606257 to a2e7616 Compare March 7, 2024 10:06
@SergeTupchiy SergeTupchiy merged commit 1f38813 into emqx:release-56 Mar 7, 2024
166 checks passed
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.

None yet

2 participants