SLTFRAOAlgorithm.cpp 6.45 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * The CERN Tape Archive (CTA) project
 * Copyright (C) 2019  CERN
 *
 * 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

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

SLTFRAOAlgorithm::SLTFRAOAlgorithm() {}

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

33
34
std::vector<uint64_t> SLTFRAOAlgorithm::performRAO(const std::vector<std::unique_ptr<cta::RetrieveJob> >& jobs) {
  std::vector<uint64_t> ret;
35
  //Determine all the files position
36
37
38
39
40
41
42
  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);
43
  m_raoTimings.insertAndReset("RAOAlgorithmTime",totalTimer);
44
45
46
47
48
49
50
51
  return ret;
}


SLTFRAOAlgorithm::~SLTFRAOAlgorithm() {
}


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

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

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

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

void SLTFRAOAlgorithm::Builder::initializeFilePositionEstimator() {
71
72
  RAOOptions::FilePositionEstimatorType filePositionType = m_raoParams.getRAOAlgorithmOptions().getFilePositionEstimatorType();
  switch(filePositionType){
73
    case RAOOptions::FilePositionEstimatorType::interpolation: {
74
75
76
77
78
79
80
      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.");
      }
81
82
83
      break;
    }
    default:
84
85
86
      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);
87
88
89
90
91
      break;
  }
}

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

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

107
108
109
110
111
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);
112
113
  }
}
114

115
std::vector<uint64_t> SLTFRAOAlgorithm::performSLTF(SLTFRAOAlgorithm::RAOFilesContainer & files) const {
116
  std::vector<uint64_t> solution;
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  //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);
    }
131
132
  }
  return solution;
133
}
134

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

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;
}
149
150
151
152
153

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

154
}}}}