00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00048
00049
00050
00051
00052
00053
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
00070
00071
00072
00073
00074
00075
00076
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
00095
00096
00097
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 }