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 5 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
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
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
12 changes: 6 additions & 6 deletions src/pubsub/mod_pubsub.erl
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@
-type(pubsubItem() ::
#pubsub_item{
itemid :: {ItemId::mod_pubsub:itemId(), Nidx::mod_pubsub:nodeIdx()},
creation :: {erlang:timestamp(), jid:ljid()},
modification :: {erlang:timestamp(), jid:ljid()},
creation :: {integer(), jid:ljid()},
modification :: {integer(), jid:ljid()},
payload :: mod_pubsub:payload()
}
).
Expand All @@ -188,7 +188,7 @@
#pubsub_last_item{
nodeid :: mod_pubsub:nodeIdx(),
itemid :: mod_pubsub:itemId(),
creation :: {erlang:timestamp(), jid:ljid()},
creation :: {integer(), jid:ljid()},
payload :: mod_pubsub:payload()
}
).
Expand Down Expand Up @@ -3471,7 +3471,7 @@ items_event_stanza(Node, Items) ->
case Items of
[LastItem] ->
{ModifNow, ModifUSR} = LastItem#pubsub_item.modification,
Sec = usec:to_sec(usec:from_now(ModifNow)),
Sec = erlang:convert_time_unit(ModifNow, microsecond, second),
TString = calendar:system_time_to_rfc3339(Sec, [{offset, "Z"}]),
[#xmlel{name = <<"delay">>,
attrs = [{<<"xmlns">>, ?NS_DELAY},
Expand Down Expand Up @@ -4470,7 +4470,7 @@ string_to_ljid(JID) ->

-spec uniqid() -> mod_pubsub:itemId().
uniqid() ->
{T1, T2, T3} = timestamp(),
{T1, T2, T3} = os:timestamp(),
iolist_to_binary(io_lib:fwrite("~.16B~.16B~.16B", [T1, T2, T3])).
chrzaszcz marked this conversation as resolved.
Show resolved Hide resolved

node_attr(Node) -> [{<<"node">>, Node}].
Expand Down Expand Up @@ -4620,7 +4620,7 @@ purge_item_of_offline_user(Host, #pubsub_node{ id = Nidx, nodeid = {_, NodeId},
end.

timestamp() ->
os:timestamp().
os:system_time(microsecond).

make_error_reply(#iq{ sub_el = SubEl } = IQ, #xmlel{} = ErrorEl) ->
IQ#iq{type = error, sub_el = [ErrorEl, SubEl]};
Expand Down
2 changes: 1 addition & 1 deletion src/pubsub/mod_pubsub_cache_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ stop() ->
Publisher :: jid:jid(),
Payload::mod_pubsub:payload()) -> ok | {error, Reason :: term()}.
upsert_last_item(_ServerHost, Nidx, ItemId, Publisher, Payload) ->
CreatedAt = os:timestamp(),
CreatedAt = os:system_time(microsecond),
try mnesia:dirty_write(
#pubsub_last_item{
nodeid = Nidx,
Expand Down
2 changes: 1 addition & 1 deletion src/pubsub/mod_pubsub_cache_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ prepare_upsert_params(Publisher, Payload) ->
item_to_record({NodeIdx, ItemId, CreatedLUser, CreatedAt, CreatedLServer, PayloadDB}) ->
PayloadXML = mongoose_rdbms:unescape_binary(global, PayloadDB),
{ok, #xmlel{children = Payload}} = exml:parse(PayloadXML),
Creation = {usec:to_now(mongoose_rdbms:result_to_integer(CreatedAt)),
Creation = {mongoose_rdbms:result_to_integer(CreatedAt),
{CreatedLUser, CreatedLServer, <<>>}},
#pubsub_last_item{itemid = ItemId,
nodeid = NodeIdx,
Expand Down
8 changes: 4 additions & 4 deletions src/pubsub/mod_pubsub_db_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ set_item(#pubsub_item{itemid = {ItemId, NodeIdx},
payload = Payload}) ->
PayloadWrapped = #xmlel{name = <<"item">>, children = Payload},
PayloadXML = exml:to_binary(PayloadWrapped),
CreatedAt = usec:from_now(CreatedAtNow),
ModifiedAt = usec:from_now(ModifiedAtNow),
CreatedAt = CreatedAtNow,
ModifiedAt = ModifiedAtNow,
Publisher = null_or_bin_jid(PublisherIn),
InsertParams = [NodeIdx, ItemId, CreatedLUser, CreatedLServer, CreatedAt,
ModifiedLUser, ModifiedLServer, ModifiedLResource, ModifiedAt,
Expand Down Expand Up @@ -752,9 +752,9 @@ item_to_record({NodeIdx, ItemId, CreatedLUser, CreatedLServer, CreatedAt,
PayloadXML = mongoose_rdbms:unescape_binary(global, PayloadDB),
{ok, #xmlel{children = Payload}} = exml:parse(PayloadXML),
ItemAndNodeId = {ItemId, mongoose_rdbms:result_to_integer(NodeIdx)},
Creation = {usec:to_now(mongoose_rdbms:result_to_integer(CreatedAt)),
Creation = {mongoose_rdbms:result_to_integer(CreatedAt),
{CreatedLUser, CreatedLServer, <<>>}},
Modification = {usec:to_now(mongoose_rdbms:result_to_integer(ModifiedAt)),
Modification = {mongoose_rdbms:result_to_integer(ModifiedAt),
{ModifiedLUser, ModifiedLServer, ModifiedLResource}},
Publisher = decode_publisher(PublisherIn),
#pubsub_item{itemid = ItemAndNodeId,
Expand Down
2 changes: 1 addition & 1 deletion src/pubsub/node_flat.erl
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ publish_item(_ServerHost, Nidx, Publisher, PublishModel, MaxItems, ItemId, ItemP
true ->
case MaxItems > 0 of
true ->
Now = os:timestamp(),
Now = os:system_time(microsecond),
Item = make_pubsub_item(Nidx, ItemId, Now, SubKey, GenKey,
Payload, Publisher, ItemPublisher),
Items = [ItemId | GenState#pubsub_state.items -- [ItemId]],
Expand Down
5 changes: 2 additions & 3 deletions src/pubsub/pubsub_form_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ convert_value_from_binaries([Bin], boolean) ->
convert_value_from_binaries([Bin], integer) ->
binary_to_integer(Bin);
convert_value_from_binaries([Bin], datetime) ->
usec:to_now(calendar:rfc3339_to_system_time(binary_to_list(Bin), [{unit, microsecond}]));
calendar:rfc3339_to_system_time(binary_to_list(Bin), [{unit, microsecond}]);
convert_value_from_binaries(Bins, list) when is_list(Bins) ->
Bins.

Expand All @@ -215,8 +215,7 @@ convert_value_to_binaries(Value, boolean) ->
convert_value_to_binaries(Value, integer) ->
[integer_to_binary(Value)];
convert_value_to_binaries(Value, datetime) ->
USec = usec:from_now(Value),
TS = calendar:system_time_to_rfc3339(USec, [{offset, "Z"}, {unit, microsecond}]),
TS = calendar:system_time_to_rfc3339(Value, [{offset, "Z"}, {unit, microsecond}]),
list_to_binary(TS);
convert_value_to_binaries(Value, list) when is_list(Value) ->
Value.
Expand Down
12 changes: 6 additions & 6 deletions src/shaper_srv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ handle_call({wait, Host, Action, FromJID, Size},
From, State=#state{max_delay=MaxDelayMs}) ->
Key = new_key(Host, Action, FromJID),
Shaper = find_or_create_shaper(Key, State),
State1 = update_access_time(Key, erlang:timestamp(), State),
State1 = update_access_time(Key, erlang:system_time(), State),
chrzaszcz marked this conversation as resolved.
Show resolved Hide resolved
case shaper:update(Shaper, Size) of
{UpdatedShaper, 0} ->
{reply, ok, save_shaper(Key, UpdatedShaper, State1)};
Expand Down Expand Up @@ -188,7 +188,7 @@ init_dicts(State) ->

-spec delete_old_shapers(state()) -> state().
delete_old_shapers(State=#state{shapers=Shapers, a_times=Times, ttl=TTL}) ->
Min = subtract_seconds(erlang:timestamp(), TTL),
Min = subtract_seconds(TTL),
%% Copy recently modified shapers
dict:fold(fun
(_, ATime, Acc) when ATime < Min -> Acc; %% skip too old
Expand Down Expand Up @@ -230,7 +230,7 @@ reply_after(DelayMs, {Pid, Tag}, Reply) ->
erlang:send_after(DelayMs, Pid, {Tag, Reply}).


-spec subtract_seconds(erlang:timestamp(), non_neg_integer()) -> erlang:timestamp().
subtract_seconds({MegaSecs, Secs, MicroSecs}, SubSecs) ->
{MegaSecs - (SubSecs div 1000000), Secs - (SubSecs rem 1000000), MicroSecs}.

-spec subtract_seconds(integer()) -> integer().
subtract_seconds(TTL) ->
TimestampThreshold = erlang:system_time(second) - TTL,
erlang:convert_time_unit(TimestampThreshold, second, native).