~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 *  Copyright (C) 2010 Jay Pipes <jaypipes@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>

#include <drizzled/cached_directory.h>

#include <drizzled/definitions.h>
#include <drizzled/session.h>
#include <drizzled/error.h>
#include <drizzled/gettext.h>
#include <drizzled/plugin/xa_resource_manager.h>
#include <drizzled/xid.h>
#include <drizzled/errmsg_print.h>
#include <drizzled/sys_var.h>

#include <string>
#include <vector>
#include <algorithm>
#include <functional>

namespace drizzled {
namespace plugin {

typedef std::vector<XaResourceManager*> xa_resource_managers_t;
static xa_resource_managers_t xa_resource_managers;

int XaResourceManager::commitOrRollbackXID(XID *xid, bool commit)
{
  std::vector<int> results;
  BOOST_FOREACH(XaResourceManager* it, xa_resource_managers)
    results.push_back(commit ? it->xaCommitXid(xid) : it->xaRollbackXid(xid));
  return std::find(results.begin(), results.end(), 0) == results.end();
}

/**
  recover() step of xa.

  @note
    there are three modes of operation:
    - automatic recover after a crash
    in this case commit_list.size() != 0, tc_heuristic_recover==0
    all xids from commit_list are committed, others are rolled back
    - manual (heuristic) recover
    in this case commit_list.size()==0, tc_heuristic_recover != 0
    DBA has explicitly specified that all prepared transactions should
    be committed (or rolled back).
    - no recovery (Drizzle did not detect a crash)
    in this case commit_list.size()==0, tc_heuristic_recover == 0
    there should be no prepared transactions in this case.
*/
class XaRecover : std::unary_function<XaResourceManager *, void>
{
private:
  int trans_len, found_foreign_xids, found_my_xids;
  bool result;
  XID *trans_list;
  const XaResourceManager::commit_list_set &commit_list;
  bool dry_run;
public:
  XaRecover(XID *trans_list_arg, int trans_len_arg,
            const XaResourceManager::commit_list_set& commit_list_arg,
            bool dry_run_arg)
    : trans_len(trans_len_arg), found_foreign_xids(0), found_my_xids(0),
      result(false),
      trans_list(trans_list_arg), commit_list(commit_list_arg),
      dry_run(dry_run_arg)
  {}

  int getForeignXIDs()
  {
    return found_foreign_xids;
  }

  int getMyXIDs()
  {
    return found_my_xids;
  }

  result_type operator() (argument_type resource_manager)
  {

    int got;

    while ((got= resource_manager->xaRecover(trans_list, trans_len)) > 0 )
    {
      errmsg_printf(error::INFO,
                    _("Found %d prepared transaction(s) in resource manager."),
                    got);
      for (int i=0; i < got; i ++)
      {
        my_xid x=trans_list[i].get_my_xid();
        if (!x) // not "mine" - that is generated by external TM
        {
          found_foreign_xids++;
          continue;
        }
        if (dry_run)
        {
          found_my_xids++;
          continue;
        }
        // recovery mode
        if (commit_list.size() ?
            commit_list.find(x) != commit_list.end() :
            tc_heuristic_recover == TC_HEURISTIC_RECOVER_COMMIT)
        {
          resource_manager->xaCommitXid(trans_list+i);
        }
        else
        {
          resource_manager->xaRollbackXid(trans_list+i);
        }
      }
      if (got < trans_len)
        break;
    }
  }
};

int XaResourceManager::recoverAllXids()
{
  const XaResourceManager::commit_list_set empty_commit_set;
  return recoverAllXids(empty_commit_set);
}

int XaResourceManager::recoverAllXids(const XaResourceManager::commit_list_set &commit_list)
{
  XID *trans_list= NULL;
  int trans_len= 0;

  bool dry_run= (commit_list.size() == 0 && tc_heuristic_recover==0);

  /* commit_list and tc_heuristic_recover cannot be set both */
  assert(commit_list.size() == 0 || tc_heuristic_recover == 0);

  if (xa_resource_managers.size() <= 1)
    return 0;

  tc_heuristic_recover= TC_HEURISTIC_RECOVER_ROLLBACK; // forcing ROLLBACK
  dry_run=false;
  for (trans_len= MAX_XID_LIST_SIZE ;
       trans_list==0 && trans_len > MIN_XID_LIST_SIZE; trans_len/=2)
  {
    trans_list=(XID *)malloc(trans_len*sizeof(XID));
  }
  if (!trans_list)
  {
    errmsg_printf(error::ERROR, ER(ER_OUTOFMEMORY), trans_len*sizeof(XID));
    return 1;
  }

  if (commit_list.size())
    errmsg_printf(error::INFO, _("Starting crash recovery..."));

  XaRecover recover_func(trans_list, trans_len, commit_list, dry_run);
  std::for_each(xa_resource_managers.begin(),
                xa_resource_managers.end(),
                recover_func);
  free(trans_list);

  if (recover_func.getForeignXIDs())
    errmsg_printf(error::WARN,
                  _("Found %d prepared XA transactions"),
                  recover_func.getForeignXIDs());

  if (dry_run && recover_func.getMyXIDs())
  {
    errmsg_printf(error::ERROR,
                  _("Found %d prepared transactions! It means that drizzled "
                    "was not shut down properly last time and critical "
                    "recovery information (last binlog or %s file) was "
                    "manually deleted after a crash. You have to start "
                    "drizzled with the --tc-heuristic-recover switch to "
                    "commit or rollback pending transactions."),
                    recover_func.getMyXIDs(), opt_tc_log_file);
    return 1;
  }

  if (commit_list.size())
    errmsg_printf(error::INFO, _("Crash recovery finished."));

  return 0;
}

bool XaResourceManager::addPlugin(XaResourceManager *resource_manager)
{
  xa_resource_managers.push_back(resource_manager);
  return false;
}

void XaResourceManager::removePlugin(XaResourceManager *)
{
  xa_resource_managers.clear();
}

} /* namespace plugin */
} /* namespace drizzled */