DiskWriteTask.hpp 5.14 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/tapeserver/daemon/RecallReportPacker.hpp"
David COME's avatar
David COME committed
33
34
35
36
37
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
  
38
/**
39
40
41
 * 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?).
42
 */
David COME's avatar
David COME committed
43
class DiskWriteTask: public DiskWriteTaskInterface, public DataConsumer {
44
public:
45
46
47
48
49
50
  /**
   * Constructor
   * @param fileId: file id of the file to write to disk
   * @param blockCount: number of memory blocks that will be used
   * @param mm: memory manager of the session
   */
51
  DiskWriteTask(int fileId, int blockCount, const std::string& filePath,MemoryManager& mm,RecallReportPacker& report): 
52
  m_fifo(blockCount),m_blockCount(blockCount), m_fileId(fileId), 
53
          m_memManager(mm),m_path(filePath),m_reporter(report){
54
55
    mm.addClient(&m_fifo); 
  }
56
57
  
  /**
58
59
   * Return the numebr of files to write to disk
   * @return always 1
60
   */
61
  virtual int files() { return 1; };
62
63
  
  /**
64
   * @return the number of memory blocks to be used
65
   */
66
  virtual int blocks() { return m_blockCount; }
67
68
  
  /**
69
   * Main routine: takes each memory block in the fifo and writes it to disk
70
   */
71
72
73
  virtual void execute(RecallReportPacker& reporter,log::LogContext& lc) {
    using log::LogContext;
    using log::Param;
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    try{
      tape::diskFile::WriteFile writer(m_path);
      int blockId  = 0;
      
      while(!m_fifo.finished()) {
        MemBlock *mb = m_fifo.popDataBlock();
        //TODO why are we modifying block id ?
        mb->m_fileid = m_fileId;
        mb->m_fileBlock = blockId++;
        writer.write(mb->m_payload.get(),mb->m_payload.size());
        m_memManager.releaseBlock(mb);
      }
      writer.close();
      tapegateway::FileToRecallStruct recalledFile;
      //fill recalledFile
      //We are currently lacking of information to fill recalledFile
      //log is done in m_reporter while processing reportCompletedJob
      m_reporter.reportCompletedJob(recalledFile);
    }
    catch(const castor::exception::Exception& e){
      tapegateway::FileToRecallStruct failedrecalledFile;
      //fill failedrecalledFile
      //log is done in m_reporter while processing reportFailedJob
      m_reporter.reportFailedJob(failedrecalledFile,e.getMessageValue(),e.code());
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    }
  }
  
  /**
   * 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
   */
119
120
121
  virtual void waitCompletion() { 
    volatile castor::tape::threading::MutexLocker ml(&m_producerProtection); 
  }
122
123
124
125
  
  /**
   * Destructor (also waiting for the end of the write operation)
   */
126
127
128
  virtual ~DiskWriteTask() { 
    volatile castor::tape::threading::MutexLocker ml(&m_producerProtection); 
  }
129
130
131
132
133
134
135
136
137
138
139
  
private:
  
  /**
   * The fifo containing the memory blocks holding data to be written to disk
   */
  DataFifo m_fifo;
  
  /**
   * Number of blocks in the fifo
   */
140
  const int m_blockCount;
141
142
143
144
145
  
  /**
   * File id of the file that will be written to disk
   */
  int m_fileId;
146
147
  
  /**
148
   * Reference to the Memory Manager in use
149
   */
150
  MemoryManager & m_memManager;
151
152
  
  /**
153
   * Mutex forcing serial access to the fifo
154
   */
155
  castor::tape::threading::Mutex m_producerProtection;
156
  
157
158
  const std::string m_path;
  RecallReportPacker& m_reporter;
159
};
David COME's avatar
David COME committed
160
161

}}}}