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