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 "SquidConfig.h"
55 #include "SquidTime.h"
57 #include "StoreSearch.h"
65 class StoreDigestState
69 StoreDigestCBlock cblock
;
70 int rebuild_lock
; /* bucket number */
71 StoreEntry
* rewrite_lock
; /* points to store entry with the digest */
72 StoreSearchPointer theSearch
;
79 int del_count
; /* #store entries deleted from store_digest */
80 int del_lost_count
; /* #store entries not found in store_digest on delete */
81 int add_count
; /* #store entries accepted to store_digest */
82 int add_coll_count
; /* #accepted entries that collided with existing ones */
83 int rej_count
; /* #store entries not accepted to store_digest */
84 int rej_coll_count
; /* #not accepted entries that collided with existing ones */
88 static StoreDigestState sd_state
;
89 static StoreDigestStats sd_stats
;
91 /* local prototypes */
92 static void storeDigestRebuildStart(void *datanotused
);
93 static void storeDigestRebuildResume(void);
94 static void storeDigestRebuildFinish(void);
95 static void storeDigestRebuildStep(void *datanotused
);
96 static void storeDigestRewriteStart(void *);
97 static void storeDigestRewriteResume(void);
98 static void storeDigestRewriteFinish(StoreEntry
* e
);
99 static EVH storeDigestSwapOutStep
;
100 static void storeDigestCBlockSwapOut(StoreEntry
* e
);
101 static int storeDigestCalcCap(void);
102 static int storeDigestResize(void);
103 static void storeDigestAdd(const StoreEntry
*);
105 #endif /* USE_CACHE_DIGESTS */
108 storeDigestRegisterWithCacheManager(void)
110 Mgr::RegisterAction("store_digest", "Store Digest", storeDigestReport
, 0, 1);
118 storeDigestInit(void)
120 storeDigestRegisterWithCacheManager();
122 #if USE_CACHE_DIGESTS
123 const int cap
= storeDigestCalcCap();
125 if (!Config
.onoff
.digest_generation
) {
127 debugs(71, 3, "Local cache digest generation disabled");
131 store_digest
= cacheDigestCreate(cap
, Config
.digest
.bits_per_entry
);
132 debugs(71, DBG_IMPORTANT
, "Local cache digest enabled; rebuild/rewrite every " <<
133 (int) Config
.digest
.rebuild_period
<< "/" <<
134 (int) Config
.digest
.rewrite_period
<< " sec");
136 memset(&sd_state
, 0, sizeof(sd_state
));
139 debugs(71, 3, "Local cache digest is 'off'");
143 /* called when store_rebuild completes */
145 storeDigestNoteStoreReady(void)
147 #if USE_CACHE_DIGESTS
149 if (Config
.onoff
.digest_generation
) {
150 storeDigestRebuildStart(NULL
);
151 storeDigestRewriteStart(NULL
);
157 //TODO: this seems to be dead code. Is it needed?
159 storeDigestDel(const StoreEntry
* entry
)
161 #if USE_CACHE_DIGESTS
163 if (!Config
.onoff
.digest_generation
) {
167 assert(entry
&& store_digest
);
168 debugs(71, 6, "storeDigestDel: checking entry, key: " << entry
->getMD5Text());
170 if (!EBIT_TEST(entry
->flags
, KEY_PRIVATE
)) {
171 if (!cacheDigestTest(store_digest
, (const cache_key
*)entry
->key
)) {
172 ++sd_stats
.del_lost_count
;
173 debugs(71, 6, "storeDigestDel: lost entry, key: " << entry
->getMD5Text() << " url: " << entry
->url() );
175 ++sd_stats
.del_count
;
176 cacheDigestDel(store_digest
, (const cache_key
*)entry
->key
);
177 debugs(71, 6, "storeDigestDel: deled entry, key: " << entry
->getMD5Text());
180 #endif //USE_CACHE_DIGESTS
184 storeDigestReport(StoreEntry
* e
)
186 #if USE_CACHE_DIGESTS
188 if (!Config
.onoff
.digest_generation
) {
193 cacheDigestReport(store_digest
, "store", e
);
194 storeAppendPrintf(e
, "\t added: %d rejected: %d ( %.2f %%) del-ed: %d\n",
197 xpercent(sd_stats
.rej_count
, sd_stats
.rej_count
+ sd_stats
.add_count
),
199 storeAppendPrintf(e
, "\t collisions: on add: %.2f %% on rej: %.2f %%\n",
200 xpercent(sd_stats
.add_coll_count
, sd_stats
.add_count
),
201 xpercent(sd_stats
.rej_coll_count
, sd_stats
.rej_count
));
203 storeAppendPrintf(e
, "store digest: disabled.\n");
206 #endif //USE_CACHE_DIGESTS
213 #if USE_CACHE_DIGESTS
215 /* should we digest this entry? used by storeDigestAdd() */
217 storeDigestAddable(const StoreEntry
* e
)
219 /* add some stats! XXX */
221 debugs(71, 6, "storeDigestAddable: checking entry, key: " << e
->getMD5Text());
223 /* check various entry flags (mimics StoreEntry::checkCachable XXX) */
225 if (EBIT_TEST(e
->flags
, KEY_PRIVATE
)) {
226 debugs(71, 6, "storeDigestAddable: NO: private key");
230 if (EBIT_TEST(e
->flags
, ENTRY_NEGCACHED
)) {
231 debugs(71, 6, "storeDigestAddable: NO: negative cached");
235 if (EBIT_TEST(e
->flags
, RELEASE_REQUEST
)) {
236 debugs(71, 6, "storeDigestAddable: NO: release requested");
240 if (e
->store_status
== STORE_OK
&& EBIT_TEST(e
->flags
, ENTRY_BAD_LENGTH
)) {
241 debugs(71, 6, "storeDigestAddable: NO: wrong content-length");
245 /* do not digest huge objects */
246 if (e
->swap_file_sz
> (uint64_t )Config
.Store
.maxObjectSize
) {
247 debugs(71, 6, "storeDigestAddable: NO: too big");
251 /* still here? check staleness */
252 /* Note: We should use the time of the next rebuild, not (cur_time+period) */
253 if (refreshCheckDigest(e
, Config
.digest
.rebuild_period
)) {
254 debugs(71, 6, "storeDigestAdd: entry expires within " << Config
.digest
.rebuild_period
<< " secs, ignoring");
259 * idea: how about also skipping very fresh (thus, potentially
260 * unstable) entries? Should be configurable through
261 * cd_refresh_pattern, of course.
264 * idea: skip objects that are going to be purged before the next
271 storeDigestAdd(const StoreEntry
* entry
)
273 assert(entry
&& store_digest
);
275 if (storeDigestAddable(entry
)) {
276 ++sd_stats
.add_count
;
278 if (cacheDigestTest(store_digest
, (const cache_key
*)entry
->key
))
279 ++sd_stats
.add_coll_count
;
281 cacheDigestAdd(store_digest
, (const cache_key
*)entry
->key
);
283 debugs(71, 6, "storeDigestAdd: added entry, key: " << entry
->getMD5Text());
285 ++sd_stats
.rej_count
;
287 if (cacheDigestTest(store_digest
, (const cache_key
*)entry
->key
))
288 ++sd_stats
.rej_coll_count
;
292 /* rebuilds digest from scratch */
294 storeDigestRebuildStart(void *datanotused
)
296 assert(store_digest
);
297 /* prevent overlapping if rebuild schedule is too tight */
299 if (sd_state
.rebuild_lock
) {
300 debugs(71, DBG_IMPORTANT
, "storeDigestRebuildStart: overlap detected, consider increasing rebuild period");
304 sd_state
.rebuild_lock
= 1;
305 debugs(71, 2, "storeDigestRebuildStart: rebuild #" << sd_state
.rebuild_count
+ 1);
307 if (sd_state
.rewrite_lock
) {
308 debugs(71, 2, "storeDigestRebuildStart: waiting for Rewrite to finish.");
312 storeDigestRebuildResume();
315 /* called be Rewrite to push Rebuild forward */
317 storeDigestRebuildResume(void)
319 assert(sd_state
.rebuild_lock
);
320 assert(!sd_state
.rewrite_lock
);
321 sd_state
.theSearch
= Store::Root().search(NULL
, NULL
);
322 /* resize or clear */
324 if (!storeDigestResize())
325 cacheDigestClear(store_digest
); /* not clean()! */
327 memset(&sd_stats
, 0, sizeof(sd_stats
));
329 eventAdd("storeDigestRebuildStep", storeDigestRebuildStep
, NULL
, 0.0, 1);
332 /* finishes swap out sequence for the digest; schedules next rebuild */
334 storeDigestRebuildFinish(void)
336 assert(sd_state
.rebuild_lock
);
337 sd_state
.rebuild_lock
= 0;
338 ++sd_state
.rebuild_count
;
339 debugs(71, 2, "storeDigestRebuildFinish: done.");
340 eventAdd("storeDigestRebuildStart", storeDigestRebuildStart
, NULL
, (double)
341 Config
.digest
.rebuild_period
, 1);
342 /* resume pending Rewrite if any */
344 if (sd_state
.rewrite_lock
)
345 storeDigestRewriteResume();
348 /* recalculate a few hash buckets per invocation; schedules next step */
350 storeDigestRebuildStep(void *datanotused
)
352 /* TODO: call Store::Root().size() to determine this.. */
353 int count
= Config
.Store
.objectsPerBucket
* (int) ceil((double) store_hash_buckets
*
354 (double) Config
.digest
.rebuild_chunk_percentage
/ 100.0);
355 assert(sd_state
.rebuild_lock
);
357 debugs(71, 3, "storeDigestRebuildStep: buckets: " << store_hash_buckets
<< " entries to check: " << count
);
359 while (count
-- && !sd_state
.theSearch
->isDone() && sd_state
.theSearch
->next())
360 storeDigestAdd(sd_state
.theSearch
->currentItem());
363 if (sd_state
.theSearch
->isDone())
364 storeDigestRebuildFinish();
366 eventAdd("storeDigestRebuildStep", storeDigestRebuildStep
, NULL
, 0.0, 1);
369 /* starts swap out sequence for the digest */
371 storeDigestRewriteStart(void *datanotused
)
377 assert(store_digest
);
378 /* prevent overlapping if rewrite schedule is too tight */
380 if (sd_state
.rewrite_lock
) {
381 debugs(71, DBG_IMPORTANT
, "storeDigestRewrite: overlap detected, consider increasing rewrite period");
385 debugs(71, 2, "storeDigestRewrite: start rewrite #" << sd_state
.rewrite_count
+ 1);
386 /* make new store entry */
387 url
= internalLocalUri("/squid-internal-periodic/", StoreDigestFileName
);
388 flags
.cachable
= true;
389 e
= storeCreateEntry(url
, url
, flags
, Http::METHOD_GET
);
391 sd_state
.rewrite_lock
= e
;
392 debugs(71, 3, "storeDigestRewrite: url: " << url
<< " key: " << e
->getMD5Text());
393 HttpRequest
*req
= HttpRequest::CreateFromUrl(url
);
394 e
->mem_obj
->request
= req
;
395 HTTPMSGLOCK(e
->mem_obj
->request
);
396 /* wait for rebuild (if any) to finish */
398 if (sd_state
.rebuild_lock
) {
399 debugs(71, 2, "storeDigestRewriteStart: waiting for rebuild to finish.");
403 storeDigestRewriteResume();
407 storeDigestRewriteResume(void)
411 assert(sd_state
.rewrite_lock
);
412 assert(!sd_state
.rebuild_lock
);
413 e
= sd_state
.rewrite_lock
;
414 sd_state
.rewrite_offset
= 0;
415 EBIT_SET(e
->flags
, ENTRY_SPECIAL
);
416 /* setting public key will purge old digest entry if any */
419 HttpReply
*rep
= new HttpReply
;
420 rep
->setHeaders(Http::scOkay
, "Cache Digest OK",
421 "application/cache-digest", (store_digest
->mask_size
+ sizeof(sd_state
.cblock
)),
422 squid_curtime
, (squid_curtime
+ Config
.digest
.rewrite_period
) );
423 debugs(71, 3, "storeDigestRewrite: entry expires on " << rep
->expires
<<
424 " (" << std::showpos
<< (int) (rep
->expires
- squid_curtime
) << ")");
426 e
->replaceHttpReply(rep
);
427 storeDigestCBlockSwapOut(e
);
429 eventAdd("storeDigestSwapOutStep", storeDigestSwapOutStep
, sd_state
.rewrite_lock
, 0.0, 1, false);
432 /* finishes swap out sequence for the digest; schedules next rewrite */
434 storeDigestRewriteFinish(StoreEntry
* e
)
436 assert(e
== sd_state
.rewrite_lock
);
439 debugs(71, 2, "storeDigestRewriteFinish: digest expires at " << e
->expires
<<
440 " (" << std::showpos
<< (int) (e
->expires
- squid_curtime
) << ")");
441 /* is this the write order? @?@ */
442 e
->mem_obj
->unlinkRequest();
443 e
->unlock("storeDigestRewriteFinish");
444 sd_state
.rewrite_lock
= NULL
;
445 ++sd_state
.rewrite_count
;
446 eventAdd("storeDigestRewriteStart", storeDigestRewriteStart
, NULL
, (double)
447 Config
.digest
.rewrite_period
, 1);
448 /* resume pending Rebuild if any */
450 if (sd_state
.rebuild_lock
)
451 storeDigestRebuildResume();
454 /* swaps out one digest "chunk" per invocation; schedules next swap out */
456 storeDigestSwapOutStep(void *data
)
458 StoreEntry
*e
= static_cast<StoreEntry
*>(data
);
459 int chunk_size
= Config
.digest
.swapout_chunk_size
;
460 assert(e
== sd_state
.rewrite_lock
);
462 /* _add_ check that nothing bad happened while we were waiting @?@ @?@ */
464 if (sd_state
.rewrite_offset
+ chunk_size
> store_digest
->mask_size
)
465 chunk_size
= store_digest
->mask_size
- sd_state
.rewrite_offset
;
467 e
->append(store_digest
->mask
+ sd_state
.rewrite_offset
, chunk_size
);
469 debugs(71, 3, "storeDigestSwapOutStep: size: " << store_digest
->mask_size
<<
470 " offset: " << sd_state
.rewrite_offset
<< " chunk: " <<
471 chunk_size
<< " bytes");
473 sd_state
.rewrite_offset
+= chunk_size
;
476 if (sd_state
.rewrite_offset
>= store_digest
->mask_size
)
477 storeDigestRewriteFinish(e
);
479 eventAdd("storeDigestSwapOutStep", storeDigestSwapOutStep
, data
, 0.0, 1, false);
483 storeDigestCBlockSwapOut(StoreEntry
* e
)
485 memset(&sd_state
.cblock
, 0, sizeof(sd_state
.cblock
));
486 sd_state
.cblock
.ver
.current
= htons(CacheDigestVer
.current
);
487 sd_state
.cblock
.ver
.required
= htons(CacheDigestVer
.required
);
488 sd_state
.cblock
.capacity
= htonl(store_digest
->capacity
);
489 sd_state
.cblock
.count
= htonl(store_digest
->count
);
490 sd_state
.cblock
.del_count
= htonl(store_digest
->del_count
);
491 sd_state
.cblock
.mask_size
= htonl(store_digest
->mask_size
);
492 sd_state
.cblock
.bits_per_entry
= (unsigned char)
493 Config
.digest
.bits_per_entry
;
494 sd_state
.cblock
.hash_func_count
= (unsigned char) CacheDigestHashFuncCount
;
495 e
->append((char *) &sd_state
.cblock
, sizeof(sd_state
.cblock
));
498 /* calculates digest capacity */
500 storeDigestCalcCap(void)
503 * To-Do: Bloom proved that the optimal filter utilization is 50% (half of
504 * the bits are off). However, we do not have a formula to calculate the
505 * number of _entries_ we want to pre-allocate for.
507 const int hi_cap
= Store::Root().maxSize() / Config
.Store
.avgObjectSize
;
508 const int lo_cap
= 1 + Store::Root().currentSize() / Config
.Store
.avgObjectSize
;
509 const int e_count
= StoreEntry::inUseCount();
510 int cap
= e_count
? e_count
:hi_cap
;
511 debugs(71, 2, "storeDigestCalcCap: have: " << e_count
<< ", want " << cap
<<
512 " entries; limits: [" << lo_cap
<< ", " << hi_cap
<< "]");
517 /* do not enforce hi_cap limit, average-based estimation may be wrong
524 /* returns true if we actually resized the digest */
526 storeDigestResize(void)
528 const int cap
= storeDigestCalcCap();
530 assert(store_digest
);
531 diff
= abs(cap
- store_digest
->capacity
);
532 debugs(71, 2, "storeDigestResize: " <<
533 store_digest
->capacity
<< " -> " << cap
<< "; change: " <<
534 diff
<< " (" << xpercentInt(diff
, store_digest
->capacity
) << "%)" );
535 /* avoid minor adjustments */
537 if (diff
<= store_digest
->capacity
/ 10) {
538 debugs(71, 2, "storeDigestResize: small change, will not resize.");
541 debugs(71, 2, "storeDigestResize: big change, resizing.");
542 cacheDigestChangeCap(store_digest
, cap
);
547 #endif /* USE_CACHE_DIGESTS */