Commit 099451af authored by pryanikov's avatar pryanikov
Browse files

delete old libs

parent 95143e50
CMAKE_MINIMUM_REQUIRED (VERSION 2.8)
PROJECT (libslave)
# Build flags
IF (CMAKE_COMPILER_IS_GNUCC)
# Common options
ADD_DEFINITIONS (-pipe)
ADD_DEFINITIONS (-Wall)
ADD_DEFINITIONS (-O2)
ADD_DEFINITIONS (-std=c++0x)
ADD_DEFINITIONS (-g)
ENDIF (CMAKE_COMPILER_IS_GNUCC)
SET (Boost_NO_BOOST_CMAKE TRUE)
FIND_PACKAGE (Boost REQUIRED)
INCLUDE_DIRECTORIES (${Boost_INCLUDE_DIRS})
FIND_PACKAGE (Boost 1.41.0 COMPONENTS unit_test_framework thread system)
INCLUDE (FindPackageHandleStandardArgs)
FIND_PATH (IMYSQL mysql/mysql.h)
FIND_LIBRARY (LMYSQLCLIENT_R mysqlclient_r PATH_SUFFIXES mysql)
SET (Mysql_FIND_REQUIRED YES)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Mysql DEFAULT_MSG IMYSQL LMYSQLCLIENT_R)
INCLUDE_DIRECTORIES ("${IMYSQL}/mysql")
# Check whether we've found static or dynamic library
STRING (REGEX MATCH ".*\\.a$" MYSQL_IS_STATIC ${LMYSQLCLIENT_R})
IF (MYSQL_IS_STATIC STREQUAL LMYSQLCLIENT_R)
SET (MYSQL_IS_STATIC ON)
ELSE ()
SET (MYSQL_IS_STATIC OFF)
ENDIF ()
FILE (GLOB HDR "*.h")
# INSTALL (FILES ${HDR} DESTINATION include)
AUX_SOURCE_DIRECTORY (${CMAKE_CURRENT_SOURCE_DIR} SRC)
ADD_LIBRARY (slave_a STATIC ${SRC})
SET_TARGET_PROPERTIES (slave_a PROPERTIES OUTPUT_NAME slave)
TARGET_LINK_LIBRARIES (slave_a ${LMYSQLCLIENT_R})
# INSTALL (TARGETS slave_a DESTINATION lib64)
# Most probably statc mysql is built without fPIC, so, we can't build dynamic library with it
IF (NOT MYSQL_IS_STATIC)
ADD_LIBRARY (slave_so SHARED ${SRC})
SET_TARGET_PROPERTIES (slave_so PROPERTIES OUTPUT_NAME slave)
TARGET_LINK_LIBRARIES (slave_so ${LMYSQLCLIENT_R})
# INSTALL (TARGETS slave_so DESTINATION lib64)
ENDIF ()
IF (Boost_FOUND)
ENABLE_TESTING ()
ENDIF (Boost_FOUND)
ADD_SUBDIRECTORY (test)
#ifndef __SLAVE_DEFAULTEXTSTATE_H_
#define __SLAVE_DEFAULTEXTSTATE_H_
#include "SlaveStats.h"
#include <boost/thread/mutex.hpp>
namespace slave
{
class DefaultExtState: public ExtStateIface, protected State {
boost::mutex m_mutex;
public:
virtual State getState()
{
boost::mutex::scoped_lock lock(m_mutex);
return *this;
}
virtual void setConnecting()
{
boost::mutex::scoped_lock lock(m_mutex);
connect_time = ::time(NULL);
++connect_count;
}
virtual time_t getConnectTime()
{
boost::mutex::scoped_lock lock(m_mutex);
return connect_time;
}
virtual void setLastFilteredUpdateTime()
{
boost::mutex::scoped_lock lock(m_mutex);
last_filtered_update = ::time(NULL);
}
virtual time_t getLastFilteredUpdateTime()
{
boost::mutex::scoped_lock lock(m_mutex);
return last_filtered_update;
}
virtual void setLastEventTimePos(time_t t, unsigned long pos)
{
boost::mutex::scoped_lock lock(m_mutex);
last_event_time = t; intransaction_pos = pos; last_update = ::time(NULL);
}
virtual time_t getLastUpdateTime()
{
boost::mutex::scoped_lock lock(m_mutex);
return last_update;
}
virtual time_t getLastEventTime()
{
boost::mutex::scoped_lock lock(m_mutex);
return last_event_time;
}
virtual unsigned long getIntransactionPos()
{
boost::mutex::scoped_lock lock(m_mutex);
return intransaction_pos;
}
virtual void setMasterLogNamePos(const std::string& log_name, unsigned long pos)
{
boost::mutex::scoped_lock lock(m_mutex);
master_log_name = log_name; master_log_pos = pos;
intransaction_pos = pos;
}
virtual unsigned long getMasterLogPos()
{
boost::mutex::scoped_lock lock(m_mutex);
return master_log_pos;
}
virtual std::string getMasterLogName()
{
boost::mutex::scoped_lock lock(m_mutex);
return master_log_name;
}
virtual void saveMasterInfo() {}
virtual bool loadMasterInfo(std::string& logname, unsigned long& pos)
{
boost::mutex::scoped_lock lock(m_mutex);
logname.clear();
pos = 0;
return false;
}
virtual unsigned int getConnectCount()
{
boost::mutex::scoped_lock lock(m_mutex);
return connect_count;
}
virtual void setStateProcessing(bool _state)
{
boost::mutex::scoped_lock lock(m_mutex);
state_processing = _state;
}
virtual bool getStateProcessing()
{
boost::mutex::scoped_lock lock(m_mutex);
return state_processing;
}
virtual void initTableCount(const std::string& t) {}
virtual void incTableCount(const std::string& t) {}
};
}// slave
#endif
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
/* Copyright 2011 ZAO "Begun".
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __SLAVE_LOGGING_H
#define __SLAVE_LOGGING_H
#include <iostream>
#define LOG_TRACE(LOG, S)
#define LOG_DEBUG(LOG, S)
#define LOG_INFO(LOG, S)
#define LOG_WARNING(LOG, S) std::cout << S << std::endl;
#define LOG_ERROR(LOG, S) std::cerr << S << std::endl;
#endif
ABOUT
-------------------------------------------------------------------
This is a library that allows any arbitrary C++ application to connect
to a Mysql replication master and read/parse the replication binary
logs.
In effect, any application can now act like a Mysql replication slave,
without having to compile or link with any Mysql server code.
One important use-case for this library is for receiving changes in
the master database in real-time, without having the store the
master's data on the client server.
USAGE
-------------------------------------------------------------------
Build requirements:
For building the library, you will need:
* g++
* The standard mysql C client libraries. (libmysqlclient)
* The headers of the boost libraries. (http://www.boost.org)
At the minimum, you will need at least the shared_ptr.hpp, function.hpp,
any.hpp and bind.hpp. If boost_unit_test_framework and boost_thread will
be found, tests will be built.
* You (likely) will need to review and edit the contents of Logging.h
and SlaveStats.h
These headers contain the compile-time configuration of the logging
and monitoring subsystems.
The provided defaults are sub-standard and an example only.
Usage requirements:
* Requires Mysql 5.1.23 or above. Tested only with some of the 5.1 and 5.5
versions of mysql servers.
Compiling:
Create directory "build" in source tree, step into it and run "cmake ..".
Then, if configure is complete, run "make".
Review and edit Logging.h and SlaveStats.h to interface the library to
your app's logging and monitoring subsystems.
You can type "make test" inside of "build" directory to run tests. You
will need a working mysql server for it. Settings of mysql connection
can be adjusted in test/data/mysql.conf. Type "ctest -V" if something
went wrong and you need see test output.
Using the library:
Please see the examples in 'test/'.
You can find the programmer's API documentation on our github wiki
pages, see https://github.com/vozbu/libslave/wiki.
CREDITS
-------------------------------------------------------------------
(c) 2011, ZAO "Begun"
https://github.com/Begun/libslave
This library is licensed under the GNU LGPL. Please see the file LICENSE.
This diff is collapsed.
/* Copyright 2011 ZAO "Begun".
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/*
*
* Guaranteed to work with MySQL5.1.23
*
*/
#ifndef __SLAVE_SLAVE_H_
#define __SLAVE_SLAVE_H_
#include <string>
#include <vector>
#include <map>
#include <set>
#include <memory>
#include <mysql/mysql.h>
#include "slave_log_event.h"
#include "SlaveStats.h"
#include "mysqlcompat.h"
namespace slave
{
class Slave
{
public:
typedef std::vector<std::pair<std::string, std::string> > table_order_t;
typedef std::map<std::pair<std::string, std::string>, callback> callbacks_t;
typedef std::vector<std::string> cols_t;
typedef std::map<std::pair<std::string, std::string>, cols_t> callback_filters_t;
private:
static inline bool falseFunction() { return false; };
MYSQL mysql;
int m_server_id;
MasterInfo m_master_info;
EmptyExtState empty_ext_state;
ExtStateIface &ext_state;
table_order_t m_table_order;
callbacks_t m_callbacks;
callback_filters_t m_callback_filters;
typedef boost::function<void (unsigned int)> xid_callback_t;
xid_callback_t m_xid_callback;
RelayLogInfo m_rli;
void createDatabaseStructure_(table_order_t& tabs, RelayLogInfo& rli) const;
public:
Slave() : ext_state(empty_ext_state) {}
Slave(ExtStateIface &state) : ext_state(state) {}
Slave(const MasterInfo& _master_info) : m_master_info(_master_info), ext_state(empty_ext_state) {}
Slave(const MasterInfo& _master_info, ExtStateIface &state) : m_master_info(_master_info), ext_state(state) {}
// Makes sense only when get_remote_binlog is not started
void setMasterInfo(const MasterInfo& aMasterInfo)
{
m_master_info = aMasterInfo;
ext_state.setMasterLogNamePos(aMasterInfo.master_log_name, aMasterInfo.master_log_pos);
}
const MasterInfo& masterInfo() const { return m_master_info; }
typedef std::pair<std::string, unsigned int> binlog_pos_t;
// Reads current binlog position from database
binlog_pos_t getLastBinlog() const;
void setCallback(const std::string& _db_name, const std::string& _tbl_name, callback _callback, const cols_t &filter)
{
setCallback(_db_name, _tbl_name, _callback);
m_callback_filters[std::make_pair(_db_name, _tbl_name)] = filter;
}
void setCallback(const std::string& _db_name, const std::string& _tbl_name, callback _callback)
{
m_table_order.push_back(std::make_pair(_db_name, _tbl_name));
m_callbacks[std::make_pair(_db_name, _tbl_name)] = _callback;
m_callback_filters[std::make_pair(_db_name, _tbl_name)] = cols_t();
ext_state.initTableCount(_db_name + "." + _tbl_name);
}
void setXidCallback(xid_callback_t _callback)
{
m_xid_callback = _callback;
}
void get_remote_binlog( const boost::function< bool() >& _interruptFlag = &Slave::falseFunction );
void createDatabaseStructure() {
m_rli.clear();
createDatabaseStructure_(m_table_order, m_rli);
for (RelayLogInfo::name_to_table_t::iterator i = m_rli.m_table_map.begin(); i != m_rli.m_table_map.end(); ++i) {
i->second->m_callback = m_callbacks[i->first];
i->second->set_callback_filter(m_callback_filters[i->first]);
}
}
RelayLogInfo getRli() const {
return m_rli;
}
table_order_t getTableOrder() const {
return m_table_order;
}
void init();
int serverId() const { return m_server_id; }
// Closes connection, opened in get_remote_binlog. Should be called if your have get_remote_binlog
// blocked on reading data from mysql server in the separate thread and you want to stop this thread.
// You should take care that interruptFlag will return 'true' after connection is closed.
void close_connection();
protected:
void check_master_version();
void check_master_binlog_format();
int process_event(const slave::Basic_event_info& bei, RelayLogInfo &rli, unsigned long long pos);
void request_dump(const std::string& logname, unsigned long start_position, MYSQL* mysql);
ulong read_event(MYSQL* mysql);
std::map<std::string,std::string> getRowType(const std::string& db_name,
const std::set<std::string>& tbl_names) const;
void createTable(RelayLogInfo& rli,
const std::string& db_name, const std::string& tbl_name,
const collate_map_t& collate_map, nanomysql::Connection& conn) const;
void register_slave_on_master(MYSQL* mysql);
void deregister_slave_on_master(MYSQL* mysql);
void generateSlaveId();
};
}// slave
#endif
/* Copyright 2011 ZAO "Begun".
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __SLAVE_SLAVESTATS_H_
#define __SLAVE_SLAVESTATS_H_
/*
* WARNING WARNING WARNING
*
* This file is intended as a demonstration only.
* The code here works, but is not thread-safe or production-ready.
* Please provide your own implementation to fill the gaps according
* to your particular project's needs.
*/