Blame SOURCES/Add-Client-ID-to-debug-messages.patch

c530df
From 9e9b8597c09605438a6d880b6df5aaa4696f4b21 Mon Sep 17 00:00:00 2001
c530df
From: Simo Sorce <simo@redhat.com>
c530df
Date: Thu, 25 May 2017 15:22:37 -0400
c530df
Subject: [PATCH] Add Client ID to debug messages
c530df
c530df
This allows to sort out which debug message belongs to which client when
c530df
multiple clients are preforming operations at the same time.
c530df
c530df
Signed-off-by: Simo Sorce <simo@redhat.com>
c530df
Reviewed-by: Robbie Harwood <rharwood@redhat.com>
c530df
c530df
Resolves: #189
c530df
Merges: #191
c530df
(cherry picked from commit 2f158fe4d39c11589d214d3d602c6d10411052dc)
c530df
---
c530df
 proxy/src/gp_debug.c   | 28 +++++++++++++++++++++++++++-
c530df
 proxy/src/gp_debug.h   |  1 +
c530df
 proxy/src/gp_proxy.h   |  1 +
c530df
 proxy/src/gp_socket.c  |  5 +++++
c530df
 proxy/src/gp_workers.c |  6 ++++++
c530df
 proxy/src/gssproxy.c   |  4 ++++
c530df
 6 files changed, 44 insertions(+), 1 deletion(-)
c530df
c530df
diff --git a/proxy/src/gp_debug.c b/proxy/src/gp_debug.c
c530df
index 3029574..4a141fc 100644
c530df
--- a/proxy/src/gp_debug.c
c530df
+++ b/proxy/src/gp_debug.c
c530df
@@ -64,6 +64,32 @@ const char *gp_debug_timestamp(void)
c530df
     return buffer;
c530df
 }
c530df
 
c530df
+/* thread local connection/client id */
c530df
+static __thread int cid;
c530df
+
c530df
+void gp_debug_set_conn_id(int id)
c530df
+{
c530df
+    cid = id;
c530df
+}
c530df
+
c530df
+static const char*gp_debug_conn_id(void)
c530df
+{
c530df
+    static __thread char buffer[18];
c530df
+    static __thread int last_cid = 0;
c530df
+
c530df
+    if (cid == 0) {
c530df
+        buffer[0] = '\0';
c530df
+        return buffer;
c530df
+    }
c530df
+
c530df
+    if (last_cid == cid) return buffer;
c530df
+
c530df
+    (void)snprintf(buffer, 17, "[CID %d]", cid);
c530df
+    buffer[17] = '\0';
c530df
+    last_cid = cid;
c530df
+    return buffer;
c530df
+}
c530df
+
c530df
 void gp_debug_printf(const char *format, ...)
c530df
 {
c530df
     va_list varargs;
c530df
@@ -76,7 +102,7 @@ void gp_debug_time_printf(const char *format, ...)
c530df
 {
c530df
     va_list varargs;
c530df
 
c530df
-    fprintf(stderr, "%s", gp_debug_timestamp());
c530df
+    fprintf(stderr, "%s%s", gp_debug_conn_id(), gp_debug_timestamp());
c530df
 
c530df
     va_start(varargs, format);
c530df
     vfprintf(stderr, format, varargs);
c530df
diff --git a/proxy/src/gp_debug.h b/proxy/src/gp_debug.h
c530df
index d3420b0..1c2f8a3 100644
c530df
--- a/proxy/src/gp_debug.h
c530df
+++ b/proxy/src/gp_debug.h
c530df
@@ -14,6 +14,7 @@ int gp_debug_args(int level);
c530df
 void gp_debug_toggle(int);
c530df
 void gp_debug_printf(const char *format, ...);
c530df
 void gp_debug_time_printf(const char *format, ...);
c530df
+void gp_debug_set_conn_id(int id);
c530df
 
c530df
 #define GPDEBUG(...) do { \
c530df
     if (gp_debug) { \
c530df
diff --git a/proxy/src/gp_proxy.h b/proxy/src/gp_proxy.h
c530df
index 971a7b6..55ab83c 100644
c530df
--- a/proxy/src/gp_proxy.h
c530df
+++ b/proxy/src/gp_proxy.h
c530df
@@ -113,6 +113,7 @@ void gp_socket_send_data(verto_ctx *vctx, struct gp_conn *conn,
c530df
 struct gp_creds *gp_conn_get_creds(struct gp_conn *conn);
c530df
 uid_t gp_conn_get_uid(struct gp_conn *conn);
c530df
 const char *gp_conn_get_socket(struct gp_conn *conn);
c530df
+int gp_conn_get_cid(struct gp_conn *conn);
c530df
 bool gp_selinux_ctx_equal(SELINUX_CTX ctx1, SELINUX_CTX ctx2);
c530df
 bool gp_conn_check_selinux(struct gp_conn *conn, SELINUX_CTX ctx);
c530df
 
c530df
diff --git a/proxy/src/gp_socket.c b/proxy/src/gp_socket.c
c530df
index 29b6a44..5064e51 100644
c530df
--- a/proxy/src/gp_socket.c
c530df
+++ b/proxy/src/gp_socket.c
c530df
@@ -103,6 +103,11 @@ const char *gp_conn_get_socket(struct gp_conn *conn)
c530df
     return conn->sock_ctx->socket;
c530df
 }
c530df
 
c530df
+int gp_conn_get_cid(struct gp_conn *conn)
c530df
+{
c530df
+    return conn->us.sd;
c530df
+}
c530df
+
c530df
 void gp_conn_free(struct gp_conn *conn)
c530df
 {
c530df
     if (!conn) return;
c530df
diff --git a/proxy/src/gp_workers.c b/proxy/src/gp_workers.c
c530df
index c089b54..d37e57c 100644
c530df
--- a/proxy/src/gp_workers.c
c530df
+++ b/proxy/src/gp_workers.c
c530df
@@ -357,6 +357,9 @@ static void *gp_worker_main(void *pvt)
c530df
 
c530df
     while (!t->pool->shutdown) {
c530df
 
c530df
+        /* initialize debug client id to 0 until work is scheduled */
c530df
+        gp_debug_set_conn_id(0);
c530df
+
c530df
         /* ======> COND_MUTEX */
c530df
         pthread_mutex_lock(&t->cond_mutex);
c530df
         while (t->query == NULL) {
c530df
@@ -374,6 +377,9 @@ static void *gp_worker_main(void *pvt)
c530df
         /* <====== COND_MUTEX */
c530df
         pthread_mutex_unlock(&t->cond_mutex);
c530df
 
c530df
+        /* set client id before hndling requests */
c530df
+        gp_debug_set_conn_id(gp_conn_get_cid(q->conn));
c530df
+
c530df
         /* handle the client request */
c530df
         gp_handle_query(t->pool, q);
c530df
 
c530df
diff --git a/proxy/src/gssproxy.c b/proxy/src/gssproxy.c
c530df
index 5c5937d..94a6a61 100644
c530df
--- a/proxy/src/gssproxy.c
c530df
+++ b/proxy/src/gssproxy.c
c530df
@@ -159,6 +159,10 @@ int main(int argc, const char *argv[])
c530df
     int wait_fd;
c530df
     int ret = -1;
c530df
 
c530df
+    /* initialize debug client id to 0 in the main thread */
c530df
+    /* we do this early, before any code starts using debug statements */
c530df
+    gp_debug_set_conn_id(0);
c530df
+
c530df
     struct poptOption long_options[] = {
c530df
         POPT_AUTOHELP
c530df
         {"daemon", 'D', POPT_ARG_NONE, &opt_daemon, 0, \