CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cache_posix.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_CACHE_POSIX_H_
6 #define CVMFS_CACHE_POSIX_H_
7 
8 #include <stdint.h>
9 #include <sys/types.h>
10 
11 #include <map>
12 #include <string>
13 #include <vector>
14 
15 #include "backoff.h"
16 #include "cache.h"
17 #include "catalog_mgr.h"
18 #include "crypto/signature.h"
19 #include "fd_refcount_mgr.h"
20 #include "file_chunk.h"
21 #include "gtest/gtest_prod.h"
22 #include "manifest_fetch.h"
23 #include "shortstring.h"
24 #include "statistics.h"
25 #include "util/atomic.h"
26 
27 namespace catalog {
28 class DirectoryEntry;
29 class Catalog;
30 }
31 
32 namespace download {
33 class DownloadManager;
34 }
35 
41  FRIEND_TEST(T_CacheManager, CommitTxnQuotaNotifications);
42  FRIEND_TEST(T_CacheManager, CommitTxnRenameFail);
43  FRIEND_TEST(T_CacheManager, Open);
44  FRIEND_TEST(T_CacheManager, OpenFromTxn);
45  FRIEND_TEST(T_CacheManager, OpenPinned);
46  FRIEND_TEST(T_CacheManager, Rename);
47  FRIEND_TEST(T_CacheManager, StartTxn);
48  FRIEND_TEST(T_CacheManager, TearDown2ReadOnly);
49 
50  public:
51  enum CacheModes {
54  };
55 
60  };
61 
67  static const uint64_t kBigFile;
68 
69  virtual CacheManagerIds id() { return kPosixCacheManager; }
70  virtual std::string Describe();
71 
72  static PosixCacheManager *Create(
73  const std::string &cache_path,
74  const bool alien_cache,
75  const RenameWorkarounds rename_workaround = kRenameNormal,
76  const bool do_refcount = true);
77  virtual ~PosixCacheManager() { }
79 
80  virtual int Open(const LabeledObject &object);
81  virtual int64_t GetSize(int fd);
82  virtual int Close(int fd);
83  virtual int64_t Pread(int fd, void *buf, uint64_t size, uint64_t offset);
84  virtual int Dup(int fd);
85  virtual int Readahead(int fd);
86 
87  virtual uint32_t SizeOfTxn() { return sizeof(Transaction); }
88  virtual int StartTxn(const shash::Any &id, uint64_t size, void *txn);
89  virtual void CtrlTxn(const Label &label,
90  const int flags,
91  void *txn);
92  virtual int64_t Write(const void *buf, uint64_t size, void *txn);
93  virtual int Reset(void *txn);
94  virtual int OpenFromTxn(void *txn);
95  virtual int AbortTxn(void *txn);
96  virtual int CommitTxn(void *txn);
97 
98  virtual void Spawn() { }
99 
100  virtual manifest::Breadcrumb LoadBreadcrumb(const std::string &fqrn);
101  virtual bool StoreBreadcrumb(const manifest::Manifest &manifest);
102  bool StoreBreadcrumb(std::string fqrn, manifest::Breadcrumb breadcrumb);
103 
104  void TearDown2ReadOnly();
106  bool alien_cache() { return alien_cache_; }
107  std::string cache_path() { return cache_path_; }
108  bool is_tmpfs() { return is_tmpfs_; }
109  bool do_refcount() const { return do_refcount_; }
110 
111  protected:
112  virtual void *DoSaveState();
113  virtual int DoRestoreState(void *data);
114  virtual bool DoFreeState(void *data);
115 
116  private:
117  bool InitCacheDirectory(const string &cache_path);
118 
119  struct Transaction {
120  Transaction(const shash::Any &id, const std::string &final_path)
121  : buf_pos(0)
122  , size(0)
124  , fd(-1)
125  , label()
126  , tmp_path()
127  , final_path(final_path)
128  , id(id)
129  { }
130 
131  unsigned char buffer[4096];
132  unsigned buf_pos;
133  uint64_t size;
134  uint64_t expected_size;
135  int fd;
137  std::string tmp_path;
138  std::string final_path;
140  };
141 
142  PosixCacheManager(const std::string &cache_path, const bool alien_cache,
143  const bool do_refcount = true)
144  : cache_path_(cache_path)
145  , txn_template_path_(cache_path_ + "/txn/fetchXXXXXX")
146  , alien_cache_(alien_cache)
150  , is_tmpfs_(false)
152  , fd_mgr_(new FdRefcountMgr())
153  {
154  atomic_init32(&no_inflight_txns_);
155  }
156 
157  std::string GetPathInCache(const shash::Any &id);
158  int Rename(const char *oldpath, const char *newpath);
159  int Flush(Transaction *transaction);
160 
161 
162  std::string cache_path_;
163  std::string txn_template_path_;
167 
174 
175  static const char kMagicRefcount = 123;
176  static const char kMagicNoRefcount = '\0';
177  struct SavedState {
182  unsigned int version;
184  };
185 
190 
194  bool is_tmpfs_;
200 }; // class PosixCacheManager
201 
202 #endif // CVMFS_CACHE_POSIX_H_
bool do_refcount() const
Definition: cache_posix.h:109
RenameWorkarounds rename_workaround_
Definition: cache_posix.h:165
virtual int64_t Write(const void *buf, uint64_t size, void *txn)
Definition: cache_posix.cc:624
const manifest::Manifest * manifest() const
Definition: repository.h:125
virtual int Open(const LabeledObject &object)
Definition: cache_posix.cc:414
int Flush(Transaction *transaction)
Definition: cache_posix.cc:384
void TearDown2ReadOnly()
Definition: cache_posix.cc:613
static const char kMagicNoRefcount
Definition: cache_posix.h:176
virtual int CommitTxn(void *txn)
Definition: cache_posix.cc:148
CacheModes cache_mode()
Definition: cache_posix.h:105
int OpenPinned(const LabeledObject &object)
Definition: cache.cc:168
virtual int AbortTxn(void *txn)
Definition: cache_posix.cc:113
virtual void * DoSaveState()
Definition: cache_posix.cc:319
virtual int64_t GetSize(int fd)
Definition: cache_posix.cc:405
virtual bool AcquireQuotaManager(QuotaManager *quota_mgr)
Definition: cache_posix.cc:131
CacheModes cache_mode_
Definition: cache_posix.h:166
virtual void CtrlTxn(const Label &label, const int flags, void *txn)
Definition: cache_posix.cc:293
PosixCacheManager(const std::string &cache_path, const bool alien_cache, const bool do_refcount=true)
Definition: cache_posix.h:142
std::string cache_path_
Definition: cache_posix.h:162
virtual int Dup(int fd)
Definition: cache_posix.cc:376
int Rename(const char *oldpath, const char *newpath)
Definition: cache_posix.cc:469
std::string GetPathInCache(const shash::Any &id)
Definition: cache_posix.cc:400
bool InitCacheDirectory(const string &cache_path)
Definition: cache_posix.cc:231
virtual int OpenFromTxn(void *txn)
Definition: cache_posix.cc:434
virtual int Readahead(int fd)
Definition: cache_posix.cc:499
std::string txn_template_path_
Definition: cache_posix.h:163
int32_t atomic_int32
Definition: atomic.h:17
std::string cache_path()
Definition: cache_posix.h:107
unsigned char buffer[4096]
Definition: cache_posix.h:131
virtual void Spawn()
Definition: cache_posix.h:98
void Transaction()
static PosixCacheManager * Create(const std::string &cache_path, const bool alien_cache, const RenameWorkarounds rename_workaround=kRenameNormal, const bool do_refcount=true)
Definition: cache_posix.cc:272
static const uint64_t kBigFile
Definition: cache_posix.h:67
virtual int StartTxn(const shash::Any &id, uint64_t size, void *txn)
Definition: cache_posix.cc:531
virtual manifest::Breadcrumb LoadBreadcrumb(const std::string &fqrn)
Definition: cache_posix.cc:596
bool reports_correct_filesize_
Definition: cache_posix.h:189
CacheManagerIds
Definition: cache.h:24
static const char kMagicRefcount
Definition: cache_posix.h:175
virtual CacheManagerIds id()
Definition: cache_posix.h:69
FRIEND_TEST(T_CacheManager, CommitTxnQuotaNotifications)
UniquePtr< FdRefcountMgr > fd_mgr_
Definition: cache_posix.h:199
virtual int Reset(void *txn)
Definition: cache_posix.cc:517
QuotaManager * quota_mgr()
Definition: cache.h:193
Transaction(const shash::Any &id, const std::string &final_path)
Definition: cache_posix.h:120
virtual bool DoFreeState(void *data)
Definition: cache_posix.cc:361
virtual int DoRestoreState(void *data)
Definition: cache_posix.cc:331
virtual std::string Describe()
Definition: cache_posix.cc:303
virtual bool StoreBreadcrumb(const manifest::Manifest &manifest)
Definition: cache_posix.cc:602
static void size_t size
Definition: smalloc.h:54
atomic_int32 no_inflight_txns_
Definition: cache_posix.h:173
virtual ~PosixCacheManager()
Definition: cache_posix.h:77
virtual int Close(int fd)
Definition: cache_posix.cc:140
virtual int64_t Pread(int fd, void *buf, uint64_t size, uint64_t offset)
Definition: cache_posix.cc:452
static const uint64_t kSizeUnknown
Definition: cache.h:74
virtual uint32_t SizeOfTxn()
Definition: cache_posix.h:87
UniquePtr< FdRefcountMgr > fd_mgr
Definition: cache_posix.h:183