3 * DEBUG: section 20 Storage Manager
4 * AUTHOR: Harvest Derived
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 #include "CacheDigest.h"
36 #include "CacheManager.h"
37 #include "comm/Connection.h"
43 #include "HttpReply.h"
44 #include "HttpRequest.h"
46 #include "MemObject.h"
47 #include "mgr/Registration.h"
48 #include "mgr/StoreIoAction.h"
49 #include "profiler/Profiler.h"
50 #include "repl_modules.h"
51 #include "RequestFlags.h"
52 #include "SquidConfig.h"
53 #include "SquidTime.h"
55 #include "StatCounters.h"
58 #include "store_digest.h"
59 #include "store_key_md5.h"
60 #include "store_key_md5.h"
61 #include "store_log.h"
62 #include "store_rebuild.h"
63 #include "StoreClient.h"
64 #include "StoreIOState.h"
65 #include "StoreMeta.h"
67 #include "swap_log_op.h"
71 #include "DelayPools.h"
77 #define REBUILD_TIMESTAMP_DELTA_MAX 2
79 #define STORE_IN_MEM_BUCKETS (229)
81 /** \todo Convert these string constants to enum string-arrays generated */
83 const char *memStatusStr
[] = {
88 const char *pingStatusStr
[] = {
94 const char *storeStatusStr
[] = {
99 const char *swapStatusStr
[] = {
106 * This defines an repl type
109 typedef struct _storerepl_entry storerepl_entry_t
;
111 struct _storerepl_entry
{
113 REMOVALPOLICYCREATE
*create
;
116 static storerepl_entry_t
*storerepl_list
= NULL
;
119 * local function prototypes
121 static int getKeyCounter(void);
122 static OBJH storeCheckCachableStats
;
123 static EVH storeLateRelease
;
128 static Stack
<StoreEntry
*> LateReleaseStack
;
129 MemAllocator
*StoreEntry::pool
= NULL
;
131 StorePointer
Store::CurrentRoot
= NULL
;
134 Store::Root(Store
* aRoot
)
140 Store::Root(StorePointer aRoot
)
142 Root(aRoot
.getRaw());
146 Store::Stats(StoreEntry
* output
)
149 Root().stat(*output
);
165 Store::unlink (StoreEntry
&anEntry
)
167 fatal("Store::unlink on invalid Store\n");
171 StoreEntry::operator new (size_t bytecount
)
173 assert (bytecount
== sizeof (StoreEntry
));
176 pool
= memPoolCreate ("StoreEntry", bytecount
);
177 pool
->setChunkSize(2048 * 1024);
180 return pool
->alloc();
184 StoreEntry::operator delete (void *address
)
186 pool
->freeOne(address
);
190 StoreEntry::makePublic()
192 /* This object can be cached for a long time */
194 if (!EBIT_TEST(flags
, RELEASE_REQUEST
))
199 StoreEntry::makePrivate()
201 /* This object should never be cached at all */
203 releaseRequest(); /* delete object when not used */
207 StoreEntry::cacheNegatively()
209 /* This object may be negatively cached */
215 StoreEntry::inUseCount()
219 return pool
->getInUseCount();
223 StoreEntry::getMD5Text() const
225 return storeKeyText((const cache_key
*)key
);
231 StoreEntry::DeferReader(void *theContext
, CommRead
const &aRead
)
233 StoreEntry
*anEntry
= (StoreEntry
*)theContext
;
234 anEntry
->delayAwareRead(aRead
.conn
,
241 StoreEntry::delayAwareRead(const Comm::ConnectionPointer
&conn
, char *buf
, int len
, AsyncCall::Pointer callback
)
243 size_t amountToRead
= bytesWanted(Range
<size_t>(0, len
));
244 /* sketch: readdeferer* = getdeferer.
245 * ->deferRead (fd, buf, len, callback, DelayAwareRead, this)
248 if (amountToRead
== 0) {
250 /* read ahead limit */
251 /* Perhaps these two calls should both live in MemObject */
253 if (!mem_obj
->readAheadPolicyCanRead()) {
255 mem_obj
->delayRead(DeferredRead(DeferReader
, this, CommRead(conn
, buf
, len
, callback
)));
261 mem_obj
->mostBytesAllowed().delayRead(DeferredRead(DeferReader
, this, CommRead(conn
, buf
, len
, callback
)));
268 if (fd_table
[conn
->fd
].closing()) {
269 // Readers must have closing callbacks if they want to be notified. No
270 // readers appeared to care around 2009/12/14 as they skipped reading
271 // for other reasons. Closing may already be true at the delyaAwareRead
272 // call time or may happen while we wait after delayRead() above.
273 debugs(20, 3, HERE
<< "wont read from closing " << conn
<< " for " <<
275 return; // the read callback will never be called
278 comm_read(conn
, buf
, amountToRead
, callback
);
282 StoreEntry::bytesWanted (Range
<size_t> const aRange
, bool ignoreDelayPools
) const
287 #if URL_CHECKSUM_DEBUG
289 mem_obj
->checkUrlChecksum();
293 if (!mem_obj
->readAheadPolicyCanRead())
296 return mem_obj
->mostBytesWanted(aRange
.end
, ignoreDelayPools
);
300 StoreEntry::checkDeferRead(int fd
) const
302 return (bytesWanted(Range
<size_t>(0,INT_MAX
)) == 0);
306 StoreEntry::setNoDelay (bool const newValue
)
309 mem_obj
->setNoDelay(newValue
);
312 // XXX: Type names mislead. STORE_DISK_CLIENT actually means that we should
313 // open swapin file, aggressively trim memory, and ignore read-ahead gap.
314 // It does not mean we will read from disk exclusively (or at all!).
315 // XXX: May create STORE_DISK_CLIENT with no disk caching configured.
316 // XXX: Collapsed clients cannot predict their type.
318 StoreEntry::storeClientType() const
320 /* The needed offset isn't in memory
321 * XXX TODO: this is wrong for range requests
322 * as the needed offset may *not* be 0, AND
323 * offset 0 in the memory object is the HTTP headers.
328 if (mem_obj
->inmem_lo
)
329 return STORE_DISK_CLIENT
;
331 if (EBIT_TEST(flags
, ENTRY_ABORTED
)) {
332 /* I don't think we should be adding clients to aborted entries */
333 debugs(20, DBG_IMPORTANT
, "storeClientType: adding to ENTRY_ABORTED entry");
334 return STORE_MEM_CLIENT
;
337 if (store_status
== STORE_OK
) {
338 /* the object has completed. */
340 if (mem_obj
->inmem_lo
== 0 && !isEmpty()) {
341 if (swap_status
== SWAPOUT_DONE
) {
342 debugs(20,7, HERE
<< mem_obj
<< " lo: " << mem_obj
->inmem_lo
<< " hi: " << mem_obj
->endOffset() << " size: " << mem_obj
->object_sz
);
343 if (mem_obj
->endOffset() == mem_obj
->object_sz
) {
344 /* hot object fully swapped in (XXX: or swapped out?) */
345 return STORE_MEM_CLIENT
;
348 /* Memory-only, or currently being swapped out */
349 return STORE_MEM_CLIENT
;
352 return STORE_DISK_CLIENT
;
355 /* here and past, entry is STORE_PENDING */
357 * If this is the first client, let it be the mem client
359 if (mem_obj
->nclients
== 1)
360 return STORE_MEM_CLIENT
;
363 * If there is no disk file to open yet, we must make this a
364 * mem client. If we can't open the swapin file before writing
365 * to the client, there is no guarantee that we will be able
366 * to open it later when we really need it.
368 if (swap_status
== SWAPOUT_NONE
)
369 return STORE_MEM_CLIENT
;
372 * otherwise, make subsequent clients read from disk so they
373 * can not delay the first, and vice-versa.
375 return STORE_DISK_CLIENT
;
378 StoreEntry::StoreEntry() :
389 mem_status(NOT_IN_MEMORY
),
390 ping_status(PING_NONE
),
391 store_status(STORE_PENDING
),
392 swap_status(SWAPOUT_NONE
),
395 debugs(20, 3, HERE
<< "new StoreEntry " << this);
398 StoreEntry::~StoreEntry()
404 StoreEntry::deferProducer(const AsyncCall::Pointer
&producer
)
406 if (!deferredProducer
)
407 deferredProducer
= producer
;
409 debugs(20, 5, HERE
<< "Deferred producer call is allready set to: " <<
410 *deferredProducer
<< ", requested call: " << *producer
);
414 StoreEntry::kickProducer()
416 if (deferredProducer
!= NULL
) {
417 ScheduleCallHere(deferredProducer
);
418 deferredProducer
= NULL
;
424 StoreEntry::destroyMemObject()
426 debugs(20, 3, HERE
<< "destroyMemObject " << mem_obj
);
428 if (MemObject
*mem
= mem_obj
) {
429 // Store::Root() is FATALly missing during shutdown
430 if (mem
->xitTable
.index
>= 0 && !shutting_down
)
431 Store::Root().transientsDisconnect(*mem
);
432 if (mem
->memCache
.index
>= 0 && !shutting_down
)
433 Store::Root().memoryDisconnect(*this);
435 setMemStatus(NOT_IN_MEMORY
);
442 destroyStoreEntry(void *data
)
444 debugs(20, 3, HERE
<< "destroyStoreEntry: destroying " << data
);
445 StoreEntry
*e
= static_cast<StoreEntry
*>(static_cast<hash_link
*>(data
));
448 if (e
== NullStoreEntry::getInstance())
451 // Store::Root() is FATALly missing during shutdown
452 if (e
->swap_filen
>= 0 && !shutting_down
) {
453 SwapDir
&sd
= dynamic_cast<SwapDir
&>(*e
->store());
457 e
->destroyMemObject();
461 assert(e
->key
== NULL
);
466 /* ----- INTERFACE BETWEEN STORAGE MANAGER AND HASH TABLE FUNCTIONS --------- */
469 StoreEntry::hashInsert(const cache_key
* someKey
)
471 debugs(20, 3, "StoreEntry::hashInsert: Inserting Entry " << *this << " key '" << storeKeyText(someKey
) << "'");
472 key
= storeKeyDup(someKey
);
473 hash_join(store_table
, this);
477 StoreEntry::hashDelete()
479 if (key
) { // some test cases do not create keys and do not hashInsert()
480 hash_remove_link(store_table
, this);
481 storeKeyFree((const cache_key
*)key
);
486 /* -------------------------------------------------------------------------- */
488 /* get rid of memory copy of the object */
490 StoreEntry::purgeMem()
495 debugs(20, 3, "StoreEntry::purgeMem: Freeing memory-copy of " << getMD5Text());
497 Store::Root().memoryUnlink(*this);
499 if (swap_status
!= SWAPOUT_DONE
)
504 StoreEntry::lock(const char *context
)
507 debugs(20, 3, context
<< " locked key " << getMD5Text() << ' ' << *this);
511 StoreEntry::touch() {
512 lastref
= squid_curtime
;
513 Store::Root().reference(*this);
517 StoreEntry::setReleaseFlag()
519 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
522 debugs(20, 3, "StoreEntry::setReleaseFlag: '" << getMD5Text() << "'");
524 EBIT_SET(flags
, RELEASE_REQUEST
);
526 Store::Root().markForUnlink(*this);
530 StoreEntry::releaseRequest()
532 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
535 setReleaseFlag(); // makes validToSend() false, preventing future hits
541 StoreEntry::unlock(const char *context
)
543 debugs(20, 3, (context
? context
: "somebody") <<
544 " unlocking key " << getMD5Text() << ' ' << *this);
545 assert(lock_count
> 0);
549 return (int) lock_count
;
551 if (store_status
== STORE_PENDING
)
554 assert(storePendingNClients(this) == 0);
556 if (EBIT_TEST(flags
, RELEASE_REQUEST
)) {
561 if (EBIT_TEST(flags
, KEY_PRIVATE
))
562 debugs(20, DBG_IMPORTANT
, "WARNING: " << __FILE__
<< ":" << __LINE__
<< ": found KEY_PRIVATE");
564 Store::Root().handleIdleEntry(*this); // may delete us
569 StoreEntry::getPublicByRequestMethod (StoreClient
*aClient
, HttpRequest
* request
, const HttpRequestMethod
& method
)
572 StoreEntry
*result
= storeGetPublicByRequestMethod( request
, method
);
575 aClient
->created (NullStoreEntry::getInstance());
577 aClient
->created (result
);
581 StoreEntry::getPublicByRequest (StoreClient
*aClient
, HttpRequest
* request
)
584 StoreEntry
*result
= storeGetPublicByRequest (request
);
587 result
= NullStoreEntry::getInstance();
589 aClient
->created (result
);
593 StoreEntry::getPublic (StoreClient
*aClient
, const char *uri
, const HttpRequestMethod
& method
)
596 StoreEntry
*result
= storeGetPublic (uri
, method
);
599 result
= NullStoreEntry::getInstance();
601 aClient
->created (result
);
605 storeGetPublic(const char *uri
, const HttpRequestMethod
& method
)
607 return Store::Root().get(storeKeyPublic(uri
, method
));
611 storeGetPublicByRequestMethod(HttpRequest
* req
, const HttpRequestMethod
& method
)
613 return Store::Root().get(storeKeyPublicByRequestMethod(req
, method
));
617 storeGetPublicByRequest(HttpRequest
* req
)
619 StoreEntry
*e
= storeGetPublicByRequestMethod(req
, req
->method
);
621 if (e
== NULL
&& req
->method
== Http::METHOD_HEAD
)
622 /* We can generate a HEAD reply from a cached GET object */
623 e
= storeGetPublicByRequestMethod(req
, Http::METHOD_GET
);
631 static int key_counter
= 0;
633 if (++key_counter
< 0)
639 /* RBC 20050104 AFAICT this should become simpler:
640 * rather than reinserting with a special key it should be marked
641 * as 'released' and then cleaned up when refcounting indicates.
642 * the StoreHashIndex could well implement its 'released' in the
644 * Also, clean log writing should skip over ia,t
645 * Otherwise, we need a 'remove from the index but not the store
649 StoreEntry::setPrivateKey()
651 const cache_key
*newkey
;
653 if (key
&& EBIT_TEST(flags
, KEY_PRIVATE
))
654 return; /* is already private */
657 setReleaseFlag(); // will markForUnlink(); all caches/workers will know
659 // TODO: move into SwapDir::markForUnlink() already called by Root()
661 storeDirSwapLog(this, SWAP_LOG_DEL
);
666 if (mem_obj
&& mem_obj
->hasUris()) {
667 mem_obj
->id
= getKeyCounter();
668 newkey
= storeKeyPrivate(mem_obj
->storeId(), mem_obj
->method
, mem_obj
->id
);
670 newkey
= storeKeyPrivate("JUNK", Http::METHOD_NONE
, getKeyCounter());
673 assert(hash_lookup(store_table
, newkey
) == NULL
);
674 EBIT_SET(flags
, KEY_PRIVATE
);
679 StoreEntry::setPublicKey()
681 const cache_key
*newkey
;
683 if (key
&& !EBIT_TEST(flags
, KEY_PRIVATE
))
684 return; /* is already public */
689 * We can't make RELEASE_REQUEST objects public. Depending on
690 * when RELEASE_REQUEST gets set, we might not be swapping out
691 * the object. If we're not swapping out, then subsequent
692 * store clients won't be able to access object data which has
693 * been freed from memory.
695 * If RELEASE_REQUEST is set, setPublicKey() should not be called.
697 #if MORE_DEBUG_OUTPUT
699 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
700 debugs(20, DBG_IMPORTANT
, "assertion failed: RELEASE key " << key
<< ", url " << mem_obj
->url
);
704 assert(!EBIT_TEST(flags
, RELEASE_REQUEST
));
706 if (mem_obj
->request
) {
707 HttpRequest
*request
= mem_obj
->request
;
709 if (!mem_obj
->vary_headers
) {
710 /* First handle the case where the object no longer varies */
711 safe_free(request
->vary_headers
);
713 if (request
->vary_headers
&& strcmp(request
->vary_headers
, mem_obj
->vary_headers
) != 0) {
714 /* Oops.. the variance has changed. Kill the base object
715 * to record the new variance key
717 safe_free(request
->vary_headers
); /* free old "bad" variance key */
718 if (StoreEntry
*pe
= storeGetPublic(mem_obj
->storeId(), mem_obj
->method
))
722 /* Make sure the request knows the variance status */
723 if (!request
->vary_headers
) {
724 const char *vary
= httpMakeVaryMark(request
, mem_obj
->getReply());
727 request
->vary_headers
= xstrdup(vary
);
731 // TODO: storeGetPublic() calls below may create unlocked entries.
732 // We should add/use storeHas() API or lock/unlock those entries.
733 if (mem_obj
->vary_headers
&& !storeGetPublic(mem_obj
->storeId(), mem_obj
->method
)) {
734 /* Create "vary" base object */
736 StoreEntry
*pe
= storeCreateEntry(mem_obj
->storeId(), mem_obj
->logUri(), request
->flags
, request
->method
);
737 /* We are allowed to do this typecast */
738 HttpReply
*rep
= new HttpReply
;
739 rep
->setHeaders(Http::scOkay
, "Internal marker object", "x-squid-internal/vary", -1, -1, squid_curtime
+ 100000);
740 vary
= mem_obj
->getReply()->header
.getList(HDR_VARY
);
743 /* Again, we own this structure layout */
744 rep
->header
.putStr(HDR_VARY
, vary
.termedBuf());
748 #if X_ACCELERATOR_VARY
749 vary
= mem_obj
->getReply()->header
.getList(HDR_X_ACCELERATOR_VARY
);
751 if (vary
.size() > 0) {
752 /* Again, we own this structure layout */
753 rep
->header
.putStr(HDR_X_ACCELERATOR_VARY
, vary
.termedBuf());
758 pe
->replaceHttpReply(rep
, false); // no write until key is public
764 pe
->startWriting(); // after makePublic()
768 pe
->unlock("StoreEntry::setPublicKey+Vary");
771 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
773 newkey
= storeKeyPublic(mem_obj
->storeId(), mem_obj
->method
);
775 if (StoreEntry
*e2
= (StoreEntry
*)hash_lookup(store_table
, newkey
)) {
776 debugs(20, 3, "Making old " << *e2
<< " private.");
780 if (mem_obj
->request
)
781 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
783 newkey
= storeKeyPublic(mem_obj
->storeId(), mem_obj
->method
);
789 EBIT_CLR(flags
, KEY_PRIVATE
);
794 storeDirSwapLog(this, SWAP_LOG_ADD
);
798 storeCreatePureEntry(const char *url
, const char *log_url
, const RequestFlags
&flags
, const HttpRequestMethod
& method
)
800 StoreEntry
*e
= NULL
;
801 debugs(20, 3, "storeCreateEntry: '" << url
<< "'");
803 e
= new StoreEntry();
805 e
->mem_obj
->setUris(url
, log_url
, method
);
807 if (flags
.cachable
) {
808 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
813 e
->store_status
= STORE_PENDING
;
815 e
->lastref
= squid_curtime
;
816 e
->timestamp
= -1; /* set in StoreEntry::timestampsSet() */
817 e
->ping_status
= PING_NONE
;
818 EBIT_SET(e
->flags
, ENTRY_VALIDATED
);
823 storeCreateEntry(const char *url
, const char *logUrl
, const RequestFlags
&flags
, const HttpRequestMethod
& method
)
825 StoreEntry
*e
= storeCreatePureEntry(url
, logUrl
, flags
, method
);
826 e
->lock("storeCreateEntry");
828 if (neighbors_do_private_keys
|| !flags
.hierarchical
)
836 /* Mark object as expired */
838 StoreEntry::expireNow()
840 debugs(20, 3, "StoreEntry::expireNow: '" << getMD5Text() << "'");
841 expires
= squid_curtime
;
845 StoreEntry::write (StoreIOBuffer writeBuffer
)
847 assert(mem_obj
!= NULL
);
848 /* This assert will change when we teach the store to update */
849 PROF_start(StoreEntry_write
);
850 assert(store_status
== STORE_PENDING
);
852 // XXX: caller uses content offset, but we also store headers
853 if (const HttpReply
*reply
= mem_obj
->getReply())
854 writeBuffer
.offset
+= reply
->hdr_sz
;
856 debugs(20, 5, "storeWrite: writing " << writeBuffer
.length
<< " bytes for '" << getMD5Text() << "'");
857 PROF_stop(StoreEntry_write
);
858 storeGetMemSpace(writeBuffer
.length
);
859 mem_obj
->write(writeBuffer
);
861 if (!EBIT_TEST(flags
, DELAY_SENDING
))
865 /* Append incoming data from a primary server to an entry. */
867 StoreEntry::append(char const *buf
, int len
)
869 assert(mem_obj
!= NULL
);
871 assert(store_status
== STORE_PENDING
);
873 StoreIOBuffer tempBuffer
;
874 tempBuffer
.data
= (char *)buf
;
875 tempBuffer
.length
= len
;
877 * XXX sigh, offset might be < 0 here, but it gets "corrected"
878 * later. This offset crap is such a mess.
880 tempBuffer
.offset
= mem_obj
->endOffset() - (getReply() ? getReply()->hdr_sz
: 0);
885 storeAppendPrintf(StoreEntry
* e
, const char *fmt
,...)
890 storeAppendVPrintf(e
, fmt
, args
);
894 /* used be storeAppendPrintf and Packer */
896 storeAppendVPrintf(StoreEntry
* e
, const char *fmt
, va_list vargs
)
898 LOCAL_ARRAY(char, buf
, 4096);
900 vsnprintf(buf
, 4096, fmt
, vargs
);
901 e
->append(buf
, strlen(buf
));
904 struct _store_check_cachable_hist
{
908 int not_entry_cachable
;
909 int wrong_content_length
;
914 int too_many_open_files
;
915 int too_many_open_fds
;
921 } store_check_cachable_hist
;
924 storeTooManyDiskFilesOpen(void)
926 if (Config
.max_open_disk_fds
== 0)
929 if (store_open_disk_fd
> Config
.max_open_disk_fds
)
936 StoreEntry::checkTooSmall()
938 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
941 if (STORE_OK
== store_status
)
942 if (mem_obj
->object_sz
< 0 ||
943 mem_obj
->object_sz
< Config
.Store
.minObjectSize
)
945 if (getReply()->content_length
> -1)
946 if (getReply()->content_length
< Config
.Store
.minObjectSize
)
951 // TODO: remove checks already performed by swapoutPossible()
952 // TODO: move "too many open..." checks outside -- we are called too early/late
954 StoreEntry::checkCachable()
956 #if CACHE_ALL_METHODS
958 if (mem_obj
->method
!= Http::METHOD_GET
) {
959 debugs(20, 2, "StoreEntry::checkCachable: NO: non-GET method");
960 ++store_check_cachable_hist
.no
.non_get
;
963 if (store_status
== STORE_OK
&& EBIT_TEST(flags
, ENTRY_BAD_LENGTH
)) {
964 debugs(20, 2, "StoreEntry::checkCachable: NO: wrong content-length");
965 ++store_check_cachable_hist
.no
.wrong_content_length
;
966 } else if (EBIT_TEST(flags
, RELEASE_REQUEST
)) {
967 debugs(20, 2, "StoreEntry::checkCachable: NO: not cachable");
968 ++store_check_cachable_hist
.no
.not_entry_cachable
; // TODO: rename?
969 } else if (EBIT_TEST(flags
, ENTRY_NEGCACHED
)) {
970 debugs(20, 3, "StoreEntry::checkCachable: NO: negative cached");
971 ++store_check_cachable_hist
.no
.negative_cached
;
972 return 0; /* avoid release call below */
973 } else if ((getReply()->content_length
> 0 &&
974 getReply()->content_length
> store_maxobjsize
) ||
975 mem_obj
->endOffset() > store_maxobjsize
) {
976 debugs(20, 2, "StoreEntry::checkCachable: NO: too big");
977 ++store_check_cachable_hist
.no
.too_big
;
978 } else if (checkTooSmall()) {
979 debugs(20, 2, "StoreEntry::checkCachable: NO: too small");
980 ++store_check_cachable_hist
.no
.too_small
;
981 } else if (EBIT_TEST(flags
, KEY_PRIVATE
)) {
982 debugs(20, 3, "StoreEntry::checkCachable: NO: private key");
983 ++store_check_cachable_hist
.no
.private_key
;
984 } else if (swap_status
!= SWAPOUT_NONE
) {
986 * here we checked the swap_status because the remaining
987 * cases are only relevant only if we haven't started swapping
988 * out the object yet.
991 } else if (storeTooManyDiskFilesOpen()) {
992 debugs(20, 2, "StoreEntry::checkCachable: NO: too many disk files open");
993 ++store_check_cachable_hist
.no
.too_many_open_files
;
994 } else if (fdNFree() < RESERVED_FD
) {
995 debugs(20, 2, "StoreEntry::checkCachable: NO: too many FD's open");
996 ++store_check_cachable_hist
.no
.too_many_open_fds
;
998 ++store_check_cachable_hist
.yes
.Default
;
1007 storeCheckCachableStats(StoreEntry
*sentry
)
1009 storeAppendPrintf(sentry
, "Category\t Count\n");
1011 #if CACHE_ALL_METHODS
1013 storeAppendPrintf(sentry
, "no.non_get\t%d\n",
1014 store_check_cachable_hist
.no
.non_get
);
1017 storeAppendPrintf(sentry
, "no.not_entry_cachable\t%d\n",
1018 store_check_cachable_hist
.no
.not_entry_cachable
);
1019 storeAppendPrintf(sentry
, "no.wrong_content_length\t%d\n",
1020 store_check_cachable_hist
.no
.wrong_content_length
);
1021 storeAppendPrintf(sentry
, "no.negative_cached\t%d\n",
1022 store_check_cachable_hist
.no
.negative_cached
);
1023 storeAppendPrintf(sentry
, "no.too_big\t%d\n",
1024 store_check_cachable_hist
.no
.too_big
);
1025 storeAppendPrintf(sentry
, "no.too_small\t%d\n",
1026 store_check_cachable_hist
.no
.too_small
);
1027 storeAppendPrintf(sentry
, "no.private_key\t%d\n",
1028 store_check_cachable_hist
.no
.private_key
);
1029 storeAppendPrintf(sentry
, "no.too_many_open_files\t%d\n",
1030 store_check_cachable_hist
.no
.too_many_open_files
);
1031 storeAppendPrintf(sentry
, "no.too_many_open_fds\t%d\n",
1032 store_check_cachable_hist
.no
.too_many_open_fds
);
1033 storeAppendPrintf(sentry
, "yes.default\t%d\n",
1034 store_check_cachable_hist
.yes
.Default
);
1038 StoreEntry::complete()
1040 debugs(20, 3, "storeComplete: '" << getMD5Text() << "'");
1042 if (store_status
!= STORE_PENDING
) {
1044 * if we're not STORE_PENDING, then probably we got aborted
1045 * and there should be NO clients on this entry
1047 assert(EBIT_TEST(flags
, ENTRY_ABORTED
));
1048 assert(mem_obj
->nclients
== 0);
1052 /* This is suspect: mem obj offsets include the headers. do we adjust for that
1053 * in use of object_sz?
1055 mem_obj
->object_sz
= mem_obj
->endOffset();
1057 store_status
= STORE_OK
;
1059 assert(mem_status
== NOT_IN_MEMORY
);
1061 if (!validLength()) {
1062 EBIT_SET(flags
, ENTRY_BAD_LENGTH
);
1066 #if USE_CACHE_DIGESTS
1067 if (mem_obj
->request
)
1068 mem_obj
->request
->hier
.store_complete_stop
= current_time
;
1072 * We used to call invokeHandlers, then storeSwapOut. However,
1073 * Madhukar Reddy <myreddy@persistence.com> reported that
1074 * responses without content length would sometimes get released
1075 * in client_side, thinking that the response is incomplete.
1081 * Someone wants to abort this transfer. Set the reason in the
1082 * request structure, call the server-side callback and mark the
1083 * entry for releasing
1088 ++statCounter
.aborted_requests
;
1089 assert(store_status
== STORE_PENDING
);
1090 assert(mem_obj
!= NULL
);
1091 debugs(20, 6, "storeAbort: " << getMD5Text());
1093 lock("StoreEntry::abort"); /* lock while aborting */
1098 EBIT_SET(flags
, ENTRY_ABORTED
);
1100 setMemStatus(NOT_IN_MEMORY
);
1102 store_status
= STORE_OK
;
1104 /* Notify the server side */
1108 * Should we check abort.data for validity?
1110 if (mem_obj
->abort
.callback
) {
1111 if (!cbdataReferenceValid(mem_obj
->abort
.data
))
1112 debugs(20, DBG_IMPORTANT
,HERE
<< "queueing event when abort.data is not valid");
1113 eventAdd("mem_obj->abort.callback",
1114 mem_obj
->abort
.callback
,
1115 mem_obj
->abort
.data
,
1121 /* XXX Should we reverse these two, so that there is no
1122 * unneeded disk swapping triggered?
1124 /* Notify the client side */
1127 // abort swap out, invalidating what was created so far (release follows)
1128 swapOutFileClose(StoreIOState::writerGone
);
1130 unlock("StoreEntry::abort"); /* unlock */
1134 * Clear Memory storage to accommodate the given object len
1137 storeGetMemSpace(int size
)
1139 PROF_start(storeGetMemSpace
);
1140 StoreEntry
*e
= NULL
;
1142 static time_t last_check
= 0;
1143 size_t pages_needed
;
1144 RemovalPurgeWalker
*walker
;
1146 if (squid_curtime
== last_check
) {
1147 PROF_stop(storeGetMemSpace
);
1151 last_check
= squid_curtime
;
1153 pages_needed
= (size
+ SM_PAGE_SIZE
-1) / SM_PAGE_SIZE
;
1155 if (mem_node::InUseCount() + pages_needed
< store_pages_max
) {
1156 PROF_stop(storeGetMemSpace
);
1160 debugs(20, 2, "storeGetMemSpace: Starting, need " << pages_needed
<<
1163 /* XXX what to set as max_scan here? */
1164 walker
= mem_policy
->PurgeInit(mem_policy
, 100000);
1166 while ((e
= walker
->Next(walker
))) {
1170 if (mem_node::InUseCount() + pages_needed
< store_pages_max
)
1174 walker
->Done(walker
);
1175 debugs(20, 3, "storeGetMemSpace stats:");
1176 debugs(20, 3, " " << std::setw(6) << hot_obj_count
<< " HOT objects");
1177 debugs(20, 3, " " << std::setw(6) << released
<< " were released");
1178 PROF_stop(storeGetMemSpace
);
1181 /* thunk through to Store::Root().maintain(). Note that this would be better still
1182 * if registered against the root store itself, but that requires more complex
1183 * update logic - bigger fish to fry first. Long term each store when
1184 * it becomes active will self register
1187 Store::Maintain(void *notused
)
1189 Store::Root().maintain();
1191 /* Reregister a maintain event .. */
1192 eventAdd("MaintainSwapSpace", Maintain
, NULL
, 1.0, 1);
1196 /* The maximum objects to scan for maintain storage space */
1197 #define MAINTAIN_MAX_SCAN 1024
1198 #define MAINTAIN_MAX_REMOVE 64
1201 * This routine is to be called by main loop in main.c.
1202 * It removes expired objects on only one bucket for each time called.
1204 * This should get called 1/s from main().
1207 StoreController::maintain()
1209 static time_t last_warn_time
= 0;
1211 PROF_start(storeMaintainSwapSpace
);
1212 swapDir
->maintain();
1214 /* this should be emitted by the oversize dir, not globally */
1216 if (Store::Root().currentSize() > Store::Root().maxSize()) {
1217 if (squid_curtime
- last_warn_time
> 10) {
1218 debugs(20, DBG_CRITICAL
, "WARNING: Disk space over limit: "
1219 << Store::Root().currentSize() / 1024.0 << " KB > "
1220 << (Store::Root().maxSize() >> 10) << " KB");
1221 last_warn_time
= squid_curtime
;
1225 PROF_stop(storeMaintainSwapSpace
);
1228 /* release an object from a cache */
1230 StoreEntry::release()
1232 PROF_start(storeRelease
);
1233 debugs(20, 3, "releasing " << *this << ' ' << getMD5Text());
1234 /* If, for any reason we can't discard this object because of an
1235 * outstanding request, mark it for pending release */
1239 debugs(20, 3, "storeRelease: Only setting RELEASE_REQUEST bit");
1241 PROF_stop(storeRelease
);
1245 Store::Root().memoryUnlink(*this);
1247 if (StoreController::store_dirs_rebuilding
&& swap_filen
> -1) {
1250 if (swap_filen
> -1) {
1251 // lock the entry until rebuilding is done
1252 lock("storeLateRelease");
1254 LateReleaseStack
.push_back(this);
1256 destroyStoreEntry(static_cast<hash_link
*>(this));
1257 // "this" is no longer valid
1260 PROF_stop(storeRelease
);
1264 storeLog(STORE_LOG_RELEASE
, this);
1266 if (swap_filen
> -1) {
1267 // log before unlink() below clears swap_filen
1268 if (!EBIT_TEST(flags
, KEY_PRIVATE
))
1269 storeDirSwapLog(this, SWAP_LOG_DEL
);
1274 destroyStoreEntry(static_cast<hash_link
*>(this));
1275 PROF_stop(storeRelease
);
1279 storeLateRelease(void *unused
)
1285 if (StoreController::store_dirs_rebuilding
) {
1286 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
1290 for (i
= 0; i
< 10; ++i
) {
1291 e
= LateReleaseStack
.count
? LateReleaseStack
.pop() : NULL
;
1295 debugs(20, DBG_IMPORTANT
, "storeLateRelease: released " << n
<< " objects");
1299 e
->unlock("storeLateRelease");
1303 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 0.0, 1);
1306 /* return 1 if a store entry is locked */
1308 StoreEntry::locked() const
1314 * SPECIAL, PUBLIC entries should be "locked";
1315 * XXX: Their owner should lock them then instead of relying on this hack.
1317 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
1318 if (!EBIT_TEST(flags
, KEY_PRIVATE
))
1325 StoreEntry::validLength() const
1328 const HttpReply
*reply
;
1329 assert(mem_obj
!= NULL
);
1331 debugs(20, 3, "storeEntryValidLength: Checking '" << getMD5Text() << "'");
1332 debugs(20, 5, "storeEntryValidLength: object_len = " <<
1334 debugs(20, 5, "storeEntryValidLength: hdr_sz = " << reply
->hdr_sz
);
1335 debugs(20, 5, "storeEntryValidLength: content_length = " << reply
->content_length
);
1337 if (reply
->content_length
< 0) {
1338 debugs(20, 5, "storeEntryValidLength: Unspecified content length: " << getMD5Text());
1342 if (reply
->hdr_sz
== 0) {
1343 debugs(20, 5, "storeEntryValidLength: Zero header size: " << getMD5Text());
1347 if (mem_obj
->method
== Http::METHOD_HEAD
) {
1348 debugs(20, 5, "storeEntryValidLength: HEAD request: " << getMD5Text());
1352 if (reply
->sline
.status() == Http::scNotModified
)
1355 if (reply
->sline
.status() == Http::scNoContent
)
1358 diff
= reply
->hdr_sz
+ reply
->content_length
- objectLen();
1363 debugs(20, 3, "storeEntryValidLength: " << (diff
< 0 ? -diff
: diff
) << " bytes too " << (diff
< 0 ? "big" : "small") <<"; '" << getMD5Text() << "'" );
1369 storeRegisterWithCacheManager(void)
1371 Mgr::RegisterAction("storedir", "Store Directory Stats", Store::Stats
, 0, 1);
1372 Mgr::RegisterAction("store_io", "Store IO Interface Stats", &Mgr::StoreIoAction::Create
, 0, 1);
1373 Mgr::RegisterAction("store_check_cachable_stats", "storeCheckCachable() Stats",
1374 storeCheckCachableStats
, 0, 1);
1381 mem_policy
= createRemovalPolicy(Config
.memPolicy
);
1384 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
1385 Store::Root().init();
1386 storeRebuildStart();
1388 storeRegisterWithCacheManager();
1392 storeConfigure(void)
1394 store_swap_high
= (long) (((float) Store::Root().maxSize() *
1395 (float) Config
.Swap
.highWaterMark
) / (float) 100);
1396 store_swap_low
= (long) (((float) Store::Root().maxSize() *
1397 (float) Config
.Swap
.lowWaterMark
) / (float) 100);
1398 store_pages_max
= Config
.memMaxSize
/ sizeof(mem_node
);
1402 StoreEntry::memoryCachable() const
1404 if (mem_obj
== NULL
)
1407 if (mem_obj
->data_hdr
.size() == 0)
1410 if (mem_obj
->inmem_lo
!= 0)
1413 if (!Config
.onoff
.memory_cache_first
&& swap_status
== SWAPOUT_DONE
&& refcount
== 1)
1420 StoreEntry::checkNegativeHit() const
1422 if (!EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1425 if (expires
<= squid_curtime
)
1428 if (store_status
!= STORE_OK
)
1435 * Set object for negative caching.
1436 * Preserves any expiry information given by the server.
1437 * In absence of proper expiry info it will set to expire immediately,
1438 * or with HTTP-violations enabled the configured negative-TTL is observed
1441 StoreEntry::negativeCache()
1443 // XXX: should make the default for expires 0 instead of -1
1444 // so we can distinguish "Expires: -1" from nothing.
1446 #if USE_HTTP_VIOLATIONS
1447 expires
= squid_curtime
+ Config
.negativeTtl
;
1449 expires
= squid_curtime
;
1451 EBIT_SET(flags
, ENTRY_NEGCACHED
);
1455 storeFreeMemory(void)
1458 #if USE_CACHE_DIGESTS
1461 cacheDigestDestroy(store_digest
);
1465 store_digest
= NULL
;
1469 expiresMoreThan(time_t expires
, time_t when
)
1471 if (expires
< 0) /* No Expires given */
1474 return (expires
> (squid_curtime
+ when
));
1478 StoreEntry::validToSend() const
1480 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
1483 if (EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1484 if (expires
<= squid_curtime
)
1487 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
1490 // now check that the entry has a cache backing or is collapsed
1491 if (swap_filen
> -1) // backed by a disk cache
1494 if (swappingOut()) // will be backed by a disk cache
1497 if (!mem_obj
) // not backed by a memory cache and not collapsed
1500 if (mem_obj
->memCache
.index
>= 0) // backed by a shared memory cache
1503 // StoreEntry::storeClientType() assumes DISK_CLIENT here, but there is no
1504 // disk cache backing so we should not rely on the store cache at all. This
1505 // is wrong for range requests that could feed off nibbled memory (XXX).
1506 if (mem_obj
->inmem_lo
) // in local memory cache, but got nibbled at
1513 StoreEntry::timestampsSet()
1515 const HttpReply
*reply
= getReply();
1516 time_t served_date
= reply
->date
;
1517 int age
= reply
->header
.getInt(HDR_AGE
);
1518 /* Compute the timestamp, mimicking RFC2616 section 13.2.3. */
1519 /* make sure that 0 <= served_date <= squid_curtime */
1521 if (served_date
< 0 || served_date
> squid_curtime
)
1522 served_date
= squid_curtime
;
1525 * If the returned Date: is more than 24 hours older than
1526 * the squid_curtime, then one of us needs to use NTP to set our
1527 * clock. We'll pretend that our clock is right.
1529 else if (served_date
< (squid_curtime
- 24 * 60 * 60) )
1530 served_date
= squid_curtime
;
1533 * Compensate with Age header if origin server clock is ahead
1534 * of us and there is a cache in between us and the origin
1535 * server. But DONT compensate if the age value is larger than
1536 * squid_curtime because it results in a negative served_date.
1538 if (age
> squid_curtime
- served_date
)
1539 if (squid_curtime
> age
)
1540 served_date
= squid_curtime
- age
;
1542 // compensate for Squid-to-server and server-to-Squid delays
1543 if (mem_obj
&& mem_obj
->request
) {
1544 const time_t request_sent
=
1545 mem_obj
->request
->hier
.peer_http_request_sent
.tv_sec
;
1546 if (0 < request_sent
&& request_sent
< squid_curtime
)
1547 served_date
-= (squid_curtime
- request_sent
);
1550 if (reply
->expires
> 0 && reply
->date
> -1)
1551 expires
= served_date
+ (reply
->expires
- reply
->date
);
1553 expires
= reply
->expires
;
1555 lastmod
= reply
->last_modified
;
1557 timestamp
= served_date
;
1561 StoreEntry::registerAbort(STABH
* cb
, void *data
)
1564 assert(mem_obj
->abort
.callback
== NULL
);
1565 mem_obj
->abort
.callback
= cb
;
1566 mem_obj
->abort
.data
= cbdataReference(data
);
1570 StoreEntry::unregisterAbort()
1573 if (mem_obj
->abort
.callback
) {
1574 mem_obj
->abort
.callback
= NULL
;
1575 cbdataReferenceDone(mem_obj
->abort
.data
);
1580 StoreEntry::dump(int l
) const
1582 debugs(20, l
, "StoreEntry->key: " << getMD5Text());
1583 debugs(20, l
, "StoreEntry->next: " << next
);
1584 debugs(20, l
, "StoreEntry->mem_obj: " << mem_obj
);
1585 debugs(20, l
, "StoreEntry->timestamp: " << timestamp
);
1586 debugs(20, l
, "StoreEntry->lastref: " << lastref
);
1587 debugs(20, l
, "StoreEntry->expires: " << expires
);
1588 debugs(20, l
, "StoreEntry->lastmod: " << lastmod
);
1589 debugs(20, l
, "StoreEntry->swap_file_sz: " << swap_file_sz
);
1590 debugs(20, l
, "StoreEntry->refcount: " << refcount
);
1591 debugs(20, l
, "StoreEntry->flags: " << storeEntryFlags(this));
1592 debugs(20, l
, "StoreEntry->swap_dirn: " << swap_dirn
);
1593 debugs(20, l
, "StoreEntry->swap_filen: " << swap_filen
);
1594 debugs(20, l
, "StoreEntry->lock_count: " << lock_count
);
1595 debugs(20, l
, "StoreEntry->mem_status: " << mem_status
);
1596 debugs(20, l
, "StoreEntry->ping_status: " << ping_status
);
1597 debugs(20, l
, "StoreEntry->store_status: " << store_status
);
1598 debugs(20, l
, "StoreEntry->swap_status: " << swap_status
);
1602 * NOTE, this function assumes only two mem states
1605 StoreEntry::setMemStatus(mem_status_t new_status
)
1607 if (new_status
== mem_status
)
1610 // are we using a shared memory cache?
1611 if (Config
.memShared
&& IamWorkerProcess()) {
1612 // This method was designed to update replacement policy, not to
1613 // actually purge something from the memory cache (TODO: rename?).
1614 // Shared memory cache does not have a policy that needs updates.
1615 mem_status
= new_status
;
1619 assert(mem_obj
!= NULL
);
1621 if (new_status
== IN_MEMORY
) {
1622 assert(mem_obj
->inmem_lo
== 0);
1624 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1625 debugs(20, 4, "not inserting special " << *this << " into policy");
1627 mem_policy
->Add(mem_policy
, this, &mem_obj
->repl
);
1628 debugs(20, 4, "inserted " << *this << " key: " << getMD5Text());
1631 ++hot_obj_count
; // TODO: maintain for the shared hot cache as well
1633 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1634 debugs(20, 4, "not removing special " << *this << " from policy");
1636 mem_policy
->Remove(mem_policy
, this, &mem_obj
->repl
);
1637 debugs(20, 4, "removed " << *this);
1643 mem_status
= new_status
;
1647 StoreEntry::url() const
1650 return "[null_entry]";
1651 else if (mem_obj
== NULL
)
1652 return "[null_mem_obj]";
1654 return mem_obj
->storeId();
1658 StoreEntry::makeMemObject()
1661 mem_obj
= new MemObject();
1666 StoreEntry::createMemObject(const char *aUrl
, const char *aLogUrl
, const HttpRequestMethod
&aMethod
)
1669 mem_obj
->setUris(aUrl
, aLogUrl
, aMethod
);
1672 /* this just sets DELAY_SENDING */
1674 StoreEntry::buffer()
1676 EBIT_SET(flags
, DELAY_SENDING
);
1679 /* this just clears DELAY_SENDING and Invokes the handlers */
1683 if (EBIT_TEST(flags
, DELAY_SENDING
)) {
1684 EBIT_CLR(flags
, DELAY_SENDING
);
1690 StoreEntry::objectLen() const
1692 assert(mem_obj
!= NULL
);
1693 return mem_obj
->object_sz
;
1697 StoreEntry::contentLen() const
1699 assert(mem_obj
!= NULL
);
1700 assert(getReply() != NULL
);
1701 return objectLen() - getReply()->hdr_sz
;
1705 StoreEntry::getReply () const
1707 if (NULL
== mem_obj
)
1710 return mem_obj
->getReply();
1717 debugs(20, 3, "StoreEntry::reset: " << url());
1719 HttpReply
*rep
= (HttpReply
*) getReply(); // bypass const
1721 expires
= lastmod
= timestamp
= -1;
1727 * This routine calls the SETUP routine for each fs type.
1728 * I don't know where the best place for this is, and I'm not going to shuffle
1729 * around large chunks of code right now (that can be done once its working.)
1738 * called to add another store removal policy module
1741 storeReplAdd(const char *type
, REMOVALPOLICYCREATE
* create
)
1745 /* find the number of currently known repl types */
1746 for (i
= 0; storerepl_list
&& storerepl_list
[i
].typestr
; ++i
) {
1747 if (strcmp(storerepl_list
[i
].typestr
, type
) == 0) {
1748 debugs(20, DBG_IMPORTANT
, "WARNING: Trying to load store replacement policy " << type
<< " twice.");
1753 /* add the new type */
1754 storerepl_list
= static_cast<storerepl_entry_t
*>(xrealloc(storerepl_list
, (i
+ 2) * sizeof(storerepl_entry_t
)));
1756 memset(&storerepl_list
[i
+ 1], 0, sizeof(storerepl_entry_t
));
1758 storerepl_list
[i
].typestr
= type
;
1760 storerepl_list
[i
].create
= create
;
1764 * Create a removal policy instance
1767 createRemovalPolicy(RemovalPolicySettings
* settings
)
1769 storerepl_entry_t
*r
;
1771 for (r
= storerepl_list
; r
&& r
->typestr
; ++r
) {
1772 if (strcmp(r
->typestr
, settings
->type
) == 0)
1773 return r
->create(settings
->args
);
1776 debugs(20, DBG_IMPORTANT
, "ERROR: Unknown policy " << settings
->type
);
1777 debugs(20, DBG_IMPORTANT
, "ERROR: Be sure to have set cache_replacement_policy");
1778 debugs(20, DBG_IMPORTANT
, "ERROR: and memory_replacement_policy in squid.conf!");
1779 fatalf("ERROR: Unknown policy %s\n", settings
->type
);
1780 return NULL
; /* NOTREACHED */
1785 storeSwapFileNumberSet(StoreEntry
* e
, sfileno filn
)
1787 if (e
->swap_file_number
== filn
)
1792 storeDirMapBitReset(e
->swap_file_number
);
1793 storeDirLRUDelete(e
);
1794 e
->swap_file_number
= -1;
1796 assert(-1 == e
->swap_file_number
);
1797 storeDirMapBitSet(e
->swap_file_number
= filn
);
1805 * Replace a store entry with
1806 * a new reply. This eats the reply.
1809 StoreEntry::replaceHttpReply(HttpReply
*rep
, bool andStartWriting
)
1811 debugs(20, 3, "StoreEntry::replaceHttpReply: " << url());
1814 debugs(20, DBG_CRITICAL
, "Attempt to replace object with no in-memory representation");
1818 mem_obj
->replaceHttpReply(rep
);
1820 if (andStartWriting
)
1825 StoreEntry::startWriting()
1829 /* TODO: when we store headers serparately remove the header portion */
1830 /* TODO: mark the length of the headers ? */
1831 /* We ONLY want the headers */
1832 packerToStoreInit(&p
, this);
1837 const HttpReply
*rep
= getReply();
1840 rep
->packHeadersInto(&p
);
1841 mem_obj
->markEndOfReplyHeaders();
1842 EBIT_CLR(flags
, ENTRY_FWD_HDR_WAIT
);
1844 rep
->body
.packInto(&p
);
1850 StoreEntry::getSerialisedMetaData()
1852 StoreMeta
*tlv_list
= storeSwapMetaBuild(this);
1854 char *result
= storeSwapMetaPack(tlv_list
, &swap_hdr_sz
);
1855 storeSwapTLVFree(tlv_list
);
1856 assert (swap_hdr_sz
>= 0);
1857 mem_obj
->swap_hdr_sz
= (size_t) swap_hdr_sz
;
1862 StoreEntry::trimMemory(const bool preserveSwappable
)
1866 * Bug #1943. We must not let go any data for IN_MEMORY
1867 * objects. We have to wait until the mem_status changes.
1869 if (mem_status
== IN_MEMORY
)
1872 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
1873 return; // cannot trim because we do not load them again
1875 if (preserveSwappable
)
1876 mem_obj
->trimSwappable();
1878 mem_obj
->trimUnSwappable();
1880 debugs(88, 7, *this << " inmem_lo=" << mem_obj
->inmem_lo
);
1884 StoreEntry::modifiedSince(HttpRequest
* request
) const
1887 time_t mod_time
= lastmod
;
1890 mod_time
= timestamp
;
1892 debugs(88, 3, "modifiedSince: '" << url() << "'");
1894 debugs(88, 3, "modifiedSince: mod_time = " << mod_time
);
1899 /* Find size of the object */
1900 object_length
= getReply()->content_length
;
1902 if (object_length
< 0)
1903 object_length
= contentLen();
1905 if (mod_time
> request
->ims
) {
1906 debugs(88, 3, "--> YES: entry newer than client");
1908 } else if (mod_time
< request
->ims
) {
1909 debugs(88, 3, "--> NO: entry older than client");
1911 } else if (request
->imslen
< 0) {
1912 debugs(88, 3, "--> NO: same LMT, no client length");
1914 } else if (request
->imslen
== object_length
) {
1915 debugs(88, 3, "--> NO: same LMT, same length");
1918 debugs(88, 3, "--> YES: same LMT, different length");
1924 StoreEntry::hasEtag(ETag
&etag
) const
1926 if (const HttpReply
*reply
= getReply()) {
1927 etag
= reply
->header
.getETag(HDR_ETAG
);
1935 StoreEntry::hasIfMatchEtag(const HttpRequest
&request
) const
1937 const String reqETags
= request
.header
.getList(HDR_IF_MATCH
);
1938 return hasOneOfEtags(reqETags
, false);
1942 StoreEntry::hasIfNoneMatchEtag(const HttpRequest
&request
) const
1944 const String reqETags
= request
.header
.getList(HDR_IF_NONE_MATCH
);
1945 // weak comparison is allowed only for HEAD or full-body GET requests
1946 const bool allowWeakMatch
= !request
.flags
.isRanged
&&
1947 (request
.method
== Http::METHOD_GET
|| request
.method
== Http::METHOD_HEAD
);
1948 return hasOneOfEtags(reqETags
, allowWeakMatch
);
1951 /// whether at least one of the request ETags matches entity ETag
1953 StoreEntry::hasOneOfEtags(const String
&reqETags
, const bool allowWeakMatch
) const
1955 const ETag repETag
= getReply()->header
.getETag(HDR_ETAG
);
1957 return strListIsMember(&reqETags
, "*", ',');
1959 bool matched
= false;
1960 const char *pos
= NULL
;
1963 while (!matched
&& strListGetItem(&reqETags
, ',', &item
, &ilen
, &pos
)) {
1964 if (!strncmp(item
, "*", ilen
))
1968 str
.append(item
, ilen
);
1970 if (etagParseInit(&reqETag
, str
.termedBuf())) {
1971 matched
= allowWeakMatch
? etagIsWeakEqual(repETag
, reqETag
) :
1972 etagIsStrongEqual(repETag
, reqETag
);
1980 StoreEntry::store() const
1982 assert(0 <= swap_dirn
&& swap_dirn
< Config
.cacheSwap
.n_configured
);
1983 return INDEXSD(swap_dirn
);
1987 StoreEntry::unlink()
1989 store()->unlink(*this); // implies disconnect()
1992 swap_status
= SWAPOUT_NONE
;
1996 * return true if the entry is in a state where
1997 * it can accept more data (ie with write() method)
2000 StoreEntry::isAccepting() const
2002 if (STORE_PENDING
!= store_status
)
2005 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
2011 std::ostream
&operator <<(std::ostream
&os
, const StoreEntry
&e
)
2016 if (e
.mem_obj
->xitTable
.index
> -1)
2017 os
<< 't' << e
.mem_obj
->xitTable
.index
;
2018 if (e
.mem_obj
->memCache
.index
> -1)
2019 os
<< 'm' << e
.mem_obj
->memCache
.index
;
2021 if (e
.swap_filen
> -1 || e
.swap_dirn
> -1)
2022 os
<< 'd' << e
.swap_filen
<< '@' << e
.swap_dirn
;
2026 // print only non-default status values, using unique letters
2027 if (e
.mem_status
!= NOT_IN_MEMORY
||
2028 e
.store_status
!= STORE_PENDING
||
2029 e
.swap_status
!= SWAPOUT_NONE
||
2030 e
.ping_status
!= PING_NONE
) {
2031 if (e
.mem_status
!= NOT_IN_MEMORY
) os
<< 'm';
2032 if (e
.store_status
!= STORE_PENDING
) os
<< 's';
2033 if (e
.swap_status
!= SWAPOUT_NONE
) os
<< 'w' << e
.swap_status
;
2034 if (e
.ping_status
!= PING_NONE
) os
<< 'p' << e
.ping_status
;
2037 // print only set flags, using unique letters
2039 if (EBIT_TEST(e
.flags
, ENTRY_SPECIAL
)) os
<< 'S';
2040 if (EBIT_TEST(e
.flags
, ENTRY_REVALIDATE
)) os
<< 'R';
2041 if (EBIT_TEST(e
.flags
, DELAY_SENDING
)) os
<< 'P';
2042 if (EBIT_TEST(e
.flags
, RELEASE_REQUEST
)) os
<< 'X';
2043 if (EBIT_TEST(e
.flags
, REFRESH_REQUEST
)) os
<< 'F';
2044 if (EBIT_TEST(e
.flags
, ENTRY_DISPATCHED
)) os
<< 'D';
2045 if (EBIT_TEST(e
.flags
, KEY_PRIVATE
)) os
<< 'I';
2046 if (EBIT_TEST(e
.flags
, ENTRY_FWD_HDR_WAIT
)) os
<< 'W';
2047 if (EBIT_TEST(e
.flags
, ENTRY_NEGCACHED
)) os
<< 'N';
2048 if (EBIT_TEST(e
.flags
, ENTRY_VALIDATED
)) os
<< 'V';
2049 if (EBIT_TEST(e
.flags
, ENTRY_BAD_LENGTH
)) os
<< 'L';
2050 if (EBIT_TEST(e
.flags
, ENTRY_ABORTED
)) os
<< 'A';
2053 if (e
.mem_obj
&& e
.mem_obj
->smpCollapsed
)
2056 return os
<< '/' << &e
<< '*' << e
.locks();
2059 /* NullStoreEntry */
2061 NullStoreEntry
NullStoreEntry::_instance
;
2064 NullStoreEntry::getInstance()
2070 NullStoreEntry::getMD5Text() const
2076 NullStoreEntry::operator delete(void*)
2078 fatal ("Attempt to delete NullStoreEntry\n");
2082 NullStoreEntry::getSerialisedMetaData()
2088 #include "Store.cci"