2 * DEBUG: section 71 Store Digest Manager
3 * AUTHOR: Alex Rousskov
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 * TODO: We probably do not track all the cases when
35 * storeDigestNoteStoreReady() must be called; this may prevent
36 * storeDigestRebuild/write schedule to be activated
43 #include "mgr/Registration.h"
44 #include "store_digest.h"
47 #include "CacheDigest.h"
48 #include "HttpReply.h"
49 #include "HttpRequest.h"
51 #include "MemObject.h"
52 #include "PeerDigest.h"
54 #include "SquidTime.h"
56 #include "StoreSearch.h"
66 class StoreDigestState
70 StoreDigestCBlock cblock
;
71 int rebuild_lock
; /* bucket number */
72 StoreEntry
* rewrite_lock
; /* points to store entry with the digest */
73 StoreSearchPointer theSearch
;
80 int del_count
; /* #store entries deleted from store_digest */
81 int del_lost_count
; /* #store entries not found in store_digest on delete */
82 int add_count
; /* #store entries accepted to store_digest */
83 int add_coll_count
; /* #accepted entries that collided with existing ones */
84 int rej_count
; /* #store entries not accepted to store_digest */
85 int rej_coll_count
; /* #not accepted entries that collided with existing ones */
89 static StoreDigestState sd_state
;
90 static StoreDigestStats sd_stats
;
92 /* local prototypes */
93 static void storeDigestRebuildStart(void *datanotused
);
94 static void storeDigestRebuildResume(void);
95 static void storeDigestRebuildFinish(void);
96 static void storeDigestRebuildStep(void *datanotused
);
97 static void storeDigestRewriteStart(void *);
98 static void storeDigestRewriteResume(void);
99 static void storeDigestRewriteFinish(StoreEntry
* e
);
100 static EVH storeDigestSwapOutStep
;
101 static void storeDigestCBlockSwapOut(StoreEntry
* e
);
102 static int storeDigestCalcCap(void);
103 static int storeDigestResize(void);
104 static void storeDigestAdd(const StoreEntry
*);
106 #endif /* USE_CACHE_DIGESTS */
109 storeDigestRegisterWithCacheManager(void)
111 Mgr::RegisterAction("store_digest", "Store Digest", storeDigestReport
, 0, 1);
119 storeDigestInit(void)
121 storeDigestRegisterWithCacheManager();
123 #if USE_CACHE_DIGESTS
124 const int cap
= storeDigestCalcCap();
126 if (!Config
.onoff
.digest_generation
) {
128 debugs(71, 3, "Local cache digest generation disabled");
132 store_digest
= cacheDigestCreate(cap
, Config
.digest
.bits_per_entry
);
133 debugs(71, DBG_IMPORTANT
, "Local cache digest enabled; rebuild/rewrite every " <<
134 (int) Config
.digest
.rebuild_period
<< "/" <<
135 (int) Config
.digest
.rewrite_period
<< " sec");
137 memset(&sd_state
, 0, sizeof(sd_state
));
140 debugs(71, 3, "Local cache digest is 'off'");
144 /* called when store_rebuild completes */
146 storeDigestNoteStoreReady(void)
148 #if USE_CACHE_DIGESTS
150 if (Config
.onoff
.digest_generation
) {
151 storeDigestRebuildStart(NULL
);
152 storeDigestRewriteStart(NULL
);
158 //TODO: this seems to be dead code. Is it needed?
160 storeDigestDel(const StoreEntry
* entry
)
162 #if USE_CACHE_DIGESTS
164 if (!Config
.onoff
.digest_generation
) {
168 assert(entry
&& store_digest
);
169 debugs(71, 6, "storeDigestDel: checking entry, key: " << entry
->getMD5Text());
171 if (!EBIT_TEST(entry
->flags
, KEY_PRIVATE
)) {
172 if (!cacheDigestTest(store_digest
, (const cache_key
*)entry
->key
)) {
173 ++sd_stats
.del_lost_count
;
174 debugs(71, 6, "storeDigestDel: lost entry, key: " << entry
->getMD5Text() << " url: " << entry
->url() );
176 ++sd_stats
.del_count
;
177 cacheDigestDel(store_digest
, (const cache_key
*)entry
->key
);
178 debugs(71, 6, "storeDigestDel: deled entry, key: " << entry
->getMD5Text());
181 #endif //USE_CACHE_DIGESTS
185 storeDigestReport(StoreEntry
* e
)
187 #if USE_CACHE_DIGESTS
189 if (!Config
.onoff
.digest_generation
) {
194 cacheDigestReport(store_digest
, "store", e
);
195 storeAppendPrintf(e
, "\t added: %d rejected: %d ( %.2f %%) del-ed: %d\n",
198 xpercent(sd_stats
.rej_count
, sd_stats
.rej_count
+ sd_stats
.add_count
),
200 storeAppendPrintf(e
, "\t collisions: on add: %.2f %% on rej: %.2f %%\n",
201 xpercent(sd_stats
.add_coll_count
, sd_stats
.add_count
),
202 xpercent(sd_stats
.rej_coll_count
, sd_stats
.rej_count
));
204 storeAppendPrintf(e
, "store digest: disabled.\n");
207 #endif //USE_CACHE_DIGESTS
214 #if USE_CACHE_DIGESTS
216 /* should we digest this entry? used by storeDigestAdd() */
218 storeDigestAddable(const StoreEntry
* e
)
220 /* add some stats! XXX */
222 debugs(71, 6, "storeDigestAddable: checking entry, key: " << e
->getMD5Text());
224 /* check various entry flags (mimics StoreEntry::checkCachable XXX) */
226 if (!EBIT_TEST(e
->flags
, ENTRY_CACHABLE
)) {
227 debugs(71, 6, "storeDigestAddable: NO: not cachable");
231 if (EBIT_TEST(e
->flags
, KEY_PRIVATE
)) {
232 debugs(71, 6, "storeDigestAddable: NO: private key");
236 if (EBIT_TEST(e
->flags
, ENTRY_NEGCACHED
)) {
237 debugs(71, 6, "storeDigestAddable: NO: negative cached");
241 if (EBIT_TEST(e
->flags
, RELEASE_REQUEST
)) {
242 debugs(71, 6, "storeDigestAddable: NO: release requested");
246 if (e
->store_status
== STORE_OK
&& EBIT_TEST(e
->flags
, ENTRY_BAD_LENGTH
)) {
247 debugs(71, 6, "storeDigestAddable: NO: wrong content-length");
251 /* do not digest huge objects */
252 if (e
->swap_file_sz
> (uint64_t )Config
.Store
.maxObjectSize
) {
253 debugs(71, 6, "storeDigestAddable: NO: too big");
257 /* still here? check staleness */
258 /* Note: We should use the time of the next rebuild, not (cur_time+period) */
259 if (refreshCheckDigest(e
, Config
.digest
.rebuild_period
)) {
260 debugs(71, 6, "storeDigestAdd: entry expires within " << Config
.digest
.rebuild_period
<< " secs, ignoring");
265 * idea: how about also skipping very fresh (thus, potentially
266 * unstable) entries? Should be configurable through
267 * cd_refresh_pattern, of course.
270 * idea: skip objects that are going to be purged before the next
277 storeDigestAdd(const StoreEntry
* entry
)
279 assert(entry
&& store_digest
);
281 if (storeDigestAddable(entry
)) {
282 ++sd_stats
.add_count
;
284 if (cacheDigestTest(store_digest
, (const cache_key
*)entry
->key
))
285 ++sd_stats
.add_coll_count
;
287 cacheDigestAdd(store_digest
, (const cache_key
*)entry
->key
);
289 debugs(71, 6, "storeDigestAdd: added entry, key: " << entry
->getMD5Text());
291 ++sd_stats
.rej_count
;
293 if (cacheDigestTest(store_digest
, (const cache_key
*)entry
->key
))
294 ++sd_stats
.rej_coll_count
;
298 /* rebuilds digest from scratch */
300 storeDigestRebuildStart(void *datanotused
)
302 assert(store_digest
);
303 /* prevent overlapping if rebuild schedule is too tight */
305 if (sd_state
.rebuild_lock
) {
306 debugs(71, DBG_IMPORTANT
, "storeDigestRebuildStart: overlap detected, consider increasing rebuild period");
310 sd_state
.rebuild_lock
= 1;
311 debugs(71, 2, "storeDigestRebuildStart: rebuild #" << sd_state
.rebuild_count
+ 1);
313 if (sd_state
.rewrite_lock
) {
314 debugs(71, 2, "storeDigestRebuildStart: waiting for Rewrite to finish.");
318 storeDigestRebuildResume();
321 /* called be Rewrite to push Rebuild forward */
323 storeDigestRebuildResume(void)
325 assert(sd_state
.rebuild_lock
);
326 assert(!sd_state
.rewrite_lock
);
327 sd_state
.theSearch
= Store::Root().search(NULL
, NULL
);
328 /* resize or clear */
330 if (!storeDigestResize())
331 cacheDigestClear(store_digest
); /* not clean()! */
333 memset(&sd_stats
, 0, sizeof(sd_stats
));
335 eventAdd("storeDigestRebuildStep", storeDigestRebuildStep
, NULL
, 0.0, 1);
338 /* finishes swap out sequence for the digest; schedules next rebuild */
340 storeDigestRebuildFinish(void)
342 assert(sd_state
.rebuild_lock
);
343 sd_state
.rebuild_lock
= 0;
344 ++sd_state
.rebuild_count
;
345 debugs(71, 2, "storeDigestRebuildFinish: done.");
346 eventAdd("storeDigestRebuildStart", storeDigestRebuildStart
, NULL
, (double)
347 Config
.digest
.rebuild_period
, 1);
348 /* resume pending Rewrite if any */
350 if (sd_state
.rewrite_lock
)
351 storeDigestRewriteResume();
354 /* recalculate a few hash buckets per invocation; schedules next step */
356 storeDigestRebuildStep(void *datanotused
)
358 /* TODO: call Store::Root().size() to determine this.. */
359 int count
= Config
.Store
.objectsPerBucket
* (int) ceil((double) store_hash_buckets
*
360 (double) Config
.digest
.rebuild_chunk_percentage
/ 100.0);
361 assert(sd_state
.rebuild_lock
);
363 debugs(71, 3, "storeDigestRebuildStep: buckets: " << store_hash_buckets
<< " entries to check: " << count
);
365 while (count
-- && !sd_state
.theSearch
->isDone() && sd_state
.theSearch
->next())
366 storeDigestAdd(sd_state
.theSearch
->currentItem());
369 if (sd_state
.theSearch
->isDone())
370 storeDigestRebuildFinish();
372 eventAdd("storeDigestRebuildStep", storeDigestRebuildStep
, NULL
, 0.0, 1);
375 /* starts swap out sequence for the digest */
377 storeDigestRewriteStart(void *datanotused
)
383 assert(store_digest
);
384 /* prevent overlapping if rewrite schedule is too tight */
386 if (sd_state
.rewrite_lock
) {
387 debugs(71, DBG_IMPORTANT
, "storeDigestRewrite: overlap detected, consider increasing rewrite period");
391 debugs(71, 2, "storeDigestRewrite: start rewrite #" << sd_state
.rewrite_count
+ 1);
392 /* make new store entry */
393 url
= internalLocalUri("/squid-internal-periodic/", StoreDigestFileName
);
395 e
= storeCreateEntry(url
, url
, flags
, METHOD_GET
);
397 sd_state
.rewrite_lock
= e
;
398 debugs(71, 3, "storeDigestRewrite: url: " << url
<< " key: " << e
->getMD5Text());
399 HttpRequest
*req
= HttpRequest::CreateFromUrl(url
);
400 e
->mem_obj
->request
= HTTPMSGLOCK(req
);
401 /* wait for rebuild (if any) to finish */
403 if (sd_state
.rebuild_lock
) {
404 debugs(71, 2, "storeDigestRewriteStart: waiting for rebuild to finish.");
408 storeDigestRewriteResume();
412 storeDigestRewriteResume(void)
416 assert(sd_state
.rewrite_lock
);
417 assert(!sd_state
.rebuild_lock
);
418 e
= sd_state
.rewrite_lock
;
419 sd_state
.rewrite_offset
= 0;
420 EBIT_SET(e
->flags
, ENTRY_SPECIAL
);
421 /* setting public key will purge old digest entry if any */
424 HttpReply
*rep
= new HttpReply
;
425 rep
->setHeaders(HTTP_OK
, "Cache Digest OK",
426 "application/cache-digest", (store_digest
->mask_size
+ sizeof(sd_state
.cblock
)),
427 squid_curtime
, (squid_curtime
+ Config
.digest
.rewrite_period
) );
428 debugs(71, 3, "storeDigestRewrite: entry expires on " << rep
->expires
<<
429 " (" << std::showpos
<< (int) (rep
->expires
- squid_curtime
) << ")");
431 e
->replaceHttpReply(rep
);
432 storeDigestCBlockSwapOut(e
);
434 eventAdd("storeDigestSwapOutStep", storeDigestSwapOutStep
, sd_state
.rewrite_lock
, 0.0, 1, false);
437 /* finishes swap out sequence for the digest; schedules next rewrite */
439 storeDigestRewriteFinish(StoreEntry
* e
)
441 assert(e
== sd_state
.rewrite_lock
);
444 debugs(71, 2, "storeDigestRewriteFinish: digest expires at " << e
->expires
<<
445 " (" << std::showpos
<< (int) (e
->expires
- squid_curtime
) << ")");
446 /* is this the write order? @?@ */
447 e
->mem_obj
->unlinkRequest();
449 sd_state
.rewrite_lock
= NULL
;
450 ++sd_state
.rewrite_count
;
451 eventAdd("storeDigestRewriteStart", storeDigestRewriteStart
, NULL
, (double)
452 Config
.digest
.rewrite_period
, 1);
453 /* resume pending Rebuild if any */
455 if (sd_state
.rebuild_lock
)
456 storeDigestRebuildResume();
459 /* swaps out one digest "chunk" per invocation; schedules next swap out */
461 storeDigestSwapOutStep(void *data
)
463 StoreEntry
*e
= static_cast<StoreEntry
*>(data
);
464 int chunk_size
= Config
.digest
.swapout_chunk_size
;
465 assert(e
== sd_state
.rewrite_lock
);
467 /* _add_ check that nothing bad happened while we were waiting @?@ @?@ */
469 if (sd_state
.rewrite_offset
+ chunk_size
> store_digest
->mask_size
)
470 chunk_size
= store_digest
->mask_size
- sd_state
.rewrite_offset
;
472 e
->append(store_digest
->mask
+ sd_state
.rewrite_offset
, chunk_size
);
474 debugs(71, 3, "storeDigestSwapOutStep: size: " << store_digest
->mask_size
<<
475 " offset: " << sd_state
.rewrite_offset
<< " chunk: " <<
476 chunk_size
<< " bytes");
478 sd_state
.rewrite_offset
+= chunk_size
;
481 if (sd_state
.rewrite_offset
>= store_digest
->mask_size
)
482 storeDigestRewriteFinish(e
);
484 eventAdd("storeDigestSwapOutStep", storeDigestSwapOutStep
, data
, 0.0, 1, false);
488 storeDigestCBlockSwapOut(StoreEntry
* e
)
490 memset(&sd_state
.cblock
, 0, sizeof(sd_state
.cblock
));
491 sd_state
.cblock
.ver
.current
= htons(CacheDigestVer
.current
);
492 sd_state
.cblock
.ver
.required
= htons(CacheDigestVer
.required
);
493 sd_state
.cblock
.capacity
= htonl(store_digest
->capacity
);
494 sd_state
.cblock
.count
= htonl(store_digest
->count
);
495 sd_state
.cblock
.del_count
= htonl(store_digest
->del_count
);
496 sd_state
.cblock
.mask_size
= htonl(store_digest
->mask_size
);
497 sd_state
.cblock
.bits_per_entry
= (unsigned char)
498 Config
.digest
.bits_per_entry
;
499 sd_state
.cblock
.hash_func_count
= (unsigned char) CacheDigestHashFuncCount
;
500 e
->append((char *) &sd_state
.cblock
, sizeof(sd_state
.cblock
));
503 /* calculates digest capacity */
505 storeDigestCalcCap(void)
508 * To-Do: Bloom proved that the optimal filter utilization is 50% (half of
509 * the bits are off). However, we do not have a formula to calculate the
510 * number of _entries_ we want to pre-allocate for.
512 const int hi_cap
= Store::Root().maxSize() / Config
.Store
.avgObjectSize
;
513 const int lo_cap
= 1 + Store::Root().currentSize() / Config
.Store
.avgObjectSize
;
514 const int e_count
= StoreEntry::inUseCount();
515 int cap
= e_count
? e_count
:hi_cap
;
516 debugs(71, 2, "storeDigestCalcCap: have: " << e_count
<< ", want " << cap
<<
517 " entries; limits: [" << lo_cap
<< ", " << hi_cap
<< "]");
522 /* do not enforce hi_cap limit, average-based estimation may be wrong
529 /* returns true if we actually resized the digest */
531 storeDigestResize(void)
533 const int cap
= storeDigestCalcCap();
535 assert(store_digest
);
536 diff
= abs(cap
- store_digest
->capacity
);
537 debugs(71, 2, "storeDigestResize: " <<
538 store_digest
->capacity
<< " -> " << cap
<< "; change: " <<
539 diff
<< " (" << xpercentInt(diff
, store_digest
->capacity
) << "%)" );
540 /* avoid minor adjustments */
542 if (diff
<= store_digest
->capacity
/ 10) {
543 debugs(71, 2, "storeDigestResize: small change, will not resize.");
546 debugs(71, 2, "storeDigestResize: big change, resizing.");
547 cacheDigestChangeCap(store_digest
, cap
);
552 #endif /* USE_CACHE_DIGESTS */