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

26
#include "common/threading/BlockingQueue.hpp"
27
#include "castor/tape/tapeserver/daemon/ReportPackerInterface.hpp"
28
#include "castor/tape/tapeserver/drive/DriveInterface.hpp"
29
#include "scheduler/ArchiveMount.hpp"
30
#include "scheduler/ArchiveJob.hpp"
31
#include <list>
32
#include <memory>
33

34
35
36
37
38
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
 
39
class MigrationReportPacker : public ReportPackerInterface<detail::Migration> {
40
public:
41
42
43
44
  /**
   * @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.
   */
Victor Kotlyar's avatar
Victor Kotlyar committed
45
  MigrationReportPacker(cta::ArchiveMount *archiveMount, cta::log::LogContext&  lc);
46
  
47
48
  ~MigrationReportPacker();
    
49
50
51
52
  /**
   * Create into the MigrationReportPacker a report for the successful migration
   * of migratedFile
   * @param migratedFile the file successfully migrated
53
   * @param checksum the checksum we computed of the file we have just migrated
54
55
56
   * @param blockId The tape logical object ID of the first block of the header
   * of the file. This is 0 (instead of 1) for the first file on the tape (aka
   * fseq = 1).
57
   * @param lc log context provided by the calling thread.
58
   */
59
  virtual void reportCompletedJob(std::unique_ptr<cta::ArchiveJob> successfulArchiveJob, cta::log::LogContext & lc);
60
61
62
63
64
  
  /**
   * Create into the MigrationReportPacker a report for the failled migration
   * of migratedFile
   * @param migratedFile the file which failled 
65
66
   * @param ex the reason for the failure
   * @param lc log context provided by the calling thread.
67
   */
68
  virtual void reportFailedJob(std::unique_ptr<cta::ArchiveJob> failedArchiveJob, const cta::exception::Exception& ex, cta::log::LogContext & lc);
69
70
     
   /**
71
72
    * Create into the MigrationReportPacker a report for the signaling a flusing on tape
    * @param compressStats 
73
74
    * @param lc log context provided by the calling thread.
    *
75
    */
76
  virtual void reportFlush(drive::compressionStats compressStats, cta::log::LogContext & lc);
77
78
79
  
  /**
   * Create into the MigrationReportPacker a report of reaching the end of the tape.
80
   * @param lc log context provided by the calling thread.
81
   */
82
  virtual void reportTapeFull(cta::log::LogContext & lc);
Daniele Kruse's avatar
Daniele Kruse committed
83
84
85
86
87
  
  /**
   * Report the drive state and set it in the central drive register. This
   * function is to be used by the tape thread when running.
   * @param state the new drive state.
88
   * @param lc log context provided by the calling thread.
Daniele Kruse's avatar
Daniele Kruse committed
89
   */
90
  virtual void reportDriveStatus(cta::common::dataStructures::DriveStatus status, cta::log::LogContext & lc);
91
92
93
  
  /**
   * Create into the MigrationReportPacker a report for the nominal end of session
94
   * @param lc log context provided by the calling thread.
95
   */
96
  virtual void reportEndOfSession(cta::log::LogContext & lc);
97
  
98
99
  /**
   * Function for testing purposes. It is used to tell the report packer that this is the last report
100
   * @param lc log context provided by the calling thread.
101
   */
102
  virtual void reportTestGoingToEnd(cta::log::LogContext & lc);
103
  
104
105
106
107
  /**
   * 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
108
   * @param lc log context provided by the calling thread.
109
   */
110
  virtual void reportEndOfSessionWithErrors(const std::string msg,int error_code, cta::log::LogContext & lc);
111

112
113
114
115
  /**
   * Immediately report the end of session to the client.
   * @param msg The error message 
   * @param error_code The error code given by the drive
116
   * @param lc log context provided by the calling thread.
117
   */
118
  virtual void synchronousReportEndWithErrors(const std::string msg,int error_code, cta::log::LogContext & lc);
119
  
120
121
  void startThreads() { m_workerThread.start(); }
  void waitThread() { m_workerThread.wait(); }
122
  
123
124
125
private:
  class Report {
  public:
126
127
    virtual ~Report(){}
    virtual void execute(MigrationReportPacker& packer)=0;
128
  };
129
  class ReportSuccessful :  public Report {
130
131
132
133
    /**
     * The successful archive job to be pushed in the report packer queue and reported later
     */
    std::unique_ptr<cta::ArchiveJob> m_successfulArchiveJob;
134
  public:
135
136
    ReportSuccessful(std::unique_ptr<cta::ArchiveJob> successfulArchiveJob): 
    m_successfulArchiveJob(std::move(successfulArchiveJob)) {}
137
    void execute(MigrationReportPacker& reportPacker) override;
138
  };
139
140
141
  class ReportTestGoingToEnd :  public Report {
  public:
    ReportTestGoingToEnd() {}
142
    virtual void execute(MigrationReportPacker& reportPacker) override {
143
144
145
      reportPacker.m_continue=false;
      reportPacker.m_lc.log(cta::log::DEBUG, "In MigrationReportPacker::ReportTestGoingToEnd::execute(): Reporting session complete.");
      reportPacker.m_archiveMount->complete();
146
    }
147
  };
Daniele Kruse's avatar
Daniele Kruse committed
148
149
  
  class ReportDriveStatus : public Report {
150
    cta::common::dataStructures::DriveStatus m_status;
Daniele Kruse's avatar
Daniele Kruse committed
151
  public:
152
    ReportDriveStatus(cta::common::dataStructures::DriveStatus status): m_status(status) {}
153
    void execute(MigrationReportPacker& reportPacker) override;
Daniele Kruse's avatar
Daniele Kruse committed
154
155
  };
  
156
  class ReportFlush : public Report {
157
    drive::compressionStats m_compressStats;
158
    
159
    public:
160
161
162
163
164
    /* We only can compute the compressed size once we have flushed on the drive
     * We can get from the drive the number of byte it really wrote to tape
     * @param nbByte the number of byte it really wrote to tape between 
     * this flush and the previous one
     *  */
165
      ReportFlush(drive::compressionStats compressStats):m_compressStats(compressStats){}
166
      
167
      void execute(MigrationReportPacker& reportPacker) override;
168
  };
169
170
171
  class ReportTapeFull: public Report {
    public:
      ReportTapeFull() {}
172
      void execute(MigrationReportPacker& reportPacker) override;
173
  };
174
  class ReportError : public Report {
175
    const cta::exception::Exception m_ex;
176
177
178
179
180
    
    /**
     * The failed archive job to be reported immediately
     */
    std::unique_ptr<cta::ArchiveJob> m_failedArchiveJob;
181
  public:
182
    ReportError(std::unique_ptr<cta::ArchiveJob> failedArchiveJob, const cta::exception::Exception &ex):
183
    m_ex(ex), m_failedArchiveJob(std::move(failedArchiveJob)){}
184
    
185
    void execute(MigrationReportPacker& reportPacker) override;
186
  };
187
  class ReportEndofSession : public Report {
188
  public:
189
    virtual void execute(MigrationReportPacker& reportPacker) override;
190
191
192
  };
  class ReportEndofSessionWithErrors : public Report {
    std::string m_message;
193
    int m_errorCode;
194
  public:
195
196
    ReportEndofSessionWithErrors(std::string msg,int errorCode):
    m_message(msg),m_errorCode(errorCode){}
197
    
198
    void execute(MigrationReportPacker& reportPacker) override;
199
200
  };
  
201
  class WorkerThread: public cta::threading::Thread {
202
    MigrationReportPacker & m_parent;
203
204
205
  public:
    WorkerThread(MigrationReportPacker& parent);
    virtual void run();
206
  } m_workerThread;
207
  
208
209
210
  /** 
   * m_fifo is holding all the report waiting to be processed
   */
211
  cta::threading::BlockingQueue<Report*> m_fifo;
212

213

214
  cta::threading::Mutex m_producterProtection;
215
216
217
218
219
220
221
  
  /** 
   * 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;
  
222
223
224
  /* bool to keep the inner thread running. Is set at false 
   * when a end of session (with error) is called
   */
225
  bool m_continue;
226
227
228
229
230
231
232
233
234
235
  
  /**
   * The mount object used to send reports
   */
  cta::ArchiveMount * m_archiveMount;
  
  /**
   * The successful archive jobs to be reported when flushing
   */
  std::queue<std::unique_ptr<cta::ArchiveJob> > m_successfulArchiveJobs;
236
};
237
238

}}}}