[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[lvm-devel] [PATCH] automatic snapshot extension with dmeventd (BZ 427298)



Hi,

the attached patch adds support for automatic snapshot extension. It
uses 2 configuration variables (in lvm.conf):

    # 'snapshot_extend_threshold' and 'snapshot_extend_amount' define how to
    # handle automatic snapshot extension. The former defines when the snapshot
    # should be extended: when its space usage exceeds this many percent. The
    # latter defines how much extra space should be allocated for the snapshot,
    # in percent of its current size.
    #
    # For example, if you set snapshot_extend_threshold to 70 and
    # snapshot_extend_amount to 20, whenever a snapshot exceeds 70% usage, it
    # will be extended by another 20%. For a 1G snapshot, using 700M usage will
    # trigger a resize to 1.2G. When the usage exceeds 840M, the snapshot will
    # be extended to 1.44G, and so on.
    #
    # Setting snapshot_extend_threshold to 100 disables automatic extensions.

    snapshot_extend_threshold = 100
    snapshot_extend_amount = 20

It works by calling lvextend --use-policies every time that the snapshot
use grows by a 5% step, starting at 50% (i.e. it checks at 50, 55, 60,
... 95). The implementation of lvextend --use-policies checks the
configuration options and if the threshold is hit, it extends the
volume. (On a second thought, this 50% should be either relaxed (to 5)
or documented in lvm.conf.)

The patch comes with two automated tests, one testing lvextend
--use-policies directly (test/t-lvextend-snapshot-policy.sh), another
for the dmeventd integration (test/t-lvextend-snapshot-dmeventd.sh).

The latter is somewhat suboptimal, since dmeventd only checks the
snapshot every 10 seconds, so it takes a bit longish. This can be fixed
later though, shouldn't be a real problem for anything.

Yours,
   Petr.

PS: I'll look at the related BZ 189462 next (umount when the snapshot is
invalidated). Shouldn't be too hard, it just needs to find the right
mount point, presumably by consulting /proc/mounts, and use umount2 with
MNT_FORCE and MNT_DETACH. It's probably better not to touch (nor trust)
/etc/mtab.

diff -rN -u -p old-snapshot-monitoring/daemons/dmeventd/plugins/snapshot/dmeventd_snapshot.c new-snapshot-monitoring/daemons/dmeventd/plugins/snapshot/dmeventd_snapshot.c
--- old-snapshot-monitoring/daemons/dmeventd/plugins/snapshot/dmeventd_snapshot.c	2010-10-05 15:50:53.000000000 +0200
+++ new-snapshot-monitoring/daemons/dmeventd/plugins/snapshot/dmeventd_snapshot.c	2010-10-05 15:50:53.000000000 +0200
@@ -26,8 +26,8 @@
 
 /* First warning when snapshot is 80% full. */
 #define WARNING_THRESH 80
-/* Further warnings at 85%, 90% and 95% fullness. */
-#define WARNING_STEP 5
+/* Run a check every 5%. */
+#define CHECK_STEP 5
 
 struct snap_status {
 	int invalid;
@@ -69,6 +69,27 @@ static void _parse_snapshot_params(char 
 	status->max = atoi(p);
 }
 
+static int _extend(const char *device)
+{
+	char *vg = NULL, *lv = NULL, *layer = NULL;
+	char cmd_str[1024];
+	int r = 0;
+
+	if (!dm_split_lvm_name(dmeventd_lvm2_pool(), device, &vg, &lv, &layer)) {
+		syslog(LOG_ERR, "Unable to determine VG name from %s.", device);
+		return 0;
+	}
+	if (1024 <= snprintf(cmd_str, 1024, "lvextend --use-policies %s/%s", vg, lv)) {
+		syslog(LOG_ERR, "Unable to form LVM command: Device name too long.");
+		return 0;
+	}
+
+	r = dmeventd_lvm2_run(cmd_str);
+	syslog(LOG_INFO, "Extension of snapshot %s/%s %s.", vg, lv,
+	       (r == ECMD_PROCESSED) ? "finished successfully" : "failed");
+	return r == ECMD_PROCESSED;
+}
+
 void process_event(struct dm_task *dmt,
 		   enum dm_event_mask event __attribute__((unused)),
 		   void **private)
@@ -79,10 +100,10 @@ void process_event(struct dm_task *dmt,
 	char *params;
 	struct snap_status status = { 0 };
 	const char *device = dm_task_get_name(dmt);
-	int percent, *percent_warning = (int*)private;
+	int percent, *percent_check = (int*)private;
 
 	/* No longer monitoring, waiting for remove */
-	if (!*percent_warning)
+	if (!*percent_check)
 		return;
 
 	dmeventd_lvm2_lock();
@@ -92,21 +113,27 @@ void process_event(struct dm_task *dmt,
 		goto out;
 
 	_parse_snapshot_params(params, &status);
+
 	/*
 	 * If the snapshot has been invalidated or we failed to parse
 	 * the status string. Report the full status string to syslog.
 	 */
 	if (status.invalid || !status.max) {
 		syslog(LOG_ERR, "Snapshot %s changed state to: %s\n", device, params);
-		*percent_warning = 0;
+		*percent_check = 0;
 		goto out;
 	}
 
 	percent = 100 * status.used / status.max;
-	if (percent >= *percent_warning) {
-		syslog(LOG_WARNING, "Snapshot %s is now %i%% full.\n", device, percent);
-		/* Print warning on the next multiple of WARNING_STEP. */
-		*percent_warning = (percent / WARNING_STEP) * WARNING_STEP + WARNING_STEP;
+	if (percent >= *percent_check) {
+		/* Usage has raised more than CHECK_STEP since the last
+		   time. Run actions. */
+		*percent_check = (percent / CHECK_STEP) * CHECK_STEP + CHECK_STEP;
+		if (percent >= WARNING_THRESH) /* Print a warning to syslog. */
+			syslog(LOG_WARNING, "Snapshot %s is now %i%% full.\n", device, percent);
+		/* Try to extend the snapshot, in accord with user-set policies */
+		if (!_extend(device))
+			syslog(LOG_ERR, "Failed to extend snapshot %s.", device);
 	}
 out:
 	dmeventd_lvm2_unlock();
@@ -118,10 +145,11 @@ int register_device(const char *device,
 		    int minor __attribute__((unused)),
 		    void **private)
 {
-	int *percent_warning = (int*)private;
+	int *percent_check = (int*)private;
 	int r = dmeventd_lvm2_init();
 
-	*percent_warning = WARNING_THRESH; /* Print warning if snapshot is full */
+	/* Do not bother checking snapshots less than 50% full. */
+	*percent_check = 50;
 
 	syslog(LOG_INFO, "Monitoring snapshot %s\n", device);
 	return r;
diff -rN -u -p old-snapshot-monitoring/doc/example.conf.in new-snapshot-monitoring/doc/example.conf.in
--- old-snapshot-monitoring/doc/example.conf.in	2010-10-05 15:50:53.000000000 +0200
+++ new-snapshot-monitoring/doc/example.conf.in	2010-10-05 15:50:53.000000000 +0200
@@ -422,6 +422,28 @@ activation {
     mirror_log_fault_policy = "allocate"
     mirror_image_fault_policy = "remove"
 
+    # 'snapshot_extend_threshold' and 'snapshot_extend_amount' define how to
+    # handle automatic snapshot extension. The former defines when the snapshot
+    # should be extended: when its space usage exceeds this many percent. The
+    # latter defines how much extra space should be allocated for the snapshot,
+    # in percent of its current size.
+    #
+    # For example, if you set snapshot_extend_threshold to 70 and
+    # snapshot_extend_amount to 20, whenever a snapshot exceeds 70% usage, it
+    # will be extended by another 20%. For a 1G snapshot, using 700M usage will
+    # trigger a resize to 1.2G. When the usage exceeds 840M, the snapshot will
+    # be extended to 1.44G, and so on.
+    #
+    # Setting snapshot_extend_threshold to 100 disables automatic extensions.
+
+    snapshot_extend_threshold = 100
+    snapshot_extend_amount = 20
+
+    how a device failure affecting a
+    # mirror is handled.  A mirror is composed of mirror images (copies) and a
+    # log.  A disk log ensures that a mirror does not need to be re-synced (all
+    # copies made the same) every time a machine reboots or crashes.
+    #
     # While activating devices, I/O to devices being (re)configured is
     # suspended, and as a precaution against deadlocks, LVM2 needs to pin
     # any memory it is using so it is not paged out.  Groups of pages that
diff -rN -u -p old-snapshot-monitoring/test/test-utils.sh new-snapshot-monitoring/test/test-utils.sh
--- old-snapshot-monitoring/test/test-utils.sh	2010-10-05 15:50:53.000000000 +0200
+++ new-snapshot-monitoring/test/test-utils.sh	2010-10-05 15:50:53.000000000 +0200
@@ -121,6 +121,7 @@ teardown_devs() {
 		init_udev_transaction
 		while dmsetup table | grep -q ^$PREFIX; do
 			for s in `dmsetup info -c -o name --noheading | grep ^$PREFIX`; do
+				umount -fl $DM_DEV_DIR/mapper/$s || true
 				dmsetup remove $s >& /dev/null || true
 			done
 		done
@@ -394,6 +395,8 @@ prepare_lvmconf() {
     udev_sync = 1
     udev_rules = 1
     polling_interval = 0
+    snapshot_extend_amount = 50
+    snapshot_extend_threshold = 50
   }
 EOF
 	# FIXME remove this workaround after mmap & truncating file problems solved
diff -rN -u -p old-snapshot-monitoring/test/t-lvextend-snapshot-dmeventd.sh new-snapshot-monitoring/test/t-lvextend-snapshot-dmeventd.sh
--- old-snapshot-monitoring/test/t-lvextend-snapshot-dmeventd.sh	1970-01-01 01:00:00.000000000 +0100
+++ new-snapshot-monitoring/test/t-lvextend-snapshot-dmeventd.sh	2010-10-05 15:50:53.000000000 +0200
@@ -0,0 +1,51 @@
+#!/bin/bash
+# Copyright (C) 2010 Red Hat, Inc. All rights reserved.
+#
+# This copyrighted material is made available to anyone wishing to use,
+# modify, copy, or redistribute it subject to the terms and conditions
+# of the GNU General Public License v.2.
+#
+# 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
+
+. ./test-utils.sh
+
+extend() {
+	lvextend --use-policies --config "activation { snapshot_extend_threshold = $1 }" $vg/snap
+}
+
+write() {
+	mount $DM_DEV_DIR/$vg/snap mnt
+	dd if=/dev/zero of=mnt/file$1 bs=1k count=$2
+	umount mnt
+}
+
+percent() {
+	lvs $vg/snap -o snap_percent --noheadings | cut -c4- | cut -d. -f1
+}
+
+which mkfs.ext2 || exit 200
+
+aux prepare_vg 2
+aux prepare_dmeventd
+
+lvcreate -l 8 -n base $vg
+mkfs.ext2 $DM_DEV_DIR/$vg/base
+
+lvcreate -s -l 4 -n snap $vg/base
+lvchange --monitor y $vg/snap
+
+mkdir mnt
+
+write 1 4096
+pre=`percent`
+sleep 10 # dmeventd only checks every 10 seconds :(
+post=`percent`
+
+test $pre = $post
+write 2 5000
+pre=`percent`
+sleep 10 # dmeventd only checks every 10 seconds :(
+post=`percent`
+test $pre -gt $post
diff -rN -u -p old-snapshot-monitoring/test/t-lvextend-snapshot-policy.sh new-snapshot-monitoring/test/t-lvextend-snapshot-policy.sh
--- old-snapshot-monitoring/test/t-lvextend-snapshot-policy.sh	1970-01-01 01:00:00.000000000 +0100
+++ new-snapshot-monitoring/test/t-lvextend-snapshot-policy.sh	2010-10-05 15:50:53.000000000 +0200
@@ -0,0 +1,47 @@
+#!/bin/bash
+# Copyright (C) 2010 Red Hat, Inc. All rights reserved.
+#
+# This copyrighted material is made available to anyone wishing to use,
+# modify, copy, or redistribute it subject to the terms and conditions
+# of the GNU General Public License v.2.
+#
+# 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
+
+. ./test-utils.sh
+
+extend() {
+	lvextend --use-policies --config "activation { snapshot_extend_threshold = $1 }" $vg/snap
+}
+
+write() {
+	mount $DM_DEV_DIR/$vg/snap mnt
+	dd if=/dev/zero of=mnt/file$1 bs=1k count=$2
+	umount mnt
+}
+
+percent() {
+	lvs $vg/snap -o snap_percent --noheadings | cut -c4- | cut -d. -f1
+}
+
+which mkfs.ext2 || exit 200
+
+aux prepare_vg 2
+lvcreate -l 8 -n base $vg
+mkfs.ext2 $DM_DEV_DIR/$vg/base
+
+lvcreate -s -l 4 -n snap $vg/base
+mkdir mnt
+
+write 1 4096
+pre=`percent`
+extend 50
+post=`percent`
+
+test $pre = $post
+write 2 4096
+pre=`percent`
+extend 50
+post=`percent`
+test $pre -gt $post
diff -rN -u -p old-snapshot-monitoring/tools/commands.h new-snapshot-monitoring/tools/commands.h
--- old-snapshot-monitoring/tools/commands.h	2010-10-05 15:50:53.000000000 +0200
+++ new-snapshot-monitoring/tools/commands.h	2010-10-05 15:50:53.000000000 +0200
@@ -259,6 +259,7 @@ xx(lvextend,
    "\t{-l|--extents [+]LogicalExtentsNumber[%{VG|LV|PVS|FREE|ORIGIN}] |\n"
    "\t -L|--size [+]LogicalVolumeSize[bBsSkKmMgGtTpPeE]}\n"
    "\t[-m|--mirrors Mirrors]\n"
+   "\t[--use-policies]\n"
    "\t[-n|--nofsck]\n"
    "\t[--noudevsync]\n"
    "\t[-r|--resizefs]\n"
@@ -270,7 +271,7 @@ xx(lvextend,
 
    alloc_ARG, autobackup_ARG, extents_ARG, force_ARG, mirrors_ARG,
    nofsck_ARG, noudevsync_ARG, resizefs_ARG, size_ARG, stripes_ARG,
-   stripesize_ARG, test_ARG, type_ARG)
+   stripesize_ARG, test_ARG, type_ARG, use_policies_ARG)
 
 xx(lvmchange,
    "With the device mapper, this is obsolete and does nothing.",
diff -rN -u -p old-snapshot-monitoring/tools/lvresize.c new-snapshot-monitoring/tools/lvresize.c
--- old-snapshot-monitoring/tools/lvresize.c	2010-10-05 15:50:53.000000000 +0200
+++ new-snapshot-monitoring/tools/lvresize.c	2010-10-05 15:50:53.000000000 +0200
@@ -186,6 +186,7 @@ static int _lvresize_params(struct cmd_c
 	const char *cmd_name;
 	char *st;
 	unsigned dev_dir_found = 0;
+	int use_policy = arg_count(cmd, use_policies_ARG);
 
 	lp->sign = SIGN_NONE;
 	lp->resize = LV_ANY;
@@ -196,34 +197,41 @@ static int _lvresize_params(struct cmd_c
 	if (!strcmp(cmd_name, "lvextend"))
 		lp->resize = LV_EXTEND;
 
-	/*
-	 * Allow omission of extents and size if the user has given us
-	 * one or more PVs.  Most likely, the intent was "resize this
-	 * LV the best you can with these PVs"
-	 */
-	if ((arg_count(cmd, extents_ARG) + arg_count(cmd, size_ARG) == 0) &&
-	    (argc >= 2)) {
-		lp->extents = 100;
-		lp->percent = PERCENT_PVS;
+	if (use_policy) {
+		/* do nothing; _lvresize will handle --use-policies itself */
+		lp->extents = 0;
 		lp->sign = SIGN_PLUS;
-	} else if ((arg_count(cmd, extents_ARG) +
-		    arg_count(cmd, size_ARG) != 1)) {
-		log_error("Please specify either size or extents but not "
-			  "both.");
-		return 0;
-	}
-
-	if (arg_count(cmd, extents_ARG)) {
-		lp->extents = arg_uint_value(cmd, extents_ARG, 0);
-		lp->sign = arg_sign_value(cmd, extents_ARG, SIGN_NONE);
-		lp->percent = arg_percent_value(cmd, extents_ARG, PERCENT_NONE);
-	}
-
-	/* Size returned in kilobyte units; held in sectors */
-	if (arg_count(cmd, size_ARG)) {
-		lp->size = arg_uint64_value(cmd, size_ARG, UINT64_C(0));
-		lp->sign = arg_sign_value(cmd, size_ARG, SIGN_NONE);
-		lp->percent = PERCENT_NONE;
+		lp->percent = PERCENT_LV;
+	} else {
+		/*
+		 * Allow omission of extents and size if the user has given us
+		 * one or more PVs.  Most likely, the intent was "resize this
+		 * LV the best you can with these PVs"
+		 */
+		if ((arg_count(cmd, extents_ARG) + arg_count(cmd, size_ARG) == 0) &&
+		    (argc >= 2)) {
+			lp->extents = 100;
+			lp->percent = PERCENT_PVS;
+			lp->sign = SIGN_PLUS;
+		} else if ((arg_count(cmd, extents_ARG) +
+			    arg_count(cmd, size_ARG) != 1)) {
+			log_error("Please specify either size or extents but not "
+				  "both.");
+			return 0;
+		}
+
+		if (arg_count(cmd, extents_ARG)) {
+			lp->extents = arg_uint_value(cmd, extents_ARG, 0);
+			lp->sign = arg_sign_value(cmd, extents_ARG, SIGN_NONE);
+			lp->percent = arg_percent_value(cmd, extents_ARG, PERCENT_NONE);
+		}
+
+		/* Size returned in kilobyte units; held in sectors */
+		if (arg_count(cmd, size_ARG)) {
+			lp->size = arg_uint64_value(cmd, size_ARG, UINT64_C(0));
+			lp->sign = arg_sign_value(cmd, size_ARG, SIGN_NONE);
+			lp->percent = PERCENT_NONE;
+		}
 	}
 
 	if (lp->resize == LV_EXTEND && lp->sign == SIGN_MINUS) {
@@ -269,6 +277,31 @@ static int _lvresize_params(struct cmd_c
 	return 1;
 }
 
+static int _adjust_policy_params(struct cmd_context *cmd,
+				 struct logical_volume *lv, struct lvresize_params *lp)
+{
+	float percent;
+	percent_range_t range;
+	int policy_threshold, policy_amount;
+
+	policy_threshold =
+		find_config_tree_int(cmd, "activation/snapshot_extend_threshold", 100);
+	policy_amount =
+		find_config_tree_int(cmd, "activation/snapshot_extend_amount", 20);
+
+	if (policy_thershold >= 100)
+		return 1; /* nothing to do */
+
+	if (!lv_snapshot_percent(lv, &percent, &range))
+		return_0;
+
+	if (range != PERCENT_0_TO_100 || percent <= policy_threshold)
+		return 1; /* nothing to do */
+
+	lp->extents = policy_amount;
+	return 1;
+}
+
 static int _lvresize(struct cmd_context *cmd, struct volume_group *vg,
 		     struct lvresize_params *lp)
 {
@@ -287,6 +320,7 @@ static int _lvresize(struct cmd_context 
 	uint32_t seg_extents;
 	uint32_t sz, str;
 	struct dm_list *pvh = NULL;
+	int use_policy = arg_count(cmd, use_policies_ARG);
 
 	/* does LV exist? */
 	if (!(lvl = find_lv_in_vg(vg, lp->lv_name))) {
@@ -319,6 +353,14 @@ static int _lvresize(struct cmd_context 
 
 	lv = lvl->lv;
 
+	if (use_policy) {
+		if (!lv_is_cow(lv)) {
+			log_error("Can't use policy-based resize for non-snapshot volumes.");
+			return ECMD_FAILED;
+		}
+		_adjust_policy_params(cmd, lv, lp);
+	}
+
 	if (!lv_is_visible(lv)) {
 		log_error("Can't resize internal logical volume %s", lv->name);
 		return ECMD_FAILED;
@@ -404,6 +446,8 @@ static int _lvresize(struct cmd_context 
 	}
 
 	if (lp->extents == lv->le_count) {
+		if (use_policy)
+			return ECMD_PROCESSED; /* Nothing to do. */
 		if (!lp->resizefs) {
 			log_error("New size (%d extents) matches existing size "
 				  "(%d extents)", lp->extents, lv->le_count);

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]