~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/xa_resource_manager.cc

Merged vcol stuff.

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, Inc.
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/cached_directory.h"
24
 
 
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"
31
 
 
32
 
 
33
 
#include <string>
34
 
#include <vector>
35
 
#include <algorithm>
36
 
#include <functional>
37
 
 
38
 
namespace drizzled
39
 
{
40
 
 
41
 
namespace plugin
42
 
{
43
 
 
44
 
static std::vector<XaResourceManager *> xa_resource_managers;
45
 
 
46
 
int XaResourceManager::commitOrRollbackXID(XID *xid, bool commit)
47
 
{
48
 
  std::vector<int> results;
49
 
  
50
 
  if (commit)
51
 
    transform(xa_resource_managers.begin(), xa_resource_managers.end(), results.begin(),
52
 
              std::bind2nd(std::mem_fun(&XaResourceManager::xaCommitXid), xid));
53
 
  else
54
 
    transform(xa_resource_managers.begin(), xa_resource_managers.end(), results.begin(),
55
 
              std::bind2nd(std::mem_fun(&XaResourceManager::xaRollbackXid), xid));
56
 
 
57
 
  if (std::find_if(results.begin(), results.end(), std::bind2nd(std::equal_to<int>(),0)) == results.end())
58
 
    return 1;
59
 
 
60
 
  return 0;
61
 
}
62
 
 
63
 
/**
64
 
  recover() step of xa.
65
 
 
66
 
  @note
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.
78
 
*/
79
 
class XaRecover : std::unary_function<XaResourceManager *, void>
80
 
{
81
 
private:
82
 
  int trans_len, found_foreign_xids, found_my_xids;
83
 
  bool result;
84
 
  XID *trans_list;
85
 
  const XaResourceManager::commit_list_set &commit_list;
86
 
  bool dry_run;
87
 
public:
88
 
  XaRecover(XID *trans_list_arg, int trans_len_arg,
89
 
            const XaResourceManager::commit_list_set& commit_list_arg,
90
 
            bool dry_run_arg) 
91
 
    : trans_len(trans_len_arg), found_foreign_xids(0), found_my_xids(0),
92
 
      result(false),
93
 
      trans_list(trans_list_arg), commit_list(commit_list_arg),
94
 
      dry_run(dry_run_arg)
95
 
  {}
96
 
  
97
 
  int getForeignXIDs()
98
 
  {
99
 
    return found_foreign_xids; 
100
 
  }
101
 
 
102
 
  int getMyXIDs()
103
 
  {
104
 
    return found_my_xids; 
105
 
  }
106
 
 
107
 
  result_type operator() (argument_type resource_manager)
108
 
  {
109
 
  
110
 
    int got;
111
 
  
112
 
    while ((got= resource_manager->xaRecover(trans_list, trans_len)) > 0 )
113
 
    {
114
 
      errmsg_printf(error::INFO,
115
 
                    _("Found %d prepared transaction(s) in resource manager."),
116
 
                    got);
117
 
      for (int i=0; i < got; i ++)
118
 
      {
119
 
        my_xid x=trans_list[i].get_my_xid();
120
 
        if (!x) // not "mine" - that is generated by external TM
121
 
        {
122
 
          xid_cache_insert(trans_list+i, XA_PREPARED);
123
 
          found_foreign_xids++;
124
 
          continue;
125
 
        }
126
 
        if (dry_run)
127
 
        {
128
 
          found_my_xids++;
129
 
          continue;
130
 
        }
131
 
        // recovery mode
132
 
        if (commit_list.size() ?
133
 
            commit_list.find(x) != commit_list.end() :
134
 
            tc_heuristic_recover == TC_HEURISTIC_RECOVER_COMMIT)
135
 
        {
136
 
          resource_manager->xaCommitXid(trans_list+i);
137
 
        }
138
 
        else
139
 
        {
140
 
          resource_manager->xaRollbackXid(trans_list+i);
141
 
        }
142
 
      }
143
 
      if (got < trans_len)
144
 
        break;
145
 
    }
146
 
  }
147
 
};
148
 
 
149
 
int XaResourceManager::recoverAllXids()
150
 
{
151
 
  const XaResourceManager::commit_list_set empty_commit_set;
152
 
  return recoverAllXids(empty_commit_set);
153
 
}
154
 
 
155
 
int XaResourceManager::recoverAllXids(const XaResourceManager::commit_list_set &commit_list)
156
 
{
157
 
  XID *trans_list= NULL;
158
 
  int trans_len= 0;
159
 
 
160
 
  bool dry_run= (commit_list.size() == 0 && tc_heuristic_recover==0);
161
 
 
162
 
  /* commit_list and tc_heuristic_recover cannot be set both */
163
 
  assert(commit_list.size() == 0 || tc_heuristic_recover == 0);
164
 
 
165
 
  if (xa_resource_managers.size() <= 1)
166
 
    return 0;
167
 
 
168
 
  tc_heuristic_recover= TC_HEURISTIC_RECOVER_ROLLBACK; // forcing ROLLBACK
169
 
  dry_run=false;
170
 
  for (trans_len= MAX_XID_LIST_SIZE ;
171
 
       trans_list==0 && trans_len > MIN_XID_LIST_SIZE; trans_len/=2)
172
 
  {
173
 
    trans_list=(XID *)malloc(trans_len*sizeof(XID));
174
 
  }
175
 
  if (!trans_list)
176
 
  {
177
 
    errmsg_printf(error::ERROR, ER(ER_OUTOFMEMORY), trans_len*sizeof(XID));
178
 
    return(1);
179
 
  }
180
 
 
181
 
  if (commit_list.size())
182
 
    errmsg_printf(error::INFO, _("Starting crash recovery..."));
183
 
 
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(),
187
 
                recover_func);
188
 
  free(trans_list);
189
 
 
190
 
  if (recover_func.getForeignXIDs())
191
 
    errmsg_printf(error::WARN,
192
 
                  _("Found %d prepared XA transactions"),
193
 
                  recover_func.getForeignXIDs());
194
 
 
195
 
  if (dry_run && recover_func.getMyXIDs())
196
 
  {
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);
205
 
    return(1);
206
 
  }
207
 
 
208
 
  if (commit_list.size())
209
 
    errmsg_printf(error::INFO, _("Crash recovery finished."));
210
 
 
211
 
  return(0);
212
 
}
213
 
 
214
 
bool XaResourceManager::addPlugin(XaResourceManager *resource_manager)
215
 
{
216
 
  xa_resource_managers.push_back(resource_manager);
217
 
  return false;
218
 
}
219
 
 
220
 
void XaResourceManager::removePlugin(XaResourceManager *)
221
 
{
222
 
  xa_resource_managers.clear();
223
 
}
224
 
 
225
 
} /* namespace plugin */
226
 
} /* namespace drizzled */