DiskWriteTask.hpp 4.22 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"
David COME's avatar
David COME committed
32
33
34
35
36
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
  
37
/**
38
39
40
 * 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?).
41
 */
David COME's avatar
David COME committed
42
class DiskWriteTask: public DiskWriteTaskInterface, public DataConsumer {
43
public:
44
45
46
47
48
49
  /**
   * 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
   */
50
51
52
53
54
  DiskWriteTask(int fileId, int blockCount, const std::string& filePath,MemoryManager& mm): 
  m_fifo(blockCount),m_blockCount(blockCount), m_fileId(fileId), 
          m_memManager(mm),m_writer(filePath){
    mm.addClient(&m_fifo); 
  }
55
56
  
  /**
57
58
   * Return the numebr of files to write to disk
   * @return always 1
59
   */
60
  virtual int files() { return 1; };
61
62
  
  /**
63
   * @return the number of memory blocks to be used
64
   */
65
  virtual int blocks() { return m_blockCount; }
66
67
  
  /**
68
   * Main routine: takes each memory block in the fifo and writes it to disk
69
70
   */
  virtual void execute() {
71
72
73
74
75
76
    int blockId  = 0;
    while(!m_fifo.finished()) {
      //printf("+++ In disk write file, id=%d\n",m_fileId);
      MemBlock *mb = m_fifo.popDataBlock();
      mb->m_fileid = m_fileId;
      mb->m_fileBlock = blockId++;
77
      m_writer.write(mb->m_payload.get(),mb->m_payload.size());
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    }
  }
  
  /**
   * 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
   */
  virtual void waitCompletion() { volatile castor::tape::threading::MutexLocker ml(&m_producerProtection); }
  
  /**
   * Destructor (also waiting for the end of the write operation)
   */
David COME's avatar
David COME committed
104
  virtual ~DiskWriteTask() { volatile castor::tape::threading::MutexLocker ml(&m_producerProtection); }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  
private:
  
  /**
   * The fifo containing the memory blocks holding data to be written to disk
   */
  DataFifo m_fifo;
  
  /**
   * Number of blocks in the fifo
   */
  int m_blockCount;
  
  /**
   * File id of the file that will be written to disk
   */
  int m_fileId;
122
123
  
  /**
124
   * Reference to the Memory Manager in use
125
   */
126
  MemoryManager & m_memManager;
127
128
  
  /**
129
   * Mutex forcing serial access to the fifo
130
   */
131
  castor::tape::threading::Mutex m_producerProtection;
132
133
  
  tape::diskFile::WriteFile m_writer;
134
};
David COME's avatar
David COME committed
135
136

}}}}