5 * DEBUG: section 71 Store Digest Manager
6 * AUTHOR: Alex Rousskov
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
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.
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.
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.
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.
38 * TODO: We probably do not track all the cases when
39 * storeDigestNoteStoreReady() must be called; this may prevent
40 * storeDigestRebuild/write schedule to be activated
47 #include "mgr/Registration.h"
51 #include "CacheDigest.h"
52 #include "HttpReply.h"
53 #include "HttpRequest.h"
54 #include "MemObject.h"
55 #include "PeerDigest.h"
57 #include "SquidTime.h"
59 #include "StoreSearch.h"
69 class StoreDigestState
73 StoreDigestCBlock cblock
;
74 int rebuild_lock
; /* bucket number */
75 StoreEntry
* rewrite_lock
; /* points to store entry with the digest */
76 StoreSearchPointer theSearch
;
84 int del_count
; /* #store entries deleted from store_digest */
85 int del_lost_count
; /* #store entries not found in store_digest on delete */
86 int add_count
; /* #store entries accepted to store_digest */
87 int add_coll_count
; /* #accepted entries that collided with existing ones */
88 int rej_count
; /* #store entries not accepted to store_digest */
89 int rej_coll_count
; /* #not accepted entries that collided with existing ones */
93 static StoreDigestState sd_state
;
94 static StoreDigestStats sd_stats
;
96 /* local prototypes */
97 static void storeDigestRebuildStart(void *datanotused
);
98 static void storeDigestRebuildResume(void);
99 static void storeDigestRebuildFinish(void);
100 static void storeDigestRebuildStep(void *datanotused
);
101 static void storeDigestRewriteStart(void *);
102 static void storeDigestRewriteResume(void);
103 static void storeDigestRewriteFinish(StoreEntry
* e
);
104 static EVH storeDigestSwapOutStep
;
105 static void storeDigestCBlockSwapOut(StoreEntry
* e
);
106 static int storeDigestCalcCap(void);
107 static int storeDigestResize(void);
108 static void storeDigestAdd(const StoreEntry
*);
110 #endif /* USE_CACHE_DIGESTS */
113 storeDigestRegisterWithCacheManager(void)
115 Mgr::RegisterAction("store_digest", "Store Digest", storeDigestReport
, 0, 1);
123 storeDigestInit(void)
125 storeDigestRegisterWithCacheManager();
127 #if USE_CACHE_DIGESTS
128 const int cap
= storeDigestCalcCap();
130 if (!Config
.onoff
.digest_generation
) {
132 debugs(71, 3, "Local cache digest generation disabled");
136 store_digest
= cacheDigestCreate(cap
, Config
.digest
.bits_per_entry
);
137 debugs(71, DBG_IMPORTANT
, "Local cache digest enabled; rebuild/rewrite every " <<
138 (int) Config
.digest
.rebuild_period
<< "/" <<
139 (int) Config
.digest
.rewrite_period
<< " sec");
141 memset(&sd_state
, 0, sizeof(sd_state
));
145 debugs(71, 3, "Local cache digest is 'off'");
149 /* called when store_rebuild completes */
151 storeDigestNoteStoreReady(void)
153 #if USE_CACHE_DIGESTS
155 if (Config
.onoff
.digest_generation
) {
156 storeDigestRebuildStart(NULL
);
157 storeDigestRewriteStart(NULL
);
164 storeDigestDel(const StoreEntry
* entry
)
166 #if USE_CACHE_DIGESTS
168 if (!Config
.onoff
.digest_generation
) {
172 assert(entry
&& store_digest
);
173 debugs(71, 6, "storeDigestDel: checking entry, key: " << entry
->getMD5Text());
175 if (!EBIT_TEST(entry
->flags
, KEY_PRIVATE
)) {
176 if (!cacheDigestTest(store_digest
, (const cache_key
*)entry
->key
)) {
177 ++sd_stats
.del_lost_count
;
178 debugs(71, 6, "storeDigestDel: lost entry, key: " << entry
->getMD5Text() << " url: " << entry
->url() );
180 ++sd_stats
.del_count
;
181 cacheDigestDel(store_digest
, (const cache_key
*)entry
->key
);
182 debugs(71, 6, "storeDigestDel: deled entry, key: " << entry
->getMD5Text());
190 storeDigestReport(StoreEntry
* e
)
192 #if USE_CACHE_DIGESTS
194 if (!Config
.onoff
.digest_generation
) {
199 cacheDigestReport(store_digest
, "store", e
);
200 storeAppendPrintf(e
, "\t added: %d rejected: %d ( %.2f %%) del-ed: %d\n",
203 xpercent(sd_stats
.rej_count
, sd_stats
.rej_count
+ sd_stats
.add_count
),
205 storeAppendPrintf(e
, "\t collisions: on add: %.2f %% on rej: %.2f %%\n",
206 xpercent(sd_stats
.add_coll_count
, sd_stats
.add_count
),
207 xpercent(sd_stats
.rej_coll_count
, sd_stats
.rej_count
));
209 storeAppendPrintf(e
, "store digest: disabled.\n");
219 #if USE_CACHE_DIGESTS
221 /* should we digest this entry? used by storeDigestAdd() */
223 storeDigestAddable(const StoreEntry
* e
)
225 /* add some stats! XXX */
227 debugs(71, 6, "storeDigestAddable: checking entry, key: " << e
->getMD5Text());
229 /* check various entry flags (mimics StoreEntry::checkCachable XXX) */
231 if (!EBIT_TEST(e
->flags
, ENTRY_CACHABLE
)) {
232 debugs(71, 6, "storeDigestAddable: NO: not cachable");
236 if (EBIT_TEST(e
->flags
, KEY_PRIVATE
)) {
237 debugs(71, 6, "storeDigestAddable: NO: private key");
241 if (EBIT_TEST(e
->flags
, ENTRY_NEGCACHED
)) {
242 debugs(71, 6, "storeDigestAddable: NO: negative cached");
246 if (EBIT_TEST(e
->flags
, RELEASE_REQUEST
)) {
247 debugs(71, 6, "storeDigestAddable: NO: release requested");
251 if (e
->store_status
== STORE_OK
&& EBIT_TEST(e
->flags
, ENTRY_BAD_LENGTH
)) {
252 debugs(71, 6, "storeDigestAddable: NO: wrong content-length");
256 /* do not digest huge objects */
257 if (e
->swap_file_sz
> (uint64_t )Config
.Store
.maxObjectSize
) {
258 debugs(71, 6, "storeDigestAddable: NO: too big");
262 /* still here? check staleness */
263 /* Note: We should use the time of the next rebuild, not (cur_time+period) */
264 if (refreshCheckDigest(e
, Config
.digest
.rebuild_period
)) {
265 debugs(71, 6, "storeDigestAdd: entry expires within " << Config
.digest
.rebuild_period
<< " secs, ignoring");
270 * idea: how about also skipping very fresh (thus, potentially
271 * unstable) entries? Should be configurable through
272 * cd_refresh_pattern, of course.
275 * idea: skip objects that are going to be purged before the next
282 storeDigestAdd(const StoreEntry
* entry
)
284 assert(entry
&& store_digest
);
286 if (storeDigestAddable(entry
)) {
287 ++sd_stats
.add_count
;
289 if (cacheDigestTest(store_digest
, (const cache_key
*)entry
->key
))
290 ++sd_stats
.add_coll_count
;
292 cacheDigestAdd(store_digest
, (const cache_key
*)entry
->key
);
294 debugs(71, 6, "storeDigestAdd: added entry, key: " << entry
->getMD5Text());
296 ++sd_stats
.rej_count
;
298 if (cacheDigestTest(store_digest
, (const cache_key
*)entry
->key
))
299 ++sd_stats
.rej_coll_count
;
303 /* rebuilds digest from scratch */
305 storeDigestRebuildStart(void *datanotused
)
307 assert(store_digest
);
308 /* prevent overlapping if rebuild schedule is too tight */
310 if (sd_state
.rebuild_lock
) {
311 debugs(71, DBG_IMPORTANT
, "storeDigestRebuildStart: overlap detected, consider increasing rebuild period");
315 sd_state
.rebuild_lock
= 1;
316 debugs(71, 2, "storeDigestRebuildStart: rebuild #" << sd_state
.rebuild_count
+ 1);
318 if (sd_state
.rewrite_lock
) {
319 debugs(71, 2, "storeDigestRebuildStart: waiting for Rewrite to finish.");
323 storeDigestRebuildResume();
326 /* called be Rewrite to push Rebuild forward */
328 storeDigestRebuildResume(void)
330 assert(sd_state
.rebuild_lock
);
331 assert(!sd_state
.rewrite_lock
);
332 sd_state
.theSearch
= Store::Root().search(NULL
, NULL
);
333 /* resize or clear */
335 if (!storeDigestResize())
336 cacheDigestClear(store_digest
); /* not clean()! */
338 memset(&sd_stats
, 0, sizeof(sd_stats
));
340 eventAdd("storeDigestRebuildStep", storeDigestRebuildStep
, NULL
, 0.0, 1);
343 /* finishes swap out sequence for the digest; schedules next rebuild */
345 storeDigestRebuildFinish(void)
347 assert(sd_state
.rebuild_lock
);
348 sd_state
.rebuild_lock
= 0;
349 ++sd_state
.rebuild_count
;
350 debugs(71, 2, "storeDigestRebuildFinish: done.");
351 eventAdd("storeDigestRebuildStart", storeDigestRebuildStart
, NULL
, (double)
352 Config
.digest
.rebuild_period
, 1);
353 /* resume pending Rewrite if any */
355 if (sd_state
.rewrite_lock
)
356 storeDigestRewriteResume();
359 /* recalculate a few hash buckets per invocation; schedules next step */
361 storeDigestRebuildStep(void *datanotused
)
363 /* TODO: call Store::Root().size() to determine this.. */
364 int count
= Config
.Store
.objectsPerBucket
* (int) ceil((double) store_hash_buckets
*
365 (double) Config
.digest
.rebuild_chunk_percentage
/ 100.0);
366 assert(sd_state
.rebuild_lock
);
368 debugs(71, 3, "storeDigestRebuildStep: buckets: " << store_hash_buckets
<< " entries to check: " << count
);
370 while (count
-- && !sd_state
.theSearch
->isDone() && sd_state
.theSearch
->next())
371 storeDigestAdd(sd_state
.theSearch
->currentItem());
374 if (sd_state
.theSearch
->isDone())
375 storeDigestRebuildFinish();
377 eventAdd("storeDigestRebuildStep", storeDigestRebuildStep
, NULL
, 0.0, 1);
381 /* starts swap out sequence for the digest */
383 storeDigestRewriteStart(void *datanotused
)
389 assert(store_digest
);
390 /* prevent overlapping if rewrite schedule is too tight */
392 if (sd_state
.rewrite_lock
) {
393 debugs(71, DBG_IMPORTANT
, "storeDigestRewrite: overlap detected, consider increasing rewrite period");
397 debugs(71, 2, "storeDigestRewrite: start rewrite #" << sd_state
.rewrite_count
+ 1);
398 /* make new store entry */
399 url
= internalLocalUri("/squid-internal-periodic/", StoreDigestFileName
);
401 e
= storeCreateEntry(url
, url
, flags
, METHOD_GET
);
403 sd_state
.rewrite_lock
= e
;
404 debugs(71, 3, "storeDigestRewrite: url: " << url
<< " key: " << e
->getMD5Text());
405 HttpRequest
*req
= HttpRequest::CreateFromUrl(url
);
406 e
->mem_obj
->request
= HTTPMSGLOCK(req
);
407 /* wait for rebuild (if any) to finish */
409 if (sd_state
.rebuild_lock
) {
410 debugs(71, 2, "storeDigestRewriteStart: waiting for rebuild to finish.");
414 storeDigestRewriteResume();
418 storeDigestRewriteResume(void)
422 assert(sd_state
.rewrite_lock
);
423 assert(!sd_state
.rebuild_lock
);
424 e
= sd_state
.rewrite_lock
;
425 sd_state
.rewrite_offset
= 0;
426 EBIT_SET(e
->flags
, ENTRY_SPECIAL
);
427 /* setting public key will purge old digest entry if any */
430 HttpReply
*rep
= new HttpReply
;
431 rep
->setHeaders(HTTP_OK
, "Cache Digest OK",
432 "application/cache-digest", (store_digest
->mask_size
+ sizeof(sd_state
.cblock
)),
433 squid_curtime
, (squid_curtime
+ Config
.digest
.rewrite_period
) );
434 debugs(71, 3, "storeDigestRewrite: entry expires on " << rep
->expires
<<
435 " (" << std::showpos
<< (int) (rep
->expires
- squid_curtime
) << ")");
437 e
->replaceHttpReply(rep
);
438 storeDigestCBlockSwapOut(e
);
440 eventAdd("storeDigestSwapOutStep", storeDigestSwapOutStep
, sd_state
.rewrite_lock
, 0.0, 1, false);
443 /* finishes swap out sequence for the digest; schedules next rewrite */
445 storeDigestRewriteFinish(StoreEntry
* e
)
447 assert(e
== sd_state
.rewrite_lock
);
450 debugs(71, 2, "storeDigestRewriteFinish: digest expires at " << e
->expires
<<
451 " (" << std::showpos
<< (int) (e
->expires
- squid_curtime
) << ")");
452 /* is this the write order? @?@ */
453 e
->mem_obj
->unlinkRequest();
455 sd_state
.rewrite_lock
= NULL
;
456 ++sd_state
.rewrite_count
;
457 eventAdd("storeDigestRewriteStart", storeDigestRewriteStart
, NULL
, (double)
458 Config
.digest
.rewrite_period
, 1);
459 /* resume pending Rebuild if any */
461 if (sd_state
.rebuild_lock
)
462 storeDigestRebuildResume();
465 /* swaps out one digest "chunk" per invocation; schedules next swap out */
467 storeDigestSwapOutStep(void *data
)
469 StoreEntry
*e
= static_cast<StoreEntry
*>(data
);
470 int chunk_size
= Config
.digest
.swapout_chunk_size
;
471 assert(e
== sd_state
.rewrite_lock
);
473 /* _add_ check that nothing bad happened while we were waiting @?@ @?@ */
475 if (sd_state
.rewrite_offset
+ chunk_size
> store_digest
->mask_size
)
476 chunk_size
= store_digest
->mask_size
- sd_state
.rewrite_offset
;
478 e
->append(store_digest
->mask
+ sd_state
.rewrite_offset
, chunk_size
);
480 debugs(71, 3, "storeDigestSwapOutStep: size: " << store_digest
->mask_size
<<
481 " offset: " << sd_state
.rewrite_offset
<< " chunk: " <<
482 chunk_size
<< " bytes");
484 sd_state
.rewrite_offset
+= chunk_size
;
487 if (sd_state
.rewrite_offset
>= store_digest
->mask_size
)
488 storeDigestRewriteFinish(e
);
490 eventAdd("storeDigestSwapOutStep", storeDigestSwapOutStep
, data
, 0.0, 1, false);
494 storeDigestCBlockSwapOut(StoreEntry
* e
)
496 memset(&sd_state
.cblock
, 0, sizeof(sd_state
.cblock
));
497 sd_state
.cblock
.ver
.current
= htons(CacheDigestVer
.current
);
498 sd_state
.cblock
.ver
.required
= htons(CacheDigestVer
.required
);
499 sd_state
.cblock
.capacity
= htonl(store_digest
->capacity
);
500 sd_state
.cblock
.count
= htonl(store_digest
->count
);
501 sd_state
.cblock
.del_count
= htonl(store_digest
->del_count
);
502 sd_state
.cblock
.mask_size
= htonl(store_digest
->mask_size
);
503 sd_state
.cblock
.bits_per_entry
= (unsigned char)
504 Config
.digest
.bits_per_entry
;
505 sd_state
.cblock
.hash_func_count
= (unsigned char) CacheDigestHashFuncCount
;
506 e
->append((char *) &sd_state
.cblock
, sizeof(sd_state
.cblock
));
509 /* calculates digest capacity */
511 storeDigestCalcCap(void)
514 * To-Do: Bloom proved that the optimal filter utilization is 50% (half of
515 * the bits are off). However, we do not have a formula to calculate the
516 * number of _entries_ we want to pre-allocate for.
518 const int hi_cap
= Store::Root().maxSize() / Config
.Store
.avgObjectSize
;
519 const int lo_cap
= 1 + Store::Root().currentSize() / Config
.Store
.avgObjectSize
;
520 const int e_count
= StoreEntry::inUseCount();
521 int cap
= e_count
? e_count
:hi_cap
;
522 debugs(71, 2, "storeDigestCalcCap: have: " << e_count
<< ", want " << cap
<<
523 " entries; limits: [" << lo_cap
<< ", " << hi_cap
<< "]");
528 /* do not enforce hi_cap limit, average-based estimation may be wrong
535 /* returns true if we actually resized the digest */
537 storeDigestResize(void)
539 const int cap
= storeDigestCalcCap();
541 assert(store_digest
);
542 diff
= abs(cap
- store_digest
->capacity
);
543 debugs(71, 2, "storeDigestResize: " <<
544 store_digest
->capacity
<< " -> " << cap
<< "; change: " <<
545 diff
<< " (" << xpercentInt(diff
, store_digest
->capacity
) << "%)" );
546 /* avoid minor adjustments */
548 if (diff
<= store_digest
->capacity
/ 10) {
549 debugs(71, 2, "storeDigestResize: small change, will not resize.");
552 debugs(71, 2, "storeDigestResize: big change, resizing.");
553 cacheDigestChangeCap(store_digest
, cap
);
558 #endif /* USE_CACHE_DIGESTS */