DiskWriteTask.cpp 10.9 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
#include "serrno.h"
30

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

Eric Cano's avatar
Eric Cano committed
42
}
43

Eric Cano's avatar
Eric Cano committed
44
45
46
//------------------------------------------------------------------------------
// DiskWriteTask::execute
//------------------------------------------------------------------------------
47
bool DiskWriteTask::execute(RecallReportPacker& reporter,log::LogContext& lc,
48
    diskFile::DiskFileFactory & fileFactory, RecallWatchDog & watchdog) {
Eric Cano's avatar
Eric Cano committed
49
50
  using log::LogContext;
  using log::Param;
51
  castor::utils::Timer localTime;
52
  castor::utils::Timer totalTime(localTime);
53
  castor::utils::Timer transferTime(localTime);
54
  log::ScopedParamContainer URLcontext(lc);
55
  URLcontext.add("NSFILEID",m_retrieveJob->archiveFile.fileId)
56
            .add("path", m_retrieveJob->remotePath)
57
            .add("fSeq",m_retrieveJob->nameServerTapeFile.tapeFileLocation.fSeq);
58
59
60
61
62
  // 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
63
  try{
64
    currentErrorToCount = "";
65
66
    // Placeholder for the disk file. We will open it only
    // after getting a first correct memory block.
67
    std::unique_ptr<tape::diskFile::WriteFile> writeFile;
68
    
Eric Cano's avatar
Eric Cano committed
69
70
71
72
    int blockId  = 0;
    unsigned long checksum = Payload::zeroAdler32();
    while(1) {
      if(MemBlock* const mb = m_fifo.pop()) {
73
        m_stats.waitDataTime+=localTime.secs(castor::utils::Timer::resetCounter);
Eric Cano's avatar
Eric Cano committed
74
        AutoReleaseBlock<RecallMemoryManager> releaser(mb,m_memManager);
75
76
        if(mb->isCanceled()) {
          // If the tape side got canceled, we report nothing and count
77
          // it as a success.
78
          lc.log(LOG_DEBUG, "File transfer canceled");
79
80
          return true;
        }
81
82
        
        //will throw (thus exiting the loop) if something is wrong
83
        checkErrors(mb,blockId,lc);
84
        m_stats.checkingErrorTime += localTime.secs(castor::utils::Timer::resetCounter);
85
86
87
        // If we got that far on the first pass, it's now good enough to open
        // the disk file for writing...
        if (!writeFile.get()) {
88
          lc.log(LOG_DEBUG, "About to open disk file for writing");
89
          // Synchronise the counter with the open time counter.
90
          currentErrorToCount = "Error_diskOpenForWrite";
91
          transferTime = localTime;
92
          writeFile.reset(fileFactory.createWriteFile(m_retrieveJob->remotePath));
93
          URLcontext.add("actualURL", writeFile->URL());
94
          lc.log(LOG_INFO, "Opened disk file for writing");
95
96
          m_stats.openingTime+=localTime.secs(castor::utils::Timer::resetCounter);
        }
97
        
98
        // Write the data.
99
        currentErrorToCount = "Error_diskWrite";
100
        m_stats.dataVolume+=mb->m_payload.size();
101
        mb->m_payload.write(*writeFile);
102
        m_stats.readWriteTime+=localTime.secs(castor::utils::Timer::resetCounter);
103
        
Eric Cano's avatar
Eric Cano committed
104
        checksum = mb->m_payload.adler32(checksum);
105
        m_stats.checksumingTime+=localTime.secs(castor::utils::Timer::resetCounter);
106
        currentErrorToCount = "";
107
       
Eric Cano's avatar
Eric Cano committed
108
        blockId++;
109
      } //end if block non NULL
110
111
112
113
      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
114
        currentErrorToCount = "Error_diskCloseAfterWrite";
115
        writeFile->close();
116
        m_stats.closingTime +=localTime.secs(castor::utils::Timer::resetCounter);
117
        m_stats.filesCount++;
118
        break;
119
        currentErrorToCount = "";
120
      }
Eric Cano's avatar
Eric Cano committed
121
    } //end of while(1)
122
    logWithStat(LOG_INFO, "File successfully transfered to disk",lc);
123
124
125
126
    m_retrieveJob->transferredSize = m_stats.dataVolume;
    m_retrieveJob->transferredChecksum = cta::Checksum(cta::Checksum::CHECKSUMTYPE_ADLER32, 
      cta::ByteArray(checksum));
    reporter.reportCompletedJob(std::move(m_retrieveJob));
127
    m_stats.waitReportingTime+=localTime.secs(castor::utils::Timer::resetCounter);
128
    m_stats.transferTime = transferTime.secs();
129
    m_stats.totalTime = totalTime.secs();
130
    
131
    //everything went well, return true
Eric Cano's avatar
Eric Cano committed
132
133
134
135
    return true;
  } //end of try
  catch(const castor::exception::Exception& e){
    /*
136
137
138
139
140
     *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
141
     */
142
143
144
    
    //there might still be some blocks into m_fifo
    // We need to empty it
Eric Cano's avatar
Eric Cano committed
145
146
    releaseAllBlock();
    
147
148
149
150
151
    // Propagate the error to the watchdog
    if (currentErrorToCount.size()) {
      watchdog.addToErrorCount(currentErrorToCount);
    }
    
152
    m_stats.waitReportingTime+=localTime.secs(castor::utils::Timer::resetCounter);
153
154
155
156
157
    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());
158
159
    m_retrieveJob->failureMessage = e.getMessageValue();
    reporter.reportFailedJob(std::move(m_retrieveJob));
160

161
162
    
    //got an exception, return false
Eric Cano's avatar
Eric Cano committed
163
164
165
166
167
168
169
170
    return false;
  }
}

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

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

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

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

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

232
233
234
235
236
237
238
239
240
241
242
//------------------------------------------------------------------------------
// 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);
243
244
245
246
247
248
249
250
251
     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)
252
           .add("dataVolume", m_stats.dataVolume)
253
           .add("globalPayloadTransferSpeedMBps",
254
              m_stats.totalTime?1.0*m_stats.dataVolume/1000/1000/m_stats.totalTime:0)
255
           .add("diskPerformanceMBps",
256
              m_stats.transferTime?1.0*m_stats.dataVolume/1000/1000/m_stats.transferTime:0)
257
           .add("openRWCloseToTransferTimeRatio", 
258
              m_stats.transferTime?(m_stats.openingTime+m_stats.readWriteTime+m_stats.closingTime)/m_stats.transferTime:0.0)
259
           .add("FILEID",m_retrieveJob->archiveFile.fileId)
260
           .add("path",m_retrieveJob->remotePath);
261
262
    lc.log(level,msg);
}
263
264
}}}}