MigrationReportPacker.hpp 7.41 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 "castor/server/BlockingQueue.hpp"
27
#include "castor/tape/tapeserver/client/ClientInterface.hpp"
28
#include "castor/tape/tapegateway/FileToMigrateStruct.hpp"
29
#include "castor/tape/tapeserver/daemon/ReportPackerInterface.hpp"
30
#include "castor/tape/tapeserver/drive/DriveInterface.hpp"
31
#include "scheduler/ArchiveMount.hpp"
32
#include "scheduler/ArchiveJob.hpp"
33
#include <list>
34
#include <memory>
35

36
37
38
39
40
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
 
41
class MigrationReportPacker : public ReportPackerInterface<detail::Migration> {
42
public:
43
44
45
46
  /**
   * @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.
   */
47
  MigrationReportPacker(cta::ArchiveMount *archiveMount, log::LogContext lc);
48
  
49
50
  ~MigrationReportPacker();
    
51
52
53
54
  /**
   * Create into the MigrationReportPacker a report for the successful migration
   * of migratedFile
   * @param migratedFile the file successfully migrated
55
   * @param checksum the checksum we computed of the file we have just migrated
56
57
58
   * @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).
59
   */
60
  void reportCompletedJob(std::unique_ptr<cta::ArchiveJob> successfulArchiveJob,
61
  u_int32_t checksum, u_int32_t blockId);
62
63
64
65
66
  
  /**
   * Create into the MigrationReportPacker a report for the failled migration
   * of migratedFile
   * @param migratedFile the file which failled 
67
68
   * @param msg the error message to the failure 
   * @param error_code the error code related to the failure 
69
   */
70
  void reportFailedJob(std::unique_ptr<cta::ArchiveJob> failedArchiveJob,const std::string& msg,int error_code);
71
72
     
   /**
73
74
75
76
    * Create into the MigrationReportPacker a report for the signaling a flusing on tape
    * @param compressStats 
    * 
    */
77
  void reportFlush(drive::compressionStats compressStats);
78
79
80
81
82
83
84
85
86
87
88
89
  
  /**
   * 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);
90

91
92
93
94
95
96
97
  /**
   * Immediately report the end of session to the client.
   * @param msg The error message 
   * @param error_code The error code given by the drive
   */
  void synchronousReportEndWithErrors(const std::string msg,int error_code);
  
98
99
  void startThreads() { m_workerThread.start(); }
  void waitThread() { m_workerThread.wait(); }
100
  
101
102
103
private:
  class Report {
  public:
104
105
    virtual ~Report(){}
    virtual void execute(MigrationReportPacker& packer)=0;
106
  };
107
  class ReportSuccessful :  public Report {
David COME's avatar
David COME committed
108
    const unsigned long m_checksum;
109
    const uint32_t m_blockId;
110
111
112
113
114
    
    /**
     * The successful archive job to be pushed in the report packer queue and reported later
     */
    std::unique_ptr<cta::ArchiveJob> m_successfulArchiveJob;
115
  public:
116
117
    ReportSuccessful(std::unique_ptr<cta::ArchiveJob> successfulArchiveJob, unsigned long checksum, u_int32_t blockId): 
    m_checksum(checksum),m_blockId(blockId), m_successfulArchiveJob(std::move(successfulArchiveJob)) {}
118
    virtual void execute(MigrationReportPacker& reportPacker);
119
120
  };
  class ReportFlush : public Report {
121
    drive::compressionStats m_compressStats;
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    
    /**
     * This function will approximate the compressed size of the files which 
     * have been migrated. The idea is to compute the average ration 
     * logicalSize/nbByteWritenWithCompression for the whole batch 
     * and apply that ratio to the whole set of files
     * We currently computing it only to the file that have been successfully 
     * migrated
     * @param beg Beginning of the upper class' successfulMigrations()
     * @param end End of upper class' successfulMigrations()
     */
    void computeCompressedSize(
    std::vector<tapegateway::FileMigratedNotificationStruct*>::iterator beg,
    std::vector<tapegateway::FileMigratedNotificationStruct*>::iterator end);
    
137
    public:
138
139
140
141
142
    /* 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
     *  */
143
      ReportFlush(drive::compressionStats compressStats):m_compressStats(compressStats){}
144
      
145
      void execute(MigrationReportPacker& reportPacker);
146
  };
147
  class ReportError : public Report {
148
149
    const std::string m_error_msg;
    const int m_error_code;
150
151
152
153
154
    
    /**
     * The failed archive job to be reported immediately
     */
    std::unique_ptr<cta::ArchiveJob> m_failedArchiveJob;
155
  public:
156
157
    ReportError(std::unique_ptr<cta::ArchiveJob> failedArchiveJob, std::string msg,int error_code):
    m_error_msg(msg), m_error_code(error_code), m_failedArchiveJob(std::move(failedArchiveJob)){}
158
    
159
    virtual void execute(MigrationReportPacker& reportPacker);
160
  };
161
  class ReportEndofSession : public Report {
162
  public:
163
    virtual void execute(MigrationReportPacker& reportPacker);
164
165
166
  };
  class ReportEndofSessionWithErrors : public Report {
    std::string m_message;
167
    int m_errorCode;
168
  public:
169
170
    ReportEndofSessionWithErrors(std::string msg,int errorCode):
    m_message(msg),m_errorCode(errorCode){}
171

172
    virtual void execute(MigrationReportPacker& reportPacker);
173
174
  };
  
175
  class WorkerThread: public castor::server::Thread {
176
    MigrationReportPacker & m_parent;
177
178
179
  public:
    WorkerThread(MigrationReportPacker& parent);
    virtual void run();
180
  } m_workerThread;
181
  
182
183
184
  /** 
   * m_fifo is holding all the report waiting to be processed
   */
185
  castor::server::BlockingQueue<Report*> m_fifo;
186

187

188
  castor::server::Mutex m_producterProtection;
189
190
191
192
193
194
195
  
  /** 
   * 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;
  
196
197
198
  /* bool to keep the inner thread running. Is set at false 
   * when a end of session (with error) is called
   */
199
  bool m_continue;
200
201
202
203
204
205
206
207
208
209
  
  /**
   * 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;
210
};
211
212

}}}}