1%% This Source Code Form is subject to the terms of the Mozilla Public
2%% License, v. 2.0. If a copy of the MPL was not distributed with this
3%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4%%
5%% Copyright (c) 2007-2021 VMware, Inc. or its affiliates.  All rights reserved.
6%%
7
8-module(rabbit_fhc_helpers).
9
10-export([clear_read_cache/0]).
11
12-include("amqqueue.hrl").
13
14clear_read_cache() ->
15    case application:get_env(rabbit, fhc_read_buffering) of
16        {ok, true} ->
17            file_handle_cache:clear_read_cache(),
18            clear_vhost_read_cache(rabbit_vhost:list_names());
19        _ -> %% undefined or {ok, false}
20            ok
21    end.
22
23clear_vhost_read_cache([]) ->
24    ok;
25clear_vhost_read_cache([VHost | Rest]) ->
26    clear_queue_read_cache(rabbit_amqqueue:list(VHost)),
27    clear_vhost_read_cache(Rest).
28
29clear_queue_read_cache([]) ->
30    ok;
31clear_queue_read_cache([Q | Rest]) when ?is_amqqueue(Q) ->
32    MPid = amqqueue:get_pid(Q),
33    SPids = amqqueue:get_slave_pids(Q),
34    %% Limit the action to the current node.
35    Pids = [P || P <- [MPid | SPids], node(P) =:= node()],
36    %% This function is executed in the context of the backing queue
37    %% process because the read buffer is stored in the process
38    %% dictionary.
39    Fun = fun(_, State) ->
40                  _ = file_handle_cache:clear_process_read_cache(),
41                  State
42          end,
43    [rabbit_amqqueue:run_backing_queue(Pid, rabbit_variable_queue, Fun)
44     || Pid <- Pids],
45    clear_queue_read_cache(Rest).
46