CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cache_stream.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_CACHE_STREAM_H_
6 #define CVMFS_CACHE_STREAM_H_
7 
8 #include <pthread.h>
9 
10 #include <string>
11 
12 #include "cache.h"
13 #include "crypto/hash.h"
14 #include "fd_table.h"
15 #include "ring_buffer.h"
16 #include "smallhash.h"
17 #include "util/pointer.h"
18 
19 namespace download {
20 class DownloadManager;
21 }
22 namespace perf {
23 class Counter;
24 class Statistics;
25 }
26 
32  public:
33  static const size_t kDefaultBufferSize;
34 
35  struct Counters {
43 
44  explicit Counters(perf::Statistics *statistics);
45  };
46 
47  StreamingCacheManager(unsigned max_open_fds,
48  CacheManager *cache_mgr,
49  download::DownloadManager *regular_download_mgr,
50  download::DownloadManager *external_download_mgr,
51  size_t buffer_size,
52  perf::Statistics *statistics);
53  virtual ~StreamingCacheManager();
54 
55  // In the files system / mountpoint initialization, we create the cache
56  // manager before we know about the download manager. Hence we allow to
57  // patch in the download manager at a later point.
59  regular_download_mgr_ = download_mgr;
60  }
62  external_download_mgr_ = download_mgr;
63  }
64 
66  virtual std::string Describe();
67 
69 
70  virtual int Open(const LabeledObject &object);
71  virtual int64_t GetSize(int fd);
72  virtual int Close(int fd);
73  virtual int64_t Pread(int fd, void *buf, uint64_t size, uint64_t offset);
74  virtual int Dup(int fd);
75  virtual int Readahead(int fd);
76 
77  // Only pinned objects and catalogs are written to the cache. Transactions
78  // are passed through to the backing cache manager.
79  virtual uint32_t SizeOfTxn() { return cache_mgr_->SizeOfTxn(); }
80  virtual int StartTxn(const shash::Any &id, uint64_t size, void *txn) {
81  return cache_mgr_->StartTxn(id, size, txn);
82  }
83  virtual void CtrlTxn(const Label &label, const int flags, void *txn) {
84  cache_mgr_->CtrlTxn(label, flags, txn);
85  }
86  virtual int64_t Write(const void *buf, uint64_t size, void *txn)
87  {
88  return cache_mgr_->Write(buf, size, txn);
89  }
90  virtual int Reset(void *txn) { return cache_mgr_->Reset(txn); }
91  virtual int OpenFromTxn(void *txn);
92  virtual int AbortTxn(void *txn) { return cache_mgr_->AbortTxn(txn); }
93  virtual int CommitTxn(void *txn) { return cache_mgr_->CommitTxn(txn); }
94 
95  virtual void Spawn() { cache_mgr_->Spawn(); }
96 
97  virtual manifest::Breadcrumb LoadBreadcrumb(const std::string &fqrn) {
98  return cache_mgr_->LoadBreadcrumb(fqrn);
99  }
101  return cache_mgr_->StoreBreadcrumb(manifest);
102  }
103 
104  // Used in cvmfs' RestoreState to switch back from the streaming to the
105  // regular cache manager. At this point, the streaming cache manager has
106  // opened the root file catalog. We need to return the file descriptor in
107  // the wrapped cache manager, too.
108  CacheManager *MoveOutBackingCacheMgr(int *root_fd);
109  // Used in cvmfs' RestoreState to create a virtual file descriptor for the
110  // root catalog fd, that has been already opened in the backing cache manager
111  int PlantFd(int fd_in_cache_mgr);
112 
113  const Counters &counters() const { return *counters_; }
114 
115  protected:
116  virtual void *DoSaveState();
117  virtual int DoRestoreState(void *data);
118  virtual bool DoFreeState(void *data);
119 
120  private:
121  struct FdInfo {
125 
127  explicit FdInfo(int fd) : fd_in_cache_mgr(fd) {}
128  explicit FdInfo(const CacheManager::LabeledObject &object)
129  : fd_in_cache_mgr(-1), object_id(object.id), label(object.label) {}
130 
131  bool operator ==(const FdInfo &other) const {
132  return this->fd_in_cache_mgr == other.fd_in_cache_mgr &&
133  this->object_id == other.object_id;
134  }
135  bool operator !=(const FdInfo &other) const {
136  return !(*this == other);
137  }
138 
139  bool IsValid() const { return fd_in_cache_mgr >= 0 || !object_id.IsNull(); }
140  };
141 
142  struct SavedState {
144  unsigned int version;
147  };
148 
152 
157  int64_t Stream(const FdInfo &info, void *buf, uint64_t size, uint64_t offset);
158 
162 
163  pthread_mutex_t *lock_fd_table_;
165 
170  pthread_mutex_t *lock_buffer_;
171 
173 }; // class StreamingCacheManager
174 
175 #endif // CVMFS_CACHE_STREAM_H_
Counters(perf::Statistics *statistics)
virtual int Close(int fd)
bool IsNull() const
Definition: hash.h:383
const manifest::Manifest * manifest() const
Definition: repository.h:125
virtual uint32_t SizeOfTxn()
Definition: cache_stream.h:79
int PlantFd(int fd_in_cache_mgr)
virtual bool AcquireQuotaManager(QuotaManager *quota_mgr)
UniquePtr< Counters > counters_
Definition: cache_stream.h:172
pthread_mutex_t * lock_fd_table_
Definition: cache_stream.h:163
virtual int Reset(void *txn)
Definition: cache_stream.h:90
virtual int CommitTxn(void *txn)
Definition: cache_stream.h:93
static const size_t kDefaultBufferSize
Definition: cache_stream.h:33
perf::Counter * sz_transferred_bytes
Definition: cache_stream.h:36
virtual CacheManagerIds id()
Definition: cache_stream.h:65
FdInfo(const CacheManager::LabeledObject &object)
Definition: cache_stream.h:128
virtual void Spawn()
Definition: cache_stream.h:95
SmallHashDynamic< shash::Any, RingBuffer::ObjectHandle_t > buffered_objects_
Definition: cache_stream.h:169
StreamingCacheManager(unsigned max_open_fds, CacheManager *cache_mgr, download::DownloadManager *regular_download_mgr, download::DownloadManager *external_download_mgr, size_t buffer_size, perf::Statistics *statistics)
download::DownloadManager * external_download_mgr_
Definition: cache_stream.h:161
virtual int64_t GetSize(int fd)
download::DownloadManager * regular_download_mgr_
Definition: cache_stream.h:160
virtual int Dup(int fd)
FdTable< FdInfo > fd_table_
Definition: cache_stream.h:164
CacheManager * MoveOutBackingCacheMgr(int *root_fd)
virtual int OpenFromTxn(void *txn)
const Counters & counters() const
Definition: cache_stream.h:113
virtual bool StoreBreadcrumb(const manifest::Manifest &manifest)
Definition: cache_stream.h:100
CacheManagerIds
Definition: cache.h:24
bool operator==(const FdInfo &other) const
Definition: cache_stream.h:131
virtual int64_t Pread(int fd, void *buf, uint64_t size, uint64_t offset)
bool operator!=(const FdInfo &other) const
Definition: cache_stream.h:135
UniquePtr< RingBuffer > buffer_
Definition: cache_stream.h:168
perf::Counter * n_buffer_objects
Definition: cache_stream.h:41
virtual void CtrlTxn(const Label &label, const int flags, void *txn)
Definition: cache_stream.h:83
virtual int AbortTxn(void *txn)
Definition: cache_stream.h:92
UniquePtr< CacheManager > cache_mgr_
Definition: cache_stream.h:159
void SetExternalDownloadManager(download::DownloadManager *download_mgr)
Definition: cache_stream.h:61
download::DownloadManager * SelectDownloadManager(const FdInfo &info)
virtual int StartTxn(const shash::Any &id, uint64_t size, void *txn)
Definition: cache_stream.h:80
pthread_mutex_t * lock_buffer_
Definition: cache_stream.h:170
virtual int64_t Write(const void *buf, uint64_t size, void *txn)
Definition: cache_stream.h:86
virtual ~StreamingCacheManager()
CacheManager::Label label
Definition: cache_stream.h:124
perf::Counter * n_buffer_obstacles
Definition: cache_stream.h:42
QuotaManager * quota_mgr()
Definition: cache.h:193
virtual std::string Describe()
virtual int Readahead(int fd)
void SetRegularDownloadManager(download::DownloadManager *download_mgr)
Definition: cache_stream.h:58
virtual manifest::Breadcrumb LoadBreadcrumb(const std::string &fqrn)
Definition: cache_stream.h:97
virtual int Open(const LabeledObject &object)
virtual bool DoFreeState(void *data)
virtual int DoRestoreState(void *data)
int64_t Stream(const FdInfo &info, void *buf, uint64_t size, uint64_t offset)
static void size_t size
Definition: smalloc.h:54
virtual void * DoSaveState()