From 5b1c706fe41aea4469e833184426bba68ac53861 Mon Sep 17 00:00:00 2001
From: Joao Afonso <joao.afonso@cern.ch>
Date: Thu, 28 Jul 2022 12:00:30 +0200
Subject: [PATCH] Move logic to minimize mounts for multi-copy tape pool
 recalls out of scheduler

---
 objectstore/Helpers.cpp | 21 +++++++++------------
 scheduler/Scheduler.cpp | 32 --------------------------------
 2 files changed, 9 insertions(+), 44 deletions(-)

diff --git a/objectstore/Helpers.cpp b/objectstore/Helpers.cpp
index 4cf120c690..67d3f1a5b6 100644
--- a/objectstore/Helpers.cpp
+++ b/objectstore/Helpers.cpp
@@ -453,22 +453,19 @@ std::string Helpers::selectBestRetrieveQueue(const std::set<std::string>& candid
   // Sort the tapes.
   candidateVidsStats.sort(SchedulerDatabase::RetrieveQueueStatistics::leftGreaterThanRight);
   // Get a list of equivalent best tapes
-  std::set<std::string> shortlistVids;
+  std::set<std::string> shortSetVids;
   for (auto & s: candidateVidsStats) {
     if (!(s<candidateVidsStats.front()) && !(s>candidateVidsStats.front()))
-      shortlistVids.insert(s.vid);
+      shortSetVids.insert(s.vid);
   }
   // If there is only one best tape, we're done
-  if (shortlistVids.size()==1) return *shortlistVids.begin();
-  // There are several equivalent entries, choose randomly among them.
-  // First element will always be selected.
-  // We need to get a random number [0, candidateVids.size() -1]
-  std::default_random_engine dre(std::chrono::system_clock::now().time_since_epoch().count());
-  std::uniform_int_distribution<size_t> distribution(0, candidateVids.size() -1);
-  size_t index=distribution(dre);
-  auto it=candidateVids.cbegin();
-  std::advance(it, index);
-  return *it;
+  if (shortSetVids.size()==1) return *shortSetVids.begin();
+  // There are several equivalent entries, choose one among them based on the number of days since epoch
+  std::vector<std::string> shortListVids(shortSetVids.begin(), shortSetVids.end());
+  std::sort(shortListVids.begin(), shortListVids.end());
+  const time_t secondsSinceEpoch = time(nullptr);
+  const uint64_t daysSinceEpoch = secondsSinceEpoch / (60*60*24);
+  return shortListVids[daysSinceEpoch % shortListVids.size()];
 }
 
 //------------------------------------------------------------------------------
diff --git a/scheduler/Scheduler.cpp b/scheduler/Scheduler.cpp
index 8e16af5800..48d9bb2445 100644
--- a/scheduler/Scheduler.cpp
+++ b/scheduler/Scheduler.cpp
@@ -192,40 +192,8 @@ std::string Scheduler::queueRetrieve(
   common::dataStructures::RetrieveFileQueueCriteria queueCriteria;
 
   queueCriteria = m_catalogue.prepareToRetrieveFile(instanceName, request.archiveFileID, request.requester, request.activity, lc, request.mountPolicy);
-
   queueCriteria.archiveFile.diskFileInfo = request.diskFileInfo;
 
-  // The following block of code is a temporary fix for the following CTA issue:
-  //
-  //   cta/CTA#777 Minimize mounts for dual copy tape pool recalls
-  //
-  // The code tries to force a recall to use the tape copy with the
-  // tape copy number equal to the number of days since the epoch.
-  // It reduces the number of overall number tape mounts
-  // in situations where files with multiple tape copies are being recalled.
-  {
-
-    const time_t secondsSinceEpoch = time(nullptr);
-    const uint64_t daysSinceEpoch = secondsSinceEpoch / (60*60*24);
-    const uint64_t indexToChose = daysSinceEpoch % queueCriteria.archiveFile.tapeFiles.size();
-
-    std::vector<uint64_t> tapeFileCopyNbs;
-
-    std::transform(queueCriteria.archiveFile.tapeFiles.begin(), queueCriteria.archiveFile.tapeFiles.end(),std::back_inserter(tapeFileCopyNbs),
-       [](const common::dataStructures::TapeFile &a) -> uint64_t {
-         return a.copyNb;});
-
-    std::sort(tapeFileCopyNbs.begin(), tapeFileCopyNbs.end(),
-      [](const uint64_t &a, const uint64_t &b) -> bool
-        {return a > b;});
-
-    const uint64_t chosenCopyNb = tapeFileCopyNbs.at(indexToChose);
-
-    const common::dataStructures::TapeFile chosenTapeFile = queueCriteria.archiveFile.tapeFiles.at(chosenCopyNb);
-    queueCriteria.archiveFile.tapeFiles.clear();
-    queueCriteria.archiveFile.tapeFiles.push_back(chosenTapeFile);
-  }
-
   auto diskSystemList = m_catalogue.getAllDiskSystems();
   auto catalogueTime = t.secs(cta::utils::Timer::resetCounter);
   // By default, the scheduler makes its decision based on all available vids. But if a vid is specified in the protobuf,
-- 
GitLab