summaryrefslogtreecommitdiff
path: root/mysql-test/ndb/ndbcluster.sh
blob: 16bb3a9b122a474dd40f0a8b081f72c98c3657ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/bin/sh
# Copyright (C) 2004 MySQL AB
# For a more info consult the file COPYRIGHT distributed with this file

# This scripts starts the table handler ndbcluster

# configurable parameters, make sure to change in mysqlcluterd as well
port=@ndb_port@
port_base=@ndb_port_base@
fsdir=`pwd`
# end configurable parameters

#BASEDIR is always one above mysql-test directory
CWD=`pwd`
cd ..
BASEDIR=`pwd`
cd $CWD

# Are we using a source or a binary distribution?
if [ -d ../sql ] ; then
   SOURCE_DIST=1
   ndbtop=$BASEDIR/ndb
   exec_ndb=$ndbtop/src/kernel/ndbd
   exec_mgmtsrvr=$ndbtop/src/mgmsrv/ndb_mgmd
   exec_waiter=$ndbtop/tools/ndb_waiter
   exec_test=$ndbtop/tools/ndb_test_platform
   exec_mgmtclient=$ndbtop/src/mgmclient/ndb_mgm
else
   BINARY_DIST=1
   if test -x "$BASEDIR/libexec/ndbd"
   then
     exec_ndb=$BASEDIR/libexec/ndbd
     exec_mgmtsrvr=$BASEDIR/libexec/ndb_mgmd
   else
     exec_ndb=$BASEDIR/bin/ndbd
     exec_mgmtsrvr=$BASEDIR/bin/ndb_mgmd
   fi
   exec_waiter=$BASEDIR/bin/ndb_waiter
   exec_test=$BASEDIR/bin/ndb_test_platform
   exec_mgmtclient=$BASEDIR/bin/ndb_mgm
fi

if $exec_test ; then :; else
  echo "ndb not correctly compiled to support this platform"
  exit 1
fi

pidfile=ndbcluster.pid
cfgfile=Ndb.cfg
test_ndb=
stop_ndb=
initial_ndb=
status_ndb=
ndb_diskless=0

ndb_no_ord=512
ndb_con_op=105000
ndb_dmem=80M
ndb_imem=24M

NDB_MGM_EXTRA_OPTS=
NDB_MGMD_EXTRA_OPTS=
NDBD_EXTRA_OPTS=

while test $# -gt 0; do
  case "$1" in
    --test)
     test_ndb=1
     ;;
    --stop)
     stop_ndb=1
     ;;
    --initial)
     flags_ndb="$flags_ndb --initial"
     initial_ndb=1
     ;;
    --debug*)
     flags_ndb="$flags_ndb $1"
     ;;
    --status)
     status_ndb=1
     ;;
    --small)
     ndb_no_ord=128
     ndb_con_op=10000
     ndb_dmem=40M
     ndb_imem=12M
     ;;
    --diskless)
     ndb_diskless=1
     ;;
    --data-dir=*)
     fsdir=`echo "$1" | sed -e "s;--data-dir=;;"`
     ;;
    --port=*)
     port=`echo "$1" | sed -e "s;--port=;;"`
     ;;
    --port-base=*)
     port_base=`echo "$1" | sed -e "s;--port-base=;;"`
     ;;
    --ndb_mgm-extra-opts=*)
     NDB_MGM_EXTRA_OPTS=`echo "$1" | sed -e "s;--ndb_mgm-extra-opts=;;"`
     ;;
    --ndb_mgmd-extra-opts=*)
     NDB_MGMD_EXTRA_OPTS=`echo "$1" | sed -e "s;--ndb_mgmd-extra-opts=;;"`
     ;;
    --ndbd-extra-opts=*)
     NDBD_EXTRA_OPTS=`echo "$1" | sed -e "s;--ndbd-extra-opts=;;"`
     ;;
    -- )  shift; break ;;
    --* ) $ECHO "Unrecognized option: $1"; exit 1 ;;
    * ) break ;;
  esac
  shift
done

fs_ndb="$fsdir/ndbcluster-$port"

NDB_HOME=
if [ ! -x "$fsdir" ]; then
  echo "$fsdir missing"
  exit 1
fi
if [ ! -x "$exec_ndb" ]; then
  echo "$exec_ndb missing"
  exit 1
fi
if [ ! -x "$exec_mgmtsrvr" ]; then
  echo "$exec_mgmtsrvr missing"
  exit 1
fi
if [ ! -x "$exec_waiter" ]; then
  echo "$exec_waiter missing"
  exit 1
fi

exec_mgmtclient="$exec_mgmtclient --no-defaults $NDB_MGM_EXTRA_OPTS"
exec_mgmtsrvr="$exec_mgmtsrvr --no-defaults $NDB_MGMD_EXTRA_OPTS"
exec_ndb="$exec_ndb --no-defaults $NDBD_EXTRA_OPTS"
exec_waiter="$exec_waiter --no-defaults"

ndb_host="localhost"
ndb_mgmd_port=$port
NDB_CONNECTSTRING="host=$ndb_host:$ndb_mgmd_port"
export NDB_CONNECTSTRING

sleep_until_file_created () {
  file=$1
  loop=$2
  org_time=$2
  message=$3
  while (test $loop -gt 0)
  do
    if [ -r $file ]
    then
      return 0
    fi
    sleep 1
    loop=`expr $loop - 1`
  done
  if [ $message ]
  then
    echo $message
  fi
  echo "ERROR: $file was not created in $org_time seconds;  Aborting"
  return 1;
}

start_default_ndbcluster() {

# do some checks

if [ "$initial_ndb" ] ; then
  [ -d "$fs_ndb" ] || mkdir "$fs_ndb"
fi
if [ -d "$fs_ndb" ]; then :; else
  echo "$fs_ndb filesystem directory does not exist"
  exit 1
fi

# Start management server as deamon

# Edit file system path and ports in config file
if [ $initial_ndb ] ; then
  rm -f $fs_ndb/ndb_* 2>&1 | cat > /dev/null
  sed \
    -e s,"CHOOSE_MaxNoOfOrderedIndexes","$ndb_no_ord",g \
    -e s,"CHOOSE_MaxNoOfConcurrentOperations","$ndb_con_op",g \
    -e s,"CHOOSE_DataMemory","$ndb_dmem",g \
    -e s,"CHOOSE_IndexMemory","$ndb_imem",g \
    -e s,"CHOOSE_Diskless","$ndb_diskless",g \
    -e s,"CHOOSE_HOSTNAME_".*,"$ndb_host",g \
    -e s,"CHOOSE_FILESYSTEM","$fs_ndb",g \
    -e s,"CHOOSE_PORT_MGM","$ndb_mgmd_port",g \
    -e s,"CHOOSE_PORT_TRANSPORTER","$port_base",g \
    < ndb/ndb_config_2_node.ini \
    > "$fs_ndb/config.ini"
fi

rm -f "$cfgfile" 2>&1 | cat > /dev/null
rm -f "$fs_ndb/$cfgfile" 2>&1 | cat > /dev/null

if ( cd "$fs_ndb" ; $exec_mgmtsrvr -f config.ini ) ; then :; else
  echo "Unable to start $exec_mgmtsrvr from `pwd`"
  exit 1
fi
if sleep_until_file_created $fs_ndb/ndb_3.pid 120
then :; else
  exit 1
fi
cat `find "$fs_ndb" -name 'ndb_*.pid'` > "$fs_ndb/$pidfile"

# Start database node 

echo "Starting ndbd"
( cd "$fs_ndb" ; $exec_ndb $flags_ndb & )
if sleep_until_file_created $fs_ndb/ndb_1.pid 120
then :; else
  stop_default_ndbcluster
  exit 1
fi
cat `find "$fs_ndb" -name 'ndb_*.pid'` > "$fs_ndb/$pidfile"

# Start database node 

echo "Starting ndbd"
( cd "$fs_ndb" ; $exec_ndb $flags_ndb & )
if sleep_until_file_created $fs_ndb/ndb_2.pid 120
then :; else
  stop_default_ndbcluster
  exit 1
fi
cat `find "$fs_ndb" -name 'ndb_*.pid'` > "$fs_ndb/$pidfile"

# test if Ndb Cluster starts properly

echo "Waiting for started..."
if ( $exec_waiter ) | grep "NDBT_ProgramExit: 0 - OK"; then :; else
  echo "Ndbcluster startup failed"
  stop_default_ndbcluster
  exit 1
fi

cat `find "$fs_ndb" -name 'ndb_*.pid'` > $fs_ndb/$pidfile

status_ndbcluster
}

status_ndbcluster() {
  # Start management client
  $exec_mgmtclient -e show
}

stop_default_ndbcluster() {

# Start management client

exec_mgmtclient="$exec_mgmtclient --try-reconnect=1"

$exec_mgmtclient -e shutdown 2>&1 | cat > /dev/null

if [ -f "$fs_ndb/$pidfile" ] ; then
  kill_pids=`cat "$fs_ndb/$pidfile"`
  attempt=0
  while [ $attempt -lt 10 ] ; do
    new_kill_pid=""
    kill_pids2=""
    for p in $kill_pids ; do
      kill -0 $p 2> /dev/null
      if [ $? -eq 0 ] ; then
        new_kill_pid="$p $new_kill_pid"
        kill_pids2="-$p $kill_pids2"
      fi
    done
    kill_pids=$new_kill_pid
    if [ -z "$kill_pids" ] ; then
      break
    fi
    sleep 1
    attempt=`expr $attempt + 1`
  done
  if [ "$kill_pids2" != "" ] ; then
    echo "Failed to shutdown ndbcluster, executing kill "$kill_pids2
    kill          -9 -- $kill_pids2 2> /dev/null
    /bin/kill     -9 -- $kill_pids2 2> /dev/null
    /usr/bin/kill -9 -- $kill_pids2 2> /dev/null
    kill          -9    $kill_pids2 2> /dev/null
    /bin/kill     -9    $kill_pids2 2> /dev/null
    /usr/bin/kill -9    $kill_pids2 2> /dev/null
  fi
  rm "$fs_ndb/$pidfile"
fi
}

initialize_ndb_test ()
{
  fs_result=$fs_ndb/r
  rm -rf $fs_result
  mkdir $fs_result
  echo ------------------
  echo starting ndb tests
  echo ------------------
}

do_ndb_test ()
{
  test_name=$1

  clusterlog=$fs_ndb/ndb_3_cluster.log

  test_log_result=$fs_result/${test_name}_log.result
  test_log_reject=$fs_result/${test_name}_log.reject
  test_result=$fs_result/${test_name}.result
  test_reject=$fs_result/${test_name}.reject

  clean_log='s/.*\[MgmSrvr\]//'

  cat $clusterlog ndb/${test_name}_log.result | sed -e $clean_log > $test_log_result

  cp ndb/${test_name}.result $test_result

  cat ndb/${test_name}.test | $exec_mgmtclient > $test_reject
  cat $clusterlog | sed -e $clean_log > $test_log_reject

  t="pass"
  diff -C 5 $test_result $test_reject || t="fail"
  printf "ndb_mgm output    %20s [%s]\n" $test_name $t
  t="pass"
  diff -C 5 $test_log_result $test_log_reject || t="fail"
  printf "clusterlog output %20s [%s]\n" $test_name $t
}

if [ $status_ndb ] ; then
  status_ndbcluster
  exit 0
fi

if [ $stop_ndb ] ; then
  stop_default_ndbcluster
else
  start_default_ndbcluster
fi

if [ $test_ndb ] ; then
  initialize_ndb_test
  all_tests=`ls ndb/*.test | sed "s#ndb/##" | sed "s#.test##"`  
  for a in $all_tests ; do
    do_ndb_test $a
  done
  echo ------------------
  echo shutting down cluster
  stop_default_ndbcluster
fi

exit 0