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
45 #include "mgr/Registration.h"
49 #include "HttpRequest.h"
50 #include "HttpReply.h"
51 #include "MemObject.h"
52 #include "PeerDigest.h"
53 #include "SquidTime.h"
54 #include "StoreSearch.h"
60 class StoreDigestState
64 StoreDigestCBlock cblock
;
65 int rebuild_lock
; /* bucket number */
66 StoreEntry
* rewrite_lock
; /* points to store entry with the digest */
67 StoreSearchPointer theSearch
;
75 int del_count
; /* #store entries deleted from store_digest */
76 int del_lost_count
; /* #store entries not found in store_digest on delete */
77 int add_count
; /* #store entries accepted to store_digest */
78 int add_coll_count
; /* #accepted entries that collided with existing ones */
79 int rej_count
; /* #store entries not accepted to store_digest */
80 int rej_coll_count
; /* #not accepted entries that collided with existing ones */
84 static StoreDigestState sd_state
;
85 static StoreDigestStats sd_stats
;
87 /* local prototypes */
88 static void storeDigestRebuildStart(void *datanotused
);
89 static void storeDigestRebuildResume(void);
90 static void storeDigestRebuildFinish(void);
91 static void storeDigestRebuildStep(void *datanotused
);
92 static void storeDigestRewriteStart(void *);
93 static void storeDigestRewriteResume(void);
94 static void storeDigestRewriteFinish(StoreEntry
* e
);
95 static EVH storeDigestSwapOutStep
;
96 static void storeDigestCBlockSwapOut(StoreEntry
* e
);
97 static int storeDigestCalcCap(void);
98 static int storeDigestResize(void);
99 static void storeDigestAdd(const StoreEntry
*);
101 #endif /* USE_CACHE_DIGESTS */
104 storeDigestRegisterWithCacheManager(void)
106 Mgr::RegisterAction("store_digest", "Store Digest", storeDigestReport
, 0, 1);
114 storeDigestInit(void)
116 storeDigestRegisterWithCacheManager();
118 #if USE_CACHE_DIGESTS
119 const int cap
= storeDigestCalcCap();
121 if (!Config
.onoff
.digest_generation
) {
123 debugs(71, 3, "Local cache digest generation disabled");
127 store_digest
= cacheDigestCreate(cap
, Config
.digest
.bits_per_entry
);
128 debugs(71, 1, "Local cache digest enabled; rebuild/rewrite every " <<
129 (int) Config
.digest
.rebuild_period
<< "/" <<
130 (int) Config
.digest
.rewrite_period
<< " sec");
132 memset(&sd_state
, 0, sizeof(sd_state
));
136 debugs(71, 3, "Local cache digest is 'off'");
140 /* called when store_rebuild completes */
142 storeDigestNoteStoreReady(void)
144 #if USE_CACHE_DIGESTS
146 if (Config
.onoff
.digest_generation
) {
147 storeDigestRebuildStart(NULL
);
148 storeDigestRewriteStart(NULL
);
155 storeDigestDel(const StoreEntry
* entry
)
157 #if USE_CACHE_DIGESTS
159 if (!Config
.onoff
.digest_generation
) {
163 assert(entry
&& store_digest
);
164 debugs(71, 6, "storeDigestDel: checking entry, key: " << entry
->getMD5Text());
166 if (!EBIT_TEST(entry
->flags
, KEY_PRIVATE
)) {
167 if (!cacheDigestTest(store_digest
, (const cache_key
*)entry
->key
)) {
168 sd_stats
.del_lost_count
++;
169 debugs(71, 6, "storeDigestDel: lost entry, key: " << entry
->getMD5Text() << " url: " << entry
->url() );
171 sd_stats
.del_count
++;
172 cacheDigestDel(store_digest
, (const cache_key
*)entry
->key
);
173 debugs(71, 6, "storeDigestDel: deled entry, key: " << entry
->getMD5Text());
181 storeDigestReport(StoreEntry
* e
)
183 #if USE_CACHE_DIGESTS
185 if (!Config
.onoff
.digest_generation
) {
190 cacheDigestReport(store_digest
, "store", e
);
191 storeAppendPrintf(e
, "\t added: %d rejected: %d ( %.2f %%) del-ed: %d\n",
194 xpercent(sd_stats
.rej_count
, sd_stats
.rej_count
+ sd_stats
.add_count
),
196 storeAppendPrintf(e
, "\t collisions: on add: %.2f %% on rej: %.2f %%\n",
197 xpercent(sd_stats
.add_coll_count
, sd_stats
.add_count
),
198 xpercent(sd_stats
.rej_coll_count
, sd_stats
.rej_count
));
200 storeAppendPrintf(e
, "store digest: disabled.\n");
210 #if USE_CACHE_DIGESTS
212 /* should we digest this entry? used by storeDigestAdd() */
214 storeDigestAddable(const StoreEntry
* e
)
216 /* add some stats! XXX */
218 debugs(71, 6, "storeDigestAddable: checking entry, key: " << e
->getMD5Text());
220 /* check various entry flags (mimics StoreEntry::checkCachable XXX) */
222 if (!EBIT_TEST(e
->flags
, ENTRY_CACHABLE
)) {
223 debugs(71, 6, "storeDigestAddable: NO: not cachable");
227 if (EBIT_TEST(e
->flags
, KEY_PRIVATE
)) {
228 debugs(71, 6, "storeDigestAddable: NO: private key");
232 if (EBIT_TEST(e
->flags
, ENTRY_NEGCACHED
)) {
233 debugs(71, 6, "storeDigestAddable: NO: negative cached");
237 if (EBIT_TEST(e
->flags
, RELEASE_REQUEST
)) {
238 debugs(71, 6, "storeDigestAddable: NO: release requested");
242 if (e
->store_status
== STORE_OK
&& EBIT_TEST(e
->flags
, ENTRY_BAD_LENGTH
)) {
243 debugs(71, 6, "storeDigestAddable: NO: wrong content-length");
247 /* do not digest huge objects */
248 if (e
->swap_file_sz
> (uint64_t )Config
.Store
.maxObjectSize
) {
249 debugs(71, 6, "storeDigestAddable: NO: too big");
253 /* still here? check staleness */
254 /* Note: We should use the time of the next rebuild, not (cur_time+period) */
255 if (refreshCheckDigest(e
, Config
.digest
.rebuild_period
)) {
256 debugs(71, 6, "storeDigestAdd: entry expires within " << Config
.digest
.rebuild_period
<< " secs, ignoring");
261 * idea: how about also skipping very fresh (thus, potentially
262 * unstable) entries? Should be configurable through
263 * cd_refresh_pattern, of course.
266 * idea: skip objects that are going to be purged before the next
273 storeDigestAdd(const StoreEntry
* entry
)
275 assert(entry
&& store_digest
);
277 if (storeDigestAddable(entry
)) {
278 sd_stats
.add_count
++;
280 if (cacheDigestTest(store_digest
, (const cache_key
*)entry
->key
))
281 sd_stats
.add_coll_count
++;
283 cacheDigestAdd(store_digest
, (const cache_key
*)entry
->key
);
285 debugs(71, 6, "storeDigestAdd: added entry, key: " << entry
->getMD5Text());
287 sd_stats
.rej_count
++;
289 if (cacheDigestTest(store_digest
, (const cache_key
*)entry
->key
))
290 sd_stats
.rej_coll_count
++;
294 /* rebuilds digest from scratch */
296 storeDigestRebuildStart(void *datanotused
)
298 assert(store_digest
);
299 /* prevent overlapping if rebuild schedule is too tight */
301 if (sd_state
.rebuild_lock
) {
302 debugs(71, 1, "storeDigestRebuildStart: overlap detected, consider increasing rebuild period");
306 sd_state
.rebuild_lock
= 1;
307 debugs(71, 2, "storeDigestRebuildStart: rebuild #" << sd_state
.rebuild_count
+ 1);
309 if (sd_state
.rewrite_lock
) {
310 debugs(71, 2, "storeDigestRebuildStart: waiting for Rewrite to finish.");
314 storeDigestRebuildResume();
317 /* called be Rewrite to push Rebuild forward */
319 storeDigestRebuildResume(void)
321 assert(sd_state
.rebuild_lock
);
322 assert(!sd_state
.rewrite_lock
);
323 sd_state
.theSearch
= Store::Root().search(NULL
, NULL
);
324 /* resize or clear */
326 if (!storeDigestResize())
327 cacheDigestClear(store_digest
); /* not clean()! */
329 memset(&sd_stats
, 0, sizeof(sd_stats
));
331 eventAdd("storeDigestRebuildStep", storeDigestRebuildStep
, NULL
, 0.0, 1);
334 /* finishes swap out sequence for the digest; schedules next rebuild */
336 storeDigestRebuildFinish(void)
338 assert(sd_state
.rebuild_lock
);
339 sd_state
.rebuild_lock
= 0;
340 sd_state
.rebuild_count
++;
341 debugs(71, 2, "storeDigestRebuildFinish: done.");
342 eventAdd("storeDigestRebuildStart", storeDigestRebuildStart
, NULL
, (double)
343 Config
.digest
.rebuild_period
, 1);
344 /* resume pending Rewrite if any */
346 if (sd_state
.rewrite_lock
)
347 storeDigestRewriteResume();
350 /* recalculate a few hash buckets per invocation; schedules next step */
352 storeDigestRebuildStep(void *datanotused
)
354 /* TODO: call Store::Root().size() to determine this.. */
355 int count
= Config
.Store
.objectsPerBucket
* (int) ceil((double) store_hash_buckets
*
356 (double) Config
.digest
.rebuild_chunk_percentage
/ 100.0);
357 assert(sd_state
.rebuild_lock
);
359 debugs(71, 3, "storeDigestRebuildStep: buckets: " << store_hash_buckets
<< " entries to check: " << count
);
361 while (count
-- && !sd_state
.theSearch
->isDone() && sd_state
.theSearch
->next())
362 storeDigestAdd(sd_state
.theSearch
->currentItem());
365 if (sd_state
.theSearch
->isDone())
366 storeDigestRebuildFinish();
368 eventAdd("storeDigestRebuildStep", storeDigestRebuildStep
, NULL
, 0.0, 1);
372 /* starts swap out sequence for the digest */
374 storeDigestRewriteStart(void *datanotused
)
380 assert(store_digest
);
381 /* prevent overlapping if rewrite schedule is too tight */
383 if (sd_state
.rewrite_lock
) {
384 debugs(71, 1, "storeDigestRewrite: overlap detected, consider increasing rewrite period");
388 debugs(71, 2, "storeDigestRewrite: start rewrite #" << sd_state
.rewrite_count
+ 1);
389 /* make new store entry */
390 url
= internalLocalUri("/squid-internal-periodic/", StoreDigestFileName
);
392 e
= storeCreateEntry(url
, url
, flags
, METHOD_GET
);
394 sd_state
.rewrite_lock
= e
;
395 debugs(71, 3, "storeDigestRewrite: url: " << url
<< " key: " << e
->getMD5Text());
396 HttpRequest
*req
= HttpRequest::CreateFromUrl(url
);
397 e
->mem_obj
->request
= HTTPMSGLOCK(req
);
398 /* wait for rebuild (if any) to finish */
400 if (sd_state
.rebuild_lock
) {
401 debugs(71, 2, "storeDigestRewriteStart: waiting for rebuild to finish.");
405 storeDigestRewriteResume();
409 storeDigestRewriteResume(void)
413 assert(sd_state
.rewrite_lock
);
414 assert(!sd_state
.rebuild_lock
);
415 e
= sd_state
.rewrite_lock
;
416 sd_state
.rewrite_offset
= 0;
417 EBIT_SET(e
->flags
, ENTRY_SPECIAL
);
418 /* setting public key will purge old digest entry if any */
421 HttpReply
*rep
= new HttpReply
;
422 rep
->setHeaders(HTTP_OK
, "Cache Digest OK",
423 "application/cache-digest", (store_digest
->mask_size
+ sizeof(sd_state
.cblock
)),
424 squid_curtime
, (squid_curtime
+ Config
.digest
.rewrite_period
) );
425 debugs(71, 3, "storeDigestRewrite: entry expires on " << rep
->expires
<<
426 " (" << std::showpos
<< (int) (rep
->expires
- squid_curtime
) << ")");
428 e
->replaceHttpReply(rep
);
429 storeDigestCBlockSwapOut(e
);
431 eventAdd("storeDigestSwapOutStep", storeDigestSwapOutStep
, sd_state
.rewrite_lock
, 0.0, 1, false);
434 /* finishes swap out sequence for the digest; schedules next rewrite */
436 storeDigestRewriteFinish(StoreEntry
* e
)
438 assert(e
== sd_state
.rewrite_lock
);
441 debugs(71, 2, "storeDigestRewriteFinish: digest expires at " << e
->expires
<<
442 " (" << std::showpos
<< (int) (e
->expires
- squid_curtime
) << ")");
443 /* is this the write order? @?@ */
444 e
->mem_obj
->unlinkRequest();
446 sd_state
.rewrite_lock
= NULL
;
447 sd_state
.rewrite_count
++;
448 eventAdd("storeDigestRewriteStart", storeDigestRewriteStart
, NULL
, (double)
449 Config
.digest
.rewrite_period
, 1);
450 /* resume pending Rebuild if any */
452 if (sd_state
.rebuild_lock
)
453 storeDigestRebuildResume();
456 /* swaps out one digest "chunk" per invocation; schedules next swap out */
458 storeDigestSwapOutStep(void *data
)
460 StoreEntry
*e
= static_cast<StoreEntry
*>(data
);
461 int chunk_size
= Config
.digest
.swapout_chunk_size
;
462 assert(e
== sd_state
.rewrite_lock
);
464 /* _add_ check that nothing bad happened while we were waiting @?@ @?@ */
466 if (sd_state
.rewrite_offset
+ chunk_size
> store_digest
->mask_size
)
467 chunk_size
= store_digest
->mask_size
- sd_state
.rewrite_offset
;
469 e
->append(store_digest
->mask
+ sd_state
.rewrite_offset
, chunk_size
);
471 debugs(71, 3, "storeDigestSwapOutStep: size: " << store_digest
->mask_size
<<
472 " offset: " << sd_state
.rewrite_offset
<< " chunk: " <<
473 chunk_size
<< " bytes");
475 sd_state
.rewrite_offset
+= chunk_size
;
478 if (sd_state
.rewrite_offset
>= store_digest
->mask_size
)
479 storeDigestRewriteFinish(e
);
481 eventAdd("storeDigestSwapOutStep", storeDigestSwapOutStep
, data
, 0.0, 1, false);
485 storeDigestCBlockSwapOut(StoreEntry
* e
)
487 memset(&sd_state
.cblock
, 0, sizeof(sd_state
.cblock
));
488 sd_state
.cblock
.ver
.current
= htons(CacheDigestVer
.current
);
489 sd_state
.cblock
.ver
.required
= htons(CacheDigestVer
.required
);
490 sd_state
.cblock
.capacity
= htonl(store_digest
->capacity
);
491 sd_state
.cblock
.count
= htonl(store_digest
->count
);
492 sd_state
.cblock
.del_count
= htonl(store_digest
->del_count
);
493 sd_state
.cblock
.mask_size
= htonl(store_digest
->mask_size
);
494 sd_state
.cblock
.bits_per_entry
= (unsigned char)
495 Config
.digest
.bits_per_entry
;
496 sd_state
.cblock
.hash_func_count
= (unsigned char) CacheDigestHashFuncCount
;
497 e
->append((char *) &sd_state
.cblock
, sizeof(sd_state
.cblock
));
500 /* calculates digest capacity */
502 storeDigestCalcCap(void)
505 * To-Do: Bloom proved that the optimal filter utilization is 50% (half of
506 * the bits are off). However, we do not have a formula to calculate the
507 * number of _entries_ we want to pre-allocate for.
509 const int hi_cap
= Store::Root().maxSize() / Config
.Store
.avgObjectSize
;
510 const int lo_cap
= 1 + Store::Root().currentSize() / Config
.Store
.avgObjectSize
;
511 const int e_count
= StoreEntry::inUseCount();
512 int cap
= e_count
? e_count
:hi_cap
;
513 debugs(71, 2, "storeDigestCalcCap: have: " << e_count
<< ", want " << cap
<<
514 " entries; limits: [" << lo_cap
<< ", " << hi_cap
<< "]");
519 /* do not enforce hi_cap limit, average-based estimation may be wrong
526 /* returns true if we actually resized the digest */
528 storeDigestResize(void)
530 const int cap
= storeDigestCalcCap();
532 assert(store_digest
);
533 diff
= abs(cap
- store_digest
->capacity
);
534 debugs(71, 2, "storeDigestResize: " <<
535 store_digest
->capacity
<< " -> " << cap
<< "; change: " <<
536 diff
<< " (" << xpercentInt(diff
, store_digest
->capacity
) << "%)" );
537 /* avoid minor adjustments */
539 if (diff
<= store_digest
->capacity
/ 10) {
540 debugs(71, 2, "storeDigestResize: small change, will not resize.");
543 debugs(71, 2, "storeDigestResize: big change, resizing.");
544 cacheDigestChangeCap(store_digest
, cap
);
549 #endif /* USE_CACHE_DIGESTS */