dovecot-2.2: stats: Use the same session ID string for stats tra...

dovecot at dovecot.org dovecot at dovecot.org
Thu Nov 13 00:41:08 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/0694bfec140a
changeset: 18072:0694bfec140a
user:      Timo Sirainen <tss at iki.fi>
date:      Thu Nov 13 02:39:51 2014 +0200
description:
stats: Use the same session ID string for stats tracking as everywhere else.
Generate a new ID only if there is no session ID assigned.

diffstat:

 src/plugins/imap-stats/imap-stats-plugin.c |   2 +-
 src/plugins/stats/stats-connection.c       |   6 +-
 src/plugins/stats/stats-plugin.c           |  10 ++-
 src/plugins/stats/stats-plugin.h           |   3 +-
 src/stats/client-export.c                  |   5 +-
 src/stats/mail-session.c                   |  95 ++++++++++++------------------
 src/stats/mail-stats.h                     |   4 +-
 7 files changed, 56 insertions(+), 69 deletions(-)

diffs (truncated from 362 to 300 lines):

diff -r f348e669546c -r 0694bfec140a src/plugins/imap-stats/imap-stats-plugin.c
--- a/src/plugins/imap-stats/imap-stats-plugin.c	Thu Nov 13 02:37:39 2014 +0200
+++ b/src/plugins/imap-stats/imap-stats-plugin.c	Thu Nov 13 02:39:51 2014 +0200
@@ -73,7 +73,7 @@
 
 	str = t_str_new(128);
 	str_append(str, "UPDATE-CMD\t");
-	str_append(str, guid_128_to_string(suser->session_guid));
+	str_append(str, suser->stats_session_id);
 
 	str_printfa(str, "\t%u\t", scmd->id);
 	if (cmd->state == CLIENT_COMMAND_STATE_DONE)
diff -r f348e669546c -r 0694bfec140a src/plugins/stats/stats-connection.c
--- a/src/plugins/stats/stats-connection.c	Thu Nov 13 02:37:39 2014 +0200
+++ b/src/plugins/stats/stats-connection.c	Thu Nov 13 02:39:51 2014 +0200
@@ -113,7 +113,7 @@
 
 	str_append(str, "CONNECT\t");
 	/* required fields */
-	str_append(str, guid_128_to_string(suser->session_guid));
+	str_append(str, suser->stats_session_id);
 	str_append_c(str, '\t');
 	str_append_tabescaped(str, user->username);
 	str_append_c(str, '\t');
@@ -140,7 +140,7 @@
 	string_t *str = t_str_new(128);
 
 	str_append(str, "DISCONNECT\t");
-	str_append(str, guid_128_to_string(suser->session_guid));
+	str_append(str, suser->stats_session_id);
 	str_append_c(str, '\n');
 	stats_connection_send(conn, str);
 }
@@ -153,7 +153,7 @@
 	string_t *str = t_str_new(128);
 
 	str_append(str, "UPDATE-SESSION\t");
-	str_append(str, guid_128_to_string(suser->session_guid));
+	str_append(str, suser->stats_session_id);
 
 	mail_stats_export(str, stats);
 
diff -r f348e669546c -r 0694bfec140a src/plugins/stats/stats-plugin.c
--- a/src/plugins/stats/stats-plugin.c	Thu Nov 13 02:37:39 2014 +0200
+++ b/src/plugins/stats/stats-plugin.c	Thu Nov 13 02:39:51 2014 +0200
@@ -631,7 +631,15 @@
 		suser->track_commands = TRUE;
 
 	suser->stats_conn = global_stats_conn;
-	guid_128_generate(suser->session_guid);
+	if (user->session_id != NULL && user->session_id[0] != '\0')
+		suser->stats_session_id = user->session_id;
+	else {
+		guid_128_t guid;
+
+		guid_128_generate(guid);
+		suser->stats_session_id =
+			p_strdup(user->pool, guid_128_to_string(guid));
+	}
 	suser->last_session_update = time(NULL);
 
 	suser->ioloop_ctx = ioloop_ctx;
diff -r f348e669546c -r 0694bfec140a src/plugins/stats/stats-plugin.h
--- a/src/plugins/stats/stats-plugin.h	Thu Nov 13 02:37:39 2014 +0200
+++ b/src/plugins/stats/stats-plugin.h	Thu Nov 13 02:39:51 2014 +0200
@@ -2,7 +2,6 @@
 #define STATS_PLUGIN_H
 
 #include "module-context.h"
-#include "guid.h"
 #include "mail-user.h"
 #include "mail-storage-private.h"
 
@@ -33,7 +32,7 @@
 
 	struct ioloop_context *ioloop_ctx;
 	struct stats_connection *stats_conn;
-	guid_128_t session_guid;
+	const char *stats_session_id;
 
 	unsigned int refresh_secs;
 	bool track_commands;
diff -r f348e669546c -r 0694bfec140a src/stats/client-export.c
--- a/src/stats/client-export.c	Thu Nov 13 02:37:39 2014 +0200
+++ b/src/stats/client-export.c	Thu Nov 13 02:39:51 2014 +0200
@@ -276,8 +276,7 @@
 		str_append_tabescaped(cmd->str, command->args);
 		str_append_c(cmd->str, '\t');
 		T_BEGIN {
-			str_append(cmd->str,
-				   guid_128_to_string(command->session->guid));
+			str_append(cmd->str, command->session->id);
 			str_append_c(cmd->str, '\t');
 			str_append_tabescaped(cmd->str,
 					      command->session->user->name);
@@ -321,7 +320,7 @@
 
 		str_truncate(cmd->str, 0);
 		T_BEGIN {
-			str_append(cmd->str, guid_128_to_string(session->guid));
+			str_append(cmd->str, session->id);
 			str_append_c(cmd->str, '\t');
 			str_append_tabescaped(cmd->str, session->user->name);
 			str_append_c(cmd->str, '\t');
diff -r f348e669546c -r 0694bfec140a src/stats/mail-session.c
--- a/src/stats/mail-session.c	Thu Nov 13 02:37:39 2014 +0200
+++ b/src/stats/mail-session.c	Thu Nov 13 02:39:51 2014 +0200
@@ -18,37 +18,35 @@
    stats plugin */
 #define MAIL_SESSION_IDLE_TIMEOUT_MSECS (1000*60*15)
 /* If stats process crashes/restarts, existing processes keep sending status
-   updates to it, but this process doesn't know their GUIDs. If these missing
-   GUIDs are found within this many seconds of starting the stats process,
+   updates to it, but this process doesn't know their session IDs. If these
+   missing IDs are found within this many seconds of starting the stats process,
    don't log a warning about them. (On a larger installation this avoids
    flooding the error log with hundreds of warnings.) */
-#define SESSION_GUID_WARN_HIDE_SECS (60*5)
+#define SESSION_ID_WARN_HIDE_SECS (60*5)
 
-static HASH_TABLE(uint8_t *, struct mail_session *) mail_sessions_hash;
+static HASH_TABLE(char *, struct mail_session *) mail_sessions_hash;
 /* sessions are sorted by their last_update timestamp, oldest first */
 static struct mail_session *mail_sessions_head, *mail_sessions_tail;
-static time_t session_guid_warn_hide_until;
-static bool session_guid_hide_warned = FALSE;
+static time_t session_id_warn_hide_until;
+static bool session_id_hide_warned = FALSE;
 static struct str_table *services;
 
 struct mail_session *stable_mail_sessions;
 
 static size_t mail_session_memsize(const struct mail_session *session)
 {
-	return sizeof(*session) + 1;
+	return sizeof(*session) + strlen(session->id) + 1;
 }
 
 static void mail_session_disconnect(struct mail_session *session)
 {
-	uint8_t *guid_p = session->guid;
-
 	i_assert(!session->disconnected);
 
 	mail_user_disconnected(session->user);
 	if (session->ip != NULL)
 		mail_ip_disconnected(session->ip);
 
-	hash_table_remove(mail_sessions_hash, guid_p);
+	hash_table_remove(mail_sessions_hash, session->id);
 	session->disconnected = TRUE;
 	timeout_remove(&session->to_idle);
 	mail_session_unref(&session);
@@ -64,8 +62,7 @@
 	    session->pid == 0) {
 		i_warning("Session %s (user %s, service %s) "
 			  "appears to have crashed, disconnecting it",
-			  guid_128_to_string(session->guid),
-			  session->user->name, session->service);
+			  session->id, session->user->name, session->service);
 	}
 	mail_session_disconnect(session);
 }
@@ -73,36 +70,31 @@
 int mail_session_connect_parse(const char *const *args, const char **error_r)
 {
 	struct mail_session *session;
-	guid_128_t session_guid;
-	uint8_t *guid_p;
+	const char *session_id;
 	pid_t pid;
 	struct ip_addr ip;
 	unsigned int i;
 
-	/* <session guid> <username> <service> <pid> [key=value ..] */
+	/* <session id> <username> <service> <pid> [key=value ..] */
 	if (str_array_length(args) < 4) {
 		*error_r = "CONNECT: Too few parameters";
 		return -1;
 	}
-	if (guid_128_from_string(args[0], session_guid) < 0) {
-		*error_r = "CONNECT: Invalid GUID";
-		return -1;
-	}
+	session_id = args[0];
 	if (str_to_pid(args[3], &pid) < 0) {
 		*error_r = "CONNECT: Invalid pid";
 		return -1;
 	}
 
-	guid_p = session_guid;
-	session = hash_table_lookup(mail_sessions_hash, guid_p);
+	session = hash_table_lookup(mail_sessions_hash, session_id);
 	if (session != NULL) {
-		*error_r = "CONNECT: Duplicate session GUID";
+		*error_r = "CONNECT: Duplicate session ID";
 		return -1;
 	}
 	session = i_new(struct mail_session, 1);
 	session->refcount = 1; /* unrefed at disconnect */
+	session->id = i_strdup(session_id);
 	session->service = str_table_ref(services, args[2]);
-	memcpy(session->guid, session_guid, sizeof(session->guid));
 	session->pid = pid;
 	session->last_update = ioloop_timeval;
 	session->to_idle = timeout_add(MAIL_SESSION_IDLE_TIMEOUT_MSECS,
@@ -115,8 +107,7 @@
 			session->ip = mail_ip_login(&ip);
 	}
 
-	guid_p = session->guid;
-	hash_table_insert(mail_sessions_hash, guid_p, session);
+	hash_table_insert(mail_sessions_hash, session->id, session);
 	DLLIST_PREPEND_FULL(&stable_mail_sessions, session,
 			    stable_prev, stable_next);
 	DLLIST2_APPEND_FULL(&mail_sessions_head, &mail_sessions_tail, session,
@@ -150,8 +141,6 @@
 
 static void mail_session_free(struct mail_session *session)
 {
-	uint8_t *guid_p = session->guid;
-
 	i_assert(session->refcount == 0);
 
 	global_memory_free(mail_session_memsize(session));
@@ -159,7 +148,7 @@
 	if (session->to_idle != NULL)
 		timeout_remove(&session->to_idle);
 	if (!session->disconnected)
-		hash_table_remove(mail_sessions_hash, guid_p);
+		hash_table_remove(mail_sessions_hash, session->id);
 	DLLIST_REMOVE_FULL(&stable_mail_sessions, session,
 			   stable_prev, stable_next);
 	DLLIST2_REMOVE_FULL(&mail_sessions_head, &mail_sessions_tail, session,
@@ -174,65 +163,57 @@
 	}
 
 	str_table_unref(services, &session->service);
+	i_free(session->id);
 	i_free(session);
 }
 
-static void mail_session_guid_lost(guid_128_t session_guid)
+static void mail_session_id_lost(const char *session_id)
 {
-	if (ioloop_time < session_guid_warn_hide_until) {
-		if (session_guid_hide_warned)
+	if (ioloop_time < session_id_warn_hide_until) {
+		if (session_id_hide_warned)
 			return;
-		session_guid_hide_warned = TRUE;
+		session_id_hide_warned = TRUE;
 		i_warning("stats process appears to have crashed/restarted, "
-			  "hiding missing session GUID warnings for %d seconds",
-			  (int)(session_guid_warn_hide_until - ioloop_time));
+			  "hiding missing session ID warnings for %d seconds",
+			  (int)(session_id_warn_hide_until - ioloop_time));
 		return;
 	}
-	i_warning("Couldn't find session GUID: %s",
-		  guid_128_to_string(session_guid));
+	i_warning("Couldn't find session ID: %s", session_id);
 }
 
-int mail_session_lookup(const char *guid, struct mail_session **session_r,
+int mail_session_lookup(const char *id, struct mail_session **session_r,
 			const char **error_r)
 {
-	guid_128_t session_guid;
-	uint8_t *guid_p;
-
-	if (guid == NULL) {
+	if (id == NULL) {
 		*error_r = "Too few parameters";
 		return -1;
 	}
-	if (guid_128_from_string(guid, session_guid) < 0) {
-		*error_r = "Invalid GUID";
-		return -1;
-	}
-	guid_p = session_guid;
-	*session_r = hash_table_lookup(mail_sessions_hash, guid_p);
+	*session_r = hash_table_lookup(mail_sessions_hash, id);
 	if (*session_r == NULL) {
-		mail_session_guid_lost(session_guid);
+		mail_session_id_lost(id);
 		return 0;
 	}
 	return 1;
 }
 
-int mail_session_get(const char *guid, struct mail_session **session_r,
+int mail_session_get(const char *id, struct mail_session **session_r,
 		     const char **error_r)
 {
 	const char *new_args[5];
 	int ret;
 
-	if ((ret = mail_session_lookup(guid, session_r, error_r)) != 0)
+	if ((ret = mail_session_lookup(id, session_r, error_r)) != 0)


More information about the dovecot-cvs mailing list