summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_sharding/src/rabbit_sharding_interceptor.erl
blob: 4acba7882739bc9549a4347e81712b7128a25ef0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2020 VMware, Inc. or its affiliates.  All rights reserved.
%%

-module(rabbit_sharding_interceptor).

-include_lib("rabbit_common/include/rabbit_framing.hrl").

-behaviour(rabbit_channel_interceptor).

-export([description/0, intercept/3, applies_to/0, init/1]).

%% exported for tests
-export([consumer_count/1]).

-import(rabbit_sharding_util, [a2b/1, shards_per_node/1]).
-import(rabbit_misc, [r/3, format/2, protocol_error/3]).

-rabbit_boot_step({?MODULE,
                   [{description, "sharding interceptor"},
                    {mfa, {rabbit_registry, register,
                           [channel_interceptor,
                            <<"sharding interceptor">>, ?MODULE]}},
                    {cleanup, {rabbit_registry, unregister,
                               [channel_interceptor,
                                <<"sharding interceptor">>]}},
                    {requires, rabbit_registry},
                    {enables, recovery}]}).

init(Ch) ->
    rabbit_channel:get_vhost(Ch).

description() ->
    [{description, <<"Sharding interceptor for channel methods">>}].

intercept(#'basic.consume'{queue = QName} = Method, Content, VHost) ->
    case queue_name(VHost, QName) of
        {ok, QName2} ->
            {Method#'basic.consume'{queue = QName2}, Content};
        {error, QName} ->
            precondition_failed("Error finding sharded queue for: ~p", [QName])
    end;

intercept(#'basic.get'{queue = QName} = Method, Content, VHost) ->
    case queue_name(VHost, QName) of
        {ok, QName2} ->
            {Method#'basic.get'{queue = QName2}, Content};
        {error, QName} ->
            precondition_failed("Error finding sharded queue for: ~p", [QName])
    end;

intercept(#'queue.delete'{queue = QName} = Method, Content, VHost) ->
    case is_sharded(VHost, QName) of
        true ->
            precondition_failed("Can't delete sharded queue: ~p", [QName]);
        _    ->
            {Method, Content}
    end;

intercept(#'queue.declare'{queue = QName} = Method, Content, VHost) ->
    case is_sharded(VHost, QName) of
        true ->
            %% Since as an interceptor we can't modify what the channel
            %% will return, we then modify the queue name so the channel
            %% can at least return a queue.declare_ok for that particular
            %% queue. Picking the first queue over the others is totally
            %% arbitrary.
            QName2 = rabbit_sharding_util:make_queue_name(
                                      QName, a2b(node()), 0),
            {Method#'queue.declare'{queue = QName2}, Content};
        _    ->
            {Method, Content}
    end;

intercept(#'queue.bind'{queue = QName} = Method, Content, VHost) ->
    case is_sharded(VHost, QName) of
        true ->
            precondition_failed("Can't bind sharded queue: ~p", [QName]);
        _    ->
            {Method, Content}
    end;

intercept(#'queue.unbind'{queue = QName} = Method, Content, VHost) ->
    case is_sharded(VHost, QName) of
        true ->
            precondition_failed("Can't unbind sharded queue: ~p", [QName]);
        _    ->
            {Method, Content}
    end;

intercept(#'queue.purge'{queue = QName} = Method, Content, VHost) ->
    case is_sharded(VHost, QName) of
        true ->
            precondition_failed("Can't purge sharded queue: ~p", [QName]);
        _    ->
            {Method, Content}
    end;

intercept(Method, Content, _VHost) ->
    {Method, Content}.

applies_to() ->
    ['basic.consume', 'basic.get', 'queue.delete', 'queue.declare',
     'queue.bind', 'queue.unbind', 'queue.purge'].

%%----------------------------------------------------------------------------

%% If the queue is not part of a shard, return unmodified name
queue_name(VHost, QBin) ->
    case lookup_exchange(VHost, QBin) of
        {ok, X}  ->
            case rabbit_sharding_util:shard(X) of
                true ->
                    least_consumers(VHost, QBin, shards_per_node(X));
                _    ->
                    {ok, QBin}
            end;
        _Error ->
            {ok, QBin}
    end.

is_sharded(VHost, QBin) ->
    case lookup_exchange(VHost, QBin) of
        {ok, X} ->
            rabbit_sharding_util:shard(X);
        _Error ->
            false
    end.

lookup_exchange(VHost, QBin) ->
    rabbit_exchange:lookup(r(VHost, exchange, QBin)).

least_consumers(VHost, QBin, N) ->
    F = fun(QNum) ->
                QBin2 = rabbit_sharding_util:make_queue_name(
                          QBin, a2b(node()), QNum),
                case consumer_count(r(VHost, queue, QBin2)) of
                    {error, E}       -> {error, E};
                    [{consumers, C}] -> {C, QBin2}
                end

        end,
    case queues_with_count(F, N) of
        []     ->
            {error, QBin};
        Queues ->
            [{_, QBin3} | _ ] = lists:sort(Queues),
            {ok, QBin3}
    end.

queues_with_count(F, N) ->
    lists:foldl(fun (C, Acc) ->
                        case F(C) of
                            {error, _} -> Acc;
                            Ret        -> [Ret|Acc]
                        end
                end, [], lists:seq(0, N-1)).

consumer_count(QName) ->
    rabbit_amqqueue:with(
      QName,
      fun(Q) ->
              rabbit_amqqueue:info(Q, [consumers])
      end).

precondition_failed(Format, QName) ->
    protocol_error(precondition_failed, Format, QName).