3 * DEBUG: section 20 Storage Manager
4 * AUTHOR: Harvest Derived
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 #include "CacheDigest.h"
36 #include "CacheManager.h"
37 #include "comm/Connection.h"
43 #include "HttpReply.h"
44 #include "HttpRequest.h"
46 #include "MemObject.h"
47 #include "mgr/Registration.h"
48 #include "mgr/StoreIoAction.h"
49 #include "profiler/Profiler.h"
50 #include "repl_modules.h"
51 #include "RequestFlags.h"
52 #include "SquidConfig.h"
53 #include "SquidTime.h"
55 #include "StatCounters.h"
57 #include "store_digest.h"
58 #include "store_key_md5.h"
59 #include "store_key_md5.h"
60 #include "store_log.h"
61 #include "store_rebuild.h"
63 #include "StoreClient.h"
64 #include "StoreIOState.h"
65 #include "StoreMeta.h"
67 #include "swap_log_op.h"
71 #include "DelayPools.h"
77 static STMCB storeWriteComplete
;
79 #define REBUILD_TIMESTAMP_DELTA_MAX 2
81 #define STORE_IN_MEM_BUCKETS (229)
83 /** \todo Convert these string constants to enum string-arrays generated */
85 const char *memStatusStr
[] = {
90 const char *pingStatusStr
[] = {
96 const char *storeStatusStr
[] = {
101 const char *swapStatusStr
[] = {
108 * This defines an repl type
111 typedef struct _storerepl_entry storerepl_entry_t
;
113 struct _storerepl_entry
{
115 REMOVALPOLICYCREATE
*create
;
118 static storerepl_entry_t
*storerepl_list
= NULL
;
121 * local function prototypes
123 static int getKeyCounter(void);
124 static OBJH storeCheckCachableStats
;
125 static EVH storeLateRelease
;
130 static Stack
<StoreEntry
*> LateReleaseStack
;
131 MemAllocator
*StoreEntry::pool
= NULL
;
133 StorePointer
Store::CurrentRoot
= NULL
;
136 Store::Root(Store
* aRoot
)
142 Store::Root(StorePointer aRoot
)
144 Root(aRoot
.getRaw());
148 Store::Stats(StoreEntry
* output
)
151 Root().stat(*output
);
167 Store::unlink (StoreEntry
&anEntry
)
169 fatal("Store::unlink on invalid Store\n");
173 StoreEntry::operator new (size_t bytecount
)
175 assert (bytecount
== sizeof (StoreEntry
));
178 pool
= memPoolCreate ("StoreEntry", bytecount
);
179 pool
->setChunkSize(2048 * 1024);
182 return pool
->alloc();
186 StoreEntry::operator delete (void *address
)
188 pool
->freeOne(address
);
192 StoreEntry::makePublic()
194 /* This object can be cached for a long time */
196 if (EBIT_TEST(flags
, ENTRY_CACHABLE
))
201 StoreEntry::makePrivate()
203 /* This object should never be cached at all */
205 releaseRequest(); /* delete object when not used */
206 /* releaseRequest clears ENTRY_CACHABLE flag */
210 StoreEntry::cacheNegatively()
212 /* This object may be negatively cached */
215 if (EBIT_TEST(flags
, ENTRY_CACHABLE
))
220 StoreEntry::inUseCount()
224 return pool
->getInUseCount();
228 StoreEntry::getMD5Text() const
230 return storeKeyText((const cache_key
*)key
);
236 StoreEntry::DeferReader(void *theContext
, CommRead
const &aRead
)
238 StoreEntry
*anEntry
= (StoreEntry
*)theContext
;
239 anEntry
->delayAwareRead(aRead
.conn
,
246 StoreEntry::delayAwareRead(const Comm::ConnectionPointer
&conn
, char *buf
, int len
, AsyncCall::Pointer callback
)
248 size_t amountToRead
= bytesWanted(Range
<size_t>(0, len
));
249 /* sketch: readdeferer* = getdeferer.
250 * ->deferRead (fd, buf, len, callback, DelayAwareRead, this)
253 if (amountToRead
== 0) {
255 /* read ahead limit */
256 /* Perhaps these two calls should both live in MemObject */
258 if (!mem_obj
->readAheadPolicyCanRead()) {
260 mem_obj
->delayRead(DeferredRead(DeferReader
, this, CommRead(conn
, buf
, len
, callback
)));
266 mem_obj
->mostBytesAllowed().delayRead(DeferredRead(DeferReader
, this, CommRead(conn
, buf
, len
, callback
)));
273 if (fd_table
[conn
->fd
].closing()) {
274 // Readers must have closing callbacks if they want to be notified. No
275 // readers appeared to care around 2009/12/14 as they skipped reading
276 // for other reasons. Closing may already be true at the delyaAwareRead
277 // call time or may happen while we wait after delayRead() above.
278 debugs(20, 3, HERE
<< "wont read from closing " << conn
<< " for " <<
280 return; // the read callback will never be called
283 comm_read(conn
, buf
, amountToRead
, callback
);
287 StoreEntry::bytesWanted (Range
<size_t> const aRange
, bool ignoreDelayPools
) const
292 #if URL_CHECKSUM_DEBUG
294 mem_obj
->checkUrlChecksum();
298 if (!mem_obj
->readAheadPolicyCanRead())
301 return mem_obj
->mostBytesWanted(aRange
.end
, ignoreDelayPools
);
305 StoreEntry::checkDeferRead(int fd
) const
307 return (bytesWanted(Range
<size_t>(0,INT_MAX
)) == 0);
311 StoreEntry::setNoDelay (bool const newValue
)
314 mem_obj
->setNoDelay(newValue
);
318 StoreEntry::storeClientType() const
320 /* The needed offset isn't in memory
321 * XXX TODO: this is wrong for range requests
322 * as the needed offset may *not* be 0, AND
323 * offset 0 in the memory object is the HTTP headers.
326 if (mem_status
== IN_MEMORY
&& Config
.memShared
&& IamWorkerProcess()) {
327 // clients of an object cached in shared memory are memory clients
328 return STORE_MEM_CLIENT
;
333 if (mem_obj
->inmem_lo
)
334 return STORE_DISK_CLIENT
;
336 if (EBIT_TEST(flags
, ENTRY_ABORTED
)) {
337 /* I don't think we should be adding clients to aborted entries */
338 debugs(20, DBG_IMPORTANT
, "storeClientType: adding to ENTRY_ABORTED entry");
339 return STORE_MEM_CLIENT
;
342 if (store_status
== STORE_OK
) {
343 /* the object has completed. */
345 if (mem_obj
->inmem_lo
== 0 && !isEmpty()) {
346 if (swap_status
== SWAPOUT_DONE
) {
347 debugs(20,7, HERE
<< mem_obj
<< " lo: " << mem_obj
->inmem_lo
<< " hi: " << mem_obj
->endOffset() << " size: " << mem_obj
->object_sz
);
348 if (mem_obj
->endOffset() == mem_obj
->object_sz
) {
349 /* hot object fully swapped in */
350 return STORE_MEM_CLIENT
;
353 /* Memory-only, or currently being swapped out */
354 return STORE_MEM_CLIENT
;
357 return STORE_DISK_CLIENT
;
360 /* here and past, entry is STORE_PENDING */
362 * If this is the first client, let it be the mem client
364 if (mem_obj
->nclients
== 1)
365 return STORE_MEM_CLIENT
;
368 * If there is no disk file to open yet, we must make this a
369 * mem client. If we can't open the swapin file before writing
370 * to the client, there is no guarantee that we will be able
371 * to open it later when we really need it.
373 if (swap_status
== SWAPOUT_NONE
)
374 return STORE_MEM_CLIENT
;
377 * otherwise, make subsequent clients read from disk so they
378 * can not delay the first, and vice-versa.
380 return STORE_DISK_CLIENT
;
383 StoreEntry::StoreEntry():
384 hidden_mem_obj(NULL
),
387 debugs(20, 3, HERE
<< "new StoreEntry " << this);
390 expires
= lastmod
= lastref
= timestamp
= -1;
392 swap_status
= SWAPOUT_NONE
;
397 StoreEntry::StoreEntry(const char *aUrl
, const char *aLogUrl
):
398 hidden_mem_obj(NULL
),
401 debugs(20, 3, HERE
<< "new StoreEntry " << this);
402 mem_obj
= new MemObject(aUrl
, aLogUrl
);
404 expires
= lastmod
= lastref
= timestamp
= -1;
406 swap_status
= SWAPOUT_NONE
;
411 StoreEntry::~StoreEntry()
413 if (swap_filen
>= 0) {
414 SwapDir
&sd
= dynamic_cast<SwapDir
&>(*store());
415 sd
.disconnect(*this);
417 delete hidden_mem_obj
;
422 StoreEntry::deferProducer(const AsyncCall::Pointer
&producer
)
424 if (!deferredProducer
)
425 deferredProducer
= producer
;
427 debugs(20, 5, HERE
<< "Deferred producer call is allready set to: " <<
428 *deferredProducer
<< ", requested call: " << *producer
);
432 StoreEntry::kickProducer()
434 if (deferredProducer
!= NULL
) {
435 ScheduleCallHere(deferredProducer
);
436 deferredProducer
= NULL
;
442 StoreEntry::destroyMemObject()
444 debugs(20, 3, HERE
<< "destroyMemObject " << mem_obj
);
445 setMemStatus(NOT_IN_MEMORY
);
446 MemObject
*mem
= mem_obj
;
449 delete hidden_mem_obj
;
450 hidden_mem_obj
= NULL
;
454 StoreEntry::hideMemObject()
456 debugs(20, 3, HERE
<< "hiding " << mem_obj
);
458 assert(!hidden_mem_obj
);
459 hidden_mem_obj
= mem_obj
;
464 destroyStoreEntry(void *data
)
466 debugs(20, 3, HERE
<< "destroyStoreEntry: destroying " << data
);
467 StoreEntry
*e
= static_cast<StoreEntry
*>(static_cast<hash_link
*>(data
));
470 if (e
== NullStoreEntry::getInstance())
473 e
->destroyMemObject();
477 assert(e
->key
== NULL
);
482 /* ----- INTERFACE BETWEEN STORAGE MANAGER AND HASH TABLE FUNCTIONS --------- */
485 StoreEntry::hashInsert(const cache_key
* someKey
)
487 debugs(20, 3, "StoreEntry::hashInsert: Inserting Entry " << this << " key '" << storeKeyText(someKey
) << "'");
488 key
= storeKeyDup(someKey
);
489 hash_join(store_table
, this);
493 StoreEntry::hashDelete()
495 hash_remove_link(store_table
, this);
496 storeKeyFree((const cache_key
*)key
);
500 /* -------------------------------------------------------------------------- */
502 /* get rid of memory copy of the object */
504 StoreEntry::purgeMem()
509 debugs(20, 3, "StoreEntry::purgeMem: Freeing memory-copy of " << getMD5Text());
513 if (swap_status
!= SWAPOUT_DONE
)
517 /* RBC 20050104 this is wrong- memory ref counting
518 * is not at all equivalent to the store 'usage' concept
519 * which the replacement policies should be acting upon.
520 * specifically, object iteration within stores needs
521 * memory ref counting to prevent race conditions,
522 * but this should not influence store replacement.
529 debugs(20, 3, "StoreEntry::lock: key '" << getMD5Text() <<"' count=" <<
531 lastref
= squid_curtime
;
532 Store::Root().reference(*this);
536 StoreEntry::setReleaseFlag()
538 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
541 debugs(20, 3, "StoreEntry::setReleaseFlag: '" << getMD5Text() << "'");
543 EBIT_SET(flags
, RELEASE_REQUEST
);
547 StoreEntry::releaseRequest()
549 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
555 * Clear cachable flag here because we might get called before
556 * anyone else even looks at the cachability flag. Also, this
557 * prevents httpMakePublic from really setting a public key.
559 EBIT_CLR(flags
, ENTRY_CACHABLE
);
564 /* unlock object, return -1 if object get released after unlock
565 * otherwise lock_count */
570 debugs(20, 3, "StoreEntry::unlock: key '" << getMD5Text() << "' count=" << lock_count
);
573 return (int) lock_count
;
575 if (store_status
== STORE_PENDING
)
578 assert(storePendingNClients(this) == 0);
580 if (EBIT_TEST(flags
, RELEASE_REQUEST
)) {
585 if (EBIT_TEST(flags
, KEY_PRIVATE
))
586 debugs(20, DBG_IMPORTANT
, "WARNING: " << __FILE__
<< ":" << __LINE__
<< ": found KEY_PRIVATE");
588 Store::Root().handleIdleEntry(*this); // may delete us
593 StoreEntry::getPublicByRequestMethod (StoreClient
*aClient
, HttpRequest
* request
, const HttpRequestMethod
& method
)
596 StoreEntry
*result
= storeGetPublicByRequestMethod( request
, method
);
599 aClient
->created (NullStoreEntry::getInstance());
601 aClient
->created (result
);
605 StoreEntry::getPublicByRequest (StoreClient
*aClient
, HttpRequest
* request
)
608 StoreEntry
*result
= storeGetPublicByRequest (request
);
611 result
= NullStoreEntry::getInstance();
613 aClient
->created (result
);
617 StoreEntry::getPublic (StoreClient
*aClient
, const char *uri
, const HttpRequestMethod
& method
)
620 StoreEntry
*result
= storeGetPublic (uri
, method
);
623 result
= NullStoreEntry::getInstance();
625 aClient
->created (result
);
629 storeGetPublic(const char *uri
, const HttpRequestMethod
& method
)
631 return Store::Root().get(storeKeyPublic(uri
, method
));
635 storeGetPublicByRequestMethod(HttpRequest
* req
, const HttpRequestMethod
& method
)
637 return Store::Root().get(storeKeyPublicByRequestMethod(req
, method
));
641 storeGetPublicByRequest(HttpRequest
* req
)
643 StoreEntry
*e
= storeGetPublicByRequestMethod(req
, req
->method
);
645 if (e
== NULL
&& req
->method
== Http::METHOD_HEAD
)
646 /* We can generate a HEAD reply from a cached GET object */
647 e
= storeGetPublicByRequestMethod(req
, Http::METHOD_GET
);
655 static int key_counter
= 0;
657 if (++key_counter
< 0)
663 /* RBC 20050104 AFAICT this should become simpler:
664 * rather than reinserting with a special key it should be marked
665 * as 'released' and then cleaned up when refcounting indicates.
666 * the StoreHashIndex could well implement its 'released' in the
668 * Also, clean log writing should skip over ia,t
669 * Otherwise, we need a 'remove from the index but not the store
673 StoreEntry::setPrivateKey()
675 const cache_key
*newkey
;
677 if (key
&& EBIT_TEST(flags
, KEY_PRIVATE
))
678 return; /* is already private */
682 storeDirSwapLog(this, SWAP_LOG_DEL
);
687 if (mem_obj
!= NULL
) {
688 mem_obj
->id
= getKeyCounter();
689 newkey
= storeKeyPrivate(mem_obj
->url
, mem_obj
->method
, mem_obj
->id
);
691 newkey
= storeKeyPrivate("JUNK", Http::METHOD_NONE
, getKeyCounter());
694 assert(hash_lookup(store_table
, newkey
) == NULL
);
695 EBIT_SET(flags
, KEY_PRIVATE
);
700 StoreEntry::setPublicKey()
702 StoreEntry
*e2
= NULL
;
703 const cache_key
*newkey
;
705 if (key
&& !EBIT_TEST(flags
, KEY_PRIVATE
))
706 return; /* is already public */
711 * We can't make RELEASE_REQUEST objects public. Depending on
712 * when RELEASE_REQUEST gets set, we might not be swapping out
713 * the object. If we're not swapping out, then subsequent
714 * store clients won't be able to access object data which has
715 * been freed from memory.
717 * If RELEASE_REQUEST is set, then ENTRY_CACHABLE should not
718 * be set, and StoreEntry::setPublicKey() should not be called.
720 #if MORE_DEBUG_OUTPUT
722 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
723 debugs(20, DBG_IMPORTANT
, "assertion failed: RELEASE key " << key
<< ", url " << mem_obj
->url
);
727 assert(!EBIT_TEST(flags
, RELEASE_REQUEST
));
729 if (mem_obj
->request
) {
730 HttpRequest
*request
= mem_obj
->request
;
732 if (!mem_obj
->vary_headers
) {
733 /* First handle the case where the object no longer varies */
734 safe_free(request
->vary_headers
);
736 if (request
->vary_headers
&& strcmp(request
->vary_headers
, mem_obj
->vary_headers
) != 0) {
737 /* Oops.. the variance has changed. Kill the base object
738 * to record the new variance key
740 safe_free(request
->vary_headers
); /* free old "bad" variance key */
741 StoreEntry
*pe
= storeGetPublic(mem_obj
->url
, mem_obj
->method
);
747 /* Make sure the request knows the variance status */
748 if (!request
->vary_headers
) {
749 const char *vary
= httpMakeVaryMark(request
, mem_obj
->getReply());
752 request
->vary_headers
= xstrdup(vary
);
756 // TODO: storeGetPublic() calls below may create unlocked entries.
757 // We should add/use storeHas() API or lock/unlock those entries.
758 if (mem_obj
->vary_headers
&& !storeGetPublic(mem_obj
->url
, mem_obj
->method
)) {
759 /* Create "vary" base object */
761 StoreEntry
*pe
= storeCreateEntry(mem_obj
->url
, mem_obj
->log_url
, request
->flags
, request
->method
);
762 /* We are allowed to do this typecast */
763 HttpReply
*rep
= new HttpReply
;
764 rep
->setHeaders(HTTP_OK
, "Internal marker object", "x-squid-internal/vary", -1, -1, squid_curtime
+ 100000);
765 vary
= mem_obj
->getReply()->header
.getList(HDR_VARY
);
768 /* Again, we own this structure layout */
769 rep
->header
.putStr(HDR_VARY
, vary
.termedBuf());
773 #if X_ACCELERATOR_VARY
774 vary
= mem_obj
->getReply()->header
.getList(HDR_X_ACCELERATOR_VARY
);
776 if (vary
.defined()) {
777 /* Again, we own this structure layout */
778 rep
->header
.putStr(HDR_X_ACCELERATOR_VARY
, vary
.termedBuf());
783 pe
->replaceHttpReply(rep
);
794 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
796 newkey
= storeKeyPublic(mem_obj
->url
, mem_obj
->method
);
798 if ((e2
= (StoreEntry
*) hash_lookup(store_table
, newkey
))) {
799 debugs(20, 3, "StoreEntry::setPublicKey: Making old '" << mem_obj
->url
<< "' private.");
803 if (mem_obj
->request
)
804 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
806 newkey
= storeKeyPublic(mem_obj
->url
, mem_obj
->method
);
812 EBIT_CLR(flags
, KEY_PRIVATE
);
817 storeDirSwapLog(this, SWAP_LOG_ADD
);
821 storeCreateEntry(const char *url
, const char *log_url
, const RequestFlags
&flags
, const HttpRequestMethod
& method
)
823 StoreEntry
*e
= NULL
;
824 MemObject
*mem
= NULL
;
825 debugs(20, 3, "storeCreateEntry: '" << url
<< "'");
827 e
= new StoreEntry(url
, log_url
);
828 e
->lock_count
= 1; /* Note lock here w/o calling storeLock() */
830 mem
->method
= method
;
832 if (neighbors_do_private_keys
|| !flags
.hierarchical
)
837 if (flags
.cachable
) {
838 EBIT_SET(e
->flags
, ENTRY_CACHABLE
);
839 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
841 /* StoreEntry::releaseRequest() clears ENTRY_CACHABLE */
845 e
->store_status
= STORE_PENDING
;
846 e
->setMemStatus(NOT_IN_MEMORY
);
848 e
->lastref
= squid_curtime
;
849 e
->timestamp
= -1; /* set in StoreEntry::timestampsSet() */
850 e
->ping_status
= PING_NONE
;
851 EBIT_SET(e
->flags
, ENTRY_VALIDATED
);
855 /* Mark object as expired */
857 StoreEntry::expireNow()
859 debugs(20, 3, "StoreEntry::expireNow: '" << getMD5Text() << "'");
860 expires
= squid_curtime
;
864 storeWriteComplete (void *data
, StoreIOBuffer wroteBuffer
)
866 PROF_start(storeWriteComplete
);
867 StoreEntry
*e
= (StoreEntry
*)data
;
869 if (EBIT_TEST(e
->flags
, DELAY_SENDING
)) {
870 PROF_stop(storeWriteComplete
);
875 PROF_stop(storeWriteComplete
);
879 StoreEntry::write (StoreIOBuffer writeBuffer
)
881 assert(mem_obj
!= NULL
);
882 /* This assert will change when we teach the store to update */
883 PROF_start(StoreEntry_write
);
884 assert(store_status
== STORE_PENDING
);
886 debugs(20, 5, "storeWrite: writing " << writeBuffer
.length
<< " bytes for '" << getMD5Text() << "'");
887 PROF_stop(StoreEntry_write
);
888 storeGetMemSpace(writeBuffer
.length
);
889 mem_obj
->write (writeBuffer
, storeWriteComplete
, this);
892 /* Append incoming data from a primary server to an entry. */
894 StoreEntry::append(char const *buf
, int len
)
896 assert(mem_obj
!= NULL
);
898 assert(store_status
== STORE_PENDING
);
900 StoreIOBuffer tempBuffer
;
901 tempBuffer
.data
= (char *)buf
;
902 tempBuffer
.length
= len
;
904 * XXX sigh, offset might be < 0 here, but it gets "corrected"
905 * later. This offset crap is such a mess.
907 tempBuffer
.offset
= mem_obj
->endOffset() - (getReply() ? getReply()->hdr_sz
: 0);
912 storeAppendPrintf(StoreEntry
* e
, const char *fmt
,...)
917 storeAppendVPrintf(e
, fmt
, args
);
921 /* used be storeAppendPrintf and Packer */
923 storeAppendVPrintf(StoreEntry
* e
, const char *fmt
, va_list vargs
)
925 LOCAL_ARRAY(char, buf
, 4096);
927 vsnprintf(buf
, 4096, fmt
, vargs
);
928 e
->append(buf
, strlen(buf
));
931 struct _store_check_cachable_hist
{
935 int not_entry_cachable
;
936 int wrong_content_length
;
941 int too_many_open_files
;
942 int too_many_open_fds
;
948 } store_check_cachable_hist
;
951 storeTooManyDiskFilesOpen(void)
953 if (Config
.max_open_disk_fds
== 0)
956 if (store_open_disk_fd
> Config
.max_open_disk_fds
)
963 StoreEntry::checkTooSmall()
965 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
968 if (STORE_OK
== store_status
)
969 if (mem_obj
->object_sz
< 0 ||
970 mem_obj
->object_sz
< Config
.Store
.minObjectSize
)
972 if (getReply()->content_length
> -1)
973 if (getReply()->content_length
< Config
.Store
.minObjectSize
)
978 // TODO: remove checks already performed by swapoutPossible()
979 // TODO: move "too many open..." checks outside -- we are called too early/late
981 StoreEntry::checkCachable()
983 #if CACHE_ALL_METHODS
985 if (mem_obj
->method
!= Http::METHOD_GET
) {
986 debugs(20, 2, "StoreEntry::checkCachable: NO: non-GET method");
987 ++store_check_cachable_hist
.no
.non_get
;
990 if (store_status
== STORE_OK
&& EBIT_TEST(flags
, ENTRY_BAD_LENGTH
)) {
991 debugs(20, 2, "StoreEntry::checkCachable: NO: wrong content-length");
992 ++store_check_cachable_hist
.no
.wrong_content_length
;
993 } else if (!EBIT_TEST(flags
, ENTRY_CACHABLE
)) {
994 debugs(20, 2, "StoreEntry::checkCachable: NO: not cachable");
995 ++store_check_cachable_hist
.no
.not_entry_cachable
;
996 } else if (EBIT_TEST(flags
, ENTRY_NEGCACHED
)) {
997 debugs(20, 3, "StoreEntry::checkCachable: NO: negative cached");
998 ++store_check_cachable_hist
.no
.negative_cached
;
999 return 0; /* avoid release call below */
1000 } else if ((getReply()->content_length
> 0 &&
1001 getReply()->content_length
1002 > Config
.Store
.maxObjectSize
) ||
1003 mem_obj
->endOffset() > Config
.Store
.maxObjectSize
) {
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
;
1031 /* StoreEntry::releaseRequest() cleared ENTRY_CACHABLE */
1036 storeCheckCachableStats(StoreEntry
*sentry
)
1038 storeAppendPrintf(sentry
, "Category\t Count\n");
1040 #if CACHE_ALL_METHODS
1042 storeAppendPrintf(sentry
, "no.non_get\t%d\n",
1043 store_check_cachable_hist
.no
.non_get
);
1046 storeAppendPrintf(sentry
, "no.not_entry_cachable\t%d\n",
1047 store_check_cachable_hist
.no
.not_entry_cachable
);
1048 storeAppendPrintf(sentry
, "no.wrong_content_length\t%d\n",
1049 store_check_cachable_hist
.no
.wrong_content_length
);
1050 storeAppendPrintf(sentry
, "no.negative_cached\t%d\n",
1051 store_check_cachable_hist
.no
.negative_cached
);
1052 storeAppendPrintf(sentry
, "no.too_big\t%d\n",
1053 store_check_cachable_hist
.no
.too_big
);
1054 storeAppendPrintf(sentry
, "no.too_small\t%d\n",
1055 store_check_cachable_hist
.no
.too_small
);
1056 storeAppendPrintf(sentry
, "no.private_key\t%d\n",
1057 store_check_cachable_hist
.no
.private_key
);
1058 storeAppendPrintf(sentry
, "no.too_many_open_files\t%d\n",
1059 store_check_cachable_hist
.no
.too_many_open_files
);
1060 storeAppendPrintf(sentry
, "no.too_many_open_fds\t%d\n",
1061 store_check_cachable_hist
.no
.too_many_open_fds
);
1062 storeAppendPrintf(sentry
, "yes.default\t%d\n",
1063 store_check_cachable_hist
.yes
.Default
);
1067 StoreEntry::complete()
1069 debugs(20, 3, "storeComplete: '" << getMD5Text() << "'");
1071 if (store_status
!= STORE_PENDING
) {
1073 * if we're not STORE_PENDING, then probably we got aborted
1074 * and there should be NO clients on this entry
1076 assert(EBIT_TEST(flags
, ENTRY_ABORTED
));
1077 assert(mem_obj
->nclients
== 0);
1081 /* This is suspect: mem obj offsets include the headers. do we adjust for that
1082 * in use of object_sz?
1084 mem_obj
->object_sz
= mem_obj
->endOffset();
1086 store_status
= STORE_OK
;
1088 assert(mem_status
== NOT_IN_MEMORY
);
1090 if (!validLength()) {
1091 EBIT_SET(flags
, ENTRY_BAD_LENGTH
);
1095 #if USE_CACHE_DIGESTS
1096 if (mem_obj
->request
)
1097 mem_obj
->request
->hier
.store_complete_stop
= current_time
;
1101 * We used to call invokeHandlers, then storeSwapOut. However,
1102 * Madhukar Reddy <myreddy@persistence.com> reported that
1103 * responses without content length would sometimes get released
1104 * in client_side, thinking that the response is incomplete.
1110 * Someone wants to abort this transfer. Set the reason in the
1111 * request structure, call the server-side callback and mark the
1112 * entry for releasing
1117 ++statCounter
.aborted_requests
;
1118 assert(store_status
== STORE_PENDING
);
1119 assert(mem_obj
!= NULL
);
1120 debugs(20, 6, "storeAbort: " << getMD5Text());
1122 lock(); /* lock while aborting */
1127 EBIT_SET(flags
, ENTRY_ABORTED
);
1129 setMemStatus(NOT_IN_MEMORY
);
1131 store_status
= STORE_OK
;
1133 /* Notify the server side */
1137 * Should we check abort.data for validity?
1139 if (mem_obj
->abort
.callback
) {
1140 if (!cbdataReferenceValid(mem_obj
->abort
.data
))
1141 debugs(20, DBG_IMPORTANT
,HERE
<< "queueing event when abort.data is not valid");
1142 eventAdd("mem_obj->abort.callback",
1143 mem_obj
->abort
.callback
,
1144 mem_obj
->abort
.data
,
1150 /* XXX Should we reverse these two, so that there is no
1151 * unneeded disk swapping triggered?
1153 /* Notify the client side */
1156 // abort swap out, invalidating what was created so far (release follows)
1157 swapOutFileClose(StoreIOState::writerGone
);
1159 unlock(); /* unlock */
1163 * Clear Memory storage to accommodate the given object len
1166 storeGetMemSpace(int size
)
1168 PROF_start(storeGetMemSpace
);
1169 StoreEntry
*e
= NULL
;
1171 static time_t last_check
= 0;
1172 size_t pages_needed
;
1173 RemovalPurgeWalker
*walker
;
1175 if (squid_curtime
== last_check
) {
1176 PROF_stop(storeGetMemSpace
);
1180 last_check
= squid_curtime
;
1182 pages_needed
= (size
+ SM_PAGE_SIZE
-1) / SM_PAGE_SIZE
;
1184 if (mem_node::InUseCount() + pages_needed
< store_pages_max
) {
1185 PROF_stop(storeGetMemSpace
);
1189 debugs(20, 2, "storeGetMemSpace: Starting, need " << pages_needed
<<
1192 /* XXX what to set as max_scan here? */
1193 walker
= mem_policy
->PurgeInit(mem_policy
, 100000);
1195 while ((e
= walker
->Next(walker
))) {
1199 if (mem_node::InUseCount() + pages_needed
< store_pages_max
)
1203 walker
->Done(walker
);
1204 debugs(20, 3, "storeGetMemSpace stats:");
1205 debugs(20, 3, " " << std::setw(6) << hot_obj_count
<< " HOT objects");
1206 debugs(20, 3, " " << std::setw(6) << released
<< " were released");
1207 PROF_stop(storeGetMemSpace
);
1210 /* thunk through to Store::Root().maintain(). Note that this would be better still
1211 * if registered against the root store itself, but that requires more complex
1212 * update logic - bigger fish to fry first. Long term each store when
1213 * it becomes active will self register
1216 Store::Maintain(void *notused
)
1218 Store::Root().maintain();
1220 /* Reregister a maintain event .. */
1221 eventAdd("MaintainSwapSpace", Maintain
, NULL
, 1.0, 1);
1225 /* The maximum objects to scan for maintain storage space */
1226 #define MAINTAIN_MAX_SCAN 1024
1227 #define MAINTAIN_MAX_REMOVE 64
1230 * This routine is to be called by main loop in main.c.
1231 * It removes expired objects on only one bucket for each time called.
1233 * This should get called 1/s from main().
1236 StoreController::maintain()
1238 static time_t last_warn_time
= 0;
1240 PROF_start(storeMaintainSwapSpace
);
1241 swapDir
->maintain();
1243 /* this should be emitted by the oversize dir, not globally */
1245 if (Store::Root().currentSize() > Store::Root().maxSize()) {
1246 if (squid_curtime
- last_warn_time
> 10) {
1247 debugs(20, DBG_CRITICAL
, "WARNING: Disk space over limit: "
1248 << Store::Root().currentSize() / 1024.0 << " KB > "
1249 << (Store::Root().maxSize() >> 10) << " KB");
1250 last_warn_time
= squid_curtime
;
1254 PROF_stop(storeMaintainSwapSpace
);
1257 /* release an object from a cache */
1259 StoreEntry::release()
1261 PROF_start(storeRelease
);
1262 debugs(20, 3, "storeRelease: Releasing: '" << getMD5Text() << "'");
1263 /* If, for any reason we can't discard this object because of an
1264 * outstanding request, mark it for pending release */
1268 debugs(20, 3, "storeRelease: Only setting RELEASE_REQUEST bit");
1270 PROF_stop(storeRelease
);
1274 if (StoreController::store_dirs_rebuilding
&& swap_filen
> -1) {
1280 if (swap_filen
> -1) {
1282 * Fake a call to StoreEntry->lock() When rebuilding is done,
1283 * we'll just call StoreEntry->unlock() on these.
1287 LateReleaseStack
.push_back(this);
1289 destroyStoreEntry(static_cast<hash_link
*>(this));
1290 // "this" is no longer valid
1293 PROF_stop(storeRelease
);
1297 storeLog(STORE_LOG_RELEASE
, this);
1299 if (swap_filen
> -1) {
1300 // log before unlink() below clears swap_filen
1301 if (!EBIT_TEST(flags
, KEY_PRIVATE
))
1302 storeDirSwapLog(this, SWAP_LOG_DEL
);
1307 setMemStatus(NOT_IN_MEMORY
);
1308 destroyStoreEntry(static_cast<hash_link
*>(this));
1309 PROF_stop(storeRelease
);
1313 storeLateRelease(void *unused
)
1319 if (StoreController::store_dirs_rebuilding
) {
1320 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
1324 for (i
= 0; i
< 10; ++i
) {
1325 e
= LateReleaseStack
.count
? LateReleaseStack
.pop() : NULL
;
1329 debugs(20, DBG_IMPORTANT
, "storeLateRelease: released " << n
<< " objects");
1337 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 0.0, 1);
1340 /* return 1 if a store entry is locked */
1342 StoreEntry::locked() const
1347 if (swap_status
== SWAPOUT_WRITING
)
1350 if (store_status
== STORE_PENDING
)
1354 * SPECIAL, PUBLIC entries should be "locked"
1356 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
1357 if (!EBIT_TEST(flags
, KEY_PRIVATE
))
1364 StoreEntry::validLength() const
1367 const HttpReply
*reply
;
1368 assert(mem_obj
!= NULL
);
1370 debugs(20, 3, "storeEntryValidLength: Checking '" << getMD5Text() << "'");
1371 debugs(20, 5, "storeEntryValidLength: object_len = " <<
1373 debugs(20, 5, "storeEntryValidLength: hdr_sz = " << reply
->hdr_sz
);
1374 debugs(20, 5, "storeEntryValidLength: content_length = " << reply
->content_length
);
1376 if (reply
->content_length
< 0) {
1377 debugs(20, 5, "storeEntryValidLength: Unspecified content length: " << getMD5Text());
1381 if (reply
->hdr_sz
== 0) {
1382 debugs(20, 5, "storeEntryValidLength: Zero header size: " << getMD5Text());
1386 if (mem_obj
->method
== Http::METHOD_HEAD
) {
1387 debugs(20, 5, "storeEntryValidLength: HEAD request: " << getMD5Text());
1391 if (reply
->sline
.status
== HTTP_NOT_MODIFIED
)
1394 if (reply
->sline
.status
== HTTP_NO_CONTENT
)
1397 diff
= reply
->hdr_sz
+ reply
->content_length
- objectLen();
1402 debugs(20, 3, "storeEntryValidLength: " << (diff
< 0 ? -diff
: diff
) << " bytes too " << (diff
< 0 ? "big" : "small") <<"; '" << getMD5Text() << "'" );
1408 storeRegisterWithCacheManager(void)
1410 Mgr::RegisterAction("storedir", "Store Directory Stats", Store::Stats
, 0, 1);
1411 Mgr::RegisterAction("store_io", "Store IO Interface Stats", &Mgr::StoreIoAction::Create
, 0, 1);
1412 Mgr::RegisterAction("store_check_cachable_stats", "storeCheckCachable() Stats",
1413 storeCheckCachableStats
, 0, 1);
1420 mem_policy
= createRemovalPolicy(Config
.memPolicy
);
1423 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
1424 Store::Root().init();
1425 storeRebuildStart();
1427 storeRegisterWithCacheManager();
1431 storeConfigure(void)
1433 store_swap_high
= (long) (((float) Store::Root().maxSize() *
1434 (float) Config
.Swap
.highWaterMark
) / (float) 100);
1435 store_swap_low
= (long) (((float) Store::Root().maxSize() *
1436 (float) Config
.Swap
.lowWaterMark
) / (float) 100);
1437 store_pages_max
= Config
.memMaxSize
/ sizeof(mem_node
);
1441 StoreEntry::memoryCachable() const
1443 if (mem_obj
== NULL
)
1446 if (mem_obj
->data_hdr
.size() == 0)
1449 if (mem_obj
->inmem_lo
!= 0)
1452 if (!Config
.onoff
.memory_cache_first
&& swap_status
== SWAPOUT_DONE
&& refcount
== 1)
1459 StoreEntry::checkNegativeHit() const
1461 if (!EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1464 if (expires
<= squid_curtime
)
1467 if (store_status
!= STORE_OK
)
1474 * Set object for negative caching.
1475 * Preserves any expiry information given by the server.
1476 * In absence of proper expiry info it will set to expire immediately,
1477 * or with HTTP-violations enabled the configured negative-TTL is observed
1480 StoreEntry::negativeCache()
1482 // XXX: should make the default for expires 0 instead of -1
1483 // so we can distinguish "Expires: -1" from nothing.
1485 #if USE_HTTP_VIOLATIONS
1486 expires
= squid_curtime
+ Config
.negativeTtl
;
1488 expires
= squid_curtime
;
1490 EBIT_SET(flags
, ENTRY_NEGCACHED
);
1494 storeFreeMemory(void)
1497 #if USE_CACHE_DIGESTS
1500 cacheDigestDestroy(store_digest
);
1504 store_digest
= NULL
;
1508 expiresMoreThan(time_t expires
, time_t when
)
1510 if (expires
< 0) /* No Expires given */
1513 return (expires
> (squid_curtime
+ when
));
1517 StoreEntry::validToSend() const
1519 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
1522 if (EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1523 if (expires
<= squid_curtime
)
1526 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
1533 StoreEntry::timestampsSet()
1535 const HttpReply
*reply
= getReply();
1536 time_t served_date
= reply
->date
;
1537 int age
= reply
->header
.getInt(HDR_AGE
);
1538 /* Compute the timestamp, mimicking RFC2616 section 13.2.3. */
1539 /* make sure that 0 <= served_date <= squid_curtime */
1541 if (served_date
< 0 || served_date
> squid_curtime
)
1542 served_date
= squid_curtime
;
1545 * If the returned Date: is more than 24 hours older than
1546 * the squid_curtime, then one of us needs to use NTP to set our
1547 * clock. We'll pretend that our clock is right.
1549 else if (served_date
< (squid_curtime
- 24 * 60 * 60) )
1550 served_date
= squid_curtime
;
1553 * Compensate with Age header if origin server clock is ahead
1554 * of us and there is a cache in between us and the origin
1555 * server. But DONT compensate if the age value is larger than
1556 * squid_curtime because it results in a negative served_date.
1558 if (age
> squid_curtime
- served_date
)
1559 if (squid_curtime
> age
)
1560 served_date
= squid_curtime
- age
;
1562 // compensate for Squid-to-server and server-to-Squid delays
1563 if (mem_obj
&& mem_obj
->request
) {
1564 const time_t request_sent
=
1565 mem_obj
->request
->hier
.peer_http_request_sent
.tv_sec
;
1566 if (0 < request_sent
&& request_sent
< squid_curtime
)
1567 served_date
-= (squid_curtime
- request_sent
);
1570 if (reply
->expires
> 0 && reply
->date
> -1)
1571 expires
= served_date
+ (reply
->expires
- reply
->date
);
1573 expires
= reply
->expires
;
1575 lastmod
= reply
->last_modified
;
1577 timestamp
= served_date
;
1581 StoreEntry::registerAbort(STABH
* cb
, void *data
)
1584 assert(mem_obj
->abort
.callback
== NULL
);
1585 mem_obj
->abort
.callback
= cb
;
1586 mem_obj
->abort
.data
= cbdataReference(data
);
1590 StoreEntry::unregisterAbort()
1593 if (mem_obj
->abort
.callback
) {
1594 mem_obj
->abort
.callback
= NULL
;
1595 cbdataReferenceDone(mem_obj
->abort
.data
);
1600 StoreEntry::dump(int l
) const
1602 debugs(20, l
, "StoreEntry->key: " << getMD5Text());
1603 debugs(20, l
, "StoreEntry->next: " << next
);
1604 debugs(20, l
, "StoreEntry->mem_obj: " << mem_obj
);
1605 debugs(20, l
, "StoreEntry->timestamp: " << timestamp
);
1606 debugs(20, l
, "StoreEntry->lastref: " << lastref
);
1607 debugs(20, l
, "StoreEntry->expires: " << expires
);
1608 debugs(20, l
, "StoreEntry->lastmod: " << lastmod
);
1609 debugs(20, l
, "StoreEntry->swap_file_sz: " << swap_file_sz
);
1610 debugs(20, l
, "StoreEntry->refcount: " << refcount
);
1611 debugs(20, l
, "StoreEntry->flags: " << storeEntryFlags(this));
1612 debugs(20, l
, "StoreEntry->swap_dirn: " << swap_dirn
);
1613 debugs(20, l
, "StoreEntry->swap_filen: " << swap_filen
);
1614 debugs(20, l
, "StoreEntry->lock_count: " << lock_count
);
1615 debugs(20, l
, "StoreEntry->mem_status: " << mem_status
);
1616 debugs(20, l
, "StoreEntry->ping_status: " << ping_status
);
1617 debugs(20, l
, "StoreEntry->store_status: " << store_status
);
1618 debugs(20, l
, "StoreEntry->swap_status: " << swap_status
);
1622 * NOTE, this function assumes only two mem states
1625 StoreEntry::setMemStatus(mem_status_t new_status
)
1627 if (new_status
== mem_status
)
1630 // are we using a shared memory cache?
1631 if (Config
.memShared
&& IamWorkerProcess()) {
1632 // enumerate calling cases if shared memory is enabled
1633 assert(new_status
!= IN_MEMORY
|| EBIT_TEST(flags
, ENTRY_SPECIAL
));
1634 // This method was designed to update replacement policy, not to
1635 // actually purge something from the memory cache (TODO: rename?).
1636 // Shared memory cache does not have a policy that needs updates.
1637 mem_status
= new_status
;
1641 assert(mem_obj
!= NULL
);
1643 if (new_status
== IN_MEMORY
) {
1644 assert(mem_obj
->inmem_lo
== 0);
1646 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1647 debugs(20, 4, "StoreEntry::setMemStatus: not inserting special " << mem_obj
->url
<< " into policy");
1649 mem_policy
->Add(mem_policy
, this, &mem_obj
->repl
);
1650 debugs(20, 4, "StoreEntry::setMemStatus: inserted mem node " << mem_obj
->url
<< " key: " << getMD5Text());
1653 ++hot_obj_count
; // TODO: maintain for the shared hot cache as well
1655 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1656 debugs(20, 4, "StoreEntry::setMemStatus: special entry " << mem_obj
->url
);
1658 mem_policy
->Remove(mem_policy
, this, &mem_obj
->repl
);
1659 debugs(20, 4, "StoreEntry::setMemStatus: removed mem node " << mem_obj
->url
);
1665 mem_status
= new_status
;
1669 StoreEntry::url() const
1672 return "[null_entry]";
1673 else if (mem_obj
== NULL
)
1674 return "[null_mem_obj]";
1676 return mem_obj
->url
;
1680 StoreEntry::createMemObject(const char *aUrl
, const char *aLogUrl
)
1685 if (hidden_mem_obj
) {
1686 debugs(20, 3, HERE
<< "restoring " << hidden_mem_obj
);
1687 mem_obj
= hidden_mem_obj
;
1688 hidden_mem_obj
= NULL
;
1689 mem_obj
->resetUrls(aUrl
, aLogUrl
);
1693 mem_obj
= new MemObject(aUrl
, aLogUrl
);
1696 /* this just sets DELAY_SENDING */
1698 StoreEntry::buffer()
1700 EBIT_SET(flags
, DELAY_SENDING
);
1703 /* this just clears DELAY_SENDING and Invokes the handlers */
1707 if (EBIT_TEST(flags
, DELAY_SENDING
)) {
1708 EBIT_CLR(flags
, DELAY_SENDING
);
1714 StoreEntry::objectLen() const
1716 assert(mem_obj
!= NULL
);
1717 return mem_obj
->object_sz
;
1721 StoreEntry::contentLen() const
1723 assert(mem_obj
!= NULL
);
1724 assert(getReply() != NULL
);
1725 return objectLen() - getReply()->hdr_sz
;
1729 StoreEntry::getReply () const
1731 if (NULL
== mem_obj
)
1734 return mem_obj
->getReply();
1741 debugs(20, 3, "StoreEntry::reset: " << url());
1743 HttpReply
*rep
= (HttpReply
*) getReply(); // bypass const
1745 expires
= lastmod
= timestamp
= -1;
1751 * This routine calls the SETUP routine for each fs type.
1752 * I don't know where the best place for this is, and I'm not going to shuffle
1753 * around large chunks of code right now (that can be done once its working.)
1762 * called to add another store removal policy module
1765 storeReplAdd(const char *type
, REMOVALPOLICYCREATE
* create
)
1769 /* find the number of currently known repl types */
1770 for (i
= 0; storerepl_list
&& storerepl_list
[i
].typestr
; ++i
) {
1771 if (strcmp(storerepl_list
[i
].typestr
, type
) == 0) {
1772 debugs(20, DBG_IMPORTANT
, "WARNING: Trying to load store replacement policy " << type
<< " twice.");
1777 /* add the new type */
1778 storerepl_list
= static_cast<storerepl_entry_t
*>(xrealloc(storerepl_list
, (i
+ 2) * sizeof(storerepl_entry_t
)));
1780 memset(&storerepl_list
[i
+ 1], 0, sizeof(storerepl_entry_t
));
1782 storerepl_list
[i
].typestr
= type
;
1784 storerepl_list
[i
].create
= create
;
1788 * Create a removal policy instance
1791 createRemovalPolicy(RemovalPolicySettings
* settings
)
1793 storerepl_entry_t
*r
;
1795 for (r
= storerepl_list
; r
&& r
->typestr
; ++r
) {
1796 if (strcmp(r
->typestr
, settings
->type
) == 0)
1797 return r
->create(settings
->args
);
1800 debugs(20, DBG_IMPORTANT
, "ERROR: Unknown policy " << settings
->type
);
1801 debugs(20, DBG_IMPORTANT
, "ERROR: Be sure to have set cache_replacement_policy");
1802 debugs(20, DBG_IMPORTANT
, "ERROR: and memory_replacement_policy in squid.conf!");
1803 fatalf("ERROR: Unknown policy %s\n", settings
->type
);
1804 return NULL
; /* NOTREACHED */
1809 storeSwapFileNumberSet(StoreEntry
* e
, sfileno filn
)
1811 if (e
->swap_file_number
== filn
)
1816 storeDirMapBitReset(e
->swap_file_number
);
1817 storeDirLRUDelete(e
);
1818 e
->swap_file_number
= -1;
1820 assert(-1 == e
->swap_file_number
);
1821 storeDirMapBitSet(e
->swap_file_number
= filn
);
1829 * Replace a store entry with
1830 * a new reply. This eats the reply.
1833 StoreEntry::replaceHttpReply(HttpReply
*rep
, bool andStartWriting
)
1835 debugs(20, 3, "StoreEntry::replaceHttpReply: " << url());
1838 debugs(20, DBG_CRITICAL
, "Attempt to replace object with no in-memory representation");
1842 mem_obj
->replaceHttpReply(rep
);
1844 if (andStartWriting
)
1849 StoreEntry::startWriting()
1853 /* TODO: when we store headers serparately remove the header portion */
1854 /* TODO: mark the length of the headers ? */
1855 /* We ONLY want the headers */
1856 packerToStoreInit(&p
, this);
1861 const HttpReply
*rep
= getReply();
1864 rep
->packHeadersInto(&p
);
1865 mem_obj
->markEndOfReplyHeaders();
1867 rep
->body
.packInto(&p
);
1873 StoreEntry::getSerialisedMetaData()
1875 StoreMeta
*tlv_list
= storeSwapMetaBuild(this);
1877 char *result
= storeSwapMetaPack(tlv_list
, &swap_hdr_sz
);
1878 storeSwapTLVFree(tlv_list
);
1879 assert (swap_hdr_sz
>= 0);
1880 mem_obj
->swap_hdr_sz
= (size_t) swap_hdr_sz
;
1885 StoreEntry::trimMemory(const bool preserveSwappable
)
1889 * Bug #1943. We must not let go any data for IN_MEMORY
1890 * objects. We have to wait until the mem_status changes.
1892 if (mem_status
== IN_MEMORY
)
1895 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
1896 return; // cannot trim because we do not load them again
1898 if (!preserveSwappable
) {
1899 if (mem_obj
->policyLowestOffsetToKeep(0) == 0) {
1904 * Its not swap-able, and we're about to delete a chunk,
1905 * so we must make it PRIVATE. This is tricky/ugly because
1906 * for the most part, we treat swapable == cachable here.
1909 mem_obj
->trimUnSwappable ();
1911 mem_obj
->trimSwappable ();
1916 StoreEntry::modifiedSince(HttpRequest
* request
) const
1919 time_t mod_time
= lastmod
;
1922 mod_time
= timestamp
;
1924 debugs(88, 3, "modifiedSince: '" << url() << "'");
1926 debugs(88, 3, "modifiedSince: mod_time = " << mod_time
);
1931 /* Find size of the object */
1932 object_length
= getReply()->content_length
;
1934 if (object_length
< 0)
1935 object_length
= contentLen();
1937 if (mod_time
> request
->ims
) {
1938 debugs(88, 3, "--> YES: entry newer than client");
1940 } else if (mod_time
< request
->ims
) {
1941 debugs(88, 3, "--> NO: entry older than client");
1943 } else if (request
->imslen
< 0) {
1944 debugs(88, 3, "--> NO: same LMT, no client length");
1946 } else if (request
->imslen
== object_length
) {
1947 debugs(88, 3, "--> NO: same LMT, same length");
1950 debugs(88, 3, "--> YES: same LMT, different length");
1956 StoreEntry::hasIfMatchEtag(const HttpRequest
&request
) const
1958 const String reqETags
= request
.header
.getList(HDR_IF_MATCH
);
1959 return hasOneOfEtags(reqETags
, false);
1963 StoreEntry::hasIfNoneMatchEtag(const HttpRequest
&request
) const
1965 const String reqETags
= request
.header
.getList(HDR_IF_NONE_MATCH
);
1966 // weak comparison is allowed only for HEAD or full-body GET requests
1967 const bool allowWeakMatch
= !request
.flags
.isRanged
&&
1968 (request
.method
== Http::METHOD_GET
|| request
.method
== Http::METHOD_HEAD
);
1969 return hasOneOfEtags(reqETags
, allowWeakMatch
);
1972 /// whether at least one of the request ETags matches entity ETag
1974 StoreEntry::hasOneOfEtags(const String
&reqETags
, const bool allowWeakMatch
) const
1976 const ETag repETag
= getReply()->header
.getETag(HDR_ETAG
);
1978 return strListIsMember(&reqETags
, "*", ',');
1980 bool matched
= false;
1981 const char *pos
= NULL
;
1984 while (!matched
&& strListGetItem(&reqETags
, ',', &item
, &ilen
, &pos
)) {
1985 if (!strncmp(item
, "*", ilen
))
1989 str
.append(item
, ilen
);
1991 if (etagParseInit(&reqETag
, str
.termedBuf())) {
1992 matched
= allowWeakMatch
? etagIsWeakEqual(repETag
, reqETag
) :
1993 etagIsStrongEqual(repETag
, reqETag
);
2001 StoreEntry::store() const
2003 assert(0 <= swap_dirn
&& swap_dirn
< Config
.cacheSwap
.n_configured
);
2004 return INDEXSD(swap_dirn
);
2008 StoreEntry::unlink()
2010 store()->unlink(*this); // implies disconnect()
2013 swap_status
= SWAPOUT_NONE
;
2017 * return true if the entry is in a state where
2018 * it can accept more data (ie with write() method)
2021 StoreEntry::isAccepting() const
2023 if (STORE_PENDING
!= store_status
)
2026 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
2032 std::ostream
&operator <<(std::ostream
&os
, const StoreEntry
&e
)
2034 return os
<< e
.swap_filen
<< '@' << e
.swap_dirn
<< '=' <<
2035 e
.mem_status
<< '/' << e
.ping_status
<< '/' << e
.store_status
<< '/' <<
2039 /* NullStoreEntry */
2041 NullStoreEntry
NullStoreEntry::_instance
;
2044 NullStoreEntry::getInstance()
2050 NullStoreEntry::getMD5Text() const
2056 NullStoreEntry::operator delete(void*)
2058 fatal ("Attempt to delete NullStoreEntry\n");
2062 NullStoreEntry::getSerialisedMetaData()
2068 #include "Store.cci"