1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
5
* Copyright (c) 2010 Jay Pipes <jaypipes@gmail.com>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; version 2 of the License.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
#include "drizzled/cached_directory.h"
25
#include <drizzled/definitions.h>
26
#include <drizzled/session.h>
27
#include <drizzled/error.h>
28
#include <drizzled/gettext.h>
29
#include <drizzled/plugin/xa_resource_manager.h>
30
#include "drizzled/xid.h"
46
static vector<XaResourceManager *> xa_resource_managers;
48
int XaResourceManager::commitOrRollbackXID(XID *xid, bool commit)
53
transform(xa_resource_managers.begin(), xa_resource_managers.end(), results.begin(),
54
bind2nd(mem_fun(&XaResourceManager::xaCommitXid), xid));
56
transform(xa_resource_managers.begin(), xa_resource_managers.end(), results.begin(),
57
bind2nd(mem_fun(&XaResourceManager::xaRollbackXid), xid));
59
if (find_if(results.begin(), results.end(), bind2nd(equal_to<int>(),0))
69
there are three modes of operation:
70
- automatic recover after a crash
71
in this case commit_list.size() != 0, tc_heuristic_recover==0
72
all xids from commit_list are committed, others are rolled back
73
- manual (heuristic) recover
74
in this case commit_list.size()==0, tc_heuristic_recover != 0
75
DBA has explicitly specified that all prepared transactions should
76
be committed (or rolled back).
77
- no recovery (Drizzle did not detect a crash)
78
in this case commit_list.size()==0, tc_heuristic_recover == 0
79
there should be no prepared transactions in this case.
81
class XaRecover : unary_function<XaResourceManager *, void>
84
int trans_len, found_foreign_xids, found_my_xids;
87
const XaResourceManager::commit_list_set &commit_list;
90
XaRecover(XID *trans_list_arg, int trans_len_arg,
91
const XaResourceManager::commit_list_set& commit_list_arg,
93
: trans_len(trans_len_arg), found_foreign_xids(0), found_my_xids(0),
95
trans_list(trans_list_arg), commit_list(commit_list_arg),
101
return found_foreign_xids;
106
return found_my_xids;
109
result_type operator() (argument_type resource_manager)
114
while ((got= resource_manager->xaRecover(trans_list, trans_len)) > 0 )
116
errmsg_printf(ERRMSG_LVL_INFO,
117
_("Found %d prepared transaction(s) in resource manager."),
119
for (int i=0; i < got; i ++)
121
my_xid x=trans_list[i].get_my_xid();
122
if (!x) // not "mine" - that is generated by external TM
124
xid_cache_insert(trans_list+i, XA_PREPARED);
125
found_foreign_xids++;
134
if (commit_list.size() ?
135
commit_list.find(x) != commit_list.end() :
136
tc_heuristic_recover == TC_HEURISTIC_RECOVER_COMMIT)
138
resource_manager->xaCommitXid(trans_list+i);
142
resource_manager->xaRollbackXid(trans_list+i);
151
int XaResourceManager::recoverAllXids()
153
const XaResourceManager::commit_list_set empty_commit_set;
154
return recoverAllXids(empty_commit_set);
157
int XaResourceManager::recoverAllXids(const XaResourceManager::commit_list_set &commit_list)
159
XID *trans_list= NULL;
162
bool dry_run= (commit_list.size() == 0 && tc_heuristic_recover==0);
164
/* commit_list and tc_heuristic_recover cannot be set both */
165
assert(commit_list.size() == 0 || tc_heuristic_recover == 0);
167
if (xa_resource_managers.size() <= 1)
170
tc_heuristic_recover= TC_HEURISTIC_RECOVER_ROLLBACK; // forcing ROLLBACK
172
for (trans_len= MAX_XID_LIST_SIZE ;
173
trans_list==0 && trans_len > MIN_XID_LIST_SIZE; trans_len/=2)
175
trans_list=(XID *)malloc(trans_len*sizeof(XID));
179
errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_OUTOFMEMORY), trans_len*sizeof(XID));
183
if (commit_list.size())
184
errmsg_printf(ERRMSG_LVL_INFO, _("Starting crash recovery..."));
186
XaRecover recover_func(trans_list, trans_len, commit_list, dry_run);
187
for_each(xa_resource_managers.begin(),
188
xa_resource_managers.end(),
192
if (recover_func.getForeignXIDs())
193
errmsg_printf(ERRMSG_LVL_WARN,
194
_("Found %d prepared XA transactions"),
195
recover_func.getForeignXIDs());
196
if (dry_run && recover_func.getMyXIDs())
198
errmsg_printf(ERRMSG_LVL_ERROR,
199
_("Found %d prepared transactions! It means that drizzled "
200
"was not shut down properly last time and critical "
201
"recovery information (last binlog or %s file) was "
202
"manually deleted after a crash. You have to start "
203
"drizzled with the --tc-heuristic-recover switch to "
204
"commit or rollback pending transactions."),
205
recover_func.getMyXIDs(), opt_tc_log_file);
208
if (commit_list.size())
209
errmsg_printf(ERRMSG_LVL_INFO, _("Crash recovery finished."));
213
bool XaResourceManager::addPlugin(XaResourceManager *resource_manager)
215
xa_resource_managers.push_back(resource_manager);
219
void XaResourceManager::removePlugin(XaResourceManager *)
221
xa_resource_managers.clear();
224
} /* namespace plugin */
225
} /* namespace drizzled */