1 /*
2    Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 // Implements the interface defined in
26 #include "storage/ndb/plugin/ndb_schema_result_table.h"
27 
28 #include <sstream>
29 
30 #include "storage/ndb/plugin/ndb_thd_ndb.h"
31 
32 const std::string Ndb_schema_result_table::DB_NAME = "mysql";
33 const std::string Ndb_schema_result_table::TABLE_NAME = "ndb_schema_result";
34 
35 const char *Ndb_schema_result_table::COL_NODEID = "nodeid";
36 const char *Ndb_schema_result_table::COL_SCHEMA_OP_ID = "schema_op_id";
37 const char *Ndb_schema_result_table::COL_PARTICIPANT_NODEID =
38     "participant_nodeid";
39 const char *Ndb_schema_result_table::COL_RESULT = "result";
40 const char *Ndb_schema_result_table::COL_MESSAGE = "message";
41 
Ndb_schema_result_table(Thd_ndb * thd_ndb)42 Ndb_schema_result_table::Ndb_schema_result_table(Thd_ndb *thd_ndb)
43     : Ndb_util_table(thd_ndb, DB_NAME, TABLE_NAME, true) {}
44 
~Ndb_schema_result_table()45 Ndb_schema_result_table::~Ndb_schema_result_table() {}
46 
check_schema() const47 bool Ndb_schema_result_table::check_schema() const {
48   // nodeid
49   // unsigned int
50   if (!(check_column_exist(COL_NODEID) && check_column_unsigned(COL_NODEID))) {
51     return false;
52   }
53 
54   // schema_op_id
55   // unsigned int
56   if (!(check_column_exist(COL_SCHEMA_OP_ID) &&
57         check_column_unsigned(COL_SCHEMA_OP_ID))) {
58     return false;
59   }
60 
61   // participant_nodeid
62   // unsigned int
63   if (!(check_column_exist(COL_PARTICIPANT_NODEID) &&
64         check_column_unsigned(COL_PARTICIPANT_NODEID))) {
65     return false;
66   }
67 
68   // Check that nodeid + schema_op_id + participant_nodeid is the primary key
69   if (!check_primary_key(
70           {COL_NODEID, COL_SCHEMA_OP_ID, COL_PARTICIPANT_NODEID})) {
71     return false;
72   }
73 
74   // result
75   // unsigned int
76   if (!(check_column_exist(COL_RESULT) && check_column_unsigned(COL_RESULT))) {
77     return false;
78   }
79 
80   // message
81   // varbinary, at least 255 bytes long
82   if (!(check_column_exist(COL_MESSAGE) &&
83         check_column_varbinary(COL_MESSAGE) &&
84         check_column_minlength(COL_MESSAGE, 255))) {
85     return false;
86   }
87 
88   return true;
89 }
90 
define_table_ndb(NdbDictionary::Table & new_table,unsigned mysql_version) const91 bool Ndb_schema_result_table::define_table_ndb(NdbDictionary::Table &new_table,
92                                                unsigned mysql_version) const {
93   // Allow later online add column
94   new_table.setForceVarPart(true);
95 
96   // Allow table to be read+write also in single user mode
97   new_table.setSingleUserMode(NdbDictionary::Table::SingleUserModeReadWrite);
98 
99   {
100     // nodeid UNSIGNED NOT NULL
101     NdbDictionary::Column col_nodeid(COL_NODEID);
102     col_nodeid.setType(NdbDictionary::Column::Unsigned);
103     col_nodeid.setNullable(false);
104     col_nodeid.setPrimaryKey(true);
105     if (!define_table_add_column(new_table, col_nodeid)) return false;
106   }
107 
108   {
109     // schema_op_id UNSIGNED NOT NULL
110     NdbDictionary::Column col_schema_op_id(COL_SCHEMA_OP_ID);
111     col_schema_op_id.setType(NdbDictionary::Column::Unsigned);
112     col_schema_op_id.setNullable(false);
113     col_schema_op_id.setPrimaryKey(true);
114     if (!define_table_add_column(new_table, col_schema_op_id)) return false;
115   }
116 
117   {
118     // participant_nodeid UNSIGNED NOT NULL
119     NdbDictionary::Column col_participant_nodeid(COL_PARTICIPANT_NODEID);
120     col_participant_nodeid.setType(NdbDictionary::Column::Unsigned);
121     col_participant_nodeid.setNullable(false);
122     col_participant_nodeid.setPrimaryKey(true);
123     if (!define_table_add_column(new_table, col_participant_nodeid))
124       return false;
125   }
126 
127   {
128     // result UNSIGNED NOT NULL
129     NdbDictionary::Column col_result(COL_RESULT);
130     col_result.setType(NdbDictionary::Column::Unsigned);
131     col_result.setNullable(false);
132     if (!define_table_add_column(new_table, col_result)) return false;
133   }
134 
135   {
136     // message VARBINARY(255) NOT NULL
137     NdbDictionary::Column col_message(COL_MESSAGE);
138     col_message.setType(NdbDictionary::Column::Varbinary);
139     col_message.setLength(255);
140     col_message.setNullable(false);
141     if (!define_table_add_column(new_table, col_message)) return false;
142   }
143 
144   (void)mysql_version;  // Only one version can be created
145 
146   return true;
147 }
148 
need_upgrade() const149 bool Ndb_schema_result_table::need_upgrade() const { return false; }
150 
define_table_dd() const151 std::string Ndb_schema_result_table::define_table_dd() const {
152   std::stringstream ss;
153   ss << "CREATE TABLE " << db_name() << "." << table_name() << "(\n";
154   ss << "nodeid INT UNSIGNED NOT NULL,"
155         "schema_op_id INT UNSIGNED NOT NULL,"
156         "participant_nodeid INT UNSIGNED NOT NULL,"
157         "result INT UNSIGNED NOT NULL,"
158         "message VARBINARY(255) NOT NULL,"
159         "PRIMARY KEY(nodeid, schema_op_id, participant_nodeid)"
160      << ") ENGINE=ndbcluster";
161   return ss.str();
162 }
163 
drop_events_in_NDB() const164 bool Ndb_schema_result_table::drop_events_in_NDB() const {
165   // Drop the default event
166   if (!drop_event_in_NDB("REPL$mysql/ndb_schema_result")) return false;
167 
168   return true;
169 }
170 
pack_message(const char * message,char * buf)171 void Ndb_schema_result_table::pack_message(const char *message, char *buf) {
172   pack_varbinary(Ndb_schema_result_table::COL_MESSAGE, message, buf);
173 }
174 
unpack_message(const std::string & packed_message)175 std::string Ndb_schema_result_table::unpack_message(
176     const std::string &packed_message) {
177   if (!open()) {
178     return std::string("");
179   }
180   return unpack_varbinary(Ndb_schema_result_table::COL_MESSAGE,
181                           packed_message.c_str());
182 }
183