2 * Copyright (C) 1996-2016 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 71 Store Digest Manager */
12 * TODO: We probably do not track all the cases when
13 * storeDigestNoteStoreReady() must be called; this may prevent
14 * storeDigestRebuild/write schedule to be activated
21 #include "mgr/Registration.h"
22 #include "store_digest.h"
25 #include "CacheDigest.h"
26 #include "HttpReply.h"
27 #include "HttpRequest.h"
29 #include "MemObject.h"
30 #include "PeerDigest.h"
32 #include "SquidConfig.h"
33 #include "SquidTime.h"
35 #include "StoreSearch.h"
44 class StoreDigestState
48 StoreDigestCBlock cblock
;
49 int rebuild_lock
; /* bucket number */
50 StoreEntry
* rewrite_lock
; /* points to store entry with the digest */
51 StoreSearchPointer theSearch
;
58 int del_count
; /* #store entries deleted from store_digest */
59 int del_lost_count
; /* #store entries not found in store_digest on delete */
60 int add_count
; /* #store entries accepted to store_digest */
61 int add_coll_count
; /* #accepted entries that collided with existing ones */
62 int rej_count
; /* #store entries not accepted to store_digest */
63 int rej_coll_count
; /* #not accepted entries that collided with existing ones */
67 static StoreDigestState sd_state
;
68 static StoreDigestStats sd_stats
;
70 /* local prototypes */
71 static void storeDigestRebuildStart(void *datanotused
);
72 static void storeDigestRebuildResume(void);
73 static void storeDigestRebuildFinish(void);
74 static void storeDigestRebuildStep(void *datanotused
);
75 static void storeDigestRewriteStart(void *);
76 static void storeDigestRewriteResume(void);
77 static void storeDigestRewriteFinish(StoreEntry
* e
);
78 static EVH storeDigestSwapOutStep
;
79 static void storeDigestCBlockSwapOut(StoreEntry
* e
);
80 static void storeDigestAdd(const StoreEntry
*);
82 /// calculates digest capacity
87 * To-Do: Bloom proved that the optimal filter utilization is 50% (half of
88 * the bits are off). However, we do not have a formula to calculate the
89 * number of _entries_ we want to pre-allocate for.
91 const uint64_t hi_cap
= Store::Root().maxSize() / Config
.Store
.avgObjectSize
;
92 const uint64_t lo_cap
= 1 + Store::Root().currentSize() / Config
.Store
.avgObjectSize
;
93 const uint64_t e_count
= StoreEntry::inUseCount();
94 uint64_t cap
= e_count
? e_count
: hi_cap
;
95 debugs(71, 2, "have: " << e_count
<< ", want " << cap
<<
96 " entries; limits: [" << lo_cap
<< ", " << hi_cap
<< "]");
101 /* do not enforce hi_cap limit, average-based estimation may be wrong
106 // Bug 4534: we still have to set an upper-limit at some reasonable value though.
107 // this matches cacheDigestCalcMaskSize doing (cap*bpe)+7 < INT_MAX
108 const uint64_t absolute_max
= (INT_MAX
-8) / Config
.digest
.bits_per_entry
;
109 if (cap
> absolute_max
) {
110 debugs(71, DBG_CRITICAL
, "WARNING: Cache Digest cannot store " << cap
<< " entries. Limiting to " << absolute_max
);
116 #endif /* USE_CACHE_DIGESTS */
119 storeDigestInit(void)
121 Mgr::RegisterAction("store_digest", "Store Digest", storeDigestReport
, 0, 1);
123 #if USE_CACHE_DIGESTS
124 if (!Config
.onoff
.digest_generation
) {
126 debugs(71, 3, "Local cache digest generation disabled");
130 const uint64_t cap
= storeDigestCalcCap();
131 store_digest
= new CacheDigest(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 (!store_digest
->contains(static_cast<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 store_digest
->remove(static_cast<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 (store_digest
->contains(static_cast<const cache_key
*>(entry
->key
)))
279 ++sd_stats
.add_coll_count
;
281 store_digest
->add(static_cast<const cache_key
*>(entry
->key
));
283 debugs(71, 6, "storeDigestAdd: added entry, key: " << entry
->getMD5Text());
285 ++sd_stats
.rej_count
;
287 if (store_digest
->contains(static_cast<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 /// \returns true if we actually resized the digest
319 const uint64_t cap
= storeDigestCalcCap();
320 assert(store_digest
);
321 uint64_t diff
= abs(cap
- store_digest
->capacity
);
322 debugs(71, 2, store_digest
->capacity
<< " -> " << cap
<< "; change: " <<
323 diff
<< " (" << xpercentInt(diff
, store_digest
->capacity
) << "%)" );
324 /* avoid minor adjustments */
326 if (diff
<= store_digest
->capacity
/ 10) {
327 debugs(71, 2, "small change, will not resize.");
330 debugs(71, 2, "big change, resizing.");
331 store_digest
->updateCapacity(cap
);
336 /* called be Rewrite to push Rebuild forward */
338 storeDigestRebuildResume(void)
340 assert(sd_state
.rebuild_lock
);
341 assert(!sd_state
.rewrite_lock
);
342 sd_state
.theSearch
= Store::Root().search();
343 /* resize or clear */
345 if (!storeDigestResize())
346 store_digest
->clear(); /* not clean()! */
348 memset(&sd_stats
, 0, sizeof(sd_stats
));
350 eventAdd("storeDigestRebuildStep", storeDigestRebuildStep
, NULL
, 0.0, 1);
353 /* finishes swap out sequence for the digest; schedules next rebuild */
355 storeDigestRebuildFinish(void)
357 assert(sd_state
.rebuild_lock
);
358 sd_state
.rebuild_lock
= 0;
359 ++sd_state
.rebuild_count
;
360 debugs(71, 2, "storeDigestRebuildFinish: done.");
361 eventAdd("storeDigestRebuildStart", storeDigestRebuildStart
, NULL
, (double)
362 Config
.digest
.rebuild_period
, 1);
363 /* resume pending Rewrite if any */
365 if (sd_state
.rewrite_lock
)
366 storeDigestRewriteResume();
369 /* recalculate a few hash buckets per invocation; schedules next step */
371 storeDigestRebuildStep(void *datanotused
)
373 /* TODO: call Store::Root().size() to determine this.. */
374 int count
= Config
.Store
.objectsPerBucket
* (int) ceil((double) store_hash_buckets
*
375 (double) Config
.digest
.rebuild_chunk_percentage
/ 100.0);
376 assert(sd_state
.rebuild_lock
);
378 debugs(71, 3, "storeDigestRebuildStep: buckets: " << store_hash_buckets
<< " entries to check: " << count
);
380 while (count
-- && !sd_state
.theSearch
->isDone() && sd_state
.theSearch
->next())
381 storeDigestAdd(sd_state
.theSearch
->currentItem());
384 if (sd_state
.theSearch
->isDone())
385 storeDigestRebuildFinish();
387 eventAdd("storeDigestRebuildStep", storeDigestRebuildStep
, NULL
, 0.0, 1);
390 /* starts swap out sequence for the digest */
392 storeDigestRewriteStart(void *datanotused
)
398 assert(store_digest
);
399 /* prevent overlapping if rewrite schedule is too tight */
401 if (sd_state
.rewrite_lock
) {
402 debugs(71, DBG_IMPORTANT
, "storeDigestRewrite: overlap detected, consider increasing rewrite period");
406 debugs(71, 2, "storeDigestRewrite: start rewrite #" << sd_state
.rewrite_count
+ 1);
407 /* make new store entry */
408 url
= internalLocalUri("/squid-internal-periodic/", SBuf(StoreDigestFileName
));
409 flags
.cachable
= true;
410 e
= storeCreateEntry(url
, url
, flags
, Http::METHOD_GET
);
412 sd_state
.rewrite_lock
= e
;
413 debugs(71, 3, "storeDigestRewrite: url: " << url
<< " key: " << e
->getMD5Text());
414 HttpRequest
*req
= HttpRequest::CreateFromUrl(url
);
415 e
->mem_obj
->request
= req
;
416 HTTPMSGLOCK(e
->mem_obj
->request
);
417 /* wait for rebuild (if any) to finish */
419 if (sd_state
.rebuild_lock
) {
420 debugs(71, 2, "storeDigestRewriteStart: waiting for rebuild to finish.");
424 storeDigestRewriteResume();
428 storeDigestRewriteResume(void)
432 assert(sd_state
.rewrite_lock
);
433 assert(!sd_state
.rebuild_lock
);
434 e
= sd_state
.rewrite_lock
;
435 sd_state
.rewrite_offset
= 0;
436 EBIT_SET(e
->flags
, ENTRY_SPECIAL
);
437 /* setting public key will purge old digest entry if any */
440 HttpReply
*rep
= new HttpReply
;
441 rep
->setHeaders(Http::scOkay
, "Cache Digest OK",
442 "application/cache-digest", (store_digest
->mask_size
+ sizeof(sd_state
.cblock
)),
443 squid_curtime
, (squid_curtime
+ Config
.digest
.rewrite_period
) );
444 debugs(71, 3, "storeDigestRewrite: entry expires on " << rep
->expires
<<
445 " (" << std::showpos
<< (int) (rep
->expires
- squid_curtime
) << ")");
447 e
->replaceHttpReply(rep
);
448 storeDigestCBlockSwapOut(e
);
450 eventAdd("storeDigestSwapOutStep", storeDigestSwapOutStep
, sd_state
.rewrite_lock
, 0.0, 1, false);
453 /* finishes swap out sequence for the digest; schedules next rewrite */
455 storeDigestRewriteFinish(StoreEntry
* e
)
457 assert(e
== sd_state
.rewrite_lock
);
460 debugs(71, 2, "storeDigestRewriteFinish: digest expires at " << e
->expires
<<
461 " (" << std::showpos
<< (int) (e
->expires
- squid_curtime
) << ")");
462 /* is this the write order? @?@ */
463 e
->mem_obj
->unlinkRequest();
464 e
->unlock("storeDigestRewriteFinish");
465 sd_state
.rewrite_lock
= NULL
;
466 ++sd_state
.rewrite_count
;
467 eventAdd("storeDigestRewriteStart", storeDigestRewriteStart
, NULL
, (double)
468 Config
.digest
.rewrite_period
, 1);
469 /* resume pending Rebuild if any */
471 if (sd_state
.rebuild_lock
)
472 storeDigestRebuildResume();
475 /* swaps out one digest "chunk" per invocation; schedules next swap out */
477 storeDigestSwapOutStep(void *data
)
479 StoreEntry
*e
= static_cast<StoreEntry
*>(data
);
480 int chunk_size
= Config
.digest
.swapout_chunk_size
;
481 assert(e
== sd_state
.rewrite_lock
);
483 /* _add_ check that nothing bad happened while we were waiting @?@ @?@ */
485 if (static_cast<uint32_t>(sd_state
.rewrite_offset
+ chunk_size
) > store_digest
->mask_size
)
486 chunk_size
= store_digest
->mask_size
- sd_state
.rewrite_offset
;
488 e
->append(store_digest
->mask
+ sd_state
.rewrite_offset
, chunk_size
);
490 debugs(71, 3, "storeDigestSwapOutStep: size: " << store_digest
->mask_size
<<
491 " offset: " << sd_state
.rewrite_offset
<< " chunk: " <<
492 chunk_size
<< " bytes");
494 sd_state
.rewrite_offset
+= chunk_size
;
497 if (static_cast<uint32_t>(sd_state
.rewrite_offset
) >= store_digest
->mask_size
)
498 storeDigestRewriteFinish(e
);
500 eventAdd("storeDigestSwapOutStep", storeDigestSwapOutStep
, data
, 0.0, 1, false);
504 storeDigestCBlockSwapOut(StoreEntry
* e
)
506 memset(&sd_state
.cblock
, 0, sizeof(sd_state
.cblock
));
507 sd_state
.cblock
.ver
.current
= htons(CacheDigestVer
.current
);
508 sd_state
.cblock
.ver
.required
= htons(CacheDigestVer
.required
);
509 sd_state
.cblock
.capacity
= htonl(store_digest
->capacity
);
510 sd_state
.cblock
.count
= htonl(store_digest
->count
);
511 sd_state
.cblock
.del_count
= htonl(store_digest
->del_count
);
512 sd_state
.cblock
.mask_size
= htonl(store_digest
->mask_size
);
513 sd_state
.cblock
.bits_per_entry
= Config
.digest
.bits_per_entry
;
514 sd_state
.cblock
.hash_func_count
= (unsigned char) CacheDigestHashFuncCount
;
515 e
->append((char *) &sd_state
.cblock
, sizeof(sd_state
.cblock
));
518 #endif /* USE_CACHE_DIGESTS */