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
- upstream /1 , upstream_node /2 ,
32
+ upstream /1 , upstream_node /1 ,
32
33
shards_status /0 , shards_up /0 , shards_syncing /0 , shards_down /0 ,
33
34
get_shard_stats /1 , agents /0 , agents /1 , replicants /0 , get_shard_lag /1 ,
34
35
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. If shard is currently down, wait until started
85
- % % for at most `Timeout' millisecond. Note the difference in behavior
86
- % % as compared with `get_core_node'.
87
- -spec upstream_node (mria_rlog :shard (), timeout ()) -> {ok , node ()} | timeout .
88
- upstream_node (Shard , Timeout ) ->
89
- optvar :read (? optvar ({? core_node , Shard }), Timeout ).
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.
111
+ -spec upstream_node (mria_rlog :shard ()) -> {ok , node ()} | disconnected .
112
+ upstream_node (Shard ) ->
113
+ case upstream (Shard ) of
114
+ {ok , Pid } -> {ok , node (Pid )};
115
+ disconnected -> disconnected
116
+ end .
90
117
91
- % % @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).
92
120
-spec upstream (mria_rlog :shard ()) -> {ok , pid ()} | disconnected .
93
121
upstream (Shard ) ->
94
122
case optvar :peek (? optvar ({? upstream_pid , Shard })) of
95
123
{ok , Pid } -> {ok , Pid };
96
124
undefined -> disconnected
97
125
end .
98
126
99
- % % @deprecated Return a core node that _might_ be able to serve the
100
- % % specified shard. WARNING: use of this function leads to unbalanced
101
- % % load of the core nodes.
102
- -spec get_core_node (mria_rlog :shard (), timeout ()) -> {ok , node ()} | timeout .
103
- 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 ) ->
104
134
optvar :read (? optvar ({? core_node , Shard }), Timeout ).
105
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
+
106
144
-spec notify_shard_up (mria_rlog :shard (), _AgentPid :: pid ()) -> ok .
107
145
notify_shard_up (Shard , Upstream ) ->
108
146
do_notify_up (? upstream_pid , Shard , Upstream ).
@@ -120,14 +158,6 @@ notify_shard_down(Shard) ->
120
158
? replicant_bootstrap_import
121
159
]).
122
160
123
- -spec notify_core_node_up (mria_rlog :shard (), node ()) -> ok .
124
- notify_core_node_up (Shard , Node ) ->
125
- do_notify_up (? core_node , Shard , Node ).
126
-
127
- -spec notify_core_node_down (mria_rlog :shard ()) -> ok .
128
- notify_core_node_down (Shard ) ->
129
- do_notify_down (? core_node , Shard ).
130
-
131
161
-spec notify_core_intercept_trans (mria_rlog :shard (), mria_rlog :seqno ()) -> ok .
132
162
notify_core_intercept_trans (Shard , SeqNo ) ->
133
163
set_stat (Shard , ? core_intercept , SeqNo ).
@@ -171,10 +201,10 @@ replicants() ->
171
201
172
202
-spec get_shard_lag (mria_rlog :shard ()) -> non_neg_integer () | disconnected .
173
203
get_shard_lag (Shard ) ->
174
- case {mria_config :role (), upstream_node (Shard , 0 )} of
204
+ case {mria_config :role (), upstream_node (Shard )} of
175
205
{core , _ } ->
176
206
0 ;
177
- {replicant , timeout } ->
207
+ {replicant , disconnected } ->
178
208
disconnected ;
179
209
{replicant , {ok , Upstream }} ->
180
210
RemoteSeqNo = erpc :call (Upstream , ? MODULE , get_stat , [Shard , ? core_intercept ], 1000 ),
@@ -242,7 +272,7 @@ get_shard_stats(Shard) ->
242
272
, server_mql => get_mql (Shard )
243
273
};
244
274
replicant ->
245
- case upstream_node (Shard , 0 ) of
275
+ case upstream_node (Shard ) of
246
276
{ok , Upstream } -> ok ;
247
277
_ -> Upstream = undefined
248
278
end ,
0 commit comments