From cb32c3e8f0e6a4c7289ebd1e8f00775e974f5acb Mon Sep 17 00:00:00 2001
From: mvelosob <miguel.veloso.barros@cern.ch>
Date: Mon, 23 May 2022 16:18:06 +0200
Subject: [PATCH] add --loadtimeout option to cta-tape-label and increase
 default value (#1231)

---
 ReleaseNotes.md                               |  1 +
 tapeserver/tapelabel/TapeLabelCmd.cpp         |  3 +-
 tapeserver/tapelabel/TapeLabelCmdLineArgs.cpp | 34 +++++++++++++++----
 tapeserver/tapelabel/TapeLabelCmdLineArgs.hpp |  5 +++
 4 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/ReleaseNotes.md b/ReleaseNotes.md
index e6330739f4..dec4ce1563 100644
--- a/ReleaseNotes.md
+++ b/ReleaseNotes.md
@@ -8,6 +8,7 @@
 - cta/CTA#1217 - Schema verification should just issue an warning if there are extra indexes in the db that are not in the catalogue
 - cta/CTA#1220 - Improve queued retrieve logging message
 - cta/CTA#1152 - Reduce eos free space query load
+- cta/CTA#1231 - Add --loadtimeout option to cta-tape-label and increase default value
 
 ### Bug fixes
 - cta/CTA#1120 - Fix negative disk space reservation content
diff --git a/tapeserver/tapelabel/TapeLabelCmd.cpp b/tapeserver/tapelabel/TapeLabelCmd.cpp
index 81b9f0313a..40189185e7 100644
--- a/tapeserver/tapelabel/TapeLabelCmd.cpp
+++ b/tapeserver/tapelabel/TapeLabelCmd.cpp
@@ -73,6 +73,7 @@ int TapeLabelCmd::exceptionThrowingMain(const int argc, char *const *const argv)
   params.push_back(cta::log::Param("tapeVid", cmdLineArgs.m_vid));
   params.push_back(cta::log::Param("tapeOldLabel",cmdLineArgs.m_oldLabel));
   params.push_back(cta::log::Param("force", boolToStr(m_force)));
+  params.push_back(cta::log::Param("tapeLoadTimeout", cmdLineArgs.m_tapeLoadTimeout));
   m_log(cta::log::INFO, "Label session started", params);
   
   readAndSetConfiguration(getUsername(), cmdLineArgs.m_vid, cmdLineArgs.m_oldLabel);
@@ -96,7 +97,7 @@ int TapeLabelCmd::exceptionThrowingMain(const int argc, char *const *const argv)
   };
   
   mountTape(m_vid);
-  waitUntilTapeLoaded(drive, TAPE_LABEL_UNITREADY_TIMEOUT);
+  waitUntilTapeLoaded(drive, cmdLineArgs.m_tapeLoadTimeout);
   
   int returnCode = 0;
   if(drive.isWriteProtected()) {
diff --git a/tapeserver/tapelabel/TapeLabelCmdLineArgs.cpp b/tapeserver/tapelabel/TapeLabelCmdLineArgs.cpp
index 08b8b7e345..7b92159769 100644
--- a/tapeserver/tapelabel/TapeLabelCmdLineArgs.cpp
+++ b/tapeserver/tapelabel/TapeLabelCmdLineArgs.cpp
@@ -39,6 +39,7 @@ TapeLabelCmdLineArgs::TapeLabelCmdLineArgs(const int argc, char *const *const ar
     {"vid", required_argument, nullptr, 'v'},
     {"oldlabel", required_argument, nullptr, 'o'},
     {"debug", no_argument, nullptr, 'd'},
+    {"loadtimeout", required_argument, nullptr, 't'},
     {"force", no_argument, nullptr, 'f'},
     {"help", no_argument, nullptr, 'h'},
     {nullptr,          0, nullptr,   0}
@@ -50,7 +51,7 @@ TapeLabelCmdLineArgs::TapeLabelCmdLineArgs(const int argc, char *const *const ar
 
   int opt = 0;
 
-  while((opt = getopt_long(argc, argv, ":v:o:hdf", longopts, nullptr)) != -1) {
+  while((opt = getopt_long(argc, argv, ":v:o:t:hdf", longopts, nullptr)) != -1) {
     switch(opt) {
     case 'v':
       if (strlen(optarg) > CA_MAXVIDLEN) {
@@ -69,7 +70,25 @@ TapeLabelCmdLineArgs::TapeLabelCmdLineArgs(const int argc, char *const *const ar
         throw ex;
       } else {
         m_oldLabel = std::string(optarg);
-	utils::toUpper(m_oldLabel);
+	      utils::toUpper(m_oldLabel);
+      }
+      break;
+    case 't':
+      try {
+        m_tapeLoadTimeout = std::stoul(optarg, nullptr, 0);
+      } catch (std::invalid_argument &) {
+        exception::CommandLineNotParsed ex;
+        ex.getMessage() << "Invalid value for the tape load timeout: " << optarg;
+        throw ex;
+      } catch (std::out_of_range &) {
+        exception::CommandLineNotParsed ex;
+        ex.getMessage() << "Too large value for the tape load timeout: " << optarg;
+        throw ex;
+      }
+      if (!m_tapeLoadTimeout) {
+        exception::CommandLineNotParsed ex;
+        ex.getMessage() << "The tape load timeout cannot be 0";
+        throw ex;
       }
       break;
     case 'h':
@@ -128,12 +147,13 @@ void TapeLabelCmdLineArgs::printUsage(std::ostream &os) {
     "Usage:" << std::endl <<
     "  cta-tape-label [options] --vid/-v VID" << std::endl <<
     "Where:" << std::endl <<
-    "  -v, --vid        The VID of the tape to be labeled" << std::endl <<
+    "  -v, --vid           The VID of the tape to be labeled" << std::endl <<
     "Options:" <<std::endl <<
-    "  -o, --oldlabel   The vid of the current tape label on the tape if it is not the same as VID" << std::endl <<
-    "  -h, --help       Print this help message and exit" << std::endl <<
-    "  -d, --debug      Print more logs for label operations" << std::endl <<
-    "  -f, --force      Force labeling for not-blank tapes for testing purpose and without label checks. Must only be used manually." << std::endl;  
+    "  -o, --oldlabel      The vid of the current tape label on the tape if it is not the same as VID" << std::endl <<
+    "  -t, --loadtimeout   The timeout to load the tape in the drive slot in seconds (default: 2 hours)" << std::endl <<
+    "  -h, --help          Print this help message and exit" << std::endl <<
+    "  -d, --debug         Print more logs for label operations" << std::endl <<
+    "  -f, --force         Force labeling for not-blank tapes for testing purpose and without label checks. Must only be used manually." << std::endl;  
 }
 
 } // namespace tapelabel
diff --git a/tapeserver/tapelabel/TapeLabelCmdLineArgs.hpp b/tapeserver/tapelabel/TapeLabelCmdLineArgs.hpp
index cd59f7367c..3f59ab6b16 100644
--- a/tapeserver/tapelabel/TapeLabelCmdLineArgs.hpp
+++ b/tapeserver/tapelabel/TapeLabelCmdLineArgs.hpp
@@ -54,6 +54,11 @@ struct TapeLabelCmdLineArgs {
    */ 
   bool m_force;
 
+  /**
+   * The timeout to load the tape in the drive slot in seconds
+   */
+  uint64_t m_tapeLoadTimeout = 2 * 60 * 60; // 2 hours
+
   /**
    * Constructor that parses the specified command-line arguments.
    *
-- 
GitLab