]> git.ipfire.org Git - thirdparty/squid.git/blame - src/Store.h
Several fixes and improvements to help collapsed forwarding work reliably:
[thirdparty/squid.git] / src / Store.h
CommitLineData
e6ccf245 1/*
e6ccf245 2 *
3 * SQUID Web Proxy Cache http://www.squid-cache.org/
4 * ----------------------------------------------------------
5 *
6 * Squid is the result of efforts by numerous individuals from
7 * the Internet community; see the CONTRIBUTORS file for full
8 * details. Many organizations have provided support for Squid's
9 * development; see the SPONSORS file for full details. Squid is
10 * Copyrighted (C) 2001 by the Regents of the University of
11 * California; see the COPYRIGHT file for full details. Squid
12 * incorporates software developed and/or copyrighted by other
13 * sources; see the CREDITS file for full details.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
26ac0430 19 *
e6ccf245 20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
26ac0430 24 *
e6ccf245 25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
28 *
29 */
e6ccf245 30#ifndef SQUID_STORE_H
31#define SQUID_STORE_H
32
63be0a78 33/**
34 \defgroup StoreAPI Store API
35 \ingroup FileSystems
36 */
37
8bf217bd 38#include "base/RefCount.h"
3e4bebf8 39#include "comm/forward.h"
582c2af2 40#include "CommRead.h"
c6983ec7 41#include "hash.h"
43ca19e0 42#include "HttpReply.h"
582c2af2 43#include "HttpRequestMethod.h"
582c2af2 44#include "Range.h"
aa839030 45#include "RemovalPolicy.h"
582c2af2 46#include "StoreIOBuffer.h"
93bc1434 47#include "StoreStats.h"
528b2c61 48
f41735ea 49#if USE_SQUID_ESI
f99c2cfe 50#include "esi/Element.h"
43ae1d95 51#endif
52
27e059d4
AJ
53#if HAVE_OSTREAM
54#include <ostream>
55#endif
56
65517dc8 57class AsyncCall;
582c2af2 58class HttpRequest;
528b2c61 59class MemObject;
71b673d4
FC
60class Packer;
61class RequestFlags;
582c2af2 62class StoreClient;
c8f4eac4 63class StoreSearch;
7d3c4ca1 64class SwapDir;
c8f4eac4 65
8822ebee
AR
66extern StoreIoStats store_io_stats;
67
051dedf9
AR
68/// maximum number of entries per cache_dir
69enum { SwapFilenMax = 0xFFFFFF }; // keep in sync with StoreEntry::swap_filen
70
63be0a78 71/**
72 \ingroup StoreAPI
73 */
62e76326 74class StoreEntry : public hash_link
75{
76
332dafa2 77public:
a46d2c0e 78 static DeferredRead::DeferrableRead DeferReader;
79 bool checkDeferRead(int fd) const;
62e76326 80
528b2c61 81 virtual const char *getMD5Text() const;
c8f4eac4 82 StoreEntry();
6d8d05b5 83 virtual ~StoreEntry();
43ae1d95 84
528b2c61 85 virtual HttpReply const *getReply() const;
86 virtual void write (StoreIOBuffer);
87 virtual _SQUID_INLINE_ bool isEmpty() const;
aa18a4ca 88 virtual bool isAccepting() const;
384a7590 89 virtual size_t bytesWanted(Range<size_t> const aRange, bool ignoreDelayPool = false) const;
528b2c61 90 virtual void complete();
91 virtual store_client_t storeClientType() const;
92 virtual char const *getSerialisedMetaData();
3756e5c0
AR
93 void replaceHttpReply(HttpReply *, bool andStartWriting = true);
94 void startWriting(); ///< pack and write reply headers and, maybe, body
5b55f1f1
CT
95 /// whether we may start writing to disk (now or in the future)
96 virtual bool mayStartSwapOut();
97 virtual void trimMemory(const bool preserveSwappable);
bfb55b6f 98 void abort();
c8f4eac4 99 void unlink();
5ed72359 100 void makePublic();
101 void makePrivate();
d88e3c49 102 void setPublicKey();
103 void setPrivateKey();
104 void expireNow();
105 void releaseRequest();
106 void negativeCache();
63be0a78 107 void cacheNegatively(); /** \todo argh, why both? */
d88e3c49 108 void invokeHandlers();
109 void purgeMem();
9487bae9 110 void cacheInMemory(); ///< start or continue storing in memory cache
c07cbbf4 111 void swapOut();
5b55f1f1
CT
112 /// whether we are in the process of writing this entry to disk
113 bool swappingOut() const { return swap_status == SWAPOUT_WRITING; }
aa1a691e 114 void swapOutFileClose(int how);
3900307b 115 const char *url() const;
116 int checkCachable();
117 int checkNegativeHit() const;
118 int locked() const;
119 int validToSend() const;
9487bae9 120 bool memoryCachable() const; ///< may be cached in memory
4475555f 121
c877c0bc
AR
122 /// if needed, initialize mem_obj member w/o URI-related information
123 MemObject *makeMemObject();
124
125 /// initialize mem_obj member (if needed) and supply URI-related info
126 void createMemObject(const char *storeId, const char *logUri, const HttpRequestMethod &aMethod);
4475555f 127
3900307b 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();
133 void setMemStatus(mem_status_t);
134 void timestampsSet();
135 void unregisterAbort();
136 void destroyMemObject();
137 int checkTooSmall();
528b2c61 138
3e4bebf8 139 void delayAwareRead(const Comm::ConnectionPointer &conn, char *buf, int len, AsyncCall::Pointer callback);
43ae1d95 140
a46d2c0e 141 void setNoDelay (bool const);
190154cf 142 bool modifiedSince(HttpRequest * request) const;
79c8035e
AR
143 /// has ETag matching at least one of the If-Match etags
144 bool hasIfMatchEtag(const HttpRequest &request) const;
145 /// has ETag matching at least one of the If-None-Match etags
146 bool hasIfNoneMatchEtag(const HttpRequest &request) 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
AR
202 /// claim shared ownership of this entry (for use in a given context)
203 void lock(const char *context);
1bfe9ade 204
18994992
AR
205 /// disclaim shared ownership; may remove entry from store and delete it
206 /// returns remaning lock level (zero for unlocked and possibly gone entry)
207 int unlock(const char *context = "somebody");
1bfe9ade
AR
208
209 /// returns a local concurrent use counter, for debugging
210 int locks() const { return static_cast<int>(lock_count); }
211
18994992
AR
212 /// update last reference timestamp and related Store metadata
213 void touch();
214
5f33b71d 215 virtual void release();
62e76326 216
0ad2b63b
CT
217#if USE_ADAPTATION
218 /// call back producer when more buffer space is available
219 void deferProducer(const AsyncCall::Pointer &producer);
220 /// calls back producer registered with deferProducer
221 void kickProducer();
222#endif
223
e6ccf245 224private:
04eb0689 225 static MemAllocator *pool;
528b2c61 226
1bfe9ade
AR
227 unsigned short lock_count; /* Assume < 65536! */
228
0ad2b63b
CT
229#if USE_ADAPTATION
230 /// producer callback registered with deferProducer
231 AsyncCall::Pointer deferredProducer;
232#endif
233
528b2c61 234 bool validLength() const;
79c8035e 235 bool hasOneOfEtags(const String &reqETags, const bool allowWeakMatch) const;
e6ccf245 236};
237
2c4cd1ad
AR
238std::ostream &operator <<(std::ostream &os, const StoreEntry &e);
239
63be0a78 240/// \ingroup StoreAPI
3b13a8fd 241class NullStoreEntry:public StoreEntry
e6ccf245 242{
62e76326 243
e6ccf245 244public:
245 static NullStoreEntry *getInstance();
26ac0430 246 bool isNull() {
62e76326 247 return true;
e6ccf245 248 }
62e76326 249
332dafa2 250 const char *getMD5Text() const;
528b2c61 251 _SQUID_INLINE_ HttpReply const *getReply() const;
26ac0430 252 void write (StoreIOBuffer) {}
62e76326 253
528b2c61 254 bool isEmpty () const {return true;}
62e76326 255
384a7590 256 virtual size_t bytesWanted(Range<size_t> const aRange, bool ignoreDelayPool = false) const { return aRange.end; }
62e76326 257
e6ccf245 258 void operator delete(void *address);
26ac0430 259 void complete() {}
62e76326 260
261private:
26ac0430 262 store_client_t storeClientType() const {return STORE_MEM_CLIENT;}
62e76326 263
528b2c61 264 char const *getSerialisedMetaData();
5b55f1f1 265 bool mayStartSwapout() {return false;}
62e76326 266
74b9c248 267 void trimMemory(const bool preserveSwappable) {}
528b2c61 268
e6ccf245 269 static NullStoreEntry _instance;
270};
271
63be0a78 272/// \ingroup StoreAPI
c8f4eac4 273typedef void (*STOREGETCLIENT) (StoreEntry *, void *cbdata);
274
63be0a78 275/**
276 \ingroup StoreAPI
277 * Abstract base class that will replace the whole store and swapdir interface.
278 */
c8f4eac4 279class Store : public RefCountable
280{
281
282public:
63be0a78 283 /** The root store */
88e13392 284 static _SQUID_INLINE_ Store &Root();
c8f4eac4 285 static void Root(Store *);
286 static void Root(RefCount<Store>);
287 static void Stats(StoreEntry * output);
288 static void Maintain(void *unused);
289
290 virtual ~Store() {}
291
63be0a78 292 /** Handle pending callbacks - called by the event loop. */
c8f4eac4 293 virtual int callback() = 0;
63be0a78 294
295 /** create the resources needed for this store to operate */
c8f4eac4 296 virtual void create();
63be0a78 297
298 /**
299 * Notify this store that its disk is full.
300 \todo XXX move into a protected api call between store files and their stores, rather than a top level api call
c8f4eac4 301 */
302 virtual void diskFull();
c8f4eac4 303
63be0a78 304 /** Retrieve a store entry from the store */
b21767da 305 virtual StoreEntry * get(const cache_key *) = 0;
c8f4eac4 306
63be0a78 307 /** \todo imeplement the async version */
b21767da 308 virtual void get(String const key , STOREGETCLIENT callback, void *cbdata) = 0;
c8f4eac4 309
310 /* prepare the store for use. The store need not be usable immediately,
311 * it should respond to readable() and writable() with true as soon
312 * as it can provide those services
313 */
314 virtual void init() = 0;
315
63be0a78 316 /**
317 * The maximum size the store will support in normal use. Inaccuracy is permitted,
318 * but may throw estimates for memory etc out of whack.
319 */
12e11a5c 320 virtual uint64_t maxSize() const = 0;
c8f4eac4 321
63be0a78 322 /** The minimum size the store will shrink to via normal housekeeping */
12e11a5c 323 virtual uint64_t minSize() const = 0;
c8f4eac4 324
57f583f1
DK
325 /** current store size */
326 virtual uint64_t currentSize() const = 0;
39c1e1d9
DK
327
328 /** the total number of objects stored */
329 virtual uint64_t currentCount() const = 0;
330
af2fda07
DK
331 /** the maximum object size that can be stored, -1 if unlimited */
332 virtual int64_t maxObjectSize() const = 0;
333
93bc1434
AR
334 /// collect cache storage-related statistics
335 virtual void getStats(StoreInfoStats &stats) const = 0;
336
63be0a78 337 /**
338 * Output stats to the provided store entry.
339 \todo make these calls asynchronous
340 */
341 virtual void stat(StoreEntry &) const = 0;
c8f4eac4 342
63be0a78 343 /** Sync the store prior to shutdown */
344 virtual void sync();
c8f4eac4 345
63be0a78 346 /** remove a Store entry from the store */
c8f4eac4 347 virtual void unlink (StoreEntry &);
348
349 /* search in the store */
30abd221 350 virtual StoreSearch *search(String const url, HttpRequest *) = 0;
c8f4eac4 351
352 /* pulled up from SwapDir for migration.... probably do not belong here */
353 virtual void reference(StoreEntry &) = 0; /* Reference this object */
354
4c973beb 355 /// Undo reference(), returning false iff idle e should be destroyed
54347cbd 356 virtual bool dereference(StoreEntry &e, bool wantsLocalMemory) = 0;
c8f4eac4 357
358 virtual void maintain() = 0; /* perform regular maintenance should be private and self registered ... */
359
1bfe9ade
AR
360 // XXX: This method belongs to Store::Root/StoreController, but it is here
361 // to avoid casting Root() to StoreController until Root() API is fixed.
362 /// informs stores that this entry will be eventually unlinked
363 virtual void markForUnlink(StoreEntry &e) {}
364
9487bae9
AR
365 // XXX: This method belongs to Store::Root/StoreController, but it is here
366 // because test cases use non-StoreController derivatives as Root
367 /// called when the entry is no longer needed by any transaction
368 virtual void handleIdleEntry(StoreEntry &e) {}
369
96a7de88
DK
370 // XXX: This method belongs to Store::Root/StoreController, but it is here
371 // because test cases use non-StoreController derivatives as Root
372 /// called to get rid of no longer needed entry data in RAM, if any
4475555f 373 virtual void memoryOut(StoreEntry &e, const bool preserveSwappable) {}
96a7de88 374
9a9954ba
AR
375 // XXX: This method belongs to Store::Root/StoreController, but it is here
376 // to avoid casting Root() to StoreController until Root() API is fixed.
377 /// makes the entry available for collapsing future requests
378 virtual void allowCollapsing(StoreEntry *e, const RequestFlags &reqFlags, const HttpRequestMethod &reqMethod) {}
379
99921d9d
AR
380 // XXX: This method belongs to Store::Root/StoreController, but it is here
381 // to avoid casting Root() to StoreController until Root() API is fixed.
382 /// marks the entry completed for collapsed requests
383 virtual void transientsCompleteWriting(StoreEntry &e) {}
384
ce49546e
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 /// Update local intransit entry after changes made by appending worker.
6919be24 388 virtual void syncCollapsed(const sfileno xitIndex) {}
ce49546e 389
4475555f
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 /// calls Root().transients->abandon() if transients are tracked
393 virtual void transientsAbandon(StoreEntry &e) {}
394
395 // XXX: This method belongs to Store::Root/StoreController, but it is here
d366a7fa
AR
396 // to avoid casting Root() to StoreController until Root() API is fixed.
397 /// number of the transient entry readers some time ago
398 virtual int transientReaders(const StoreEntry &e) const { return 0; }
399
400 // XXX: This method belongs to Store::Root/StoreController, but it is here
4475555f
AR
401 // to avoid casting Root() to StoreController until Root() API is fixed.
402 /// disassociates the entry from the intransit table
403 virtual void transientsDisconnect(MemObject &mem_obj) {}
404
ce49546e
AR
405 // XXX: This method belongs to Store::Root/StoreController, but it is here
406 // to avoid casting Root() to StoreController until Root() API is fixed.
407 /// removes the entry from the memory cache
408 virtual void memoryUnlink(StoreEntry &e) {}
409
4475555f
AR
410 // XXX: This method belongs to Store::Root/StoreController, but it is here
411 // to avoid casting Root() to StoreController until Root() API is fixed.
412 /// disassociates the entry from the memory cache, preserving cached data
413 virtual void memoryDisconnect(MemObject &mem_obj) {}
414
415 /// If the entry is not found, return false. Otherwise, return true after
416 /// tying the entry to this cache and setting inSync to updateCollapsed().
417 virtual bool anchorCollapsed(StoreEntry &collapsed, bool &inSync) { return false; }
ce49546e
AR
418
419 /// update a local collapsed entry with fresh info from this cache (if any)
420 virtual bool updateCollapsed(StoreEntry &collapsed) { return false; }
421
c8f4eac4 422private:
423 static RefCount<Store> CurrentRoot;
424};
425
63be0a78 426/// \ingroup StoreAPI
c8f4eac4 427typedef RefCount<Store> StorePointer;
428
63be0a78 429/// \ingroup StoreAPI
71b673d4 430size_t storeEntryInUse();
63be0a78 431
432/// \ingroup StoreAPI
71b673d4 433const char *storeEntryFlags(const StoreEntry *);
63be0a78 434
435/// \ingroup StoreAPI
71b673d4 436void storeEntryReplaceObject(StoreEntry *, HttpReply *);
e6ccf245 437
63be0a78 438/// \ingroup StoreAPI
71b673d4 439StoreEntry *storeGetPublic(const char *uri, const HttpRequestMethod& method);
63be0a78 440
441/// \ingroup StoreAPI
71b673d4 442StoreEntry *storeGetPublicByRequest(HttpRequest * request);
63be0a78 443
444/// \ingroup StoreAPI
71b673d4 445StoreEntry *storeGetPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method);
63be0a78 446
447/// \ingroup StoreAPI
1bfe9ade 448/// Like storeCreatePureEntry(), but also locks the entry and sets entry key.
71b673d4 449StoreEntry *storeCreateEntry(const char *, const char *, const RequestFlags &, const HttpRequestMethod&);
63be0a78 450
1bfe9ade
AR
451/// \ingroup StoreAPI
452/// Creates a new StoreEntry with mem_obj and sets initial flags/states.
453StoreEntry *storeCreatePureEntry(const char *storeId, const char *logUrl, const RequestFlags &, const HttpRequestMethod&);
454
63be0a78 455/// \ingroup StoreAPI
71b673d4 456void storeInit(void);
63be0a78 457
63be0a78 458/// \ingroup StoreAPI
71b673d4 459void storeConfigure(void);
63be0a78 460
461/// \ingroup StoreAPI
71b673d4 462void storeFreeMemory(void);
63be0a78 463
464/// \ingroup StoreAPI
71b673d4 465int expiresMoreThan(time_t, time_t);
63be0a78 466
63be0a78 467/// \ingroup StoreAPI
71b673d4 468void storeAppendPrintf(StoreEntry *, const char *,...) PRINTF_FORMAT_ARG2;
63be0a78 469
470/// \ingroup StoreAPI
71b673d4 471void storeAppendVPrintf(StoreEntry *, const char *, va_list ap);
63be0a78 472
473/// \ingroup StoreAPI
71b673d4 474int storeTooManyDiskFilesOpen(void);
63be0a78 475
e1f7507e 476class SwapDir;
63be0a78 477/// \ingroup StoreAPI
71b673d4 478void storeHeapPositionUpdate(StoreEntry *, SwapDir *);
63be0a78 479
480/// \ingroup StoreAPI
71b673d4 481void storeSwapFileNumberSet(StoreEntry * e, sfileno filn);
63be0a78 482
483/// \ingroup StoreAPI
71b673d4 484void storeFsInit(void);
63be0a78 485
486/// \ingroup StoreAPI
71b673d4 487void storeFsDone(void);
63be0a78 488
489/// \ingroup StoreAPI
71b673d4 490void storeReplAdd(const char *, REMOVALPOLICYCREATE *);
63be0a78 491
492/// \ingroup StoreAPI
c8f4eac4 493extern FREE destroyStoreEntry;
e6ccf245 494
63be0a78 495/**
496 \ingroup StoreAPI
497 \todo should be a subclass of Packer perhaps ?
498 */
71b673d4 499void packerToStoreInit(Packer * p, StoreEntry * e);
25b6a907 500
6d3c2758 501/// \ingroup StoreAPI
71b673d4 502void storeGetMemSpace(int size);
6d3c2758 503
32d002cb 504#if _USE_INLINE_
528b2c61 505#include "Store.cci"
506#endif
507
e6ccf245 508#endif /* SQUID_STORE_H */