]>
Commit | Line | Data |
---|---|---|
9cef6668 | 1 | |
2 | /* | |
262a0e14 | 3 | * $Id$ |
9cef6668 | 4 | * |
2f44bd34 | 5 | * DEBUG: section 90 Storage Manager Client-Side Interface |
9cef6668 | 6 | * AUTHOR: Duane Wessels |
7 | * | |
2b6662ba | 8 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
9cef6668 | 9 | * ---------------------------------------------------------- |
10 | * | |
2b6662ba | 11 | * Squid is the result of efforts by numerous individuals from |
12 | * the Internet community; see the CONTRIBUTORS file for full | |
13 | * details. Many organizations have provided support for Squid's | |
14 | * development; see the SPONSORS file for full details. Squid is | |
15 | * Copyrighted (C) 2001 by the Regents of the University of | |
16 | * California; see the COPYRIGHT file for full details. Squid | |
17 | * incorporates software developed and/or copyrighted by other | |
18 | * sources; see the CREDITS file for full details. | |
9cef6668 | 19 | * |
20 | * This program is free software; you can redistribute it and/or modify | |
21 | * it under the terms of the GNU General Public License as published by | |
22 | * the Free Software Foundation; either version 2 of the License, or | |
23 | * (at your option) any later version. | |
26ac0430 | 24 | * |
9cef6668 | 25 | * This program is distributed in the hope that it will be useful, |
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
28 | * GNU General Public License for more details. | |
26ac0430 | 29 | * |
9cef6668 | 30 | * You should have received a copy of the GNU General Public License |
31 | * along with this program; if not, write to the Free Software | |
32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
33 | * | |
2f44bd34 | 34 | * Portions copyright (c) 2003 Robert Collins <robertc@squid-cache.org> |
9cef6668 | 35 | */ |
36 | ||
f09f5b26 | 37 | #include "squid.h" |
a553a5a3 | 38 | #include "event.h" |
c8be6d7b | 39 | #include "StoreClient.h" |
e6ccf245 | 40 | #include "Store.h" |
528b2c61 | 41 | #include "HttpReply.h" |
42 | #include "MemObject.h" | |
43 | #include "StoreMeta.h" | |
44 | #include "StoreMetaUnpacker.h" | |
b67e2c8c | 45 | #if DELAY_POOLS |
46 | #include "DelayPools.h" | |
47 | #endif | |
a2ac85d9 | 48 | #include "HttpRequest.h" |
0eb49b6d | 49 | #include "MemBuf.h" |
c8be6d7b | 50 | |
e3ef2b09 | 51 | /* |
52 | * NOTE: 'Header' refers to the swapfile metadata header. | |
528b2c61 | 53 | * 'OBJHeader' refers to the object header, with cannonical |
54 | * processed object headers (which may derive from FTP/HTTP etc | |
55 | * upstream protocols | |
e3ef2b09 | 56 | * 'Body' refers to the swapfile body, which is the full |
57 | * HTTP reply (including HTTP headers and body). | |
58 | */ | |
4fcc8876 | 59 | static StoreIOState::STRCB storeClientReadBody; |
60 | static StoreIOState::STRCB storeClientReadHeader; | |
f09f5b26 | 61 | static void storeClientCopy2(StoreEntry * e, store_client * sc); |
d6f51e3c | 62 | static EVH storeClientCopyEvent; |
77b32a34 | 63 | static int CheckQuickAbort2(StoreEntry * entry); |
64 | static void CheckQuickAbort(StoreEntry * entry); | |
f09f5b26 | 65 | |
b001e822 | 66 | CBDATA_CLASS_INIT(store_client); |
528b2c61 | 67 | |
68 | void * | |
b001e822 | 69 | store_client::operator new (size_t) |
528b2c61 | 70 | { |
528b2c61 | 71 | CBDATA_INIT_TYPE(store_client); |
b001e822 | 72 | store_client *result = cbdataAlloc(store_client); |
73 | return result; | |
528b2c61 | 74 | } |
75 | ||
76 | void | |
77 | store_client::operator delete (void *address) | |
78 | { | |
b001e822 | 79 | store_client *t = static_cast<store_client *>(address); |
80 | cbdataFree(t); | |
528b2c61 | 81 | } |
82 | ||
83 | bool | |
47f6e231 | 84 | store_client::memReaderHasLowerOffset(int64_t anOffset) const |
528b2c61 | 85 | { |
86 | return getType() == STORE_MEM_CLIENT && copyInto.offset < anOffset; | |
87 | } | |
88 | ||
89 | int | |
90 | store_client::getType() const | |
91 | { | |
92 | return type; | |
93 | } | |
94 | ||
06d2839d | 95 | #if STORE_CLIENT_LIST_DEBUG |
fa80a8ef | 96 | static store_client * |
f09f5b26 | 97 | storeClientListSearch(const MemObject * mem, void *data) |
98 | { | |
06d2839d | 99 | dlink_node *node; |
100 | store_client *sc = NULL; | |
62e76326 | 101 | |
06d2839d | 102 | for (node = mem->clients.head; node; node = node->next) { |
62e76326 | 103 | sc = node->data; |
104 | ||
105 | if (sc->owner == data) | |
106 | return sc; | |
f09f5b26 | 107 | } |
62e76326 | 108 | |
06d2839d | 109 | return NULL; |
f09f5b26 | 110 | } |
c8be6d7b | 111 | |
112 | int | |
113 | storeClientIsThisAClient(store_client * sc, void *someClient) | |
114 | { | |
115 | return sc->owner == someClient; | |
116 | } | |
62e76326 | 117 | |
06d2839d | 118 | #endif |
924f73bc | 119 | #include "HttpRequest.h" |
f09f5b26 | 120 | |
121 | /* add client with fd to client list */ | |
06d2839d | 122 | store_client * |
f09f5b26 | 123 | storeClientListAdd(StoreEntry * e, void *data) |
124 | { | |
125 | MemObject *mem = e->mem_obj; | |
f09f5b26 | 126 | store_client *sc; |
127 | assert(mem); | |
06d2839d | 128 | #if STORE_CLIENT_LIST_DEBUG |
62e76326 | 129 | |
f09f5b26 | 130 | if (storeClientListSearch(mem, data) != NULL) |
62e76326 | 131 | /* XXX die! */ |
132 | assert(1 == 0); | |
133 | ||
6b8e7481 | 134 | #endif |
62e76326 | 135 | |
528b2c61 | 136 | sc = new store_client (e); |
62e76326 | 137 | |
528b2c61 | 138 | mem->addClient(sc); |
62e76326 | 139 | |
06d2839d | 140 | return sc; |
f09f5b26 | 141 | } |
142 | ||
528b2c61 | 143 | void |
144 | store_client::callback(ssize_t sz, bool error) | |
b04e66e0 | 145 | { |
967a359a | 146 | StoreIOBuffer result(sz, 0 ,copyInto.data); |
62e76326 | 147 | |
964ae882 | 148 | if (sz < 0) { |
62e76326 | 149 | result.flags.error = 1; |
964ae882 AJ |
150 | result.length = 0; |
151 | } else { | |
152 | result.flags.error = error ? 1 : 0; | |
c8be6d7b | 153 | } |
62e76326 | 154 | |
528b2c61 | 155 | result.offset = cmp_offset; |
90703668 | 156 | assert(_callback.pending()); |
528b2c61 | 157 | cmp_offset = copyInto.offset + sz; |
158 | STCB *temphandler = _callback.callback_handler; | |
159 | void *cbdata = _callback.callback_data; | |
160 | _callback = Callback(NULL, NULL); | |
161 | copyInto.data = NULL; | |
62e76326 | 162 | |
528b2c61 | 163 | if (cbdataReferenceValid(cbdata)) |
62e76326 | 164 | temphandler(cbdata, result); |
165 | ||
528b2c61 | 166 | cbdataReferenceDone(cbdata); |
b04e66e0 | 167 | } |
168 | ||
f115fadd | 169 | static void |
170 | storeClientCopyEvent(void *data) | |
171 | { | |
e6ccf245 | 172 | store_client *sc = (store_client *)data; |
bf8fe701 | 173 | debugs(90, 3, "storeClientCopyEvent: Running"); |
528b2c61 | 174 | assert (sc->flags.copy_event_pending); |
67fd69de | 175 | sc->flags.copy_event_pending = 0; |
62e76326 | 176 | |
90703668 | 177 | if (!sc->_callback.pending()) |
62e76326 | 178 | return; |
179 | ||
f115fadd | 180 | storeClientCopy2(sc->entry, sc); |
f115fadd | 181 | } |
182 | ||
b67e2c8c | 183 | store_client::store_client(StoreEntry *e) : entry (e) |
184 | #if DELAY_POOLS | |
62e76326 | 185 | , delayId() |
b67e2c8c | 186 | #endif |
62e76326 | 187 | , type (e->storeClientType()) |
188 | , object_ok(true) | |
528b2c61 | 189 | { |
190 | cmp_offset = 0; | |
191 | flags.disk_io_pending = 0; | |
192 | entry->refcount++; | |
62e76326 | 193 | |
528b2c61 | 194 | if (getType() == STORE_DISK_CLIENT) |
62e76326 | 195 | /* assert we'll be able to get the data we want */ |
02a2d80b | 196 | /* maybe we should open swapin_sio here */ |
c07cbbf4 | 197 | assert(entry->swap_filen > -1 || entry->swapOutAble()); |
62e76326 | 198 | |
528b2c61 | 199 | #if STORE_CLIENT_LIST_DEBUG |
62e76326 | 200 | |
528b2c61 | 201 | owner = cbdataReference(data); |
62e76326 | 202 | |
528b2c61 | 203 | #endif |
204 | } | |
205 | ||
2f44bd34 | 206 | store_client::~store_client() |
62e76326 | 207 | {} |
2f44bd34 | 208 | |
f09f5b26 | 209 | /* copy bytes requested by the client */ |
210 | void | |
a4b8110e | 211 | storeClientCopy(store_client * sc, |
62e76326 | 212 | StoreEntry * e, |
213 | StoreIOBuffer copyInto, | |
214 | STCB * callback, | |
215 | void *data) | |
f09f5b26 | 216 | { |
528b2c61 | 217 | assert (sc != NULL); |
218 | sc->copy(e, copyInto,callback,data); | |
219 | } | |
220 | ||
221 | void | |
222 | store_client::copy(StoreEntry * anEntry, | |
62e76326 | 223 | StoreIOBuffer copyRequest, |
224 | STCB * callback_fn, | |
225 | void *data) | |
528b2c61 | 226 | { |
227 | assert (anEntry == entry); | |
228 | assert (callback_fn); | |
229 | assert (data); | |
230 | assert(!EBIT_TEST(entry->flags, ENTRY_ABORTED)); | |
bf8fe701 | 231 | debugs(90, 3, "store_client::copy: " << entry->getMD5Text() << ", from " << |
47f6e231 | 232 | copyRequest.offset << ", for length " << |
bf8fe701 | 233 | (int) copyRequest.length << ", cb " << callback_fn << ", cbdata " << |
234 | data); | |
235 | ||
06d2839d | 236 | #if STORE_CLIENT_LIST_DEBUG |
62e76326 | 237 | |
528b2c61 | 238 | assert(this == storeClientListSearch(entry->mem_obj, data)); |
06d2839d | 239 | #endif |
62e76326 | 240 | |
90703668 | 241 | assert(!_callback.pending()); |
528b2c61 | 242 | #if ONLYCONTIGUOUSREQUESTS |
62e76326 | 243 | |
528b2c61 | 244 | assert(cmp_offset == copyRequest.offset); |
245 | #endif | |
246 | /* range requests will skip into the body */ | |
247 | cmp_offset = copyRequest.offset; | |
248 | _callback = Callback (callback_fn, cbdataReference(data)); | |
249 | copyInto.data = copyRequest.data; | |
250 | copyInto.length = copyRequest.length; | |
251 | copyInto.offset = copyRequest.offset; | |
252 | ||
1d5161bd | 253 | static bool copying (false); |
254 | assert (!copying); | |
255 | copying = true; | |
256 | PROF_start(storeClient_kickReads); | |
a46d2c0e | 257 | /* we might be blocking comm reads due to readahead limits |
258 | * now we have a new offset, trigger those reads... | |
259 | */ | |
260 | entry->mem_obj->kickReads(); | |
1d5161bd | 261 | PROF_stop(storeClient_kickReads); |
262 | copying = false; | |
a46d2c0e | 263 | |
528b2c61 | 264 | storeClientCopy2(entry, this); |
f09f5b26 | 265 | } |
266 | ||
07304bf9 | 267 | /* |
268 | * This function is used below to decide if we have any more data to | |
0bb129ee | 269 | * send to the client. If the store_status is STORE_PENDING, then we |
b7fe0ab0 | 270 | * do have more data to send. If its STORE_OK, then |
0bb129ee | 271 | * we continue checking. If the object length is negative, then we |
272 | * don't know the real length and must open the swap file to find out. | |
273 | * If the length is >= 0, then we compare it to the requested copy | |
274 | * offset. | |
07304bf9 | 275 | */ |
276 | static int | |
277 | storeClientNoMoreToSend(StoreEntry * e, store_client * sc) | |
278 | { | |
47f6e231 | 279 | int64_t len; |
62e76326 | 280 | |
0bb129ee | 281 | if (e->store_status == STORE_PENDING) |
62e76326 | 282 | return 0; |
283 | ||
707fdc47 | 284 | if ((len = e->objectLen()) < 0) |
62e76326 | 285 | return 0; |
286 | ||
c8be6d7b | 287 | if (sc->copyInto.offset < len) |
62e76326 | 288 | return 0; |
289 | ||
07304bf9 | 290 | return 1; |
291 | } | |
292 | ||
f09f5b26 | 293 | static void |
294 | storeClientCopy2(StoreEntry * e, store_client * sc) | |
295 | { | |
528b2c61 | 296 | /* reentrancy not allowed - note this could lead to |
297 | * dropped events | |
298 | */ | |
62e76326 | 299 | |
fa80a8ef | 300 | if (sc->flags.copy_event_pending) { |
62e76326 | 301 | return; |
fa80a8ef | 302 | } |
62e76326 | 303 | |
db1cd23c | 304 | if (EBIT_TEST(e->flags, ENTRY_FWD_HDR_WAIT)) { |
bf8fe701 | 305 | debugs(90, 5, "storeClientCopy2: returning because ENTRY_FWD_HDR_WAIT set"); |
62e76326 | 306 | return; |
db1cd23c | 307 | } |
62e76326 | 308 | |
67fd69de | 309 | if (sc->flags.store_copying) { |
62e76326 | 310 | sc->flags.copy_event_pending = 1; |
bf8fe701 | 311 | debugs(90, 3, "storeClientCopy2: Queueing storeClientCopyEvent()"); |
62e76326 | 312 | eventAdd("storeClientCopyEvent", storeClientCopyEvent, sc, 0.0, 0); |
313 | return; | |
67fd69de | 314 | } |
62e76326 | 315 | |
bf8fe701 | 316 | debugs(90, 3, "storeClientCopy2: " << e->getMD5Text()); |
90703668 | 317 | assert(sc->_callback.pending()); |
0bb129ee | 318 | /* |
b7fe0ab0 | 319 | * We used to check for ENTRY_ABORTED here. But there were some |
0bb129ee | 320 | * problems. For example, we might have a slow client (or two) and |
321 | * the server-side is reading far ahead and swapping to disk. Even | |
322 | * if the server-side aborts, we want to give the client(s) | |
323 | * everything we got before the abort condition occurred. | |
324 | */ | |
528b2c61 | 325 | /* Warning: doCopy may indirectly free itself in callbacks, |
7e6b941f | 326 | * hence the lock to keep it active for the duration of |
fa80a8ef | 327 | * this function |
328 | */ | |
7e6b941f | 329 | cbdataInternalLock(sc); |
528b2c61 | 330 | assert (sc->flags.store_copying == 0); |
331 | sc->doCopy(e); | |
332 | assert (sc->flags.store_copying == 0); | |
7e6b941f | 333 | cbdataInternalUnlock(sc); |
cfac48c2 | 334 | } |
335 | ||
528b2c61 | 336 | void |
337 | store_client::doCopy(StoreEntry *anEntry) | |
cfac48c2 | 338 | { |
528b2c61 | 339 | assert (anEntry == entry); |
340 | flags.store_copying = 1; | |
341 | MemObject *mem = entry->mem_obj; | |
cd748f27 | 342 | |
bf8fe701 | 343 | debugs(33, 5, "store_client::doCopy: co: " << |
47f6e231 | 344 | copyInto.offset << ", hi: " << |
345 | mem->endOffset()); | |
add2192d | 346 | |
528b2c61 | 347 | if (storeClientNoMoreToSend(entry, this)) { |
62e76326 | 348 | /* There is no more to send! */ |
26ac0430 | 349 | debugs(33, 3, HERE << "There is no more to send!"); |
62e76326 | 350 | callback(0); |
351 | flags.store_copying = 0; | |
352 | return; | |
cfac48c2 | 353 | } |
62e76326 | 354 | |
add2192d | 355 | /* Check that we actually have data */ |
528b2c61 | 356 | if (anEntry->store_status == STORE_PENDING && copyInto.offset >= mem->endOffset()) { |
bf8fe701 | 357 | debugs(90, 3, "store_client::doCopy: Waiting for more"); |
62e76326 | 358 | flags.store_copying = 0; |
359 | return; | |
cfac48c2 | 360 | } |
62e76326 | 361 | |
cfac48c2 | 362 | /* |
363 | * Slight weirdness here. We open a swapin file for any | |
364 | * STORE_DISK_CLIENT, even if we can copy the requested chunk | |
365 | * from memory in the next block. We must try to open the | |
366 | * swapin file before sending any data to the client side. If | |
367 | * we postpone the open, and then can not open the file later | |
368 | * on, the client loses big time. Its transfer just gets cut | |
369 | * off. Better to open it early (while the client side handler | |
370 | * is clientCacheHit) so that we can fall back to a cache miss | |
371 | * if needed. | |
372 | */ | |
fa80a8ef | 373 | |
85a4b153 | 374 | if (STORE_DISK_CLIENT == getType() && swapin_sio == NULL) |
4e70dae3 | 375 | startSwapin(); |
376 | else | |
377 | scheduleRead(); | |
378 | } | |
379 | ||
380 | void | |
381 | store_client::startSwapin() | |
382 | { | |
bf8fe701 | 383 | debugs(90, 3, "store_client::doCopy: Need to open swap in file"); |
4e70dae3 | 384 | /* gotta open the swapin file */ |
385 | ||
386 | if (storeTooManyDiskFilesOpen()) { | |
387 | /* yuck -- this causes a TCP_SWAPFAIL_MISS on the client side */ | |
388 | fail(); | |
389 | flags.store_copying = 0; | |
390 | return; | |
391 | } else if (!flags.disk_io_pending) { | |
392 | /* Don't set store_io_pending here */ | |
393 | storeSwapInStart(this); | |
62e76326 | 394 | |
85a4b153 | 395 | if (swapin_sio == NULL) { |
62e76326 | 396 | fail(); |
397 | flags.store_copying = 0; | |
398 | return; | |
62e76326 | 399 | } |
62e76326 | 400 | |
4e70dae3 | 401 | /* |
402 | * If the open succeeds we either copy from memory, or | |
403 | * schedule a disk read in the next block. | |
404 | */ | |
405 | scheduleRead(); | |
406 | ||
407 | return; | |
408 | } else { | |
bf8fe701 | 409 | debugs(90, 1, "WARNING: Averted multiple fd operation (1)"); |
62e76326 | 410 | flags.store_copying = 0; |
411 | return; | |
cfac48c2 | 412 | } |
4e70dae3 | 413 | } |
62e76326 | 414 | |
4e70dae3 | 415 | void |
416 | store_client::scheduleRead() | |
417 | { | |
418 | MemObject *mem = entry->mem_obj; | |
419 | ||
420 | if (copyInto.offset >= mem->inmem_lo && copyInto.offset < mem->endOffset()) | |
421 | scheduleMemRead(); | |
422 | else | |
423 | scheduleDiskRead(); | |
424 | } | |
425 | ||
426 | void | |
427 | store_client::scheduleDiskRead() | |
428 | { | |
cd748f27 | 429 | /* What the client wants is not in memory. Schedule a disk read */ |
528b2c61 | 430 | assert(STORE_DISK_CLIENT == getType()); |
62e76326 | 431 | |
528b2c61 | 432 | assert(!flags.disk_io_pending); |
62e76326 | 433 | |
bf8fe701 | 434 | debugs(90, 3, "store_client::doCopy: reading from STORE"); |
62e76326 | 435 | |
528b2c61 | 436 | fileRead(); |
62e76326 | 437 | |
528b2c61 | 438 | flags.store_copying = 0; |
f09f5b26 | 439 | } |
440 | ||
4e70dae3 | 441 | void |
442 | store_client::scheduleMemRead() | |
443 | { | |
444 | /* What the client wants is in memory */ | |
445 | /* Old style */ | |
bf8fe701 | 446 | debugs(90, 3, "store_client::doCopy: Copying normal from memory"); |
90703668 | 447 | size_t sz = entry->mem_obj->data_hdr.copy(copyInto); |
4e70dae3 | 448 | callback(sz); |
449 | flags.store_copying = 0; | |
450 | } | |
451 | ||
528b2c61 | 452 | void |
453 | store_client::fileRead() | |
f09f5b26 | 454 | { |
528b2c61 | 455 | MemObject *mem = entry->mem_obj; |
456 | ||
90703668 | 457 | assert(_callback.pending()); |
528b2c61 | 458 | assert(!flags.disk_io_pending); |
459 | flags.disk_io_pending = 1; | |
62e76326 | 460 | |
528b2c61 | 461 | if (mem->swap_hdr_sz != 0) |
62e76326 | 462 | if (entry->swap_status == SWAPOUT_WRITING) |
47f6e231 | 463 | assert(mem->swapout.sio->offset() > copyInto.offset + (int64_t)mem->swap_hdr_sz); |
62e76326 | 464 | |
528b2c61 | 465 | storeRead(swapin_sio, |
62e76326 | 466 | copyInto.data, |
467 | copyInto.length, | |
468 | copyInto.offset + mem->swap_hdr_sz, | |
469 | mem->swap_hdr_sz == 0 ? storeClientReadHeader | |
470 | : storeClientReadBody, | |
471 | this); | |
f09f5b26 | 472 | } |
473 | ||
474 | static void | |
e5de8b13 | 475 | storeClientReadBody(void *data, const char *buf, ssize_t len, StoreIOState::Pointer self) |
f09f5b26 | 476 | { |
e6ccf245 | 477 | store_client *sc = (store_client *)data; |
27002b34 | 478 | assert(sc->flags.disk_io_pending); |
f115fadd | 479 | sc->flags.disk_io_pending = 0; |
90703668 | 480 | assert(sc->_callback.pending()); |
4a7a3d56 | 481 | debugs(90, 3, "storeClientReadBody: len " << len << ""); |
62e76326 | 482 | |
06a5ae20 | 483 | if (sc->copyInto.offset == 0 && len > 0 && sc->entry->getReply()->sline.status == 0) { |
62e76326 | 484 | /* Our structure ! */ |
06a5ae20 | 485 | HttpReply *rep = (HttpReply *) sc->entry->getReply(); // bypass const |
486 | ||
59eed7dc | 487 | if (!rep->parseCharBuf(sc->copyInto.data, headersEnd(sc->copyInto.data, len))) { |
bf8fe701 | 488 | debugs(90, 0, "Could not parse headers from on disk object"); |
62e76326 | 489 | } |
06a5ae20 | 490 | } |
62e76326 | 491 | |
528b2c61 | 492 | sc->callback(len); |
493 | } | |
494 | ||
495 | void | |
62e76326 | 496 | store_client::fail() |
528b2c61 | 497 | { |
498 | object_ok = false; | |
db2ff94c | 499 | /* synchronous open failures callback from the store, |
500 | * before startSwapin detects the failure. | |
501 | * TODO: fix this inconsistent behaviour - probably by | |
26ac0430 | 502 | * having storeSwapInStart become a callback functions, |
db2ff94c | 503 | * not synchronous |
504 | */ | |
505 | ||
90703668 | 506 | if (_callback.pending()) |
db2ff94c | 507 | callback(0, true); |
f09f5b26 | 508 | } |
509 | ||
e3ef2b09 | 510 | static void |
e5de8b13 | 511 | storeClientReadHeader(void *data, const char *buf, ssize_t len, StoreIOState::Pointer self) |
e3ef2b09 | 512 | { |
e6ccf245 | 513 | store_client *sc = (store_client *)data; |
528b2c61 | 514 | sc->readHeader(buf, len); |
515 | } | |
516 | ||
517 | void | |
518 | store_client::unpackHeader(char const *buf, ssize_t len) | |
519 | { | |
4a7a3d56 | 520 | debugs(90, 3, "store_client::unpackHeader: len " << len << ""); |
62e76326 | 521 | |
e3ef2b09 | 522 | if (len < 0) { |
bf8fe701 | 523 | debugs(90, 3, "store_client::unpackHeader: " << xstrerror() << ""); |
62e76326 | 524 | fail(); |
525 | return; | |
e3ef2b09 | 526 | } |
62e76326 | 527 | |
528b2c61 | 528 | int swap_hdr_sz = 0; |
529 | StoreMetaUnpacker aBuilder(buf, len, &swap_hdr_sz); | |
62e76326 | 530 | |
528b2c61 | 531 | if (!aBuilder.isBufferSane()) { |
62e76326 | 532 | /* oops, bad disk file? */ |
bf8fe701 | 533 | debugs(90, 1, "WARNING: swapfile header inconsistent with available data"); |
62e76326 | 534 | fail(); |
535 | return; | |
9bc73deb | 536 | } |
62e76326 | 537 | |
528b2c61 | 538 | tlv *tlv_list = aBuilder.createStoreMeta (); |
62e76326 | 539 | |
e3ef2b09 | 540 | if (tlv_list == NULL) { |
bf8fe701 | 541 | debugs(90, 1, "WARNING: failed to unpack meta data"); |
62e76326 | 542 | fail(); |
543 | return; | |
e3ef2b09 | 544 | } |
62e76326 | 545 | |
e3ef2b09 | 546 | /* |
7e3ce7b9 | 547 | * Check the meta data and make sure we got the right object. |
e3ef2b09 | 548 | */ |
528b2c61 | 549 | for (tlv *t = tlv_list; t; t = t->next) { |
62e76326 | 550 | if (!t->checkConsistency(entry)) { |
551 | storeSwapTLVFree(tlv_list); | |
552 | fail(); | |
553 | return; | |
554 | } | |
7e3ce7b9 | 555 | } |
62e76326 | 556 | |
07304bf9 | 557 | storeSwapTLVFree(tlv_list); |
528b2c61 | 558 | |
559 | entry->mem_obj->swap_hdr_sz = swap_hdr_sz; | |
560 | entry->mem_obj->object_sz = entry->swap_file_sz - swap_hdr_sz; | |
561 | ||
562 | } | |
563 | ||
564 | void | |
565 | store_client::readHeader(char const *buf, ssize_t len) | |
566 | { | |
567 | MemObject *const mem = entry->mem_obj; | |
62e76326 | 568 | |
528b2c61 | 569 | assert(flags.disk_io_pending); |
570 | flags.disk_io_pending = 0; | |
90703668 | 571 | assert(_callback.pending()); |
528b2c61 | 572 | |
573 | unpackHeader (buf, len); | |
62e76326 | 574 | |
528b2c61 | 575 | if (!object_ok) |
62e76326 | 576 | return; |
577 | ||
e3ef2b09 | 578 | /* |
579 | * If our last read got some data the client wants, then give | |
580 | * it to them, otherwise schedule another read. | |
581 | */ | |
528b2c61 | 582 | size_t body_sz = len - mem->swap_hdr_sz; |
62e76326 | 583 | |
47f6e231 | 584 | if (copyInto.offset < static_cast<int64_t>(body_sz)) { |
62e76326 | 585 | /* |
586 | * we have (part of) what they want | |
587 | */ | |
d85c3078 | 588 | size_t copy_sz = min(copyInto.length, body_sz); |
4a7a3d56 | 589 | debugs(90, 3, "storeClientReadHeader: copying " << copy_sz << " bytes of body"); |
62e76326 | 590 | xmemmove(copyInto.data, copyInto.data + mem->swap_hdr_sz, copy_sz); |
591 | ||
06a5ae20 | 592 | if (copyInto.offset == 0 && len > 0 && entry->getReply()->sline.status == 0) { |
62e76326 | 593 | /* Our structure ! */ |
06a5ae20 | 594 | HttpReply *rep = (HttpReply *) entry->getReply(); // bypass const |
595 | ||
59eed7dc | 596 | if (!rep->parseCharBuf(copyInto.data, headersEnd(copyInto.data, copy_sz))) { |
bf8fe701 | 597 | debugs(90, 0, "could not parse headers from on disk structure!"); |
62e76326 | 598 | } |
06a5ae20 | 599 | } |
528b2c61 | 600 | |
601 | callback(copy_sz); | |
62e76326 | 602 | return; |
e3ef2b09 | 603 | } |
62e76326 | 604 | |
e3ef2b09 | 605 | /* |
606 | * we don't have what the client wants, but at least we now | |
607 | * know the swap header size. | |
608 | */ | |
528b2c61 | 609 | fileRead(); |
e3ef2b09 | 610 | } |
611 | ||
f09f5b26 | 612 | int |
a4b8110e | 613 | storeClientCopyPending(store_client * sc, StoreEntry * e, void *data) |
f09f5b26 | 614 | { |
06d2839d | 615 | #if STORE_CLIENT_LIST_DEBUG |
616 | assert(sc == storeClientListSearch(e->mem_obj, data)); | |
edce4d98 | 617 | #endif |
618 | #ifndef SILLY_CODE | |
62e76326 | 619 | |
edce4d98 | 620 | assert(sc); |
06d2839d | 621 | #endif |
62e76326 | 622 | |
06d2839d | 623 | assert(sc->entry == e); |
edce4d98 | 624 | #if SILLY_CODE |
62e76326 | 625 | |
f09f5b26 | 626 | if (sc == NULL) |
62e76326 | 627 | return 0; |
628 | ||
edce4d98 | 629 | #endif |
62e76326 | 630 | |
90703668 | 631 | if (!sc->_callback.pending()) |
62e76326 | 632 | return 0; |
633 | ||
f09f5b26 | 634 | return 1; |
635 | } | |
636 | ||
06d2839d | 637 | /* |
638 | * This routine hasn't been optimised to take advantage of the | |
639 | * passed sc. Yet. | |
640 | */ | |
f09f5b26 | 641 | int |
a4b8110e | 642 | storeUnregister(store_client * sc, StoreEntry * e, void *data) |
f09f5b26 | 643 | { |
644 | MemObject *mem = e->mem_obj; | |
06d2839d | 645 | #if STORE_CLIENT_LIST_DEBUG |
62e76326 | 646 | |
06d2839d | 647 | assert(sc == storeClientListSearch(e->mem_obj, data)); |
648 | #endif | |
62e76326 | 649 | |
f09f5b26 | 650 | if (mem == NULL) |
62e76326 | 651 | return 0; |
652 | ||
26ac0430 | 653 | debugs(90, 3, "storeUnregister: called for '" << e->getMD5Text() << "'"); |
62e76326 | 654 | |
2f44bd34 | 655 | if (sc == NULL) { |
bf8fe701 | 656 | debugs(90, 3, "storeUnregister: No matching client for '" << e->getMD5Text() << "'"); |
62e76326 | 657 | return 0; |
2f44bd34 | 658 | } |
62e76326 | 659 | |
0e3f3e0d | 660 | if (mem->clientCount() == 0) { |
bf8fe701 | 661 | debugs(90, 3, "storeUnregister: Consistency failure - store client being unregistered is not in the mem object's list for '" << e->getMD5Text() << "'"); |
62e76326 | 662 | return 0; |
2f44bd34 | 663 | } |
62e76326 | 664 | |
06d2839d | 665 | dlinkDelete(&sc->node, &mem->clients); |
f09f5b26 | 666 | mem->nclients--; |
62e76326 | 667 | |
f09f5b26 | 668 | if (e->store_status == STORE_OK && e->swap_status != SWAPOUT_DONE) |
c07cbbf4 | 669 | e->swapOut(); |
62e76326 | 670 | |
85a4b153 | 671 | if (sc->swapin_sio != NULL) { |
62e76326 | 672 | storeClose(sc->swapin_sio); |
673 | sc->swapin_sio = NULL; | |
674 | statCounter.swap.ins++; | |
eb824054 | 675 | } |
62e76326 | 676 | |
90703668 | 677 | if (sc->_callback.pending()) { |
62e76326 | 678 | /* callback with ssize = -1 to indicate unexpected termination */ |
bf8fe701 | 679 | debugs(90, 3, "storeUnregister: store_client for " << mem->url << " has a callback"); |
62e76326 | 680 | sc->fail(); |
f09f5b26 | 681 | } |
62e76326 | 682 | |
fa80a8ef | 683 | #if STORE_CLIENT_LIST_DEBUG |
684 | cbdataReferenceDone(sc->owner); | |
62e76326 | 685 | |
fa80a8ef | 686 | #endif |
62e76326 | 687 | |
528b2c61 | 688 | delete sc; |
62e76326 | 689 | |
77b32a34 | 690 | assert(e->lock_count > 0); |
62e76326 | 691 | |
77b32a34 | 692 | if (mem->nclients == 0) |
62e76326 | 693 | CheckQuickAbort(e); |
a46d2c0e | 694 | else |
695 | mem->kickReads(); | |
62e76326 | 696 | |
f09f5b26 | 697 | return 1; |
698 | } | |
699 | ||
f09f5b26 | 700 | /* Call handlers waiting for data to be appended to E. */ |
701 | void | |
d88e3c49 | 702 | StoreEntry::invokeHandlers() |
f09f5b26 | 703 | { |
528b2c61 | 704 | /* Commit what we can to disk, if appropriate */ |
d88e3c49 | 705 | swapOut(); |
f09f5b26 | 706 | int i = 0; |
f09f5b26 | 707 | store_client *sc; |
06d2839d | 708 | dlink_node *nx = NULL; |
709 | dlink_node *node; | |
710 | ||
0bc8db64 | 711 | PROF_start(InvokeHandlers); |
9ea37c79 | 712 | |
bf8fe701 | 713 | debugs(90, 3, "InvokeHandlers: " << getMD5Text() ); |
f09f5b26 | 714 | /* walk the entire list looking for valid callbacks */ |
62e76326 | 715 | |
d88e3c49 | 716 | for (node = mem_obj->clients.head; node; node = nx) { |
62e76326 | 717 | sc = (store_client *)node->data; |
718 | nx = node->next; | |
bf8fe701 | 719 | debugs(90, 3, "StoreEntry::InvokeHandlers: checking client #" << i++ ); |
62e76326 | 720 | |
90703668 | 721 | if (!sc->_callback.pending()) |
62e76326 | 722 | continue; |
723 | ||
724 | if (sc->flags.disk_io_pending) | |
725 | continue; | |
726 | ||
d88e3c49 | 727 | storeClientCopy2(this, sc); |
f09f5b26 | 728 | } |
0bc8db64 | 729 | PROF_stop(InvokeHandlers); |
f09f5b26 | 730 | } |
731 | ||
732 | int | |
733 | storePendingNClients(const StoreEntry * e) | |
734 | { | |
f09f5b26 | 735 | MemObject *mem = e->mem_obj; |
36547bcf | 736 | int npend = NULL == mem ? 0 : mem->nclients; |
bf8fe701 | 737 | debugs(90, 3, "storePendingNClients: returning " << npend); |
f09f5b26 | 738 | return npend; |
739 | } | |
77b32a34 | 740 | |
741 | /* return 1 if the request should be aborted */ | |
742 | static int | |
743 | CheckQuickAbort2(StoreEntry * entry) | |
744 | { | |
528b2c61 | 745 | MemObject * const mem = entry->mem_obj; |
77b32a34 | 746 | assert(mem); |
bf8fe701 | 747 | debugs(90, 3, "CheckQuickAbort2: entry=" << entry << ", mem=" << mem); |
62e76326 | 748 | |
0e5bd28f | 749 | if (mem->request && !mem->request->flags.cachable) { |
bf8fe701 | 750 | debugs(90, 3, "CheckQuickAbort2: YES !mem->request->flags.cachable"); |
62e76326 | 751 | return 1; |
77b32a34 | 752 | } |
62e76326 | 753 | |
77b32a34 | 754 | if (EBIT_TEST(entry->flags, KEY_PRIVATE)) { |
bf8fe701 | 755 | debugs(90, 3, "CheckQuickAbort2: YES KEY_PRIVATE"); |
62e76326 | 756 | return 1; |
77b32a34 | 757 | } |
62e76326 | 758 | |
47f6e231 | 759 | int64_t expectlen = entry->getReply()->content_length + entry->getReply()->hdr_sz; |
0e3f3e0d | 760 | |
761 | if (expectlen < 0) | |
2324cda2 | 762 | /* expectlen is < 0 if *no* information about the object has been received */ |
0e3f3e0d | 763 | return 1; |
764 | ||
47f6e231 | 765 | int64_t curlen = mem->endOffset (); |
62e76326 | 766 | |
47f6e231 | 767 | if (Config.quickAbort.min < 0) { |
bf8fe701 | 768 | debugs(90, 3, "CheckQuickAbort2: NO disabled"); |
62e76326 | 769 | return 0; |
77b32a34 | 770 | } |
62e76326 | 771 | |
ab275c7b AJ |
772 | if ( Config.rangeOffsetLimit < 0 && mem->request && mem->request->range ) { |
773 | /* Don't abort if the admin has configured range_ofset -1 to download fully for caching. */ | |
774 | debugs(90, 3, "CheckQuickAbort2: NO admin configured range replies to full-download"); | |
775 | return 0; | |
776 | } | |
777 | ||
77b32a34 | 778 | if (curlen > expectlen) { |
bf8fe701 | 779 | debugs(90, 3, "CheckQuickAbort2: YES bad content length"); |
62e76326 | 780 | return 1; |
77b32a34 | 781 | } |
62e76326 | 782 | |
47f6e231 | 783 | if ((expectlen - curlen) < (Config.quickAbort.min << 10)) { |
bf8fe701 | 784 | debugs(90, 3, "CheckQuickAbort2: NO only little more left"); |
62e76326 | 785 | return 0; |
77b32a34 | 786 | } |
62e76326 | 787 | |
77b32a34 | 788 | if ((expectlen - curlen) > (Config.quickAbort.max << 10)) { |
bf8fe701 | 789 | debugs(90, 3, "CheckQuickAbort2: YES too much left to go"); |
62e76326 | 790 | return 1; |
77b32a34 | 791 | } |
62e76326 | 792 | |
77b32a34 | 793 | if (expectlen < 100) { |
bf8fe701 | 794 | debugs(90, 3, "CheckQuickAbort2: NO avoid FPE"); |
62e76326 | 795 | return 0; |
77b32a34 | 796 | } |
62e76326 | 797 | |
47f6e231 | 798 | if ((curlen / (expectlen / 100)) > (Config.quickAbort.pct)) { |
bf8fe701 | 799 | debugs(90, 3, "CheckQuickAbort2: NO past point of no return"); |
62e76326 | 800 | return 0; |
77b32a34 | 801 | } |
62e76326 | 802 | |
bf8fe701 | 803 | debugs(90, 3, "CheckQuickAbort2: YES default, returning 1"); |
77b32a34 | 804 | return 1; |
805 | } | |
806 | ||
807 | static void | |
808 | CheckQuickAbort(StoreEntry * entry) | |
809 | { | |
528b2c61 | 810 | assert (entry); |
62e76326 | 811 | |
77b32a34 | 812 | if (storePendingNClients(entry) > 0) |
62e76326 | 813 | return; |
814 | ||
77b32a34 | 815 | if (entry->store_status != STORE_PENDING) |
62e76326 | 816 | return; |
817 | ||
986ebffc | 818 | if (EBIT_TEST(entry->flags, ENTRY_SPECIAL)) |
62e76326 | 819 | return; |
820 | ||
77b32a34 | 821 | if (CheckQuickAbort2(entry) == 0) |
62e76326 | 822 | return; |
823 | ||
bfb55b6f | 824 | entry->abort(); |
77b32a34 | 825 | } |
c8be6d7b | 826 | |
827 | void | |
fcc35180 | 828 | store_client::dumpStats(MemBuf * output, int clientNumber) const |
c8be6d7b | 829 | { |
90703668 | 830 | if (_callback.pending()) |
62e76326 | 831 | return; |
832 | ||
2fe7eff9 | 833 | output->Printf("\tClient #%d, %p\n", clientNumber, _callback.callback_data); |
62e76326 | 834 | |
47f6e231 | 835 | output->Printf("\t\tcopy_offset: %"PRId64"\n", |
836 | copyInto.offset); | |
62e76326 | 837 | |
2fe7eff9 | 838 | output->Printf("\t\tcopy_size: %d\n", |
839 | (int) copyInto.length); | |
62e76326 | 840 | |
2fe7eff9 | 841 | output->Printf("\t\tflags:"); |
62e76326 | 842 | |
528b2c61 | 843 | if (flags.disk_io_pending) |
2fe7eff9 | 844 | output->Printf(" disk_io_pending"); |
62e76326 | 845 | |
528b2c61 | 846 | if (flags.store_copying) |
2fe7eff9 | 847 | output->Printf(" store_copying"); |
62e76326 | 848 | |
528b2c61 | 849 | if (flags.copy_event_pending) |
2fe7eff9 | 850 | output->Printf(" copy_event_pending"); |
62e76326 | 851 | |
2fe7eff9 | 852 | output->Printf("\n"); |
528b2c61 | 853 | } |
c8be6d7b | 854 | |
528b2c61 | 855 | bool |
90703668 | 856 | store_client::Callback::pending() const |
528b2c61 | 857 | { |
90703668 | 858 | return callback_handler && callback_data; |
c8be6d7b | 859 | } |
528b2c61 | 860 | |
861 | store_client::Callback::Callback(STCB *function, void *data) : callback_handler(function), callback_data (data) {} | |
b67e2c8c | 862 | |
515ec4dc | 863 | #if DELAY_POOLS |
b67e2c8c | 864 | void |
865 | store_client::setDelayId(DelayId delay_id) | |
866 | { | |
867 | delayId = delay_id; | |
868 | } | |
62e76326 | 869 | |
515ec4dc | 870 | #endif |