~drizzle-trunk/drizzle/development

636.1.1 by Mark Atwood
add replicator plugin type
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
4
 *  Copyright (C) 2008-2009 Sun Microsystems
1336.2.2 by Jay Pipes
NO CODE CHANGES - Simply moves pieces of ReplicationServices to TransactionServices. Preparation for making the ReplicationServices component only responsible for communication between replicators, appliers, publishers, and subscribers.
5
 *  Copyright (c) 2009-2010 Jay Pipes <jaypipes@gmail.com>
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
6
 *
7
 *  Authors:
8
 *
1336.2.2 by Jay Pipes
NO CODE CHANGES - Simply moves pieces of ReplicationServices to TransactionServices. Preparation for making the ReplicationServices component only responsible for communication between replicators, appliers, publishers, and subscribers.
9
 *    Jay Pipes <jaypipes@gmail.com>
636.1.1 by Mark Atwood
add replicator plugin type
10
 *
11
 *  This program is free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; version 2 of the License.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program; if not, write to the Free Software
22
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23
 */
24
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
25
/**
26
 * @file Server-side utility which is responsible for managing the 
1336.2.2 by Jay Pipes
NO CODE CHANGES - Simply moves pieces of ReplicationServices to TransactionServices. Preparation for making the ReplicationServices component only responsible for communication between replicators, appliers, publishers, and subscribers.
27
 * communication between the kernel and the replication plugins:
28
 *
29
 * - TransactionReplicator
30
 * - TransactionApplier
31
 * - Publisher
32
 * - Subscriber
33
 *
34
 * ReplicationServices is a bridge between replication modules and the kernel,
35
 * and its primary function is to  */
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
36
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
37
#include "config.h"
1039.5.31 by Jay Pipes
This patch does a few things:
38
#include "drizzled/replication_services.h"
1143.2.10 by Jay Pipes
Phase 2 new replication work:
39
#include "drizzled/plugin/transaction_replicator.h"
40
#include "drizzled/plugin/transaction_applier.h"
41
#include "drizzled/message/transaction.pb.h"
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
42
#include "drizzled/gettext.h"
43
#include "drizzled/session.h"
1143.4.15 by Jay Pipes
Adds error for when a record is inserted into a table containing
44
#include "drizzled/error.h"
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
45
1405.6.1 by Jay Pipes
This patch adds the following functionality:
46
#include <string>
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
47
#include <vector>
1405.6.1 by Jay Pipes
This patch adds the following functionality:
48
#include <algorithm>
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
49
1039.5.39 by Jay Pipes
This patch does a couple things in preparation for publisher and
50
using namespace std;
1130.3.18 by Monty Taylor
Small namespace cleanup.
51
52
namespace drizzled
53
{
1039.5.39 by Jay Pipes
This patch does a couple things in preparation for publisher and
54
1405.6.1 by Jay Pipes
This patch adds the following functionality:
55
ReplicationServices::ReplicationServices() :
56
  is_active(false)
1039.5.5 by Jay Pipes
This commit does two things:
57
{
58
}
59
1405.6.1 by Jay Pipes
This patch adds the following functionality:
60
void ReplicationServices::normalizeReplicatorName(string &name)
1039.5.5 by Jay Pipes
This commit does two things:
61
{
1405.6.1 by Jay Pipes
This patch adds the following functionality:
62
  transform(name.begin(),
63
            name.end(),
64
            name.begin(),
65
            ::tolower);
66
  if (name.find("replicator") == string::npos)
67
    name.append("replicator", 10);
68
  {
69
    size_t found_underscore= name.find('_');
70
    while (found_underscore != string::npos)
1039.5.5 by Jay Pipes
This commit does two things:
71
    {
1405.6.1 by Jay Pipes
This patch adds the following functionality:
72
      name.erase(found_underscore, 1);
73
      found_underscore= name.find('_');
1039.5.5 by Jay Pipes
This commit does two things:
74
    }
75
  }
1405.6.1 by Jay Pipes
This patch adds the following functionality:
76
}
1039.5.5 by Jay Pipes
This commit does two things:
77
1405.6.1 by Jay Pipes
This patch adds the following functionality:
78
bool ReplicationServices::evaluateRegisteredPlugins()
79
{
1039.5.5 by Jay Pipes
This commit does two things:
80
  /* 
1405.6.1 by Jay Pipes
This patch adds the following functionality:
81
   * We loop through appliers that have registered with us
82
   * and attempts to pair the applier with its requested
83
   * replicator.  If an applier has requested a replicator
84
   * that has either not been built or has not registered
85
   * with the replication services, we print an error and
86
   * return false
1039.5.5 by Jay Pipes
This commit does two things:
87
   */
1405.6.1 by Jay Pipes
This patch adds the following functionality:
88
  if (appliers.empty())
89
    return true;
90
91
  if (replicators.empty() && not appliers.empty())
92
  {
93
    errmsg_printf(ERRMSG_LVL_ERROR,
94
                  N_("You registered a TransactionApplier plugin but no "
95
                     "TransactionReplicator plugins were registered.\n"));
96
    return false;
97
  }
98
1143.4.7 by Jay Pipes
Removes unused ReplicationServices::startNormalTransaction() and switches from while to for loop in evaluateActivePlugins().
99
  for (Appliers::iterator appl_iter= appliers.begin();
100
       appl_iter != appliers.end();
101
       ++appl_iter)
1039.5.5 by Jay Pipes
This commit does two things:
102
  {
1405.6.1 by Jay Pipes
This patch adds the following functionality:
103
    plugin::TransactionApplier *applier= (*appl_iter).second;
104
    string requested_replicator_name= (*appl_iter).first;
105
    normalizeReplicatorName(requested_replicator_name);
106
107
    bool found= false;
108
    Replicators::iterator repl_iter;
109
    for (repl_iter= replicators.begin();
110
         repl_iter != replicators.end();
111
         ++repl_iter)
112
    {
113
      string replicator_name= (*repl_iter)->getName();
114
      normalizeReplicatorName(replicator_name);
115
116
      if (requested_replicator_name.compare(replicator_name) == 0)
117
      {
118
        found= true;
119
        break;
120
      }
121
    }
122
    if (not found)
123
    {
124
      errmsg_printf(ERRMSG_LVL_ERROR,
125
                    N_("You registered a TransactionApplier plugin but no "
126
                       "TransactionReplicator plugins were registered that match the "
127
                       "requested replicator name of '%s'.\n"
128
                       "We have deactivated the TransactionApplier '%s'.\n"),
129
                       requested_replicator_name.c_str(),
130
                       applier->getName().c_str());
131
      applier->deactivate();
132
      return false;
133
    }
134
    else
135
    {
136
      replication_streams.push_back(make_pair(*repl_iter, applier));
1039.5.5 by Jay Pipes
This commit does two things:
137
    }
138
  }
1405.6.1 by Jay Pipes
This patch adds the following functionality:
139
  is_active= true;
140
  return true;
1039.5.5 by Jay Pipes
This commit does two things:
141
}
142
1143.2.10 by Jay Pipes
Phase 2 new replication work:
143
void ReplicationServices::attachReplicator(plugin::TransactionReplicator *in_replicator)
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
144
{
145
  replicators.push_back(in_replicator);
146
}
147
1143.2.10 by Jay Pipes
Phase 2 new replication work:
148
void ReplicationServices::detachReplicator(plugin::TransactionReplicator *in_replicator)
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
149
{
150
  replicators.erase(std::find(replicators.begin(), replicators.end(), in_replicator));
1405.6.1 by Jay Pipes
This patch adds the following functionality:
151
}
152
153
void ReplicationServices::attachApplier(plugin::TransactionApplier *in_applier, const string &requested_replicator_name)
154
{
155
  appliers.push_back(make_pair(requested_replicator_name, in_applier));
156
}
157
158
void ReplicationServices::detachApplier(plugin::TransactionApplier *)
159
{
1039.5.5 by Jay Pipes
This commit does two things:
160
}
161
1039.5.31 by Jay Pipes
This patch does a few things:
162
bool ReplicationServices::isActive() const
1039.5.5 by Jay Pipes
This commit does two things:
163
{
164
  return is_active;
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
165
}
166
1405.3.3 by Jay Pipes
Adds Session reference to replication API
167
plugin::ReplicationReturnCode ReplicationServices::pushTransactionMessage(Session &in_session,
168
                                                                          message::Transaction &to_push)
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
169
{
1405.3.9 by Jay Pipes
Possible that result was returned uninitialized. Initialize to plugin::SUCCESS before iterating across replication streams.
170
  plugin::ReplicationReturnCode result= plugin::SUCCESS;
1405.3.1 by Jay Pipes
Adapts the plugin::TransactionReplicator and plugin::TransactionApplier
171
1405.6.1 by Jay Pipes
This patch adds the following functionality:
172
  for (ReplicationStreams::iterator iter= replication_streams.begin();
173
       iter != replication_streams.end();
174
       ++iter)
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
175
  {
1405.6.1 by Jay Pipes
This patch adds the following functionality:
176
    plugin::TransactionReplicator *cur_repl= (*iter).first;
177
    plugin::TransactionApplier *cur_appl= (*iter).second;
178
179
    result= cur_repl->replicate(cur_appl, in_session, to_push);
180
181
    if (result == plugin::SUCCESS)
182
    {
183
      /* 
184
       * We update the timestamp for the last applied Transaction so that
185
       * publisher plugins can ask the replication services when the
186
       * last known applied Transaction was using the getLastAppliedTimestamp()
187
       * method.
188
       */
189
      last_applied_timestamp.fetch_and_store(to_push.transaction_context().end_timestamp());
190
    }
191
    else
192
      return result;
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
193
  }
1405.3.1 by Jay Pipes
Adapts the plugin::TransactionReplicator and plugin::TransactionApplier
194
  return result;
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
195
}
1143.2.10 by Jay Pipes
Phase 2 new replication work:
196
1405.6.1 by Jay Pipes
This patch adds the following functionality:
197
ReplicationServices::ReplicationStreams &ReplicationServices::getReplicationStreams()
198
{
199
  return replication_streams;
200
}
201
1130.3.18 by Monty Taylor
Small namespace cleanup.
202
} /* namespace drizzled */