[sheepdog] [PATCH] dog: add 'vdi cache purge' option

Liu Yuan namei.unix at gmail.com
Wed Aug 21 12:55:58 CEST 2013


This command is intended to be used when

1. cache of some vdi isn't clean on node A, e.g, due to sheep crash
2. This vdi is run on other node B.

After some time, A comes back without manually 'rm cache/', the cache about this
vdi is stale. For this case, we provide a manual option for users to purge the
stale cache.

usage:
 $ dog vdi cache purge image # purge cache of image
 $ dog vdi cache purge # purge cache of all the images

Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 dog/vdi.c                |   37 +++++++++++++++++++++++++++++++++++++
 include/internal_proto.h |    1 +
 sheep/ops.c              |   20 ++++++++++++++++++++
 3 files changed, 58 insertions(+)

diff --git a/dog/vdi.c b/dog/vdi.c
index 6b8d677..6101141 100644
--- a/dog/vdi.c
+++ b/dog/vdi.c
@@ -2025,6 +2025,41 @@ static int vdi_cache_info(int argc, char **argv)
 	return EXIT_SUCCESS;
 }
 
+static int vdi_cache_purge(int argc, char **argv)
+{
+	const char *vdiname;
+	struct sd_req hdr;
+	uint32_t vid;
+	int ret = EXIT_SUCCESS;
+
+	sd_init_req(&hdr, SD_OP_CACHE_PURGE);
+
+	if (argc == 5) {
+		vdiname = argv[optind++];
+		ret = find_vdi_name(vdiname, vdi_cmd_data.snapshot_id,
+				    vdi_cmd_data.snapshot_tag, &vid, 0);
+		if (ret < 0) {
+			sd_err("Failed to open VDI %s", vdiname);
+			ret = EXIT_FAILURE;
+			goto out;
+		}
+		hdr.obj.oid = vid_to_vdi_oid(vid);
+		hdr.flags = SD_FLAG_CMD_WRITE;
+		hdr.data_length = 0;
+	} else {
+		confirm("This operation purges the cache of all the vdi"
+			". Continue? [yes/no]: ");
+	}
+
+	ret = send_light_req(&hdr, sdhost, sdport);
+	if (ret) {
+		sd_err("failed to execute request");
+		return EXIT_FAILURE;
+	}
+out:
+	return ret;
+}
+
 static struct subcommand vdi_cache_cmd[] = {
 	{"flush", NULL, NULL, "flush the cache of the vdi specified.",
 	 NULL, CMD_NEED_ARG, vdi_cache_flush},
@@ -2032,6 +2067,8 @@ static struct subcommand vdi_cache_cmd[] = {
 	 NULL, CMD_NEED_ARG, vdi_cache_delete},
 	{"info", NULL, NULL, "show usage of the cache",
 	 NULL, 0, vdi_cache_info},
+	{"purge", NULL, NULL, "purge the cache of all vdi (no flush)",
+	 NULL, 0, vdi_cache_purge},
 	{NULL,},
 };
 
diff --git a/include/internal_proto.h b/include/internal_proto.h
index 8802300..64a2770 100644
--- a/include/internal_proto.h
+++ b/include/internal_proto.h
@@ -79,6 +79,7 @@
 #define SD_OP_GET_HASH       0xB4
 #define SD_OP_REWEIGHT       0xB5
 #define SD_OP_GET_CACHE_INFO 0xB6
+#define SD_OP_CACHE_PURGE    0xB7
 
 /* internal flags for hdr.flags, must be above 0x80 */
 #define SD_FLAG_CMD_RECOVERY 0x0080
diff --git a/sheep/ops.c b/sheep/ops.c
index d0937f8..4c0f975 100644
--- a/sheep/ops.c
+++ b/sheep/ops.c
@@ -744,6 +744,20 @@ static int local_get_cache_info(struct request *request)
 	return SD_RES_SUCCESS;
 }
 
+static int local_cache_purge(struct request *req)
+{
+	const struct sd_req *hdr = &req->rq;
+	uint32_t vid = oid_to_vid(req->rq.obj.oid);
+
+	if (hdr->flags == SD_FLAG_CMD_WRITE) {
+		object_cache_delete(vid);
+		goto out;
+	}
+	object_cache_format();
+out:
+	return SD_RES_SUCCESS;
+}
+
 /* Return SD_RES_INVALID_PARMS to ask client not to send flush req again */
 static int local_flush_vdi(struct request *req)
 {
@@ -1226,6 +1240,12 @@ static struct sd_op_template sd_ops[] = {
 		.process_work = local_get_cache_info,
 	},
 
+	[SD_OP_CACHE_PURGE] = {
+		.name = "CACHE_PURGE",
+		.type = SD_OP_TYPE_LOCAL,
+		.process_work = local_cache_purge,
+	},
+
 	/* gateway I/O operations */
 	[SD_OP_CREATE_AND_WRITE_OBJ] = {
 		.name = "CREATE_AND_WRITE_OBJ",
-- 
1.7.9.5




More information about the sheepdog mailing list