RecallTaskInjector.cpp 9.89 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/****************************************************************************** 
 *
 * 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
 *****************************************************************************/
23
#include "castor/tape/tapeserver/daemon/RecallTaskInjector.hpp"
David COME's avatar
David COME committed
24
#include "castor/log/LogContext.hpp"
25
#include "castor/tape/tapeserver/utils/suppressUnusedVariable.hpp"
26
27
#include "castor/tape/tapeserver/daemon/DiskWriteThreadPool.hpp"
#include "castor/tape/tapeserver/daemon/TapeReadTask.hpp"
28
#include "castor/tape/tapeserver/daemon/TapeReadSingleThread.hpp"
29
#include "castor/tape/tapeserver/daemon/VolumeInfo.hpp"
30
31
32
#include "log.h"
#include "scheduler/RetrieveJob.hpp"

33
34
#include <stdint.h>

David COME's avatar
David COME committed
35
36
37
using castor::log::LogContext;
using castor::log::Param;

38
39
40
41
42
namespace castor{
namespace tape{
namespace tapeserver{
namespace daemon {
  
43
RecallTaskInjector::RecallTaskInjector(RecallMemoryManager & mm, 
44
        TapeSingleThreadInterface<TapeReadTask> & tapeReader,
45
        DiskWriteThreadPool & diskWriter,
46
        cta::RetrieveMount &retrieveMount,
47
        uint64_t maxFiles, uint64_t byteSizeThreshold,castor::log::LogContext lc) : 
48
49
        m_thread(*this),m_memManager(mm),
        m_tapeReader(tapeReader),m_diskWriter(diskWriter),
Daniele Kruse's avatar
Daniele Kruse committed
50
        m_retrieveMount(retrieveMount),m_lc(lc),m_maxFiles(maxFiles),m_maxBytes(byteSizeThreshold)
51
{}
52
//------------------------------------------------------------------------------
53
54
55
56
57
//destructor
//------------------------------------------------------------------------------
RecallTaskInjector::~RecallTaskInjector(){
}
//------------------------------------------------------------------------------
58
59
//finish
//------------------------------------------------------------------------------
60

61
void RecallTaskInjector::finish(){
62
  castor::server::MutexLocker ml(&m_producerProtection);
63
64
  m_queue.push(Request());
}
65
66
67
//------------------------------------------------------------------------------
//requestInjection
//------------------------------------------------------------------------------
68
void RecallTaskInjector::requestInjection(bool lastCall) {
69
  //@TODO where shall we  acquire the lock ? There of just before the push ?
70
  castor::server::MutexLocker ml(&m_producerProtection);
71
  m_queue.push(Request(m_maxFiles, m_maxBytes, lastCall));
72
}
73
74
75
//------------------------------------------------------------------------------
//waitThreads
//------------------------------------------------------------------------------
76
77
78
void RecallTaskInjector::waitThreads() {
  m_thread.wait();
}
79
80
81
//------------------------------------------------------------------------------
//startThreads
//------------------------------------------------------------------------------
82
83
84
void RecallTaskInjector::startThreads() {
  m_thread.start();
}
85
86
87
//------------------------------------------------------------------------------
//injectBulkRecalls
//------------------------------------------------------------------------------
88
89
void RecallTaskInjector::injectBulkRecalls(const std::vector<cta::RetrieveJob *>& jobs) {
  for (auto it = jobs.begin(); it != jobs.end(); ++it) {
90
    
91
    (*it)->positioningMethod=cta::PositioningMethod::ByBlock;
92
93

    LogContext::ScopedParam sp[]={
94
      LogContext::ScopedParam(m_lc, Param("NSHOSTNAME", (*it)->archiveFile.nsHostName)),
95
      LogContext::ScopedParam(m_lc, Param("NSFILEID", (*it)->archiveFile.fileId)),
96
97
      LogContext::ScopedParam(m_lc, Param("fSeq", (*it)->nameServerTapeFile.tapeFileLocation.fSeq)),
      LogContext::ScopedParam(m_lc, Param("blockID", (*it)->nameServerTapeFile.tapeFileLocation.blockId)),
98
      LogContext::ScopedParam(m_lc, Param("path", (*it)->remotePath))
99
    };
100
    tape::utils::suppresUnusedVariable(sp);
David COME's avatar
David COME committed
101
    
102
    m_lc.log(LOG_INFO, "Recall task created");
David COME's avatar
David COME committed
103
    
104
105
    DiskWriteTask * dwt = new DiskWriteTask(*it, m_memManager);
    TapeReadTask * trt = new TapeReadTask(*it, *dwt, m_memManager);
106
    
107
108
    m_diskWriter.push(dwt);
    m_tapeReader.push(trt);
109
    m_lc.log(LOG_INFO, "Created tasks for recalling a file");
110
  }
David COME's avatar
David COME committed
111
  LogContext::ScopedParam sp03(m_lc, Param("nbFile", jobs.size()));
112
  m_lc.log(LOG_INFO, "Finished processing batch of recall tasks from client");
113
}
114
115
116
//------------------------------------------------------------------------------
//synchronousInjection
//------------------------------------------------------------------------------
117
bool RecallTaskInjector::synchronousInjection()
118
{
119
120
  std::vector<cta::RetrieveJob *> jobs;
  
121
  try {
122
123
124
125
126
127
    uint64_t files=0;
    uint64_t bytes=0;
    while(files<=m_maxFiles && bytes<=m_maxBytes) {
      std::unique_ptr<cta::RetrieveJob> job=m_retrieveMount.getNextJob();
      if(!job.get()) break;
      files++;
128
      bytes+=job->archiveFile.size;
129
      jobs.push_back(job.release());
130
    }
131
132
  } catch (castor::exception::Exception & ex) {
    castor::log::ScopedParamContainer scoped(m_lc);
133
    scoped.add("transactionId", m_retrieveMount.getMountTransactionId())
134
135
136
          .add("byteSizeThreshold",m_maxBytes)
          .add("maxFiles", m_maxFiles)
          .add("message", ex.getMessageValue());
137
    m_lc.log(LOG_ERR, "Failed to getFilesToRecall");
138
139
    return false;
  }
140
  castor::log::ScopedParamContainer scoped(m_lc); 
141
  scoped.add("byteSizeThreshold",m_maxBytes)
142
        .add("maxFiles", m_maxFiles);
143
  if(jobs.empty()) { 
144
    m_lc.log(LOG_ERR, "No files to recall: empty mount");
145
146
147
148
149
150
151
    return false;
  }
  else {
    injectBulkRecalls(jobs);
    return true;
  }
}
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//------------------------------------------------------------------------------
//signalEndDataMovement
//------------------------------------------------------------------------------
  void RecallTaskInjector::signalEndDataMovement(){        
    //first send the end signal to the threads
    m_tapeReader.finish();
    m_diskWriter.finish();
  }
//------------------------------------------------------------------------------
//deleteAllTasks
//------------------------------------------------------------------------------
  void RecallTaskInjector::deleteAllTasks(){
    //discard all the tasks !!
    while(m_queue.size()>0){
      m_queue.pop();
    }
  }
  
170
171
172
//------------------------------------------------------------------------------
//WorkerThread::run
//------------------------------------------------------------------------------
173
174
void RecallTaskInjector::WorkerThread::run()
{
David COME's avatar
David COME committed
175
  using castor::log::LogContext;
176
  m_parent.m_lc.pushOrReplace(Param("thread", "RecallTaskInjector"));
177
  m_parent.m_lc.log(LOG_DEBUG, "Starting RecallTaskInjector thread");
David COME's avatar
David COME committed
178
  
179
180
181
  try{
    while (1) {
      Request req = m_parent.m_queue.pop();
182
183
184
185
186
      if (req.end) {
        m_parent.m_lc.log(LOG_INFO,"Received a end notification from tape thread: triggering the end of session.");
        m_parent.signalEndDataMovement();
        break;
      }
187
      m_parent.m_lc.log(LOG_DEBUG,"RecallJobInjector:run: about to call client interface");
188
189
190
191
192
193
194
195
      std::vector<cta::RetrieveJob *> jobs;
      
      uint64_t files=0;
      uint64_t bytes=0;
      while(files<=req.filesRequested && bytes<=req.bytesRequested) {
        std::unique_ptr<cta::RetrieveJob> job=m_parent.m_retrieveMount.getNextJob();
        if(!job.get()) break;
        files++;
196
        bytes+=job->archiveFile.size;
197
        jobs.push_back(job.release());
198
      }
199
      
200
      LogContext::ScopedParam sp01(m_parent.m_lc, Param("transactionId", m_parent.m_retrieveMount.getMountTransactionId()));
201
      
202
      if (jobs.empty()) {
203
        if (req.lastCall) {
204
          m_parent.m_lc.log(LOG_INFO,"No more file to recall: triggering the end of session.");
205
206
207
          m_parent.signalEndDataMovement();
          break;
        } else {
208
          m_parent.m_lc.log(LOG_DEBUG,"In RecallJobInjector::WorkerThread::run(): got empty list, but not last call. NoOp.");
209
        }
210
      } else {
211
        m_parent.injectBulkRecalls(jobs);
212
      }
213
214
215
216
217
218
219
220
221
222
223
224
225
    } // end of while(1)
  } //end of try
  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 RecallJobInjector::WorkerThread::run(): "
      "could not retrieve a list of file to recall. End of session");
      
      m_parent.signalEndDataMovement();
      m_parent.deleteAllTasks();
226
    }
227
  //-------------
228
  m_parent.m_lc.log(LOG_DEBUG, "Finishing RecallTaskInjector thread");
229
230
231
232
233
  /* 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
   */
234
  if(m_parent.m_queue.size()>0) {
235
236
    bool stillReading =true;
    while(stillReading) {
237
      Request req = m_parent.m_queue.pop();
238
239
240
      if (req.end){
        stillReading = false;
      }
241
      LogContext::ScopedParam sp(m_parent.m_lc, Param("lastCall", req.lastCall));
242
      m_parent.m_lc.log(LOG_DEBUG,"In RecallJobInjector::WorkerThread::run(): popping extra request");
243
    }
244
  }
245
246
247
248
249
250
}

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