RecallReportPacker.cpp 8.22 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
#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
44
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
 
45
RecallReportPacker::RecallReportPacker(client::ClientInterface & tg,unsigned int reportFilePeriod,log::LogContext lc):
46
ReportPackerInterface<detail::Recall>(tg,lc),
47
        m_workerThread(*this),m_reportFilePeriod(reportFilePeriod),m_errorHappened(false){
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
87
88
89
90
91
92
93

}
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
94
    
95
    client::ClientInterface::RequestReport chrono;
96
97
    try{
      m_client.reportRecallResults(*m_listReports,chrono);
98
99
100
      logRequestReport(chrono,"RecallReportList successfully transmitted to client (contents follow)");

      logReport(m_listReports->failedRecalls(),"A file failed to be recalled");
101
102
103
      logReport(m_listReports->successfulRecalls(),"A file was successfully recalled");
    }
   catch(const castor::tape::Exception& e){
104
    LogContext::ScopedParam s(m_lc, Param("exceptionCode",e.code()));
105
106
    LogContext::ScopedParam ss(m_lc, Param("exceptionMessageValue", e.getMessageValue()));
    LogContext::ScopedParam sss(m_lc, Param("exceptionWhat",e.what()));
107
108
109
110
    const std::string msg_error="An exception was caught trying to call reportRecallResults";
    m_lc.log(LOG_ERR,msg_error);
    throw failedReportRecallResult(msg_error);
    }
111
  }
112
113
114
    //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);
115
116
117
}

void RecallReportPacker::ReportEndofSession::execute(RecallReportPacker& _this){
118
  client::ClientInterface::RequestReport chrono;
119
120
121
122
123
124
125
126
127
128
    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);
    }
129
130
131
}

void RecallReportPacker::ReportEndofSessionWithErrors::execute(RecallReportPacker& _this){
132
  client::ClientInterface::RequestReport chrono;
133
  if(_this.m_errorHappened) {
134
  _this.m_client.reportEndOfSessionWithError(m_message,m_error_code,chrono); 
135
136
  LogContext::ScopedParam(_this.m_lc,Param("errorCode",m_error_code));
  _this.m_lc.log(LOG_ERR,m_message);
137
138
  }
  else{
139
   const std::string& msg ="RecallReportPacker::EndofSessionWithErrors has been reported  but NO error was detected during the process";
140
   _this.m_lc.log(LOG_ERR,msg);
141
   _this.m_client.reportEndOfSessionWithError(msg,SEINTERNAL,chrono); 
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  }
}

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(){
166
167
  m_parent.m_lc.pushOrReplace(Param("thread", "RecallReportPacker"));
  m_parent.m_lc.log(LOG_DEBUG, "Starting RecallReportPacker thread");
168
169
170
171
172
173
174
175
176
177
178
179
  client::ClientInterface::RequestReport chrono;
  try{
      while(1) {    
        std::auto_ptr<Report> rep (m_parent.m_fifo.pop());    
        
        unsigned int totalSize = m_parent.m_listReports->failedRecalls().size() +
                                 m_parent.m_listReports->successfulRecalls().size();
     
        if(totalSize >= m_parent.m_reportFilePeriod || rep->goingToEnd() )
        {
        
          try{
180
            m_parent.flush();
181
182
183
184
185
186
          }
          catch(const failedReportRecallResult& e){
            //got there because we failed tp report the recall results
            //we have to try to close the connection
            m_parent.m_client.reportEndOfSessionWithError(e.getMessageValue(),SEINTERNAL,chrono);
            m_parent.logRequestReport(chrono,"Successfully closed client's session after the failed report RecallResult");
187
            break;
188
189
          }
        }
190
        
191
192
193
194
        rep->execute(m_parent);
        if(rep->goingToEnd()) {
          break;
        }
195
    }
196
197
198
199
200
  }
  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);
201
  }
202
  m_parent.m_lc.log(LOG_DEBUG, "Finishing RecallReportPacker thread");
203
204
}
}}}}