MigrationTaskInjector.cpp 4.99 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
25
/******************************************************************************
 *                      MigrationTaskInjector.cpp
 *
 * 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"
26
#include "castor/tape/tapegateway/FilesToMigrateList.hpp"
27
#include "log.h"
David COME's avatar
David COME committed
28
29
30
31
32
33
34
35
36

namespace{

  castor::tape::tapegateway::FileToMigrateStruct* removeOwningList(castor::tape::tapegateway::FileToMigrateStruct* ptr){
    ptr->setFilesToMigrateList(0);
    return ptr;
  }
}

37
38
39
using castor::log::LogContext;
using castor::log::Param;

David COME's avatar
David COME committed
40
41
42
43
44
45
46
47
48
49
50
namespace castor{
namespace tape{
namespace tapeserver{
namespace daemon {


  MigrationTaskInjector::MigrationTaskInjector(MemoryManager & mm, 
          DiskThreadPoolInterface<DiskReadTaskInterface> & diskReader,
          TapeSingleThreadInterface<TapeWriteTask> & tapeWriter,client::ClientInterface& client,
          castor::log::LogContext lc):
          m_thread(*this),m_memManager(mm),m_tapeWriter(tapeWriter),
51
          m_diskReader(diskReader),m_client(client),m_lc(lc)
David COME's avatar
David COME committed
52
53
54
55
56
57
58
  {
    
  }
  
 
  /**
   * Create all the tape-read and write-disk tasks for set of files to retrieve
59
   * @param jobs to transform into tasks
David COME's avatar
David COME committed
60
61
   */
  void MigrationTaskInjector::injectBulkMigrations(const std::vector<tapegateway::FileToMigrateStruct*>& jobs){
62
63
64

    const u_signed64 blockCapacity = m_memManager.blockCapacity();
    for(std::vector<tapegateway::FileToMigrateStruct*>::const_iterator it= jobs.begin();it!=jobs.end();++it){
David COME's avatar
David COME committed
65
      const u_signed64 fileSize = (*it)->fileSize();
66
67
68
69
70
71
72
73
      LogContext::ScopedParam sp[]={
        LogContext::ScopedParam(m_lc, Param("NSFILEID", (*it)->fileid())),
        LogContext::ScopedParam(m_lc, Param("NSFILESEQNUMBER", (*it)->fseq())),
        LogContext::ScopedParam(m_lc, Param("NSFILENSHOST", (*it)->nshost())),
        LogContext::ScopedParam(m_lc, Param("NSFILEPATH", (*it)->path()))
      };
      tape::utils::suppresUnusedVariable(sp);
      m_lc.log(LOG_INFO, "Logged file to migrate");
David COME's avatar
David COME committed
74
75
76
      
      const u_signed64 neededBlock = fileSize/blockCapacity + ((fileSize%blockCapacity==0) ? 0 : 1); 
      
77
78
79
80
81
      TapeWriteTask *twt = new TapeWriteTask(neededBlock,removeOwningList((*it)->clone()),m_memManager);
      DiskReadTask *drt = new DiskReadTask(*twt,removeOwningList((*it)->clone()),neededBlock);
      
      m_tapeWriter.push(twt);
      m_diskReader.push(drt);
David COME's avatar
David COME committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    }
  }
  
  /**
   * Wait for the inner thread to finish
   */
  void MigrationTaskInjector::waitThreads(){
    m_thread.wait();
  }
  
  /**
   * Start the inner thread 
   */
  void MigrationTaskInjector::startThreads(){
    m_thread.start();
  }
98
99
100
101
102
103
  
  void MigrationTaskInjector::requestInjection(int maxFiles, int byteSizeThreshold, bool lastCall) {
    //@TODO where shall we  acquire the lock ? There of just before the push ?
    castor::tape::threading::MutexLocker ml(&m_producerProtection);
    m_queue.push(Request(maxFiles, byteSizeThreshold, lastCall));
  }
David COME's avatar
David COME committed
104
 
105
106
//------------------------------------------------------------------------------  
  void MigrationTaskInjector::WorkerThread::run(){
107
108
    _this.m_lc.pushOrReplace(Param("thread", "MigrationTaskInjector"));
    _this.m_lc.log(LOG_DEBUG, "Starting MigrationTaskInjector thread");
109
110
111
112
    while(1){
      Request req = _this.m_queue.pop();
      client::ClientProxy::RequestReport reqReport;
      std::auto_ptr<tapegateway::FilesToMigrateList> filesToMigrateList(_this.m_client.getFilesToMigrate(req.nbMaxFiles, req.byteSizeThreshold,reqReport));
David COME's avatar
David COME committed
113

114
115
116
117
118
119
120
121
122
123
124
125
126
      if(NULL==filesToMigrateList.get()){
        if (req.lastCall) {
          _this.m_lc.log(LOG_INFO,"No more file to migrate: triggering the end of session.\n");
          _this.m_tapeWriter.finish();
          _this.m_diskReader.finish();
          break;
        } else {
          _this.m_lc.log(LOG_INFO,"In MigrationTaskInjector::WorkerThread::run(): got empty list, but not last call");
        }
      } else {
        _this.injectBulkMigrations(filesToMigrateList->filesToMigrate());
      }
    }
127
    _this.m_lc.log(LOG_DEBUG, "Finishing MigrationTaskInjector thread");
128
  }
David COME's avatar
David COME committed
129
130
131
132
133
134


} //end namespace daemon
} //end namespace tapeserver
} //end namespace tape
} //end namespace castor