diff options
author | mskold/marty@mysql.com/linux.site <> | 2006-09-01 15:14:50 +0200 |
---|---|---|
committer | mskold/marty@mysql.com/linux.site <> | 2006-09-01 15:14:50 +0200 |
commit | 8fdc781d3991e183086809332ac5cace548a7fc5 (patch) | |
tree | 9d1b05ab23ca550c8e52260f1bd0be73bec28471 | |
parent | 6bac3b8f403ecc88b6f677449d6ee7cb70ea8087 (diff) | |
download | mariadb-git-8fdc781d3991e183086809332ac5cace548a7fc5.tar.gz |
Fixed ndbapi-examples, and added tests for mysql-test-run.pl
28 files changed, 1016 insertions, 80 deletions
diff --git a/mysql-test/include/have_ndbapi_examples.inc b/mysql-test/include/have_ndbapi_examples.inc new file mode 100644 index 00000000000..3ab1f063b61 --- /dev/null +++ b/mysql-test/include/have_ndbapi_examples.inc @@ -0,0 +1,4 @@ +--require r/have_ndbapi_examples.require +disable_query_log; +eval select LENGTH("$MY_NDB_EXAMPLES_BINARY") > 0 as "have_ndb_example"; +enable_query_log; diff --git a/mysql-test/mysql-test-run.pl b/mysql-test/mysql-test-run.pl index 7d5a3af46a6..15044dd6d0b 100755 --- a/mysql-test/mysql-test-run.pl +++ b/mysql-test/mysql-test-run.pl @@ -323,6 +323,8 @@ our $opt_skip_slave_binlog= 0; our $exe_ndb_mgm; our $exe_ndb_waiter; our $path_ndb_tools_dir; +our $path_ndb_examples_dir; +our $exe_ndb_example; our $file_ndb_testrun_log; our @data_dir_lst; @@ -1233,6 +1235,9 @@ sub executable_setup () { mtr_script_exists("$glob_basedir/scripts/mysql_fix_privilege_tables", "/usr/bin/false"); $path_ndb_tools_dir= mtr_path_exists("$glob_basedir/storage/ndb/tools"); + $path_ndb_examples_dir= mtr_path_exists("$glob_basedir/storage/ndb/ndbapi-examples"); + $exe_ndb_example= mtr_exe_exists("$path_ndb_examples_dir/ndbapi_simple/ndbapi_simple", + $exe_mysqld); $exe_ndb_mgm= "$glob_basedir/storage/ndb/src/mgmclient/ndb_mgm"; $exe_ndb_waiter= "$glob_basedir/storage/ndb/tools/ndb_waiter"; $exe_ndbd= "$glob_basedir/storage/ndb/src/kernel/ndbd"; @@ -1297,6 +1302,7 @@ sub executable_setup () { } $path_ndb_tools_dir= "$glob_basedir/bin"; + $path_ndb_examples_dir= "$glob_basedir/ndbapi-examples"; $exe_ndb_mgm= "$glob_basedir/bin/ndb_mgm"; $exe_ndb_waiter= "$glob_basedir/bin/ndb_waiter"; $exe_ndbd= "$glob_basedir/bin/ndbd"; @@ -1344,6 +1350,20 @@ sub environment_setup () { ($lib_udf_example ? dirname($lib_udf_example) : "") . ($ENV{'LD_LIBRARY_PATH'} ? ":$ENV{'LD_LIBRARY_PATH'}" : ""); + # -------------------------------------------------------------------------- + # Add the path where libndbclient can be found + # -------------------------------------------------------------------------- + $ENV{'LD_LIBRARY_PATH'}= + (mtr_path_exists("$glob_basedir/storage/ndb/src/.libs") ? "$glob_basedir/storage/ndb/src/.libs" : "") . + ($ENV{'LD_LIBRARY_PATH'} ? ":$ENV{'LD_LIBRARY_PATH'}" : ""); + + # -------------------------------------------------------------------------- + # Add the path where libmysqlclient can be found + # -------------------------------------------------------------------------- + $ENV{'LD_LIBRARY_PATH'}= + (mtr_path_exists("$glob_basedir/libmysql_r/.libs") ? "$glob_basedir/libmysql_r/.libs" : "") . + ($ENV{'LD_LIBRARY_PATH'} ? ":$ENV{'LD_LIBRARY_PATH'}" : ""); + # -------------------------------------------------------------------------- # Also command lines in .opt files may contain env vars @@ -1360,6 +1380,7 @@ sub environment_setup () { $ENV{'MASTER_MYSOCK1'}= $master->[1]->{'path_sock'}; $ENV{'MASTER_MYPORT'}= $master->[0]->{'port'}; $ENV{'MASTER_MYPORT1'}= $master->[1]->{'port'}; + $ENV{'SLAVE_MYSOCK'}= $slave->[0]->{'path_sock'}; $ENV{'SLAVE_MYPORT'}= $slave->[0]->{'port'}; $ENV{'SLAVE_MYPORT1'}= $slave->[1]->{'port'}; $ENV{'SLAVE_MYPORT2'}= $slave->[2]->{'port'}; @@ -3802,7 +3823,10 @@ sub run_mysqltest ($) { $ENV{'NDB_BACKUP_DIR'}= $clusters->[0]->{'data_dir'}; $ENV{'NDB_DATA_DIR'}= $clusters->[0]->{'data_dir'}; $ENV{'NDB_TOOLS_DIR'}= $path_ndb_tools_dir; + $ENV{'NDB_EXAMPLES_DIR'}= $path_ndb_examples_dir; + $ENV{'MY_NDB_EXAMPLES_BINARY'}= ($exe_ndb_example eq "$path_ndb_examples_dir/ndbapi_simple/ndbapi_simple")?$exe_ndb_example:""; $ENV{'NDB_TOOLS_OUTPUT'}= $file_ndb_testrun_log; + $ENV{'NDB_EXAMPLES_OUTPUT'}= $file_ndb_testrun_log; $ENV{'NDB_CONNECTSTRING'}= $opt_ndbconnectstring; my $exe= $exe_mysqltest; diff --git a/mysql-test/r/have_ndbapi_examples.require b/mysql-test/r/have_ndbapi_examples.require new file mode 100644 index 00000000000..924d2d51708 --- /dev/null +++ b/mysql-test/r/have_ndbapi_examples.require @@ -0,0 +1,2 @@ +have_ndb_example +1 diff --git a/mysql-test/r/ndbapi.result b/mysql-test/r/ndbapi.result new file mode 100644 index 00000000000..3a36757fe09 --- /dev/null +++ b/mysql-test/r/ndbapi.result @@ -0,0 +1,22 @@ +DROP TABLE IF EXISTS t0; +drop database if exists mysqltest; +Running ndbapi_simple, results in /windows/Linux_space/MySQL/mysql-5.1/mysql-test/var/log/ndb_testrun.log +Running ndbapi_simple_index, results in /windows/Linux_space/MySQL/mysql-5.1/mysql-test/var/log/ndb_testrun.log +Running ndbapi_scan, results in /windows/Linux_space/MySQL/mysql-5.1/mysql-test/var/log/ndb_testrun.log +Running ndbapi_retries, results in /windows/Linux_space/MySQL/mysql-5.1/mysql-test/var/log/ndb_testrun.log +Running ndbapi_async, results in /windows/Linux_space/MySQL/mysql-5.1/mysql-test/var/log/ndb_testrun.log +Running ndbapi_async1, results in /windows/Linux_space/MySQL/mysql-5.1/mysql-test/var/log/ndb_testrun.log +use TEST_DB; +create table t0(c0 int, c1 int, c2 char(4), c3 char(4), c4 text, +primary key(c0, c2)) engine ndb charset latin1; +insert into t0 values (1, 2, 'a', 'b', null); +insert into t0 values (3, 4, 'c', 'd', null); +update t0 set c3 = 'e' where c0 = 1 and c2 = 'a'; +update t0 set c3 = 'f'; +update t0 set c3 = 'F'; +update t0 set c2 = 'g' where c0 = 1; +update t0 set c2 = 'G' where c0 = 1; +update t0 set c0 = 5, c2 = 'H' where c0 = 3; +delete from t0; +drop table t0; +Running mgmapi_logevent, results in /windows/Linux_space/MySQL/mysql-5.1/mysql-test/var/log/ndb_testrun.log diff --git a/mysql-test/r/rpl_ndbapi_multi.result b/mysql-test/r/rpl_ndbapi_multi.result new file mode 100644 index 00000000000..cedd14ff994 --- /dev/null +++ b/mysql-test/r/rpl_ndbapi_multi.result @@ -0,0 +1,7 @@ +stop slave; +drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t9; +reset master; +reset slave; +drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t9; +start slave; +Running mgmapi_logevent, results in /windows/Linux_space/MySQL/mysql-5.1/mysql-test/var/log/ndb_testrun.log diff --git a/mysql-test/t/ndbapi.test b/mysql-test/t/ndbapi.test new file mode 100644 index 00000000000..68e40ccbe96 --- /dev/null +++ b/mysql-test/t/ndbapi.test @@ -0,0 +1,44 @@ +-- source include/have_ndb.inc +-- source include/have_ndbapi_examples.inc + +--disable_warnings +DROP TABLE IF EXISTS t0; +drop database if exists mysqltest; +--enable_warnings + +--exec echo Running ndbapi_simple, results in $NDB_EXAMPLES_OUTPUT +--exec $NDB_EXAMPLES_DIR/ndbapi_simple/ndbapi_simple $MASTER_MYSOCK "localhost:$NDBCLUSTER_PORT" >> $NDB_EXAMPLES_OUTPUT + +--exec echo Running ndbapi_simple_index, results in $NDB_EXAMPLES_OUTPUT +--exec $NDB_EXAMPLES_DIR/ndbapi_simple_index/ndbapi_simple_index $MASTER_MYSOCK "localhost:$NDBCLUSTER_PORT" >> $NDB_EXAMPLES_OUTPUT + +--exec echo Running ndbapi_scan, results in $NDB_EXAMPLES_OUTPUT +--exec $NDB_EXAMPLES_DIR/ndbapi_scan/ndbapi_scan $MASTER_MYSOCK "localhost:$NDBCLUSTER_PORT" >> $NDB_EXAMPLES_OUTPUT + +--exec echo Running ndbapi_retries, results in $NDB_EXAMPLES_OUTPUT +--exec $NDB_EXAMPLES_DIR/ndbapi_retries/ndbapi_retries $MASTER_MYSOCK "localhost:$NDBCLUSTER_PORT" >> $NDB_EXAMPLES_OUTPUT + +--exec echo Running ndbapi_async, results in $NDB_EXAMPLES_OUTPUT +--exec $NDB_EXAMPLES_DIR/ndbapi_async/ndbapi_async $MASTER_MYSOCK "localhost:$NDBCLUSTER_PORT" >> $NDB_EXAMPLES_OUTPUT + +--exec echo Running ndbapi_async1, results in $NDB_EXAMPLES_OUTPUT +--exec $NDB_EXAMPLES_DIR/ndbapi_async1/ndbapi_async1 $MASTER_MYSOCK "localhost:$NDBCLUSTER_PORT" >> $NDB_EXAMPLES_OUTPUT + +use TEST_DB; +create table t0(c0 int, c1 int, c2 char(4), c3 char(4), c4 text, + primary key(c0, c2)) engine ndb charset latin1; +#--exec echo Running ndbapi_event, results in $NDB_EXAMPLES_OUTPUT +#--exec $NDB_EXAMPLES_DIR/ndbapi_event/ndbapi_event "localhost:$NDBCLUSTER_PORT" 1 >> $NDB_EXAMPLES_OUTPUT +insert into t0 values (1, 2, 'a', 'b', null); +insert into t0 values (3, 4, 'c', 'd', null); +update t0 set c3 = 'e' where c0 = 1 and c2 = 'a'; -- use pk +update t0 set c3 = 'f'; -- use scan +update t0 set c3 = 'F'; -- use scan update to 'same' +update t0 set c2 = 'g' where c0 = 1; -- update pk part +update t0 set c2 = 'G' where c0 = 1; -- update pk part to 'same' +update t0 set c0 = 5, c2 = 'H' where c0 = 3; -- update full PK +delete from t0; +drop table t0; + +--exec echo Running mgmapi_logevent, results in $NDB_EXAMPLES_OUTPUT +--exec $NDB_EXAMPLES_DIR/mgmapi_logevent/mgmapi_logevent "localhost:$NDBCLUSTER_PORT" 1 >> $NDB_EXAMPLES_OUTPUT diff --git a/mysql-test/t/rpl_ndbapi_multi.test b/mysql-test/t/rpl_ndbapi_multi.test new file mode 100644 index 00000000000..e7f80e380ed --- /dev/null +++ b/mysql-test/t/rpl_ndbapi_multi.test @@ -0,0 +1,10 @@ +-- source include/have_ndb.inc +-- source include/have_binlog_format_row.inc +-- source include/master-slave.inc +-- source include/have_ndbapi_examples.inc + +--exec $NDB_EXAMPLES_DIR/ndbapi_simple_dual/ndbapi_simple_dual $MASTER_MYSOCK "localhost:$NDBCLUSTER_PORT" $SLAVE_MYSOCK "localhost:$NDBCLUSTER_PORT_SLAVE" >> $NDB_EXAMPLES_OUTPUT + +--exec echo Running mgmapi_logevent, results in $NDB_EXAMPLES_OUTPUT +--exec $NDB_EXAMPLES_DIR/mgmapi_logevent/mgmapi_logevent "localhost:$NDBCLUSTER_PORT" "localhost:$NDBCLUSTER_PORT_SLAVE" 1 >> $NDB_EXAMPLES_OUTPUT + diff --git a/storage/ndb/ndbapi-examples/Makefile b/storage/ndb/ndbapi-examples/Makefile index 8e60857dc81..2a77cb20afe 100644 --- a/storage/ndb/ndbapi-examples/Makefile +++ b/storage/ndb/ndbapi-examples/Makefile @@ -5,7 +5,9 @@ BIN_DIRS := ndbapi_simple \ ndbapi_simple_index \ ndbapi_event \ ndbapi_scan \ - mgmapi_logevent + mgmapi_logevent \ + ndbapi_simple_dual \ + mgmapi_logevent_dual bins: $(patsubst %, _bins_%, $(BIN_DIRS)) diff --git a/storage/ndb/ndbapi-examples/mgmapi_logevent/Makefile b/storage/ndb/ndbapi-examples/mgmapi_logevent/Makefile index f96989c885c..c9b4507c4a7 100644 --- a/storage/ndb/ndbapi-examples/mgmapi_logevent/Makefile +++ b/storage/ndb/ndbapi-examples/mgmapi_logevent/Makefile @@ -10,6 +10,7 @@ TOP_SRCDIR = ../../../.. INCLUDE_DIR = $(TOP_SRCDIR)/storage/ndb/include LIB_DIR = -L$(TOP_SRCDIR)/storage/ndb/src/.libs \ -L$(TOP_SRCDIR)/libmysql_r/.libs \ + -L$(TOP_SRCDIR)/zlib/.libs \ -L$(TOP_SRCDIR)/mysys -L$(TOP_SRCDIR)/strings SYS_LIB = diff --git a/storage/ndb/ndbapi-examples/mgmapi_logevent/mgmapi_logevent.cpp b/storage/ndb/ndbapi-examples/mgmapi_logevent/mgmapi_logevent.cpp index 5ec1fba6314..34db5e2ff8c 100644 --- a/storage/ndb/ndbapi-examples/mgmapi_logevent/mgmapi_logevent.cpp +++ b/storage/ndb/ndbapi-examples/mgmapi_logevent/mgmapi_logevent.cpp @@ -39,7 +39,7 @@ exit(-1); \ } -int main() +int main(int argc, char** argv) { NdbMgmHandle h; NdbLogEventHandle le; @@ -51,22 +51,36 @@ int main() 0 }; struct ndb_logevent event; + if (argc < 2) + { + printf("Arguments are <connect_string cluster> [<iterations>].\n"); + exit(-1); + } + const char *connectstring = argv[1]; + int iterations = -1; + if (argc > 2) + iterations = atoi(argv[2]); ndb_init(); - + h= ndb_mgm_create_handle(); if ( h == 0) { printf("Unable to create handle\n"); exit(-1); } + if (ndb_mgm_set_connectstring(h, connectstring) == -1) + { + printf("Unable to set connectstring\n"); + exit(-1); + } if (ndb_mgm_connect(h,0,0,0)) MGMERROR(h); le= ndb_mgm_create_logevent_handle(h, filter); if ( le == 0 ) MGMERROR(h); - while (1) + while (iterations-- != 0) { - int timeout= 5000; + int timeout= 1000; int r= ndb_logevent_get_next(le,&event,timeout); if (r == 0) printf("No event within %d milliseconds\n", timeout); diff --git a/storage/ndb/ndbapi-examples/mgmapi_logevent_dual/Makefile b/storage/ndb/ndbapi-examples/mgmapi_logevent_dual/Makefile new file mode 100644 index 00000000000..4a80a9fe087 --- /dev/null +++ b/storage/ndb/ndbapi-examples/mgmapi_logevent_dual/Makefile @@ -0,0 +1,24 @@ +TARGET = mgmapi_logevent_dual +SRCS = $(TARGET).cpp +OBJS = $(TARGET).o +CXX = g++ +CFLAGS = -c -Wall -fno-rtti -fno-exceptions +CXXFLAGS = +DEBUG = +LFLAGS = -Wall +TOP_SRCDIR = ../../../.. +INCLUDE_DIR = $(TOP_SRCDIR)/storage/ndb/include +LIB_DIR = -L$(TOP_SRCDIR)/storage/ndb/src/.libs \ + -L$(TOP_SRCDIR)/libmysql_r/.libs \ + -L$(TOP_SRCDIR)/zlib/.libs \ + -L$(TOP_SRCDIR)/mysys -L$(TOP_SRCDIR)/strings +SYS_LIB = + +$(TARGET): $(OBJS) + $(CXX) $(CXXFLAGS) $(LFLAGS) $(LIB_DIR) $(OBJS) -lndbclient -lmysqlclient_r -lmysys -lmystrings -lz $(SYS_LIB) -o $(TARGET) + +$(TARGET).o: $(SRCS) + $(CXX) $(CFLAGS) -I$(TOP_SRCDIR)/include -I$(INCLUDE_DIR) -I$(INCLUDE_DIR)/mgmapi -I$(INCLUDE_DIR)/ndbapi $(SRCS) + +clean: + rm -f *.o $(TARGET) diff --git a/storage/ndb/ndbapi-examples/mgmapi_logevent_dual/mgmapi_logevent_dual.cpp b/storage/ndb/ndbapi-examples/mgmapi_logevent_dual/mgmapi_logevent_dual.cpp new file mode 100644 index 00000000000..2073ec540d7 --- /dev/null +++ b/storage/ndb/ndbapi-examples/mgmapi_logevent_dual/mgmapi_logevent_dual.cpp @@ -0,0 +1,225 @@ +/* Copyright (C) 2003 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#include <mysql.h> +#include <ndbapi/NdbApi.hpp> +#include <mgmapi.h> +#include <stdio.h> + +/* + * export LD_LIBRARY_PATH=../../../libmysql_r/.libs:../../../ndb/src/.libs + */ + +#define MGMERROR(h) \ +{ \ + fprintf(stderr, "code: %d msg: %s\n", \ + ndb_mgm_get_latest_error(h), \ + ndb_mgm_get_latest_error_msg(h)); \ + exit(-1); \ +} + +#define LOGEVENTERROR(h) \ +{ \ + fprintf(stderr, "code: %d msg: %s\n", \ + ndb_logevent_get_latest_error(h), \ + ndb_logevent_get_latest_error_msg(h)); \ + exit(-1); \ +} + +int main(int argc, char** argv) +{ + NdbMgmHandle h1,h2; + NdbLogEventHandle le1,le2; + int filter[] = { 15, NDB_MGM_EVENT_CATEGORY_BACKUP, + 15, NDB_MGM_EVENT_CATEGORY_CONNECTION, + 15, NDB_MGM_EVENT_CATEGORY_NODE_RESTART, + 15, NDB_MGM_EVENT_CATEGORY_STARTUP, + 15, NDB_MGM_EVENT_CATEGORY_ERROR, + 0 }; + struct ndb_logevent event1, event2; + + if (argc < 3) + { + printf("Arguments are <connect_string cluster 1> <connect_string cluster 2> [<iterations>].\n"); + exit(-1); + } + const char *connectstring1 = argv[1]; + const char *connectstring2 = argv[2]; + int iterations = -1; + if (argc > 3) + iterations = atoi(argv[3]); + ndb_init(); + + h1= ndb_mgm_create_handle(); + h2= ndb_mgm_create_handle(); + if ( h1 == 0 || h2 == 0 ) + { + printf("Unable to create handle\n"); + exit(-1); + } + if (ndb_mgm_set_connectstring(h1, connectstring1) == -1 || + ndb_mgm_set_connectstring(h2, connectstring1)) + { + printf("Unable to set connectstring\n"); + exit(-1); + } + if (ndb_mgm_connect(h1,0,0,0)) MGMERROR(h1); + if (ndb_mgm_connect(h2,0,0,0)) MGMERROR(h2); + + if ((le1= ndb_mgm_create_logevent_handle(h1, filter)) == 0) MGMERROR(h1); + if ((le2= ndb_mgm_create_logevent_handle(h1, filter)) == 0) MGMERROR(h2); + + while (iterations-- != 0) + { + int timeout= 1000; + int r1= ndb_logevent_get_next(le1,&event1,timeout); + if (r1 == 0) + printf("No event within %d milliseconds\n", timeout); + else if (r1 < 0) + LOGEVENTERROR(le1) + else + { + switch (event1.type) { + case NDB_LE_BackupStarted: + printf("Node %d: BackupStarted\n", event1.source_nodeid); + printf(" Starting node ID: %d\n", event1.BackupStarted.starting_node); + printf(" Backup ID: %d\n", event1.BackupStarted.backup_id); + break; + case NDB_LE_BackupCompleted: + printf("Node %d: BackupCompleted\n", event1.source_nodeid); + printf(" Backup ID: %d\n", event1.BackupStarted.backup_id); + break; + case NDB_LE_BackupAborted: + printf("Node %d: BackupAborted\n", event1.source_nodeid); + break; + case NDB_LE_BackupFailedToStart: + printf("Node %d: BackupFailedToStart\n", event1.source_nodeid); + break; + + case NDB_LE_NodeFailCompleted: + printf("Node %d: NodeFailCompleted\n", event1.source_nodeid); + break; + case NDB_LE_ArbitResult: + printf("Node %d: ArbitResult\n", event1.source_nodeid); + printf(" code %d, arbit_node %d\n", + event1.ArbitResult.code & 0xffff, + event1.ArbitResult.arbit_node); + break; + case NDB_LE_DeadDueToHeartbeat: + printf("Node %d: DeadDueToHeartbeat\n", event1.source_nodeid); + printf(" node %d\n", event1.DeadDueToHeartbeat.node); + break; + + case NDB_LE_Connected: + printf("Node %d: Connected\n", event1.source_nodeid); + printf(" node %d\n", event1.Connected.node); + break; + case NDB_LE_Disconnected: + printf("Node %d: Disconnected\n", event1.source_nodeid); + printf(" node %d\n", event1.Disconnected.node); + break; + case NDB_LE_NDBStartCompleted: + printf("Node %d: StartCompleted\n", event1.source_nodeid); + printf(" version %d.%d.%d\n", + event1.NDBStartCompleted.version >> 16 & 0xff, + event1.NDBStartCompleted.version >> 8 & 0xff, + event1.NDBStartCompleted.version >> 0 & 0xff); + break; + case NDB_LE_ArbitState: + printf("Node %d: ArbitState\n", event1.source_nodeid); + printf(" code %d, arbit_node %d\n", + event1.ArbitState.code & 0xffff, + event1.ArbitResult.arbit_node); + break; + + default: + break; + } + } + + int r2= ndb_logevent_get_next(le1,&event2,timeout); + if (r2 == 0) + printf("No event within %d milliseconds\n", timeout); + else if (r2 < 0) + LOGEVENTERROR(le2) + else + { + switch (event2.type) { + case NDB_LE_BackupStarted: + printf("Node %d: BackupStarted\n", event2.source_nodeid); + printf(" Starting node ID: %d\n", event2.BackupStarted.starting_node); + printf(" Backup ID: %d\n", event2.BackupStarted.backup_id); + break; + case NDB_LE_BackupCompleted: + printf("Node %d: BackupCompleted\n", event2.source_nodeid); + printf(" Backup ID: %d\n", event2.BackupStarted.backup_id); + break; + case NDB_LE_BackupAborted: + printf("Node %d: BackupAborted\n", event2.source_nodeid); + break; + case NDB_LE_BackupFailedToStart: + printf("Node %d: BackupFailedToStart\n", event2.source_nodeid); + break; + + case NDB_LE_NodeFailCompleted: + printf("Node %d: NodeFailCompleted\n", event2.source_nodeid); + break; + case NDB_LE_ArbitResult: + printf("Node %d: ArbitResult\n", event2.source_nodeid); + printf(" code %d, arbit_node %d\n", + event2.ArbitResult.code & 0xffff, + event2.ArbitResult.arbit_node); + break; + case NDB_LE_DeadDueToHeartbeat: + printf("Node %d: DeadDueToHeartbeat\n", event2.source_nodeid); + printf(" node %d\n", event2.DeadDueToHeartbeat.node); + break; + + case NDB_LE_Connected: + printf("Node %d: Connected\n", event2.source_nodeid); + printf(" node %d\n", event2.Connected.node); + break; + case NDB_LE_Disconnected: + printf("Node %d: Disconnected\n", event2.source_nodeid); + printf(" node %d\n", event2.Disconnected.node); + break; + case NDB_LE_NDBStartCompleted: + printf("Node %d: StartCompleted\n", event2.source_nodeid); + printf(" version %d.%d.%d\n", + event2.NDBStartCompleted.version >> 16 & 0xff, + event2.NDBStartCompleted.version >> 8 & 0xff, + event2.NDBStartCompleted.version >> 0 & 0xff); + break; + case NDB_LE_ArbitState: + printf("Node %d: ArbitState\n", event2.source_nodeid); + printf(" code %d, arbit_node %d\n", + event2.ArbitState.code & 0xffff, + event2.ArbitResult.arbit_node); + break; + + default: + break; + } + } + } + + ndb_mgm_destroy_logevent_handle(&le1); + ndb_mgm_destroy_logevent_handle(&le2); + ndb_mgm_destroy_handle(&h1); + ndb_mgm_destroy_handle(&h2); + ndb_end(0); + return 0; +} diff --git a/storage/ndb/ndbapi-examples/ndbapi_async/Makefile b/storage/ndb/ndbapi-examples/ndbapi_async/Makefile index c5de3b06fc7..c18e9676b58 100644 --- a/storage/ndb/ndbapi-examples/ndbapi_async/Makefile +++ b/storage/ndb/ndbapi-examples/ndbapi_async/Makefile @@ -10,6 +10,7 @@ TOP_SRCDIR = ../../../.. INCLUDE_DIR = $(TOP_SRCDIR) LIB_DIR = -L$(TOP_SRCDIR)/storage/ndb/src/.libs \ -L$(TOP_SRCDIR)/libmysql_r/.libs \ + -L$(TOP_SRCDIR)/zlib/.libs \ -L$(TOP_SRCDIR)/mysys -L$(TOP_SRCDIR)/strings SYS_LIB = diff --git a/storage/ndb/ndbapi-examples/ndbapi_async/ndbapi_async.cpp b/storage/ndb/ndbapi-examples/ndbapi_async/ndbapi_async.cpp index aa745f4d28d..d0306b0c51d 100644 --- a/storage/ndb/ndbapi-examples/ndbapi_async/ndbapi_async.cpp +++ b/storage/ndb/ndbapi-examples/ndbapi_async/ndbapi_async.cpp @@ -136,7 +136,12 @@ void closeTransaction(Ndb * ndb , async_callback_t * cb); /** * Function to create table */ -int create_table(Ndb * myNdb); +void create_table(MYSQL &mysql); + +/** + * Function to drop table + */ +void drop_table(MYSQL &mysql); /** * stat. variables @@ -193,7 +198,7 @@ callback(int result, NdbTransaction* trans, void* aObject) /** * Create table "GARAGE" */ -int create_table(MYSQL &mysql) +void create_table(MYSQL &mysql) { while (mysql_query(&mysql, "CREATE TABLE" @@ -208,15 +213,21 @@ int create_table(MYSQL &mysql) MYSQLERROR(mysql); std::cout << "MySQL Cluster already has example table: GARAGE. " << "Dropping it..." << std::endl; - /************** - * Drop table * - **************/ - if (mysql_query(&mysql, "DROP TABLE GARAGE")) - MYSQLERROR(mysql); + drop_table(mysql); + create_table(mysql); } - return 1; } +/** + * Drop table GARAGE + */ +void drop_table(MYSQL &mysql) +{ + if (mysql_query(&mysql, "DROP TABLE GARAGE")) + MYSQLERROR(mysql); +} + + void asynchExitHandler(Ndb * m_ndb) { if (m_ndb != NULL) @@ -339,16 +350,12 @@ int populate(Ndb * myNdb, int data, async_callback_t * cbData) { transaction[current].conn = myNdb->startTransaction(); if (transaction[current].conn == NULL) { - if (asynchErrorHandler(transaction[current].conn, myNdb)) - { - /** - * no transaction to close since conn == null - */ - milliSleep(10); - retries++; - continue; - } - asynchExitHandler(myNdb); + /** + * no transaction to close since conn == null + */ + milliSleep(10); + retries++; + continue; } myNdbOperation = transaction[current].conn->getNdbOperation(myTable); if (myNdbOperation == NULL) @@ -406,8 +413,15 @@ int populate(Ndb * myNdb, int data, async_callback_t * cbData) return -1; } -int main() +int main(int argc, char** argv) { + if (argc != 3) + { + std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n"; + exit(-1); + } + char * mysqld_sock = argv[1]; + const char *connectstring = argv[2]; ndb_init(); MYSQL mysql; @@ -420,7 +434,7 @@ int main() exit(-1); } if ( !mysql_real_connect(&mysql, "localhost", "root", "", "", - 3306, "/tmp/mysql.sock", 0) ) + 0, mysqld_sock, 0) ) MYSQLERROR(mysql); mysql_query(&mysql, "CREATE DATABASE TEST_DB"); @@ -432,7 +446,7 @@ int main() /************************************************************** * Connect to ndb cluster * **************************************************************/ - Ndb_cluster_connection cluster_connection; + Ndb_cluster_connection cluster_connection(connectstring); if (cluster_connection.connect(4, 5, 1)) { std::cout << "Unable to connect to cluster within 30 secs." << std::endl; @@ -447,14 +461,14 @@ int main() Ndb* myNdb = new Ndb( &cluster_connection, "TEST_DB" ); // Object representing the database - if (myNdb->init(1024) == -1) { // Set max 1024 parallel transactions + if (myNdb->init(1024) == -1) { // Set max 1024 parallel transactions APIERROR(myNdb->getNdbError()); } /** * Initialise transaction array */ - for(int i = 0 ; i < 1024 ; i++) + for(int i = 0 ; i < 10 ; i++) { transaction[i].used = 0; transaction[i].conn = 0; @@ -462,9 +476,9 @@ int main() } int i=0; /** - * Do 20000 insert transactions. + * Do 10 insert transactions. */ - while(i < 20000) + while(i < 10) { while(populate(myNdb,i,0)<0) // <0, no space on free list. Sleep and try again. milliSleep(10); @@ -473,4 +487,6 @@ int main() } std::cout << "Number of temporary errors: " << tempErrors << std::endl; delete myNdb; + + drop_table(mysql); } diff --git a/storage/ndb/ndbapi-examples/ndbapi_async1/Makefile b/storage/ndb/ndbapi-examples/ndbapi_async1/Makefile index cc6bcebb71b..c88086157e7 100644 --- a/storage/ndb/ndbapi-examples/ndbapi_async1/Makefile +++ b/storage/ndb/ndbapi-examples/ndbapi_async1/Makefile @@ -6,9 +6,10 @@ CFLAGS = -c -Wall -fno-rtti -fno-exceptions DEBUG = LFLAGS = -Wall TOP_SRCDIR = ../../../.. -INCLUDE_DIR = $(TOP_SRCDIR) +INCLUDE_DIR = $(TOP_SRCDIR)/storage/ndb/include LIB_DIR = -L$(TOP_SRCDIR)/storage/ndb/src/.libs \ -L$(TOP_SRCDIR)/libmysql_r/.libs \ + -L$(TOP_SRCDIR)/zlib/.libs \ -L$(TOP_SRCDIR)/mysys -L$(TOP_SRCDIR)/strings SYS_LIB = @@ -16,7 +17,7 @@ $(TARGET): $(OBJS) $(CXX) $(LFLAGS) $(LIB_DIR) $(OBJS) -lndbclient -lmysqlclient_r -lmysys -lmystrings -lz $(SYS_LIB) -o $(TARGET) $(TARGET).o: $(SRCS) - $(CXX) $(CFLAGS) -I$(INCLUDE_DIR)/storage/ndb/include -I$(INCLUDE_DIR)/storage/ndb/include/ndbapi $(SRCS) + $(CXX) $(CFLAGS) -I$(TOP_SRCDIR)/include -I$(INCLUDE_DIR) -I$(INCLUDE_DIR)/ndbapi $(SRCS) clean: rm -f *.o $(TARGET) diff --git a/storage/ndb/ndbapi-examples/ndbapi_async1/ndbapi_async1.cpp b/storage/ndb/ndbapi-examples/ndbapi_async1/ndbapi_async1.cpp index e8bc19e267b..beb38ce771d 100644 --- a/storage/ndb/ndbapi-examples/ndbapi_async1/ndbapi_async1.cpp +++ b/storage/ndb/ndbapi-examples/ndbapi_async1/ndbapi_async1.cpp @@ -17,38 +17,48 @@ // // ndbapi_async1.cpp: Using asynchronous transactions in NDB API // -// Execute ndbapi_example1 to create the table "MYTABLENAME" -// before executing this program. // // Correct output from this program is: // // Successful insert. // Successful insert. +#include <mysql.h> #include <NdbApi.hpp> // Used for cout #include <iostream> + +#define PRINT_ERROR(code,msg) \ + std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \ + << ", code: " << code \ + << ", msg: " << msg << "." << std::endl +#define MYSQLERROR(mysql) { \ + PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \ + exit(-1); } #define APIERROR(error) \ { std::cout << "Error in " << __FILE__ << ", line:" << __LINE__ << ", code:" \ << error.code << ", msg: " << error.message << "." << std::endl; \ exit(-1); } +static void create_table(MYSQL &); +static void drop_table(MYSQL &); static void callback(int result, NdbTransaction* NdbObject, void* aObject); -int main() +int main(int argc, char** argv) { - ndb_init(); - - Ndb_cluster_connection *cluster_connection= - new Ndb_cluster_connection(); // Object representing the cluster - - if (cluster_connection->wait_until_ready(30,30)) + if (argc != 3) { - std::cout << "Cluster was not ready within 30 secs." << std::endl; + std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n"; exit(-1); } + char * mysqld_sock = argv[1]; + const char *connectstring = argv[2]; + ndb_init(); + + Ndb_cluster_connection *cluster_connection= + new Ndb_cluster_connection(connectstring); // Object representing the cluster int r= cluster_connection->connect(5 /* retries */, 3 /* delay between retries */, @@ -65,15 +75,32 @@ int main() << "Cluster connect failed.\n"; exit(-1); } - - if (cluster_connection->wait_until_ready(30,30)) + + if (cluster_connection->wait_until_ready(30,0)) { std::cout << "Cluster was not ready within 30 secs." << std::endl; exit(-1); } + + // connect to mysql server + MYSQL mysql; + if ( !mysql_init(&mysql) ) { + std::cout << "mysql_init failed\n"; + exit(-1); + } + if ( !mysql_real_connect(&mysql, "localhost", "root", "", "", + 0, mysqld_sock, 0) ) + MYSQLERROR(mysql); + + /******************************************** + * Connect to database via mysql-c * + ********************************************/ + mysql_query(&mysql, "CREATE DATABASE TEST_DB_1"); + if (mysql_query(&mysql, "USE TEST_DB_1") != 0) MYSQLERROR(mysql); + create_table(mysql); Ndb* myNdb = new Ndb( cluster_connection, - "TEST_DB_2" ); // Object representing the database + "TEST_DB_1" ); // Object representing the database NdbTransaction* myNdbTransaction[2]; // For transactions NdbOperation* myNdbOperation; // For operations @@ -119,10 +146,38 @@ int main() delete myNdb; delete cluster_connection; + drop_table(mysql); + ndb_end(0); return 0; } +/********************************************************* + * Create a table named MYTABLENAME if it does not exist * + *********************************************************/ +static void create_table(MYSQL &mysql) +{ + if (mysql_query(&mysql, + "CREATE TABLE" + " MYTABLENAME" + " (ATTR1 INT UNSIGNED NOT NULL PRIMARY KEY," + " ATTR2 INT UNSIGNED NOT NULL)" + " ENGINE=NDB")) + MYSQLERROR(mysql); +} + +/*********************************** + * Drop a table named MYTABLENAME + ***********************************/ +static void drop_table(MYSQL &mysql) +{ + if (mysql_query(&mysql, + "DROP TABLE" + " MYTABLENAME")) + MYSQLERROR(mysql); +} + + /* * callback : This is called when the transaction is polled * diff --git a/storage/ndb/ndbapi-examples/ndbapi_event/Makefile b/storage/ndb/ndbapi-examples/ndbapi_event/Makefile index a3ca6fd61e3..c0430011ab6 100644 --- a/storage/ndb/ndbapi-examples/ndbapi_event/Makefile +++ b/storage/ndb/ndbapi-examples/ndbapi_event/Makefile @@ -10,6 +10,7 @@ TOP_SRCDIR = ../../../.. INCLUDE_DIR = $(TOP_SRCDIR)/storage/ndb/include LIB_DIR = -L$(TOP_SRCDIR)/storage/ndb/src/.libs \ -L$(TOP_SRCDIR)/libmysql_r/.libs \ + -L$(TOP_SRCDIR)/zlib/.libs \ -L$(TOP_SRCDIR)/mysys -L$(TOP_SRCDIR)/strings SYS_LIB = diff --git a/storage/ndb/ndbapi-examples/ndbapi_event/ndbapi_event.cpp b/storage/ndb/ndbapi-examples/ndbapi_event/ndbapi_event.cpp index 328d43caf28..ce568af3e1f 100644 --- a/storage/ndb/ndbapi-examples/ndbapi_event/ndbapi_event.cpp +++ b/storage/ndb/ndbapi-examples/ndbapi_event/ndbapi_event.cpp @@ -117,16 +117,23 @@ int myCreateEvent(Ndb* myNdb, int main(int argc, char** argv) { + if (argc < 3) + { + std::cout << "Arguments are <connect_string cluster> <timeout> [m(merge events)|d(debug)].\n"; + exit(-1); + } + const char *connectstring = argv[1]; + int timeout = atoi(argv[2]); ndb_init(); - bool merge_events = argc > 1 && strchr(argv[1], 'm') != 0; + bool merge_events = argc > 3 && strchr(argv[3], 'm') != 0; #ifdef VM_TRACE - bool dbug = argc > 1 && strchr(argv[1], 'd') != 0; + bool dbug = argc > 3 && strchr(argv[3], 'd') != 0; if (dbug) DBUG_PUSH("d:t:"); if (dbug) putenv("API_SIGNAL_LOG=-"); #endif Ndb_cluster_connection *cluster_connection= - new Ndb_cluster_connection(); // Object representing the cluster + new Ndb_cluster_connection(connectstring); // Object representing the cluster int r= cluster_connection->connect(5 /* retries */, 3 /* delay between retries */, @@ -179,7 +186,7 @@ int main(int argc, char** argv) int i, j, k, l; j = 0; - while (j < 99) { + while (j < timeout) { // Start "transaction" for handling events NdbEventOperation* op; @@ -211,7 +218,7 @@ int main(int argc, char** argv) NdbEventOperation* the_op = op; i= 0; - while (i < 40) { + while (i < timeout) { // printf("now waiting for event...\n"); int r = myNdb->pollEvents(1000); // wait for event or 1000 ms if (r > 0) { @@ -287,7 +294,7 @@ int main(int argc, char** argv) } } } else - ;//printf("timed out\n"); + printf("timed out (%i)\n", timeout); } // don't want to listen to events anymore if (myNdb->dropEventOperation(the_op)) APIERROR(myNdb->getNdbError()); diff --git a/storage/ndb/ndbapi-examples/ndbapi_retries/Makefile b/storage/ndb/ndbapi-examples/ndbapi_retries/Makefile index 3dee4f77e35..1b4a316f406 100644 --- a/storage/ndb/ndbapi-examples/ndbapi_retries/Makefile +++ b/storage/ndb/ndbapi-examples/ndbapi_retries/Makefile @@ -6,9 +6,10 @@ CFLAGS = -c -Wall -fno-rtti -fno-exceptions DEBUG = LFLAGS = -Wall TOP_SRCDIR = ../../../.. -INCLUDE_DIR = ../../include +INCLUDE_DIR = $(TOP_SRCDIR)/storage/ndb/include LIB_DIR = -L$(TOP_SRCDIR)/storage/ndb/src/.libs \ -L$(TOP_SRCDIR)/libmysql_r/.libs \ + -L$(TOP_SRCDIR)/zlib/.libs \ -L$(TOP_SRCDIR)/mysys -L$(TOP_SRCDIR)/strings SYS_LIB = @@ -16,7 +17,7 @@ $(TARGET): $(OBJS) $(CXX) $(LFLAGS) $(LIB_DIR) $(OBJS) -lndbclient -lmysqlclient_r -lmysys -lmystrings -lz $(SYS_LIB) -o $(TARGET) $(TARGET).o: $(SRCS) - $(CXX) $(CFLAGS) -I$(INCLUDE_DIR) -I$(INCLUDE_DIR)/ndbapi $(SRCS) + $(CXX) $(CFLAGS) -I$(TOP_SRCDIR)/include -I$(INCLUDE_DIR) -I$(INCLUDE_DIR)/ndbapi $(SRCS) clean: rm -f *.o $(TARGET) diff --git a/storage/ndb/ndbapi-examples/ndbapi_retries/ndbapi_retries.cpp b/storage/ndb/ndbapi-examples/ndbapi_retries/ndbapi_retries.cpp index 8c29fe31446..940a62c7080 100644 --- a/storage/ndb/ndbapi-examples/ndbapi_retries/ndbapi_retries.cpp +++ b/storage/ndb/ndbapi-examples/ndbapi_retries/ndbapi_retries.cpp @@ -17,9 +17,6 @@ // // ndbapi_retries.cpp: Error handling and transaction retries // -// Execute ndbapi_simple to create the table "MYTABLENAME" -// before executing this program. -// // There are many ways to program using the NDB API. In this example // we execute two inserts in the same transaction using // NdbConnection::execute(NoCommit). @@ -29,6 +26,7 @@ // Application errors (i.e. errors at points marked with APIERROR) // should be handled by the application programmer. +#include <mysql.h> #include <NdbApi.hpp> // Used for cout @@ -38,6 +36,14 @@ #include <unistd.h> #define TIME_TO_SLEEP_BETWEEN_TRANSACTION_RETRIES 1 +#define PRINT_ERROR(code,msg) \ + std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \ + << ", code: " << code \ + << ", msg: " << msg << "." << std::endl +#define MYSQLERROR(mysql) { \ + PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \ + exit(-1); } + // // APIERROR prints an NdbError object // @@ -176,13 +182,44 @@ int executeInsertTransaction(int transactionId, Ndb* myNdb, return result; } +/********************************************************* + * Create a table named MYTABLENAME if it does not exist * + *********************************************************/ +static void create_table(MYSQL &mysql) +{ + if (mysql_query(&mysql, + "CREATE TABLE" + " MYTABLENAME" + " (ATTR1 INT UNSIGNED NOT NULL PRIMARY KEY," + " ATTR2 INT UNSIGNED NOT NULL)" + " ENGINE=NDB")) + MYSQLERROR(mysql); +} + +/*********************************** + * Drop a table named MYTABLENAME + ***********************************/ +static void drop_table(MYSQL &mysql) +{ + if (mysql_query(&mysql, + "DROP TABLE" + " MYTABLENAME")) + MYSQLERROR(mysql); +} -int main() +int main(int argc, char** argv) { + if (argc != 3) + { + std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n"; + exit(-1); + } + char * mysqld_sock = argv[1]; + const char *connectstring = argv[2]; ndb_init(); Ndb_cluster_connection *cluster_connection= - new Ndb_cluster_connection(); // Object representing the cluster + new Ndb_cluster_connection(connectstring); // Object representing the cluster int r= cluster_connection->connect(5 /* retries */, 3 /* delay between retries */, @@ -205,6 +242,22 @@ int main() std::cout << "Cluster was not ready within 30 secs." << std::endl; exit(-1); } + // connect to mysql server + MYSQL mysql; + if ( !mysql_init(&mysql) ) { + std::cout << "mysql_init failed\n"; + exit(-1); + } + if ( !mysql_real_connect(&mysql, "localhost", "root", "", "", + 0, mysqld_sock, 0) ) + MYSQLERROR(mysql); + + /******************************************** + * Connect to database via mysql-c * + ********************************************/ + mysql_query(&mysql, "CREATE DATABASE TEST_DB_1"); + if (mysql_query(&mysql, "USE TEST_DB_1") != 0) MYSQLERROR(mysql); + create_table(mysql); Ndb* myNdb= new Ndb( cluster_connection, "TEST_DB_1" ); // Object representing the database @@ -230,7 +283,9 @@ int main() delete myNdb; delete cluster_connection; - + + drop_table(mysql); + ndb_end(0); return 0; } diff --git a/storage/ndb/ndbapi-examples/ndbapi_scan/Makefile b/storage/ndb/ndbapi-examples/ndbapi_scan/Makefile index e3a7d9c97b0..30742509f75 100644 --- a/storage/ndb/ndbapi-examples/ndbapi_scan/Makefile +++ b/storage/ndb/ndbapi-examples/ndbapi_scan/Makefile @@ -10,6 +10,7 @@ TOP_SRCDIR = ../../../.. INCLUDE_DIR = $(TOP_SRCDIR) LIB_DIR = -L$(TOP_SRCDIR)/storage/ndb/src/.libs \ -L$(TOP_SRCDIR)/libmysql_r/.libs \ + -L$(TOP_SRCDIR)/zlib/.libs \ -L$(TOP_SRCDIR)/mysys -L$(TOP_SRCDIR)/strings SYS_LIB = diff --git a/storage/ndb/ndbapi-examples/ndbapi_scan/ndbapi_scan.cpp b/storage/ndb/ndbapi-examples/ndbapi_scan/ndbapi_scan.cpp index 69ffd99b8ca..8f1057f2531 100644 --- a/storage/ndb/ndbapi-examples/ndbapi_scan/ndbapi_scan.cpp +++ b/storage/ndb/ndbapi-examples/ndbapi_scan/ndbapi_scan.cpp @@ -114,9 +114,19 @@ struct Car }; /** + * Function to drop table + */ +void drop_table(MYSQL &mysql) +{ + if (mysql_query(&mysql, "DROP TABLE GARAGE")) + MYSQLERROR(mysql); +} + + +/** * Function to create table */ -int create_table(MYSQL &mysql) +void create_table(MYSQL &mysql) { while (mysql_query(&mysql, "CREATE TABLE" @@ -131,16 +141,14 @@ int create_table(MYSQL &mysql) MYSQLERROR(mysql); std::cout << "MySQL Cluster already has example table: GARAGE. " << "Dropping it..." << std::endl; - /************** - * Drop table * - **************/ - if (mysql_query(&mysql, "DROP TABLE GARAGE")) - MYSQLERROR(mysql); + /****************** + * Recreate table * + ******************/ + drop_table(mysql); + create_table(mysql); } - return 1; } - int populate(Ndb * myNdb) { int i; @@ -721,8 +729,15 @@ int scan_print(Ndb * myNdb) } -int main() +int main(int argc, char** argv) { + if (argc != 3) + { + std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n"; + exit(-1); + } + char * mysqld_sock = argv[1]; + const char *connectstring = argv[2]; ndb_init(); MYSQL mysql; @@ -735,7 +750,7 @@ int main() exit(-1); } if ( !mysql_real_connect(&mysql, "localhost", "root", "", "", - 3306, "/tmp/mysql.sock", 0) ) + 0, mysqld_sock, 0) ) MYSQLERROR(mysql); mysql_query(&mysql, "CREATE DATABASE TEST_DB"); @@ -748,7 +763,7 @@ int main() * Connect to ndb cluster * **************************************************************/ - Ndb_cluster_connection cluster_connection; + Ndb_cluster_connection cluster_connection(connectstring); if (cluster_connection.connect(4, 5, 1)) { std::cout << "Unable to connect to cluster within 30 secs." << std::endl; @@ -821,5 +836,10 @@ int main() if(scan_print(&myNdb) > 0) std::cout << "scan_print: Success!" << std::endl << std::endl; + /** + * Drop table + */ + drop_table(mysql); + return 0; } diff --git a/storage/ndb/ndbapi-examples/ndbapi_simple/Makefile b/storage/ndb/ndbapi-examples/ndbapi_simple/Makefile index b792c4c4a47..fa407fb7d63 100644 --- a/storage/ndb/ndbapi-examples/ndbapi_simple/Makefile +++ b/storage/ndb/ndbapi-examples/ndbapi_simple/Makefile @@ -10,6 +10,7 @@ TOP_SRCDIR = ../../../.. INCLUDE_DIR = $(TOP_SRCDIR)/storage/ndb/include LIB_DIR = -L$(TOP_SRCDIR)/storage/ndb/src/.libs \ -L$(TOP_SRCDIR)/libmysql_r/.libs \ + -L$(TOP_SRCDIR)/zlib/.libs \ -L$(TOP_SRCDIR)/mysys -L$(TOP_SRCDIR)/strings SYS_LIB = diff --git a/storage/ndb/ndbapi-examples/ndbapi_simple/ndbapi_simple.cpp b/storage/ndb/ndbapi-examples/ndbapi_simple/ndbapi_simple.cpp index 152d4fa44af..08e681755c6 100644 --- a/storage/ndb/ndbapi-examples/ndbapi_simple/ndbapi_simple.cpp +++ b/storage/ndb/ndbapi-examples/ndbapi_simple/ndbapi_simple.cpp @@ -52,15 +52,22 @@ static void run_application(MYSQL &, Ndb_cluster_connection &); PRINT_ERROR(error.code,error.message); \ exit(-1); } -int main() +int main(int argc, char** argv) { + if (argc != 3) + { + std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n"; + exit(-1); + } // ndb_init must be called first ndb_init(); // connect to mysql server and cluster and run application { + char * mysqld_sock = argv[1]; + const char *connectstring = argv[2]; // Object representing the cluster - Ndb_cluster_connection cluster_connection; + Ndb_cluster_connection cluster_connection(connectstring); // Connect to cluster management server (ndb_mgmd) if (cluster_connection.connect(4 /* retries */, @@ -85,7 +92,7 @@ int main() exit(-1); } if ( !mysql_real_connect(&mysql, "localhost", "root", "", "", - 3306, "/tmp/mysql.sock", 0) ) + 0, mysqld_sock, 0) ) MYSQLERROR(mysql); // run the application code @@ -94,13 +101,11 @@ int main() ndb_end(0); - std::cout << "\nTo drop created table use:\n" - << "echo \"drop table MYTABLENAME\" | mysql TEST_DB_1 -u root\n"; - return 0; } static void create_table(MYSQL &); +static void drop_table(MYSQL &); static void do_insert(Ndb &); static void do_update(Ndb &); static void do_delete(Ndb &); @@ -130,6 +135,8 @@ static void run_application(MYSQL &mysql, do_update(myNdb); do_delete(myNdb); do_read(myNdb); + drop_table(mysql); + mysql_query(&mysql, "DROP DATABASE TEST_DB_1"); } /********************************************************* @@ -146,6 +153,17 @@ static void create_table(MYSQL &mysql) MYSQLERROR(mysql); } +/*********************************** + * Drop a table named MYTABLENAME + ***********************************/ +static void drop_table(MYSQL &mysql) +{ + if (mysql_query(&mysql, + "DROP TABLE" + " MYTABLENAME")) + MYSQLERROR(mysql); +} + /************************************************************************** * Using 5 transactions, insert 10 tuples in table: (0,0),(1,1),...,(9,9) * **************************************************************************/ diff --git a/storage/ndb/ndbapi-examples/ndbapi_simple_dual/Makefile b/storage/ndb/ndbapi-examples/ndbapi_simple_dual/Makefile new file mode 100644 index 00000000000..7f0ca52fcc3 --- /dev/null +++ b/storage/ndb/ndbapi-examples/ndbapi_simple_dual/Makefile @@ -0,0 +1,24 @@ +TARGET = ndbapi_simple_dual +SRCS = $(TARGET).cpp +OBJS = $(TARGET).o +CXX = g++ +CFLAGS = -c -Wall -fno-rtti -fno-exceptions +CXXFLAGS = +DEBUG = +LFLAGS = -Wall +TOP_SRCDIR = ../../../.. +INCLUDE_DIR = $(TOP_SRCDIR)/storage/ndb/include +LIB_DIR = -L$(TOP_SRCDIR)/storage/ndb/src/.libs \ + -L$(TOP_SRCDIR)/libmysql_r/.libs \ + -L$(TOP_SRCDIR)/zlib/.libs \ + -L$(TOP_SRCDIR)/mysys -L$(TOP_SRCDIR)/strings +SYS_LIB = + +$(TARGET): $(OBJS) + $(CXX) $(CXXFLAGS) $(LFLAGS) $(LIB_DIR) $(OBJS) -lndbclient -lmysqlclient_r -lmysys -lmystrings -lz $(SYS_LIB) -o $(TARGET) + +$(TARGET).o: $(SRCS) + $(CXX) $(CFLAGS) -I$(TOP_SRCDIR)/include -I$(INCLUDE_DIR) -I$(INCLUDE_DIR)/ndbapi $(SRCS) + +clean: + rm -f *.o $(TARGET) diff --git a/storage/ndb/ndbapi-examples/ndbapi_simple_dual/ndbapi_simple_dual.cpp b/storage/ndb/ndbapi-examples/ndbapi_simple_dual/ndbapi_simple_dual.cpp new file mode 100644 index 00000000000..838884f8090 --- /dev/null +++ b/storage/ndb/ndbapi-examples/ndbapi_simple_dual/ndbapi_simple_dual.cpp @@ -0,0 +1,348 @@ +/* Copyright (C) 2003 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +/* + * ndbapi_simple_dual.cpp: Using synchronous transactions in NDB API + * + * Correct output from this program is: + * + * ATTR1 ATTR2 + * 0 10 + * 1 1 + * 2 12 + * Detected that deleted tuple doesn't exist! + * 4 14 + * 5 5 + * 6 16 + * 7 7 + * 8 18 + * 9 9 + * ATTR1 ATTR2 + * 0 10 + * 1 1 + * 2 12 + * Detected that deleted tuple doesn't exist! + * 4 14 + * 5 5 + * 6 16 + * 7 7 + * 8 18 + * 9 9 + * + */ + +#include <mysql.h> +#include <NdbApi.hpp> +// Used for cout +#include <stdio.h> +#include <iostream> + +static void run_application(MYSQL &, Ndb_cluster_connection &, const char* table, const char* db); + +#define PRINT_ERROR(code,msg) \ + std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \ + << ", code: " << code \ + << ", msg: " << msg << "." << std::endl +#define MYSQLERROR(mysql) { \ + PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \ + exit(-1); } +#define APIERROR(error) { \ + PRINT_ERROR(error.code,error.message); \ + exit(-1); } + +int main(int argc, char** argv) +{ + if (argc != 5) + { + std::cout << "Arguments are <socket mysqld1> <connect_string cluster 1> <socket mysqld2> <connect_string cluster 2>.\n"; + exit(-1); + } + // ndb_init must be called first + ndb_init(); + { + char * mysqld1_sock = argv[1]; + const char *connectstring1 = argv[2]; + char * mysqld2_sock = argv[3]; + const char *connectstring2 = argv[4]; + + // Object representing the cluster 1 + Ndb_cluster_connection cluster1_connection(connectstring1); + MYSQL mysql1; + // Object representing the cluster 2 + Ndb_cluster_connection cluster2_connection(connectstring2); + MYSQL mysql2; + + // connect to mysql server and cluster 1 and run application + // Connect to cluster 1 management server (ndb_mgmd) + if (cluster1_connection.connect(4 /* retries */, + 5 /* delay between retries */, + 1 /* verbose */)) + { + std::cout << "Cluster 1 management server was not ready within 30 secs.\n"; + exit(-1); + } + // Optionally connect and wait for the storage nodes (ndbd's) + if (cluster1_connection.wait_until_ready(30,0) < 0) + { + std::cout << "Cluster 1 was not ready within 30 secs.\n"; + exit(-1); + } + // connect to mysql server in cluster 1 + if ( !mysql_init(&mysql1) ) { + std::cout << "mysql_init failed\n"; + exit(-1); + } + if ( !mysql_real_connect(&mysql1, "localhost", "root", "", "", + 0, mysqld1_sock, 0) ) + MYSQLERROR(mysql1); + + + // connect to mysql server and cluster 2 and run application + + // Connect to cluster management server (ndb_mgmd) + if (cluster2_connection.connect(4 /* retries */, + 5 /* delay between retries */, + 1 /* verbose */)) + { + std::cout << "Cluster 2 management server was not ready within 30 secs.\n"; + exit(-1); + } + // Optionally connect and wait for the storage nodes (ndbd's) + if (cluster2_connection.wait_until_ready(30,0) < 0) + { + std::cout << "Cluster 2 was not ready within 30 secs.\n"; + exit(-1); + } + // connect to mysql server in cluster 2 + if ( !mysql_init(&mysql2) ) { + std::cout << "mysql_init failed\n"; + exit(-1); + } + if ( !mysql_real_connect(&mysql2, "localhost", "root", "", "", + 0, mysqld2_sock, 0) ) + MYSQLERROR(mysql2); + + // run the application code + run_application(mysql1, cluster1_connection, "MYTABLENAME1", "TEST_DB_1"); + run_application(mysql2, cluster2_connection, "MYTABLENAME2", "TEST_DB_2"); + } + // Note: all connections must have been destroyed before calling ndb_end() + ndb_end(0); + + return 0; +} + +static void create_table(MYSQL &, const char* table); +static void drop_table(MYSQL &, const char* table); +static void do_insert(Ndb &, const char* table); +static void do_update(Ndb &, const char* table); +static void do_delete(Ndb &, const char* table); +static void do_read(Ndb &, const char* table); + +static void run_application(MYSQL &mysql, + Ndb_cluster_connection &cluster_connection, + const char* table, + const char* db) +{ + /******************************************** + * Connect to database via mysql-c * + ********************************************/ + char db_stmt[256]; + sprintf(db_stmt, "CREATE DATABASE %s\n", db); + mysql_query(&mysql, db_stmt); + sprintf(db_stmt, "USE %s", db); + if (mysql_query(&mysql, db_stmt) != 0) MYSQLERROR(mysql); + create_table(mysql, table); + + /******************************************** + * Connect to database via NdbApi * + ********************************************/ + // Object representing the database + Ndb myNdb( &cluster_connection, db ); + if (myNdb.init()) APIERROR(myNdb.getNdbError()); + + /* + * Do different operations on database + */ + do_insert(myNdb, table); + do_update(myNdb, table); + do_delete(myNdb, table); + do_read(myNdb, table); + /* + * Drop the table + */ + drop_table(mysql, table); + sprintf(db_stmt, "DROP DATABASE %s\n", db); + mysql_query(&mysql, db_stmt); +} + +/********************************************************* + * Create a table named by table if it does not exist * + *********************************************************/ +static void create_table(MYSQL &mysql, const char* table) +{ + char create_stmt[256]; + + sprintf(create_stmt, "CREATE TABLE %s \ + (ATTR1 INT UNSIGNED NOT NULL PRIMARY KEY,\ + ATTR2 INT UNSIGNED NOT NULL)\ + ENGINE=NDB", table); + if (mysql_query(&mysql, create_stmt)) + MYSQLERROR(mysql); +} + +/******************************* + * Drop a table named by table + *******************************/ +static void drop_table(MYSQL &mysql, const char* table) +{ + char drop_stmt[256]; + + sprintf(drop_stmt, "DROP TABLE IF EXISTS %s", table); + if (mysql_query(&mysql, drop_stmt)) + MYSQLERROR(mysql); +} + +/************************************************************************** + * Using 5 transactions, insert 10 tuples in table: (0,0),(1,1),...,(9,9) * + **************************************************************************/ +static void do_insert(Ndb &myNdb, const char* table) +{ + const NdbDictionary::Dictionary* myDict= myNdb.getDictionary(); + const NdbDictionary::Table *myTable= myDict->getTable(table); + + if (myTable == NULL) + APIERROR(myDict->getNdbError()); + + for (int i = 0; i < 5; i++) { + NdbTransaction *myTransaction= myNdb.startTransaction(); + if (myTransaction == NULL) APIERROR(myNdb.getNdbError()); + + NdbOperation *myOperation= myTransaction->getNdbOperation(myTable); + if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); + + myOperation->insertTuple(); + myOperation->equal("ATTR1", i); + myOperation->setValue("ATTR2", i); + + myOperation= myTransaction->getNdbOperation(myTable); + if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); + + myOperation->insertTuple(); + myOperation->equal("ATTR1", i+5); + myOperation->setValue("ATTR2", i+5); + + if (myTransaction->execute( NdbTransaction::Commit ) == -1) + APIERROR(myTransaction->getNdbError()); + + myNdb.closeTransaction(myTransaction); + } +} + +/***************************************************************** + * Update the second attribute in half of the tuples (adding 10) * + *****************************************************************/ +static void do_update(Ndb &myNdb, const char* table) +{ + const NdbDictionary::Dictionary* myDict= myNdb.getDictionary(); + const NdbDictionary::Table *myTable= myDict->getTable(table); + + if (myTable == NULL) + APIERROR(myDict->getNdbError()); + + for (int i = 0; i < 10; i+=2) { + NdbTransaction *myTransaction= myNdb.startTransaction(); + if (myTransaction == NULL) APIERROR(myNdb.getNdbError()); + + NdbOperation *myOperation= myTransaction->getNdbOperation(myTable); + if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); + + myOperation->updateTuple(); + myOperation->equal( "ATTR1", i ); + myOperation->setValue( "ATTR2", i+10); + + if( myTransaction->execute( NdbTransaction::Commit ) == -1 ) + APIERROR(myTransaction->getNdbError()); + + myNdb.closeTransaction(myTransaction); + } +} + +/************************************************* + * Delete one tuple (the one with primary key 3) * + *************************************************/ +static void do_delete(Ndb &myNdb, const char* table) +{ + const NdbDictionary::Dictionary* myDict= myNdb.getDictionary(); + const NdbDictionary::Table *myTable= myDict->getTable(table); + + if (myTable == NULL) + APIERROR(myDict->getNdbError()); + + NdbTransaction *myTransaction= myNdb.startTransaction(); + if (myTransaction == NULL) APIERROR(myNdb.getNdbError()); + + NdbOperation *myOperation= myTransaction->getNdbOperation(myTable); + if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); + + myOperation->deleteTuple(); + myOperation->equal( "ATTR1", 3 ); + + if (myTransaction->execute(NdbTransaction::Commit) == -1) + APIERROR(myTransaction->getNdbError()); + + myNdb.closeTransaction(myTransaction); +} + +/***************************** + * Read and print all tuples * + *****************************/ +static void do_read(Ndb &myNdb, const char* table) +{ + const NdbDictionary::Dictionary* myDict= myNdb.getDictionary(); + const NdbDictionary::Table *myTable= myDict->getTable(table); + + if (myTable == NULL) + APIERROR(myDict->getNdbError()); + + std::cout << "ATTR1 ATTR2" << std::endl; + + for (int i = 0; i < 10; i++) { + NdbTransaction *myTransaction= myNdb.startTransaction(); + if (myTransaction == NULL) APIERROR(myNdb.getNdbError()); + + NdbOperation *myOperation= myTransaction->getNdbOperation(myTable); + if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); + + myOperation->readTuple(NdbOperation::LM_Read); + myOperation->equal("ATTR1", i); + + NdbRecAttr *myRecAttr= myOperation->getValue("ATTR2", NULL); + if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError()); + + if(myTransaction->execute( NdbTransaction::Commit ) == -1) + if (i == 3) { + std::cout << "Detected that deleted tuple doesn't exist!" << std::endl; + } else { + APIERROR(myTransaction->getNdbError()); + } + + if (i != 3) { + printf(" %2d %2d\n", i, myRecAttr->u_32_value()); + } + myNdb.closeTransaction(myTransaction); + } +} diff --git a/storage/ndb/ndbapi-examples/ndbapi_simple_index/Makefile b/storage/ndb/ndbapi-examples/ndbapi_simple_index/Makefile index 3b3ac7f484a..c38975381f5 100644 --- a/storage/ndb/ndbapi-examples/ndbapi_simple_index/Makefile +++ b/storage/ndb/ndbapi-examples/ndbapi_simple_index/Makefile @@ -10,6 +10,7 @@ TOP_SRCDIR = ../../../.. INCLUDE_DIR = $(TOP_SRCDIR) LIB_DIR = -L$(TOP_SRCDIR)/storage/ndb/src/.libs \ -L$(TOP_SRCDIR)/libmysql_r/.libs \ + -L$(TOP_SRCDIR)/zlib/.libs \ -L$(TOP_SRCDIR)/mysys -L$(TOP_SRCDIR)/strings SYS_LIB = diff --git a/storage/ndb/ndbapi-examples/ndbapi_simple_index/ndbapi_simple_index.cpp b/storage/ndb/ndbapi-examples/ndbapi_simple_index/ndbapi_simple_index.cpp index 5afaf6078d1..c1cc20f2ea1 100644 --- a/storage/ndb/ndbapi-examples/ndbapi_simple_index/ndbapi_simple_index.cpp +++ b/storage/ndb/ndbapi-examples/ndbapi_simple_index/ndbapi_simple_index.cpp @@ -49,8 +49,15 @@ PRINT_ERROR(error.code,error.message); \ exit(-1); } -int main() +int main(int argc, char** argv) { + if (argc != 3) + { + std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n"; + exit(-1); + } + char * mysqld_sock = argv[1]; + const char *connectstring = argv[2]; ndb_init(); MYSQL mysql; @@ -63,7 +70,7 @@ int main() exit(-1); } if ( !mysql_real_connect(&mysql, "localhost", "root", "", "", - 3306, "/tmp/mysql.sock", 0) ) + 0, mysqld_sock, 0) ) MYSQLERROR(mysql); mysql_query(&mysql, "CREATE DATABASE TEST_DB_1"); @@ -85,7 +92,7 @@ int main() **************************************************************/ Ndb_cluster_connection *cluster_connection= - new Ndb_cluster_connection(); // Object representing the cluster + new Ndb_cluster_connection(connectstring); // Object representing the cluster if (cluster_connection->connect(5,3,1)) { @@ -110,7 +117,7 @@ int main() const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME"); if (myTable == NULL) APIERROR(myDict->getNdbError()); - const NdbDictionary::Index *myIndex= myDict->getIndex("MYINDEXNAME","MYTABLENAME"); + const NdbDictionary::Index *myIndex= myDict->getIndex("MYINDEXNAME$unique","MYTABLENAME"); if (myIndex == NULL) APIERROR(myDict->getNdbError()); |