CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fetch.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_FETCH_H_
6 #define CVMFS_FETCH_H_
7 
8 #include <pthread.h>
9 
10 #include <map>
11 #include <string>
12 #include <vector>
13 
14 #include "cache.h"
15 #include "crypto/hash.h"
16 #include "gtest/gtest_prod.h"
17 #include "network/download.h"
18 #include "network/sink.h"
19 
20 class BackoffThrottle;
21 
22 namespace perf {
23 class Statistics;
24 }
25 
26 namespace cvmfs {
27 
33 class TransactionSink : public Sink {
34  public:
35  TransactionSink(CacheManager *cache_mgr, void *open_txn)
36  : Sink(false), cache_mgr_(cache_mgr), open_txn_(open_txn) { }
37  virtual ~TransactionSink() { }
38 
45  virtual int64_t Write(const void *buf, uint64_t sz) {
46  return cache_mgr_->Write(buf, sz, open_txn_);
47  }
48 
55  virtual int Reset() { return cache_mgr_->Reset(open_txn_); }
56 
65  virtual int Purge() { return Reset(); }
69  virtual bool IsValid() { return cache_mgr_ != NULL && open_txn_ != NULL; }
70 
71  virtual int Flush() { return 0; }
72  virtual bool Reserve(size_t /*size*/) { return true; }
73  virtual bool RequiresReserve() { return false; }
74 
78  virtual std::string Describe() {
79  std::string result = "Transaction sink that is ";
80  result += IsValid() ? "valid" : "invalid";
81  return result;
82  }
83 
84  private:
86  void *open_txn_;
87 };
88 
89 
99  FRIEND_TEST(T_Fetcher, GetTls);
100  FRIEND_TEST(T_Fetcher, SignalWaitingThreads);
101  friend void *TestGetTls(void *data);
102  friend void *TestFetchCollapse(void *data);
103  friend void *TestFetchCollapse2(void *data);
104  friend void TLSDestructor(void *data);
105 
106  public:
109  BackoffThrottle *backoff_throttle,
110  perf::StatisticsTemplate statistics);
111  ~Fetcher();
112 
113  int Fetch(const CacheManager::LabeledObject &object,
114  const std::string &alt_url = "");
115 
116  void ReplaceCacheManager(CacheManager *new_cache_mgr) {
117  cache_mgr_ = new_cache_mgr;
118  }
121 
122  private:
130  pipe_wait[0] = -1;
131  pipe_wait[1] = -1;
132  fetcher = NULL;
133  }
134 
143  int pipe_wait[2];
148  std::vector<int> other_pipes_waiting;
154  };
155 
162  typedef std::map<shash::Any, std::vector<int> *> ThreadQueues;
163 
165  void CleanupTls(ThreadLocalStorage *tls);
166  void SignalWaitingThreads(const int fd, const shash::Any &id,
167  ThreadLocalStorage *tls);
168  int OpenSelect(const CacheManager::LabeledObject &object);
169 
173  pthread_key_t thread_local_storage_;
174 
176  pthread_mutex_t *lock_queues_download_;
177 
182  std::vector<ThreadLocalStorage *> tls_blocks_;
183  pthread_mutex_t *lock_tls_blocks_;
184 
190 };
191 
192 } // namespace cvmfs
193 
194 #endif // CVMFS_FETCH_H_
void CleanupTls(ThreadLocalStorage *tls)
Definition: fetch.cc:52
friend void TLSDestructor(void *data)
Definition: fetch.cc:25
virtual int64_t Write(const void *buf, uint64_t sz)
Definition: fetch.h:45
Fetcher(CacheManager *cache_mgr, download::DownloadManager *download_mgr, BackoffThrottle *backoff_throttle, perf::StatisticsTemplate statistics)
Definition: fetch.cc:206
BackoffThrottle * backoff_throttle_
Definition: fetch.h:187
ThreadLocalStorage * GetTls()
Definition: fetch.cc:61
virtual int Flush()
Definition: fetch.h:71
download::DownloadManager * download_mgr()
Definition: fetch.h:120
perf::Counter * n_invocations
Definition: fetch.h:189
virtual bool IsValid()
Definition: fetch.h:69
CacheManager * cache_mgr_
Definition: fetch.h:185
std::map< shash::Any, std::vector< int > * > ThreadQueues
Definition: fetch.h:162
ThreadQueues queues_download_
Definition: fetch.h:175
CacheManager * cache_mgr()
Definition: fetch.h:119
virtual bool Reserve(size_t)
Definition: fetch.h:72
std::vector< ThreadLocalStorage * > tls_blocks_
Definition: fetch.h:182
std::vector< int > other_pipes_waiting
Definition: fetch.h:148
pthread_mutex_t * lock_tls_blocks_
Definition: fetch.h:183
pthread_mutex_t * lock_queues_download_
Definition: fetch.h:176
CacheManager * cache_mgr_
Definition: fetch.h:85
TransactionSink(CacheManager *cache_mgr, void *open_txn)
Definition: fetch.h:35
int Fetch(const CacheManager::LabeledObject &object, const std::string &alt_url="")
Definition: fetch.cc:82
int OpenSelect(const CacheManager::LabeledObject &object)
Definition: fetch.cc:261
download::DownloadManager * download_mgr_
Definition: fetch.h:186
virtual bool RequiresReserve()
Definition: fetch.h:73
void ReplaceCacheManager(CacheManager *new_cache_mgr)
Definition: fetch.h:116
virtual ~TransactionSink()
Definition: fetch.h:37
void SignalWaitingThreads(const int fd, const shash::Any &id, ThreadLocalStorage *tls)
Definition: fetch.cc:270
FRIEND_TEST(T_Fetcher, GetTls)
virtual int Reset(void *txn)=0
download::JobInfo download_job
Definition: fetch.h:153
friend void * TestFetchCollapse2(void *data)
virtual int Purge()
Definition: fetch.h:65
friend void * TestGetTls(void *data)
perf::Counter * n_downloads
Definition: fetch.h:188
virtual std::string Describe()
Definition: fetch.h:78
virtual int Reset()
Definition: fetch.h:55
pthread_key_t thread_local_storage_
Definition: fetch.h:173
friend void * TestFetchCollapse(void *data)
virtual int64_t Write(const void *buf, uint64_t sz, void *txn)=0