-
Notifications
You must be signed in to change notification settings - Fork 16
/
nkcluster_protocol.erl
720 lines (567 loc) · 20.7 KB
/
nkcluster_protocol.erl
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
%% -------------------------------------------------------------------
%%
%% Copyright (c) 2015 Carlos Gonzalez Florido. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (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.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
%% @doc Protocol behaviour
%%
%% This module implements the wire protocol for control and worker nodes
%%
-module(nkcluster_protocol).
-author('Carlos Gonzalez <[email protected]>').
-behaviour(nkpacket_protocol).
-export([wait_auth/1, set_master/2, take_control/2]).
-export([send_rpc/2, send_reply/3, send_event/2, send_announce/0, send_announce/1]).
-export([get_all/0, encode/1, stop/1]).
-export([transports/1, default_port/1, naptr/2, encode/2]).
-export([conn_init/1, conn_parse/3, conn_handle_call/4,
conn_handle_cast/3, conn_handle_info/3, conn_stop/3]).
-export_type([conn_id/0, from/0, msg/0, rpc/0, trans_id/0]).
-include_lib("nklib/include/nklib.hrl").
-type conn_id() :: pid() | nkpacket:nkport().
-type trans_id() :: pos_integer().
-type from() :: {pid(), trans_id()}.
-type msg() ::
announce |
set_master |
{auth, map()|{error, term()}} |
{rpc, trans_id(), rpc()} |
{rep, trans_id(), nkcluster:reply()} |
{ev, nkcluster:job_class(), nkcluster:event()}.
-type rpc() ::
{req, nkcluster:job_class(), nkcluster:request()} |
{tsk, nkcluster:job_class(), nkcluster:task_id(), nkcluster:task()} |
{cmd, nkcluster:job_class(), nkcluster:task_id(), nkcluster:command()}.
-define(VSNS, [0]). % Supported versions
-define(MAX_TIME_DIFF, 5000).
%% ===================================================================
%% Public
%% ===================================================================
%% @private Perfoms a synchronous request to authenticate to the worker
-spec wait_auth(conn_id()) ->
{ok, nkcluster:node_id(), map()} | {error, term()}.
wait_auth(ConnId) ->
do_call(ConnId, wait_auth).
%% @private Sets this connection as master for this cluster
-spec set_master(conn_id(), [nklib:uri()]) ->
ok | {error, term()}.
set_master(ConnId, Uris) ->
do_call(ConnId, {send, {set_master, Uris}}).
-spec take_control(ConnPid::pid(), Proxy::pid()) ->
ok.
take_control(ConnPid, Proxy) ->
gen_server:cast(ConnPid, {take_control, Proxy}).
%% @private
-spec send_rpc(conn_id(), rpc()) ->
{ok, trans_id()} | {error, term()}.
send_rpc(ConnId, Rpc) ->
do_call(ConnId, {rpc, Rpc}).
-spec send_reply(conn_id(), trans_id(), nkcluster:reply()) ->
ok | {error, term()}.
send_reply(ConnId, TransId, Reply) ->
do_call(ConnId, {send, encode({rep, TransId, Reply})}).
%% @private
-spec send_event(nkcluster:job_class(), nkcluster:event()) ->
ok | {error, term()}.
send_event(Class, Msg) ->
send_event(get_worker_master(), Class, Msg).
%% @doc Gets all worker 'main' connections
-spec get_worker_master() ->
[pid()].
get_worker_master() ->
[Pid || {undefined, Pid} <- nklib_proc:values(nkcluster_worker_master)].
%% @private
send_event([], _Class, _Msg) ->
{error, no_connections};
send_event([Pid|Rest], Class, Msg) ->
case do_call(Pid, {send, encode({ev, Class, Msg})}) of
ok ->
ok;
{error, _} ->
send_event(Rest, Class, Msg)
end.
%% @private
-spec send_announce() ->
ok | error.
send_announce() ->
send_announce(get_all_worker()).
%% @private
send_announce([]) ->
error;
send_announce([Pid|Rest]) ->
case do_call(Pid, {send, announce}) of
ok -> ok;
_ -> send_announce(Rest)
end.
%% @doc Gets all started connections
-spec get_all() ->
[{control|worker, Node::nkcluster:node_id(), Remote::nkcluster:node_id(), pid()}].
get_all() ->
[
{Type, NodeId, Remote, Pid} ||
{{Type, NodeId, Remote}, Pid} <- nklib_proc:values(nkcluster_conn)
].
%% @doc Gets all worker started connections to primary nodes
-spec get_all_worker() ->
[pid()].
get_all_worker() ->
[Pid || {worker, _NodeId, _Remote, Pid} <- get_all()].
%% @private
stop(Pid) ->
nkpacket_connection:stop(Pid).
%% @private
-spec encode(term()) ->
binary().
encode(Msg) ->
erlang:term_to_binary(Msg, [compressed]).
%% ===================================================================
%% Protocol callbacks
%% ===================================================================
-spec transports(nklib:scheme()) ->
[nkpacket:transport()].
transports(nkcluster) ->
[tcp, tls, sctp, ws, wss].
-spec default_port(nkpacket:transport()) ->
inet:port_number() | invalid.
default_port(tcp) -> 1972;
default_port(tls) -> 1973;
default_port(sctp) -> 1972;
default_port(ws) -> 1974;
default_port(wss) -> 1975;
default_port(_) -> invalid.
-spec naptr(nklib:scheme(), string()) ->
{ok, nkpacket:transport()} | invalid.
naptr(nkcluster, "nks+d2t") -> {ok, tls};
naptr(nkcluster, "nk+d2t") -> {ok, tcp};
naptr(nkcluster, "nks+d2w") -> {ok, wss};
naptr(nkcluster, "nk+d2w") -> {ok, ws};
naptr(nkcluster, "nk+d2s") -> {ok, sctp};
naptr(_, _) -> invalid.
-spec encode(term(), nkpacket:nkport()) ->
{ok, nkpacket:outcoming()} | continue | {error, term()}.
encode(Term, _NkPort) ->
{ok, encode(Term)}.
%% ===================================================================
%% Connection callbacks
%% ===================================================================
-record(state, {
nkport :: nkpacket:nkport(),
type :: control | worker,
node_id :: term(),
auth = false :: boolean(),
cluster :: term(),
password :: term(),
remote_node_id :: term(),
remote_listen = [] :: [nklib:uri()],
remote_meta = [] :: [nklib:token()],
auth_froms = [] :: [{pid(), reference()}],
vsn :: term(),
pos_id :: trans_id(),
control :: pid(),
worker_master = false :: boolean()
}).
-spec conn_init(nkpacket:nkport()) ->
{ok, #state{}}.
conn_init(NkPort) ->
NodeId = nkcluster_agent:node_id(),
{ok, #{type:=Type}=User} = nkpacket:get_user(NkPort),
State = #state{
nkport = NkPort,
node_id = NodeId,
pos_id = erlang:phash2({nklib_util:l_timestamp(), NodeId}) * 1000,
password = maps:get(password, User, undefined)
},
lager:debug("NkCLUSTER node ~s (~p) starting connection", [NodeId, Type]),
case User of
#{type:=listen} ->
% We don't know yet our type
{ok, State};
#{type:=control} ->
% Later we will call take_control
State1 = State#state{type=control},
send_auth(NkPort, State1);
#{type:={control, Pid}} when is_pid(Pid) ->
State1 = State#state{type=control, control=Pid},
send_auth(NkPort, State1);
#{type:=worker} ->
State1 = State#state{type=worker},
send_auth(NkPort, State1)
end.
%% @private
-spec conn_parse(term()|close, nkpacket:nkpacket(), #state{}) ->
{ok, #state{}} | {stop, term(), #state{}}.
conn_parse(close, _NkPort, State) ->
{ok, State};
conn_parse({binary, WsBinary}, NkPort, State) ->
conn_parse(WsBinary, NkPort, State);
conn_parse(Data, NkPort, #state{auth=false}=State) ->
case catch binary_to_term(Data) of
{auth, Msg} ->
process_auth(Msg, NkPort, State);
Other ->
lager:warning("NkCLUSTER node received unexpected object, closing: ~p",
[Other]),
{stop, normal, State}
end;
conn_parse(Data, NkPort, #state{auth=true, type=worker}=State) ->
case catch binary_to_term(Data) of
{set_master, Uris} ->
ok = nkcluster_agent:update_cluster_addr(true, Uris),
State1 = case State#state.worker_master of
true ->
State;
false ->
nklib_proc:put(nkcluster_worker_master),
State#state{worker_master=true}
end,
{ok, State1};
{rpc, TransId, {req, nkcluster, {ping, Time}}} ->
nkcluster_agent:received_ping(),
Reply = {reply, {Time, nklib_util:l_timestamp()}},
ret_send({rep, TransId, Reply}, NkPort, State);
{rpc, TransId, Rpc} ->
case process_rpc(Rpc, {self(), TransId}) of
defer -> {ok, State};
Reply -> ret_send({rep, TransId, Reply}, NkPort, State)
end;
Other ->
lager:warning("NkCLUSTER Worker received unexpected object, "
"closing: ~p", [Other]),
{stop, normal, State}
end;
conn_parse(Data, _NkPort, #state{auth=true, type=control}=State) ->
case catch binary_to_term(Data) of
announce ->
#state{remote_node_id=RemNodeId} = State,
nkcluster_nodes:node_announce(RemNodeId, self()),
{ok, State};
{rep, TransId, Reply} ->
process_resp(TransId, Reply, State);
{ev, Class, Event} ->
process_event(Class, Event, State);
Other ->
lager:warning("NkCLUSTER Control received unexpected object, "
"closing: ~p", [Other]),
{stop, normal, State}
end.
%% @private
-spec conn_handle_call(term(), {pid(), term()}, nkpacket:nkpacket(), #state{}) ->
{ok, #state{}} | {stop, term(), #state{}}.
conn_handle_call(wait_auth, From, NkPort, #state{auth=true}=State) ->
gen_server:reply(From, get_remote(NkPort, State)),
{ok, State};
conn_handle_call(wait_auth, From, _NkPort, State) ->
#state{auth_froms=Froms} = State,
{ok, State#state{auth_froms=[From|Froms]}};
conn_handle_call({rpc, Rpc}, From, NkPort, #state{pos_id=TransId}=State) ->
do_send_rpc({rpc, TransId, Rpc}, From, NkPort, State);
conn_handle_call({send, Msg}, From, NkPort, #state{auth=true}=State)->
ret_send2(Msg, From, NkPort, State);
conn_handle_call({send, _Msg}, From, _NkPort, State) ->
gen_server:reply(From, {error, not_authenticated}),
{ok, State};
conn_handle_call(Msg, _From, _NkPort, State) ->
lager:error("Module ~p received unexpected call: ~p", [?MODULE, Msg]),
{stop, unexpected_call, State}.
%% @private
-spec conn_handle_cast(term(), nkpacket:nkpacket(), #state{}) ->
{ok, #state{}} | {stop, term(), #state{}}.
conn_handle_cast({take_control, Proxy}, _NkPort, #state{type=control}=State) ->
nkpacket_connection:update_monitor(self(), Proxy),
{ok, State#state{control=Proxy}};
conn_handle_cast(Msg, _NkPort, State) ->
lager:error("Module ~p received unexpected cast: ~p", [?MODULE, Msg]),
{ok, State}.
%% @private
-spec conn_handle_info(term(), nkpacket:nkpacket(), #state{}) ->
{ok, #state{}} | {stop, term(), #state{}}.
% nkcluster_jobs launchs processes with start_link
conn_handle_info({'EXIT', _, normal}, _NkPort, State) ->
{ok, State};
conn_handle_info(Msg, _NkPort, State) ->
lager:info("Module ~p received unexpected info: ~p", [?MODULE, Msg]),
{ok, State}.
%% @doc Called when the connection stops
-spec conn_stop(Reason::term(), nkpacket:nkpacket(), #state{}) ->
ok.
conn_stop(_Reason, NkPort, #state{type=Type}) ->
lager:info("NkCLUSTER node (~p) disconnected from ~s", [Type, get_remote_id(NkPort)]).
%% ===================================================================
%% Internal
%% ===================================================================
%% @private
-spec do_call(pid()|nkpacket:nkport(), term()) ->
term().
do_call(Id, Msg) ->
Pid = nkpacket:pid(Id),
case catch gen_server:call(Pid, Msg, 5*60*1000) of
{'EXIT', Error} -> {error, {process_failed, Error}};
Other -> Other
end.
%% @private
send_auth(NkPort, #state{node_id=NodeId, type=Type}=State) ->
AuthMsg = #{
stage => 1,
vsns => ?VSNS,
id => NodeId,
cluster => nkcluster_app:get(cluster_name),
time => nklib_util:l_timestamp() div 1000,
type => Type
},
case raw_send({auth, AuthMsg}, NkPort) of
ok ->
{ok, State};
error ->
{stop, connection_error}
end.
%% @private
do_send_rpc(Msg, From, NkPort, #state{auth=true, pos_id=TransId}=State) ->
case raw_send(Msg, NkPort) of
ok ->
gen_server:reply(From, {ok, TransId}),
{ok, State#state{pos_id=TransId+1}};
error ->
gen_server:reply(From, {ok, connection_error}),
{stop, normal, State}
end;
do_send_rpc(_Msg, From, _NkPort, State) ->
gen_server:reply(From, {error, not_authenticated}),
{ok, State}.
%% @private
%% Processed at the listening side
process_auth(#{stage:=1, vsns:=Vsns, id:=RemNodeId, cluster:=Cluster,
type:=Type, time:=Time}, NkPort, State) ->
case select_vsn(Vsns) of
error ->
not_authorized(unsupported_version, NkPort, State);
Vsn ->
Now = nklib_util:l_timestamp() div 1000,
Drift = abs(Now-Time),
case Drift > ?MAX_TIME_DIFF of
true ->
lager:warning("NkCLUSTER node big time drift: ~p", [Drift]);
% not_authorized(time_drift, State);
false ->
ok
end,
Hash = make_auth_hash(RemNodeId, NkPort, State),
#state{node_id=NodeId} = State,
Stage2 = #{
stage => 2,
id => NodeId,
vsn => Vsn,
hash => Hash
},
OurType = case Type of
control -> worker;
worker -> control
end,
State1 = State#state{
type = OurType,
cluster = Cluster,
remote_node_id = RemNodeId,
vsn=Vsn
},
ret_send({auth, Stage2}, NkPort, State1)
end;
%% Processed at the connecting side
process_auth(#{stage:=2, id:=ListenId, vsn:=Vsn, hash:=Hash}, NkPort, State) ->
true = lists:member(Vsn, ?VSNS),
#state{node_id=NodeId} = State,
case make_auth_hash(NodeId, NkPort, State) of
Hash ->
% The remote (listening) side has a valid password
% We send listen and meta, and try to authenticated ourselves
State1 = State#state{remote_node_id=ListenId},
Base3 = #{
stage => 3,
hash => make_auth_hash(ListenId, NkPort, State),
listen => nkcluster_app:get(listen),
meta => nkcluster_app:get(meta)
},
Stage3 = case nkcluster_agent:is_primary() of
true -> Base3#{primary_nodes=>riak_core_node_watcher:nodes(nkdist)};
false -> Base3
end,
ret_send({auth, Stage3}, NkPort, State1);
_ ->
not_authorized(invalid_password, NkPort, State)
end;
%% Processed at the listening side again
process_auth(#{stage:=3, listen:=Listen, meta:=Meta, hash:=Hash}=Msg, NkPort, State) ->
#state{node_id=ListenId, type=Type} = State,
case make_auth_hash(ListenId, NkPort, State) of
Hash ->
% The remote (connecting) side has a valid password
% We send listen and meta
lager:info("NkCLUSTER node (~p) connected to ~s",
[Type, get_remote_id(NkPort)]),
State1 = State#state{
remote_listen = Listen,
remote_meta = Meta,
auth = true
},
register(State1),
join_nodes(Msg),
Base4 = #{
stage => 4,
listen => nkcluster_app:get(listen),
meta => nkcluster_app:get(meta)
},
Stage4 = case nkcluster_agent:is_primary() of
true -> Base4#{primary_nodes=>riak_core_node_watcher:nodes(nkdist)};
false -> Base4
end,
ret_send({auth, Stage4}, NkPort, State1);
_ ->
not_authorized(invalid_password, NkPort, State)
end;
%% Processed at the connecting side again, both sides are autenticated
process_auth(#{stage:=4, listen:=Listen, meta:=Meta}=Msg, NkPort, State) ->
State2 = State#state{
remote_listen = Listen,
remote_meta = Meta
},
#state{type=Type} = State,
lager:info("NkCLUSTER node (~p) connected to ~s", [Type, get_remote_id(NkPort)]),
join_nodes(Msg),
register(State2),
#state{auth_froms=AuthFroms} = State,
lists:foreach(
fun(From) -> gen_server:reply(From, get_remote(NkPort, State2)) end,
AuthFroms),
{ok, State2#state{auth=true, auth_froms=[]}};
process_auth({error, Error}, NkPort, #state{node_id=NodeId}=State) ->
lager:notice("NkCLUSTER node ~s authentication error: ~p", [NodeId, Error]),
not_authorized(Error, NkPort, State).
%% @private
process_rpc({req, Class, Req}, From) ->
nkcluster_jobs:request(Class, Req, From);
process_rpc({tsk, Class, TaskId, Spec}, From) ->
nkcluster_jobs:task(Class, TaskId, Spec, From);
process_rpc({cmd, Class, TaskId, Cmd}, From) ->
nkcluster_jobs:command(Class, TaskId, Cmd, From).
%% @private
process_resp(TransId, Msg, #state{control=Pid}=State) ->
nkcluster_node_proxy:received_resp(Pid, TransId, Msg),
{ok, State}.
%% @private
process_event(Class, Event, #state{control=Pid}=State) ->
nkcluster_node_proxy:received_event(Pid, Class, Event),
{ok, State}.
%% @private
-spec ret_send2(msg()|binary(), {pid(), term()}, nkpacket:nkport(), #state{}) ->
{ok, #state{}} | {stop, term(), #state{}}.
ret_send2(Msg, From, NkPort, State) ->
case raw_send(Msg, NkPort) of
ok ->
gen_server:reply(From, ok),
{ok, State};
error ->
gen_server:reply(From, {error, connection_error}),
{stop, normal, State}
end.
%% @private
-spec ret_send(msg()|binary(), nkpacket:nkport(), #state{}) ->
{ok, #state{}} | {stop, term(), #state{}}.
ret_send(Msg, NkPort, State) ->
case raw_send(Msg, NkPort) of
ok ->
{ok, State};
error ->
{stop, normal, State}
end.
%% @private
-spec raw_send(msg()|binary(), nkpacket:nkport()) ->
ok | error.
raw_send(Msg, NkPort) when is_binary(Msg) ->
case nkpacket_connection_lib:raw_send(NkPort, Msg) of
ok ->
ok;
{error, closed} ->
error;
{error, Error} ->
lager:notice("NkCLUSTER node error sending ~p: ~p", [Msg, Error]),
error
end;
raw_send(Msg, NkPort) ->
raw_send(encode(Msg), NkPort).
%% @private
select_vsn(Vsns) ->
case sort_vsns(Vsns, []) of
[Vsn|_] -> Vsn;
[] -> error
end.
%% @private
sort_vsns([], Acc) ->
lists:reverse(lists:sort(Acc));
sort_vsns([Vsn|Rest], Acc) ->
case lists:member(Vsn, ?VSNS) of
true -> sort_vsns(Rest, [Vsn|Acc]);
false -> sort_vsns(Rest, Acc)
end.
%% @private
make_auth_hash(Salt, NkPort, #state{password=Pass}) ->
Pass2 = case Pass of
undefined -> nkcluster_app:get(password);
_ -> Pass
end,
{ok, {_Proto, Transp, _, _}} = nkpacket:get_local(NkPort),
case Transp==tls orelse Transp==wss of
true ->
Pass2;
false ->
pbkdf2(Pass2, Salt)
end.
%% @private
register(#state{type=Type, node_id=NodeId, remote_node_id=RemNodeId}) ->
nklib_proc:put(nkcluster_conn, {Type, NodeId, RemNodeId}).
%% @private
not_authorized(Error, NkPort, #state{auth_froms=AuthFroms}=State) ->
lists:foreach(
fun(From) -> gen_server:reply(From, {error, Error}) end,
AuthFroms),
case Error of
invalid_password -> timer:sleep(500);
_ -> ok
end,
raw_send({auth, {error, Error}}, NkPort),
{stop, normal, State}.
%% @private
get_remote(NkPort, State) ->
#state{
remote_node_id = NodeId,
remote_listen = Listen,
remote_meta = Meta
} = State,
Remote = get_remote_id(NkPort),
Status = nkcluster_agent:get_status(),
{ok, NodeId, #{status=>Status, listen=>Listen, meta=>Meta, remote=>Remote}}.
%% @private
get_remote_id(NkPort) ->
{ok, {_, Transp, Ip, Port}} = nkpacket:get_remote(NkPort),
nklib_util:bjoin([Transp, nklib_util:to_host(Ip), Port], <<":">>).
%% @private
join_nodes(#{primary_nodes:=Nodes}) ->
nkcluster_agent:join_nodes(Nodes);
join_nodes(_) ->
ok.
%% @private
pbkdf2(Pass, Salt) ->
Iters = nkcluster_app:get(pbkdf2_iters),
{ok, Hash} = pbkdf2:pbkdf2(sha, Pass, Salt, Iters),
Hash.