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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (the "License"); you may not use this file except in
%% compliance with the License. You may obtain a copy of the License at
%% http://www.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
%% License for the specific language governing rights and limitations
%% under the License.
%%
%% The Original Code is RabbitMQ.
%%
%% The Initial Developer of the Original Code is VMware, Inc.
%% Copyright (c) 2007-2010 VMware, Inc. All rights reserved.
%%
-module(rabbit_mirror_queue_master).
-export([init/2, terminate/1, delete_and_terminate/1,
purge/1, publish/4, publish_delivered/5, fetch/2, ack/2,
tx_publish/5, tx_ack/3, tx_rollback/2, tx_commit/4,
requeue/3, len/1, is_empty/1, dropwhile/2,
set_ram_duration_target/2, ram_duration/1,
needs_idle_timeout/1, idle_timeout/1, handle_pre_hibernate/1,
status/1]).
-export([start/1, stop/0]).
-export([promote_backing_queue_state/5]).
-behaviour(rabbit_backing_queue).
-include("rabbit.hrl").
-record(state, { gm,
coordinator,
backing_queue,
backing_queue_state,
set_delivered,
fakes
}).
%% ---------------------------------------------------------------------------
%% Backing queue
%% ---------------------------------------------------------------------------
start(_DurableQueues) ->
%% This will never get called as this module will never be
%% installed as the default BQ implementation.
exit({not_valid_for_generic_backing_queue, ?MODULE}).
stop() ->
%% Same as start/1.
exit({not_valid_for_generic_backing_queue, ?MODULE}).
init(#amqqueue { arguments = Args } = Q, Recover) ->
{ok, CPid} = rabbit_mirror_queue_coordinator:start_link(Q, undefined),
GM = rabbit_mirror_queue_coordinator:get_gm(CPid),
{_Type, Nodes} = rabbit_misc:table_lookup(Args, <<"x-mirror">>),
[rabbit_mirror_queue_coordinator:add_slave(CPid, binary_to_atom(Node, utf8))
|| {longstr, Node} <- Nodes],
{ok, BQ} = application:get_env(backing_queue_module),
BQS = BQ:init(Q, Recover),
#state { gm = GM,
coordinator = CPid,
backing_queue = BQ,
backing_queue_state = BQS,
set_delivered = 0,
fakes = sets:new() }.
promote_backing_queue_state(CPid, BQ, BQS, GM, Fakes) ->
#state { gm = GM,
coordinator = CPid,
backing_queue = BQ,
backing_queue_state = BQS,
set_delivered = BQ:len(BQS),
fakes = Fakes }.
terminate(State = #state { backing_queue = BQ, backing_queue_state = BQS }) ->
%% Backing queue termination. The queue is going down but
%% shouldn't be deleted. Most likely safe shutdown of this
%% node. Thus just let some other slave take over.
State #state { backing_queue_state = BQ:terminate(BQS) }.
delete_and_terminate(State = #state { gm = GM,
backing_queue = BQ,
backing_queue_state = BQS }) ->
ok = gm:broadcast(GM, delete_and_terminate),
State #state { backing_queue_state = BQ:delete_and_terminate(BQS),
set_delivered = 0 }.
purge(State = #state { gm = GM,
backing_queue = BQ,
backing_queue_state = BQS }) ->
ok = gm:broadcast(GM, {set_length, 0}),
{Count, BQS1} = BQ:purge(BQS),
{Count, State #state { backing_queue_state = BQS1,
set_delivered = 0 }}.
publish(Msg = #basic_message { guid = Guid },
MsgProps, ChPid, State = #state { gm = GM,
backing_queue = BQ,
backing_queue_state = BQS }) ->
ok = gm:broadcast(GM, {publish, false, Guid, MsgProps, ChPid}),
BQS1 = BQ:publish(Msg, MsgProps, ChPid, BQS),
State #state { backing_queue_state = BQS1 }.
publish_delivered(AckRequired, Msg = #basic_message { guid = Guid },
MsgProps, ChPid,
State = #state { gm = GM,
backing_queue = BQ,
backing_queue_state = BQS }) ->
ok = gm:broadcast(GM, {publish, {true, AckRequired}, Guid, MsgProps, ChPid}),
{AckTag, BQS1} = BQ:publish_delivered(AckRequired, Msg, MsgProps, ChPid, BQS),
{AckTag, State #state { backing_queue_state = BQS1 }}.
dropwhile(Fun, State = #state { gm = GM,
backing_queue = BQ,
backing_queue_state = BQS,
set_delivered = SetDelivered }) ->
Len = BQ:len(BQS),
BQS1 = BQ:dropwhile(Fun, BQS),
Dropped = Len - BQ:len(BQS1),
SetDelivered1 = lists:max([0, SetDelivered - Dropped]),
ok = gm:broadcast(GM, {set_length, BQ:len(BQS1)}),
State #state { backing_queue_state = BQS1,
set_delivered = SetDelivered1 }.
fetch(AckRequired, State = #state { gm = GM,
backing_queue = BQ,
backing_queue_state = BQS,
set_delivered = SetDelivered,
fakes = Fakes }) ->
{Result, BQS1} = BQ:fetch(AckRequired, BQS),
case Result of
empty ->
{Result, State #state { backing_queue_state = BQS1 }};
{#basic_message { guid = Guid } = Message, IsDelivered, AckTag,
Remaining} ->
SetDelivered1 = lists:max([0, SetDelivered - 1]),
case sets:is_element(Guid, Fakes) of
true ->
{BQS2, Fakes1} =
case AckRequired of
true -> {[Guid], BQS3} = BQ:ack([AckTag], BQS1),
{BQS3, Fakes};
false -> {BQS1, sets:del_element(Guid, Fakes)}
end,
ok = gm:broadcast(GM, {fetch, false, Guid, Remaining}),
fetch(AckRequired,
State #state { backing_queue_state = BQS2,
set_delivered = SetDelivered1,
fakes = Fakes1 });
false ->
ok = gm:broadcast(GM,
{fetch, AckRequired, Guid, Remaining}),
IsDelivered1 = IsDelivered orelse SetDelivered > 0,
Fakes1 = case SetDelivered + SetDelivered1 of
1 -> sets:new(); %% transition to 0
_ -> Fakes
end,
{{Message, IsDelivered1, AckTag, Remaining},
State #state { backing_queue_state = BQS1,
set_delivered = SetDelivered1,
fakes = Fakes1 }}
end
end.
ack(AckTags, State = #state { gm = GM,
backing_queue = BQ,
backing_queue_state = BQS,
fakes = Fakes }) ->
{Guids, BQS1} = BQ:ack(AckTags, BQS),
Fakes1 = case Guids of
[] -> Fakes;
_ -> ok = gm:broadcast(GM, {ack, Guids}),
sets:difference(Fakes, sets:from_list(Guids))
end,
{Guids, State #state { backing_queue_state = BQS1, fakes = Fakes1 }}.
tx_publish(Txn, Msg, MsgProps, ChPid, #state {} = State) ->
%% gm:broadcast(GM, {tx_publish, Txn, Guid, MsgProps, ChPid})
State.
tx_ack(Txn, AckTags, #state {} = State) ->
%% gm:broadcast(GM, {tx_ack, Txn, Guids})
State.
tx_rollback(Txn, #state {} = State) ->
%% gm:broadcast(GM, {tx_rollback, Txn})
{[], State}.
tx_commit(Txn, PostCommitFun, MsgPropsFun, #state {} = State) ->
%% Maybe don't want to transmit the MsgPropsFun but what choice do
%% we have? OTOH, on the slaves, things won't be expiring on their
%% own (props are interpreted by amqqueue, not vq), so if the msg
%% props aren't quite the same, that doesn't matter.
%%
%% The PostCommitFun is actually worse - we need to prevent that
%% from being invoked until we have confirmation from all the
%% slaves that they've done everything up to there.
%%
%% In fact, transactions are going to need work seeing as it's at
%% this point that VQ mentions amqqueue, which will thus not work
%% on the slaves - we need to make sure that all the slaves do the
%% tx_commit_post_msg_store at the same point, and then when they
%% all confirm that (scatter/gather), we can finally invoke the
%% PostCommitFun.
%%
%% Another idea is that the slaves are actually driven with
%% pubacks and thus only the master needs to support txns
%% directly.
{[], State}.
requeue(AckTags, MsgPropsFun, State = #state { gm = GM,
backing_queue = BQ,
backing_queue_state = BQS }) ->
{Guids, BQS1} = BQ:requeue(AckTags, MsgPropsFun, BQS),
ok = gm:broadcast(GM, {requeue, MsgPropsFun, Guids}),
{Guids, State #state { backing_queue_state = BQS1 }}.
len(#state { backing_queue = BQ, backing_queue_state = BQS}) ->
BQ:len(BQS).
is_empty(#state { backing_queue = BQ, backing_queue_state = BQS}) ->
BQ:is_empty(BQS).
set_ram_duration_target(Target, State = #state { backing_queue = BQ,
backing_queue_state = BQS}) ->
State #state { backing_queue_state =
BQ:set_ram_duration_target(Target, BQS) }.
ram_duration(State = #state { backing_queue = BQ, backing_queue_state = BQS}) ->
{Result, BQS1} = BQ:ram_duration(BQS),
{Result, State #state { backing_queue_state = BQS1 }}.
needs_idle_timeout(#state { backing_queue = BQ, backing_queue_state = BQS}) ->
BQ:needs_idle_timeout(BQS).
idle_timeout(#state { backing_queue = BQ, backing_queue_state = BQS}) ->
BQ:idle_timeout(BQS).
handle_pre_hibernate(State = #state { backing_queue = BQ,
backing_queue_state = BQS}) ->
State #state { backing_queue_state = BQ:handle_pre_hibernate(BQS) }.
status(#state { backing_queue = BQ, backing_queue_state = BQS}) ->
BQ:status(BQS).
|