~drizzle-trunk/drizzle/development

2116.1.31 by David Shrewsbury
Major refactor of common functionality into new classes.
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
2116.1.38 by David Shrewsbury
Change include style
21
#include <config.h>
22
#include <plugin/slave/replication_schema.h>
2116.1.31 by David Shrewsbury
Major refactor of common functionality into new classes.
23
#include <drizzled/execute.h>
24
#include <drizzled/sql/result_set.h>
25
#include <string>
26
#include <vector>
2225.3.1 by David Shrewsbury
Persist --slave.max-commit-id value to applier_state table.
27
#include <boost/lexical_cast.hpp>
2116.1.31 by David Shrewsbury
Major refactor of common functionality into new classes.
28
29
using namespace std;
30
using namespace drizzled;
2225.3.1 by David Shrewsbury
Persist --slave.max-commit-id value to applier_state table.
31
using namespace boost;
2116.1.31 by David Shrewsbury
Major refactor of common functionality into new classes.
32
33
namespace slave
34
{
35
36
bool ReplicationSchema::create()
37
{
38
  vector<string> sql;
39
40
  sql.push_back("COMMIT");
2116.1.52 by David Shrewsbury
Changed slave services dedicated schema name from 'replication' to 'sys_replication' to avoid use of reserved word.
41
  sql.push_back("CREATE SCHEMA IF NOT EXISTS `sys_replication` REPLICATE=FALSE");
2116.1.31 by David Shrewsbury
Major refactor of common functionality into new classes.
42
43
  if (not executeSQL(sql))
44
    return false;
45
46
  /*
2116.1.33 by David Shrewsbury
incremental
47
   * Create our IO thread state information table if we need to.
48
   */
49
50
  sql.clear();
51
  sql.push_back("COMMIT");
2116.1.52 by David Shrewsbury
Changed slave services dedicated schema name from 'replication' to 'sys_replication' to avoid use of reserved word.
52
  sql.push_back("CREATE TABLE IF NOT EXISTS `sys_replication`.`io_state` ("
2116.1.40 by David Shrewsbury
Add quotes around schema and table names (replication is now a reserved word)
53
                " `status` VARCHAR(20) NOT NULL,"
2260.1.1 by David Shrewsbury
Revert multi-master code
54
                " `error_msg` VARCHAR(250))"
55
                " COMMENT = 'VERSION 1.0'");
2116.1.33 by David Shrewsbury
incremental
56
57
  if (not executeSQL(sql))
58
    return false;
59
2260.1.1 by David Shrewsbury
Revert multi-master code
60
  sql.clear();
61
  sql.push_back("SELECT COUNT(*) FROM `sys_replication`.`io_state`");
62
63
  {
64
    sql::ResultSet result_set(1);
65
    Execute execute(*(_session.get()), true);
66
    execute.run(sql[0], result_set);
67
    result_set.next();
68
    string count= result_set.getString(0);
69
70
    /* Must always be at least one row in the table */
71
    if (count == "0")
72
    {
73
      sql.clear();
74
      sql.push_back("INSERT INTO `sys_replication`.`io_state` (`status`)"
75
                    " VALUES ('STOPPED')");
76
      if (not executeSQL(sql))
77
        return false;
78
    }
79
  }
80
2116.1.33 by David Shrewsbury
incremental
81
  /*
2116.1.31 by David Shrewsbury
Major refactor of common functionality into new classes.
82
   * Create our applier thread state information table if we need to.
83
   */
84
2290.1.3 by Joseph Daly
slave plugin work
85
  /*
86
   * Table: applier_state
87
   * Version 1.0: Initial definition
88
   * Version 1.1: Added originating_server_uuid and originating_commit_id
89
   */
90
2116.1.31 by David Shrewsbury
Major refactor of common functionality into new classes.
91
  sql.clear();
92
  sql.push_back("COMMIT");
2260.1.1 by David Shrewsbury
Revert multi-master code
93
  sql.push_back("CREATE TABLE IF NOT EXISTS `sys_replication`.`applier_state`"
94
                " (`last_applied_commit_id` BIGINT NOT NULL PRIMARY KEY,"
2290.1.5 by Joseph Daly
slave fixes
95
                " `originating_server_uuid` VARCHAR(36) NOT NULL,"
2290.1.3 by Joseph Daly
slave plugin work
96
                " `originating_commit_id` BIGINT NOT NULL,"
2116.1.40 by David Shrewsbury
Add quotes around schema and table names (replication is now a reserved word)
97
                " `status` VARCHAR(20) NOT NULL,"
2260.1.1 by David Shrewsbury
Revert multi-master code
98
                " `error_msg` VARCHAR(250))"
2290.1.3 by Joseph Daly
slave plugin work
99
                " COMMENT = 'VERSION 1.1'");
2116.1.31 by David Shrewsbury
Major refactor of common functionality into new classes.
100
101
  if (not executeSQL(sql))
102
    return false;
103
2260.1.1 by David Shrewsbury
Revert multi-master code
104
  sql.clear();
105
  sql.push_back("SELECT COUNT(*) FROM `sys_replication`.`applier_state`");
106
107
  {
108
    sql::ResultSet result_set(1);
109
    Execute execute(*(_session.get()), true);
110
    execute.run(sql[0], result_set);
111
    result_set.next();
112
    string count= result_set.getString(0);
113
114
    /* Must always be at least one row in the table */
115
    if (count == "0")
116
    {
117
      sql.clear();
118
      sql.push_back("INSERT INTO `sys_replication`.`applier_state`"
2290.1.5 by Joseph Daly
slave fixes
119
                    " (`last_applied_commit_id`, `originating_server_uuid`,"
2290.1.3 by Joseph Daly
slave plugin work
120
                    "  `originating_commit_id`, `status`)"
2290.1.5 by Joseph Daly
slave fixes
121
                    " VALUES (0, '', 0, 'STOPPED')");
2260.1.1 by David Shrewsbury
Revert multi-master code
122
      if (not executeSQL(sql))
123
        return false;
124
    }
125
  }
126
2116.1.31 by David Shrewsbury
Major refactor of common functionality into new classes.
127
  /*
128
   * Create our message queue table if we need to.
2290.1.3 by Joseph Daly
slave plugin work
129
   * Version 1.0: Initial definition
130
   * Version 1.1: Added originating_server_uuid and originating_commit_id
2116.1.31 by David Shrewsbury
Major refactor of common functionality into new classes.
131
   */
132
133
  sql.clear();
134
  sql.push_back("COMMIT");
2260.1.1 by David Shrewsbury
Revert multi-master code
135
  sql.push_back("CREATE TABLE IF NOT EXISTS `sys_replication`.`queue`"
136
                " (`trx_id` BIGINT NOT NULL, `seg_id` INT NOT NULL,"
2290.1.3 by Joseph Daly
slave plugin work
137
                " `commit_order` BIGINT,"
2290.1.5 by Joseph Daly
slave fixes
138
                " `originating_server_uuid` VARCHAR(36) NOT NULL,"
2290.1.3 by Joseph Daly
slave plugin work
139
                " `originating_commit_id` BIGINT NOT NULL,"
140
                " `msg` BLOB,"
2260.1.1 by David Shrewsbury
Revert multi-master code
141
                " PRIMARY KEY(`trx_id`, `seg_id`))"
2290.1.3 by Joseph Daly
slave plugin work
142
                " COMMENT = 'VERSION 1.1'");
2116.1.31 by David Shrewsbury
Major refactor of common functionality into new classes.
143
  if (not executeSQL(sql))
144
    return false;
145
146
  return true;
147
}
148
2260.1.1 by David Shrewsbury
Revert multi-master code
149
bool ReplicationSchema::setInitialMaxCommitId(uint64_t value)
2225.3.1 by David Shrewsbury
Persist --slave.max-commit-id value to applier_state table.
150
{
151
  vector<string> sql;
152
153
  sql.push_back("UPDATE `sys_replication`.`applier_state`"
154
                " SET `last_applied_commit_id` = "
2260.1.1 by David Shrewsbury
Revert multi-master code
155
                + lexical_cast<string>(value));
2225.3.1 by David Shrewsbury
Persist --slave.max-commit-id value to applier_state table.
156
157
  return executeSQL(sql);
158
}
159
2116.1.31 by David Shrewsbury
Major refactor of common functionality into new classes.
160
} /* namespace slave */