From b0022ff6d820d17428aa04e6be148e84dade0855 Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Fri, 28 Oct 2022 16:32:03 +0800
Subject: [PATCH 35/43] [improve] debug information for console io

Signed-off-by: haozi007 <liuhao27@huawei.com>
---
 .../entry/cri/websocket/service/ws_server.cc  | 22 +++++++++++++------
 src/daemon/modules/events/collector.c         | 12 +++++-----
 src/utils/console/console.c                   |  8 ++++++-
 src/utils/cutils/io_wrapper.h                 | 11 ++++++++++
 4 files changed, 39 insertions(+), 14 deletions(-)

diff --git a/src/daemon/entry/cri/websocket/service/ws_server.cc b/src/daemon/entry/cri/websocket/service/ws_server.cc
index a9b58685..41bb3fe8 100644
--- a/src/daemon/entry/cri/websocket/service/ws_server.cc
+++ b/src/daemon/entry/cri/websocket/service/ws_server.cc
@@ -79,17 +79,25 @@ int SessionData::PushMessage(unsigned char *message)
 
     sessionMutex->lock();
 
-    // In extreme scenarios, websocket data cannot be processed,
-    // ignore the data coming in later to prevent iSulad from getting stuck
-    if (close || buffer.size() >= FIFO_LIST_BUFFER_MAX_LEN) {
-        free(message);
+    if (!close && buffer.size() < FIFO_LIST_BUFFER_MAX_LEN) {
+        buffer.push_back(message);
         sessionMutex->unlock();
-        return -1;
+        return 0;
     }
 
-    buffer.push_back(message);
+    // In extreme scenarios, websocket data cannot be processed,
+    // ignore the data coming in later to prevent iSulad from getting stuck
+    free(message);
     sessionMutex->unlock();
-    return 0;
+
+    if (close) {
+        DEBUG("Closed session");
+    }
+    if (buffer.size() >= FIFO_LIST_BUFFER_MAX_LEN) {
+        ERROR("Too large: %zu message!", buffer.size());
+    }
+
+    return -1;
 }
 
 bool SessionData::IsClosed()
diff --git a/src/daemon/modules/events/collector.c b/src/daemon/modules/events/collector.c
index 9dc4dbe7..433cb88b 100644
--- a/src/daemon/modules/events/collector.c
+++ b/src/daemon/modules/events/collector.c
@@ -339,28 +339,28 @@ static void supplement_annotations_for_container_msg(const container_t *cont, co
                                                      struct isulad_events_format *format_msg)
 {
     if (supplement_pid_for_container_msg(cont, msg, format_msg) != 0) {
-        ERROR("Failed to supplement pid info");
+        WARN("Failed to supplement pid info");
     }
 
     if (supplement_exitcode_for_container_msg(cont, msg, format_msg) != 0) {
-        ERROR("Failed to supplement exitCode info");
+        WARN("Failed to supplement exitCode info");
     }
 
     if (supplement_image_for_container_msg(cont, msg, format_msg) != 0) {
-        ERROR("Failed to supplement image info");
+        WARN("Failed to supplement image info");
     }
 
     if (supplement_name_for_container_msg(cont, msg, format_msg) != 0) {
-        ERROR("Failed to supplement name info");
+        WARN("Failed to supplement name info");
     }
 
     if (supplement_labels_for_container_msg(cont, msg, format_msg) != 0) {
-        ERROR("Failed to supplement label info");
+        WARN("Failed to supplement label info");
     }
 
     if (strlen(msg->extra_annations) != 0) {
         if (util_array_append(&format_msg->annotations, msg->extra_annations) != 0) {
-            ERROR("Failed to supplement extra annations info");
+            WARN("Failed to supplement extra annations info");
         }
     }
 
diff --git a/src/utils/console/console.c b/src/utils/console/console.c
index 3565eef3..d5e5d9af 100644
--- a/src/utils/console/console.c
+++ b/src/utils/console/console.c
@@ -100,7 +100,7 @@ static int console_writer_write_data(const struct io_write_wrapper *writer, cons
     }
     ret = writer->write_func(writer->context, buf, (size_t)len);
     if (ret <= 0 || ret != len) {
-        ERROR("failed to write, error:%s", strerror(errno));
+        ERROR("Failed to write, type: %d, expect: %zd, wrote: %zd, error: %s!", writer->io_type, len, ret, strerror(errno));
         return -1;
     }
     return 0;
@@ -401,6 +401,7 @@ int console_loop_with_std_fd(int stdinfd, int stdoutfd, int stderrfd, int fifoin
     if (fifoinfd >= 0) {
         ts.stdin_reader = stdinfd;
         ts.stdin_writer.context = &fifoinfd;
+        ts.stdin_writer.io_type = FIFO_IN_IO;
         ts.stdin_writer.write_func = fd_write_function;
         if (tty) {
             ret = epoll_loop_add_handler(&descr, ts.stdin_reader, console_cb_tty_stdin_with_escape, &ts);
@@ -418,6 +419,7 @@ int console_loop_with_std_fd(int stdinfd, int stdoutfd, int stderrfd, int fifoin
     if (fifooutfd >= 0) {
         ts.stdout_reader = fifooutfd;
         ts.stdout_writer.context = &stdoutfd;
+        ts.stdin_writer.io_type = FIFO_OUT_IO;
         ts.stdout_writer.write_func = fd_write_function;
         ret = epoll_loop_add_handler(&descr, ts.stdout_reader, console_cb_stdio_copy, &ts);
         if (ret) {
@@ -429,6 +431,7 @@ int console_loop_with_std_fd(int stdinfd, int stdoutfd, int stderrfd, int fifoin
     if (fifoerrfd >= 0) {
         ts.stderr_reader = fifoerrfd;
         ts.stderr_writer.context = &stderrfd;
+        ts.stdin_writer.io_type = FIFO_ERR_IO;
         ts.stderr_writer.write_func = fd_write_function;
         ret = epoll_loop_add_handler(&descr, ts.stderr_reader, console_cb_stdio_copy, &ts);
         if (ret) {
@@ -477,18 +480,21 @@ int console_loop_io_copy(int sync_fd, const int *srcfds, struct io_write_wrapper
             ts[i].stdin_reader = srcfds[i];
             ts[i].stdin_writer.context = writers[i].context;
             ts[i].stdin_writer.write_func = writers[i].write_func;
+            ts[i].stdin_writer.io_type = FUNC_IN_IO;
             ret = epoll_loop_add_handler(&descr, ts[i].stdin_reader, console_cb_stdio_copy, &ts[i]);
         } else if (channels[i] == STDOUT_CHANNEL) {
             // Reusing ts.stdout_reader and ts.stdout_writer for coping io
             ts[i].stdout_reader = srcfds[i];
             ts[i].stdout_writer.context = writers[i].context;
             ts[i].stdout_writer.write_func = writers[i].write_func;
+            ts[i].stdin_writer.io_type = FUNC_OUT_IO;
             ret = epoll_loop_add_handler(&descr, ts[i].stdout_reader, console_cb_stdio_copy, &ts[i]);
         } else {
             // Reusing ts.stderr_reader and ts.stderr_writer for coping io
             ts[i].stderr_reader = srcfds[i];
             ts[i].stderr_writer.context = writers[i].context;
             ts[i].stderr_writer.write_func = writers[i].write_func;
+            ts[i].stdin_writer.io_type = FUNC_ERR_IO;
             ret = epoll_loop_add_handler(&descr, ts[i].stderr_reader, console_cb_stdio_copy, &ts[i]);
         }
         if (ret != 0) {
diff --git a/src/utils/cutils/io_wrapper.h b/src/utils/cutils/io_wrapper.h
index 0134a822..a9340d01 100644
--- a/src/utils/cutils/io_wrapper.h
+++ b/src/utils/cutils/io_wrapper.h
@@ -21,6 +21,15 @@
 extern "C" {
 #endif
 
+enum {
+    FIFO_IN_IO = 0,
+    FIFO_OUT_IO,
+    FIFO_ERR_IO,
+    FUNC_IN_IO,
+    FUNC_OUT_IO,
+    FUNC_ERR_IO,
+};
+
 typedef ssize_t (*io_write_func_t)(void *context, const void *data, size_t len);
 typedef int (*io_close_func_t)(void *context, char **err);
 
@@ -28,6 +37,7 @@ struct io_write_wrapper {
     void *context;
     io_write_func_t write_func;
     io_close_func_t close_func;
+    int io_type;
 };
 
 typedef ssize_t (*io_read_func_t)(void *context, void *buf, size_t len);
@@ -36,6 +46,7 @@ struct io_read_wrapper {
     void *context;
     io_read_func_t read;
     io_close_func_t close;
+    int io_type;
 };
 
 #ifdef __cplusplus
-- 
2.25.1

