DiskWriteTask.cpp 11.1 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
23
/******************************************************************************
 *
 * 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
 *****************************************************************************/

24
#include "castor/log/LogContext.hpp"
25
#include "castor/tape/tapeserver/daemon/DiskWriteTask.hpp"
26
#include "castor/tape/tapeserver/daemon/AutoReleaseBlock.hpp"
27
#include "castor/tape/tapeserver/daemon/MemBlock.hpp"
28
#include "castor/utils/Timer.hpp"
29

30
31
32
33
34
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
  
Eric Cano's avatar
Eric Cano committed
35
36
37
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
38
39
DiskWriteTask::DiskWriteTask(cta::RetrieveJob *retrieveJob, RecallMemoryManager& mm): 
m_retrieveJob(retrieveJob),m_memManager(mm){
40

Eric Cano's avatar
Eric Cano committed
41
}
42

Eric Cano's avatar
Eric Cano committed
43
44
45
//------------------------------------------------------------------------------
// DiskWriteTask::execute
//------------------------------------------------------------------------------
46
bool DiskWriteTask::execute(RecallReportPacker& reporter,log::LogContext& lc,
47
    diskFile::DiskFileFactory & fileFactory, RecallWatchDog & watchdog) {
Eric Cano's avatar
Eric Cano committed
48
49
  using log::LogContext;
  using log::Param;
50
  castor::utils::Timer localTime;
51
  castor::utils::Timer totalTime(localTime);
52
  castor::utils::Timer transferTime(localTime);
53
  log::ScopedParamContainer URLcontext(lc);
54
55
56
  URLcontext.add("archiveFileID",m_retrieveJob->retrieveRequest.archiveFileID)
            .add("dstURL", m_retrieveJob->retrieveRequest.dstURL)
            .add("fSeq",m_retrieveJob->selectedTapeFile().fSeq);
57
58
59
60
61
  // This out-of-try-catch variables allows us to record the stage of the 
  // process we're in, and to count the error if it occurs.
  // We will not record errors for an empty string. This will allow us to
  // prevent counting where error happened upstream.
  std::string currentErrorToCount = "";
Eric Cano's avatar
Eric Cano committed
62
  try{
63
    currentErrorToCount = "";
64
65
    // Placeholder for the disk file. We will open it only
    // after getting a first correct memory block.
66
    std::unique_ptr<tape::diskFile::WriteFile> writeFile;
67
    
Eric Cano's avatar
Eric Cano committed
68
69
70
71
    int blockId  = 0;
    unsigned long checksum = Payload::zeroAdler32();
    while(1) {
      if(MemBlock* const mb = m_fifo.pop()) {
72
        m_stats.waitDataTime+=localTime.secs(castor::utils::Timer::resetCounter);
Eric Cano's avatar
Eric Cano committed
73
        AutoReleaseBlock<RecallMemoryManager> releaser(mb,m_memManager);
74
75
        if(mb->isCanceled()) {
          // If the tape side got canceled, we report nothing and count
76
          // it as a success.
77
          lc.log(LOG_DEBUG, "File transfer canceled");
78
79
          return true;
        }
80
81
        
        //will throw (thus exiting the loop) if something is wrong
82
        checkErrors(mb,blockId,lc);
83
        m_stats.checkingErrorTime += localTime.secs(castor::utils::Timer::resetCounter);
84
85
86
        // If we got that far on the first pass, it's now good enough to open
        // the disk file for writing...
        if (!writeFile.get()) {
87
          lc.log(LOG_DEBUG, "About to open disk file for writing");
88
          // Synchronise the counter with the open time counter.
89
          currentErrorToCount = "Error_diskOpenForWrite";
90
          transferTime = localTime;
91
          writeFile.reset(fileFactory.createWriteFile(m_retrieveJob->retrieveRequest.dstURL));
92
          URLcontext.add("actualURL", writeFile->URL());
93
          lc.log(LOG_INFO, "Opened disk file for writing");
94
95
          m_stats.openingTime+=localTime.secs(castor::utils::Timer::resetCounter);
        }
96
        
97
        // Write the data.
98
        currentErrorToCount = "Error_diskWrite";
99
        m_stats.dataVolume+=mb->m_payload.size();
100
        mb->m_payload.write(*writeFile);
101
        m_stats.readWriteTime+=localTime.secs(castor::utils::Timer::resetCounter);
102
        
Eric Cano's avatar
Eric Cano committed
103
        checksum = mb->m_payload.adler32(checksum);
104
        m_stats.checksumingTime+=localTime.secs(castor::utils::Timer::resetCounter);
105
        currentErrorToCount = "";
106
       
Eric Cano's avatar
Eric Cano committed
107
        blockId++;
108
      } //end if block non NULL
109
110
111
112
      else { 
        //close has to be explicit, because it may throw. 
        //A close is done  in WriteFile's destructor, but it may lead to some 
        //silent data loss
113
        currentErrorToCount = "Error_diskCloseAfterWrite";
114
        writeFile->close();
115
        m_stats.closingTime +=localTime.secs(castor::utils::Timer::resetCounter);
116
        m_stats.filesCount++;
117
        break;
118
        currentErrorToCount = "";
119
      }
Eric Cano's avatar
Eric Cano committed
120
    } //end of while(1)
121
    logWithStat(LOG_INFO, "File successfully transfered to disk",lc);
122
    m_retrieveJob->transferredSize = m_stats.dataVolume;
123
    m_retrieveJob->transferredChecksumType = "ADLER32";
124
125
126
127
128
    { 
      std::stringstream cs;
      cs << std::hex << std::nouppercase << std::setfill('0') << std::setw(8) << (uint32_t)checksum;
      m_retrieveJob->transferredChecksumValue = cs.str();
    }
129
    reporter.reportCompletedJob(std::move(m_retrieveJob));
130
    m_stats.waitReportingTime+=localTime.secs(castor::utils::Timer::resetCounter);
131
    m_stats.transferTime = transferTime.secs();
132
    m_stats.totalTime = totalTime.secs();
133
    
134
    //everything went well, return true
Eric Cano's avatar
Eric Cano committed
135
136
137
138
    return true;
  } //end of try
  catch(const castor::exception::Exception& e){
    /*
139
140
141
142
143
     *We might end up there because ;
     * -- WriteFile failed 
     * -- A desynchronization between tape read and disk write
     * -- An error in tape read
     * -- An error while writing the file
Eric Cano's avatar
Eric Cano committed
144
     */
145
146
147
    
    //there might still be some blocks into m_fifo
    // We need to empty it
Eric Cano's avatar
Eric Cano committed
148
149
    releaseAllBlock();
    
150
151
152
153
154
    // Propagate the error to the watchdog
    if (currentErrorToCount.size()) {
      watchdog.addToErrorCount(currentErrorToCount);
    }
    
155
    m_stats.waitReportingTime+=localTime.secs(castor::utils::Timer::resetCounter);
156
157
158
159
160
    log::ScopedParamContainer params(lc);
    params.add("errorMessage", e.getMessageValue())
          .add("errorCode", e.code());
    logWithStat(LOG_ERR, "File writing to disk failed.", lc);
    lc.logBacktrace(LOG_ERR, e.backtrace());
161
162
    m_retrieveJob->failureMessage = e.getMessageValue();
    reporter.reportFailedJob(std::move(m_retrieveJob));
163

164
165
    
    //got an exception, return false
Eric Cano's avatar
Eric Cano committed
166
167
168
169
170
171
172
173
    return false;
  }
}

//------------------------------------------------------------------------------
// DiskWriteTask::getFreeBlock
//------------------------------------------------------------------------------
MemBlock *DiskWriteTask::getFreeBlock() { 
174
  throw castor::exception::Exception("DiskWriteTask::getFreeBlock should mot be called");
Eric Cano's avatar
Eric Cano committed
175
176
177
178
179
180
}

//------------------------------------------------------------------------------
// DiskWriteTask::pushDataBlock
//------------------------------------------------------------------------------
void DiskWriteTask::pushDataBlock(MemBlock *mb) {
181
  castor::server::MutexLocker ml(&m_producerProtection);
Eric Cano's avatar
Eric Cano committed
182
183
184
185
186
187
188
  m_fifo.push(mb);
}

//------------------------------------------------------------------------------
// DiskWriteTask::~DiskWriteTask
//------------------------------------------------------------------------------
DiskWriteTask::~DiskWriteTask() { 
189
  volatile castor::server::MutexLocker ml(&m_producerProtection); 
Eric Cano's avatar
Eric Cano committed
190
191
192
193
194
195
196
197
198
199
200
}

//------------------------------------------------------------------------------
// DiskWriteTask::releaseAllBlock
//------------------------------------------------------------------------------
void DiskWriteTask::releaseAllBlock(){
  while(1){
    if(MemBlock* mb=m_fifo.pop())
      AutoReleaseBlock<RecallMemoryManager> release(mb,m_memManager);
    else 
      break;
201
  }
Eric Cano's avatar
Eric Cano committed
202
}
203

204
205
206
207
208
//------------------------------------------------------------------------------
// checkErrors
//------------------------------------------------------------------------------  
  void DiskWriteTask::checkErrors(MemBlock* mb,int blockId,castor::log::LogContext& lc){
    using namespace castor::log;
209
    if(m_retrieveJob->retrieveRequest.archiveFileID != static_cast<unsigned int>(mb->m_fileid)
210
211
            || blockId != mb->m_fileBlock  || mb->isFailed() ){
      LogContext::ScopedParam sp[]={
212
213
214
        LogContext::ScopedParam(lc, Param("received_archiveFileID", mb->m_fileid)),
        LogContext::ScopedParam(lc, Param("expected_NSBLOCKId", blockId)),
        LogContext::ScopedParam(lc, Param("received_NSBLOCKId", mb->m_fileBlock)),
215
216
217
218
        LogContext::ScopedParam(lc, Param("failed_Status", mb->isFailed()))
      };
      tape::utils::suppresUnusedVariable(sp);
      std::string errorMsg;
Steven Murray's avatar
Steven Murray committed
219
      //int errCode;
220
221
      if(mb->isFailed()){
        errorMsg=mb->errorMsg();
222
223
224
        
        //disabled temporarily (see comment in MemBlock)
        //errCode=mb->errorCode();
225
226
      }
      else{
227
        errorMsg="Mismatch between expected and received filed or blockid";
Steven Murray's avatar
Steven Murray committed
228
        //errCode=666;
229
230
      }
      lc.log(LOG_ERR,errorMsg);
231
      throw castor::exception::Exception(errorMsg);
232
233
234
    }
  }

235
236
237
238
239
240
241
242
243
244
245
//------------------------------------------------------------------------------
// getTiming
//------------------------------------------------------------------------------  
const DiskStats DiskWriteTask::getTaskStats() const{
  return m_stats;
}
//------------------------------------------------------------------------------
// logWithStat
//------------------------------------------------------------------------------  
void DiskWriteTask::logWithStat(int level,const std::string& msg,log::LogContext& lc){
  log::ScopedParamContainer params(lc);
246
247
248
249
250
251
252
253
254
     params.add("readWriteTime", m_stats.readWriteTime)
           .add("checksumingTime",m_stats.checksumingTime)
           .add("waitDataTime",m_stats.waitDataTime)
           .add("waitReportingTime",m_stats.waitReportingTime)
           .add("checkingErrorTime",m_stats.checkingErrorTime)
           .add("openingTime",m_stats.openingTime)
           .add("closingTime",m_stats.closingTime)
           .add("transferTime", m_stats.transferTime)
           .add("totalTime", m_stats.totalTime)
255
           .add("dataVolume", m_stats.dataVolume)
256
           .add("globalPayloadTransferSpeedMBps",
257
              m_stats.totalTime?1.0*m_stats.dataVolume/1000/1000/m_stats.totalTime:0)
258
           .add("diskPerformanceMBps",
259
              m_stats.transferTime?1.0*m_stats.dataVolume/1000/1000/m_stats.transferTime:0)
260
           .add("openRWCloseToTransferTimeRatio", 
261
              m_stats.transferTime?(m_stats.openingTime+m_stats.readWriteTime+m_stats.closingTime)/m_stats.transferTime:0.0)
262
263
           .add("archiveFileID",m_retrieveJob->retrieveRequest.archiveFileID)
           .add("dstURL",m_retrieveJob->retrieveRequest.dstURL);
264
265
    lc.log(level,msg);
}
266
267
}}}}