~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/replication_services.cc

  • Committer: Monty Taylor
  • Date: 2008-11-16 20:15:33 UTC
  • mto: (584.1.9 devel)
  • mto: This revision was merged to the branch mainline in revision 589.
  • Revision ID: monty@inaugust.com-20081116201533-d0f19s1bk1h95iyw
Removed a big bank of includes from item.h.

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-2009 Sun Microsystems
5
 
 *  Copyright (c) 2009-2010 Jay Pipes <jaypipes@gmail.com>
6
 
 *
7
 
 *  Authors:
8
 
 *
9
 
 *    Jay Pipes <jaypipes@gmail.com>
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
 
 
25
 
/**
26
 
 * @file Server-side utility which is responsible for managing the 
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  */
36
 
 
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/message/table.pb.h"
43
 
#include "drizzled/message/statement_transform.h"
44
 
#include "drizzled/gettext.h"
45
 
#include "drizzled/session.h"
46
 
#include "drizzled/error.h"
47
 
 
48
 
#include <vector>
49
 
 
50
 
using namespace std;
51
 
 
52
 
namespace drizzled
53
 
{
54
 
 
55
 
ReplicationServices::ReplicationServices()
56
 
{
57
 
  is_active= false;
58
 
}
59
 
 
60
 
void ReplicationServices::evaluateActivePlugins()
61
 
{
62
 
  /* 
63
 
   * We loop through replicators and appliers, evaluating
64
 
   * whether or not there is at least one active replicator
65
 
   * and one active applier.  If not, we set is_active
66
 
   * to false.
67
 
   */
68
 
  bool tmp_is_active= false;
69
 
 
70
 
  if (replicators.empty() || appliers.empty())
71
 
  {
72
 
    is_active= false;
73
 
    return;
74
 
  }
75
 
 
76
 
  /* 
77
 
   * Determine if any remaining replicators and if those
78
 
   * replicators are active...if not, set is_active
79
 
   * to false
80
 
   */
81
 
  for (Replicators::iterator repl_iter= replicators.begin();
82
 
       repl_iter != replicators.end();
83
 
       ++repl_iter)
84
 
  {
85
 
    if ((*repl_iter)->isEnabled())
86
 
    {
87
 
      tmp_is_active= true;
88
 
      break;
89
 
    }
90
 
  }
91
 
  if (! tmp_is_active)
92
 
  {
93
 
    /* No active replicators. Set is_active to false and exit. */
94
 
    is_active= false;
95
 
    return;
96
 
  }
97
 
 
98
 
  /* 
99
 
   * OK, we know there's at least one active replicator.
100
 
   *
101
 
   * Now determine if any remaining replicators and if those
102
 
   * replicators are active...if not, set is_active
103
 
   * to false
104
 
   */
105
 
  for (Appliers::iterator appl_iter= appliers.begin();
106
 
       appl_iter != appliers.end();
107
 
       ++appl_iter)
108
 
  {
109
 
    if ((*appl_iter)->isEnabled())
110
 
    {
111
 
      is_active= true;
112
 
      return;
113
 
    }
114
 
  }
115
 
  /* If we get here, there are no active appliers */
116
 
  is_active= false;
117
 
}
118
 
 
119
 
void ReplicationServices::attachReplicator(plugin::TransactionReplicator *in_replicator)
120
 
{
121
 
  replicators.push_back(in_replicator);
122
 
  evaluateActivePlugins();
123
 
}
124
 
 
125
 
void ReplicationServices::detachReplicator(plugin::TransactionReplicator *in_replicator)
126
 
{
127
 
  replicators.erase(std::find(replicators.begin(), replicators.end(), in_replicator));
128
 
  evaluateActivePlugins();
129
 
}
130
 
 
131
 
void ReplicationServices::attachApplier(plugin::TransactionApplier *in_applier)
132
 
{
133
 
  appliers.push_back(in_applier);
134
 
  evaluateActivePlugins();
135
 
}
136
 
 
137
 
void ReplicationServices::detachApplier(plugin::TransactionApplier *in_applier)
138
 
{
139
 
  appliers.erase(std::find(appliers.begin(), appliers.end(), in_applier));
140
 
  evaluateActivePlugins();
141
 
}
142
 
 
143
 
bool ReplicationServices::isActive() const
144
 
{
145
 
  return is_active;
146
 
}
147
 
 
148
 
void ReplicationServices::pushTransactionMessage(message::Transaction &to_push)
149
 
{
150
 
  vector<plugin::TransactionReplicator *>::iterator repl_iter= replicators.begin();
151
 
  vector<plugin::TransactionApplier *>::iterator appl_start_iter, appl_iter;
152
 
  appl_start_iter= appliers.begin();
153
 
 
154
 
  plugin::TransactionReplicator *cur_repl;
155
 
  plugin::TransactionApplier *cur_appl;
156
 
 
157
 
  while (repl_iter != replicators.end())
158
 
  {
159
 
    cur_repl= *repl_iter;
160
 
    if (! cur_repl->isEnabled())
161
 
    {
162
 
      ++repl_iter;
163
 
      continue;
164
 
    }
165
 
    
166
 
    appl_iter= appl_start_iter;
167
 
    while (appl_iter != appliers.end())
168
 
    {
169
 
      cur_appl= *appl_iter;
170
 
 
171
 
      if (! cur_appl->isEnabled())
172
 
      {
173
 
        ++appl_iter;
174
 
        continue;
175
 
      }
176
 
 
177
 
      cur_repl->replicate(cur_appl, to_push);
178
 
      
179
 
      /* 
180
 
       * We update the timestamp for the last applied Transaction so that
181
 
       * publisher plugins can ask the replication services when the
182
 
       * last known applied Transaction was using the getLastAppliedTimestamp()
183
 
       * method.
184
 
       */
185
 
      last_applied_timestamp.fetch_and_store(to_push.transaction_context().end_timestamp());
186
 
      ++appl_iter;
187
 
    }
188
 
    ++repl_iter;
189
 
  }
190
 
}
191
 
 
192
 
} /* namespace drizzled */