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
 *****************************************************************************/

Victor Kotlyar's avatar
Victor Kotlyar committed
24
#include "common/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"
Victor Kotlyar's avatar
Victor Kotlyar committed
28
#include "common/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
//------------------------------------------------------------------------------
Victor Kotlyar's avatar
Victor Kotlyar committed
46
bool DiskWriteTask::execute(RecallReportPacker& reporter,cta::log::LogContext&  lc,
47
    diskFile::DiskFileFactory & fileFactory, RecallWatchDog & watchdog) {
Victor Kotlyar's avatar
Victor Kotlyar committed
48
49
  using cta::log::LogContext;
  using cta::log::Param;
Victor Kotlyar's avatar
Victor Kotlyar committed
50
51
52
  cta::utils::Timer localTime;
  cta::utils::Timer totalTime(localTime);
  cta::utils::Timer transferTime(localTime);
Victor Kotlyar's avatar
Victor Kotlyar committed
53
  cta::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()) {
Victor Kotlyar's avatar
Victor Kotlyar committed
72
        m_stats.waitDataTime+=localTime.secs(cta::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.
Victor Kotlyar's avatar
Victor Kotlyar committed
77
          lc.log(cta::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);
Victor Kotlyar's avatar
Victor Kotlyar committed
83
        m_stats.checkingErrorTime += localTime.secs(cta::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()) {
Victor Kotlyar's avatar
Victor Kotlyar committed
87
          lc.log(cta::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());
Victor Kotlyar's avatar
Victor Kotlyar committed
93
          lc.log(cta::log::INFO, "Opened disk file for writing");
Victor Kotlyar's avatar
Victor Kotlyar committed
94
          m_stats.openingTime+=localTime.secs(cta::utils::Timer::resetCounter);
95
        }
96
        
97
        // Write the data.
98
        currentErrorToCount = "Error_diskWrite";
99
        m_stats.dataVolume+=mb->m_payload.size();
100
        mb->m_payload.write(*writeFile);
Victor Kotlyar's avatar
Victor Kotlyar committed
101
        m_stats.readWriteTime+=localTime.secs(cta::utils::Timer::resetCounter);
102
        
Eric Cano's avatar
Eric Cano committed
103
        checksum = mb->m_payload.adler32(checksum);
Victor Kotlyar's avatar
Victor Kotlyar committed
104
        m_stats.checksumingTime+=localTime.secs(cta::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();
Victor Kotlyar's avatar
Victor Kotlyar committed
115
        m_stats.closingTime +=localTime.secs(cta::utils::Timer::resetCounter);
116
        m_stats.filesCount++;
117
        break;
118
        currentErrorToCount = "";
119
      }
Eric Cano's avatar
Eric Cano committed
120
    } //end of while(1)
Victor Kotlyar's avatar
Victor Kotlyar committed
121
    logWithStat(cta::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));
Victor Kotlyar's avatar
Victor Kotlyar committed
130
    m_stats.waitReportingTime+=localTime.secs(cta::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
    return true;
  } //end of try
137
  catch(const cta::exception::Exception& e){
Eric Cano's avatar
Eric Cano committed
138
    /*
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);
    }
    
Victor Kotlyar's avatar
Victor Kotlyar committed
155
    m_stats.waitReportingTime+=localTime.secs(cta::utils::Timer::resetCounter);
Victor Kotlyar's avatar
Victor Kotlyar committed
156
    cta::log::ScopedParamContainer params(lc);
157
    params.add("errorMessage", e.getMessageValue());
Victor Kotlyar's avatar
Victor Kotlyar committed
158
159
    logWithStat(cta::log::ERR, "File writing to disk failed.", lc);
    lc.logBacktrace(cta::log::ERR, e.backtrace());
160
161
    m_retrieveJob->failureMessage = e.getMessageValue();
    reporter.reportFailedJob(std::move(m_retrieveJob));
162

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

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

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

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

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

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

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