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, 5, "StoreEntry constructed, this=" << this);
398 StoreEntry::~StoreEntry()
400 debugs(20, 5, "StoreEntry destructed, this=" << this);
405 StoreEntry::deferProducer(const AsyncCall::Pointer
&producer
)
407 if (!deferredProducer
)
408 deferredProducer
= producer
;
410 debugs(20, 5, HERE
<< "Deferred producer call is allready set to: " <<
411 *deferredProducer
<< ", requested call: " << *producer
);
415 StoreEntry::kickProducer()
417 if (deferredProducer
!= NULL
) {
418 ScheduleCallHere(deferredProducer
);
419 deferredProducer
= NULL
;
425 StoreEntry::destroyMemObject()
427 debugs(20, 3, HERE
<< "destroyMemObject " << mem_obj
);
429 if (MemObject
*mem
= mem_obj
) {
430 // Store::Root() is FATALly missing during shutdown
431 if (mem
->xitTable
.index
>= 0 && !shutting_down
)
432 Store::Root().transientsDisconnect(*mem
);
433 if (mem
->memCache
.index
>= 0 && !shutting_down
)
434 Store::Root().memoryDisconnect(*this);
436 setMemStatus(NOT_IN_MEMORY
);
443 destroyStoreEntry(void *data
)
445 debugs(20, 3, HERE
<< "destroyStoreEntry: destroying " << data
);
446 StoreEntry
*e
= static_cast<StoreEntry
*>(static_cast<hash_link
*>(data
));
449 if (e
== NullStoreEntry::getInstance())
452 // Store::Root() is FATALly missing during shutdown
453 if (e
->swap_filen
>= 0 && !shutting_down
) {
454 SwapDir
&sd
= dynamic_cast<SwapDir
&>(*e
->store());
458 e
->destroyMemObject();
462 assert(e
->key
== NULL
);
467 /* ----- INTERFACE BETWEEN STORAGE MANAGER AND HASH TABLE FUNCTIONS --------- */
470 StoreEntry::hashInsert(const cache_key
* someKey
)
472 debugs(20, 3, "StoreEntry::hashInsert: Inserting Entry " << *this << " key '" << storeKeyText(someKey
) << "'");
473 key
= storeKeyDup(someKey
);
474 hash_join(store_table
, this);
478 StoreEntry::hashDelete()
480 if (key
) { // some test cases do not create keys and do not hashInsert()
481 hash_remove_link(store_table
, this);
482 storeKeyFree((const cache_key
*)key
);
487 /* -------------------------------------------------------------------------- */
489 /* get rid of memory copy of the object */
491 StoreEntry::purgeMem()
496 debugs(20, 3, "StoreEntry::purgeMem: Freeing memory-copy of " << getMD5Text());
498 Store::Root().memoryUnlink(*this);
500 if (swap_status
!= SWAPOUT_DONE
)
505 StoreEntry::lock(const char *context
)
508 debugs(20, 3, context
<< " locked key " << getMD5Text() << ' ' << *this);
512 StoreEntry::touch() {
513 lastref
= squid_curtime
;
514 Store::Root().reference(*this);
518 StoreEntry::setReleaseFlag()
520 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
523 debugs(20, 3, "StoreEntry::setReleaseFlag: '" << getMD5Text() << "'");
525 EBIT_SET(flags
, RELEASE_REQUEST
);
527 Store::Root().markForUnlink(*this);
531 StoreEntry::releaseRequest()
533 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
536 setReleaseFlag(); // makes validToSend() false, preventing future hits
542 StoreEntry::unlock(const char *context
)
544 debugs(20, 3, (context
? context
: "somebody") <<
545 " unlocking key " << getMD5Text() << ' ' << *this);
546 assert(lock_count
> 0);
550 return (int) lock_count
;
552 if (store_status
== STORE_PENDING
)
555 assert(storePendingNClients(this) == 0);
557 if (EBIT_TEST(flags
, RELEASE_REQUEST
)) {
562 if (EBIT_TEST(flags
, KEY_PRIVATE
))
563 debugs(20, DBG_IMPORTANT
, "WARNING: " << __FILE__
<< ":" << __LINE__
<< ": found KEY_PRIVATE");
565 Store::Root().handleIdleEntry(*this); // may delete us
570 StoreEntry::getPublicByRequestMethod (StoreClient
*aClient
, HttpRequest
* request
, const HttpRequestMethod
& method
)
573 StoreEntry
*result
= storeGetPublicByRequestMethod( request
, method
);
576 aClient
->created (NullStoreEntry::getInstance());
578 aClient
->created (result
);
582 StoreEntry::getPublicByRequest (StoreClient
*aClient
, HttpRequest
* request
)
585 StoreEntry
*result
= storeGetPublicByRequest (request
);
588 result
= NullStoreEntry::getInstance();
590 aClient
->created (result
);
594 StoreEntry::getPublic (StoreClient
*aClient
, const char *uri
, const HttpRequestMethod
& method
)
597 StoreEntry
*result
= storeGetPublic (uri
, method
);
600 result
= NullStoreEntry::getInstance();
602 aClient
->created (result
);
606 storeGetPublic(const char *uri
, const HttpRequestMethod
& method
)
608 return Store::Root().get(storeKeyPublic(uri
, method
));
612 storeGetPublicByRequestMethod(HttpRequest
* req
, const HttpRequestMethod
& method
)
614 return Store::Root().get(storeKeyPublicByRequestMethod(req
, method
));
618 storeGetPublicByRequest(HttpRequest
* req
)
620 StoreEntry
*e
= storeGetPublicByRequestMethod(req
, req
->method
);
622 if (e
== NULL
&& req
->method
== Http::METHOD_HEAD
)
623 /* We can generate a HEAD reply from a cached GET object */
624 e
= storeGetPublicByRequestMethod(req
, Http::METHOD_GET
);
632 static int key_counter
= 0;
634 if (++key_counter
< 0)
640 /* RBC 20050104 AFAICT this should become simpler:
641 * rather than reinserting with a special key it should be marked
642 * as 'released' and then cleaned up when refcounting indicates.
643 * the StoreHashIndex could well implement its 'released' in the
645 * Also, clean log writing should skip over ia,t
646 * Otherwise, we need a 'remove from the index but not the store
650 StoreEntry::setPrivateKey()
652 const cache_key
*newkey
;
654 if (key
&& EBIT_TEST(flags
, KEY_PRIVATE
))
655 return; /* is already private */
658 setReleaseFlag(); // will markForUnlink(); all caches/workers will know
660 // TODO: move into SwapDir::markForUnlink() already called by Root()
662 storeDirSwapLog(this, SWAP_LOG_DEL
);
667 if (mem_obj
&& mem_obj
->hasUris()) {
668 mem_obj
->id
= getKeyCounter();
669 newkey
= storeKeyPrivate(mem_obj
->storeId(), mem_obj
->method
, mem_obj
->id
);
671 newkey
= storeKeyPrivate("JUNK", Http::METHOD_NONE
, getKeyCounter());
674 assert(hash_lookup(store_table
, newkey
) == NULL
);
675 EBIT_SET(flags
, KEY_PRIVATE
);
680 StoreEntry::setPublicKey()
682 const cache_key
*newkey
;
684 if (key
&& !EBIT_TEST(flags
, KEY_PRIVATE
))
685 return; /* is already public */
690 * We can't make RELEASE_REQUEST objects public. Depending on
691 * when RELEASE_REQUEST gets set, we might not be swapping out
692 * the object. If we're not swapping out, then subsequent
693 * store clients won't be able to access object data which has
694 * been freed from memory.
696 * If RELEASE_REQUEST is set, setPublicKey() should not be called.
698 #if MORE_DEBUG_OUTPUT
700 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
701 debugs(20, DBG_IMPORTANT
, "assertion failed: RELEASE key " << key
<< ", url " << mem_obj
->url
);
705 assert(!EBIT_TEST(flags
, RELEASE_REQUEST
));
707 if (mem_obj
->request
) {
708 HttpRequest
*request
= mem_obj
->request
;
710 if (!mem_obj
->vary_headers
) {
711 /* First handle the case where the object no longer varies */
712 safe_free(request
->vary_headers
);
714 if (request
->vary_headers
&& strcmp(request
->vary_headers
, mem_obj
->vary_headers
) != 0) {
715 /* Oops.. the variance has changed. Kill the base object
716 * to record the new variance key
718 safe_free(request
->vary_headers
); /* free old "bad" variance key */
719 if (StoreEntry
*pe
= storeGetPublic(mem_obj
->storeId(), mem_obj
->method
))
723 /* Make sure the request knows the variance status */
724 if (!request
->vary_headers
) {
725 const char *vary
= httpMakeVaryMark(request
, mem_obj
->getReply());
728 request
->vary_headers
= xstrdup(vary
);
732 // TODO: storeGetPublic() calls below may create unlocked entries.
733 // We should add/use storeHas() API or lock/unlock those entries.
734 if (mem_obj
->vary_headers
&& !storeGetPublic(mem_obj
->storeId(), mem_obj
->method
)) {
735 /* Create "vary" base object */
737 StoreEntry
*pe
= storeCreateEntry(mem_obj
->storeId(), mem_obj
->logUri(), request
->flags
, request
->method
);
738 /* We are allowed to do this typecast */
739 HttpReply
*rep
= new HttpReply
;
740 rep
->setHeaders(Http::scOkay
, "Internal marker object", "x-squid-internal/vary", -1, -1, squid_curtime
+ 100000);
741 vary
= mem_obj
->getReply()->header
.getList(HDR_VARY
);
744 /* Again, we own this structure layout */
745 rep
->header
.putStr(HDR_VARY
, vary
.termedBuf());
749 #if X_ACCELERATOR_VARY
750 vary
= mem_obj
->getReply()->header
.getList(HDR_X_ACCELERATOR_VARY
);
752 if (vary
.size() > 0) {
753 /* Again, we own this structure layout */
754 rep
->header
.putStr(HDR_X_ACCELERATOR_VARY
, vary
.termedBuf());
759 pe
->replaceHttpReply(rep
, false); // no write until key is public
765 pe
->startWriting(); // after makePublic()
769 pe
->unlock("StoreEntry::setPublicKey+Vary");
772 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
774 newkey
= storeKeyPublic(mem_obj
->storeId(), mem_obj
->method
);
776 if (StoreEntry
*e2
= (StoreEntry
*)hash_lookup(store_table
, newkey
)) {
777 debugs(20, 3, "Making old " << *e2
<< " private.");
781 if (mem_obj
->request
)
782 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
784 newkey
= storeKeyPublic(mem_obj
->storeId(), mem_obj
->method
);
790 EBIT_CLR(flags
, KEY_PRIVATE
);
795 storeDirSwapLog(this, SWAP_LOG_ADD
);
799 storeCreatePureEntry(const char *url
, const char *log_url
, const RequestFlags
&flags
, const HttpRequestMethod
& method
)
801 StoreEntry
*e
= NULL
;
802 debugs(20, 3, "storeCreateEntry: '" << url
<< "'");
804 e
= new StoreEntry();
806 e
->mem_obj
->setUris(url
, log_url
, method
);
808 if (flags
.cachable
) {
809 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
814 e
->store_status
= STORE_PENDING
;
816 e
->lastref
= squid_curtime
;
817 e
->timestamp
= -1; /* set in StoreEntry::timestampsSet() */
818 e
->ping_status
= PING_NONE
;
819 EBIT_SET(e
->flags
, ENTRY_VALIDATED
);
824 storeCreateEntry(const char *url
, const char *logUrl
, const RequestFlags
&flags
, const HttpRequestMethod
& method
)
826 StoreEntry
*e
= storeCreatePureEntry(url
, logUrl
, flags
, method
);
827 e
->lock("storeCreateEntry");
829 if (neighbors_do_private_keys
|| !flags
.hierarchical
)
837 /* Mark object as expired */
839 StoreEntry::expireNow()
841 debugs(20, 3, "StoreEntry::expireNow: '" << getMD5Text() << "'");
842 expires
= squid_curtime
;
846 StoreEntry::write (StoreIOBuffer writeBuffer
)
848 assert(mem_obj
!= NULL
);
849 /* This assert will change when we teach the store to update */
850 PROF_start(StoreEntry_write
);
851 assert(store_status
== STORE_PENDING
);
853 // XXX: caller uses content offset, but we also store headers
854 if (const HttpReply
*reply
= mem_obj
->getReply())
855 writeBuffer
.offset
+= reply
->hdr_sz
;
857 debugs(20, 5, "storeWrite: writing " << writeBuffer
.length
<< " bytes for '" << getMD5Text() << "'");
858 PROF_stop(StoreEntry_write
);
859 storeGetMemSpace(writeBuffer
.length
);
860 mem_obj
->write(writeBuffer
);
862 if (!EBIT_TEST(flags
, DELAY_SENDING
))
866 /* Append incoming data from a primary server to an entry. */
868 StoreEntry::append(char const *buf
, int len
)
870 assert(mem_obj
!= NULL
);
872 assert(store_status
== STORE_PENDING
);
874 StoreIOBuffer tempBuffer
;
875 tempBuffer
.data
= (char *)buf
;
876 tempBuffer
.length
= len
;
878 * XXX sigh, offset might be < 0 here, but it gets "corrected"
879 * later. This offset crap is such a mess.
881 tempBuffer
.offset
= mem_obj
->endOffset() - (getReply() ? getReply()->hdr_sz
: 0);
886 storeAppendPrintf(StoreEntry
* e
, const char *fmt
,...)
891 storeAppendVPrintf(e
, fmt
, args
);
895 /* used be storeAppendPrintf and Packer */
897 storeAppendVPrintf(StoreEntry
* e
, const char *fmt
, va_list vargs
)
899 LOCAL_ARRAY(char, buf
, 4096);
901 vsnprintf(buf
, 4096, fmt
, vargs
);
902 e
->append(buf
, strlen(buf
));
905 struct _store_check_cachable_hist
{
909 int not_entry_cachable
;
910 int wrong_content_length
;
915 int too_many_open_files
;
916 int too_many_open_fds
;
922 } store_check_cachable_hist
;
925 storeTooManyDiskFilesOpen(void)
927 if (Config
.max_open_disk_fds
== 0)
930 if (store_open_disk_fd
> Config
.max_open_disk_fds
)
937 StoreEntry::checkTooSmall()
939 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
942 if (STORE_OK
== store_status
)
943 if (mem_obj
->object_sz
< 0 ||
944 mem_obj
->object_sz
< Config
.Store
.minObjectSize
)
946 if (getReply()->content_length
> -1)
947 if (getReply()->content_length
< Config
.Store
.minObjectSize
)
952 // TODO: remove checks already performed by swapoutPossible()
953 // TODO: move "too many open..." checks outside -- we are called too early/late
955 StoreEntry::checkCachable()
957 #if CACHE_ALL_METHODS
959 if (mem_obj
->method
!= Http::METHOD_GET
) {
960 debugs(20, 2, "StoreEntry::checkCachable: NO: non-GET method");
961 ++store_check_cachable_hist
.no
.non_get
;
964 if (store_status
== STORE_OK
&& EBIT_TEST(flags
, ENTRY_BAD_LENGTH
)) {
965 debugs(20, 2, "StoreEntry::checkCachable: NO: wrong content-length");
966 ++store_check_cachable_hist
.no
.wrong_content_length
;
967 } else if (EBIT_TEST(flags
, RELEASE_REQUEST
)) {
968 debugs(20, 2, "StoreEntry::checkCachable: NO: not cachable");
969 ++store_check_cachable_hist
.no
.not_entry_cachable
; // TODO: rename?
970 } else if (EBIT_TEST(flags
, ENTRY_NEGCACHED
)) {
971 debugs(20, 3, "StoreEntry::checkCachable: NO: negative cached");
972 ++store_check_cachable_hist
.no
.negative_cached
;
973 return 0; /* avoid release call below */
974 } else if ((getReply()->content_length
> 0 &&
975 getReply()->content_length
> store_maxobjsize
) ||
976 mem_obj
->endOffset() > store_maxobjsize
) {
977 debugs(20, 2, "StoreEntry::checkCachable: NO: too big");
978 ++store_check_cachable_hist
.no
.too_big
;
979 } else if (checkTooSmall()) {
980 debugs(20, 2, "StoreEntry::checkCachable: NO: too small");
981 ++store_check_cachable_hist
.no
.too_small
;
982 } else if (EBIT_TEST(flags
, KEY_PRIVATE
)) {
983 debugs(20, 3, "StoreEntry::checkCachable: NO: private key");
984 ++store_check_cachable_hist
.no
.private_key
;
985 } else if (swap_status
!= SWAPOUT_NONE
) {
987 * here we checked the swap_status because the remaining
988 * cases are only relevant only if we haven't started swapping
989 * out the object yet.
992 } else if (storeTooManyDiskFilesOpen()) {
993 debugs(20, 2, "StoreEntry::checkCachable: NO: too many disk files open");
994 ++store_check_cachable_hist
.no
.too_many_open_files
;
995 } else if (fdNFree() < RESERVED_FD
) {
996 debugs(20, 2, "StoreEntry::checkCachable: NO: too many FD's open");
997 ++store_check_cachable_hist
.no
.too_many_open_fds
;
999 ++store_check_cachable_hist
.yes
.Default
;
1008 storeCheckCachableStats(StoreEntry
*sentry
)
1010 storeAppendPrintf(sentry
, "Category\t Count\n");
1012 #if CACHE_ALL_METHODS
1014 storeAppendPrintf(sentry
, "no.non_get\t%d\n",
1015 store_check_cachable_hist
.no
.non_get
);
1018 storeAppendPrintf(sentry
, "no.not_entry_cachable\t%d\n",
1019 store_check_cachable_hist
.no
.not_entry_cachable
);
1020 storeAppendPrintf(sentry
, "no.wrong_content_length\t%d\n",
1021 store_check_cachable_hist
.no
.wrong_content_length
);
1022 storeAppendPrintf(sentry
, "no.negative_cached\t%d\n",
1023 store_check_cachable_hist
.no
.negative_cached
);
1024 storeAppendPrintf(sentry
, "no.too_big\t%d\n",
1025 store_check_cachable_hist
.no
.too_big
);
1026 storeAppendPrintf(sentry
, "no.too_small\t%d\n",
1027 store_check_cachable_hist
.no
.too_small
);
1028 storeAppendPrintf(sentry
, "no.private_key\t%d\n",
1029 store_check_cachable_hist
.no
.private_key
);
1030 storeAppendPrintf(sentry
, "no.too_many_open_files\t%d\n",
1031 store_check_cachable_hist
.no
.too_many_open_files
);
1032 storeAppendPrintf(sentry
, "no.too_many_open_fds\t%d\n",
1033 store_check_cachable_hist
.no
.too_many_open_fds
);
1034 storeAppendPrintf(sentry
, "yes.default\t%d\n",
1035 store_check_cachable_hist
.yes
.Default
);
1039 StoreEntry::complete()
1041 debugs(20, 3, "storeComplete: '" << getMD5Text() << "'");
1043 if (store_status
!= STORE_PENDING
) {
1045 * if we're not STORE_PENDING, then probably we got aborted
1046 * and there should be NO clients on this entry
1048 assert(EBIT_TEST(flags
, ENTRY_ABORTED
));
1049 assert(mem_obj
->nclients
== 0);
1053 /* This is suspect: mem obj offsets include the headers. do we adjust for that
1054 * in use of object_sz?
1056 mem_obj
->object_sz
= mem_obj
->endOffset();
1058 store_status
= STORE_OK
;
1060 assert(mem_status
== NOT_IN_MEMORY
);
1062 if (!validLength()) {
1063 EBIT_SET(flags
, ENTRY_BAD_LENGTH
);
1067 #if USE_CACHE_DIGESTS
1068 if (mem_obj
->request
)
1069 mem_obj
->request
->hier
.store_complete_stop
= current_time
;
1073 * We used to call invokeHandlers, then storeSwapOut. However,
1074 * Madhukar Reddy <myreddy@persistence.com> reported that
1075 * responses without content length would sometimes get released
1076 * in client_side, thinking that the response is incomplete.
1082 * Someone wants to abort this transfer. Set the reason in the
1083 * request structure, call the server-side callback and mark the
1084 * entry for releasing
1089 ++statCounter
.aborted_requests
;
1090 assert(store_status
== STORE_PENDING
);
1091 assert(mem_obj
!= NULL
);
1092 debugs(20, 6, "storeAbort: " << getMD5Text());
1094 lock("StoreEntry::abort"); /* lock while aborting */
1099 EBIT_SET(flags
, ENTRY_ABORTED
);
1101 setMemStatus(NOT_IN_MEMORY
);
1103 store_status
= STORE_OK
;
1105 /* Notify the server side */
1109 * Should we check abort.data for validity?
1111 if (mem_obj
->abort
.callback
) {
1112 if (!cbdataReferenceValid(mem_obj
->abort
.data
))
1113 debugs(20, DBG_IMPORTANT
,HERE
<< "queueing event when abort.data is not valid");
1114 eventAdd("mem_obj->abort.callback",
1115 mem_obj
->abort
.callback
,
1116 mem_obj
->abort
.data
,
1122 /* XXX Should we reverse these two, so that there is no
1123 * unneeded disk swapping triggered?
1125 /* Notify the client side */
1128 // abort swap out, invalidating what was created so far (release follows)
1129 swapOutFileClose(StoreIOState::writerGone
);
1131 unlock("StoreEntry::abort"); /* unlock */
1135 * Clear Memory storage to accommodate the given object len
1138 storeGetMemSpace(int size
)
1140 PROF_start(storeGetMemSpace
);
1141 StoreEntry
*e
= NULL
;
1143 static time_t last_check
= 0;
1144 size_t pages_needed
;
1145 RemovalPurgeWalker
*walker
;
1147 if (squid_curtime
== last_check
) {
1148 PROF_stop(storeGetMemSpace
);
1152 last_check
= squid_curtime
;
1154 pages_needed
= (size
+ SM_PAGE_SIZE
-1) / SM_PAGE_SIZE
;
1156 if (mem_node::InUseCount() + pages_needed
< store_pages_max
) {
1157 PROF_stop(storeGetMemSpace
);
1161 debugs(20, 2, "storeGetMemSpace: Starting, need " << pages_needed
<<
1164 /* XXX what to set as max_scan here? */
1165 walker
= mem_policy
->PurgeInit(mem_policy
, 100000);
1167 while ((e
= walker
->Next(walker
))) {
1171 if (mem_node::InUseCount() + pages_needed
< store_pages_max
)
1175 walker
->Done(walker
);
1176 debugs(20, 3, "storeGetMemSpace stats:");
1177 debugs(20, 3, " " << std::setw(6) << hot_obj_count
<< " HOT objects");
1178 debugs(20, 3, " " << std::setw(6) << released
<< " were released");
1179 PROF_stop(storeGetMemSpace
);
1182 /* thunk through to Store::Root().maintain(). Note that this would be better still
1183 * if registered against the root store itself, but that requires more complex
1184 * update logic - bigger fish to fry first. Long term each store when
1185 * it becomes active will self register
1188 Store::Maintain(void *notused
)
1190 Store::Root().maintain();
1192 /* Reregister a maintain event .. */
1193 eventAdd("MaintainSwapSpace", Maintain
, NULL
, 1.0, 1);
1197 /* The maximum objects to scan for maintain storage space */
1198 #define MAINTAIN_MAX_SCAN 1024
1199 #define MAINTAIN_MAX_REMOVE 64
1202 * This routine is to be called by main loop in main.c.
1203 * It removes expired objects on only one bucket for each time called.
1205 * This should get called 1/s from main().
1208 StoreController::maintain()
1210 static time_t last_warn_time
= 0;
1212 PROF_start(storeMaintainSwapSpace
);
1213 swapDir
->maintain();
1215 /* this should be emitted by the oversize dir, not globally */
1217 if (Store::Root().currentSize() > Store::Root().maxSize()) {
1218 if (squid_curtime
- last_warn_time
> 10) {
1219 debugs(20, DBG_CRITICAL
, "WARNING: Disk space over limit: "
1220 << Store::Root().currentSize() / 1024.0 << " KB > "
1221 << (Store::Root().maxSize() >> 10) << " KB");
1222 last_warn_time
= squid_curtime
;
1226 PROF_stop(storeMaintainSwapSpace
);
1229 /* release an object from a cache */
1231 StoreEntry::release()
1233 PROF_start(storeRelease
);
1234 debugs(20, 3, "releasing " << *this << ' ' << getMD5Text());
1235 /* If, for any reason we can't discard this object because of an
1236 * outstanding request, mark it for pending release */
1240 debugs(20, 3, "storeRelease: Only setting RELEASE_REQUEST bit");
1242 PROF_stop(storeRelease
);
1246 Store::Root().memoryUnlink(*this);
1248 if (StoreController::store_dirs_rebuilding
&& swap_filen
> -1) {
1251 if (swap_filen
> -1) {
1252 // lock the entry until rebuilding is done
1253 lock("storeLateRelease");
1255 LateReleaseStack
.push_back(this);
1257 destroyStoreEntry(static_cast<hash_link
*>(this));
1258 // "this" is no longer valid
1261 PROF_stop(storeRelease
);
1265 storeLog(STORE_LOG_RELEASE
, this);
1267 if (swap_filen
> -1) {
1268 // log before unlink() below clears swap_filen
1269 if (!EBIT_TEST(flags
, KEY_PRIVATE
))
1270 storeDirSwapLog(this, SWAP_LOG_DEL
);
1275 destroyStoreEntry(static_cast<hash_link
*>(this));
1276 PROF_stop(storeRelease
);
1280 storeLateRelease(void *unused
)
1286 if (StoreController::store_dirs_rebuilding
) {
1287 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
1291 for (i
= 0; i
< 10; ++i
) {
1292 e
= LateReleaseStack
.count
? LateReleaseStack
.pop() : NULL
;
1296 debugs(20, DBG_IMPORTANT
, "storeLateRelease: released " << n
<< " objects");
1300 e
->unlock("storeLateRelease");
1304 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 0.0, 1);
1307 /* return 1 if a store entry is locked */
1309 StoreEntry::locked() const
1315 * SPECIAL, PUBLIC entries should be "locked";
1316 * XXX: Their owner should lock them then instead of relying on this hack.
1318 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
1319 if (!EBIT_TEST(flags
, KEY_PRIVATE
))
1326 StoreEntry::validLength() const
1329 const HttpReply
*reply
;
1330 assert(mem_obj
!= NULL
);
1332 debugs(20, 3, "storeEntryValidLength: Checking '" << getMD5Text() << "'");
1333 debugs(20, 5, "storeEntryValidLength: object_len = " <<
1335 debugs(20, 5, "storeEntryValidLength: hdr_sz = " << reply
->hdr_sz
);
1336 debugs(20, 5, "storeEntryValidLength: content_length = " << reply
->content_length
);
1338 if (reply
->content_length
< 0) {
1339 debugs(20, 5, "storeEntryValidLength: Unspecified content length: " << getMD5Text());
1343 if (reply
->hdr_sz
== 0) {
1344 debugs(20, 5, "storeEntryValidLength: Zero header size: " << getMD5Text());
1348 if (mem_obj
->method
== Http::METHOD_HEAD
) {
1349 debugs(20, 5, "storeEntryValidLength: HEAD request: " << getMD5Text());
1353 if (reply
->sline
.status() == Http::scNotModified
)
1356 if (reply
->sline
.status() == Http::scNoContent
)
1359 diff
= reply
->hdr_sz
+ reply
->content_length
- objectLen();
1364 debugs(20, 3, "storeEntryValidLength: " << (diff
< 0 ? -diff
: diff
) << " bytes too " << (diff
< 0 ? "big" : "small") <<"; '" << getMD5Text() << "'" );
1370 storeRegisterWithCacheManager(void)
1372 Mgr::RegisterAction("storedir", "Store Directory Stats", Store::Stats
, 0, 1);
1373 Mgr::RegisterAction("store_io", "Store IO Interface Stats", &Mgr::StoreIoAction::Create
, 0, 1);
1374 Mgr::RegisterAction("store_check_cachable_stats", "storeCheckCachable() Stats",
1375 storeCheckCachableStats
, 0, 1);
1382 mem_policy
= createRemovalPolicy(Config
.memPolicy
);
1385 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
1386 Store::Root().init();
1387 storeRebuildStart();
1389 storeRegisterWithCacheManager();
1393 storeConfigure(void)
1395 store_swap_high
= (long) (((float) Store::Root().maxSize() *
1396 (float) Config
.Swap
.highWaterMark
) / (float) 100);
1397 store_swap_low
= (long) (((float) Store::Root().maxSize() *
1398 (float) Config
.Swap
.lowWaterMark
) / (float) 100);
1399 store_pages_max
= Config
.memMaxSize
/ sizeof(mem_node
);
1403 StoreEntry::memoryCachable() const
1405 if (mem_obj
== NULL
)
1408 if (mem_obj
->data_hdr
.size() == 0)
1411 if (mem_obj
->inmem_lo
!= 0)
1414 if (!Config
.onoff
.memory_cache_first
&& swap_status
== SWAPOUT_DONE
&& refcount
== 1)
1421 StoreEntry::checkNegativeHit() const
1423 if (!EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1426 if (expires
<= squid_curtime
)
1429 if (store_status
!= STORE_OK
)
1436 * Set object for negative caching.
1437 * Preserves any expiry information given by the server.
1438 * In absence of proper expiry info it will set to expire immediately,
1439 * or with HTTP-violations enabled the configured negative-TTL is observed
1442 StoreEntry::negativeCache()
1444 // XXX: should make the default for expires 0 instead of -1
1445 // so we can distinguish "Expires: -1" from nothing.
1447 #if USE_HTTP_VIOLATIONS
1448 expires
= squid_curtime
+ Config
.negativeTtl
;
1450 expires
= squid_curtime
;
1452 EBIT_SET(flags
, ENTRY_NEGCACHED
);
1456 storeFreeMemory(void)
1459 #if USE_CACHE_DIGESTS
1462 cacheDigestDestroy(store_digest
);
1466 store_digest
= NULL
;
1470 expiresMoreThan(time_t expires
, time_t when
)
1472 if (expires
< 0) /* No Expires given */
1475 return (expires
> (squid_curtime
+ when
));
1479 StoreEntry::validToSend() const
1481 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
1484 if (EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1485 if (expires
<= squid_curtime
)
1488 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
1491 // now check that the entry has a cache backing or is collapsed
1492 if (swap_filen
> -1) // backed by a disk cache
1495 if (swappingOut()) // will be backed by a disk cache
1498 if (!mem_obj
) // not backed by a memory cache and not collapsed
1501 if (mem_obj
->memCache
.index
>= 0) // backed by a shared memory cache
1504 // StoreEntry::storeClientType() assumes DISK_CLIENT here, but there is no
1505 // disk cache backing so we should not rely on the store cache at all. This
1506 // is wrong for range requests that could feed off nibbled memory (XXX).
1507 if (mem_obj
->inmem_lo
) // in local memory cache, but got nibbled at
1514 StoreEntry::timestampsSet()
1516 const HttpReply
*reply
= getReply();
1517 time_t served_date
= reply
->date
;
1518 int age
= reply
->header
.getInt(HDR_AGE
);
1519 /* Compute the timestamp, mimicking RFC2616 section 13.2.3. */
1520 /* make sure that 0 <= served_date <= squid_curtime */
1522 if (served_date
< 0 || served_date
> squid_curtime
)
1523 served_date
= squid_curtime
;
1526 * If the returned Date: is more than 24 hours older than
1527 * the squid_curtime, then one of us needs to use NTP to set our
1528 * clock. We'll pretend that our clock is right.
1530 else if (served_date
< (squid_curtime
- 24 * 60 * 60) )
1531 served_date
= squid_curtime
;
1534 * Compensate with Age header if origin server clock is ahead
1535 * of us and there is a cache in between us and the origin
1536 * server. But DONT compensate if the age value is larger than
1537 * squid_curtime because it results in a negative served_date.
1539 if (age
> squid_curtime
- served_date
)
1540 if (squid_curtime
> age
)
1541 served_date
= squid_curtime
- age
;
1543 // compensate for Squid-to-server and server-to-Squid delays
1544 if (mem_obj
&& mem_obj
->request
) {
1545 const time_t request_sent
=
1546 mem_obj
->request
->hier
.peer_http_request_sent
.tv_sec
;
1547 if (0 < request_sent
&& request_sent
< squid_curtime
)
1548 served_date
-= (squid_curtime
- request_sent
);
1551 if (reply
->expires
> 0 && reply
->date
> -1)
1552 expires
= served_date
+ (reply
->expires
- reply
->date
);
1554 expires
= reply
->expires
;
1556 lastmod
= reply
->last_modified
;
1558 timestamp
= served_date
;
1562 StoreEntry::registerAbort(STABH
* cb
, void *data
)
1565 assert(mem_obj
->abort
.callback
== NULL
);
1566 mem_obj
->abort
.callback
= cb
;
1567 mem_obj
->abort
.data
= cbdataReference(data
);
1571 StoreEntry::unregisterAbort()
1574 if (mem_obj
->abort
.callback
) {
1575 mem_obj
->abort
.callback
= NULL
;
1576 cbdataReferenceDone(mem_obj
->abort
.data
);
1581 StoreEntry::dump(int l
) const
1583 debugs(20, l
, "StoreEntry->key: " << getMD5Text());
1584 debugs(20, l
, "StoreEntry->next: " << next
);
1585 debugs(20, l
, "StoreEntry->mem_obj: " << mem_obj
);
1586 debugs(20, l
, "StoreEntry->timestamp: " << timestamp
);
1587 debugs(20, l
, "StoreEntry->lastref: " << lastref
);
1588 debugs(20, l
, "StoreEntry->expires: " << expires
);
1589 debugs(20, l
, "StoreEntry->lastmod: " << lastmod
);
1590 debugs(20, l
, "StoreEntry->swap_file_sz: " << swap_file_sz
);
1591 debugs(20, l
, "StoreEntry->refcount: " << refcount
);
1592 debugs(20, l
, "StoreEntry->flags: " << storeEntryFlags(this));
1593 debugs(20, l
, "StoreEntry->swap_dirn: " << swap_dirn
);
1594 debugs(20, l
, "StoreEntry->swap_filen: " << swap_filen
);
1595 debugs(20, l
, "StoreEntry->lock_count: " << lock_count
);
1596 debugs(20, l
, "StoreEntry->mem_status: " << mem_status
);
1597 debugs(20, l
, "StoreEntry->ping_status: " << ping_status
);
1598 debugs(20, l
, "StoreEntry->store_status: " << store_status
);
1599 debugs(20, l
, "StoreEntry->swap_status: " << swap_status
);
1603 * NOTE, this function assumes only two mem states
1606 StoreEntry::setMemStatus(mem_status_t new_status
)
1608 if (new_status
== mem_status
)
1611 // are we using a shared memory cache?
1612 if (Config
.memShared
&& IamWorkerProcess()) {
1613 // This method was designed to update replacement policy, not to
1614 // actually purge something from the memory cache (TODO: rename?).
1615 // Shared memory cache does not have a policy that needs updates.
1616 mem_status
= new_status
;
1620 assert(mem_obj
!= NULL
);
1622 if (new_status
== IN_MEMORY
) {
1623 assert(mem_obj
->inmem_lo
== 0);
1625 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1626 debugs(20, 4, "not inserting special " << *this << " into policy");
1628 mem_policy
->Add(mem_policy
, this, &mem_obj
->repl
);
1629 debugs(20, 4, "inserted " << *this << " key: " << getMD5Text());
1632 ++hot_obj_count
; // TODO: maintain for the shared hot cache as well
1634 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1635 debugs(20, 4, "not removing special " << *this << " from policy");
1637 mem_policy
->Remove(mem_policy
, this, &mem_obj
->repl
);
1638 debugs(20, 4, "removed " << *this);
1644 mem_status
= new_status
;
1648 StoreEntry::url() const
1651 return "[null_entry]";
1652 else if (mem_obj
== NULL
)
1653 return "[null_mem_obj]";
1655 return mem_obj
->storeId();
1659 StoreEntry::makeMemObject()
1662 mem_obj
= new MemObject();
1667 StoreEntry::createMemObject(const char *aUrl
, const char *aLogUrl
, const HttpRequestMethod
&aMethod
)
1670 mem_obj
->setUris(aUrl
, aLogUrl
, aMethod
);
1673 /* this just sets DELAY_SENDING */
1675 StoreEntry::buffer()
1677 EBIT_SET(flags
, DELAY_SENDING
);
1680 /* this just clears DELAY_SENDING and Invokes the handlers */
1684 if (EBIT_TEST(flags
, DELAY_SENDING
)) {
1685 EBIT_CLR(flags
, DELAY_SENDING
);
1691 StoreEntry::objectLen() const
1693 assert(mem_obj
!= NULL
);
1694 return mem_obj
->object_sz
;
1698 StoreEntry::contentLen() const
1700 assert(mem_obj
!= NULL
);
1701 assert(getReply() != NULL
);
1702 return objectLen() - getReply()->hdr_sz
;
1706 StoreEntry::getReply () const
1708 if (NULL
== mem_obj
)
1711 return mem_obj
->getReply();
1718 debugs(20, 3, "StoreEntry::reset: " << url());
1720 HttpReply
*rep
= (HttpReply
*) getReply(); // bypass const
1722 expires
= lastmod
= timestamp
= -1;
1728 * This routine calls the SETUP routine for each fs type.
1729 * I don't know where the best place for this is, and I'm not going to shuffle
1730 * around large chunks of code right now (that can be done once its working.)
1739 * called to add another store removal policy module
1742 storeReplAdd(const char *type
, REMOVALPOLICYCREATE
* create
)
1746 /* find the number of currently known repl types */
1747 for (i
= 0; storerepl_list
&& storerepl_list
[i
].typestr
; ++i
) {
1748 if (strcmp(storerepl_list
[i
].typestr
, type
) == 0) {
1749 debugs(20, DBG_IMPORTANT
, "WARNING: Trying to load store replacement policy " << type
<< " twice.");
1754 /* add the new type */
1755 storerepl_list
= static_cast<storerepl_entry_t
*>(xrealloc(storerepl_list
, (i
+ 2) * sizeof(storerepl_entry_t
)));
1757 memset(&storerepl_list
[i
+ 1], 0, sizeof(storerepl_entry_t
));
1759 storerepl_list
[i
].typestr
= type
;
1761 storerepl_list
[i
].create
= create
;
1765 * Create a removal policy instance
1768 createRemovalPolicy(RemovalPolicySettings
* settings
)
1770 storerepl_entry_t
*r
;
1772 for (r
= storerepl_list
; r
&& r
->typestr
; ++r
) {
1773 if (strcmp(r
->typestr
, settings
->type
) == 0)
1774 return r
->create(settings
->args
);
1777 debugs(20, DBG_IMPORTANT
, "ERROR: Unknown policy " << settings
->type
);
1778 debugs(20, DBG_IMPORTANT
, "ERROR: Be sure to have set cache_replacement_policy");
1779 debugs(20, DBG_IMPORTANT
, "ERROR: and memory_replacement_policy in squid.conf!");
1780 fatalf("ERROR: Unknown policy %s\n", settings
->type
);
1781 return NULL
; /* NOTREACHED */
1786 storeSwapFileNumberSet(StoreEntry
* e
, sfileno filn
)
1788 if (e
->swap_file_number
== filn
)
1793 storeDirMapBitReset(e
->swap_file_number
);
1794 storeDirLRUDelete(e
);
1795 e
->swap_file_number
= -1;
1797 assert(-1 == e
->swap_file_number
);
1798 storeDirMapBitSet(e
->swap_file_number
= filn
);
1806 * Replace a store entry with
1807 * a new reply. This eats the reply.
1810 StoreEntry::replaceHttpReply(HttpReply
*rep
, bool andStartWriting
)
1812 debugs(20, 3, "StoreEntry::replaceHttpReply: " << url());
1815 debugs(20, DBG_CRITICAL
, "Attempt to replace object with no in-memory representation");
1819 mem_obj
->replaceHttpReply(rep
);
1821 if (andStartWriting
)
1826 StoreEntry::startWriting()
1830 /* TODO: when we store headers serparately remove the header portion */
1831 /* TODO: mark the length of the headers ? */
1832 /* We ONLY want the headers */
1833 packerToStoreInit(&p
, this);
1838 const HttpReply
*rep
= getReply();
1841 rep
->packHeadersInto(&p
);
1842 mem_obj
->markEndOfReplyHeaders();
1843 EBIT_CLR(flags
, ENTRY_FWD_HDR_WAIT
);
1845 rep
->body
.packInto(&p
);
1851 StoreEntry::getSerialisedMetaData()
1853 StoreMeta
*tlv_list
= storeSwapMetaBuild(this);
1855 char *result
= storeSwapMetaPack(tlv_list
, &swap_hdr_sz
);
1856 storeSwapTLVFree(tlv_list
);
1857 assert (swap_hdr_sz
>= 0);
1858 mem_obj
->swap_hdr_sz
= (size_t) swap_hdr_sz
;
1863 StoreEntry::trimMemory(const bool preserveSwappable
)
1867 * Bug #1943. We must not let go any data for IN_MEMORY
1868 * objects. We have to wait until the mem_status changes.
1870 if (mem_status
== IN_MEMORY
)
1873 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
1874 return; // cannot trim because we do not load them again
1876 if (preserveSwappable
)
1877 mem_obj
->trimSwappable();
1879 mem_obj
->trimUnSwappable();
1881 debugs(88, 7, *this << " inmem_lo=" << mem_obj
->inmem_lo
);
1885 StoreEntry::modifiedSince(HttpRequest
* request
) const
1888 time_t mod_time
= lastmod
;
1891 mod_time
= timestamp
;
1893 debugs(88, 3, "modifiedSince: '" << url() << "'");
1895 debugs(88, 3, "modifiedSince: mod_time = " << mod_time
);
1900 /* Find size of the object */
1901 object_length
= getReply()->content_length
;
1903 if (object_length
< 0)
1904 object_length
= contentLen();
1906 if (mod_time
> request
->ims
) {
1907 debugs(88, 3, "--> YES: entry newer than client");
1909 } else if (mod_time
< request
->ims
) {
1910 debugs(88, 3, "--> NO: entry older than client");
1912 } else if (request
->imslen
< 0) {
1913 debugs(88, 3, "--> NO: same LMT, no client length");
1915 } else if (request
->imslen
== object_length
) {
1916 debugs(88, 3, "--> NO: same LMT, same length");
1919 debugs(88, 3, "--> YES: same LMT, different length");
1925 StoreEntry::hasEtag(ETag
&etag
) const
1927 if (const HttpReply
*reply
= getReply()) {
1928 etag
= reply
->header
.getETag(HDR_ETAG
);
1936 StoreEntry::hasIfMatchEtag(const HttpRequest
&request
) const
1938 const String reqETags
= request
.header
.getList(HDR_IF_MATCH
);
1939 return hasOneOfEtags(reqETags
, false);
1943 StoreEntry::hasIfNoneMatchEtag(const HttpRequest
&request
) const
1945 const String reqETags
= request
.header
.getList(HDR_IF_NONE_MATCH
);
1946 // weak comparison is allowed only for HEAD or full-body GET requests
1947 const bool allowWeakMatch
= !request
.flags
.isRanged
&&
1948 (request
.method
== Http::METHOD_GET
|| request
.method
== Http::METHOD_HEAD
);
1949 return hasOneOfEtags(reqETags
, allowWeakMatch
);
1952 /// whether at least one of the request ETags matches entity ETag
1954 StoreEntry::hasOneOfEtags(const String
&reqETags
, const bool allowWeakMatch
) const
1956 const ETag repETag
= getReply()->header
.getETag(HDR_ETAG
);
1958 return strListIsMember(&reqETags
, "*", ',');
1960 bool matched
= false;
1961 const char *pos
= NULL
;
1964 while (!matched
&& strListGetItem(&reqETags
, ',', &item
, &ilen
, &pos
)) {
1965 if (!strncmp(item
, "*", ilen
))
1969 str
.append(item
, ilen
);
1971 if (etagParseInit(&reqETag
, str
.termedBuf())) {
1972 matched
= allowWeakMatch
? etagIsWeakEqual(repETag
, reqETag
) :
1973 etagIsStrongEqual(repETag
, reqETag
);
1981 StoreEntry::store() const
1983 assert(0 <= swap_dirn
&& swap_dirn
< Config
.cacheSwap
.n_configured
);
1984 return INDEXSD(swap_dirn
);
1988 StoreEntry::unlink()
1990 store()->unlink(*this); // implies disconnect()
1993 swap_status
= SWAPOUT_NONE
;
1997 * return true if the entry is in a state where
1998 * it can accept more data (ie with write() method)
2001 StoreEntry::isAccepting() const
2003 if (STORE_PENDING
!= store_status
)
2006 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
2012 std::ostream
&operator <<(std::ostream
&os
, const StoreEntry
&e
)
2017 if (e
.mem_obj
->xitTable
.index
> -1)
2018 os
<< 't' << e
.mem_obj
->xitTable
.index
;
2019 if (e
.mem_obj
->memCache
.index
> -1)
2020 os
<< 'm' << e
.mem_obj
->memCache
.index
;
2022 if (e
.swap_filen
> -1 || e
.swap_dirn
> -1)
2023 os
<< 'd' << e
.swap_filen
<< '@' << e
.swap_dirn
;
2027 // print only non-default status values, using unique letters
2028 if (e
.mem_status
!= NOT_IN_MEMORY
||
2029 e
.store_status
!= STORE_PENDING
||
2030 e
.swap_status
!= SWAPOUT_NONE
||
2031 e
.ping_status
!= PING_NONE
) {
2032 if (e
.mem_status
!= NOT_IN_MEMORY
) os
<< 'm';
2033 if (e
.store_status
!= STORE_PENDING
) os
<< 's';
2034 if (e
.swap_status
!= SWAPOUT_NONE
) os
<< 'w' << e
.swap_status
;
2035 if (e
.ping_status
!= PING_NONE
) os
<< 'p' << e
.ping_status
;
2038 // print only set flags, using unique letters
2040 if (EBIT_TEST(e
.flags
, ENTRY_SPECIAL
)) os
<< 'S';
2041 if (EBIT_TEST(e
.flags
, ENTRY_REVALIDATE
)) os
<< 'R';
2042 if (EBIT_TEST(e
.flags
, DELAY_SENDING
)) os
<< 'P';
2043 if (EBIT_TEST(e
.flags
, RELEASE_REQUEST
)) os
<< 'X';
2044 if (EBIT_TEST(e
.flags
, REFRESH_REQUEST
)) os
<< 'F';
2045 if (EBIT_TEST(e
.flags
, ENTRY_DISPATCHED
)) os
<< 'D';
2046 if (EBIT_TEST(e
.flags
, KEY_PRIVATE
)) os
<< 'I';
2047 if (EBIT_TEST(e
.flags
, ENTRY_FWD_HDR_WAIT
)) os
<< 'W';
2048 if (EBIT_TEST(e
.flags
, ENTRY_NEGCACHED
)) os
<< 'N';
2049 if (EBIT_TEST(e
.flags
, ENTRY_VALIDATED
)) os
<< 'V';
2050 if (EBIT_TEST(e
.flags
, ENTRY_BAD_LENGTH
)) os
<< 'L';
2051 if (EBIT_TEST(e
.flags
, ENTRY_ABORTED
)) os
<< 'A';
2054 if (e
.mem_obj
&& e
.mem_obj
->smpCollapsed
)
2057 return os
<< '/' << &e
<< '*' << e
.locks();
2060 /* NullStoreEntry */
2062 NullStoreEntry
NullStoreEntry::_instance
;
2065 NullStoreEntry::getInstance()
2071 NullStoreEntry::getMD5Text() const
2077 NullStoreEntry::operator delete(void*)
2079 fatal ("Attempt to delete NullStoreEntry\n");
2083 NullStoreEntry::getSerialisedMetaData()
2089 #include "Store.cci"