@@ -983,7 +983,7 @@ which_module(3) -> ?MODULE.
983
983
capacity :: term (),
984
984
gc = # aux_gc {} :: # aux_gc {},
985
985
tick_pid ,
986
- unused2 }).
986
+ cache = #{} :: map () }).
987
987
988
988
init_aux (Name ) when is_atom (Name ) ->
989
989
% % TODO: catch specific exception throw if table already exists
@@ -1102,21 +1102,31 @@ handle_aux(_RaState, cast, tick, #?AUX{name = Name,
1102
1102
handle_aux (_RaState , cast , eol , #? AUX {name = Name } = Aux , Log , _ ) ->
1103
1103
ets :delete (rabbit_fifo_usage , Name ),
1104
1104
{no_reply , Aux , Log };
1105
- handle_aux (_RaState , {call , _From }, oldest_entry_timestamp , Aux ,
1105
+ handle_aux (_RaState , {call , _From }, oldest_entry_timestamp ,
1106
+ #? AUX {cache = Cache } = Aux0 ,
1106
1107
Log0 , #? MODULE {} = State ) ->
1107
- {Ts , Log } = case smallest_raft_index (State ) of
1108
- % % if there are no entries, we return current timestamp
1109
- % % so that any previously obtained entries are considered
1110
- % % older than this
1111
- undefined ->
1112
- {erlang :system_time (millisecond ), Log0 };
1113
- Idx when is_integer (Idx ) ->
1114
- % % TODO: make more defensive to avoid potential crash
1115
- {{_ , _ , {_ , Meta , _ , _ }}, Log1 } = ra_log :fetch (Idx , Log0 ),
1116
- #{ts := Timestamp } = Meta ,
1117
- {Timestamp , Log1 }
1118
- end ,
1119
- {reply , {ok , Ts }, Aux , Log };
1108
+ {CachedIdx , CachedTs } = maps :get (oldest_entry , Cache , {undefined , undefined }),
1109
+ case smallest_raft_index (State ) of
1110
+ % % if there are no entries, we return current timestamp
1111
+ % % so that any previously obtained entries are considered
1112
+ % % older than this
1113
+ undefined ->
1114
+ Aux1 = Aux0 #? AUX {cache = maps :remove (oldest_entry , Cache )},
1115
+ {reply , {ok , erlang :system_time (millisecond )}, Aux1 , Log0 };
1116
+ CachedIdx ->
1117
+ % % cache hit
1118
+ {reply , {ok , CachedTs }, Aux0 , Log0 };
1119
+ Idx when is_integer (Idx ) ->
1120
+ case ra_log :fetch (Idx , Log0 ) of
1121
+ {{_ , _ , {_ , #{ts := Timestamp }, _ , _ }}, Log1 } ->
1122
+ Aux1 = Aux0 #? AUX {cache = Cache #{oldest_entry =>
1123
+ {Idx , Timestamp }}},
1124
+ {reply , {ok , Timestamp }, Aux1 , Log1 };
1125
+ {undefined , Log1 } ->
1126
+ % % fetch failed
1127
+ {reply , {error , failed_to_get_timestamp }, Aux0 , Log1 }
1128
+ end
1129
+ end ;
1120
1130
handle_aux (_RaState , {call , _From }, {peek , Pos }, Aux0 ,
1121
1131
Log0 , MacState ) ->
1122
1132
case rabbit_fifo :query_peek (Pos , MacState ) of
0 commit comments