MigrationReportPacker.hpp 9.69 KB
Newer Older
1
2
/*
 * @project        The CERN Tape Archive (CTA)
3
 * @copyright      Copyright(C) 2003-2021 CERN
4
5
6
7
 * @license        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 3 of the License, or
 *                 (at your option) any later version.
8
 *
9
10
11
12
 *                 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.
13
 *
14
15
16
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17
18
19

#pragma once

20
#include "common/threading/BlockingQueue.hpp"
21
22
23
24
/*#include "castor/tape/tapeserver/daemon/ReportPackerInterface.hpp"
#include "castor/tape/tapeserver/drive/DriveInterface.hpp"*/
#include "tapeserver/castor/tape/tapeserver/daemon/ReportPackerInterface.hpp"
#include "tapeserver/castor/tape/tapeserver/drive/DriveInterface.hpp"
25
#include "scheduler/ArchiveMount.hpp"
26
#include "scheduler/ArchiveJob.hpp"
27
#include <list>
28
#include <memory>
29

30
31
32
33
34
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
 
35
class MigrationReportPacker : public ReportPackerInterface<detail::Migration> {
36
public:
37
38
39
40
  /**
   * @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
41
  MigrationReportPacker(cta::ArchiveMount *archiveMount, cta::log::LogContext&  lc);
42
  
43
44
  ~MigrationReportPacker();
    
45
46
47
48
  /**
   * Create into the MigrationReportPacker a report for the successful migration
   * of migratedFile
   * @param migratedFile the file successfully migrated
49
   * @param checksum the checksum we computed of the file we have just migrated
50
51
52
   * @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).
53
   * @param lc log context provided by the calling thread.
54
   */
55
  virtual void reportCompletedJob(std::unique_ptr<cta::ArchiveJob> successfulArchiveJob, cta::log::LogContext & lc);
56
  
57
58
59
60
61
62
63
64
  /**
   * Create into the MigrationReportPacker a report for a skipped file. We left a placeholder on tape, so
   * writing can carry on, but this fSeq holds no data. In the mean time, the job has to count a failure.
   * @param skippedArchiveJob the failed file
   * @param ex the reason for the failure
   * @param lc log context provided by the calling thread.
   */
  virtual void reportSkippedJob(std::unique_ptr<cta::ArchiveJob> skippedArchiveJob, const std::string& failure, cta::log::LogContext & lc);
65
66
67
68
  /**
   * Create into the MigrationReportPacker a report for the failled migration
   * of migratedFile
   * @param migratedFile the file which failled 
69
70
   * @param ex the reason for the failure
   * @param lc log context provided by the calling thread.
71
   */
72
  virtual void reportFailedJob(std::unique_ptr<cta::ArchiveJob> failedArchiveJob, const cta::exception::Exception& ex, cta::log::LogContext & lc);
73
74
     
   /**
75
76
    * Create into the MigrationReportPacker a report for the signaling a flusing on tape
    * @param compressStats 
77
78
    * @param lc log context provided by the calling thread.
    *
79
    */
80
  virtual void reportFlush(drive::compressionStats compressStats, cta::log::LogContext & lc);
81
82
83
  
  /**
   * Create into the MigrationReportPacker a report of reaching the end of the tape.
84
   * @param lc log context provided by the calling thread.
85
   */
86
  virtual void reportTapeFull(cta::log::LogContext & lc);
Daniele Kruse's avatar
Daniele Kruse committed
87
88
89
90
91
  
  /**
   * 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.
92
   * @param lc log context provided by the calling thread.
Daniele Kruse's avatar
Daniele Kruse committed
93
   */
94
  virtual void reportDriveStatus(cta::common::dataStructures::DriveStatus status, const cta::optional<std::string> & reason, cta::log::LogContext & lc);
95
96
97
  
  /**
   * Create into the MigrationReportPacker a report for the nominal end of session
98
   * @param lc log context provided by the calling thread.
99
   */
100
  virtual void reportEndOfSession(cta::log::LogContext & lc);
101
  
102
103
  /**
   * Function for testing purposes. It is used to tell the report packer that this is the last report
104
   * @param lc log context provided by the calling thread.
105
   */
106
  virtual void reportTestGoingToEnd(cta::log::LogContext & lc);
107
  
108
109
110
111
  /**
   * 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
112
   * @param lc log context provided by the calling thread.
113
   */
114
  virtual void reportEndOfSessionWithErrors(const std::string msg,int error_code, cta::log::LogContext & lc);
115

116
117
118
119
  /**
   * Immediately report the end of session to the client.
   * @param msg The error message 
   * @param error_code The error code given by the drive
120
   * @param lc log context provided by the calling thread.
121
   */
122
  virtual void synchronousReportEndWithErrors(const std::string msg,int error_code, cta::log::LogContext & lc);
123
  
124
125
  void startThreads() { m_workerThread.start(); }
  void waitThread() { m_workerThread.wait(); }
126
  
127
128
129
private:
  class Report {
  public:
130
131
    virtual ~Report(){}
    virtual void execute(MigrationReportPacker& packer)=0;
132
  };
133
  class ReportSuccessful :  public Report {
134
135
136
137
    /**
     * The successful archive job to be pushed in the report packer queue and reported later
     */
    std::unique_ptr<cta::ArchiveJob> m_successfulArchiveJob;
138
  public:
139
140
    ReportSuccessful(std::unique_ptr<cta::ArchiveJob> successfulArchiveJob): 
    m_successfulArchiveJob(std::move(successfulArchiveJob)) {}
141
    void execute(MigrationReportPacker& reportPacker) override;
142
  };
143
144
145
146
147
148
149
150
151
152
153
154
155
  
  class ReportSkipped : public Report{
    const std::string m_failureLog;
    /**
     * The failed archive job we skipped
     */
    std::unique_ptr<cta::ArchiveJob> m_skippedArchiveJob;
  public:
    ReportSkipped(std::unique_ptr<cta::ArchiveJob> skippedArchiveJob, std::string &failureLog):
    m_failureLog(failureLog), m_skippedArchiveJob(std::move(skippedArchiveJob)) {}
    void execute(MigrationReportPacker& reportPacker) override;
  };
  
156
157
158
  class ReportTestGoingToEnd :  public Report {
  public:
    ReportTestGoingToEnd() {}
159
    virtual void execute(MigrationReportPacker& reportPacker) override {
160
161
162
      reportPacker.m_continue=false;
      reportPacker.m_lc.log(cta::log::DEBUG, "In MigrationReportPacker::ReportTestGoingToEnd::execute(): Reporting session complete.");
      reportPacker.m_archiveMount->complete();
163
    }
164
  };
Daniele Kruse's avatar
Daniele Kruse committed
165
166
  
  class ReportDriveStatus : public Report {
167
    cta::common::dataStructures::DriveStatus m_status;
168
    cta::optional<std::string> m_reason;
Daniele Kruse's avatar
Daniele Kruse committed
169
  public:
170
    ReportDriveStatus(cta::common::dataStructures::DriveStatus status, const cta::optional<std::string> & reason): m_status(status),m_reason(reason) {}
171
    void execute(MigrationReportPacker& reportPacker) override;
Daniele Kruse's avatar
Daniele Kruse committed
172
173
  };
  
174
  class ReportFlush : public Report {
175
    drive::compressionStats m_compressStats;
176
    
177
    public:
178
179
180
181
182
    /* 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
     *  */
183
      ReportFlush(drive::compressionStats compressStats):m_compressStats(compressStats){}
184
      
185
      void execute(MigrationReportPacker& reportPacker) override;
186
  };
187
188
189
  class ReportTapeFull: public Report {
    public:
      ReportTapeFull() {}
190
      void execute(MigrationReportPacker& reportPacker) override;
191
  };
192
  class ReportError : public Report {
193
    const std::string m_failureLog;
194
195
196
197
198
    
    /**
     * The failed archive job to be reported immediately
     */
    std::unique_ptr<cta::ArchiveJob> m_failedArchiveJob;
199
  public:
200
201
    ReportError(std::unique_ptr<cta::ArchiveJob> failedArchiveJob, std::string &failureLog):
    m_failureLog(failureLog), m_failedArchiveJob(std::move(failedArchiveJob)){}
202
    
203
    void execute(MigrationReportPacker& reportPacker) override;
204
  };
205
  class ReportEndofSession : public Report {
206
  public:
207
    virtual void execute(MigrationReportPacker& reportPacker) override;
208
209
210
  };
  class ReportEndofSessionWithErrors : public Report {
    std::string m_message;
211
    int m_errorCode;
212
  public:
213
214
    ReportEndofSessionWithErrors(std::string msg,int errorCode):
    m_message(msg),m_errorCode(errorCode){}
215
    
216
    void execute(MigrationReportPacker& reportPacker) override;
217
218
  };
  
219
  class WorkerThread: public cta::threading::Thread {
220
    MigrationReportPacker & m_parent;
221
222
223
  public:
    WorkerThread(MigrationReportPacker& parent);
    virtual void run();
224
  } m_workerThread;
225
  
226
227
228
  /** 
   * m_fifo is holding all the report waiting to be processed
   */
229
  cta::threading::BlockingQueue<Report*> m_fifo;
230

231
  
232
  cta::threading::Mutex m_producterProtection;
233
234
235
236
237
238
239
  
  /** 
   * 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;
  
240
241
242
  /* bool to keep the inner thread running. Is set at false 
   * when a end of session (with error) is called
   */
243
  bool m_continue;
244
245
246
247
248
249
250
251
252
253
  
  /**
   * 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;
254
255
256
257
258
  
  /**
   * The skipped files (or placeholders list)
   */
  std::queue<cta::catalogue::TapeItemWritten> m_skippedFiles;
259
};
260
261

}}}}