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 "util/pointer.h"
16 
17 namespace download {
18 class DownloadManager;
19 }
20 
26  public:
27  StreamingCacheManager(unsigned max_open_fds,
28  CacheManager *cache_mgr,
29  download::DownloadManager *regular_download_mgr,
30  download::DownloadManager *external_download_mgr);
31  virtual ~StreamingCacheManager();
32 
33  // In the files system / mountpoint initialization, we create the cache
34  // manager before we know about the download manager. Hence we allow to
35  // patch in the download manager at a later point.
37  regular_download_mgr_ = download_mgr;
38  }
40  external_download_mgr_ = download_mgr;
41  }
42 
44  virtual std::string Describe();
45 
47 
48  virtual int Open(const LabeledObject &object);
49  virtual int64_t GetSize(int fd);
50  virtual int Close(int fd);
51  virtual int64_t Pread(int fd, void *buf, uint64_t size, uint64_t offset);
52  virtual int Dup(int fd);
53  virtual int Readahead(int fd);
54 
55  // Only pinned objects and catalogs are written to the cache. Transactions
56  // are passed through to the backing cache manager.
57  virtual uint32_t SizeOfTxn() { return cache_mgr_->SizeOfTxn(); }
58  virtual int StartTxn(const shash::Any &id, uint64_t size, void *txn) {
59  return cache_mgr_->StartTxn(id, size, txn);
60  }
61  virtual void CtrlTxn(const Label &label, const int flags, void *txn) {
62  cache_mgr_->CtrlTxn(label, flags, txn);
63  }
64  virtual int64_t Write(const void *buf, uint64_t size, void *txn)
65  {
66  return cache_mgr_->Write(buf, size, txn);
67  }
68  virtual int Reset(void *txn) { return cache_mgr_->Reset(txn); }
69  virtual int OpenFromTxn(void *txn);
70  virtual int AbortTxn(void *txn) { return cache_mgr_->AbortTxn(txn); }
71  virtual int CommitTxn(void *txn) { return cache_mgr_->CommitTxn(txn); }
72 
73  virtual void Spawn() { cache_mgr_->Spawn(); }
74 
75  virtual manifest::Breadcrumb LoadBreadcrumb(const std::string &fqrn) {
76  return cache_mgr_->LoadBreadcrumb(fqrn);
77  }
79  return cache_mgr_->StoreBreadcrumb(manifest);
80  }
81 
82  // Used in cvmfs' RestoreState to switch back from the streaming to the
83  // regular cache manager. At this point, the streaming cache manager has
84  // opened the root file catalog. We need to return the file descriptor in
85  // the wrapped cache manager, too.
86  CacheManager *MoveOutBackingCacheMgr(int *root_fd);
87  // Used in cvmfs' RestoreState to create a virtual file descriptor for the
88  // root catalog fd, that has been already opened in the backing cache manager
89  int PlantFd(int fd_in_cache_mgr);
90 
91  protected:
92  virtual void *DoSaveState();
93  virtual int DoRestoreState(void *data);
94  virtual bool DoFreeState(void *data);
95 
96  private:
97  struct FdInfo {
101 
103  explicit FdInfo(int fd) : fd_in_cache_mgr(fd) {}
104  explicit FdInfo(const CacheManager::LabeledObject &object)
105  : fd_in_cache_mgr(-1), object_id(object.id), label(object.label) {}
106 
107  bool operator ==(const FdInfo &other) const {
108  return this->fd_in_cache_mgr == other.fd_in_cache_mgr &&
109  this->object_id == other.object_id;
110  }
111  bool operator !=(const FdInfo &other) const {
112  return !(*this == other);
113  }
114 
115  bool IsValid() const { return fd_in_cache_mgr >= 0 || !object_id.IsNull(); }
116  };
117 
118  struct SavedState {
120  unsigned int version;
123  };
124 
128 
133  int64_t Stream(const FdInfo &info, void *buf, uint64_t size, uint64_t offset);
134 
138 
139  pthread_mutex_t *lock_fd_table_;
141 }; // class StreamingCacheManager
142 
143 #endif // CVMFS_CACHE_STREAM_H_
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:57
int PlantFd(int fd_in_cache_mgr)
virtual bool AcquireQuotaManager(QuotaManager *quota_mgr)
pthread_mutex_t * lock_fd_table_
Definition: cache_stream.h:139
virtual int Reset(void *txn)
Definition: cache_stream.h:68
virtual int CommitTxn(void *txn)
Definition: cache_stream.h:71
virtual CacheManagerIds id()
Definition: cache_stream.h:43
FdInfo(const CacheManager::LabeledObject &object)
Definition: cache_stream.h:104
virtual void Spawn()
Definition: cache_stream.h:73
download::DownloadManager * external_download_mgr_
Definition: cache_stream.h:137
virtual int64_t GetSize(int fd)
download::DownloadManager * regular_download_mgr_
Definition: cache_stream.h:136
virtual int Dup(int fd)
FdTable< FdInfo > fd_table_
Definition: cache_stream.h:140
CacheManager * MoveOutBackingCacheMgr(int *root_fd)
virtual int OpenFromTxn(void *txn)
virtual bool StoreBreadcrumb(const manifest::Manifest &manifest)
Definition: cache_stream.h:78
CacheManagerIds
Definition: cache.h:24
bool operator==(const FdInfo &other) const
Definition: cache_stream.h:107
virtual int64_t Pread(int fd, void *buf, uint64_t size, uint64_t offset)
bool operator!=(const FdInfo &other) const
Definition: cache_stream.h:111
virtual void CtrlTxn(const Label &label, const int flags, void *txn)
Definition: cache_stream.h:61
virtual int AbortTxn(void *txn)
Definition: cache_stream.h:70
UniquePtr< CacheManager > cache_mgr_
Definition: cache_stream.h:135
void SetExternalDownloadManager(download::DownloadManager *download_mgr)
Definition: cache_stream.h:39
download::DownloadManager * SelectDownloadManager(const FdInfo &info)
Definition: cache_stream.cc:90
virtual int StartTxn(const shash::Any &id, uint64_t size, void *txn)
Definition: cache_stream.h:58
virtual int64_t Write(const void *buf, uint64_t size, void *txn)
Definition: cache_stream.h:64
virtual ~StreamingCacheManager()
CacheManager::Label label
Definition: cache_stream.h:100
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:36
virtual manifest::Breadcrumb LoadBreadcrumb(const std::string &fqrn)
Definition: cache_stream.h:75
StreamingCacheManager(unsigned max_open_fds, CacheManager *cache_mgr, download::DownloadManager *regular_download_mgr, download::DownloadManager *external_download_mgr)
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)
Definition: cache_stream.cc:98
static void size_t size
Definition: smalloc.h:54
virtual void * DoSaveState()