~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/xa_resource_manager.cc

Updated an include guard thanks to a nice catch during code review from Jay. Thanks Jay!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
5
 
 *  Copyright (c) 2010 Jay Pipes <jaypipes@gmail.com>
6
 
 *
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.
10
 
 *
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.
15
 
 *
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
19
 
 */
20
 
 
21
 
#include "config.h"
22
 
 
23
 
#include "drizzled/my_hash.h"
24
 
#include "drizzled/cached_directory.h"
25
 
 
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"
32
 
 
33
 
#include "drizzled/hash.h"
34
 
 
35
 
#include <string>
36
 
#include <vector>
37
 
#include <algorithm>
38
 
#include <functional>
39
 
 
40
 
using namespace std;
41
 
 
42
 
namespace drizzled
43
 
{
44
 
 
45
 
namespace plugin
46
 
{
47
 
 
48
 
static vector<XaResourceManager *> xa_resource_managers;
49
 
 
50
 
int XaResourceManager::commitOrRollbackXID(XID *xid, bool commit)
51
 
{
52
 
  vector<int> results;
53
 
  
54
 
  if (commit)
55
 
    transform(xa_resource_managers.begin(), xa_resource_managers.end(), results.begin(),
56
 
              bind2nd(mem_fun(&XaResourceManager::xaCommitXid), xid));
57
 
  else
58
 
    transform(xa_resource_managers.begin(), xa_resource_managers.end(), results.begin(),
59
 
              bind2nd(mem_fun(&XaResourceManager::xaRollbackXid), xid));
60
 
 
61
 
  if (find_if(results.begin(), results.end(), bind2nd(equal_to<int>(),0))
62
 
         == results.end())
63
 
    return 1;
64
 
  return 0;
65
 
}
66
 
 
67
 
/**
68
 
  recover() step of xa.
69
 
 
70
 
  @note
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.
82
 
*/
83
 
class XaRecover : unary_function<XaResourceManager *, void>
84
 
{
85
 
  int trans_len, found_foreign_xids, found_my_xids;
86
 
  bool result;
87
 
  XID *trans_list;
88
 
  HASH *commit_list;
89
 
  bool dry_run;
90
 
public:
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),
94
 
      result(false),
95
 
      trans_list(trans_list_arg), commit_list(commit_list_arg),
96
 
      dry_run(dry_run_arg)
97
 
  {}
98
 
  
99
 
  int getForeignXIDs()
100
 
  {
101
 
    return found_foreign_xids; 
102
 
  }
103
 
 
104
 
  int getMyXIDs()
105
 
  {
106
 
    return found_my_xids; 
107
 
  }
108
 
 
109
 
  result_type operator() (argument_type resource_manager)
110
 
  {
111
 
  
112
 
    int got;
113
 
  
114
 
    while ((got= resource_manager->xaRecover(trans_list, trans_len)) > 0 )
115
 
    {
116
 
      errmsg_printf(ERRMSG_LVL_INFO,
117
 
                    _("Found %d prepared transaction(s) in resource manager."),
118
 
                    got);
119
 
      for (int i=0; i < got; i ++)
120
 
      {
121
 
        my_xid x=trans_list[i].get_my_xid();
122
 
        if (!x) // not "mine" - that is generated by external TM
123
 
        {
124
 
          xid_cache_insert(trans_list+i, XA_PREPARED);
125
 
          found_foreign_xids++;
126
 
          continue;
127
 
        }
128
 
        if (dry_run)
129
 
        {
130
 
          found_my_xids++;
131
 
          continue;
132
 
        }
133
 
        // recovery mode
134
 
        if (commit_list ?
135
 
            hash_search(commit_list, (unsigned char *)&x, sizeof(x)) != 0 :
136
 
            tc_heuristic_recover == TC_HEURISTIC_RECOVER_COMMIT)
137
 
        {
138
 
          resource_manager->xaCommitXid(trans_list+i);
139
 
        }
140
 
        else
141
 
        {
142
 
          resource_manager->xaRollbackXid(trans_list+i);
143
 
        }
144
 
      }
145
 
      if (got < trans_len)
146
 
        break;
147
 
    }
148
 
  }
149
 
};
150
 
 
151
 
int XaResourceManager::recoverAllXids(HASH *commit_list)
152
 
{
153
 
  XID *trans_list= NULL;
154
 
  int trans_len= 0;
155
 
 
156
 
  bool dry_run= (commit_list==0 && tc_heuristic_recover==0);
157
 
 
158
 
  /* commit_list and tc_heuristic_recover cannot be set both */
159
 
  assert(commit_list==0 || tc_heuristic_recover==0);
160
 
 
161
 
  if (xa_resource_managers.size() <= 1)
162
 
    return 0;
163
 
 
164
 
  tc_heuristic_recover= TC_HEURISTIC_RECOVER_ROLLBACK; // forcing ROLLBACK
165
 
  dry_run=false;
166
 
  for (trans_len= MAX_XID_LIST_SIZE ;
167
 
       trans_list==0 && trans_len > MIN_XID_LIST_SIZE; trans_len/=2)
168
 
  {
169
 
    trans_list=(XID *)malloc(trans_len*sizeof(XID));
170
 
  }
171
 
  if (!trans_list)
172
 
  {
173
 
    errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_OUTOFMEMORY), trans_len*sizeof(XID));
174
 
    return(1);
175
 
  }
176
 
 
177
 
  if (commit_list)
178
 
    errmsg_printf(ERRMSG_LVL_INFO, _("Starting crash recovery..."));
179
 
 
180
 
  XaRecover recover_func(trans_list, trans_len, commit_list, dry_run);
181
 
  for_each(xa_resource_managers.begin(),
182
 
           xa_resource_managers.end(),
183
 
           recover_func);
184
 
  free(trans_list);
185
 
 
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())
191
 
  {
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);
200
 
    return(1);
201
 
  }
202
 
  if (commit_list)
203
 
    errmsg_printf(ERRMSG_LVL_INFO, _("Crash recovery finished."));
204
 
  return(0);
205
 
}
206
 
 
207
 
bool XaResourceManager::addPlugin(XaResourceManager *resource_manager)
208
 
{
209
 
  xa_resource_managers.push_back(resource_manager);
210
 
  return false;
211
 
}
212
 
 
213
 
void XaResourceManager::removePlugin(XaResourceManager *)
214
 
{
215
 
  xa_resource_managers.clear();
216
 
}
217
 
 
218
 
} /* namespace plugin */
219
 
} /* namespace drizzled */