[Sheepdog] [PATCH] add helper function to return string describing error number

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Mon May 10 13:45:04 CEST 2010


Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 include/collie.h    |   48 ++++++++++++++++++++
 shepherd/shepherd.c |  120 ++++++++------------------------------------------
 2 files changed, 68 insertions(+), 100 deletions(-)

diff --git a/include/collie.h b/include/collie.h
index ecbb72f..ee07ebd 100644
--- a/include/collie.h
+++ b/include/collie.h
@@ -179,4 +179,52 @@ static inline int is_collie_op(uint8_t op)
 	return op & SD_OP_COLLIE;
 }
 
+static inline const char *sd_strerror(int err)
+{
+	int i;
+
+	static const struct {
+		int err;
+		const char *desc;
+	} errors[] = {
+		{SD_RES_SUCCESS, "Success"},
+		{SD_RES_UNKNOWN, "Unknown error"},
+		{SD_RES_NO_OBJ, "No object found"},
+		{SD_RES_EIO, "I/O error"},
+		{SD_RES_VDI_EXIST, "VDI exists already"},
+		{SD_RES_INVALID_PARMS, "Invalid parameters"},
+		{SD_RES_SYSTEM_ERROR, "System error"},
+		{SD_RES_VDI_LOCKED, "VDI is already locked"},
+		{SD_RES_NO_VDI, "No vdi found"},
+		{SD_RES_NO_BASE_VDI, "No base VDI found"},
+		{SD_RES_VDI_READ, "Failed read the requested VDI"},
+		{SD_RES_VDI_WRITE, "Failed to write the requested VDI"},
+		{SD_RES_BASE_VDI_READ, "Failed to read the base VDI"},
+		{SD_RES_BASE_VDI_WRITE, "Failed to write the base VDI"},
+		{SD_RES_NO_TAG, "Failed to find the requested tag"},
+		{SD_RES_STARTUP, "The system is still booting"},
+		{SD_RES_VDI_NOT_LOCKED, "VDI isn't locked"},
+		{SD_RES_SHUTDOWN, "The system is shutting down"},
+		{SD_RES_NO_MEM, "Out of memory on the server"},
+		{SD_RES_FULL_VDI, "We already have the maximum vdis"},
+		{SD_RES_VER_MISMATCH, "Protocol version mismatch"},
+		{SD_RES_NO_SPACE, "Server has no space for new objects"},
+
+		{SD_RES_OLD_NODE_VER, "Remote node has an old epoch"},
+		{SD_RES_NEW_NODE_VER, "Remote node has a new epoch"},
+		{SD_RES_WAIT_FOR_FORMAT, "Waiting for a format operation"},
+		{SD_RES_WAIT_FOR_JOIN, "Waiting for other nodes joining"},
+		{SD_RES_NOT_FORMATTED, "Not formatted yet"},
+		{SD_RES_INVALID_CTIME, "Creation time is different"},
+		{SD_RES_INVALID_EPOCH, "Invalid epoch"},
+		{SD_RES_JOIN_FAILED, "The node was failed to join sheepdog"},
+	};
+
+	for (i = 0; i < ARRAY_SIZE(errors); ++i)
+		if (errors[i].err == err)
+			return errors[i].desc;
+
+	return "Invalid error code";
+}
+
 #endif
diff --git a/shepherd/shepherd.c b/shepherd/shepherd.c
index 8a0e421..5188897 100644
--- a/shepherd/shepherd.c
+++ b/shepherd/shepherd.c
@@ -130,23 +130,8 @@ static int update_node_list(int max_nodes, int epoch)
 		goto out;
 	}
 
-	switch (rsp->result) {
-	case SD_RES_SUCCESS:
-		break;
-	case SD_RES_WAIT_FOR_FORMAT:
-		fprintf(stderr, "sheepdog is not formatted yet\n");
-		ret = -1;
-		goto out;
-	case SD_RES_WAIT_FOR_JOIN:
-		fprintf(stderr, "there is not enough nodes to start sheepdog\n");
-		ret = -1;
-		goto out;
-	case SD_RES_SHUTDOWN:
-		fprintf(stderr, "sheepdog is shutting down\n");
-		ret = -1;
-		goto out;
-	default:
-		fprintf(stderr, "unknown error: %d\n", rsp->result);
+	switch (rsp->result != SD_RES_SUCCESS) {
+		fprintf(stderr, "%s\n", sd_strerror(rsp->result));
 		ret = -1;
 		goto out;
 	}
@@ -202,23 +187,14 @@ static int cluster_format(int argc, char **argv)
 	ret = exec_req(fd, (struct sd_req *)&hdr, NULL, &wlen, &rlen);
 	close(fd);
 
-	if (ret != SD_RES_SUCCESS) {
-		fprintf(stderr, "failed to connect the dog\n");
+	if (ret) {
+		fprintf(stderr, "failed to connect\n");
 		return ret;
 	}
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		switch (rsp->result) {
-		case SD_RES_STARTUP:
-			fprintf(stderr, "the dog is not ready for the mkfs operation\n");
-			break;
-		case SD_RES_SHUTDOWN:
-			fprintf(stderr, "the dog is shutting down\n");
-			break;
-		default:
-			fprintf(stderr, "unknown error\n");
-			break;
-		}
+		fprintf(stderr, "%s\n", sd_strerror(rsp->result));
+		return 1;
 	}
 
 	return 0;
@@ -245,20 +221,14 @@ static int shutdown_sheepdog(void)
 	ret = exec_req(fd, &hdr, NULL, &wlen, &rlen);
 	close(fd);
 
-	if (ret != SD_RES_SUCCESS) {
-		fprintf(stderr, "failed to connect the dog\n");
+	if (ret) {
+		fprintf(stderr, "failed to connect\n");
 		return ret;
 	}
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		switch (rsp->result) {
-		case SD_RES_STARTUP:
-			fprintf(stderr, "the dog is not ready for the mkfs operation\n");
-			break;
-		default:
-			fprintf(stderr, "unknown error\n");
-			break;
-		}
+		fprintf(stderr, "%s\n", sd_strerror(rsp->result));
+		return 1;
 	}
 
 	return 0;
@@ -740,23 +710,14 @@ static int vdi_delete(int argc, char **argv)
 	ret = exec_req(fd, (struct sd_req *)&hdr, vdiname, &wlen, &rlen);
 	close(fd);
 
-	if (ret != SD_RES_SUCCESS) {
-		fprintf(stderr, "failed to connect the dog\n");
+	if (ret) {
+		fprintf(stderr, "failed to connect\n");
 		return ret;
 	}
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		switch (rsp->result) {
-		case SD_RES_VDI_LOCKED:
-			fprintf(stderr, "the vdi is locked\n");
-			break;
-		case SD_RES_NO_VDI:
-			fprintf(stderr, "no such vdi\n");
-			break;
-		default:
-			fprintf(stderr, "error, %d\n", rsp->result);
-			break;
-		}
+		fprintf(stderr, "%s: %s\n", vdiname, sd_strerror(rsp->result));
+		return 1;
 	}
 
 	return 0;
@@ -853,20 +814,7 @@ static int vdi_lock(int argc, char **argv)
 	}
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		switch(rsp->result) {
-		case SD_RES_VDI_LOCKED:
-			fprintf(stderr, "%s is already locked\n", vdiname);
-			break;
-		case SD_RES_VDI_NOT_LOCKED:
-			fprintf(stderr, "%s is not locked\n", vdiname);
-			break;
-		case SD_RES_NO_VDI:
-			fprintf(stderr, "%s: no such vdi\n", vdiname);
-			break;
-		default:
-			fprintf(stderr, "error %d\n", rsp->result);
-			break;
-		}
+		fprintf(stderr, "%s: %s\n", vdiname, sd_strerror(rsp->result));
 		return 1;
 	}
 
@@ -912,20 +860,7 @@ static int vdi_release(int argc, char **argv)
 	}
 
 	if (rsp->result != SD_RES_SUCCESS) {
-		switch(rsp->result) {
-		case SD_RES_VDI_LOCKED:
-			fprintf(stderr, "%s is already locked\n", vdiname);
-			break;
-		case SD_RES_VDI_NOT_LOCKED:
-			fprintf(stderr, "%s is not locked\n", vdiname);
-			break;
-		case SD_RES_NO_VDI:
-			fprintf(stderr, "%s: no such vdi\n", vdiname);
-			break;
-		default:
-			fprintf(stderr, "error %d\n", rsp->result);
-			break;
-		}
+		fprintf(stderr, "%s: %s\n", vdiname, sd_strerror(rsp->result));
 		return 1;
 	}
 
@@ -971,26 +906,11 @@ static int cluster_info(int argc, char **argv)
 	if (ret != 0)
 		return 1;
 
-	switch (rsp->result) {
-	case SD_RES_SUCCESS:
+	if (rsp->result == SD_RES_SUCCESS)
 		printf("running\n");
-		break;
-	case SD_RES_WAIT_FOR_FORMAT:
-		printf("sheepdog is not formatted yet\n");
-		break;
-	case SD_RES_WAIT_FOR_JOIN:
-		printf("sheepdog is waiting for other nodes joining\n");
-		break;
-	case SD_RES_SHUTDOWN:
-		printf("shutdown\n");
-		break;
-	case SD_RES_JOIN_FAILED:
-		printf("failed to join sheepdog\n");
-		break;
-	default:
-		printf("%d\n", rsp->rsvd);
-		break;
-	}
+	else
+		printf("%s\n", sd_strerror(rsp->result));
+
 	printf("\n");
 	printf("Ctime              Epoch Nodes\n");
 	nr_logs = rsp->data_length / sizeof(struct epoch_log);
-- 
1.5.6.5




More information about the sheepdog mailing list