RecallReportPacker.cpp 19.5 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
24
/******************************************************************************
 *
 * 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
 *****************************************************************************/

#include "castor/tape/tapeserver/daemon/RecallReportPacker.hpp"
25
#include "castor/tape/tapeserver/daemon/TaskWatchDog.hpp"
Victor Kotlyar's avatar
Victor Kotlyar committed
26
#include "common/log/Logger.hpp"
27
#include "common/utils/utils.hpp"
28
#include "objectstore/Backend.hpp"
29

30
#include <signal.h>
31
#include <iostream>
32
#include <cxxabi.h>
33

34
namespace{
35
  struct failedReportRecallResult : public cta::exception::Exception{
36
37
38
39
    failedReportRecallResult(const std::string& s): Exception(s){}
  };
}

Victor Kotlyar's avatar
Victor Kotlyar committed
40
41
using cta::log::LogContext;
using cta::log::Param;
David COME's avatar
David COME committed
42

43
44
45
46
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
47
48
49
//------------------------------------------------------------------------------
//Constructor
//------------------------------------------------------------------------------
Victor Kotlyar's avatar
Victor Kotlyar committed
50
RecallReportPacker::RecallReportPacker(cta::RetrieveMount *retrieveMount, cta::log::LogContext lc):
51
52
53
54
  ReportPackerInterface<detail::Recall>(lc),
  m_workerThread(*this), m_errorHappened(false), m_retrieveMount(retrieveMount),
  m_tapeThreadComplete(false), m_diskThreadComplete(false)
{
55
  
56
}
57
58
59
//------------------------------------------------------------------------------
//Destructor
//------------------------------------------------------------------------------
60
RecallReportPacker::~RecallReportPacker(){
61
  cta::threading::MutexLocker ml(m_producterProtection);
62
}
63
64
65
//------------------------------------------------------------------------------
//reportCompletedJob
//------------------------------------------------------------------------------
66
67
void RecallReportPacker::reportCompletedJob(std::unique_ptr<cta::RetrieveJob> successfulRetrieveJob){
  std::unique_ptr<Report> rep(new ReportSuccessful(std::move(successfulRetrieveJob)));
68
  cta::threading::MutexLocker ml(m_producterProtection);
69
70
  m_fifo.push(rep.release());
}
71
72
73
//------------------------------------------------------------------------------
//reportFailedJob
//------------------------------------------------------------------------------  
74
75
void RecallReportPacker::reportFailedJob(std::unique_ptr<cta::RetrieveJob> failedRetrieveJob, const cta::exception::Exception & ex){
  std::string failureLog = cta::utils::getCurrentLocalTime() + " " + cta::utils::getShortHostname() +
76
      " " + ex.getMessageValue();
77
  std::unique_ptr<Report> rep(new ReportError(std::move(failedRetrieveJob), failureLog));
78
  cta::threading::MutexLocker ml(m_producterProtection);
79
80
  m_fifo.push(rep.release());
}
81
82
83
//------------------------------------------------------------------------------
//reportEndOfSession
//------------------------------------------------------------------------------
84
void RecallReportPacker::reportEndOfSession(){
85
  cta::threading::MutexLocker ml(m_producterProtection);
86
87
  m_fifo.push(new ReportEndofSession());
}
88
89

//------------------------------------------------------------------------------
Daniele Kruse's avatar
Daniele Kruse committed
90
//reportDriveStatus
91
//------------------------------------------------------------------------------
92
void RecallReportPacker::reportDriveStatus(cta::common::dataStructures::DriveStatus status) {
93
  cta::threading::MutexLocker ml(m_producterProtection);
Daniele Kruse's avatar
Daniele Kruse committed
94
  m_fifo.push(new ReportDriveStatus(status));
95
96
}

97
  
98
99
100
//------------------------------------------------------------------------------
//reportEndOfSessionWithErrors
//------------------------------------------------------------------------------
101
void RecallReportPacker::reportEndOfSessionWithErrors(const std::string msg,int error_code){
102
  cta::threading::MutexLocker ml(m_producterProtection);
103
104
  m_fifo.push(new ReportEndofSessionWithErrors(msg,error_code));
}
105

106
107
108
109
110
  
//------------------------------------------------------------------------------
//reportTestGoingToEnd
//------------------------------------------------------------------------------
void RecallReportPacker::reportTestGoingToEnd(){
111
  cta::threading::MutexLocker ml(m_producterProtection);
112
113
114
  m_fifo.push(new ReportTestGoingToEnd());
}

115
116
//------------------------------------------------------------------------------
//ReportSuccessful::execute
117
//------------------------------------------------------------------------------
118
void RecallReportPacker::ReportSuccessful::execute(RecallReportPacker& parent){
119
120
121
122
123
124
125
126
127
  try{
    m_successfulRetrieveJob->asyncSetSuccessful();
    parent.m_successfulRetrieveJobs.push(std::move(m_successfulRetrieveJob));
  } catch (const cta::objectstore::Backend::NoSuchObject &ex){
    cta::log::ScopedParamContainer params(parent.m_lc);
    params.add("ExceptionMSG", ex.getMessageValue())
          .add("fileId", m_successfulRetrieveJob->archiveFile.archiveFileID);
    parent.m_lc.log(cta::log::WARNING,"In RecallReportPacker::ReportSuccessful::execute(): call to m_successfulRetrieveJob->asyncSetSuccessful() failed, job does not exist in the objectstore.");
  }
128
}
129

130
131
132
//------------------------------------------------------------------------------
//ReportEndofSession::execute
//------------------------------------------------------------------------------
133
void RecallReportPacker::ReportEndofSession::execute(RecallReportPacker& parent){
134
  parent.setDiskDone();
135
  if(!parent.errorHappened()){
Victor Kotlyar's avatar
Victor Kotlyar committed
136
    parent.m_lc.log(cta::log::INFO,"Nominal RecallReportPacker::EndofSession has been reported");
137
    if (parent.m_watchdog) {
Victor Kotlyar's avatar
Victor Kotlyar committed
138
      parent.m_watchdog->addParameter(cta::log::Param("status","success"));
139
140
141
142
      // We have a race condition here between the processing of this message by
      // the initial process and the printing of the end-of-session log, triggered
      // by the end our process. To delay the latter, we sleep half a second here.
      usleep(500*1000);
143
    }
144
145
146
  }
  else {
    const std::string& msg ="RecallReportPacker::EndofSession has been reported  but an error happened somewhere in the process";
Victor Kotlyar's avatar
Victor Kotlyar committed
147
    parent.m_lc.log(cta::log::ERR,msg);
148
    if (parent.m_watchdog) {
Victor Kotlyar's avatar
Victor Kotlyar committed
149
      parent.m_watchdog->addParameter(cta::log::Param("status","failure"));
150
151
152
153
      // We have a race condition here between the processing of this message by
      // the initial process and the printing of the end-of-session log, triggered
      // by the end our process. To delay the latter, we sleep half a second here.
      usleep(500*1000);
154
    }
155
156
157
158
159
160
  }
}

//------------------------------------------------------------------------------
//ReportEndofSession::goingToEnd
//------------------------------------------------------------------------------
161
bool RecallReportPacker::ReportEndofSession::goingToEnd() {
162
  return true;
163
}
164

Daniele Kruse's avatar
Daniele Kruse committed
165
166
167
168
169
//------------------------------------------------------------------------------
//ReportDriveStatus::execute
//------------------------------------------------------------------------------
void RecallReportPacker::ReportDriveStatus::execute(RecallReportPacker& parent){
  parent.m_retrieveMount->setDriveStatus(m_status);
170
  if(m_status==cta::common::dataStructures::DriveStatus::Unmounting) {
171
172
    parent.setTapeDone();
    parent.setTapeComplete();
173
  }
Daniele Kruse's avatar
Daniele Kruse committed
174
175
176
177
178
}

//------------------------------------------------------------------------------
//ReportDriveStatus::goingToEnd
//------------------------------------------------------------------------------
179
bool RecallReportPacker::ReportDriveStatus::goingToEnd() {
Daniele Kruse's avatar
Daniele Kruse committed
180
181
182
  return false;
}

183
184
185
//------------------------------------------------------------------------------
//ReportEndofSessionWithErrors::execute
//------------------------------------------------------------------------------
186
void RecallReportPacker::ReportEndofSessionWithErrors::execute(RecallReportPacker& parent){
187
  parent.setDiskDone();
188
  if(parent.m_errorHappened) {
189
    LogContext::ScopedParam(parent.m_lc,Param("errorCode",m_error_code));
Victor Kotlyar's avatar
Victor Kotlyar committed
190
    parent.m_lc.log(cta::log::ERR,m_message);
191
192
  }
  else{
193
    const std::string& msg ="RecallReportPacker::EndofSessionWithErrors has been reported  but NO error was detected during the process";
Victor Kotlyar's avatar
Victor Kotlyar committed
194
    parent.m_lc.log(cta::log::ERR,msg);
195
  }
196
  if (parent.m_watchdog) {
Victor Kotlyar's avatar
Victor Kotlyar committed
197
    parent.m_watchdog->addParameter(cta::log::Param("status","failure"));
198
199
200
201
    // We have a race condition here between the processing of this message by
    // the initial process and the printing of the end-of-session log, triggered
    // by the end our process. To delay the latter, we sleep half a second here.
    usleep(500*1000);
202
  }
203
}
204
205
206
207

//------------------------------------------------------------------------------
//ReportEndofSessionWithErrors::goingToEnd
//------------------------------------------------------------------------------
208
bool RecallReportPacker::ReportEndofSessionWithErrors::goingToEnd() {
209
  return true;
210
211
}

212
213
214
//------------------------------------------------------------------------------
//ReportError::execute
//------------------------------------------------------------------------------
215
216
void RecallReportPacker::ReportError::execute(RecallReportPacker& reportPacker){
  reportPacker.m_errorHappened=true;
217
  {
218
219
220
221
222
223
    cta::log::ScopedParamContainer params(reportPacker.m_lc);
    params.add("failureLog", m_failureLog)
          .add("fileId", m_failedRetrieveJob->archiveFile.archiveFileID);
    reportPacker.m_lc.log(cta::log::ERR,"In RecallReportPacker::ReportError::execute(): failing retrieve job after exception.");
  }
  try {
224
    m_failedRetrieveJob->transferFailed(m_failureLog, reportPacker.m_lc);
225
226
227
228
229
  } catch (const cta::objectstore::Backend::NoSuchObject &ex){
    cta::log::ScopedParamContainer params(reportPacker.m_lc);
    params.add("ExceptionMSG", ex.getMessageValue())
          .add("fileId", m_failedRetrieveJob->archiveFile.archiveFileID);
    reportPacker.m_lc.log(cta::log::WARNING,"In RecallReportPacker::ReportError::execute(): call to m_failedRetrieveJob->failed() , job does not exist in the objectstore.");
230
231
232
233
234
  } catch (cta::exception::Exception & ex) {
    cta::log::ScopedParamContainer params(reportPacker.m_lc);
    params.add("ExceptionMSG", ex.getMessageValue())
          .add("fileId", m_failedRetrieveJob->archiveFile.archiveFileID);
    reportPacker.m_lc.log(cta::log::ERR,"In RecallReportPacker::ReportError::execute(): call to m_failedRetrieveJob->failed() threw an exception.");
235
    reportPacker.m_lc.logBacktrace(cta::log::ERR, ex.backtrace());
236
  }
237
}
238

239
//------------------------------------------------------------------------------
240
241
//WorkerThread::WorkerThread
//------------------------------------------------------------------------------
242
243
244
RecallReportPacker::WorkerThread::WorkerThread(RecallReportPacker& parent):
m_parent(parent) {
}
245
246
247
//------------------------------------------------------------------------------
//WorkerThread::run
//------------------------------------------------------------------------------
248
void RecallReportPacker::WorkerThread::run(){
249
  m_parent.m_lc.pushOrReplace(Param("thread", "RecallReportPacker"));
Victor Kotlyar's avatar
Victor Kotlyar committed
250
  m_parent.m_lc.log(cta::log::DEBUG, "Starting RecallReportPacker thread");
251
  bool endFound = false;
252
253
  
  std::list <std::unique_ptr<Report>> reportedSuccessfully;
254
  cta::utils::Timer t;
255
256
257
258
259
  while(1) {
    std::string debugType;
    std::unique_ptr<Report> rep(m_parent.m_fifo.pop());
    {
      cta::log::ScopedParamContainer spc(m_parent.m_lc);
260
261
262
263
264
265
266
267
      int demangleStatus;
      char * demangledReportType = abi::__cxa_demangle(typeid(*rep.get()).name(), nullptr, nullptr, &demangleStatus);
      if (!demangleStatus) {
        spc.add("typeId", demangledReportType);
      } else {
        spc.add("typeId", typeid(*rep.get()).name());
      }
      free(demangledReportType);
268
      if (rep->goingToEnd())
269
270
        spc.add("goingToEnd", "true");
      m_parent.m_lc.log(cta::log::DEBUG, "Popping report");
271
    }
272
273
274
275
276
277
278
279
    // Record whether we found end before calling the potentially exception
    // throwing execute().)
    if (rep->goingToEnd())
      endFound=true;
    // We can afford to see any report to fail and keep passing the following ones
    // as opposed to migrations where one failure fails the session.
    try {
      rep->execute(m_parent);
280
281
282
283
      // This slightly hackish bit prevents too many calls to sub function and gettime()
      // m_parent.fullCheckAndFinishAsyncExecute will execute the shared half of the
      // request updates (individual, asynchronous is done in rep->execute(m_parent);
      if (typeid(*rep) == typeid(RecallReportPacker::ReportSuccessful) 
284
285
          && (m_parent.m_successfulRetrieveJobs.size() >= m_parent.RECALL_REPORT_PACKER_FLUSH_SIZE || t.secs() >= m_parent.RECALL_REPORT_PACKER_FLUSH_TIME )){
        m_parent.m_lc.log(cta::log::INFO,"m_parent.fullCheckAndFinishAsyncExecute()");
286
        m_parent.fullCheckAndFinishAsyncExecute();
287
288
        t.reset();
      }
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
    } catch(const cta::exception::Exception& e){
      //we get there because to tried to close the connection and it failed
      //either from the catch a few lines above or directly from rep->execute
      cta::log::ScopedParamContainer params(m_parent.m_lc);
      params.add("exceptionWhat", e.getMessageValue())
            .add("exceptionType", typeid(e).name());
      m_parent.m_lc.log(cta::log::ERR, "Tried to report and got a CTA exception.");
      if (m_parent.m_watchdog) {
        m_parent.m_watchdog->addToErrorCount("Error_clientCommunication");
        m_parent.m_watchdog->addParameter(cta::log::Param("status","failure"));
      }
    } catch(const std::exception& e){
      //we get there because to tried to close the connection and it failed
      //either from the catch a few lines above or directly from rep->execute
      cta::log::ScopedParamContainer params(m_parent.m_lc);
      params.add("exceptionWhat", e.what())
            .add("exceptionType", typeid(e).name());
      m_parent.m_lc.log(cta::log::ERR, "Tried to report and got a standard exception.");
      if (m_parent.m_watchdog) {
        m_parent.m_watchdog->addToErrorCount("Error_clientCommunication");
        m_parent.m_watchdog->addParameter(cta::log::Param("status","failure"));
      }
    } catch(...){
      //we get there because to tried to close the connection and it failed
      //either from the catch a few lines above or directly from rep->execute
      m_parent.m_lc.log(cta::log::ERR, "Tried to report and got an unknown exception.");
      if (m_parent.m_watchdog) {
        m_parent.m_watchdog->addToErrorCount("Error_clientCommunication");
        m_parent.m_watchdog->addParameter(cta::log::Param("status","failure"));
      }
319
    }
320
    if (endFound) break;
321
  }
322
  
323
  // Make sure the last batch of reports got cleaned up. 
324
  try {
325
    m_parent.fullCheckAndFinishAsyncExecute();
326
    if(m_parent.isDiskDone()){
327
328
329
      //The m_parent.m_diskThreadComplete is set to true when a ReportEndOfSession or a ReportAndOfSessionWithError
      //has been put. It is only after the fullCheckandFinishAsyncExecute is finished that we can say to the mount that the disk thread is complete.
      m_parent.m_lc.log(cta::log::DEBUG, "In RecallReportPacker::WorkerThread::run(): all disk threads are finished, telling the mount that Disk threads are complete");
330
      m_parent.setDiskComplete();
331
    }
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
  } catch(const cta::exception::Exception& e){
      cta::log::ScopedParamContainer params(m_parent.m_lc);
      params.add("exceptionWhat", e.getMessageValue())
            .add("exceptionType", typeid(e).name());
      m_parent.m_lc.log(cta::log::ERR, "Tried to report and got a CTA exception.");
      if (m_parent.m_watchdog) {
        m_parent.m_watchdog->addToErrorCount("Error_clientCommunication");
        m_parent.m_watchdog->addParameter(cta::log::Param("status","failure"));
      } 
  } catch(const std::exception& e){
      cta::log::ScopedParamContainer params(m_parent.m_lc);
      params.add("exceptionWhat", e.what())
            .add("exceptionType", typeid(e).name());
      m_parent.m_lc.log(cta::log::ERR, "Tried to report and got a standard exception.");
      if (m_parent.m_watchdog) {
        m_parent.m_watchdog->addToErrorCount("Error_clientCommunication");
        m_parent.m_watchdog->addParameter(cta::log::Param("status","failure"));
      }
  } catch(...){
      m_parent.m_lc.log(cta::log::ERR, "Tried to report and got an unknown exception.");
      if (m_parent.m_watchdog) {
        m_parent.m_watchdog->addToErrorCount("Error_clientCommunication");
        m_parent.m_watchdog->addParameter(cta::log::Param("status","failure"));
      }
  } 
  
358
359
360
361
  // Drain the fifo in case we got an exception
  if (!endFound) {
    while (1) {
      std::unique_ptr<Report> report(m_parent.m_fifo.pop());
362
      if (report->goingToEnd())
363
364
365
        break;
    }
  }
366
367
368
  // Cross check that the queue is indeed empty.
  while (m_parent.m_fifo.size()) {
    // There is at least one extra report we missed.
369
    // The drive status reports are not a problem though.
Victor Kotlyar's avatar
Victor Kotlyar committed
370
    cta::log::ScopedParamContainer spc(m_parent.m_lc);
371
372
373
374
    std::unique_ptr<Report> missedReport(m_parent.m_fifo.pop());
    spc.add("ReportType", typeid(*missedReport).name());
    if (missedReport->goingToEnd())
      spc.add("goingToEnd", "true");
375
376
    if (typeid(*missedReport) != typeid(RecallReportPacker::ReportDriveStatus))
      m_parent.m_lc.log(cta::log::ERR, "Popping missed report (memory leak)");
377
  }
Victor Kotlyar's avatar
Victor Kotlyar committed
378
  m_parent.m_lc.log(cta::log::DEBUG, "Finishing RecallReportPacker thread");
379
}
380
381
382
383
384
385
386
387

//------------------------------------------------------------------------------
//errorHappened()
//------------------------------------------------------------------------------
bool RecallReportPacker::errorHappened() {
  return m_errorHappened || (m_watchdog && m_watchdog->errorHappened());
}

388
389
390
//------------------------------------------------------------------------------
//fullCheckAndFinishAsyncExecute()
//------------------------------------------------------------------------------
391
void RecallReportPacker::fullCheckAndFinishAsyncExecute() {
392
  m_retrieveMount->flushAsyncSuccessReports(m_successfulRetrieveJobs, m_lc);
393
394
}

395
396
397
398
//------------------------------------------------------------------------------
//reportTapeDone()
//------------------------------------------------------------------------------
void RecallReportPacker::setTapeDone() {
399
  cta::threading::MutexLocker mutexLocker(m_mutex);
400
  m_tapeThreadComplete = true;
401
402
403
}

void RecallReportPacker::setTapeComplete(){
404
  cta::threading::MutexLocker mutexLocker(m_mutex);
405
  m_retrieveMount->tapeComplete();
406
407
}

408
void RecallReportPacker::setDiskComplete(){
409
  cta::threading::MutexLocker mutexLocker(m_mutex);
410
411
412
413
  m_retrieveMount->diskComplete();
}

bool RecallReportPacker::isDiskDone(){
414
  cta::threading::MutexLocker mutexLocker(m_mutex);
415
416
417
  return m_diskThreadComplete;
}

418
419
420
421
//------------------------------------------------------------------------------
//reportDiskDone()
//------------------------------------------------------------------------------
void RecallReportPacker::setDiskDone() {
422
  cta::threading::MutexLocker mutexLocker(m_mutex);
423
424
425
426
427
428
429
430
431
432
  m_diskThreadComplete = true;
}

//------------------------------------------------------------------------------
//reportDiskDone()
//------------------------------------------------------------------------------
bool RecallReportPacker::allThreadsDone() {
  return m_tapeThreadComplete && m_diskThreadComplete;
}

433
}}}}