DiskWriteTask.cpp 11.6 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
48
    diskFile::DiskFileFactory & fileFactory, RecallWatchDog & watchdog,
    const int threadID) {
Victor Kotlyar's avatar
Victor Kotlyar committed
49
50
  using cta::log::LogContext;
  using cta::log::Param;
Victor Kotlyar's avatar
Victor Kotlyar committed
51
52
53
  cta::utils::Timer localTime;
  cta::utils::Timer totalTime(localTime);
  cta::utils::Timer transferTime(localTime);
Victor Kotlyar's avatar
Victor Kotlyar committed
54
  cta::log::ScopedParamContainer URLcontext(lc);
55
  URLcontext.add("fileId",m_retrieveJob->retrieveRequest.archiveFileID)
56
57
            .add("dstURL", m_retrieveJob->retrieveRequest.dstURL)
            .add("fSeq",m_retrieveJob->selectedTapeFile().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()) {
Victor Kotlyar's avatar
Victor Kotlyar committed
73
        m_stats.waitDataTime+=localTime.secs(cta::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.
Victor Kotlyar's avatar
Victor Kotlyar committed
78
          lc.log(cta::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);
Victor Kotlyar's avatar
Victor Kotlyar committed
84
        m_stats.checkingErrorTime += localTime.secs(cta::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()) {
Victor Kotlyar's avatar
Victor Kotlyar committed
88
          lc.log(cta::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->retrieveRequest.dstURL));
93
          URLcontext.add("actualURL", writeFile->URL());
Victor Kotlyar's avatar
Victor Kotlyar committed
94
          lc.log(cta::log::INFO, "Opened disk file for writing");
Victor Kotlyar's avatar
Victor Kotlyar committed
95
          m_stats.openingTime+=localTime.secs(cta::utils::Timer::resetCounter);
96
97
          watchdog.addParameter(cta::log::Param("stillOpenFileForThread"+
            std::to_string((long long)threadID), writeFile->URL()));
98
        }
99
        
100
        // Write the data.
101
        currentErrorToCount = "Error_diskWrite";
102
        m_stats.dataVolume+=mb->m_payload.size();
103
104
        if (mb->m_payload.size())
          mb->m_payload.write(*writeFile);
Victor Kotlyar's avatar
Victor Kotlyar committed
105
        m_stats.readWriteTime+=localTime.secs(cta::utils::Timer::resetCounter);
106
        
Eric Cano's avatar
Eric Cano committed
107
        checksum = mb->m_payload.adler32(checksum);
Victor Kotlyar's avatar
Victor Kotlyar committed
108
        m_stats.checksumingTime+=localTime.secs(cta::utils::Timer::resetCounter);
109
        currentErrorToCount = "";
110
       
Eric Cano's avatar
Eric Cano committed
111
        blockId++;
112
      } //end if block non NULL
113
114
115
116
      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
117
        currentErrorToCount = "Error_diskCloseAfterWrite";
118
119
120
        // Set the checksum on the server (actually needed only for Rados striper
        // noop in other cases).
        writeFile->setChecksum(checksum);
121
        writeFile->close();
Victor Kotlyar's avatar
Victor Kotlyar committed
122
        m_stats.closingTime +=localTime.secs(cta::utils::Timer::resetCounter);
123
        m_stats.filesCount++;
124
        break;
125
        currentErrorToCount = "";
126
      }
Eric Cano's avatar
Eric Cano committed
127
    } //end of while(1)
Victor Kotlyar's avatar
Victor Kotlyar committed
128
    logWithStat(cta::log::INFO, "File successfully transfered to disk",lc);
129
130
131
    watchdog.deleteParameter("stillOpenFileForThread"+
      std::to_string((long long)threadID));

132
    m_retrieveJob->transferredSize = m_stats.dataVolume;
133
    m_retrieveJob->transferredChecksumType = "ADLER32";
134
135
136
137
138
    { 
      std::stringstream cs;
      cs << std::hex << std::nouppercase << std::setfill('0') << std::setw(8) << (uint32_t)checksum;
      m_retrieveJob->transferredChecksumValue = cs.str();
    }
139
    reporter.reportCompletedJob(std::move(m_retrieveJob));
Victor Kotlyar's avatar
Victor Kotlyar committed
140
    m_stats.waitReportingTime+=localTime.secs(cta::utils::Timer::resetCounter);
141
    m_stats.transferTime = transferTime.secs();
142
    m_stats.totalTime = totalTime.secs();
143
    
144
    //everything went well, return true
Eric Cano's avatar
Eric Cano committed
145
146
    return true;
  } //end of try
147
  catch(const cta::exception::Exception& e){
Eric Cano's avatar
Eric Cano committed
148
    /*
149
150
151
152
153
     *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
154
     */
155
156
157
    
    //there might still be some blocks into m_fifo
    // We need to empty it
Eric Cano's avatar
Eric Cano committed
158
159
    releaseAllBlock();
    
160
161
162
163
164
    // Propagate the error to the watchdog
    if (currentErrorToCount.size()) {
      watchdog.addToErrorCount(currentErrorToCount);
    }
    
Victor Kotlyar's avatar
Victor Kotlyar committed
165
    m_stats.waitReportingTime+=localTime.secs(cta::utils::Timer::resetCounter);
Victor Kotlyar's avatar
Victor Kotlyar committed
166
    cta::log::ScopedParamContainer params(lc);
167
    params.add("errorMessage", e.getMessageValue());
Victor Kotlyar's avatar
Victor Kotlyar committed
168
169
    logWithStat(cta::log::ERR, "File writing to disk failed.", lc);
    lc.logBacktrace(cta::log::ERR, e.backtrace());
170
171
    m_retrieveJob->failureMessage = e.getMessageValue();
    reporter.reportFailedJob(std::move(m_retrieveJob));
172

173
174
175
    watchdog.deleteParameter("stillOpenFileForThread"+
      std::to_string((long long)threadID));

176
    //got an exception, return false
Eric Cano's avatar
Eric Cano committed
177
178
179
180
181
182
183
184
    return false;
  }
}

//------------------------------------------------------------------------------
// DiskWriteTask::getFreeBlock
//------------------------------------------------------------------------------
MemBlock *DiskWriteTask::getFreeBlock() { 
185
  throw cta::exception::Exception("DiskWriteTask::getFreeBlock should mot be called");
Eric Cano's avatar
Eric Cano committed
186
187
188
189
190
191
}

//------------------------------------------------------------------------------
// DiskWriteTask::pushDataBlock
//------------------------------------------------------------------------------
void DiskWriteTask::pushDataBlock(MemBlock *mb) {
192
  cta::threading::MutexLocker ml(m_producerProtection);
Eric Cano's avatar
Eric Cano committed
193
194
195
196
197
198
199
  m_fifo.push(mb);
}

//------------------------------------------------------------------------------
// DiskWriteTask::~DiskWriteTask
//------------------------------------------------------------------------------
DiskWriteTask::~DiskWriteTask() { 
200
  volatile cta::threading::MutexLocker ml(m_producerProtection); 
Eric Cano's avatar
Eric Cano committed
201
202
203
204
205
206
207
208
209
210
211
}

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

215
216
217
//------------------------------------------------------------------------------
// checkErrors
//------------------------------------------------------------------------------  
Victor Kotlyar's avatar
Victor Kotlyar committed
218
219
  void DiskWriteTask::checkErrors(MemBlock* mb,int blockId,cta::log::LogContext&  lc){
    using namespace cta::log;
220
    if(m_retrieveJob->retrieveRequest.archiveFileID != mb->m_fileid
221
222
            || blockId != mb->m_fileBlock  || mb->isFailed() ){
      LogContext::ScopedParam sp[]={
223
224
225
        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)),
226
227
228
229
        LogContext::ScopedParam(lc, Param("failed_Status", mb->isFailed()))
      };
      tape::utils::suppresUnusedVariable(sp);
      std::string errorMsg;
Steven Murray's avatar
Steven Murray committed
230
      //int errCode;
231
232
      if(mb->isFailed()){
        errorMsg=mb->errorMsg();
233
234
235
        
        //disabled temporarily (see comment in MemBlock)
        //errCode=mb->errorCode();
236
237
      }
      else{
238
        errorMsg="Mismatch between expected and received filed or blockid";
Steven Murray's avatar
Steven Murray committed
239
        //errCode=666;
240
      }
Victor Kotlyar's avatar
Victor Kotlyar committed
241
      lc.log(cta::log::ERR,errorMsg);
242
      throw cta::exception::Exception(errorMsg);
243
244
245
    }
  }

246
247
248
249
250
251
252
253
254
//------------------------------------------------------------------------------
// getTiming
//------------------------------------------------------------------------------  
const DiskStats DiskWriteTask::getTaskStats() const{
  return m_stats;
}
//------------------------------------------------------------------------------
// logWithStat
//------------------------------------------------------------------------------  
Victor Kotlyar's avatar
Victor Kotlyar committed
255
256
void DiskWriteTask::logWithStat(int level,const std::string& msg,cta::log::LogContext&  lc){
  cta::log::ScopedParamContainer params(lc);
257
258
259
260
261
262
263
264
265
     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)
266
           .add("dataVolume", m_stats.dataVolume)
267
           .add("globalPayloadTransferSpeedMBps",
268
              m_stats.totalTime?1.0*m_stats.dataVolume/1000/1000/m_stats.totalTime:0)
269
           .add("diskPerformanceMBps",
270
              m_stats.transferTime?1.0*m_stats.dataVolume/1000/1000/m_stats.transferTime:0)
271
           .add("openRWCloseToTransferTimeRatio", 
272
              m_stats.transferTime?(m_stats.openingTime+m_stats.readWriteTime+m_stats.closingTime)/m_stats.transferTime:0.0)
273
           .add("fileId",m_retrieveJob->retrieveRequest.archiveFileID)
274
           .add("dstURL",m_retrieveJob->retrieveRequest.dstURL);
275
276
    lc.log(level,msg);
}
277
278
}}}}