RecallReportPacker.hpp 7.63 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
#pragma once
19

20
#include "tapeserver/castor/tape/tapeserver/daemon/ReportPackerInterface.hpp"
Victor Kotlyar's avatar
Victor Kotlyar committed
21
#include "common/log/LogContext.hpp"
22
#include "common/threading/Thread.hpp"
23
#include "common/threading/BlockingQueue.hpp"
24
#include "scheduler/RetrieveJob.hpp"
25
#include "scheduler/RetrieveMount.hpp"
26

27
28
#include <memory>

29
30
31
32
33
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
  
34
class RecallReportPacker : public ReportPackerInterface<detail::Recall> {
35
public:
36
37
  /**
   * Constructor
38
   * @param tg the client to whom we report the success/failures
39
40
   * @param lc log context, copied du to threads
   */
Victor Kotlyar's avatar
Victor Kotlyar committed
41
  RecallReportPacker(cta::RetrieveMount *retrieveMount, cta::log::LogContext lc);
42
  
43
  virtual ~RecallReportPacker();
44
  
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 the DWT has computed for the file 
50
   */
51
  virtual void reportCompletedJob(std::unique_ptr<cta::RetrieveJob> successfulRetrieveJob);
52
53
  
  /**
54
   * Create into the MigrationReportPacker a report for the failed migration
55
   * of migratedFile
56
   * @param migratedFile the file which failed 
57
   * @param ex the reason for the failure
58
   */
59
  virtual void reportFailedJob(std::unique_ptr<cta::RetrieveJob> failedRetrieveJob, const cta::exception::Exception & ex);
60
61
62
63
       
  /**
   * Create into the MigrationReportPacker a report for the nominal end of session
   */
64
  virtual void reportEndOfSession();
65
  
66
67
68
69
70
  /**
   * Function for testing purposes. It is used to tell the report packer that this is the last report
   */
  virtual void reportTestGoingToEnd();
  
71
72
73
74
75
  /**
   * 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
   */
76
  virtual void reportEndOfSessionWithErrors(const std::string msg,int error_code); 
77
78
79
80
81
82
  
  /**
   * 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.
   */
83
  virtual void reportDriveStatus(cta::common::dataStructures::DriveStatus status, const cta::optional<std::string> & reason = cta::nullopt);
84
85
86
87
88
89
90
91
92
93
94
  
  /**
   * Flag disk thread as done.
   */
  virtual void setDiskDone();
  
  /**
   * Flag tape thread as done. Set the drive status to draining if needed.
   */
  virtual void setTapeDone();
  
95
96
97
98
99
100
  void setTapeComplete();
  
  void setDiskComplete();
  
  bool isDiskDone();
  
101
102
103
104
105
  /**
   * Query the status of disk and tape threads (are they both done?).
   * @return true if both tape and disk threads are done.
   */
  virtual bool allThreadsDone();
106

107
108
109
  /**
   * Start the inner thread
   */
110
  void startThreads() { m_workerThread.start(); }
111
112
113
114
  
  /**
   * Stop the inner thread
   */
115
  void waitThread() { m_workerThread.wait(); }
116
  
117
118
119
120
121
  /**
   * Was there an error?
   */
  bool errorHappened();
  
122
private:
123
  //inner classes use to store content while receiving a report 
124
125
126
127
  class Report {
  public:
    virtual ~Report(){}
    virtual void execute(RecallReportPacker& packer)=0;
128
    virtual bool goingToEnd() {return false;}
129
130
131
132
  };
  class ReportTestGoingToEnd :  public Report {
  public:
    ReportTestGoingToEnd() {}
133
    void execute(RecallReportPacker& reportPacker) override {
134
135
    reportPacker.m_retrieveMount->diskComplete();
    reportPacker.m_retrieveMount->tapeComplete();}
136
    bool goingToEnd() override {return true;}
137
138
  };
  class ReportSuccessful :  public Report {
139
    /**
140
     * The successful retrieve job to be reported immediately
141
142
     */
    std::unique_ptr<cta::RetrieveJob> m_successfulRetrieveJob;
143
  public:
144
    ReportSuccessful(std::unique_ptr<cta::RetrieveJob> successfulRetrieveJob): 
145
    m_successfulRetrieveJob(std::move(successfulRetrieveJob)){}
146
    void execute(RecallReportPacker& reportPacker) override;
147
148
  };
  class ReportError : public Report {
149
    const std::string m_failureLog;
150
151
152
153
    /**
     * The failed retrieve job to be reported immediately
     */
    std::unique_ptr<cta::RetrieveJob> m_failedRetrieveJob;
154
  public:
155
156
    ReportError(std::unique_ptr<cta::RetrieveJob> failedRetrieveJob, const std::string &failureLog):
    m_failureLog(failureLog), m_failedRetrieveJob(std::move(failedRetrieveJob)) {}
157

158
    void execute(RecallReportPacker& reportPacker) override;
159
  };
160
  
161
  class ReportDriveStatus : public Report {
162
    cta::common::dataStructures::DriveStatus m_status;
163
    cta::optional<std::string> m_reason;
164
    
165
  public:
166
    ReportDriveStatus(cta::common::dataStructures::DriveStatus status,const cta::optional<std::string> & reason): m_status(status), m_reason(reason) {}
167
168
    void execute(RecallReportPacker& reportPacker) override;
    bool goingToEnd() override;
169
170
  };
  
171
172
  class ReportEndofSession : public Report {
  public:
173
    ReportEndofSession(){}
174
175
    void execute(RecallReportPacker& reportPacker) override;
    bool goingToEnd() override;
176

177
178
179
180
181
182
  };
  class ReportEndofSessionWithErrors : public Report {
    std::string m_message;
    int m_error_code;
  public:
    ReportEndofSessionWithErrors(std::string msg,int error_code):
183
    m_message(msg),m_error_code(error_code){}
184
  
185
186
    void execute(RecallReportPacker& reportPacker) override;
    bool goingToEnd() override;
187
188
  };
  
189
  class WorkerThread: public cta::threading::Thread {
190
191
192
    RecallReportPacker & m_parent;
  public:
    WorkerThread(RecallReportPacker& parent);
193
    void run() override;
194
195
  } m_workerThread;
  
196
  cta::threading::Mutex m_producterProtection;
197
198
199
200
  
  /** 
   * m_fifo is holding all the report waiting to be processed
   */
201
  cta::threading::BlockingQueue<Report*> m_fifo;
202
  
203
204
205
206
207
  /**
   * Is set as true as soon as we process a reportFailedJob
   * That we can do a sanity check to make sure we always call 
   * the right end of the session  
   */
208
  bool m_errorHappened;
209
210
211
212
213
  
  /**
   * The mount object used to send reports
   */
  cta::RetrieveMount * m_retrieveMount;
214
  
215
216
217
218
219
220
  /**
   * The successful reports that were pre-reported asynchronously.
   * They are collected and completed regularly.
   */
  std::queue<std::unique_ptr<cta::RetrieveJob> > m_successfulRetrieveJobs;
  
221
222
223
224
225
226
227
228
  /**
   * Tracking of the tape thread end
   */
  bool m_tapeThreadComplete;
  
  /**
   * Tracking of the disk thread end
   */
229
  bool m_diskThreadComplete;  
230
  
231
  cta::threading::Mutex m_mutex;
232

233
234
235
236
237
238
  /*
   * Proceed finish procedure for async execute for all reports.
   *  
   * @param reportedSuccessfuly The successful reports to check
   * @return The number of reports proceeded
   */
239
  void fullCheckAndFinishAsyncExecute();
240
241
242
243
  
  /*
   * The limit for successful reports to trigger flush.
   */
244
  const unsigned int RECALL_REPORT_PACKER_FLUSH_SIZE = 2000;
245
246
247
248
249
  
  /*
   * The time limit for successful reports to trigger flush.
   */
  const double RECALL_REPORT_PACKER_FLUSH_TIME = 180;
250
251
252
253
254
};

}}}}