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/my_hash.h"
24
#include "drizzled/cached_directory.h"
26
#include <drizzled/definitions.h>
27
#include <drizzled/session.h>
28
#include <drizzled/error.h>
29
#include <drizzled/gettext.h>
30
#include <drizzled/plugin/xa_resource_manager.h>
31
#include "drizzled/xid.h"
33
#include "drizzled/hash.h"
48
static vector<XaResourceManager *> xa_resource_managers;
50
int XaResourceManager::commitOrRollbackXID(XID *xid, bool commit)
55
transform(xa_resource_managers.begin(), xa_resource_managers.end(), results.begin(),
56
bind2nd(mem_fun(&XaResourceManager::xaCommitXid), xid));
58
transform(xa_resource_managers.begin(), xa_resource_managers.end(), results.begin(),
59
bind2nd(mem_fun(&XaResourceManager::xaRollbackXid), xid));
61
if (find_if(results.begin(), results.end(), bind2nd(equal_to<int>(),0))
71
there are three modes of operation:
72
- automatic recover after a crash
73
in this case commit_list != 0, tc_heuristic_recover==0
74
all xids from commit_list are committed, others are rolled back
75
- manual (heuristic) recover
76
in this case commit_list==0, tc_heuristic_recover != 0
77
DBA has explicitly specified that all prepared transactions should
78
be committed (or rolled back).
79
- no recovery (MySQL did not detect a crash)
80
in this case commit_list==0, tc_heuristic_recover == 0
81
there should be no prepared transactions in this case.
83
class XaRecover : unary_function<XaResourceManager *, void>
85
int trans_len, found_foreign_xids, found_my_xids;
91
XaRecover(XID *trans_list_arg, int trans_len_arg,
92
HASH *commit_list_arg, bool dry_run_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++;
135
hash_search(commit_list, (unsigned char *)&x, sizeof(x)) != 0 :
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(HASH *commit_list)
153
XID *trans_list= NULL;
156
bool dry_run= (commit_list==0 && tc_heuristic_recover==0);
158
/* commit_list and tc_heuristic_recover cannot be set both */
159
assert(commit_list==0 || tc_heuristic_recover==0);
161
if (xa_resource_managers.size() <= 1)
164
tc_heuristic_recover= TC_HEURISTIC_RECOVER_ROLLBACK; // forcing ROLLBACK
166
for (trans_len= MAX_XID_LIST_SIZE ;
167
trans_list==0 && trans_len > MIN_XID_LIST_SIZE; trans_len/=2)
169
trans_list=(XID *)malloc(trans_len*sizeof(XID));
173
errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_OUTOFMEMORY), trans_len*sizeof(XID));
178
errmsg_printf(ERRMSG_LVL_INFO, _("Starting crash recovery..."));
180
XaRecover recover_func(trans_list, trans_len, commit_list, dry_run);
181
for_each(xa_resource_managers.begin(),
182
xa_resource_managers.end(),
186
if (recover_func.getForeignXIDs())
187
errmsg_printf(ERRMSG_LVL_WARN,
188
_("Found %d prepared XA transactions"),
189
recover_func.getForeignXIDs());
190
if (dry_run && recover_func.getMyXIDs())
192
errmsg_printf(ERRMSG_LVL_ERROR,
193
_("Found %d prepared transactions! It means that drizzled "
194
"was not shut down properly last time and critical "
195
"recovery information (last binlog or %s file) was "
196
"manually deleted after a crash. You have to start "
197
"drizzled with the --tc-heuristic-recover switch to "
198
"commit or rollback pending transactions."),
199
recover_func.getMyXIDs(), opt_tc_log_file);
203
errmsg_printf(ERRMSG_LVL_INFO, _("Crash recovery finished."));
207
bool XaResourceManager::addPlugin(XaResourceManager *resource_manager)
209
xa_resource_managers.push_back(resource_manager);
213
void XaResourceManager::removePlugin(XaResourceManager *)
215
xa_resource_managers.clear();
218
} /* namespace plugin */
219
} /* namespace drizzled */