]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_digest.cc
Several fixes and improvements to help collapsed forwarding work reliably:
[thirdparty/squid.git] / src / store_digest.cc
1 /*
2 * DEBUG: section 71 Store Digest Manager
3 * AUTHOR: Alex Rousskov
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
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.
16 *
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.
21 *
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.
26 *
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.
30 *
31 */
32
33 /*
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
37 */
38
39 #include "squid.h"
40 #include "Debug.h"
41 #include "event.h"
42 #include "globals.h"
43 #include "mgr/Registration.h"
44 #include "store_digest.h"
45
46 #if USE_CACHE_DIGESTS
47 #include "CacheDigest.h"
48 #include "HttpReply.h"
49 #include "HttpRequest.h"
50 #include "internal.h"
51 #include "MemObject.h"
52 #include "PeerDigest.h"
53 #include "refresh.h"
54 #include "SquidConfig.h"
55 #include "SquidTime.h"
56 #include "Store.h"
57 #include "StoreSearch.h"
58
59 #if HAVE_MATH_H
60 #include <math.h>
61 #endif
62
63 /*
64 * local types
65 */
66
67 class StoreDigestState
68 {
69
70 public:
71 StoreDigestCBlock cblock;
72 int rebuild_lock; /* bucket number */
73 StoreEntry * rewrite_lock; /* points to store entry with the digest */
74 StoreSearchPointer theSearch;
75 int rewrite_offset;
76 int rebuild_count;
77 int rewrite_count;
78 };
79
80 typedef struct {
81 int del_count; /* #store entries deleted from store_digest */
82 int del_lost_count; /* #store entries not found in store_digest on delete */
83 int add_count; /* #store entries accepted to store_digest */
84 int add_coll_count; /* #accepted entries that collided with existing ones */
85 int rej_count; /* #store entries not accepted to store_digest */
86 int rej_coll_count; /* #not accepted entries that collided with existing ones */
87 } StoreDigestStats;
88
89 /* local vars */
90 static StoreDigestState sd_state;
91 static StoreDigestStats sd_stats;
92
93 /* local prototypes */
94 static void storeDigestRebuildStart(void *datanotused);
95 static void storeDigestRebuildResume(void);
96 static void storeDigestRebuildFinish(void);
97 static void storeDigestRebuildStep(void *datanotused);
98 static void storeDigestRewriteStart(void *);
99 static void storeDigestRewriteResume(void);
100 static void storeDigestRewriteFinish(StoreEntry * e);
101 static EVH storeDigestSwapOutStep;
102 static void storeDigestCBlockSwapOut(StoreEntry * e);
103 static int storeDigestCalcCap(void);
104 static int storeDigestResize(void);
105 static void storeDigestAdd(const StoreEntry *);
106
107 #endif /* USE_CACHE_DIGESTS */
108
109 static void
110 storeDigestRegisterWithCacheManager(void)
111 {
112 Mgr::RegisterAction("store_digest", "Store Digest", storeDigestReport, 0, 1);
113 }
114
115 /*
116 * PUBLIC FUNCTIONS
117 */
118
119 void
120 storeDigestInit(void)
121 {
122 storeDigestRegisterWithCacheManager();
123
124 #if USE_CACHE_DIGESTS
125 const int cap = storeDigestCalcCap();
126
127 if (!Config.onoff.digest_generation) {
128 store_digest = NULL;
129 debugs(71, 3, "Local cache digest generation disabled");
130 return;
131 }
132
133 store_digest = cacheDigestCreate(cap, Config.digest.bits_per_entry);
134 debugs(71, DBG_IMPORTANT, "Local cache digest enabled; rebuild/rewrite every " <<
135 (int) Config.digest.rebuild_period << "/" <<
136 (int) Config.digest.rewrite_period << " sec");
137
138 memset(&sd_state, 0, sizeof(sd_state));
139 #else
140 store_digest = NULL;
141 debugs(71, 3, "Local cache digest is 'off'");
142 #endif
143 }
144
145 /* called when store_rebuild completes */
146 void
147 storeDigestNoteStoreReady(void)
148 {
149 #if USE_CACHE_DIGESTS
150
151 if (Config.onoff.digest_generation) {
152 storeDigestRebuildStart(NULL);
153 storeDigestRewriteStart(NULL);
154 }
155
156 #endif
157 }
158
159 //TODO: this seems to be dead code. Is it needed?
160 void
161 storeDigestDel(const StoreEntry * entry)
162 {
163 #if USE_CACHE_DIGESTS
164
165 if (!Config.onoff.digest_generation) {
166 return;
167 }
168
169 assert(entry && store_digest);
170 debugs(71, 6, "storeDigestDel: checking entry, key: " << entry->getMD5Text());
171
172 if (!EBIT_TEST(entry->flags, KEY_PRIVATE)) {
173 if (!cacheDigestTest(store_digest, (const cache_key *)entry->key)) {
174 ++sd_stats.del_lost_count;
175 debugs(71, 6, "storeDigestDel: lost entry, key: " << entry->getMD5Text() << " url: " << entry->url() );
176 } else {
177 ++sd_stats.del_count;
178 cacheDigestDel(store_digest, (const cache_key *)entry->key);
179 debugs(71, 6, "storeDigestDel: deled entry, key: " << entry->getMD5Text());
180 }
181 }
182 #endif //USE_CACHE_DIGESTS
183 }
184
185 void
186 storeDigestReport(StoreEntry * e)
187 {
188 #if USE_CACHE_DIGESTS
189
190 if (!Config.onoff.digest_generation) {
191 return;
192 }
193
194 if (store_digest) {
195 cacheDigestReport(store_digest, "store", e);
196 storeAppendPrintf(e, "\t added: %d rejected: %d ( %.2f %%) del-ed: %d\n",
197 sd_stats.add_count,
198 sd_stats.rej_count,
199 xpercent(sd_stats.rej_count, sd_stats.rej_count + sd_stats.add_count),
200 sd_stats.del_count);
201 storeAppendPrintf(e, "\t collisions: on add: %.2f %% on rej: %.2f %%\n",
202 xpercent(sd_stats.add_coll_count, sd_stats.add_count),
203 xpercent(sd_stats.rej_coll_count, sd_stats.rej_count));
204 } else {
205 storeAppendPrintf(e, "store digest: disabled.\n");
206 }
207
208 #endif //USE_CACHE_DIGESTS
209 }
210
211 /*
212 * LOCAL FUNCTIONS
213 */
214
215 #if USE_CACHE_DIGESTS
216
217 /* should we digest this entry? used by storeDigestAdd() */
218 static int
219 storeDigestAddable(const StoreEntry * e)
220 {
221 /* add some stats! XXX */
222
223 debugs(71, 6, "storeDigestAddable: checking entry, key: " << e->getMD5Text());
224
225 /* check various entry flags (mimics StoreEntry::checkCachable XXX) */
226
227 if (EBIT_TEST(e->flags, KEY_PRIVATE)) {
228 debugs(71, 6, "storeDigestAddable: NO: private key");
229 return 0;
230 }
231
232 if (EBIT_TEST(e->flags, ENTRY_NEGCACHED)) {
233 debugs(71, 6, "storeDigestAddable: NO: negative cached");
234 return 0;
235 }
236
237 if (EBIT_TEST(e->flags, RELEASE_REQUEST)) {
238 debugs(71, 6, "storeDigestAddable: NO: release requested");
239 return 0;
240 }
241
242 if (e->store_status == STORE_OK && EBIT_TEST(e->flags, ENTRY_BAD_LENGTH)) {
243 debugs(71, 6, "storeDigestAddable: NO: wrong content-length");
244 return 0;
245 }
246
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");
250 return 0;
251 }
252
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");
257 return 0;
258 }
259
260 /*
261 * idea: how about also skipping very fresh (thus, potentially
262 * unstable) entries? Should be configurable through
263 * cd_refresh_pattern, of course.
264 */
265 /*
266 * idea: skip objects that are going to be purged before the next
267 * update.
268 */
269 return 1;
270 }
271
272 static void
273 storeDigestAdd(const StoreEntry * entry)
274 {
275 assert(entry && store_digest);
276
277 if (storeDigestAddable(entry)) {
278 ++sd_stats.add_count;
279
280 if (cacheDigestTest(store_digest, (const cache_key *)entry->key))
281 ++sd_stats.add_coll_count;
282
283 cacheDigestAdd(store_digest, (const cache_key *)entry->key);
284
285 debugs(71, 6, "storeDigestAdd: added entry, key: " << entry->getMD5Text());
286 } else {
287 ++sd_stats.rej_count;
288
289 if (cacheDigestTest(store_digest, (const cache_key *)entry->key))
290 ++sd_stats.rej_coll_count;
291 }
292 }
293
294 /* rebuilds digest from scratch */
295 static void
296 storeDigestRebuildStart(void *datanotused)
297 {
298 assert(store_digest);
299 /* prevent overlapping if rebuild schedule is too tight */
300
301 if (sd_state.rebuild_lock) {
302 debugs(71, DBG_IMPORTANT, "storeDigestRebuildStart: overlap detected, consider increasing rebuild period");
303 return;
304 }
305
306 sd_state.rebuild_lock = 1;
307 debugs(71, 2, "storeDigestRebuildStart: rebuild #" << sd_state.rebuild_count + 1);
308
309 if (sd_state.rewrite_lock) {
310 debugs(71, 2, "storeDigestRebuildStart: waiting for Rewrite to finish.");
311 return;
312 }
313
314 storeDigestRebuildResume();
315 }
316
317 /* called be Rewrite to push Rebuild forward */
318 static void
319 storeDigestRebuildResume(void)
320 {
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 */
325
326 if (!storeDigestResize())
327 cacheDigestClear(store_digest); /* not clean()! */
328
329 memset(&sd_stats, 0, sizeof(sd_stats));
330
331 eventAdd("storeDigestRebuildStep", storeDigestRebuildStep, NULL, 0.0, 1);
332 }
333
334 /* finishes swap out sequence for the digest; schedules next rebuild */
335 static void
336 storeDigestRebuildFinish(void)
337 {
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 */
345
346 if (sd_state.rewrite_lock)
347 storeDigestRewriteResume();
348 }
349
350 /* recalculate a few hash buckets per invocation; schedules next step */
351 static void
352 storeDigestRebuildStep(void *datanotused)
353 {
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);
358
359 debugs(71, 3, "storeDigestRebuildStep: buckets: " << store_hash_buckets << " entries to check: " << count);
360
361 while (count-- && !sd_state.theSearch->isDone() && sd_state.theSearch->next())
362 storeDigestAdd(sd_state.theSearch->currentItem());
363
364 /* are we done ? */
365 if (sd_state.theSearch->isDone())
366 storeDigestRebuildFinish();
367 else
368 eventAdd("storeDigestRebuildStep", storeDigestRebuildStep, NULL, 0.0, 1);
369 }
370
371 /* starts swap out sequence for the digest */
372 static void
373 storeDigestRewriteStart(void *datanotused)
374 {
375 RequestFlags flags;
376 char *url;
377 StoreEntry *e;
378
379 assert(store_digest);
380 /* prevent overlapping if rewrite schedule is too tight */
381
382 if (sd_state.rewrite_lock) {
383 debugs(71, DBG_IMPORTANT, "storeDigestRewrite: overlap detected, consider increasing rewrite period");
384 return;
385 }
386
387 debugs(71, 2, "storeDigestRewrite: start rewrite #" << sd_state.rewrite_count + 1);
388 /* make new store entry */
389 url = internalLocalUri("/squid-internal-periodic/", StoreDigestFileName);
390 flags.cachable = true;
391 e = storeCreateEntry(url, url, flags, Http::METHOD_GET);
392 assert(e);
393 sd_state.rewrite_lock = e;
394 debugs(71, 3, "storeDigestRewrite: url: " << url << " key: " << e->getMD5Text());
395 HttpRequest *req = HttpRequest::CreateFromUrl(url);
396 e->mem_obj->request = req;
397 HTTPMSGLOCK(e->mem_obj->request);
398 /* wait for rebuild (if any) to finish */
399
400 if (sd_state.rebuild_lock) {
401 debugs(71, 2, "storeDigestRewriteStart: waiting for rebuild to finish.");
402 return;
403 }
404
405 storeDigestRewriteResume();
406 }
407
408 static void
409 storeDigestRewriteResume(void)
410 {
411 StoreEntry *e;
412
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 */
419 e->setPublicKey();
420 /* fake reply */
421 HttpReply *rep = new HttpReply;
422 rep->setHeaders(Http::scOkay, "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) << ")");
427 e->buffer();
428 e->replaceHttpReply(rep);
429 storeDigestCBlockSwapOut(e);
430 e->flush();
431 eventAdd("storeDigestSwapOutStep", storeDigestSwapOutStep, sd_state.rewrite_lock, 0.0, 1, false);
432 }
433
434 /* finishes swap out sequence for the digest; schedules next rewrite */
435 static void
436 storeDigestRewriteFinish(StoreEntry * e)
437 {
438 assert(e == sd_state.rewrite_lock);
439 e->complete();
440 e->timestampsSet();
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();
445 e->unlock();
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 */
451
452 if (sd_state.rebuild_lock)
453 storeDigestRebuildResume();
454 }
455
456 /* swaps out one digest "chunk" per invocation; schedules next swap out */
457 static void
458 storeDigestSwapOutStep(void *data)
459 {
460 StoreEntry *e = static_cast<StoreEntry *>(data);
461 int chunk_size = Config.digest.swapout_chunk_size;
462 assert(e == sd_state.rewrite_lock);
463 assert(e);
464 /* _add_ check that nothing bad happened while we were waiting @?@ @?@ */
465
466 if (sd_state.rewrite_offset + chunk_size > store_digest->mask_size)
467 chunk_size = store_digest->mask_size - sd_state.rewrite_offset;
468
469 e->append(store_digest->mask + sd_state.rewrite_offset, chunk_size);
470
471 debugs(71, 3, "storeDigestSwapOutStep: size: " << store_digest->mask_size <<
472 " offset: " << sd_state.rewrite_offset << " chunk: " <<
473 chunk_size << " bytes");
474
475 sd_state.rewrite_offset += chunk_size;
476
477 /* are we done ? */
478 if (sd_state.rewrite_offset >= store_digest->mask_size)
479 storeDigestRewriteFinish(e);
480 else
481 eventAdd("storeDigestSwapOutStep", storeDigestSwapOutStep, data, 0.0, 1, false);
482 }
483
484 static void
485 storeDigestCBlockSwapOut(StoreEntry * e)
486 {
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));
498 }
499
500 /* calculates digest capacity */
501 static int
502 storeDigestCalcCap(void)
503 {
504 /*
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.
508 */
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 << "]");
515
516 if (cap < lo_cap)
517 cap = lo_cap;
518
519 /* do not enforce hi_cap limit, average-based estimation may be wrong
520 *if (cap > hi_cap)
521 * cap = hi_cap;
522 */
523 return cap;
524 }
525
526 /* returns true if we actually resized the digest */
527 static int
528 storeDigestResize(void)
529 {
530 const int cap = storeDigestCalcCap();
531 int diff;
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 */
538
539 if (diff <= store_digest->capacity / 10) {
540 debugs(71, 2, "storeDigestResize: small change, will not resize.");
541 return 0;
542 } else {
543 debugs(71, 2, "storeDigestResize: big change, resizing.");
544 cacheDigestChangeCap(store_digest, cap);
545 return 1;
546 }
547 }
548
549 #endif /* USE_CACHE_DIGESTS */