SLTFRAOAlgorithm.cpp 6.63 KB
Newer Older
1
/*
2
3
4
5
6
7
 * @project        The CERN Tape Archive (CTA)
 * @copyright      Copyright(C) 2021 CERN
 * @license        This program is free software: you can redistribute it and/or modify
 *                 it under the terms of the GNU General Public License as published by
 *                 the Free Software Foundation, either version 3 of the License, or
 *                 (at your option) any later version.
8
 *
9
10
11
12
 *                 This program is distributed in the hope that it will be useful,
 *                 but WITHOUT ANY WARRANTY; without even the implied warranty of
 *                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *                 GNU General Public License for more details.
13
 *
14
15
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17
18
19
 */

#include "SLTFRAOAlgorithm.hpp"
#include "InterpolationFilePositionEstimator.hpp"
20
#include "RAOHelpers.hpp"
21
#include "CTACostHeuristic.hpp"
22
#include "common/Timer.hpp"
23
24
#include "CostHeuristicFactory.hpp"
#include "FilePositionEstimatorFactory.hpp"
25
26
27
28
29

namespace castor { namespace tape { namespace tapeserver { namespace rao {

SLTFRAOAlgorithm::SLTFRAOAlgorithm() {}

30
31
SLTFRAOAlgorithm::SLTFRAOAlgorithm(std::unique_ptr<FilePositionEstimator> & filePositionEstimator, std::unique_ptr<CostHeuristic> & costHeuristic):m_filePositionEstimator(std::move(filePositionEstimator)),m_costHeuristic(std::move(costHeuristic)) {}

32
33
std::vector<uint64_t> SLTFRAOAlgorithm::performRAO(const std::vector<std::unique_ptr<cta::RetrieveJob> >& jobs) {
  std::vector<uint64_t> ret;
34
  //Determine all the files position
35
36
37
38
39
40
41
  cta::utils::Timer t;
  cta::utils::Timer totalTimer;
  SLTFRAOAlgorithm::RAOFilesContainer files = computeAllFilesPosition(jobs);
  m_raoTimings.insertAndReset("computeAllFilesPositionTime",t);
  //Perform a Short Locate Time First algorithm on the files
  ret = performSLTF(files);
  m_raoTimings.insertAndReset("performSLTFTime",t);
42
  m_raoTimings.insertAndReset("RAOAlgorithmTime",totalTimer);
43
44
45
46
47
48
49
50
  return ret;
}


SLTFRAOAlgorithm::~SLTFRAOAlgorithm() {
}


51
SLTFRAOAlgorithm::Builder::Builder(const RAOParams& data):m_raoParams(data){
52
53
54
  m_algorithm.reset(new SLTFRAOAlgorithm());
}

55
56
57
58
59
60
61
62
void SLTFRAOAlgorithm::Builder::setCatalogue(cta::catalogue::Catalogue* catalogue){
  m_catalogue = catalogue;
}

void SLTFRAOAlgorithm::Builder::setDrive(drive::DriveInterface* drive) {
  m_drive = drive;
}

63
64
65
66
67
68
69
std::unique_ptr<SLTFRAOAlgorithm> SLTFRAOAlgorithm::Builder::build() {
  initializeFilePositionEstimator();
  initializeCostHeuristic();
  return std::move(m_algorithm);
}

void SLTFRAOAlgorithm::Builder::initializeFilePositionEstimator() {
70
71
  RAOOptions::FilePositionEstimatorType filePositionType = m_raoParams.getRAOAlgorithmOptions().getFilePositionEstimatorType();
  switch(filePositionType){
72
    case RAOOptions::FilePositionEstimatorType::interpolation: {
73
74
75
76
77
78
79
      if(m_catalogue != nullptr && m_drive != nullptr) {
        m_algorithm->m_filePositionEstimator = FilePositionEstimatorFactory::createInterpolationFilePositionEstimator(m_raoParams.getMountedVid(),m_catalogue,m_drive,m_algorithm->m_raoTimings);
      } else {
        //If the catalogue or the drive is not set, we don't have any way to give the InterpolationFilePositionEstimator the end of wrap position 
        //infos and the media type. Throw an exception
        throw cta::exception::Exception("In SLTFRAOAlgorithm::Builder::initializeFilePositionEstimator(), the drive and the catalogue are needed to build the InterpolationFilePositionEstimator.");
      }
80
81
82
      break;
    }
    default:
83
84
85
      std::string errorMsg = "In SLTFRAOAlgorithm::Builder::initializeFilePositionEstimator() unable to instanciate an estimator to estimate the position of the files on tape "
        "because the type given in parameter is unknown ("+std::to_string(filePositionType)+").";
      throw cta::exception::Exception(errorMsg);
86
87
88
89
90
      break;
  }
}

void SLTFRAOAlgorithm::Builder::initializeCostHeuristic() {
91
92
  CostHeuristicFactory factory;
  m_algorithm->m_costHeuristic = factory.createCostHeuristic(m_raoParams.getRAOAlgorithmOptions().getCostHeuristicType());
93
94
}

95
96
SLTFRAOAlgorithm::RAOFilesContainer SLTFRAOAlgorithm::computeAllFilesPosition(const std::vector<std::unique_ptr<cta::RetrieveJob> >& jobs) const {
  SLTFRAOAlgorithm::RAOFilesContainer files;
97
  for(uint64_t i = 0; i < jobs.size(); ++i){
98
    files.insert({i,RAOFile(i,m_filePositionEstimator->getFilePosition(*(jobs.at(i))))});
99
  }
100
  //Create a dummy file that starts at the beginning of the tape (blockId = 0) (the SLTF algorithm will start from this file)
101
  std::unique_ptr<cta::RetrieveJob> dummyRetrieveJob = createFakeRetrieveJobForFileAtBeginningOfTape();
102
  files.insert({jobs.size(),RAOFile(jobs.size(),m_filePositionEstimator->getFilePosition(*dummyRetrieveJob))});
103
104
105
  return files;
}

106
107
108
109
110
void SLTFRAOAlgorithm::computeCostBetweenFileAndOthers(RAOFile & file, const SLTFRAOAlgorithm::RAOFilesContainer & otherFiles) const {
  FilePositionInfos filePositionInfos = file.getFilePositionInfos();
  for(auto &otherFile: otherFiles) {
    double distance = m_costHeuristic->getCost(filePositionInfos,otherFile.second.getFilePositionInfos());
    file.addDistanceToFile(distance,otherFile.second);
111
112
  }
}
113

114
std::vector<uint64_t> SLTFRAOAlgorithm::performSLTF(SLTFRAOAlgorithm::RAOFilesContainer & files) const {
115
  std::vector<uint64_t> solution;
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  //Start from the fake file that is at the beginning of the tape (end of the files container)
  RAOFile firstFile = files.rbegin()->second;
  files.erase(firstFile.getIndex());
  computeCostBetweenFileAndOthers(firstFile,files);
  uint64_t closestFileIndex = firstFile.getClosestFileIndex();
  solution.push_back(closestFileIndex);
  while(!files.empty()){
    RAOFile currentFile = files.at(closestFileIndex);
    files.erase(currentFile.getIndex());
    if(files.size()){
      computeCostBetweenFileAndOthers(currentFile,files);
      closestFileIndex = currentFile.getClosestFileIndex();
      solution.push_back(closestFileIndex);
    }
130
131
  }
  return solution;
132
}
133

134
135
136
137
138
139
140
141
142
143
144
145
146
147

std::unique_ptr<cta::RetrieveJob> SLTFRAOAlgorithm::createFakeRetrieveJobForFileAtBeginningOfTape() const {
  std::unique_ptr<cta::RetrieveJob> ret;
  cta::common::dataStructures::ArchiveFile archiveFile;
  cta::common::dataStructures::TapeFile tapeFile;
  tapeFile.blockId = 0;
  tapeFile.copyNb = 1;
  tapeFile.fSeq = 0;
  tapeFile.fileSize = 0;
  archiveFile.tapeFiles.push_back(tapeFile);
  cta::common::dataStructures::RetrieveRequest retrieveRequest;
  ret.reset(new cta::RetrieveJob(nullptr,retrieveRequest,archiveFile,1,cta::PositioningMethod::ByBlock));
  return ret;
}
148
149
150
151
152

std::string SLTFRAOAlgorithm::getName() const {
  return "sltf";
}

153
}}}}