[corosync] [PATCH 12/14] Move mon service to use icmap

Jan Friesse jfriesse at redhat.com
Wed Dec 14 15:41:20 GMT 2011


Signed-off-by: Jan Friesse <jfriesse at redhat.com>
---
 services/mon.c |  280 +++++++++++++++++++-------------------------------------
 1 files changed, 94 insertions(+), 186 deletions(-)

diff --git a/services/mon.c b/services/mon.c
index 6c068f6..15d32b5 100644
--- a/services/mon.c
+++ b/services/mon.c
@@ -45,6 +45,7 @@
 #include <corosync/engine/coroapi.h>
 #include <corosync/list.h>
 #include <corosync/engine/logsys.h>
+#include <corosync/engine/icmap.h>
 #include "../exec/fsm.h"
 
 
@@ -57,7 +58,6 @@ static int mon_exec_init_fn (
 	struct corosync_api_v1 *corosync_api);
 
 static struct corosync_api_v1 *api;
-static hdb_handle_t resources_obj;
 #define MON_DEFAULT_PERIOD 3000
 #define MON_MIN_PERIOD 500
 #define MON_MAX_PERIOD (120 * CS_TIME_MS_IN_SEC)
@@ -84,13 +84,13 @@ static DECLARE_LIST_INIT (confchg_notify);
 
 
 struct resource_instance {
-	hdb_handle_t handle;
+	const char *icmap_path;
 	const char *name;
 	corosync_timer_handle_t timer_handle;
 	void (*update_stats_fn) (void *data);
 	struct cs_fsm fsm;
 	uint64_t period;
-	objdb_value_types_t max_type;
+	icmap_value_types_t max_type;
 	union {
 		int32_t int32;
 		double dbl;
@@ -102,16 +102,18 @@ static void load_update_stats_fn (void *data);
 
 static struct resource_instance memory_used_inst = {
 	.name = "memory_used",
+	.icmap_path = "resources.system.memory_used.",
 	.update_stats_fn = mem_update_stats_fn,
-	.max_type = OBJDB_VALUETYPE_INT32,
+	.max_type = ICMAP_VALUETYPE_INT32,
 	.max.int32 = INT32_MAX,
 	.period = MON_DEFAULT_PERIOD,
 };
 
 static struct resource_instance load_15min_inst = {
 	.name = "load_15min",
+	.icmap_path = "resources.system.load_15min.",
 	.update_stats_fn = load_update_stats_fn,
-	.max_type = OBJDB_VALUETYPE_DOUBLE,
+	.max_type = ICMAP_VALUETYPE_DOUBLE,
 	.max.dbl = INT32_MAX,
 	.period = MON_DEFAULT_PERIOD,
 };
@@ -225,29 +227,12 @@ static const char * mon_res_event_to_str(struct cs_fsm* fsm,
 	return NULL;
 }
 
-static cs_error_t str_to_uint64_t(const char* str, uint64_t *out_value, uint64_t min, uint64_t max)
-{
-	char *endptr;
-
-	errno = 0;
-        *out_value = strtol(str, &endptr, 0);
-
-        /* Check for various possible errors */
-	if (errno != 0 || endptr == str) {
-		return CS_ERR_INVALID_PARAM;
-	}
-
-	if (*out_value > max || *out_value < min) {
-		return CS_ERR_INVALID_PARAM;
-	}
-	return CS_OK;
-}
-
 static void mon_fsm_state_set (struct cs_fsm* fsm,
 	enum mon_resource_state next_state, struct resource_instance* inst)
 {
 	enum mon_resource_state prev_state = fsm->curr_state;
 	const char *state_str;
+	char key_name[ICMAP_KEYNAME_MAXLEN];
 
 	ENTER();
 
@@ -258,40 +243,31 @@ static void mon_fsm_state_set (struct cs_fsm* fsm,
 	}
 	state_str = mon_res_state_to_str(fsm, fsm->curr_state);
 
-	api->object_key_replace (inst->handle,
-		"state", strlen ("state"),
-		state_str, strlen (state_str));
+	snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "state");
+	icmap_set_string(key_name, state_str);
 }
 
 
 static void mon_config_changed (struct cs_fsm* fsm, int32_t event, void * data)
 {
 	struct resource_instance * inst = (struct resource_instance *)data;
-	char *str;
-	size_t str_len;
-	objdb_value_types_t type;
 	uint64_t tmp_value;
-	int32_t res;
-	char str_copy[256];
+	char key_name[ICMAP_KEYNAME_MAXLEN];
+	int run_updater;
 
 	ENTER();
 
-	res = api->object_key_get_typed (inst->handle,
-			"poll_period",
-			(void**)&str, &str_len,
-			&type);
-	if (res == 0) {
-		memcpy(str_copy, str, str_len);
-		str_copy[str_len] = '\0';
-		if (str_to_uint64_t(str_copy, &tmp_value, MON_MIN_PERIOD, MON_MAX_PERIOD) == CS_OK) {
+	snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "poll_period");
+	if (icmap_get_uint64(key_name, &tmp_value) == CS_OK) {
+		if (tmp_value >= MON_MIN_PERIOD && tmp_value <= MON_MAX_PERIOD) {
 			log_printf (LOGSYS_LEVEL_DEBUG,
 				"poll_period changing from:%"PRIu64" to %"PRIu64".",
 				inst->period, tmp_value);
 			inst->period = tmp_value;
 		} else {
 			log_printf (LOGSYS_LEVEL_WARNING,
-				"Could NOT use poll_period:%s ms for resource %s",
-				str, inst->name);
+				"Could NOT use poll_period:%"PRIu64" ms for resource %s",
+				tmp_value, inst->name);
 		}
 	}
 
@@ -299,23 +275,30 @@ static void mon_config_changed (struct cs_fsm* fsm, int32_t event, void * data)
 		api->timer_delete(inst->timer_handle);
 		inst->timer_handle = 0;
 	}
-	res = api->object_key_get_typed (inst->handle, "max",
-			(void**)&str, &str_len, &type);
-	if (res != 0) {
-		if (inst->max_type == OBJDB_VALUETYPE_INT32) {
+
+	run_updater = 0;
+
+	snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "max");
+	if (inst->max_type == ICMAP_VALUETYPE_INT32) {
+		if (icmap_get_int32(key_name, &inst->max.int32) != CS_OK) {
 			inst->max.int32 = INT32_MAX;
-		} else
-		if (inst->max_type == OBJDB_VALUETYPE_DOUBLE) {
-			inst->max.dbl = INT32_MAX;
+
+			mon_fsm_state_set (fsm, MON_S_STOPPED, inst);
+		} else {
+			run_updater = 1;
 		}
-		mon_fsm_state_set (fsm, MON_S_STOPPED, inst);
-	} else {
-		if (inst->max_type == OBJDB_VALUETYPE_INT32) {
-			inst->max.int32 = strtol (str, NULL, 0);
-		} else
-		if (inst->max_type == OBJDB_VALUETYPE_DOUBLE) {
-			inst->max.dbl = strtod (str, NULL);
+	}
+	if (inst->max_type == ICMAP_VALUETYPE_DOUBLE) {
+		if (icmap_get_double(key_name, &inst->max.dbl) != CS_OK) {
+			inst->max.dbl = INT32_MAX;
+
+			mon_fsm_state_set (fsm, MON_S_STOPPED, inst);
+		} else {
+			run_updater = 1;
 		}
+	}
+
+	if (run_updater) {
 		mon_fsm_state_set (fsm, MON_S_RUNNING, inst);
 		/*
 		 * run the updater, incase the period has shortened
@@ -394,18 +377,18 @@ static void mem_update_stats_fn (void *data)
 	struct resource_instance * inst = (struct resource_instance *)data;
 	int32_t new_value;
 	uint64_t timestamp;
+	char key_name[ICMAP_KEYNAME_MAXLEN];
 
 	new_value = percent_mem_used_get();
+	fprintf(stderr,"BLA = %u\n", new_value);
 	if (new_value > 0) {
-		api->object_key_replace (inst->handle,
-			"current", strlen("current"),
-			&new_value, sizeof(new_value));
+		snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "current");
+		icmap_set_uint32(key_name, new_value);
 
 		timestamp = cs_timestamp_get();
 
-		api->object_key_replace (inst->handle,
-			"last_updated", strlen("last_updated"),
-			&timestamp, sizeof(uint64_t));
+		snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "last_updated");
+		icmap_set_uint64(key_name, timestamp);
 
 		if (new_value > inst->max.int32 && inst->fsm.curr_state != MON_S_FAILED) {
 			cs_fsm_process (&inst->fsm, MON_E_FAILURE, inst);
@@ -443,24 +426,18 @@ static void load_update_stats_fn (void *data)
 {
 	struct resource_instance * inst = (struct resource_instance *)data;
 	uint64_t timestamp;
-	int32_t res = 0;
+	char key_name[ICMAP_KEYNAME_MAXLEN];
 	double min15 = min15_loadavg_get();
 
 	if (min15 > 0) {
-		res = api->object_key_replace (inst->handle,
-			"current", strlen("current"),
-			&min15, sizeof (min15));
-		if (res != 0) {
-			log_printf (LOGSYS_LEVEL_ERROR, "replace current failed: %d", res);
-		}
+		snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "current");
+		icmap_set_double(key_name, min15);
+
 		timestamp = cs_timestamp_get();
 
-		res = api->object_key_replace (inst->handle,
-			"last_updated", strlen("last_updated"),
-			&timestamp, sizeof(uint64_t));
-		if (res != 0) {
-			log_printf (LOGSYS_LEVEL_ERROR, "replace last_updated failed: %d", res);
-		}
+		snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "last_updated");
+		icmap_set_uint64(key_name, timestamp);
+
 		if (min15 > inst->max.dbl && inst->fsm.curr_state != MON_S_FAILED) {
 			cs_fsm_process (&inst->fsm, MON_E_FAILURE, &inst);
 		}
@@ -470,104 +447,56 @@ static void load_update_stats_fn (void *data)
 		inst, inst->update_stats_fn, &inst->timer_handle);
 }
 
-static int object_find_or_create (
-	hdb_handle_t parent_object_handle,
-	hdb_handle_t *object_handle,
-	const void *object_name,
-	size_t object_name_len)
-{
-	hdb_handle_t obj_finder;
-	hdb_handle_t obj;
-	int ret = -1;
-
-	api->object_find_create (
-		parent_object_handle,
-		object_name,
-		object_name_len,
-		&obj_finder);
-
-	if (api->object_find_next (obj_finder, &obj) == 0) {
-		/* found it */
-		*object_handle = obj;
-		ret = 0;
-	}
-	else {
-		ret = api->object_create (parent_object_handle,
-			object_handle,
-			object_name, object_name_len);
-	}
-
-	api->object_find_destroy (obj_finder);
-	return ret;
-}
-
-static void mon_object_destroyed(
-	hdb_handle_t parent_object_handle,
-	const void *name_pt, size_t name_len,
-	void *priv_data_pt)
+static void mon_key_changed_cb (
+	int32_t event,
+	const char *key_name,
+	struct icmap_notify_value new_value,
+	struct icmap_notify_value old_value,
+	void *user_data)
 {
-	struct resource_instance* inst = (struct resource_instance*)priv_data_pt;
+	struct resource_instance* inst = (struct resource_instance*)user_data;
+	char *last_key_part;
 
-	if (inst) {
+	if (event == ICMAP_TRACK_DELETE && inst) {
 		log_printf (LOGSYS_LEVEL_WARNING,
-			"resource \"%s\" deleted from objdb!",
+			"resource \"%s\" deleted from cmap!",
 			inst->name);
 
 		cs_fsm_process (&inst->fsm, MON_E_CONFIG_CHANGED, inst);
 	}
-}
-
 
-static void mon_key_change_notify (object_change_type_t change_type,
-	hdb_handle_t parent_object_handle,
-	hdb_handle_t object_handle,
-	const void *object_name_pt, size_t object_name_len,
-	const void *key_name_pt, size_t key_len,
-	const void *key_value_pt, size_t key_value_len,
-	void *priv_data_pt)
-{
-	struct resource_instance* inst = (struct resource_instance*)priv_data_pt;
+	if (event == ICMAP_TRACK_MODIFY) {
+		last_key_part = strrchr(key_name, '.');
+		if (last_key_part == NULL)
+			return ;
 
-	if ((strncmp ((char*)key_name_pt, "max", key_len) == 0) ||
-		(strncmp ((char*)key_name_pt, "poll_period", key_len) == 0)) {
-		ENTER();
-		cs_fsm_process (&inst->fsm, MON_E_CONFIG_CHANGED, inst);
+		last_key_part++;
+		if (strcmp(last_key_part, "max") == 0 ||
+		    strcmp(last_key_part, "poll_period") == 0) {
+			ENTER();
+			cs_fsm_process (&inst->fsm, MON_E_CONFIG_CHANGED, inst);
+		}
 	}
 }
 
-static void mon_instance_init (hdb_handle_t parent, struct resource_instance* inst)
+static void mon_instance_init (struct resource_instance* inst)
 {
-	int32_t res;
-	char mon_period_str[32];
-	char *str;
-	size_t mon_period_len;
-	objdb_value_types_t mon_period_type;
 	uint64_t tmp_value;
-	int32_t zero_32 = 0;
-	time_t zero_64 = 0;
-	double zero_double = 0;
-
-	object_find_or_create (parent,
-		&inst->handle,
-		inst->name, strlen (inst->name));
-
-	if (inst->max_type == OBJDB_VALUETYPE_INT32) {
-		api->object_key_create_typed (inst->handle,
-			"current", &zero_32,
-			sizeof (zero_32), inst->max_type);
+	char key_name[ICMAP_KEYNAME_MAXLEN];
+	icmap_track_t icmap_track;
+
+	snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "current");
+	if (inst->max_type == ICMAP_VALUETYPE_INT32) {
+		icmap_set_int32(key_name, 0);
 	} else {
-		api->object_key_create_typed (inst->handle,
-			"current", &zero_double,
-			sizeof (zero_double), inst->max_type);
+		icmap_set_double(key_name, 0);
 	}
 
-	api->object_key_create_typed (inst->handle,
-		"last_updated", &zero_64,
-		sizeof (uint64_t), OBJDB_VALUETYPE_UINT64);
+	snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "last_updated");
+	icmap_set_uint64(key_name, 0);
 
-	api->object_key_create_typed (inst->handle,
-		"state", mon_stopped_str, strlen (mon_stopped_str),
-		OBJDB_VALUETYPE_STRING);
+	snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "state");
+	icmap_set_string(key_name, mon_stopped_str);
 
 	inst->fsm.name = inst->name;
 	inst->fsm.curr_entry = 0;
@@ -577,40 +506,29 @@ static void mon_instance_init (hdb_handle_t parent, struct resource_instance* in
 	inst->fsm.state_to_str = mon_res_state_to_str;
 	inst->fsm.event_to_str = mon_res_event_to_str;
 
-	res = api->object_key_get_typed (inst->handle,
-			"poll_period",
-			(void**)&str, &mon_period_len,
-			&mon_period_type);
-	if (res != 0) {
-		mon_period_len = snprintf (mon_period_str, 32, "%"PRIu64"",
-			inst->period);
-		api->object_key_create_typed (inst->handle,
-			"poll_period", &mon_period_str,
-			mon_period_len,
-			OBJDB_VALUETYPE_STRING);
+	snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s%s", inst->icmap_path, "poll_period");
+	if (icmap_get_uint64(key_name, &tmp_value) != CS_OK) {
+		icmap_set_uint64(key_name, inst->period);
 	}
 	else {
-		if (str_to_uint64_t(str, &tmp_value, MON_MIN_PERIOD, MON_MAX_PERIOD) == CS_OK) {
+		if (tmp_value >= MON_MIN_PERIOD && tmp_value <= MON_MAX_PERIOD) {
 			inst->period = tmp_value;
 		} else {
 			log_printf (LOGSYS_LEVEL_WARNING,
-				"Could NOT use poll_period:%s ms for resource %s",
-				str, inst->name);
+				"Could NOT use poll_period:%"PRIu64" ms for resource %s",
+				tmp_value, inst->name);
 		}
 	}
 	cs_fsm_process (&inst->fsm, MON_E_CONFIG_CHANGED, inst);
 
-	api->object_track_start (inst->handle, OBJECT_TRACK_DEPTH_RECURSIVE,
-		mon_key_change_notify,
-		NULL, mon_object_destroyed, NULL, inst);
-
+	icmap_track_add(inst->icmap_path,
+			ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY | ICMAP_TRACK_DELETE | ICMAP_TRACK_PREFIX,
+			mon_key_changed_cb, inst, &icmap_track);
 }
 
 static int mon_exec_init_fn (
 	struct corosync_api_v1 *corosync_api)
 {
-	hdb_handle_t obj;
-	hdb_handle_t parent;
 
 #ifdef HAVE_LIBSTATGRAB
 	sg_init();
@@ -621,18 +539,8 @@ static int mon_exec_init_fn (
 #endif
 	api = corosync_api;
 
-	object_find_or_create (OBJECT_PARENT_HANDLE,
-		&resources_obj,
-		"resources", strlen ("resources"));
-
-	object_find_or_create (resources_obj,
-		&obj,
-		"system", strlen ("system"));
-
-	parent = obj;
-
-	mon_instance_init (parent, &memory_used_inst);
-	mon_instance_init (parent, &load_15min_inst);
+	mon_instance_init (&memory_used_inst);
+	mon_instance_init (&load_15min_inst);
 
 	return 0;
 }
-- 
1.7.1



More information about the discuss mailing list