From: Timo Sirainen Date: Thu, 13 Nov 2014 00:39:51 +0000 (+0200) Subject: stats: Use the same session ID string for stats tracking as everywhere else. X-Git-Tag: 2.2.16.rc1~234 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cf07b671695d344a36d9ee681193fda8a3516432;p=thirdparty%2Fdovecot%2Fcore.git 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. --- diff --git a/src/plugins/imap-stats/imap-stats-plugin.c b/src/plugins/imap-stats/imap-stats-plugin.c index 8dc26b54d8..8605814b44 100644 --- a/src/plugins/imap-stats/imap-stats-plugin.c +++ b/src/plugins/imap-stats/imap-stats-plugin.c @@ -73,7 +73,7 @@ static void stats_command_post(struct client_command_context *cmd) 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 --git a/src/plugins/stats/stats-connection.c b/src/plugins/stats/stats-connection.c index 610aeb4b0e..8c5773392d 100644 --- a/src/plugins/stats/stats-connection.c +++ b/src/plugins/stats/stats-connection.c @@ -113,7 +113,7 @@ void stats_connection_connect(struct stats_connection *conn, 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 @@ void stats_connection_disconnect(struct stats_connection *conn, 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 @@ void stats_connection_send_session(struct stats_connection *conn, 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 --git a/src/plugins/stats/stats-plugin.c b/src/plugins/stats/stats-plugin.c index df8b447841..b534a023fd 100644 --- a/src/plugins/stats/stats-plugin.c +++ b/src/plugins/stats/stats-plugin.c @@ -631,7 +631,15 @@ static void stats_user_created(struct mail_user *user) 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 --git a/src/plugins/stats/stats-plugin.h b/src/plugins/stats/stats-plugin.h index 291ee2c79c..1f516cdae3 100644 --- a/src/plugins/stats/stats-plugin.h +++ b/src/plugins/stats/stats-plugin.h @@ -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 stats_user { 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 --git a/src/stats/client-export.c b/src/stats/client-export.c index 483e8e9dec..f41a89e295 100644 --- a/src/stats/client-export.c +++ b/src/stats/client-export.c @@ -276,8 +276,7 @@ static int client_export_iter_command(struct client *client) 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 @@ static int client_export_iter_session(struct client *client) 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 --git a/src/stats/mail-session.c b/src/stats/mail-session.c index 9903396ff5..76389f46f6 100644 --- a/src/stats/mail-session.c +++ b/src/stats/mail-session.c @@ -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 @@ static void mail_session_idle_timeout(struct mail_session *session) 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 @@ static void mail_session_idle_timeout(struct mail_session *session) 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; - /* [key=value ..] */ + /* [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 @@ int mail_session_connect_parse(const char *const *args, const char **error_r) 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 @@ void mail_session_unref(struct mail_session **_session) 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 @@ static void mail_session_free(struct mail_session *session) 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 @@ static void mail_session_free(struct mail_session *session) } 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) return ret; /* Create a new dummy session to avoid repeated warnings */ - new_args[0] = guid; + new_args[0] = id; new_args[1] = ""; /* username */ new_args[2] = ""; /* service */ new_args[3] = "0"; /* pid */ new_args[4] = NULL; if (mail_session_connect_parse(new_args, error_r) < 0) i_unreached(); - if (mail_session_lookup(guid, session_r, error_r) != 1) + if (mail_session_lookup(id, session_r, error_r) != 1) i_unreached(); return 0; } @@ -242,7 +223,7 @@ int mail_session_disconnect_parse(const char *const *args, const char **error_r) struct mail_session *session; int ret; - /* */ + /* */ if ((ret = mail_session_lookup(args[0], &session, error_r)) <= 0) return ret; @@ -275,7 +256,7 @@ int mail_session_update_parse(const char *const *args, const char **error_r) struct mail_stats stats, diff_stats; const char *error; - /* [key=value ..] */ + /* [key=value ..] */ if (mail_session_get(args[0], &session, error_r) < 0) return -1; @@ -317,10 +298,10 @@ void mail_sessions_free_memory(void) void mail_sessions_init(void) { - session_guid_warn_hide_until = - ioloop_time + SESSION_GUID_WARN_HIDE_SECS; + session_id_warn_hide_until = + ioloop_time + SESSION_ID_WARN_HIDE_SECS; hash_table_create(&mail_sessions_hash, default_pool, 0, - guid_128_hash, guid_128_cmp); + str_hash, strcmp); services = str_table_init(); } diff --git a/src/stats/mail-stats.h b/src/stats/mail-stats.h index 448d4b098b..48278f5463 100644 --- a/src/stats/mail-stats.h +++ b/src/stats/mail-stats.h @@ -39,8 +39,8 @@ struct mail_session { struct mail_session *user_prev, *user_next; struct mail_session *ip_prev, *ip_next; - /* if guid is empty, the session no longer exists */ - guid_128_t guid; + /* if id="", the session no longer exists */ + char *id; struct mail_user *user; const char *service; pid_t pid;