You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by yi...@apache.org on 2022/05/26 07:09:25 UTC

[incubator-doris] branch master updated: [improvement] Show detail status code string for be http api (#9771)

This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 9236c2efc9 [improvement] Show detail status code string for be http api (#9771)
9236c2efc9 is described below

commit 9236c2efc972d0a6838f624c70cc7f36424e70cc
Author: jacktengg <18...@users.noreply.github.com>
AuthorDate: Thu May 26 15:09:21 2022 +0800

    [improvement] Show detail status code string for be http api (#9771)
    
    1. move to_json method to common/status
    2. modify related usage in http folder
---
 be/src/common/status.cpp                           | 28 +++++++++
 be/src/common/status.h                             |  3 +
 be/src/http/action/compaction_action.cpp           |  6 +-
 be/src/http/action/meta_action.cpp                 |  2 +-
 be/src/http/action/mini_load.cpp                   |  2 +-
 be/src/http/action/restore_tablet_action.cpp       |  2 +-
 be/src/http/action/stream_load_2pc.cpp             |  8 +--
 be/src/http/action/tablet_migration_action.cpp     |  6 +-
 be/src/http/action/tablets_distribution_action.cpp |  4 +-
 be/src/util/CMakeLists.txt                         |  1 -
 be/src/util/json_util.cpp                          | 51 ----------------
 be/src/util/json_util.h                            |  2 -
 be/test/CMakeLists.txt                             |  1 -
 be/test/util/json_util_test.cpp                    | 70 ----------------------
 14 files changed, 46 insertions(+), 140 deletions(-)

diff --git a/be/src/common/status.cpp b/be/src/common/status.cpp
index a9ebd84da1..4f5c992a8f 100644
--- a/be/src/common/status.cpp
+++ b/be/src/common/status.cpp
@@ -5,6 +5,8 @@
 #include "common/status.h"
 
 #include <glog/logging.h>
+#include <rapidjson/prettywriter.h>
+#include <rapidjson/stringbuffer.h>
 
 #include <boost/stacktrace.hpp>
 
@@ -220,4 +222,30 @@ Status Status::clone_and_append(const Slice& msg) const {
     return Status(code(), message(), precise_code(), msg);
 }
 
+std::string Status::to_json() const {
+    rapidjson::StringBuffer s;
+    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(s);
+
+    writer.StartObject();
+    // status
+    writer.Key("status");
+    writer.String(code_as_string().c_str());
+    // msg
+    writer.Key("msg");
+    if (ok()) {
+        writer.String("OK");
+    } else {
+        auto err_msg = get_error_msg();
+        int16_t posix = precise_code();
+        if (posix != 1) {
+            char buf[64];
+            snprintf(buf, sizeof(buf), " (error %d)", posix);
+            err_msg.append(buf);
+        }
+        writer.String(err_msg.c_str());
+    }
+    writer.EndObject();
+    return s.GetString();
+}
+
 } // namespace doris
\ No newline at end of file
diff --git a/be/src/common/status.h b/be/src/common/status.h
index c58df454ef..cca2383670 100644
--- a/be/src/common/status.h
+++ b/be/src/common/status.h
@@ -439,6 +439,9 @@ public:
     ///   Returns the string "OK" for success.
     std::string to_string() const;
 
+    /// @return A json representation of this status.
+    std::string to_json() const;
+
     /// @return A string representation of the status code, without the message
     ///   text or sub code information.
     std::string code_as_string() const;
diff --git a/be/src/http/action/compaction_action.cpp b/be/src/http/action/compaction_action.cpp
index fe86f530f9..87fbae2d8b 100644
--- a/be/src/http/action/compaction_action.cpp
+++ b/be/src/http/action/compaction_action.cpp
@@ -249,7 +249,7 @@ void CompactionAction::handle(HttpRequest* req) {
         std::string json_result;
         Status st = _handle_show_compaction(req, &json_result);
         if (!st.ok()) {
-            HttpChannel::send_reply(req, HttpStatus::OK, to_json(st));
+            HttpChannel::send_reply(req, HttpStatus::OK, st.to_json());
         } else {
             HttpChannel::send_reply(req, HttpStatus::OK, json_result);
         }
@@ -257,7 +257,7 @@ void CompactionAction::handle(HttpRequest* req) {
         std::string json_result;
         Status st = _handle_run_compaction(req, &json_result);
         if (!st.ok()) {
-            HttpChannel::send_reply(req, HttpStatus::OK, to_json(st));
+            HttpChannel::send_reply(req, HttpStatus::OK, st.to_json());
         } else {
             HttpChannel::send_reply(req, HttpStatus::OK, json_result);
         }
@@ -265,7 +265,7 @@ void CompactionAction::handle(HttpRequest* req) {
         std::string json_result;
         Status st = _handle_run_status_compaction(req, &json_result);
         if (!st.ok()) {
-            HttpChannel::send_reply(req, HttpStatus::OK, to_json(st));
+            HttpChannel::send_reply(req, HttpStatus::OK, st.to_json());
         } else {
             HttpChannel::send_reply(req, HttpStatus::OK, json_result);
         }
diff --git a/be/src/http/action/meta_action.cpp b/be/src/http/action/meta_action.cpp
index e31ef14aa9..4855ef7cf6 100644
--- a/be/src/http/action/meta_action.cpp
+++ b/be/src/http/action/meta_action.cpp
@@ -73,7 +73,7 @@ void MetaAction::handle(HttpRequest* req) {
     if (_meta_type == META_TYPE::HEADER) {
         std::string json_meta;
         Status status = _handle_header(req, &json_meta);
-        std::string status_result = to_json(status);
+        std::string status_result = status.to_json();
         LOG(INFO) << "handle request result:" << status_result;
         if (status.ok()) {
             HttpChannel::send_reply(req, HttpStatus::OK, json_meta);
diff --git a/be/src/http/action/mini_load.cpp b/be/src/http/action/mini_load.cpp
index f7ef79ad07..b841b21add 100644
--- a/be/src/http/action/mini_load.cpp
+++ b/be/src/http/action/mini_load.cpp
@@ -667,7 +667,7 @@ void MiniLoadAction::_handle(HttpRequest* http_req) {
     }
     auto st = _load(http_req, ctx->file_path, ctx->load_check_req.user, ctx->load_check_req.cluster,
                     ctx->bytes_written);
-    std::string str = to_json(st);
+    std::string str = st.to_json();
     HttpChannel::send_reply(http_req, str);
 }
 
diff --git a/be/src/http/action/restore_tablet_action.cpp b/be/src/http/action/restore_tablet_action.cpp
index 94109f704d..2ec5ad05e7 100644
--- a/be/src/http/action/restore_tablet_action.cpp
+++ b/be/src/http/action/restore_tablet_action.cpp
@@ -53,7 +53,7 @@ void RestoreTabletAction::handle(HttpRequest* req) {
     // add tid to cgroup in order to limit read bandwidth
     CgroupsMgr::apply_system_cgroup();
     Status status = _handle(req);
-    std::string result = to_json(status);
+    std::string result = status.to_json();
     LOG(INFO) << "handle request result:" << result;
     if (status.ok()) {
         HttpChannel::send_reply(req, HttpStatus::OK, result);
diff --git a/be/src/http/action/stream_load_2pc.cpp b/be/src/http/action/stream_load_2pc.cpp
index cd71bb5e95..7aa5655e52 100644
--- a/be/src/http/action/stream_load_2pc.cpp
+++ b/be/src/http/action/stream_load_2pc.cpp
@@ -42,7 +42,7 @@ void StreamLoad2PCAction::handle(HttpRequest* req) {
 
     if (config::disable_stream_load_2pc) {
         status = Status::InternalError("Two phase commit (2PC) for stream load was disabled");
-        status_result = to_json(status);
+        status_result = status.to_json();
         HttpChannel::send_reply(req, HttpStatus::OK, status_result);
         return;
     }
@@ -56,14 +56,14 @@ void StreamLoad2PCAction::handle(HttpRequest* req) {
         ctx->txn_id = std::stoull(req_txn_id);
     } catch (const std::exception& e) {
         status = Status::InternalError("convert txn_id [" + req_txn_id + "] failed");
-        status_result = to_json(status);
+        status_result = status.to_json();
         HttpChannel::send_reply(req, HttpStatus::OK, status_result);
         return;
     }
     ctx->txn_operation = req->header(HTTP_TXN_OPERATION_KEY);
     if (ctx->txn_operation.compare("commit") != 0 && ctx->txn_operation.compare("abort") != 0) {
         status = Status::InternalError("transaction operation should be \'commit\' or \'abort\'");
-        status_result = to_json(status);
+        status_result = status.to_json();
         HttpChannel::send_reply(req, HttpStatus::OK, status_result);
         return;
     }
@@ -76,7 +76,7 @@ void StreamLoad2PCAction::handle(HttpRequest* req) {
     status = _exec_env->stream_load_executor()->operate_txn_2pc(ctx);
 
     if (!status.ok()) {
-        status_result = to_json(status);
+        status_result = status.to_json();
     } else {
         status_result = get_success_info(req_txn_id, ctx->txn_operation);
     }
diff --git a/be/src/http/action/tablet_migration_action.cpp b/be/src/http/action/tablet_migration_action.cpp
index dd5203838f..5f7a9e218e 100644
--- a/be/src/http/action/tablet_migration_action.cpp
+++ b/be/src/http/action/tablet_migration_action.cpp
@@ -109,7 +109,7 @@ void TabletMigrationAction::handle(HttpRequest* req) {
             }
             std::string status_result;
             if (!status.ok()) {
-                status_result = to_json(status);
+                status_result = status.to_json();
             } else {
                 status_result =
                         "{\"status\": \"Success\", \"msg\": \"migration task is successfully "
@@ -152,13 +152,13 @@ void TabletMigrationAction::handle(HttpRequest* req) {
                 }
             } while (0);
             if (!status.ok()) {
-                status_result = to_json(status);
+                status_result = status.to_json();
             }
             req->add_output_header(HttpHeaders::CONTENT_TYPE, HEADER_JSON.c_str());
             HttpChannel::send_reply(req, HttpStatus::OK, status_result);
         }
     } else {
-        std::string status_result = to_json(status);
+        std::string status_result = status.to_json();
         req->add_output_header(HttpHeaders::CONTENT_TYPE, HEADER_JSON.c_str());
         HttpChannel::send_reply(req, HttpStatus::OK, status_result);
     }
diff --git a/be/src/http/action/tablets_distribution_action.cpp b/be/src/http/action/tablets_distribution_action.cpp
index 8eb672a5e0..30254665ea 100644
--- a/be/src/http/action/tablets_distribution_action.cpp
+++ b/be/src/http/action/tablets_distribution_action.cpp
@@ -52,7 +52,7 @@ void TabletsDistributionAction::handle(HttpRequest* req) {
                 LOG(WARNING) << "invalid argument. partition_id:" << req_partition_id;
                 Status status = Status::InternalError(
                         strings::Substitute("invalid argument: partition_id"));
-                std::string status_result = to_json(status);
+                std::string status_result = status.to_json();
                 HttpChannel::send_reply(req, HttpStatus::INTERNAL_SERVER_ERROR, status_result);
                 return;
             }
@@ -64,7 +64,7 @@ void TabletsDistributionAction::handle(HttpRequest* req) {
     }
     LOG(WARNING) << "invalid argument. group_by:" << req_group_method;
     Status status = Status::InternalError(strings::Substitute("invalid argument: group_by"));
-    std::string status_result = to_json(status);
+    std::string status_result = status.to_json();
     HttpChannel::send_reply(req, HttpStatus::INTERNAL_SERVER_ERROR, status_result);
 }
 
diff --git a/be/src/util/CMakeLists.txt b/be/src/util/CMakeLists.txt
index 98abb3ba6d..2bb19b81dc 100644
--- a/be/src/util/CMakeLists.txt
+++ b/be/src/util/CMakeLists.txt
@@ -39,7 +39,6 @@ set(UTIL_FILES
   errno.cpp
   hash_util.hpp
   histogram.cpp
-  json_util.cpp
   doris_metrics.cpp
   mem_info.cpp
   metrics.cpp
diff --git a/be/src/util/json_util.cpp b/be/src/util/json_util.cpp
deleted file mode 100644
index 5d8cf1acd7..0000000000
--- a/be/src/util/json_util.cpp
+++ /dev/null
@@ -1,51 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License.
-// This file is copied from
-// https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/json-util.cc
-// and modified by Doris
-
-#include "util/json_util.h"
-
-#include <rapidjson/prettywriter.h>
-#include <rapidjson/stringbuffer.h>
-
-namespace doris {
-
-std::string to_json(const Status& status) {
-    rapidjson::StringBuffer s;
-    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(s);
-
-    writer.StartObject();
-    // status
-    writer.Key("status");
-    if (status.ok()) {
-        writer.String("Success");
-    } else {
-        writer.String("Fail");
-    }
-    // msg
-    writer.Key("msg");
-    if (status.ok()) {
-        writer.String("OK");
-    } else {
-        writer.String(status.get_error_msg().c_str());
-    }
-    writer.EndObject();
-    return s.GetString();
-}
-
-} // namespace doris
diff --git a/be/src/util/json_util.h b/be/src/util/json_util.h
index dd9233d043..a2aba38c59 100644
--- a/be/src/util/json_util.h
+++ b/be/src/util/json_util.h
@@ -25,7 +25,6 @@
 
 #include <string>
 
-#include "common/status.h"
 #include "util/pretty_printer.h"
 
 namespace doris {
@@ -62,5 +61,4 @@ typename boost::enable_if_c<boost::is_arithmetic<T>::value, void>::type ToJsonVa
     }
 }
 
-std::string to_json(const Status& status);
 } // namespace doris
diff --git a/be/test/CMakeLists.txt b/be/test/CMakeLists.txt
index 38700a4bc2..810fbd5279 100644
--- a/be/test/CMakeLists.txt
+++ b/be/test/CMakeLists.txt
@@ -281,7 +281,6 @@ set(UTIL_TEST_FILES
     util/string_util_test.cpp
     util/string_parser_test.cpp
     util/core_local_test.cpp
-    util/json_util_test.cpp
     util/byte_buffer2_test.cpp
     util/uid_util_test.cpp
     util/encryption_util_test.cpp
diff --git a/be/test/util/json_util_test.cpp b/be/test/util/json_util_test.cpp
deleted file mode 100644
index 94b370377d..0000000000
--- a/be/test/util/json_util_test.cpp
+++ /dev/null
@@ -1,70 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License.
-
-#include "util/json_util.h"
-
-#include <gtest/gtest.h>
-
-#include "common/logging.h"
-
-namespace doris {
-
-class JsonUtilTest : public testing::Test {
-public:
-    JsonUtilTest() {}
-    virtual ~JsonUtilTest() {}
-};
-
-TEST_F(JsonUtilTest, success) {
-    Status status;
-
-    auto str = to_json(status);
-
-    const char* result =
-            "{\n"
-            "    \"status\": \"Success\",\n"
-            "    \"msg\": \"OK\"\n}";
-    EXPECT_STREQ(result, str.c_str());
-}
-
-TEST_F(JsonUtilTest, normal_fail) {
-    Status status = Status::InternalError("so bad");
-
-    auto str = to_json(status);
-
-    const char* result =
-            "{\n"
-            "    \"status\": \"Fail\",\n"
-            "    \"msg\": \"so bad\"\n}";
-    EXPECT_STREQ(result, str.c_str());
-}
-
-TEST_F(JsonUtilTest, normal_fail_str) {
-    Status status = Status::InternalError("\"so bad\"");
-
-    auto str = to_json(status);
-
-    // "msg": "\"so bad\""
-    const char* result =
-            "{\n"
-            "    \"status\": \"Fail\",\n"
-            "    \"msg\": \"\\\"so bad\\\"\"\n}";
-    LOG(INFO) << "str: " << str;
-    EXPECT_STREQ(result, str.c_str());
-}
-
-} // namespace doris


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org