RecallReportPacker.cpp 12.3 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
/******************************************************************************
 *
 * 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"
27
#include "castor/log/Logger.hpp"
28
#include "log.h"
29
#include <signal.h>
30

31
namespace{
32
  struct failedReportRecallResult : public castor::exception::Exception{
33
34
35
36
    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::server::MutexLocker ml(&m_producterProtection);
58
}
59
60
61
//------------------------------------------------------------------------------
//reportCompletedJob
//------------------------------------------------------------------------------
62
63
64
void RecallReportPacker::reportCompletedJob(const FileStruct& recalledFile,
  u_int32_t checksum, u_int64_t size){
  std::auto_ptr<Report> rep(new ReportSuccessful(recalledFile,checksum,size));
65
  castor::server::MutexLocker ml(&m_producterProtection);
66
67
  m_fifo.push(rep.release());
}
68
69
70
//------------------------------------------------------------------------------
//reportFailedJob
//------------------------------------------------------------------------------  
71
void RecallReportPacker::reportFailedJob(const FileStruct & recalledFile
72
73
,const std::string& msg,int error_code){
  std::auto_ptr<Report> rep(new ReportError(recalledFile,msg,error_code));
74
  castor::server::MutexLocker ml(&m_producterProtection);
75
76
  m_fifo.push(rep.release());
}
77
78
79
//------------------------------------------------------------------------------
//reportEndOfSession
//------------------------------------------------------------------------------
80
void RecallReportPacker::reportEndOfSession(){
81
  castor::server::MutexLocker ml(&m_producterProtection);
82
83
84
  m_fifo.push(new ReportEndofSession());
}
  
85
86
87
//------------------------------------------------------------------------------
//reportEndOfSessionWithErrors
//------------------------------------------------------------------------------
88
void RecallReportPacker::reportEndOfSessionWithErrors(const std::string msg,int error_code){
89
  castor::server::MutexLocker ml(&m_producterProtection);
90
91
  m_fifo.push(new ReportEndofSessionWithErrors(msg,error_code));
}
92

93
94
//------------------------------------------------------------------------------
//ReportSuccessful::execute
95
//------------------------------------------------------------------------------
96
void RecallReportPacker::ReportSuccessful::execute(RecallReportPacker& parent){
97
98
  std::auto_ptr<FileSuccessStruct> successRecall(new FileSuccessStruct);
  
99
100
101
102
103
  successRecall->setFseq(m_recalledFile.fseq());
  successRecall->setFileTransactionId(m_recalledFile.fileTransactionId());
  successRecall->setId(m_recalledFile.id());
  successRecall->setNshost(m_recalledFile.nshost());
  successRecall->setFileid(m_recalledFile.fileid());
104
105
  successRecall->setPath(m_recalledFile.path());
  successRecall->setFileSize(m_size);
106

David COME's avatar
David COME committed
107
108
  //WARNING : ad hoc name of checksum algorithm
  successRecall->setChecksumName("adler32");
109
110
  successRecall->setChecksum(m_checksum);
  
111
  parent.m_listReports->addSuccessfulRecalls(successRecall.release());
112
}
113
114
115
//------------------------------------------------------------------------------
//flush
//------------------------------------------------------------------------------
116
void RecallReportPacker::flush(){
117
118
119
120
121
122
  //we dont want to send empty reports
  unsigned int totalSize = m_listReports->failedRecalls().size() +
                           m_listReports->successfulRecalls().size();
  if(totalSize==0) {
    return;
  }
123
 
124
125
126
127
128
129
130
131
132
133
134
135
  client::ClientInterface::RequestReport chrono;
  try{
    m_client.reportRecallResults(*m_listReports,chrono);
    {
      log::ScopedParamContainer params(m_lc);
      params.add("successCount", m_listReports->successfulRecalls().size())
            .add("failureCount", m_listReports->failedRecalls().size());
      logRequestReport(chrono,"RecallReportList successfully transmitted to client (contents follow)");
    }
    logReport(m_listReports->failedRecalls(),"Reported failed recall to client");
    logReport(m_listReports->successfulRecalls(),"Reported successful recall to client");
  } catch(const castor::exception::Exception& e){
136
    LogContext::ScopedParam s(m_lc, Param("exceptionCode",e.code()));
137
138
    LogContext::ScopedParam ss(m_lc, Param("exceptionMessageValue", e.getMessageValue()));
    LogContext::ScopedParam sss(m_lc, Param("exceptionWhat",e.what()));
139
140
141
    const std::string msg_error="An exception was caught trying to call reportRecallResults";
    m_lc.log(LOG_ERR,msg_error);
    throw failedReportRecallResult(msg_error);
142
143
144
145
  }
  //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);
146
}
147
148
149
//------------------------------------------------------------------------------
//ReportEndofSession::execute
//------------------------------------------------------------------------------
150
void RecallReportPacker::ReportEndofSession::execute(RecallReportPacker& parent){
151
  client::ClientInterface::RequestReport chrono;
152
153
154
    if(!parent.m_errorHappened){
      parent.m_client.reportEndOfSession(chrono);
      parent.logRequestReport(chrono,"Nominal RecallReportPacker::EndofSession has been reported",LOG_INFO);
155
156
157
    }
    else {
      const std::string& msg ="RecallReportPacker::EndofSession has been reported  but an error happened somewhere in the process";
158
159
160
      parent.m_lc.log(LOG_ERR,msg);
      parent.m_client.reportEndOfSessionWithError(msg,SEINTERNAL,chrono);
      parent.logRequestReport(chrono,"reporting EndOfSessionWithError done",LOG_ERR);
161
    }
162
}
163
164
165
//------------------------------------------------------------------------------
//ReportEndofSessionWithErrors::execute
//------------------------------------------------------------------------------
166
void RecallReportPacker::ReportEndofSessionWithErrors::execute(RecallReportPacker& parent){
167
  client::ClientInterface::RequestReport chrono;
168
169
170
171
  if(parent.m_errorHappened) {
  parent.m_client.reportEndOfSessionWithError(m_message,m_error_code,chrono); 
  LogContext::ScopedParam(parent.m_lc,Param("errorCode",m_error_code));
  parent.m_lc.log(LOG_ERR,m_message);
172
173
  }
  else{
174
   const std::string& msg ="RecallReportPacker::EndofSessionWithErrors has been reported  but NO error was detected during the process";
175
176
   parent.m_lc.log(LOG_ERR,msg);
   parent.m_client.reportEndOfSessionWithError(msg,SEINTERNAL,chrono); 
177
178
  }
}
179
180
181
//------------------------------------------------------------------------------
//ReportError::execute
//------------------------------------------------------------------------------
182
void RecallReportPacker::ReportError::execute(RecallReportPacker& parent){
183
184
   
  std::auto_ptr<FileErrorStruct> failed(new FileErrorStruct);
185
  //failedMigration->setFileMigrationReportList(parent.m_listReports.get());
186
187
  failed->setErrorCode(m_error_code);
  failed->setErrorMessage(m_error_msg);
188
189
190
191
  failed->setFseq(m_recalledFile.fseq());
  failed->setFileTransactionId(m_recalledFile.fileTransactionId());
  failed->setId(m_recalledFile.id());
  failed->setNshost(m_recalledFile.nshost());
192
  
193
194
195
196
  parent.m_listReports->addFailedRecalls(failed.release());
  parent.m_errorHappened=true;
}
//------------------------------------------------------------------------------
197
198
//WorkerThread::WorkerThread
//------------------------------------------------------------------------------
199
200
201
RecallReportPacker::WorkerThread::WorkerThread(RecallReportPacker& parent):
m_parent(parent) {
}
202
203
204
//------------------------------------------------------------------------------
//WorkerThread::run
//------------------------------------------------------------------------------
205
void RecallReportPacker::WorkerThread::run(){
206
207
  m_parent.m_lc.pushOrReplace(Param("thread", "RecallReportPacker"));
  m_parent.m_lc.log(LOG_DEBUG, "Starting RecallReportPacker thread");
208
209
210
211
212
  client::ClientInterface::RequestReport chrono;
  try{
      while(1) {    
        std::auto_ptr<Report> rep (m_parent.m_fifo.pop());    
        
213
214
215
216
217
218
219
220
221
222
223
224
225
226
        /*
         * this boolean is only true if it is the last turn of the loop
         * == rep is ReportEndOFSession or ReportEndOFSessionWithError
         */
        bool isItTheEnd = rep->goingToEnd();
        
        /*
         * if it is not the last turn, we want to execute the report 
         * (= insert the file into thr right list of results) BEFORE (trying to) 
         * flush
         */
        if(!isItTheEnd){
          rep->execute(m_parent);
        }
227
        //how mane files we have globally treated 
228
229
        unsigned int totalSize = m_parent.m_listReports->failedRecalls().size() +
                                 m_parent.m_listReports->successfulRecalls().size();
230
        
231
        //If we have enough reports or we are going to end the loop
232
233
234
235
        // or it is the end (== unconditional flush ) 
        // or we bypass the queuing system if the client is readtp
        // then we flush        
        if(totalSize >= m_parent.m_reportFilePeriod || isItTheEnd ||
236
           detail::ReportByFile == m_parent.m_reportBatching)
237
238
239
        {
        
          try{
240
            m_parent.flush();
241
242
          }
          catch(const failedReportRecallResult& e){
243
244
245
            //got there because we failed to report the recall results
            //we have to try to close the connection. 
            //reportEndOfSessionWithError might throw 
246
247
            m_parent.m_client.reportEndOfSessionWithError(e.getMessageValue(),SEINTERNAL,chrono);
            m_parent.logRequestReport(chrono,"Successfully closed client's session after the failed report RecallResult");
248
249
250
251
252
253
            
            // We need to wait until the end of session is signaled from upsteam
            while (!isItTheEnd) {
              std::auto_ptr<Report> r(m_parent.m_fifo.pop());
              isItTheEnd = r->goingToEnd();
            }
254
            break;
255
256
          }
        }
257
        
258
259
260
261
262
263
264
265
         /* 
          * It is the last turn of loop, we are going to send 
          * an EndOfSession (WithError) to the client. We need to have flushed 
          * all leftover BEFORE. Because as soon as we report the end, we can not 
          * report any the longer the success or failure of any job
         */
        if(isItTheEnd) {
          rep->execute(m_parent);
266
267
          break;
        }
268
    }
269
  }
270
  catch(const castor::exception::Exception& e){
271
272
273
    //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);
274
  }
275
  m_parent.m_lc.log(LOG_DEBUG, "Finishing RecallReportPacker thread");
276
277
  
  //When we end up there, we might have still 
278
279
}
}}}}