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
) const
277 assert (aRange
.size());
280 return aRange
.end
- 1;
282 #if URL_CHECKSUM_DEBUG
284 mem_obj
->checkUrlChecksum();
288 /* Always read *something* here - we haven't got the header yet */
289 if (EBIT_TEST(flags
, ENTRY_FWD_HDR_WAIT
))
290 return aRange
.end
- 1;
292 if (!mem_obj
->readAheadPolicyCanRead())
295 return mem_obj
->mostBytesWanted(aRange
.end
- 1);
299 StoreEntry::checkDeferRead(int fd
) const
301 return (bytesWanted(Range
<size_t>(0,INT_MAX
)) == 0);
305 StoreEntry::setNoDelay (bool const newValue
)
308 mem_obj
->setNoDelay(newValue
);
312 StoreEntry::storeClientType() const
314 /* The needed offset isn't in memory
315 * XXX TODO: this is wrong for range requests
316 * as the needed offset may *not* be 0, AND
317 * offset 0 in the memory object is the HTTP headers.
320 if (mem_status
== IN_MEMORY
&& Config
.memShared
&& IamWorkerProcess()) {
321 // clients of an object cached in shared memory are memory clients
322 return STORE_MEM_CLIENT
;
327 if (mem_obj
->inmem_lo
)
328 return STORE_DISK_CLIENT
;
330 if (EBIT_TEST(flags
, ENTRY_ABORTED
)) {
331 /* I don't think we should be adding clients to aborted entries */
332 debugs(20, 1, "storeClientType: adding to ENTRY_ABORTED entry");
333 return STORE_MEM_CLIENT
;
336 if (store_status
== STORE_OK
) {
337 /* the object has completed. */
339 if (mem_obj
->inmem_lo
== 0 && !isEmpty()) {
340 if (swap_status
== SWAPOUT_DONE
) {
341 debugs(20,7, HERE
<< mem_obj
<< " lo: " << mem_obj
->inmem_lo
<< " hi: " << mem_obj
->endOffset() << " size: " << mem_obj
->object_sz
);
342 if (mem_obj
->endOffset() == mem_obj
->object_sz
) {
343 /* hot object fully swapped in */
344 return STORE_MEM_CLIENT
;
347 /* Memory-only, or currently being swapped out */
348 return STORE_MEM_CLIENT
;
351 return STORE_DISK_CLIENT
;
354 /* here and past, entry is STORE_PENDING */
356 * If this is the first client, let it be the mem client
358 if (mem_obj
->nclients
== 1)
359 return STORE_MEM_CLIENT
;
362 * If there is no disk file to open yet, we must make this a
363 * mem client. If we can't open the swapin file before writing
364 * to the client, there is no guarantee that we will be able
365 * to open it later when we really need it.
367 if (swap_status
== SWAPOUT_NONE
)
368 return STORE_MEM_CLIENT
;
371 * otherwise, make subsequent clients read from disk so they
372 * can not delay the first, and vice-versa.
374 return STORE_DISK_CLIENT
;
377 StoreEntry::StoreEntry():
378 hidden_mem_obj(NULL
),
381 debugs(20, 3, HERE
<< "new StoreEntry " << this);
384 expires
= lastmod
= lastref
= timestamp
= -1;
386 swap_status
= SWAPOUT_NONE
;
391 StoreEntry::StoreEntry(const char *aUrl
, const char *aLogUrl
):
392 hidden_mem_obj(NULL
),
395 debugs(20, 3, HERE
<< "new StoreEntry " << this);
396 mem_obj
= new MemObject(aUrl
, aLogUrl
);
398 expires
= lastmod
= lastref
= timestamp
= -1;
400 swap_status
= SWAPOUT_NONE
;
405 StoreEntry::~StoreEntry()
407 if (swap_filen
>= 0) {
408 SwapDir
&sd
= dynamic_cast<SwapDir
&>(*store());
409 sd
.disconnect(*this);
411 delete hidden_mem_obj
;
416 StoreEntry::deferProducer(const AsyncCall::Pointer
&producer
)
418 if (!deferredProducer
)
419 deferredProducer
= producer
;
421 debugs(20, 5, HERE
<< "Deferred producer call is allready set to: " <<
422 *deferredProducer
<< ", requested call: " << *producer
);
426 StoreEntry::kickProducer()
428 if (deferredProducer
!= NULL
) {
429 ScheduleCallHere(deferredProducer
);
430 deferredProducer
= NULL
;
436 StoreEntry::destroyMemObject()
438 debugs(20, 3, HERE
<< "destroyMemObject " << mem_obj
);
439 setMemStatus(NOT_IN_MEMORY
);
440 MemObject
*mem
= mem_obj
;
443 delete hidden_mem_obj
;
444 hidden_mem_obj
= NULL
;
448 StoreEntry::hideMemObject()
450 debugs(20, 3, HERE
<< "hiding " << mem_obj
);
452 assert(!hidden_mem_obj
);
453 hidden_mem_obj
= mem_obj
;
458 destroyStoreEntry(void *data
)
460 debugs(20, 3, HERE
<< "destroyStoreEntry: destroying " << data
);
461 StoreEntry
*e
= static_cast<StoreEntry
*>(static_cast<hash_link
*>(data
));
464 if (e
== NullStoreEntry::getInstance())
467 e
->destroyMemObject();
471 assert(e
->key
== NULL
);
476 /* ----- INTERFACE BETWEEN STORAGE MANAGER AND HASH TABLE FUNCTIONS --------- */
479 StoreEntry::hashInsert(const cache_key
* someKey
)
481 debugs(20, 3, "StoreEntry::hashInsert: Inserting Entry " << this << " key '" << storeKeyText(someKey
) << "'");
482 key
= storeKeyDup(someKey
);
483 hash_join(store_table
, this);
487 StoreEntry::hashDelete()
489 hash_remove_link(store_table
, this);
490 storeKeyFree((const cache_key
*)key
);
494 /* -------------------------------------------------------------------------- */
497 /* get rid of memory copy of the object */
499 StoreEntry::purgeMem()
504 debugs(20, 3, "StoreEntry::purgeMem: Freeing memory-copy of " << getMD5Text());
508 if (swap_status
!= SWAPOUT_DONE
)
512 /* RBC 20050104 this is wrong- memory ref counting
513 * is not at all equivalent to the store 'usage' concept
514 * which the replacement policies should be acting upon.
515 * specifically, object iteration within stores needs
516 * memory ref counting to prevent race conditions,
517 * but this should not influence store replacement.
524 debugs(20, 3, "StoreEntry::lock: key '" << getMD5Text() <<"' count=" <<
526 lastref
= squid_curtime
;
527 Store::Root().reference(*this);
531 StoreEntry::setReleaseFlag()
533 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
536 debugs(20, 3, "StoreEntry::setReleaseFlag: '" << getMD5Text() << "'");
538 EBIT_SET(flags
, RELEASE_REQUEST
);
542 StoreEntry::releaseRequest()
544 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
550 * Clear cachable flag here because we might get called before
551 * anyone else even looks at the cachability flag. Also, this
552 * prevents httpMakePublic from really setting a public key.
554 EBIT_CLR(flags
, ENTRY_CACHABLE
);
559 /* unlock object, return -1 if object get released after unlock
560 * otherwise lock_count */
565 debugs(20, 3, "StoreEntry::unlock: key '" << getMD5Text() << "' count=" << lock_count
);
568 return (int) lock_count
;
570 if (store_status
== STORE_PENDING
)
573 assert(storePendingNClients(this) == 0);
575 if (EBIT_TEST(flags
, RELEASE_REQUEST
)) {
580 if (EBIT_TEST(flags
, KEY_PRIVATE
))
581 debugs(20, 1, "WARNING: " << __FILE__
<< ":" << __LINE__
<< ": found KEY_PRIVATE");
583 Store::Root().handleIdleEntry(*this); // may delete us
588 StoreEntry::getPublicByRequestMethod (StoreClient
*aClient
, HttpRequest
* request
, const HttpRequestMethod
& method
)
591 StoreEntry
*result
= storeGetPublicByRequestMethod( request
, method
);
594 aClient
->created (NullStoreEntry::getInstance());
596 aClient
->created (result
);
600 StoreEntry::getPublicByRequest (StoreClient
*aClient
, HttpRequest
* request
)
603 StoreEntry
*result
= storeGetPublicByRequest (request
);
606 result
= NullStoreEntry::getInstance();
608 aClient
->created (result
);
612 StoreEntry::getPublic (StoreClient
*aClient
, const char *uri
, const HttpRequestMethod
& method
)
615 StoreEntry
*result
= storeGetPublic (uri
, method
);
618 result
= NullStoreEntry::getInstance();
620 aClient
->created (result
);
624 storeGetPublic(const char *uri
, const HttpRequestMethod
& method
)
626 return Store::Root().get(storeKeyPublic(uri
, method
));
630 storeGetPublicByRequestMethod(HttpRequest
* req
, const HttpRequestMethod
& method
)
632 return Store::Root().get(storeKeyPublicByRequestMethod(req
, method
));
636 storeGetPublicByRequest(HttpRequest
* req
)
638 StoreEntry
*e
= storeGetPublicByRequestMethod(req
, req
->method
);
640 if (e
== NULL
&& req
->method
== METHOD_HEAD
)
641 /* We can generate a HEAD reply from a cached GET object */
642 e
= storeGetPublicByRequestMethod(req
, METHOD_GET
);
650 static int key_counter
= 0;
652 if (++key_counter
< 0)
658 /* RBC 20050104 AFAICT this should become simpler:
659 * rather than reinserting with a special key it should be marked
660 * as 'released' and then cleaned up when refcounting indicates.
661 * the StoreHashIndex could well implement its 'released' in the
663 * Also, clean log writing should skip over ia,t
664 * Otherwise, we need a 'remove from the index but not the store
668 StoreEntry::setPrivateKey()
670 const cache_key
*newkey
;
672 if (key
&& EBIT_TEST(flags
, KEY_PRIVATE
))
673 return; /* is already private */
677 storeDirSwapLog(this, SWAP_LOG_DEL
);
682 if (mem_obj
!= NULL
) {
683 mem_obj
->id
= getKeyCounter();
684 newkey
= storeKeyPrivate(mem_obj
->url
, mem_obj
->method
, mem_obj
->id
);
686 newkey
= storeKeyPrivate("JUNK", METHOD_NONE
, getKeyCounter());
689 assert(hash_lookup(store_table
, newkey
) == NULL
);
690 EBIT_SET(flags
, KEY_PRIVATE
);
695 StoreEntry::setPublicKey()
697 StoreEntry
*e2
= NULL
;
698 const cache_key
*newkey
;
700 if (key
&& !EBIT_TEST(flags
, KEY_PRIVATE
))
701 return; /* is already public */
706 * We can't make RELEASE_REQUEST objects public. Depending on
707 * when RELEASE_REQUEST gets set, we might not be swapping out
708 * the object. If we're not swapping out, then subsequent
709 * store clients won't be able to access object data which has
710 * been freed from memory.
712 * If RELEASE_REQUEST is set, then ENTRY_CACHABLE should not
713 * be set, and StoreEntry::setPublicKey() should not be called.
715 #if MORE_DEBUG_OUTPUT
717 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
718 debugs(20, 1, "assertion failed: RELEASE key " << key
<< ", url " << mem_obj
->url
);
722 assert(!EBIT_TEST(flags
, RELEASE_REQUEST
));
724 if (mem_obj
->request
) {
725 HttpRequest
*request
= mem_obj
->request
;
727 if (!mem_obj
->vary_headers
) {
728 /* First handle the case where the object no longer varies */
729 safe_free(request
->vary_headers
);
731 if (request
->vary_headers
&& strcmp(request
->vary_headers
, mem_obj
->vary_headers
) != 0) {
732 /* Oops.. the variance has changed. Kill the base object
733 * to record the new variance key
735 safe_free(request
->vary_headers
); /* free old "bad" variance key */
736 StoreEntry
*pe
= storeGetPublic(mem_obj
->url
, mem_obj
->method
);
742 /* Make sure the request knows the variance status */
743 if (!request
->vary_headers
) {
744 const char *vary
= httpMakeVaryMark(request
, mem_obj
->getReply());
747 request
->vary_headers
= xstrdup(vary
);
751 // TODO: storeGetPublic() calls below may create unlocked entries.
752 // We should add/use storeHas() API or lock/unlock those entries.
753 if (mem_obj
->vary_headers
&& !storeGetPublic(mem_obj
->url
, mem_obj
->method
)) {
754 /* Create "vary" base object */
756 StoreEntry
*pe
= storeCreateEntry(mem_obj
->url
, mem_obj
->log_url
, request
->flags
, request
->method
);
757 /* We are allowed to do this typecast */
758 HttpReply
*rep
= new HttpReply
;
759 rep
->setHeaders(HTTP_OK
, "Internal marker object", "x-squid-internal/vary", -1, -1, squid_curtime
+ 100000);
760 vary
= mem_obj
->getReply()->header
.getList(HDR_VARY
);
763 /* Again, we own this structure layout */
764 rep
->header
.putStr(HDR_VARY
, vary
.termedBuf());
768 #if X_ACCELERATOR_VARY
769 vary
= mem_obj
->getReply()->header
.getList(HDR_X_ACCELERATOR_VARY
);
771 if (vary
.defined()) {
772 /* Again, we own this structure layout */
773 rep
->header
.putStr(HDR_X_ACCELERATOR_VARY
, vary
.termedBuf());
778 pe
->replaceHttpReply(rep
);
789 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
791 newkey
= storeKeyPublic(mem_obj
->url
, mem_obj
->method
);
793 if ((e2
= (StoreEntry
*) hash_lookup(store_table
, newkey
))) {
794 debugs(20, 3, "StoreEntry::setPublicKey: Making old '" << mem_obj
->url
<< "' private.");
798 if (mem_obj
->request
)
799 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
801 newkey
= storeKeyPublic(mem_obj
->url
, mem_obj
->method
);
807 EBIT_CLR(flags
, KEY_PRIVATE
);
812 storeDirSwapLog(this, SWAP_LOG_ADD
);
816 storeCreateEntry(const char *url
, const char *log_url
, request_flags flags
, const HttpRequestMethod
& method
)
818 StoreEntry
*e
= NULL
;
819 MemObject
*mem
= NULL
;
820 debugs(20, 3, "storeCreateEntry: '" << url
<< "'");
822 e
= new StoreEntry(url
, log_url
);
823 e
->lock_count
= 1; /* Note lock here w/o calling storeLock() */
825 mem
->method
= method
;
827 if (neighbors_do_private_keys
|| !flags
.hierarchical
)
832 if (flags
.cachable
) {
833 EBIT_SET(e
->flags
, ENTRY_CACHABLE
);
834 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
836 /* StoreEntry::releaseRequest() clears ENTRY_CACHABLE */
840 e
->store_status
= STORE_PENDING
;
841 e
->setMemStatus(NOT_IN_MEMORY
);
843 e
->lastref
= squid_curtime
;
844 e
->timestamp
= -1; /* set in StoreEntry::timestampsSet() */
845 e
->ping_status
= PING_NONE
;
846 EBIT_SET(e
->flags
, ENTRY_VALIDATED
);
850 /* Mark object as expired */
852 StoreEntry::expireNow()
854 debugs(20, 3, "StoreEntry::expireNow: '" << getMD5Text() << "'");
855 expires
= squid_curtime
;
859 storeWriteComplete (void *data
, StoreIOBuffer wroteBuffer
)
861 PROF_start(storeWriteComplete
);
862 StoreEntry
*e
= (StoreEntry
*)data
;
864 if (EBIT_TEST(e
->flags
, DELAY_SENDING
)) {
865 PROF_stop(storeWriteComplete
);
870 PROF_stop(storeWriteComplete
);
874 StoreEntry::write (StoreIOBuffer writeBuffer
)
876 assert(mem_obj
!= NULL
);
877 /* This assert will change when we teach the store to update */
878 PROF_start(StoreEntry_write
);
879 assert(store_status
== STORE_PENDING
);
881 debugs(20, 5, "storeWrite: writing " << writeBuffer
.length
<< " bytes for '" << getMD5Text() << "'");
882 PROF_stop(StoreEntry_write
);
883 storeGetMemSpace(writeBuffer
.length
);
884 mem_obj
->write (writeBuffer
, storeWriteComplete
, this);
887 /* Append incoming data from a primary server to an entry. */
889 StoreEntry::append(char const *buf
, int len
)
891 assert(mem_obj
!= NULL
);
893 assert(store_status
== STORE_PENDING
);
895 StoreIOBuffer tempBuffer
;
896 tempBuffer
.data
= (char *)buf
;
897 tempBuffer
.length
= len
;
899 * XXX sigh, offset might be < 0 here, but it gets "corrected"
900 * later. This offset crap is such a mess.
902 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,1,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
);
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, 1, "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
== 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)
1455 if (Config
.memShared
&& IamWorkerProcess()) {
1456 const int64_t expectedSize
= mem_obj
->expectedReplySize();
1457 // objects of unknown size are not allowed into memory cache, for now
1458 if (expectedSize
< 0 ||
1459 expectedSize
> static_cast<int64_t>(Config
.Store
.maxInMemObjSize
))
1467 StoreEntry::checkNegativeHit() const
1469 if (!EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1472 if (expires
<= squid_curtime
)
1475 if (store_status
!= STORE_OK
)
1482 * Set object for negative caching.
1483 * Preserves any expiry information given by the server.
1484 * In absence of proper expiry info it will set to expire immediately,
1485 * or with HTTP-violations enabled the configured negative-TTL is observed
1488 StoreEntry::negativeCache()
1490 // XXX: should make the default for expires 0 instead of -1
1491 // so we can distinguish "Expires: -1" from nothing.
1493 #if USE_HTTP_VIOLATIONS
1494 expires
= squid_curtime
+ Config
.negativeTtl
;
1496 expires
= squid_curtime
;
1498 EBIT_SET(flags
, ENTRY_NEGCACHED
);
1502 storeFreeMemory(void)
1505 #if USE_CACHE_DIGESTS
1508 cacheDigestDestroy(store_digest
);
1512 store_digest
= NULL
;
1516 expiresMoreThan(time_t expires
, time_t when
)
1518 if (expires
< 0) /* No Expires given */
1521 return (expires
> (squid_curtime
+ when
));
1525 StoreEntry::validToSend() const
1527 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
1530 if (EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1531 if (expires
<= squid_curtime
)
1534 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
1541 StoreEntry::timestampsSet()
1543 const HttpReply
*reply
= getReply();
1544 time_t served_date
= reply
->date
;
1545 int age
= reply
->header
.getInt(HDR_AGE
);
1546 /* Compute the timestamp, mimicking RFC2616 section 13.2.3. */
1547 /* make sure that 0 <= served_date <= squid_curtime */
1549 if (served_date
< 0 || served_date
> squid_curtime
)
1550 served_date
= squid_curtime
;
1553 * If the returned Date: is more than 24 hours older than
1554 * the squid_curtime, then one of us needs to use NTP to set our
1555 * clock. We'll pretend that our clock is right.
1557 else if (served_date
< (squid_curtime
- 24 * 60 * 60) )
1558 served_date
= squid_curtime
;
1561 * Compensate with Age header if origin server clock is ahead
1562 * of us and there is a cache in between us and the origin
1563 * server. But DONT compensate if the age value is larger than
1564 * squid_curtime because it results in a negative served_date.
1566 if (age
> squid_curtime
- served_date
)
1567 if (squid_curtime
> age
)
1568 served_date
= squid_curtime
- age
;
1570 // compensate for Squid-to-server and server-to-Squid delays
1571 if (mem_obj
&& mem_obj
->request
) {
1572 const time_t request_sent
=
1573 mem_obj
->request
->hier
.peer_http_request_sent
.tv_sec
;
1574 if (0 < request_sent
&& request_sent
< squid_curtime
)
1575 served_date
-= (squid_curtime
- request_sent
);
1578 if (reply
->expires
> 0 && reply
->date
> -1)
1579 expires
= served_date
+ (reply
->expires
- reply
->date
);
1581 expires
= reply
->expires
;
1583 lastmod
= reply
->last_modified
;
1585 timestamp
= served_date
;
1589 StoreEntry::registerAbort(STABH
* cb
, void *data
)
1592 assert(mem_obj
->abort
.callback
== NULL
);
1593 mem_obj
->abort
.callback
= cb
;
1594 mem_obj
->abort
.data
= cbdataReference(data
);
1598 StoreEntry::unregisterAbort()
1601 if (mem_obj
->abort
.callback
) {
1602 mem_obj
->abort
.callback
= NULL
;
1603 cbdataReferenceDone(mem_obj
->abort
.data
);
1608 StoreEntry::dump(int l
) const
1610 debugs(20, l
, "StoreEntry->key: " << getMD5Text());
1611 debugs(20, l
, "StoreEntry->next: " << next
);
1612 debugs(20, l
, "StoreEntry->mem_obj: " << mem_obj
);
1613 debugs(20, l
, "StoreEntry->timestamp: " << timestamp
);
1614 debugs(20, l
, "StoreEntry->lastref: " << lastref
);
1615 debugs(20, l
, "StoreEntry->expires: " << expires
);
1616 debugs(20, l
, "StoreEntry->lastmod: " << lastmod
);
1617 debugs(20, l
, "StoreEntry->swap_file_sz: " << swap_file_sz
);
1618 debugs(20, l
, "StoreEntry->refcount: " << refcount
);
1619 debugs(20, l
, "StoreEntry->flags: " << storeEntryFlags(this));
1620 debugs(20, l
, "StoreEntry->swap_dirn: " << swap_dirn
);
1621 debugs(20, l
, "StoreEntry->swap_filen: " << swap_filen
);
1622 debugs(20, l
, "StoreEntry->lock_count: " << lock_count
);
1623 debugs(20, l
, "StoreEntry->mem_status: " << mem_status
);
1624 debugs(20, l
, "StoreEntry->ping_status: " << ping_status
);
1625 debugs(20, l
, "StoreEntry->store_status: " << store_status
);
1626 debugs(20, l
, "StoreEntry->swap_status: " << swap_status
);
1630 * NOTE, this function assumes only two mem states
1633 StoreEntry::setMemStatus(mem_status_t new_status
)
1635 if (new_status
== mem_status
)
1638 // are we using a shared memory cache?
1639 if (Config
.memShared
&& IamWorkerProcess()) {
1640 // enumerate calling cases if shared memory is enabled
1641 assert(new_status
!= IN_MEMORY
|| EBIT_TEST(flags
, ENTRY_SPECIAL
));
1642 // This method was designed to update replacement policy, not to
1643 // actually purge something from the memory cache (TODO: rename?).
1644 // Shared memory cache does not have a policy that needs updates.
1645 mem_status
= new_status
;
1649 assert(mem_obj
!= NULL
);
1651 if (new_status
== IN_MEMORY
) {
1652 assert(mem_obj
->inmem_lo
== 0);
1654 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1655 debugs(20, 4, "StoreEntry::setMemStatus: not inserting special " << mem_obj
->url
<< " into policy");
1657 mem_policy
->Add(mem_policy
, this, &mem_obj
->repl
);
1658 debugs(20, 4, "StoreEntry::setMemStatus: inserted mem node " << mem_obj
->url
<< " key: " << getMD5Text());
1661 hot_obj_count
++; // TODO: maintain for the shared hot cache as well
1663 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1664 debugs(20, 4, "StoreEntry::setMemStatus: special entry " << mem_obj
->url
);
1666 mem_policy
->Remove(mem_policy
, this, &mem_obj
->repl
);
1667 debugs(20, 4, "StoreEntry::setMemStatus: removed mem node " << mem_obj
->url
);
1673 mem_status
= new_status
;
1677 StoreEntry::url() const
1680 return "[null_entry]";
1681 else if (mem_obj
== NULL
)
1682 return "[null_mem_obj]";
1684 return mem_obj
->url
;
1688 StoreEntry::createMemObject(const char *aUrl
, const char *aLogUrl
)
1693 if (hidden_mem_obj
) {
1694 debugs(20, 3, HERE
<< "restoring " << hidden_mem_obj
);
1695 mem_obj
= hidden_mem_obj
;
1696 hidden_mem_obj
= NULL
;
1697 mem_obj
->resetUrls(aUrl
, aLogUrl
);
1701 mem_obj
= new MemObject(aUrl
, aLogUrl
);
1704 /* this just sets DELAY_SENDING */
1706 StoreEntry::buffer()
1708 EBIT_SET(flags
, DELAY_SENDING
);
1711 /* this just clears DELAY_SENDING and Invokes the handlers */
1715 if (EBIT_TEST(flags
, DELAY_SENDING
)) {
1716 EBIT_CLR(flags
, DELAY_SENDING
);
1722 StoreEntry::objectLen() const
1724 assert(mem_obj
!= NULL
);
1725 return mem_obj
->object_sz
;
1729 StoreEntry::contentLen() const
1731 assert(mem_obj
!= NULL
);
1732 assert(getReply() != NULL
);
1733 return objectLen() - getReply()->hdr_sz
;
1737 StoreEntry::getReply () const
1739 if (NULL
== mem_obj
)
1742 return mem_obj
->getReply();
1749 debugs(20, 3, "StoreEntry::reset: " << url());
1751 HttpReply
*rep
= (HttpReply
*) getReply(); // bypass const
1753 expires
= lastmod
= timestamp
= -1;
1759 * This routine calls the SETUP routine for each fs type.
1760 * I don't know where the best place for this is, and I'm not going to shuffle
1761 * around large chunks of code right now (that can be done once its working.)
1770 * called to add another store removal policy module
1773 storeReplAdd(const char *type
, REMOVALPOLICYCREATE
* create
)
1777 /* find the number of currently known repl types */
1778 for (i
= 0; storerepl_list
&& storerepl_list
[i
].typestr
; i
++) {
1779 if (strcmp(storerepl_list
[i
].typestr
, type
) == 0) {
1780 debugs(20, 1, "WARNING: Trying to load store replacement policy " << type
<< " twice.");
1785 /* add the new type */
1786 storerepl_list
= static_cast<storerepl_entry_t
*>(xrealloc(storerepl_list
, (i
+ 2) * sizeof(storerepl_entry_t
)));
1788 memset(&storerepl_list
[i
+ 1], 0, sizeof(storerepl_entry_t
));
1790 storerepl_list
[i
].typestr
= type
;
1792 storerepl_list
[i
].create
= create
;
1796 * Create a removal policy instance
1799 createRemovalPolicy(RemovalPolicySettings
* settings
)
1801 storerepl_entry_t
*r
;
1803 for (r
= storerepl_list
; r
&& r
->typestr
; r
++) {
1804 if (strcmp(r
->typestr
, settings
->type
) == 0)
1805 return r
->create(settings
->args
);
1808 debugs(20, 1, "ERROR: Unknown policy " << settings
->type
);
1809 debugs(20, 1, "ERROR: Be sure to have set cache_replacement_policy");
1810 debugs(20, 1, "ERROR: and memory_replacement_policy in squid.conf!");
1811 fatalf("ERROR: Unknown policy %s\n", settings
->type
);
1812 return NULL
; /* NOTREACHED */
1817 storeSwapFileNumberSet(StoreEntry
* e
, sfileno filn
)
1819 if (e
->swap_file_number
== filn
)
1824 storeDirMapBitReset(e
->swap_file_number
);
1825 storeDirLRUDelete(e
);
1826 e
->swap_file_number
= -1;
1828 assert(-1 == e
->swap_file_number
);
1829 storeDirMapBitSet(e
->swap_file_number
= filn
);
1838 * Replace a store entry with
1839 * a new reply. This eats the reply.
1842 StoreEntry::replaceHttpReply(HttpReply
*rep
, bool andStartWriting
)
1844 debugs(20, 3, "StoreEntry::replaceHttpReply: " << url());
1847 debugs(20, 0, "Attempt to replace object with no in-memory representation");
1851 mem_obj
->replaceHttpReply(rep
);
1853 if (andStartWriting
)
1859 StoreEntry::startWriting()
1863 /* TODO: when we store headers serparately remove the header portion */
1864 /* TODO: mark the length of the headers ? */
1865 /* We ONLY want the headers */
1866 packerToStoreInit(&p
, this);
1871 const HttpReply
*rep
= getReply();
1874 rep
->packHeadersInto(&p
);
1875 mem_obj
->markEndOfReplyHeaders();
1877 rep
->body
.packInto(&p
);
1884 StoreEntry::getSerialisedMetaData()
1886 StoreMeta
*tlv_list
= storeSwapMetaBuild(this);
1888 char *result
= storeSwapMetaPack(tlv_list
, &swap_hdr_sz
);
1889 storeSwapTLVFree(tlv_list
);
1890 assert (swap_hdr_sz
>= 0);
1891 mem_obj
->swap_hdr_sz
= (size_t) swap_hdr_sz
;
1896 StoreEntry::trimMemory(const bool preserveSwappable
)
1900 * Bug #1943. We must not let go any data for IN_MEMORY
1901 * objects. We have to wait until the mem_status changes.
1903 if (mem_status
== IN_MEMORY
)
1906 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
1907 return; // cannot trim because we do not load them again
1909 if (!preserveSwappable
) {
1910 if (mem_obj
->policyLowestOffsetToKeep(0) == 0) {
1915 * Its not swap-able, and we're about to delete a chunk,
1916 * so we must make it PRIVATE. This is tricky/ugly because
1917 * for the most part, we treat swapable == cachable here.
1920 mem_obj
->trimUnSwappable ();
1922 mem_obj
->trimSwappable ();
1927 StoreEntry::modifiedSince(HttpRequest
* request
) const
1930 time_t mod_time
= lastmod
;
1933 mod_time
= timestamp
;
1935 debugs(88, 3, "modifiedSince: '" << url() << "'");
1937 debugs(88, 3, "modifiedSince: mod_time = " << mod_time
);
1942 /* Find size of the object */
1943 object_length
= getReply()->content_length
;
1945 if (object_length
< 0)
1946 object_length
= contentLen();
1948 if (mod_time
> request
->ims
) {
1949 debugs(88, 3, "--> YES: entry newer than client");
1951 } else if (mod_time
< request
->ims
) {
1952 debugs(88, 3, "--> NO: entry older than client");
1954 } else if (request
->imslen
< 0) {
1955 debugs(88, 3, "--> NO: same LMT, no client length");
1957 } else if (request
->imslen
== object_length
) {
1958 debugs(88, 3, "--> NO: same LMT, same length");
1961 debugs(88, 3, "--> YES: same LMT, different length");
1967 StoreEntry::hasIfMatchEtag(const HttpRequest
&request
) const
1969 const String reqETags
= request
.header
.getList(HDR_IF_MATCH
);
1970 return hasOneOfEtags(reqETags
, false);
1974 StoreEntry::hasIfNoneMatchEtag(const HttpRequest
&request
) const
1976 const String reqETags
= request
.header
.getList(HDR_IF_NONE_MATCH
);
1977 // weak comparison is allowed only for HEAD or full-body GET requests
1978 const bool allowWeakMatch
= !request
.flags
.range
&&
1979 (request
.method
== METHOD_GET
|| request
.method
== METHOD_HEAD
);
1980 return hasOneOfEtags(reqETags
, allowWeakMatch
);
1983 /// whether at least one of the request ETags matches entity ETag
1985 StoreEntry::hasOneOfEtags(const String
&reqETags
, const bool allowWeakMatch
) const
1987 const ETag repETag
= getReply()->header
.getETag(HDR_ETAG
);
1989 return strListIsMember(&reqETags
, "*", ',');
1991 bool matched
= false;
1992 const char *pos
= NULL
;
1995 while (!matched
&& strListGetItem(&reqETags
, ',', &item
, &ilen
, &pos
)) {
1996 if (!strncmp(item
, "*", ilen
))
2000 str
.append(item
, ilen
);
2002 if (etagParseInit(&reqETag
, str
.termedBuf())) {
2003 matched
= allowWeakMatch
? etagIsWeakEqual(repETag
, reqETag
) :
2004 etagIsStrongEqual(repETag
, reqETag
);
2012 StoreEntry::store() const
2014 assert(0 <= swap_dirn
&& swap_dirn
< Config
.cacheSwap
.n_configured
);
2015 return INDEXSD(swap_dirn
);
2019 StoreEntry::unlink()
2021 store()->unlink(*this); // implies disconnect()
2024 swap_status
= SWAPOUT_NONE
;
2028 * return true if the entry is in a state where
2029 * it can accept more data (ie with write() method)
2032 StoreEntry::isAccepting() const
2034 if (STORE_PENDING
!= store_status
)
2037 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
2043 std::ostream
&operator <<(std::ostream
&os
, const StoreEntry
&e
)
2045 return os
<< e
.swap_filen
<< '@' << e
.swap_dirn
<< '=' <<
2046 e
.mem_status
<< '/' << e
.ping_status
<< '/' << e
.store_status
<< '/' <<
2050 /* NullStoreEntry */
2052 NullStoreEntry
NullStoreEntry::_instance
;
2055 NullStoreEntry::getInstance()
2061 NullStoreEntry::getMD5Text() const
2067 NullStoreEntry::operator delete(void*)
2069 fatal ("Attempt to delete NullStoreEntry\n");
2073 NullStoreEntry::getSerialisedMetaData()
2079 #include "Store.cci"