Skip to content

Commit

Permalink
scripts: Move TDB checking from initscript to "init" event
Browse files Browse the repository at this point in the history
It makes sense to do this in the "init" event and make the initscript
less complicated.

Signed-off-by: Martin Schwenke <[email protected]>

(This used to be ctdb commit 3bc93f312b8464fbfa2b2c44fffedc591fe5a3e0)
  • Loading branch information
martin-schwenke committed Jun 20, 2013
1 parent 9614681 commit 6317285
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 98 deletions.
97 changes: 0 additions & 97 deletions ctdb/config/ctdb.init
Original file line number Diff line number Diff line change
Expand Up @@ -120,98 +120,6 @@ export_debug_variables ()
export CTDB_DEBUG_HUNG_SCRIPT CTDB_EXTERNAL_TRACE
}

# Log given message or stdin to either syslog or a CTDB log file
do_log ()
{
script_log "ctdb.init" "$@"
}

select_tdb_checker ()
{
# Find the best TDB consistency check available.
use_tdb_tool_check=false
if which tdbtool >/dev/null 2>&1 && \
echo "help" | tdbtool | grep -q check ; then

use_tdb_tool_check=true
elif which tdbtool >/dev/null 2>&1 && which tdbdump >/dev/null 2>&1 ; then
do_log <<EOF
WARNING: The installed 'tdbtool' does not offer the 'check' subcommand.
Using 'tdbdump' for database checks.
Consider updating 'tdbtool' for better checks!
EOF
elif which tdbdump >/dev/null 2>&1 ; then
do_log <<EOF
WARNING: 'tdbtool' is not available.
Using 'tdbdump' to check the databases.
Consider installing a recent 'tdbtool' for better checks!
EOF
else
do_log <<EOF
WARNING: Cannot check databases since neither
'tdbdump' nor 'tdbtool check' is available.
Consider installing tdbtool or at least tdbdump!
EOF
return 1
fi
}

check_tdb ()
{
_db="$1"

if $use_tdb_tool_check ; then
# tdbtool always exits with 0 :-(
if tdbtool "$_db" check 2>/dev/null |
grep -q "Database integrity is OK" ; then
return 0
else
return 1
fi
else
tdbdump "$_db" >/dev/null 2>/dev/null
return $?
fi
}

check_persistent_databases ()
{
_dir="${CTDB_DBDIR_PERSISTENT:-${CTDB_DBDIR:-/var/ctdb}/persistent}"
mkdir -p "$_dir" 2>/dev/null

[ "${CTDB_MAX_PERSISTENT_CHECK_ERRORS:-0}" = "0" ] || return 0

for _db in $(ls "$_dir/"*.tdb.*[0-9] 2>/dev/null) ; do
check_tdb $_db || {
do_log "Persistent database $_db is corrupted! CTDB will not start."
return 1
}
done
}

check_non_persistent_databases ()
{
_dir="${CTDB_DBDIR:-/var/ctdb}"
mkdir -p "$_dir" 2>/dev/null

for _db in $(ls "${_dir}/"*.tdb.*[0-9] 2>/dev/null) ; do
check_tdb $_db || {
_backup="${_db}.$(date +'%Y%m%d.%H%M%S.%N').corrupt"
do_log <<EOF
WARNING: database ${_db} is corrupted.
Moving to backup ${_backup} for later analysis.
EOF
mv "$_db" "$_backup"

# Now remove excess backups
ls -td "${_db}."*".corrupt" |
tail -n +$((${CTDB_MAX_CORRUPT_DB_BACKUPS:-10} + 1)) |
xargs rm -f

}
done
}

set_retval() {
return $1
}
Expand Down Expand Up @@ -247,11 +155,6 @@ start() {

export_debug_variables

if select_tdb_checker ; then
check_persistent_databases || return $?
check_non_persistent_databases
fi

if [ "$CTDB_SUPPRESS_COREFILE" = "yes" ]; then
ulimit -c 0
else
Expand Down
96 changes: 95 additions & 1 deletion ctdb/config/events.d/00.ctdb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,94 @@ loadconfig

ctdb_setup_service_state_dir "ctdb"

#
############################################################

select_tdb_checker ()
{
# Find the best TDB consistency check available.
use_tdb_tool_check=false
if which tdbtool >/dev/null 2>&1 && \
echo "help" | tdbtool | grep -q check ; then

use_tdb_tool_check=true
elif which tdbtool >/dev/null 2>&1 && which tdbdump >/dev/null 2>&1 ; then
cat <<EOF
WARNING: The installed 'tdbtool' does not offer the 'check' subcommand.
Using 'tdbdump' for database checks.
Consider updating 'tdbtool' for better checks!
EOF
elif which tdbdump >/dev/null 2>&1 ; then
cat <<EOF
WARNING: 'tdbtool' is not available.
Using 'tdbdump' to check the databases.
Consider installing a recent 'tdbtool' for better checks!
EOF
else
cat <<EOF
WARNING: Cannot check databases since neither
'tdbdump' nor 'tdbtool check' is available.
Consider installing tdbtool or at least tdbdump!
EOF
return 1
fi
}

check_tdb ()
{
_db="$1"

if $use_tdb_tool_check ; then
# tdbtool always exits with 0 :-(
if tdbtool "$_db" check 2>/dev/null |
grep -q "Database integrity is OK" ; then
return 0
else
return 1
fi
else
tdbdump "$_db" >/dev/null 2>/dev/null
return $?
fi
}

check_persistent_databases ()
{
_dir="${CTDB_DBDIR_PERSISTENT:-${CTDB_DBDIR:-/var/ctdb}/persistent}"
mkdir -p "$_dir" 2>/dev/null

[ "${CTDB_MAX_PERSISTENT_CHECK_ERRORS:-0}" = "0" ] || return 0

for _db in $(ls "$_dir/"*.tdb.*[0-9] 2>/dev/null) ; do
check_tdb $_db || {
echo "Persistent database $_db is corrupted! CTDB will not start."
return 1
}
done
}

check_non_persistent_databases ()
{
_dir="${CTDB_DBDIR:-/var/ctdb}"
mkdir -p "$_dir" 2>/dev/null

for _db in $(ls "${_dir}/"*.tdb.*[0-9] 2>/dev/null) ; do
check_tdb $_db || {
_backup="${_db}.$(date +'%Y%m%d.%H%M%S.%N').corrupt"
cat <<EOF
WARNING: database ${_db} is corrupted.
Moving to backup ${_backup} for later analysis.
EOF
mv "$_db" "$_backup"

# Now remove excess backups
ls -td "${_db}."*".corrupt" |
tail -n +$((${CTDB_MAX_CORRUPT_DB_BACKUPS:-10} + 1)) |
xargs rm -f

}
done
}

update_config_from_tdb() {

# Pull optional ctdb configuration data out of config.tdb
Expand Down Expand Up @@ -62,6 +149,8 @@ wait_until_ready () {
done
}

############################################################

ctdb_check_args "$@"

case "$1" in
Expand All @@ -79,6 +168,11 @@ case "$1" in
# make sure we drop any ips that might still be held if
# previous instance of ctdb got killed with -9 or similar
drop_all_public_ips

if select_tdb_checker ; then
check_persistent_databases || exit $?
check_non_persistent_databases
fi
;;

setup)
Expand Down

0 comments on commit 6317285

Please sign in to comment.