CernVM-FS  2.12.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),
37  cache_mgr_(cache_mgr),
38  open_txn_(open_txn) { }
39  virtual ~TransactionSink() { }
40 
47  virtual int64_t Write(const void *buf, uint64_t sz) {
48  return cache_mgr_->Write(buf, sz, open_txn_);
49  }
50 
57  virtual int Reset() {
58  return cache_mgr_->Reset(open_txn_);
59  }
60 
69  virtual int Purge() {
70  return Reset();
71  }
75  virtual bool IsValid() {
76  return cache_mgr_ != NULL && open_txn_ != NULL;
77  }
78 
79  virtual int Flush() { return 0; }
80  virtual bool Reserve(size_t /*size*/) { return true; }
81  virtual bool RequiresReserve() { return false; }
82 
86  virtual std::string Describe() {
87  std::string result = "Transaction sink that is ";
88  result += IsValid() ? "valid" : "invalid";
89  return result;
90  }
91 
92  private:
94  void *open_txn_;
95 };
96 
97 
107  FRIEND_TEST(T_Fetcher, GetTls);
108  FRIEND_TEST(T_Fetcher, SignalWaitingThreads);
109  friend void *TestGetTls(void *data);
110  friend void *TestFetchCollapse(void *data);
111  friend void *TestFetchCollapse2(void *data);
112  friend void TLSDestructor(void *data);
113 
114  public:
117  BackoffThrottle *backoff_throttle,
118  perf::StatisticsTemplate statistics);
119  ~Fetcher();
120 
121  int Fetch(const CacheManager::LabeledObject &object,
122  const std::string &alt_url = "");
123 
124  void ReplaceCacheManager(CacheManager *new_cache_mgr) {
125  cache_mgr_ = new_cache_mgr;
126  }
129 
130  private:
138  pipe_wait[0] = -1;
139  pipe_wait[1] = -1;
140  fetcher = NULL;
141  }
142 
151  int pipe_wait[2];
156  std::vector<int> other_pipes_waiting;
162  };
163 
170  typedef std::map< shash::Any, std::vector<int> * > ThreadQueues;
171 
173  void CleanupTls(ThreadLocalStorage *tls);
174  void SignalWaitingThreads(const int fd, const shash::Any &id,
175  ThreadLocalStorage *tls);
176  int OpenSelect(const CacheManager::LabeledObject &object);
177 
181  pthread_key_t thread_local_storage_;
182 
184  pthread_mutex_t *lock_queues_download_;
185 
190  std::vector<ThreadLocalStorage *> tls_blocks_;
191  pthread_mutex_t *lock_tls_blocks_;
192 
198 };
199 
200 } // namespace cvmfs
201 
202 #endif // CVMFS_FETCH_H_
void CleanupTls(ThreadLocalStorage *tls)
Definition: fetch.cc:51
friend void TLSDestructor(void *data)
Definition: fetch.cc:25
virtual int64_t Write(const void *buf, uint64_t sz)
Definition: fetch.h:47
Fetcher(CacheManager *cache_mgr, download::DownloadManager *download_mgr, BackoffThrottle *backoff_throttle, perf::StatisticsTemplate statistics)
Definition: fetch.cc:208
BackoffThrottle * backoff_throttle_
Definition: fetch.h:195
std::map< shash::Any, std::vector< int > * > ThreadQueues
Definition: fetch.h:170
ThreadLocalStorage * GetTls()
Definition: fetch.cc:60
virtual int Flush()
Definition: fetch.h:79
download::DownloadManager * download_mgr()
Definition: fetch.h:128
perf::Counter * n_invocations
Definition: fetch.h:197
virtual bool IsValid()
Definition: fetch.h:75
CacheManager * cache_mgr_
Definition: fetch.h:193
ThreadQueues queues_download_
Definition: fetch.h:183
CacheManager * cache_mgr()
Definition: fetch.h:127
virtual bool Reserve(size_t)
Definition: fetch.h:80
std::vector< ThreadLocalStorage * > tls_blocks_
Definition: fetch.h:190
std::vector< int > other_pipes_waiting
Definition: fetch.h:156
pthread_mutex_t * lock_tls_blocks_
Definition: fetch.h:191
pthread_mutex_t * lock_queues_download_
Definition: fetch.h:184
CacheManager * cache_mgr_
Definition: fetch.h:93
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:81
int OpenSelect(const CacheManager::LabeledObject &object)
Definition: fetch.cc:263
download::DownloadManager * download_mgr_
Definition: fetch.h:194
virtual bool RequiresReserve()
Definition: fetch.h:81
void ReplaceCacheManager(CacheManager *new_cache_mgr)
Definition: fetch.h:124
virtual ~TransactionSink()
Definition: fetch.h:39
void SignalWaitingThreads(const int fd, const shash::Any &id, ThreadLocalStorage *tls)
Definition: fetch.cc:272
FRIEND_TEST(T_Fetcher, GetTls)
virtual int Reset(void *txn)=0
download::JobInfo download_job
Definition: fetch.h:161
friend void * TestFetchCollapse2(void *data)
virtual int Purge()
Definition: fetch.h:69
friend void * TestGetTls(void *data)
perf::Counter * n_downloads
Definition: fetch.h:196
virtual std::string Describe()
Definition: fetch.h:86
virtual int Reset()
Definition: fetch.h:57
pthread_key_t thread_local_storage_
Definition: fetch.h:181
friend void * TestFetchCollapse(void *data)
virtual int64_t Write(const void *buf, uint64_t sz, void *txn)=0