RecallReportPacker.cpp 10.6 KB
Newer Older
1
/******************************************************************************
2
 *                      RecallReportPacker.cpp
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
 *
 * 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
#include "log.h"
30

31
32
33
34
35
36
namespace{
  struct failedReportRecallResult : public castor::tape::Exception{
    failedReportRecallResult(const std::string& s): Exception(s){}
  };
}

David COME's avatar
David COME committed
37
38
39
using castor::log::LogContext;
using castor::log::Param;

40
41
42
43
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
44
45
46
47
48
//------------------------------------------------------------------------------
//Constructor
//------------------------------------------------------------------------------
RecallReportPacker::RecallReportPacker(client::ClientInterface & tg,
        unsigned int reportFilePeriod,log::LogContext lc):
49
ReportPackerInterface<detail::Recall>(tg,lc),
50
        m_workerThread(*this),m_reportFilePeriod(reportFilePeriod),m_errorHappened(false){
51
52

}
53
54
55
//------------------------------------------------------------------------------
//Destructor
//------------------------------------------------------------------------------
56
RecallReportPacker::~RecallReportPacker(){
57
  castor::tape::threading::MutexLocker ml(&m_producterProtection);
58
}
59
60
61
//------------------------------------------------------------------------------
//reportCompletedJob
//------------------------------------------------------------------------------
62
void RecallReportPacker::reportCompletedJob(const FileStruct& recalledFile,unsigned long checksum){
David COME's avatar
David COME committed
63
  std::auto_ptr<Report> rep(new ReportSuccessful(recalledFile,checksum));
64
65
66
  castor::tape::threading::MutexLocker ml(&m_producterProtection);
  m_fifo.push(rep.release());
}
67
68
69
//------------------------------------------------------------------------------
//reportFailedJob
//------------------------------------------------------------------------------  
70
void RecallReportPacker::reportFailedJob(const FileStruct & recalledFile
71
72
73
74
75
,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());
}
76
77
78
//------------------------------------------------------------------------------
//reportEndOfSession
//------------------------------------------------------------------------------
79
80
81
82
83
void RecallReportPacker::reportEndOfSession(){
  castor::tape::threading::MutexLocker ml(&m_producterProtection);
  m_fifo.push(new ReportEndofSession());
}
  
84
85
86
//------------------------------------------------------------------------------
//reportEndOfSessionWithErrors
//------------------------------------------------------------------------------
87
88
89
90
91
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));
}
 
92
93
//------------------------------------------------------------------------------
//ReportSuccessful::execute
94
95
96
97
98
99
100
101
102
//------------------------------------------------------------------------------
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());
David COME's avatar
David COME committed
103
  successRecall->setChecksum(m_checksum);
104
  
David COME's avatar
David COME committed
105
106
  //WARNING : ad hoc name of checksum algorithm
  successRecall->setChecksumName("adler32");
107
108
  _this.m_listReports->addSuccessfulRecalls(successRecall.release());
}
109
110
111
//------------------------------------------------------------------------------
//flush
//------------------------------------------------------------------------------
112
void RecallReportPacker::flush(){
David COME's avatar
David COME committed
113
    
114
    client::ClientInterface::RequestReport chrono;
115
116
    try{
      m_client.reportRecallResults(*m_listReports,chrono);
117
118
119
      logRequestReport(chrono,"RecallReportList successfully transmitted to client (contents follow)");

      logReport(m_listReports->failedRecalls(),"A file failed to be recalled");
120
121
122
      logReport(m_listReports->successfulRecalls(),"A file was successfully recalled");
    }
   catch(const castor::tape::Exception& e){
123
    LogContext::ScopedParam s(m_lc, Param("exceptionCode",e.code()));
124
125
    LogContext::ScopedParam ss(m_lc, Param("exceptionMessageValue", e.getMessageValue()));
    LogContext::ScopedParam sss(m_lc, Param("exceptionWhat",e.what()));
126
127
128
129
130
131
132
    const std::string msg_error="An exception was caught trying to call reportRecallResults";
    m_lc.log(LOG_ERR,msg_error);
    throw failedReportRecallResult(msg_error);
    }
    //delete the old pointer and replace it with the new one provided
    //that way, all the reports that have been send are deleted (by FileReportList's destructor)
    m_listReports.reset(new FileReportList);
133
}
134
135
136
//------------------------------------------------------------------------------
//ReportEndofSession::execute
//------------------------------------------------------------------------------
137
void RecallReportPacker::ReportEndofSession::execute(RecallReportPacker& _this){
138
  client::ClientInterface::RequestReport chrono;
139
140
141
142
143
144
145
146
147
148
    if(!_this.m_errorHappened){
      _this.m_client.reportEndOfSession(chrono);
      _this.logRequestReport(chrono,"Nominal RecallReportPacker::EndofSession has been reported",LOG_INFO);
    }
    else {
      const std::string& msg ="RecallReportPacker::EndofSession has been reported  but an error happened somewhere in the process";
      _this.m_lc.log(LOG_ERR,msg);
      _this.m_client.reportEndOfSessionWithError(msg,SEINTERNAL,chrono);
      _this.logRequestReport(chrono,"reporting EndOfSessionWithError done",LOG_ERR);
    }
149
}
150
151
152
//------------------------------------------------------------------------------
//ReportEndofSessionWithErrors::execute
//------------------------------------------------------------------------------
153
void RecallReportPacker::ReportEndofSessionWithErrors::execute(RecallReportPacker& _this){
154
  client::ClientInterface::RequestReport chrono;
155
  if(_this.m_errorHappened) {
156
  _this.m_client.reportEndOfSessionWithError(m_message,m_error_code,chrono); 
157
158
  LogContext::ScopedParam(_this.m_lc,Param("errorCode",m_error_code));
  _this.m_lc.log(LOG_ERR,m_message);
159
160
  }
  else{
161
   const std::string& msg ="RecallReportPacker::EndofSessionWithErrors has been reported  but NO error was detected during the process";
162
   _this.m_lc.log(LOG_ERR,msg);
163
   _this.m_client.reportEndOfSessionWithError(msg,SEINTERNAL,chrono); 
164
165
  }
}
166
167
168
//------------------------------------------------------------------------------
//ReportError::execute
//------------------------------------------------------------------------------
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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;
}
//------------------------------------------------------------------------------
184
185
//WorkerThread::WorkerThread
//------------------------------------------------------------------------------
186
187
188
RecallReportPacker::WorkerThread::WorkerThread(RecallReportPacker& parent):
m_parent(parent) {
}
189
190
191
//------------------------------------------------------------------------------
//WorkerThread::run
//------------------------------------------------------------------------------
192
void RecallReportPacker::WorkerThread::run(){
193
194
  m_parent.m_lc.pushOrReplace(Param("thread", "RecallReportPacker"));
  m_parent.m_lc.log(LOG_DEBUG, "Starting RecallReportPacker thread");
195
196
197
198
199
  client::ClientInterface::RequestReport chrono;
  try{
      while(1) {    
        std::auto_ptr<Report> rep (m_parent.m_fifo.pop());    
        
200
        //how mane files we have globally treated 
201
202
        unsigned int totalSize = m_parent.m_listReports->failedRecalls().size() +
                                 m_parent.m_listReports->successfulRecalls().size();
203
204
        //If we have enough reports or we are going to end the loop
        //then we flush        
205
206
207
208
        if(totalSize >= m_parent.m_reportFilePeriod || rep->goingToEnd() )
        {
        
          try{
209
            m_parent.flush();
210
211
          }
          catch(const failedReportRecallResult& e){
212
213
214
            //got there because we failed to report the recall results
            //we have to try to close the connection. 
            //reportEndOfSessionWithError might throw 
215
216
            m_parent.m_client.reportEndOfSessionWithError(e.getMessageValue(),SEINTERNAL,chrono);
            m_parent.logRequestReport(chrono,"Successfully closed client's session after the failed report RecallResult");
217
            break;
218
219
          }
        }
220
        
221
222
223
224
        rep->execute(m_parent);
        if(rep->goingToEnd()) {
          break;
        }
225
    }
226
227
228
229
230
  }
  catch(const castor::tape::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
    m_parent.logRequestReport(chrono,"tried to report endOfSession(WithError) and got an exception, cant do much more",LOG_ERR);
231
  }
232
  m_parent.m_lc.log(LOG_DEBUG, "Finishing RecallReportPacker thread");
233
234
}
}}}}