StoreMap.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2019 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_IPC_STORE_MAP_H
10 #define SQUID_IPC_STORE_MAP_H
11 
12 #include "ipc/mem/FlexibleArray.h"
13 #include "ipc/mem/Pointer.h"
14 #include "ipc/ReadWriteLock.h"
15 #include "sbuf/SBuf.h"
16 #include "store/forward.h"
17 #include "store_key_md5.h"
18 
19 #include <functional>
20 
21 namespace Ipc
22 {
23 
24 typedef int32_t StoreMapSliceId;
25 
29 {
30 public:
31  typedef uint32_t Size;
32 
33  StoreMapSlice(): size(0), next(-1) {}
35  size.exchange(o.size);
36  next.exchange(o.next);
37  }
38 
40  size.store(o.size);
41  next.store(o.next);
42  return *this;
43  }
44 
46  void clear() { size = 0; next = -1; }
47 
48  std::atomic<Size> size;
49  std::atomic<StoreMapSliceId> next;
50 };
51 
57 {
58 public:
60 
62  void set(const StoreEntry &anEntry, const cache_key *aKey = nullptr);
64  void exportInto(StoreEntry &) const;
65 
66  void setKey(const cache_key *const aKey);
67  bool sameKey(const cache_key *const aKey) const;
68 
70  void rewind();
71 
72  /* entry state may change immediately after calling these methods unless
73  * the caller holds an appropriate lock */
74  bool empty() const { return !key[0] && !key[1]; }
75  bool reading() const { return lock.readers; }
76  bool writing() const { return lock.writing; }
77  bool complete() const { return !empty() && !writing(); }
78 
79 public:
80  mutable ReadWriteLock lock;
81  std::atomic<uint8_t> waitingToBeFreed;
82  std::atomic<uint8_t> writerHalted;
84 
85  // fields marked with [app] can be modified when appending-while-reading
86  // fields marked with [update] can be modified when updating-while-reading
87 
88  uint64_t key[2] = {0, 0};
89 
90  // STORE_META_STD TLV field from StoreEntry
91  struct Basics {
92  void clear() {
93  timestamp = 0;
94  lastref = 0;
95  expires = 0;
96  lastmod = 0;
97  swap_file_sz.store(0);
98  refcount = 0;
99  flags = 0;
100  }
101  time_t timestamp = 0;
102  time_t lastref = 0;
103  time_t expires = 0;
104  time_t lastmod = 0;
105  std::atomic<uint64_t> swap_file_sz; // [app]
106  uint16_t refcount = 0;
107  uint16_t flags = 0;
108  } basics;
109 
111  std::atomic<StoreMapSliceId> start;
112 
115  std::atomic<StoreMapSliceId> splicingPoint;
116 };
117 
120 template <class C>
122 {
123 public:
124  typedef C Item;
126 
127  explicit StoreMapItems(const int aCapacity): capacity(aCapacity), items(aCapacity) {}
128 
129  size_t sharedMemorySize() const { return SharedMemorySize(capacity); }
130  static size_t SharedMemorySize(const int aCapacity) { return sizeof(StoreMapItems<Item>) + aCapacity*sizeof(Item); }
131 
132  const int capacity;
134 };
135 
138 
142 {
143 public:
145 
146  explicit StoreMapAnchors(const int aCapacity);
147 
148  size_t sharedMemorySize() const;
149  static size_t SharedMemorySize(const int anAnchorLimit);
150 
151  std::atomic<int32_t> count;
152  std::atomic<uint32_t> victim;
153  const int capacity;
155 };
156 // TODO: Find an elegant way to use StoreMapItems in StoreMapAnchors
157 
160 
163 {
164 public:
166  class Edition
167  {
168  public:
169  Edition(): anchor(nullptr), fileNo(-1), name(-1), splicingPoint(-1) {}
170 
172  explicit operator bool() const { return anchor; }
173 
177 
179  StoreMapSliceId splicingPoint;
180  };
181 
182  explicit StoreMapUpdate(StoreEntry *anEntry);
183  StoreMapUpdate(const StoreMapUpdate &other);
184  ~StoreMapUpdate();
185 
186  StoreMapUpdate &operator =(const StoreMapUpdate &other) = delete;
187 
191 };
192 
193 class StoreMapCleaner;
194 
199 class StoreMap
200 {
201 public:
202  typedef StoreMapFileNos FileNos;
205  typedef sfileno AnchorId;
207  typedef StoreMapSlices Slices;
208  typedef StoreMapSliceId SliceId;
210 
211 public:
213  class Owner
214  {
215  public:
216  Owner();
217  ~Owner();
221  private:
222  Owner(const Owner &); // not implemented
223  Owner &operator =(const Owner &); // not implemented
224  };
225 
227  static Owner *Init(const SBuf &path, const int slotLimit);
228 
229  StoreMap(const SBuf &aPath);
230 
232  sfileno fileNoByKey(const cache_key *const key) const;
233 
237  int compareVersions(const sfileno oldFileno, time_t newVersion) const;
238 
241  Anchor *openForWriting(const cache_key *const key, sfileno &fileno);
244  Anchor *openForWritingAt(sfileno fileno, bool overwriteExisting = true);
246  void startAppending(const sfileno fileno);
248  void closeForWriting(const sfileno fileno);
250  void switchWritingToReading(const sfileno fileno);
253  void forgetWritingEntry(const sfileno fileno);
254 
256  bool openForUpdating(Update &update, sfileno fileNoHint);
258  void closeForUpdating(Update &update);
260  void abortUpdating(Update &update);
261 
263  const Anchor *peekAtReader(const sfileno fileno) const;
264 
266  const Anchor &peekAtEntry(const sfileno fileno) const;
267 
270  bool freeEntry(const sfileno);
273  void freeEntryByKey(const cache_key *const key);
274 
277  bool markedForDeletion(const cache_key *const);
278 
280  bool hasReadableEntry(const cache_key *const);
281 
283  const Anchor *openForReading(const cache_key *const key, sfileno &fileno);
285  const Anchor *openForReadingAt(const sfileno fileno);
287  void closeForReading(const sfileno fileno);
288 
290  Slice &writeableSlice(const AnchorId anchorId, const SliceId sliceId);
292  const Slice &readableSlice(const AnchorId anchorId, const SliceId sliceId) const;
294  Anchor &writeableEntry(const AnchorId anchorId);
296  const Anchor &readableEntry(const AnchorId anchorId) const;
297 
299  void prepFreeSlice(const SliceId sliceId);
300 
304  SliceId sliceContaining(const sfileno fileno, const uint64_t nth) const;
305 
307  void abortWriting(const sfileno fileno);
308 
310  bool purgeOne();
311 
313  void importSlice(const SliceId sliceId, const Slice &slice);
314 
315  /* SwapFilenMax limits the number of entries, but not slices or slots */
316  bool validEntry(const int n) const;
317  bool validSlice(const int n) const;
318  int entryCount() const;
319  int entryLimit() const;
320  int sliceLimit() const;
321 
323  void updateStats(ReadWriteLockStats &stats) const;
324 
326 
327 protected:
328  const SBuf path;
332 
333 private:
335  sfileno nameByKey(const cache_key *const key) const;
337  sfileno fileNoByName(const sfileno name) const;
338  void relocate(const sfileno name, const sfileno fileno);
339 
340  Anchor &anchorAt(const sfileno fileno);
341  const Anchor &anchorAt(const sfileno fileno) const;
342  Anchor &anchorByKey(const cache_key *const key);
343 
344  Slice &sliceAt(const SliceId sliceId);
345  const Slice &sliceAt(const SliceId sliceId) const;
346  Anchor *openForReading(Slice &s);
347  bool openKeyless(Update::Edition &edition);
348  void closeForUpdateFinal(Update &update);
349 
350  typedef std::function<bool (const sfileno name)> NameFilter; // a "name"-based test
351  bool visitVictims(const NameFilter filter);
352 
353  void freeChain(const sfileno fileno, Anchor &inode, const bool keepLock);
354  void freeChainAt(SliceId sliceId, const SliceId splicingPoint);
355 };
356 
359 {
360 public:
361  virtual ~StoreMapCleaner() {}
362 
364  virtual void noteFreeMapSlice(const StoreMapSliceId sliceId) = 0;
365 };
366 
367 } // namespace Ipc
368 
369 // We do not reuse FileMap because we cannot control its size,
370 // resulting in sfilenos that are pointing beyond the database.
371 
372 #endif /* SQUID_IPC_STORE_MAP_H */
373 
Mem::Pointer< StoreMapAnchors > anchors
entry inodes (starting blocks)
Definition: StoreMap.h:330
sfileno fileNo
StoreMap::fileNos[name], for convenience/speed.
Definition: StoreMap.h:175
std::atomic< uint64_t > swap_file_sz
Definition: StoreMap.h:105
Slices::Owner * slices
Definition: StoreMap.h:220
Anchors::Owner * anchors
Definition: StoreMap.h:219
std::atomic< StoreMapSliceId > next
ID of the next entry slice.
Definition: StoreMap.h:49
virtual ~StoreMapCleaner()
Definition: StoreMap.h:361
StoreMapSlice & operator=(const StoreMapSlice &o)
Definition: StoreMap.h:39
Aggregates information required for updating entry metadata and headers.
Definition: StoreMap.h:162
std::atomic< uint8_t > waitingToBeFreed
Definition: StoreMap.h:81
Definition: SBuf.h:86
std::atomic< Size > size
slice contents size
Definition: StoreMap.h:48
unsigned char cache_key
Store key.
Definition: forward.h:29
std::function< bool(const sfileno name)> NameFilter
Definition: StoreMap.h:350
StoreMapAnchors Anchors
Definition: StoreMap.h:204
Definition: IpcIoFile.h:23
class Ping::pingStats_ stats
bool reading() const
Definition: StoreMap.h:75
StoreMapCleaner * cleaner
notified before a readable entry is freed
Definition: StoreMap.h:325
StoreMapSliceId SliceId
Definition: StoreMap.h:208
approximate stats of a set of ReadWriteLocks
Definition: ReadWriteLock.h:56
uint32_t Size
Definition: StoreMap.h:31
StoreMapUpdate Update
Definition: StoreMap.h:209
std::atomic< int32_t > count
current number of entries
Definition: StoreMap.h:151
StoreMapSlices Slices
Definition: StoreMap.h:207
bool empty() const
Definition: StoreMap.h:74
bool complete() const
Definition: StoreMap.h:77
API for adjusting external state when dirty map slice is being freed.
Definition: StoreMap.h:358
StoreMapItems(const int aCapacity)
Definition: StoreMap.h:127
StoreMapItems< StoreMapSlice > StoreMapSlices
StoreMapSlices indexed by their slice ID.
Definition: StoreMap.h:137
static size_t SharedMemorySize(const int aCapacity)
Definition: StoreMap.h:130
StoreMapFileNos FileNos
Definition: StoreMap.h:202
void Init(void)
prepares to parse ACLs configuration
Definition: AclRegs.cc:114
int32_t StoreMapSliceId
Definition: StoreMap.h:24
Ipc::Mem::FlexibleArray< Item > items
storage
Definition: StoreMap.h:133
std::atomic< StoreMapSliceId > splicingPoint
Definition: StoreMap.h:115
void clear()
restore default-constructed state
Definition: StoreMap.h:46
const int capacity
total number of anchors
Definition: StoreMap.h:153
StoreMapAnchor * anchor
StoreMap::anchors[fileNo], for convenience/speed.
Definition: StoreMap.h:174
signed_int32_t sfileno
Definition: forward.h:22
size_t sharedMemorySize() const
Definition: StoreMap.h:129
StoreMapItems< std::atomic< sfileno > > StoreMapFileNos
StoreMapAnchor positions, indexed by entry "name" (i.e., the entry key hash)
Definition: StoreMap.h:159
StoreMapSlice Slice
Definition: StoreMap.h:206
StoreMapAnchor Anchor
Definition: StoreMap.h:203
bool writing() const
Definition: StoreMap.h:76
Ipc::Mem::FlexibleArray< StoreMapAnchor > items
anchors storage
Definition: StoreMap.h:154
std::atomic< uint32_t > victim
starting point for purge search
Definition: StoreMap.h:152
Edition stale
old anchor and chain being updated
Definition: StoreMap.h:189
Edition fresh
new anchor and updated chain prefix
Definition: StoreMap.h:190
aggregates anchor and slice owners for Init() caller convenience
Definition: StoreMap.h:213
sfileno name
StoreEntry position in StoreMap::fileNos, for swapping Editions.
Definition: StoreMap.h:176
Ipc::Mem::Owner< StoreMapItems< Item > > Owner
Definition: StoreMap.h:125
const SBuf path
cache_dir path or similar cache name; for logging
Definition: StoreMap.h:328
Mem::Pointer< StoreMapFileNos > fileNos
entry inodes (starting blocks)
Definition: StoreMap.h:329
StoreMapSlice(const StoreMapSlice &o)
Definition: StoreMap.h:34
StoreMapSliceId splicingPoint
the last slice in the chain still containing metadata/headers
Definition: StoreMap.h:179
ReadWriteLock lock
protects slot data below
Definition: StoreMap.h:80
Ipc::Mem::Owner< StoreMapAnchors > Owner
Definition: StoreMap.h:144
std::atomic< StoreMapSliceId > start
where the chain of StoreEntry slices begins [app]
Definition: StoreMap.h:111
sfileno AnchorId
Definition: StoreMap.h:205
FileNos::Owner * fileNos
Definition: StoreMap.h:218
Mem::Pointer< StoreMapSlices > slices
chained entry pieces positions
Definition: StoreMap.h:331
During an update, the stored entry has two editions: stale and fresh.
Definition: StoreMap.h:166
StoreEntry * entry
the store entry being updated
Definition: StoreMap.h:188
const int capacity
total number of items
Definition: StoreMap.h:132

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors