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.
36 #include "squid-old.h"
37 #include "CacheManager.h"
38 #include "comm/Connection.h"
43 #include "mgr/Registration.h"
44 #include "StoreClient.h"
46 #include "HttpReply.h"
47 #include "HttpRequest.h"
48 #include "MemObject.h"
50 #include "StatCounters.h"
51 #include "StoreMeta.h"
53 #include "StoreIOState.h"
55 #include "DelayPools.h"
58 #include "SquidTime.h"
59 #include "swap_log_op.h"
60 #include "mgr/StoreIoAction.h"
62 static STMCB storeWriteComplete
;
64 #define REBUILD_TIMESTAMP_DELTA_MAX 2
66 #define STORE_IN_MEM_BUCKETS (229)
69 /** \todo Convert these string constants to enum string-arrays generated */
71 const char *memStatusStr
[] = {
76 const char *pingStatusStr
[] = {
82 const char *storeStatusStr
[] = {
87 const char *swapStatusStr
[] = {
95 * This defines an repl type
98 typedef struct _storerepl_entry storerepl_entry_t
;
100 struct _storerepl_entry
{
102 REMOVALPOLICYCREATE
*create
;
105 static storerepl_entry_t
*storerepl_list
= NULL
;
109 * local function prototypes
111 static int getKeyCounter(void);
112 static OBJH storeCheckCachableStats
;
113 static EVH storeLateRelease
;
118 static Stack
<StoreEntry
*> LateReleaseStack
;
119 MemAllocator
*StoreEntry::pool
= NULL
;
121 StorePointer
Store::CurrentRoot
= NULL
;
124 Store::Root(Store
* aRoot
)
130 Store::Root(StorePointer aRoot
)
132 Root(aRoot
.getRaw());
136 Store::Stats(StoreEntry
* output
)
139 Root().stat(*output
);
155 Store::unlink (StoreEntry
&anEntry
)
157 fatal("Store::unlink on invalid Store\n");
161 StoreEntry::operator new (size_t bytecount
)
163 assert (bytecount
== sizeof (StoreEntry
));
166 pool
= memPoolCreate ("StoreEntry", bytecount
);
167 pool
->setChunkSize(2048 * 1024);
170 return pool
->alloc();
174 StoreEntry::operator delete (void *address
)
176 pool
->freeOne(address
);
180 StoreEntry::makePublic()
182 /* This object can be cached for a long time */
184 if (EBIT_TEST(flags
, ENTRY_CACHABLE
))
189 StoreEntry::makePrivate()
191 /* This object should never be cached at all */
193 releaseRequest(); /* delete object when not used */
194 /* releaseRequest clears ENTRY_CACHABLE flag */
198 StoreEntry::cacheNegatively()
200 /* This object may be negatively cached */
203 if (EBIT_TEST(flags
, ENTRY_CACHABLE
))
208 StoreEntry::inUseCount()
212 return pool
->getInUseCount();
216 StoreEntry::getMD5Text() const
218 return storeKeyText((const cache_key
*)key
);
224 StoreEntry::DeferReader(void *theContext
, CommRead
const &aRead
)
226 StoreEntry
*anEntry
= (StoreEntry
*)theContext
;
227 anEntry
->delayAwareRead(aRead
.conn
,
234 StoreEntry::delayAwareRead(const Comm::ConnectionPointer
&conn
, char *buf
, int len
, AsyncCall::Pointer callback
)
236 size_t amountToRead
= bytesWanted(Range
<size_t>(0, len
));
237 /* sketch: readdeferer* = getdeferer.
238 * ->deferRead (fd, buf, len, callback, DelayAwareRead, this)
241 if (amountToRead
== 0) {
243 /* read ahead limit */
244 /* Perhaps these two calls should both live in MemObject */
246 if (!mem_obj
->readAheadPolicyCanRead()) {
248 mem_obj
->delayRead(DeferredRead(DeferReader
, this, CommRead(conn
, buf
, len
, callback
)));
254 mem_obj
->mostBytesAllowed().delayRead(DeferredRead(DeferReader
, this, CommRead(conn
, buf
, len
, callback
)));
261 if (fd_table
[conn
->fd
].closing()) {
262 // Readers must have closing callbacks if they want to be notified. No
263 // readers appeared to care around 2009/12/14 as they skipped reading
264 // for other reasons. Closing may already be true at the delyaAwareRead
265 // call time or may happen while we wait after delayRead() above.
266 debugs(20, 3, HERE
<< "wont read from closing " << conn
<< " for " <<
268 return; // the read callback will never be called
271 comm_read(conn
, buf
, amountToRead
, callback
);
275 StoreEntry::bytesWanted (Range
<size_t> const aRange
, bool ignoreDelayPools
) const
280 #if URL_CHECKSUM_DEBUG
282 mem_obj
->checkUrlChecksum();
286 if (!mem_obj
->readAheadPolicyCanRead())
289 return mem_obj
->mostBytesWanted(aRange
.end
, ignoreDelayPools
);
293 StoreEntry::checkDeferRead(int fd
) const
295 return (bytesWanted(Range
<size_t>(0,INT_MAX
)) == 0);
299 StoreEntry::setNoDelay (bool const newValue
)
302 mem_obj
->setNoDelay(newValue
);
306 StoreEntry::storeClientType() const
308 /* The needed offset isn't in memory
309 * XXX TODO: this is wrong for range requests
310 * as the needed offset may *not* be 0, AND
311 * offset 0 in the memory object is the HTTP headers.
314 if (mem_status
== IN_MEMORY
&& Config
.memShared
&& IamWorkerProcess()) {
315 // clients of an object cached in shared memory are memory clients
316 return STORE_MEM_CLIENT
;
321 if (mem_obj
->inmem_lo
)
322 return STORE_DISK_CLIENT
;
324 if (EBIT_TEST(flags
, ENTRY_ABORTED
)) {
325 /* I don't think we should be adding clients to aborted entries */
326 debugs(20, DBG_IMPORTANT
, "storeClientType: adding to ENTRY_ABORTED entry");
327 return STORE_MEM_CLIENT
;
330 if (store_status
== STORE_OK
) {
331 /* the object has completed. */
333 if (mem_obj
->inmem_lo
== 0 && !isEmpty()) {
334 if (swap_status
== SWAPOUT_DONE
) {
335 debugs(20,7, HERE
<< mem_obj
<< " lo: " << mem_obj
->inmem_lo
<< " hi: " << mem_obj
->endOffset() << " size: " << mem_obj
->object_sz
);
336 if (mem_obj
->endOffset() == mem_obj
->object_sz
) {
337 /* hot object fully swapped in */
338 return STORE_MEM_CLIENT
;
341 /* Memory-only, or currently being swapped out */
342 return STORE_MEM_CLIENT
;
345 return STORE_DISK_CLIENT
;
348 /* here and past, entry is STORE_PENDING */
350 * If this is the first client, let it be the mem client
352 if (mem_obj
->nclients
== 1)
353 return STORE_MEM_CLIENT
;
356 * If there is no disk file to open yet, we must make this a
357 * mem client. If we can't open the swapin file before writing
358 * to the client, there is no guarantee that we will be able
359 * to open it later when we really need it.
361 if (swap_status
== SWAPOUT_NONE
)
362 return STORE_MEM_CLIENT
;
365 * otherwise, make subsequent clients read from disk so they
366 * can not delay the first, and vice-versa.
368 return STORE_DISK_CLIENT
;
371 StoreEntry::StoreEntry():
372 hidden_mem_obj(NULL
),
375 debugs(20, 3, HERE
<< "new StoreEntry " << this);
378 expires
= lastmod
= lastref
= timestamp
= -1;
380 swap_status
= SWAPOUT_NONE
;
385 StoreEntry::StoreEntry(const char *aUrl
, const char *aLogUrl
):
386 hidden_mem_obj(NULL
),
389 debugs(20, 3, HERE
<< "new StoreEntry " << this);
390 mem_obj
= new MemObject(aUrl
, aLogUrl
);
392 expires
= lastmod
= lastref
= timestamp
= -1;
394 swap_status
= SWAPOUT_NONE
;
399 StoreEntry::~StoreEntry()
401 if (swap_filen
>= 0) {
402 SwapDir
&sd
= dynamic_cast<SwapDir
&>(*store());
403 sd
.disconnect(*this);
405 delete hidden_mem_obj
;
410 StoreEntry::deferProducer(const AsyncCall::Pointer
&producer
)
412 if (!deferredProducer
)
413 deferredProducer
= producer
;
415 debugs(20, 5, HERE
<< "Deferred producer call is allready set to: " <<
416 *deferredProducer
<< ", requested call: " << *producer
);
420 StoreEntry::kickProducer()
422 if (deferredProducer
!= NULL
) {
423 ScheduleCallHere(deferredProducer
);
424 deferredProducer
= NULL
;
430 StoreEntry::destroyMemObject()
432 debugs(20, 3, HERE
<< "destroyMemObject " << mem_obj
);
433 setMemStatus(NOT_IN_MEMORY
);
434 MemObject
*mem
= mem_obj
;
437 delete hidden_mem_obj
;
438 hidden_mem_obj
= NULL
;
442 StoreEntry::hideMemObject()
444 debugs(20, 3, HERE
<< "hiding " << mem_obj
);
446 assert(!hidden_mem_obj
);
447 hidden_mem_obj
= mem_obj
;
452 destroyStoreEntry(void *data
)
454 debugs(20, 3, HERE
<< "destroyStoreEntry: destroying " << data
);
455 StoreEntry
*e
= static_cast<StoreEntry
*>(static_cast<hash_link
*>(data
));
458 if (e
== NullStoreEntry::getInstance())
461 e
->destroyMemObject();
465 assert(e
->key
== NULL
);
470 /* ----- INTERFACE BETWEEN STORAGE MANAGER AND HASH TABLE FUNCTIONS --------- */
473 StoreEntry::hashInsert(const cache_key
* someKey
)
475 debugs(20, 3, "StoreEntry::hashInsert: Inserting Entry " << this << " key '" << storeKeyText(someKey
) << "'");
476 key
= storeKeyDup(someKey
);
477 hash_join(store_table
, this);
481 StoreEntry::hashDelete()
483 hash_remove_link(store_table
, this);
484 storeKeyFree((const cache_key
*)key
);
488 /* -------------------------------------------------------------------------- */
491 /* get rid of memory copy of the object */
493 StoreEntry::purgeMem()
498 debugs(20, 3, "StoreEntry::purgeMem: Freeing memory-copy of " << getMD5Text());
502 if (swap_status
!= SWAPOUT_DONE
)
506 /* RBC 20050104 this is wrong- memory ref counting
507 * is not at all equivalent to the store 'usage' concept
508 * which the replacement policies should be acting upon.
509 * specifically, object iteration within stores needs
510 * memory ref counting to prevent race conditions,
511 * but this should not influence store replacement.
518 debugs(20, 3, "StoreEntry::lock: key '" << getMD5Text() <<"' count=" <<
520 lastref
= squid_curtime
;
521 Store::Root().reference(*this);
525 StoreEntry::setReleaseFlag()
527 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
530 debugs(20, 3, "StoreEntry::setReleaseFlag: '" << getMD5Text() << "'");
532 EBIT_SET(flags
, RELEASE_REQUEST
);
536 StoreEntry::releaseRequest()
538 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
544 * Clear cachable flag here because we might get called before
545 * anyone else even looks at the cachability flag. Also, this
546 * prevents httpMakePublic from really setting a public key.
548 EBIT_CLR(flags
, ENTRY_CACHABLE
);
553 /* unlock object, return -1 if object get released after unlock
554 * otherwise lock_count */
559 debugs(20, 3, "StoreEntry::unlock: key '" << getMD5Text() << "' count=" << lock_count
);
562 return (int) lock_count
;
564 if (store_status
== STORE_PENDING
)
567 assert(storePendingNClients(this) == 0);
569 if (EBIT_TEST(flags
, RELEASE_REQUEST
)) {
574 if (EBIT_TEST(flags
, KEY_PRIVATE
))
575 debugs(20, DBG_IMPORTANT
, "WARNING: " << __FILE__
<< ":" << __LINE__
<< ": found KEY_PRIVATE");
577 Store::Root().handleIdleEntry(*this); // may delete us
582 StoreEntry::getPublicByRequestMethod (StoreClient
*aClient
, HttpRequest
* request
, const HttpRequestMethod
& method
)
585 StoreEntry
*result
= storeGetPublicByRequestMethod( request
, method
);
588 aClient
->created (NullStoreEntry::getInstance());
590 aClient
->created (result
);
594 StoreEntry::getPublicByRequest (StoreClient
*aClient
, HttpRequest
* request
)
597 StoreEntry
*result
= storeGetPublicByRequest (request
);
600 result
= NullStoreEntry::getInstance();
602 aClient
->created (result
);
606 StoreEntry::getPublic (StoreClient
*aClient
, const char *uri
, const HttpRequestMethod
& method
)
609 StoreEntry
*result
= storeGetPublic (uri
, method
);
612 result
= NullStoreEntry::getInstance();
614 aClient
->created (result
);
618 storeGetPublic(const char *uri
, const HttpRequestMethod
& method
)
620 return Store::Root().get(storeKeyPublic(uri
, method
));
624 storeGetPublicByRequestMethod(HttpRequest
* req
, const HttpRequestMethod
& method
)
626 return Store::Root().get(storeKeyPublicByRequestMethod(req
, method
));
630 storeGetPublicByRequest(HttpRequest
* req
)
632 StoreEntry
*e
= storeGetPublicByRequestMethod(req
, req
->method
);
634 if (e
== NULL
&& req
->method
== METHOD_HEAD
)
635 /* We can generate a HEAD reply from a cached GET object */
636 e
= storeGetPublicByRequestMethod(req
, METHOD_GET
);
644 static int key_counter
= 0;
646 if (++key_counter
< 0)
652 /* RBC 20050104 AFAICT this should become simpler:
653 * rather than reinserting with a special key it should be marked
654 * as 'released' and then cleaned up when refcounting indicates.
655 * the StoreHashIndex could well implement its 'released' in the
657 * Also, clean log writing should skip over ia,t
658 * Otherwise, we need a 'remove from the index but not the store
662 StoreEntry::setPrivateKey()
664 const cache_key
*newkey
;
666 if (key
&& EBIT_TEST(flags
, KEY_PRIVATE
))
667 return; /* is already private */
671 storeDirSwapLog(this, SWAP_LOG_DEL
);
676 if (mem_obj
!= NULL
) {
677 mem_obj
->id
= getKeyCounter();
678 newkey
= storeKeyPrivate(mem_obj
->url
, mem_obj
->method
, mem_obj
->id
);
680 newkey
= storeKeyPrivate("JUNK", METHOD_NONE
, getKeyCounter());
683 assert(hash_lookup(store_table
, newkey
) == NULL
);
684 EBIT_SET(flags
, KEY_PRIVATE
);
689 StoreEntry::setPublicKey()
691 StoreEntry
*e2
= NULL
;
692 const cache_key
*newkey
;
694 if (key
&& !EBIT_TEST(flags
, KEY_PRIVATE
))
695 return; /* is already public */
700 * We can't make RELEASE_REQUEST objects public. Depending on
701 * when RELEASE_REQUEST gets set, we might not be swapping out
702 * the object. If we're not swapping out, then subsequent
703 * store clients won't be able to access object data which has
704 * been freed from memory.
706 * If RELEASE_REQUEST is set, then ENTRY_CACHABLE should not
707 * be set, and StoreEntry::setPublicKey() should not be called.
709 #if MORE_DEBUG_OUTPUT
711 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
712 debugs(20, DBG_IMPORTANT
, "assertion failed: RELEASE key " << key
<< ", url " << mem_obj
->url
);
716 assert(!EBIT_TEST(flags
, RELEASE_REQUEST
));
718 if (mem_obj
->request
) {
719 HttpRequest
*request
= mem_obj
->request
;
721 if (!mem_obj
->vary_headers
) {
722 /* First handle the case where the object no longer varies */
723 safe_free(request
->vary_headers
);
725 if (request
->vary_headers
&& strcmp(request
->vary_headers
, mem_obj
->vary_headers
) != 0) {
726 /* Oops.. the variance has changed. Kill the base object
727 * to record the new variance key
729 safe_free(request
->vary_headers
); /* free old "bad" variance key */
730 StoreEntry
*pe
= storeGetPublic(mem_obj
->url
, mem_obj
->method
);
736 /* Make sure the request knows the variance status */
737 if (!request
->vary_headers
) {
738 const char *vary
= httpMakeVaryMark(request
, mem_obj
->getReply());
741 request
->vary_headers
= xstrdup(vary
);
745 // TODO: storeGetPublic() calls below may create unlocked entries.
746 // We should add/use storeHas() API or lock/unlock those entries.
747 if (mem_obj
->vary_headers
&& !storeGetPublic(mem_obj
->url
, mem_obj
->method
)) {
748 /* Create "vary" base object */
750 StoreEntry
*pe
= storeCreateEntry(mem_obj
->url
, mem_obj
->log_url
, request
->flags
, request
->method
);
751 /* We are allowed to do this typecast */
752 HttpReply
*rep
= new HttpReply
;
753 rep
->setHeaders(HTTP_OK
, "Internal marker object", "x-squid-internal/vary", -1, -1, squid_curtime
+ 100000);
754 vary
= mem_obj
->getReply()->header
.getList(HDR_VARY
);
757 /* Again, we own this structure layout */
758 rep
->header
.putStr(HDR_VARY
, vary
.termedBuf());
762 #if X_ACCELERATOR_VARY
763 vary
= mem_obj
->getReply()->header
.getList(HDR_X_ACCELERATOR_VARY
);
765 if (vary
.defined()) {
766 /* Again, we own this structure layout */
767 rep
->header
.putStr(HDR_X_ACCELERATOR_VARY
, vary
.termedBuf());
772 pe
->replaceHttpReply(rep
);
783 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
785 newkey
= storeKeyPublic(mem_obj
->url
, mem_obj
->method
);
787 if ((e2
= (StoreEntry
*) hash_lookup(store_table
, newkey
))) {
788 debugs(20, 3, "StoreEntry::setPublicKey: Making old '" << mem_obj
->url
<< "' private.");
792 if (mem_obj
->request
)
793 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
795 newkey
= storeKeyPublic(mem_obj
->url
, mem_obj
->method
);
801 EBIT_CLR(flags
, KEY_PRIVATE
);
806 storeDirSwapLog(this, SWAP_LOG_ADD
);
810 storeCreateEntry(const char *url
, const char *log_url
, request_flags flags
, const HttpRequestMethod
& method
)
812 StoreEntry
*e
= NULL
;
813 MemObject
*mem
= NULL
;
814 debugs(20, 3, "storeCreateEntry: '" << url
<< "'");
816 e
= new StoreEntry(url
, log_url
);
817 e
->lock_count
= 1; /* Note lock here w/o calling storeLock() */
819 mem
->method
= method
;
821 if (neighbors_do_private_keys
|| !flags
.hierarchical
)
826 if (flags
.cachable
) {
827 EBIT_SET(e
->flags
, ENTRY_CACHABLE
);
828 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
830 /* StoreEntry::releaseRequest() clears ENTRY_CACHABLE */
834 e
->store_status
= STORE_PENDING
;
835 e
->setMemStatus(NOT_IN_MEMORY
);
837 e
->lastref
= squid_curtime
;
838 e
->timestamp
= -1; /* set in StoreEntry::timestampsSet() */
839 e
->ping_status
= PING_NONE
;
840 EBIT_SET(e
->flags
, ENTRY_VALIDATED
);
844 /* Mark object as expired */
846 StoreEntry::expireNow()
848 debugs(20, 3, "StoreEntry::expireNow: '" << getMD5Text() << "'");
849 expires
= squid_curtime
;
853 storeWriteComplete (void *data
, StoreIOBuffer wroteBuffer
)
855 PROF_start(storeWriteComplete
);
856 StoreEntry
*e
= (StoreEntry
*)data
;
858 if (EBIT_TEST(e
->flags
, DELAY_SENDING
)) {
859 PROF_stop(storeWriteComplete
);
864 PROF_stop(storeWriteComplete
);
868 StoreEntry::write (StoreIOBuffer writeBuffer
)
870 assert(mem_obj
!= NULL
);
871 /* This assert will change when we teach the store to update */
872 PROF_start(StoreEntry_write
);
873 assert(store_status
== STORE_PENDING
);
875 debugs(20, 5, "storeWrite: writing " << writeBuffer
.length
<< " bytes for '" << getMD5Text() << "'");
876 PROF_stop(StoreEntry_write
);
877 storeGetMemSpace(writeBuffer
.length
);
878 mem_obj
->write (writeBuffer
, storeWriteComplete
, this);
881 /* Append incoming data from a primary server to an entry. */
883 StoreEntry::append(char const *buf
, int len
)
885 assert(mem_obj
!= NULL
);
887 assert(store_status
== STORE_PENDING
);
889 StoreIOBuffer tempBuffer
;
890 tempBuffer
.data
= (char *)buf
;
891 tempBuffer
.length
= len
;
893 * XXX sigh, offset might be < 0 here, but it gets "corrected"
894 * later. This offset crap is such a mess.
896 tempBuffer
.offset
= mem_obj
->endOffset() - (getReply() ? getReply()->hdr_sz
: 0);
902 storeAppendPrintf(StoreEntry
* e
, const char *fmt
,...)
907 storeAppendVPrintf(e
, fmt
, args
);
911 /* used be storeAppendPrintf and Packer */
913 storeAppendVPrintf(StoreEntry
* e
, const char *fmt
, va_list vargs
)
915 LOCAL_ARRAY(char, buf
, 4096);
917 vsnprintf(buf
, 4096, fmt
, vargs
);
918 e
->append(buf
, strlen(buf
));
921 struct _store_check_cachable_hist
{
925 int not_entry_cachable
;
926 int wrong_content_length
;
931 int too_many_open_files
;
932 int too_many_open_fds
;
938 } store_check_cachable_hist
;
941 storeTooManyDiskFilesOpen(void)
943 if (Config
.max_open_disk_fds
== 0)
946 if (store_open_disk_fd
> Config
.max_open_disk_fds
)
953 StoreEntry::checkTooSmall()
955 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
958 if (STORE_OK
== store_status
)
959 if (mem_obj
->object_sz
< 0 ||
960 mem_obj
->object_sz
< Config
.Store
.minObjectSize
)
962 if (getReply()->content_length
> -1)
963 if (getReply()->content_length
< Config
.Store
.minObjectSize
)
968 // TODO: remove checks already performed by swapoutPossible()
969 // TODO: move "too many open..." checks outside -- we are called too early/late
971 StoreEntry::checkCachable()
973 #if CACHE_ALL_METHODS
975 if (mem_obj
->method
!= METHOD_GET
) {
976 debugs(20, 2, "StoreEntry::checkCachable: NO: non-GET method");
977 ++store_check_cachable_hist
.no
.non_get
;
980 if (store_status
== STORE_OK
&& EBIT_TEST(flags
, ENTRY_BAD_LENGTH
)) {
981 debugs(20, 2, "StoreEntry::checkCachable: NO: wrong content-length");
982 ++store_check_cachable_hist
.no
.wrong_content_length
;
983 } else if (!EBIT_TEST(flags
, ENTRY_CACHABLE
)) {
984 debugs(20, 2, "StoreEntry::checkCachable: NO: not cachable");
985 ++store_check_cachable_hist
.no
.not_entry_cachable
;
986 } else if (EBIT_TEST(flags
, ENTRY_NEGCACHED
)) {
987 debugs(20, 3, "StoreEntry::checkCachable: NO: negative cached");
988 ++store_check_cachable_hist
.no
.negative_cached
;
989 return 0; /* avoid release call below */
990 } else if ((getReply()->content_length
> 0 &&
991 getReply()->content_length
992 > Config
.Store
.maxObjectSize
) ||
993 mem_obj
->endOffset() > Config
.Store
.maxObjectSize
) {
994 debugs(20, 2, "StoreEntry::checkCachable: NO: too big");
995 ++store_check_cachable_hist
.no
.too_big
;
996 } else if (getReply()->content_length
> Config
.Store
.maxObjectSize
) {
997 debugs(20, 2, "StoreEntry::checkCachable: NO: too big");
998 ++store_check_cachable_hist
.no
.too_big
;
999 } else if (checkTooSmall()) {
1000 debugs(20, 2, "StoreEntry::checkCachable: NO: too small");
1001 ++store_check_cachable_hist
.no
.too_small
;
1002 } else if (EBIT_TEST(flags
, KEY_PRIVATE
)) {
1003 debugs(20, 3, "StoreEntry::checkCachable: NO: private key");
1004 ++store_check_cachable_hist
.no
.private_key
;
1005 } else if (swap_status
!= SWAPOUT_NONE
) {
1007 * here we checked the swap_status because the remaining
1008 * cases are only relevant only if we haven't started swapping
1009 * out the object yet.
1012 } else if (storeTooManyDiskFilesOpen()) {
1013 debugs(20, 2, "StoreEntry::checkCachable: NO: too many disk files open");
1014 ++store_check_cachable_hist
.no
.too_many_open_files
;
1015 } else if (fdNFree() < RESERVED_FD
) {
1016 debugs(20, 2, "StoreEntry::checkCachable: NO: too many FD's open");
1017 ++store_check_cachable_hist
.no
.too_many_open_fds
;
1019 ++store_check_cachable_hist
.yes
.Default
;
1024 /* StoreEntry::releaseRequest() cleared ENTRY_CACHABLE */
1029 storeCheckCachableStats(StoreEntry
*sentry
)
1031 storeAppendPrintf(sentry
, "Category\t Count\n");
1033 #if CACHE_ALL_METHODS
1035 storeAppendPrintf(sentry
, "no.non_get\t%d\n",
1036 store_check_cachable_hist
.no
.non_get
);
1039 storeAppendPrintf(sentry
, "no.not_entry_cachable\t%d\n",
1040 store_check_cachable_hist
.no
.not_entry_cachable
);
1041 storeAppendPrintf(sentry
, "no.wrong_content_length\t%d\n",
1042 store_check_cachable_hist
.no
.wrong_content_length
);
1043 storeAppendPrintf(sentry
, "no.negative_cached\t%d\n",
1044 store_check_cachable_hist
.no
.negative_cached
);
1045 storeAppendPrintf(sentry
, "no.too_big\t%d\n",
1046 store_check_cachable_hist
.no
.too_big
);
1047 storeAppendPrintf(sentry
, "no.too_small\t%d\n",
1048 store_check_cachable_hist
.no
.too_small
);
1049 storeAppendPrintf(sentry
, "no.private_key\t%d\n",
1050 store_check_cachable_hist
.no
.private_key
);
1051 storeAppendPrintf(sentry
, "no.too_many_open_files\t%d\n",
1052 store_check_cachable_hist
.no
.too_many_open_files
);
1053 storeAppendPrintf(sentry
, "no.too_many_open_fds\t%d\n",
1054 store_check_cachable_hist
.no
.too_many_open_fds
);
1055 storeAppendPrintf(sentry
, "yes.default\t%d\n",
1056 store_check_cachable_hist
.yes
.Default
);
1060 StoreEntry::complete()
1062 debugs(20, 3, "storeComplete: '" << getMD5Text() << "'");
1064 if (store_status
!= STORE_PENDING
) {
1066 * if we're not STORE_PENDING, then probably we got aborted
1067 * and there should be NO clients on this entry
1069 assert(EBIT_TEST(flags
, ENTRY_ABORTED
));
1070 assert(mem_obj
->nclients
== 0);
1074 /* This is suspect: mem obj offsets include the headers. do we adjust for that
1075 * in use of object_sz?
1077 mem_obj
->object_sz
= mem_obj
->endOffset();
1079 store_status
= STORE_OK
;
1081 assert(mem_status
== NOT_IN_MEMORY
);
1083 if (!validLength()) {
1084 EBIT_SET(flags
, ENTRY_BAD_LENGTH
);
1088 #if USE_CACHE_DIGESTS
1089 if (mem_obj
->request
)
1090 mem_obj
->request
->hier
.store_complete_stop
= current_time
;
1094 * We used to call invokeHandlers, then storeSwapOut. However,
1095 * Madhukar Reddy <myreddy@persistence.com> reported that
1096 * responses without content length would sometimes get released
1097 * in client_side, thinking that the response is incomplete.
1103 * Someone wants to abort this transfer. Set the reason in the
1104 * request structure, call the server-side callback and mark the
1105 * entry for releasing
1110 ++statCounter
.aborted_requests
;
1111 assert(store_status
== STORE_PENDING
);
1112 assert(mem_obj
!= NULL
);
1113 debugs(20, 6, "storeAbort: " << getMD5Text());
1115 lock(); /* lock while aborting */
1120 EBIT_SET(flags
, ENTRY_ABORTED
);
1122 setMemStatus(NOT_IN_MEMORY
);
1124 store_status
= STORE_OK
;
1126 /* Notify the server side */
1130 * Should we check abort.data for validity?
1132 if (mem_obj
->abort
.callback
) {
1133 if (!cbdataReferenceValid(mem_obj
->abort
.data
))
1134 debugs(20, DBG_IMPORTANT
,HERE
<< "queueing event when abort.data is not valid");
1135 eventAdd("mem_obj->abort.callback",
1136 mem_obj
->abort
.callback
,
1137 mem_obj
->abort
.data
,
1143 /* XXX Should we reverse these two, so that there is no
1144 * unneeded disk swapping triggered?
1146 /* Notify the client side */
1149 // abort swap out, invalidating what was created so far (release follows)
1150 swapOutFileClose(StoreIOState::writerGone
);
1152 unlock(); /* unlock */
1156 * Clear Memory storage to accommodate the given object len
1159 storeGetMemSpace(int size
)
1161 PROF_start(storeGetMemSpace
);
1162 StoreEntry
*e
= NULL
;
1164 static time_t last_check
= 0;
1165 size_t pages_needed
;
1166 RemovalPurgeWalker
*walker
;
1168 if (squid_curtime
== last_check
) {
1169 PROF_stop(storeGetMemSpace
);
1173 last_check
= squid_curtime
;
1175 pages_needed
= (size
+ SM_PAGE_SIZE
-1) / SM_PAGE_SIZE
;
1177 if (mem_node::InUseCount() + pages_needed
< store_pages_max
) {
1178 PROF_stop(storeGetMemSpace
);
1182 debugs(20, 2, "storeGetMemSpace: Starting, need " << pages_needed
<<
1185 /* XXX what to set as max_scan here? */
1186 walker
= mem_policy
->PurgeInit(mem_policy
, 100000);
1188 while ((e
= walker
->Next(walker
))) {
1192 if (mem_node::InUseCount() + pages_needed
< store_pages_max
)
1196 walker
->Done(walker
);
1197 debugs(20, 3, "storeGetMemSpace stats:");
1198 debugs(20, 3, " " << std::setw(6) << hot_obj_count
<< " HOT objects");
1199 debugs(20, 3, " " << std::setw(6) << released
<< " were released");
1200 PROF_stop(storeGetMemSpace
);
1204 /* thunk through to Store::Root().maintain(). Note that this would be better still
1205 * if registered against the root store itself, but that requires more complex
1206 * update logic - bigger fish to fry first. Long term each store when
1207 * it becomes active will self register
1210 Store::Maintain(void *notused
)
1212 Store::Root().maintain();
1214 /* Reregister a maintain event .. */
1215 eventAdd("MaintainSwapSpace", Maintain
, NULL
, 1.0, 1);
1219 /* The maximum objects to scan for maintain storage space */
1220 #define MAINTAIN_MAX_SCAN 1024
1221 #define MAINTAIN_MAX_REMOVE 64
1224 * This routine is to be called by main loop in main.c.
1225 * It removes expired objects on only one bucket for each time called.
1227 * This should get called 1/s from main().
1230 StoreController::maintain()
1232 static time_t last_warn_time
= 0;
1234 PROF_start(storeMaintainSwapSpace
);
1235 swapDir
->maintain();
1237 /* this should be emitted by the oversize dir, not globally */
1239 if (Store::Root().currentSize() > Store::Root().maxSize()) {
1240 if (squid_curtime
- last_warn_time
> 10) {
1241 debugs(20, DBG_CRITICAL
, "WARNING: Disk space over limit: "
1242 << Store::Root().currentSize() / 1024.0 << " KB > "
1243 << (Store::Root().maxSize() >> 10) << " KB");
1244 last_warn_time
= squid_curtime
;
1248 PROF_stop(storeMaintainSwapSpace
);
1251 /* release an object from a cache */
1253 StoreEntry::release()
1255 PROF_start(storeRelease
);
1256 debugs(20, 3, "storeRelease: Releasing: '" << getMD5Text() << "'");
1257 /* If, for any reason we can't discard this object because of an
1258 * outstanding request, mark it for pending release */
1262 debugs(20, 3, "storeRelease: Only setting RELEASE_REQUEST bit");
1264 PROF_stop(storeRelease
);
1268 if (StoreController::store_dirs_rebuilding
&& swap_filen
> -1) {
1274 if (swap_filen
> -1) {
1276 * Fake a call to StoreEntry->lock() When rebuilding is done,
1277 * we'll just call StoreEntry->unlock() on these.
1281 LateReleaseStack
.push_back(this);
1283 destroyStoreEntry(static_cast<hash_link
*>(this));
1284 // "this" is no longer valid
1287 PROF_stop(storeRelease
);
1291 storeLog(STORE_LOG_RELEASE
, this);
1293 if (swap_filen
> -1) {
1294 // log before unlink() below clears swap_filen
1295 if (!EBIT_TEST(flags
, KEY_PRIVATE
))
1296 storeDirSwapLog(this, SWAP_LOG_DEL
);
1301 setMemStatus(NOT_IN_MEMORY
);
1302 destroyStoreEntry(static_cast<hash_link
*>(this));
1303 PROF_stop(storeRelease
);
1307 storeLateRelease(void *unused
)
1313 if (StoreController::store_dirs_rebuilding
) {
1314 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
1318 for (i
= 0; i
< 10; ++i
) {
1319 e
= LateReleaseStack
.count
? LateReleaseStack
.pop() : NULL
;
1323 debugs(20, DBG_IMPORTANT
, "storeLateRelease: released " << n
<< " objects");
1331 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 0.0, 1);
1334 /* return 1 if a store entry is locked */
1336 StoreEntry::locked() const
1341 if (swap_status
== SWAPOUT_WRITING
)
1344 if (store_status
== STORE_PENDING
)
1348 * SPECIAL, PUBLIC entries should be "locked"
1350 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
1351 if (!EBIT_TEST(flags
, KEY_PRIVATE
))
1358 StoreEntry::validLength() const
1361 const HttpReply
*reply
;
1362 assert(mem_obj
!= NULL
);
1364 debugs(20, 3, "storeEntryValidLength: Checking '" << getMD5Text() << "'");
1365 debugs(20, 5, "storeEntryValidLength: object_len = " <<
1367 debugs(20, 5, "storeEntryValidLength: hdr_sz = " << reply
->hdr_sz
);
1368 debugs(20, 5, "storeEntryValidLength: content_length = " << reply
->content_length
);
1370 if (reply
->content_length
< 0) {
1371 debugs(20, 5, "storeEntryValidLength: Unspecified content length: " << getMD5Text());
1375 if (reply
->hdr_sz
== 0) {
1376 debugs(20, 5, "storeEntryValidLength: Zero header size: " << getMD5Text());
1380 if (mem_obj
->method
== METHOD_HEAD
) {
1381 debugs(20, 5, "storeEntryValidLength: HEAD request: " << getMD5Text());
1385 if (reply
->sline
.status
== HTTP_NOT_MODIFIED
)
1388 if (reply
->sline
.status
== HTTP_NO_CONTENT
)
1391 diff
= reply
->hdr_sz
+ reply
->content_length
- objectLen();
1396 debugs(20, 3, "storeEntryValidLength: " << (diff
< 0 ? -diff
: diff
) << " bytes too " << (diff
< 0 ? "big" : "small") <<"; '" << getMD5Text() << "'" );
1402 storeRegisterWithCacheManager(void)
1404 Mgr::RegisterAction("storedir", "Store Directory Stats", Store::Stats
, 0, 1);
1405 Mgr::RegisterAction("store_io", "Store IO Interface Stats", &Mgr::StoreIoAction::Create
, 0, 1);
1406 Mgr::RegisterAction("store_check_cachable_stats", "storeCheckCachable() Stats",
1407 storeCheckCachableStats
, 0, 1);
1414 mem_policy
= createRemovalPolicy(Config
.memPolicy
);
1417 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
1418 Store::Root().init();
1419 storeRebuildStart();
1421 storeRegisterWithCacheManager();
1425 storeConfigure(void)
1427 store_swap_high
= (long) (((float) Store::Root().maxSize() *
1428 (float) Config
.Swap
.highWaterMark
) / (float) 100);
1429 store_swap_low
= (long) (((float) Store::Root().maxSize() *
1430 (float) Config
.Swap
.lowWaterMark
) / (float) 100);
1431 store_pages_max
= Config
.memMaxSize
/ sizeof(mem_node
);
1435 StoreEntry::memoryCachable() const
1437 if (mem_obj
== NULL
)
1440 if (mem_obj
->data_hdr
.size() == 0)
1443 if (mem_obj
->inmem_lo
!= 0)
1446 if (!Config
.onoff
.memory_cache_first
&& swap_status
== SWAPOUT_DONE
&& refcount
== 1)
1453 StoreEntry::checkNegativeHit() const
1455 if (!EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1458 if (expires
<= squid_curtime
)
1461 if (store_status
!= STORE_OK
)
1468 * Set object for negative caching.
1469 * Preserves any expiry information given by the server.
1470 * In absence of proper expiry info it will set to expire immediately,
1471 * or with HTTP-violations enabled the configured negative-TTL is observed
1474 StoreEntry::negativeCache()
1476 // XXX: should make the default for expires 0 instead of -1
1477 // so we can distinguish "Expires: -1" from nothing.
1479 #if USE_HTTP_VIOLATIONS
1480 expires
= squid_curtime
+ Config
.negativeTtl
;
1482 expires
= squid_curtime
;
1484 EBIT_SET(flags
, ENTRY_NEGCACHED
);
1488 storeFreeMemory(void)
1491 #if USE_CACHE_DIGESTS
1494 cacheDigestDestroy(store_digest
);
1498 store_digest
= NULL
;
1502 expiresMoreThan(time_t expires
, time_t when
)
1504 if (expires
< 0) /* No Expires given */
1507 return (expires
> (squid_curtime
+ when
));
1511 StoreEntry::validToSend() const
1513 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
1516 if (EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1517 if (expires
<= squid_curtime
)
1520 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
1527 StoreEntry::timestampsSet()
1529 const HttpReply
*reply
= getReply();
1530 time_t served_date
= reply
->date
;
1531 int age
= reply
->header
.getInt(HDR_AGE
);
1532 /* Compute the timestamp, mimicking RFC2616 section 13.2.3. */
1533 /* make sure that 0 <= served_date <= squid_curtime */
1535 if (served_date
< 0 || served_date
> squid_curtime
)
1536 served_date
= squid_curtime
;
1539 * If the returned Date: is more than 24 hours older than
1540 * the squid_curtime, then one of us needs to use NTP to set our
1541 * clock. We'll pretend that our clock is right.
1543 else if (served_date
< (squid_curtime
- 24 * 60 * 60) )
1544 served_date
= squid_curtime
;
1547 * Compensate with Age header if origin server clock is ahead
1548 * of us and there is a cache in between us and the origin
1549 * server. But DONT compensate if the age value is larger than
1550 * squid_curtime because it results in a negative served_date.
1552 if (age
> squid_curtime
- served_date
)
1553 if (squid_curtime
> age
)
1554 served_date
= squid_curtime
- age
;
1556 // compensate for Squid-to-server and server-to-Squid delays
1557 if (mem_obj
&& mem_obj
->request
) {
1558 const time_t request_sent
=
1559 mem_obj
->request
->hier
.peer_http_request_sent
.tv_sec
;
1560 if (0 < request_sent
&& request_sent
< squid_curtime
)
1561 served_date
-= (squid_curtime
- request_sent
);
1564 if (reply
->expires
> 0 && reply
->date
> -1)
1565 expires
= served_date
+ (reply
->expires
- reply
->date
);
1567 expires
= reply
->expires
;
1569 lastmod
= reply
->last_modified
;
1571 timestamp
= served_date
;
1575 StoreEntry::registerAbort(STABH
* cb
, void *data
)
1578 assert(mem_obj
->abort
.callback
== NULL
);
1579 mem_obj
->abort
.callback
= cb
;
1580 mem_obj
->abort
.data
= cbdataReference(data
);
1584 StoreEntry::unregisterAbort()
1587 if (mem_obj
->abort
.callback
) {
1588 mem_obj
->abort
.callback
= NULL
;
1589 cbdataReferenceDone(mem_obj
->abort
.data
);
1594 StoreEntry::dump(int l
) const
1596 debugs(20, l
, "StoreEntry->key: " << getMD5Text());
1597 debugs(20, l
, "StoreEntry->next: " << next
);
1598 debugs(20, l
, "StoreEntry->mem_obj: " << mem_obj
);
1599 debugs(20, l
, "StoreEntry->timestamp: " << timestamp
);
1600 debugs(20, l
, "StoreEntry->lastref: " << lastref
);
1601 debugs(20, l
, "StoreEntry->expires: " << expires
);
1602 debugs(20, l
, "StoreEntry->lastmod: " << lastmod
);
1603 debugs(20, l
, "StoreEntry->swap_file_sz: " << swap_file_sz
);
1604 debugs(20, l
, "StoreEntry->refcount: " << refcount
);
1605 debugs(20, l
, "StoreEntry->flags: " << storeEntryFlags(this));
1606 debugs(20, l
, "StoreEntry->swap_dirn: " << swap_dirn
);
1607 debugs(20, l
, "StoreEntry->swap_filen: " << swap_filen
);
1608 debugs(20, l
, "StoreEntry->lock_count: " << lock_count
);
1609 debugs(20, l
, "StoreEntry->mem_status: " << mem_status
);
1610 debugs(20, l
, "StoreEntry->ping_status: " << ping_status
);
1611 debugs(20, l
, "StoreEntry->store_status: " << store_status
);
1612 debugs(20, l
, "StoreEntry->swap_status: " << swap_status
);
1616 * NOTE, this function assumes only two mem states
1619 StoreEntry::setMemStatus(mem_status_t new_status
)
1621 if (new_status
== mem_status
)
1624 // are we using a shared memory cache?
1625 if (Config
.memShared
&& IamWorkerProcess()) {
1626 // enumerate calling cases if shared memory is enabled
1627 assert(new_status
!= IN_MEMORY
|| EBIT_TEST(flags
, ENTRY_SPECIAL
));
1628 // This method was designed to update replacement policy, not to
1629 // actually purge something from the memory cache (TODO: rename?).
1630 // Shared memory cache does not have a policy that needs updates.
1631 mem_status
= new_status
;
1635 assert(mem_obj
!= NULL
);
1637 if (new_status
== IN_MEMORY
) {
1638 assert(mem_obj
->inmem_lo
== 0);
1640 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1641 debugs(20, 4, "StoreEntry::setMemStatus: not inserting special " << mem_obj
->url
<< " into policy");
1643 mem_policy
->Add(mem_policy
, this, &mem_obj
->repl
);
1644 debugs(20, 4, "StoreEntry::setMemStatus: inserted mem node " << mem_obj
->url
<< " key: " << getMD5Text());
1647 ++hot_obj_count
; // TODO: maintain for the shared hot cache as well
1649 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1650 debugs(20, 4, "StoreEntry::setMemStatus: special entry " << mem_obj
->url
);
1652 mem_policy
->Remove(mem_policy
, this, &mem_obj
->repl
);
1653 debugs(20, 4, "StoreEntry::setMemStatus: removed mem node " << mem_obj
->url
);
1659 mem_status
= new_status
;
1663 StoreEntry::url() const
1666 return "[null_entry]";
1667 else if (mem_obj
== NULL
)
1668 return "[null_mem_obj]";
1670 return mem_obj
->url
;
1674 StoreEntry::createMemObject(const char *aUrl
, const char *aLogUrl
)
1679 if (hidden_mem_obj
) {
1680 debugs(20, 3, HERE
<< "restoring " << hidden_mem_obj
);
1681 mem_obj
= hidden_mem_obj
;
1682 hidden_mem_obj
= NULL
;
1683 mem_obj
->resetUrls(aUrl
, aLogUrl
);
1687 mem_obj
= new MemObject(aUrl
, aLogUrl
);
1690 /* this just sets DELAY_SENDING */
1692 StoreEntry::buffer()
1694 EBIT_SET(flags
, DELAY_SENDING
);
1697 /* this just clears DELAY_SENDING and Invokes the handlers */
1701 if (EBIT_TEST(flags
, DELAY_SENDING
)) {
1702 EBIT_CLR(flags
, DELAY_SENDING
);
1708 StoreEntry::objectLen() const
1710 assert(mem_obj
!= NULL
);
1711 return mem_obj
->object_sz
;
1715 StoreEntry::contentLen() const
1717 assert(mem_obj
!= NULL
);
1718 assert(getReply() != NULL
);
1719 return objectLen() - getReply()->hdr_sz
;
1723 StoreEntry::getReply () const
1725 if (NULL
== mem_obj
)
1728 return mem_obj
->getReply();
1735 debugs(20, 3, "StoreEntry::reset: " << url());
1737 HttpReply
*rep
= (HttpReply
*) getReply(); // bypass const
1739 expires
= lastmod
= timestamp
= -1;
1745 * This routine calls the SETUP routine for each fs type.
1746 * I don't know where the best place for this is, and I'm not going to shuffle
1747 * around large chunks of code right now (that can be done once its working.)
1756 * called to add another store removal policy module
1759 storeReplAdd(const char *type
, REMOVALPOLICYCREATE
* create
)
1763 /* find the number of currently known repl types */
1764 for (i
= 0; storerepl_list
&& storerepl_list
[i
].typestr
; ++i
) {
1765 if (strcmp(storerepl_list
[i
].typestr
, type
) == 0) {
1766 debugs(20, DBG_IMPORTANT
, "WARNING: Trying to load store replacement policy " << type
<< " twice.");
1771 /* add the new type */
1772 storerepl_list
= static_cast<storerepl_entry_t
*>(xrealloc(storerepl_list
, (i
+ 2) * sizeof(storerepl_entry_t
)));
1774 memset(&storerepl_list
[i
+ 1], 0, sizeof(storerepl_entry_t
));
1776 storerepl_list
[i
].typestr
= type
;
1778 storerepl_list
[i
].create
= create
;
1782 * Create a removal policy instance
1785 createRemovalPolicy(RemovalPolicySettings
* settings
)
1787 storerepl_entry_t
*r
;
1789 for (r
= storerepl_list
; r
&& r
->typestr
; ++r
) {
1790 if (strcmp(r
->typestr
, settings
->type
) == 0)
1791 return r
->create(settings
->args
);
1794 debugs(20, DBG_IMPORTANT
, "ERROR: Unknown policy " << settings
->type
);
1795 debugs(20, DBG_IMPORTANT
, "ERROR: Be sure to have set cache_replacement_policy");
1796 debugs(20, DBG_IMPORTANT
, "ERROR: and memory_replacement_policy in squid.conf!");
1797 fatalf("ERROR: Unknown policy %s\n", settings
->type
);
1798 return NULL
; /* NOTREACHED */
1803 storeSwapFileNumberSet(StoreEntry
* e
, sfileno filn
)
1805 if (e
->swap_file_number
== filn
)
1810 storeDirMapBitReset(e
->swap_file_number
);
1811 storeDirLRUDelete(e
);
1812 e
->swap_file_number
= -1;
1814 assert(-1 == e
->swap_file_number
);
1815 storeDirMapBitSet(e
->swap_file_number
= filn
);
1824 * Replace a store entry with
1825 * a new reply. This eats the reply.
1828 StoreEntry::replaceHttpReply(HttpReply
*rep
, bool andStartWriting
)
1830 debugs(20, 3, "StoreEntry::replaceHttpReply: " << url());
1833 debugs(20, DBG_CRITICAL
, "Attempt to replace object with no in-memory representation");
1837 mem_obj
->replaceHttpReply(rep
);
1839 if (andStartWriting
)
1845 StoreEntry::startWriting()
1849 /* TODO: when we store headers serparately remove the header portion */
1850 /* TODO: mark the length of the headers ? */
1851 /* We ONLY want the headers */
1852 packerToStoreInit(&p
, this);
1857 const HttpReply
*rep
= getReply();
1860 rep
->packHeadersInto(&p
);
1861 mem_obj
->markEndOfReplyHeaders();
1863 rep
->body
.packInto(&p
);
1870 StoreEntry::getSerialisedMetaData()
1872 StoreMeta
*tlv_list
= storeSwapMetaBuild(this);
1874 char *result
= storeSwapMetaPack(tlv_list
, &swap_hdr_sz
);
1875 storeSwapTLVFree(tlv_list
);
1876 assert (swap_hdr_sz
>= 0);
1877 mem_obj
->swap_hdr_sz
= (size_t) swap_hdr_sz
;
1882 StoreEntry::trimMemory(const bool preserveSwappable
)
1886 * Bug #1943. We must not let go any data for IN_MEMORY
1887 * objects. We have to wait until the mem_status changes.
1889 if (mem_status
== IN_MEMORY
)
1892 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
1893 return; // cannot trim because we do not load them again
1895 if (!preserveSwappable
) {
1896 if (mem_obj
->policyLowestOffsetToKeep(0) == 0) {
1901 * Its not swap-able, and we're about to delete a chunk,
1902 * so we must make it PRIVATE. This is tricky/ugly because
1903 * for the most part, we treat swapable == cachable here.
1906 mem_obj
->trimUnSwappable ();
1908 mem_obj
->trimSwappable ();
1913 StoreEntry::modifiedSince(HttpRequest
* request
) const
1916 time_t mod_time
= lastmod
;
1919 mod_time
= timestamp
;
1921 debugs(88, 3, "modifiedSince: '" << url() << "'");
1923 debugs(88, 3, "modifiedSince: mod_time = " << mod_time
);
1928 /* Find size of the object */
1929 object_length
= getReply()->content_length
;
1931 if (object_length
< 0)
1932 object_length
= contentLen();
1934 if (mod_time
> request
->ims
) {
1935 debugs(88, 3, "--> YES: entry newer than client");
1937 } else if (mod_time
< request
->ims
) {
1938 debugs(88, 3, "--> NO: entry older than client");
1940 } else if (request
->imslen
< 0) {
1941 debugs(88, 3, "--> NO: same LMT, no client length");
1943 } else if (request
->imslen
== object_length
) {
1944 debugs(88, 3, "--> NO: same LMT, same length");
1947 debugs(88, 3, "--> YES: same LMT, different length");
1953 StoreEntry::hasIfMatchEtag(const HttpRequest
&request
) const
1955 const String reqETags
= request
.header
.getList(HDR_IF_MATCH
);
1956 return hasOneOfEtags(reqETags
, false);
1960 StoreEntry::hasIfNoneMatchEtag(const HttpRequest
&request
) const
1962 const String reqETags
= request
.header
.getList(HDR_IF_NONE_MATCH
);
1963 // weak comparison is allowed only for HEAD or full-body GET requests
1964 const bool allowWeakMatch
= !request
.flags
.range
&&
1965 (request
.method
== METHOD_GET
|| request
.method
== METHOD_HEAD
);
1966 return hasOneOfEtags(reqETags
, allowWeakMatch
);
1969 /// whether at least one of the request ETags matches entity ETag
1971 StoreEntry::hasOneOfEtags(const String
&reqETags
, const bool allowWeakMatch
) const
1973 const ETag repETag
= getReply()->header
.getETag(HDR_ETAG
);
1975 return strListIsMember(&reqETags
, "*", ',');
1977 bool matched
= false;
1978 const char *pos
= NULL
;
1981 while (!matched
&& strListGetItem(&reqETags
, ',', &item
, &ilen
, &pos
)) {
1982 if (!strncmp(item
, "*", ilen
))
1986 str
.append(item
, ilen
);
1988 if (etagParseInit(&reqETag
, str
.termedBuf())) {
1989 matched
= allowWeakMatch
? etagIsWeakEqual(repETag
, reqETag
) :
1990 etagIsStrongEqual(repETag
, reqETag
);
1998 StoreEntry::store() const
2000 assert(0 <= swap_dirn
&& swap_dirn
< Config
.cacheSwap
.n_configured
);
2001 return INDEXSD(swap_dirn
);
2005 StoreEntry::unlink()
2007 store()->unlink(*this); // implies disconnect()
2010 swap_status
= SWAPOUT_NONE
;
2014 * return true if the entry is in a state where
2015 * it can accept more data (ie with write() method)
2018 StoreEntry::isAccepting() const
2020 if (STORE_PENDING
!= store_status
)
2023 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
2029 std::ostream
&operator <<(std::ostream
&os
, const StoreEntry
&e
)
2031 return os
<< e
.swap_filen
<< '@' << e
.swap_dirn
<< '=' <<
2032 e
.mem_status
<< '/' << e
.ping_status
<< '/' << e
.store_status
<< '/' <<
2036 /* NullStoreEntry */
2038 NullStoreEntry
NullStoreEntry::_instance
;
2041 NullStoreEntry::getInstance()
2047 NullStoreEntry::getMD5Text() const
2053 NullStoreEntry::operator delete(void*)
2055 fatal ("Attempt to delete NullStoreEntry\n");
2059 NullStoreEntry::getSerialisedMetaData()
2065 #include "Store.cci"