~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
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
37
#include <config.h>
38
#include <drizzled/replication_services.h>
39
#include <drizzled/plugin/transaction_replicator.h>
40
#include <drizzled/plugin/transaction_applier.h>
41
#include <drizzled/message/transaction.pb.h>
42
#include <drizzled/gettext.h>
43
#include <drizzled/session.h>
44
#include <drizzled/error.h>
2239.1.6 by Olaf van der Spek
Refactor includes
45
#include <drizzled/errmsg_print.h>
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
46
1405.6.1 by Jay Pipes
This patch adds the following functionality:
47
#include <string>
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
48
#include <vector>
1405.6.1 by Jay Pipes
This patch adds the following functionality:
49
#include <algorithm>
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
50
1039.5.39 by Jay Pipes
This patch does a couple things in preparation for publisher and
51
using namespace std;
1130.3.18 by Monty Taylor
Small namespace cleanup.
52
2318.6.68 by Olaf van der Spek
Refactor
53
namespace drizzled {
54
55
typedef std::vector<plugin::TransactionReplicator*> Replicators;
56
typedef std::vector<std::pair<std::string, plugin::TransactionApplier*> > Appliers;
57
58
/** 
59
  * Atomic boolean set to true if any *active* replicators
60
  * or appliers are actually registered.
61
  */
62
static bool is_active= false;
63
/**
64
  * The timestamp of the last time a Transaction message was successfully
65
  * applied (sent to an Applier)
66
  */
67
static atomic<uint64_t> last_applied_timestamp;
68
/** Our collection of registered replicator plugins */
69
static Replicators replicators;
70
/** Our collection of registered applier plugins and their requested replicator plugin names */
71
static Appliers appliers;
72
/** Our replication streams */
73
static ReplicationServices::ReplicationStreams replication_streams;
74
75
  /**
76
   * Strips underscores and lowercases supplied replicator name
77
   * or requested name, and appends the suffix "replicator" if missing...
78
   */
79
static void normalizeReplicatorName(string &name)
80
{
2318.7.21 by Olaf van der Spek
Use boost::to_lower
81
  boost::to_lower(name);
1405.6.1 by Jay Pipes
This patch adds the following functionality:
82
  if (name.find("replicator") == string::npos)
2318.7.21 by Olaf van der Spek
Use boost::to_lower
83
    name.append("replicator");
1405.6.1 by Jay Pipes
This patch adds the following functionality:
84
  {
85
    size_t found_underscore= name.find('_');
86
    while (found_underscore != string::npos)
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
      name.erase(found_underscore, 1);
89
      found_underscore= name.find('_');
1039.5.5 by Jay Pipes
This commit does two things:
90
    }
91
  }
1405.6.1 by Jay Pipes
This patch adds the following functionality:
92
}
1039.5.5 by Jay Pipes
This commit does two things:
93
1405.6.1 by Jay Pipes
This patch adds the following functionality:
94
bool ReplicationServices::evaluateRegisteredPlugins()
95
{
1039.5.5 by Jay Pipes
This commit does two things:
96
  /* 
1405.6.1 by Jay Pipes
This patch adds the following functionality:
97
   * We loop through appliers that have registered with us
98
   * and attempts to pair the applier with its requested
99
   * replicator.  If an applier has requested a replicator
100
   * that has either not been built or has not registered
101
   * with the replication services, we print an error and
102
   * return false
1039.5.5 by Jay Pipes
This commit does two things:
103
   */
1405.6.1 by Jay Pipes
This patch adds the following functionality:
104
  if (appliers.empty())
105
    return true;
106
107
  if (replicators.empty() && not appliers.empty())
108
  {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
109
    errmsg_printf(error::ERROR,
1405.6.1 by Jay Pipes
This patch adds the following functionality:
110
                  N_("You registered a TransactionApplier plugin but no "
111
                     "TransactionReplicator plugins were registered.\n"));
112
    return false;
113
  }
114
2318.6.63 by Olaf van der Spek
Refactor
115
  BOOST_FOREACH(Appliers::reference appl_iter, appliers)
1039.5.5 by Jay Pipes
This commit does two things:
116
  {
2318.6.63 by Olaf van der Spek
Refactor
117
    plugin::TransactionApplier *applier= appl_iter.second;
118
    string requested_replicator_name= appl_iter.first;
1405.6.1 by Jay Pipes
This patch adds the following functionality:
119
    normalizeReplicatorName(requested_replicator_name);
120
121
    bool found= false;
122
    Replicators::iterator repl_iter;
2318.6.63 by Olaf van der Spek
Refactor
123
    for (repl_iter= replicators.begin(); repl_iter != replicators.end(); ++repl_iter)
1405.6.1 by Jay Pipes
This patch adds the following functionality:
124
    {
125
      string replicator_name= (*repl_iter)->getName();
126
      normalizeReplicatorName(replicator_name);
127
128
      if (requested_replicator_name.compare(replicator_name) == 0)
129
      {
130
        found= true;
131
        break;
132
      }
133
    }
134
    if (not found)
135
    {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
136
      errmsg_printf(error::ERROR,
1405.6.1 by Jay Pipes
This patch adds the following functionality:
137
                    N_("You registered a TransactionApplier plugin but no "
138
                       "TransactionReplicator plugins were registered that match the "
139
                       "requested replicator name of '%s'.\n"
140
                       "We have deactivated the TransactionApplier '%s'.\n"),
141
                       requested_replicator_name.c_str(),
142
                       applier->getName().c_str());
143
      applier->deactivate();
144
      return false;
145
    }
146
    else
147
    {
148
      replication_streams.push_back(make_pair(*repl_iter, applier));
1039.5.5 by Jay Pipes
This commit does two things:
149
    }
150
  }
1405.6.1 by Jay Pipes
This patch adds the following functionality:
151
  is_active= true;
152
  return true;
1039.5.5 by Jay Pipes
This commit does two things:
153
}
154
1143.2.10 by Jay Pipes
Phase 2 new replication work:
155
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
156
{
157
  replicators.push_back(in_replicator);
158
}
159
1143.2.10 by Jay Pipes
Phase 2 new replication work:
160
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
161
{
162
  replicators.erase(std::find(replicators.begin(), replicators.end(), in_replicator));
1405.6.1 by Jay Pipes
This patch adds the following functionality:
163
}
164
165
void ReplicationServices::attachApplier(plugin::TransactionApplier *in_applier, const string &requested_replicator_name)
166
{
167
  appliers.push_back(make_pair(requested_replicator_name, in_applier));
168
}
169
170
void ReplicationServices::detachApplier(plugin::TransactionApplier *)
171
{
1039.5.5 by Jay Pipes
This commit does two things:
172
}
173
2318.6.72 by Olaf van der Spek
Refactor
174
bool ReplicationServices::isActive()
1039.5.5 by Jay Pipes
This commit does two things:
175
{
176
  return is_active;
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
177
}
178
1405.3.3 by Jay Pipes
Adds Session reference to replication API
179
plugin::ReplicationReturnCode ReplicationServices::pushTransactionMessage(Session &in_session,
180
                                                                          message::Transaction &to_push)
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
181
{
1405.3.9 by Jay Pipes
Possible that result was returned uninitialized. Initialize to plugin::SUCCESS before iterating across replication streams.
182
  plugin::ReplicationReturnCode result= plugin::SUCCESS;
1405.3.1 by Jay Pipes
Adapts the plugin::TransactionReplicator and plugin::TransactionApplier
183
2318.6.63 by Olaf van der Spek
Refactor
184
  BOOST_FOREACH(ReplicationStreams::reference iter, replication_streams)
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
185
  {
2318.6.63 by Olaf van der Spek
Refactor
186
    plugin::TransactionReplicator *cur_repl= iter.first;
187
    plugin::TransactionApplier *cur_appl= iter.second;
1405.6.1 by Jay Pipes
This patch adds the following functionality:
188
189
    result= cur_repl->replicate(cur_appl, in_session, to_push);
190
191
    if (result == plugin::SUCCESS)
192
    {
193
      /* 
194
       * We update the timestamp for the last applied Transaction so that
195
       * publisher plugins can ask the replication services when the
196
       * last known applied Transaction was using the getLastAppliedTimestamp()
197
       * method.
198
       */
199
      last_applied_timestamp.fetch_and_store(to_push.transaction_context().end_timestamp());
200
    }
201
    else
202
      return result;
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
203
  }
1405.3.1 by Jay Pipes
Adapts the plugin::TransactionReplicator and plugin::TransactionApplier
204
  return result;
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
205
}
1143.2.10 by Jay Pipes
Phase 2 new replication work:
206
1405.6.1 by Jay Pipes
This patch adds the following functionality:
207
ReplicationServices::ReplicationStreams &ReplicationServices::getReplicationStreams()
208
{
209
  return replication_streams;
210
}
211
1130.3.18 by Monty Taylor
Small namespace cleanup.
212
} /* namespace drizzled */