DiskReadThreadPool.hpp 7.04 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/******************************************************************************
 *                      DiskReadThreadPool.hpp
 *
 * 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.
 *
 * 
 *
22
 * @author Castor Dev team, castor-dev@cern.ch
23
24
25
26
 *****************************************************************************/

#pragma once

27
#include "castor/tape/tapeserver/daemon/DiskReadTask.hpp"
28
29
30
#include "castor/server/BlockingQueue.hpp"
#include "castor/server/Threading.hpp"
#include "castor/server/AtomicCounter.hpp"
31
#include "castor/log/LogContext.hpp"
32
#include <vector>
33
#include <stdint.h>
34

David COME's avatar
David COME committed
35
36
37
38
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
39
  class MigrationTaskInjector;
40
class DiskReadThreadPool {
41
public:
42
  /**
43
44
   * Constructor. The constructor creates the threads for the pool, but does not
   * start them.
45
46
47
48
49
50
51
   * @param nbThread Number of thread for reading files 
   * @param maxFilesReq maximal number of files we might require 
   * within a single request to the task injectore
   * @param maxBytesReq maximal number of bytes we might require
   *  within a single request a single request to the task injectore
   * @param lc log context fpr logging purpose
   */
52
  DiskReadThreadPool(int nbThread, uint64_t maxFilesReq,uint64_t maxBytesReq, 
53
          castor::log::LogContext lc);
54
  
55
56
57
58
59
  /**
   * Destructor.
   * Simply destroys the thread, which should have been joined by the caller
   * before using waitThreads()
   */
60
  ~DiskReadThreadPool();
61
  
62
63
64
  /**
   * Starts the threads which were created at construction time.
   */
65
  void startThreads();
66
67
68
69
70
  
  /**
   * Waits for threads completion of all threads. Should be called before
   * destructor
   */
71
  void waitThreads();
72
  
73
74
75
76
77
78
79
  /**
   * Adds a DiskReadTask to the tape pool's queue of tasks.
   * @param task pointer to the new task. The thread pool takes ownership of the
   * task and will delete it after execution. Push() is not protected against races
   * with finish() as the task injection is done from a single thread (the task
   * injector)
   */
80
  void push(DiskReadTask *task);
81
82
83
84
85
86
87
  
  /**
   * Injects as many "end" tasks as there are threads in the thread pool.
   * A thread getting such an end task will exit. This method is called by the
   * task injector at the end of the tape session. It is not race protected. 
   * See push()
   */
88
  void finish();
89
90
91
92
93
94
95
96
97
  
  /**
   * Sets up the pointer to the task injector. This cannot be done at
   * construction time as both task injector and read thread pool refer to 
   * each other. This function should be called before starting the threads.
   * This is used for the feedback loop where the injector is requested to
   * fetch more work by the read thread pool when the task queue of the thread
   * pool starts to run low.
   */
98
99
100
  void setTaskInjector(MigrationTaskInjector* injector){
      m_injector = injector;
  }
101
private:
102
103
104
105
106
107
108
  /**
   * When the last thread finish, we log all m_pooldStat members + message
   * at the given level
   * @param level
   * @param message
   */
  void logWithStat(int level, const std::string& message);
109
110
111
  /** 
   * Get the next task to execute and if there is not enough tasks in queue,
   * it will ask the TaskInjector to get more jobs.
112
113
   * @return the next task to execute
   */
114
  DiskReadTask* popAndRequestMore(castor::log::LogContext & lc);
115
  
116
117
118
119
120
121
122
123
124
125
  /**
   * When a thread finishm it call this function to Add its stats to one one of the
   * Threadpool
   * @param threadStats
   */
  void addThreadStats(const DiskStats& stats);
  
  /**
   To protect addThreadStats from concurrent calls
   */
126
  castor::server::Mutex m_statAddingProtection;
127
128
129
130
131
132
  
  /**
   * Aggregate all threads' stats 
   */
  DiskStats m_pooldStat;
  
133
134
135
  /**
   * Subclass of the thread pool's worker thread.
   */
136
  class DiskReadWorkerThread: private castor::server::Thread {
137
  public:
138
    DiskReadWorkerThread(DiskReadThreadPool & parent):
139
    m_parent(parent),m_threadID(parent.m_nbActiveThread++),m_lc(parent.m_lc) {
140
       log::LogContext::ScopedParam param(m_lc, log::Param("threadID", m_threadID));
141
       m_lc.log(LOG_INFO,"DisReadThread created");
142
    }
143
144
    void start() { castor::server::Thread::start(); }
    void wait() { castor::server::Thread::wait(); }
145
  private:
146
147
148
149
150
    void logWithStat(int level, const std::string& message);
    /*
     * For measuring how long  are the the different steps 
     */
    DiskStats m_threadStat;
151
    
152
153
154
    /** Pointer to the thread pool, allowing calls to popAndRequestMore,
     * and calling finish() on the task injector when the last thread
     * is finishing (thanks to the actomic counter m_parent.m_nbActiveThread) */
155
    DiskReadThreadPool & m_parent;
156
    
157
    /** The sequential ID of the thread, used in logs */
158
    const int m_threadID;
159
    
160
161
    /** The local copy of the log context, allowing race-free logging with context
     between threads. */
162
    castor::log::LogContext m_lc;
163
    
164
165
    /** The execution thread: pops and executes tasks (potentially asking for
     more) and calls task injector's finish() on exit of the last thread. */
166
    virtual void run();
167
  };
168
  
169
  /** Container for the threads */
170
  std::vector<DiskReadWorkerThread *> m_threads;
171
  
172
173
  /** The queue of pointer to tasks to be executed. We own the tasks (they are 
   * deleted by the threads after execution) */
174
  castor::server::BlockingQueue<DiskReadTask *> m_tasks;
175
  
176
177
178
  /** The log context. This is copied on construction to prevent interferences
   * between threads.
   */
179
  castor::log::LogContext m_lc;
180
  
181
  /** Pointer to the task injector allowing request for more work, and 
182
   * termination signaling */
183
  MigrationTaskInjector* m_injector;
184
  
185
  /** The maximum number of files we ask per request. This value is also used as
186
   * a threshold (half of it, indeed) to trigger the request for more work. 
187
188
   * Another request for more work is also triggered when the task FIFO gets empty.*/
  const uint64_t m_maxFilesReq;
189
190
  
  /** Same as m_maxFilesReq for size per request. */
191
  const uint64_t m_maxBytesReq;
192
  
193
194
  /** An atomic (i.e. thread safe) counter of the current number of thread (they
   are counted up at creation time and down at completion time) */
195
  castor::server::AtomicCounter<int> m_nbActiveThread;
196
};
David COME's avatar
David COME committed
197
198

}}}}