ArchiveQueueShard.cpp 5.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

/*
 * The CERN Tape Archive (CTA) project
 * Copyright (C) 2015  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 "ArchiveQueueShard.hpp"


namespace cta { namespace objectstore {

ArchiveQueueShard::ArchiveQueueShard(Backend& os):
  ObjectOps<serializers::ArchiveQueueShard, serializers::ArchiveQueueShard_t>(os) { }

ArchiveQueueShard::ArchiveQueueShard(const std::string& address, Backend& os):
  ObjectOps<serializers::ArchiveQueueShard, serializers::ArchiveQueueShard_t>(os, address) { }

void ArchiveQueueShard::rebuild() {
  checkPayloadWritable();
  uint64_t totalSize=0;
  for (auto j: m_payload.archivejobs()) {
    totalSize += j.size();
  }
  m_payload.set_archivejobstotalsize(totalSize);
}

void ArchiveQueueShard::garbageCollect(const std::string& presumedOwner, AgentReference& agentReference, log::LogContext& lc, cta::catalogue::Catalogue& catalogue) {
  throw exception::Exception("In ArchiveQueueShard::garbageCollect(): garbage collection should not be necessary for this type of object.");
}

ArchiveQueue::CandidateJobList ArchiveQueueShard::getCandidateJobList(uint64_t maxBytes, uint64_t maxFiles, std::set<std::string> archiveRequestsToSkip) {
  checkPayloadReadable();
  ArchiveQueue::CandidateJobList ret;
  ret.remainingBytesAfterCandidates = m_payload.archivejobstotalsize();
  ret.remainingFilesAfterCandidates = m_payload.archivejobs_size();
  for (auto & j: m_payload.archivejobs()) {
    if (!archiveRequestsToSkip.count(j.address())) {
      ret.candidates.push_back({j.size(), j.address(), (uint16_t)j.copynb()});
      ret.candidateBytes += j.size();
      ret.candidateFiles ++;
    }
    ret.remainingBytesAfterCandidates -= j.size();
    ret.remainingFilesAfterCandidates--;
    if (ret.candidateBytes >= maxBytes || ret.candidateFiles >= maxFiles) break;
  }
  return ret;
}

auto ArchiveQueueShard::removeJobs(const std::list<std::string>& jobsToRemove) -> RemovalResult {
  checkPayloadWritable();
  RemovalResult ret;
  uint64_t totalSize = m_payload.archivejobstotalsize();
  auto * jl=m_payload.mutable_archivejobs();
  for (auto &rrt: jobsToRemove) {
    bool found = false;
    do {
      found = false;
      // Push the found entry all the way to the end.
      for (size_t i=0; i<(size_t)jl->size(); i++) {
        if (jl->Get(i).address() == rrt) {
          found = true;
          const auto & j = jl->Get(i);
          ret.removedJobs.emplace_back(JobInfo());
          ret.removedJobs.back().address = j.address();
          ret.removedJobs.back().copyNb = j.copynb();
          ret.removedJobs.back().maxDrivesAllowed = j.maxdrivesallowed();
          ret.removedJobs.back().minArchiveRequestAge = j.minarchiverequestage();
          ret.removedJobs.back().priority = j.priority();
          ret.removedJobs.back().size = j.size();
          ret.removedJobs.back().startTime = j.starttime();
          ret.bytesRemoved += j.size();
          totalSize -= j.size();
          ret.jobsRemoved++;
          m_payload.set_archivejobstotalsize(m_payload.archivejobstotalsize() - j.size());
          while (i+1 < (size_t)jl->size()) {
            jl->SwapElements(i, i+1);
            i++;
          }
          break;
        }
      }
      // and remove it
      if (found)
        jl->RemoveLast();
    } while (found);
  }
  ret.bytesAfter = totalSize;
  ret.jobsAfter = m_payload.archivejobs_size();
  return ret;
}

void ArchiveQueueShard::initialize(const std::string& owner) {
  ObjectOps<serializers::ArchiveQueueShard, serializers::ArchiveQueueShard_t>::initialize();
  setOwner(owner);
  setBackupOwner(owner);
  m_payload.set_archivejobstotalsize(0);
  m_payloadInterpreted=true;
}

auto ArchiveQueueShard::dumpJobs() -> std::list<JobInfo> { 
  checkPayloadReadable();
  std::list<JobInfo> ret;
  for (auto &j: m_payload.archivejobs()) {
    ret.emplace_back(JobInfo{j.size(), j.address(), (uint16_t)j.copynb(), j.priority(), 
        j.minarchiverequestage(), j.maxdrivesallowed(), (time_t)j.starttime()});
  }
  return ret;
}

auto ArchiveQueueShard::getJobsSummary() -> JobsSummary {
  checkPayloadReadable();
  JobsSummary ret;
  ret.bytes = m_payload.archivejobstotalsize();
  return ret;
}

uint64_t ArchiveQueueShard::addJob(ArchiveQueue::JobToAdd& jobToAdd) {
  checkPayloadWritable();
  auto * j = m_payload.mutable_archivejobs()->Add();
  j->set_address(jobToAdd.archiveRequestAddress);
  j->set_size(jobToAdd.fileSize);
  j->set_fileid(jobToAdd.archiveFileId);
  j->set_copynb(jobToAdd.job.copyNb);
  j->set_maxdrivesallowed(jobToAdd.policy.maxDrivesAllowed);
  j->set_priority(jobToAdd.policy.archivePriority);
  j->set_minarchiverequestage(jobToAdd.policy.archiveMinRequestAge);
  j->set_starttime(jobToAdd.startTime);
  m_payload.set_archivejobstotalsize(m_payload.archivejobstotalsize()+jobToAdd.fileSize);
  return m_payload.archivejobs_size();
}




}}