2116.1.20
by David Shrewsbury
Refactor design pattern |
1 |
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
|
2 |
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
|
|
3 |
*
|
|
4 |
* Copyright (C) 2011 David Shrewsbury
|
|
5 |
*
|
|
6 |
* This program is free software; you can redistribute it and/or modify
|
|
7 |
* it under the terms of the GNU General Public License as published by
|
|
8 |
* the Free Software Foundation; either version 2 of the License, or
|
|
9 |
* (at your option) any later version.
|
|
10 |
*
|
|
11 |
* This program is distributed in the hope that it will be useful,
|
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 |
* GNU General Public License for more details.
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GNU General Public License
|
|
17 |
* along with this program; if not, write to the Free Software
|
|
18 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
19 |
*/
|
|
20 |
||
2234
by Brian Aker
Mass removal of ifdef/endif in favor of pragma once. |
21 |
#pragma once
|
2116.1.20
by David Shrewsbury
Refactor design pattern |
22 |
|
2116.1.38
by David Shrewsbury
Change include style |
23 |
#include <plugin/slave/queue_consumer.h> |
24 |
#include <plugin/slave/queue_producer.h> |
|
25 |
#include <plugin/slave/replication_schema.h> |
|
2116.1.31
by David Shrewsbury
Major refactor of common functionality into new classes. |
26 |
#include <drizzled/plugin/daemon.h> |
2116.1.20
by David Shrewsbury
Refactor design pattern |
27 |
#include <boost/thread.hpp> |
2360.1.1
by Mark Atwood
restore multi master replication |
28 |
#include <boost/unordered_map.hpp> |
2116.1.28
by David Shrewsbury
More config file work |
29 |
|
2116.1.31
by David Shrewsbury
Major refactor of common functionality into new classes. |
30 |
namespace drizzled |
31 |
{
|
|
32 |
class Session; |
|
33 |
}
|
|
2116.1.20
by David Shrewsbury
Refactor design pattern |
34 |
|
35 |
namespace slave |
|
36 |
{
|
|
37 |
||
38 |
class ReplicationSlave : public drizzled::plugin::Daemon |
|
39 |
{
|
|
40 |
public: |
|
41 |
||
2116.1.31
by David Shrewsbury
Major refactor of common functionality into new classes. |
42 |
ReplicationSlave(const std::string &config) |
43 |
: drizzled::plugin::Daemon("Replication Slave"), |
|
2360.1.1
by Mark Atwood
restore multi master replication |
44 |
_config_file(config) |
2116.1.26
by David Shrewsbury
Add framework for loading config file |
45 |
{}
|
2116.1.20
by David Shrewsbury
Refactor design pattern |
46 |
|
47 |
~ReplicationSlave() |
|
48 |
{
|
|
49 |
_consumer_thread.interrupt(); |
|
2360.1.1
by Mark Atwood
restore multi master replication |
50 |
|
51 |
boost::unordered_map<uint32_t, Master *>::const_iterator it; |
|
52 |
||
53 |
for (it= _masters.begin(); it != _masters.end(); ++it) |
|
54 |
{
|
|
55 |
it->second->thread().interrupt(); |
|
56 |
}
|
|
2116.1.20
by David Shrewsbury
Refactor design pattern |
57 |
}
|
58 |
||
2360.1.1
by Mark Atwood
restore multi master replication |
59 |
/** Gets called after all plugins are initialized */
|
2116.1.31
by David Shrewsbury
Major refactor of common functionality into new classes. |
60 |
void startup(drizzled::Session &session); |
2116.1.26
by David Shrewsbury
Add framework for loading config file |
61 |
|
2116.1.20
by David Shrewsbury
Refactor design pattern |
62 |
private: |
2360.1.1
by Mark Atwood
restore multi master replication |
63 |
|
64 |
/**
|
|
65 |
* Class representing a master server.
|
|
66 |
*/
|
|
67 |
class Master |
|
68 |
{
|
|
69 |
public: |
|
70 |
Master(uint32_t id) |
|
71 |
{
|
|
72 |
_producer.setMasterId(id); |
|
73 |
}
|
|
74 |
||
75 |
QueueProducer &producer() |
|
76 |
{
|
|
77 |
return _producer; |
|
78 |
}
|
|
79 |
||
80 |
boost::thread &thread() |
|
81 |
{
|
|
82 |
return _producer_thread; |
|
83 |
}
|
|
84 |
||
85 |
void start() |
|
86 |
{
|
|
87 |
_producer_thread= boost::thread(&QueueProducer::run, &_producer); |
|
88 |
}
|
|
89 |
||
90 |
private: |
|
91 |
/** Manages a single master */
|
|
92 |
QueueProducer _producer; |
|
93 |
||
94 |
/** I/O thread that will populate the work queue */
|
|
95 |
boost::thread _producer_thread; |
|
96 |
};
|
|
97 |
||
98 |
/** Configuration file containing master info */
|
|
2116.1.31
by David Shrewsbury
Major refactor of common functionality into new classes. |
99 |
std::string _config_file; |
2360.1.1
by Mark Atwood
restore multi master replication |
100 |
|
2116.1.26
by David Shrewsbury
Add framework for loading config file |
101 |
std::string _error; |
2360.1.1
by Mark Atwood
restore multi master replication |
102 |
|
103 |
/** Object to use with the consumer thread */
|
|
2116.1.20
by David Shrewsbury
Refactor design pattern |
104 |
QueueConsumer _consumer; |
105 |
||
2360.1.1
by Mark Atwood
restore multi master replication |
106 |
/**
|
107 |
* Applier thread that will drain the work queue.
|
|
108 |
* @todo Support more than one consumer thread.
|
|
109 |
*/
|
|
2116.1.20
by David Shrewsbury
Refactor design pattern |
110 |
boost::thread _consumer_thread; |
111 |
||
2360.1.1
by Mark Atwood
restore multi master replication |
112 |
/** List of master objects, one per master */
|
113 |
boost::unordered_map<uint32_t, Master *> _masters; |
|
2116.1.31
by David Shrewsbury
Major refactor of common functionality into new classes. |
114 |
|
2360.1.1
by Mark Atwood
restore multi master replication |
115 |
/** Convenience method to get object reference */
|
116 |
Master &master(size_t index) |
|
117 |
{
|
|
118 |
return *(_masters[index]); |
|
119 |
}
|
|
2225.3.1
by David Shrewsbury
Persist --slave.max-commit-id value to applier_state table. |
120 |
|
2116.1.31
by David Shrewsbury
Major refactor of common functionality into new classes. |
121 |
/**
|
122 |
* Initialize slave services with the given configuration file.
|
|
123 |
*
|
|
2360.1.1
by Mark Atwood
restore multi master replication |
124 |
* In case of an error during initialization, _error contains a
|
125 |
* string describing what went wrong.
|
|
2116.1.31
by David Shrewsbury
Major refactor of common functionality into new classes. |
126 |
*
|
127 |
* @retval true Success
|
|
128 |
* @retval false Failure
|
|
129 |
*/
|
|
130 |
bool initWithConfig(); |
|
2116.1.20
by David Shrewsbury
Refactor design pattern |
131 |
};
|
132 |
||
133 |
} /* namespace slave */ |
|
134 |