2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 90 Storage Manager Client-Side Interface */
14 #include "HttpReply.h"
15 #include "HttpRequest.h"
17 #include "MemObject.h"
18 #include "mime_header.h"
19 #include "profiler/Profiler.h"
20 #include "SquidConfig.h"
21 #include "StatCounters.h"
23 #include "store_swapin.h"
24 #include "StoreClient.h"
25 #include "StoreMeta.h"
26 #include "StoreMetaUnpacker.h"
28 #include "DelayPools.h"
32 * NOTE: 'Header' refers to the swapfile metadata header.
33 * 'OBJHeader' refers to the object header, with cannonical
34 * processed object headers (which may derive from FTP/HTTP etc
36 * 'Body' refers to the swapfile body, which is the full
37 * HTTP reply (including HTTP headers and body).
39 static StoreIOState::STRCB storeClientReadBody
;
40 static StoreIOState::STRCB storeClientReadHeader
;
41 static void storeClientCopy2(StoreEntry
* e
, store_client
* sc
);
42 static EVH storeClientCopyEvent
;
43 static bool CheckQuickAbortIsReasonable(StoreEntry
* entry
);
44 static void CheckQuickAbort(StoreEntry
* entry
);
46 CBDATA_CLASS_INIT(store_client
);
49 store_client::memReaderHasLowerOffset(int64_t anOffset
) const
51 return getType() == STORE_MEM_CLIENT
&& copyInto
.offset
< anOffset
;
55 store_client::getType() const
60 #if STORE_CLIENT_LIST_DEBUG
62 storeClientListSearch(const MemObject
* mem
, void *data
)
65 store_client
*sc
= NULL
;
67 for (node
= mem
->clients
.head
; node
; node
= node
->next
) {
70 if (sc
->owner
== data
)
78 storeClientIsThisAClient(store_client
* sc
, void *someClient
)
80 return sc
->owner
== someClient
;
84 #include "HttpRequest.h"
86 /* add client with fd to client list */
88 storeClientListAdd(StoreEntry
* e
, void *data
)
90 MemObject
*mem
= e
->mem_obj
;
93 #if STORE_CLIENT_LIST_DEBUG
95 if (storeClientListSearch(mem
, data
) != NULL
)
101 sc
= new store_client (e
);
109 store_client::callback(ssize_t sz
, bool error
)
113 if (sz
>= 0 && !error
)
116 StoreIOBuffer
result(bSz
, 0 ,copyInto
.data
);
119 result
.flags
.error
= 1;
121 result
.offset
= cmp_offset
;
122 assert(_callback
.pending());
123 cmp_offset
= copyInto
.offset
+ bSz
;
124 STCB
*temphandler
= _callback
.callback_handler
;
125 void *cbdata
= _callback
.callback_data
;
126 _callback
= Callback(NULL
, NULL
);
127 copyInto
.data
= NULL
;
129 if (cbdataReferenceValid(cbdata
))
130 temphandler(cbdata
, result
);
132 cbdataReferenceDone(cbdata
);
136 storeClientCopyEvent(void *data
)
138 store_client
*sc
= (store_client
*)data
;
139 debugs(90, 3, "storeClientCopyEvent: Running");
140 assert (sc
->flags
.copy_event_pending
);
141 sc
->flags
.copy_event_pending
= false;
143 if (!sc
->_callback
.pending())
146 storeClientCopy2(sc
->entry
, sc
);
149 store_client::store_client(StoreEntry
*e
) : entry (e
)
153 , type (e
->storeClientType())
157 flags
.disk_io_pending
= false;
160 if (getType() == STORE_DISK_CLIENT
)
161 /* assert we'll be able to get the data we want */
162 /* maybe we should open swapin_sio here */
163 assert(entry
->swap_filen
> -1 || entry
->swappingOut());
165 #if STORE_CLIENT_LIST_DEBUG
167 owner
= cbdataReference(data
);
172 store_client::~store_client()
175 /* copy bytes requested by the client */
177 storeClientCopy(store_client
* sc
,
179 StoreIOBuffer copyInto
,
184 sc
->copy(e
, copyInto
,callback
,data
);
188 store_client::copy(StoreEntry
* anEntry
,
189 StoreIOBuffer copyRequest
,
193 assert (anEntry
== entry
);
194 assert (callback_fn
);
196 assert(!EBIT_TEST(entry
->flags
, ENTRY_ABORTED
));
197 debugs(90, 3, "store_client::copy: " << entry
->getMD5Text() << ", from " <<
198 copyRequest
.offset
<< ", for length " <<
199 (int) copyRequest
.length
<< ", cb " << callback_fn
<< ", cbdata " <<
202 #if STORE_CLIENT_LIST_DEBUG
204 assert(this == storeClientListSearch(entry
->mem_obj
, data
));
207 assert(!_callback
.pending());
208 #if ONLYCONTIGUOUSREQUESTS
210 assert(cmp_offset
== copyRequest
.offset
);
212 /* range requests will skip into the body */
213 cmp_offset
= copyRequest
.offset
;
214 _callback
= Callback (callback_fn
, cbdataReference(data
));
215 copyInto
.data
= copyRequest
.data
;
216 copyInto
.length
= copyRequest
.length
;
217 copyInto
.offset
= copyRequest
.offset
;
219 static bool copying (false);
222 PROF_start(storeClient_kickReads
);
223 /* we might be blocking comm reads due to readahead limits
224 * now we have a new offset, trigger those reads...
226 entry
->mem_obj
->kickReads();
227 PROF_stop(storeClient_kickReads
);
230 anEntry
->lock("store_client::copy"); // see deletion note below
232 storeClientCopy2(entry
, this);
234 // Bug 3480: This store_client object may be deleted now if, for example,
235 // the client rejects the hit response copied above. Use on-stack pointers!
238 anEntry
->kickProducer();
240 anEntry
->unlock("store_client::copy");
242 // Add no code here. This object may no longer exist.
245 /// Whether there is (or will be) more entry data for us.
247 store_client::moreToSend() const
249 if (entry
->store_status
== STORE_PENDING
)
250 return true; // there may be more coming
252 /* STORE_OK, including aborted entries: no more data is coming */
254 const int64_t len
= entry
->objectLen();
256 // If we do not know the entry length, then we have to open the swap file.
257 const bool canSwapIn
= entry
->swap_filen
>= 0;
261 if (copyInto
.offset
>= len
)
262 return false; // sent everything there is
265 return true; // if we lack prefix, we can swap it in
267 // If we cannot swap in, make sure we have what we want in RAM. Otherwise,
268 // scheduleRead calls scheduleDiskRead which asserts without a swap file.
269 const MemObject
*mem
= entry
->mem_obj
;
271 mem
->inmem_lo
<= copyInto
.offset
&& copyInto
.offset
< mem
->endOffset();
275 storeClientCopy2(StoreEntry
* e
, store_client
* sc
)
277 /* reentrancy not allowed - note this could lead to
281 if (sc
->flags
.copy_event_pending
) {
285 if (EBIT_TEST(e
->flags
, ENTRY_FWD_HDR_WAIT
)) {
286 debugs(90, 5, "storeClientCopy2: returning because ENTRY_FWD_HDR_WAIT set");
290 if (sc
->flags
.store_copying
) {
291 sc
->flags
.copy_event_pending
= true;
292 debugs(90, 3, "storeClientCopy2: Queueing storeClientCopyEvent()");
293 eventAdd("storeClientCopyEvent", storeClientCopyEvent
, sc
, 0.0, 0);
297 debugs(90, 3, "storeClientCopy2: " << e
->getMD5Text());
298 assert(sc
->_callback
.pending());
300 * We used to check for ENTRY_ABORTED here. But there were some
301 * problems. For example, we might have a slow client (or two) and
302 * the peer server is reading far ahead and swapping to disk. Even
303 * if the peer aborts, we want to give the client(s)
304 * everything we got before the abort condition occurred.
306 /* Warning: doCopy may indirectly free itself in callbacks,
307 * hence the lock to keep it active for the duration of
309 * XXX: Locking does not prevent calling sc destructor (it only prevents
310 * freeing sc memory) so sc may become invalid from C++ p.o.v.
312 CbcPointer
<store_client
> tmpLock
= sc
;
313 assert (!sc
->flags
.store_copying
);
315 assert(!sc
->flags
.store_copying
);
319 store_client::doCopy(StoreEntry
*anEntry
)
321 assert (anEntry
== entry
);
322 flags
.store_copying
= true;
323 MemObject
*mem
= entry
->mem_obj
;
325 debugs(33, 5, "store_client::doCopy: co: " <<
326 copyInto
.offset
<< ", hi: " <<
330 /* There is no more to send! */
331 debugs(33, 3, HERE
<< "There is no more to send!");
333 flags
.store_copying
= false;
337 /* Check that we actually have data */
338 if (anEntry
->store_status
== STORE_PENDING
&& copyInto
.offset
>= mem
->endOffset()) {
339 debugs(90, 3, "store_client::doCopy: Waiting for more");
340 flags
.store_copying
= false;
345 * Slight weirdness here. We open a swapin file for any
346 * STORE_DISK_CLIENT, even if we can copy the requested chunk
347 * from memory in the next block. We must try to open the
348 * swapin file before sending any data to the client side. If
349 * we postpone the open, and then can not open the file later
350 * on, the client loses big time. Its transfer just gets cut
351 * off. Better to open it early (while the client side handler
352 * is clientCacheHit) so that we can fall back to a cache miss
356 if (STORE_DISK_CLIENT
== getType() && swapin_sio
== NULL
) {
363 /// opens the swapin "file" if possible; otherwise, fail()s and returns false
365 store_client::startSwapin()
367 debugs(90, 3, "store_client::doCopy: Need to open swap in file");
368 /* gotta open the swapin file */
370 if (storeTooManyDiskFilesOpen()) {
371 /* yuck -- this causes a TCP_SWAPFAIL_MISS on the client side */
373 flags
.store_copying
= false;
375 } else if (!flags
.disk_io_pending
) {
376 /* Don't set store_io_pending here */
377 storeSwapInStart(this);
379 if (swapin_sio
== NULL
) {
381 flags
.store_copying
= false;
387 debugs(90, DBG_IMPORTANT
, "WARNING: Averted multiple fd operation (1)");
388 flags
.store_copying
= false;
394 store_client::scheduleRead()
396 MemObject
*mem
= entry
->mem_obj
;
398 if (copyInto
.offset
>= mem
->inmem_lo
&& copyInto
.offset
< mem
->endOffset())
405 store_client::scheduleDiskRead()
407 /* What the client wants is not in memory. Schedule a disk read */
408 if (getType() == STORE_DISK_CLIENT
) {
409 // we should have called startSwapin() already
410 assert(swapin_sio
!= NULL
);
411 } else if (!swapin_sio
&& !startSwapin()) {
412 debugs(90, 3, "bailing after swapin start failure for " << *entry
);
413 assert(!flags
.store_copying
);
417 assert(!flags
.disk_io_pending
);
419 debugs(90, 3, "reading " << *entry
<< " from disk");
423 flags
.store_copying
= false;
427 store_client::scheduleMemRead()
429 /* What the client wants is in memory */
431 debugs(90, 3, "store_client::doCopy: Copying normal from memory");
432 size_t sz
= entry
->mem_obj
->data_hdr
.copy(copyInto
);
434 flags
.store_copying
= false;
438 store_client::fileRead()
440 MemObject
*mem
= entry
->mem_obj
;
442 assert(_callback
.pending());
443 assert(!flags
.disk_io_pending
);
444 flags
.disk_io_pending
= true;
446 if (mem
->swap_hdr_sz
!= 0)
447 if (entry
->swap_status
== SWAPOUT_WRITING
)
448 assert(mem
->swapout
.sio
->offset() > copyInto
.offset
+ (int64_t)mem
->swap_hdr_sz
);
450 storeRead(swapin_sio
,
453 copyInto
.offset
+ mem
->swap_hdr_sz
,
454 mem
->swap_hdr_sz
== 0 ? storeClientReadHeader
455 : storeClientReadBody
,
460 store_client::readBody(const char *, ssize_t len
)
462 int parsed_header
= 0;
464 // Don't assert disk_io_pending here.. may be called by read_header
465 flags
.disk_io_pending
= false;
466 assert(_callback
.pending());
467 debugs(90, 3, "storeClientReadBody: len " << len
<< "");
469 if (copyInto
.offset
== 0 && len
> 0 && entry
->getReply()->sline
.status() == Http::scNone
) {
470 /* Our structure ! */
471 HttpReply
*rep
= (HttpReply
*) entry
->getReply(); // bypass const
473 if (!rep
->parseCharBuf(copyInto
.data
, headersEnd(copyInto
.data
, len
))) {
474 debugs(90, DBG_CRITICAL
, "Could not parse headers from on disk object");
480 const HttpReply
*rep
= entry
->getReply();
481 if (len
> 0 && rep
&& entry
->mem_obj
->inmem_lo
== 0 && entry
->objectLen() <= (int64_t)Config
.Store
.maxInMemObjSize
&& Config
.onoff
.memory_cache_disk
) {
482 storeGetMemSpace(len
);
483 // The above may start to free our object so we need to check again
484 if (entry
->mem_obj
->inmem_lo
== 0) {
485 /* Copy read data back into memory.
486 * copyInto.offset includes headers, which is what mem cache needs
488 int64_t mem_offset
= entry
->mem_obj
->endOffset();
489 if ((copyInto
.offset
== mem_offset
) || (parsed_header
&& mem_offset
== rep
->hdr_sz
)) {
490 entry
->mem_obj
->write(StoreIOBuffer(len
, copyInto
.offset
, copyInto
.data
));
502 /* synchronous open failures callback from the store,
503 * before startSwapin detects the failure.
504 * TODO: fix this inconsistent behaviour - probably by
505 * having storeSwapInStart become a callback functions,
509 if (_callback
.pending())
514 storeClientReadHeader(void *data
, const char *buf
, ssize_t len
, StoreIOState::Pointer
)
516 store_client
*sc
= (store_client
*)data
;
517 sc
->readHeader(buf
, len
);
521 storeClientReadBody(void *data
, const char *buf
, ssize_t len
, StoreIOState::Pointer
)
523 store_client
*sc
= (store_client
*)data
;
524 sc
->readBody(buf
, len
);
528 store_client::unpackHeader(char const *buf
, ssize_t len
)
530 debugs(90, 3, "store_client::unpackHeader: len " << len
<< "");
533 debugs(90, 3, "store_client::unpackHeader: " << xstrerror() << "");
539 StoreMetaUnpacker
aBuilder(buf
, len
, &swap_hdr_sz
);
541 if (!aBuilder
.isBufferSane()) {
542 /* oops, bad disk file? */
543 debugs(90, DBG_IMPORTANT
, "WARNING: swapfile header inconsistent with available data");
548 tlv
*tlv_list
= aBuilder
.createStoreMeta ();
550 if (tlv_list
== NULL
) {
551 debugs(90, DBG_IMPORTANT
, "WARNING: failed to unpack meta data");
557 * Check the meta data and make sure we got the right object.
559 for (tlv
*t
= tlv_list
; t
; t
= t
->next
) {
560 if (!t
->checkConsistency(entry
)) {
561 storeSwapTLVFree(tlv_list
);
567 storeSwapTLVFree(tlv_list
);
569 assert(swap_hdr_sz
>= 0);
570 entry
->mem_obj
->swap_hdr_sz
= swap_hdr_sz
;
571 if (entry
->swap_file_sz
> 0) { // collapsed hits may not know swap_file_sz
572 assert(entry
->swap_file_sz
>= static_cast<uint64_t>(swap_hdr_sz
));
573 entry
->mem_obj
->object_sz
= entry
->swap_file_sz
- swap_hdr_sz
;
575 debugs(90, 5, "store_client::unpackHeader: swap_file_sz=" <<
576 entry
->swap_file_sz
<< "( " << swap_hdr_sz
<< " + " <<
577 entry
->mem_obj
->object_sz
<< ")");
581 store_client::readHeader(char const *buf
, ssize_t len
)
583 MemObject
*const mem
= entry
->mem_obj
;
585 assert(flags
.disk_io_pending
);
586 flags
.disk_io_pending
= false;
587 assert(_callback
.pending());
589 unpackHeader (buf
, len
);
595 * If our last read got some data the client wants, then give
596 * it to them, otherwise schedule another read.
598 size_t body_sz
= len
- mem
->swap_hdr_sz
;
600 if (copyInto
.offset
< static_cast<int64_t>(body_sz
)) {
602 * we have (part of) what they want
604 size_t copy_sz
= min(copyInto
.length
, body_sz
);
605 debugs(90, 3, "storeClientReadHeader: copying " << copy_sz
<< " bytes of body");
606 memmove(copyInto
.data
, copyInto
.data
+ mem
->swap_hdr_sz
, copy_sz
);
608 readBody(copyInto
.data
, copy_sz
);
614 * we don't have what the client wants, but at least we now
615 * know the swap header size.
621 storeClientCopyPending(store_client
* sc
, StoreEntry
* e
, void *data
)
623 #if STORE_CLIENT_LIST_DEBUG
624 assert(sc
== storeClientListSearch(e
->mem_obj
, data
));
631 assert(sc
->entry
== e
);
639 if (!sc
->_callback
.pending())
646 * This routine hasn't been optimised to take advantage of the
650 storeUnregister(store_client
* sc
, StoreEntry
* e
, void *data
)
652 MemObject
*mem
= e
->mem_obj
;
653 #if STORE_CLIENT_LIST_DEBUG
655 assert(sc
== storeClientListSearch(e
->mem_obj
, data
));
661 debugs(90, 3, "storeUnregister: called for '" << e
->getMD5Text() << "'");
664 debugs(90, 3, "storeUnregister: No matching client for '" << e
->getMD5Text() << "'");
668 if (mem
->clientCount() == 0) {
669 debugs(90, 3, "storeUnregister: Consistency failure - store client being unregistered is not in the mem object's list for '" << e
->getMD5Text() << "'");
673 dlinkDelete(&sc
->node
, &mem
->clients
);
676 if (e
->store_status
== STORE_OK
&& e
->swap_status
!= SWAPOUT_DONE
)
679 if (sc
->swapin_sio
!= NULL
) {
680 storeClose(sc
->swapin_sio
, StoreIOState::readerDone
);
681 sc
->swapin_sio
= NULL
;
682 ++statCounter
.swap
.ins
;
685 if (sc
->_callback
.pending()) {
686 /* callback with ssize = -1 to indicate unexpected termination */
687 debugs(90, 3, "store_client for " << *e
<< " has a callback");
691 #if STORE_CLIENT_LIST_DEBUG
692 cbdataReferenceDone(sc
->owner
);
699 // An entry locked by others may be unlocked (and destructed) by others, so
700 // we must lock again to safely dereference e after CheckQuickAbort().
701 e
->lock("storeUnregister");
703 if (mem
->nclients
== 0)
712 e
->unlock("storeUnregister");
716 /* Call handlers waiting for data to be appended to E. */
718 StoreEntry::invokeHandlers()
720 /* Commit what we can to disk, if appropriate */
724 dlink_node
*nx
= NULL
;
727 PROF_start(InvokeHandlers
);
729 debugs(90, 3, "InvokeHandlers: " << getMD5Text() );
730 /* walk the entire list looking for valid callbacks */
732 for (node
= mem_obj
->clients
.head
; node
; node
= nx
) {
733 sc
= (store_client
*)node
->data
;
735 debugs(90, 3, "StoreEntry::InvokeHandlers: checking client #" << i
);
738 if (!sc
->_callback
.pending())
741 if (sc
->flags
.disk_io_pending
)
744 storeClientCopy2(this, sc
);
746 PROF_stop(InvokeHandlers
);
749 // Does not account for remote readers/clients.
751 storePendingNClients(const StoreEntry
* e
)
753 MemObject
*mem
= e
->mem_obj
;
754 int npend
= NULL
== mem
? 0 : mem
->nclients
;
755 debugs(90, 3, "storePendingNClients: returning " << npend
);
759 /* return true if the request should be aborted */
761 CheckQuickAbortIsReasonable(StoreEntry
* entry
)
763 MemObject
* const mem
= entry
->mem_obj
;
765 debugs(90, 3, "entry=" << entry
<< ", mem=" << mem
);
767 if (mem
->request
&& !mem
->request
->flags
.cachable
) {
768 debugs(90, 3, "quick-abort? YES !mem->request->flags.cachable");
772 if (EBIT_TEST(entry
->flags
, KEY_PRIVATE
)) {
773 debugs(90, 3, "quick-abort? YES KEY_PRIVATE");
777 int64_t expectlen
= entry
->getReply()->content_length
+ entry
->getReply()->hdr_sz
;
780 /* expectlen is < 0 if *no* information about the object has been received */
781 debugs(90, 3, "quick-abort? YES no object data received yet");
785 int64_t curlen
= mem
->endOffset();
787 if (Config
.quickAbort
.min
< 0) {
788 debugs(90, 3, "quick-abort? NO disabled");
792 if (mem
->request
&& mem
->request
->range
&& mem
->request
->getRangeOffsetLimit() < 0) {
793 /* Don't abort if the admin has configured range_ofset -1 to download fully for caching. */
794 debugs(90, 3, "quick-abort? NO admin configured range replies to full-download");
798 if (curlen
> expectlen
) {
799 debugs(90, 3, "quick-abort? YES bad content length (" << curlen
<< " of " << expectlen
<< " bytes received)");
803 if ((expectlen
- curlen
) < (Config
.quickAbort
.min
<< 10)) {
804 debugs(90, 3, "quick-abort? NO only a little more object left to receive");
808 if ((expectlen
- curlen
) > (Config
.quickAbort
.max
<< 10)) {
809 debugs(90, 3, "quick-abort? YES too much left to go");
813 if (expectlen
< 100) {
814 debugs(90, 3, "quick-abort? NO avoid FPE");
818 if ((curlen
/ (expectlen
/ 100)) > (Config
.quickAbort
.pct
)) {
819 debugs(90, 3, "quick-abort? NO past point of no return");
823 debugs(90, 3, "quick-abort? YES default");
827 /// Aborts a swapping-out entry if nobody needs it any more _and_
828 /// continuing swap out is not reasonable per CheckQuickAbortIsReasonable().
830 CheckQuickAbort(StoreEntry
* entry
)
834 if (storePendingNClients(entry
) > 0)
837 if (!shutting_down
&& Store::Root().transientReaders(*entry
))
840 if (entry
->store_status
!= STORE_PENDING
)
843 if (EBIT_TEST(entry
->flags
, ENTRY_SPECIAL
))
846 if (!CheckQuickAbortIsReasonable(entry
))
853 store_client::dumpStats(MemBuf
* output
, int clientNumber
) const
855 if (_callback
.pending())
858 output
->appendf("\tClient #%d, %p\n", clientNumber
, _callback
.callback_data
);
859 output
->appendf("\t\tcopy_offset: %" PRId64
"\n", copyInto
.offset
);
860 output
->appendf("\t\tcopy_size: %d\n", copyInto
.length
);
861 output
->append("\t\tflags:", 8);
863 if (flags
.disk_io_pending
)
864 output
->append(" disk_io_pending", 16);
866 if (flags
.store_copying
)
867 output
->append(" store_copying", 14);
869 if (flags
.copy_event_pending
)
870 output
->append(" copy_event_pending", 19);
872 output
->append("\n",1);
876 store_client::Callback::pending() const
878 return callback_handler
&& callback_data
;
881 store_client::Callback::Callback(STCB
*function
, void *data
) : callback_handler(function
), callback_data (data
) {}
885 store_client::setDelayId(DelayId delay_id
)