morningman commented on a change in pull request #4312:
URL: https://github.com/apache/incubator-doris/pull/4312#discussion_r468680589



##########
File path: be/src/http/action/compaction_action.cpp
##########
@@ -67,6 +82,167 @@ Status 
CompactionAction::_handle_show_compaction(HttpRequest* req, std::string*
     return Status::OK();
 }
 
+Status CompactionAction::_handle_run_compaction(HttpRequest *req, std::string* 
json_result) {
+
+    // 1. param check
+    uint64_t tablet_id = 0;
+    uint32_t schema_hash = 0;
+    
+    // check req_tablet_id and req_schema_hash is not empty
+    Status check_status = _check_param(req, &tablet_id, &schema_hash);
+    RETURN_IF_ERROR(check_status);
+
+    std::string compaction_type = req->param(PARAM_COMPACTION_TYPE);
+    // check compaction_type is not empty and equals base or cumulative
+    if (compaction_type == "" && !(compaction_type == PARAM_COMPACTION_BASE || 
compaction_type == PARAM_COMPACTION_CUMULATIVE)) {
+        return Status::NotSupported("The compaction type is not supported");
+    }
+
+    // 2. fetch the tablet by tablet_id and schema_hash
+    TabletSharedPtr tablet =
+            StorageEngine::instance()->tablet_manager()->get_tablet(tablet_id, 
schema_hash);
+
+    if (tablet == nullptr) {
+        LOG(WARNING) << "invalid argument.tablet_id:" << tablet_id
+                     << ", schema_hash:" << schema_hash;
+        return Status::InternalError(
+                strings::Substitute("fail to get $0, $1", tablet_id, 
schema_hash));
+    }
+
+    // 3. execute compaction task
+    std::packaged_task<OLAPStatus()> task([this, tablet, compaction_type]() { 
+            return _execute_compaction_callback(tablet, compaction_type);
+    });
+    std::future<OLAPStatus> future_obj = task.get_future();
+
+    {
+        // 3.1 check is there compaction running
+        std::lock_guard<std::mutex> lock(_compaction_running_mutex);
+        if (_is_compaction_running) {
+            return Status::TooManyTasks("Manual compaction task is running");
+        } else {
+            // 3.2 execute the compaction task and set compaction task running 
+            _is_compaction_running = true;
+            std::thread(std::move(task)).detach();
+        }
+    }
+
+    // 4. wait for result for 2 seconds by async
+    std::future_status status = future_obj.wait_for(std::chrono::seconds(2));
+    if (status == std::future_status::ready) {
+        // fetch execute result
+        OLAPStatus olap_status = future_obj.get();
+        if (olap_status != OLAP_SUCCESS) {
+            return Status::InternalError(
+                    strings::Substitute("fail to execute compaction, error = 
$0", olap_status));
+        }
+    } else {
+        LOG(INFO) << "Manual compaction task is timeout for waiting " << 
(status == std::future_status::timeout);
+    }
+   
+    LOG(INFO) << "Manual compaction task is successfully triggered";
+    *json_result = "{\"status\": \"Success\", \"msg\": \"compaction task is 
successfully triggered.\"}";
+
+    return Status::OK();
+}
+
+Status CompactionAction::_handle_run_status_compaction(HttpRequest *req, 
std::string* json_result) {
+
+    uint64_t tablet_id = 0;
+    uint32_t schema_hash = 0;
+    
+    // check req_tablet_id and req_schema_hash is not empty
+    Status check_status = _check_param(req, &tablet_id, &schema_hash);
+    RETURN_IF_ERROR(check_status);
+
+    // fetch the tablet by tablet_id and schema_hash
+    TabletSharedPtr tablet =
+            StorageEngine::instance()->tablet_manager()->get_tablet(tablet_id, 
schema_hash);
+
+    if (tablet == nullptr) {
+        LOG(WARNING) << "invalid argument.tablet_id:" << tablet_id
+                     << ", schema_hash:" << schema_hash;
+        return Status::InternalError(
+                strings::Substitute("fail to get $0, $1", tablet_id, 
schema_hash));
+    }
+
+    std::string json_template = R"({
+        "status" : "Success",
+        "run_status" : $0,
+        "msg" : "$1",
+        "tablet_id" : $2,
+        "schema_hash" : $3,
+        "compact_type" : "$4"
+})";
+
+    std::string msg = "this tablet_id is not running";
+    std::string compaction_type = "";
+    bool run_status = 0;
+
+    // use try lock to check this tablet is running cumulative compaction
+    MutexLock lock_cumulativie(tablet->get_cumulative_lock(), TRY_LOCK);

Review comment:
       Miss to unlock?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



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

Reply via email to