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

Better timestamp logic #3031

Merged
merged 8 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion big_tests/rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
%% We use https:// instead of git://
{deps, [
{base16, {git, "https://github.com/esl/base16.git", {tag, "1.1.0"}}},
{usec, {git, "https://github.com/esl/usec.git", {branch, "master"}}},
{bbmustache, "1.10.0"},
{erlsh, {git, "https://github.com/proger/erlsh.git", {ref, "4e8a107"}}},
{jiffy, "1.0.5"},
Expand Down
4 changes: 0 additions & 4 deletions big_tests/rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@
{ref,"0f0df1b10aac78cbe6088b142d172de43fc387a3"}},
1},
{<<"stun">>,{pkg,<<"stun">>,<<"1.0.32">>},1},
{<<"usec">>,
{git,"https://github.com/esl/usec.git",
{ref,"f85ffd8350d7000c26392c18bdfcdbb30f3b5ee8"}},
0},
{<<"uuid">>,{pkg,<<"uuid_erl">>,<<"1.8.0">>},1},
{<<"worker_pool">>,{pkg,<<"worker_pool">>,<<"3.2.0">>},1}]}.
[
Expand Down
38 changes: 16 additions & 22 deletions big_tests/tests/ejabberdctl_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ set_last(Config) ->

escalus:wait_for_stanza(Alice), % ignore push

Now = usec:to_sec(usec:from_now(os:timestamp())),
Now = os:system_time(second),
TS = integer_to_list(Now - 7200),
{_, 0} = ejabberdctl("set_last", [BobName, Domain, TS, "Status"], Config),
escalus:send(Alice, escalus_stanza:last_activity(BobJid)),
Expand Down Expand Up @@ -1200,9 +1200,9 @@ remove_old_messages_test(Config) ->
"Hi, how are you? Its old message!"),
Msg2 = escalus_stanza:chat_to(<<"bob@", Domain/binary>>,
"Hello its new message!"),
OldTimestamp = fallback_timestamp(10, os:timestamp()),
OldTimestamp = fallback_timestamp(10, os:system_time(microsecond)),
OfflineOld = generate_offline_message(JidRecordAlice, JidRecordBob, Msg1, OldTimestamp),
OfflineNew = generate_offline_message(JidRecordAlice, JidRecordBob, Msg2, os:timestamp()),
OfflineNew = generate_offline_message(JidRecordAlice, JidRecordBob, Msg2, os:system_time(microsecond)),
{jid, _, _, _, LUser, LServer, _} = JidRecordBob,
rpc_call(mod_offline_backend, write_messages, [LUser, LServer, [OfflineOld, OfflineNew]]),
%% when
Expand All @@ -1220,25 +1220,21 @@ remove_expired_messages_test(Config) ->
JidRecordMike = jid:from_binary(JidA),
JidRecordKate = jid:from_binary(JidB),
Domain = domain(),
Msg1 = escalus_stanza:chat_to(<<"kate@", Domain/binary>>,
"Rolling stones"),
Msg2 = escalus_stanza:chat_to(<<"kate@", Domain/binary>>,
"Arctic monkeys!"),
Msg3 = escalus_stanza:chat_to(<<"kate@", Domain/binary>>,
"More wine..."),
Msg4 = escalus_stanza:chat_to(<<"kate@", Domain/binary>>,
"kings of leon"),
OldTimestamp = fallback_timestamp(10, os:timestamp()),
ExpirationTime = fallback_timestamp(2, os:timestamp()),
ExpirationTimeFuture= fallback_timestamp(-5, os:timestamp()),
Msg1 = escalus_stanza:chat_to(<<"kate@", Domain/binary>>, "Rolling stones"),
Msg2 = escalus_stanza:chat_to(<<"kate@", Domain/binary>>, "Arctic monkeys!"),
Msg3 = escalus_stanza:chat_to(<<"kate@", Domain/binary>>, "More wine..."),
Msg4 = escalus_stanza:chat_to(<<"kate@", Domain/binary>>, "kings of leon"),
OldTimestamp = fallback_timestamp(10, os:system_time(microsecond)),
ExpirationTime = fallback_timestamp(2, os:system_time(microsecond)),
ExpirationTimeFuture= fallback_timestamp(-5, os:system_time(microsecond)),
OfflineOld = generate_offline_expired_message(JidRecordMike,
JidRecordKate, Msg1,
OldTimestamp,
ExpirationTime),
OfflineNow = generate_offline_expired_message(JidRecordMike,
JidRecordKate, Msg2, os:timestamp(), ExpirationTime),
JidRecordKate, Msg2, os:system_time(microsecond), ExpirationTime),
OfflineFuture = generate_offline_expired_message(JidRecordMike,
JidRecordKate, Msg3, os:timestamp(), ExpirationTimeFuture),
JidRecordKate, Msg3, os:system_time(microsecond), ExpirationTimeFuture),
OfflineFuture2 = generate_offline_expired_message(JidRecordMike,
JidRecordKate, Msg4,
OldTimestamp,
Expand Down Expand Up @@ -1273,12 +1269,10 @@ generate_offline_expired_message(From, To, Msg, TimeStamp, ExpirationTime) ->
expire = ExpirationTime, from = From, to = To, packet = Msg}.


fallback_timestamp(Days, {MegaSecs, Secs, _MicroSecs}) ->
S = MegaSecs * 1000000 + Secs - 60 * 60 * 24 * Days,
MegaSecs1 = S div 1000000,
Secs1 = S rem 1000000,
{MegaSecs1, Secs1, 0}.

fallback_timestamp(HowManyDays, TS_MicroSeconds) ->
HowManySeconds = HowManyDays * 86400,
HowManyMicroSeconds = erlang:convert_time_unit(HowManySeconds, second, microsecond),
TS_MicroSeconds - HowManyMicroSeconds.

get_user_data(User, Config) when is_atom(User) ->
get_user_data(escalus_users:get_options(Config, User, <<"newres">>), Config);
Expand Down
2 changes: 1 addition & 1 deletion big_tests/tests/mam_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ wait_for_complete_archive_response(P, Alice, ExpectedCompleteValue)
#{mam_props => P, parsed_iq => ParsedIQ}).

make_iso_time(Micro) ->
calendar:system_time_to_rfc3339(usec:to_sec(Micro), [{offset, "Z"}]).
calendar:system_time_to_rfc3339(erlang:convert_time_unit(Micro, microsecond, second), [{offset, "Z"}]).

generate_message_text(N) when is_integer(N) ->
<<"Message #", (list_to_binary(integer_to_list(N)))/binary>>.
Expand Down
2 changes: 1 addition & 1 deletion include/mod_inbox.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
-type inbox_res() :: {RemoteBinJID :: binary(),
MsgContent :: content(),
UnreadCount :: count_bin(),
Timestamp :: erlang:timestamp()}.
Timestamp :: integer()}.

-type inbox_write_res() :: ok | {error, any()}.

Expand Down
1 change: 0 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
{meck, "0.8.13"},

{pa, {git, "https://github.com/erszcz/pa.git", {branch, "master"}}},
{usec, {git, "https://github.com/esl/usec.git", {branch, "master"}}},
{bbmustache, "1.10.0"},
{recon, "2.5.1"},
{erlcloud, "3.3.1"},
Expand Down
4 changes: 0 additions & 4 deletions rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,6 @@
{<<"tomerl">>,{pkg,<<"tomerl">>,<<"0.4.0">>},0},
{<<"trails">>,{pkg,<<"trails">>,<<"2.1.0">>},0},
{<<"unicode_util_compat">>,{pkg,<<"unicode_util_compat">>,<<"0.5.0">>},1},
{<<"usec">>,
{git,"https://github.com/esl/usec.git",
{ref,"f85ffd8350d7000c26392c18bdfcdbb30f3b5ee8"}},
0},
{<<"uuid">>,{pkg,<<"uuid_erl">>,<<"1.8.0">>},0},
{<<"worker_pool">>,{pkg,<<"worker_pool">>,<<"4.0.3">>},0}]}.
[
Expand Down
26 changes: 12 additions & 14 deletions src/inbox/mod_inbox.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
ToBareJid :: binary(),
Content :: binary(),
MsgId :: binary(),
Timestamp :: erlang:timestamp().
Timestamp :: integer().

-callback reset_unread(Username, Server, BareJid, MsgId) -> inbox_write_res() when
Username :: jid:luser(),
Expand All @@ -85,8 +85,8 @@
InterlocutorJID :: jid:jid().

-type get_inbox_params() :: #{
start => erlang:timestamp(),
'end' => erlang:timestamp(),
start => integer(),
'end' => integer(),
order => asc | desc,
hidden_read => true | false
}.
Expand All @@ -101,8 +101,8 @@
get_personal_data(Acc, #jid{ luser = LUser, lserver = LServer }) ->
Schema = ["jid", "content", "unread_count", "timestamp"],
InboxParams = #{
start => {0,0,0},
'end' => erlang:timestamp(),
start => 0,
'end' => erlang:system_time(microsecond),
order => asc,
hidden_read => false
},
Expand All @@ -111,8 +111,7 @@ get_personal_data(Acc, #jid{ luser = LUser, lserver = LServer }) ->
[{inbox, Schema, ProcessedEntries} | Acc].

process_entry({RemJID, Content, UnreadCount, Timestamp}) ->
USec = usec:from_now(Timestamp),
TS = calendar:system_time_to_rfc3339(USec, [{offset, "Z"}, {unit, microsecond}]),
TS = calendar:system_time_to_rfc3339(Timestamp, [{offset, "Z"}, {unit, microsecond}]),
{RemJID, Content, UnreadCount, TS}.

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -334,7 +333,7 @@ build_inbox_message({_Username, Content, Count, Timestamp}, QueryId) ->
#xmlel{name = <<"message">>, attrs = [{<<"id">>, mod_inbox_utils:wrapper_id()}],
children = [build_result_el(Content, QueryId, Count, Timestamp)]}.

-spec build_result_el(content(), id(), count_bin(), erlang:timestamp()) -> exml:element().
-spec build_result_el(content(), id(), count_bin(), integer()) -> exml:element().
build_result_el(Msg, QueryId, BinUnread, Timestamp) ->
Forwarded = build_forward_el(Msg, Timestamp),
QueryAttr = [{<<"queryid">>, QueryId} || QueryId =/= undefined, QueryId =/= <<>>],
Expand All @@ -358,17 +357,16 @@ build_result_iq(List) ->
build_result_el(Name, CountBin) ->
#xmlel{name = Name, children = [#xmlcdata{content = CountBin}]}.

-spec build_forward_el(content(), erlang:timestamp()) -> exml:element().
-spec build_forward_el(content(), integer()) -> exml:element().
build_forward_el(Content, Timestamp) ->
{ok, Parsed} = exml:parse(Content),
Delay = build_delay_el(Timestamp),
#xmlel{name = <<"forwarded">>, attrs = [{<<"xmlns">>, ?NS_FORWARD}],
children = [Delay, Parsed]}.

-spec build_delay_el(Timestamp :: erlang:timestamp()) -> exml:element().
-spec build_delay_el(Timestamp :: integer()) -> exml:element().
build_delay_el(Timestamp) ->
USec = usec:from_now(Timestamp),
TS = calendar:system_time_to_rfc3339(USec, [{offset, "Z"}, {unit, microsecond}]),
TS = calendar:system_time_to_rfc3339(Timestamp, [{offset, "Z"}, {unit, microsecond}]),
jlib:timestamp_to_xml(TS, undefined, undefined).

-spec build_inbox_form() -> exml:element().
Expand Down Expand Up @@ -465,7 +463,7 @@ fields_to_params([], Acc) ->
fields_to_params([{<<"start">>, [StartISO]} | RFields], Acc) ->
try calendar:rfc3339_to_system_time(binary_to_list(StartISO), [{unit, microsecond}]) of
StartStamp ->
fields_to_params(RFields, Acc#{ start => usec:to_now(StartStamp) })
fields_to_params(RFields, Acc#{ start => StartStamp })
catch error:Error ->
?LOG_WARNING(#{what => inbox_invalid_form_field,
reason => Error, field => start, value => StartISO}),
Expand All @@ -474,7 +472,7 @@ fields_to_params([{<<"start">>, [StartISO]} | RFields], Acc) ->
fields_to_params([{<<"end">>, [EndISO]} | RFields], Acc) ->
try calendar:rfc3339_to_system_time(binary_to_list(EndISO), [{unit, microsecond}]) of
EndStamp ->
fields_to_params(RFields, Acc#{ 'end' => usec:to_now(EndStamp) })
fields_to_params(RFields, Acc#{ 'end' => EndStamp })
catch error:Error ->
?LOG_WARNING(#{what => inbox_invalid_form_field,
reason => Error, field => 'end', value => EndISO}),
Expand Down
9 changes: 4 additions & 5 deletions src/inbox/mod_inbox_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ remove_inbox_rdbms(Username, Server, ToBareJid) ->
ToBareJid :: binary(),
Content :: binary(),
MsgId :: binary(),
Timestamp :: erlang:timestamp()) -> ok | {ok, integer()}.
Timestamp :: integer()) -> ok | {ok, integer()}.
set_inbox_incr_unread(Username, Server, ToBareJid, Content, MsgId, Timestamp) ->
LUsername = jid:nodeprep(Username),
LServer = jid:nameprep(Server),
Expand Down Expand Up @@ -198,11 +198,10 @@ esc_int(Integer) ->
order_to_sql(asc) -> <<"ASC">>;
order_to_sql(desc) -> <<"DESC">>.

-spec sql_and_where_timestamp(Operator :: string(), Timestamp :: erlang:timestamp()) -> iolist().
-spec sql_and_where_timestamp(Operator :: string(), Timestamp :: integer()) -> iolist().
sql_and_where_timestamp(_Operator, undefined) ->
[];
sql_and_where_timestamp(Operator, Timestamp) ->
NumericTimestamp = usec:from_now(Timestamp),
sql_and_where_timestamp(Operator, NumericTimestamp) ->
[" AND timestamp ", Operator, esc_int(NumericTimestamp)].

-spec sql_and_where_unread_count(HiddenRead :: boolean()) -> iolist().
Expand All @@ -226,7 +225,7 @@ decode_row(LServer, {Username, Content, Count, Timestamp}) ->
Data = mongoose_rdbms:unescape_binary(LServer, Content),
BCount = count_to_bin(Count),
NumericTimestamp = mongoose_rdbms:result_to_integer(Timestamp),
{Username, Data, BCount, usec:to_now(NumericTimestamp)}.
{Username, Data, BCount, NumericTimestamp}.


rdbms_specific_backend(Host) ->
Expand Down
10 changes: 5 additions & 5 deletions src/mam/mod_mam.erl
Original file line number Diff line number Diff line change
Expand Up @@ -588,18 +588,18 @@ lookup_messages_without_policy_violation_check(Host, #{search_text := SearchText
true -> %% Use of disabled full text search
{error, 'not-supported'};
false ->
StartT = os:timestamp(),
StartT = erlang:monotonic_time(microsecond),
R = mongoose_hooks:mam_lookup_messages(Host, {ok, {0, 0, []}}, Params),
Diff = timer:now_diff(os:timestamp(), StartT),
Diff = erlang:monotonic_time(microsecond) - StartT,
mongoose_metrics:update(Host, [backends, ?MODULE, lookup], Diff),
R
end.

-spec archive_message(jid:server(), mod_mam:archive_message_params()) -> ok | {error, timeout}.
archive_message(Host, Params) ->
StartT = os:timestamp(),
StartT = erlang:monotonic_time(microsecond),
chrzaszcz marked this conversation as resolved.
Show resolved Hide resolved
R = mongoose_hooks:mam_archive_message(Host, ok, Params),
Diff = timer:now_diff(os:timestamp(), StartT),
Diff = erlang:monotonic_time(microsecond) - StartT,
mongoose_metrics:update(Host, [backends, ?MODULE, archive], Diff),
R.

Expand All @@ -613,7 +613,7 @@ archive_message(Host, Params) ->
exml:element().
message_row_to_xml(MamNs, {MessID, SrcJID, Packet}, QueryID, SetClientNs) ->
{Microseconds, _NodeMessID} = decode_compact_uuid(MessID),
TS = calendar:system_time_to_rfc3339(usec:to_sec(Microseconds), [{offset, "Z"}]),
TS = calendar:system_time_to_rfc3339(erlang:convert_time_unit(Microseconds, microsecond, second), [{offset, "Z"}]),
BExtMessID = mess_id_to_external_binary(MessID),
Packet1 = mod_mam_utils:maybe_set_client_xmlns(SetClientNs, Packet),
wrap_message(MamNs, Packet1, QueryID, BExtMessID, TS, SrcJID).
Expand Down
2 changes: 1 addition & 1 deletion src/mam/mod_mam_muc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ archive_message(Host, Params) ->
message_row_to_xml(MamNs, ReceiverJID, HideUser, SetClientNs, {MessID, SrcJID, Packet}, QueryID) ->

{Microseconds, _NodeMessID} = decode_compact_uuid(MessID),
TS = calendar:system_time_to_rfc3339(usec:to_sec(Microseconds), [{offset, "Z"}]),
TS = calendar:system_time_to_rfc3339(erlang:convert_time_unit(Microseconds, microsecond, second), [{offset, "Z"}]),
BExtMessID = mess_id_to_external_binary(MessID),
Packet1 = maybe_delete_x_user_element(HideUser, ReceiverJID, Packet),
Packet2 = mod_mam_utils:maybe_set_client_xmlns(SetClientNs, Packet1),
Expand Down
3 changes: 1 addition & 2 deletions src/mam/mod_mam_riak_timed_arch_yz.erl
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ archive_size(_Size, Host, _ArchiveID, ArchiveJID) ->
{binary(), binary()} | undefined.
bucket(Host, MsgId) when is_integer(MsgId) ->
{MicroSec, _} = mod_mam_utils:decode_compact_uuid(MsgId),
MsgNow = usec:to_now(MicroSec),
{MsgDate, _} = calendar:now_to_datetime(MsgNow),
{MsgDate, _} = calendar:system_time_to_universal_time(MicroSec, microsecond),
bucket(Host, MsgDate);
bucket(Host, {_, _, _} = Date) ->
bucket(Host, calendar:iso_week_number(Date));
Expand Down
1 change: 0 additions & 1 deletion src/mongooseim.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
jid,
fast_tls,
tirerl,
usec,
uuid,
xmerl,
worker_pool,
Expand Down
Loading