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"
57 #include "store_digest.h"
58 #include "store_key_md5.h"
59 #include "store_key_md5.h"
60 #include "store_log.h"
61 #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 hash_remove_link(store_table
, this);
480 storeKeyFree((const cache_key
*)key
);
484 /* -------------------------------------------------------------------------- */
486 /* get rid of memory copy of the object */
488 StoreEntry::purgeMem()
493 debugs(20, 3, "StoreEntry::purgeMem: Freeing memory-copy of " << getMD5Text());
495 Store::Root().memoryUnlink(*this);
497 if (swap_status
!= SWAPOUT_DONE
)
502 StoreEntry::lock(const char *context
)
505 debugs(20, 3, context
<< " locked key " << getMD5Text() << ' ' << *this);
509 StoreEntry::touch() {
510 lastref
= squid_curtime
;
511 Store::Root().reference(*this);
515 StoreEntry::setReleaseFlag()
517 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
520 debugs(20, 3, "StoreEntry::setReleaseFlag: '" << getMD5Text() << "'");
522 EBIT_SET(flags
, RELEASE_REQUEST
);
524 Store::Root().markForUnlink(*this);
528 StoreEntry::releaseRequest()
530 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
533 setReleaseFlag(); // makes validToSend() false, preventing future hits
539 StoreEntry::unlock(const char *context
)
541 debugs(20, 3, (context
? context
: "somebody") <<
542 " unlocking key " << getMD5Text() << ' ' << *this);
546 return (int) lock_count
;
548 if (store_status
== STORE_PENDING
)
551 assert(storePendingNClients(this) == 0);
553 if (EBIT_TEST(flags
, RELEASE_REQUEST
)) {
558 if (EBIT_TEST(flags
, KEY_PRIVATE
))
559 debugs(20, DBG_IMPORTANT
, "WARNING: " << __FILE__
<< ":" << __LINE__
<< ": found KEY_PRIVATE");
561 Store::Root().handleIdleEntry(*this); // may delete us
566 StoreEntry::getPublicByRequestMethod (StoreClient
*aClient
, HttpRequest
* request
, const HttpRequestMethod
& method
)
569 StoreEntry
*result
= storeGetPublicByRequestMethod( request
, method
);
572 aClient
->created (NullStoreEntry::getInstance());
574 aClient
->created (result
);
578 StoreEntry::getPublicByRequest (StoreClient
*aClient
, HttpRequest
* request
)
581 StoreEntry
*result
= storeGetPublicByRequest (request
);
584 result
= NullStoreEntry::getInstance();
586 aClient
->created (result
);
590 StoreEntry::getPublic (StoreClient
*aClient
, const char *uri
, const HttpRequestMethod
& method
)
593 StoreEntry
*result
= storeGetPublic (uri
, method
);
596 result
= NullStoreEntry::getInstance();
598 aClient
->created (result
);
602 storeGetPublic(const char *uri
, const HttpRequestMethod
& method
)
604 return Store::Root().get(storeKeyPublic(uri
, method
));
608 storeGetPublicByRequestMethod(HttpRequest
* req
, const HttpRequestMethod
& method
)
610 return Store::Root().get(storeKeyPublicByRequestMethod(req
, method
));
614 storeGetPublicByRequest(HttpRequest
* req
)
616 StoreEntry
*e
= storeGetPublicByRequestMethod(req
, req
->method
);
618 if (e
== NULL
&& req
->method
== Http::METHOD_HEAD
)
619 /* We can generate a HEAD reply from a cached GET object */
620 e
= storeGetPublicByRequestMethod(req
, Http::METHOD_GET
);
628 static int key_counter
= 0;
630 if (++key_counter
< 0)
636 /* RBC 20050104 AFAICT this should become simpler:
637 * rather than reinserting with a special key it should be marked
638 * as 'released' and then cleaned up when refcounting indicates.
639 * the StoreHashIndex could well implement its 'released' in the
641 * Also, clean log writing should skip over ia,t
642 * Otherwise, we need a 'remove from the index but not the store
646 StoreEntry::setPrivateKey()
648 const cache_key
*newkey
;
650 if (key
&& EBIT_TEST(flags
, KEY_PRIVATE
))
651 return; /* is already private */
654 setReleaseFlag(); // will markForUnlink(); all caches/workers will know
656 // TODO: move into SwapDir::markForUnlink() already called by Root()
658 storeDirSwapLog(this, SWAP_LOG_DEL
);
663 if (mem_obj
&& mem_obj
->hasUris()) {
664 mem_obj
->id
= getKeyCounter();
665 newkey
= storeKeyPrivate(mem_obj
->storeId(), mem_obj
->method
, mem_obj
->id
);
667 newkey
= storeKeyPrivate("JUNK", Http::METHOD_NONE
, getKeyCounter());
670 assert(hash_lookup(store_table
, newkey
) == NULL
);
671 EBIT_SET(flags
, KEY_PRIVATE
);
676 StoreEntry::setPublicKey()
678 const cache_key
*newkey
;
680 if (key
&& !EBIT_TEST(flags
, KEY_PRIVATE
))
681 return; /* is already public */
686 * We can't make RELEASE_REQUEST objects public. Depending on
687 * when RELEASE_REQUEST gets set, we might not be swapping out
688 * the object. If we're not swapping out, then subsequent
689 * store clients won't be able to access object data which has
690 * been freed from memory.
692 * If RELEASE_REQUEST is set, setPublicKey() should not be called.
694 #if MORE_DEBUG_OUTPUT
696 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
697 debugs(20, DBG_IMPORTANT
, "assertion failed: RELEASE key " << key
<< ", url " << mem_obj
->url
);
701 assert(!EBIT_TEST(flags
, RELEASE_REQUEST
));
703 if (mem_obj
->request
) {
704 HttpRequest
*request
= mem_obj
->request
;
706 if (!mem_obj
->vary_headers
) {
707 /* First handle the case where the object no longer varies */
708 safe_free(request
->vary_headers
);
710 if (request
->vary_headers
&& strcmp(request
->vary_headers
, mem_obj
->vary_headers
) != 0) {
711 /* Oops.. the variance has changed. Kill the base object
712 * to record the new variance key
714 safe_free(request
->vary_headers
); /* free old "bad" variance key */
715 if (StoreEntry
*pe
= storeGetPublic(mem_obj
->storeId(), mem_obj
->method
))
719 /* Make sure the request knows the variance status */
720 if (!request
->vary_headers
) {
721 const char *vary
= httpMakeVaryMark(request
, mem_obj
->getReply());
724 request
->vary_headers
= xstrdup(vary
);
728 // TODO: storeGetPublic() calls below may create unlocked entries.
729 // We should add/use storeHas() API or lock/unlock those entries.
730 if (mem_obj
->vary_headers
&& !storeGetPublic(mem_obj
->storeId(), mem_obj
->method
)) {
731 /* Create "vary" base object */
733 StoreEntry
*pe
= storeCreateEntry(mem_obj
->storeId(), mem_obj
->logUri(), request
->flags
, request
->method
);
734 /* We are allowed to do this typecast */
735 HttpReply
*rep
= new HttpReply
;
736 rep
->setHeaders(Http::scOkay
, "Internal marker object", "x-squid-internal/vary", -1, -1, squid_curtime
+ 100000);
737 vary
= mem_obj
->getReply()->header
.getList(HDR_VARY
);
740 /* Again, we own this structure layout */
741 rep
->header
.putStr(HDR_VARY
, vary
.termedBuf());
745 #if X_ACCELERATOR_VARY
746 vary
= mem_obj
->getReply()->header
.getList(HDR_X_ACCELERATOR_VARY
);
748 if (vary
.defined()) {
749 /* Again, we own this structure layout */
750 rep
->header
.putStr(HDR_X_ACCELERATOR_VARY
, vary
.termedBuf());
755 pe
->replaceHttpReply(rep
);
763 pe
->unlock("StoreEntry::setPublicKey+Vary");
766 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
768 newkey
= storeKeyPublic(mem_obj
->storeId(), mem_obj
->method
);
770 if (StoreEntry
*e2
= (StoreEntry
*)hash_lookup(store_table
, newkey
)) {
771 debugs(20, 3, "Making old " << *e2
<< " private.");
775 if (mem_obj
->request
)
776 newkey
= storeKeyPublicByRequest(mem_obj
->request
);
778 newkey
= storeKeyPublic(mem_obj
->storeId(), mem_obj
->method
);
784 EBIT_CLR(flags
, KEY_PRIVATE
);
789 storeDirSwapLog(this, SWAP_LOG_ADD
);
793 storeCreatePureEntry(const char *url
, const char *log_url
, const RequestFlags
&flags
, const HttpRequestMethod
& method
)
795 StoreEntry
*e
= NULL
;
796 debugs(20, 3, "storeCreateEntry: '" << url
<< "'");
798 e
= new StoreEntry();
800 e
->mem_obj
->setUris(url
, log_url
, method
);
802 if (flags
.cachable
) {
803 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
808 e
->store_status
= STORE_PENDING
;
810 e
->lastref
= squid_curtime
;
811 e
->timestamp
= -1; /* set in StoreEntry::timestampsSet() */
812 e
->ping_status
= PING_NONE
;
813 EBIT_SET(e
->flags
, ENTRY_VALIDATED
);
818 storeCreateEntry(const char *url
, const char *logUrl
, const RequestFlags
&flags
, const HttpRequestMethod
& method
)
820 StoreEntry
*e
= storeCreatePureEntry(url
, logUrl
, flags
, method
);
821 e
->lock("storeCreateEntry");
823 if (neighbors_do_private_keys
|| !flags
.hierarchical
)
831 /* Mark object as expired */
833 StoreEntry::expireNow()
835 debugs(20, 3, "StoreEntry::expireNow: '" << getMD5Text() << "'");
836 expires
= squid_curtime
;
840 StoreEntry::write (StoreIOBuffer writeBuffer
)
842 assert(mem_obj
!= NULL
);
843 /* This assert will change when we teach the store to update */
844 PROF_start(StoreEntry_write
);
845 assert(store_status
== STORE_PENDING
);
847 // XXX: caller uses content offset, but we also store headers
848 if (const HttpReply
*reply
= mem_obj
->getReply())
849 writeBuffer
.offset
+= reply
->hdr_sz
;
851 debugs(20, 5, "storeWrite: writing " << writeBuffer
.length
<< " bytes for '" << getMD5Text() << "'");
852 PROF_stop(StoreEntry_write
);
853 storeGetMemSpace(writeBuffer
.length
);
854 mem_obj
->write(writeBuffer
);
856 if (!EBIT_TEST(flags
, DELAY_SENDING
))
860 /* Append incoming data from a primary server to an entry. */
862 StoreEntry::append(char const *buf
, int len
)
864 assert(mem_obj
!= NULL
);
866 assert(store_status
== STORE_PENDING
);
868 StoreIOBuffer tempBuffer
;
869 tempBuffer
.data
= (char *)buf
;
870 tempBuffer
.length
= len
;
872 * XXX sigh, offset might be < 0 here, but it gets "corrected"
873 * later. This offset crap is such a mess.
875 tempBuffer
.offset
= mem_obj
->endOffset() - (getReply() ? getReply()->hdr_sz
: 0);
880 storeAppendPrintf(StoreEntry
* e
, const char *fmt
,...)
885 storeAppendVPrintf(e
, fmt
, args
);
889 /* used be storeAppendPrintf and Packer */
891 storeAppendVPrintf(StoreEntry
* e
, const char *fmt
, va_list vargs
)
893 LOCAL_ARRAY(char, buf
, 4096);
895 vsnprintf(buf
, 4096, fmt
, vargs
);
896 e
->append(buf
, strlen(buf
));
899 struct _store_check_cachable_hist
{
903 int not_entry_cachable
;
904 int wrong_content_length
;
909 int too_many_open_files
;
910 int too_many_open_fds
;
916 } store_check_cachable_hist
;
919 storeTooManyDiskFilesOpen(void)
921 if (Config
.max_open_disk_fds
== 0)
924 if (store_open_disk_fd
> Config
.max_open_disk_fds
)
931 StoreEntry::checkTooSmall()
933 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
936 if (STORE_OK
== store_status
)
937 if (mem_obj
->object_sz
< 0 ||
938 mem_obj
->object_sz
< Config
.Store
.minObjectSize
)
940 if (getReply()->content_length
> -1)
941 if (getReply()->content_length
< Config
.Store
.minObjectSize
)
946 // TODO: remove checks already performed by swapoutPossible()
947 // TODO: move "too many open..." checks outside -- we are called too early/late
949 StoreEntry::checkCachable()
951 #if CACHE_ALL_METHODS
953 if (mem_obj
->method
!= Http::METHOD_GET
) {
954 debugs(20, 2, "StoreEntry::checkCachable: NO: non-GET method");
955 ++store_check_cachable_hist
.no
.non_get
;
958 if (store_status
== STORE_OK
&& EBIT_TEST(flags
, ENTRY_BAD_LENGTH
)) {
959 debugs(20, 2, "StoreEntry::checkCachable: NO: wrong content-length");
960 ++store_check_cachable_hist
.no
.wrong_content_length
;
961 } else if (EBIT_TEST(flags
, RELEASE_REQUEST
)) {
962 debugs(20, 2, "StoreEntry::checkCachable: NO: not cachable");
963 ++store_check_cachable_hist
.no
.not_entry_cachable
; // TODO: rename?
964 } else if (EBIT_TEST(flags
, ENTRY_NEGCACHED
)) {
965 debugs(20, 3, "StoreEntry::checkCachable: NO: negative cached");
966 ++store_check_cachable_hist
.no
.negative_cached
;
967 return 0; /* avoid release call below */
968 } else if ((getReply()->content_length
> 0 &&
969 getReply()->content_length
> store_maxobjsize
) ||
970 mem_obj
->endOffset() > store_maxobjsize
) {
971 debugs(20, 2, "StoreEntry::checkCachable: NO: too big");
972 ++store_check_cachable_hist
.no
.too_big
;
973 } else if (checkTooSmall()) {
974 debugs(20, 2, "StoreEntry::checkCachable: NO: too small");
975 ++store_check_cachable_hist
.no
.too_small
;
976 } else if (EBIT_TEST(flags
, KEY_PRIVATE
)) {
977 debugs(20, 3, "StoreEntry::checkCachable: NO: private key");
978 ++store_check_cachable_hist
.no
.private_key
;
979 } else if (swap_status
!= SWAPOUT_NONE
) {
981 * here we checked the swap_status because the remaining
982 * cases are only relevant only if we haven't started swapping
983 * out the object yet.
986 } else if (storeTooManyDiskFilesOpen()) {
987 debugs(20, 2, "StoreEntry::checkCachable: NO: too many disk files open");
988 ++store_check_cachable_hist
.no
.too_many_open_files
;
989 } else if (fdNFree() < RESERVED_FD
) {
990 debugs(20, 2, "StoreEntry::checkCachable: NO: too many FD's open");
991 ++store_check_cachable_hist
.no
.too_many_open_fds
;
993 ++store_check_cachable_hist
.yes
.Default
;
1002 storeCheckCachableStats(StoreEntry
*sentry
)
1004 storeAppendPrintf(sentry
, "Category\t Count\n");
1006 #if CACHE_ALL_METHODS
1008 storeAppendPrintf(sentry
, "no.non_get\t%d\n",
1009 store_check_cachable_hist
.no
.non_get
);
1012 storeAppendPrintf(sentry
, "no.not_entry_cachable\t%d\n",
1013 store_check_cachable_hist
.no
.not_entry_cachable
);
1014 storeAppendPrintf(sentry
, "no.wrong_content_length\t%d\n",
1015 store_check_cachable_hist
.no
.wrong_content_length
);
1016 storeAppendPrintf(sentry
, "no.negative_cached\t%d\n",
1017 store_check_cachable_hist
.no
.negative_cached
);
1018 storeAppendPrintf(sentry
, "no.too_big\t%d\n",
1019 store_check_cachable_hist
.no
.too_big
);
1020 storeAppendPrintf(sentry
, "no.too_small\t%d\n",
1021 store_check_cachable_hist
.no
.too_small
);
1022 storeAppendPrintf(sentry
, "no.private_key\t%d\n",
1023 store_check_cachable_hist
.no
.private_key
);
1024 storeAppendPrintf(sentry
, "no.too_many_open_files\t%d\n",
1025 store_check_cachable_hist
.no
.too_many_open_files
);
1026 storeAppendPrintf(sentry
, "no.too_many_open_fds\t%d\n",
1027 store_check_cachable_hist
.no
.too_many_open_fds
);
1028 storeAppendPrintf(sentry
, "yes.default\t%d\n",
1029 store_check_cachable_hist
.yes
.Default
);
1033 StoreEntry::complete()
1035 debugs(20, 3, "storeComplete: '" << getMD5Text() << "'");
1037 if (store_status
!= STORE_PENDING
) {
1039 * if we're not STORE_PENDING, then probably we got aborted
1040 * and there should be NO clients on this entry
1042 assert(EBIT_TEST(flags
, ENTRY_ABORTED
));
1043 assert(mem_obj
->nclients
== 0);
1047 /* This is suspect: mem obj offsets include the headers. do we adjust for that
1048 * in use of object_sz?
1050 mem_obj
->object_sz
= mem_obj
->endOffset();
1052 store_status
= STORE_OK
;
1054 assert(mem_status
== NOT_IN_MEMORY
);
1056 if (!validLength()) {
1057 EBIT_SET(flags
, ENTRY_BAD_LENGTH
);
1061 #if USE_CACHE_DIGESTS
1062 if (mem_obj
->request
)
1063 mem_obj
->request
->hier
.store_complete_stop
= current_time
;
1067 * We used to call invokeHandlers, then storeSwapOut. However,
1068 * Madhukar Reddy <myreddy@persistence.com> reported that
1069 * responses without content length would sometimes get released
1070 * in client_side, thinking that the response is incomplete.
1076 * Someone wants to abort this transfer. Set the reason in the
1077 * request structure, call the server-side callback and mark the
1078 * entry for releasing
1083 ++statCounter
.aborted_requests
;
1084 assert(store_status
== STORE_PENDING
);
1085 assert(mem_obj
!= NULL
);
1086 debugs(20, 6, "storeAbort: " << getMD5Text());
1088 lock("StoreEntry::abort"); /* lock while aborting */
1093 EBIT_SET(flags
, ENTRY_ABORTED
);
1095 setMemStatus(NOT_IN_MEMORY
);
1097 store_status
= STORE_OK
;
1099 /* Notify the server side */
1103 * Should we check abort.data for validity?
1105 if (mem_obj
->abort
.callback
) {
1106 if (!cbdataReferenceValid(mem_obj
->abort
.data
))
1107 debugs(20, DBG_IMPORTANT
,HERE
<< "queueing event when abort.data is not valid");
1108 eventAdd("mem_obj->abort.callback",
1109 mem_obj
->abort
.callback
,
1110 mem_obj
->abort
.data
,
1116 /* XXX Should we reverse these two, so that there is no
1117 * unneeded disk swapping triggered?
1119 /* Notify the client side */
1122 // abort swap out, invalidating what was created so far (release follows)
1123 swapOutFileClose(StoreIOState::writerGone
);
1125 unlock("StoreEntry::abort"); /* unlock */
1129 * Clear Memory storage to accommodate the given object len
1132 storeGetMemSpace(int size
)
1134 PROF_start(storeGetMemSpace
);
1135 StoreEntry
*e
= NULL
;
1137 static time_t last_check
= 0;
1138 size_t pages_needed
;
1139 RemovalPurgeWalker
*walker
;
1141 if (squid_curtime
== last_check
) {
1142 PROF_stop(storeGetMemSpace
);
1146 last_check
= squid_curtime
;
1148 pages_needed
= (size
+ SM_PAGE_SIZE
-1) / SM_PAGE_SIZE
;
1150 if (mem_node::InUseCount() + pages_needed
< store_pages_max
) {
1151 PROF_stop(storeGetMemSpace
);
1155 debugs(20, 2, "storeGetMemSpace: Starting, need " << pages_needed
<<
1158 /* XXX what to set as max_scan here? */
1159 walker
= mem_policy
->PurgeInit(mem_policy
, 100000);
1161 while ((e
= walker
->Next(walker
))) {
1165 if (mem_node::InUseCount() + pages_needed
< store_pages_max
)
1169 walker
->Done(walker
);
1170 debugs(20, 3, "storeGetMemSpace stats:");
1171 debugs(20, 3, " " << std::setw(6) << hot_obj_count
<< " HOT objects");
1172 debugs(20, 3, " " << std::setw(6) << released
<< " were released");
1173 PROF_stop(storeGetMemSpace
);
1176 /* thunk through to Store::Root().maintain(). Note that this would be better still
1177 * if registered against the root store itself, but that requires more complex
1178 * update logic - bigger fish to fry first. Long term each store when
1179 * it becomes active will self register
1182 Store::Maintain(void *notused
)
1184 Store::Root().maintain();
1186 /* Reregister a maintain event .. */
1187 eventAdd("MaintainSwapSpace", Maintain
, NULL
, 1.0, 1);
1191 /* The maximum objects to scan for maintain storage space */
1192 #define MAINTAIN_MAX_SCAN 1024
1193 #define MAINTAIN_MAX_REMOVE 64
1196 * This routine is to be called by main loop in main.c.
1197 * It removes expired objects on only one bucket for each time called.
1199 * This should get called 1/s from main().
1202 StoreController::maintain()
1204 static time_t last_warn_time
= 0;
1206 PROF_start(storeMaintainSwapSpace
);
1207 swapDir
->maintain();
1209 /* this should be emitted by the oversize dir, not globally */
1211 if (Store::Root().currentSize() > Store::Root().maxSize()) {
1212 if (squid_curtime
- last_warn_time
> 10) {
1213 debugs(20, DBG_CRITICAL
, "WARNING: Disk space over limit: "
1214 << Store::Root().currentSize() / 1024.0 << " KB > "
1215 << (Store::Root().maxSize() >> 10) << " KB");
1216 last_warn_time
= squid_curtime
;
1220 PROF_stop(storeMaintainSwapSpace
);
1223 /* release an object from a cache */
1225 StoreEntry::release()
1227 PROF_start(storeRelease
);
1228 debugs(20, 3, "releasing " << *this << ' ' << getMD5Text());
1229 /* If, for any reason we can't discard this object because of an
1230 * outstanding request, mark it for pending release */
1234 debugs(20, 3, "storeRelease: Only setting RELEASE_REQUEST bit");
1236 PROF_stop(storeRelease
);
1240 Store::Root().memoryUnlink(*this);
1242 if (StoreController::store_dirs_rebuilding
&& swap_filen
> -1) {
1245 if (swap_filen
> -1) {
1247 * Fake a call to StoreEntry->lock() When rebuilding is done,
1248 * we'll just call StoreEntry->unlock() on these.
1250 lock("StoreEntry::release+rebuilding");
1252 LateReleaseStack
.push_back(this);
1254 destroyStoreEntry(static_cast<hash_link
*>(this));
1255 // "this" is no longer valid
1258 PROF_stop(storeRelease
);
1262 storeLog(STORE_LOG_RELEASE
, this);
1264 if (swap_filen
> -1) {
1265 // log before unlink() below clears swap_filen
1266 if (!EBIT_TEST(flags
, KEY_PRIVATE
))
1267 storeDirSwapLog(this, SWAP_LOG_DEL
);
1272 destroyStoreEntry(static_cast<hash_link
*>(this));
1273 PROF_stop(storeRelease
);
1277 storeLateRelease(void *unused
)
1283 if (StoreController::store_dirs_rebuilding
) {
1284 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
1288 for (i
= 0; i
< 10; ++i
) {
1289 e
= LateReleaseStack
.count
? LateReleaseStack
.pop() : NULL
;
1293 debugs(20, DBG_IMPORTANT
, "storeLateRelease: released " << n
<< " objects");
1297 e
->unlock("storeLateRelease");
1301 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 0.0, 1);
1304 /* return 1 if a store entry is locked */
1306 StoreEntry::locked() const
1312 * SPECIAL, PUBLIC entries should be "locked";
1313 * XXX: Their owner should lock them then instead of relying on this hack.
1315 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
1316 if (!EBIT_TEST(flags
, KEY_PRIVATE
))
1323 StoreEntry::validLength() const
1326 const HttpReply
*reply
;
1327 assert(mem_obj
!= NULL
);
1329 debugs(20, 3, "storeEntryValidLength: Checking '" << getMD5Text() << "'");
1330 debugs(20, 5, "storeEntryValidLength: object_len = " <<
1332 debugs(20, 5, "storeEntryValidLength: hdr_sz = " << reply
->hdr_sz
);
1333 debugs(20, 5, "storeEntryValidLength: content_length = " << reply
->content_length
);
1335 if (reply
->content_length
< 0) {
1336 debugs(20, 5, "storeEntryValidLength: Unspecified content length: " << getMD5Text());
1340 if (reply
->hdr_sz
== 0) {
1341 debugs(20, 5, "storeEntryValidLength: Zero header size: " << getMD5Text());
1345 if (mem_obj
->method
== Http::METHOD_HEAD
) {
1346 debugs(20, 5, "storeEntryValidLength: HEAD request: " << getMD5Text());
1350 if (reply
->sline
.status() == Http::scNotModified
)
1353 if (reply
->sline
.status() == Http::scNoContent
)
1356 diff
= reply
->hdr_sz
+ reply
->content_length
- objectLen();
1361 debugs(20, 3, "storeEntryValidLength: " << (diff
< 0 ? -diff
: diff
) << " bytes too " << (diff
< 0 ? "big" : "small") <<"; '" << getMD5Text() << "'" );
1367 storeRegisterWithCacheManager(void)
1369 Mgr::RegisterAction("storedir", "Store Directory Stats", Store::Stats
, 0, 1);
1370 Mgr::RegisterAction("store_io", "Store IO Interface Stats", &Mgr::StoreIoAction::Create
, 0, 1);
1371 Mgr::RegisterAction("store_check_cachable_stats", "storeCheckCachable() Stats",
1372 storeCheckCachableStats
, 0, 1);
1379 mem_policy
= createRemovalPolicy(Config
.memPolicy
);
1382 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
1383 Store::Root().init();
1384 storeRebuildStart();
1386 storeRegisterWithCacheManager();
1390 storeConfigure(void)
1392 store_swap_high
= (long) (((float) Store::Root().maxSize() *
1393 (float) Config
.Swap
.highWaterMark
) / (float) 100);
1394 store_swap_low
= (long) (((float) Store::Root().maxSize() *
1395 (float) Config
.Swap
.lowWaterMark
) / (float) 100);
1396 store_pages_max
= Config
.memMaxSize
/ sizeof(mem_node
);
1400 StoreEntry::memoryCachable() const
1402 if (mem_obj
== NULL
)
1405 if (mem_obj
->data_hdr
.size() == 0)
1408 if (mem_obj
->inmem_lo
!= 0)
1411 if (!Config
.onoff
.memory_cache_first
&& swap_status
== SWAPOUT_DONE
&& refcount
== 1)
1418 StoreEntry::checkNegativeHit() const
1420 if (!EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1423 if (expires
<= squid_curtime
)
1426 if (store_status
!= STORE_OK
)
1433 * Set object for negative caching.
1434 * Preserves any expiry information given by the server.
1435 * In absence of proper expiry info it will set to expire immediately,
1436 * or with HTTP-violations enabled the configured negative-TTL is observed
1439 StoreEntry::negativeCache()
1441 // XXX: should make the default for expires 0 instead of -1
1442 // so we can distinguish "Expires: -1" from nothing.
1444 #if USE_HTTP_VIOLATIONS
1445 expires
= squid_curtime
+ Config
.negativeTtl
;
1447 expires
= squid_curtime
;
1449 EBIT_SET(flags
, ENTRY_NEGCACHED
);
1453 storeFreeMemory(void)
1456 #if USE_CACHE_DIGESTS
1459 cacheDigestDestroy(store_digest
);
1463 store_digest
= NULL
;
1467 expiresMoreThan(time_t expires
, time_t when
)
1469 if (expires
< 0) /* No Expires given */
1472 return (expires
> (squid_curtime
+ when
));
1476 StoreEntry::validToSend() const
1478 if (EBIT_TEST(flags
, RELEASE_REQUEST
))
1481 if (EBIT_TEST(flags
, ENTRY_NEGCACHED
))
1482 if (expires
<= squid_curtime
)
1485 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
1488 // now check that the entry has a cache backing or is collapsed
1489 if (swap_filen
> -1) // backed by a disk cache
1492 if (swappingOut()) // will be backed by a disk cache
1495 if (!mem_obj
) // not backed by a memory cache and not collapsed
1498 if (mem_obj
->memCache
.index
>= 0) // backed by a shared memory cache
1501 // StoreEntry::storeClientType() assumes DISK_CLIENT here, but there is no
1502 // disk cache backing so we should not rely on the store cache at all. This
1503 // is wrong for range requests that could feed off nibbled memory (XXX).
1504 if (mem_obj
->inmem_lo
) // in local memory cache, but got nibbled at
1511 StoreEntry::timestampsSet()
1513 const HttpReply
*reply
= getReply();
1514 time_t served_date
= reply
->date
;
1515 int age
= reply
->header
.getInt(HDR_AGE
);
1516 /* Compute the timestamp, mimicking RFC2616 section 13.2.3. */
1517 /* make sure that 0 <= served_date <= squid_curtime */
1519 if (served_date
< 0 || served_date
> squid_curtime
)
1520 served_date
= squid_curtime
;
1523 * If the returned Date: is more than 24 hours older than
1524 * the squid_curtime, then one of us needs to use NTP to set our
1525 * clock. We'll pretend that our clock is right.
1527 else if (served_date
< (squid_curtime
- 24 * 60 * 60) )
1528 served_date
= squid_curtime
;
1531 * Compensate with Age header if origin server clock is ahead
1532 * of us and there is a cache in between us and the origin
1533 * server. But DONT compensate if the age value is larger than
1534 * squid_curtime because it results in a negative served_date.
1536 if (age
> squid_curtime
- served_date
)
1537 if (squid_curtime
> age
)
1538 served_date
= squid_curtime
- age
;
1540 // compensate for Squid-to-server and server-to-Squid delays
1541 if (mem_obj
&& mem_obj
->request
) {
1542 const time_t request_sent
=
1543 mem_obj
->request
->hier
.peer_http_request_sent
.tv_sec
;
1544 if (0 < request_sent
&& request_sent
< squid_curtime
)
1545 served_date
-= (squid_curtime
- request_sent
);
1548 if (reply
->expires
> 0 && reply
->date
> -1)
1549 expires
= served_date
+ (reply
->expires
- reply
->date
);
1551 expires
= reply
->expires
;
1553 lastmod
= reply
->last_modified
;
1555 timestamp
= served_date
;
1559 StoreEntry::registerAbort(STABH
* cb
, void *data
)
1562 assert(mem_obj
->abort
.callback
== NULL
);
1563 mem_obj
->abort
.callback
= cb
;
1564 mem_obj
->abort
.data
= cbdataReference(data
);
1568 StoreEntry::unregisterAbort()
1571 if (mem_obj
->abort
.callback
) {
1572 mem_obj
->abort
.callback
= NULL
;
1573 cbdataReferenceDone(mem_obj
->abort
.data
);
1578 StoreEntry::dump(int l
) const
1580 debugs(20, l
, "StoreEntry->key: " << getMD5Text());
1581 debugs(20, l
, "StoreEntry->next: " << next
);
1582 debugs(20, l
, "StoreEntry->mem_obj: " << mem_obj
);
1583 debugs(20, l
, "StoreEntry->timestamp: " << timestamp
);
1584 debugs(20, l
, "StoreEntry->lastref: " << lastref
);
1585 debugs(20, l
, "StoreEntry->expires: " << expires
);
1586 debugs(20, l
, "StoreEntry->lastmod: " << lastmod
);
1587 debugs(20, l
, "StoreEntry->swap_file_sz: " << swap_file_sz
);
1588 debugs(20, l
, "StoreEntry->refcount: " << refcount
);
1589 debugs(20, l
, "StoreEntry->flags: " << storeEntryFlags(this));
1590 debugs(20, l
, "StoreEntry->swap_dirn: " << swap_dirn
);
1591 debugs(20, l
, "StoreEntry->swap_filen: " << swap_filen
);
1592 debugs(20, l
, "StoreEntry->lock_count: " << lock_count
);
1593 debugs(20, l
, "StoreEntry->mem_status: " << mem_status
);
1594 debugs(20, l
, "StoreEntry->ping_status: " << ping_status
);
1595 debugs(20, l
, "StoreEntry->store_status: " << store_status
);
1596 debugs(20, l
, "StoreEntry->swap_status: " << swap_status
);
1600 * NOTE, this function assumes only two mem states
1603 StoreEntry::setMemStatus(mem_status_t new_status
)
1605 if (new_status
== mem_status
)
1608 // are we using a shared memory cache?
1609 if (Config
.memShared
&& IamWorkerProcess()) {
1610 // This method was designed to update replacement policy, not to
1611 // actually purge something from the memory cache (TODO: rename?).
1612 // Shared memory cache does not have a policy that needs updates.
1613 mem_status
= new_status
;
1617 assert(mem_obj
!= NULL
);
1619 if (new_status
== IN_MEMORY
) {
1620 assert(mem_obj
->inmem_lo
== 0);
1622 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1623 debugs(20, 4, "not inserting special " << *this << " into policy");
1625 mem_policy
->Add(mem_policy
, this, &mem_obj
->repl
);
1626 debugs(20, 4, "inserted " << *this << " key: " << getMD5Text());
1629 ++hot_obj_count
; // TODO: maintain for the shared hot cache as well
1631 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
1632 debugs(20, 4, "not removing special " << *this << " from policy");
1634 mem_policy
->Remove(mem_policy
, this, &mem_obj
->repl
);
1635 debugs(20, 4, "removed " << *this);
1641 mem_status
= new_status
;
1645 StoreEntry::url() const
1648 return "[null_entry]";
1649 else if (mem_obj
== NULL
)
1650 return "[null_mem_obj]";
1652 return mem_obj
->storeId();
1656 StoreEntry::makeMemObject()
1659 mem_obj
= new MemObject();
1664 StoreEntry::createMemObject(const char *aUrl
, const char *aLogUrl
, const HttpRequestMethod
&aMethod
)
1667 mem_obj
->setUris(aUrl
, aLogUrl
, aMethod
);
1670 /* this just sets DELAY_SENDING */
1672 StoreEntry::buffer()
1674 EBIT_SET(flags
, DELAY_SENDING
);
1677 /* this just clears DELAY_SENDING and Invokes the handlers */
1681 if (EBIT_TEST(flags
, DELAY_SENDING
)) {
1682 EBIT_CLR(flags
, DELAY_SENDING
);
1688 StoreEntry::objectLen() const
1690 assert(mem_obj
!= NULL
);
1691 return mem_obj
->object_sz
;
1695 StoreEntry::contentLen() const
1697 assert(mem_obj
!= NULL
);
1698 assert(getReply() != NULL
);
1699 return objectLen() - getReply()->hdr_sz
;
1703 StoreEntry::getReply () const
1705 if (NULL
== mem_obj
)
1708 return mem_obj
->getReply();
1715 debugs(20, 3, "StoreEntry::reset: " << url());
1717 HttpReply
*rep
= (HttpReply
*) getReply(); // bypass const
1719 expires
= lastmod
= timestamp
= -1;
1725 * This routine calls the SETUP routine for each fs type.
1726 * I don't know where the best place for this is, and I'm not going to shuffle
1727 * around large chunks of code right now (that can be done once its working.)
1736 * called to add another store removal policy module
1739 storeReplAdd(const char *type
, REMOVALPOLICYCREATE
* create
)
1743 /* find the number of currently known repl types */
1744 for (i
= 0; storerepl_list
&& storerepl_list
[i
].typestr
; ++i
) {
1745 if (strcmp(storerepl_list
[i
].typestr
, type
) == 0) {
1746 debugs(20, DBG_IMPORTANT
, "WARNING: Trying to load store replacement policy " << type
<< " twice.");
1751 /* add the new type */
1752 storerepl_list
= static_cast<storerepl_entry_t
*>(xrealloc(storerepl_list
, (i
+ 2) * sizeof(storerepl_entry_t
)));
1754 memset(&storerepl_list
[i
+ 1], 0, sizeof(storerepl_entry_t
));
1756 storerepl_list
[i
].typestr
= type
;
1758 storerepl_list
[i
].create
= create
;
1762 * Create a removal policy instance
1765 createRemovalPolicy(RemovalPolicySettings
* settings
)
1767 storerepl_entry_t
*r
;
1769 for (r
= storerepl_list
; r
&& r
->typestr
; ++r
) {
1770 if (strcmp(r
->typestr
, settings
->type
) == 0)
1771 return r
->create(settings
->args
);
1774 debugs(20, DBG_IMPORTANT
, "ERROR: Unknown policy " << settings
->type
);
1775 debugs(20, DBG_IMPORTANT
, "ERROR: Be sure to have set cache_replacement_policy");
1776 debugs(20, DBG_IMPORTANT
, "ERROR: and memory_replacement_policy in squid.conf!");
1777 fatalf("ERROR: Unknown policy %s\n", settings
->type
);
1778 return NULL
; /* NOTREACHED */
1783 storeSwapFileNumberSet(StoreEntry
* e
, sfileno filn
)
1785 if (e
->swap_file_number
== filn
)
1790 storeDirMapBitReset(e
->swap_file_number
);
1791 storeDirLRUDelete(e
);
1792 e
->swap_file_number
= -1;
1794 assert(-1 == e
->swap_file_number
);
1795 storeDirMapBitSet(e
->swap_file_number
= filn
);
1803 * Replace a store entry with
1804 * a new reply. This eats the reply.
1807 StoreEntry::replaceHttpReply(HttpReply
*rep
, bool andStartWriting
)
1809 debugs(20, 3, "StoreEntry::replaceHttpReply: " << url());
1812 debugs(20, DBG_CRITICAL
, "Attempt to replace object with no in-memory representation");
1816 mem_obj
->replaceHttpReply(rep
);
1818 if (andStartWriting
)
1823 StoreEntry::startWriting()
1827 /* TODO: when we store headers serparately remove the header portion */
1828 /* TODO: mark the length of the headers ? */
1829 /* We ONLY want the headers */
1830 packerToStoreInit(&p
, this);
1835 const HttpReply
*rep
= getReply();
1838 rep
->packHeadersInto(&p
);
1839 mem_obj
->markEndOfReplyHeaders();
1840 EBIT_CLR(flags
, ENTRY_FWD_HDR_WAIT
);
1842 rep
->body
.packInto(&p
);
1848 StoreEntry::getSerialisedMetaData()
1850 StoreMeta
*tlv_list
= storeSwapMetaBuild(this);
1852 char *result
= storeSwapMetaPack(tlv_list
, &swap_hdr_sz
);
1853 storeSwapTLVFree(tlv_list
);
1854 assert (swap_hdr_sz
>= 0);
1855 mem_obj
->swap_hdr_sz
= (size_t) swap_hdr_sz
;
1860 StoreEntry::trimMemory(const bool preserveSwappable
)
1864 * Bug #1943. We must not let go any data for IN_MEMORY
1865 * objects. We have to wait until the mem_status changes.
1867 if (mem_status
== IN_MEMORY
)
1870 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
1871 return; // cannot trim because we do not load them again
1873 if (preserveSwappable
)
1874 mem_obj
->trimSwappable();
1876 mem_obj
->trimUnSwappable();
1878 debugs(88, 7, *this << " inmem_lo=" << mem_obj
->inmem_lo
);
1882 StoreEntry::modifiedSince(HttpRequest
* request
) const
1885 time_t mod_time
= lastmod
;
1888 mod_time
= timestamp
;
1890 debugs(88, 3, "modifiedSince: '" << url() << "'");
1892 debugs(88, 3, "modifiedSince: mod_time = " << mod_time
);
1897 /* Find size of the object */
1898 object_length
= getReply()->content_length
;
1900 if (object_length
< 0)
1901 object_length
= contentLen();
1903 if (mod_time
> request
->ims
) {
1904 debugs(88, 3, "--> YES: entry newer than client");
1906 } else if (mod_time
< request
->ims
) {
1907 debugs(88, 3, "--> NO: entry older than client");
1909 } else if (request
->imslen
< 0) {
1910 debugs(88, 3, "--> NO: same LMT, no client length");
1912 } else if (request
->imslen
== object_length
) {
1913 debugs(88, 3, "--> NO: same LMT, same length");
1916 debugs(88, 3, "--> YES: same LMT, different length");
1922 StoreEntry::hasEtag(ETag
&etag
) const
1924 if (const HttpReply
*reply
= getReply()) {
1925 etag
= reply
->header
.getETag(HDR_ETAG
);
1933 StoreEntry::hasIfMatchEtag(const HttpRequest
&request
) const
1935 const String reqETags
= request
.header
.getList(HDR_IF_MATCH
);
1936 return hasOneOfEtags(reqETags
, false);
1940 StoreEntry::hasIfNoneMatchEtag(const HttpRequest
&request
) const
1942 const String reqETags
= request
.header
.getList(HDR_IF_NONE_MATCH
);
1943 // weak comparison is allowed only for HEAD or full-body GET requests
1944 const bool allowWeakMatch
= !request
.flags
.isRanged
&&
1945 (request
.method
== Http::METHOD_GET
|| request
.method
== Http::METHOD_HEAD
);
1946 return hasOneOfEtags(reqETags
, allowWeakMatch
);
1949 /// whether at least one of the request ETags matches entity ETag
1951 StoreEntry::hasOneOfEtags(const String
&reqETags
, const bool allowWeakMatch
) const
1953 const ETag repETag
= getReply()->header
.getETag(HDR_ETAG
);
1955 return strListIsMember(&reqETags
, "*", ',');
1957 bool matched
= false;
1958 const char *pos
= NULL
;
1961 while (!matched
&& strListGetItem(&reqETags
, ',', &item
, &ilen
, &pos
)) {
1962 if (!strncmp(item
, "*", ilen
))
1966 str
.append(item
, ilen
);
1968 if (etagParseInit(&reqETag
, str
.termedBuf())) {
1969 matched
= allowWeakMatch
? etagIsWeakEqual(repETag
, reqETag
) :
1970 etagIsStrongEqual(repETag
, reqETag
);
1978 StoreEntry::store() const
1980 assert(0 <= swap_dirn
&& swap_dirn
< Config
.cacheSwap
.n_configured
);
1981 return INDEXSD(swap_dirn
);
1985 StoreEntry::unlink()
1987 store()->unlink(*this); // implies disconnect()
1990 swap_status
= SWAPOUT_NONE
;
1994 * return true if the entry is in a state where
1995 * it can accept more data (ie with write() method)
1998 StoreEntry::isAccepting() const
2000 if (STORE_PENDING
!= store_status
)
2003 if (EBIT_TEST(flags
, ENTRY_ABORTED
))
2009 std::ostream
&operator <<(std::ostream
&os
, const StoreEntry
&e
)
2014 if (e
.mem_obj
->xitTable
.index
> -1)
2015 os
<< 't' << e
.mem_obj
->xitTable
.index
;
2016 if (e
.mem_obj
->memCache
.index
> -1)
2017 os
<< 'm' << e
.mem_obj
->memCache
.index
;
2019 if (e
.swap_filen
> -1 || e
.swap_dirn
> -1)
2020 os
<< 'd' << e
.swap_filen
<< '@' << e
.swap_dirn
;
2024 // print only non-default status values, using unique letters
2025 if (e
.mem_status
!= NOT_IN_MEMORY
||
2026 e
.store_status
!= STORE_PENDING
||
2027 e
.swap_status
!= SWAPOUT_NONE
||
2028 e
.ping_status
!= PING_NONE
) {
2029 if (e
.mem_status
!= NOT_IN_MEMORY
) os
<< 'm';
2030 if (e
.store_status
!= STORE_PENDING
) os
<< 's';
2031 if (e
.swap_status
!= SWAPOUT_NONE
) os
<< 'w' << e
.swap_status
;
2032 if (e
.ping_status
!= PING_NONE
) os
<< 'p' << e
.ping_status
;
2035 // print only set flags, using unique letters
2037 if (EBIT_TEST(e
.flags
, ENTRY_SPECIAL
)) os
<< 'S';
2038 if (EBIT_TEST(e
.flags
, ENTRY_REVALIDATE
)) os
<< 'R';
2039 if (EBIT_TEST(e
.flags
, DELAY_SENDING
)) os
<< 'P';
2040 if (EBIT_TEST(e
.flags
, RELEASE_REQUEST
)) os
<< 'X';
2041 if (EBIT_TEST(e
.flags
, REFRESH_REQUEST
)) os
<< 'F';
2042 if (EBIT_TEST(e
.flags
, ENTRY_DISPATCHED
)) os
<< 'D';
2043 if (EBIT_TEST(e
.flags
, KEY_PRIVATE
)) os
<< 'I';
2044 if (EBIT_TEST(e
.flags
, ENTRY_FWD_HDR_WAIT
)) os
<< 'W';
2045 if (EBIT_TEST(e
.flags
, ENTRY_NEGCACHED
)) os
<< 'N';
2046 if (EBIT_TEST(e
.flags
, ENTRY_VALIDATED
)) os
<< 'V';
2047 if (EBIT_TEST(e
.flags
, ENTRY_BAD_LENGTH
)) os
<< 'L';
2048 if (EBIT_TEST(e
.flags
, ENTRY_ABORTED
)) os
<< 'A';
2051 if (e
.mem_obj
&& e
.mem_obj
->smpCollapsed
)
2054 return os
<< '/' << &e
<< '*' << e
.locks();
2057 /* NullStoreEntry */
2059 NullStoreEntry
NullStoreEntry::_instance
;
2062 NullStoreEntry::getInstance()
2068 NullStoreEntry::getMD5Text() const
2074 NullStoreEntry::operator delete(void*)
2076 fatal ("Attempt to delete NullStoreEntry\n");
2080 NullStoreEntry::getSerialisedMetaData()
2086 #include "Store.cci"