1 /*
2    Copyright (c) 2020, 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 functions declared in ndb_upgrade_util.h
26 #include "storage/ndb/plugin/ndb_upgrade_util.h"
27 
28 #include "include/my_dbug.h"
29 #include "storage/ndb/include/ndb_version.h"
30 #include "storage/ndb/include/ndbapi/ndb_cluster_connection.hpp"
31 
32 // The first 8.0 GA version for MySQL NDBCluster
33 static constexpr unsigned int NDB_VERSION_8_0_19 = NDB_MAKE_VERSION(8, 0, 19);
34 
35 extern Ndb_cluster_connection *g_ndb_cluster_connection;
36 extern bool opt_ndb_schema_dist_upgrade_allowed;
37 
38 /**
39    @brief Check if it is okay to upgrade the ndb_schema table
40    @return true if it is okay to upgrade, false otherwise
41  */
ndb_allow_ndb_schema_upgrade()42 bool ndb_allow_ndb_schema_upgrade() {
43   DBUG_TRACE;
44 
45   if (DBUG_EVALUATE_IF("ndb_simulate_upgrade_from_non_dd_version", true,
46                        false)) {
47     // Simulate an ongoing upgrade from a version that doesn't have support
48     // for MySQL Data Dictionary
49     return false;
50   }
51 
52   // Find out the minimum API version connected to cluster.
53   const unsigned int min_api_version =
54       g_ndb_cluster_connection->get_min_api_version();
55 
56   if (min_api_version == 0) {
57     // Minimum connected API version is not available in the NDBAPI, which
58     // implies that a data node with a lower version is connected to the
59     // cluster. The table upgrade is denied in this case as there is a chance
60     // that a MySQL Server with lower version is connected to the cluster. The
61     // table will be upgraded after all the data nodes are upgraded.
62     // This requirement means that the ndb_schema table upgrade will be denied
63     // even if there is one connected data node, running a version, that does
64     // not have the support for maintaining the min_api_version in NDBAPI.
65     return false;
66   }
67 
68   // Allow ndb_schema table upgrade if all API nodes connected are atleast
69   // on 8.0.19 and the ndb-schema-dist-upgrade-allowed option is enabled
70   return (min_api_version >= NDB_VERSION_8_0_19) &&
71          opt_ndb_schema_dist_upgrade_allowed;
72 }
73 
74 /**
75    @brief Check if all the nodes connected to cluster have support for
76           MySQL Data Dictionary
77    @return true if all nodes have support for DD, false otherwise
78  */
ndb_all_nodes_support_mysql_dd()79 bool ndb_all_nodes_support_mysql_dd() {
80   DBUG_TRACE;
81 
82   if (DBUG_EVALUATE_IF("ndb_simulate_upgrade_from_non_dd_version", true,
83                        false)) {
84     // Simulate an ongoing upgrade from a version that doesn't have support
85     // for MySQL Data Dictionary
86     return false;
87   }
88 
89   // Find out the minimum node version connected to cluster.
90   const unsigned int min_db_version =
91       g_ndb_cluster_connection->get_min_db_version();
92   const unsigned int min_api_version =
93       g_ndb_cluster_connection->get_min_api_version();
94   const unsigned int min_node_version =
95       (min_db_version < min_api_version) ? min_db_version : min_api_version;
96 
97   // All nodes support MySQL Data Dictionary if the lowest connected version is
98   // atleast 8.0.19, which has the support for MySQL DD.
99   return min_node_version >= NDB_VERSION_8_0_19;
100 }
101