DiskWriteTask.hpp 6.15 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
  DiskWriteTask(tape::tapegateway::FileToRecallStruct* file,MemoryManager& mm): 
75
76
  m_recallingFile(file),m_memManager(mm){
 
77
  }
78
  /**
79
   * Main routine: takes each memory block in the fifo and writes it to disk
80
   * @return true if the file has been successfully written false otherwise.
81
   */
82
  virtual bool execute(ReportPackerInterface<detail::Recall>& reporter,log::LogContext& lc) {
83
84
    using log::LogContext;
    using log::Param;
85
    try{
86
      tape::diskFile::WriteFile ourFile(m_recallingFile->path());
87
88
      int blockId  = 0;
      
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
      while(1) {
        if(MemBlock* const mb = m_fifo.pop()) {
          AutoReleaseBlock releaser(mb,m_memManager);
          
          if(m_recallingFile->fileid() != static_cast<unsigned int>(mb->m_fileid)
                  || blockId != mb->m_fileBlock  || mb->m_failled ){
            LogContext::ScopedParam sp[]={
              LogContext::ScopedParam(lc, Param("expected_NSFILEID",m_recallingFile->fileid())),
              LogContext::ScopedParam(lc, Param("received_NSFILEID", mb->m_fileid)),
              LogContext::ScopedParam(lc, Param("expected_NSFBLOCKId", blockId)),
              LogContext::ScopedParam(lc, Param("received_NSFBLOCKId", mb->m_fileBlock)),
              LogContext::ScopedParam(lc, Param("failed_Status", mb->m_failled))
            };
            tape::utils::suppresUnusedVariable(sp);
            lc.log(LOG_ERR,"received a bad block for writing");
            throw castor::tape::Exception("received a bad block for writing");
          }
          mb->m_payload.write(ourFile);
          blockId++;
108
        }
109
110
111
        else 
          break;
      } //end of while(1)
112
      reporter.reportCompletedJob(*m_recallingFile);
113
      return true;
114
115
    }
    catch(const castor::exception::Exception& e){
116
117
118
119
120
121
      /*
       *We might end up there with some blocks into m_fifo
       * We need to empty it
       */
      releaseAllBlock();
      
122
      reporter.reportFailedJob(*m_recallingFile,e.getMessageValue(),e.code());
123
      return false;
124
125
126
127
    }
  }
  
  /**
128
   * Allows client code to return a reusable memory block. Should not been called
129
130
   * @return the pointer to the memory block that can be reused
   */
131
132
133
  virtual MemBlock *getFreeBlock() { 
    throw castor::tape::Exception("DiskWriteTask::getFreeBlock should mot be called");
  }
134
135
136
137
138
139
140
  
  /**
   * 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);
141
    m_fifo.push(mb);
142
  }
143

144
145
146
  /**
   * Destructor (also waiting for the end of the write operation)
   */
147
148
149
  virtual ~DiskWriteTask() { 
    volatile castor::tape::threading::MutexLocker ml(&m_producerProtection); 
  }
150
151
  
private:
152

153
  void releaseAllBlock(){
154
155
156
157
158
159
160
    while(1){
      MemBlock* mb=m_fifo.pop();
      if(mb)
        AutoReleaseBlock release(mb,m_memManager);
      else 
        break;
    }
161
  }
162
  /**
163
   * The fifo containing the memory blocks holding data to be written to disk
164
   */
165
  castor::tape::threading::BlockingQueue<MemBlock *> m_fifo;
166
167
168
  /** 
   * All we need to know about the file we are currently recalling
   */
169
  std::auto_ptr<tape::tapegateway::FileToRecallStruct> m_recallingFile;
170
    
171
  /**
172
   * Reference to the Memory Manager in use
173
   */
174
  MemoryManager & m_memManager;
175
176
  
  /**
177
   * Mutex forcing serial access to the fifo
178
   */
179
  castor::tape::threading::Mutex m_producerProtection;
180
  
181
};
David COME's avatar
David COME committed
182
183

}}}}