RecallReportPacker.cpp 6.38 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
25
26
27
/******************************************************************************
 *                      RecallReportPacker.hpp
 *
 * 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"
#include "castor/tape/tapegateway/FileRecalledNotificationStruct.hpp"
#include "castor/tape/tapegateway/FileRecalledNotificationStruct.hpp"
28
#include "castor/log/Logger.hpp"
29
30
31
32
33
34

namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
 
35
36
37
using castor::log::LogContext;
using castor::log::Param;

38
RecallReportPacker::RecallReportPacker(client::ClientInterface & tg,unsigned int reportFilePeriod,log::LogContext lc):
39
ReportPackerInterface<detail::Recall>(tg,lc),
40
        m_workerThread(*this),m_reportFilePeriod(reportFilePeriod),m_errorHappened(false){
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

}
RecallReportPacker::~RecallReportPacker(){

}
void RecallReportPacker::reportCompletedJob(const tapegateway::FileToRecallStruct& recalledFile){
  std::auto_ptr<Report> rep(new ReportSuccessful(recalledFile));
  castor::tape::threading::MutexLocker ml(&m_producterProtection);
  m_fifo.push(rep.release());
}
  
void RecallReportPacker::reportFailedJob(const tapegateway::FileToRecallStruct & recalledFile
,const std::string& msg,int error_code){
  std::auto_ptr<Report> rep(new ReportError(recalledFile,msg,error_code));
  castor::tape::threading::MutexLocker ml(&m_producterProtection);
  m_fifo.push(rep.release());
}

void RecallReportPacker::reportEndOfSession(){
  castor::tape::threading::MutexLocker ml(&m_producterProtection);
  m_fifo.push(new ReportEndofSession());
}
  

void RecallReportPacker::reportEndOfSessionWithErrors(const std::string msg,int error_code){
  castor::tape::threading::MutexLocker ml(&m_producterProtection);
  m_fifo.push(new ReportEndofSessionWithErrors(msg,error_code));
}
 
//------------------------------------------------------------------------------
void RecallReportPacker::ReportSuccessful::execute(RecallReportPacker& _this){
  std::auto_ptr<FileSuccessStruct> successRecall(new FileSuccessStruct);
  
  successRecall->setFseq(m_migratedFile.fseq());
  successRecall->setFileTransactionId(m_migratedFile.fileTransactionId());
  successRecall->setId(m_migratedFile.id());
  successRecall->setNshost(m_migratedFile.nshost());
  successRecall->setFileid(m_migratedFile.fileid());
  
  _this.m_listReports->addSuccessfulRecalls(successRecall.release());
}

void RecallReportPacker::flush(){
  unsigned int totalSize = m_listReports->failedRecalls().size() +
                       m_listReports->successfulRecalls().size();
  if(totalSize > 0){
David COME's avatar
David COME committed
87
88
89
    logReport(m_listReports->failedRecalls(),"A file failed to be recalled");
    logReport(m_listReports->successfulRecalls(),"A file was successfully recalled");
    
90
    client::ClientInterface::RequestReport chrono;
91
    m_client.reportRecallResults(*m_listReports,chrono);
92
93
94
    
    //delete the old pointer and remplace it with the new one provided
    //that way,  all the reports that have been send are deleted (by m_listReports's destructor)
David COME's avatar
David COME committed
95
    m_listReports.reset(new FileReportList);
96
97
98
99
  }
}

void RecallReportPacker::ReportEndofSession::execute(RecallReportPacker& _this){
100
  client::ClientInterface::RequestReport chrono;
101
102
  if(!_this.m_errorHappened){
    _this.m_client.reportEndOfSession(chrono);
103
    _this.m_lc.log(LOG_INFO,"Nominal RecallReportPacker::EndofSession has been reported");
104
105
  }
  else {
106
     const std::string& msg ="RecallReportPacker::EndofSession has been reported  but an error happened somewhere in the process";
107
     _this.m_lc.log(LOG_ERR,msg);
108
     _this.m_client.reportEndOfSessionWithError(msg,SEINTERNAL,chrono);
109
110
111
112
113
     //throw castor::exception::Exception(msg);
  }
}

void RecallReportPacker::ReportEndofSessionWithErrors::execute(RecallReportPacker& _this){
114
  client::ClientInterface::RequestReport chrono;
115
  if(_this.m_errorHappened) {
116
  _this.m_client.reportEndOfSessionWithError(m_message,m_error_code,chrono); 
117
118
  LogContext::ScopedParam(_this.m_lc,Param("errorCode",m_error_code));
  _this.m_lc.log(LOG_ERR,m_message);
119
120
  }
  else{
121
   const std::string& msg ="RecallReportPacker::EndofSessionWithErrors has been reported  but NO error was detected during the process";
122
   _this.m_lc.log(LOG_ERR,msg);
123
   _this.m_client.reportEndOfSessionWithError(msg,SEINTERNAL,chrono); 
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
   //throw castor::exception::Exception(msg);
  }
}

void RecallReportPacker::ReportError::execute(RecallReportPacker& _this){
   
  std::auto_ptr<FileErrorStruct> failed(new FileErrorStruct);
  //failedMigration->setFileMigrationReportList(_this.m_listReports.get());
  failed->setErrorCode(m_error_code);
  failed->setErrorMessage(m_error_msg);
  failed->setFseq(m_migratedFile.fseq());
  failed->setFileTransactionId(m_migratedFile.fileTransactionId());
  failed->setId(m_migratedFile.id());
  failed->setNshost(m_migratedFile.nshost());
  
  _this.m_listReports->addFailedRecalls(failed.release());
  _this.m_errorHappened=true;
}
//------------------------------------------------------------------------------

RecallReportPacker::WorkerThread::WorkerThread(RecallReportPacker& parent):
m_parent(parent) {
}

void RecallReportPacker::WorkerThread::run(){
149
  while(1) {    
150
    std::auto_ptr<Report> rep (m_parent.m_fifo.pop());    
151
152
153
    unsigned int totalSize = m_parent.m_listReports->failedRecalls().size() +
                             m_parent.m_listReports->successfulRecalls().size();
    
154
    if(totalSize >= m_parent.m_reportFilePeriod || rep->goingToEnd())
155
156
157
    {
      m_parent.flush();
    }
158
    
159
    rep->execute(m_parent);
160
161
162
163
164
    
    if(rep->goingToEnd()) {
      break;
    }
    
165
166
167
  }
}
}}}}