DiskWriteTask.cpp 11.7 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
  m_stats.dstURL = m_retrieveJob->retrieveRequest.dstURL;
  m_stats.fileId = m_retrieveJob->retrieveRequest.archiveFileID;
60
61
62
63
64
  // 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
65
  try{
66
    currentErrorToCount = "";
67
68
    // Placeholder for the disk file. We will open it only
    // after getting a first correct memory block.
69
    std::unique_ptr<tape::diskFile::WriteFile> writeFile;
70
    
Eric Cano's avatar
Eric Cano committed
71
72
73
74
    int blockId  = 0;
    unsigned long checksum = Payload::zeroAdler32();
    while(1) {
      if(MemBlock* const mb = m_fifo.pop()) {
Victor Kotlyar's avatar
Victor Kotlyar committed
75
        m_stats.waitDataTime+=localTime.secs(cta::utils::Timer::resetCounter);
Eric Cano's avatar
Eric Cano committed
76
        AutoReleaseBlock<RecallMemoryManager> releaser(mb,m_memManager);
77
78
        if(mb->isCanceled()) {
          // If the tape side got canceled, we report nothing and count
79
          // it as a success.
Victor Kotlyar's avatar
Victor Kotlyar committed
80
          lc.log(cta::log::DEBUG, "File transfer canceled");
81
82
          return true;
        }
83
84
        
        //will throw (thus exiting the loop) if something is wrong
85
        checkErrors(mb,blockId,lc);
Victor Kotlyar's avatar
Victor Kotlyar committed
86
        m_stats.checkingErrorTime += localTime.secs(cta::utils::Timer::resetCounter);
87
88
89
        // 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
90
          lc.log(cta::log::DEBUG, "About to open disk file for writing");
91
          // Synchronise the counter with the open time counter.
92
          currentErrorToCount = "Error_diskOpenForWrite";
93
          transferTime = localTime;
94
          writeFile.reset(fileFactory.createWriteFile(m_retrieveJob->retrieveRequest.dstURL));
95
          URLcontext.add("actualURL", writeFile->URL());
Victor Kotlyar's avatar
Victor Kotlyar committed
96
          lc.log(cta::log::INFO, "Opened disk file for writing");
Victor Kotlyar's avatar
Victor Kotlyar committed
97
          m_stats.openingTime+=localTime.secs(cta::utils::Timer::resetCounter);
98
99
          watchdog.addParameter(cta::log::Param("stillOpenFileForThread"+
            std::to_string((long long)threadID), writeFile->URL()));
100
        }
101
        
102
        // Write the data.
103
        currentErrorToCount = "Error_diskWrite";
104
        m_stats.dataVolume+=mb->m_payload.size();
105
106
        if (mb->m_payload.size())
          mb->m_payload.write(*writeFile);
Victor Kotlyar's avatar
Victor Kotlyar committed
107
        m_stats.readWriteTime+=localTime.secs(cta::utils::Timer::resetCounter);
108
        
Eric Cano's avatar
Eric Cano committed
109
        checksum = mb->m_payload.adler32(checksum);
Victor Kotlyar's avatar
Victor Kotlyar committed
110
        m_stats.checksumingTime+=localTime.secs(cta::utils::Timer::resetCounter);
111
        currentErrorToCount = "";
112
       
Eric Cano's avatar
Eric Cano committed
113
        blockId++;
114
      } //end if block non NULL
115
116
117
118
      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
119
        currentErrorToCount = "Error_diskCloseAfterWrite";
120
121
122
        // Set the checksum on the server (actually needed only for Rados striper
        // noop in other cases).
        writeFile->setChecksum(checksum);
123
        writeFile->close();
Victor Kotlyar's avatar
Victor Kotlyar committed
124
        m_stats.closingTime +=localTime.secs(cta::utils::Timer::resetCounter);
125
        m_stats.filesCount++;
126
        break;
127
        currentErrorToCount = "";
128
      }
Eric Cano's avatar
Eric Cano committed
129
    } //end of while(1)
130
    m_retrieveJob->transferredSize = m_stats.dataVolume;
131
    m_retrieveJob->transferredChecksumType = "ADLER32";
132
133
134
135
136
    { 
      std::stringstream cs;
      cs << std::hex << std::nouppercase << std::setfill('0') << std::setw(8) << (uint32_t)checksum;
      m_retrieveJob->transferredChecksumValue = cs.str();
    }
137
    reporter.reportCompletedJob(std::move(m_retrieveJob));
Victor Kotlyar's avatar
Victor Kotlyar committed
138
    m_stats.waitReportingTime+=localTime.secs(cta::utils::Timer::resetCounter);
139
    m_stats.transferTime = transferTime.secs();
140
    m_stats.totalTime = totalTime.secs();
141
142
143
    logWithStat(cta::log::INFO, "File successfully transfered to disk",lc);
    watchdog.deleteParameter("stillOpenFileForThread"+
      std::to_string((long long)threadID));
144
    
145
    //everything went well, return true
Eric Cano's avatar
Eric Cano committed
146
147
    return true;
  } //end of try
148
  catch(const cta::exception::Exception& e){
Eric Cano's avatar
Eric Cano committed
149
    /*
150
151
152
153
154
     *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
155
     */
156
157
158
    
    //there might still be some blocks into m_fifo
    // We need to empty it
Eric Cano's avatar
Eric Cano committed
159
160
    releaseAllBlock();
    
161
162
163
164
165
    // Propagate the error to the watchdog
    if (currentErrorToCount.size()) {
      watchdog.addToErrorCount(currentErrorToCount);
    }
    
Victor Kotlyar's avatar
Victor Kotlyar committed
166
    m_stats.waitReportingTime+=localTime.secs(cta::utils::Timer::resetCounter);
Victor Kotlyar's avatar
Victor Kotlyar committed
167
    cta::log::ScopedParamContainer params(lc);
168
    params.add("errorMessage", e.getMessageValue());
Victor Kotlyar's avatar
Victor Kotlyar committed
169
170
    logWithStat(cta::log::ERR, "File writing to disk failed.", lc);
    lc.logBacktrace(cta::log::ERR, e.backtrace());
171
172
    m_retrieveJob->failureMessage = e.getMessageValue();
    reporter.reportFailedJob(std::move(m_retrieveJob));
173

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

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

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

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

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

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

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

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