MigrationReportPacker.hpp 5.91 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
/******************************************************************************
 *                      MigrationReportPacker.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
 *****************************************************************************/

#pragma once

27
#include "castor/tape/tapeserver/threading/BlockingQueue.hpp"
28
#include "castor/tape/tapeserver/daemon/MigrationJob.hpp"
29
30
#include "castor/tape/tapeserver/daemon/ClientInterface.hpp"
#include "castor/tape/tapegateway/FileToMigrateStruct.hpp"
31
32
#include "castor/log/LogContext.hpp"
#include "castor/tape/tapeserver/utils/suppressUnusedVariable.hpp"
33
#include <list>
34
#include <memory>
35

36
37
38
39
40
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
 
41
42
class MigrationReportPacker {
public:
43
44
45
46
47
  /**
   * @param tg The client who is asking for a migration of his files 
   * and to whom we have to report to the status of the operations.
   */
  MigrationReportPacker(ClientInterface & tg,log::LogContext& lc);
48
  
49
50
  ~MigrationReportPacker();
    
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
  /**
   * Create into the MigrationReportPacker a report for the successful migration
   * of migratedFile
   * @param migratedFile the file successfully migrated
   */
  void reportCompletedJob(const tapegateway::FileToMigrateStruct& migratedFile);
  
  /**
   * Create into the MigrationReportPacker a report for the failled migration
   * of migratedFile
   * @param migratedFile the file which failled 
   */
  void reportFailedJob(const tapegateway::FileToMigrateStruct& migratedFile,const std::string& msg,int error_code);
     
   /**
   * Create into the MigrationReportPacker a report for the signaling a flusing on tape
   */
  void reportFlush();
  
  /**
   * Create into the MigrationReportPacker a report for the nominal end of session
   */
  void reportEndOfSession();
  
  /**
   * Create into the MigrationReportPacker a report for an erroneous end of session
   * @param msg The error message 
   * @param error_code The error code given by the drive
   */
  void reportEndOfSessionWithErrors(const std::string msg,int error_code);
  
82
83
  void startThreads() { m_workerThread.start(); }
  void waitThread() { m_workerThread.wait(); }
84
  
85
private:
86
  
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  /**
   * Log a set of files independently of the success/failure 
   * @param c The set of files to log
   * @param msg The message to be append at the end.
   */
  template <class C> void logReport(const C& c,const std::string& msg){
    using castor::log::LogContext;
    using castor::log::Param;
      for(typename C::const_iterator it=c.begin();it!=c.end();++it)
      {
        LogContext::ScopedParam sp[]={
          LogContext::ScopedParam(m_lc, Param("ID", (*it)->id())),
          LogContext::ScopedParam(m_lc, Param("FILEID",(*it)->fileid())),
          LogContext::ScopedParam(m_lc, Param("FSEQ", (*it)->fseq())),
          LogContext::ScopedParam(m_lc, Param("NSHOST", (*it)->nshost())),
          LogContext::ScopedParam(m_lc, Param("FILETRANSACTIONID", (*it)->fileTransactionId()))
        };
        tape::utils::suppresUnusedVariable(sp);
        m_lc.log(LOG_INFO,msg);
      }
  }
  
109
110
  class Report {
  public:
111
112
    virtual ~Report(){}
    virtual void execute(MigrationReportPacker& packer)=0;
113
  };
114
  class ReportSuccessful :  public Report {
115
    const tapegateway::FileToMigrateStruct m_migratedFile;
116
117
118
119
120
121
122
123
124
  public:
    ReportSuccessful(const tapegateway::FileToMigrateStruct& file): 
    m_migratedFile(file){}
    virtual void execute(MigrationReportPacker& _this);
  };
  class ReportFlush : public Report {
    public:
      void execute(MigrationReportPacker& _this);
  };
125
126
  class ReportError : public Report {
    const tapegateway::FileToMigrateStruct m_migratedFile;
127
128
129
    const std::string m_error_msg;
    const int m_error_code;
  public:
130
    ReportError(const tapegateway::FileToMigrateStruct& file,std::string msg,int error_code):
131
132
133
134
    m_migratedFile(file),m_error_msg(msg),m_error_code(error_code){}
    
    virtual void execute(MigrationReportPacker& _this);
  };
135
  class ReportEndofSession : public Report {
136
137
138
139
140
141
  public:
    virtual void execute(MigrationReportPacker& _this);
  };
  class ReportEndofSessionWithErrors : public Report {
    std::string m_message;
    int m_error_code;
142
  public:
143
    ReportEndofSessionWithErrors(std::string msg,int error_code):
144
145
146
147
148
149
    m_message(msg),m_error_code(error_code){}

    virtual void execute(MigrationReportPacker& _this);
  };
  
  class WorkerThread: public castor::tape::threading::Thread {
150
    MigrationReportPacker & m_parent;
151
152
153
  public:
    WorkerThread(MigrationReportPacker& parent);
    virtual void run();
154
  } m_workerThread;
155
156

  ClientInterface & m_client;
157
  castor::log::LogContext& m_lc;
158
  
159
160
161
  /** 
   * m_fifo is holding all the report waiting to be processed
   */
162
  castor::tape::threading::BlockingQueue<Report*> m_fifo;
163
164
165
166
167
168

  /** 
   * m_listReports is holding all the report waiting to be processed
   */
  std::auto_ptr<tapegateway::FileMigrationReportList> m_listReports;   
  
169
  castor::tape::threading::Mutex m_producterProtection;
170
171
172
173
174
175
176
177
178
  
  /** 
   * Sanity check variable to register if an error has happened 
   * Is set at true as soon as a ReportError has been processed.
   */
  bool m_errorHappened;
  
  
  bool m_continue;
179
};
180
181

}}}}