Drizzled Public API Documentation

replication_schema.cc
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2011 David Shrewsbury
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 #include <plugin/slave/replication_schema.h>
00023 #include <drizzled/execute.h>
00024 #include <drizzled/sql/result_set.h>
00025 #include <string>
00026 #include <vector>
00027 #include <boost/lexical_cast.hpp>
00028 
00029 using namespace std;
00030 using namespace drizzled;
00031 using namespace boost;
00032 
00033 namespace slave
00034 {
00035 
00036 bool ReplicationSchema::create()
00037 {
00038   vector<string> sql;
00039 
00040   sql.push_back("COMMIT");
00041   sql.push_back("CREATE SCHEMA IF NOT EXISTS `sys_replication` REPLICATE=FALSE");
00042 
00043   if (not executeSQL(sql))
00044     return false;
00045 
00046   /*
00047     Create our IO thread state information table if we need to.
00048    */
00049 
00050   /*
00051     Table: io_state
00052     Version 1.0: Initial definition
00053     Version 1.1: Added master_id and PK on master_id
00054   */
00055 
00056 
00057   sql.clear();
00058   sql.push_back("COMMIT");
00059   sql.push_back("CREATE TABLE IF NOT EXISTS `sys_replication`.`io_state` ("
00060                 " `master_id` BIGINT NOT NULL,"
00061                 " `status` VARCHAR(20) NOT NULL,"
00062                 " `error_msg` VARCHAR(250))"
00063                 " COMMENT = 'VERSION 1.1'");
00064 
00065   if (not executeSQL(sql))
00066     return false;
00067 
00068   /*
00069    * Create our applier thread state information table if we need to.
00070    */
00071 
00072   /*
00073    * Table: applier_state
00074    * Version 1.0: Initial definition
00075    * Version 1.1: Added originating_server_uuid and originating_commit_id
00076    * Version 1.2: Added master_id and changed PK to master_id
00077    */
00078 
00079   sql.clear();
00080   sql.push_back("COMMIT");
00081   sql.push_back("CREATE TABLE IF NOT EXISTS `sys_replication`.`applier_state`"
00082                 " (`master_id` BIGINT NOT NULL,"      
00083                 " `last_applied_commit_id` BIGINT NOT NULL,"
00084                 " `originating_server_uuid` VARCHAR(36) NOT NULL,"
00085                 " `originating_commit_id` BIGINT NOT NULL,"
00086                 " `status` VARCHAR(20) NOT NULL,"
00087                 " `error_msg` VARCHAR(250))"
00088                 " COMMENT = 'VERSION 1.2'");
00089 
00090   if (not executeSQL(sql))
00091     return false;
00092 
00093   /*
00094    * Create our message queue table if we need to.
00095    * Version 1.0: Initial definition
00096    * Version 1.1: Added originating_server_uuid and originating_commit_id
00097    * Version 1.2: Added master_id and changed PK to (master_id, trx_id, seg_id)
00098    */
00099 
00100   sql.clear();
00101   sql.push_back("COMMIT");
00102   sql.push_back("CREATE TABLE IF NOT EXISTS `sys_replication`.`queue`"
00103                 " (`trx_id` BIGINT NOT NULL, `seg_id` INT NOT NULL,"
00104                 " `commit_order` BIGINT,"
00105                 " `originating_server_uuid` VARCHAR(36) NOT NULL,"
00106                 " `originating_commit_id` BIGINT NOT NULL,"
00107                 " `msg` BLOB,"
00108                 " `master_id` BIGINT NOT NULL,"
00109                 " PRIMARY KEY(`master_id`, `trx_id`, `seg_id`))"
00110                 " COMMENT = 'VERSION 1.2'");
00111   if (not executeSQL(sql))
00112     return false;
00113 
00114   return true;
00115 }
00116 
00117 bool ReplicationSchema::createInitialIORow(uint32_t master_id)
00118 {
00119   vector<string> sql;
00120 
00121   sql.push_back("SELECT COUNT(*) FROM `sys_replication`.`io_state` WHERE `master_id` = " + boost::lexical_cast<string>(master_id));
00122 
00123   sql::ResultSet result_set(1);
00124   Execute execute(*(_session.get()), true);
00125   execute.run(sql[0], result_set);
00126   result_set.next();
00127   string count= result_set.getString(0);
00128 
00129   if (count == "0")
00130   {
00131     sql.clear();
00132     sql.push_back("INSERT INTO `sys_replication`.`io_state` (`master_id`, `status`) VALUES ("
00133                   + boost::lexical_cast<string>(master_id)
00134                   + ", 'STOPPED')");
00135     if (not executeSQL(sql))
00136       return false;
00137   }
00138 
00139   return true;
00140 }
00141 
00142 bool ReplicationSchema::createInitialApplierRow(uint32_t master_id)
00143 {
00144   vector<string> sql;
00145 
00146   sql.push_back("SELECT COUNT(*) FROM `sys_replication`.`applier_state` WHERE `master_id` = " + boost::lexical_cast<string>(master_id));
00147 
00148   sql::ResultSet result_set(1);
00149   Execute execute(*(_session.get()), true);
00150   execute.run(sql[0], result_set);
00151   result_set.next();
00152   string count= result_set.getString(0);
00153 
00154   if (count == "0")
00155   {
00156     sql.clear();
00157     sql.push_back("INSERT INTO `sys_replication`.`applier_state`"
00158                   " (`master_id`, `last_applied_commit_id`,"
00159                   " `originating_server_uuid`, `originating_commit_id`," 
00160                   " `status`) VALUES ("
00161                   + boost::lexical_cast<string>(master_id)
00162                   + ",0,"
00163                   + "'" + _session.get()->getServerUUID() + "'"
00164                   + ",0,'STOPPED')");
00165      
00166     if (not executeSQL(sql))
00167       return false;
00168   }
00169 
00170   return true;
00171 }
00172 
00173 
00174 bool ReplicationSchema::setInitialMaxCommitId(uint32_t master_id, uint64_t value)
00175 {
00176   vector<string> sql;
00177 
00178   sql.push_back("UPDATE `sys_replication`.`applier_state`"
00179                 " SET `last_applied_commit_id` = "
00180                 + lexical_cast<string>(value)
00181                 + " WHERE `master_id` = "
00182                 + lexical_cast<string>(master_id));
00183 
00184   return executeSQL(sql);
00185 }
00186 
00187 } /* namespace slave */