DiskWriteTask.hpp 6.1 KB
Newer Older
1
/******************************************************************************
2
 *                      DiskWriteFileTask.hpp
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * 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

27
28
29
30
#include "castor/tape/tapeserver/daemon/DiskWriteTask.hpp"
#include "castor/tape/tapeserver/daemon/DataFifo.hpp"
#include "castor/tape/tapeserver/daemon/MemManager.hpp"
#include "castor/tape/tapeserver/daemon/DataConsumer.hpp"
31
#include "castor/tape/tapeserver/file/File.hpp"
32
#include "castor/tape/tapegateway/FileToRecallStruct.hpp"
33
#include <memory>
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
namespace {
  
  uint32_t blockID(const castor::tape::tapegateway::FileToRecallStruct& ftr)
  {
    return (ftr.blockId0() << 24) | (ftr.blockId1() << 16) |  (ftr.blockId2() << 8) | ftr.blockId3();
  }
  
  /*Use RAII to make sure the memory block is released  
   *(ie pushed back to the memory manager) in any case (exception or not)
   */
  class AutoReleaseBlock{
    castor::tape::tapeserver::daemon::MemBlock *block;
    castor::tape::tapeserver::daemon::MemoryManager& memManager;
  public:
    AutoReleaseBlock(castor::tape::tapeserver::daemon::MemBlock* mb, 
            castor::tape::tapeserver::daemon::MemoryManager& mm):
    block(mb),memManager(mm){}
    
    ~AutoReleaseBlock(){
      memManager.releaseBlock(block);
    }
  };
}
David COME's avatar
David COME committed
57
58
59
60
61
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
  
62
/**
63
64
65
 * The DiskWriteFileTask is responsible to write a single file onto disk as part of a recall
 * session. Being a consumer of memory blocks, it inherits from the DataConsumer class. It also
 * inherits several methods from the DiskWriteTask (TODO: do we really need this base class?).
66
 */
David COME's avatar
David COME committed
67
class DiskWriteTask: public DiskWriteTaskInterface, public DataConsumer {
68
public:
69
70
  /**
   * Constructor
71
   * @param file: All we need to know about the file we  are recalling
72
73
   * @param mm: memory manager of the session
   */
74
75
  DiskWriteTask(tape::tapegateway::FileToRecallStruct* file,MemoryManager& mm): 
  m_blockCount(blockID(*file)),m_fifo(m_blockCount),m_recallingFile(file),m_memManager(mm){
76
77
    mm.addClient(&m_fifo); 
  }
78
79
  
  /**
80
   * Return the number of files to write to disk
81
   * @return always 1
82
   */
83
  virtual int files() { return 1; };
84
85
  
  /**
86
   * @return the number of memory blocks to be used
87
   */
88
  virtual int blocks() { return m_blockCount; }
89
90
  
  /**
91
   * Main routine: takes each memory block in the fifo and writes it to disk
92
   * @return true if the file has been successfully written false otherwise.
93
   */
94
  virtual bool execute(RecallReportPacker& reporter,log::LogContext& lc) {
95
96
    using log::LogContext;
    using log::Param;
97
    try{
98
      tape::diskFile::WriteFile ourFile(m_recallingFile->path());
99
100
101
102
      int blockId  = 0;
      
      while(!m_fifo.finished()) {
        MemBlock *mb = m_fifo.popDataBlock();
103
104
        AutoReleaseBlock releaser(mb,m_memManager);
        
105
        if(m_recallingFile->fileid() != static_cast<unsigned int>(mb->m_fileid)
106
107
                || blockId != mb->m_fileBlock){
          LogContext::ScopedParam sp[]={
108
            LogContext::ScopedParam(lc, Param("expectedFILEID",m_recallingFile->fileid())),
109
110
111
112
113
114
115
116
            LogContext::ScopedParam(lc, Param("receivedFILEID", mb->m_fileid)),
            LogContext::ScopedParam(lc, Param("expectedFBLOCKId", blockId)),
            LogContext::ScopedParam(lc, Param("receivedFBLOCKId", mb->m_fileBlock)),
        };
        tape::utils::suppresUnusedVariable(sp);
          lc.log(LOG_ERR,"received a bad block for writing");
          throw castor::tape::Exception("received a bad block for writing");
        }
117
         mb->m_payload.write(ourFile);
118
      }
119
      ourFile.close();
120
      reporter.reportCompletedJob(*m_recallingFile);
121
      return true;
122
123
    }
    catch(const castor::exception::Exception& e){
124
      reporter.reportFailedJob(*m_recallingFile,e.getMessageValue(),e.code());
125
      return false;
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    }
  }
  
  /**
   * Allows client code to return a reusable memory block
   * @return the pointer to the memory block that can be reused
   */
  virtual MemBlock *getFreeBlock() { return m_fifo.getFreeBlock(); }
  
  /**
   * Function used to enqueue a new memory block holding data to be written to disk
   * @param mb: corresponding memory block
   */
  virtual void pushDataBlock(MemBlock *mb) {
    castor::tape::threading::MutexLocker ml(&m_producerProtection);
    m_fifo.pushDataBlock(mb);
  }
  
  /**
   * Function used to wait until the end of the write
   */
147
148
149
  virtual void waitCompletion() { 
    volatile castor::tape::threading::MutexLocker ml(&m_producerProtection); 
  }
150
151
152
153
  
  /**
   * Destructor (also waiting for the end of the write operation)
   */
154
155
156
  virtual ~DiskWriteTask() { 
    volatile castor::tape::threading::MutexLocker ml(&m_producerProtection); 
  }
157
158
159
160
161
  
private:
  /**
   * Number of blocks in the fifo
   */
162
  const int m_blockCount;
163
164
  
  /**
165
   * The fifo containing the memory blocks holding data to be written to disk
166
   */
167
  DataFifo m_fifo;
168
  
169
170
171
  /** 
   * All we need to know about the file we are currently recalling
   */
172
  std::auto_ptr<tape::tapegateway::FileToRecallStruct> m_recallingFile;
173
    
174
  /**
175
   * Reference to the Memory Manager in use
176
   */
177
  MemoryManager & m_memManager;
178
179
  
  /**
180
   * Mutex forcing serial access to the fifo
181
   */
182
  castor::tape::threading::Mutex m_producerProtection;
183
  
184
};
David COME's avatar
David COME committed
185
186

}}}}