]> git.ipfire.org Git - thirdparty/squid.git/blame - src/Store.h
Maintenance: bump astyle to 2.04 and quieten report
[thirdparty/squid.git] / src / Store.h
CommitLineData
e6ccf245 1/*
bbc27441 2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
e6ccf245 3 *
bbc27441
AJ
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.
e6ccf245 7 */
bbc27441 8
e6ccf245 9#ifndef SQUID_STORE_H
10#define SQUID_STORE_H
11
63be0a78 12/**
13 \defgroup StoreAPI Store API
14 \ingroup FileSystems
15 */
16
8bf217bd 17#include "base/RefCount.h"
3e4bebf8 18#include "comm/forward.h"
582c2af2 19#include "CommRead.h"
c6983ec7 20#include "hash.h"
f35c0145 21#include "http/forward.h"
c24f7fb6 22#include "http/RequestMethod.h"
43ca19e0 23#include "HttpReply.h"
04ebce55 24#include "MemObject.h"
582c2af2 25#include "Range.h"
aa839030 26#include "RemovalPolicy.h"
582c2af2 27#include "StoreIOBuffer.h"
93bc1434 28#include "StoreStats.h"
528b2c61 29
f41735ea 30#if USE_SQUID_ESI
f99c2cfe 31#include "esi/Element.h"
43ae1d95 32#endif
33
27e059d4 34#include <ostream>
27e059d4 35
65517dc8 36class AsyncCall;
582c2af2 37class HttpRequest;
71b673d4
FC
38class Packer;
39class RequestFlags;
582c2af2 40class StoreClient;
c8f4eac4 41class StoreSearch;
7d3c4ca1 42class SwapDir;
c8f4eac4 43
8822ebee
AR
44extern StoreIoStats store_io_stats;
45
051dedf9
AR
46/// maximum number of entries per cache_dir
47enum { SwapFilenMax = 0xFFFFFF }; // keep in sync with StoreEntry::swap_filen
48
63be0a78 49/**
50 \ingroup StoreAPI
51 */
62e76326 52class StoreEntry : public hash_link
53{
54
332dafa2 55public:
a46d2c0e 56 static DeferredRead::DeferrableRead DeferReader;
57 bool checkDeferRead(int fd) const;
62e76326 58
528b2c61 59 virtual const char *getMD5Text() const;
c8f4eac4 60 StoreEntry();
6d8d05b5 61 virtual ~StoreEntry();
43ae1d95 62
528b2c61 63 virtual HttpReply const *getReply() const;
64 virtual void write (StoreIOBuffer);
04ebce55
FC
65
66 /** Check if the Store entry is emtpty
67 * \retval true Store contains 0 bytes of data.
68 * \retval false Store contains 1 or more bytes of data.
69 * \retval false Store contains negative content !!!!!!
70 */
71 virtual bool isEmpty() const {
72 assert (mem_obj);
73 return mem_obj->endOffset() == 0;
74 }
aa18a4ca 75 virtual bool isAccepting() const;
384a7590 76 virtual size_t bytesWanted(Range<size_t> const aRange, bool ignoreDelayPool = false) const;
528b2c61 77 virtual void complete();
78 virtual store_client_t storeClientType() const;
79 virtual char const *getSerialisedMetaData();
eacfca83
AR
80 /// Store a prepared error response. MemObject locks the reply object.
81 void storeErrorResponse(HttpReply *reply);
3756e5c0
AR
82 void replaceHttpReply(HttpReply *, bool andStartWriting = true);
83 void startWriting(); ///< pack and write reply headers and, maybe, body
5b55f1f1
CT
84 /// whether we may start writing to disk (now or in the future)
85 virtual bool mayStartSwapOut();
86 virtual void trimMemory(const bool preserveSwappable);
0cdcf3d7 87
2da4bfe6 88 // called when a decision to cache in memory has been made
0cdcf3d7
AR
89 void memOutDecision(const bool willCacheInRam);
90 // called when a decision to cache on disk has been made
91 void swapOutDecision(const MemObject::SwapOut::Decision &decision);
92
bfb55b6f 93 void abort();
c8f4eac4 94 void unlink();
5ed72359 95 void makePublic();
96 void makePrivate();
d88e3c49 97 void setPublicKey();
98 void setPrivateKey();
99 void expireNow();
100 void releaseRequest();
101 void negativeCache();
63be0a78 102 void cacheNegatively(); /** \todo argh, why both? */
d88e3c49 103 void invokeHandlers();
104 void purgeMem();
9487bae9 105 void cacheInMemory(); ///< start or continue storing in memory cache
c07cbbf4 106 void swapOut();
5b55f1f1
CT
107 /// whether we are in the process of writing this entry to disk
108 bool swappingOut() const { return swap_status == SWAPOUT_WRITING; }
aa1a691e 109 void swapOutFileClose(int how);
3900307b 110 const char *url() const;
7015a149
AR
111 /// Satisfies cachability requirements shared among disk and RAM caches.
112 /// Encapsulates common checks of mayStartSwapOut() and memoryCachable().
113 /// TODO: Rename and make private so only those two methods can call this.
114 bool checkCachable();
3900307b 115 int checkNegativeHit() const;
116 int locked() const;
117 int validToSend() const;
97754f5a 118 bool memoryCachable(); ///< checkCachable() and can be cached in memory
4475555f 119
c877c0bc
AR
120 /// if needed, initialize mem_obj member w/o URI-related information
121 MemObject *makeMemObject();
122
123 /// initialize mem_obj member (if needed) and supply URI-related info
124 void createMemObject(const char *storeId, const char *logUri, const HttpRequestMethod &aMethod);
4475555f 125
3900307b 126 void dump(int debug_lvl) const;
127 void hashDelete();
128 void hashInsert(const cache_key *);
129 void registerAbort(STABH * cb, void *);
130 void reset();
131 void setMemStatus(mem_status_t);
132 void timestampsSet();
133 void unregisterAbort();
134 void destroyMemObject();
135 int checkTooSmall();
528b2c61 136
3e4bebf8 137 void delayAwareRead(const Comm::ConnectionPointer &conn, char *buf, int len, AsyncCall::Pointer callback);
43ae1d95 138
a46d2c0e 139 void setNoDelay (bool const);
190154cf 140 bool modifiedSince(HttpRequest * request) const;
79c8035e
AR
141 /// has ETag matching at least one of the If-Match etags
142 bool hasIfMatchEtag(const HttpRequest &request) const;
143 /// has ETag matching at least one of the If-None-Match etags
144 bool hasIfNoneMatchEtag(const HttpRequest &request) const;
46017fdd
CT
145 /// whether this entry has an ETag; if yes, puts ETag value into parameter
146 bool hasEtag(ETag &etag) const;
63be0a78 147
148 /** What store does this entry belong too ? */
7d3c4ca1 149 virtual RefCount<SwapDir> store() const;
a46d2c0e 150
e6ccf245 151 MemObject *mem_obj;
152 RemovalPolicyNode repl;
153 /* START OF ON-DISK STORE_META_STD TLV field */
154 time_t timestamp;
155 time_t lastref;
156 time_t expires;
157 time_t lastmod;
47f6e231 158 uint64_t swap_file_sz;
f45dd259
AJ
159 uint16_t refcount;
160 uint16_t flags;
e6ccf245 161 /* END OF ON-DISK STORE_META_STD */
62e76326 162
051dedf9
AR
163 /// unique ID inside a cache_dir for swapped out entries; -1 for others
164 sfileno swap_filen:25; // keep in sync with SwapFilenMax
d1398b75
HN
165
166 sdirno swap_dirn:7;
62e76326 167
d1398b75 168 mem_status_t mem_status:3;
62e76326 169
d1398b75 170 ping_status_t ping_status:3;
62e76326 171
d1398b75 172 store_status_t store_status:3;
62e76326 173
d1398b75 174 swap_status_t swap_status:3;
62e76326 175
e6ccf245 176public:
177 static size_t inUseCount();
60745f24 178 static void getPublicByRequestMethod(StoreClient * aClient, HttpRequest * request, const HttpRequestMethod& method);
190154cf 179 static void getPublicByRequest(StoreClient * aClient, HttpRequest * request);
60745f24 180 static void getPublic(StoreClient * aClient, const char *uri, const HttpRequestMethod& method);
528b2c61 181
26ac0430 182 virtual bool isNull() {
62e76326 183 return false;
63be0a78 184 };
62e76326 185
35db0b4e 186 void *operator new(size_t byteCount);
e6ccf245 187 void operator delete(void *address);
43ae1d95 188 void setReleaseFlag();
f41735ea 189#if USE_SQUID_ESI
43ae1d95 190
191 ESIElement::Pointer cachedESITree;
192#endif
63be0a78 193 /** append bytes to the buffer */
c21ad0f5 194 virtual void append(char const *, int len);
63be0a78 195 /** disable sending content to the clients */
c21ad0f5 196 virtual void buffer();
63be0a78 197 /** flush any buffered content */
c21ad0f5 198 virtual void flush();
47f6e231 199 virtual int64_t objectLen() const;
200 virtual int64_t contentLen() const;
c21ad0f5 201
18994992 202 /// claim shared ownership of this entry (for use in a given context)
acc5dc4c 203 /// matching lock() and unlock() contexts eases leak triage but is optional
18994992 204 void lock(const char *context);
1bfe9ade 205
18994992
AR
206 /// disclaim shared ownership; may remove entry from store and delete it
207 /// returns remaning lock level (zero for unlocked and possibly gone entry)
acc5dc4c 208 int unlock(const char *context);
1bfe9ade
AR
209
210 /// returns a local concurrent use counter, for debugging
211 int locks() const { return static_cast<int>(lock_count); }
212
18994992
AR
213 /// update last reference timestamp and related Store metadata
214 void touch();
215
5f33b71d 216 virtual void release();
62e76326 217
0ad2b63b
CT
218#if USE_ADAPTATION
219 /// call back producer when more buffer space is available
220 void deferProducer(const AsyncCall::Pointer &producer);
221 /// calls back producer registered with deferProducer
222 void kickProducer();
223#endif
224
0cdcf3d7
AR
225protected:
226 void transientsAbandonmentCheck();
227
e6ccf245 228private:
2be042f6
AR
229 bool checkTooBig() const;
230
04eb0689 231 static MemAllocator *pool;
528b2c61 232
1bfe9ade
AR
233 unsigned short lock_count; /* Assume < 65536! */
234
0ad2b63b
CT
235#if USE_ADAPTATION
236 /// producer callback registered with deferProducer
237 AsyncCall::Pointer deferredProducer;
238#endif
239
528b2c61 240 bool validLength() const;
79c8035e 241 bool hasOneOfEtags(const String &reqETags, const bool allowWeakMatch) const;
e6ccf245 242};
243
2c4cd1ad
AR
244std::ostream &operator <<(std::ostream &os, const StoreEntry &e);
245
63be0a78 246/// \ingroup StoreAPI
3b13a8fd 247class NullStoreEntry:public StoreEntry
e6ccf245 248{
62e76326 249
e6ccf245 250public:
251 static NullStoreEntry *getInstance();
26ac0430 252 bool isNull() {
62e76326 253 return true;
e6ccf245 254 }
62e76326 255
332dafa2 256 const char *getMD5Text() const;
04ebce55 257 HttpReply const *getReply() const { return NULL; }
26ac0430 258 void write (StoreIOBuffer) {}
62e76326 259
528b2c61 260 bool isEmpty () const {return true;}
62e76326 261
384a7590 262 virtual size_t bytesWanted(Range<size_t> const aRange, bool ignoreDelayPool = false) const { return aRange.end; }
62e76326 263
e6ccf245 264 void operator delete(void *address);
26ac0430 265 void complete() {}
62e76326 266
267private:
26ac0430 268 store_client_t storeClientType() const {return STORE_MEM_CLIENT;}
62e76326 269
528b2c61 270 char const *getSerialisedMetaData();
97754f5a 271 virtual bool mayStartSwapOut() { return false; }
62e76326 272
74b9c248 273 void trimMemory(const bool preserveSwappable) {}
528b2c61 274
e6ccf245 275 static NullStoreEntry _instance;
276};
277
63be0a78 278/// \ingroup StoreAPI
c8f4eac4 279typedef void (*STOREGETCLIENT) (StoreEntry *, void *cbdata);
280
63be0a78 281/**
282 \ingroup StoreAPI
283 * Abstract base class that will replace the whole store and swapdir interface.
284 */
c8f4eac4 285class Store : public RefCountable
286{
287
288public:
63be0a78 289 /** The root store */
04ebce55
FC
290 static Store &Root() {
291 if (CurrentRoot == NULL)
292 fatal("No Store Root has been set");
293 return *CurrentRoot;
294 }
c8f4eac4 295 static void Root(Store *);
296 static void Root(RefCount<Store>);
297 static void Stats(StoreEntry * output);
298 static void Maintain(void *unused);
299
300 virtual ~Store() {}
301
63be0a78 302 /** Handle pending callbacks - called by the event loop. */
c8f4eac4 303 virtual int callback() = 0;
63be0a78 304
305 /** create the resources needed for this store to operate */
c8f4eac4 306 virtual void create();
63be0a78 307
308 /**
309 * Notify this store that its disk is full.
310 \todo XXX move into a protected api call between store files and their stores, rather than a top level api call
c8f4eac4 311 */
312 virtual void diskFull();
c8f4eac4 313
63be0a78 314 /** Retrieve a store entry from the store */
b21767da 315 virtual StoreEntry * get(const cache_key *) = 0;
c8f4eac4 316
63be0a78 317 /** \todo imeplement the async version */
b21767da 318 virtual void get(String const key , STOREGETCLIENT callback, void *cbdata) = 0;
c8f4eac4 319
320 /* prepare the store for use. The store need not be usable immediately,
321 * it should respond to readable() and writable() with true as soon
322 * as it can provide those services
323 */
324 virtual void init() = 0;
325
63be0a78 326 /**
327 * The maximum size the store will support in normal use. Inaccuracy is permitted,
328 * but may throw estimates for memory etc out of whack.
329 */
12e11a5c 330 virtual uint64_t maxSize() const = 0;
c8f4eac4 331
63be0a78 332 /** The minimum size the store will shrink to via normal housekeeping */
12e11a5c 333 virtual uint64_t minSize() const = 0;
c8f4eac4 334
57f583f1
DK
335 /** current store size */
336 virtual uint64_t currentSize() const = 0;
39c1e1d9
DK
337
338 /** the total number of objects stored */
339 virtual uint64_t currentCount() const = 0;
340
af2fda07
DK
341 /** the maximum object size that can be stored, -1 if unlimited */
342 virtual int64_t maxObjectSize() const = 0;
343
93bc1434
AR
344 /// collect cache storage-related statistics
345 virtual void getStats(StoreInfoStats &stats) const = 0;
346
63be0a78 347 /**
348 * Output stats to the provided store entry.
349 \todo make these calls asynchronous
350 */
351 virtual void stat(StoreEntry &) const = 0;
c8f4eac4 352
63be0a78 353 /** Sync the store prior to shutdown */
354 virtual void sync();
c8f4eac4 355
63be0a78 356 /** remove a Store entry from the store */
c8f4eac4 357 virtual void unlink (StoreEntry &);
358
359 /* search in the store */
30abd221 360 virtual StoreSearch *search(String const url, HttpRequest *) = 0;
c8f4eac4 361
362 /* pulled up from SwapDir for migration.... probably do not belong here */
363 virtual void reference(StoreEntry &) = 0; /* Reference this object */
364
4c973beb 365 /// Undo reference(), returning false iff idle e should be destroyed
54347cbd 366 virtual bool dereference(StoreEntry &e, bool wantsLocalMemory) = 0;
c8f4eac4 367
368 virtual void maintain() = 0; /* perform regular maintenance should be private and self registered ... */
369
1bfe9ade
AR
370 // XXX: This method belongs to Store::Root/StoreController, but it is here
371 // to avoid casting Root() to StoreController until Root() API is fixed.
372 /// informs stores that this entry will be eventually unlinked
373 virtual void markForUnlink(StoreEntry &e) {}
374
9487bae9
AR
375 // XXX: This method belongs to Store::Root/StoreController, but it is here
376 // because test cases use non-StoreController derivatives as Root
377 /// called when the entry is no longer needed by any transaction
378 virtual void handleIdleEntry(StoreEntry &e) {}
379
96a7de88
DK
380 // XXX: This method belongs to Store::Root/StoreController, but it is here
381 // because test cases use non-StoreController derivatives as Root
382 /// called to get rid of no longer needed entry data in RAM, if any
4475555f 383 virtual void memoryOut(StoreEntry &e, const bool preserveSwappable) {}
96a7de88 384
9a9954ba
AR
385 // XXX: This method belongs to Store::Root/StoreController, but it is here
386 // to avoid casting Root() to StoreController until Root() API is fixed.
387 /// makes the entry available for collapsing future requests
388 virtual void allowCollapsing(StoreEntry *e, const RequestFlags &reqFlags, const HttpRequestMethod &reqMethod) {}
389
99921d9d
AR
390 // XXX: This method belongs to Store::Root/StoreController, but it is here
391 // to avoid casting Root() to StoreController until Root() API is fixed.
392 /// marks the entry completed for collapsed requests
393 virtual void transientsCompleteWriting(StoreEntry &e) {}
394
ce49546e
AR
395 // XXX: This method belongs to Store::Root/StoreController, but it is here
396 // to avoid casting Root() to StoreController until Root() API is fixed.
397 /// Update local intransit entry after changes made by appending worker.
6919be24 398 virtual void syncCollapsed(const sfileno xitIndex) {}
ce49546e 399
4475555f
AR
400 // XXX: This method belongs to Store::Root/StoreController, but it is here
401 // to avoid casting Root() to StoreController until Root() API is fixed.
402 /// calls Root().transients->abandon() if transients are tracked
403 virtual void transientsAbandon(StoreEntry &e) {}
404
405 // XXX: This method belongs to Store::Root/StoreController, but it is here
d366a7fa
AR
406 // to avoid casting Root() to StoreController until Root() API is fixed.
407 /// number of the transient entry readers some time ago
408 virtual int transientReaders(const StoreEntry &e) const { return 0; }
409
410 // XXX: This method belongs to Store::Root/StoreController, but it is here
4475555f
AR
411 // to avoid casting Root() to StoreController until Root() API is fixed.
412 /// disassociates the entry from the intransit table
413 virtual void transientsDisconnect(MemObject &mem_obj) {}
414
ce49546e
AR
415 // XXX: This method belongs to Store::Root/StoreController, but it is here
416 // to avoid casting Root() to StoreController until Root() API is fixed.
417 /// removes the entry from the memory cache
418 virtual void memoryUnlink(StoreEntry &e) {}
419
4475555f
AR
420 // XXX: This method belongs to Store::Root/StoreController, but it is here
421 // to avoid casting Root() to StoreController until Root() API is fixed.
422 /// disassociates the entry from the memory cache, preserving cached data
29c56e41 423 virtual void memoryDisconnect(StoreEntry &e) {}
4475555f
AR
424
425 /// If the entry is not found, return false. Otherwise, return true after
426 /// tying the entry to this cache and setting inSync to updateCollapsed().
427 virtual bool anchorCollapsed(StoreEntry &collapsed, bool &inSync) { return false; }
ce49546e
AR
428
429 /// update a local collapsed entry with fresh info from this cache (if any)
430 virtual bool updateCollapsed(StoreEntry &collapsed) { return false; }
96a7de88 431
c8f4eac4 432private:
433 static RefCount<Store> CurrentRoot;
434};
435
63be0a78 436/// \ingroup StoreAPI
c8f4eac4 437typedef RefCount<Store> StorePointer;
438
63be0a78 439/// \ingroup StoreAPI
71b673d4 440size_t storeEntryInUse();
63be0a78 441
442/// \ingroup StoreAPI
71b673d4 443const char *storeEntryFlags(const StoreEntry *);
63be0a78 444
445/// \ingroup StoreAPI
71b673d4 446void storeEntryReplaceObject(StoreEntry *, HttpReply *);
e6ccf245 447
63be0a78 448/// \ingroup StoreAPI
71b673d4 449StoreEntry *storeGetPublic(const char *uri, const HttpRequestMethod& method);
63be0a78 450
451/// \ingroup StoreAPI
71b673d4 452StoreEntry *storeGetPublicByRequest(HttpRequest * request);
63be0a78 453
454/// \ingroup StoreAPI
71b673d4 455StoreEntry *storeGetPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method);
63be0a78 456
457/// \ingroup StoreAPI
1bfe9ade 458/// Like storeCreatePureEntry(), but also locks the entry and sets entry key.
71b673d4 459StoreEntry *storeCreateEntry(const char *, const char *, const RequestFlags &, const HttpRequestMethod&);
63be0a78 460
1bfe9ade
AR
461/// \ingroup StoreAPI
462/// Creates a new StoreEntry with mem_obj and sets initial flags/states.
463StoreEntry *storeCreatePureEntry(const char *storeId, const char *logUrl, const RequestFlags &, const HttpRequestMethod&);
464
63be0a78 465/// \ingroup StoreAPI
71b673d4 466void storeInit(void);
63be0a78 467
63be0a78 468/// \ingroup StoreAPI
71b673d4 469void storeConfigure(void);
63be0a78 470
471/// \ingroup StoreAPI
71b673d4 472void storeFreeMemory(void);
63be0a78 473
474/// \ingroup StoreAPI
71b673d4 475int expiresMoreThan(time_t, time_t);
63be0a78 476
63be0a78 477/// \ingroup StoreAPI
71b673d4 478void storeAppendPrintf(StoreEntry *, const char *,...) PRINTF_FORMAT_ARG2;
63be0a78 479
480/// \ingroup StoreAPI
71b673d4 481void storeAppendVPrintf(StoreEntry *, const char *, va_list ap);
63be0a78 482
483/// \ingroup StoreAPI
71b673d4 484int storeTooManyDiskFilesOpen(void);
63be0a78 485
e1f7507e 486class SwapDir;
63be0a78 487/// \ingroup StoreAPI
71b673d4 488void storeHeapPositionUpdate(StoreEntry *, SwapDir *);
63be0a78 489
490/// \ingroup StoreAPI
71b673d4 491void storeSwapFileNumberSet(StoreEntry * e, sfileno filn);
63be0a78 492
493/// \ingroup StoreAPI
71b673d4 494void storeFsInit(void);
63be0a78 495
496/// \ingroup StoreAPI
71b673d4 497void storeFsDone(void);
63be0a78 498
499/// \ingroup StoreAPI
71b673d4 500void storeReplAdd(const char *, REMOVALPOLICYCREATE *);
63be0a78 501
502/// \ingroup StoreAPI
c8f4eac4 503extern FREE destroyStoreEntry;
e6ccf245 504
63be0a78 505/**
506 \ingroup StoreAPI
507 \todo should be a subclass of Packer perhaps ?
508 */
71b673d4 509void packerToStoreInit(Packer * p, StoreEntry * e);
25b6a907 510
6d3c2758 511/// \ingroup StoreAPI
71b673d4 512void storeGetMemSpace(int size);
6d3c2758 513
e6ccf245 514#endif /* SQUID_STORE_H */