SLTFRAOAlgorithm.cpp 6.3 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
26
27
28

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

SLTFRAOAlgorithm::SLTFRAOAlgorithm() {}

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

31
32
std::vector<uint64_t> SLTFRAOAlgorithm::performRAO(const std::vector<std::unique_ptr<cta::RetrieveJob> >& jobs) {
  std::vector<uint64_t> ret;
33
  //Determine all the files position
34
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);
  m_raoTimings.insertAndReset("totalTime",totalTimer);
42
43
44
45
46
47
48
49
  return ret;
}


SLTFRAOAlgorithm::~SLTFRAOAlgorithm() {
}


50
SLTFRAOAlgorithm::Builder::Builder(const RAOParams& data, drive::DriveInterface * drive, cta::catalogue::Catalogue * catalogue):m_raoParams(data),m_drive(drive),m_catalogue(catalogue){
51
52
53
54
55
56
57
58
59
60
  m_algorithm.reset(new SLTFRAOAlgorithm());
}

std::unique_ptr<SLTFRAOAlgorithm> SLTFRAOAlgorithm::Builder::build() {
  initializeFilePositionEstimator();
  initializeCostHeuristic();
  return std::move(m_algorithm);
}

void SLTFRAOAlgorithm::Builder::initializeFilePositionEstimator() {
61
  switch(m_raoParams.getRAOAlgorithmOptions().getFilePositionEstimatorType()){
62
    case RAOOptions::FilePositionEstimatorType::interpolation: {
63
64
      std::string vid = m_raoParams.getMountedVid();
      cta::utils::Timer t;
65
      cta::catalogue::MediaType tapeMediaType = m_catalogue->getMediaTypeByVid(vid);
66
      m_algorithm->m_raoTimings.insertAndReset("catalogueGetMediaTypeByVidTime",t);
67
      std::vector<drive::endOfWrapPosition> endOfWrapPositions = m_drive->getEndOfWrapPositions();
68
      m_algorithm->m_raoTimings.insertAndReset("getEndOfWrapPositionsTime",t);
69
      RAOHelpers::improveEndOfLastWrapPositionIfPossible(endOfWrapPositions);
70
      m_algorithm->m_raoTimings.insertAndReset("improveEndOfWrapPositionsIfPossibleTime",t);
71
72
73
74
75
76
77
78
79
80
      m_algorithm->m_filePositionEstimator.reset(new InterpolationFilePositionEstimator(endOfWrapPositions,tapeMediaType));
      break;
    }
    default:
      throw cta::exception::Exception("In SLTFRAOAlgorithm::Builder::initializeFilePositionEstimator() unable to instanciate an estimator to estimate the position of the files on tape.");
      break;
  }
}

void SLTFRAOAlgorithm::Builder::initializeCostHeuristic() {
81
  switch(m_raoParams.getRAOAlgorithmOptions().getCostHeuristicType()){
82
83
    case RAOOptions::CostHeuristicType::cta:
    {
84
85
      m_algorithm->m_costHeuristic.reset(new CTACostHeuristic());
      break;
86
    }
87
88
89
    default:
      throw cta::exception::Exception("In SLTFRAOAlgorithm::Builder::initializeCostHeuristic() unknown type of cost heuristic.");
  }
90
91
}

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

103
104
105
106
107
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);
108
109
  }
}
110

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

131
132
133
134
135
136
137
138
139
140
141
142
143
144

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;
}
145
146
147
148
149

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

150
}}}}