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::makePublic()
155 /* This object can be cached for a long time */
157 if (!EBIT_TEST(flags
, RELEASE_REQUEST
))
162 StoreEntry::makePrivate()
164 /* This object should never be cached at all */
166 releaseRequest(); /* delete object when not used */
170 StoreEntry::cacheNegatively()
172 /* This object may be negatively cached */
178 StoreEntry::inUseCount()
182 return pool
->getInUseCount();
186 StoreEntry::getMD5Text() const
188 return storeKeyText((const cache_key
*)key
);
194 StoreEntry::DeferReader(void *theContext
, CommRead
const &aRead
)
196 StoreEntry
*anEntry
= (StoreEntry
*)theContext
;
197 anEntry
->delayAwareRead(aRead
.conn
,
204 StoreEntry::delayAwareRead(const Comm::ConnectionPointer
&conn
, char *buf
, int len
, AsyncCall::Pointer callback
)
206 size_t amountToRead
= bytesWanted(Range
<size_t>(0, len
));
207 /* sketch: readdeferer* = getdeferer.
208 * ->deferRead (fd, buf, len, callback, DelayAwareRead, this)
211 if (amountToRead
== 0) {
213 /* read ahead limit */
214 /* Perhaps these two calls should both live in MemObject */
216 if (!mem_obj
->readAheadPolicyCanRead()) {
218 mem_obj
->delayRead(DeferredRead(DeferReader
, this, CommRead(conn
, buf
, len
, callback
)));
224 mem_obj
->mostBytesAllowed().delayRead(DeferredRead(DeferReader
, this, CommRead(conn
, buf
, len
, callback
)));
231 if (fd_table
[conn
->fd
].closing()) {
232 // Readers must have closing callbacks if they want to be notified. No
233 // readers appeared to care around 2009/12/14 as they skipped reading
234 // for other reasons. Closing may already be true at the delyaAwareRead
235 // call time or may happen while we wait after delayRead() above.
236 debugs(20, 3, HERE
<< "wont read from closing " << conn
<< " for " <<
238 return; // the read callback will never be called
241 comm_read(conn
, buf
, amountToRead
, callback
);
245 StoreEntry::bytesWanted (Range
<size_t> const aRange
, bool ignoreDelayPools
) const
250 #if URL_CHECKSUM_DEBUG
252 mem_obj
->checkUrlChecksum();
256 if (!mem_obj
->readAheadPolicyCanRead())
259 return mem_obj
->mostBytesWanted(aRange
.end
, ignoreDelayPools
);
263 StoreEntry::checkDeferRead(int) const
265 return (bytesWanted(Range
<size_t>(0,INT_MAX
)) == 0);
269 StoreEntry::setNoDelay(bool const newValue
)
272 mem_obj
->setNoDelay(newValue
);
275 // XXX: Type names mislead. STORE_DISK_CLIENT actually means that we should
276 // open swapin file, aggressively trim memory, and ignore read-ahead gap.
277 // It does not mean we will read from disk exclusively (or at all!).
278 // XXX: May create STORE_DISK_CLIENT with no disk caching configured.
279 // XXX: Collapsed clients cannot predict their type.
281 StoreEntry::storeClientType() const
283 /* The needed offset isn't in memory
284 * XXX TODO: this is wrong for range requests
285 * as the needed offset may *not* be 0, AND
286 * offset 0 in the memory object is the HTTP headers.
291 if (mem_obj
->inmem_lo
)
292 return STORE_DISK_CLIENT
;
294 if (EBIT_TEST(flags
, ENTRY_ABORTED
)) {
295 /* I don't think we should be adding clients to aborted entries */
296 debugs(20, DBG_IMPORTANT
, "storeClientType: adding to ENTRY_ABORTED entry");
297 return STORE_MEM_CLIENT
;
300 if (store_status
== STORE_OK
) {
301 /* the object has completed. */
303 if (mem_obj
->inmem_lo
== 0 && !isEmpty()) {
304 if (swap_status
== SWAPOUT_DONE
) {
305 debugs(20,7, HERE
<< mem_obj
<< " lo: " << mem_obj
->inmem_lo
<< " hi: " << mem_obj
->endOffset() << " size: " << mem_obj
->object_sz
);
306 if (mem_obj
->endOffset() == mem_obj
->object_sz
) {
307 /* hot object fully swapped in (XXX: or swapped out?) */
308 return STORE_MEM_CLIENT
;
311 /* Memory-only, or currently being swapped out */
312 return STORE_MEM_CLIENT
;
315 return STORE_DISK_CLIENT
;
318 /* here and past, entry is STORE_PENDING */
320 * If this is the first client, let it be the mem client
322 if (mem_obj
->nclients
== 1)
323 return STORE_MEM_CLIENT
;
326 * If there is no disk file to open yet, we must make this a
327 * mem client. If we can't open the swapin file before writing
328 * to the client, there is no guarantee that we will be able
329 * to open it later when we really need it.
331 if (swap_status
== SWAPOUT_NONE
)
332 return STORE_MEM_CLIENT
;
335 * otherwise, make subsequent clients read from disk so they
336 * can not delay the first, and vice-versa.
338 return STORE_DISK_CLIENT
;
341 StoreEntry::StoreEntry() :
352 mem_status(NOT_IN_MEMORY
),
353 ping_status(PING_NONE
),
354 store_status(STORE_PENDING
),
355 swap_status(SWAPOUT_NONE
),
358 debugs(20, 5, "StoreEntry constructed, this=" << this);
361 StoreEntry::~StoreEntry()
363 debugs(20, 5, "StoreEntry destructed, this=" << this);
368 StoreEntry::deferProducer(const AsyncCall::Pointer
&producer
)
370 if (!deferredProducer
)
371 deferredProducer
= producer
;
373 debugs(20, 5, HERE
<< "Deferred producer call is allready set to: " <<
374 *deferredProducer
<< ", requested call: " << *producer
);
378 StoreEntry::kickProducer()
380 if (deferredProducer
!= NULL
) {
381 ScheduleCallHere(deferredProducer
);
382 deferredProducer
= NULL
;
388 StoreEntry::destroyMemObject()
390 debugs(20, 3, HERE
<< "destroyMemObject " << mem_obj
);
392 if (MemObject
*mem
= mem_obj
) {
393 // Store::Root() is FATALly missing during shutdown
394 if (mem
->xitTable
.index
>= 0 && !shutting_down
)
395 Store::Root().transientsDisconnect(*mem
);
396 if (mem
->memCache
.index
>= 0 && !shutting_down
)
397 Store::Root().memoryDisconnect(*this);
399 setMemStatus(NOT_IN_MEMORY
);
406 destroyStoreEntry(void *data
)
408 debugs(20, 3, HERE
<< "destroyStoreEntry: destroying " << data
);
409 StoreEntry
*e
= static_cast<StoreEntry
*>(static_cast<hash_link
*>(data
));
412 if (e
== NullStoreEntry::getInstance())
415 // Store::Root() is FATALly missing during shutdown
416 if (e
->swap_filen
>= 0 && !shutting_down
) {
417 SwapDir
&sd
= dynamic_cast<SwapDir
&>(*e
->store());
421 e
->destroyMemObject();
425 assert(e
->key
== NULL
);
430 /* ----- INTERFACE BETWEEN STORAGE MANAGER AND HASH TABLE FUNCTIONS --------- */
433 StoreEntry::hashInsert(const cache_key
* someKey
)
435 debugs(20, 3, "StoreEntry::hashInsert: Inserting Entry " << *this << " key '" << storeKeyText(someKey
) << "'");
436 key
= storeKeyDup(someKey
);
437 hash_join(store_table
, this);
441 StoreEntry::hashDelete()
443 if (key
) { // some test cases do not create keys and do not hashInsert()
444 hash_remove_link(store_table
, this);
445 storeKeyFree((const cache_key
*)key
);
450 /* -------------------------------------------------------------------------- */
452 /* get rid of memory copy of the object */
454 StoreEntry::purgeMem()
459 debugs(20, 3, "StoreEntry::purgeMem: Freeing memory-copy of " << getMD5Text());
461 Store::Root().memoryUnlink(*this);
463 if (swap_status
!= SWAPOUT_DONE
)
468 StoreEntry::lock(const char *context
)
471 debugs(20, 3, context
<< " locked key " << getMD5Text() << ' ' << *this);
477 lastref
= squid_curtime
;
478 Store::Root().reference(*this);
482 StoreEntry::setReleaseFlag()
484 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
487 debugs(20, 3, "StoreEntry::setReleaseFlag: '" << getMD5Text() << "'");
489 EBIT_SET(flags
, RELEASE_REQUEST
);
491 Store::Root().markForUnlink(*this);
495 StoreEntry::releaseRequest()
497 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
500 setReleaseFlag(); // makes validToSend() false, preventing future hits
506 StoreEntry::unlock(const char *context
)
508 debugs(20, 3, (context
? context
: "somebody") <<
509 " unlocking key " << getMD5Text() << ' ' << *this);
510 assert(lock_count
> 0);
514 return (int) lock_count
;
516 if (store_status
== STORE_PENDING
)
519 assert(storePendingNClients(this) == 0);
521 if (EBIT_TEST(flags
, RELEASE_REQUEST
)) {
526 if (EBIT_TEST(flags
, KEY_PRIVATE
))
527 debugs(20, DBG_IMPORTANT
, "WARNING: " << __FILE__
<< ":" << __LINE__
<< ": found KEY_PRIVATE");
529 Store::Root().handleIdleEntry(*this); // may delete us
534 StoreEntry::getPublicByRequestMethod (StoreClient
*aClient
, HttpRequest
* request
, const HttpRequestMethod
& method
)
537 StoreEntry
*result
= storeGetPublicByRequestMethod( request
, method
);
540 aClient
->created (NullStoreEntry::getInstance());
542 aClient
->created (result
);
546 StoreEntry::getPublicByRequest (StoreClient
*aClient
, HttpRequest
* request
)
549 StoreEntry
*result
= storeGetPublicByRequest (request
);
552 result
= NullStoreEntry::getInstance();
554 aClient
->created (result
);
558 StoreEntry::getPublic (StoreClient
*aClient
, const char *uri
, const HttpRequestMethod
& method
)
561 StoreEntry
*result
= storeGetPublic (uri
, method
);
564 result
= NullStoreEntry::getInstance();
566 aClient
->created (result
);
570 storeGetPublic(const char *uri
, const HttpRequestMethod
& method
)
572 return Store::Root().get(storeKeyPublic(uri
, method
));
576 storeGetPublicByRequestMethod(HttpRequest
* req
, const HttpRequestMethod
& method
)
578 return Store::Root().get(storeKeyPublicByRequestMethod(req
, method
));
582 storeGetPublicByRequest(HttpRequest
* req
)
584 StoreEntry
*e
= storeGetPublicByRequestMethod(req
, req
->method
);
586 if (e
== NULL
&& req
->method
== Http::METHOD_HEAD
)
587 /* We can generate a HEAD reply from a cached GET object */
588 e
= storeGetPublicByRequestMethod(req
, Http::METHOD_GET
);
596 static int key_counter
= 0;
598 if (++key_counter
< 0)
604 /* RBC 20050104 AFAICT this should become simpler:
605 * rather than reinserting with a special key it should be marked
606 * as 'released' and then cleaned up when refcounting indicates.
607 * the StoreHashIndex could well implement its 'released' in the
609 * Also, clean log writing should skip over ia,t
610 * Otherwise, we need a 'remove from the index but not the store
614 StoreEntry::setPrivateKey()
616 const cache_key
*newkey
;
618 if (key
&& EBIT_TEST(flags
, KEY_PRIVATE
))
619 return; /* is already private */
622 setReleaseFlag(); // will markForUnlink(); all caches/workers will know
624 // TODO: move into SwapDir::markForUnlink() already called by Root()
626 storeDirSwapLog(this, SWAP_LOG_DEL
);
631 if (mem_obj
&& mem_obj
->hasUris()) {
632 mem_obj
->id
= getKeyCounter();
633 newkey
= storeKeyPrivate(mem_obj
->storeId(), mem_obj
->method
, mem_obj
->id
);
635 newkey
= storeKeyPrivate("JUNK", Http::METHOD_NONE
, getKeyCounter());
638 assert(hash_lookup(store_table
, newkey
) == NULL
);
639 EBIT_SET(flags
, KEY_PRIVATE
);
644 StoreEntry::setPublicKey()
646 const cache_key
*newkey
;
648 if (key
&& !EBIT_TEST(flags
, KEY_PRIVATE
))
649 return; /* is already public */
654 * We can't make RELEASE_REQUEST objects public. Depending on
655 * when RELEASE_REQUEST gets set, we might not be swapping out
656 * the object. If we're not swapping out, then subsequent
657 * store clients won't be able to access object data which has
658 * been freed from memory.
660 * If RELEASE_REQUEST is set, setPublicKey() should not be called.
662 #if MORE_DEBUG_OUTPUT
664 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
665 debugs(20, DBG_IMPORTANT
, "assertion failed: RELEASE key " << key
<< ", url " << mem_obj
->url
);
669 assert(!EBIT_TEST(flags
, RELEASE_REQUEST
));
671 if (mem_obj
->request
) {
672 HttpRequest
*request
= mem_obj
->request
;
674 if (!mem_obj
->vary_headers
) {
675 /* First handle the case where the object no longer varies */
676 safe_free(request
->vary_headers
);
678 if (request
->vary_headers
&& strcmp(request
->vary_headers
, mem_obj
->vary_headers
) != 0) {
679 /* Oops.. the variance has changed. Kill the base object
680 * to record the new variance key
682 safe_free(request
->vary_headers
); /* free old "bad" variance key */
683 if (StoreEntry
*pe
= storeGetPublic(mem_obj
->storeId(), mem_obj
->method
))
687 /* Make sure the request knows the variance status */
688 if (!request
->vary_headers
) {
689 const char *vary
= httpMakeVaryMark(request
, mem_obj
->getReply());
692 request
->vary_headers
= xstrdup(vary
);
696 // TODO: storeGetPublic() calls below may create unlocked entries.
697 // We should add/use storeHas() API or lock/unlock those entries.
698 if (mem_obj
->vary_headers
&& !storeGetPublic(mem_obj
->storeId(), mem_obj
->method
)) {
699 /* Create "vary" base object */
701 StoreEntry
*pe
= storeCreateEntry(mem_obj
->storeId(), mem_obj
->logUri(), request
->flags
, request
->method
);
702 /* We are allowed to do this typecast */
703 HttpReply
*rep
= new HttpReply
;
704 rep
->setHeaders(Http::scOkay
, "Internal marker object", "x-squid-internal/vary", -1, -1, squid_curtime
+ 100000);
705 vary
= mem_obj
->getReply()->header
.getList(Http::HdrType::VARY
);
708 /* Again, we own this structure layout */
709 rep
->header
.putStr(Http::HdrType::VARY
, vary
.termedBuf());
713 #if X_ACCELERATOR_VARY
714 vary
= mem_obj
->getReply()->header
.getList(Http::HdrType::HDR_X_ACCELERATOR_VARY
);
716 if (vary
.size() > 0) {
717 /* Again, we own this structure layout */
718 rep
->header
.putStr(Http::HdrType::HDR_X_ACCELERATOR_VARY
, vary
.termedBuf());
723 pe
->replaceHttpReply(rep
, false); // no write until key is public
729 pe
->startWriting(); // after makePublic()
733 pe
->unlock("StoreEntry::setPublicKey+Vary");
736 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
738 newkey
= storeKeyPublic(mem_obj
->storeId(), mem_obj
->method
);
740 if (StoreEntry
*e2
= (StoreEntry
*)hash_lookup(store_table
, newkey
)) {
741 debugs(20, 3, "Making old " << *e2
<< " private.");
745 if (mem_obj
->request
)
746 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
748 newkey
= storeKeyPublic(mem_obj
->storeId(), mem_obj
->method
);
754 EBIT_CLR(flags
, KEY_PRIVATE
);
759 storeDirSwapLog(this, SWAP_LOG_ADD
);
763 storeCreatePureEntry(const char *url
, const char *log_url
, const RequestFlags
&flags
, const HttpRequestMethod
& method
)
765 StoreEntry
*e
= NULL
;
766 debugs(20, 3, "storeCreateEntry: '" << url
<< "'");
768 e
= new StoreEntry();
770 e
->mem_obj
->setUris(url
, log_url
, method
);
772 if (flags
.cachable
) {
773 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
778 e
->store_status
= STORE_PENDING
;
780 e
->lastref
= squid_curtime
;
781 e
->timestamp
= -1; /* set in StoreEntry::timestampsSet() */
782 e
->ping_status
= PING_NONE
;
783 EBIT_SET(e
->flags
, ENTRY_VALIDATED
);
788 storeCreateEntry(const char *url
, const char *logUrl
, const RequestFlags
&flags
, const HttpRequestMethod
& method
)
790 StoreEntry
*e
= storeCreatePureEntry(url
, logUrl
, flags
, method
);
791 e
->lock("storeCreateEntry");
793 if (neighbors_do_private_keys
|| !flags
.hierarchical
)
801 /* Mark object as expired */
803 StoreEntry::expireNow()
805 debugs(20, 3, "StoreEntry::expireNow: '" << getMD5Text() << "'");
806 expires
= squid_curtime
;
810 StoreEntry::write (StoreIOBuffer writeBuffer
)
812 assert(mem_obj
!= NULL
);
813 /* This assert will change when we teach the store to update */
814 PROF_start(StoreEntry_write
);
815 assert(store_status
== STORE_PENDING
);
817 // XXX: caller uses content offset, but we also store headers
818 if (const HttpReply
*reply
= mem_obj
->getReply())
819 writeBuffer
.offset
+= reply
->hdr_sz
;
821 debugs(20, 5, "storeWrite: writing " << writeBuffer
.length
<< " bytes for '" << getMD5Text() << "'");
822 PROF_stop(StoreEntry_write
);
823 storeGetMemSpace(writeBuffer
.length
);
824 mem_obj
->write(writeBuffer
);
826 if (!EBIT_TEST(flags
, DELAY_SENDING
))
830 /* Append incoming data from a primary server to an entry. */
832 StoreEntry::append(char const *buf
, int len
)
834 assert(mem_obj
!= NULL
);
836 assert(store_status
== STORE_PENDING
);
838 StoreIOBuffer tempBuffer
;
839 tempBuffer
.data
= (char *)buf
;
840 tempBuffer
.length
= len
;
842 * XXX sigh, offset might be < 0 here, but it gets "corrected"
843 * later. This offset crap is such a mess.
845 tempBuffer
.offset
= mem_obj
->endOffset() - (getReply() ? getReply()->hdr_sz
: 0);
850 StoreEntry::vappendf(const char *fmt
, va_list vargs
)
852 LOCAL_ARRAY(char, buf
, 4096);
858 /* Fix of bug 753r. The value of vargs is undefined
859 * after vsnprintf() returns. Make a copy of vargs
860 * incase we loop around and call vsnprintf() again.
864 if ((x
= vsnprintf(buf
, sizeof(buf
), fmt
, ap
)) < 0) {
865 fatal(xstrerr(errno
));
871 if ((x
= vsnprintf(buf
, sizeof(buf
), fmt
, vargs
)) < 0) {
872 fatal(xstrerr(errno
));
877 if (x
< static_cast<int>(sizeof(buf
))) {
882 // okay, do it the slow way.
883 char *buf2
= new char[x
+1];
884 int y
= vsnprintf(buf2
, x
+1, fmt
, vargs
);
885 assert(y
>= 0 && y
== x
);
890 // deprecated. use StoreEntry::appendf() instead.
892 storeAppendPrintf(StoreEntry
* e
, const char *fmt
,...)
896 e
->vappendf(fmt
, args
);
900 // deprecated. use StoreEntry::appendf() instead.
902 storeAppendVPrintf(StoreEntry
* e
, const char *fmt
, va_list vargs
)
904 e
->vappendf(fmt
, vargs
);
907 struct _store_check_cachable_hist
{
911 int not_entry_cachable
;
912 int wrong_content_length
;
917 int too_many_open_files
;
918 int too_many_open_fds
;
925 } store_check_cachable_hist
;
928 storeTooManyDiskFilesOpen(void)
930 if (Config
.max_open_disk_fds
== 0)
933 if (store_open_disk_fd
> Config
.max_open_disk_fds
)
940 StoreEntry::checkTooSmall()
942 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
945 if (STORE_OK
== store_status
)
946 if (mem_obj
->object_sz
>= 0 &&
947 mem_obj
->object_sz
< Config
.Store
.minObjectSize
)
949 if (getReply()->content_length
> -1)
950 if (getReply()->content_length
< Config
.Store
.minObjectSize
)
956 StoreEntry::checkTooBig() const
958 if (mem_obj
->endOffset() > store_maxobjsize
)
961 if (getReply()->content_length
< 0)
964 return (getReply()->content_length
> store_maxobjsize
);
967 // TODO: move "too many open..." checks outside -- we are called too early/late
969 StoreEntry::checkCachable()
971 // XXX: This method is used for both memory and disk caches, but some
972 // checks are specific to disk caches. Move them to mayStartSwapOut().
974 // XXX: This method may be called several times, sometimes with different
975 // outcomes, making store_check_cachable_hist counters misleading.
977 // check this first to optimize handling of repeated calls for uncachables
978 if (EBIT_TEST(flags
, RELEASE_REQUEST
)) {
979 debugs(20, 2, "StoreEntry::checkCachable: NO: not cachable");
980 ++store_check_cachable_hist
.no
.not_entry_cachable
; // TODO: rename?
981 return 0; // avoid rerequesting release below
984 #if CACHE_ALL_METHODS
986 if (mem_obj
->method
!= Http::METHOD_GET
) {
987 debugs(20, 2, "StoreEntry::checkCachable: NO: non-GET method");
988 ++store_check_cachable_hist
.no
.non_get
;
991 if (store_status
== STORE_OK
&& EBIT_TEST(flags
, ENTRY_BAD_LENGTH
)) {
992 debugs(20, 2, "StoreEntry::checkCachable: NO: wrong content-length");
993 ++store_check_cachable_hist
.no
.wrong_content_length
;
994 } else if (EBIT_TEST(flags
, ENTRY_NEGCACHED
)) {
995 debugs(20, 3, "StoreEntry::checkCachable: NO: negative cached");
996 ++store_check_cachable_hist
.no
.negative_cached
;
997 return 0; /* avoid release call below */
998 } else if (!mem_obj
|| !getReply()) {
999 // XXX: In bug 4131, we forgetHit() without mem_obj, so we need
1000 // this segfault protection, but how can we get such a HIT?
1001 debugs(20, 2, "StoreEntry::checkCachable: NO: missing parts: " << *this);
1002 ++store_check_cachable_hist
.no
.missing_parts
;
1003 } else if (checkTooBig()) {
1004 debugs(20, 2, "StoreEntry::checkCachable: NO: too big");
1005 ++store_check_cachable_hist
.no
.too_big
;
1006 } else if (checkTooSmall()) {
1007 debugs(20, 2, "StoreEntry::checkCachable: NO: too small");
1008 ++store_check_cachable_hist
.no
.too_small
;
1009 } else if (EBIT_TEST(flags
, KEY_PRIVATE
)) {
1010 debugs(20, 3, "StoreEntry::checkCachable: NO: private key");
1011 ++store_check_cachable_hist
.no
.private_key
;
1012 } else if (swap_status
!= SWAPOUT_NONE
) {
1014 * here we checked the swap_status because the remaining
1015 * cases are only relevant only if we haven't started swapping
1016 * out the object yet.
1019 } else if (storeTooManyDiskFilesOpen()) {
1020 debugs(20, 2, "StoreEntry::checkCachable: NO: too many disk files open");
1021 ++store_check_cachable_hist
.no
.too_many_open_files
;
1022 } else if (fdNFree() < RESERVED_FD
) {
1023 debugs(20, 2, "StoreEntry::checkCachable: NO: too many FD's open");
1024 ++store_check_cachable_hist
.no
.too_many_open_fds
;
1026 ++store_check_cachable_hist
.yes
.Default
;
1035 storeCheckCachableStats(StoreEntry
*sentry
)
1037 storeAppendPrintf(sentry
, "Category\t Count\n");
1039 #if CACHE_ALL_METHODS
1041 storeAppendPrintf(sentry
, "no.non_get\t%d\n",
1042 store_check_cachable_hist
.no
.non_get
);
1045 storeAppendPrintf(sentry
, "no.not_entry_cachable\t%d\n",
1046 store_check_cachable_hist
.no
.not_entry_cachable
);
1047 storeAppendPrintf(sentry
, "no.wrong_content_length\t%d\n",
1048 store_check_cachable_hist
.no
.wrong_content_length
);
1049 storeAppendPrintf(sentry
, "no.negative_cached\t%d\n",
1050 store_check_cachable_hist
.no
.negative_cached
);
1051 storeAppendPrintf(sentry
, "no.missing_parts\t%d\n",
1052 store_check_cachable_hist
.no
.missing_parts
);
1053 storeAppendPrintf(sentry
, "no.too_big\t%d\n",
1054 store_check_cachable_hist
.no
.too_big
);
1055 storeAppendPrintf(sentry
, "no.too_small\t%d\n",
1056 store_check_cachable_hist
.no
.too_small
);
1057 storeAppendPrintf(sentry
, "no.private_key\t%d\n",
1058 store_check_cachable_hist
.no
.private_key
);
1059 storeAppendPrintf(sentry
, "no.too_many_open_files\t%d\n",
1060 store_check_cachable_hist
.no
.too_many_open_files
);
1061 storeAppendPrintf(sentry
, "no.too_many_open_fds\t%d\n",
1062 store_check_cachable_hist
.no
.too_many_open_fds
);
1063 storeAppendPrintf(sentry
, "yes.default\t%d\n",
1064 store_check_cachable_hist
.yes
.Default
);
1068 StoreEntry::lengthWentBad(const char *reason
)
1070 debugs(20, 3, "because " << reason
<< ": " << *this);
1071 EBIT_SET(flags
, ENTRY_BAD_LENGTH
);
1076 StoreEntry::complete()
1078 debugs(20, 3, "storeComplete: '" << getMD5Text() << "'");
1080 if (store_status
!= STORE_PENDING
) {
1082 * if we're not STORE_PENDING, then probably we got aborted
1083 * and there should be NO clients on this entry
1085 assert(EBIT_TEST(flags
, ENTRY_ABORTED
));
1086 assert(mem_obj
->nclients
== 0);
1090 /* This is suspect: mem obj offsets include the headers. do we adjust for that
1091 * in use of object_sz?
1093 mem_obj
->object_sz
= mem_obj
->endOffset();
1095 store_status
= STORE_OK
;
1097 assert(mem_status
== NOT_IN_MEMORY
);
1099 if (!EBIT_TEST(flags
, ENTRY_BAD_LENGTH
) && !validLength())
1100 lengthWentBad("!validLength() in complete()");
1102 #if USE_CACHE_DIGESTS
1103 if (mem_obj
->request
)
1104 mem_obj
->request
->hier
.store_complete_stop
= current_time
;
1108 * We used to call invokeHandlers, then storeSwapOut. However,
1109 * Madhukar Reddy <myreddy@persistence.com> reported that
1110 * responses without content length would sometimes get released
1111 * in client_side, thinking that the response is incomplete.
1117 * Someone wants to abort this transfer. Set the reason in the
1118 * request structure, call the callback and mark the
1119 * entry for releasing
1124 ++statCounter
.aborted_requests
;
1125 assert(store_status
== STORE_PENDING
);
1126 assert(mem_obj
!= NULL
);
1127 debugs(20, 6, "storeAbort: " << getMD5Text());
1129 lock("StoreEntry::abort"); /* lock while aborting */
1134 EBIT_SET(flags
, ENTRY_ABORTED
);
1136 setMemStatus(NOT_IN_MEMORY
);
1138 store_status
= STORE_OK
;
1140 /* Notify the server side */
1144 * Should we check abort.data for validity?
1146 if (mem_obj
->abort
.callback
) {
1147 if (!cbdataReferenceValid(mem_obj
->abort
.data
))
1148 debugs(20, DBG_IMPORTANT
,HERE
<< "queueing event when abort.data is not valid");
1149 eventAdd("mem_obj->abort.callback",
1150 mem_obj
->abort
.callback
,
1151 mem_obj
->abort
.data
,
1157 /* XXX Should we reverse these two, so that there is no
1158 * unneeded disk swapping triggered?
1160 /* Notify the client side */
1163 // abort swap out, invalidating what was created so far (release follows)
1164 swapOutFileClose(StoreIOState::writerGone
);
1166 unlock("StoreEntry::abort"); /* unlock */
1170 * Clear Memory storage to accommodate the given object len
1173 storeGetMemSpace(int size
)
1175 PROF_start(storeGetMemSpace
);
1176 StoreEntry
*e
= NULL
;
1178 static time_t last_check
= 0;
1179 size_t pages_needed
;
1180 RemovalPurgeWalker
*walker
;
1182 if (squid_curtime
== last_check
) {
1183 PROF_stop(storeGetMemSpace
);
1187 last_check
= squid_curtime
;
1189 pages_needed
= (size
+ SM_PAGE_SIZE
-1) / SM_PAGE_SIZE
;
1191 if (mem_node::InUseCount() + pages_needed
< store_pages_max
) {
1192 PROF_stop(storeGetMemSpace
);
1196 debugs(20, 2, "storeGetMemSpace: Starting, need " << pages_needed
<<
1199 /* XXX what to set as max_scan here? */
1200 walker
= mem_policy
->PurgeInit(mem_policy
, 100000);
1202 while ((e
= walker
->Next(walker
))) {
1206 if (mem_node::InUseCount() + pages_needed
< store_pages_max
)
1210 walker
->Done(walker
);
1211 debugs(20, 3, "storeGetMemSpace stats:");
1212 debugs(20, 3, " " << std::setw(6) << hot_obj_count
<< " HOT objects");
1213 debugs(20, 3, " " << std::setw(6) << released
<< " were released");
1214 PROF_stop(storeGetMemSpace
);
1217 /* thunk through to Store::Root().maintain(). Note that this would be better still
1218 * if registered against the root store itself, but that requires more complex
1219 * update logic - bigger fish to fry first. Long term each store when
1220 * it becomes active will self register
1223 Store::Maintain(void *)
1225 Store::Root().maintain();
1227 /* Reregister a maintain event .. */
1228 eventAdd("MaintainSwapSpace", Maintain
, NULL
, 1.0, 1);
1232 /* The maximum objects to scan for maintain storage space */
1233 #define MAINTAIN_MAX_SCAN 1024
1234 #define MAINTAIN_MAX_REMOVE 64
1237 * This routine is to be called by main loop in main.c.
1238 * It removes expired objects on only one bucket for each time called.
1240 * This should get called 1/s from main().
1243 StoreController::maintain()
1245 static time_t last_warn_time
= 0;
1247 PROF_start(storeMaintainSwapSpace
);
1248 swapDir
->maintain();
1250 /* this should be emitted by the oversize dir, not globally */
1252 if (Store::Root().currentSize() > Store::Root().maxSize()) {
1253 if (squid_curtime
- last_warn_time
> 10) {
1254 debugs(20, DBG_CRITICAL
, "WARNING: Disk space over limit: "
1255 << Store::Root().currentSize() / 1024.0 << " KB > "
1256 << (Store::Root().maxSize() >> 10) << " KB");
1257 last_warn_time
= squid_curtime
;
1261 PROF_stop(storeMaintainSwapSpace
);
1264 /* release an object from a cache */
1266 StoreEntry::release()
1268 PROF_start(storeRelease
);
1269 debugs(20, 3, "releasing " << *this << ' ' << getMD5Text());
1270 /* If, for any reason we can't discard this object because of an
1271 * outstanding request, mark it for pending release */
1275 debugs(20, 3, "storeRelease: Only setting RELEASE_REQUEST bit");
1277 PROF_stop(storeRelease
);
1281 Store::Root().memoryUnlink(*this);
1283 if (StoreController::store_dirs_rebuilding
&& swap_filen
> -1) {
1286 if (swap_filen
> -1) {
1287 // lock the entry until rebuilding is done
1288 lock("storeLateRelease");
1290 LateReleaseStack
.push(this);
1292 destroyStoreEntry(static_cast<hash_link
*>(this));
1293 // "this" is no longer valid
1296 PROF_stop(storeRelease
);
1300 storeLog(STORE_LOG_RELEASE
, this);
1302 if (swap_filen
> -1) {
1303 // log before unlink() below clears swap_filen
1304 if (!EBIT_TEST(flags
, KEY_PRIVATE
))
1305 storeDirSwapLog(this, SWAP_LOG_DEL
);
1310 destroyStoreEntry(static_cast<hash_link
*>(this));
1311 PROF_stop(storeRelease
);
1315 storeLateRelease(void *)
1320 if (StoreController::store_dirs_rebuilding
) {
1321 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
1325 // TODO: this works but looks unelegant.
1326 for (int i
= 0; i
< 10; ++i
) {
1327 if (LateReleaseStack
.empty()) {
1328 debugs(20, DBG_IMPORTANT
, "storeLateRelease: released " << n
<< " objects");
1331 e
= LateReleaseStack
.top();
1332 LateReleaseStack
.pop();
1335 e
->unlock("storeLateRelease");
1339 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 0.0, 1);
1342 /* return 1 if a store entry is locked */
1344 StoreEntry::locked() const
1350 * SPECIAL, PUBLIC entries should be "locked";
1351 * XXX: Their owner should lock them then instead of relying on this hack.
1353 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
1354 if (!EBIT_TEST(flags
, KEY_PRIVATE
))
1361 StoreEntry::validLength() const
1364 const HttpReply
*reply
;
1365 assert(mem_obj
!= NULL
);
1367 debugs(20, 3, "storeEntryValidLength: Checking '" << getMD5Text() << "'");
1368 debugs(20, 5, "storeEntryValidLength: object_len = " <<
1370 debugs(20, 5, "storeEntryValidLength: hdr_sz = " << reply
->hdr_sz
);
1371 debugs(20, 5, "storeEntryValidLength: content_length = " << reply
->content_length
);
1373 if (reply
->content_length
< 0) {
1374 debugs(20, 5, "storeEntryValidLength: Unspecified content length: " << getMD5Text());
1378 if (reply
->hdr_sz
== 0) {
1379 debugs(20, 5, "storeEntryValidLength: Zero header size: " << getMD5Text());
1383 if (mem_obj
->method
== Http::METHOD_HEAD
) {
1384 debugs(20, 5, "storeEntryValidLength: HEAD request: " << getMD5Text());
1388 if (reply
->sline
.status() == Http::scNotModified
)
1391 if (reply
->sline
.status() == Http::scNoContent
)
1394 diff
= reply
->hdr_sz
+ reply
->content_length
- objectLen();
1399 debugs(20, 3, "storeEntryValidLength: " << (diff
< 0 ? -diff
: diff
) << " bytes too " << (diff
< 0 ? "big" : "small") <<"; '" << getMD5Text() << "'" );
1405 storeRegisterWithCacheManager(void)
1407 Mgr::RegisterAction("storedir", "Store Directory Stats", Store::Stats
, 0, 1);
1408 Mgr::RegisterAction("store_io", "Store IO Interface Stats", &Mgr::StoreIoAction::Create
, 0, 1);
1409 Mgr::RegisterAction("store_check_cachable_stats", "storeCheckCachable() Stats",
1410 storeCheckCachableStats
, 0, 1);
1417 mem_policy
= createRemovalPolicy(Config
.memPolicy
);
1420 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
1421 Store::Root().init();
1422 storeRebuildStart();
1424 storeRegisterWithCacheManager();
1427 /// computes maximum size of a cachable object
1428 /// larger objects are rejected by all (disk and memory) cache stores
1430 storeCalcMaxObjSize()
1432 int64_t ms
= 0; // nothing can be cached without at least one store consent
1434 // global maximum is at least the disk store maximum
1435 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1436 assert (Config
.cacheSwap
.swapDirs
[i
].getRaw());
1437 const int64_t storeMax
= dynamic_cast<SwapDir
*>(Config
.cacheSwap
.swapDirs
[i
].getRaw())->maxObjectSize();
1442 // global maximum is at least the memory store maximum
1443 // TODO: move this into a memory cache class when we have one
1444 const int64_t memMax
= static_cast<int64_t>(min(Config
.Store
.maxInMemObjSize
, Config
.memMaxSize
));
1452 storeConfigure(void)
1454 store_swap_high
= (long) (((float) Store::Root().maxSize() *
1455 (float) Config
.Swap
.highWaterMark
) / (float) 100);
1456 store_swap_low
= (long) (((float) Store::Root().maxSize() *
1457 (float) Config
.Swap
.lowWaterMark
) / (float) 100);
1458 store_pages_max
= Config
.memMaxSize
/ sizeof(mem_node
);
1460 store_maxobjsize
= storeCalcMaxObjSize();
1464 StoreEntry::memoryCachable()
1466 if (!checkCachable())
1469 if (mem_obj
== NULL
)
1472 if (mem_obj
->data_hdr
.size() == 0)
1475 if (mem_obj
->inmem_lo
!= 0)
1478 if (!Config
.onoff
.memory_cache_first
&& swap_status
== SWAPOUT_DONE
&& refcount
== 1)
1485 StoreEntry::checkNegativeHit() const
1487 if (!EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1490 if (expires
<= squid_curtime
)
1493 if (store_status
!= STORE_OK
)
1500 * Set object for negative caching.
1501 * Preserves any expiry information given by the server.
1502 * In absence of proper expiry info it will set to expire immediately,
1503 * or with HTTP-violations enabled the configured negative-TTL is observed
1506 StoreEntry::negativeCache()
1508 // XXX: should make the default for expires 0 instead of -1
1509 // so we can distinguish "Expires: -1" from nothing.
1511 #if USE_HTTP_VIOLATIONS
1512 expires
= squid_curtime
+ Config
.negativeTtl
;
1514 expires
= squid_curtime
;
1516 EBIT_SET(flags
, ENTRY_NEGCACHED
);
1520 storeFreeMemory(void)
1523 #if USE_CACHE_DIGESTS
1524 delete store_digest
;
1526 store_digest
= NULL
;
1530 expiresMoreThan(time_t expires
, time_t when
)
1532 if (expires
< 0) /* No Expires given */
1535 return (expires
> (squid_curtime
+ when
));
1539 StoreEntry::validToSend() const
1541 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
1544 if (EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1545 if (expires
<= squid_curtime
)
1548 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
1551 // now check that the entry has a cache backing or is collapsed
1552 if (swap_filen
> -1) // backed by a disk cache
1555 if (swappingOut()) // will be backed by a disk cache
1558 if (!mem_obj
) // not backed by a memory cache and not collapsed
1561 // StoreEntry::storeClientType() assumes DISK_CLIENT here, but there is no
1562 // disk cache backing that store_client constructor will assert. XXX: This
1563 // is wrong for range requests (that could feed off nibbled memory) and for
1564 // entries backed by the shared memory cache (that could, in theory, get
1565 // nibbled bytes from that cache, but there is no such "memoryIn" code).
1566 if (mem_obj
->inmem_lo
) // in memory cache, but got nibbled at
1569 // The following check is correct but useless at this position. TODO: Move
1570 // it up when the shared memory cache can either replenish locally nibbled
1571 // bytes or, better, does not use local RAM copy at all.
1572 // if (mem_obj->memCache.index >= 0) // backed by a shared memory cache
1579 StoreEntry::timestampsSet()
1581 const HttpReply
*reply
= getReply();
1582 time_t served_date
= reply
->date
;
1583 int age
= reply
->header
.getInt(Http::HdrType::AGE
);
1584 /* Compute the timestamp, mimicking RFC2616 section 13.2.3. */
1585 /* make sure that 0 <= served_date <= squid_curtime */
1587 if (served_date
< 0 || served_date
> squid_curtime
)
1588 served_date
= squid_curtime
;
1591 * If the returned Date: is more than 24 hours older than
1592 * the squid_curtime, then one of us needs to use NTP to set our
1593 * clock. We'll pretend that our clock is right.
1595 else if (served_date
< (squid_curtime
- 24 * 60 * 60) )
1596 served_date
= squid_curtime
;
1599 * Compensate with Age header if origin server clock is ahead
1600 * of us and there is a cache in between us and the origin
1601 * server. But DONT compensate if the age value is larger than
1602 * squid_curtime because it results in a negative served_date.
1604 if (age
> squid_curtime
- served_date
)
1605 if (squid_curtime
> age
)
1606 served_date
= squid_curtime
- age
;
1608 // compensate for Squid-to-server and server-to-Squid delays
1609 if (mem_obj
&& mem_obj
->request
) {
1610 const time_t request_sent
=
1611 mem_obj
->request
->hier
.peer_http_request_sent
.tv_sec
;
1612 if (0 < request_sent
&& request_sent
< squid_curtime
)
1613 served_date
-= (squid_curtime
- request_sent
);
1616 if (reply
->expires
> 0 && reply
->date
> -1)
1617 expires
= served_date
+ (reply
->expires
- reply
->date
);
1619 expires
= reply
->expires
;
1621 lastmod
= reply
->last_modified
;
1623 timestamp
= served_date
;
1627 StoreEntry::registerAbort(STABH
* cb
, void *data
)
1630 assert(mem_obj
->abort
.callback
== NULL
);
1631 mem_obj
->abort
.callback
= cb
;
1632 mem_obj
->abort
.data
= cbdataReference(data
);
1636 StoreEntry::unregisterAbort()
1639 if (mem_obj
->abort
.callback
) {
1640 mem_obj
->abort
.callback
= NULL
;
1641 cbdataReferenceDone(mem_obj
->abort
.data
);
1646 StoreEntry::dump(int l
) const
1648 debugs(20, l
, "StoreEntry->key: " << getMD5Text());
1649 debugs(20, l
, "StoreEntry->next: " << next
);
1650 debugs(20, l
, "StoreEntry->mem_obj: " << mem_obj
);
1651 debugs(20, l
, "StoreEntry->timestamp: " << timestamp
);
1652 debugs(20, l
, "StoreEntry->lastref: " << lastref
);
1653 debugs(20, l
, "StoreEntry->expires: " << expires
);
1654 debugs(20, l
, "StoreEntry->lastmod: " << lastmod
);
1655 debugs(20, l
, "StoreEntry->swap_file_sz: " << swap_file_sz
);
1656 debugs(20, l
, "StoreEntry->refcount: " << refcount
);
1657 debugs(20, l
, "StoreEntry->flags: " << storeEntryFlags(this));
1658 debugs(20, l
, "StoreEntry->swap_dirn: " << swap_dirn
);
1659 debugs(20, l
, "StoreEntry->swap_filen: " << swap_filen
);
1660 debugs(20, l
, "StoreEntry->lock_count: " << lock_count
);
1661 debugs(20, l
, "StoreEntry->mem_status: " << mem_status
);
1662 debugs(20, l
, "StoreEntry->ping_status: " << ping_status
);
1663 debugs(20, l
, "StoreEntry->store_status: " << store_status
);
1664 debugs(20, l
, "StoreEntry->swap_status: " << swap_status
);
1668 * NOTE, this function assumes only two mem states
1671 StoreEntry::setMemStatus(mem_status_t new_status
)
1673 if (new_status
== mem_status
)
1676 // are we using a shared memory cache?
1677 if (Config
.memShared
&& IamWorkerProcess()) {
1678 // This method was designed to update replacement policy, not to
1679 // actually purge something from the memory cache (TODO: rename?).
1680 // Shared memory cache does not have a policy that needs updates.
1681 mem_status
= new_status
;
1685 assert(mem_obj
!= NULL
);
1687 if (new_status
== IN_MEMORY
) {
1688 assert(mem_obj
->inmem_lo
== 0);
1690 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1691 debugs(20, 4, "not inserting special " << *this << " into policy");
1693 mem_policy
->Add(mem_policy
, this, &mem_obj
->repl
);
1694 debugs(20, 4, "inserted " << *this << " key: " << getMD5Text());
1697 ++hot_obj_count
; // TODO: maintain for the shared hot cache as well
1699 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1700 debugs(20, 4, "not removing special " << *this << " from policy");
1702 mem_policy
->Remove(mem_policy
, this, &mem_obj
->repl
);
1703 debugs(20, 4, "removed " << *this);
1709 mem_status
= new_status
;
1713 StoreEntry::url() const
1715 if (mem_obj
== NULL
)
1716 return "[null_mem_obj]";
1718 return mem_obj
->storeId();
1722 StoreEntry::makeMemObject()
1725 mem_obj
= new MemObject();
1730 StoreEntry::createMemObject(const char *aUrl
, const char *aLogUrl
, const HttpRequestMethod
&aMethod
)
1733 mem_obj
->setUris(aUrl
, aLogUrl
, aMethod
);
1736 /** disable sending content to the clients.
1738 * This just sets DELAY_SENDING.
1741 StoreEntry::buffer()
1743 EBIT_SET(flags
, DELAY_SENDING
);
1746 /** flush any buffered content.
1748 * This just clears DELAY_SENDING and Invokes the handlers
1749 * to begin sending anything that may be buffered.
1754 if (EBIT_TEST(flags
, DELAY_SENDING
)) {
1755 EBIT_CLR(flags
, DELAY_SENDING
);
1761 StoreEntry::objectLen() const
1763 assert(mem_obj
!= NULL
);
1764 return mem_obj
->object_sz
;
1768 StoreEntry::contentLen() const
1770 assert(mem_obj
!= NULL
);
1771 assert(getReply() != NULL
);
1772 return objectLen() - getReply()->hdr_sz
;
1776 StoreEntry::getReply () const
1778 if (NULL
== mem_obj
)
1781 return mem_obj
->getReply();
1788 debugs(20, 3, "StoreEntry::reset: " << url());
1790 HttpReply
*rep
= (HttpReply
*) getReply(); // bypass const
1792 expires
= lastmod
= timestamp
= -1;
1798 * This routine calls the SETUP routine for each fs type.
1799 * I don't know where the best place for this is, and I'm not going to shuffle
1800 * around large chunks of code right now (that can be done once its working.)
1809 * called to add another store removal policy module
1812 storeReplAdd(const char *type
, REMOVALPOLICYCREATE
* create
)
1816 /* find the number of currently known repl types */
1817 for (i
= 0; storerepl_list
&& storerepl_list
[i
].typestr
; ++i
) {
1818 if (strcmp(storerepl_list
[i
].typestr
, type
) == 0) {
1819 debugs(20, DBG_IMPORTANT
, "WARNING: Trying to load store replacement policy " << type
<< " twice.");
1824 /* add the new type */
1825 storerepl_list
= static_cast<storerepl_entry_t
*>(xrealloc(storerepl_list
, (i
+ 2) * sizeof(storerepl_entry_t
)));
1827 memset(&storerepl_list
[i
+ 1], 0, sizeof(storerepl_entry_t
));
1829 storerepl_list
[i
].typestr
= type
;
1831 storerepl_list
[i
].create
= create
;
1835 * Create a removal policy instance
1838 createRemovalPolicy(RemovalPolicySettings
* settings
)
1840 storerepl_entry_t
*r
;
1842 for (r
= storerepl_list
; r
&& r
->typestr
; ++r
) {
1843 if (strcmp(r
->typestr
, settings
->type
) == 0)
1844 return r
->create(settings
->args
);
1847 debugs(20, DBG_IMPORTANT
, "ERROR: Unknown policy " << settings
->type
);
1848 debugs(20, DBG_IMPORTANT
, "ERROR: Be sure to have set cache_replacement_policy");
1849 debugs(20, DBG_IMPORTANT
, "ERROR: and memory_replacement_policy in squid.conf!");
1850 fatalf("ERROR: Unknown policy %s\n", settings
->type
);
1851 return NULL
; /* NOTREACHED */
1856 storeSwapFileNumberSet(StoreEntry
* e
, sfileno filn
)
1858 if (e
->swap_file_number
== filn
)
1863 storeDirMapBitReset(e
->swap_file_number
);
1864 storeDirLRUDelete(e
);
1865 e
->swap_file_number
= -1;
1867 assert(-1 == e
->swap_file_number
);
1868 storeDirMapBitSet(e
->swap_file_number
= filn
);
1876 StoreEntry::storeErrorResponse(HttpReply
*reply
)
1878 lock("StoreEntry::storeErrorResponse");
1880 replaceHttpReply(reply
);
1885 unlock("StoreEntry::storeErrorResponse");
1889 * Replace a store entry with
1890 * a new reply. This eats the reply.
1893 StoreEntry::replaceHttpReply(HttpReply
*rep
, bool andStartWriting
)
1895 debugs(20, 3, "StoreEntry::replaceHttpReply: " << url());
1898 debugs(20, DBG_CRITICAL
, "Attempt to replace object with no in-memory representation");
1902 mem_obj
->replaceHttpReply(rep
);
1904 if (andStartWriting
)
1909 StoreEntry::startWriting()
1911 /* TODO: when we store headers separately remove the header portion */
1912 /* TODO: mark the length of the headers ? */
1913 /* We ONLY want the headers */
1918 const HttpReply
*rep
= getReply();
1922 rep
->packHeadersInto(this);
1923 mem_obj
->markEndOfReplyHeaders();
1924 EBIT_CLR(flags
, ENTRY_FWD_HDR_WAIT
);
1926 rep
->body
.packInto(this);
1931 StoreEntry::getSerialisedMetaData()
1933 StoreMeta
*tlv_list
= storeSwapMetaBuild(this);
1935 char *result
= storeSwapMetaPack(tlv_list
, &swap_hdr_sz
);
1936 storeSwapTLVFree(tlv_list
);
1937 assert (swap_hdr_sz
>= 0);
1938 mem_obj
->swap_hdr_sz
= (size_t) swap_hdr_sz
;
1943 * Abandon the transient entry our worker has created if neither the shared
1944 * memory cache nor the disk cache wants to store it. Collapsed requests, if
1945 * any, should notice and use Plan B instead of getting stuck waiting for us
1946 * to start swapping the entry out.
1949 StoreEntry::transientsAbandonmentCheck()
1951 if (mem_obj
&& !mem_obj
->smpCollapsed
&& // this worker is responsible
1952 mem_obj
->xitTable
.index
>= 0 && // other workers may be interested
1953 mem_obj
->memCache
.index
< 0 && // rejected by the shared memory cache
1954 mem_obj
->swapout
.decision
== MemObject::SwapOut::swImpossible
) {
1955 debugs(20, 7, "cannot be shared: " << *this);
1956 if (!shutting_down
) // Store::Root() is FATALly missing during shutdown
1957 Store::Root().transientsAbandon(*this);
1962 StoreEntry::memOutDecision(const bool)
1964 transientsAbandonmentCheck();
1968 StoreEntry::swapOutDecision(const MemObject::SwapOut::Decision
&decision
)
1970 // Abandon our transient entry if neither shared memory nor disk wants it.
1972 mem_obj
->swapout
.decision
= decision
;
1973 transientsAbandonmentCheck();
1977 StoreEntry::trimMemory(const bool preserveSwappable
)
1981 * Bug #1943. We must not let go any data for IN_MEMORY
1982 * objects. We have to wait until the mem_status changes.
1984 if (mem_status
== IN_MEMORY
)
1987 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
1988 return; // cannot trim because we do not load them again
1990 if (preserveSwappable
)
1991 mem_obj
->trimSwappable();
1993 mem_obj
->trimUnSwappable();
1995 debugs(88, 7, *this << " inmem_lo=" << mem_obj
->inmem_lo
);
1999 StoreEntry::modifiedSince(HttpRequest
* request
) const
2002 time_t mod_time
= lastmod
;
2005 mod_time
= timestamp
;
2007 debugs(88, 3, "modifiedSince: '" << url() << "'");
2009 debugs(88, 3, "modifiedSince: mod_time = " << mod_time
);
2014 /* Find size of the object */
2015 object_length
= getReply()->content_length
;
2017 if (object_length
< 0)
2018 object_length
= contentLen();
2020 if (mod_time
> request
->ims
) {
2021 debugs(88, 3, "--> YES: entry newer than client");
2023 } else if (mod_time
< request
->ims
) {
2024 debugs(88, 3, "--> NO: entry older than client");
2026 } else if (request
->imslen
< 0) {
2027 debugs(88, 3, "--> NO: same LMT, no client length");
2029 } else if (request
->imslen
== object_length
) {
2030 debugs(88, 3, "--> NO: same LMT, same length");
2033 debugs(88, 3, "--> YES: same LMT, different length");
2039 StoreEntry::hasEtag(ETag
&etag
) const
2041 if (const HttpReply
*reply
= getReply()) {
2042 etag
= reply
->header
.getETag(Http::HdrType::ETAG
);
2050 StoreEntry::hasIfMatchEtag(const HttpRequest
&request
) const
2052 const String reqETags
= request
.header
.getList(Http::HdrType::IF_MATCH
);
2053 return hasOneOfEtags(reqETags
, false);
2057 StoreEntry::hasIfNoneMatchEtag(const HttpRequest
&request
) const
2059 const String reqETags
= request
.header
.getList(Http::HdrType::IF_NONE_MATCH
);
2060 // weak comparison is allowed only for HEAD or full-body GET requests
2061 const bool allowWeakMatch
= !request
.flags
.isRanged
&&
2062 (request
.method
== Http::METHOD_GET
|| request
.method
== Http::METHOD_HEAD
);
2063 return hasOneOfEtags(reqETags
, allowWeakMatch
);
2066 /// whether at least one of the request ETags matches entity ETag
2068 StoreEntry::hasOneOfEtags(const String
&reqETags
, const bool allowWeakMatch
) const
2070 const ETag repETag
= getReply()->header
.getETag(Http::HdrType::ETAG
);
2072 return strListIsMember(&reqETags
, "*", ',');
2074 bool matched
= false;
2075 const char *pos
= NULL
;
2078 while (!matched
&& strListGetItem(&reqETags
, ',', &item
, &ilen
, &pos
)) {
2079 if (!strncmp(item
, "*", ilen
))
2083 str
.append(item
, ilen
);
2085 if (etagParseInit(&reqETag
, str
.termedBuf())) {
2086 matched
= allowWeakMatch
? etagIsWeakEqual(repETag
, reqETag
) :
2087 etagIsStrongEqual(repETag
, reqETag
);
2095 StoreEntry::store() const
2097 assert(0 <= swap_dirn
&& swap_dirn
< Config
.cacheSwap
.n_configured
);
2098 return INDEXSD(swap_dirn
);
2102 StoreEntry::unlink()
2104 store()->unlink(*this); // implies disconnect()
2107 swap_status
= SWAPOUT_NONE
;
2111 * return true if the entry is in a state where
2112 * it can accept more data (ie with write() method)
2115 StoreEntry::isAccepting() const
2117 if (STORE_PENDING
!= store_status
)
2120 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
2126 std::ostream
&operator <<(std::ostream
&os
, const StoreEntry
&e
)
2131 if (e
.mem_obj
->xitTable
.index
> -1)
2132 os
<< 't' << e
.mem_obj
->xitTable
.index
;
2133 if (e
.mem_obj
->memCache
.index
> -1)
2134 os
<< 'm' << e
.mem_obj
->memCache
.index
;
2136 if (e
.swap_filen
> -1 || e
.swap_dirn
> -1)
2137 os
<< 'd' << e
.swap_filen
<< '@' << e
.swap_dirn
;
2141 // print only non-default status values, using unique letters
2142 if (e
.mem_status
!= NOT_IN_MEMORY
||
2143 e
.store_status
!= STORE_PENDING
||
2144 e
.swap_status
!= SWAPOUT_NONE
||
2145 e
.ping_status
!= PING_NONE
) {
2146 if (e
.mem_status
!= NOT_IN_MEMORY
) os
<< 'm';
2147 if (e
.store_status
!= STORE_PENDING
) os
<< 's';
2148 if (e
.swap_status
!= SWAPOUT_NONE
) os
<< 'w' << e
.swap_status
;
2149 if (e
.ping_status
!= PING_NONE
) os
<< 'p' << e
.ping_status
;
2152 // print only set flags, using unique letters
2154 if (EBIT_TEST(e
.flags
, ENTRY_SPECIAL
)) os
<< 'S';
2155 if (EBIT_TEST(e
.flags
, ENTRY_REVALIDATE
)) os
<< 'R';
2156 if (EBIT_TEST(e
.flags
, DELAY_SENDING
)) os
<< 'P';
2157 if (EBIT_TEST(e
.flags
, RELEASE_REQUEST
)) os
<< 'X';
2158 if (EBIT_TEST(e
.flags
, REFRESH_REQUEST
)) os
<< 'F';
2159 if (EBIT_TEST(e
.flags
, ENTRY_DISPATCHED
)) os
<< 'D';
2160 if (EBIT_TEST(e
.flags
, KEY_PRIVATE
)) os
<< 'I';
2161 if (EBIT_TEST(e
.flags
, ENTRY_FWD_HDR_WAIT
)) os
<< 'W';
2162 if (EBIT_TEST(e
.flags
, ENTRY_NEGCACHED
)) os
<< 'N';
2163 if (EBIT_TEST(e
.flags
, ENTRY_VALIDATED
)) os
<< 'V';
2164 if (EBIT_TEST(e
.flags
, ENTRY_BAD_LENGTH
)) os
<< 'L';
2165 if (EBIT_TEST(e
.flags
, ENTRY_ABORTED
)) os
<< 'A';
2168 if (e
.mem_obj
&& e
.mem_obj
->smpCollapsed
)
2171 return os
<< '/' << &e
<< '*' << e
.locks();
2174 /* NullStoreEntry */
2176 NullStoreEntry
NullStoreEntry::_instance
;
2179 NullStoreEntry::getInstance()
2185 NullStoreEntry::getMD5Text() const
2191 NullStoreEntry::operator delete(void*)
2193 fatal ("Attempt to delete NullStoreEntry\n");
2197 NullStoreEntry::getSerialisedMetaData()