3 * DEBUG: section 90 Storage Manager Client-Side Interface
4 * AUTHOR: Duane Wessels
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.
32 * Portions copyright (c) 2003 Robert Collins <robertc@squid-cache.org>
38 #include "HttpReply.h"
39 #include "HttpRequest.h"
41 #include "MemObject.h"
42 #include "mime_header.h"
43 #include "profiler/Profiler.h"
44 #include "SquidConfig.h"
45 #include "StatCounters.h"
47 #include "store_swapin.h"
48 #include "StoreClient.h"
49 #include "StoreMeta.h"
50 #include "StoreMetaUnpacker.h"
52 #include "DelayPools.h"
56 * NOTE: 'Header' refers to the swapfile metadata header.
57 * 'OBJHeader' refers to the object header, with cannonical
58 * processed object headers (which may derive from FTP/HTTP etc
60 * 'Body' refers to the swapfile body, which is the full
61 * HTTP reply (including HTTP headers and body).
63 static StoreIOState::STRCB storeClientReadBody
;
64 static StoreIOState::STRCB storeClientReadHeader
;
65 static void storeClientCopy2(StoreEntry
* e
, store_client
* sc
);
66 static EVH storeClientCopyEvent
;
67 static bool CheckQuickAbortIsReasonable(StoreEntry
* entry
);
68 static void CheckQuickAbort(StoreEntry
* entry
);
70 CBDATA_CLASS_INIT(store_client
);
73 store_client::memReaderHasLowerOffset(int64_t anOffset
) const
75 return getType() == STORE_MEM_CLIENT
&& copyInto
.offset
< anOffset
;
79 store_client::getType() const
84 #if STORE_CLIENT_LIST_DEBUG
86 storeClientListSearch(const MemObject
* mem
, void *data
)
89 store_client
*sc
= NULL
;
91 for (node
= mem
->clients
.head
; node
; node
= node
->next
) {
94 if (sc
->owner
== data
)
102 storeClientIsThisAClient(store_client
* sc
, void *someClient
)
104 return sc
->owner
== someClient
;
108 #include "HttpRequest.h"
110 /* add client with fd to client list */
112 storeClientListAdd(StoreEntry
* e
, void *data
)
114 MemObject
*mem
= e
->mem_obj
;
117 #if STORE_CLIENT_LIST_DEBUG
119 if (storeClientListSearch(mem
, data
) != NULL
)
125 sc
= new store_client (e
);
133 store_client::callback(ssize_t sz
, bool error
)
135 StoreIOBuffer
result(sz
, 0 ,copyInto
.data
);
138 result
.flags
.error
= 1;
141 result
.flags
.error
= error
? 1 : 0;
144 result
.offset
= cmp_offset
;
145 assert(_callback
.pending());
146 cmp_offset
= copyInto
.offset
+ sz
;
147 STCB
*temphandler
= _callback
.callback_handler
;
148 void *cbdata
= _callback
.callback_data
;
149 _callback
= Callback(NULL
, NULL
);
150 copyInto
.data
= NULL
;
152 if (cbdataReferenceValid(cbdata
))
153 temphandler(cbdata
, result
);
155 cbdataReferenceDone(cbdata
);
159 storeClientCopyEvent(void *data
)
161 store_client
*sc
= (store_client
*)data
;
162 debugs(90, 3, "storeClientCopyEvent: Running");
163 assert (sc
->flags
.copy_event_pending
);
164 sc
->flags
.copy_event_pending
= false;
166 if (!sc
->_callback
.pending())
169 storeClientCopy2(sc
->entry
, sc
);
172 store_client::store_client(StoreEntry
*e
) : entry (e
)
176 , type (e
->storeClientType())
180 flags
.disk_io_pending
= false;
183 if (getType() == STORE_DISK_CLIENT
)
184 /* assert we'll be able to get the data we want */
185 /* maybe we should open swapin_sio here */
186 assert(entry
->swap_filen
> -1 || entry
->swappingOut());
188 #if STORE_CLIENT_LIST_DEBUG
190 owner
= cbdataReference(data
);
195 store_client::~store_client()
198 /* copy bytes requested by the client */
200 storeClientCopy(store_client
* sc
,
202 StoreIOBuffer copyInto
,
207 sc
->copy(e
, copyInto
,callback
,data
);
211 store_client::copy(StoreEntry
* anEntry
,
212 StoreIOBuffer copyRequest
,
216 assert (anEntry
== entry
);
217 assert (callback_fn
);
219 assert(!EBIT_TEST(entry
->flags
, ENTRY_ABORTED
));
220 debugs(90, 3, "store_client::copy: " << entry
->getMD5Text() << ", from " <<
221 copyRequest
.offset
<< ", for length " <<
222 (int) copyRequest
.length
<< ", cb " << callback_fn
<< ", cbdata " <<
225 #if STORE_CLIENT_LIST_DEBUG
227 assert(this == storeClientListSearch(entry
->mem_obj
, data
));
230 assert(!_callback
.pending());
231 #if ONLYCONTIGUOUSREQUESTS
233 assert(cmp_offset
== copyRequest
.offset
);
235 /* range requests will skip into the body */
236 cmp_offset
= copyRequest
.offset
;
237 _callback
= Callback (callback_fn
, cbdataReference(data
));
238 copyInto
.data
= copyRequest
.data
;
239 copyInto
.length
= copyRequest
.length
;
240 copyInto
.offset
= copyRequest
.offset
;
242 static bool copying (false);
245 PROF_start(storeClient_kickReads
);
246 /* we might be blocking comm reads due to readahead limits
247 * now we have a new offset, trigger those reads...
249 entry
->mem_obj
->kickReads();
250 PROF_stop(storeClient_kickReads
);
253 anEntry
->lock("store_client::copy"); // see deletion note below
255 storeClientCopy2(entry
, this);
257 // Bug 3480: This store_client object may be deleted now if, for example,
258 // the client rejects the hit response copied above. Use on-stack pointers!
261 anEntry
->kickProducer();
263 anEntry
->unlock("store_client::copy");
265 // Add no code here. This object may no longer exist.
268 /// Whether there is (or will be) more entry data for us.
270 store_client::moreToSend() const
272 if (entry
->store_status
== STORE_PENDING
)
273 return true; // there may be more coming
275 /* STORE_OK, including aborted entries: no more data is coming */
277 const int64_t len
= entry
->objectLen();
279 // If we do not know the entry length, then we have to open the swap file,
280 // which is only possible if there is one AND if we are allowed to use it.
281 const bool canSwapIn
= entry
->swap_filen
>= 0 &&
282 getType() == STORE_DISK_CLIENT
;
286 if (copyInto
.offset
>= len
)
287 return false; // sent everything there is
290 return true; // if we lack prefix, we can swap it in
292 // If we cannot swap in, make sure we have what we want in RAM. Otherwise,
293 // scheduleRead calls scheduleDiskRead which asserts on STORE_MEM_CLIENTs.
294 const MemObject
*mem
= entry
->mem_obj
;
296 mem
->inmem_lo
<= copyInto
.offset
&& copyInto
.offset
< mem
->endOffset();
300 storeClientCopy2(StoreEntry
* e
, store_client
* sc
)
302 /* reentrancy not allowed - note this could lead to
306 if (sc
->flags
.copy_event_pending
) {
310 if (EBIT_TEST(e
->flags
, ENTRY_FWD_HDR_WAIT
)) {
311 debugs(90, 5, "storeClientCopy2: returning because ENTRY_FWD_HDR_WAIT set");
315 if (sc
->flags
.store_copying
) {
316 sc
->flags
.copy_event_pending
= true;
317 debugs(90, 3, "storeClientCopy2: Queueing storeClientCopyEvent()");
318 eventAdd("storeClientCopyEvent", storeClientCopyEvent
, sc
, 0.0, 0);
322 debugs(90, 3, "storeClientCopy2: " << e
->getMD5Text());
323 assert(sc
->_callback
.pending());
325 * We used to check for ENTRY_ABORTED here. But there were some
326 * problems. For example, we might have a slow client (or two) and
327 * the server-side is reading far ahead and swapping to disk. Even
328 * if the server-side aborts, we want to give the client(s)
329 * everything we got before the abort condition occurred.
331 /* Warning: doCopy may indirectly free itself in callbacks,
332 * hence the lock to keep it active for the duration of
334 * XXX: Locking does not prevent calling sc destructor (it only prevents
335 * freeing sc memory) so sc may become invalid from C++ p.o.v.
338 cbdataInternalLock(sc
);
339 assert (!sc
->flags
.store_copying
);
341 assert (!sc
->flags
.store_copying
);
342 cbdataInternalUnlock(sc
);
346 store_client::doCopy(StoreEntry
*anEntry
)
348 assert (anEntry
== entry
);
349 flags
.store_copying
= true;
350 MemObject
*mem
= entry
->mem_obj
;
352 debugs(33, 5, "store_client::doCopy: co: " <<
353 copyInto
.offset
<< ", hi: " <<
357 /* There is no more to send! */
358 debugs(33, 3, HERE
<< "There is no more to send!");
360 flags
.store_copying
= false;
364 /* Check that we actually have data */
365 if (anEntry
->store_status
== STORE_PENDING
&& copyInto
.offset
>= mem
->endOffset()) {
366 debugs(90, 3, "store_client::doCopy: Waiting for more");
367 flags
.store_copying
= false;
372 * Slight weirdness here. We open a swapin file for any
373 * STORE_DISK_CLIENT, even if we can copy the requested chunk
374 * from memory in the next block. We must try to open the
375 * swapin file before sending any data to the client side. If
376 * we postpone the open, and then can not open the file later
377 * on, the client loses big time. Its transfer just gets cut
378 * off. Better to open it early (while the client side handler
379 * is clientCacheHit) so that we can fall back to a cache miss
383 if (STORE_DISK_CLIENT
== getType() && swapin_sio
== NULL
)
390 store_client::startSwapin()
392 debugs(90, 3, "store_client::doCopy: Need to open swap in file");
393 /* gotta open the swapin file */
395 if (storeTooManyDiskFilesOpen()) {
396 /* yuck -- this causes a TCP_SWAPFAIL_MISS on the client side */
398 flags
.store_copying
= false;
400 } else if (!flags
.disk_io_pending
) {
401 /* Don't set store_io_pending here */
402 storeSwapInStart(this);
404 if (swapin_sio
== NULL
) {
406 flags
.store_copying
= false;
411 * If the open succeeds we either copy from memory, or
412 * schedule a disk read in the next block.
418 debugs(90, DBG_IMPORTANT
, "WARNING: Averted multiple fd operation (1)");
419 flags
.store_copying
= false;
425 store_client::scheduleRead()
427 MemObject
*mem
= entry
->mem_obj
;
429 if (copyInto
.offset
>= mem
->inmem_lo
&& copyInto
.offset
< mem
->endOffset())
436 store_client::scheduleDiskRead()
438 /* What the client wants is not in memory. Schedule a disk read */
439 assert(STORE_DISK_CLIENT
== getType());
441 assert(!flags
.disk_io_pending
);
443 debugs(90, 3, "store_client::doCopy: reading from STORE");
447 flags
.store_copying
= false;
451 store_client::scheduleMemRead()
453 /* What the client wants is in memory */
455 debugs(90, 3, "store_client::doCopy: Copying normal from memory");
456 size_t sz
= entry
->mem_obj
->data_hdr
.copy(copyInto
);
458 flags
.store_copying
= false;
462 store_client::fileRead()
464 MemObject
*mem
= entry
->mem_obj
;
466 assert(_callback
.pending());
467 assert(!flags
.disk_io_pending
);
468 flags
.disk_io_pending
= true;
470 if (mem
->swap_hdr_sz
!= 0)
471 if (entry
->swap_status
== SWAPOUT_WRITING
)
472 assert(mem
->swapout
.sio
->offset() > copyInto
.offset
+ (int64_t)mem
->swap_hdr_sz
);
474 storeRead(swapin_sio
,
477 copyInto
.offset
+ mem
->swap_hdr_sz
,
478 mem
->swap_hdr_sz
== 0 ? storeClientReadHeader
479 : storeClientReadBody
,
484 store_client::readBody(const char *buf
, ssize_t len
)
486 int parsed_header
= 0;
488 // Don't assert disk_io_pending here.. may be called by read_header
489 flags
.disk_io_pending
= false;
490 assert(_callback
.pending());
491 debugs(90, 3, "storeClientReadBody: len " << len
<< "");
493 if (copyInto
.offset
== 0 && len
> 0 && entry
->getReply()->sline
.status() == Http::scNone
) {
494 /* Our structure ! */
495 HttpReply
*rep
= (HttpReply
*) entry
->getReply(); // bypass const
497 if (!rep
->parseCharBuf(copyInto
.data
, headersEnd(copyInto
.data
, len
))) {
498 debugs(90, DBG_CRITICAL
, "Could not parse headers from on disk object");
504 const HttpReply
*rep
= entry
->getReply();
505 if (len
> 0 && rep
&& entry
->mem_obj
->inmem_lo
== 0 && entry
->objectLen() <= (int64_t)Config
.Store
.maxInMemObjSize
&& Config
.onoff
.memory_cache_disk
) {
506 storeGetMemSpace(len
);
507 // The above may start to free our object so we need to check again
508 if (entry
->mem_obj
->inmem_lo
== 0) {
509 /* Copy read data back into memory.
510 * copyInto.offset includes headers, which is what mem cache needs
512 int64_t mem_offset
= entry
->mem_obj
->endOffset();
513 if ((copyInto
.offset
== mem_offset
) || (parsed_header
&& mem_offset
== rep
->hdr_sz
)) {
514 entry
->mem_obj
->write(StoreIOBuffer(len
, copyInto
.offset
, copyInto
.data
));
526 /* synchronous open failures callback from the store,
527 * before startSwapin detects the failure.
528 * TODO: fix this inconsistent behaviour - probably by
529 * having storeSwapInStart become a callback functions,
533 if (_callback
.pending())
538 storeClientReadHeader(void *data
, const char *buf
, ssize_t len
, StoreIOState::Pointer self
)
540 store_client
*sc
= (store_client
*)data
;
541 sc
->readHeader(buf
, len
);
545 storeClientReadBody(void *data
, const char *buf
, ssize_t len
, StoreIOState::Pointer self
)
547 store_client
*sc
= (store_client
*)data
;
548 sc
->readBody(buf
, len
);
552 store_client::unpackHeader(char const *buf
, ssize_t len
)
554 debugs(90, 3, "store_client::unpackHeader: len " << len
<< "");
557 debugs(90, 3, "store_client::unpackHeader: " << xstrerror() << "");
563 StoreMetaUnpacker
aBuilder(buf
, len
, &swap_hdr_sz
);
565 if (!aBuilder
.isBufferSane()) {
566 /* oops, bad disk file? */
567 debugs(90, DBG_IMPORTANT
, "WARNING: swapfile header inconsistent with available data");
572 tlv
*tlv_list
= aBuilder
.createStoreMeta ();
574 if (tlv_list
== NULL
) {
575 debugs(90, DBG_IMPORTANT
, "WARNING: failed to unpack meta data");
581 * Check the meta data and make sure we got the right object.
583 for (tlv
*t
= tlv_list
; t
; t
= t
->next
) {
584 if (!t
->checkConsistency(entry
)) {
585 storeSwapTLVFree(tlv_list
);
591 storeSwapTLVFree(tlv_list
);
593 assert(swap_hdr_sz
>= 0);
594 entry
->mem_obj
->swap_hdr_sz
= swap_hdr_sz
;
595 if (entry
->swap_file_sz
> 0) { // collapsed hits may not know swap_file_sz
596 assert(entry
->swap_file_sz
>= static_cast<uint64_t>(swap_hdr_sz
));
597 entry
->mem_obj
->object_sz
= entry
->swap_file_sz
- swap_hdr_sz
;
599 debugs(90, 5, "store_client::unpackHeader: swap_file_sz=" <<
600 entry
->swap_file_sz
<< "( " << swap_hdr_sz
<< " + " <<
601 entry
->mem_obj
->object_sz
<< ")");
605 store_client::readHeader(char const *buf
, ssize_t len
)
607 MemObject
*const mem
= entry
->mem_obj
;
609 assert(flags
.disk_io_pending
);
610 flags
.disk_io_pending
= false;
611 assert(_callback
.pending());
613 unpackHeader (buf
, len
);
619 * If our last read got some data the client wants, then give
620 * it to them, otherwise schedule another read.
622 size_t body_sz
= len
- mem
->swap_hdr_sz
;
624 if (copyInto
.offset
< static_cast<int64_t>(body_sz
)) {
626 * we have (part of) what they want
628 size_t copy_sz
= min(copyInto
.length
, body_sz
);
629 debugs(90, 3, "storeClientReadHeader: copying " << copy_sz
<< " bytes of body");
630 memmove(copyInto
.data
, copyInto
.data
+ mem
->swap_hdr_sz
, copy_sz
);
632 readBody(copyInto
.data
, copy_sz
);
638 * we don't have what the client wants, but at least we now
639 * know the swap header size.
645 storeClientCopyPending(store_client
* sc
, StoreEntry
* e
, void *data
)
647 #if STORE_CLIENT_LIST_DEBUG
648 assert(sc
== storeClientListSearch(e
->mem_obj
, data
));
655 assert(sc
->entry
== e
);
663 if (!sc
->_callback
.pending())
670 * This routine hasn't been optimised to take advantage of the
674 storeUnregister(store_client
* sc
, StoreEntry
* e
, void *data
)
676 MemObject
*mem
= e
->mem_obj
;
677 #if STORE_CLIENT_LIST_DEBUG
679 assert(sc
== storeClientListSearch(e
->mem_obj
, data
));
685 debugs(90, 3, "storeUnregister: called for '" << e
->getMD5Text() << "'");
688 debugs(90, 3, "storeUnregister: No matching client for '" << e
->getMD5Text() << "'");
692 if (mem
->clientCount() == 0) {
693 debugs(90, 3, "storeUnregister: Consistency failure - store client being unregistered is not in the mem object's list for '" << e
->getMD5Text() << "'");
697 dlinkDelete(&sc
->node
, &mem
->clients
);
700 if (e
->store_status
== STORE_OK
&& e
->swap_status
!= SWAPOUT_DONE
)
703 if (sc
->swapin_sio
!= NULL
) {
704 storeClose(sc
->swapin_sio
, StoreIOState::readerDone
);
705 sc
->swapin_sio
= NULL
;
706 ++statCounter
.swap
.ins
;
709 if (sc
->_callback
.pending()) {
710 /* callback with ssize = -1 to indicate unexpected termination */
711 debugs(90, 3, "store_client for " << *e
<< " has a callback");
715 #if STORE_CLIENT_LIST_DEBUG
716 cbdataReferenceDone(sc
->owner
);
724 if (mem
->nclients
== 0)
733 e
->unlock(); // after the "++e->lock_count" above
737 /* Call handlers waiting for data to be appended to E. */
739 StoreEntry::invokeHandlers()
741 /* Commit what we can to disk, if appropriate */
745 dlink_node
*nx
= NULL
;
748 PROF_start(InvokeHandlers
);
750 debugs(90, 3, "InvokeHandlers: " << getMD5Text() );
751 /* walk the entire list looking for valid callbacks */
753 for (node
= mem_obj
->clients
.head
; node
; node
= nx
) {
754 sc
= (store_client
*)node
->data
;
756 debugs(90, 3, "StoreEntry::InvokeHandlers: checking client #" << i
);
759 if (!sc
->_callback
.pending())
762 if (sc
->flags
.disk_io_pending
)
765 storeClientCopy2(this, sc
);
767 PROF_stop(InvokeHandlers
);
770 // Does not account for remote readers/clients.
772 storePendingNClients(const StoreEntry
* e
)
774 MemObject
*mem
= e
->mem_obj
;
775 int npend
= NULL
== mem
? 0 : mem
->nclients
;
776 debugs(90, 3, "storePendingNClients: returning " << npend
);
780 /* return true if the request should be aborted */
782 CheckQuickAbortIsReasonable(StoreEntry
* entry
)
784 MemObject
* const mem
= entry
->mem_obj
;
786 debugs(90, 3, "entry=" << entry
<< ", mem=" << mem
);
788 if (mem
->request
&& !mem
->request
->flags
.cachable
) {
789 debugs(90, 3, "quick-abort? YES !mem->request->flags.cachable");
793 if (EBIT_TEST(entry
->flags
, KEY_PRIVATE
)) {
794 debugs(90, 3, "quick-abort? YES KEY_PRIVATE");
798 int64_t expectlen
= entry
->getReply()->content_length
+ entry
->getReply()->hdr_sz
;
801 /* expectlen is < 0 if *no* information about the object has been received */
802 debugs(90, 3, "quick-abort? YES no object data received yet");
806 int64_t curlen
= mem
->endOffset();
808 if (Config
.quickAbort
.min
< 0) {
809 debugs(90, 3, "quick-abort? NO disabled");
813 if (mem
->request
&& mem
->request
->range
&& mem
->request
->getRangeOffsetLimit() < 0) {
814 /* Don't abort if the admin has configured range_ofset -1 to download fully for caching. */
815 debugs(90, 3, "quick-abort? NO admin configured range replies to full-download");
819 if (curlen
> expectlen
) {
820 debugs(90, 3, "quick-abort? YES bad content length (" << curlen
<< " of " << expectlen
<< " bytes received)");
824 if ((expectlen
- curlen
) < (Config
.quickAbort
.min
<< 10)) {
825 debugs(90, 3, "quick-abort? NO only a little more object left to receive");
829 if ((expectlen
- curlen
) > (Config
.quickAbort
.max
<< 10)) {
830 debugs(90, 3, "quick-abort? YES too much left to go");
834 if (expectlen
< 100) {
835 debugs(90, 3, "quick-abort? NO avoid FPE");
839 if ((curlen
/ (expectlen
/ 100)) > (Config
.quickAbort
.pct
)) {
840 debugs(90, 3, "quick-abort? NO past point of no return");
844 debugs(90, 3, "quick-abort? YES default");
848 /// Aborts a swapping-out entry if nobody needs it any more _and_
849 /// continuing swap out is not reasonable per CheckQuickAbortIsReasonable().
851 CheckQuickAbort(StoreEntry
* entry
)
855 if (storePendingNClients(entry
) > 0)
858 if (!shutting_down
&& Store::Root().transientReaders(*entry
))
861 if (entry
->store_status
!= STORE_PENDING
)
864 if (EBIT_TEST(entry
->flags
, ENTRY_SPECIAL
))
867 if (!CheckQuickAbortIsReasonable(entry
))
874 store_client::dumpStats(MemBuf
* output
, int clientNumber
) const
876 if (_callback
.pending())
879 output
->Printf("\tClient #%d, %p\n", clientNumber
, _callback
.callback_data
);
881 output
->Printf("\t\tcopy_offset: %" PRId64
"\n",
884 output
->Printf("\t\tcopy_size: %d\n",
885 (int) copyInto
.length
);
887 output
->Printf("\t\tflags:");
889 if (flags
.disk_io_pending
)
890 output
->Printf(" disk_io_pending");
892 if (flags
.store_copying
)
893 output
->Printf(" store_copying");
895 if (flags
.copy_event_pending
)
896 output
->Printf(" copy_event_pending");
898 output
->Printf("\n");
902 store_client::Callback::pending() const
904 return callback_handler
&& callback_data
;
907 store_client::Callback::Callback(STCB
*function
, void *data
) : callback_handler(function
), callback_data (data
) {}
911 store_client::setDelayId(DelayId delay_id
)