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 72 Peer Digest Routines */
13 #include "CacheDigest.h"
14 #include "CachePeer.h"
18 #include "HttpReply.h"
19 #include "HttpRequest.h"
21 #include "MemObject.h"
22 #include "mime_header.h"
23 #include "neighbors.h"
24 #include "PeerDigest.h"
25 #include "SquidTime.h"
27 #include "store_key_md5.h"
28 #include "StoreClient.h"
34 /* local prototypes */
35 static time_t peerDigestIncDelay(const PeerDigest
* pd
);
36 static time_t peerDigestNewDelay(const StoreEntry
* e
);
37 static void peerDigestSetCheck(PeerDigest
* pd
, time_t delay
);
38 static void peerDigestClean(PeerDigest
*);
39 static EVH peerDigestCheck
;
40 static void peerDigestRequest(PeerDigest
* pd
);
41 static STCB peerDigestHandleReply
;
42 static int peerDigestFetchReply(void *, char *, ssize_t
);
43 int peerDigestSwapInHeaders(void *, char *, ssize_t
);
44 int peerDigestSwapInCBlock(void *, char *, ssize_t
);
45 int peerDigestSwapInMask(void *, char *, ssize_t
);
46 static int peerDigestFetchedEnough(DigestFetchState
* fetch
, char *buf
, ssize_t size
, const char *step_name
);
47 static void peerDigestFetchStop(DigestFetchState
* fetch
, char *buf
, const char *reason
);
48 static void peerDigestFetchAbort(DigestFetchState
* fetch
, char *buf
, const char *reason
);
49 static void peerDigestReqFinish(DigestFetchState
* fetch
, char *buf
, int, int, int, const char *reason
, int err
);
50 static void peerDigestPDFinish(DigestFetchState
* fetch
, int pcb_valid
, int err
);
51 static void peerDigestFetchFinish(DigestFetchState
* fetch
, int err
);
52 static void peerDigestFetchSetStats(DigestFetchState
* fetch
);
53 static int peerDigestSetCBlock(PeerDigest
* pd
, const char *buf
);
54 static int peerDigestUseful(const PeerDigest
* pd
);
57 Version
const CacheDigestVer
= { 5, 3 };
59 #define StoreDigestCBlockSize sizeof(StoreDigestCBlock)
61 /* min interval for requesting digests from a given peer */
62 static const time_t PeerDigestReqMinGap
= 5 * 60; /* seconds */
63 /* min interval for requesting digests (cumulative request stream) */
64 static const time_t GlobDigestReqMinGap
= 1 * 60; /* seconds */
68 static time_t pd_last_req_time
= 0; /* last call to Check */
70 /* initialize peer digest */
72 peerDigestInit(PeerDigest
* pd
, CachePeer
* p
)
76 memset(pd
, 0, sizeof(*pd
));
79 * Lock on to the peer here. The corresponding cbdataReferenceDone()
80 * is in peerDigestDestroy().
82 pd
->peer
= cbdataReference(p
);
83 /* if peer disappears, we will know it's name */
86 pd
->times
.initialized
= squid_curtime
;
90 peerDigestClean(PeerDigest
* pd
)
95 cacheDigestDestroy(pd
->cd
);
100 CBDATA_CLASS_INIT(PeerDigest
);
102 /* allocate new peer digest, call Init, and lock everything */
104 peerDigestCreate(CachePeer
* p
)
110 peerDigestInit(pd
, p
);
112 /* XXX This does not look right, and the same thing again in the caller */
113 return cbdataReference(pd
);
116 /* call Clean and free/unlock everything */
118 peerDigestDestroy(PeerDigest
* pd
)
122 void * peerTmp
= pd
->peer
;
126 * We locked the peer in peerDigestInit(), this is
127 * where we unlock it. If the peer is still valid,
128 * tell it that the digest is gone.
130 if (cbdataReferenceValidDone(peerTmp
, &p
))
131 peerNoteDigestGone((CachePeer
*)p
);
138 /* called by peer to indicate that somebody actually needs this digest */
140 peerDigestNeeded(PeerDigest
* pd
)
143 assert(!pd
->flags
.needed
);
146 pd
->flags
.needed
= true;
147 pd
->times
.needed
= squid_curtime
;
148 peerDigestSetCheck(pd
, 0); /* check asap */
151 /* currently we do not have a reason to disable without destroying */
153 /* disables peer for good */
155 peerDigestDisable(PeerDigest
* pd
)
157 debugs(72, 2, "peerDigestDisable: peer " << pd
->host
.buf() << " disabled for good");
158 pd
->times
.disabled
= squid_curtime
;
159 pd
->times
.next_check
= -1; /* never */
160 pd
->flags
.usable
= 0;
163 cacheDigestDestroy(pd
->cd
);
167 /* we do not destroy the pd itself to preserve its "history" and stats */
172 /* increment retry delay [after an unsuccessful attempt] */
174 peerDigestIncDelay(const PeerDigest
* pd
)
177 return pd
->times
.retry_delay
> 0 ?
178 2 * pd
->times
.retry_delay
: /* exponential backoff */
179 PeerDigestReqMinGap
; /* minimal delay */
182 /* artificially increases Expires: setting to avoid race conditions
183 * returns the delay till that [increased] expiration time */
185 peerDigestNewDelay(const StoreEntry
* e
)
190 return e
->expires
+ PeerDigestReqMinGap
- squid_curtime
;
192 return PeerDigestReqMinGap
;
195 /* registers next digest verification */
197 peerDigestSetCheck(PeerDigest
* pd
, time_t delay
)
199 eventAdd("peerDigestCheck", peerDigestCheck
, pd
, (double) delay
, 1);
200 pd
->times
.next_check
= squid_curtime
+ delay
;
201 debugs(72, 3, "peerDigestSetCheck: will check peer " << pd
->host
<< " in " << delay
<< " secs");
205 * called when peer is about to disappear or have already disappeared
208 peerDigestNotePeerGone(PeerDigest
* pd
)
210 if (pd
->flags
.requested
) {
211 debugs(72, 2, "peerDigest: peer " << pd
->host
<< " gone, will destroy after fetch.");
212 /* do nothing now, the fetching chain will notice and take action */
214 debugs(72, 2, "peerDigest: peer " << pd
->host
<< " is gone, destroying now.");
215 peerDigestDestroy(pd
);
219 /* callback for eventAdd() (with peer digest locked)
220 * request new digest if our copy is too old or if we lack one;
221 * schedule next check otherwise */
223 peerDigestCheck(void *data
)
225 PeerDigest
*pd
= (PeerDigest
*)data
;
228 assert(!pd
->flags
.requested
);
230 pd
->times
.next_check
= 0; /* unknown */
232 if (!cbdataReferenceValid(pd
->peer
)) {
233 peerDigestNotePeerGone(pd
);
237 debugs(72, 3, "peerDigestCheck: peer " << pd
->peer
->host
<< ":" << pd
->peer
->http_port
);
238 debugs(72, 3, "peerDigestCheck: time: " << squid_curtime
<<
239 ", last received: " << (long int) pd
->times
.received
<< " (" <<
240 std::showpos
<< (int) (squid_curtime
- pd
->times
.received
) << ")");
242 /* decide when we should send the request:
243 * request now unless too close to other requests */
244 req_time
= squid_curtime
;
248 if (req_time
- pd
->times
.received
< PeerDigestReqMinGap
) {
249 debugs(72, 2, "peerDigestCheck: " << pd
->host
<<
250 ", avoiding close peer requests (" <<
251 (int) (req_time
- pd
->times
.received
) << " < " <<
252 (int) PeerDigestReqMinGap
<< " secs).");
254 req_time
= pd
->times
.received
+ PeerDigestReqMinGap
;
258 if (req_time
- pd_last_req_time
< GlobDigestReqMinGap
) {
259 debugs(72, 2, "peerDigestCheck: " << pd
->host
<<
260 ", avoiding close requests (" <<
261 (int) (req_time
- pd_last_req_time
) << " < " <<
262 (int) GlobDigestReqMinGap
<< " secs).");
264 req_time
= pd_last_req_time
+ GlobDigestReqMinGap
;
267 if (req_time
<= squid_curtime
)
268 peerDigestRequest(pd
); /* will set pd->flags.requested */
270 peerDigestSetCheck(pd
, req_time
- squid_curtime
);
273 CBDATA_TYPE(DigestFetchState
);
275 /* ask store for a digest */
277 peerDigestRequest(PeerDigest
* pd
)
279 CachePeer
*p
= pd
->peer
;
280 StoreEntry
*e
, *old_e
;
282 const cache_key
*key
;
284 DigestFetchState
*fetch
= NULL
;
285 StoreIOBuffer tempBuffer
;
287 pd
->req_result
= NULL
;
288 pd
->flags
.requested
= true;
290 /* compute future request components */
293 url
= xstrdup(p
->digest_url
);
295 url
= xstrdup(internalRemoteUri(p
->host
, p
->http_port
, "/squid-internal-periodic/", StoreDigestFileName
));
297 req
= HttpRequest::CreateFromUrl(url
);
301 key
= storeKeyPublicByRequest(req
);
303 debugs(72, 2, "peerDigestRequest: " << url
<< " key: " << storeKeyText(key
));
305 /* add custom headers */
306 assert(!req
->header
.len
);
308 req
->header
.putStr(HDR_ACCEPT
, StoreDigestMimeStr
);
310 req
->header
.putStr(HDR_ACCEPT
, "text/html");
313 p
->login
[0] != '*' &&
314 strcmp(p
->login
, "PASS") != 0 &&
315 strcmp(p
->login
, "PASSTHRU") != 0 &&
316 strcmp(p
->login
, "NEGOTIATE") != 0 &&
317 strcmp(p
->login
, "PROXYPASS") != 0) {
318 req
->url
.userInfo(SBuf(p
->login
)); // XXX: performance regression make peer login SBuf as well.
320 /* create fetch state structure */
321 CBDATA_INIT_TYPE(DigestFetchState
);
323 fetch
= cbdataAlloc(DigestFetchState
);
325 fetch
->request
= req
;
326 HTTPMSGLOCK(fetch
->request
);
328 fetch
->pd
= cbdataReference(pd
);
332 fetch
->state
= DIGEST_READ_REPLY
;
334 /* update timestamps */
335 fetch
->start_time
= squid_curtime
;
337 pd
->times
.requested
= squid_curtime
;
339 pd_last_req_time
= squid_curtime
;
341 req
->flags
.cachable
= true;
343 /* the rest is based on clientProcessExpired() */
344 req
->flags
.refresh
= true;
346 old_e
= fetch
->old_entry
= Store::Root().get(key
);
349 debugs(72, 5, "peerDigestRequest: found old entry");
351 old_e
->lock("peerDigestRequest");
352 old_e
->createMemObject(url
, url
, req
->method
);
354 fetch
->old_sc
= storeClientListAdd(old_e
, fetch
);
357 e
= fetch
->entry
= storeCreateEntry(url
, url
, req
->flags
, req
->method
);
358 assert(EBIT_TEST(e
->flags
, KEY_PRIVATE
));
359 fetch
->sc
= storeClientListAdd(e
, fetch
);
360 /* set lastmod to trigger IMS request if possible */
363 e
->lastmod
= old_e
->lastmod
;
365 /* push towards peer cache */
366 debugs(72, 3, "peerDigestRequest: forwarding to fwdStart...");
368 FwdState::fwdStart(Comm::ConnectionPointer(), e
, req
);
370 tempBuffer
.offset
= 0;
372 tempBuffer
.length
= SM_PAGE_SIZE
;
374 tempBuffer
.data
= fetch
->buf
;
376 storeClientCopy(fetch
->sc
, e
, tempBuffer
,
377 peerDigestHandleReply
, fetch
);
382 /* Handle the data copying .. */
385 * This routine handles the copy data and then redirects the
386 * copy to a bunch of subfunctions depending upon the copy state.
387 * It also tracks the buffer offset and "seen", since I'm actually
388 * not interested in rewriting everything to suit my little idea.
391 peerDigestHandleReply(void *data
, StoreIOBuffer receivedData
)
393 DigestFetchState
*fetch
= (DigestFetchState
*)data
;
395 digest_read_state_t prevstate
;
398 assert(fetch
->pd
&& receivedData
.data
);
399 /* The existing code assumes that the received pointer is
400 * where we asked the data to be put
402 assert(fetch
->buf
+ fetch
->bufofs
== receivedData
.data
);
404 /* Update the buffer size */
405 fetch
->bufofs
+= receivedData
.length
;
407 assert(fetch
->bufofs
<= SM_PAGE_SIZE
);
409 /* If we've fetched enough, return */
411 if (peerDigestFetchedEnough(fetch
, fetch
->buf
, fetch
->bufofs
, "peerDigestHandleReply"))
414 /* Call the right function based on the state */
415 /* (Those functions will update the state if needed) */
417 /* Give us a temporary reference. Some of the calls we make may
418 * try to destroy the fetch structure, and we like to know if they
421 fetch
= cbdataReference(fetch
);
423 /* Repeat this loop until we're out of data OR the state changes */
424 /* (So keep going if the state has changed and we still have data */
426 prevstate
= fetch
->state
;
428 switch (fetch
->state
) {
430 case DIGEST_READ_REPLY
:
431 retsize
= peerDigestFetchReply(fetch
, fetch
->buf
, fetch
->bufofs
);
434 case DIGEST_READ_HEADERS
:
435 retsize
= peerDigestSwapInHeaders(fetch
, fetch
->buf
, fetch
->bufofs
);
438 case DIGEST_READ_CBLOCK
:
439 retsize
= peerDigestSwapInCBlock(fetch
, fetch
->buf
, fetch
->bufofs
);
442 case DIGEST_READ_MASK
:
443 retsize
= peerDigestSwapInMask(fetch
, fetch
->buf
, fetch
->bufofs
);
446 case DIGEST_READ_NONE
:
449 case DIGEST_READ_DONE
:
454 fatal("Bad digest transfer mode!\n");
461 * The returned size indicates how much of the buffer was read -
462 * so move the remainder of the buffer to the beginning
463 * and update the bufofs / bufsize
465 newsize
= fetch
->bufofs
- retsize
;
467 memmove(fetch
->buf
, fetch
->buf
+ retsize
, fetch
->bufofs
- newsize
);
469 fetch
->bufofs
= newsize
;
471 } while (cbdataReferenceValid(fetch
) && prevstate
!= fetch
->state
&& fetch
->bufofs
> 0);
473 /* Update the copy offset */
474 fetch
->offset
+= receivedData
.length
;
476 /* Schedule another copy */
477 if (cbdataReferenceValid(fetch
)) {
478 StoreIOBuffer tempBuffer
;
479 tempBuffer
.offset
= fetch
->offset
;
480 tempBuffer
.length
= SM_PAGE_SIZE
- fetch
->bufofs
;
481 tempBuffer
.data
= fetch
->buf
+ fetch
->bufofs
;
482 storeClientCopy(fetch
->sc
, fetch
->entry
, tempBuffer
,
483 peerDigestHandleReply
, fetch
);
487 /* Get rid of our reference, we've finished with it for now */
488 cbdataReferenceDone(fetch
);
491 /* wait for full http headers to be received then parse them */
493 * This routine handles parsing the reply line.
494 * If the reply line indicates an OK, the same data is thrown
495 * to SwapInHeaders(). If the reply line is a NOT_MODIFIED,
496 * we simply stop parsing.
499 peerDigestFetchReply(void *data
, char *buf
, ssize_t size
)
501 DigestFetchState
*fetch
= (DigestFetchState
*)data
;
502 PeerDigest
*pd
= fetch
->pd
;
505 assert(!fetch
->offset
);
507 assert(fetch
->state
== DIGEST_READ_REPLY
);
509 if (peerDigestFetchedEnough(fetch
, buf
, size
, "peerDigestFetchReply"))
512 if ((hdr_size
= headersEnd(buf
, size
))) {
513 HttpReply
const *reply
= fetch
->entry
->getReply();
515 assert(reply
->sline
.status() != Http::scNone
);
516 const Http::StatusCode status
= reply
->sline
.status();
517 debugs(72, 3, "peerDigestFetchReply: " << pd
->host
<< " status: " << status
<<
518 ", expires: " << (long int) reply
->expires
<< " (" << std::showpos
<<
519 (int) (reply
->expires
- squid_curtime
) << ")");
521 /* this "if" is based on clientHandleIMSReply() */
523 if (status
== Http::scNotModified
) {
524 /* our old entry is fine */
525 assert(fetch
->old_entry
);
527 if (!fetch
->old_entry
->mem_obj
->request
) {
528 fetch
->old_entry
->mem_obj
->request
= fetch
->entry
->mem_obj
->request
;
529 HTTPMSGLOCK(fetch
->old_entry
->mem_obj
->request
);
532 assert(fetch
->old_entry
->mem_obj
->request
);
534 HttpReply
*old_rep
= (HttpReply
*) fetch
->old_entry
->getReply();
536 old_rep
->updateOnNotModified(reply
);
538 fetch
->old_entry
->timestampsSet();
540 /* get rid of 304 reply */
541 storeUnregister(fetch
->sc
, fetch
->entry
, fetch
);
543 fetch
->entry
->unlock("peerDigestFetchReply 304");
545 fetch
->entry
= fetch
->old_entry
;
547 fetch
->old_entry
= NULL
;
549 /* preserve request -- we need its size to update counters */
550 /* requestUnlink(r); */
551 /* fetch->entry->mem_obj->request = NULL; */
552 } else if (status
== Http::scOkay
) {
553 /* get rid of old entry if any */
555 if (fetch
->old_entry
) {
556 debugs(72, 3, "peerDigestFetchReply: got new digest, releasing old one");
557 storeUnregister(fetch
->old_sc
, fetch
->old_entry
, fetch
);
558 fetch
->old_entry
->releaseRequest();
559 fetch
->old_entry
->unlock("peerDigestFetchReply 200");
560 fetch
->old_entry
= NULL
;
563 /* some kind of a bug */
564 peerDigestFetchAbort(fetch
, buf
, reply
->sline
.reason());
565 return -1; /* XXX -1 will abort stuff in ReadReply! */
568 /* must have a ready-to-use store entry if we got here */
569 /* can we stay with the old in-memory digest? */
570 if (status
== Http::scNotModified
&& fetch
->pd
->cd
) {
571 peerDigestFetchStop(fetch
, buf
, "Not modified");
572 fetch
->state
= DIGEST_READ_DONE
;
574 fetch
->state
= DIGEST_READ_HEADERS
;
577 /* need more data, do we have space? */
579 if (size
>= SM_PAGE_SIZE
)
580 peerDigestFetchAbort(fetch
, buf
, "reply header too big");
583 /* We don't want to actually ack that we've handled anything,
584 * otherwise SwapInHeaders() won't get the reply line .. */
588 /* fetch headers from disk, pass on to SwapInCBlock */
590 peerDigestSwapInHeaders(void *data
, char *buf
, ssize_t size
)
592 DigestFetchState
*fetch
= (DigestFetchState
*)data
;
595 assert(fetch
->state
== DIGEST_READ_HEADERS
);
597 if (peerDigestFetchedEnough(fetch
, buf
, size
, "peerDigestSwapInHeaders"))
600 assert(!fetch
->offset
);
602 if ((hdr_size
= headersEnd(buf
, size
))) {
603 assert(fetch
->entry
->getReply());
604 assert(fetch
->entry
->getReply()->sline
.status() != Http::scNone
);
606 if (fetch
->entry
->getReply()->sline
.status() != Http::scOkay
) {
607 debugs(72, DBG_IMPORTANT
, "peerDigestSwapInHeaders: " << fetch
->pd
->host
<<
608 " status " << fetch
->entry
->getReply()->sline
.status() <<
611 peerDigestFetchAbort(fetch
, buf
, "internal status error");
615 fetch
->state
= DIGEST_READ_CBLOCK
;
616 return hdr_size
; /* Say how much data we read */
618 /* need more data, do we have space? */
620 if (size
>= SM_PAGE_SIZE
) {
621 peerDigestFetchAbort(fetch
, buf
, "stored header too big");
624 return 0; /* We need to read more to parse .. */
628 fatal("peerDigestSwapInHeaders() - shouldn't get here!\n");
629 return 0; /* keep gcc happy */
633 peerDigestSwapInCBlock(void *data
, char *buf
, ssize_t size
)
635 DigestFetchState
*fetch
= (DigestFetchState
*)data
;
637 assert(fetch
->state
== DIGEST_READ_CBLOCK
);
639 if (peerDigestFetchedEnough(fetch
, buf
, size
, "peerDigestSwapInCBlock"))
642 if (size
>= (ssize_t
)StoreDigestCBlockSize
) {
643 PeerDigest
*pd
= fetch
->pd
;
645 assert(pd
&& fetch
->entry
->getReply());
647 if (peerDigestSetCBlock(pd
, buf
)) {
648 /* XXX: soon we will have variable header size */
649 /* switch to CD buffer and fetch digest guts */
651 assert(pd
->cd
->mask
);
652 fetch
->state
= DIGEST_READ_MASK
;
653 return StoreDigestCBlockSize
;
655 peerDigestFetchAbort(fetch
, buf
, "invalid digest cblock");
659 /* need more data, do we have space? */
661 if (size
>= SM_PAGE_SIZE
) {
662 peerDigestFetchAbort(fetch
, buf
, "digest cblock too big");
665 return 0; /* We need more data */
669 fatal("peerDigestSwapInCBlock(): shouldn't get here!\n");
670 return 0; /* keep gcc happy */
674 peerDigestSwapInMask(void *data
, char *buf
, ssize_t size
)
676 DigestFetchState
*fetch
= (DigestFetchState
*)data
;
680 assert(pd
->cd
&& pd
->cd
->mask
);
683 * NOTENOTENOTENOTENOTE: buf doesn't point to pd->cd->mask anymore!
684 * we need to do the copy ourselves!
686 memcpy(pd
->cd
->mask
+ fetch
->mask_offset
, buf
, size
);
688 /* NOTE! buf points to the middle of pd->cd->mask! */
690 if (peerDigestFetchedEnough(fetch
, NULL
, size
, "peerDigestSwapInMask"))
693 fetch
->mask_offset
+= size
;
695 if (fetch
->mask_offset
>= pd
->cd
->mask_size
) {
696 debugs(72, 2, "peerDigestSwapInMask: Done! Got " <<
697 fetch
->mask_offset
<< ", expected " << pd
->cd
->mask_size
);
698 assert(fetch
->mask_offset
== pd
->cd
->mask_size
);
699 assert(peerDigestFetchedEnough(fetch
, NULL
, 0, "peerDigestSwapInMask"));
700 return -1; /* XXX! */
702 /* We always read everything, so return so */
706 fatal("peerDigestSwapInMask(): shouldn't get here!\n");
707 return 0; /* keep gcc happy */
711 peerDigestFetchedEnough(DigestFetchState
* fetch
, char *buf
, ssize_t size
, const char *step_name
)
713 PeerDigest
*pd
= NULL
;
714 const char *host
= "<unknown>"; /* peer host */
715 const char *reason
= NULL
; /* reason for completion */
716 const char *no_bug
= NULL
; /* successful completion if set */
717 const int pdcb_valid
= cbdataReferenceValid(fetch
->pd
);
718 const int pcb_valid
= cbdataReferenceValid(fetch
->pd
->peer
);
720 /* test possible exiting conditions (the same for most steps!)
721 * cases marked with '?!' should not happen */
724 if (!(pd
= fetch
->pd
))
725 reason
= "peer digest disappeared?!";
727 #if DONT /* WHY NOT? /HNO */
729 else if (!cbdataReferenceValid(pd
))
730 reason
= "invalidated peer digest?!";
735 host
= pd
->host
.termedBuf();
738 debugs(72, 6, step_name
<< ": peer " << host
<< ", offset: " <<
739 fetch
->offset
<< " size: " << size
<< ".");
741 /* continue checking (with pd and host known and valid) */
744 if (!cbdataReferenceValid(pd
->peer
))
745 reason
= "peer disappeared";
747 reason
= "swap failure";
748 else if (!fetch
->entry
)
749 reason
= "swap aborted?!";
750 else if (EBIT_TEST(fetch
->entry
->flags
, ENTRY_ABORTED
))
751 reason
= "swap aborted";
754 /* continue checking (maybe-successful eof case) */
755 if (!reason
&& !size
) {
757 reason
= "null digest?!";
758 else if (fetch
->mask_offset
!= (int)pd
->cd
->mask_size
)
759 reason
= "premature end of digest?!";
760 else if (!peerDigestUseful(pd
))
761 reason
= "useless digest";
763 reason
= no_bug
= "success";
766 /* finish if we have a reason */
768 const int level
= strstr(reason
, "?!") ? 1 : 3;
769 debugs(72, level
, "" << step_name
<< ": peer " << host
<< ", exiting after '" << reason
<< "'");
770 peerDigestReqFinish(fetch
, buf
,
771 1, pdcb_valid
, pcb_valid
, reason
, !no_bug
);
774 assert(pdcb_valid
&& pcb_valid
);
777 return reason
!= NULL
;
780 /* call this when all callback data is valid and fetch must be stopped but
781 * no error has occurred (e.g. we received 304 reply and reuse old digest) */
783 peerDigestFetchStop(DigestFetchState
* fetch
, char *buf
, const char *reason
)
786 debugs(72, 2, "peerDigestFetchStop: peer " << fetch
->pd
->host
<< ", reason: " << reason
);
787 peerDigestReqFinish(fetch
, buf
, 1, 1, 1, reason
, 0);
790 /* call this when all callback data is valid but something bad happened */
792 peerDigestFetchAbort(DigestFetchState
* fetch
, char *buf
, const char *reason
)
795 debugs(72, 2, "peerDigestFetchAbort: peer " << fetch
->pd
->host
<< ", reason: " << reason
);
796 peerDigestReqFinish(fetch
, buf
, 1, 1, 1, reason
, 1);
799 /* complete the digest transfer, update stats, unlock/release everything */
801 peerDigestReqFinish(DigestFetchState
* fetch
, char *buf
,
802 int fcb_valid
, int pdcb_valid
, int pcb_valid
,
803 const char *reason
, int err
)
807 /* must go before peerDigestPDFinish */
810 fetch
->pd
->flags
.requested
= false;
811 fetch
->pd
->req_result
= reason
;
814 /* schedule next check if peer is still out there */
816 PeerDigest
*pd
= fetch
->pd
;
819 pd
->times
.retry_delay
= peerDigestIncDelay(pd
);
820 peerDigestSetCheck(pd
, pd
->times
.retry_delay
);
822 pd
->times
.retry_delay
= 0;
823 peerDigestSetCheck(pd
, peerDigestNewDelay(fetch
->entry
));
827 /* note: order is significant */
829 peerDigestFetchSetStats(fetch
);
832 peerDigestPDFinish(fetch
, pcb_valid
, err
);
835 peerDigestFetchFinish(fetch
, err
);
838 /* destroys digest if peer disappeared
839 * must be called only when fetch and pd cbdata are valid */
841 peerDigestPDFinish(DigestFetchState
* fetch
, int pcb_valid
, int err
)
843 PeerDigest
*pd
= fetch
->pd
;
844 const char *host
= pd
->host
.termedBuf();
846 pd
->times
.received
= squid_curtime
;
847 pd
->times
.req_delay
= fetch
->resp_time
;
848 kb_incr(&pd
->stats
.sent
.kbytes
, (size_t) fetch
->sent
.bytes
);
849 kb_incr(&pd
->stats
.recv
.kbytes
, (size_t) fetch
->recv
.bytes
);
850 pd
->stats
.sent
.msgs
+= fetch
->sent
.msg
;
851 pd
->stats
.recv
.msgs
+= fetch
->recv
.msg
;
854 debugs(72, DBG_IMPORTANT
, "" << (pcb_valid
? "temporary " : "" ) << "disabling (" << pd
->req_result
<< ") digest from " << host
);
857 cacheDigestDestroy(pd
->cd
);
861 pd
->flags
.usable
= false;
864 peerDigestNotePeerGone(pd
);
868 pd
->flags
.usable
= true;
870 /* XXX: ugly condition, but how? */
872 if (fetch
->entry
->store_status
== STORE_OK
)
873 debugs(72, 2, "re-used old digest from " << host
);
875 debugs(72, 2, "received valid digest from " << host
);
878 cbdataReferenceDone(fetch
->pd
);
881 /* free fetch state structures
882 * must be called only when fetch cbdata is valid */
884 peerDigestFetchFinish(DigestFetchState
* fetch
, int err
)
886 assert(fetch
->entry
&& fetch
->request
);
888 if (fetch
->old_entry
) {
889 debugs(72, 3, "peerDigestFetchFinish: deleting old entry");
890 storeUnregister(fetch
->old_sc
, fetch
->old_entry
, fetch
);
891 fetch
->old_entry
->releaseRequest();
892 fetch
->old_entry
->unlock("peerDigestFetchFinish old");
893 fetch
->old_entry
= NULL
;
896 /* update global stats */
897 kb_incr(&statCounter
.cd
.kbytes_sent
, (size_t) fetch
->sent
.bytes
);
899 kb_incr(&statCounter
.cd
.kbytes_recv
, (size_t) fetch
->recv
.bytes
);
901 statCounter
.cd
.msgs_sent
+= fetch
->sent
.msg
;
903 statCounter
.cd
.msgs_recv
+= fetch
->recv
.msg
;
905 /* unlock everything */
906 storeUnregister(fetch
->sc
, fetch
->entry
, fetch
);
908 fetch
->entry
->unlock("peerDigestFetchFinish new");
910 HTTPMSGUNLOCK(fetch
->request
);
914 assert(fetch
->pd
== NULL
);
919 /* calculate fetch stats after completion */
921 peerDigestFetchSetStats(DigestFetchState
* fetch
)
924 assert(fetch
->entry
&& fetch
->request
);
926 mem
= fetch
->entry
->mem_obj
;
929 /* XXX: outgoing numbers are not precise */
930 /* XXX: we must distinguish between 304 hits and misses here */
931 fetch
->sent
.bytes
= fetch
->request
->prefixLen();
932 /* XXX: this is slightly wrong: we don't KNOW that the entire memobject
933 * was fetched. We only know how big it is
935 fetch
->recv
.bytes
= mem
->size();
936 fetch
->sent
.msg
= fetch
->recv
.msg
= 1;
937 fetch
->expires
= fetch
->entry
->expires
;
938 fetch
->resp_time
= squid_curtime
- fetch
->start_time
;
940 debugs(72, 3, "peerDigestFetchFinish: recv " << fetch
->recv
.bytes
<<
941 " bytes in " << (int) fetch
->resp_time
<< " secs");
943 debugs(72, 3, "peerDigestFetchFinish: expires: " <<
944 (long int) fetch
->expires
<< " (" << std::showpos
<<
945 (int) (fetch
->expires
- squid_curtime
) << "), lmt: " <<
946 std::noshowpos
<< (long int) fetch
->entry
->lastmod
<< " (" <<
947 std::showpos
<< (int) (fetch
->entry
->lastmod
- squid_curtime
) <<
953 peerDigestSetCBlock(PeerDigest
* pd
, const char *buf
)
955 StoreDigestCBlock cblock
;
957 const char *host
= pd
->host
.termedBuf();
959 memcpy(&cblock
, buf
, sizeof(cblock
));
960 /* network -> host conversions */
961 cblock
.ver
.current
= ntohs(cblock
.ver
.current
);
962 cblock
.ver
.required
= ntohs(cblock
.ver
.required
);
963 cblock
.capacity
= ntohl(cblock
.capacity
);
964 cblock
.count
= ntohl(cblock
.count
);
965 cblock
.del_count
= ntohl(cblock
.del_count
);
966 cblock
.mask_size
= ntohl(cblock
.mask_size
);
967 debugs(72, 2, "got digest cblock from " << host
<< "; ver: " <<
968 (int) cblock
.ver
.current
<< " (req: " << (int) cblock
.ver
.required
<<
971 debugs(72, 2, "\t size: " <<
972 cblock
.mask_size
<< " bytes, e-cnt: " <<
973 cblock
.count
<< ", e-util: " <<
974 xpercentInt(cblock
.count
, cblock
.capacity
) << "%" );
975 /* check version requirements (both ways) */
977 if (cblock
.ver
.required
> CacheDigestVer
.current
) {
978 debugs(72, DBG_IMPORTANT
, "" << host
<< " digest requires version " <<
979 cblock
.ver
.required
<< "; have: " << CacheDigestVer
.current
);
984 if (cblock
.ver
.current
< CacheDigestVer
.required
) {
985 debugs(72, DBG_IMPORTANT
, "" << host
<< " digest is version " <<
986 cblock
.ver
.current
<< "; we require: " <<
987 CacheDigestVer
.required
);
992 /* check consistency */
993 if (cblock
.ver
.required
> cblock
.ver
.current
||
994 cblock
.mask_size
<= 0 || cblock
.capacity
<= 0 ||
995 cblock
.bits_per_entry
<= 0 || cblock
.hash_func_count
<= 0) {
996 debugs(72, DBG_CRITICAL
, "" << host
<< " digest cblock is corrupted.");
1000 /* check consistency further */
1001 if ((size_t)cblock
.mask_size
!= cacheDigestCalcMaskSize(cblock
.capacity
, cblock
.bits_per_entry
)) {
1002 debugs(72, DBG_CRITICAL
, host
<< " digest cblock is corrupted " <<
1003 "(mask size mismatch: " << cblock
.mask_size
<< " ? " <<
1004 cacheDigestCalcMaskSize(cblock
.capacity
, cblock
.bits_per_entry
)
1009 /* there are some things we cannot do yet */
1010 if (cblock
.hash_func_count
!= CacheDigestHashFuncCount
) {
1011 debugs(72, DBG_CRITICAL
, "" << host
<< " digest: unsupported #hash functions: " <<
1012 cblock
.hash_func_count
<< " ? " << CacheDigestHashFuncCount
<< ".");
1017 * no cblock bugs below this point
1019 /* check size changes */
1020 if (pd
->cd
&& cblock
.mask_size
!= (ssize_t
)pd
->cd
->mask_size
) {
1021 debugs(72, 2, host
<< " digest changed size: " << cblock
.mask_size
<<
1022 " -> " << pd
->cd
->mask_size
);
1023 freed_size
= pd
->cd
->mask_size
;
1024 cacheDigestDestroy(pd
->cd
);
1029 debugs(72, 2, "creating " << host
<< " digest; size: " << cblock
.mask_size
<< " (" <<
1030 std::showpos
<< (int) (cblock
.mask_size
- freed_size
) << ") bytes");
1031 pd
->cd
= cacheDigestCreate(cblock
.capacity
, cblock
.bits_per_entry
);
1033 if (cblock
.mask_size
>= freed_size
)
1034 kb_incr(&statCounter
.cd
.memory
, cblock
.mask_size
- freed_size
);
1038 /* these assignments leave us in an inconsistent state until we finish reading the digest */
1039 pd
->cd
->count
= cblock
.count
;
1040 pd
->cd
->del_count
= cblock
.del_count
;
1045 peerDigestUseful(const PeerDigest
* pd
)
1047 /* TODO: we should calculate the prob of a false hit instead of bit util */
1048 const int bit_util
= cacheDigestBitUtil(pd
->cd
);
1050 if (bit_util
> 65) {
1051 debugs(72, DBG_CRITICAL
, "Warning: " << pd
->host
<<
1052 " peer digest has too many bits on (" << bit_util
<< "%%).");
1061 saneDiff(time_t diff
)
1063 return abs((int) diff
) > squid_curtime
/ 2 ? 0 : diff
;
1067 peerDigestStatsReport(const PeerDigest
* pd
, StoreEntry
* e
)
1069 #define f2s(flag) (pd->flags.flag ? "yes" : "no")
1070 #define appendTime(tm) storeAppendPrintf(e, "%s\t %10ld\t %+d\t %+d\n", \
1071 ""#tm, (long int)pd->times.tm, \
1072 saneDiff(pd->times.tm - squid_curtime), \
1073 saneDiff(pd->times.tm - pd->times.initialized))
1077 const char *host
= pd
->host
.termedBuf();
1078 storeAppendPrintf(e
, "\npeer digest from %s\n", host
);
1080 cacheDigestGuessStatsReport(&pd
->stats
.guess
, e
, host
);
1082 storeAppendPrintf(e
, "\nevent\t timestamp\t secs from now\t secs from init\n");
1083 appendTime(initialized
);
1085 appendTime(requested
);
1086 appendTime(received
);
1087 appendTime(next_check
);
1089 storeAppendPrintf(e
, "peer digest state:\n");
1090 storeAppendPrintf(e
, "\tneeded: %3s, usable: %3s, requested: %3s\n",
1091 f2s(needed
), f2s(usable
), f2s(requested
));
1092 storeAppendPrintf(e
, "\n\tlast retry delay: %d secs\n",
1093 (int) pd
->times
.retry_delay
);
1094 storeAppendPrintf(e
, "\tlast request response time: %d secs\n",
1095 (int) pd
->times
.req_delay
);
1096 storeAppendPrintf(e
, "\tlast request result: %s\n",
1097 pd
->req_result
? pd
->req_result
: "(none)");
1099 storeAppendPrintf(e
, "\npeer digest traffic:\n");
1100 storeAppendPrintf(e
, "\trequests sent: %d, volume: %d KB\n",
1101 pd
->stats
.sent
.msgs
, (int) pd
->stats
.sent
.kbytes
.kb
);
1102 storeAppendPrintf(e
, "\treplies recv: %d, volume: %d KB\n",
1103 pd
->stats
.recv
.msgs
, (int) pd
->stats
.recv
.kbytes
.kb
);
1105 storeAppendPrintf(e
, "\npeer digest structure:\n");
1108 cacheDigestReport(pd
->cd
, host
, e
);
1110 storeAppendPrintf(e
, "\tno in-memory copy\n");