summaryrefslogtreecommitdiff
path: root/lib/inets/examples/httpd_load_test/hdlt_slave.erl
blob: 30ddfd76afbf3125d4feb7be0b3ba353b68e0e37 (plain)
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
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2010-2016. All Rights Reserved.
%%
%% Licensed 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.
%%
%% %CopyrightEnd%
%%
-module(hdlt_slave).


-export([start_link/4, start_link/5, start_link/6, stop/1]).

%% Internal exports 
-export([wait_for_slave/9, slave_start/1, wait_for_master_to_die/3]).

-include("hdlt_logger.hrl").

-define(SSH_PORT, 22).
-define(TIMEOUT,  60000).
-define(LOGGER,   hdlt_logger).


%% ***********************************************************************
%% start_link/4,5 --
%%
%% The start/4,5 functions are used to start a slave Erlang node.
%% The node on which the start/N functions are used is called the
%% master in the description below.
%%
%% If hostname is the same for the master and the slave,
%% the Erlang node will simply be spawned.  The only requirment for
%% this to work is that the 'erl' program can be found in PATH.
%%
%% If the master and slave are on different hosts, start/N uses
%% the 'ssh' program to spawn an Erlang node on the other host.
%% Alternative, if the master was started as
%% 'erl -sname xxx -rsh my_rsh...', then 'my_rsh' will be used instead
%% of 'ssh' (this is useful for systems still using rsh or remsh).
%%
%% For this to work, the following conditions must be fulfilled:
%%
%% 1. There must be an ssh program on computer; if not an error
%%    is returned.
%%
%% 2. The hosts must be configured to allow 'ssh' access without
%%    prompts for password.
%%
%% The slave node will have its filer and user server redirected
%% to the master.  When the master node dies, the slave node will
%% terminate.  For the start_link functions, the slave node will
%% terminate also if the process which called start_link terminates.
%%
%% Returns: {ok, Name@Host} |
%%	    {error, timeout} |
%%          {error, no_rsh} |
%%	    {error, {already_running, Name@Host}}

start_link(Host, Name, ErlPath, Paths) ->
    start_link(Host, Name, ErlPath, Paths, [], silence).

start_link(Host, Name, ErlPath, Paths, DebugLevel) when is_atom(DebugLevel) ->
    start_link(Host, Name, ErlPath, Paths, [], DebugLevel);
start_link(Host, Name, ErlPath, Paths, Args) when is_list(Args) ->
    start_link(Host, Name, ErlPath, Paths, Args, silence).

start_link(Host, Name, ErlPath, Paths, Args, DebugLevel) ->
    Node = list_to_atom(lists:concat([Name, "@", Host])),
    case net_adm:ping(Node) of
	pang ->
	    start_it(Host, Name, Node, ErlPath, Paths, Args, DebugLevel);
	pong -> 
	    {error, {already_running, Node}}
    end.

%% Stops a running node.

stop(Node) ->
    rpc:call(Node, erlang, halt, []),
    ok.


%% Starts a new slave node.

start_it(Host, Name, Node, ErlPath, Paths, Args, DebugLevel) ->
    Prog = filename:join([ErlPath, "erl"]), 
    spawn(?MODULE, wait_for_slave, [self(), Host, Name, Node, Paths, Args, self(), Prog, DebugLevel]),
    receive
	{result, Result} -> Result
    end.

%% Waits for the slave to start.

wait_for_slave(Parent, Host, Name, Node, Paths, Args, 
	       LinkTo, Prog, DebugLevel) ->
    ?SET_NAME("HDLT SLAVE STARTER"), 
    ?SET_LEVEL(DebugLevel),
    ?DEBUG("begin", []),
    Waiter = register_unique_name(0),
    case mk_cmd(Host, Name, Paths, Args, Waiter, Prog) of
	{ok, Cmd} ->
  	    ?DEBUG("command generated: ~n~s", [Cmd]),
	    case (catch ssh_slave_start(Host, Cmd)) of
		{ok, Conn, _Chan} ->
 		    ?DEBUG("ssh channel created", []),
		    receive
			{SlavePid, slave_started} ->
 			    ?DEBUG("slave started: ~p", [SlavePid]),
			    unregister(Waiter),
			    slave_started(Parent, LinkTo, SlavePid, Conn, 
					  DebugLevel)
		    after 32000 ->
			    ?INFO("slave node failed to report in on time", 
				  []),
			    %% If it seems that the node was partially started,
			    %% try to kill it.
			    case net_adm:ping(Node) of
				pong ->
				    spawn(Node, erlang, halt, []),
				    ok;
				_ ->
				    ok
			    end,
			    Parent ! {result, {error, timeout}}
		    end;
		{error, Reason} = Error ->
		    ?INFO("FAILED starting node: "
			  "~n   ~p"
			  "~n   ~p", [Reason, Cmd]),
		    Parent ! {result, Error}
	    end;
	Other ->
	    ?INFO("FAILED creating node command string: "
		  "~n   ~p", [Other]),
	    Parent ! {result, Other}
    end.


ssh_slave_start(Host, ErlCmd) ->
    ?DEBUG("ssh_slave_start -> try connect to ~p", [Host]),
    Connection = 
	case (catch ssh:connect(Host, ?SSH_PORT, 
				[{silently_accept_hosts, true}])) of
	    {ok, Conn} ->
 		?DEBUG("ssh_exec_erl -> connected: ~p", [Conn]),
		Conn;
	    Error1 ->
 		?LOG("failed connecting to ~p: ~p", [Host, Error1]),
		throw({error, {ssh_connect_failed, Error1}})
	end,

    ?DEBUG("ssh_exec_erl -> connected - now create channel", []), 
    Channel = 
	case (catch ssh_connection:session_channel(Connection, ?TIMEOUT)) of
	    {ok, Chan} ->
 		?DEBUG("ssh_exec_erl -> channel ~p created", [Chan]),
		Chan;
	    Error2 ->
 		?LOG("failed creating channel: ~p", [Error2]),
		throw({error, {ssh_channel_create_failed, Error2}})
	end,

    ?DEBUG("ssh_exec_erl -> channel created - now exec command: "
 	"~n   ~p", [ErlCmd]), 
    case (catch ssh_connection:exec(Connection, Channel, ErlCmd, infinity)) of
	success ->
	    ?DEBUG("ssh_exec_erl -> command exec'ed - clean ssh msg", []), 
	    clean_ssh_msg(),
 	    ?DEBUG("ssh_exec_erl -> done", []), 
	    {ok, Connection, Channel};
	Error3 ->
	    ?LOG("failed exec command: ~p", [Error3]),
	    throw({error, {ssh_exec_failed, Error3}})
    end.

clean_ssh_msg() ->
    receive
	{ssh_cm, _X, _Y} ->
	    clean_ssh_msg()
    after 1000 ->
	    ok
    end.
    

slave_started(ReplyTo, Master, Slave, Conn, Level) 
  when is_pid(Master) andalso is_pid(Slave) ->
    process_flag(trap_exit, true),
    SName = lists:flatten(
	      io_lib:format("HDLT SLAVE CTRL[~p,~p]", 
			    [self(), node(Slave)])), 
    ?SET_NAME(SName),
    ?SET_LEVEL(Level), 
    ?LOG("initiating", []),
    MasterRef = erlang:monitor(process, Master),
    SlaveRef  = erlang:monitor(process, Slave),
    ReplyTo ! {result, {ok, node(Slave)}},
    slave_running(Master, MasterRef, Slave, SlaveRef, Conn).


%% The slave node will be killed if the master process terminates, 
%% The master process will not be killed if the slave node terminates.

slave_running(Master, MasterRef, Slave, SlaveRef, Conn) ->
    ?DEBUG("await message", []),
    receive
	{'DOWN', MasterRef, process, _Object, _Info} ->
	    ?LOG("received DOWN from master", []),
	    erlang:demonitor(SlaveRef, [flush]),
	    Slave ! {nodedown, node()},
	    ssh:close(Conn);

	{'DOWN', SlaveRef, process, Object, _Info} ->
	    ?LOG("received DOWN from slave (~p)", [Object]),
	    erlang:demonitor(MasterRef, [flush]),
	    ssh:close(Conn);

	Other ->
	    ?DEBUG("received unknown: ~n~p", [Other]),
	    slave_running(Master, MasterRef, Slave, SlaveRef, Conn)

    end.

register_unique_name(Number) ->
    Name = list_to_atom(lists:concat([?MODULE, "_waiter_", Number])),
    case catch register(Name, self()) of
	true ->
	    Name;
	{'EXIT', {badarg, _}} ->
	    register_unique_name(Number+1)
    end.


%% Makes up the command to start the nodes.
%% If the node should run on the local host, there is
%% no need to use ssh.

mk_cmd(Host, Name, Paths, Args, Waiter, Prog) ->
    PaPaths = [[" -pa ", Path] || Path <- Paths], 
    {ok, lists:flatten(
	   lists:concat([Prog,
			 " -detached -nopinput ", 
			 Args, " ", 
			 " -sname ", Name, "@", Host,
			 " -s ", ?MODULE, " slave_start ", node(),
			 " ", Waiter,
			 " ", PaPaths]))}.


%% This function will be invoked on the slave, using the -s option of erl.
%% It will wait for the master node to terminate.

slave_start([Master, Waiter]) ->
    spawn(?MODULE, wait_for_master_to_die, [Master, Waiter, silence]);
slave_start([Master, Waiter, Level]) ->
    spawn(?MODULE, wait_for_master_to_die, [Master, Waiter, Level]).
	

wait_for_master_to_die(Master, Waiter, Level) ->
    process_flag(trap_exit, true),
    SName = lists:flatten(
	      io_lib:format("HDLT-SLAVE MASTER MONITOR[~p,~p,~p]", 
			    [self(), node(), Master])), 
    ?SET_NAME(SName),
    ?SET_LEVEL(Level), 
    erlang:monitor_node(Master, true),
    {Waiter, Master} ! {self(), slave_started},
    wloop(Master).

wloop(Master) ->
    ?DEBUG("await message", []),
    receive
	{nodedown, Master} ->
	    ?INFO("received master nodedown", []),
	    halt();
	_Other ->
	    wloop(Master)
    end.