1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-23 17:34:34 +03:00

eventscripts: kill_tcp_connections() should send connections to stdin

This avoids issuing multiple "ctdb killtcp" commands to terminate tcp
connections, one per connection.  This will considerably reduce the
time when there is a large number of tcp connections.  This also makes
it possible to avoid calling "ctdb killtcp" when there are no connections.

Add a couple of unit tests for killtcp and update eventscript unit
test infrastructure to support.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Pair-programmed-with: Amitay Isaacs <amitay@gmail.com>

(This used to be ctdb commit a20d94717d2e4ab866d8a002cdf39c0669b74c6a)
This commit is contained in:
Martin Schwenke 2013-07-25 13:40:43 +10:00 committed by Amitay Isaacs
parent 200c28fbb2
commit a8dd716146
6 changed files with 98 additions and 11 deletions

View File

@ -648,32 +648,35 @@ kill_tcp_connections ()
get_tcp_connections_for_ip "$_ip" | {
_killcount=0
_failed=false
while read dest src; do
echo "Killing TCP connection $src $dest"
ctdb killtcp $src $dest >/dev/null 2>&1 || _failed=true
_destport="${dest##*:}"
_connections=""
_nl="
"
while read _dst _src; do
_destport="${_dst##*:}"
__oneway=$_oneway
case $_destport in
# we only do one-way killtcp for CIFS
139|445) __oneway=true ;;
esac
echo "Killing TCP connection $_src $_dst"
_connections="${_connections}${_nl}${_src} ${_dst}"
if ! $__oneway ; then
ctdb killtcp $dest $src >/dev/null 2>&1 || _failed=true
_connections="${_connections}${_nl}${_dst} ${_src}"
fi
_killcount=$(($_killcount + 1))
done
if $_failed ; then
echo "Failed to send killtcp control"
return
fi
if [ $_killcount -eq 0 ] ; then
return
fi
echo "$_connections" | ctdb killtcp || {
echo "Failed to send killtcp control"
return
}
_count=0
while : ; do
if [ -z "$(get_tcp_connections_for_ip $_ip)" ] ; then

View File

@ -0,0 +1,32 @@
#!/bin/sh
. "${TEST_SCRIPTS_DIR}/unit.sh"
define_test "Release 1 IP, 10 connections killed OK"
setup_ctdb
ctdb_get_1_public_address |
while read dev ip bits ; do
ip addr add "${ip}/${bits}" dev "$dev"
# Setup 10 fake connections...
count=10
out=""
nl="
"
i=0
while [ $i -lt $count ] ; do
echo "${ip}:445 10.254.254.1:1230${i}"
# Expected output for killing this connection
out="${out}${out:+${nl}}Killing TCP connection 10.254.254.1:1230${i} ${ip}:445"
i=$(($i + 1))
done >"$FAKE_NETSTAT_TCP_ESTABLISHED_FILE"
ok <<EOF
$out
Killed $count TCP connections to released IP $ip
EOF
simple_test $dev $ip $bits
done

View File

@ -0,0 +1,38 @@
#!/bin/sh
. "${TEST_SCRIPTS_DIR}/unit.sh"
define_test "Release 1 IP, 10 connections killed, 1 fails"
setup_ctdb
ctdb_get_1_public_address |
while read dev ip bits ; do
ip addr add "${ip}/${bits}" dev "$dev"
# Setup 10 fake connections...
count=10
out=""
nl="
"
i=0
while [ $i -lt $count ] ; do
echo "${ip}:445 10.254.254.1:1230${i}"
# Expected output for killing this connection
out="${out}${out:+${nl}}Killing TCP connection 10.254.254.1:1230${i} ${ip}:445"
i=$(($i + 1))
done >"$FAKE_NETSTAT_TCP_ESTABLISHED_FILE"
# Note that the fake TCP killing done by the "ctdb killtcp" stub
# can only kill conections in the file, so killing this connection
# will never succeed so it will look like a time out.
FAKE_NETSTAT_TCP_ESTABLISHED="${ip}:445|10.254.254.1:43210"
ok <<EOF
Killing TCP connection 10.254.254.1:43210 ${ip}:445
$out
Timed out killing tcp connections for IP $ip
EOF
simple_test $dev $ip $bits
done

View File

@ -123,6 +123,9 @@ setup_generic ()
export FAKE_TDBTOOL_SUPPORTS_CHECK="yes"
export FAKE_TDB_IS_OK
export FAKE_DATE_OUTPUT
export FAKE_NETSTAT_TCP_ESTABLISHED FAKE_TCP_LISTEN FAKE_NETSTAT_UNIX_LISTEN
export FAKE_NETSTAT_TCP_ESTABLISHED_FILE=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
}
tcp_port_down ()

View File

@ -53,6 +53,13 @@ setup_tickles ()
touch "$tickles_file"
}
ctdb_killtcp ()
{
while read _src _dst ; do
sed -i -e "/^$_dst $_src\$/d" "$FAKE_NETSTAT_TCP_ESTABLISHED_FILE"
done
}
setup_pstore ()
{
pstore_dir="$CTDB_VARDIR/fake-ctdb/pstore/$1"
@ -309,6 +316,7 @@ case "$1" in
done
;;
gratiousarp) : ;; # Do nothing for now
killtcp) ctdb_killtcp "$@" ;;
ip) ctdb_ip "$@" ;;
pnn|xpnn) ctdb_pnn ;;
enable) ctdb_enable "$@";;

View File

@ -78,6 +78,9 @@ if $tcp ; then
dst="${i#*|}"
printf "$tcp_fmt" $src $dst "ESTABLISHED"
done
while read src dst ; do
printf "$tcp_fmt" $src $dst "ESTABLISHED"
done <"$FAKE_NETSTAT_TCP_ESTABLISHED_FILE"
if $all || $listen ; then
for i in $FAKE_TCP_LISTEN ; do