TapeWriteTask.hpp 5.71 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

David COME's avatar
David COME committed
21
#include "castor/tape/tapeserver/daemon/DataPipeline.hpp"
22
#include "castor/tape/tapeserver/daemon/MigrationMemoryManager.hpp"
23
#include "castor/tape/tapeserver/daemon/DataConsumer.hpp"
24
#include "castor/tape/tapeserver/daemon/TapeWriteSingleThread.hpp"
Victor Kotlyar's avatar
Victor Kotlyar committed
25
#include "common/log/LogContext.hpp"
26
#include "common/threading/Thread.hpp"
27
#include "common/threading/AtomicFlag.hpp"
28
#include "castor/tape/tapeserver/daemon/TapeSessionStats.hpp"
29
#include "castor/tape/tapeserver/daemon/TaskWatchDog.hpp"
Victor Kotlyar's avatar
Victor Kotlyar committed
30
#include "common/Timer.hpp"
31
#include "scheduler/ArchiveJob.hpp"
32

33
34
35
36
37
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {

38
  class MigrationReportPacker;
39
  class Memblock;
40
  class TapeSessionStats;
41
/**
42
43
44
 * The TapeWriteFileTask is responsible to write a single file onto tape as part of a migration
 * session. Being a consumer of memory blocks, it inherits from the DataConsumer class. It also
 * inherits several methods from the TapeWriteTask (TODO: do we really need this base class?).
45
 */
46

47
class TapeWriteTask : public DataConsumer {
48
49
public:
  /**
50
51
52
53
   * Constructor
   * @param fSeq: file sequence number of the file to be written on tape
   * @param blockCount: number of memory blocks (TODO:?)
   * @param mm: reference to the memory manager in use
54
   */
55
  TapeWriteTask(int blockCount, cta::ArchiveJob *archiveJob,
56
          MigrationMemoryManager& mm,cta::threading::AtomicFlag& errorFlag);
57
58
59
  
  
  /**
60
   * @return the size of the file in byte
61
   */
62
  virtual uint64_t fileSize();
63
    
64
65
  /**
   * Main execution routine
66
67
68
69
   * @param session
   * @param reportPacker For reporting success of or failure of the task
   * @param lc For logging
   * @param timer
70
   */
71
  virtual void execute(castor::tape::tapeFile::WriteSession & session,
72
   MigrationReportPacker & reportPacker, MigrationWatchDog & watchdog,
Victor Kotlyar's avatar
Victor Kotlyar committed
73
   cta::log::LogContext&  lc, cta::utils::Timer & timer);
74
  
75
76
77
78
79
80
81
82
private:
  /** Utility class used in execute()'s implementation*/
  class Skip: public std::string {
  public:
    template<typename T> Skip(const T&t): std::string(t) {}
  };
  
public:
83
84
85
86
  /**
   * Used to reclaim used memory blocks
   * @return the recyclable memory block
   */
87
  virtual MemBlock * getFreeBlock();
88
89
90
91
92
  
  /**
   * This is to enqueue one memory block full of data to be written on tape
   * @param mb: the memory block in question
   */
93
  virtual void pushDataBlock(MemBlock *mb) ;
94
95
96
97
  
  /**
   * Destructor
   */
98
  virtual ~TapeWriteTask();
99

100
  /**
101
102
   * Should only be called in case of error !!
   * Just pop data block and put in back into the memory manager
103
   */
104
  void circulateMemBlocks();
105
106
107
108
109
110
  
  /**
   * Return the tasl stats. Should only be called after execute
   * @return 
   */
  const TapeSessionStats getTaskStats() const ;
111
private:
112
  /**
113
   * Log  all localStats' stats +  m_fileToMigrate's parameters
114
115
116
   * into lc with msg at the given level
   */
  void logWithStats(int level, const std::string& msg,
Victor Kotlyar's avatar
Victor Kotlyar committed
117
   cta::log::LogContext&  lc) const;
118
     
119
120
121
122
123
124
125
126
127
  /**
   *Throw an exception if  m_errorFlag is set
   */
  void hasAnotherTaskTailed() const ;
  
  /**
   * This function will check the consistency of the mem block and 
   * throw exception is something goes wrong
   * @param mb The mem block to check
128
   * @param memBlockId The block id the mem blopck should be at
129
130
   * @param lc FOr logging
   */
Victor Kotlyar's avatar
Victor Kotlyar committed
131
  void checkErrors(MemBlock* mb,int memBlockId,cta::log::LogContext&  lc);
132
    
133
134
135
136
137
138
139
  /**
   * Function in charge of opening the WriteFile for m_fileToMigrate
   * Throw an exception it it fails
   * @param session The session on which relies the WriteFile
   * @param lc for logging purpose
   * @return the WriteFile if everything went well
   */
140
  std::unique_ptr<castor::tape::tapeFile::WriteFile> openWriteFile(
Victor Kotlyar's avatar
Victor Kotlyar committed
141
  castor::tape::tapeFile::WriteSession & session,cta::log::LogContext&  lc);
142

143
  /**
144
   * All we need to know about the file we are migrating
145
   */
146
  std::unique_ptr<cta::ArchiveJob> m_archiveJob;
David COME's avatar
David COME committed
147
  
148
149
150
  /**
   * reference to the memory manager in use   
   */
151
  MigrationMemoryManager & m_memManager;
152
153
154
155
  
  /**
   * The fifo containing the memory blocks holding data to be written to tape
   */
David COME's avatar
David COME committed
156
  DataPipeline m_fifo;
157
158
159
160
  
  /**
   * Mutex forcing serial access to the fifo
   */
161
  cta::threading::Mutex m_producerProtection;
162
163
164
165
166
  
  /**
   * The number of memory blocks to be used
   */
  int m_blockCount;
167
  
168
169
170
171
  /**
   * A shared flag among the the tasks and the task injector, set as true as soon
   * as task failed to do its job 
   */
172
  cta::threading::AtomicFlag& m_errorFlag;
173
174
175
176
177
  
  /**
   * Stats
   */
  TapeSessionStats m_taskStats;
178
179
180
181
182

  /**
   * LBP mode tracking
   */
  std::string m_LBPMode;
183
184
185
186
  
  /**
   * The NS archive file information
   */
187
  cta::common::dataStructures::ArchiveFile m_archiveFile;
188
189
190
191
  
  /**
   * The file archive result for the NS
   */
192
  cta::common::dataStructures::TapeFile m_tapeFile;
193
194
195
196
  
  /**
   * The remote file information
   */
197
  std::string m_srcURL; 
198

199
};
200
201
202

}}}}