1 |
|
|
/** |
2 |
|
|
* This file is part of the CernVM File System. |
3 |
|
|
* |
4 |
|
|
* This provides a framework for a cache with an upper and a lower branch. |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
#ifndef CVMFS_CACHE_TIERED_H_ |
8 |
|
|
#define CVMFS_CACHE_TIERED_H_ |
9 |
|
|
|
10 |
|
|
#include <string> |
11 |
|
|
|
12 |
|
|
#include "cache.h" |
13 |
|
|
#include "gtest/gtest_prod.h" |
14 |
|
|
|
15 |
|
|
/** |
16 |
|
|
* Cache manager implementation that provides a hierarchical cache. |
17 |
|
|
* Given an "upper" and "lower" cache manager object: |
18 |
|
|
* - Reads are done first from the upper cache |
19 |
|
|
* - On upper cache miss, then this tries the lower cache. |
20 |
|
|
* If there's a lower cache hit, then the file is written |
21 |
|
|
* to the upper cache. |
22 |
|
|
* - Writes are done to both caches simultaneously. |
23 |
|
|
* |
24 |
|
|
* The quota manager is only applied to the upper cache. |
25 |
|
|
*/ |
26 |
|
|
class TieredCacheManager : public CacheManager { |
27 |
|
|
FRIEND_TEST(T_MountPoint, TieredCacheMgr); |
28 |
|
|
FRIEND_TEST(T_MountPoint, TieredComplex); |
29 |
|
|
|
30 |
|
|
public: |
31 |
|
7 |
virtual CacheManagerIds id() { return kTieredCacheManager; } |
32 |
|
|
virtual std::string Describe(); |
33 |
|
|
|
34 |
|
|
static CacheManager *Create(CacheManager *upper_cache, |
35 |
|
|
CacheManager *lower_cache); |
36 |
|
1 |
void SetLowerReadOnly() { lower_readonly_ = true; } |
37 |
|
|
|
38 |
|
|
virtual ~TieredCacheManager(); |
39 |
|
|
virtual bool AcquireQuotaManager(QuotaManager *quota_mgr) { |
40 |
|
|
bool result = upper_->AcquireQuotaManager(quota_mgr); |
41 |
|
|
quota_mgr_ = upper_->quota_mgr(); |
42 |
|
|
return result; |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
virtual int Open(const BlessedObject &object); |
46 |
|
2 |
virtual int64_t GetSize(int fd) {return upper_->GetSize(fd);} |
47 |
|
2 |
virtual int Close(int fd) {return upper_->Close(fd);} |
48 |
|
2 |
virtual int64_t Pread(int fd, void *buf, uint64_t size, uint64_t offset) |
49 |
|
2 |
{ return upper_->Pread(fd, buf, size, offset); } |
50 |
|
|
virtual int Dup(int fd) { return upper_->Dup(fd); } |
51 |
|
|
virtual int Readahead(int fd) { return upper_->Readahead(fd); } |
52 |
|
|
|
53 |
|
3 |
virtual uint32_t SizeOfTxn() |
54 |
|
3 |
{ return upper_->SizeOfTxn() + lower_->SizeOfTxn(); } |
55 |
|
|
virtual int StartTxn(const shash::Any &id, uint64_t size, void *txn); |
56 |
|
|
virtual void CtrlTxn(const ObjectInfo &object_info, |
57 |
|
|
const int flags, |
58 |
|
|
void *txn); |
59 |
|
|
virtual int64_t Write(const void *buf, uint64_t size, void *txn); |
60 |
|
|
virtual int Reset(void *txn); |
61 |
|
|
virtual int OpenFromTxn(void *txn) { return upper_->OpenFromTxn(txn); } |
62 |
|
|
virtual int AbortTxn(void *txn); |
63 |
|
|
virtual int CommitTxn(void *txn); |
64 |
|
|
virtual void Spawn(); |
65 |
|
|
|
66 |
|
30 |
virtual std::string GetBackingDirectory() { return backing_directory_; } |
67 |
|
|
|
68 |
|
|
protected: |
69 |
|
|
virtual void *DoSaveState(); |
70 |
|
|
virtual bool DoRestoreState(void *data); |
71 |
|
|
virtual bool DoFreeState(void *data); |
72 |
|
|
|
73 |
|
|
private: |
74 |
|
|
static const unsigned kCopyBufferSize = 64 * 1024; // 64kB |
75 |
|
|
|
76 |
|
|
struct SavedState { |
77 |
|
|
SavedState() : state_upper(NULL), state_lower(NULL) { } |
78 |
|
|
void *state_upper; |
79 |
|
|
void *state_lower; |
80 |
|
|
}; |
81 |
|
|
|
82 |
|
|
// NOTE: TieredCacheManager takes ownership of both caches passed. |
83 |
|
26 |
TieredCacheManager(CacheManager *upper_cache, |
84 |
|
|
CacheManager *lower_cache) |
85 |
|
26 |
: upper_(upper_cache), lower_(lower_cache), lower_readonly_(false) { } |
86 |
|
|
|
87 |
|
|
CacheManager *upper_; |
88 |
|
|
CacheManager *lower_; |
89 |
|
|
bool lower_readonly_; |
90 |
|
|
std::string backing_directory_; |
91 |
|
|
}; // class TieredCacheManager |
92 |
|
|
|
93 |
|
|
#endif // CVMFS_CACHE_TIERED_H_ |