1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
5
* Copyright (C) 2010 Jay Pipes <jaypipes@gmail.com>
4
* Copyright (C) 2008 Sun Microsystems
5
* Copyright (c) 2010 Jay Pipes <jaypipes@gmail.com>
7
7
* This program is free software; you can redistribute it and/or modify
8
8
* it under the terms of the GNU General Public License as published by
18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
#include <drizzled/cached_directory.h>
23
#include "drizzled/my_hash.h"
24
#include "drizzled/cached_directory.h"
25
26
#include <drizzled/definitions.h>
26
27
#include <drizzled/session.h>
27
28
#include <drizzled/error.h>
28
29
#include <drizzled/gettext.h>
29
30
#include <drizzled/plugin/xa_resource_manager.h>
30
#include <drizzled/xid.h>
31
#include "drizzled/xid.h"
35
36
#include <algorithm>
36
37
#include <functional>
44
static std::vector<XaResourceManager *> xa_resource_managers;
47
static vector<XaResourceManager *> xa_resource_managers;
46
49
int XaResourceManager::commitOrRollbackXID(XID *xid, bool commit)
48
std::vector<int> results;
51
54
transform(xa_resource_managers.begin(), xa_resource_managers.end(), results.begin(),
52
std::bind2nd(std::mem_fun(&XaResourceManager::xaCommitXid), xid));
55
bind2nd(mem_fun(&XaResourceManager::xaCommitXid), xid));
54
57
transform(xa_resource_managers.begin(), xa_resource_managers.end(), results.begin(),
55
std::bind2nd(std::mem_fun(&XaResourceManager::xaRollbackXid), xid));
58
bind2nd(mem_fun(&XaResourceManager::xaRollbackXid), xid));
57
if (std::find_if(results.begin(), results.end(), std::bind2nd(std::equal_to<int>(),0)) == results.end())
60
if (find_if(results.begin(), results.end(), bind2nd(equal_to<int>(),0))
67
70
there are three modes of operation:
68
71
- automatic recover after a crash
69
in this case commit_list.size() != 0, tc_heuristic_recover==0
72
in this case commit_list != 0, tc_heuristic_recover==0
70
73
all xids from commit_list are committed, others are rolled back
71
74
- manual (heuristic) recover
72
in this case commit_list.size()==0, tc_heuristic_recover != 0
75
in this case commit_list==0, tc_heuristic_recover != 0
73
76
DBA has explicitly specified that all prepared transactions should
74
77
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
78
- no recovery (MySQL did not detect a crash)
79
in this case commit_list==0, tc_heuristic_recover == 0
77
80
there should be no prepared transactions in this case.
79
class XaRecover : std::unary_function<XaResourceManager *, void>
82
class XaRecover : unary_function<XaResourceManager *, void>
82
84
int trans_len, found_foreign_xids, found_my_xids;
85
const XaResourceManager::commit_list_set &commit_list;
88
90
XaRecover(XID *trans_list_arg, int trans_len_arg,
89
const XaResourceManager::commit_list_set& commit_list_arg,
91
HASH *commit_list_arg, bool dry_run_arg)
91
92
: trans_len(trans_len_arg), found_foreign_xids(0), found_my_xids(0),
93
94
trans_list(trans_list_arg), commit_list(commit_list_arg),
112
113
while ((got= resource_manager->xaRecover(trans_list, trans_len)) > 0 )
114
errmsg_printf(error::INFO,
115
errmsg_printf(ERRMSG_LVL_INFO,
115
116
_("Found %d prepared transaction(s) in resource manager."),
117
118
for (int i=0; i < got; i ++)
132
if (commit_list.size() ?
133
commit_list.find(x) != commit_list.end() :
134
hash_search(commit_list, (unsigned char *)&x, sizeof(x)) != 0 :
134
135
tc_heuristic_recover == TC_HEURISTIC_RECOVER_COMMIT)
136
137
resource_manager->xaCommitXid(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)
150
int XaResourceManager::recoverAllXids(HASH *commit_list)
157
152
XID *trans_list= NULL;
158
153
int trans_len= 0;
160
bool dry_run= (commit_list.size() == 0 && tc_heuristic_recover==0);
155
bool dry_run= (commit_list==0 && tc_heuristic_recover==0);
162
157
/* commit_list and tc_heuristic_recover cannot be set both */
163
assert(commit_list.size() == 0 || tc_heuristic_recover == 0);
158
assert(commit_list==0 || tc_heuristic_recover==0);
165
160
if (xa_resource_managers.size() <= 1)
177
errmsg_printf(error::ERROR, ER(ER_OUTOFMEMORY), trans_len*sizeof(XID));
172
errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_OUTOFMEMORY), trans_len*sizeof(XID));
181
if (commit_list.size())
182
errmsg_printf(error::INFO, _("Starting crash recovery..."));
177
errmsg_printf(ERRMSG_LVL_INFO, _("Starting crash recovery..."));
184
179
XaRecover recover_func(trans_list, trans_len, commit_list, dry_run);
185
std::for_each(xa_resource_managers.begin(),
186
xa_resource_managers.end(),
180
for_each(xa_resource_managers.begin(),
181
xa_resource_managers.end(),
188
183
free(trans_list);
190
185
if (recover_func.getForeignXIDs())
191
errmsg_printf(error::WARN,
186
errmsg_printf(ERRMSG_LVL_WARN,
192
187
_("Found %d prepared XA transactions"),
193
188
recover_func.getForeignXIDs());
195
189
if (dry_run && recover_func.getMyXIDs())
197
errmsg_printf(error::ERROR,
191
errmsg_printf(ERRMSG_LVL_ERROR,
198
192
_("Found %d prepared transactions! It means that drizzled "
199
193
"was not shut down properly last time and critical "
200
194
"recovery information (last binlog or %s file) was "
204
198
recover_func.getMyXIDs(), opt_tc_log_file);
208
if (commit_list.size())
209
errmsg_printf(error::INFO, _("Crash recovery finished."));
202
errmsg_printf(ERRMSG_LVL_INFO, _("Crash recovery finished."));