16
16
-include (" rabbit_peer_discovery_consul.hrl" ).
17
17
18
18
-export ([init /0 , list_nodes /0 , supports_registration /0 , register /0 , unregister /0 ,
19
- post_registration /0 , lock /1 , unlock /1 ]).
19
+ post_registration /0 , lock /2 , unlock /2 ,
20
+ pre_discovery /0 , post_discovery /1 ]).
20
21
-export ([send_health_check_pass /0 ]).
21
22
-export ([session_ttl_update_callback /1 ]).
22
23
% % for debugging from the REPL
23
24
-export ([service_id /0 , service_address /0 ]).
24
25
% % for debugging from the REPL
25
26
-export ([http_options /1 , http_options /2 ]).
26
27
28
+ -type backend_priv () :: {Config :: #{atom () => peer_discovery_config_value ()},
29
+ SessionId :: string (),
30
+ TRef :: timer :tref ()}.
31
+
27
32
% % for tests
28
33
-ifdef (TEST ).
29
34
-compile (export_all ).
@@ -160,10 +165,9 @@ post_registration() ->
160
165
send_health_check_pass (),
161
166
ok .
162
167
163
- -spec lock (Nodes :: [node ()]) ->
164
- {ok , Data :: term ()} | {error , Reason :: string ()}.
168
+ -spec pre_discovery () -> {ok , backend_priv ()} | {error , string ()}.
165
169
166
- lock ( _Nodes ) ->
170
+ pre_discovery ( ) ->
167
171
M = ? CONFIG_MODULE :config_map (? BACKEND_CONFIG_KEY ),
168
172
? LOG_DEBUG (
169
173
" Effective Consul peer discovery configuration: ~tp " , [M ],
@@ -172,21 +176,33 @@ lock(_Nodes) ->
172
176
case create_session (Node , get_config_key (consul_svc_ttl , M )) of
173
177
{ok , SessionId } ->
174
178
TRef = start_session_ttl_updater (SessionId ),
175
- Now = erlang :system_time (seconds ),
176
- EndTime = Now + get_config_key (lock_wait_time , M ),
177
- lock (TRef , SessionId , Now , EndTime );
179
+ {ok , {M , SessionId , TRef }};
178
180
{error , Reason } ->
179
181
{error , lists :flatten (io_lib :format (" Error while creating a session, reason: ~ts " ,
180
182
[Reason ]))}
181
183
end .
182
184
183
- -spec unlock ({ SessionId :: string (), TRef :: timer : tref ()} ) -> ok .
185
+ -spec post_discovery ( backend_priv () ) -> ok .
184
186
185
- unlock ({ SessionId , TRef }) ->
187
+ post_discovery ({ _M , SessionId , TRef }) ->
186
188
_ = timer :cancel (TRef ),
189
+ delete_session (SessionId ),
187
190
? LOG_DEBUG (
188
191
" Stopped session renewal" ,
189
192
#{domain => ? RMQLOG_DOMAIN_PEER_DIS }),
193
+ ok .
194
+
195
+ -spec lock (Nodes :: [node ()], BackendPriv :: backend_priv ()) ->
196
+ {ok , ok } | {error , Reason :: string ()}.
197
+
198
+ lock (_Nodes , {M , SessionId , _TRef }) ->
199
+ Now = erlang :system_time (seconds ),
200
+ EndTime = Now + get_config_key (lock_wait_time , M ),
201
+ lock (SessionId , Now , EndTime ).
202
+
203
+ -spec unlock (LockData :: ok , backend_priv ()) -> ok .
204
+
205
+ unlock (ok , {_M , SessionId , _TRef }) ->
190
206
case release_lock (SessionId ) of
191
207
{ok , true } ->
192
208
ok ;
@@ -613,7 +629,11 @@ create_session(Name, TTL) ->
613
629
[{'Name' , Name },
614
630
{'TTL' , rabbit_data_coercion :to_atom (service_ttl (TTL ))}]) of
615
631
{ok , Response } ->
616
- {ok , get_session_id (Response )};
632
+ SessionId = get_session_id (Response ),
633
+ ? LOG_DEBUG (
634
+ " Consul created session ID: ~s " , [SessionId ],
635
+ #{domain => ? RMQLOG_DOMAIN_PEER_DIS }),
636
+ {ok , SessionId };
617
637
{error , _ } = Err ->
618
638
Err
619
639
end .
@@ -645,6 +665,31 @@ consul_session_create(Query, Headers, Body) ->
645
665
Err
646
666
end .
647
667
668
+ % %--------------------------------------------------------------------
669
+ % % @private
670
+ % % @doc
671
+ % % Delete a session
672
+ % % @end
673
+ % %--------------------------------------------------------------------
674
+ -spec delete_session (string ()) -> ok .
675
+ delete_session (SessionId ) ->
676
+ M = ? CONFIG_MODULE :config_map (? BACKEND_CONFIG_KEY ),
677
+ Headers = maybe_add_acl ([]),
678
+ HttpOpts = http_options (M ),
679
+ Ret = rabbit_peer_discovery_httpc :put (
680
+ get_config_key (consul_scheme , M ),
681
+ get_config_key (consul_host , M ),
682
+ get_integer_config_key (consul_port , M ),
683
+ " v1/session/destroy/" ++ SessionId ,
684
+ [],
685
+ Headers ,
686
+ HttpOpts ,
687
+ <<>>),
688
+ ? LOG_DEBUG (
689
+ " Consul deleted session: ~p " , [Ret ],
690
+ #{domain => ? RMQLOG_DOMAIN_PEER_DIS }),
691
+ ok .
692
+
648
693
% %--------------------------------------------------------------------
649
694
% % @private
650
695
% % @doc
@@ -692,31 +737,27 @@ start_session_ttl_updater(SessionId) ->
692
737
% % Tries to acquire lock. If the lock is held by someone else, waits until it
693
738
% % is released, or too much time has passed
694
739
% % @end
695
- -spec lock (timer :tref (), string (), pos_integer (), pos_integer ()) -> {ok , string ()} | {error , string ()}.
696
- lock (TRef , _ , Now , EndTime ) when EndTime < Now ->
697
- _ = timer :cancel (TRef ),
740
+ -spec lock (string (), pos_integer (), pos_integer ()) -> {ok , ok } | {error , string ()}.
741
+ lock (_ , Now , EndTime ) when EndTime < Now ->
698
742
{error , " Acquiring lock taking too long, bailing out" };
699
- lock (TRef , SessionId , _ , EndTime ) ->
743
+ lock (SessionId , _ , EndTime ) ->
700
744
case acquire_lock (SessionId ) of
701
745
{ok , true } ->
702
- {ok , { SessionId , TRef } };
746
+ {ok , ok };
703
747
{ok , false } ->
704
748
case get_lock_status () of
705
749
{ok , {SessionHeld , ModifyIndex }} ->
706
750
Wait = max (EndTime - erlang :system_time (seconds ), 0 ),
707
751
case wait_for_lock_release (SessionHeld , ModifyIndex , Wait ) of
708
752
ok ->
709
- lock (TRef , SessionId , erlang :system_time (seconds ), EndTime );
753
+ lock (SessionId , erlang :system_time (seconds ), EndTime );
710
754
{error , Reason } ->
711
- _ = timer :cancel (TRef ),
712
755
{error , lists :flatten (io_lib :format (" Error waiting for lock release, reason: ~ts " ,[Reason ]))}
713
756
end ;
714
757
{error , Reason } ->
715
- _ = timer :cancel (TRef ),
716
758
{error , lists :flatten (io_lib :format (" Error obtaining lock status, reason: ~ts " , [Reason ]))}
717
759
end ;
718
760
{error , Reason } ->
719
- _ = timer :cancel (TRef ),
720
761
{error , lists :flatten (io_lib :format (" Error while acquiring lock, reason: ~ts " , [Reason ]))}
721
762
end .
722
763
0 commit comments