TapeWriteSingleThread.hpp 7.53 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
20
21
22
23
24
25
26
27

#pragma once

#include "common/processCap/ProcessCap.hpp"
#include "castor/tape/tapeserver/daemon/MigrationReportPacker.hpp"
#include "castor/tape/tapeserver/daemon/TapeSingleThreadInterface.hpp"
#include "castor/tape/tapeserver/daemon/TapeWriteTask.hpp"
#include "castor/tape/tapeserver/daemon/TaskWatchDog.hpp"
#include "castor/tape/tapeserver/daemon/TapeServerReporter.hpp"
#include "castor/tape/tapeserver/drive/DriveInterface.hpp"
#include "common/threading/BlockingQueue.hpp"
28
#include "common/threading/Thread.hpp"
29
#include "castor/tape/tapeserver/file/File.hpp"
Victor Kotlyar's avatar
Victor Kotlyar committed
30
#include "common/Timer.hpp"
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

#include <iostream>
#include <stdio.h>

namespace castor     {
namespace tape       {
namespace tapeserver {
namespace daemon     {
// forward definition
class MigrationTaskInjector;
  
class TapeWriteSingleThread : public TapeSingleThreadInterface<TapeWriteTask> {
public:
  /**
   * Constructor
   * @param drive an interface for manipulating the drive in order 
   * to write on the tape 
   * @param vid the volume ID of the tape on which we are going to write
   * @param lc 
   * @param repPacker the object that will send reports to the client
   * @param filesBeforeFlush  how many file written before flushing on tape
   * @param bytesBeforeFlush how many bytes written before flushing on tape
   * @param lastFseq the last fSeq 
54
   * @param tapeLoadTimeout the timeout after which we consider the tape mount to be failed
55
56
57
   */
  TapeWriteSingleThread(
    castor::tape::tapeserver::drive::DriveInterface & drive, 
58
    cta::mediachanger::MediaChangerFacade &mc,
59
60
61
    TapeServerReporter & tsr,
    MigrationWatchDog & mwd,
    const VolumeInfo& volInfo,
Victor Kotlyar's avatar
Victor Kotlyar committed
62
    cta::log::LogContext & lc,
63
64
    MigrationReportPacker & repPacker,
    cta::server::ProcessCap &capUtils,
65
    uint64_t filesBeforeFlush, uint64_t bytesBeforeFlush, const bool useLbp,
66
    const std::string & externalEncryptionKeyScript,
67
68
    const cta::ArchiveMount & archiveMount,
    const uint64_t tapeLoadTimeout);
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  
  /**
   * 
   * @param lastFseq
   */
  void setlastFseq(uint64_t lastFseq);
  
  /**
   * Sets up the pointer to the task injector. This cannot be done at
   * construction time as both task injector and tape write single thread refer to 
   * each other. This function should be called before starting the threads.
   * This is used for signalling problems during mounting. After that, each 
   * tape write task does the signalling itself, either on tape problem, or
   * when receiving an error from the disk tasks via memory blocks.
   * @param injector pointer to the task injector
   */
  void setTaskInjector(MigrationTaskInjector* injector){
      m_injector = injector;
  }
private:  
  
  /**
   * Returns the string representation of the specified mount type
   */
93
  const char *mountTypeToString(const cta::common::dataStructures::MountType mountType) const
94
95
96
97
98
    throw();
  
    class TapeCleaning{
    TapeWriteSingleThread& m_this;
    // As we are living in the single thread of tape, we can borrow the timer
Victor Kotlyar's avatar
Victor Kotlyar committed
99
    cta::utils::Timer & m_timer;
100
  public:
Victor Kotlyar's avatar
Victor Kotlyar committed
101
    TapeCleaning(TapeWriteSingleThread& parent, cta::utils::Timer & timer):
102
      m_this(parent), m_timer(timer) {}
103
    ~TapeCleaning();
104
105
106
107
108
109
110
111
112
113
  };
  /**
   * Will throw an exception if we cant write on the tape
   */
  void isTapeWritable() const;

  /**
   * Log  m_stats  parameters into m_logContext with msg at the given level
   */
  void logWithStats(int level,const std::string& msg,
Victor Kotlyar's avatar
Victor Kotlyar committed
114
    cta::log::ScopedParamContainer& params);
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  
  /**
   * Function to open the WriteSession 
   * If successful, returns a std::unique_ptr on it. A copy of that std::unique_ptr
   * will give the caller the ownership of the opened session (see unique_ptr 
   * copy constructor, which has a move semantic)
   * @return the WriteSession we need to write on tape
   */
  std::unique_ptr<castor::tape::tapeFile::WriteSession> openWriteSession();
  /**
   * Execute flush on tape, do some log and report the flush to the client
   * @param message the message the log will register
   * @param bytes the number of bytes that have been written since the last flush  
   * (for logging)
   * @param files the number of files that have been written since the last flush  
   * (also for logging)
   */
  void tapeFlush(const std::string& message,uint64_t bytes,uint64_t files,
Victor Kotlyar's avatar
Victor Kotlyar committed
133
    cta::utils::Timer & timer);
134
  
135
136
137
138
139
140
  /**
   * After waiting for the drive, we will dump the tape alert log content, if it
   * is not empty 
   * @return true if any critical for write alert was detected
   */
  bool logAndCheckTapeAlertsForWrite();
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

  virtual void run() ;
  
  //m_filesBeforeFlush and m_bytesBeforeFlush are thresholds for flushing 
  //the first one crossed will trigger the flush on tape
  
  ///how many file written before flushing on tape
  const uint64_t m_filesBeforeFlush;
  
  ///how many bytes written before flushing on tape
  const uint64_t m_bytesBeforeFlush;

  ///an interface for manipulating all type of drives
  castor::tape::tapeserver::drive::DriveInterface& m_drive;
  
  ///the object that will send reports to the client
  MigrationReportPacker & m_reportPacker;
  
  /**
   * the last fseq that has been written on the tape = the starting point 
   * of our session. The last Fseq is computed by subtracting 1 to fSeg
   * of the first file to migrate we receive. That part is done by the 
   * MigrationTaskInjector.::synchronousInjection. Thus, we compute it into 
   * that function and retrieve/set it within DataTransferSession executeWrite
   * after we make sure synchronousInjection returned true. 
   * 
   * It should be const, but it cant 
   * (because there is no mutable function member in c++)
   */
   uint64_t m_lastFseq;

  /**
   * Should the compression be enabled ? This is currently hard coded to true 
   */
  const bool m_compress;
  
  /**
   * The boolean variable describing to use on not to use Logical
   * Block Protection.
   */
  const bool m_useLbp;
  
  /**
   * Reference to the watchdog, used in run()
   */
  MigrationWatchDog & m_watchdog;
  
188
189
190
191
192
193
194
  /**
   * Reference to the archive mount object that
   * stores the virtual organization (vo) of the tape, the tape pool in which the tape is
   * and the density of the tape
   */
  const cta::ArchiveMount & m_archiveMount;
  
195
196
197
198
199
200
201
protected:
  /***
   * Helper virtual function to access the watchdog from parent class
   */
  virtual void countTapeLogError(const std::string & error) { 
    m_watchdog.addToErrorCount(error);
  }
202
203
204
205
206

  /**
   * Logs SCSI metrics for write session.
   */
  virtual void logSCSIMetrics();
207
208
209
210
211
212
213
214
215
216
217
218
219
  
private:
  /**
   *  Pointer to the task injector allowing termination signaling 
   */
  MigrationTaskInjector* m_injector;

}; // class TapeWriteSingleThread

} // namespace daemon
} // namespace tapeserver
} // namsepace tape
} // namespace castor