5 * DEBUG: section 20 Storage Manager
6 * AUTHOR: Harvest Derived
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
37 #include "CacheDigest.h"
38 #include "CacheManager.h"
39 #include "comm/Connection.h"
44 #include "HttpReply.h"
45 #include "HttpRequest.h"
47 #include "MemObject.h"
48 #include "mgr/Registration.h"
49 #include "mgr/StoreIoAction.h"
50 #include "profiler/Profiler.h"
52 #include "repl_modules.h"
53 #include "SquidTime.h"
55 #include "StatCounters.h"
57 #include "StoreClient.h"
59 #include "store_key_md5.h"
60 #include "StoreIOState.h"
61 #include "StoreMeta.h"
63 #include "store_key_md5.h"
65 #include "swap_log_op.h"
67 #include "DelayPools.h"
73 static STMCB storeWriteComplete
;
75 #define REBUILD_TIMESTAMP_DELTA_MAX 2
77 #define STORE_IN_MEM_BUCKETS (229)
79 /** \todo Convert these string constants to enum string-arrays generated */
81 const char *memStatusStr
[] = {
86 const char *pingStatusStr
[] = {
92 const char *storeStatusStr
[] = {
97 const char *swapStatusStr
[] = {
104 * This defines an repl type
107 typedef struct _storerepl_entry storerepl_entry_t
;
109 struct _storerepl_entry
{
111 REMOVALPOLICYCREATE
*create
;
114 static storerepl_entry_t
*storerepl_list
= NULL
;
117 * local function prototypes
119 static int getKeyCounter(void);
120 static OBJH storeCheckCachableStats
;
121 static EVH storeLateRelease
;
126 static Stack
<StoreEntry
*> LateReleaseStack
;
127 MemAllocator
*StoreEntry::pool
= NULL
;
129 StorePointer
Store::CurrentRoot
= NULL
;
132 Store::Root(Store
* aRoot
)
138 Store::Root(StorePointer aRoot
)
140 Root(aRoot
.getRaw());
144 Store::Stats(StoreEntry
* output
)
147 Root().stat(*output
);
163 Store::unlink (StoreEntry
&anEntry
)
165 fatal("Store::unlink on invalid Store\n");
169 StoreEntry::operator new (size_t bytecount
)
171 assert (bytecount
== sizeof (StoreEntry
));
174 pool
= memPoolCreate ("StoreEntry", bytecount
);
175 pool
->setChunkSize(2048 * 1024);
178 return pool
->alloc();
182 StoreEntry::operator delete (void *address
)
184 pool
->freeOne(address
);
188 StoreEntry::makePublic()
190 /* This object can be cached for a long time */
192 if (EBIT_TEST(flags
, ENTRY_CACHABLE
))
197 StoreEntry::makePrivate()
199 /* This object should never be cached at all */
201 releaseRequest(); /* delete object when not used */
202 /* releaseRequest clears ENTRY_CACHABLE flag */
206 StoreEntry::cacheNegatively()
208 /* This object may be negatively cached */
211 if (EBIT_TEST(flags
, ENTRY_CACHABLE
))
216 StoreEntry::inUseCount()
220 return pool
->getInUseCount();
224 StoreEntry::getMD5Text() const
226 return storeKeyText((const cache_key
*)key
);
232 StoreEntry::DeferReader(void *theContext
, CommRead
const &aRead
)
234 StoreEntry
*anEntry
= (StoreEntry
*)theContext
;
235 anEntry
->delayAwareRead(aRead
.conn
,
242 StoreEntry::delayAwareRead(const Comm::ConnectionPointer
&conn
, char *buf
, int len
, AsyncCall::Pointer callback
)
244 size_t amountToRead
= bytesWanted(Range
<size_t>(0, len
));
245 /* sketch: readdeferer* = getdeferer.
246 * ->deferRead (fd, buf, len, callback, DelayAwareRead, this)
249 if (amountToRead
== 0) {
251 /* read ahead limit */
252 /* Perhaps these two calls should both live in MemObject */
254 if (!mem_obj
->readAheadPolicyCanRead()) {
256 mem_obj
->delayRead(DeferredRead(DeferReader
, this, CommRead(conn
, buf
, len
, callback
)));
262 mem_obj
->mostBytesAllowed().delayRead(DeferredRead(DeferReader
, this, CommRead(conn
, buf
, len
, callback
)));
269 if (fd_table
[conn
->fd
].closing()) {
270 // Readers must have closing callbacks if they want to be notified. No
271 // readers appeared to care around 2009/12/14 as they skipped reading
272 // for other reasons. Closing may already be true at the delyaAwareRead
273 // call time or may happen while we wait after delayRead() above.
274 debugs(20, 3, HERE
<< "wont read from closing " << conn
<< " for " <<
276 return; // the read callback will never be called
279 comm_read(conn
, buf
, amountToRead
, callback
);
283 StoreEntry::bytesWanted (Range
<size_t> const aRange
, bool ignoreDelayPools
) const
288 #if URL_CHECKSUM_DEBUG
290 mem_obj
->checkUrlChecksum();
294 if (!mem_obj
->readAheadPolicyCanRead())
297 return mem_obj
->mostBytesWanted(aRange
.end
, ignoreDelayPools
);
301 StoreEntry::checkDeferRead(int fd
) const
303 return (bytesWanted(Range
<size_t>(0,INT_MAX
)) == 0);
307 StoreEntry::setNoDelay (bool const newValue
)
310 mem_obj
->setNoDelay(newValue
);
314 StoreEntry::storeClientType() const
316 /* The needed offset isn't in memory
317 * XXX TODO: this is wrong for range requests
318 * as the needed offset may *not* be 0, AND
319 * offset 0 in the memory object is the HTTP headers.
322 if (mem_status
== IN_MEMORY
&& Config
.memShared
&& IamWorkerProcess()) {
323 // clients of an object cached in shared memory are memory clients
324 return STORE_MEM_CLIENT
;
329 if (mem_obj
->inmem_lo
)
330 return STORE_DISK_CLIENT
;
332 if (EBIT_TEST(flags
, ENTRY_ABORTED
)) {
333 /* I don't think we should be adding clients to aborted entries */
334 debugs(20, DBG_IMPORTANT
, "storeClientType: adding to ENTRY_ABORTED entry");
335 return STORE_MEM_CLIENT
;
338 if (store_status
== STORE_OK
) {
339 /* the object has completed. */
341 if (mem_obj
->inmem_lo
== 0 && !isEmpty()) {
342 if (swap_status
== SWAPOUT_DONE
) {
343 debugs(20,7, HERE
<< mem_obj
<< " lo: " << mem_obj
->inmem_lo
<< " hi: " << mem_obj
->endOffset() << " size: " << mem_obj
->object_sz
);
344 if (mem_obj
->endOffset() == mem_obj
->object_sz
) {
345 /* hot object fully swapped in */
346 return STORE_MEM_CLIENT
;
349 /* Memory-only, or currently being swapped out */
350 return STORE_MEM_CLIENT
;
353 return STORE_DISK_CLIENT
;
356 /* here and past, entry is STORE_PENDING */
358 * If this is the first client, let it be the mem client
360 if (mem_obj
->nclients
== 1)
361 return STORE_MEM_CLIENT
;
364 * If there is no disk file to open yet, we must make this a
365 * mem client. If we can't open the swapin file before writing
366 * to the client, there is no guarantee that we will be able
367 * to open it later when we really need it.
369 if (swap_status
== SWAPOUT_NONE
)
370 return STORE_MEM_CLIENT
;
373 * otherwise, make subsequent clients read from disk so they
374 * can not delay the first, and vice-versa.
376 return STORE_DISK_CLIENT
;
379 StoreEntry::StoreEntry():
380 hidden_mem_obj(NULL
),
383 debugs(20, 3, HERE
<< "new StoreEntry " << this);
386 expires
= lastmod
= lastref
= timestamp
= -1;
388 swap_status
= SWAPOUT_NONE
;
393 StoreEntry::StoreEntry(const char *aUrl
, const char *aLogUrl
):
394 hidden_mem_obj(NULL
),
397 debugs(20, 3, HERE
<< "new StoreEntry " << this);
398 mem_obj
= new MemObject(aUrl
, aLogUrl
);
400 expires
= lastmod
= lastref
= timestamp
= -1;
402 swap_status
= SWAPOUT_NONE
;
407 StoreEntry::~StoreEntry()
409 if (swap_filen
>= 0) {
410 SwapDir
&sd
= dynamic_cast<SwapDir
&>(*store());
411 sd
.disconnect(*this);
413 delete hidden_mem_obj
;
418 StoreEntry::deferProducer(const AsyncCall::Pointer
&producer
)
420 if (!deferredProducer
)
421 deferredProducer
= producer
;
423 debugs(20, 5, HERE
<< "Deferred producer call is allready set to: " <<
424 *deferredProducer
<< ", requested call: " << *producer
);
428 StoreEntry::kickProducer()
430 if (deferredProducer
!= NULL
) {
431 ScheduleCallHere(deferredProducer
);
432 deferredProducer
= NULL
;
438 StoreEntry::destroyMemObject()
440 debugs(20, 3, HERE
<< "destroyMemObject " << mem_obj
);
441 setMemStatus(NOT_IN_MEMORY
);
442 MemObject
*mem
= mem_obj
;
445 delete hidden_mem_obj
;
446 hidden_mem_obj
= NULL
;
450 StoreEntry::hideMemObject()
452 debugs(20, 3, HERE
<< "hiding " << mem_obj
);
454 assert(!hidden_mem_obj
);
455 hidden_mem_obj
= mem_obj
;
460 destroyStoreEntry(void *data
)
462 debugs(20, 3, HERE
<< "destroyStoreEntry: destroying " << data
);
463 StoreEntry
*e
= static_cast<StoreEntry
*>(static_cast<hash_link
*>(data
));
466 if (e
== NullStoreEntry::getInstance())
469 e
->destroyMemObject();
473 assert(e
->key
== NULL
);
478 /* ----- INTERFACE BETWEEN STORAGE MANAGER AND HASH TABLE FUNCTIONS --------- */
481 StoreEntry::hashInsert(const cache_key
* someKey
)
483 debugs(20, 3, "StoreEntry::hashInsert: Inserting Entry " << this << " key '" << storeKeyText(someKey
) << "'");
484 key
= storeKeyDup(someKey
);
485 hash_join(store_table
, this);
489 StoreEntry::hashDelete()
491 hash_remove_link(store_table
, this);
492 storeKeyFree((const cache_key
*)key
);
496 /* -------------------------------------------------------------------------- */
498 /* get rid of memory copy of the object */
500 StoreEntry::purgeMem()
505 debugs(20, 3, "StoreEntry::purgeMem: Freeing memory-copy of " << getMD5Text());
509 if (swap_status
!= SWAPOUT_DONE
)
513 /* RBC 20050104 this is wrong- memory ref counting
514 * is not at all equivalent to the store 'usage' concept
515 * which the replacement policies should be acting upon.
516 * specifically, object iteration within stores needs
517 * memory ref counting to prevent race conditions,
518 * but this should not influence store replacement.
525 debugs(20, 3, "StoreEntry::lock: key '" << getMD5Text() <<"' count=" <<
527 lastref
= squid_curtime
;
528 Store::Root().reference(*this);
532 StoreEntry::setReleaseFlag()
534 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
537 debugs(20, 3, "StoreEntry::setReleaseFlag: '" << getMD5Text() << "'");
539 EBIT_SET(flags
, RELEASE_REQUEST
);
543 StoreEntry::releaseRequest()
545 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
551 * Clear cachable flag here because we might get called before
552 * anyone else even looks at the cachability flag. Also, this
553 * prevents httpMakePublic from really setting a public key.
555 EBIT_CLR(flags
, ENTRY_CACHABLE
);
560 /* unlock object, return -1 if object get released after unlock
561 * otherwise lock_count */
566 debugs(20, 3, "StoreEntry::unlock: key '" << getMD5Text() << "' count=" << lock_count
);
569 return (int) lock_count
;
571 if (store_status
== STORE_PENDING
)
574 assert(storePendingNClients(this) == 0);
576 if (EBIT_TEST(flags
, RELEASE_REQUEST
)) {
581 if (EBIT_TEST(flags
, KEY_PRIVATE
))
582 debugs(20, DBG_IMPORTANT
, "WARNING: " << __FILE__
<< ":" << __LINE__
<< ": found KEY_PRIVATE");
584 Store::Root().handleIdleEntry(*this); // may delete us
589 StoreEntry::getPublicByRequestMethod (StoreClient
*aClient
, HttpRequest
* request
, const HttpRequestMethod
& method
)
592 StoreEntry
*result
= storeGetPublicByRequestMethod( request
, method
);
595 aClient
->created (NullStoreEntry::getInstance());
597 aClient
->created (result
);
601 StoreEntry::getPublicByRequest (StoreClient
*aClient
, HttpRequest
* request
)
604 StoreEntry
*result
= storeGetPublicByRequest (request
);
607 result
= NullStoreEntry::getInstance();
609 aClient
->created (result
);
613 StoreEntry::getPublic (StoreClient
*aClient
, const char *uri
, const HttpRequestMethod
& method
)
616 StoreEntry
*result
= storeGetPublic (uri
, method
);
619 result
= NullStoreEntry::getInstance();
621 aClient
->created (result
);
625 storeGetPublic(const char *uri
, const HttpRequestMethod
& method
)
627 return Store::Root().get(storeKeyPublic(uri
, method
));
631 storeGetPublicByRequestMethod(HttpRequest
* req
, const HttpRequestMethod
& method
)
633 return Store::Root().get(storeKeyPublicByRequestMethod(req
, method
));
637 storeGetPublicByRequest(HttpRequest
* req
)
639 StoreEntry
*e
= storeGetPublicByRequestMethod(req
, req
->method
);
641 if (e
== NULL
&& req
->method
== METHOD_HEAD
)
642 /* We can generate a HEAD reply from a cached GET object */
643 e
= storeGetPublicByRequestMethod(req
, METHOD_GET
);
651 static int key_counter
= 0;
653 if (++key_counter
< 0)
659 /* RBC 20050104 AFAICT this should become simpler:
660 * rather than reinserting with a special key it should be marked
661 * as 'released' and then cleaned up when refcounting indicates.
662 * the StoreHashIndex could well implement its 'released' in the
664 * Also, clean log writing should skip over ia,t
665 * Otherwise, we need a 'remove from the index but not the store
669 StoreEntry::setPrivateKey()
671 const cache_key
*newkey
;
673 if (key
&& EBIT_TEST(flags
, KEY_PRIVATE
))
674 return; /* is already private */
678 storeDirSwapLog(this, SWAP_LOG_DEL
);
683 if (mem_obj
!= NULL
) {
684 mem_obj
->id
= getKeyCounter();
685 newkey
= storeKeyPrivate(mem_obj
->url
, mem_obj
->method
, mem_obj
->id
);
687 newkey
= storeKeyPrivate("JUNK", METHOD_NONE
, getKeyCounter());
690 assert(hash_lookup(store_table
, newkey
) == NULL
);
691 EBIT_SET(flags
, KEY_PRIVATE
);
696 StoreEntry::setPublicKey()
698 StoreEntry
*e2
= NULL
;
699 const cache_key
*newkey
;
701 if (key
&& !EBIT_TEST(flags
, KEY_PRIVATE
))
702 return; /* is already public */
707 * We can't make RELEASE_REQUEST objects public. Depending on
708 * when RELEASE_REQUEST gets set, we might not be swapping out
709 * the object. If we're not swapping out, then subsequent
710 * store clients won't be able to access object data which has
711 * been freed from memory.
713 * If RELEASE_REQUEST is set, then ENTRY_CACHABLE should not
714 * be set, and StoreEntry::setPublicKey() should not be called.
716 #if MORE_DEBUG_OUTPUT
718 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
719 debugs(20, DBG_IMPORTANT
, "assertion failed: RELEASE key " << key
<< ", url " << mem_obj
->url
);
723 assert(!EBIT_TEST(flags
, RELEASE_REQUEST
));
725 if (mem_obj
->request
) {
726 HttpRequest
*request
= mem_obj
->request
;
728 if (!mem_obj
->vary_headers
) {
729 /* First handle the case where the object no longer varies */
730 safe_free(request
->vary_headers
);
732 if (request
->vary_headers
&& strcmp(request
->vary_headers
, mem_obj
->vary_headers
) != 0) {
733 /* Oops.. the variance has changed. Kill the base object
734 * to record the new variance key
736 safe_free(request
->vary_headers
); /* free old "bad" variance key */
737 StoreEntry
*pe
= storeGetPublic(mem_obj
->url
, mem_obj
->method
);
743 /* Make sure the request knows the variance status */
744 if (!request
->vary_headers
) {
745 const char *vary
= httpMakeVaryMark(request
, mem_obj
->getReply());
748 request
->vary_headers
= xstrdup(vary
);
752 // TODO: storeGetPublic() calls below may create unlocked entries.
753 // We should add/use storeHas() API or lock/unlock those entries.
754 if (mem_obj
->vary_headers
&& !storeGetPublic(mem_obj
->url
, mem_obj
->method
)) {
755 /* Create "vary" base object */
757 StoreEntry
*pe
= storeCreateEntry(mem_obj
->url
, mem_obj
->log_url
, request
->flags
, request
->method
);
758 /* We are allowed to do this typecast */
759 HttpReply
*rep
= new HttpReply
;
760 rep
->setHeaders(HTTP_OK
, "Internal marker object", "x-squid-internal/vary", -1, -1, squid_curtime
+ 100000);
761 vary
= mem_obj
->getReply()->header
.getList(HDR_VARY
);
764 /* Again, we own this structure layout */
765 rep
->header
.putStr(HDR_VARY
, vary
.termedBuf());
769 #if X_ACCELERATOR_VARY
770 vary
= mem_obj
->getReply()->header
.getList(HDR_X_ACCELERATOR_VARY
);
772 if (vary
.defined()) {
773 /* Again, we own this structure layout */
774 rep
->header
.putStr(HDR_X_ACCELERATOR_VARY
, vary
.termedBuf());
779 pe
->replaceHttpReply(rep
);
790 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
792 newkey
= storeKeyPublic(mem_obj
->url
, mem_obj
->method
);
794 if ((e2
= (StoreEntry
*) hash_lookup(store_table
, newkey
))) {
795 debugs(20, 3, "StoreEntry::setPublicKey: Making old '" << mem_obj
->url
<< "' private.");
799 if (mem_obj
->request
)
800 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
802 newkey
= storeKeyPublic(mem_obj
->url
, mem_obj
->method
);
808 EBIT_CLR(flags
, KEY_PRIVATE
);
813 storeDirSwapLog(this, SWAP_LOG_ADD
);
817 storeCreateEntry(const char *url
, const char *log_url
, request_flags flags
, const HttpRequestMethod
& method
)
819 StoreEntry
*e
= NULL
;
820 MemObject
*mem
= NULL
;
821 debugs(20, 3, "storeCreateEntry: '" << url
<< "'");
823 e
= new StoreEntry(url
, log_url
);
824 e
->lock_count
= 1; /* Note lock here w/o calling storeLock() */
826 mem
->method
= method
;
828 if (neighbors_do_private_keys
|| !flags
.hierarchical
)
833 if (flags
.cachable
) {
834 EBIT_SET(e
->flags
, ENTRY_CACHABLE
);
835 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
837 /* StoreEntry::releaseRequest() clears ENTRY_CACHABLE */
841 e
->store_status
= STORE_PENDING
;
842 e
->setMemStatus(NOT_IN_MEMORY
);
844 e
->lastref
= squid_curtime
;
845 e
->timestamp
= -1; /* set in StoreEntry::timestampsSet() */
846 e
->ping_status
= PING_NONE
;
847 EBIT_SET(e
->flags
, ENTRY_VALIDATED
);
851 /* Mark object as expired */
853 StoreEntry::expireNow()
855 debugs(20, 3, "StoreEntry::expireNow: '" << getMD5Text() << "'");
856 expires
= squid_curtime
;
860 storeWriteComplete (void *data
, StoreIOBuffer wroteBuffer
)
862 PROF_start(storeWriteComplete
);
863 StoreEntry
*e
= (StoreEntry
*)data
;
865 if (EBIT_TEST(e
->flags
, DELAY_SENDING
)) {
866 PROF_stop(storeWriteComplete
);
871 PROF_stop(storeWriteComplete
);
875 StoreEntry::write (StoreIOBuffer writeBuffer
)
877 assert(mem_obj
!= NULL
);
878 /* This assert will change when we teach the store to update */
879 PROF_start(StoreEntry_write
);
880 assert(store_status
== STORE_PENDING
);
882 debugs(20, 5, "storeWrite: writing " << writeBuffer
.length
<< " bytes for '" << getMD5Text() << "'");
883 PROF_stop(StoreEntry_write
);
884 storeGetMemSpace(writeBuffer
.length
);
885 mem_obj
->write (writeBuffer
, storeWriteComplete
, this);
888 /* Append incoming data from a primary server to an entry. */
890 StoreEntry::append(char const *buf
, int len
)
892 assert(mem_obj
!= NULL
);
894 assert(store_status
== STORE_PENDING
);
896 StoreIOBuffer tempBuffer
;
897 tempBuffer
.data
= (char *)buf
;
898 tempBuffer
.length
= len
;
900 * XXX sigh, offset might be < 0 here, but it gets "corrected"
901 * later. This offset crap is such a mess.
903 tempBuffer
.offset
= mem_obj
->endOffset() - (getReply() ? getReply()->hdr_sz
: 0);
908 storeAppendPrintf(StoreEntry
* e
, const char *fmt
,...)
913 storeAppendVPrintf(e
, fmt
, args
);
917 /* used be storeAppendPrintf and Packer */
919 storeAppendVPrintf(StoreEntry
* e
, const char *fmt
, va_list vargs
)
921 LOCAL_ARRAY(char, buf
, 4096);
923 vsnprintf(buf
, 4096, fmt
, vargs
);
924 e
->append(buf
, strlen(buf
));
927 struct _store_check_cachable_hist
{
931 int not_entry_cachable
;
932 int wrong_content_length
;
937 int too_many_open_files
;
938 int too_many_open_fds
;
944 } store_check_cachable_hist
;
947 storeTooManyDiskFilesOpen(void)
949 if (Config
.max_open_disk_fds
== 0)
952 if (store_open_disk_fd
> Config
.max_open_disk_fds
)
959 StoreEntry::checkTooSmall()
961 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
964 if (STORE_OK
== store_status
)
965 if (mem_obj
->object_sz
< 0 ||
966 mem_obj
->object_sz
< Config
.Store
.minObjectSize
)
968 if (getReply()->content_length
> -1)
969 if (getReply()->content_length
< Config
.Store
.minObjectSize
)
974 // TODO: remove checks already performed by swapoutPossible()
975 // TODO: move "too many open..." checks outside -- we are called too early/late
977 StoreEntry::checkCachable()
979 #if CACHE_ALL_METHODS
981 if (mem_obj
->method
!= METHOD_GET
) {
982 debugs(20, 2, "StoreEntry::checkCachable: NO: non-GET method");
983 ++store_check_cachable_hist
.no
.non_get
;
986 if (store_status
== STORE_OK
&& EBIT_TEST(flags
, ENTRY_BAD_LENGTH
)) {
987 debugs(20, 2, "StoreEntry::checkCachable: NO: wrong content-length");
988 ++store_check_cachable_hist
.no
.wrong_content_length
;
989 } else if (!EBIT_TEST(flags
, ENTRY_CACHABLE
)) {
990 debugs(20, 2, "StoreEntry::checkCachable: NO: not cachable");
991 ++store_check_cachable_hist
.no
.not_entry_cachable
;
992 } else if (EBIT_TEST(flags
, ENTRY_NEGCACHED
)) {
993 debugs(20, 3, "StoreEntry::checkCachable: NO: negative cached");
994 ++store_check_cachable_hist
.no
.negative_cached
;
995 return 0; /* avoid release call below */
996 } else if ((getReply()->content_length
> 0 &&
997 getReply()->content_length
998 > Config
.Store
.maxObjectSize
) ||
999 mem_obj
->endOffset() > Config
.Store
.maxObjectSize
) {
1000 debugs(20, 2, "StoreEntry::checkCachable: NO: too big");
1001 ++store_check_cachable_hist
.no
.too_big
;
1002 } else if (getReply()->content_length
> Config
.Store
.maxObjectSize
) {
1003 debugs(20, 2, "StoreEntry::checkCachable: NO: too big");
1004 ++store_check_cachable_hist
.no
.too_big
;
1005 } else if (checkTooSmall()) {
1006 debugs(20, 2, "StoreEntry::checkCachable: NO: too small");
1007 ++store_check_cachable_hist
.no
.too_small
;
1008 } else if (EBIT_TEST(flags
, KEY_PRIVATE
)) {
1009 debugs(20, 3, "StoreEntry::checkCachable: NO: private key");
1010 ++store_check_cachable_hist
.no
.private_key
;
1011 } else if (swap_status
!= SWAPOUT_NONE
) {
1013 * here we checked the swap_status because the remaining
1014 * cases are only relevant only if we haven't started swapping
1015 * out the object yet.
1018 } else if (storeTooManyDiskFilesOpen()) {
1019 debugs(20, 2, "StoreEntry::checkCachable: NO: too many disk files open");
1020 ++store_check_cachable_hist
.no
.too_many_open_files
;
1021 } else if (fdNFree() < RESERVED_FD
) {
1022 debugs(20, 2, "StoreEntry::checkCachable: NO: too many FD's open");
1023 ++store_check_cachable_hist
.no
.too_many_open_fds
;
1025 ++store_check_cachable_hist
.yes
.Default
;
1030 /* StoreEntry::releaseRequest() cleared ENTRY_CACHABLE */
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.too_big\t%d\n",
1052 store_check_cachable_hist
.no
.too_big
);
1053 storeAppendPrintf(sentry
, "no.too_small\t%d\n",
1054 store_check_cachable_hist
.no
.too_small
);
1055 storeAppendPrintf(sentry
, "no.private_key\t%d\n",
1056 store_check_cachable_hist
.no
.private_key
);
1057 storeAppendPrintf(sentry
, "no.too_many_open_files\t%d\n",
1058 store_check_cachable_hist
.no
.too_many_open_files
);
1059 storeAppendPrintf(sentry
, "no.too_many_open_fds\t%d\n",
1060 store_check_cachable_hist
.no
.too_many_open_fds
);
1061 storeAppendPrintf(sentry
, "yes.default\t%d\n",
1062 store_check_cachable_hist
.yes
.Default
);
1066 StoreEntry::complete()
1068 debugs(20, 3, "storeComplete: '" << getMD5Text() << "'");
1070 if (store_status
!= STORE_PENDING
) {
1072 * if we're not STORE_PENDING, then probably we got aborted
1073 * and there should be NO clients on this entry
1075 assert(EBIT_TEST(flags
, ENTRY_ABORTED
));
1076 assert(mem_obj
->nclients
== 0);
1080 /* This is suspect: mem obj offsets include the headers. do we adjust for that
1081 * in use of object_sz?
1083 mem_obj
->object_sz
= mem_obj
->endOffset();
1085 store_status
= STORE_OK
;
1087 assert(mem_status
== NOT_IN_MEMORY
);
1089 if (!validLength()) {
1090 EBIT_SET(flags
, ENTRY_BAD_LENGTH
);
1094 #if USE_CACHE_DIGESTS
1095 if (mem_obj
->request
)
1096 mem_obj
->request
->hier
.store_complete_stop
= current_time
;
1100 * We used to call invokeHandlers, then storeSwapOut. However,
1101 * Madhukar Reddy <myreddy@persistence.com> reported that
1102 * responses without content length would sometimes get released
1103 * in client_side, thinking that the response is incomplete.
1109 * Someone wants to abort this transfer. Set the reason in the
1110 * request structure, call the server-side callback and mark the
1111 * entry for releasing
1116 ++statCounter
.aborted_requests
;
1117 assert(store_status
== STORE_PENDING
);
1118 assert(mem_obj
!= NULL
);
1119 debugs(20, 6, "storeAbort: " << getMD5Text());
1121 lock(); /* lock while aborting */
1126 EBIT_SET(flags
, ENTRY_ABORTED
);
1128 setMemStatus(NOT_IN_MEMORY
);
1130 store_status
= STORE_OK
;
1132 /* Notify the server side */
1136 * Should we check abort.data for validity?
1138 if (mem_obj
->abort
.callback
) {
1139 if (!cbdataReferenceValid(mem_obj
->abort
.data
))
1140 debugs(20, DBG_IMPORTANT
,HERE
<< "queueing event when abort.data is not valid");
1141 eventAdd("mem_obj->abort.callback",
1142 mem_obj
->abort
.callback
,
1143 mem_obj
->abort
.data
,
1149 /* XXX Should we reverse these two, so that there is no
1150 * unneeded disk swapping triggered?
1152 /* Notify the client side */
1155 // abort swap out, invalidating what was created so far (release follows)
1156 swapOutFileClose(StoreIOState::writerGone
);
1158 unlock(); /* unlock */
1162 * Clear Memory storage to accommodate the given object len
1165 storeGetMemSpace(int size
)
1167 PROF_start(storeGetMemSpace
);
1168 StoreEntry
*e
= NULL
;
1170 static time_t last_check
= 0;
1171 size_t pages_needed
;
1172 RemovalPurgeWalker
*walker
;
1174 if (squid_curtime
== last_check
) {
1175 PROF_stop(storeGetMemSpace
);
1179 last_check
= squid_curtime
;
1181 pages_needed
= (size
+ SM_PAGE_SIZE
-1) / SM_PAGE_SIZE
;
1183 if (mem_node::InUseCount() + pages_needed
< store_pages_max
) {
1184 PROF_stop(storeGetMemSpace
);
1188 debugs(20, 2, "storeGetMemSpace: Starting, need " << pages_needed
<<
1191 /* XXX what to set as max_scan here? */
1192 walker
= mem_policy
->PurgeInit(mem_policy
, 100000);
1194 while ((e
= walker
->Next(walker
))) {
1198 if (mem_node::InUseCount() + pages_needed
< store_pages_max
)
1202 walker
->Done(walker
);
1203 debugs(20, 3, "storeGetMemSpace stats:");
1204 debugs(20, 3, " " << std::setw(6) << hot_obj_count
<< " HOT objects");
1205 debugs(20, 3, " " << std::setw(6) << released
<< " were released");
1206 PROF_stop(storeGetMemSpace
);
1209 /* thunk through to Store::Root().maintain(). Note that this would be better still
1210 * if registered against the root store itself, but that requires more complex
1211 * update logic - bigger fish to fry first. Long term each store when
1212 * it becomes active will self register
1215 Store::Maintain(void *notused
)
1217 Store::Root().maintain();
1219 /* Reregister a maintain event .. */
1220 eventAdd("MaintainSwapSpace", Maintain
, NULL
, 1.0, 1);
1224 /* The maximum objects to scan for maintain storage space */
1225 #define MAINTAIN_MAX_SCAN 1024
1226 #define MAINTAIN_MAX_REMOVE 64
1229 * This routine is to be called by main loop in main.c.
1230 * It removes expired objects on only one bucket for each time called.
1232 * This should get called 1/s from main().
1235 StoreController::maintain()
1237 static time_t last_warn_time
= 0;
1239 PROF_start(storeMaintainSwapSpace
);
1240 swapDir
->maintain();
1242 /* this should be emitted by the oversize dir, not globally */
1244 if (Store::Root().currentSize() > Store::Root().maxSize()) {
1245 if (squid_curtime
- last_warn_time
> 10) {
1246 debugs(20, DBG_CRITICAL
, "WARNING: Disk space over limit: "
1247 << Store::Root().currentSize() / 1024.0 << " KB > "
1248 << (Store::Root().maxSize() >> 10) << " KB");
1249 last_warn_time
= squid_curtime
;
1253 PROF_stop(storeMaintainSwapSpace
);
1256 /* release an object from a cache */
1258 StoreEntry::release()
1260 PROF_start(storeRelease
);
1261 debugs(20, 3, "storeRelease: Releasing: '" << getMD5Text() << "'");
1262 /* If, for any reason we can't discard this object because of an
1263 * outstanding request, mark it for pending release */
1267 debugs(20, 3, "storeRelease: Only setting RELEASE_REQUEST bit");
1269 PROF_stop(storeRelease
);
1273 if (StoreController::store_dirs_rebuilding
&& swap_filen
> -1) {
1279 if (swap_filen
> -1) {
1281 * Fake a call to StoreEntry->lock() When rebuilding is done,
1282 * we'll just call StoreEntry->unlock() on these.
1286 LateReleaseStack
.push_back(this);
1288 destroyStoreEntry(static_cast<hash_link
*>(this));
1289 // "this" is no longer valid
1292 PROF_stop(storeRelease
);
1296 storeLog(STORE_LOG_RELEASE
, this);
1298 if (swap_filen
> -1) {
1299 // log before unlink() below clears swap_filen
1300 if (!EBIT_TEST(flags
, KEY_PRIVATE
))
1301 storeDirSwapLog(this, SWAP_LOG_DEL
);
1306 setMemStatus(NOT_IN_MEMORY
);
1307 destroyStoreEntry(static_cast<hash_link
*>(this));
1308 PROF_stop(storeRelease
);
1312 storeLateRelease(void *unused
)
1318 if (StoreController::store_dirs_rebuilding
) {
1319 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
1323 for (i
= 0; i
< 10; ++i
) {
1324 e
= LateReleaseStack
.count
? LateReleaseStack
.pop() : NULL
;
1328 debugs(20, DBG_IMPORTANT
, "storeLateRelease: released " << n
<< " objects");
1336 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 0.0, 1);
1339 /* return 1 if a store entry is locked */
1341 StoreEntry::locked() const
1346 if (swap_status
== SWAPOUT_WRITING
)
1349 if (store_status
== STORE_PENDING
)
1353 * SPECIAL, PUBLIC entries should be "locked"
1355 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
1356 if (!EBIT_TEST(flags
, KEY_PRIVATE
))
1363 StoreEntry::validLength() const
1366 const HttpReply
*reply
;
1367 assert(mem_obj
!= NULL
);
1369 debugs(20, 3, "storeEntryValidLength: Checking '" << getMD5Text() << "'");
1370 debugs(20, 5, "storeEntryValidLength: object_len = " <<
1372 debugs(20, 5, "storeEntryValidLength: hdr_sz = " << reply
->hdr_sz
);
1373 debugs(20, 5, "storeEntryValidLength: content_length = " << reply
->content_length
);
1375 if (reply
->content_length
< 0) {
1376 debugs(20, 5, "storeEntryValidLength: Unspecified content length: " << getMD5Text());
1380 if (reply
->hdr_sz
== 0) {
1381 debugs(20, 5, "storeEntryValidLength: Zero header size: " << getMD5Text());
1385 if (mem_obj
->method
== METHOD_HEAD
) {
1386 debugs(20, 5, "storeEntryValidLength: HEAD request: " << getMD5Text());
1390 if (reply
->sline
.status
== HTTP_NOT_MODIFIED
)
1393 if (reply
->sline
.status
== HTTP_NO_CONTENT
)
1396 diff
= reply
->hdr_sz
+ reply
->content_length
- objectLen();
1401 debugs(20, 3, "storeEntryValidLength: " << (diff
< 0 ? -diff
: diff
) << " bytes too " << (diff
< 0 ? "big" : "small") <<"; '" << getMD5Text() << "'" );
1407 storeRegisterWithCacheManager(void)
1409 Mgr::RegisterAction("storedir", "Store Directory Stats", Store::Stats
, 0, 1);
1410 Mgr::RegisterAction("store_io", "Store IO Interface Stats", &Mgr::StoreIoAction::Create
, 0, 1);
1411 Mgr::RegisterAction("store_check_cachable_stats", "storeCheckCachable() Stats",
1412 storeCheckCachableStats
, 0, 1);
1419 mem_policy
= createRemovalPolicy(Config
.memPolicy
);
1422 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
1423 Store::Root().init();
1424 storeRebuildStart();
1426 storeRegisterWithCacheManager();
1430 storeConfigure(void)
1432 store_swap_high
= (long) (((float) Store::Root().maxSize() *
1433 (float) Config
.Swap
.highWaterMark
) / (float) 100);
1434 store_swap_low
= (long) (((float) Store::Root().maxSize() *
1435 (float) Config
.Swap
.lowWaterMark
) / (float) 100);
1436 store_pages_max
= Config
.memMaxSize
/ sizeof(mem_node
);
1440 StoreEntry::memoryCachable() const
1442 if (mem_obj
== NULL
)
1445 if (mem_obj
->data_hdr
.size() == 0)
1448 if (mem_obj
->inmem_lo
!= 0)
1451 if (!Config
.onoff
.memory_cache_first
&& swap_status
== SWAPOUT_DONE
&& refcount
== 1)
1458 StoreEntry::checkNegativeHit() const
1460 if (!EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1463 if (expires
<= squid_curtime
)
1466 if (store_status
!= STORE_OK
)
1473 * Set object for negative caching.
1474 * Preserves any expiry information given by the server.
1475 * In absence of proper expiry info it will set to expire immediately,
1476 * or with HTTP-violations enabled the configured negative-TTL is observed
1479 StoreEntry::negativeCache()
1481 // XXX: should make the default for expires 0 instead of -1
1482 // so we can distinguish "Expires: -1" from nothing.
1484 #if USE_HTTP_VIOLATIONS
1485 expires
= squid_curtime
+ Config
.negativeTtl
;
1487 expires
= squid_curtime
;
1489 EBIT_SET(flags
, ENTRY_NEGCACHED
);
1493 storeFreeMemory(void)
1496 #if USE_CACHE_DIGESTS
1499 cacheDigestDestroy(store_digest
);
1503 store_digest
= NULL
;
1507 expiresMoreThan(time_t expires
, time_t when
)
1509 if (expires
< 0) /* No Expires given */
1512 return (expires
> (squid_curtime
+ when
));
1516 StoreEntry::validToSend() const
1518 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
1521 if (EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1522 if (expires
<= squid_curtime
)
1525 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
1532 StoreEntry::timestampsSet()
1534 const HttpReply
*reply
= getReply();
1535 time_t served_date
= reply
->date
;
1536 int age
= reply
->header
.getInt(HDR_AGE
);
1537 /* Compute the timestamp, mimicking RFC2616 section 13.2.3. */
1538 /* make sure that 0 <= served_date <= squid_curtime */
1540 if (served_date
< 0 || served_date
> squid_curtime
)
1541 served_date
= squid_curtime
;
1544 * If the returned Date: is more than 24 hours older than
1545 * the squid_curtime, then one of us needs to use NTP to set our
1546 * clock. We'll pretend that our clock is right.
1548 else if (served_date
< (squid_curtime
- 24 * 60 * 60) )
1549 served_date
= squid_curtime
;
1552 * Compensate with Age header if origin server clock is ahead
1553 * of us and there is a cache in between us and the origin
1554 * server. But DONT compensate if the age value is larger than
1555 * squid_curtime because it results in a negative served_date.
1557 if (age
> squid_curtime
- served_date
)
1558 if (squid_curtime
> age
)
1559 served_date
= squid_curtime
- age
;
1561 // compensate for Squid-to-server and server-to-Squid delays
1562 if (mem_obj
&& mem_obj
->request
) {
1563 const time_t request_sent
=
1564 mem_obj
->request
->hier
.peer_http_request_sent
.tv_sec
;
1565 if (0 < request_sent
&& request_sent
< squid_curtime
)
1566 served_date
-= (squid_curtime
- request_sent
);
1569 if (reply
->expires
> 0 && reply
->date
> -1)
1570 expires
= served_date
+ (reply
->expires
- reply
->date
);
1572 expires
= reply
->expires
;
1574 lastmod
= reply
->last_modified
;
1576 timestamp
= served_date
;
1580 StoreEntry::registerAbort(STABH
* cb
, void *data
)
1583 assert(mem_obj
->abort
.callback
== NULL
);
1584 mem_obj
->abort
.callback
= cb
;
1585 mem_obj
->abort
.data
= cbdataReference(data
);
1589 StoreEntry::unregisterAbort()
1592 if (mem_obj
->abort
.callback
) {
1593 mem_obj
->abort
.callback
= NULL
;
1594 cbdataReferenceDone(mem_obj
->abort
.data
);
1599 StoreEntry::dump(int l
) const
1601 debugs(20, l
, "StoreEntry->key: " << getMD5Text());
1602 debugs(20, l
, "StoreEntry->next: " << next
);
1603 debugs(20, l
, "StoreEntry->mem_obj: " << mem_obj
);
1604 debugs(20, l
, "StoreEntry->timestamp: " << timestamp
);
1605 debugs(20, l
, "StoreEntry->lastref: " << lastref
);
1606 debugs(20, l
, "StoreEntry->expires: " << expires
);
1607 debugs(20, l
, "StoreEntry->lastmod: " << lastmod
);
1608 debugs(20, l
, "StoreEntry->swap_file_sz: " << swap_file_sz
);
1609 debugs(20, l
, "StoreEntry->refcount: " << refcount
);
1610 debugs(20, l
, "StoreEntry->flags: " << storeEntryFlags(this));
1611 debugs(20, l
, "StoreEntry->swap_dirn: " << swap_dirn
);
1612 debugs(20, l
, "StoreEntry->swap_filen: " << swap_filen
);
1613 debugs(20, l
, "StoreEntry->lock_count: " << lock_count
);
1614 debugs(20, l
, "StoreEntry->mem_status: " << mem_status
);
1615 debugs(20, l
, "StoreEntry->ping_status: " << ping_status
);
1616 debugs(20, l
, "StoreEntry->store_status: " << store_status
);
1617 debugs(20, l
, "StoreEntry->swap_status: " << swap_status
);
1621 * NOTE, this function assumes only two mem states
1624 StoreEntry::setMemStatus(mem_status_t new_status
)
1626 if (new_status
== mem_status
)
1629 // are we using a shared memory cache?
1630 if (Config
.memShared
&& IamWorkerProcess()) {
1631 // enumerate calling cases if shared memory is enabled
1632 assert(new_status
!= IN_MEMORY
|| EBIT_TEST(flags
, ENTRY_SPECIAL
));
1633 // This method was designed to update replacement policy, not to
1634 // actually purge something from the memory cache (TODO: rename?).
1635 // Shared memory cache does not have a policy that needs updates.
1636 mem_status
= new_status
;
1640 assert(mem_obj
!= NULL
);
1642 if (new_status
== IN_MEMORY
) {
1643 assert(mem_obj
->inmem_lo
== 0);
1645 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1646 debugs(20, 4, "StoreEntry::setMemStatus: not inserting special " << mem_obj
->url
<< " into policy");
1648 mem_policy
->Add(mem_policy
, this, &mem_obj
->repl
);
1649 debugs(20, 4, "StoreEntry::setMemStatus: inserted mem node " << mem_obj
->url
<< " key: " << getMD5Text());
1652 ++hot_obj_count
; // TODO: maintain for the shared hot cache as well
1654 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1655 debugs(20, 4, "StoreEntry::setMemStatus: special entry " << mem_obj
->url
);
1657 mem_policy
->Remove(mem_policy
, this, &mem_obj
->repl
);
1658 debugs(20, 4, "StoreEntry::setMemStatus: removed mem node " << mem_obj
->url
);
1664 mem_status
= new_status
;
1668 StoreEntry::url() const
1671 return "[null_entry]";
1672 else if (mem_obj
== NULL
)
1673 return "[null_mem_obj]";
1675 return mem_obj
->url
;
1679 StoreEntry::createMemObject(const char *aUrl
, const char *aLogUrl
)
1684 if (hidden_mem_obj
) {
1685 debugs(20, 3, HERE
<< "restoring " << hidden_mem_obj
);
1686 mem_obj
= hidden_mem_obj
;
1687 hidden_mem_obj
= NULL
;
1688 mem_obj
->resetUrls(aUrl
, aLogUrl
);
1692 mem_obj
= new MemObject(aUrl
, aLogUrl
);
1695 /* this just sets DELAY_SENDING */
1697 StoreEntry::buffer()
1699 EBIT_SET(flags
, DELAY_SENDING
);
1702 /* this just clears DELAY_SENDING and Invokes the handlers */
1706 if (EBIT_TEST(flags
, DELAY_SENDING
)) {
1707 EBIT_CLR(flags
, DELAY_SENDING
);
1713 StoreEntry::objectLen() const
1715 assert(mem_obj
!= NULL
);
1716 return mem_obj
->object_sz
;
1720 StoreEntry::contentLen() const
1722 assert(mem_obj
!= NULL
);
1723 assert(getReply() != NULL
);
1724 return objectLen() - getReply()->hdr_sz
;
1728 StoreEntry::getReply () const
1730 if (NULL
== mem_obj
)
1733 return mem_obj
->getReply();
1740 debugs(20, 3, "StoreEntry::reset: " << url());
1742 HttpReply
*rep
= (HttpReply
*) getReply(); // bypass const
1744 expires
= lastmod
= timestamp
= -1;
1750 * This routine calls the SETUP routine for each fs type.
1751 * I don't know where the best place for this is, and I'm not going to shuffle
1752 * around large chunks of code right now (that can be done once its working.)
1761 * called to add another store removal policy module
1764 storeReplAdd(const char *type
, REMOVALPOLICYCREATE
* create
)
1768 /* find the number of currently known repl types */
1769 for (i
= 0; storerepl_list
&& storerepl_list
[i
].typestr
; ++i
) {
1770 if (strcmp(storerepl_list
[i
].typestr
, type
) == 0) {
1771 debugs(20, DBG_IMPORTANT
, "WARNING: Trying to load store replacement policy " << type
<< " twice.");
1776 /* add the new type */
1777 storerepl_list
= static_cast<storerepl_entry_t
*>(xrealloc(storerepl_list
, (i
+ 2) * sizeof(storerepl_entry_t
)));
1779 memset(&storerepl_list
[i
+ 1], 0, sizeof(storerepl_entry_t
));
1781 storerepl_list
[i
].typestr
= type
;
1783 storerepl_list
[i
].create
= create
;
1787 * Create a removal policy instance
1790 createRemovalPolicy(RemovalPolicySettings
* settings
)
1792 storerepl_entry_t
*r
;
1794 for (r
= storerepl_list
; r
&& r
->typestr
; ++r
) {
1795 if (strcmp(r
->typestr
, settings
->type
) == 0)
1796 return r
->create(settings
->args
);
1799 debugs(20, DBG_IMPORTANT
, "ERROR: Unknown policy " << settings
->type
);
1800 debugs(20, DBG_IMPORTANT
, "ERROR: Be sure to have set cache_replacement_policy");
1801 debugs(20, DBG_IMPORTANT
, "ERROR: and memory_replacement_policy in squid.conf!");
1802 fatalf("ERROR: Unknown policy %s\n", settings
->type
);
1803 return NULL
; /* NOTREACHED */
1808 storeSwapFileNumberSet(StoreEntry
* e
, sfileno filn
)
1810 if (e
->swap_file_number
== filn
)
1815 storeDirMapBitReset(e
->swap_file_number
);
1816 storeDirLRUDelete(e
);
1817 e
->swap_file_number
= -1;
1819 assert(-1 == e
->swap_file_number
);
1820 storeDirMapBitSet(e
->swap_file_number
= filn
);
1828 * Replace a store entry with
1829 * a new reply. This eats the reply.
1832 StoreEntry::replaceHttpReply(HttpReply
*rep
, bool andStartWriting
)
1834 debugs(20, 3, "StoreEntry::replaceHttpReply: " << url());
1837 debugs(20, DBG_CRITICAL
, "Attempt to replace object with no in-memory representation");
1841 mem_obj
->replaceHttpReply(rep
);
1843 if (andStartWriting
)
1848 StoreEntry::startWriting()
1852 /* TODO: when we store headers serparately remove the header portion */
1853 /* TODO: mark the length of the headers ? */
1854 /* We ONLY want the headers */
1855 packerToStoreInit(&p
, this);
1860 const HttpReply
*rep
= getReply();
1863 rep
->packHeadersInto(&p
);
1864 mem_obj
->markEndOfReplyHeaders();
1866 rep
->body
.packInto(&p
);
1872 StoreEntry::getSerialisedMetaData()
1874 StoreMeta
*tlv_list
= storeSwapMetaBuild(this);
1876 char *result
= storeSwapMetaPack(tlv_list
, &swap_hdr_sz
);
1877 storeSwapTLVFree(tlv_list
);
1878 assert (swap_hdr_sz
>= 0);
1879 mem_obj
->swap_hdr_sz
= (size_t) swap_hdr_sz
;
1884 StoreEntry::trimMemory(const bool preserveSwappable
)
1888 * Bug #1943. We must not let go any data for IN_MEMORY
1889 * objects. We have to wait until the mem_status changes.
1891 if (mem_status
== IN_MEMORY
)
1894 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
1895 return; // cannot trim because we do not load them again
1897 if (!preserveSwappable
) {
1898 if (mem_obj
->policyLowestOffsetToKeep(0) == 0) {
1903 * Its not swap-able, and we're about to delete a chunk,
1904 * so we must make it PRIVATE. This is tricky/ugly because
1905 * for the most part, we treat swapable == cachable here.
1908 mem_obj
->trimUnSwappable ();
1910 mem_obj
->trimSwappable ();
1915 StoreEntry::modifiedSince(HttpRequest
* request
) const
1918 time_t mod_time
= lastmod
;
1921 mod_time
= timestamp
;
1923 debugs(88, 3, "modifiedSince: '" << url() << "'");
1925 debugs(88, 3, "modifiedSince: mod_time = " << mod_time
);
1930 /* Find size of the object */
1931 object_length
= getReply()->content_length
;
1933 if (object_length
< 0)
1934 object_length
= contentLen();
1936 if (mod_time
> request
->ims
) {
1937 debugs(88, 3, "--> YES: entry newer than client");
1939 } else if (mod_time
< request
->ims
) {
1940 debugs(88, 3, "--> NO: entry older than client");
1942 } else if (request
->imslen
< 0) {
1943 debugs(88, 3, "--> NO: same LMT, no client length");
1945 } else if (request
->imslen
== object_length
) {
1946 debugs(88, 3, "--> NO: same LMT, same length");
1949 debugs(88, 3, "--> YES: same LMT, different length");
1955 StoreEntry::hasIfMatchEtag(const HttpRequest
&request
) const
1957 const String reqETags
= request
.header
.getList(HDR_IF_MATCH
);
1958 return hasOneOfEtags(reqETags
, false);
1962 StoreEntry::hasIfNoneMatchEtag(const HttpRequest
&request
) const
1964 const String reqETags
= request
.header
.getList(HDR_IF_NONE_MATCH
);
1965 // weak comparison is allowed only for HEAD or full-body GET requests
1966 const bool allowWeakMatch
= !request
.flags
.range
&&
1967 (request
.method
== METHOD_GET
|| request
.method
== METHOD_HEAD
);
1968 return hasOneOfEtags(reqETags
, allowWeakMatch
);
1971 /// whether at least one of the request ETags matches entity ETag
1973 StoreEntry::hasOneOfEtags(const String
&reqETags
, const bool allowWeakMatch
) const
1975 const ETag repETag
= getReply()->header
.getETag(HDR_ETAG
);
1977 return strListIsMember(&reqETags
, "*", ',');
1979 bool matched
= false;
1980 const char *pos
= NULL
;
1983 while (!matched
&& strListGetItem(&reqETags
, ',', &item
, &ilen
, &pos
)) {
1984 if (!strncmp(item
, "*", ilen
))
1988 str
.append(item
, ilen
);
1990 if (etagParseInit(&reqETag
, str
.termedBuf())) {
1991 matched
= allowWeakMatch
? etagIsWeakEqual(repETag
, reqETag
) :
1992 etagIsStrongEqual(repETag
, reqETag
);
2000 StoreEntry::store() const
2002 assert(0 <= swap_dirn
&& swap_dirn
< Config
.cacheSwap
.n_configured
);
2003 return INDEXSD(swap_dirn
);
2007 StoreEntry::unlink()
2009 store()->unlink(*this); // implies disconnect()
2012 swap_status
= SWAPOUT_NONE
;
2016 * return true if the entry is in a state where
2017 * it can accept more data (ie with write() method)
2020 StoreEntry::isAccepting() const
2022 if (STORE_PENDING
!= store_status
)
2025 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
2031 std::ostream
&operator <<(std::ostream
&os
, const StoreEntry
&e
)
2033 return os
<< e
.swap_filen
<< '@' << e
.swap_dirn
<< '=' <<
2034 e
.mem_status
<< '/' << e
.ping_status
<< '/' << e
.store_status
<< '/' <<
2038 /* NullStoreEntry */
2040 NullStoreEntry
NullStoreEntry::_instance
;
2043 NullStoreEntry::getInstance()
2049 NullStoreEntry::getMD5Text() const
2055 NullStoreEntry::operator delete(void*)
2057 fatal ("Attempt to delete NullStoreEntry\n");
2061 NullStoreEntry::getSerialisedMetaData()
2067 #include "Store.cci"