~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,"
54
                " `error_msg` VARCHAR(250))"
2116.1.33 by David Shrewsbury
incremental
55
                " COMMENT = 'VERSION 1.0'");
56
57
  if (not executeSQL(sql))
58
    return false;
59
60
  sql.clear();
2116.1.52 by David Shrewsbury
Changed slave services dedicated schema name from 'replication' to 'sys_replication' to avoid use of reserved word.
61
  sql.push_back("SELECT COUNT(*) FROM `sys_replication`.`io_state`");
2116.1.33 by David Shrewsbury
incremental
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();
2116.1.52 by David Shrewsbury
Changed slave services dedicated schema name from 'replication' to 'sys_replication' to avoid use of reserved word.
74
      sql.push_back("INSERT INTO `sys_replication`.`io_state` (`status`)"
2116.1.33 by David Shrewsbury
incremental
75
                    " VALUES ('STOPPED')");
76
      if (not executeSQL(sql))
77
        return false;
78
    }
79
  }
80
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
85
  sql.clear();
86
  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.
87
  sql.push_back("CREATE TABLE IF NOT EXISTS `sys_replication`.`applier_state`"
2116.1.40 by David Shrewsbury
Add quotes around schema and table names (replication is now a reserved word)
88
                " (`last_applied_commit_id` BIGINT NOT NULL PRIMARY KEY,"
89
                " `status` VARCHAR(20) NOT NULL,"
90
                " `error_msg` VARCHAR(250))"
2116.1.33 by David Shrewsbury
incremental
91
                " COMMENT = 'VERSION 1.0'");
2116.1.31 by David Shrewsbury
Major refactor of common functionality into new classes.
92
93
  if (not executeSQL(sql))
94
    return false;
95
96
  sql.clear();
2116.1.52 by David Shrewsbury
Changed slave services dedicated schema name from 'replication' to 'sys_replication' to avoid use of reserved word.
97
  sql.push_back("SELECT COUNT(*) FROM `sys_replication`.`applier_state`");
2116.1.31 by David Shrewsbury
Major refactor of common functionality into new classes.
98
99
  {
2116.1.33 by David Shrewsbury
incremental
100
    sql::ResultSet result_set(1);
101
    Execute execute(*(_session.get()), true);
102
    execute.run(sql[0], result_set);
103
    result_set.next();
104
    string count= result_set.getString(0);
105
106
    /* Must always be at least one row in the table */
107
    if (count == "0")
108
    {
109
      sql.clear();
2116.1.52 by David Shrewsbury
Changed slave services dedicated schema name from 'replication' to 'sys_replication' to avoid use of reserved word.
110
      sql.push_back("INSERT INTO `sys_replication`.`applier_state`"
2116.1.40 by David Shrewsbury
Add quotes around schema and table names (replication is now a reserved word)
111
                    " (`last_applied_commit_id`, `status`)"
2116.1.33 by David Shrewsbury
incremental
112
                    " VALUES (0, 'STOPPED')");
113
      if (not executeSQL(sql))
114
        return false;
115
    }
2116.1.31 by David Shrewsbury
Major refactor of common functionality into new classes.
116
  }
117
118
  /*
119
   * Create our message queue table if we need to.
120
   */
121
122
  sql.clear();
123
  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.
124
  sql.push_back("CREATE TABLE IF NOT EXISTS `sys_replication`.`queue`"
2116.1.40 by David Shrewsbury
Add quotes around schema and table names (replication is now a reserved word)
125
                " (`trx_id` BIGINT NOT NULL, `seg_id` INT NOT NULL,"
2116.1.50 by David Shrewsbury
Fix commit_id to correct uint64_t type
126
                " `commit_order` BIGINT, `msg` BLOB,"
127
                " PRIMARY KEY(`trx_id`, `seg_id`))"
2116.1.33 by David Shrewsbury
incremental
128
                " COMMENT = 'VERSION 1.0'");
2116.1.31 by David Shrewsbury
Major refactor of common functionality into new classes.
129
  if (not executeSQL(sql))
130
    return false;
131
132
  return true;
133
}
134
2225.3.1 by David Shrewsbury
Persist --slave.max-commit-id value to applier_state table.
135
bool ReplicationSchema::setInitialMaxCommitId(uint64_t value)
136
{
137
  vector<string> sql;
138
139
  sql.push_back("UPDATE `sys_replication`.`applier_state`"
140
                " SET `last_applied_commit_id` = "
141
                + lexical_cast<string>(value));
142
143
  return executeSQL(sql);
144
}
145
2116.1.31 by David Shrewsbury
Major refactor of common functionality into new classes.
146
} /* namespace slave */