2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 20 Storage Manager */
12 #include "CacheDigest.h"
13 #include "CacheManager.h"
14 #include "comm/Connection.h"
15 #include "comm/Read.h"
21 #include "HttpReply.h"
22 #include "HttpRequest.h"
24 #include "MemObject.h"
25 #include "mgr/Registration.h"
26 #include "mgr/StoreIoAction.h"
27 #include "profiler/Profiler.h"
28 #include "repl_modules.h"
29 #include "RequestFlags.h"
30 #include "SquidConfig.h"
31 #include "SquidTime.h"
32 #include "StatCounters.h"
35 #include "store_digest.h"
36 #include "store_key_md5.h"
37 #include "store_key_md5.h"
38 #include "store_log.h"
39 #include "store_rebuild.h"
40 #include "StoreClient.h"
41 #include "StoreIOState.h"
42 #include "StoreMeta.h"
44 #include "swap_log_op.h"
48 #include "DelayPools.h"
51 /** StoreEntry uses explicit new/delete operators, which set pool chunk size to 2MB
52 * XXX: convert to MEMPROXY_CLASS() API
59 #define REBUILD_TIMESTAMP_DELTA_MAX 2
61 #define STORE_IN_MEM_BUCKETS (229)
63 /** \todo Convert these string constants to enum string-arrays generated */
65 const char *memStatusStr
[] = {
70 const char *pingStatusStr
[] = {
76 const char *storeStatusStr
[] = {
81 const char *swapStatusStr
[] = {
88 * This defines an repl type
91 typedef struct _storerepl_entry storerepl_entry_t
;
93 struct _storerepl_entry
{
95 REMOVALPOLICYCREATE
*create
;
98 static storerepl_entry_t
*storerepl_list
= NULL
;
101 * local function prototypes
103 static int getKeyCounter(void);
104 static OBJH storeCheckCachableStats
;
105 static EVH storeLateRelease
;
110 static std::stack
<StoreEntry
*> LateReleaseStack
;
111 MemAllocator
*StoreEntry::pool
= NULL
;
113 StorePointer
Store::CurrentRoot
= NULL
;
116 Store::Root(Store
* aRoot
)
122 Store::Root(StorePointer aRoot
)
124 Root(aRoot
.getRaw());
128 Store::Stats(StoreEntry
* output
)
131 Root().stat(*output
);
147 Store::unlink(StoreEntry
&)
149 fatal("Store::unlink on invalid Store\n");
153 StoreEntry::operator new (size_t bytecount
)
155 assert(bytecount
== sizeof (StoreEntry
));
158 pool
= memPoolCreate ("StoreEntry", bytecount
);
159 pool
->setChunkSize(2048 * 1024);
162 return pool
->alloc();
166 StoreEntry::operator delete (void *address
)
168 pool
->freeOne(address
);
172 StoreEntry::makePublic()
174 /* This object can be cached for a long time */
176 if (!EBIT_TEST(flags
, RELEASE_REQUEST
))
181 StoreEntry::makePrivate()
183 /* This object should never be cached at all */
185 releaseRequest(); /* delete object when not used */
189 StoreEntry::cacheNegatively()
191 /* This object may be negatively cached */
197 StoreEntry::inUseCount()
201 return pool
->getInUseCount();
205 StoreEntry::getMD5Text() const
207 return storeKeyText((const cache_key
*)key
);
213 StoreEntry::DeferReader(void *theContext
, CommRead
const &aRead
)
215 StoreEntry
*anEntry
= (StoreEntry
*)theContext
;
216 anEntry
->delayAwareRead(aRead
.conn
,
223 StoreEntry::delayAwareRead(const Comm::ConnectionPointer
&conn
, char *buf
, int len
, AsyncCall::Pointer callback
)
225 size_t amountToRead
= bytesWanted(Range
<size_t>(0, len
));
226 /* sketch: readdeferer* = getdeferer.
227 * ->deferRead (fd, buf, len, callback, DelayAwareRead, this)
230 if (amountToRead
== 0) {
232 /* read ahead limit */
233 /* Perhaps these two calls should both live in MemObject */
235 if (!mem_obj
->readAheadPolicyCanRead()) {
237 mem_obj
->delayRead(DeferredRead(DeferReader
, this, CommRead(conn
, buf
, len
, callback
)));
243 mem_obj
->mostBytesAllowed().delayRead(DeferredRead(DeferReader
, this, CommRead(conn
, buf
, len
, callback
)));
250 if (fd_table
[conn
->fd
].closing()) {
251 // Readers must have closing callbacks if they want to be notified. No
252 // readers appeared to care around 2009/12/14 as they skipped reading
253 // for other reasons. Closing may already be true at the delyaAwareRead
254 // call time or may happen while we wait after delayRead() above.
255 debugs(20, 3, HERE
<< "wont read from closing " << conn
<< " for " <<
257 return; // the read callback will never be called
260 comm_read(conn
, buf
, amountToRead
, callback
);
264 StoreEntry::bytesWanted (Range
<size_t> const aRange
, bool ignoreDelayPools
) const
269 #if URL_CHECKSUM_DEBUG
271 mem_obj
->checkUrlChecksum();
275 if (!mem_obj
->readAheadPolicyCanRead())
278 return mem_obj
->mostBytesWanted(aRange
.end
, ignoreDelayPools
);
282 StoreEntry::checkDeferRead(int) const
284 return (bytesWanted(Range
<size_t>(0,INT_MAX
)) == 0);
288 StoreEntry::setNoDelay(bool const newValue
)
291 mem_obj
->setNoDelay(newValue
);
294 // XXX: Type names mislead. STORE_DISK_CLIENT actually means that we should
295 // open swapin file, aggressively trim memory, and ignore read-ahead gap.
296 // It does not mean we will read from disk exclusively (or at all!).
297 // XXX: May create STORE_DISK_CLIENT with no disk caching configured.
298 // XXX: Collapsed clients cannot predict their type.
300 StoreEntry::storeClientType() const
302 /* The needed offset isn't in memory
303 * XXX TODO: this is wrong for range requests
304 * as the needed offset may *not* be 0, AND
305 * offset 0 in the memory object is the HTTP headers.
310 if (mem_obj
->inmem_lo
)
311 return STORE_DISK_CLIENT
;
313 if (EBIT_TEST(flags
, ENTRY_ABORTED
)) {
314 /* I don't think we should be adding clients to aborted entries */
315 debugs(20, DBG_IMPORTANT
, "storeClientType: adding to ENTRY_ABORTED entry");
316 return STORE_MEM_CLIENT
;
319 if (store_status
== STORE_OK
) {
320 /* the object has completed. */
322 if (mem_obj
->inmem_lo
== 0 && !isEmpty()) {
323 if (swap_status
== SWAPOUT_DONE
) {
324 debugs(20,7, HERE
<< mem_obj
<< " lo: " << mem_obj
->inmem_lo
<< " hi: " << mem_obj
->endOffset() << " size: " << mem_obj
->object_sz
);
325 if (mem_obj
->endOffset() == mem_obj
->object_sz
) {
326 /* hot object fully swapped in (XXX: or swapped out?) */
327 return STORE_MEM_CLIENT
;
330 /* Memory-only, or currently being swapped out */
331 return STORE_MEM_CLIENT
;
334 return STORE_DISK_CLIENT
;
337 /* here and past, entry is STORE_PENDING */
339 * If this is the first client, let it be the mem client
341 if (mem_obj
->nclients
== 1)
342 return STORE_MEM_CLIENT
;
345 * If there is no disk file to open yet, we must make this a
346 * mem client. If we can't open the swapin file before writing
347 * to the client, there is no guarantee that we will be able
348 * to open it later when we really need it.
350 if (swap_status
== SWAPOUT_NONE
)
351 return STORE_MEM_CLIENT
;
354 * otherwise, make subsequent clients read from disk so they
355 * can not delay the first, and vice-versa.
357 return STORE_DISK_CLIENT
;
360 StoreEntry::StoreEntry() :
371 mem_status(NOT_IN_MEMORY
),
372 ping_status(PING_NONE
),
373 store_status(STORE_PENDING
),
374 swap_status(SWAPOUT_NONE
),
377 debugs(20, 5, "StoreEntry constructed, this=" << this);
380 StoreEntry::~StoreEntry()
382 debugs(20, 5, "StoreEntry destructed, this=" << this);
387 StoreEntry::deferProducer(const AsyncCall::Pointer
&producer
)
389 if (!deferredProducer
)
390 deferredProducer
= producer
;
392 debugs(20, 5, HERE
<< "Deferred producer call is allready set to: " <<
393 *deferredProducer
<< ", requested call: " << *producer
);
397 StoreEntry::kickProducer()
399 if (deferredProducer
!= NULL
) {
400 ScheduleCallHere(deferredProducer
);
401 deferredProducer
= NULL
;
407 StoreEntry::destroyMemObject()
409 debugs(20, 3, HERE
<< "destroyMemObject " << mem_obj
);
411 if (MemObject
*mem
= mem_obj
) {
412 // Store::Root() is FATALly missing during shutdown
413 if (mem
->xitTable
.index
>= 0 && !shutting_down
)
414 Store::Root().transientsDisconnect(*mem
);
415 if (mem
->memCache
.index
>= 0 && !shutting_down
)
416 Store::Root().memoryDisconnect(*this);
418 setMemStatus(NOT_IN_MEMORY
);
425 destroyStoreEntry(void *data
)
427 debugs(20, 3, HERE
<< "destroyStoreEntry: destroying " << data
);
428 StoreEntry
*e
= static_cast<StoreEntry
*>(static_cast<hash_link
*>(data
));
431 if (e
== NullStoreEntry::getInstance())
434 // Store::Root() is FATALly missing during shutdown
435 if (e
->swap_filen
>= 0 && !shutting_down
) {
436 SwapDir
&sd
= dynamic_cast<SwapDir
&>(*e
->store());
440 e
->destroyMemObject();
444 assert(e
->key
== NULL
);
449 /* ----- INTERFACE BETWEEN STORAGE MANAGER AND HASH TABLE FUNCTIONS --------- */
452 StoreEntry::hashInsert(const cache_key
* someKey
)
454 debugs(20, 3, "StoreEntry::hashInsert: Inserting Entry " << *this << " key '" << storeKeyText(someKey
) << "'");
455 key
= storeKeyDup(someKey
);
456 hash_join(store_table
, this);
460 StoreEntry::hashDelete()
462 if (key
) { // some test cases do not create keys and do not hashInsert()
463 hash_remove_link(store_table
, this);
464 storeKeyFree((const cache_key
*)key
);
469 /* -------------------------------------------------------------------------- */
471 /* get rid of memory copy of the object */
473 StoreEntry::purgeMem()
478 debugs(20, 3, "StoreEntry::purgeMem: Freeing memory-copy of " << getMD5Text());
480 Store::Root().memoryUnlink(*this);
482 if (swap_status
!= SWAPOUT_DONE
)
487 StoreEntry::lock(const char *context
)
490 debugs(20, 3, context
<< " locked key " << getMD5Text() << ' ' << *this);
496 lastref
= squid_curtime
;
497 Store::Root().reference(*this);
501 StoreEntry::setReleaseFlag()
503 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
506 debugs(20, 3, "StoreEntry::setReleaseFlag: '" << getMD5Text() << "'");
508 EBIT_SET(flags
, RELEASE_REQUEST
);
510 Store::Root().markForUnlink(*this);
514 StoreEntry::releaseRequest()
516 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
519 setReleaseFlag(); // makes validToSend() false, preventing future hits
525 StoreEntry::unlock(const char *context
)
527 debugs(20, 3, (context
? context
: "somebody") <<
528 " unlocking key " << getMD5Text() << ' ' << *this);
529 assert(lock_count
> 0);
533 return (int) lock_count
;
535 if (store_status
== STORE_PENDING
)
538 assert(storePendingNClients(this) == 0);
540 if (EBIT_TEST(flags
, RELEASE_REQUEST
)) {
545 if (EBIT_TEST(flags
, KEY_PRIVATE
))
546 debugs(20, DBG_IMPORTANT
, "WARNING: " << __FILE__
<< ":" << __LINE__
<< ": found KEY_PRIVATE");
548 Store::Root().handleIdleEntry(*this); // may delete us
553 StoreEntry::getPublicByRequestMethod (StoreClient
*aClient
, HttpRequest
* request
, const HttpRequestMethod
& method
)
556 StoreEntry
*result
= storeGetPublicByRequestMethod( request
, method
);
559 aClient
->created (NullStoreEntry::getInstance());
561 aClient
->created (result
);
565 StoreEntry::getPublicByRequest (StoreClient
*aClient
, HttpRequest
* request
)
568 StoreEntry
*result
= storeGetPublicByRequest (request
);
571 result
= NullStoreEntry::getInstance();
573 aClient
->created (result
);
577 StoreEntry::getPublic (StoreClient
*aClient
, const char *uri
, const HttpRequestMethod
& method
)
580 StoreEntry
*result
= storeGetPublic (uri
, method
);
583 result
= NullStoreEntry::getInstance();
585 aClient
->created (result
);
589 storeGetPublic(const char *uri
, const HttpRequestMethod
& method
)
591 return Store::Root().get(storeKeyPublic(uri
, method
));
595 storeGetPublicByRequestMethod(HttpRequest
* req
, const HttpRequestMethod
& method
)
597 return Store::Root().get(storeKeyPublicByRequestMethod(req
, method
));
601 storeGetPublicByRequest(HttpRequest
* req
)
603 StoreEntry
*e
= storeGetPublicByRequestMethod(req
, req
->method
);
605 if (e
== NULL
&& req
->method
== Http::METHOD_HEAD
)
606 /* We can generate a HEAD reply from a cached GET object */
607 e
= storeGetPublicByRequestMethod(req
, Http::METHOD_GET
);
615 static int key_counter
= 0;
617 if (++key_counter
< 0)
623 /* RBC 20050104 AFAICT this should become simpler:
624 * rather than reinserting with a special key it should be marked
625 * as 'released' and then cleaned up when refcounting indicates.
626 * the StoreHashIndex could well implement its 'released' in the
628 * Also, clean log writing should skip over ia,t
629 * Otherwise, we need a 'remove from the index but not the store
633 StoreEntry::setPrivateKey()
635 const cache_key
*newkey
;
637 if (key
&& EBIT_TEST(flags
, KEY_PRIVATE
))
638 return; /* is already private */
641 setReleaseFlag(); // will markForUnlink(); all caches/workers will know
643 // TODO: move into SwapDir::markForUnlink() already called by Root()
645 storeDirSwapLog(this, SWAP_LOG_DEL
);
650 if (mem_obj
&& mem_obj
->hasUris()) {
651 mem_obj
->id
= getKeyCounter();
652 newkey
= storeKeyPrivate(mem_obj
->storeId(), mem_obj
->method
, mem_obj
->id
);
654 newkey
= storeKeyPrivate("JUNK", Http::METHOD_NONE
, getKeyCounter());
657 assert(hash_lookup(store_table
, newkey
) == NULL
);
658 EBIT_SET(flags
, KEY_PRIVATE
);
663 StoreEntry::setPublicKey()
665 const cache_key
*newkey
;
667 if (key
&& !EBIT_TEST(flags
, KEY_PRIVATE
))
668 return; /* is already public */
673 * We can't make RELEASE_REQUEST objects public. Depending on
674 * when RELEASE_REQUEST gets set, we might not be swapping out
675 * the object. If we're not swapping out, then subsequent
676 * store clients won't be able to access object data which has
677 * been freed from memory.
679 * If RELEASE_REQUEST is set, setPublicKey() should not be called.
681 #if MORE_DEBUG_OUTPUT
683 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
684 debugs(20, DBG_IMPORTANT
, "assertion failed: RELEASE key " << key
<< ", url " << mem_obj
->url
);
688 assert(!EBIT_TEST(flags
, RELEASE_REQUEST
));
690 if (mem_obj
->request
) {
691 HttpRequest
*request
= mem_obj
->request
;
693 if (!mem_obj
->vary_headers
) {
694 /* First handle the case where the object no longer varies */
695 safe_free(request
->vary_headers
);
697 if (request
->vary_headers
&& strcmp(request
->vary_headers
, mem_obj
->vary_headers
) != 0) {
698 /* Oops.. the variance has changed. Kill the base object
699 * to record the new variance key
701 safe_free(request
->vary_headers
); /* free old "bad" variance key */
702 if (StoreEntry
*pe
= storeGetPublic(mem_obj
->storeId(), mem_obj
->method
))
706 /* Make sure the request knows the variance status */
707 if (!request
->vary_headers
) {
708 const char *vary
= httpMakeVaryMark(request
, mem_obj
->getReply());
711 request
->vary_headers
= xstrdup(vary
);
715 // TODO: storeGetPublic() calls below may create unlocked entries.
716 // We should add/use storeHas() API or lock/unlock those entries.
717 if (mem_obj
->vary_headers
&& !storeGetPublic(mem_obj
->storeId(), mem_obj
->method
)) {
718 /* Create "vary" base object */
720 StoreEntry
*pe
= storeCreateEntry(mem_obj
->storeId(), mem_obj
->logUri(), request
->flags
, request
->method
);
721 /* We are allowed to do this typecast */
722 HttpReply
*rep
= new HttpReply
;
723 rep
->setHeaders(Http::scOkay
, "Internal marker object", "x-squid-internal/vary", -1, -1, squid_curtime
+ 100000);
724 vary
= mem_obj
->getReply()->header
.getList(HDR_VARY
);
727 /* Again, we own this structure layout */
728 rep
->header
.putStr(HDR_VARY
, vary
.termedBuf());
732 #if X_ACCELERATOR_VARY
733 vary
= mem_obj
->getReply()->header
.getList(HDR_X_ACCELERATOR_VARY
);
735 if (vary
.size() > 0) {
736 /* Again, we own this structure layout */
737 rep
->header
.putStr(HDR_X_ACCELERATOR_VARY
, vary
.termedBuf());
742 pe
->replaceHttpReply(rep
, false); // no write until key is public
748 pe
->startWriting(); // after makePublic()
752 pe
->unlock("StoreEntry::setPublicKey+Vary");
755 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
757 newkey
= storeKeyPublic(mem_obj
->storeId(), mem_obj
->method
);
759 if (StoreEntry
*e2
= (StoreEntry
*)hash_lookup(store_table
, newkey
)) {
760 debugs(20, 3, "Making old " << *e2
<< " private.");
764 if (mem_obj
->request
)
765 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
767 newkey
= storeKeyPublic(mem_obj
->storeId(), mem_obj
->method
);
773 EBIT_CLR(flags
, KEY_PRIVATE
);
778 storeDirSwapLog(this, SWAP_LOG_ADD
);
782 storeCreatePureEntry(const char *url
, const char *log_url
, const RequestFlags
&flags
, const HttpRequestMethod
& method
)
784 StoreEntry
*e
= NULL
;
785 debugs(20, 3, "storeCreateEntry: '" << url
<< "'");
787 e
= new StoreEntry();
789 e
->mem_obj
->setUris(url
, log_url
, method
);
791 if (flags
.cachable
) {
792 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
797 e
->store_status
= STORE_PENDING
;
799 e
->lastref
= squid_curtime
;
800 e
->timestamp
= -1; /* set in StoreEntry::timestampsSet() */
801 e
->ping_status
= PING_NONE
;
802 EBIT_SET(e
->flags
, ENTRY_VALIDATED
);
807 storeCreateEntry(const char *url
, const char *logUrl
, const RequestFlags
&flags
, const HttpRequestMethod
& method
)
809 StoreEntry
*e
= storeCreatePureEntry(url
, logUrl
, flags
, method
);
810 e
->lock("storeCreateEntry");
812 if (neighbors_do_private_keys
|| !flags
.hierarchical
)
820 /* Mark object as expired */
822 StoreEntry::expireNow()
824 debugs(20, 3, "StoreEntry::expireNow: '" << getMD5Text() << "'");
825 expires
= squid_curtime
;
829 StoreEntry::write (StoreIOBuffer writeBuffer
)
831 assert(mem_obj
!= NULL
);
832 /* This assert will change when we teach the store to update */
833 PROF_start(StoreEntry_write
);
834 assert(store_status
== STORE_PENDING
);
836 // XXX: caller uses content offset, but we also store headers
837 if (const HttpReply
*reply
= mem_obj
->getReply())
838 writeBuffer
.offset
+= reply
->hdr_sz
;
840 debugs(20, 5, "storeWrite: writing " << writeBuffer
.length
<< " bytes for '" << getMD5Text() << "'");
841 PROF_stop(StoreEntry_write
);
842 storeGetMemSpace(writeBuffer
.length
);
843 mem_obj
->write(writeBuffer
);
845 if (!EBIT_TEST(flags
, DELAY_SENDING
))
849 /* Append incoming data from a primary server to an entry. */
851 StoreEntry::append(char const *buf
, int len
)
853 assert(mem_obj
!= NULL
);
855 assert(store_status
== STORE_PENDING
);
857 StoreIOBuffer tempBuffer
;
858 tempBuffer
.data
= (char *)buf
;
859 tempBuffer
.length
= len
;
861 * XXX sigh, offset might be < 0 here, but it gets "corrected"
862 * later. This offset crap is such a mess.
864 tempBuffer
.offset
= mem_obj
->endOffset() - (getReply() ? getReply()->hdr_sz
: 0);
869 StoreEntry::vappendf(const char *fmt
, va_list vargs
)
871 LOCAL_ARRAY(char, buf
, 4096);
877 /* Fix of bug 753r. The value of vargs is undefined
878 * after vsnprintf() returns. Make a copy of vargs
879 * incase we loop around and call vsnprintf() again.
883 if ((x
= vsnprintf(buf
, sizeof(buf
), fmt
, ap
)) < 0) {
884 fatal(xstrerr(errno
));
890 if ((x
= vsnprintf(buf
, sizeof(buf
), fmt
, vargs
)) < 0) {
891 fatal(xstrerr(errno
));
896 if (x
< static_cast<int>(sizeof(buf
))) {
901 // okay, do it the slow way.
902 char *buf2
= new char[x
+1];
903 int y
= vsnprintf(buf2
, x
+1, fmt
, vargs
);
904 assert(y
>= 0 && y
== x
);
909 // deprecated. use StoreEntry::appendf() instead.
911 storeAppendPrintf(StoreEntry
* e
, const char *fmt
,...)
915 e
->vappendf(fmt
, args
);
919 // deprecated. use StoreEntry::appendf() instead.
921 storeAppendVPrintf(StoreEntry
* e
, const char *fmt
, va_list vargs
)
923 e
->vappendf(fmt
, vargs
);
926 struct _store_check_cachable_hist
{
930 int not_entry_cachable
;
931 int wrong_content_length
;
936 int too_many_open_files
;
937 int too_many_open_fds
;
944 } store_check_cachable_hist
;
947 storeTooManyDiskFilesOpen(void)
949 if (Config
.max_open_disk_fds
== 0)
952 if (store_open_disk_fd
> Config
.max_open_disk_fds
)
959 StoreEntry::checkTooSmall()
961 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
964 if (STORE_OK
== store_status
)
965 if (mem_obj
->object_sz
>= 0 &&
966 mem_obj
->object_sz
< Config
.Store
.minObjectSize
)
968 if (getReply()->content_length
> -1)
969 if (getReply()->content_length
< Config
.Store
.minObjectSize
)
975 StoreEntry::checkTooBig() const
977 if (mem_obj
->endOffset() > store_maxobjsize
)
980 if (getReply()->content_length
< 0)
983 return (getReply()->content_length
> store_maxobjsize
);
986 // TODO: move "too many open..." checks outside -- we are called too early/late
988 StoreEntry::checkCachable()
990 // XXX: This method is used for both memory and disk caches, but some
991 // checks are specific to disk caches. Move them to mayStartSwapOut().
993 // XXX: This method may be called several times, sometimes with different
994 // outcomes, making store_check_cachable_hist counters misleading.
996 // check this first to optimize handling of repeated calls for uncachables
997 if (EBIT_TEST(flags
, RELEASE_REQUEST
)) {
998 debugs(20, 2, "StoreEntry::checkCachable: NO: not cachable");
999 ++store_check_cachable_hist
.no
.not_entry_cachable
; // TODO: rename?
1000 return 0; // avoid rerequesting release below
1003 #if CACHE_ALL_METHODS
1005 if (mem_obj
->method
!= Http::METHOD_GET
) {
1006 debugs(20, 2, "StoreEntry::checkCachable: NO: non-GET method");
1007 ++store_check_cachable_hist
.no
.non_get
;
1010 if (store_status
== STORE_OK
&& EBIT_TEST(flags
, ENTRY_BAD_LENGTH
)) {
1011 debugs(20, 2, "StoreEntry::checkCachable: NO: wrong content-length");
1012 ++store_check_cachable_hist
.no
.wrong_content_length
;
1013 } else if (EBIT_TEST(flags
, ENTRY_NEGCACHED
)) {
1014 debugs(20, 3, "StoreEntry::checkCachable: NO: negative cached");
1015 ++store_check_cachable_hist
.no
.negative_cached
;
1016 return 0; /* avoid release call below */
1017 } else if (!mem_obj
|| !getReply()) {
1018 // XXX: In bug 4131, we forgetHit() without mem_obj, so we need
1019 // this segfault protection, but how can we get such a HIT?
1020 debugs(20, 2, "StoreEntry::checkCachable: NO: missing parts: " << *this);
1021 ++store_check_cachable_hist
.no
.missing_parts
;
1022 } else if (checkTooBig()) {
1023 debugs(20, 2, "StoreEntry::checkCachable: NO: too big");
1024 ++store_check_cachable_hist
.no
.too_big
;
1025 } else if (checkTooSmall()) {
1026 debugs(20, 2, "StoreEntry::checkCachable: NO: too small");
1027 ++store_check_cachable_hist
.no
.too_small
;
1028 } else if (EBIT_TEST(flags
, KEY_PRIVATE
)) {
1029 debugs(20, 3, "StoreEntry::checkCachable: NO: private key");
1030 ++store_check_cachable_hist
.no
.private_key
;
1031 } else if (swap_status
!= SWAPOUT_NONE
) {
1033 * here we checked the swap_status because the remaining
1034 * cases are only relevant only if we haven't started swapping
1035 * out the object yet.
1038 } else if (storeTooManyDiskFilesOpen()) {
1039 debugs(20, 2, "StoreEntry::checkCachable: NO: too many disk files open");
1040 ++store_check_cachable_hist
.no
.too_many_open_files
;
1041 } else if (fdNFree() < RESERVED_FD
) {
1042 debugs(20, 2, "StoreEntry::checkCachable: NO: too many FD's open");
1043 ++store_check_cachable_hist
.no
.too_many_open_fds
;
1045 ++store_check_cachable_hist
.yes
.Default
;
1054 storeCheckCachableStats(StoreEntry
*sentry
)
1056 storeAppendPrintf(sentry
, "Category\t Count\n");
1058 #if CACHE_ALL_METHODS
1060 storeAppendPrintf(sentry
, "no.non_get\t%d\n",
1061 store_check_cachable_hist
.no
.non_get
);
1064 storeAppendPrintf(sentry
, "no.not_entry_cachable\t%d\n",
1065 store_check_cachable_hist
.no
.not_entry_cachable
);
1066 storeAppendPrintf(sentry
, "no.wrong_content_length\t%d\n",
1067 store_check_cachable_hist
.no
.wrong_content_length
);
1068 storeAppendPrintf(sentry
, "no.negative_cached\t%d\n",
1069 store_check_cachable_hist
.no
.negative_cached
);
1070 storeAppendPrintf(sentry
, "no.missing_parts\t%d\n",
1071 store_check_cachable_hist
.no
.missing_parts
);
1072 storeAppendPrintf(sentry
, "no.too_big\t%d\n",
1073 store_check_cachable_hist
.no
.too_big
);
1074 storeAppendPrintf(sentry
, "no.too_small\t%d\n",
1075 store_check_cachable_hist
.no
.too_small
);
1076 storeAppendPrintf(sentry
, "no.private_key\t%d\n",
1077 store_check_cachable_hist
.no
.private_key
);
1078 storeAppendPrintf(sentry
, "no.too_many_open_files\t%d\n",
1079 store_check_cachable_hist
.no
.too_many_open_files
);
1080 storeAppendPrintf(sentry
, "no.too_many_open_fds\t%d\n",
1081 store_check_cachable_hist
.no
.too_many_open_fds
);
1082 storeAppendPrintf(sentry
, "yes.default\t%d\n",
1083 store_check_cachable_hist
.yes
.Default
);
1087 StoreEntry::complete()
1089 debugs(20, 3, "storeComplete: '" << getMD5Text() << "'");
1091 if (store_status
!= STORE_PENDING
) {
1093 * if we're not STORE_PENDING, then probably we got aborted
1094 * and there should be NO clients on this entry
1096 assert(EBIT_TEST(flags
, ENTRY_ABORTED
));
1097 assert(mem_obj
->nclients
== 0);
1101 /* This is suspect: mem obj offsets include the headers. do we adjust for that
1102 * in use of object_sz?
1104 mem_obj
->object_sz
= mem_obj
->endOffset();
1106 store_status
= STORE_OK
;
1108 assert(mem_status
== NOT_IN_MEMORY
);
1110 if (!validLength()) {
1111 EBIT_SET(flags
, ENTRY_BAD_LENGTH
);
1115 #if USE_CACHE_DIGESTS
1116 if (mem_obj
->request
)
1117 mem_obj
->request
->hier
.store_complete_stop
= current_time
;
1121 * We used to call invokeHandlers, then storeSwapOut. However,
1122 * Madhukar Reddy <myreddy@persistence.com> reported that
1123 * responses without content length would sometimes get released
1124 * in client_side, thinking that the response is incomplete.
1130 * Someone wants to abort this transfer. Set the reason in the
1131 * request structure, call the callback and mark the
1132 * entry for releasing
1137 ++statCounter
.aborted_requests
;
1138 assert(store_status
== STORE_PENDING
);
1139 assert(mem_obj
!= NULL
);
1140 debugs(20, 6, "storeAbort: " << getMD5Text());
1142 lock("StoreEntry::abort"); /* lock while aborting */
1147 EBIT_SET(flags
, ENTRY_ABORTED
);
1149 setMemStatus(NOT_IN_MEMORY
);
1151 store_status
= STORE_OK
;
1153 /* Notify the server side */
1157 * Should we check abort.data for validity?
1159 if (mem_obj
->abort
.callback
) {
1160 if (!cbdataReferenceValid(mem_obj
->abort
.data
))
1161 debugs(20, DBG_IMPORTANT
,HERE
<< "queueing event when abort.data is not valid");
1162 eventAdd("mem_obj->abort.callback",
1163 mem_obj
->abort
.callback
,
1164 mem_obj
->abort
.data
,
1170 /* XXX Should we reverse these two, so that there is no
1171 * unneeded disk swapping triggered?
1173 /* Notify the client side */
1176 // abort swap out, invalidating what was created so far (release follows)
1177 swapOutFileClose(StoreIOState::writerGone
);
1179 unlock("StoreEntry::abort"); /* unlock */
1183 * Clear Memory storage to accommodate the given object len
1186 storeGetMemSpace(int size
)
1188 PROF_start(storeGetMemSpace
);
1189 StoreEntry
*e
= NULL
;
1191 static time_t last_check
= 0;
1192 size_t pages_needed
;
1193 RemovalPurgeWalker
*walker
;
1195 if (squid_curtime
== last_check
) {
1196 PROF_stop(storeGetMemSpace
);
1200 last_check
= squid_curtime
;
1202 pages_needed
= (size
+ SM_PAGE_SIZE
-1) / SM_PAGE_SIZE
;
1204 if (mem_node::InUseCount() + pages_needed
< store_pages_max
) {
1205 PROF_stop(storeGetMemSpace
);
1209 debugs(20, 2, "storeGetMemSpace: Starting, need " << pages_needed
<<
1212 /* XXX what to set as max_scan here? */
1213 walker
= mem_policy
->PurgeInit(mem_policy
, 100000);
1215 while ((e
= walker
->Next(walker
))) {
1219 if (mem_node::InUseCount() + pages_needed
< store_pages_max
)
1223 walker
->Done(walker
);
1224 debugs(20, 3, "storeGetMemSpace stats:");
1225 debugs(20, 3, " " << std::setw(6) << hot_obj_count
<< " HOT objects");
1226 debugs(20, 3, " " << std::setw(6) << released
<< " were released");
1227 PROF_stop(storeGetMemSpace
);
1230 /* thunk through to Store::Root().maintain(). Note that this would be better still
1231 * if registered against the root store itself, but that requires more complex
1232 * update logic - bigger fish to fry first. Long term each store when
1233 * it becomes active will self register
1236 Store::Maintain(void *)
1238 Store::Root().maintain();
1240 /* Reregister a maintain event .. */
1241 eventAdd("MaintainSwapSpace", Maintain
, NULL
, 1.0, 1);
1245 /* The maximum objects to scan for maintain storage space */
1246 #define MAINTAIN_MAX_SCAN 1024
1247 #define MAINTAIN_MAX_REMOVE 64
1250 * This routine is to be called by main loop in main.c.
1251 * It removes expired objects on only one bucket for each time called.
1253 * This should get called 1/s from main().
1256 StoreController::maintain()
1258 static time_t last_warn_time
= 0;
1260 PROF_start(storeMaintainSwapSpace
);
1261 swapDir
->maintain();
1263 /* this should be emitted by the oversize dir, not globally */
1265 if (Store::Root().currentSize() > Store::Root().maxSize()) {
1266 if (squid_curtime
- last_warn_time
> 10) {
1267 debugs(20, DBG_CRITICAL
, "WARNING: Disk space over limit: "
1268 << Store::Root().currentSize() / 1024.0 << " KB > "
1269 << (Store::Root().maxSize() >> 10) << " KB");
1270 last_warn_time
= squid_curtime
;
1274 PROF_stop(storeMaintainSwapSpace
);
1277 /* release an object from a cache */
1279 StoreEntry::release()
1281 PROF_start(storeRelease
);
1282 debugs(20, 3, "releasing " << *this << ' ' << getMD5Text());
1283 /* If, for any reason we can't discard this object because of an
1284 * outstanding request, mark it for pending release */
1288 debugs(20, 3, "storeRelease: Only setting RELEASE_REQUEST bit");
1290 PROF_stop(storeRelease
);
1294 Store::Root().memoryUnlink(*this);
1296 if (StoreController::store_dirs_rebuilding
&& swap_filen
> -1) {
1299 if (swap_filen
> -1) {
1300 // lock the entry until rebuilding is done
1301 lock("storeLateRelease");
1303 LateReleaseStack
.push(this);
1305 destroyStoreEntry(static_cast<hash_link
*>(this));
1306 // "this" is no longer valid
1309 PROF_stop(storeRelease
);
1313 storeLog(STORE_LOG_RELEASE
, this);
1315 if (swap_filen
> -1) {
1316 // log before unlink() below clears swap_filen
1317 if (!EBIT_TEST(flags
, KEY_PRIVATE
))
1318 storeDirSwapLog(this, SWAP_LOG_DEL
);
1323 destroyStoreEntry(static_cast<hash_link
*>(this));
1324 PROF_stop(storeRelease
);
1328 storeLateRelease(void *)
1333 if (StoreController::store_dirs_rebuilding
) {
1334 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
1338 // TODO: this works but looks unelegant.
1339 for (int i
= 0; i
< 10; ++i
) {
1340 if (LateReleaseStack
.empty()) {
1341 debugs(20, DBG_IMPORTANT
, "storeLateRelease: released " << n
<< " objects");
1344 e
= LateReleaseStack
.top();
1345 LateReleaseStack
.pop();
1348 e
->unlock("storeLateRelease");
1352 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 0.0, 1);
1355 /* return 1 if a store entry is locked */
1357 StoreEntry::locked() const
1363 * SPECIAL, PUBLIC entries should be "locked";
1364 * XXX: Their owner should lock them then instead of relying on this hack.
1366 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
1367 if (!EBIT_TEST(flags
, KEY_PRIVATE
))
1374 StoreEntry::validLength() const
1377 const HttpReply
*reply
;
1378 assert(mem_obj
!= NULL
);
1380 debugs(20, 3, "storeEntryValidLength: Checking '" << getMD5Text() << "'");
1381 debugs(20, 5, "storeEntryValidLength: object_len = " <<
1383 debugs(20, 5, "storeEntryValidLength: hdr_sz = " << reply
->hdr_sz
);
1384 debugs(20, 5, "storeEntryValidLength: content_length = " << reply
->content_length
);
1386 if (reply
->content_length
< 0) {
1387 debugs(20, 5, "storeEntryValidLength: Unspecified content length: " << getMD5Text());
1391 if (reply
->hdr_sz
== 0) {
1392 debugs(20, 5, "storeEntryValidLength: Zero header size: " << getMD5Text());
1396 if (mem_obj
->method
== Http::METHOD_HEAD
) {
1397 debugs(20, 5, "storeEntryValidLength: HEAD request: " << getMD5Text());
1401 if (reply
->sline
.status() == Http::scNotModified
)
1404 if (reply
->sline
.status() == Http::scNoContent
)
1407 diff
= reply
->hdr_sz
+ reply
->content_length
- objectLen();
1412 debugs(20, 3, "storeEntryValidLength: " << (diff
< 0 ? -diff
: diff
) << " bytes too " << (diff
< 0 ? "big" : "small") <<"; '" << getMD5Text() << "'" );
1418 storeRegisterWithCacheManager(void)
1420 Mgr::RegisterAction("storedir", "Store Directory Stats", Store::Stats
, 0, 1);
1421 Mgr::RegisterAction("store_io", "Store IO Interface Stats", &Mgr::StoreIoAction::Create
, 0, 1);
1422 Mgr::RegisterAction("store_check_cachable_stats", "storeCheckCachable() Stats",
1423 storeCheckCachableStats
, 0, 1);
1430 mem_policy
= createRemovalPolicy(Config
.memPolicy
);
1433 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
1434 Store::Root().init();
1435 storeRebuildStart();
1437 storeRegisterWithCacheManager();
1440 /// computes maximum size of a cachable object
1441 /// larger objects are rejected by all (disk and memory) cache stores
1443 storeCalcMaxObjSize()
1445 int64_t ms
= 0; // nothing can be cached without at least one store consent
1447 // global maximum is at least the disk store maximum
1448 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1449 assert (Config
.cacheSwap
.swapDirs
[i
].getRaw());
1450 const int64_t storeMax
= dynamic_cast<SwapDir
*>(Config
.cacheSwap
.swapDirs
[i
].getRaw())->maxObjectSize();
1455 // global maximum is at least the memory store maximum
1456 // TODO: move this into a memory cache class when we have one
1457 const int64_t memMax
= static_cast<int64_t>(min(Config
.Store
.maxInMemObjSize
, Config
.memMaxSize
));
1465 storeConfigure(void)
1467 store_swap_high
= (long) (((float) Store::Root().maxSize() *
1468 (float) Config
.Swap
.highWaterMark
) / (float) 100);
1469 store_swap_low
= (long) (((float) Store::Root().maxSize() *
1470 (float) Config
.Swap
.lowWaterMark
) / (float) 100);
1471 store_pages_max
= Config
.memMaxSize
/ sizeof(mem_node
);
1473 store_maxobjsize
= storeCalcMaxObjSize();
1477 StoreEntry::memoryCachable()
1479 if (!checkCachable())
1482 if (mem_obj
== NULL
)
1485 if (mem_obj
->data_hdr
.size() == 0)
1488 if (mem_obj
->inmem_lo
!= 0)
1491 if (!Config
.onoff
.memory_cache_first
&& swap_status
== SWAPOUT_DONE
&& refcount
== 1)
1498 StoreEntry::checkNegativeHit() const
1500 if (!EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1503 if (expires
<= squid_curtime
)
1506 if (store_status
!= STORE_OK
)
1513 * Set object for negative caching.
1514 * Preserves any expiry information given by the server.
1515 * In absence of proper expiry info it will set to expire immediately,
1516 * or with HTTP-violations enabled the configured negative-TTL is observed
1519 StoreEntry::negativeCache()
1521 // XXX: should make the default for expires 0 instead of -1
1522 // so we can distinguish "Expires: -1" from nothing.
1524 #if USE_HTTP_VIOLATIONS
1525 expires
= squid_curtime
+ Config
.negativeTtl
;
1527 expires
= squid_curtime
;
1529 EBIT_SET(flags
, ENTRY_NEGCACHED
);
1533 storeFreeMemory(void)
1536 #if USE_CACHE_DIGESTS
1539 cacheDigestDestroy(store_digest
);
1543 store_digest
= NULL
;
1547 expiresMoreThan(time_t expires
, time_t when
)
1549 if (expires
< 0) /* No Expires given */
1552 return (expires
> (squid_curtime
+ when
));
1556 StoreEntry::validToSend() const
1558 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
1561 if (EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1562 if (expires
<= squid_curtime
)
1565 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
1568 // now check that the entry has a cache backing or is collapsed
1569 if (swap_filen
> -1) // backed by a disk cache
1572 if (swappingOut()) // will be backed by a disk cache
1575 if (!mem_obj
) // not backed by a memory cache and not collapsed
1578 // StoreEntry::storeClientType() assumes DISK_CLIENT here, but there is no
1579 // disk cache backing that store_client constructor will assert. XXX: This
1580 // is wrong for range requests (that could feed off nibbled memory) and for
1581 // entries backed by the shared memory cache (that could, in theory, get
1582 // nibbled bytes from that cache, but there is no such "memoryIn" code).
1583 if (mem_obj
->inmem_lo
) // in memory cache, but got nibbled at
1586 // The following check is correct but useless at this position. TODO: Move
1587 // it up when the shared memory cache can either replenish locally nibbled
1588 // bytes or, better, does not use local RAM copy at all.
1589 // if (mem_obj->memCache.index >= 0) // backed by a shared memory cache
1596 StoreEntry::timestampsSet()
1598 const HttpReply
*reply
= getReply();
1599 time_t served_date
= reply
->date
;
1600 int age
= reply
->header
.getInt(HDR_AGE
);
1601 /* Compute the timestamp, mimicking RFC2616 section 13.2.3. */
1602 /* make sure that 0 <= served_date <= squid_curtime */
1604 if (served_date
< 0 || served_date
> squid_curtime
)
1605 served_date
= squid_curtime
;
1608 * If the returned Date: is more than 24 hours older than
1609 * the squid_curtime, then one of us needs to use NTP to set our
1610 * clock. We'll pretend that our clock is right.
1612 else if (served_date
< (squid_curtime
- 24 * 60 * 60) )
1613 served_date
= squid_curtime
;
1616 * Compensate with Age header if origin server clock is ahead
1617 * of us and there is a cache in between us and the origin
1618 * server. But DONT compensate if the age value is larger than
1619 * squid_curtime because it results in a negative served_date.
1621 if (age
> squid_curtime
- served_date
)
1622 if (squid_curtime
> age
)
1623 served_date
= squid_curtime
- age
;
1625 // compensate for Squid-to-server and server-to-Squid delays
1626 if (mem_obj
&& mem_obj
->request
) {
1627 const time_t request_sent
=
1628 mem_obj
->request
->hier
.peer_http_request_sent
.tv_sec
;
1629 if (0 < request_sent
&& request_sent
< squid_curtime
)
1630 served_date
-= (squid_curtime
- request_sent
);
1633 if (reply
->expires
> 0 && reply
->date
> -1)
1634 expires
= served_date
+ (reply
->expires
- reply
->date
);
1636 expires
= reply
->expires
;
1638 lastmod
= reply
->last_modified
;
1640 timestamp
= served_date
;
1644 StoreEntry::registerAbort(STABH
* cb
, void *data
)
1647 assert(mem_obj
->abort
.callback
== NULL
);
1648 mem_obj
->abort
.callback
= cb
;
1649 mem_obj
->abort
.data
= cbdataReference(data
);
1653 StoreEntry::unregisterAbort()
1656 if (mem_obj
->abort
.callback
) {
1657 mem_obj
->abort
.callback
= NULL
;
1658 cbdataReferenceDone(mem_obj
->abort
.data
);
1663 StoreEntry::dump(int l
) const
1665 debugs(20, l
, "StoreEntry->key: " << getMD5Text());
1666 debugs(20, l
, "StoreEntry->next: " << next
);
1667 debugs(20, l
, "StoreEntry->mem_obj: " << mem_obj
);
1668 debugs(20, l
, "StoreEntry->timestamp: " << timestamp
);
1669 debugs(20, l
, "StoreEntry->lastref: " << lastref
);
1670 debugs(20, l
, "StoreEntry->expires: " << expires
);
1671 debugs(20, l
, "StoreEntry->lastmod: " << lastmod
);
1672 debugs(20, l
, "StoreEntry->swap_file_sz: " << swap_file_sz
);
1673 debugs(20, l
, "StoreEntry->refcount: " << refcount
);
1674 debugs(20, l
, "StoreEntry->flags: " << storeEntryFlags(this));
1675 debugs(20, l
, "StoreEntry->swap_dirn: " << swap_dirn
);
1676 debugs(20, l
, "StoreEntry->swap_filen: " << swap_filen
);
1677 debugs(20, l
, "StoreEntry->lock_count: " << lock_count
);
1678 debugs(20, l
, "StoreEntry->mem_status: " << mem_status
);
1679 debugs(20, l
, "StoreEntry->ping_status: " << ping_status
);
1680 debugs(20, l
, "StoreEntry->store_status: " << store_status
);
1681 debugs(20, l
, "StoreEntry->swap_status: " << swap_status
);
1685 * NOTE, this function assumes only two mem states
1688 StoreEntry::setMemStatus(mem_status_t new_status
)
1690 if (new_status
== mem_status
)
1693 // are we using a shared memory cache?
1694 if (Config
.memShared
&& IamWorkerProcess()) {
1695 // This method was designed to update replacement policy, not to
1696 // actually purge something from the memory cache (TODO: rename?).
1697 // Shared memory cache does not have a policy that needs updates.
1698 mem_status
= new_status
;
1702 assert(mem_obj
!= NULL
);
1704 if (new_status
== IN_MEMORY
) {
1705 assert(mem_obj
->inmem_lo
== 0);
1707 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1708 debugs(20, 4, "not inserting special " << *this << " into policy");
1710 mem_policy
->Add(mem_policy
, this, &mem_obj
->repl
);
1711 debugs(20, 4, "inserted " << *this << " key: " << getMD5Text());
1714 ++hot_obj_count
; // TODO: maintain for the shared hot cache as well
1716 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1717 debugs(20, 4, "not removing special " << *this << " from policy");
1719 mem_policy
->Remove(mem_policy
, this, &mem_obj
->repl
);
1720 debugs(20, 4, "removed " << *this);
1726 mem_status
= new_status
;
1730 StoreEntry::url() const
1732 if (mem_obj
== NULL
)
1733 return "[null_mem_obj]";
1735 return mem_obj
->storeId();
1739 StoreEntry::makeMemObject()
1742 mem_obj
= new MemObject();
1747 StoreEntry::createMemObject(const char *aUrl
, const char *aLogUrl
, const HttpRequestMethod
&aMethod
)
1750 mem_obj
->setUris(aUrl
, aLogUrl
, aMethod
);
1753 /* this just sets DELAY_SENDING */
1755 StoreEntry::buffer()
1757 EBIT_SET(flags
, DELAY_SENDING
);
1760 /* this just clears DELAY_SENDING and Invokes the handlers */
1764 if (EBIT_TEST(flags
, DELAY_SENDING
)) {
1765 EBIT_CLR(flags
, DELAY_SENDING
);
1771 StoreEntry::objectLen() const
1773 assert(mem_obj
!= NULL
);
1774 return mem_obj
->object_sz
;
1778 StoreEntry::contentLen() const
1780 assert(mem_obj
!= NULL
);
1781 assert(getReply() != NULL
);
1782 return objectLen() - getReply()->hdr_sz
;
1786 StoreEntry::getReply () const
1788 if (NULL
== mem_obj
)
1791 return mem_obj
->getReply();
1798 debugs(20, 3, "StoreEntry::reset: " << url());
1800 HttpReply
*rep
= (HttpReply
*) getReply(); // bypass const
1802 expires
= lastmod
= timestamp
= -1;
1808 * This routine calls the SETUP routine for each fs type.
1809 * I don't know where the best place for this is, and I'm not going to shuffle
1810 * around large chunks of code right now (that can be done once its working.)
1819 * called to add another store removal policy module
1822 storeReplAdd(const char *type
, REMOVALPOLICYCREATE
* create
)
1826 /* find the number of currently known repl types */
1827 for (i
= 0; storerepl_list
&& storerepl_list
[i
].typestr
; ++i
) {
1828 if (strcmp(storerepl_list
[i
].typestr
, type
) == 0) {
1829 debugs(20, DBG_IMPORTANT
, "WARNING: Trying to load store replacement policy " << type
<< " twice.");
1834 /* add the new type */
1835 storerepl_list
= static_cast<storerepl_entry_t
*>(xrealloc(storerepl_list
, (i
+ 2) * sizeof(storerepl_entry_t
)));
1837 memset(&storerepl_list
[i
+ 1], 0, sizeof(storerepl_entry_t
));
1839 storerepl_list
[i
].typestr
= type
;
1841 storerepl_list
[i
].create
= create
;
1845 * Create a removal policy instance
1848 createRemovalPolicy(RemovalPolicySettings
* settings
)
1850 storerepl_entry_t
*r
;
1852 for (r
= storerepl_list
; r
&& r
->typestr
; ++r
) {
1853 if (strcmp(r
->typestr
, settings
->type
) == 0)
1854 return r
->create(settings
->args
);
1857 debugs(20, DBG_IMPORTANT
, "ERROR: Unknown policy " << settings
->type
);
1858 debugs(20, DBG_IMPORTANT
, "ERROR: Be sure to have set cache_replacement_policy");
1859 debugs(20, DBG_IMPORTANT
, "ERROR: and memory_replacement_policy in squid.conf!");
1860 fatalf("ERROR: Unknown policy %s\n", settings
->type
);
1861 return NULL
; /* NOTREACHED */
1866 storeSwapFileNumberSet(StoreEntry
* e
, sfileno filn
)
1868 if (e
->swap_file_number
== filn
)
1873 storeDirMapBitReset(e
->swap_file_number
);
1874 storeDirLRUDelete(e
);
1875 e
->swap_file_number
= -1;
1877 assert(-1 == e
->swap_file_number
);
1878 storeDirMapBitSet(e
->swap_file_number
= filn
);
1886 StoreEntry::storeErrorResponse(HttpReply
*reply
)
1888 lock("StoreEntry::storeErrorResponse");
1890 replaceHttpReply(reply
);
1895 unlock("StoreEntry::storeErrorResponse");
1899 * Replace a store entry with
1900 * a new reply. This eats the reply.
1903 StoreEntry::replaceHttpReply(HttpReply
*rep
, bool andStartWriting
)
1905 debugs(20, 3, "StoreEntry::replaceHttpReply: " << url());
1908 debugs(20, DBG_CRITICAL
, "Attempt to replace object with no in-memory representation");
1912 mem_obj
->replaceHttpReply(rep
);
1914 if (andStartWriting
)
1919 StoreEntry::startWriting()
1921 /* TODO: when we store headers serparately remove the header portion */
1922 /* TODO: mark the length of the headers ? */
1923 /* We ONLY want the headers */
1928 const HttpReply
*rep
= getReply();
1932 rep
->packHeadersInto(this);
1933 mem_obj
->markEndOfReplyHeaders();
1934 EBIT_CLR(flags
, ENTRY_FWD_HDR_WAIT
);
1936 rep
->body
.packInto(this);
1940 StoreEntry::getSerialisedMetaData()
1942 StoreMeta
*tlv_list
= storeSwapMetaBuild(this);
1944 char *result
= storeSwapMetaPack(tlv_list
, &swap_hdr_sz
);
1945 storeSwapTLVFree(tlv_list
);
1946 assert (swap_hdr_sz
>= 0);
1947 mem_obj
->swap_hdr_sz
= (size_t) swap_hdr_sz
;
1952 * Abandon the transient entry our worker has created if neither the shared
1953 * memory cache nor the disk cache wants to store it. Collapsed requests, if
1954 * any, should notice and use Plan B instead of getting stuck waiting for us
1955 * to start swapping the entry out.
1958 StoreEntry::transientsAbandonmentCheck()
1960 if (mem_obj
&& !mem_obj
->smpCollapsed
&& // this worker is responsible
1961 mem_obj
->xitTable
.index
>= 0 && // other workers may be interested
1962 mem_obj
->memCache
.index
< 0 && // rejected by the shared memory cache
1963 mem_obj
->swapout
.decision
== MemObject::SwapOut::swImpossible
) {
1964 debugs(20, 7, "cannot be shared: " << *this);
1965 if (!shutting_down
) // Store::Root() is FATALly missing during shutdown
1966 Store::Root().transientsAbandon(*this);
1971 StoreEntry::memOutDecision(const bool)
1973 transientsAbandonmentCheck();
1977 StoreEntry::swapOutDecision(const MemObject::SwapOut::Decision
&decision
)
1979 // Abandon our transient entry if neither shared memory nor disk wants it.
1981 mem_obj
->swapout
.decision
= decision
;
1982 transientsAbandonmentCheck();
1986 StoreEntry::trimMemory(const bool preserveSwappable
)
1990 * Bug #1943. We must not let go any data for IN_MEMORY
1991 * objects. We have to wait until the mem_status changes.
1993 if (mem_status
== IN_MEMORY
)
1996 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
1997 return; // cannot trim because we do not load them again
1999 if (preserveSwappable
)
2000 mem_obj
->trimSwappable();
2002 mem_obj
->trimUnSwappable();
2004 debugs(88, 7, *this << " inmem_lo=" << mem_obj
->inmem_lo
);
2008 StoreEntry::modifiedSince(HttpRequest
* request
) const
2011 time_t mod_time
= lastmod
;
2014 mod_time
= timestamp
;
2016 debugs(88, 3, "modifiedSince: '" << url() << "'");
2018 debugs(88, 3, "modifiedSince: mod_time = " << mod_time
);
2023 /* Find size of the object */
2024 object_length
= getReply()->content_length
;
2026 if (object_length
< 0)
2027 object_length
= contentLen();
2029 if (mod_time
> request
->ims
) {
2030 debugs(88, 3, "--> YES: entry newer than client");
2032 } else if (mod_time
< request
->ims
) {
2033 debugs(88, 3, "--> NO: entry older than client");
2035 } else if (request
->imslen
< 0) {
2036 debugs(88, 3, "--> NO: same LMT, no client length");
2038 } else if (request
->imslen
== object_length
) {
2039 debugs(88, 3, "--> NO: same LMT, same length");
2042 debugs(88, 3, "--> YES: same LMT, different length");
2048 StoreEntry::hasEtag(ETag
&etag
) const
2050 if (const HttpReply
*reply
= getReply()) {
2051 etag
= reply
->header
.getETag(HDR_ETAG
);
2059 StoreEntry::hasIfMatchEtag(const HttpRequest
&request
) const
2061 const String reqETags
= request
.header
.getList(HDR_IF_MATCH
);
2062 return hasOneOfEtags(reqETags
, false);
2066 StoreEntry::hasIfNoneMatchEtag(const HttpRequest
&request
) const
2068 const String reqETags
= request
.header
.getList(HDR_IF_NONE_MATCH
);
2069 // weak comparison is allowed only for HEAD or full-body GET requests
2070 const bool allowWeakMatch
= !request
.flags
.isRanged
&&
2071 (request
.method
== Http::METHOD_GET
|| request
.method
== Http::METHOD_HEAD
);
2072 return hasOneOfEtags(reqETags
, allowWeakMatch
);
2075 /// whether at least one of the request ETags matches entity ETag
2077 StoreEntry::hasOneOfEtags(const String
&reqETags
, const bool allowWeakMatch
) const
2079 const ETag repETag
= getReply()->header
.getETag(HDR_ETAG
);
2081 return strListIsMember(&reqETags
, "*", ',');
2083 bool matched
= false;
2084 const char *pos
= NULL
;
2087 while (!matched
&& strListGetItem(&reqETags
, ',', &item
, &ilen
, &pos
)) {
2088 if (!strncmp(item
, "*", ilen
))
2092 str
.append(item
, ilen
);
2094 if (etagParseInit(&reqETag
, str
.termedBuf())) {
2095 matched
= allowWeakMatch
? etagIsWeakEqual(repETag
, reqETag
) :
2096 etagIsStrongEqual(repETag
, reqETag
);
2104 StoreEntry::store() const
2106 assert(0 <= swap_dirn
&& swap_dirn
< Config
.cacheSwap
.n_configured
);
2107 return INDEXSD(swap_dirn
);
2111 StoreEntry::unlink()
2113 store()->unlink(*this); // implies disconnect()
2116 swap_status
= SWAPOUT_NONE
;
2120 * return true if the entry is in a state where
2121 * it can accept more data (ie with write() method)
2124 StoreEntry::isAccepting() const
2126 if (STORE_PENDING
!= store_status
)
2129 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
2135 std::ostream
&operator <<(std::ostream
&os
, const StoreEntry
&e
)
2140 if (e
.mem_obj
->xitTable
.index
> -1)
2141 os
<< 't' << e
.mem_obj
->xitTable
.index
;
2142 if (e
.mem_obj
->memCache
.index
> -1)
2143 os
<< 'm' << e
.mem_obj
->memCache
.index
;
2145 if (e
.swap_filen
> -1 || e
.swap_dirn
> -1)
2146 os
<< 'd' << e
.swap_filen
<< '@' << e
.swap_dirn
;
2150 // print only non-default status values, using unique letters
2151 if (e
.mem_status
!= NOT_IN_MEMORY
||
2152 e
.store_status
!= STORE_PENDING
||
2153 e
.swap_status
!= SWAPOUT_NONE
||
2154 e
.ping_status
!= PING_NONE
) {
2155 if (e
.mem_status
!= NOT_IN_MEMORY
) os
<< 'm';
2156 if (e
.store_status
!= STORE_PENDING
) os
<< 's';
2157 if (e
.swap_status
!= SWAPOUT_NONE
) os
<< 'w' << e
.swap_status
;
2158 if (e
.ping_status
!= PING_NONE
) os
<< 'p' << e
.ping_status
;
2161 // print only set flags, using unique letters
2163 if (EBIT_TEST(e
.flags
, ENTRY_SPECIAL
)) os
<< 'S';
2164 if (EBIT_TEST(e
.flags
, ENTRY_REVALIDATE
)) os
<< 'R';
2165 if (EBIT_TEST(e
.flags
, DELAY_SENDING
)) os
<< 'P';
2166 if (EBIT_TEST(e
.flags
, RELEASE_REQUEST
)) os
<< 'X';
2167 if (EBIT_TEST(e
.flags
, REFRESH_REQUEST
)) os
<< 'F';
2168 if (EBIT_TEST(e
.flags
, ENTRY_DISPATCHED
)) os
<< 'D';
2169 if (EBIT_TEST(e
.flags
, KEY_PRIVATE
)) os
<< 'I';
2170 if (EBIT_TEST(e
.flags
, ENTRY_FWD_HDR_WAIT
)) os
<< 'W';
2171 if (EBIT_TEST(e
.flags
, ENTRY_NEGCACHED
)) os
<< 'N';
2172 if (EBIT_TEST(e
.flags
, ENTRY_VALIDATED
)) os
<< 'V';
2173 if (EBIT_TEST(e
.flags
, ENTRY_BAD_LENGTH
)) os
<< 'L';
2174 if (EBIT_TEST(e
.flags
, ENTRY_ABORTED
)) os
<< 'A';
2177 if (e
.mem_obj
&& e
.mem_obj
->smpCollapsed
)
2180 return os
<< '/' << &e
<< '*' << e
.locks();
2183 /* NullStoreEntry */
2185 NullStoreEntry
NullStoreEntry::_instance
;
2188 NullStoreEntry::getInstance()
2194 NullStoreEntry::getMD5Text() const
2200 NullStoreEntry::operator delete(void*)
2202 fatal ("Attempt to delete NullStoreEntry\n");
2206 NullStoreEntry::getSerialisedMetaData()