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

27
28
29
using castor::log::LogContext;
using castor::log::Param;

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,
41
           uint64_t maxFiles, uint64_t byteSizeThreshold,castor::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(const std::vector<cta::ArchiveJob *>& jobs){
54
55

    const u_signed64 blockCapacity = m_memManager.blockCapacity();
56
57
    for(auto it= jobs.begin();it!=jobs.end();++it){
      const u_signed64 fileSize = (*it)->archiveFile.size;
58
      LogContext::ScopedParam sp[]={
59
60
      LogContext::ScopedParam(m_lc, Param("NSHOSTNAME", (*it)->archiveFile.nsHostName)),
      LogContext::ScopedParam(m_lc, Param("NSFILEID", (*it)->archiveFile.fileId)),
61
      LogContext::ScopedParam(m_lc, Param("fSeq", (*it)->nameServerTapeFile.tapeFileLocation.fSeq)),
62
      LogContext::ScopedParam(m_lc, Param("path", (*it)->remotePathAndStatus.path.getRaw()))
63
      };
64
      tape::utils::suppresUnusedVariable(sp);      
David COME's avatar
David COME committed
65
      
66
      const u_signed64 neededBlock = howManyBlocksNeeded(fileSize,blockCapacity);
David COME's avatar
David COME committed
67
      
68
69
      std::unique_ptr<TapeWriteTask> twt(new TapeWriteTask(neededBlock, *it, m_memManager, m_errorFlag));
      std::unique_ptr<DiskReadTask> drt(new DiskReadTask(*twt, *it, neededBlock, m_errorFlag));
70
      
71
72
      m_tapeWriter.push(twt.release());
      m_diskReader.push(drt.release());
73
      m_lc.log(LOG_INFO, "Created tasks for migrating a file");
David COME's avatar
David COME committed
74
    }
75
76
    LogContext::ScopedParam(m_lc, Param("numbnerOfFiles", jobs.size()));
    m_lc.log(LOG_INFO, "Finished creating tasks for migrating");
David COME's avatar
David COME committed
77
78
  }
  
79
80
81
//------------------------------------------------------------------------------
//injectBulkMigrations
//------------------------------------------------------------------------------
David COME's avatar
David COME committed
82
83
84
85
  void MigrationTaskInjector::waitThreads(){
    m_thread.wait();
  }
  
86
87
88
//------------------------------------------------------------------------------
//injectBulkMigrations
//------------------------------------------------------------------------------
David COME's avatar
David COME committed
89
90
91
  void MigrationTaskInjector::startThreads(){
    m_thread.start();
  }
92
  
93
94
95
//------------------------------------------------------------------------------
//requestInjection
//------------------------------------------------------------------------------
96
  void MigrationTaskInjector::requestInjection( bool lastCall) {
97
    castor::server::MutexLocker ml(&m_producerProtection);
98
99
    m_queue.push(Request(m_maxFiles, m_maxBytes, lastCall));

100
  }
101
102
103
//------------------------------------------------------------------------------
//synchronousInjection
//------------------------------------------------------------------------------ 
104
  bool MigrationTaskInjector::synchronousInjection() {
105
    std::vector<cta::ArchiveJob *> jobs;
106
    try {
107
108
109
110
111
112
113
      uint64_t files=0;
      uint64_t bytes=0;
      while(files<=m_maxFiles && bytes<=m_maxBytes) {
        std::unique_ptr<cta::ArchiveJob> job=m_archiveMount.getNextJob();
        if(!job.get()) break;
        files++;
        bytes+=job->archiveFile.size;
114
        jobs.push_back(job.release());
115
      }
116
117
    } catch (castor::exception::Exception & ex) {
      castor::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());
122
      m_lc.log(LOG_ERR, "Failed to getFilesToMigrate");
123
124
125
      return false;
    }
    castor::log::ScopedParamContainer scoped(m_lc); 
126
    scoped.add("byteSizeThreshold",m_maxBytes)
127
          .add("maxFiles", m_maxFiles);
128
    if(jobs.empty()) {
129
130
131
      m_lc.log(LOG_ERR, "No files to migrate: empty mount");
      return false;
    } else {
132
      m_firstFseqToWrite = jobs.front()->nameServerTapeFile.tapeFileLocation.fSeq;
133
134
135
136
      injectBulkMigrations(jobs);
      return true;
    }
  }
137
138
139
//------------------------------------------------------------------------------
//finish
//------------------------------------------------------------------------------ 
140
  void MigrationTaskInjector::finish(){
141
    castor::server::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"));
159
    m_parent.m_lc.log(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
167
168
169
170
171
172
173
174
        std::vector<cta::ArchiveJob *> jobs;
        
        uint64_t files=0;
        uint64_t bytes=0;
        while(files<=req.filesRequested && bytes<=req.bytesRequested) {
          std::unique_ptr<cta::ArchiveJob> job=m_parent.m_archiveMount.getNextJob();
          if(!job.get()) break;
          files++;
          bytes+=job->archiveFile.size;
175
          jobs.push_back(job.release());
176
        }
177

178
        if(jobs.empty()){
179
          if (req.lastCall) {
180
            m_parent.m_lc.log(LOG_INFO,"No more file to migrate: triggering the end of session.");
181
            m_parent.signalEndDataMovement();
182
183
184
185
            break;
          } else {
            m_parent.m_lc.log(LOG_INFO,"In MigrationTaskInjector::WorkerThread::run(): got empty list, but not last call");
          }
186
        } else {
187
          
188
          // Inject the tasks
189
          m_parent.injectBulkMigrations(jobs);
190
          // Decide on continuation
191
          if(files < req.filesRequested / 2 && bytes < req.bytesRequested) {
192
193
194
            // 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.
            log::ScopedParamContainer params(m_parent.m_lc);
195
196
            params.add("filesRequested", req.filesRequested)
                  .add("bytesRequested", req.bytesRequested)
197
198
                  .add("filesReceived", files)
                  .add("bytesReceived", bytes);
199
200
201
202
            m_parent.m_lc.log(LOG_INFO, "Got less than half the requested work to do: triggering the end of session.");
            m_parent.signalEndDataMovement();
            break;
          }
203
        }
204
205
      } //end of while(1)
    }//end of try
206
    catch(const castor::tape::tapeserver::daemon::ErrorFlag&){
207
      //we end up there because a task screw up somewhere 
208
      m_parent.m_lc.log(LOG_INFO,"In MigrationTaskInjector::WorkerThread::run(): a task failed, "
209
      "indicating finish of run");
210
      
211
212
213
214
215
216
217
218
219
220
      m_parent.signalEndDataMovement();
    } 
    catch(const castor::exception::Exception& ex){
      //we end up there because we could not talk to the client
      
      log::ScopedParamContainer container( m_parent.m_lc);
      container.add("exception code",ex.code())
               .add("exception message",ex.getMessageValue());
      m_parent.m_lc.logBacktrace(LOG_ERR,ex.backtrace());
      m_parent.m_lc.log(LOG_ERR,"In MigrationTaskInjector::WorkerThread::run(): "
221
      "could not retrieve a list of file to migrate, indicating finish of run");
222
      
223
224
      m_parent.signalEndDataMovement();
    } 
225
    //-------------
226
    m_parent.m_lc.log(LOG_INFO, "Finishing MigrationTaskInjector thread");
227
228
229
230
231
232
233
234
235
236
237
238
    /* 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));
      m_parent.m_lc.log(LOG_INFO,"In MigrationTaskInjector::WorkerThread::run(): popping extra request");
    }
239
  }
David COME's avatar
David COME committed
240

241
242
243
  uint64_t MigrationTaskInjector::firstFseqToWrite() const {
    return m_firstFseqToWrite;
  }
David COME's avatar
David COME committed
244
245
246
247
} //end namespace daemon
} //end namespace tapeserver
} //end namespace tape
} //end namespace castor