1
1
% %--------------------------------------------------------------------
2
- % % Copyright (c) 2021-2023 EMQ Technologies Co., Ltd. All Rights Reserved.
2
+ % % Copyright (c) 2021-2025 EMQ Technologies Co., Ltd. All Rights Reserved.
3
3
% %
4
4
% % Licensed under the Apache License, Version 2.0 (the "License");
5
5
% % you may not use this file except in compliance with the License.
24
24
25
25
% % API:
26
26
-export ([start_link /0 ,
27
+ notify_rpc_target_up /2 , notify_rpc_target_down /1 , rpc_target /2 ,
27
28
notify_shard_up /2 , notify_shard_down /1 , wait_for_shards /2 ,
28
- notify_core_node_up /2 , notify_core_node_down /1 , get_core_node /2 ,
29
+ notify_core_node_up /2 , notify_core_node_down /1 , replica_get_core_node /2 ,
29
30
notify_core_intercept_trans /2 ,
30
31
31
32
upstream /1 , upstream_node /1 ,
57
58
-include (" mria_rlog.hrl" ).
58
59
-include_lib (" snabbkaffe/include/trace.hrl" ).
59
60
60
- % % Tables and table keys :
61
+ % % Optvars :
61
62
-define (optvar (KEY ), {mria , KEY }).
62
63
-define (upstream_pid , upstream_pid ).
63
64
-define (core_node , core_node ).
65
+ -define (rpc_target , rpc_target ).
64
66
67
+ % % Tables and table keys:
65
68
-define (stats_tab , mria_rlog_stats_tab ).
66
69
-define (core_intercept , core_intercept ).
67
70
-define (replicant_state , replicant_state ).
80
83
start_link () ->
81
84
gen_server :start_link ({local , ? SERVER }, ? MODULE , [], []).
82
85
83
- % % @doc Return name of the core node that is _currently serving_ the
84
- % % downstream shard. Note the difference in behavior as compared with
85
- % % `get_core_node'. Returns `disconnected' if the local replica of the
86
- % % shard is down.
86
+ % % @doc Return name of the core node that can serve as the RPC target.
87
+ % % It is the same node that serves the local replica, but this optvar
88
+ % % is set before local replica goes up fully. WARNING: this `optvar'
89
+ % % is set before local replica becomes consistent.
90
+ -spec rpc_target (mria_rlog :shard (), timeout ()) -> {ok , node ()} | disconnected .
91
+ rpc_target (Shard , Timeout ) ->
92
+ case optvar :read (? optvar ({? rpc_target , Shard }), Timeout ) of
93
+ OK = {ok , _ } ->
94
+ OK ;
95
+ timeout ->
96
+ disconnected
97
+ end .
98
+
99
+ -spec notify_rpc_target_up (mria_rlog :shard (), node ()) -> ok .
100
+ notify_rpc_target_up (Shard , Upstream ) ->
101
+ do_notify_up (? rpc_target , Shard , Upstream ).
102
+
103
+ -spec notify_rpc_target_down (mria_rlog :shard ()) -> ok .
104
+ notify_rpc_target_down (Shard ) ->
105
+ do_notify_down (? rpc_target , Shard ).
106
+
107
+ % % @doc Return name of the core node that is currently serving the
108
+ % % downstream shard. In contrast with `rpc_target', this optvar is set
109
+ % % when the shard reaches `normal' state and local reads become
110
+ % % consistent.
87
111
-spec upstream_node (mria_rlog :shard ()) -> {ok , node ()} | disconnected .
88
112
upstream_node (Shard ) ->
89
113
case upstream (Shard ) of
90
114
{ok , Pid } -> {ok , node (Pid )};
91
115
disconnected -> disconnected
92
116
end .
93
117
94
- % % @doc Return pid of the core node agent that serves us.
118
+ % % @doc Return pid of the core node agent that serves us (when shard
119
+ % % is in `normal' state).
95
120
-spec upstream (mria_rlog :shard ()) -> {ok , pid ()} | disconnected .
96
121
upstream (Shard ) ->
97
122
case optvar :peek (? optvar ({? upstream_pid , Shard })) of
98
123
{ok , Pid } -> {ok , Pid };
99
124
undefined -> disconnected
100
125
end .
101
126
102
- % % @doc Return a core node that _might_ be able to serve the specified
103
- % % shard.
104
- -spec get_core_node (mria_rlog :shard (), timeout ()) -> {ok , node ()} | timeout .
105
- get_core_node (Shard , Timeout ) ->
127
+ % % @doc WARNING: this optvar is used STRICTLY for interaction between
128
+ % % `mria_lb' and `mria_replica' FSM. Its value is equal to core node
129
+ % % that serves minimal number of replicants. As such, it must NOT be
130
+ % % used for RPC targeting: all RPCs from the entire cluster will end
131
+ % % up on a single node.
132
+ -spec replica_get_core_node (mria_rlog :shard (), timeout ()) -> {ok , node ()} | timeout .
133
+ replica_get_core_node (Shard , Timeout ) ->
106
134
optvar :read (? optvar ({? core_node , Shard }), Timeout ).
107
135
136
+ -spec notify_core_node_up (mria_rlog :shard (), node ()) -> ok .
137
+ notify_core_node_up (Shard , Node ) ->
138
+ do_notify_up (? core_node , Shard , Node ).
139
+
140
+ -spec notify_core_node_down (mria_rlog :shard ()) -> ok .
141
+ notify_core_node_down (Shard ) ->
142
+ do_notify_down (? core_node , Shard ).
143
+
108
144
-spec notify_shard_up (mria_rlog :shard (), _AgentPid :: pid ()) -> ok .
109
145
notify_shard_up (Shard , Upstream ) ->
110
146
do_notify_up (? upstream_pid , Shard , Upstream ).
@@ -122,14 +158,6 @@ notify_shard_down(Shard) ->
122
158
? replicant_bootstrap_import
123
159
]).
124
160
125
- -spec notify_core_node_up (mria_rlog :shard (), node ()) -> ok .
126
- notify_core_node_up (Shard , Node ) ->
127
- do_notify_up (? core_node , Shard , Node ).
128
-
129
- -spec notify_core_node_down (mria_rlog :shard ()) -> ok .
130
- notify_core_node_down (Shard ) ->
131
- do_notify_down (? core_node , Shard ).
132
-
133
161
-spec notify_core_intercept_trans (mria_rlog :shard (), mria_rlog :seqno ()) -> ok .
134
162
notify_core_intercept_trans (Shard , SeqNo ) ->
135
163
set_stat (Shard , ? core_intercept , SeqNo ).
0 commit comments