1 From 20ddb6f200f61332ff43aca7ad9421303d0a3138 Mon Sep 17 00:00:00 2001
2 From: Simo Sorce <simo@redhat.com>
3 Date: Thu, 25 May 2017 15:22:37 -0400
4 Subject: [PATCH] Add Client ID to debug messages
6 This allows to sort out which debug message belongs to which client when
7 multiple clients are preforming operations at the same time.
9 Signed-off-by: Simo Sorce <simo@redhat.com>
10 Reviewed-by: Robbie Harwood <rharwood@redhat.com>
14 (cherry picked from commit 2f158fe4d39c11589d214d3d602c6d10411052dc)
16 proxy/src/gp_debug.c | 28 +++++++++++++++++++++++++++-
17 proxy/src/gp_debug.h | 1 +
18 proxy/src/gp_proxy.h | 1 +
19 proxy/src/gp_socket.c | 5 +++++
20 proxy/src/gp_workers.c | 6 ++++++
21 proxy/src/gssproxy.c | 4 ++++
22 6 files changed, 44 insertions(+), 1 deletion(-)
24 diff --git a/proxy/src/gp_debug.c b/proxy/src/gp_debug.c
25 index 3029574..4a141fc 100644
26 --- a/proxy/src/gp_debug.c
27 +++ b/proxy/src/gp_debug.c
28 @@ -64,6 +64,32 @@ const char *gp_debug_timestamp(void)
32 +/* thread local connection/client id */
33 +static __thread int cid;
35 +void gp_debug_set_conn_id(int id)
40 +static const char*gp_debug_conn_id(void)
42 + static __thread char buffer[18];
43 + static __thread int last_cid = 0;
50 + if (last_cid == cid) return buffer;
52 + (void)snprintf(buffer, 17, "[CID %d]", cid);
58 void gp_debug_printf(const char *format, ...)
61 @@ -76,7 +102,7 @@ void gp_debug_time_printf(const char *format, ...)
65 - fprintf(stderr, "%s", gp_debug_timestamp());
66 + fprintf(stderr, "%s%s", gp_debug_conn_id(), gp_debug_timestamp());
68 va_start(varargs, format);
69 vfprintf(stderr, format, varargs);
70 diff --git a/proxy/src/gp_debug.h b/proxy/src/gp_debug.h
71 index d3420b0..1c2f8a3 100644
72 --- a/proxy/src/gp_debug.h
73 +++ b/proxy/src/gp_debug.h
74 @@ -14,6 +14,7 @@ int gp_debug_args(int level);
75 void gp_debug_toggle(int);
76 void gp_debug_printf(const char *format, ...);
77 void gp_debug_time_printf(const char *format, ...);
78 +void gp_debug_set_conn_id(int id);
80 #define GPDEBUG(...) do { \
82 diff --git a/proxy/src/gp_proxy.h b/proxy/src/gp_proxy.h
83 index 971a7b6..55ab83c 100644
84 --- a/proxy/src/gp_proxy.h
85 +++ b/proxy/src/gp_proxy.h
86 @@ -113,6 +113,7 @@ void gp_socket_send_data(verto_ctx *vctx, struct gp_conn *conn,
87 struct gp_creds *gp_conn_get_creds(struct gp_conn *conn);
88 uid_t gp_conn_get_uid(struct gp_conn *conn);
89 const char *gp_conn_get_socket(struct gp_conn *conn);
90 +int gp_conn_get_cid(struct gp_conn *conn);
91 bool gp_selinux_ctx_equal(SELINUX_CTX ctx1, SELINUX_CTX ctx2);
92 bool gp_conn_check_selinux(struct gp_conn *conn, SELINUX_CTX ctx);
94 diff --git a/proxy/src/gp_socket.c b/proxy/src/gp_socket.c
95 index 29b6a44..5064e51 100644
96 --- a/proxy/src/gp_socket.c
97 +++ b/proxy/src/gp_socket.c
98 @@ -103,6 +103,11 @@ const char *gp_conn_get_socket(struct gp_conn *conn)
99 return conn->sock_ctx->socket;
102 +int gp_conn_get_cid(struct gp_conn *conn)
104 + return conn->us.sd;
107 void gp_conn_free(struct gp_conn *conn)
110 diff --git a/proxy/src/gp_workers.c b/proxy/src/gp_workers.c
111 index c089b54..d37e57c 100644
112 --- a/proxy/src/gp_workers.c
113 +++ b/proxy/src/gp_workers.c
114 @@ -357,6 +357,9 @@ static void *gp_worker_main(void *pvt)
116 while (!t->pool->shutdown) {
118 + /* initialize debug client id to 0 until work is scheduled */
119 + gp_debug_set_conn_id(0);
121 /* ======> COND_MUTEX */
122 pthread_mutex_lock(&t->cond_mutex);
123 while (t->query == NULL) {
124 @@ -374,6 +377,9 @@ static void *gp_worker_main(void *pvt)
125 /* <====== COND_MUTEX */
126 pthread_mutex_unlock(&t->cond_mutex);
128 + /* set client id before hndling requests */
129 + gp_debug_set_conn_id(gp_conn_get_cid(q->conn));
131 /* handle the client request */
132 gp_handle_query(t->pool, q);
134 diff --git a/proxy/src/gssproxy.c b/proxy/src/gssproxy.c
135 index 5c5937d..94a6a61 100644
136 --- a/proxy/src/gssproxy.c
137 +++ b/proxy/src/gssproxy.c
138 @@ -159,6 +159,10 @@ int main(int argc, const char *argv[])
142 + /* initialize debug client id to 0 in the main thread */
143 + /* we do this early, before any code starts using debug statements */
144 + gp_debug_set_conn_id(0);
146 struct poptOption long_options[] = {
148 {"daemon", 'D', POPT_ARG_NONE, &opt_daemon, 0, \