Store.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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_STORE_H
10 #define SQUID_STORE_H
11 
12 #include "base/Packable.h"
13 #include "base/Range.h"
14 #include "base/RefCount.h"
15 #include "comm/forward.h"
16 #include "CommRead.h"
17 #include "hash.h"
18 #include "http/forward.h"
19 #include "http/RequestMethod.h"
20 #include "HttpReply.h"
21 #include "MemObject.h"
22 #include "RemovalPolicy.h"
23 #include "store/Controller.h"
24 #include "store/forward.h"
25 #include "store_key_md5.h"
26 #include "StoreIOBuffer.h"
27 #include "StoreStats.h"
28 
29 #if USE_SQUID_ESI
30 #include "esi/Element.h"
31 #endif
32 
33 #include <ostream>
34 
35 class AsyncCall;
36 class HttpRequest;
37 class RequestFlags;
38 
40 
41 class StoreEntry : public hash_link, public Packable
42 {
43 
44 public:
46  bool checkDeferRead(int fd) const;
47 
48  virtual const char *getMD5Text() const;
49  StoreEntry();
50  virtual ~StoreEntry();
51 
52  virtual HttpReply const *getReply() const;
53  virtual void write (StoreIOBuffer);
54 
60  virtual bool isEmpty() const {
61  assert (mem_obj);
62  return mem_obj->endOffset() == 0;
63  }
64  virtual bool isAccepting() const;
65  virtual size_t bytesWanted(Range<size_t> const aRange, bool ignoreDelayPool = false) const;
67  void lengthWentBad(const char *reason);
68  virtual void complete();
69  virtual store_client_t storeClientType() const;
70  virtual char const *getSerialisedMetaData();
72  void storeErrorResponse(HttpReply *reply);
73  void replaceHttpReply(HttpReply *, bool andStartWriting = true);
74  void startWriting();
75  virtual bool mayStartSwapOut();
77  virtual void trimMemory(const bool preserveSwappable);
78 
79  // called when a decision to cache in memory has been made
80  void memOutDecision(const bool willCacheInRam);
81  // called when a decision to cache on disk has been made
82  void swapOutDecision(const MemObject::SwapOut::Decision &decision);
83 
84  void abort();
85  void makePublic(const KeyScope keyScope = ksDefault);
86  void makePrivate(const bool shareable);
90  void clearPrivate();
91  void setPublicKey(const KeyScope keyScope = ksDefault);
95  void clearPublicKeyScope();
96  void setPrivateKey(const bool shareable);
97  void expireNow();
98  void releaseRequest(const bool shareable = false);
99  void negativeCache();
100  void cacheNegatively();
101  void invokeHandlers();
102  void purgeMem();
103  void cacheInMemory();
104  void swapOut();
106  bool swappingOut() const { return swap_status == SWAPOUT_WRITING; }
107  void swapOutFileClose(int how);
108  const char *url() const;
112  bool checkCachable();
113  int checkNegativeHit() const;
114  int locked() const;
115  int validToSend() const;
116  bool memoryCachable();
117 
120  void createMemObject();
121 
123  void createMemObject(const char *storeId, const char *logUri, const HttpRequestMethod &aMethod);
124 
126  void ensureMemObject(const char *storeId, const char *logUri, const HttpRequestMethod &aMethod);
127 
128  void dump(int debug_lvl) const;
129  void hashDelete();
130  void hashInsert(const cache_key *);
131  void registerAbort(STABH * cb, void *);
132  void reset();
134  bool timestampsSet();
135  void unregisterAbort();
136  void destroyMemObject();
137  int checkTooSmall();
138 
140 
141  void setNoDelay (bool const);
142  void lastModified(const time_t when) { lastModified_ = when; }
144  time_t lastModified() const {
145  // may still return -1 if timestamp is not set
146  return lastModified_ < 0 ? timestamp : lastModified_;
147  }
149  const char *describeTimestamps() const;
150  // TODO: consider removing currently unsupported imslen parameter
151  bool modifiedSince(const time_t ims, const int imslen = -1) const;
153  bool hasIfMatchEtag(const HttpRequest &request) const;
155  bool hasIfNoneMatchEtag(const HttpRequest &request) const;
157  bool hasEtag(ETag &etag) const;
158 
160  Store::Disk &disk() const;
161 
164  /* START OF ON-DISK STORE_META_STD TLV field */
165  time_t timestamp;
166  time_t lastref;
167  time_t expires;
168 private:
169  time_t lastModified_;
170 public:
171  uint64_t swap_file_sz;
172  uint16_t refcount;
173  uint16_t flags;
174  /* END OF ON-DISK STORE_META_STD */
175 
177  sfileno swap_filen:25; // keep in sync with SwapFilenMax
178 
180 
182 
184 
186 
188 
189 public:
190  static size_t inUseCount();
192  static void getPublicByRequest(StoreClient * aClient, HttpRequest * request);
193  static void getPublic(StoreClient * aClient, const char *uri, const HttpRequestMethod& method);
194 
195  virtual bool isNull() {
196  return false;
197  };
198 
199  void *operator new(size_t byteCount);
200  void operator delete(void *address);
201  void setReleaseFlag();
202 #if USE_SQUID_ESI
203 
205 #endif
206  virtual int64_t objectLen() const;
207  virtual int64_t contentLen() const;
208 
211  void lock(const char *context);
212 
215  int unlock(const char *context);
216 
218  int locks() const { return static_cast<int>(lock_count); }
219 
221  void touch();
222 
223  virtual void release(const bool shareable = false);
224 
227  bool mayStartHitting() const {
229  }
230 
231 #if USE_ADAPTATION
232  void deferProducer(const AsyncCall::Pointer &producer);
235  void kickProducer();
236 #endif
237 
238  /* Packable API */
239  virtual void append(char const *, int);
240  virtual void vappendf(const char *, va_list);
241  virtual void buffer();
242  virtual void flush();
243 
244 protected:
246 
247 private:
248  bool checkTooBig() const;
249  void forcePublicKey(const cache_key *newkey);
250  void adjustVary();
251  const cache_key *calcPublicKey(const KeyScope keyScope);
252 
254 
255  unsigned short lock_count; /* Assume < 65536! */
256 
263 
264 #if USE_ADAPTATION
267 #endif
268 
269  bool validLength() const;
270  bool hasOneOfEtags(const String &reqETags, const bool allowWeakMatch) const;
271 
272  friend std::ostream &operator <<(std::ostream &os, const StoreEntry &e);
273 };
274 
275 std::ostream &operator <<(std::ostream &os, const StoreEntry &e);
276 
278 class NullStoreEntry:public StoreEntry
279 {
280 
281 public:
282  static NullStoreEntry *getInstance();
283  bool isNull() {
284  return true;
285  }
286 
287  const char *getMD5Text() const;
288  HttpReply const *getReply() const { return NULL; }
290 
291  bool isEmpty () const {return true;}
292 
293  virtual size_t bytesWanted(Range<size_t> const aRange, bool) const { return aRange.end; }
294 
295  void operator delete(void *address);
296  void complete() {}
297 
298 private:
300 
301  char const *getSerialisedMetaData();
302  virtual bool mayStartSwapOut() { return false; }
303 
304  void trimMemory(const bool) {}
305 
307 };
308 
310 typedef void (*STOREGETCLIENT) (StoreEntry *, void *cbdata);
311 
312 namespace Store {
313 void Stats(StoreEntry *output);
314 void Maintain(void *unused);
315 };
316 
318 size_t storeEntryInUse();
319 
321 const char *storeEntryFlags(const StoreEntry *);
322 
324 void storeEntryReplaceObject(StoreEntry *, HttpReply *);
325 
327 StoreEntry *storeGetPublic(const char *uri, const HttpRequestMethod& method);
328 
330 StoreEntry *storeGetPublicByRequest(HttpRequest * request, const KeyScope keyScope = ksDefault);
331 
334 
337 StoreEntry *storeCreateEntry(const char *, const char *, const RequestFlags &, const HttpRequestMethod&);
338 
341 StoreEntry *storeCreatePureEntry(const char *storeId, const char *logUrl, const RequestFlags &, const HttpRequestMethod&);
342 
344 void storeInit(void);
345 
347 void storeConfigure(void);
348 
350 void storeFreeMemory(void);
351 
353 int expiresMoreThan(time_t, time_t);
354 
356 void storeAppendPrintf(StoreEntry *, const char *,...) PRINTF_FORMAT_ARG2;
357 
359 void storeAppendVPrintf(StoreEntry *, const char *, va_list ap);
360 
362 int storeTooManyDiskFilesOpen(void);
363 
365 void storeHeapPositionUpdate(StoreEntry *, SwapDir *);
366 
368 void storeSwapFileNumberSet(StoreEntry * e, sfileno filn);
369 
371 void storeFsInit(void);
372 
374 void storeFsDone(void);
375 
377 void storeReplAdd(const char *, REMOVALPOLICYCREATE *);
378 
380 extern FREE destroyStoreEntry;
381 
383 void storeGetMemSpace(int size);
384 
385 #endif /* SQUID_STORE_H */
386 
bool isNull()
Definition: Store.h:283
sdirno swap_dirn
Definition: Store.h:179
bool shareableWhenPrivate
Definition: Store.h:262
static MemAllocator * pool
Definition: Store.h:253
void storeAppendPrintf(StoreEntry *, const char *,...) PRINTF_FORMAT_ARG2
Definition: store.cc:904
virtual bool isAccepting() const
Definition: store.cc:2068
void makePrivate(const bool shareable)
Definition: store.cc:150
#define assert(EX)
Definition: assert.h:17
store_client_t storeClientType() const
Definition: Store.h:299
StoreEntry * storeGetPublic(const char *uri, const HttpRequestMethod &method)
Definition: store.cc:549
void const char HLPCB * callback
Definition: stub_helper.cc:16
void touch()
update last reference timestamp and related Store metadata
Definition: store.cc:455
static void getPublicByRequestMethod(StoreClient *aClient, HttpRequest *request, const HttpRequestMethod &method)
Definition: store.cc:513
store_status_t
Definition: enums.h:45
int validToSend() const
Definition: store.cc:1485
virtual void release(const bool shareable=false)
Definition: store.cc:1250
int unlock(const char *context)
Definition: store.cc:485
Definition: ETag.h:17
Definition: cbdata.cc:60
void(* STOREGETCLIENT)(StoreEntry *, void *cbdata)
Definition: Store.h:310
int locks() const
returns a local concurrent use counter, for debugging
Definition: Store.h:218
virtual int64_t contentLen() const
Definition: store.cc:1734
void storeEntryReplaceObject(StoreEntry *, HttpReply *)
bool timestampsSet()
Definition: store.cc:1525
virtual void append(char const *, int)
Appends a c-string to existing packed data.
Definition: store.cc:844
unsigned char cache_key
Store key.
Definition: forward.h:29
char const * getSerialisedMetaData()
Definition: store.cc:2167
void makePublic(const KeyScope keyScope=ksDefault)
Definition: store.cc:142
struct _request * request(char *urlin)
Definition: tcp-banger2.c:291
int64_t endOffset() const
Definition: MemObject.cc:232
void setPublicKey(const KeyScope keyScope=ksDefault)
Definition: store.cc:623
static DeferredRead::DeferrableRead DeferReader
Definition: Store.h:45
virtual ~StoreEntry()
Definition: store.cc:343
virtual void buffer()
Definition: store.cc:1707
void lastModified(const time_t when)
Definition: Store.h:142
void expireNow()
Definition: store.cc:815
void Stats(StoreEntry *output)
Definition: store.cc:115
static size_t inUseCount()
Definition: store.cc:173
StoreEntry * storeGetPublicByRequestMethod(HttpRequest *request, const HttpRequestMethod &method, const KeyScope keyScope=ksDefault)
Definition: store.cc:555
void ensureMemObject(const char *storeId, const char *logUri, const HttpRequestMethod &aMethod)
initialize mem_obj (if needed) and set URIs/method (if missing)
Definition: store.cc:1695
Definition: Range.h:18
virtual void flush()
Definition: store.cc:1718
bool swappingOut() const
whether we are in the process of writing this entry to disk
Definition: Store.h:106
std::ostream & operator<<(std::ostream &os, const StoreEntry &e)
Definition: store.cc:2091
sfileno swap_filen
unique ID inside a cache_dir for swapped out entries; -1 for others
Definition: Store.h:177
void delayAwareRead(const Comm::ConnectionPointer &conn, char *buf, int len, AsyncCall::Pointer callback)
Definition: store.cc:199
void storeFsDone(void)
void FREE(void *)
Definition: forward.h:36
int conn
the current server connection FD
Definition: Transport.cc:26
time_t expires
Definition: Store.h:167
void forcePublicKey(const cache_key *newkey)
Definition: store.cc:670
void write(StoreIOBuffer)
Definition: Store.h:289
void memOutDecision(const bool willCacheInRam)
Definition: store.cc:1923
#define PRINTF_FORMAT_ARG2
void destroyMemObject()
Definition: store.cc:370
time_t timestamp
Definition: Store.h:165
void replaceHttpReply(HttpReply *, bool andStartWriting=true)
Definition: store.cc:1854
void setReleaseFlag()
Definition: store.cc:461
void storeInit(void)
Definition: store.cc:1390
ping_status_t
Definition: enums.h:39
static void getPublicByRequest(StoreClient *aClient, HttpRequest *request)
Definition: store.cc:525
void storeReplAdd(const char *, REMOVALPOLICYCREATE *)
Definition: store.cc:1773
bool hasIfMatchEtag(const HttpRequest &request) const
has ETag matching at least one of the If-Match etags
Definition: store.cc:2008
virtual void complete()
Definition: store.cc:1088
bool validLength() const
Definition: store.cc:1337
void purgeMem()
Definition: store.cc:434
void adjustVary()
Definition: store.cc:707
static void getPublic(StoreClient *aClient, const char *uri, const HttpRequestMethod &method)
Definition: store.cc:537
void invokeHandlers()
virtual char const * getSerialisedMetaData()
Definition: store.cc:1892
StoreEntry * storeGetPublicByRequest(HttpRequest *request, const KeyScope keyScope=ksDefault)
Definition: store.cc:561
void hashDelete()
Definition: store.cc:421
int locked() const
Definition: store.cc:1320
void storeSwapFileNumberSet(StoreEntry *e, sfileno filn)
uint64_t swap_file_sz
Definition: Store.h:171
ESIElement::Pointer cachedESITree
Definition: Store.h:204
HttpReply const * getReply() const
Definition: Store.h:288
ping_status_t ping_status
Definition: Store.h:183
void hashInsert(const cache_key *)
Definition: store.cc:413
bool checkDeferRead(int fd) const
Definition: store.cc:244
store_client_t
Definition: enums.h:56
StoreEntry()
Definition: store.cc:322
const char * describeTimestamps() const
Definition: store.cc:2080
void storeFsInit(void)
Definition: store.cc:1764
void storeConfigure(void)
Definition: store.cc:1404
virtual size_t bytesWanted(Range< size_t > const aRange, bool) const
Definition: Store.h:293
virtual int64_t objectLen() const
Definition: store.cc:1727
void abort()
Definition: store.cc:1134
time_t lastModified() const
Definition: Store.h:144
size_t storeEntryInUse()
bool checkCachable()
Definition: store.cc:981
virtual HttpReply const * getReply() const
Definition: store.cc:1742
mem_status_t mem_status
Definition: Store.h:181
static NullStoreEntry * getInstance()
Definition: store.cc:2149
StoreEntry * storeCreateEntry(const char *, const char *, const RequestFlags &, const HttpRequestMethod &)
Definition: store.cc:800
bool isEmpty() const
Definition: Store.h:291
bool modifiedSince(const time_t ims, const int imslen=-1) const
Definition: store.cc:1960
int storeTooManyDiskFilesOpen(void)
Definition: store.cc:940
void storeGetMemSpace(int size)
Definition: store.cc:1185
void dump(int debug_lvl) const
Definition: store.cc:1605
int checkNegativeHit() const
Definition: store.cc:1431
bool hasOneOfEtags(const String &reqETags, const bool allowWeakMatch) const
whether at least one of the request ETags matches entity ETag
Definition: store.cc:2026
Store statistics related to low-level I/O.
Definition: StoreStats.h:58
static NullStoreEntry _instance
Definition: Store.h:306
void releaseRequest(const bool shareable=false)
Definition: store.cc:474
uint16_t flags
Definition: Store.h:173
virtual void vappendf(const char *, va_list)
Definition: store.cc:862
void setNoDelay(bool const)
Definition: store.cc:250
RemovalPolicyNode repl
Definition: Store.h:163
void cacheInMemory()
start or continue storing in memory cache
void trimMemory(const bool)
Definition: Store.h:304
void complete()
Definition: Store.h:296
signed_int32_t sfileno
Definition: forward.h:22
MemObject * mem_obj
Definition: Store.h:162
friend std::ostream & operator<<(std::ostream &os, const StoreEntry &e)
Definition: store.cc:2091
int checkTooSmall()
Definition: store.cc:952
C end
Definition: Range.h:25
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
const char * url() const
Definition: store.cc:1672
bool mayStartHitting() const
Definition: Store.h:227
uint16_t refcount
Definition: Store.h:172
void STABH(void *)
Definition: MemObject.h:27
void const char * buf
Definition: stub_helper.cc:16
void DeferrableRead(void *context, CommRead const &)
Definition: CommRead.h:35
void lengthWentBad(const char *reason)
flags [truncated or too big] entry with ENTRY_BAD_LENGTH and releases it
Definition: store.cc:1080
void Maintain(void *unused)
Definition: store.cc:1235
const char * getMD5Text() const
Definition: store.cc:2155
void storeFreeMemory(void)
Definition: store.cc:1466
AsyncCall::Pointer deferredProducer
producer callback registered with deferProducer
Definition: Store.h:266
size_t HttpReply *STUB StoreEntry const KeyScope scope const HttpRequestMethod const KeyScope scope const char const RequestFlags const HttpRequestMethod const char * logUrl
Definition: stub_store.cc:128
signed int sdirno
Definition: forward.h:23
unsigned short lock_count
Definition: Store.h:255
const cache_key * calcPublicKey(const KeyScope keyScope)
Definition: store.cc:696
void cacheNegatively()
Definition: store.cc:165
virtual const char * getMD5Text() const
Definition: store.cc:181
store_status_t store_status
Definition: Store.h:185
static const char * storeId(const int i)
Definition: testRock.cc:177
void swapOutDecision(const MemObject::SwapOut::Decision &decision)
Definition: store.cc:1929
StoreEntry * storeCreatePureEntry(const char *storeId, const char *logUrl, const RequestFlags &, const HttpRequestMethod &)
Definition: store.cc:776
void swapOutFileClose(int how)
virtual void write(StoreIOBuffer)
Definition: store.cc:822
Store::Disk & disk() const
the disk this entry is [being] cached on; asserts for entries w/o a disk
Definition: store.cc:2055
bool memoryCachable()
checkCachable() and can be cached in memory
Definition: store.cc:1410
virtual bool mayStartSwapOut()
whether we may start writing to disk (now or in the future)
virtual bool mayStartSwapOut()
whether we may start writing to disk (now or in the future)
Definition: Store.h:302
KeyScope
Definition: store_key_md5.h:18
swap_status_t
Definition: enums.h:50
StoreIoStats store_io_stats
Definition: store_io.cc:16
virtual bool isNull()
Definition: Store.h:195
size_t HttpReply *STUB StoreEntry const KeyScope scope const HttpRequestMethod & method
Definition: stub_store.cc:126
const char * storeEntryFlags(const StoreEntry *)
Definition: stat.cc:280
swap_status_t swap_status
Definition: Store.h:187
void storeAppendVPrintf(StoreEntry *, const char *, va_list ap)
Definition: store.cc:914
Decision
Decision states for StoreEntry::swapoutPossible() and related code.
Definition: MemObject.h:120
void registerAbort(STABH *cb, void *)
Definition: store.cc:1586
int expiresMoreThan(time_t, time_t)
Definition: store.cc:1476
virtual size_t bytesWanted(Range< size_t > const aRange, bool ignoreDelayPool=false) const
Definition: store.cc:226
void clearPublicKeyScope()
Definition: store.cc:653
void storeErrorResponse(HttpReply *reply)
Store a prepared error response. MemObject locks the reply object.
Definition: store.cc:1837
bool checkTooBig() const
Definition: store.cc:968
void deferProducer(const AsyncCall::Pointer &producer)
call back producer when more buffer space is available
Definition: store.cc:350
void clearPrivate()
Definition: store.cc:158
void setPrivateKey(const bool shareable)
Definition: store.cc:593
time_t lastModified_
received Last-Modified value or -1; use lastModified()
Definition: Store.h:169
bool hasEtag(ETag &etag) const
whether this entry has an ETag; if yes, puts ETag value into parameter
Definition: store.cc:1997
bool hasIfNoneMatchEtag(const HttpRequest &request) const
has ETag matching at least one of the If-None-Match etags
Definition: store.cc:2015
void startWriting()
Definition: store.cc:1870
void unregisterAbort()
Definition: store.cc:1595
time_t lastref
Definition: Store.h:166
enum _mem_status_t mem_status_t
virtual store_client_t storeClientType() const
Definition: store.cc:262
virtual bool isEmpty() const
Definition: Store.h:60
void storeHeapPositionUpdate(StoreEntry *, SwapDir *)
void swapOut()
void reset()
Definition: store.cc:1748
void transientsAbandonmentCheck()
Definition: store.cc:1910
void negativeCache()
Definition: store.cc:1452
#define EBIT_TEST(flag, bit)
Definition: defines.h:107
#define NULL
Definition: types.h:166
virtual void trimMemory(const bool preserveSwappable)
Definition: store.cc:1938
void kickProducer()
calls back producer registered with deferProducer
Definition: store.cc:360
manages a single cache_dir
Definition: Disk.h:21
void setMemStatus(mem_status_t)
Definition: store.cc:1630
void lock(const char *context)
Definition: store.cc:448
void createMemObject()
Definition: store.cc:1681

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors