MigrationTaskInjector.cpp 9.32 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

namespace{
30
31
32
33
34
35
36
37
  /*
   * function to set a NULL the owning FilesToMigrateList  of a FileToMigrateStruct
   * Indeed, a clone of a structure will only do a shallow copy (sic).
   * Otherwise at the second destruction the object will try to remove itself 
   * from the owning list and then boom !
   * @param ptr a pointer to an object to change
   * @return the parameter ptr 
   */
David COME's avatar
David COME committed
38
39
40
41
42
43
  castor::tape::tapegateway::FileToMigrateStruct* removeOwningList(castor::tape::tapegateway::FileToMigrateStruct* ptr){
    ptr->setFilesToMigrateList(0);
    return ptr;
  }
}

44
45
46
using castor::log::LogContext;
using castor::log::Param;

David COME's avatar
David COME committed
47
48
49
50
51
namespace castor{
namespace tape{
namespace tapeserver{
namespace daemon {

52
53
54
//------------------------------------------------------------------------------
//Constructor
//------------------------------------------------------------------------------
55
  MigrationTaskInjector::MigrationTaskInjector(MigrationMemoryManager & mm, 
56
          DiskReadThreadPool & diskReader,
57
          TapeSingleThreadInterface<TapeWriteTask> & tapeWriter,client::ClientInterface& client,
58
           uint64_t maxFiles, uint64_t byteSizeThreshold,castor::log::LogContext lc):
David COME's avatar
David COME committed
59
          m_thread(*this),m_memManager(mm),m_tapeWriter(tapeWriter),
60
          m_diskReader(diskReader),m_client(client),m_lc(lc),
61
          m_maxFiles(maxFiles),  m_maxBytes(byteSizeThreshold)
David COME's avatar
David COME committed
62
63
64
65
66
  {
    
  }
  
 
67
68
69
70
71
//------------------------------------------------------------------------------
//injectBulkMigrations
//------------------------------------------------------------------------------
  void MigrationTaskInjector::injectBulkMigrations(
  const std::vector<tapegateway::FileToMigrateStruct*>& jobs){
72
73
74

    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
75
      const u_signed64 fileSize = (*it)->fileSize();
76
      LogContext::ScopedParam sp[]={
77
78
79
80
      LogContext::ScopedParam(m_lc, Param("NSHOSTNAME", (*it)->nshost())),
      LogContext::ScopedParam(m_lc, Param("NSFILEID", (*it)->fileid())),
      LogContext::ScopedParam(m_lc, Param("fSeq", (*it)->fseq())),
      LogContext::ScopedParam(m_lc, Param("path", (*it)->path()))
81
82
      };
      tape::utils::suppresUnusedVariable(sp);
83
      
David COME's avatar
David COME committed
84
      
85
      const u_signed64 neededBlock = howManyBlocksNeeded(fileSize,blockCapacity);
David COME's avatar
David COME committed
86
      
87
      std::auto_ptr<TapeWriteTask> twt(
88
        new TapeWriteTask(neededBlock,removeOwningList((*it)->clone()),m_memManager,m_errorFlag)
89
90
      );
      std::auto_ptr<DiskReadTask> drt(
91
        new DiskReadTask(*twt,removeOwningList((*it)->clone()),neededBlock,m_errorFlag)
92
      );
93
      
94
95
      m_tapeWriter.push(twt.release());
      m_diskReader.push(drt.release());
96
      m_lc.log(LOG_INFO, "Logged file to migrate");
David COME's avatar
David COME committed
97
98
99
    }
  }
  
100
101
102
//------------------------------------------------------------------------------
//injectBulkMigrations
//------------------------------------------------------------------------------
David COME's avatar
David COME committed
103
104
105
106
  void MigrationTaskInjector::waitThreads(){
    m_thread.wait();
  }
  
107
108
109
//------------------------------------------------------------------------------
//injectBulkMigrations
//------------------------------------------------------------------------------
David COME's avatar
David COME committed
110
111
112
  void MigrationTaskInjector::startThreads(){
    m_thread.start();
  }
113
  
114
115
116
//------------------------------------------------------------------------------
//requestInjection
//------------------------------------------------------------------------------
117
  void MigrationTaskInjector::requestInjection( bool lastCall) {
118
    castor::tape::threading::MutexLocker ml(&m_producerProtection);
119
    if(!m_errorFlag) {
120
      m_queue.push(Request(m_maxFiles, m_maxBytes, lastCall));
121
    }
122
  }
123
124
125
//------------------------------------------------------------------------------
//synchronousInjection
//------------------------------------------------------------------------------ 
126
  bool MigrationTaskInjector::synchronousInjection() {
127
128
    client::ClientProxy::RequestReport reqReport;
    std::auto_ptr<tapegateway::FilesToMigrateList>
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
      filesToMigrateList;
    try {
      filesToMigrateList.reset(m_client.getFilesToMigrate(m_maxFiles, 
        m_maxBytes,reqReport));
    } catch (castor::exception::Exception & ex) {
      castor::log::ScopedParamContainer scoped(m_lc);
      scoped.add("transactionId", reqReport.transactionId)
            .add("byteSizeThreshold",m_maxBytes)
            .add("maxFiles", m_maxFiles)
            .add("message", ex.getMessageValue());
      m_lc.log(LOG_ERR, "Failed to getFiledToRecall.");
      return false;
    }
    castor::log::ScopedParamContainer scoped(m_lc); 
    scoped.add("sendRecvDuration", reqReport.sendRecvDuration)
          .add("byteSizeThreshold",m_maxBytes)
          .add("transactionId", reqReport.transactionId)
          .add("maxFiles", m_maxFiles);
147
148
149
150
151
    if(NULL == filesToMigrateList.get()) {
      m_lc.log(LOG_ERR, "No files to migrate: empty mount");
      return false;
    } else {
      std::vector<tapegateway::FileToMigrateStruct*>& jobs=filesToMigrateList->filesToMigrate();
152
      m_lastFseq = jobs.front()->fseq() -1;
153
154
155
156
      injectBulkMigrations(jobs);
      return true;
    }
  }
157
158
159
//------------------------------------------------------------------------------
//finish
//------------------------------------------------------------------------------ 
160
161
162
163
  void MigrationTaskInjector::finish(){
    castor::tape::threading::MutexLocker ml(&m_producerProtection);
    m_queue.push(Request());
  }
David COME's avatar
David COME committed
164
 
165
166
167
//------------------------------------------------------------------------------
//WorkerThread::run
//------------------------------------------------------------------------------
168
  void MigrationTaskInjector::WorkerThread::run(){
169
    m_parent.m_lc.pushOrReplace(Param("thread", "MigrationTaskInjector"));
170
    m_parent.m_lc.log(LOG_INFO, "Starting MigrationTaskInjector thread");
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    try{
      while(1){
        if(m_parent.m_errorFlag){
          throw castor::tape::exceptions::ErrorFlag();
        }
        Request req = m_parent.m_queue.pop();
        client::ClientProxy::RequestReport reqReport;
        std::auto_ptr<tapegateway::FilesToMigrateList> filesToMigrateList(m_parent.m_client.getFilesToMigrate(req.nbMaxFiles, req.byteSizeThreshold,reqReport));
        
        if(NULL==filesToMigrateList.get()){
          if (req.lastCall) {
            m_parent.m_lc.log(LOG_INFO,"No more file to migrate: triggering the end of session.\n");
            m_parent.m_tapeWriter.finish();
            m_parent.m_diskReader.finish();
185
            m_parent.m_memManager.finish();
186
187
188
189
            break;
          } else {
            m_parent.m_lc.log(LOG_INFO,"In MigrationTaskInjector::WorkerThread::run(): got empty list, but not last call");
          }
190
        } else {
191
          m_parent.injectBulkMigrations(filesToMigrateList->filesToMigrate());
192
        }
193
194
195
      } //end of while(1)
    }//end of try
    catch(const castor::tape::exceptions::ErrorFlag&){
196
197
198
199
200
201
202
203
      //we end up there because a task screw up somewhere 
      m_parent.m_lc.log(LOG_ERR,"In MigrationTaskInjector::WorkerThread::run(): a task screw up, "
      "finishing and discarding all tasks ");
      
      //first send the end signal to the threads
      m_parent.m_tapeWriter.finish();
      m_parent.m_diskReader.finish();
      
204
205
      //discard all the tasks !!
      while(m_parent.m_queue.size()>0){
206
        m_parent.m_queue.pop();
207
      }
208
209
    } // end of while(1)
    //-------------
210
    m_parent.m_lc.log(LOG_INFO, "Finishing MigrationTaskInjector thread");
211
212
213
214
215
216
217
218
219
220
221
222
    /* 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");
    }
223
  }
David COME's avatar
David COME committed
224
225
226
227
228
229


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