aboutsummaryrefslogtreecommitdiffstats
path: root/lib/cosTransactions/src/CosTransactions_Terminator_impl.erl
blob: d995eb64e4d960d070191462f9c13d373becc8a8 (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
%%--------------------------------------------------------------------
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1999-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%
%%
%%
%%----------------------------------------------------------------------
%% File    : CosTransactions_Terminator_impl.erl
%% Purpose : Support operations to commit or roll-back a transaction.
%%----------------------------------------------------------------------

-module('CosTransactions_Terminator_impl').

%%--------------- INCLUDES -----------------------------------
-include_lib("orber/include/corba.hrl").
%% Local
-include_lib("ETraP_Common.hrl").
-include_lib("CosTransactions.hrl").

%%--------------- IMPORTS-------------------------------------
-import(etrap_logmgr, [log_safe/2, get_next/2]).

%%--------------- EXPORTS-------------------------------------
%%-compile(export_all).
-export([commit/3, rollback/2]).
-export([init/1, terminate/2]).
-export([handle_call/3, handle_cast/2, handle_info/2, code_change/3]).

%%--------------- LOCAL DATA ---------------------------------
%-record(terminator, {reg_resources, rollback_only, regname, coordinator}).

%%------------------------------------------------------------
%% function : init, terminate
%% Arguments: 
%% Returns  : 
%% Effect   : Functions demanded by the module ic. Used to initiate
%%            and terminate a gen_server.
%%------------------------------------------------------------

init(State) ->
    process_flag(trap_exit,true),
    case catch start_object(State) of
	{'EXIT', Reason} ->
	    %% Happens when, for example, we encounter an 
	    %% error when reading from the log file.
	    {stop, Reason};
	Other ->
	    Other
    end.

start_object(State) ->
    case catch file:read_file_info(?tr_get_terminator(State)) of
	{error, enoent} -> 
	    %% File does not exist. It's the first time. No restart.
	    ?debug_print("Terminator:init(~p)~n", [?tr_get_terminator(State)]),
	    etrap_logmgr:start(?tr_get_terminator(State)),
	    {ok, State, ?tr_get_timeout(State)};
	{error, Reason} -> % File exist but error occurred.
	    ?tr_error_msg("CosTransactions_Terminator( ~p ) Cannot open log file: ~p~n",
			  [?tr_get_terminator(State), Reason]),
	    {stop, {error, "unable_to_open_log"}};
	_ -> % File exists, perform restart.
	    etrap_logmgr:start(?tr_get_terminator(State)),
	    ?debug_print("RESTART Terminator:init(~p)~n", 
			 [?tr_get_terminator(State)]),
	    do_restart(State, get_next(?tr_get_terminator(State), start), init)
    end.


terminate(Reason, State) ->
    ?debug_print("STOP ~p   ~p~n", [?tr_get_terminator(State), Reason]),
    case Reason of
	normal -> 
	    %% normal termination. Transaction completed. 
	    log_safe(?tr_get_terminator(State), done),
	    etrap_logmgr:stop(?tr_get_terminator(State)),
	    file:delete(?tr_get_terminator(State)),
	    ok;
	_ ->
	    ok
    end.

%%------------------------------------------------------------
%% function : handle_call, handle_cast, handle_info, code_change
%% Arguments: 
%% Returns  : 
%% Effect   : Functions demanded by the module ic. 
%%------------------------------------------------------------

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

handle_call(_,_, State) ->
    {noreply, State}.


handle_cast(_, State) ->
    {noreply, State}.
    

handle_info(Info, State) ->
    ?debug_print("Terminator:handle_info(~p)~n", [Info]),
    Pid = self(),
    case Info of
	timeout ->
	    ?tr_error_msg("Object( ~p ) timeout. Rolling back.~n",
			  [?tr_get_terminator(State)]),
            {stop, normal, State};
        {suicide, Pid} ->
            {stop, normal, State};
        _->
            {noreply, State}
    end.

%%------------------------------------------------------------
%% function : commit
%% Arguments: Self  - its own object reference.
%%            State - Gen-Server State
%%            Heuristics - boolean; report heuristic decisions?
%% Returns  : ok - equal to void
%% Effect   : 
%% Exception: HeuristicMixed  - Highest priority
%%            HeuristicHazard - Lowest  priority
%%------------------------------------------------------------

commit(_Self, State, _Heuristics) when ?tr_is_retransmit(State) ->
    ?debug_print("Terminator:commit() recalled.~n", []),
    {stop, normal, ?tr_get_reportH(State), State};
commit(Self, State, Heuristics) ->
    ?debug_print("Terminator:commit() called.~n", []),
    NewState = ?tr_set_reportH(State, Heuristics),
    log_safe(?tr_get_terminator(NewState), {init_commit, NewState}),
    transmit(Self, NewState, Heuristics).


transmit(Self, State, Heuristics) ->
    case catch 'ETraP_Common':try_timeout(?tr_get_alarm(State)) of
        false ->
%	    catch 'ETraP_Server':before_completion(?tr_get_etrap(State)),
	    case catch 'CosTransactions_Resource':prepare(?tr_get_etrap(State)) of
		'VoteCommit' ->
		    evaluate_answer(Self, State, Heuristics, 
				    'ETraP_Common':try_timeout(?tr_get_alarm(State)));
		'VoteRollback' ->
		    {stop, normal, 
		     {'EXCEPTION', 
		      #'TRANSACTION_ROLLEDBACK'{completion_status=?COMPLETED_YES}}, 
		     State};
		'VoteReadOnly' ->
		    {stop, normal, ok, State};
		{'EXCEPTION', E} when is_record(E,'CosTransactions_HeuristicMixed'),
				      Heuristics==true->
		    catch 'ETraP_Server':forget(?tr_get_etrap(State)),
		    {stop, normal, {'EXCEPTION', E}, State};
		{'EXCEPTION', E} when is_record(E,'CosTransactions_HeuristicHazard'),
				      Heuristics==true->
		    catch 'ETraP_Server':forget(?tr_get_etrap(State)),
		    {stop, normal, {'EXCEPTION', E}, State};
		{'EXCEPTION', E} when is_record(E,'CosTransactions_HeuristicMixed') ->
		    catch 'ETraP_Server':forget(?tr_get_etrap(State)),
		    {stop, normal, 
		     {'EXCEPTION',#'TRANSACTION_ROLLEDBACK'{completion_status=?COMPLETED_YES}},
		     State};
		{'EXCEPTION', E} when is_record(E,'CosTransactions_HeuristicHazard') ->
		    catch 'ETraP_Server':forget(?tr_get_etrap(State)),
		    {stop, normal, 
		     {'EXCEPTION', #'TRANSACTION_ROLLEDBACK'{completion_status=?COMPLETED_YES}},
		     State};
		Other ->
		    ?tr_error_msg("Coordinator:prepare( ~p ) failed. REASON ~p~n", 
				  [?tr_get_etrap(State), Other]),
		    {stop, normal, 
		     {'EXCEPTION', 
		      #'TRANSACTION_ROLLEDBACK'{completion_status=?COMPLETED_YES}}, 
		     State}
	    end;
	_ ->
	    %% Timeout, rollback.
	    log_safe(?tr_get_terminator(State), rolled_back),
	    catch 'ETraP_Server':rollback(?tr_get_etrap(State)),
%	    catch 'ETraP_Server':after_completion(?tr_get_etrap(State),
%						  'StatusRolledBack'),
	    {stop, normal, 
	     {'EXCEPTION', #'TRANSACTION_ROLLEDBACK'{completion_status=?COMPLETED_YES}}, 
	     State}
    end.

evaluate_answer(Self, State, Heuristics, false) ->
    evaluate_answer(Self, State, Heuristics, commit);
evaluate_answer(Self, State, Heuristics, true) ->
    evaluate_answer(Self, State, Heuristics, rollback);
evaluate_answer(_Self, State, Heuristics, Vote) ->
    case catch 'ETraP_Common':send_stubborn('ETraP_Server', Vote, 
					    ?tr_get_etrap(State), 
					    ?tr_get_maxR(State), 
					    ?tr_get_maxW(State)) of
	ok ->
	    ?eval_debug_fun({_Self, commit_ok1}, State),
	    log_safe(?tr_get_terminator(State), committed),
	    ?eval_debug_fun({_Self, commit_ok2}, State),
%	    catch 'ETraP_Server':after_completion(?tr_get_etrap(State),
%						  'StatusCommitted'),
	    {stop, normal, ok, State};
	{'EXCEPTION', E} when Heuristics == true andalso
			      is_record(E,'CosTransactions_HeuristicMixed') ->
	    log_safe(?tr_get_terminator(State), {heuristic, State, E}),
	    ?eval_debug_fun({_Self, commit_heuristic1}, State),
	    catch 'ETraP_Server':forget(?tr_get_etrap(State)),
%	    catch 'ETraP_Server':after_completion(?tr_get_etrap(State),
%						  'StatusRolledBack'),
	    {stop, normal, {'EXCEPTION', E}, State};
	{'EXCEPTION', E} when Heuristics == true andalso
			      is_record(E, 'CosTransactions_HeuristicHazard') ->
	    log_safe(?tr_get_terminator(State), {heuristic, State, E}),
	    catch 'ETraP_Server':forget(?tr_get_etrap(State)),
%	    catch 'ETraP_Server':after_completion(?tr_get_etrap(State),
%						  'StatusRolledBack'),
	    {stop, normal, {'EXCEPTION', E}, State};
	{'EXCEPTION', E} when is_record(E, 'OBJECT_NOT_EXIST') ->
	    log_safe(?tr_get_terminator(State), rolled_back),
	    {stop, normal, {'EXCEPTION', ?tr_hazard}, State};
	{'EXCEPTION', E} when is_record(E, 'TRANSACTION_ROLLEDBACK') ->
	    log_safe(?tr_get_terminator(State), rolled_back),
%	    catch 'ETraP_Server':after_completion(?tr_get_etrap(State),
%						  'StatusRolledBack'),
	    {stop, normal, {'EXCEPTION', E}, State};
	{'EXCEPTION', E} when is_record(E, 'CosTransactions_HeuristicCommit') ->
	    catch 'ETraP_Server':forget(?tr_get_etrap(State)),
%	    catch 'ETraP_Server':after_completion(?tr_get_etrap(State),
%						  'StatusRolledBack'),
	    {stop, normal, ok, State};
	{'EXCEPTION', E} when is_record(E, 'CosTransactions_HeuristicRollback') ->
	    catch 'ETraP_Server':forget(?tr_get_etrap(State)),
%	    catch 'ETraP_Server':after_completion(?tr_get_etrap(State),
%						  'StatusCommitted'),
	    {stop, normal, 
	     {'EXCEPTION', 
	      #'TRANSACTION_ROLLEDBACK'{completion_status=?COMPLETED_YES}}, 
	     State};
	_Other when Heuristics == true ->
	    log_safe(?tr_get_terminator(State), rolled_back),
%	    catch 'ETraP_Server':after_completion(?tr_get_etrap(State),
%						  'StatusRolledBack'),
	    {stop, normal, {'EXCEPTION', ?tr_hazard}, State};
	_Other ->
	    log_safe(?tr_get_terminator(State), rolled_back),
%	    catch 'ETraP_Server':after_completion(?tr_get_etrap(State),
%						  'StatusRolledBack'),
	    {stop, normal, ok, State}
    end.

%%-----------------------------------------------------------%
%% function : rollback
%% Arguments: Self  - its own object reference.
%%            State - Gen-Server State
%% Returns  : ok - equal to void
%% Effect   : 
%%------------------------------------------------------------

rollback(_Self, State) ->
    ?debug_print("Terminator:rollback() called.~n", []),
    log_safe(?tr_get_terminator(State), rolled_back),
    catch 'ETraP_Server':rollback(?tr_get_etrap(State)),
    {stop, normal, ok, State}.

%%-----------------------------------------------------------%
%% function : do_restart
%% Arguments: State - server context
%% Returns  : 
%% Effect   : 
%%------------------------------------------------------------

%% No data in file. Commit never initiated so we rollback (presumed rollback.
do_restart(State, eof, init) -> 
    log_safe(?tr_get_terminator(State), rolled_back),
    catch 'ETraP_Server':rollback(?tr_get_etrap(State)),
%    catch 'ETraP_Server':after_completion(?tr_get_etrap(State),  'StatusRolledBack'),
    self() ! {suicide, self()},
    {ok, State};

do_restart(State, {error, Reason}, _) ->
    ?tr_error_msg("CosTransactions_Terminator (~p) failed. Cannot read log file: ~p~n",
		  [?tr_get_terminator(State), Reason]),
    {stop, Reason};
do_restart(State, eof, Phase) ->
    ?debug_print("Terminator:do_restart(~p)~n", [Phase]),
    case Phase of
	committed ->
	    {ok, ?tr_set_reportH(State, ok)};
	rolled_back ->
	    self() ! {suicide, self()},
	    {ok, State};
        init_commit ->
	    case catch corba_object:non_existent(?tr_get_etrap(State)) of
		true ->
		    self() ! {suicide, self()},
		    {ok, State};
		_->
		    case transmit(false, State, ?tr_get_reportH(State)) of
			{stop, normal, ok, NewState} ->
			    {ok, NewState};
			{stop, normal, 
			 {'EXCEPTION', #'TRANSACTION_ROLLEDBACK'{completion_status=?COMPLETED_YES}}, 
			 NewState} ->
			    self() ! {suicide, self()},
			    {ok, NewState};
			{stop, normal, {'EXCEPTION', Exc}, NewState} ->
			    if
				?tr_dont_reportH(State) ->
				    self() ! {suicide, self()},
				    {ok, NewState};
				true ->
				    {ok, ?tr_set_reportH(NewState, Exc)}
			    end
		    end
	    end;
        {heuristic, Exc} ->
	    catch 'ETraP_Server':forget(?tr_get_etrap(State)),
%	    catch 'ETraP_Server':after_completion(?tr_get_etrap(State),
%						  'StatusRolledBack'),
	    if
		?tr_dont_reportH(State) ->
		    self() ! {suicide, self()},
		    {ok, State};
		true ->
		    {ok, ?tr_set_reportH(State, {'EXCEPTION',Exc})}
	    end
    end;
%% All done.
do_restart(State, {done, _Cursor}, _Phase) ->
    ?debug_print("Terminator:do_restart(~p)~n", [_Phase]),
    self() ! {suicide, self()},
    {ok, State};
do_restart(State, {rolled_back, Cursor}, _Phase) ->
    ?debug_print("Terminator:do_restart(~p)~n", [_Phase]),
    do_restart(State, get_next(?tr_get_terminator(State), Cursor), rolled_back);
do_restart(State, {committed, Cursor}, _Phase) ->
    ?debug_print("Terminator:do_restart(~p)~n", [_Phase]),
    do_restart(State, get_next(?tr_get_terminator(State), Cursor), committed);
do_restart(State, {{heuristic, SavedState, Exc}, Cursor}, _Phase) ->
    ?debug_print("Terminator:do_restart(~p)~n", [_Phase]),
    do_restart(SavedState, get_next(?tr_get_terminator(State), Cursor), 
	       {heuristic, Exc});
do_restart(State, {{init_commit, SavedState}, Cursor}, _) ->
    ?debug_print("Terminator:do_restart(~p)~n", [init_commit]),
    do_restart(SavedState, get_next(?tr_get_terminator(State), Cursor), init_commit).

%%--------------- END OF MODULE ------------------------------