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, Inc.
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"
44
static std::vector<XaResourceManager *> xa_resource_managers;
46
int XaResourceManager::commitOrRollbackXID(XID *xid, bool commit)
48
std::vector<int> results;
51
transform(xa_resource_managers.begin(), xa_resource_managers.end(), results.begin(),
52
std::bind2nd(std::mem_fun(&XaResourceManager::xaCommitXid), xid));
54
transform(xa_resource_managers.begin(), xa_resource_managers.end(), results.begin(),
55
std::bind2nd(std::mem_fun(&XaResourceManager::xaRollbackXid), xid));
57
if (std::find_if(results.begin(), results.end(), std::bind2nd(std::equal_to<int>(),0)) == results.end())
67
there are three modes of operation:
68
- automatic recover after a crash
69
in this case commit_list.size() != 0, tc_heuristic_recover==0
70
all xids from commit_list are committed, others are rolled back
71
- manual (heuristic) recover
72
in this case commit_list.size()==0, tc_heuristic_recover != 0
73
DBA has explicitly specified that all prepared transactions should
74
be committed (or rolled back).
75
- no recovery (Drizzle did not detect a crash)
76
in this case commit_list.size()==0, tc_heuristic_recover == 0
77
there should be no prepared transactions in this case.
79
class XaRecover : std::unary_function<XaResourceManager *, void>
82
int trans_len, found_foreign_xids, found_my_xids;
85
const XaResourceManager::commit_list_set &commit_list;
88
XaRecover(XID *trans_list_arg, int trans_len_arg,
89
const XaResourceManager::commit_list_set& commit_list_arg,
91
: trans_len(trans_len_arg), found_foreign_xids(0), found_my_xids(0),
93
trans_list(trans_list_arg), commit_list(commit_list_arg),
99
return found_foreign_xids;
104
return found_my_xids;
107
result_type operator() (argument_type resource_manager)
112
while ((got= resource_manager->xaRecover(trans_list, trans_len)) > 0 )
114
errmsg_printf(error::INFO,
115
_("Found %d prepared transaction(s) in resource manager."),
117
for (int i=0; i < got; i ++)
119
my_xid x=trans_list[i].get_my_xid();
120
if (!x) // not "mine" - that is generated by external TM
122
xid_cache_insert(trans_list+i, XA_PREPARED);
123
found_foreign_xids++;
132
if (commit_list.size() ?
133
commit_list.find(x) != commit_list.end() :
134
tc_heuristic_recover == TC_HEURISTIC_RECOVER_COMMIT)
136
resource_manager->xaCommitXid(trans_list+i);
140
resource_manager->xaRollbackXid(trans_list+i);
149
int XaResourceManager::recoverAllXids()
151
const XaResourceManager::commit_list_set empty_commit_set;
152
return recoverAllXids(empty_commit_set);
155
int XaResourceManager::recoverAllXids(const XaResourceManager::commit_list_set &commit_list)
157
XID *trans_list= NULL;
160
bool dry_run= (commit_list.size() == 0 && tc_heuristic_recover==0);
162
/* commit_list and tc_heuristic_recover cannot be set both */
163
assert(commit_list.size() == 0 || tc_heuristic_recover == 0);
165
if (xa_resource_managers.size() <= 1)
168
tc_heuristic_recover= TC_HEURISTIC_RECOVER_ROLLBACK; // forcing ROLLBACK
170
for (trans_len= MAX_XID_LIST_SIZE ;
171
trans_list==0 && trans_len > MIN_XID_LIST_SIZE; trans_len/=2)
173
trans_list=(XID *)malloc(trans_len*sizeof(XID));
177
errmsg_printf(error::ERROR, ER(ER_OUTOFMEMORY), trans_len*sizeof(XID));
181
if (commit_list.size())
182
errmsg_printf(error::INFO, _("Starting crash recovery..."));
184
XaRecover recover_func(trans_list, trans_len, commit_list, dry_run);
185
std::for_each(xa_resource_managers.begin(),
186
xa_resource_managers.end(),
190
if (recover_func.getForeignXIDs())
191
errmsg_printf(error::WARN,
192
_("Found %d prepared XA transactions"),
193
recover_func.getForeignXIDs());
195
if (dry_run && recover_func.getMyXIDs())
197
errmsg_printf(error::ERROR,
198
_("Found %d prepared transactions! It means that drizzled "
199
"was not shut down properly last time and critical "
200
"recovery information (last binlog or %s file) was "
201
"manually deleted after a crash. You have to start "
202
"drizzled with the --tc-heuristic-recover switch to "
203
"commit or rollback pending transactions."),
204
recover_func.getMyXIDs(), opt_tc_log_file);
208
if (commit_list.size())
209
errmsg_printf(error::INFO, _("Crash recovery finished."));
214
bool XaResourceManager::addPlugin(XaResourceManager *resource_manager)
216
xa_resource_managers.push_back(resource_manager);
220
void XaResourceManager::removePlugin(XaResourceManager *)
222
xa_resource_managers.clear();
225
} /* namespace plugin */
226
} /* namespace drizzled */