Modify handling pending requests 41/296741/3
authorHwankyu Jhun <h.jhun@samsung.com>
Fri, 4 Aug 2023 02:22:33 +0000 (11:22 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Fri, 4 Aug 2023 04:55:52 +0000 (13:55 +0900)
The SetTimer() method is added to the Request class. The method sets
a timer using the interval that is calculated with the timeout variable.
And, the log is added for debugging.

Change-Id: Ibb0636f8f3fc174948ae57e9b987c9ad13d4f6e1
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/lib/request/pending_item.cc
src/lib/request/pending_item.hh
src/lib/request/request.cc
src/lib/request/request.hh
src/lib/request/request_manager.cc
src/lib/request/request_manager.hh

index aed2f53..d60e2b6 100644 (file)
@@ -17,7 +17,9 @@
 #include "lib/request/pending_item.hh"
 
 #include <aul_cmd.h>
+
 #include <ctime>
+#include <utility>
 
 #include "lib/amd_socket.h"
 #include "lib/amd_util.h"
@@ -53,14 +55,11 @@ pid_t PendingItem::GetPid() const {
 }
 
 bool PendingItem::AddRequestSimple(RequestPtr req) {
-  timespec end{};
-  clock_gettime(CLOCK_MONOTONIC, &end);
-  auto interval = GetPendingInterval(req->GetStartTime(), end);
-  req->SetTimer(interval);
+  req->SetTimer();
 
-  _D("request(%s[%d]:%d:%u) is added on pending list",
+  _W("request(%s[%d]:%d:%u:%d) is added on pending list",
       aul_cmd_convert_to_string(req->GetCmd()),
-      req->GetCmd(), req->GetPID(), req->GetTargetUID());
+      req->GetCmd(), req->GetPID(), req->GetTargetUID(), req->GetID());
 
   requests_.push_back(std::move(req));
   return true;
@@ -74,10 +73,7 @@ bool PendingItem::AddRequest(RequestPtr req) {
 
   req->SetTargetPID(getpid());
   if (req->GetUID() >= REGULAR_UID_MIN || req->IsIndirectRequest()) {
-    timespec end{};
-    clock_gettime(CLOCK_MONOTONIC, &end);
-    auto interval = GetPendingInterval(req->GetStartTime(), end);
-    req->SetTimer(interval);
+    req->SetTimer();
   } else {
     req->SendResult(0);
     req->SetTimer(kSystemRequestTimeout);
@@ -112,6 +108,13 @@ bool PendingItem::IsEmpty() const {
   return requests_.empty() && reply_infos_.empty();
 }
 
+bool PendingItem::Contains(unsigned int req_id) const {
+  return std::find_if(std::begin(requests_), std::end(requests_),
+      [req_id](const auto& request) -> bool {
+        return request->GetID() == static_cast<int>(req_id & INT_MAX);
+      }) != std::end(requests_);
+}
+
 bool PendingItem::AddReplyInfo(ReplyInfoPtr info) {
   reply_infos_.push_back(std::move(info));
   return true;
index 063c691..fffe39c 100644 (file)
@@ -49,6 +49,7 @@ class PendingItem {
   bool RemoveReplyInfo(const ReplyInfoPtr& info);
   void Flush();
   bool IsEmpty() const;
+  bool Contains(unsigned int req_id) const;
 
   std::list<std::shared_ptr<Request>>& GetRequests();
   std::list<std::shared_ptr<ReplyInfo>>& GetReplyInfos();
index c423d87..9d52c74 100644 (file)
@@ -225,6 +225,14 @@ timespec& Request::GetStartTime() {
   return start_;
 }
 
+void Request::SetTimer() {
+  timespec end{};
+  clock_gettime(CLOCK_MONOTONIC, &end);
+  auto interval = GetPendingIntervalWithTimeout(GetStartTime(), end,
+      GetTimeout());
+  SetTimer(interval);
+}
+
 void Request::SetTimer(unsigned int timeout) {
   if (timer_ != 0) {
     _W("Already registered timeout event");
index 3613c4d..a7c3de0 100644 (file)
@@ -96,6 +96,7 @@ class Request : public std::enable_shared_from_this<Request> {
   unsigned char* GetRaw() const;
   const std::shared_ptr<tizen_base::Bundle>& GetBundle() const;
   void SetBundle(std::shared_ptr<tizen_base::Bundle> b);
+  void SetTimer();
   void SetTimer(unsigned int timeout);
   void UnsetTimer();
   void SetCritical();
index 618b551..ea426e4 100644 (file)
@@ -164,6 +164,17 @@ std::shared_ptr<PendingItem> RequestManager::FindPendingItem(pid_t pid) {
   return nullptr;
 }
 
+std::shared_ptr<PendingItem> RequestManager::FindPendingItem(
+    unsigned int req_id) {
+  for (const auto& iter : pending_items_) {
+    auto& item = iter.second;
+    if (item->Contains(req_id))
+      return item;
+  }
+
+  return nullptr;
+}
+
 std::shared_ptr<PendingItem> RequestManager::CreatePendingItem(
     unsigned int req_id, const std::string& appid, pid_t caller_pid,
     pid_t pid) {
@@ -227,8 +238,13 @@ void RequestManager::OnRequestTimeout(Request* request) {
   if (request == nullptr)
     return;
 
+  _E("Timed out. request(%s[%d]:%d:%u:%d)",
+      aul_cmd_convert_to_string(request->GetCmd()),
+      request->GetCmd(), request->GetPID(), request->GetTargetUID(),
+      request->GetID());
+
   auto req = request->GetSharedPtr();
-  auto pending_item = FindPendingItem(req->GetTargetPID());
+  auto pending_item = FindPendingItem(static_cast<unsigned int>(req->GetID()));
   if (pending_item != nullptr) {
     pending_item->RemoveRequest(req);
     if (pending_item->IsEmpty())
@@ -351,7 +367,7 @@ Request::Status RequestManager::CheckStatus(const RequestPtr& req) {
       _W("%s is waiting to be started. request_id: %u",
           appid.c_str(), app_status ? app_status->GetRequestID() : 0);
       req->SetCritical();
-      req->SetTimer(kPendingRequestTimeout);
+      req->SetTimer();
     }
   }
 
index 16f91cc..fb1c272 100644 (file)
@@ -77,6 +77,7 @@ class RequestManager : public Request::IEvent {
       const ucred& cr);
   std::shared_ptr<PendingItem> GetPendingItem(const RequestPtr& req,
       const std::string& appid);
+  std::shared_ptr<PendingItem> FindPendingItem(unsigned int req_id);
 
   static gboolean ProcessRequest(gpointer data);
   static void DispatchRequest(const RequestPtr& req);