MigrationTaskInjector.cpp 10.3 KB
Newer Older
David COME's avatar
David COME committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/******************************************************************************
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  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 2
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * 
 *
 * @author Castor Dev team, castor-dev@cern.ch
 *****************************************************************************/

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

Victor Kotlyar's avatar
Victor Kotlyar committed
27
28
using cta::log::LogContext;
using cta::log::Param;
29

David COME's avatar
David COME committed
30
31
32
33
34
namespace castor{
namespace tape{
namespace tapeserver{
namespace daemon {

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

55
    const uint64_t blockCapacity = m_memManager.blockCapacity();
56
    for(auto it= jobs.begin();it!=jobs.end();++it){
57
      const uint64_t fileSize = (*it)->archiveFile.fileSize;
58
      LogContext::ScopedParam sp[]={
59
      LogContext::ScopedParam(m_lc, Param("fileId", (*it)->archiveFile.archiveFileID)),
60
      LogContext::ScopedParam(m_lc, Param("fSeq", (*it)->tapeFile.fSeq)),
61
      LogContext::ScopedParam(m_lc, Param("path", (*it)->srcURL))
62
      };
63
      tape::utils::suppresUnusedVariable(sp);      
David COME's avatar
David COME committed
64
      
65
      const uint64_t neededBlock = howManyBlocksNeeded(fileSize,blockCapacity);
David COME's avatar
David COME committed
66
      
67
68
69
70
71
      // 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));
72
73
74
75
      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));
76
      
77
      m_tapeWriter.push(twt.release());
78
      if (fileSize) m_diskReader.push(drt.release());
Victor Kotlyar's avatar
Victor Kotlyar committed
79
      m_lc.log(cta::log::INFO, "Created tasks for migrating a file");
80
81
      if (!fileSize)
        m_lc.log(cta::log::WARNING, "Skipped disk read task creation for zero-length file.");
David COME's avatar
David COME committed
82
    }
83
    LogContext::ScopedParam(m_lc, Param("numbnerOfFiles", jobs.size()));
Victor Kotlyar's avatar
Victor Kotlyar committed
84
    m_lc.log(cta::log::INFO, "Finished creating tasks for migrating");
David COME's avatar
David COME committed
85
86
  }
  
87
88
89
//------------------------------------------------------------------------------
//injectBulkMigrations
//------------------------------------------------------------------------------
David COME's avatar
David COME committed
90
91
92
93
  void MigrationTaskInjector::waitThreads(){
    m_thread.wait();
  }
  
94
95
96
//------------------------------------------------------------------------------
//injectBulkMigrations
//------------------------------------------------------------------------------
David COME's avatar
David COME committed
97
98
99
  void MigrationTaskInjector::startThreads(){
    m_thread.start();
  }
100
  
101
102
103
//------------------------------------------------------------------------------
//requestInjection
//------------------------------------------------------------------------------
104
  void MigrationTaskInjector::requestInjection( bool lastCall) {
105
    cta::threading::MutexLocker ml(m_producerProtection);
106
107
    m_queue.push(Request(m_maxFiles, m_maxBytes, lastCall));

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

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

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