~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
53
namespace drizzled
54
{
1039.5.39 by Jay Pipes
This patch does a couple things in preparation for publisher and
55
1405.6.1 by Jay Pipes
This patch adds the following functionality:
56
ReplicationServices::ReplicationServices() :
57
  is_active(false)
1039.5.5 by Jay Pipes
This commit does two things:
58
{
59
}
60
1405.6.1 by Jay Pipes
This patch adds the following functionality:
61
void ReplicationServices::normalizeReplicatorName(string &name)
1039.5.5 by Jay Pipes
This commit does two things:
62
{
1405.6.1 by Jay Pipes
This patch adds the following functionality:
63
  transform(name.begin(),
64
            name.end(),
65
            name.begin(),
66
            ::tolower);
67
  if (name.find("replicator") == string::npos)
68
    name.append("replicator", 10);
69
  {
70
    size_t found_underscore= name.find('_');
71
    while (found_underscore != string::npos)
1039.5.5 by Jay Pipes
This commit does two things:
72
    {
1405.6.1 by Jay Pipes
This patch adds the following functionality:
73
      name.erase(found_underscore, 1);
74
      found_underscore= name.find('_');
1039.5.5 by Jay Pipes
This commit does two things:
75
    }
76
  }
1405.6.1 by Jay Pipes
This patch adds the following functionality:
77
}
1039.5.5 by Jay Pipes
This commit does two things:
78
1405.6.1 by Jay Pipes
This patch adds the following functionality:
79
bool ReplicationServices::evaluateRegisteredPlugins()
80
{
1039.5.5 by Jay Pipes
This commit does two things:
81
  /* 
1405.6.1 by Jay Pipes
This patch adds the following functionality:
82
   * We loop through appliers that have registered with us
83
   * and attempts to pair the applier with its requested
84
   * replicator.  If an applier has requested a replicator
85
   * that has either not been built or has not registered
86
   * with the replication services, we print an error and
87
   * return false
1039.5.5 by Jay Pipes
This commit does two things:
88
   */
1405.6.1 by Jay Pipes
This patch adds the following functionality:
89
  if (appliers.empty())
90
    return true;
91
92
  if (replicators.empty() && not appliers.empty())
93
  {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
94
    errmsg_printf(error::ERROR,
1405.6.1 by Jay Pipes
This patch adds the following functionality:
95
                  N_("You registered a TransactionApplier plugin but no "
96
                     "TransactionReplicator plugins were registered.\n"));
97
    return false;
98
  }
99
1143.4.7 by Jay Pipes
Removes unused ReplicationServices::startNormalTransaction() and switches from while to for loop in evaluateActivePlugins().
100
  for (Appliers::iterator appl_iter= appliers.begin();
101
       appl_iter != appliers.end();
102
       ++appl_iter)
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
    plugin::TransactionApplier *applier= (*appl_iter).second;
105
    string requested_replicator_name= (*appl_iter).first;
106
    normalizeReplicatorName(requested_replicator_name);
107
108
    bool found= false;
109
    Replicators::iterator repl_iter;
110
    for (repl_iter= replicators.begin();
111
         repl_iter != replicators.end();
112
         ++repl_iter)
113
    {
114
      string replicator_name= (*repl_iter)->getName();
115
      normalizeReplicatorName(replicator_name);
116
117
      if (requested_replicator_name.compare(replicator_name) == 0)
118
      {
119
        found= true;
120
        break;
121
      }
122
    }
123
    if (not found)
124
    {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
125
      errmsg_printf(error::ERROR,
1405.6.1 by Jay Pipes
This patch adds the following functionality:
126
                    N_("You registered a TransactionApplier plugin but no "
127
                       "TransactionReplicator plugins were registered that match the "
128
                       "requested replicator name of '%s'.\n"
129
                       "We have deactivated the TransactionApplier '%s'.\n"),
130
                       requested_replicator_name.c_str(),
131
                       applier->getName().c_str());
132
      applier->deactivate();
133
      return false;
134
    }
135
    else
136
    {
137
      replication_streams.push_back(make_pair(*repl_iter, applier));
1039.5.5 by Jay Pipes
This commit does two things:
138
    }
139
  }
1405.6.1 by Jay Pipes
This patch adds the following functionality:
140
  is_active= true;
141
  return true;
1039.5.5 by Jay Pipes
This commit does two things:
142
}
143
1143.2.10 by Jay Pipes
Phase 2 new replication work:
144
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
145
{
146
  replicators.push_back(in_replicator);
147
}
148
1143.2.10 by Jay Pipes
Phase 2 new replication work:
149
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
150
{
151
  replicators.erase(std::find(replicators.begin(), replicators.end(), in_replicator));
1405.6.1 by Jay Pipes
This patch adds the following functionality:
152
}
153
154
void ReplicationServices::attachApplier(plugin::TransactionApplier *in_applier, const string &requested_replicator_name)
155
{
156
  appliers.push_back(make_pair(requested_replicator_name, in_applier));
157
}
158
159
void ReplicationServices::detachApplier(plugin::TransactionApplier *)
160
{
1039.5.5 by Jay Pipes
This commit does two things:
161
}
162
1039.5.31 by Jay Pipes
This patch does a few things:
163
bool ReplicationServices::isActive() const
1039.5.5 by Jay Pipes
This commit does two things:
164
{
165
  return is_active;
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
166
}
167
1405.3.3 by Jay Pipes
Adds Session reference to replication API
168
plugin::ReplicationReturnCode ReplicationServices::pushTransactionMessage(Session &in_session,
169
                                                                          message::Transaction &to_push)
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
170
{
1405.3.9 by Jay Pipes
Possible that result was returned uninitialized. Initialize to plugin::SUCCESS before iterating across replication streams.
171
  plugin::ReplicationReturnCode result= plugin::SUCCESS;
1405.3.1 by Jay Pipes
Adapts the plugin::TransactionReplicator and plugin::TransactionApplier
172
1405.6.1 by Jay Pipes
This patch adds the following functionality:
173
  for (ReplicationStreams::iterator iter= replication_streams.begin();
174
       iter != replication_streams.end();
175
       ++iter)
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
176
  {
2192.4.7 by Olaf van der Spek
Use "iter->" instead of "(*iter)."
177
    plugin::TransactionReplicator *cur_repl= iter->first;
178
    plugin::TransactionApplier *cur_appl= iter->second;
1405.6.1 by Jay Pipes
This patch adds the following functionality:
179
180
    result= cur_repl->replicate(cur_appl, in_session, to_push);
181
182
    if (result == plugin::SUCCESS)
183
    {
184
      /* 
185
       * We update the timestamp for the last applied Transaction so that
186
       * publisher plugins can ask the replication services when the
187
       * last known applied Transaction was using the getLastAppliedTimestamp()
188
       * method.
189
       */
190
      last_applied_timestamp.fetch_and_store(to_push.transaction_context().end_timestamp());
191
    }
192
    else
193
      return result;
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
194
  }
1405.3.1 by Jay Pipes
Adapts the plugin::TransactionReplicator and plugin::TransactionApplier
195
  return result;
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
196
}
1143.2.10 by Jay Pipes
Phase 2 new replication work:
197
1405.6.1 by Jay Pipes
This patch adds the following functionality:
198
ReplicationServices::ReplicationStreams &ReplicationServices::getReplicationStreams()
199
{
200
  return replication_streams;
201
}
202
1130.3.18 by Monty Taylor
Small namespace cleanup.
203
} /* namespace drizzled */