From 1fdb22f5d18d5853c9179659cc21f7eefb8b1990 Mon Sep 17 00:00:00 2001
From: Cedric CAFFY <cedric.caffy@cern.ch>
Date: Thu, 9 May 2019 11:15:54 +0200
Subject: [PATCH] The status of the repack request is updated throughout all
 its lifecycle (Pending, ToExpand, Starting, Running, Complete or failed)

---
 common/dataStructures/RepackInfo.cpp | 4 ----
 common/dataStructures/RepackInfo.hpp | 6 ++----
 objectstore/RetrieveRequest.cpp      | 2 +-
 objectstore/cta.proto                | 6 ++----
 scheduler/OStoreDB/OStoreDB.cpp      | 5 +++--
 scheduler/Scheduler.cpp              | 1 +
 scheduler/SchedulerDatabase.cpp      | 2 +-
 7 files changed, 10 insertions(+), 16 deletions(-)

diff --git a/common/dataStructures/RepackInfo.cpp b/common/dataStructures/RepackInfo.cpp
index c5855d9cd7..2f8700cc1a 100644
--- a/common/dataStructures/RepackInfo.cpp
+++ b/common/dataStructures/RepackInfo.cpp
@@ -37,10 +37,6 @@ std::string toString(RepackInfo::Type type) {
 
 std::string toString(RepackInfo::Status status) {
   switch(status) {
-    case RepackInfo::Status::Aborted:
-      return "Aborted";
-    case RepackInfo::Status::Aborting:
-      return "Aborting";
     case RepackInfo::Status::Complete:
       return "Complete";
     case RepackInfo::Status::Failed:
diff --git a/common/dataStructures/RepackInfo.hpp b/common/dataStructures/RepackInfo.hpp
index 4b38c97b1c..1d188ca172 100644
--- a/common/dataStructures/RepackInfo.hpp
+++ b/common/dataStructures/RepackInfo.hpp
@@ -43,10 +43,8 @@ struct RepackInfo {
     ToExpand = 2,
     Starting = 3,
     Running = 4,
-    Aborting = 5,
-    Aborted = 6,
-    Complete = 7,
-    Failed = 8,
+    Complete = 5,
+    Failed = 6,
     Undefined = 999
   } status;
   uint64_t totalFilesToArchive;
diff --git a/objectstore/RetrieveRequest.cpp b/objectstore/RetrieveRequest.cpp
index 8058477b6e..7325e3fc71 100644
--- a/objectstore/RetrieveRequest.cpp
+++ b/objectstore/RetrieveRequest.cpp
@@ -729,7 +729,7 @@ void RetrieveRequest::updateLifecycleTiming(serializers::RetrieveRequest& payloa
   typedef ::cta::objectstore::serializers::RetrieveJobStatus RetrieveJobStatus;
   LifecycleTimingsSerDeser lifeCycleSerDeser;
   lifeCycleSerDeser.deserialize(payload.lifecycle_timings());
-  switch(retrieveJob.status() == RetrieveJobStatus::RJS_ToTransferForUser){
+  switch(retrieveJob.status()){
     case RetrieveJobStatus::RJS_ToTransferForUser:
       if(retrieveJob.totalretries() == 0){
         //totalretries = 0 then this is the first selection of the request
diff --git a/objectstore/cta.proto b/objectstore/cta.proto
index 3c3e30afc3..1e4d465f5a 100644
--- a/objectstore/cta.proto
+++ b/objectstore/cta.proto
@@ -492,10 +492,8 @@ enum RepackRequestStatus {
   RRS_ToExpand = 2;
   RRS_Starting = 3;
   RRS_Running = 4;
-  RRS_Aborting = 5;
-  RRS_Aborted = 6;
-  RRS_Complete = 7;
-  RRS_Failed = 8;
+  RRS_Complete = 5;
+  RRS_Failed = 6;
 }
 
 // In order to properly handle retries in case of failure during reporting, we hold
diff --git a/scheduler/OStoreDB/OStoreDB.cpp b/scheduler/OStoreDB/OStoreDB.cpp
index 758fe391fe..fd445943c8 100644
--- a/scheduler/OStoreDB/OStoreDB.cpp
+++ b/scheduler/OStoreDB/OStoreDB.cpp
@@ -1038,7 +1038,7 @@ void OStoreDB::setRetrieveJobBatchReportedToUser(std::list<cta::SchedulerDatabas
         log::ScopedParamContainer params(lc);
         params.add("fileId", j->archiveFile.archiveFileID)
               .add("objectAddress", castFromSchedDBJob(j)->m_retrieveRequest.getAddressIfSet());
-        lc.log(log::ERR, "In OStoreDB::setRetrieveJobBatchReported(): unexpected job status. Leaving the job as-is.");
+        lc.log(log::ERR, "In OStoreDB::setRetrieveJobBatchReportedToUser(): unexpected job status. Leaving the job as-is.");
       }
     }
   }
@@ -1793,6 +1793,7 @@ void OStoreDB::RepackRetrieveSuccessesReportBatch::report(log::LogContext& lc) {
     objectstore::ScopedExclusiveLock rrl(m_repackRequest);
     timingList.insertAndReset("successStatsLockTime", t);
     m_repackRequest.fetch();
+    m_repackRequest.setStatus(common::dataStructures::RepackInfo::Status::Running);
     timingList.insertAndReset("successStatsFetchTime", t);
     m_repackRequest.reportRetriveSuccesses(ssl);
     timingList.insertAndReset("successStatsUpdateTime", t);
@@ -2291,7 +2292,7 @@ void OStoreDB::RepackRequest::addSubrequests(std::list<Subrequest>& repackSubreq
 //------------------------------------------------------------------------------
 void OStoreDB::RepackRequest::expandDone() {
   // We are now done with the repack request. We can set its status.
-  ScopedSharedLock rrl(m_repackRequest);
+  ScopedExclusiveLock rrl(m_repackRequest);
   m_repackRequest.fetch();
   // After expansion, 2 statuses are possible: starting (nothing reported as done) or running (anything reported as done).
   // We can find that out from the statistics...
diff --git a/scheduler/Scheduler.cpp b/scheduler/Scheduler.cpp
index 7abb7a8c10..ad12e5c892 100644
--- a/scheduler/Scheduler.cpp
+++ b/scheduler/Scheduler.cpp
@@ -526,6 +526,7 @@ void Scheduler::expandRepackRequest(std::unique_ptr<RepackRequest>& repackReques
     fSeq = std::max(fSeq, maxAddedFSeq + 1);
     repackRequest->m_dbReq->setLastExpandedFSeq(fSeq);
   }
+  repackRequest->m_dbReq->expandDone();
 }
 
 //------------------------------------------------------------------------------
diff --git a/scheduler/SchedulerDatabase.cpp b/scheduler/SchedulerDatabase.cpp
index 866d19a13f..4c257ef242 100644
--- a/scheduler/SchedulerDatabase.cpp
+++ b/scheduler/SchedulerDatabase.cpp
@@ -27,7 +27,7 @@ cta::SchedulerDatabase::~SchedulerDatabase() throw() { }
 
 SchedulerDatabase::RepackRequestStatistics::RepackRequestStatistics() {
   typedef common::dataStructures::RepackInfo::Status Status;
-  for (auto & s: {Status::Aborted, Status::Aborting, Status::Complete, Status::Failed, Status::Pending, 
+  for (auto & s: {Status::Complete, Status::Failed, Status::Pending, 
       Status::Running, Status::Starting, Status::ToExpand})
     operator [](s) = 0;
 }
-- 
GitLab