osops-tools-generic/nova/openstack_db_archive.sh

132 lines
4.4 KiB
Bash
Executable File

#!/bin/bash
#
# Copyright 2012 Hewlett-Packard Development Company, L.P. All Rights Reserved.
#
# Author: Simon McCartney <simon.mccartney@hp.com>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# purge tables of "deleted" records by archiveing them in sensible chunks to the shadow tables
# this work was started in PAASINFRA-206
#
# default to archiving all records flagged as deleted,
# use the -n option to enable dry run mode
unset DRY_RUN
# tables to arhive deleted records from
DATABASE=nova
TABLES="security_group_rules security_group_instance_association security_groups instance_info_caches instance_system_metadata instances reservations compute_node_stats "
## process the command line arguments
while getopts "hnd:H:u:p:" opt; do
case $opt in
h)
echo "openstack_db_archive.sh - archive records flagged as deleted into the shadow tables."
echo "Records are archived from the following tables:"
echo
for TABLE in ${TABLES}
do
echo " ${DATABASE}.${TABLE}"
done
echo
echo "Options:"
echo " -n dry run mode - pass --dry-run to pt-archiver"
echo " -d db name"
echo " -H db hostname"
echo " -u db username"
echo " -p db password"
echo " -h (show help)"
exit 0
;;
n)
DRY_RUN="--dry-run"
;;
d)
DATABASE=${OPTARG}
;;
H)
HOSTPT=",h=${OPTARG}"
HOST="-h ${OPTARG}"
;;
u)
USERPT=",u=${OPTARG}"
USER="-u ${OPTARG}"
;;
p)
PASSPT=",p=${OPTARG}"
PASS="-p${OPTARG}"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
echo
echo `date` "OpenStack Database Archiver starting.."
echo
echo `date` "Purging nova.instance_system_metadata of deleted instance data"
# this is back to front (on delete if you can find a record in instances flagged for deletion)
# --where 'EXISTS(SELECT * FROM instances WHERE deleted!=0 AND uuid=instance_system_metadata.instance_uuid)'
# to delete where there is no active record:
# --where 'NOT EXISTS(SELECT * FROM instances WHERE deleted=0 AND uuid=instance_system_metadata.instance_uuid)'
TABLE=instance_system_metadata
SHADOW_TABLE="shadow_${TABLE}"
pt-archiver ${DRY_RUN} --statistics --sleep-coef 0.75 --progress 100 --commit-each --limit 10 \
--source D=${DATABASE},t=${TABLE}${HOSTPT}${USERPT}${PASSPT} --no-check-charset \
--dest D=${DATABASE},t=${SHADOW_TABLE}${HOSTPT}${USERPT}${PASSPT} \
--where 'EXISTS(SELECT * FROM instances WHERE deleted!=0 AND uuid=instance_system_metadata.instance_uuid)'
for TABLE in ${TABLES}
do
SHADOW_TABLE="shadow_${TABLE}"
ACTIVE_RECORDS=`mysql ${HOST} ${USER} ${PASS} -B -e "select count(id) from ${DATABASE}.${TABLE} where deleted=0" | tail -1`
DELETED_RECORDS=`mysql ${HOST} ${USER} ${PASS} -B -e "select count(id) from ${DATABASE}.${TABLE} where deleted!=0" | tail -1`
LOCAL_ABORTS=`mysql ${HOST} ${USER} ${PASS} -B -e "SHOW STATUS LIKE 'wsrep_%'" | grep -e wsrep_local_bf_aborts -e wsrep_local_cert_failures`
echo
echo
echo `date` "Archiving ${DELETED_RECORDS} records to ${SHADOW_TABLE} from ${TABLE}, leaving ${ACTIVE_RECORDS}"
echo `date` "LOCAL_ABORTS before"
echo ${LOCAL_ABORTS}
pt-archiver ${DRY_RUN} --statistics --progress 100 --commit-each --limit 10 \
--source D=${DATABASE},t=${TABLE}${HOSTPT}${USERPT}${PASSPT} \
--dest D=${DATABASE},t=${SHADOW_TABLE}${HOSTPT}${USERPT}${PASSPT} \
--ignore --no-check-charset --sleep-coef 0.75 \
--where "deleted!=0"
echo `date` "Finished archiving ${DELETED_RECORDS} to ${SHADOW_TABLE} from ${TABLE}"
echo `date` "LOCAL_ABORTS before"
echo ${LOCAL_ABORTS}
LOCAL_ABORTS=`mysql ${HOST} ${USER} ${PASS} -B -e "SHOW STATUS LIKE 'wsrep_%'" | grep -e wsrep_local_bf_aborts -e wsrep_local_cert_failures`
echo `date` "LOCAL_ABORTS after"
echo ${LOCAL_ABORTS}
echo
done
echo
echo `date` "OpenStack Database Archiver finished."
echo