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 static time_t last_loud
= 0;
111 if (last_loud
< squid_curtime
- 86400) {
112 debugs(71, DBG_IMPORTANT
, "WARNING: Cache Digest cannot store " << cap
<< " entries. Limiting to " << absolute_max
);
113 last_loud
= squid_curtime
;
115 debugs(71, 3, "WARNING: Cache Digest cannot store " << cap
<< " entries. Limiting to " << absolute_max
);
122 #endif /* USE_CACHE_DIGESTS */
125 storeDigestInit(void)
127 Mgr::RegisterAction("store_digest", "Store Digest", storeDigestReport
, 0, 1);
129 #if USE_CACHE_DIGESTS
130 if (!Config
.onoff
.digest_generation
) {
132 debugs(71, 3, "Local cache digest generation disabled");
136 const uint64_t cap
= storeDigestCalcCap();
137 store_digest
= new CacheDigest(cap
, Config
.digest
.bits_per_entry
);
138 debugs(71, DBG_IMPORTANT
, "Local cache digest enabled; rebuild/rewrite every " <<
139 (int) Config
.digest
.rebuild_period
<< "/" <<
140 (int) Config
.digest
.rewrite_period
<< " sec");
142 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
);
163 //TODO: this seems to be dead code. Is it needed?
165 storeDigestDel(const StoreEntry
* entry
)
167 #if USE_CACHE_DIGESTS
169 if (!Config
.onoff
.digest_generation
) {
173 assert(entry
&& store_digest
);
174 debugs(71, 6, "storeDigestDel: checking entry, key: " << entry
->getMD5Text());
176 if (!EBIT_TEST(entry
->flags
, KEY_PRIVATE
)) {
177 if (!store_digest
->contains(static_cast<const cache_key
*>(entry
->key
))) {
178 ++sd_stats
.del_lost_count
;
179 debugs(71, 6, "storeDigestDel: lost entry, key: " << entry
->getMD5Text() << " url: " << entry
->url() );
181 ++sd_stats
.del_count
;
182 store_digest
->remove(static_cast<const cache_key
*>(entry
->key
));
183 debugs(71, 6, "storeDigestDel: deled entry, key: " << entry
->getMD5Text());
186 #endif //USE_CACHE_DIGESTS
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");
212 #endif //USE_CACHE_DIGESTS
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
, 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 (store_digest
->contains(static_cast<const cache_key
*>(entry
->key
)))
285 ++sd_stats
.add_coll_count
;
287 store_digest
->add(static_cast<const cache_key
*>(entry
->key
));
289 debugs(71, 6, "storeDigestAdd: added entry, key: " << entry
->getMD5Text());
291 ++sd_stats
.rej_count
;
293 if (store_digest
->contains(static_cast<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 /// \returns true if we actually resized the digest
325 const uint64_t cap
= storeDigestCalcCap();
326 assert(store_digest
);
328 if (cap
> store_digest
->capacity
)
329 diff
= cap
- store_digest
->capacity
;
331 diff
= store_digest
->capacity
- cap
;
332 debugs(71, 2, store_digest
->capacity
<< " -> " << cap
<< "; change: " <<
333 diff
<< " (" << xpercentInt(diff
, store_digest
->capacity
) << "%)" );
334 /* avoid minor adjustments */
336 if (diff
<= store_digest
->capacity
/ 10) {
337 debugs(71, 2, "small change, will not resize.");
340 debugs(71, 2, "big change, resizing.");
341 store_digest
->updateCapacity(cap
);
346 /* called be Rewrite to push Rebuild forward */
348 storeDigestRebuildResume(void)
350 assert(sd_state
.rebuild_lock
);
351 assert(!sd_state
.rewrite_lock
);
352 sd_state
.theSearch
= Store::Root().search();
353 /* resize or clear */
355 if (!storeDigestResize())
356 store_digest
->clear(); /* not clean()! */
358 memset(&sd_stats
, 0, sizeof(sd_stats
));
360 eventAdd("storeDigestRebuildStep", storeDigestRebuildStep
, NULL
, 0.0, 1);
363 /* finishes swap out sequence for the digest; schedules next rebuild */
365 storeDigestRebuildFinish(void)
367 assert(sd_state
.rebuild_lock
);
368 sd_state
.rebuild_lock
= 0;
369 ++sd_state
.rebuild_count
;
370 debugs(71, 2, "storeDigestRebuildFinish: done.");
371 eventAdd("storeDigestRebuildStart", storeDigestRebuildStart
, NULL
, (double)
372 Config
.digest
.rebuild_period
, 1);
373 /* resume pending Rewrite if any */
375 if (sd_state
.rewrite_lock
)
376 storeDigestRewriteResume();
379 /* recalculate a few hash buckets per invocation; schedules next step */
381 storeDigestRebuildStep(void *datanotused
)
383 /* TODO: call Store::Root().size() to determine this.. */
384 int count
= Config
.Store
.objectsPerBucket
* (int) ceil((double) store_hash_buckets
*
385 (double) Config
.digest
.rebuild_chunk_percentage
/ 100.0);
386 assert(sd_state
.rebuild_lock
);
388 debugs(71, 3, "storeDigestRebuildStep: buckets: " << store_hash_buckets
<< " entries to check: " << count
);
390 while (count
-- && !sd_state
.theSearch
->isDone() && sd_state
.theSearch
->next())
391 storeDigestAdd(sd_state
.theSearch
->currentItem());
394 if (sd_state
.theSearch
->isDone())
395 storeDigestRebuildFinish();
397 eventAdd("storeDigestRebuildStep", storeDigestRebuildStep
, NULL
, 0.0, 1);
400 /* starts swap out sequence for the digest */
402 storeDigestRewriteStart(void *datanotused
)
408 assert(store_digest
);
409 /* prevent overlapping if rewrite schedule is too tight */
411 if (sd_state
.rewrite_lock
) {
412 debugs(71, DBG_IMPORTANT
, "storeDigestRewrite: overlap detected, consider increasing rewrite period");
416 debugs(71, 2, "storeDigestRewrite: start rewrite #" << sd_state
.rewrite_count
+ 1);
417 /* make new store entry */
418 url
= internalLocalUri("/squid-internal-periodic/", SBuf(StoreDigestFileName
));
419 flags
.cachable
= true;
420 e
= storeCreateEntry(url
, url
, flags
, Http::METHOD_GET
);
422 sd_state
.rewrite_lock
= e
;
423 debugs(71, 3, "storeDigestRewrite: url: " << url
<< " key: " << e
->getMD5Text());
424 HttpRequest
*req
= HttpRequest::CreateFromUrl(url
);
425 e
->mem_obj
->request
= req
;
426 HTTPMSGLOCK(e
->mem_obj
->request
);
427 /* wait for rebuild (if any) to finish */
429 if (sd_state
.rebuild_lock
) {
430 debugs(71, 2, "storeDigestRewriteStart: waiting for rebuild to finish.");
434 storeDigestRewriteResume();
438 storeDigestRewriteResume(void)
442 assert(sd_state
.rewrite_lock
);
443 assert(!sd_state
.rebuild_lock
);
444 e
= sd_state
.rewrite_lock
;
445 sd_state
.rewrite_offset
= 0;
446 EBIT_SET(e
->flags
, ENTRY_SPECIAL
);
447 /* setting public key will purge old digest entry if any */
450 HttpReply
*rep
= new HttpReply
;
451 rep
->setHeaders(Http::scOkay
, "Cache Digest OK",
452 "application/cache-digest", (store_digest
->mask_size
+ sizeof(sd_state
.cblock
)),
453 squid_curtime
, (squid_curtime
+ Config
.digest
.rewrite_period
) );
454 debugs(71, 3, "storeDigestRewrite: entry expires on " << rep
->expires
<<
455 " (" << std::showpos
<< (int) (rep
->expires
- squid_curtime
) << ")");
457 e
->replaceHttpReply(rep
);
458 storeDigestCBlockSwapOut(e
);
460 eventAdd("storeDigestSwapOutStep", storeDigestSwapOutStep
, sd_state
.rewrite_lock
, 0.0, 1, false);
463 /* finishes swap out sequence for the digest; schedules next rewrite */
465 storeDigestRewriteFinish(StoreEntry
* e
)
467 assert(e
== sd_state
.rewrite_lock
);
470 debugs(71, 2, "storeDigestRewriteFinish: digest expires at " << e
->expires
<<
471 " (" << std::showpos
<< (int) (e
->expires
- squid_curtime
) << ")");
472 /* is this the write order? @?@ */
473 e
->mem_obj
->unlinkRequest();
474 e
->unlock("storeDigestRewriteFinish");
475 sd_state
.rewrite_lock
= NULL
;
476 ++sd_state
.rewrite_count
;
477 eventAdd("storeDigestRewriteStart", storeDigestRewriteStart
, NULL
, (double)
478 Config
.digest
.rewrite_period
, 1);
479 /* resume pending Rebuild if any */
481 if (sd_state
.rebuild_lock
)
482 storeDigestRebuildResume();
485 /* swaps out one digest "chunk" per invocation; schedules next swap out */
487 storeDigestSwapOutStep(void *data
)
489 StoreEntry
*e
= static_cast<StoreEntry
*>(data
);
490 int chunk_size
= Config
.digest
.swapout_chunk_size
;
491 assert(e
== sd_state
.rewrite_lock
);
493 /* _add_ check that nothing bad happened while we were waiting @?@ @?@ */
495 if (static_cast<uint32_t>(sd_state
.rewrite_offset
+ chunk_size
) > store_digest
->mask_size
)
496 chunk_size
= store_digest
->mask_size
- sd_state
.rewrite_offset
;
498 e
->append(store_digest
->mask
+ sd_state
.rewrite_offset
, chunk_size
);
500 debugs(71, 3, "storeDigestSwapOutStep: size: " << store_digest
->mask_size
<<
501 " offset: " << sd_state
.rewrite_offset
<< " chunk: " <<
502 chunk_size
<< " bytes");
504 sd_state
.rewrite_offset
+= chunk_size
;
507 if (static_cast<uint32_t>(sd_state
.rewrite_offset
) >= store_digest
->mask_size
)
508 storeDigestRewriteFinish(e
);
510 eventAdd("storeDigestSwapOutStep", storeDigestSwapOutStep
, data
, 0.0, 1, false);
514 storeDigestCBlockSwapOut(StoreEntry
* e
)
516 memset(&sd_state
.cblock
, 0, sizeof(sd_state
.cblock
));
517 sd_state
.cblock
.ver
.current
= htons(CacheDigestVer
.current
);
518 sd_state
.cblock
.ver
.required
= htons(CacheDigestVer
.required
);
519 sd_state
.cblock
.capacity
= htonl(store_digest
->capacity
);
520 sd_state
.cblock
.count
= htonl(store_digest
->count
);
521 sd_state
.cblock
.del_count
= htonl(store_digest
->del_count
);
522 sd_state
.cblock
.mask_size
= htonl(store_digest
->mask_size
);
523 sd_state
.cblock
.bits_per_entry
= Config
.digest
.bits_per_entry
;
524 sd_state
.cblock
.hash_func_count
= (unsigned char) CacheDigestHashFuncCount
;
525 e
->append((char *) &sd_state
.cblock
, sizeof(sd_state
.cblock
));
528 #endif /* USE_CACHE_DIGESTS */