MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
version.cpp
1 /*
2  Copyright (c) 2003, 2010, 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 as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include <ndb_global.h>
19 #include <ndb_version.h>
20 #include <version.h>
21 #include <basestring_vsnprintf.h>
22 #include <NdbOut.hpp>
23 
24 extern "C"
25 Uint32 ndbGetMajor(Uint32 version) {
26  return (version >> 16) & 0xFF;
27 }
28 
29 extern "C"
30 Uint32 ndbGetMinor(Uint32 version) {
31  return (version >> 8) & 0xFF;
32 }
33 
34 extern "C"
35 Uint32 ndbGetBuild(Uint32 version) {
36  return (version >> 0) & 0xFF;
37 }
38 
39 extern "C"
40 Uint32 ndbMakeVersion(Uint32 major, Uint32 minor, Uint32 build) {
41  return NDB_MAKE_VERSION(major, minor, build);
42 }
43 
44 extern "C"
45 const char * ndbGetOwnVersionString()
46 {
47  static char ndb_version_string_buf[NDB_VERSION_STRING_BUF_SZ];
48  return ndbGetVersionString(NDB_VERSION, NDB_MYSQL_VERSION_D, NDB_VERSION_STATUS,
49  ndb_version_string_buf,
50  sizeof(ndb_version_string_buf));
51 }
52 
53 extern "C"
54 const char * ndbGetVersionString(Uint32 version, Uint32 mysql_version,
55  const char * status,
56  char *buf, unsigned sz)
57 {
58  char tmp[NDB_VERSION_STRING_BUF_SZ];
59  if (status && status[0] != 0)
60  basestring_snprintf(tmp, sizeof(tmp), "%s", status);
61  else
62  tmp[0] = 0;
63 
64  if (mysql_version)
65  {
66  basestring_snprintf(buf, sz, "mysql-%d.%d.%d ndb-%d.%d.%d%s",
67  getMajor(mysql_version),
68  getMinor(mysql_version),
69  getBuild(mysql_version),
70  getMajor(version),
71  getMinor(version),
72  getBuild(version),
73  tmp);
74  }
75  else
76  {
77  basestring_snprintf(buf, sz, "ndb-%d.%d.%d%s",
78  getMajor(version),
79  getMinor(version),
80  getBuild(version),
81  tmp);
82  }
83  return buf;
84 }
85 
86 typedef enum {
87  UG_Null,
88  UG_Range,
89  UG_Exact
90 } UG_MatchType;
91 
93  Uint32 ownVersion;
94  Uint32 otherVersion;
95  UG_MatchType matchType;
96 };
97 
98 struct NdbUpGradeCompatible ndbCompatibleTable_full[] = {
99  { MAKE_VERSION(7,2,NDB_VERSION_BUILD), MAKE_VERSION(7,2,0), UG_Range },
100  { MAKE_VERSION(7,2,NDB_VERSION_BUILD), MAKE_VERSION(7,1,0), UG_Range },
101  { MAKE_VERSION(7,2,NDB_VERSION_BUILD), MAKE_VERSION(7,0,0), UG_Range },
102 
103  { MAKE_VERSION(7,1,NDB_VERSION_BUILD), MAKE_VERSION(7,1,0), UG_Range }, /* From 7.1+ */
104  { MAKE_VERSION(7,1,NDB_VERSION_BUILD), MAKE_VERSION(7,0,0), UG_Range }, /* From 7.0+ */
105  { MAKE_VERSION(7,1,NDB_VERSION_BUILD), MAKE_VERSION(6,4,0), UG_Range }, /* From 6.4+ */
106  { MAKE_VERSION(7,1,NDB_VERSION_BUILD), NDBD_MAX_RECVBYTESIZE_32K, UG_Range }, /* From 6.3.X + */
107  { MAKE_VERSION(7,0,NDB_VERSION_BUILD), MAKE_VERSION(7,0,0), UG_Range },
108  { MAKE_VERSION(7,0,NDB_VERSION_BUILD), MAKE_VERSION(6,4,0), UG_Range },
109  /* Can only upgrade to 6.4.X+ from versions >= 6.3.17 due to change
110  * in Transporter maximum sent message size
111  */
112  { MAKE_VERSION(7,0,NDB_VERSION_BUILD), NDBD_MAX_RECVBYTESIZE_32K, UG_Range },
113  { MAKE_VERSION(6,3,NDB_VERSION_BUILD), MAKE_VERSION(6,2,1), UG_Range },
114 
115  { MAKE_VERSION(6,2,NDB_VERSION_BUILD), MAKE_VERSION(6,2,1), UG_Range },
116  { MAKE_VERSION(6,2,0), MAKE_VERSION(6,2,0), UG_Range},
117 
118  { MAKE_VERSION(6,2,NDB_VERSION_BUILD), MAKE_VERSION(6,1,19), UG_Range },
119  { MAKE_VERSION(6,1,NDB_VERSION_BUILD), MAKE_VERSION(6,1,6), UG_Range},
120  /* var page reference 32bit->64bit making 6.1.6 not backwards compatible */
121  /* ndb_apply_status table changed, and no compatability code written */
122  { MAKE_VERSION(6,1,4), MAKE_VERSION(6,1,2), UG_Range},
123  { MAKE_VERSION(5,1,NDB_VERSION_BUILD), MAKE_VERSION(5,1,0), UG_Range},
124 
125  { MAKE_VERSION(5,1,NDB_VERSION_BUILD), MAKE_VERSION(5,1,18), UG_Range},
126  { MAKE_VERSION(5,1,17), MAKE_VERSION(5,1,0), UG_Range},
127 
128  { MAKE_VERSION(5,0,NDB_VERSION_BUILD), MAKE_VERSION(5,0,12), UG_Range},
129  { MAKE_VERSION(5,0,11), MAKE_VERSION(5,0,2), UG_Range},
130  { MAKE_VERSION(4,1,NDB_VERSION_BUILD), MAKE_VERSION(4,1,15), UG_Range },
131  { MAKE_VERSION(4,1,14), MAKE_VERSION(4,1,10), UG_Range },
132  { MAKE_VERSION(4,1,10), MAKE_VERSION(4,1,9), UG_Exact },
133  { MAKE_VERSION(4,1,9), MAKE_VERSION(4,1,8), UG_Exact },
134  { MAKE_VERSION(3,5,2), MAKE_VERSION(3,5,1), UG_Exact },
135  { 0, 0, UG_Null }
136 };
137 
138 struct NdbUpGradeCompatible ndbCompatibleTable_upgrade[] = {
139  { MAKE_VERSION(5,0,12), MAKE_VERSION(5,0,11), UG_Exact },
140  { MAKE_VERSION(5,0,2), MAKE_VERSION(4,1,8), UG_Exact },
141  { MAKE_VERSION(4,1,15), MAKE_VERSION(4,1,14), UG_Exact },
142  { MAKE_VERSION(3,5,4), MAKE_VERSION(3,5,3), UG_Exact },
143  { 0, 0, UG_Null }
144 };
145 
146 extern "C"
147 void ndbPrintVersion()
148 {
149  printf("Version: %u.%u.%u\n",
150  getMajor(ndbGetOwnVersion()),
151  getMinor(ndbGetOwnVersion()),
152  getBuild(ndbGetOwnVersion()));
153 }
154 
155 extern "C"
156 Uint32
157 ndbGetOwnVersion()
158 {
159  return NDB_VERSION_D;
160 }
161 
162 static
163 int
164 ndbSearchUpgradeCompatibleTable(Uint32 ownVersion, Uint32 otherVersion,
165  struct NdbUpGradeCompatible table[])
166 {
167  int i;
168  for (i = 0; table[i].ownVersion != 0 && table[i].otherVersion != 0; i++) {
169  if (table[i].ownVersion == ownVersion ||
170  table[i].ownVersion == (Uint32) ~0) {
171  switch (table[i].matchType) {
172  case UG_Range:
173  if (otherVersion >= table[i].otherVersion){
174  return 1;
175  }
176  break;
177  case UG_Exact:
178  if (otherVersion == table[i].otherVersion){
179  return 1;
180  }
181  break;
182  default:
183  break;
184  }
185  }
186  }
187  return 0;
188 }
189 
190 static
191 int
192 ndbCompatible(Uint32 ownVersion, Uint32 otherVersion, struct NdbUpGradeCompatible table[])
193 {
194  if (otherVersion >= ownVersion) {
195  return 1;
196  }
197  return ndbSearchUpgradeCompatibleTable(ownVersion, otherVersion, table);
198 }
199 
200 static
201 int
202 ndbCompatible_full(Uint32 ownVersion, Uint32 otherVersion)
203 {
204  return ndbCompatible(ownVersion, otherVersion, ndbCompatibleTable_full);
205 }
206 
207 static
208 int
209 ndbCompatible_upgrade(Uint32 ownVersion, Uint32 otherVersion)
210 {
211  if (ndbCompatible_full(ownVersion, otherVersion))
212  return 1;
213  return ndbCompatible(ownVersion, otherVersion, ndbCompatibleTable_upgrade);
214 }
215 
216 extern "C"
217 int
218 ndbCompatible_mgmt_ndb(Uint32 ownVersion, Uint32 otherVersion)
219 {
220  return ndbCompatible_upgrade(ownVersion, otherVersion);
221 }
222 
223 extern "C"
224 int
225 ndbCompatible_mgmt_api(Uint32 ownVersion, Uint32 otherVersion)
226 {
227  return ndbCompatible_upgrade(ownVersion, otherVersion);
228 }
229 
230 extern "C"
231 int
232 ndbCompatible_ndb_mgmt(Uint32 ownVersion, Uint32 otherVersion)
233 {
234  return ndbCompatible_full(ownVersion, otherVersion);
235 }
236 
237 extern "C"
238 int
239 ndbCompatible_api_mgmt(Uint32 ownVersion, Uint32 otherVersion)
240 {
241  return ndbCompatible_full(ownVersion, otherVersion);
242 }
243 
244 extern "C"
245 int
246 ndbCompatible_api_ndb(Uint32 ownVersion, Uint32 otherVersion)
247 {
248  return ndbCompatible_full(ownVersion, otherVersion);
249 }
250 
251 extern "C"
252 int
253 ndbCompatible_ndb_api(Uint32 ownVersion, Uint32 otherVersion)
254 {
255  return ndbCompatible_upgrade(ownVersion, otherVersion);
256 }
257 
258 extern "C"
259 int
260 ndbCompatible_ndb_ndb(Uint32 ownVersion, Uint32 otherVersion)
261 {
262  return ndbCompatible_upgrade(ownVersion, otherVersion);
263 }
264 
265 static
266 void
267 ndbPrintCompatibleTable(struct NdbUpGradeCompatible table[])
268 {
269  int i;
270  printf("ownVersion, matchType, otherVersion\n");
271  for (i = 0; table[i].ownVersion != 0 && table[i].otherVersion != 0; i++) {
272 
273  printf("%u.%u.%u, ",
274  getMajor(table[i].ownVersion),
275  getMinor(table[i].ownVersion),
276  getBuild(table[i].ownVersion));
277  switch (table[i].matchType) {
278  case UG_Range:
279  printf("Range");
280  break;
281  case UG_Exact:
282  printf("Exact");
283  break;
284  default:
285  break;
286  }
287  printf(", %u.%u.%u\n",
288  getMajor(table[i].otherVersion),
289  getMinor(table[i].otherVersion),
290  getBuild(table[i].otherVersion));
291 
292  }
293  printf("\n");
294 }
295 
296 
297 void
298 ndbPrintFullyCompatibleTable(void){
299  printf("ndbCompatibleTable_full\n");
300  ndbPrintCompatibleTable(ndbCompatibleTable_full);
301 }
302 
303 
304 void
305 ndbPrintUpgradeCompatibleTable(void){
306  printf("ndbCompatibleTable_upgrade\n");
307  ndbPrintCompatibleTable(ndbCompatibleTable_upgrade);
308 }
309 
310 
311 #ifdef TEST_VERSION
312 
313 #include <NdbTap.hpp>
314 
315 TAPTEST(ndb_version)
316 {
317  printf("Checking NDB version defines and functions...\n\n");
318 
319  printf(" version string: '%s'\n", MYSQL_SERVER_VERSION);
320 
321  printf(" NDB_MYSQL_VERSION_MAJOR: %d\n", NDB_MYSQL_VERSION_MAJOR);
322  printf(" NDB_MYSQL_VERSION_MINOR: %d\n", NDB_MYSQL_VERSION_MINOR);
323  printf(" NDB_MYSQL_VERSION_BUILD: %d\n\n", NDB_MYSQL_VERSION_BUILD);
324  printf(" NDB_VERSION_MAJOR: %d\n", NDB_VERSION_MAJOR);
325  printf(" NDB_VERSION_MINOR: %d\n", NDB_VERSION_MINOR);
326  printf(" NDB_VERSION_BUILD: %d\n", NDB_VERSION_BUILD);
327  printf(" NDB_VERSION_STATUS: '%s'\n\n", NDB_VERSION_STATUS);
328 
329  /*
330  Parse the VERSION string as X.X.X-status */
331  unsigned mysql_major, mysql_minor, mysql_build;
332  char mysql_status[100];
333  const int matches_version = sscanf(MYSQL_SERVER_VERSION, "%u.%u.%u-%s",
334  &mysql_major, &mysql_minor,
335  &mysql_build, mysql_status);
336  OK(matches_version == 3 || matches_version == 4);
337 
338  /*
339  Check that defined MySQL version numbers X.X.X match those parsed
340  from the version string
341  */
342  OK(NDB_MYSQL_VERSION_MAJOR == mysql_major ||
343  NDB_MYSQL_VERSION_MINOR == mysql_minor ||
344  NDB_MYSQL_VERSION_BUILD == mysql_build);
345 
346  if (matches_version == 4 &&
347  strncmp(mysql_status, "ndb", 3) == 0)
348  {
349  /* This is a MySQL Cluster build */
350  unsigned ndb_major, ndb_minor, ndb_build;
351  char ndb_status[100];
352  int matches_ndb = sscanf(mysql_status, "ndb-%u.%u.%u%s",
353  &ndb_major, &ndb_minor,
354  &ndb_build, ndb_status);
355 
356  printf("This is a MySQL Cluster build!\n");
357  printf(" MySQL Server version(X.X.X): %u.%u.%u\n",
358  mysql_major, mysql_minor, mysql_build);
359  printf(" NDB version(Y.Y.Y): %u.%u.%u\n",
360  ndb_major, ndb_minor, ndb_build);
361 
362  OK(matches_ndb == 3 || matches_ndb == 4);
363 
364  /*
365  Check that defined NDB version numbers Y.Y.Y match
366  those parsed from the version string
367  */
368  OK(NDB_VERSION_MAJOR == ndb_major ||
369  NDB_VERSION_MINOR == ndb_minor ||
370  NDB_VERSION_BUILD == ndb_build);
371 
372  }
373  else
374  {
375  /* This is a MySQL Server with NDB build */
376  printf("This is a MySQL Server with NDB build!\n");
377  printf(" MySQL Server version(X.X.X): %u.%u.%u\n",
378  mysql_major, mysql_minor, mysql_build);
379  printf(" NDB version(Y.Y.Y): %u.%u.%u\n",
380  NDB_VERSION_MAJOR, NDB_VERSION_MINOR, NDB_VERSION_BUILD);
381 
382  /* Check that MySQL and NDB version are not the same */
383  if (NDB_MYSQL_VERSION_MAJOR == NDB_VERSION_MAJOR &&
384  NDB_MYSQL_VERSION_MINOR == NDB_VERSION_MINOR &&
385  NDB_MYSQL_VERSION_BUILD == NDB_VERSION_BUILD)
386  {
387  /*
388  Building a MySQL Server with NDB set to same version
389  is most likely an error, not so severe
390  though -> print warning
391  */
392  printf("WARNING: The NDB version is set to same version as MySQL, "
393  "this is most likelky a configuration error!!\n\n");
394  }
395  }
396 
397  /* ndbPrintVersion */
398  printf("ndbPrintVersion() => ");
399  ndbPrintVersion();
400 
401  /* ndbMakeVersion */
402  Uint32 major = 1;
403  Uint32 minor = 2;
404  Uint32 build = 3;
405  Uint32 version = ndbMakeVersion(major, minor, build);
406  OK(version == 0x00010203);
407  OK(ndbGetMajor(version) == major);
408  OK(ndbGetMinor(version) == minor);
409  OK(ndbGetBuild(version) == build);
410 
411  /* ndbGetVersionString */
412  char buf[64];
413  printf("ndbGetVersionString(0x00010203, 0x00030201): '%s'\n",
414  ndbGetVersionString(version, 0x00030201, "-status",
415  buf, sizeof(buf)));
416 
417  /* ndbGetOwnVersionString */
418  printf("ndbGetOwnVersionString: '%s'\n",
419  ndbGetOwnVersionString());
420 
421  /* ndbGetOwnVersion */
422  OK(ndbGetOwnVersion() == ndbMakeVersion(NDB_VERSION_MAJOR,
423  NDB_VERSION_MINOR,
424  NDB_VERSION_BUILD));
425  OK(ndbGetOwnVersion() == NDB_VERSION_D);
426  OK(ndbGetOwnVersion() == NDB_VERSION);
427 
428  /* NDB_MYSQL_VERSION_D */
429  OK(NDB_MYSQL_VERSION_D == ndbMakeVersion(NDB_MYSQL_VERSION_MAJOR,
430  NDB_MYSQL_VERSION_MINOR,
431  NDB_MYSQL_VERSION_BUILD));
432 
433  /* Check sanity of version defines(we don't own a time machine yet...) */
434  OK(ndbMakeVersion(NDB_MYSQL_VERSION_MAJOR,
435  NDB_MYSQL_VERSION_MINOR,
436  NDB_MYSQL_VERSION_BUILD) >= 0x0005012F); // 5.1.47
437  OK(ndbMakeVersion(NDB_VERSION_MAJOR,
438  NDB_VERSION_MINOR,
439  NDB_VERSION_BUILD) >= 0x00070011); // 7.0.17
440 
441  /* Check MYSQL_VERSION_ID matches NDB_MYSQL_VERSION_XX variables */
442  OK(MYSQL_VERSION_ID == (NDB_MYSQL_VERSION_MAJOR * 10000 +
443  NDB_MYSQL_VERSION_MINOR * 100 +
444  NDB_MYSQL_VERSION_BUILD));
445 
446  return 1; // OK
447 }
448 
449 #endif