MigrationTaskInjector.cpp 10.5 KB
Newer Older
1
2
/*
 * @project        The CERN Tape Archive (CTA)
3
 * @copyright      Copyright(C) 2003-2021 CERN
4
5
6
7
 * @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.
David COME's avatar
David COME committed
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.
David COME's avatar
David COME committed
13
 *
14
15
16
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
David COME's avatar
David COME committed
17
18

#include "castor/tape/tapeserver/daemon/MigrationTaskInjector.hpp"
19
#include "castor/tape/tapeserver/daemon/ErrorFlag.hpp"
David COME's avatar
David COME committed
20

Victor Kotlyar's avatar
Victor Kotlyar committed
21
22
using cta::log::LogContext;
using cta::log::Param;
23

David COME's avatar
David COME committed
24
25
26
27
28
namespace castor{
namespace tape{
namespace tapeserver{
namespace daemon {

29
30
31
//------------------------------------------------------------------------------
//Constructor
//------------------------------------------------------------------------------
32
  MigrationTaskInjector::MigrationTaskInjector(MigrationMemoryManager & mm, 
33
          DiskReadThreadPool & diskReader,
34
          TapeSingleThreadInterface<TapeWriteTask> & tapeWriter,cta::ArchiveMount &archiveMount,
Victor Kotlyar's avatar
Victor Kotlyar committed
35
           uint64_t maxFiles, uint64_t byteSizeThreshold,cta::log::LogContext lc):
David COME's avatar
David COME committed
36
          m_thread(*this),m_memManager(mm),m_tapeWriter(tapeWriter),
37
          m_diskReader(diskReader),m_archiveMount(archiveMount),m_lc(lc),
38
          m_maxFiles(maxFiles),  m_maxBytes(byteSizeThreshold)
David COME's avatar
David COME committed
39
40
41
42
43
  {
    
  }
  
 
44
45
46
//------------------------------------------------------------------------------
//injectBulkMigrations
//------------------------------------------------------------------------------
47
  void MigrationTaskInjector::injectBulkMigrations(std::list<std::unique_ptr<cta::ArchiveJob>>& jobs){
48

49
    const uint64_t blockCapacity = m_memManager.blockCapacity();
50
    for(auto it= jobs.begin();it!=jobs.end();++it){
51
      const uint64_t fileSize = (*it)->archiveFile.fileSize;
52
      LogContext::ScopedParam sp[]={
53
      LogContext::ScopedParam(m_lc, Param("fileId", (*it)->archiveFile.archiveFileID)),
54
      LogContext::ScopedParam(m_lc, Param("fSeq", (*it)->tapeFile.fSeq)),
55
      LogContext::ScopedParam(m_lc, Param("path", (*it)->srcURL))
56
      };
57
      tape::utils::suppresUnusedVariable(sp);      
David COME's avatar
David COME committed
58
      
59
      const uint64_t neededBlock = howManyBlocksNeeded(fileSize,blockCapacity);
David COME's avatar
David COME committed
60
      
61
62
63
64
65
      // We give owner ship on the archive job to the tape write task (as last user).
      // disk read task gets a bare pointer.
      // TODO: could be changed as a shared_ptr.
      auto archiveJobPtr=it->get();
      std::unique_ptr<TapeWriteTask> twt(new TapeWriteTask(neededBlock, it->release(), m_memManager, m_errorFlag));
66
67
68
69
      std::unique_ptr<DiskReadTask> drt;
      // We will skip the disk read task creation for zero-length files. Tape write task will handle the request and mark it as an
      // error.
      if (fileSize) drt.reset(new DiskReadTask(*twt, archiveJobPtr, neededBlock, m_errorFlag));
70
      
71
      m_tapeWriter.push(twt.release());
72
      if (fileSize) m_diskReader.push(drt.release());
Victor Kotlyar's avatar
Victor Kotlyar committed
73
      m_lc.log(cta::log::INFO, "Created tasks for migrating a file");
74
75
      if (!fileSize)
        m_lc.log(cta::log::WARNING, "Skipped disk read task creation for zero-length file.");
David COME's avatar
David COME committed
76
    }
77
    LogContext::ScopedParam(m_lc, Param("numbnerOfFiles", jobs.size()));
Victor Kotlyar's avatar
Victor Kotlyar committed
78
    m_lc.log(cta::log::INFO, "Finished creating tasks for migrating");
David COME's avatar
David COME committed
79
80
  }
  
81
82
83
//------------------------------------------------------------------------------
//injectBulkMigrations
//------------------------------------------------------------------------------
David COME's avatar
David COME committed
84
85
86
87
  void MigrationTaskInjector::waitThreads(){
    m_thread.wait();
  }
  
88
89
90
//------------------------------------------------------------------------------
//injectBulkMigrations
//------------------------------------------------------------------------------
David COME's avatar
David COME committed
91
92
93
  void MigrationTaskInjector::startThreads(){
    m_thread.start();
  }
94
  
95
96
97
//------------------------------------------------------------------------------
//requestInjection
//------------------------------------------------------------------------------
98
  void MigrationTaskInjector::requestInjection( bool lastCall) {
99
    cta::threading::MutexLocker ml(m_producerProtection);
100
101
    m_queue.push(Request(m_maxFiles, m_maxBytes, lastCall));

102
  }
103
104
105
//------------------------------------------------------------------------------
//synchronousInjection
//------------------------------------------------------------------------------ 
106
  bool MigrationTaskInjector::synchronousInjection(bool & noFilesToMigrate) {
107
    std::list<std::unique_ptr<cta::ArchiveJob> > jobs;
108
    noFilesToMigrate = false;
109
    try {
110
      //First popping of files, we multiply the number of popped files / bytes by 2 to avoid multiple mounts on Repack
111
112
      //(it is applied to ArchiveForUser and ArchiveForRepack batches)
      jobs = m_archiveMount.getNextJobBatch(2 * m_maxFiles, 2 * m_maxBytes,m_lc);
113
    } catch (cta::exception::Exception & ex) {
Victor Kotlyar's avatar
Victor Kotlyar committed
114
      cta::log::ScopedParamContainer scoped(m_lc);
115
      scoped.add("transactionId", m_archiveMount.getMountTransactionId())
116
117
118
            .add("byteSizeThreshold",m_maxBytes)
            .add("maxFiles", m_maxFiles)
            .add("message", ex.getMessageValue());
Victor Kotlyar's avatar
Victor Kotlyar committed
119
      m_lc.log(cta::log::ERR, "Failed to getFilesToMigrate");
120
121
      return false;
    }
Victor Kotlyar's avatar
Victor Kotlyar committed
122
    cta::log::ScopedParamContainer scoped(m_lc); 
123
    scoped.add("byteSizeThreshold",m_maxBytes)
124
          .add("maxFiles", m_maxFiles);
125
    if(jobs.empty()) {
126
      noFilesToMigrate = true;
127
      m_lc.log(cta::log::WARNING, "No files to migrate: empty mount");
128
129
      return false;
    } else {
130
      m_firstFseqToWrite = jobs.front()->tapeFile.fSeq;
131
132
133
134
      injectBulkMigrations(jobs);
      return true;
    }
  }
135
136
137
//------------------------------------------------------------------------------
//finish
//------------------------------------------------------------------------------ 
138
  void MigrationTaskInjector::finish(){
139
    cta::threading::MutexLocker ml(m_producerProtection);
140
141
    m_queue.push(Request());
  }
142
//------------------------------------------------------------------------------
143
144
145
146
147
148
149
150
//signalEndDataMovement
//------------------------------------------------------------------------------   
  void MigrationTaskInjector::signalEndDataMovement(){        
    //first send the end signal to the threads
    m_tapeWriter.finish();
    m_diskReader.finish();
    m_memManager.finish();
  }
151

152
//------------------------------------------------------------------------------
153
154
//WorkerThread::run
//------------------------------------------------------------------------------
155
  void MigrationTaskInjector::WorkerThread::run(){
156
    m_parent.m_lc.pushOrReplace(Param("thread", "MigrationTaskInjector"));
Victor Kotlyar's avatar
Victor Kotlyar committed
157
    m_parent.m_lc.log(cta::log::INFO, "Starting MigrationTaskInjector thread");
158
159
160
    try{
      while(1){
        if(m_parent.m_errorFlag){
161
          throw castor::tape::tapeserver::daemon::ErrorFlag();
162
163
        }
        Request req = m_parent.m_queue.pop();
164
        auto jobs = m_parent.m_archiveMount.getNextJobBatch(req.filesRequested, req.bytesRequested, m_parent.m_lc);
165
        uint64_t files=jobs.size();
166
        uint64_t bytes=0;
167
        for (auto & j:jobs) bytes+=j->archiveFile.fileSize;
168
        if(jobs.empty()){
169
          if (req.lastCall) {
Victor Kotlyar's avatar
Victor Kotlyar committed
170
            m_parent.m_lc.log(cta::log::INFO,"No more file to migrate: triggering the end of session.");
171
            m_parent.signalEndDataMovement();
172
173
            break;
          } else {
Victor Kotlyar's avatar
Victor Kotlyar committed
174
            m_parent.m_lc.log(cta::log::INFO,"In MigrationTaskInjector::WorkerThread::run(): got empty list, but not last call");
175
          }
176
        } else {
177
          
178
          // Inject the tasks
179
          m_parent.injectBulkMigrations(jobs);
180
          // Decide on continuation
181
          if(files < req.filesRequested / 2 && bytes < req.bytesRequested) {
182
183
            // The client starts to dribble files at a low rate. Better finish
            // the session now, so we get a clean batch on a later mount.
Victor Kotlyar's avatar
Victor Kotlyar committed
184
            cta::log::ScopedParamContainer params(m_parent.m_lc);
185
186
            params.add("filesRequested", req.filesRequested)
                  .add("bytesRequested", req.bytesRequested)
187
188
                  .add("filesReceived", files)
                  .add("bytesReceived", bytes);
Victor Kotlyar's avatar
Victor Kotlyar committed
189
            m_parent.m_lc.log(cta::log::INFO, "Got less than half the requested work to do: triggering the end of session.");
190
191
192
            m_parent.signalEndDataMovement();
            break;
          }
193
        }
194
195
      } //end of while(1)
    }//end of try
196
    catch(const castor::tape::tapeserver::daemon::ErrorFlag&){
197
      //we end up there because a task screw up somewhere 
Victor Kotlyar's avatar
Victor Kotlyar committed
198
      m_parent.m_lc.log(cta::log::INFO,"In MigrationTaskInjector::WorkerThread::run(): a task failed, "
199
      "indicating finish of run");
200
      
201
202
      m_parent.signalEndDataMovement();
    } 
203
    catch(const cta::exception::Exception& ex){
204
205
      //we end up there because we could not talk to the client
      
Victor Kotlyar's avatar
Victor Kotlyar committed
206
      cta::log::ScopedParamContainer container( m_parent.m_lc);
207
      container.add("exception message",ex.getMessageValue());
Victor Kotlyar's avatar
Victor Kotlyar committed
208
209
      m_parent.m_lc.logBacktrace(cta::log::ERR,ex.backtrace());
      m_parent.m_lc.log(cta::log::ERR,"In MigrationTaskInjector::WorkerThread::run(): "
210
      "could not retrieve a list of file to migrate, indicating finish of run");
211
      
212
213
      m_parent.signalEndDataMovement();
    } 
214
    //-------------
Victor Kotlyar's avatar
Victor Kotlyar committed
215
    m_parent.m_lc.log(cta::log::INFO, "Finishing MigrationTaskInjector thread");
216
217
218
219
220
221
222
223
224
225
    /* We want to finish at the first lastCall we encounter.
     * But even after sending finish() to m_diskWriter and to m_tapeReader,
     * m_diskWriter might still want some more task (the threshold could be crossed),
     * so we discard everything that might still be in the queue
     */
    bool stillReading =true;
    while(stillReading) {
      Request req = m_parent.m_queue.pop();
      if (req.end) stillReading = false;
      LogContext::ScopedParam sp(m_parent.m_lc, Param("lastCall", req.lastCall));
Victor Kotlyar's avatar
Victor Kotlyar committed
226
      m_parent.m_lc.log(cta::log::INFO,"In MigrationTaskInjector::WorkerThread::run(): popping extra request");
227
    }
228
  }
David COME's avatar
David COME committed
229

230
231
232
  uint64_t MigrationTaskInjector::firstFseqToWrite() const {
    return m_firstFseqToWrite;
  }
David COME's avatar
David COME committed
233
234
235
236
} //end namespace daemon
} //end namespace tapeserver
} //end namespace tape
} //end namespace castor