aboutsummaryrefslogtreecommitdiffstats
path: root/lib/orber/src/orber_objectkeys.erl
blob: b0e759187b6f28a337815dd0e6c51ac213595e99 (plain) (blame)
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
%%--------------------------------------------------------------------
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1997-2009. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%
%%
%%-----------------------------------------------------------------
%% File: orber_objectkeys.erl
%% 
%% Description:
%%    This file contains the object keyserver in Orber
%%
%%-----------------------------------------------------------------
-module(orber_objectkeys).

-behaviour(gen_server).

-include_lib("orber/include/corba.hrl").

%%-----------------------------------------------------------------
%% External exports
%%-----------------------------------------------------------------
-export([start/1, stop/0, stop_all/0, get_pid/1, is_persistent/1,
	 register/2, register/3, delete/1, create_schema/1, check/1,
	 remove_old_keys/0]).

%%-----------------------------------------------------------------
%% Internal exports
%%-----------------------------------------------------------------
-export([init/1, terminate/2, install/2, handle_call/3, handle_info/2, code_change/3]).
-export([handle_cast/2, dump/0, get_key_from_pid/1, gc/1]).

%%-----------------------------------------------------------------
%% Mnesia Table definition record
%%-----------------------------------------------------------------
-record(orber_objkeys, {object_key, pid, persistent=false, timestamp}).

%%-----------------------------------------------------------------
%% Macros
%%-----------------------------------------------------------------
-define(dirty_query_context, true).

%% This macro returns a read fun suitable for evaluation in a transaction
-define(read_function(Objkey),
	fun() ->
		mnesia:dirty_read(Objkey)
	end).

%% This macro returns a write fun suitable for evaluation in a transaction
-define(write_function(R),
	fun() ->
		mnesia:dirty_write(R)
	end).

%% This macro returns a delete fun suitable for evaluation in a transaction
-define(delete_function(R),
	fun() ->
		mnesia:delete(R)
	end).

%% Use this fun inside a transaction to get a list of all keys.
-define(match_function(),
	fun() ->
		mnesia:match_object({orber_objkeys, '_', '_','_','_'})
	end).

-ifdef(dirty_query_context).
-define(query_check(Q_res), Q_res).
-else.
-define(query_check(Q_res), {atomic, Q_res}).
-endif.


-define(CHECK_EXCEPTION(Res), case Res of
				  {'EXCEPTION', E} ->
				      corba:raise(E);
				   R ->
				      R
			      end).

-define(DEBUG_LEVEL, 6).



%%-----------------------------------------------------------------
%% Debugging function
%%-----------------------------------------------------------------
dump() ->
    case catch mnesia:dirty_first('orber_objkeys') of
	{'EXIT', R} ->
	    io:format("Exited with ~p\n",[R]);
	Key ->
	    dump_print(Key),
	    dump_loop(Key)
    end.

dump_loop(PreviousKey) ->
    case catch mnesia:dirty_next('orber_objkeys', PreviousKey) of
	{'EXIT', R} ->
	    io:format("Exited with ~p\n",[R]);
	'$end_of_table' ->
	    ok;
	Key ->
	    dump_print(Key),
	    dump_loop(Key)
    end.

dump_print(Key) ->
    case catch mnesia:dirty_read({'orber_objkeys', Key}) of
       {'EXIT', R} ->
	    io:format("Exited with ~p\n",[R]);
	[X] ->
	    io:format("object_key: ~p, pid: ~p, persistent: ~p, timestamp: ~p\n",
		      [binary_to_term(X#orber_objkeys.object_key),
		       X#orber_objkeys.pid,
		       X#orber_objkeys.persistent,
		       X#orber_objkeys.timestamp]);
	_ ->
	    ok
    end.
       

%%-----------------------------------------------------------------
%% External interface functions
%%-----------------------------------------------------------------
start(Opts) ->
    gen_server:start_link({local, orber_objkeyserver}, orber_objectkeys, Opts, []).

stop() ->
    gen_server:call(orber_objkeyserver, stop, infinity).

remove_old_keys() ->
    %% This function may ONLY be used when restarting a crashed node.
    %% We must remove all objects started with {global, "name"} otherwise
    %% we cannot restart the node using the same name.
    Fun = fun() -> 
		  Node = node(),
		  mnesia:write_lock_table(orber_objkeys),
		  Objects = mnesia:match_object(orber_objkeys,
						mnesia:table_info(orber_objkeys,
								  wild_pattern),
						read),
		  lists:foreach(fun(Obj) ->
					case node(Obj#orber_objkeys.pid) of
					    Node ->
						mnesia:delete({orber_objkeys, 
							       Obj#orber_objkeys.object_key});
					    _->
						ok
					end
				end,
				Objects),
		  ok
	  end,
    write_result(mnesia:transaction(Fun)).

stop_and_remove_local(Reason) ->
    %% This function may ONLY be used when this server terminates with reason 
    %% normal or shutdown.
    Fun = fun() -> 
		  Node = node(),
		  mnesia:write_lock_table(orber_objkeys),
		  Objects = mnesia:match_object(orber_objkeys, 
						mnesia:table_info(orber_objkeys,
								  wild_pattern),
						read),
		  lists:foreach(fun(Obj) ->
					case node(Obj#orber_objkeys.pid) of
					    Node ->
						exit(Obj#orber_objkeys.pid, Reason),
						mnesia:delete({orber_objkeys, 
							       Obj#orber_objkeys.object_key});
					    _->
						ok
					end
				end,
				Objects),
		  ok
	  end,
    write_result(mnesia:transaction(Fun)).
    
stop_all() ->
    Fun = ?match_function(),
    case mnesia:transaction(Fun) of
	{atomic, Objects} ->
	    lists:foreach(fun(Obj) ->
				  gen_server:call(Obj#orber_objkeys.pid, 
						  stop, infinity)
			  end,
			  Objects);
	R ->
	    R
    end.

get_pid(Objkey) ->
    case catch ets:lookup_element(orber_objkeys, Objkey, 3) of
	Pid when is_pid(Pid) ->
	    Pid;
	dead ->
	    {error, "unable to contact object"};
	_ ->
	    %% This call is necessary if a persistent object have died
	    %% and the objectkey server is currently updating the Pid
	    %% to equal 'dead'. Without this case 'OBJECT_NOT_EXIST'
	    %% would be raised which is uncorrect if the object is 
	    %% persistent.
	    ?CHECK_EXCEPTION(gen_server:call(orber_objkeyserver,
					     {get_pid, Objkey}, 
					     infinity))
    end.

is_persistent(Pid) when is_pid(Pid) ->
    case catch get_key_from_pid(Pid) of
	{'EXCEPTION', _} ->
	    corba:raise(#'OBJECT_NOT_EXIST'{completion_status=?COMPLETED_NO});
	Key ->
	    is_persistent(Key)
    end;
is_persistent(Objkey) ->
    case catch ets:lookup_element(orber_objkeys, Objkey, 4) of
	{'EXIT', _R} ->
	    corba:raise(#'OBJECT_NOT_EXIST'{completion_status=?COMPLETED_NO});
	Boolean ->
	    Boolean
    end.


gc(Sec) when is_integer(Sec) ->
    Fun = fun() -> 
		  mnesia:write_lock_table(orber_objkeys),
		  Objects = mnesia:match_object({orber_objkeys, '_', dead, true,'_'}),
		  lists:foreach(fun(Obj) ->
					case timetest(Sec, Obj#orber_objkeys.timestamp) of
					    true ->
						mnesia:delete({orber_objkeys, 
							       Obj#orber_objkeys.object_key});
					    _->
						ok
					end
				end,
				Objects),
		  ok
	  end,
    write_result(mnesia:transaction(Fun)).

register(Objkey, Pid) ->
    'register'(Objkey, Pid, false).

register(Objkey, Pid, Type) when is_pid(Pid) ->
    ?CHECK_EXCEPTION(gen_server:call(orber_objkeyserver,
				     {register, Objkey, Pid, Type}, 
				     infinity));
register(Objkey, Pid, Type) ->
    orber:dbg("[~p] orber_objectkeys:register(~p, ~p); Not a Pid ~p", 
	      [?LINE, Objkey, Type, Pid], ?DEBUG_LEVEL),
    corba:raise(#'INTERNAL'{completion_status=?COMPLETED_NO}).

delete(Objkey) ->
    ?CHECK_EXCEPTION(gen_server:call(orber_objkeyserver,
				     {delete, Objkey}, infinity)).

check(Objkey) ->
    ?CHECK_EXCEPTION(gen_server:call(orber_objkeyserver,
				     {check, Objkey}, infinity)).

%%-----------------------------------------------------------------
%% Server functions
%%-----------------------------------------------------------------
init(_Env) ->
    case mnesia:wait_for_tables(['orber_objkeys'], infinity) of
	ok ->
	    process_flag(trap_exit, true),
	    start_gc_timer(orber:objectkeys_gc_time());
	StopReason ->
	    {stop, StopReason}
    end.

terminate(shutdown, _State) ->
    stop_and_remove_local(shutdown),
    ok;
terminate(normal, _State) ->
    stop_and_remove_local(normal),
    ok;
terminate(_Reason, _State) ->
    ok.

start_gc_timer(infinity) ->
    {ok, []};
start_gc_timer(Time) ->
    timer:start(),   
    case timer:send_after(timer:seconds(Time), 
			  orber_objkeyserver, {oe_gc, Time}) of
	{ok, _} ->
	    {ok, []};
	StopReason ->
	    {stop, StopReason}
    end.
	
install(Timeout, Options) ->
    %% check if there already exists a database. If not, create one.
    %% DB_initialized = perhaps_create_schema(Nodelist),
    %% check if mnesia is running. If not, start mnesia.
    perhaps_start_mnesia(),

    %% Do we have a complete set of IFR tables? If not, create them.
    AllTabs = mnesia:system_info(tables),

    DB_Result = case lists:member(orber_objkeys, AllTabs) of
		    true ->
			case lists:member({local_content, true},
					  Options) of
			    true->
				mnesia:add_table_copy(orber_objkeys,
						      node(),
						      ram_copies);
			    _ ->
				mnesia:create_table(orber_objkeys,
						    [{attributes,
						      record_info(fields,
								  orber_objkeys)}
						     |Options])
			end;
		    _ ->
			mnesia:create_table(orber_objkeys,
					    [{attributes,
					      record_info(fields,
							  orber_objkeys)}
					     |Options])	
		end,
    
    Wait = mnesia:wait_for_tables([orber_objkeys], Timeout),
    %% Check if any error has occured yet. If there are errors, return them.
    if
	DB_Result == {atomic, ok},
	Wait == ok ->
	    ok;
	true ->
	    {error, {DB_Result, Wait}}
    end.

%%-----------------------------------------------------------------
%% Func: handle_call/3
%%
%% Comment:
%%   In objectkey gen_server all exceptions are tupples and corba:raise
%%   may not be used. It is too time consuming to add catches in every 
%%   function before returning. On the client side there is a case which 
%%   maps every tupple on the format {'exception', E} to corba:raise(E).
%%-----------------------------------------------------------------
handle_call(stop, _From, State) ->
    {stop, normal, [], State};
handle_call({get, Objkey}, _From, State) ->
    R = query_result(mnesia:dirty_read({orber_objkeys, Objkey})),
    {reply, R, State};

handle_call({register, Objkey, Pid, Type}, _From, State) ->
    _WF = fun() ->
		  case mnesia:wread({orber_objkeys, Objkey}) of
		      [] ->
			  %% No key exists. Ok to register.
			  mnesia:write(#orber_objkeys{object_key=Objkey, pid=Pid,
						      persistent=Type,
						      timestamp=now()});
		      [X] when X#orber_objkeys.persistent==true,
			       X#orber_objkeys.pid == dead ->
			  %% A persistent object is being restarted. Update Pid & time.
			  mnesia:write(X#orber_objkeys{pid=Pid, timestamp=now()});
		      [X] when is_pid(X#orber_objkeys.pid) ->
			  %% Object exists, i.e., trying to create an object with
			  %% the same name.
			  orber:dbg("[~p] orber_objectkeys:register(~p, ~p); Object already exists.", 
				    [?LINE, Objkey, Type], ?DEBUG_LEVEL),
			  {'EXCEPTION', #'BAD_PARAM'{completion_status=?COMPLETED_NO}};
		      Why ->
			  %% Something else occured.
			  orber:dbg("[~p] orber_objectkeys:register(~p, ~p); error reading from DB(~p)", 
				      [?LINE, Objkey, Type, Why], ?DEBUG_LEVEL),
			  {'EXCEPTION', #'INTERNAL'{completion_status=?COMPLETED_NO}}
		  end
	  end,
    R = write_result(mnesia:transaction(_WF)),
    if
	R == ok andalso is_pid(Pid) ->
	    link(Pid);
	true ->
	    true
    end,
    {reply, R, State};

handle_call({delete, Objkey}, _From, State) ->
    ?query_check(Qres) = mnesia:dirty_read({orber_objkeys, Objkey}),
    case Qres of
	[] ->
	    true;
	[X] when is_pid(X#orber_objkeys.pid) ->
	    unlink(X#orber_objkeys.pid);
	_ ->
	    true
    end,
    _F = ?delete_function({orber_objkeys, Objkey}),
    R = write_result(mnesia:transaction(_F)),
    {reply, R, State};

handle_call({get_pid, Objkey}, _From, State) ->
    _F = fun() ->
		 mnesia:read({orber_objkeys, Objkey})
	 end,
    case mnesia:transaction(_F) of
	{atomic, [X]} when is_pid(X#orber_objkeys.pid) ->
	    {reply, X#orber_objkeys.pid, State};
	{atomic, [X]} when X#orber_objkeys.pid == dead ->
	    {reply,
	     {'EXCEPTION', #'TRANSIENT'{completion_status=?COMPLETED_NO}},
	     State};
	_Res ->
	    {reply, 
	     {'EXCEPTION', #'OBJECT_NOT_EXIST'{completion_status=?COMPLETED_NO}},
	     State}
    end;
handle_call({check, {_, 'key', Objkey, _, _, _}}, _From, State) ->
    ?query_check(Qres) = mnesia:dirty_read({orber_objkeys, Objkey}),
    case Qres of
	[_X] ->
	    {reply, 'object_here', State};
	_ ->
	    {reply, 'unknown_object', State}
    end;
handle_call({check, {_, 'registered', Objkey, _, _, _}}, _From, State) ->
    case whereis(Objkey) of
	undefined ->
	    case catch ets:lookup_element(orber_objkeys, Objkey, 4) of
		true ->
		    {reply, 'object_here', State};
		_->
		    {reply, 'unknown_object', State}
	    end;
	_ ->
	    {reply, 'object_here', State}
    end;
handle_call({check, {_, 'pseudo', Module, _, _, _}}, _From, State) ->
    case code:is_loaded(Module) of
	false ->
	    {reply, 'unknown_object', State};
	_ ->
	    {reply, 'object_here', State}
    end;

handle_call({check, "INIT"}, _From, State) ->
    {reply, 'object_here', State};
handle_call({check, _}, _From, State) ->
    {reply, 'unknown_object', State}.

	    
handle_info({'EXIT', Pid, Reason}, State) when is_pid(Pid) ->
    _WF = fun() ->
		  case mnesia:match_object({orber_objkeys, '_', Pid,'_','_'}) of
		      [] ->
			  ok;
		      [X] when X#orber_objkeys.persistent==false ->
			  mnesia:delete({orber_objkeys, X#orber_objkeys.object_key});
		      [X] when is_pid(X#orber_objkeys.pid) andalso
			       X#orber_objkeys.persistent==true andalso
			       Reason /= normal andalso
			       Reason /= shutdown ->
			  mnesia:write(X#orber_objkeys{pid=dead,
						       timestamp=now()});
		      [X] when X#orber_objkeys.persistent==true ->
			  mnesia:delete({orber_objkeys, X#orber_objkeys.object_key});
		      _->
			  ok
		  end
	  end,
    case write_result(mnesia:transaction(_WF)) of
	ok ->
	    unlink(Pid);
	_->
	    true
    end,
    {noreply, State};

handle_info({oe_gc, Secs}, State) ->
    catch gc(Secs),
    {noreply, State}.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%%-----------------------------------------------------------------
%% Internal Functions
%%-----------------------------------------------------------------

timetest(S, {MeSec, Sec, USec}) ->
    {MeSec, Sec+S, USec} < now().

get_key_from_pid(Pid) ->
    case  mnesia:dirty_match_object({orber_objkeys, '_', Pid,'_','_'}) of
	[Keys] -> 
	    Keys#orber_objkeys.object_key;
	_ ->
	    corba:raise(#'OBJECT_NOT_EXIST'{completion_status=?COMPLETED_NO})
    end.

%remove_keys([], _) ->
%    ok;
%remove_keys([H|T], R) when H#orber_objkeys.persistent==false ->
%    _F = ?delete_function({orber_objkeys, H#orber_objkeys.object_key}),
%    write_result(mnesia:transaction(_F)),
%    remove_keys(T, R).

%%-----------------------------------------------------------------
%% Check a read transaction
query_result(?query_check(Qres)) -> 
    case Qres of
	[Hres] ->
	    Hres#orber_objkeys.pid;
	[] ->
	    {'EXCEPTION', #'OBJECT_NOT_EXIST'{completion_status=?COMPLETED_NO}};
	Other ->
	    orber:dbg("[~p] orber_objectkeys:query_result(); DB lookup failed(~p)", 
				    [?LINE, Other], ?DEBUG_LEVEL),
	    {'EXCEPTION', #'INTERNAL'{completion_status=?COMPLETED_NO}}
    end.

%%-----------------------------------------------------------------
%% Check a write transaction
write_result({atomic,ok}) -> ok;
write_result(Foo) ->
    orber:dbg("[~p] orber_objectkeys:query_result(); DB write failed(~p)", 
	      [?LINE, Foo], ?DEBUG_LEVEL),
    {'EXCEPTION', #'INTERNAL'{completion_status=?COMPLETED_NO}}.


create_schema(Nodes) ->
    case mnesia:system_info(use_dir) of
	false ->
	    mnesia:create_schema(Nodes);
	_ ->
	    ok
    end.

perhaps_start_mnesia() ->
    case mnesia:system_info(is_running) of
	no ->
	    mnesia:start();
	_ ->
	    ok
    end.


%%------------------------------------------------------------
%% Standard gen_server cast handle
%%
handle_cast(_, State) ->
    {noreply,  State}.