store_digest.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3  *
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.
7  */
8 
9 /* DEBUG: section 71 Store Digest Manager */
10 
11 /*
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
15  */
16 
17 #include "squid.h"
18 #include "Debug.h"
19 #include "event.h"
20 #include "globals.h"
21 #include "mgr/Registration.h"
22 #include "store_digest.h"
23 
24 #if USE_CACHE_DIGESTS
25 #include "CacheDigest.h"
26 #include "HttpReply.h"
27 #include "HttpRequest.h"
28 #include "internal.h"
29 #include "MemObject.h"
30 #include "PeerDigest.h"
31 #include "refresh.h"
32 #include "SquidConfig.h"
33 #include "SquidTime.h"
34 #include "Store.h"
35 #include "StoreSearch.h"
36 #include "util.h"
37 
38 #include <cmath>
39 
40 /*
41  * local types
42  */
43 
45 {
46 public:
48  int rebuild_lock = 0;
49  StoreEntry * rewrite_lock = nullptr;
50  StoreEntry * publicEntry = nullptr;
52  int rewrite_offset = 0;
53  int rebuild_count = 0;
54  int rewrite_count = 0;
55 };
56 
58 {
59 public:
60  int del_count = 0; /* #store entries deleted from store_digest */
61  int del_lost_count = 0; /* #store entries not found in store_digest on delete */
62  int add_count = 0; /* #store entries accepted to store_digest */
63  int add_coll_count = 0; /* #accepted entries that collided with existing ones */
64  int rej_count = 0; /* #store entries not accepted to store_digest */
65  int rej_coll_count = 0; /* #not accepted entries that collided with existing ones */
66 };
67 
68 /* local vars */
71 
72 /* local prototypes */
73 static void storeDigestRebuildStart(void *datanotused);
74 static void storeDigestRebuildResume(void);
75 static void storeDigestRebuildFinish(void);
76 static void storeDigestRebuildStep(void *datanotused);
77 static void storeDigestRewriteStart(void *);
78 static void storeDigestRewriteResume(void);
79 static void storeDigestRewriteFinish(StoreEntry * e);
81 static void storeDigestCBlockSwapOut(StoreEntry * e);
82 static void storeDigestAdd(const StoreEntry *);
83 
85 static uint64_t
87 {
88  /*
89  * To-Do: Bloom proved that the optimal filter utilization is 50% (half of
90  * the bits are off). However, we do not have a formula to calculate the
91  * number of _entries_ we want to pre-allocate for.
92  */
93  const uint64_t hi_cap = Store::Root().maxSize() / Config.Store.avgObjectSize;
94  const uint64_t lo_cap = 1 + Store::Root().currentSize() / Config.Store.avgObjectSize;
95  const uint64_t e_count = StoreEntry::inUseCount();
96  uint64_t cap = e_count ? e_count : hi_cap;
97  debugs(71, 2, "have: " << e_count << ", want " << cap <<
98  " entries; limits: [" << lo_cap << ", " << hi_cap << "]");
99 
100  if (cap < lo_cap)
101  cap = lo_cap;
102 
103  /* do not enforce hi_cap limit, average-based estimation may be wrong
104  *if (cap > hi_cap)
105  * cap = hi_cap;
106  */
107 
108  // Bug 4534: we still have to set an upper-limit at some reasonable value though.
109  // this matches cacheDigestCalcMaskSize doing (cap*bpe)+7 < INT_MAX
110  const uint64_t absolute_max = (INT_MAX -8) / Config.digest.bits_per_entry;
111  if (cap > absolute_max) {
112  static time_t last_loud = 0;
113  if (last_loud < squid_curtime - 86400) {
114  debugs(71, DBG_IMPORTANT, "WARNING: Cache Digest cannot store " << cap << " entries. Limiting to " << absolute_max);
115  last_loud = squid_curtime;
116  } else {
117  debugs(71, 3, "WARNING: Cache Digest cannot store " << cap << " entries. Limiting to " << absolute_max);
118  }
119  cap = absolute_max;
120  }
121 
122  return cap;
123 }
124 #endif /* USE_CACHE_DIGESTS */
125 
126 void
128 {
129  Mgr::RegisterAction("store_digest", "Store Digest", storeDigestReport, 0, 1);
130 
131 #if USE_CACHE_DIGESTS
133  store_digest = NULL;
134  debugs(71, 3, "Local cache digest generation disabled");
135  return;
136  }
137 
138  const uint64_t cap = storeDigestCalcCap();
140  debugs(71, DBG_IMPORTANT, "Local cache digest enabled; rebuild/rewrite every " <<
141  (int) Config.digest.rebuild_period << "/" <<
142  (int) Config.digest.rewrite_period << " sec");
143 
145 #else
146  store_digest = NULL;
147  debugs(71, 3, "Local cache digest is 'off'");
148 #endif
149 }
150 
151 /* called when store_rebuild completes */
152 void
154 {
155 #if USE_CACHE_DIGESTS
156 
160  }
161 
162 #endif
163 }
164 
165 //TODO: this seems to be dead code. Is it needed?
166 void
168 {
169 #if USE_CACHE_DIGESTS
170 
172  return;
173  }
174 
175  assert(entry && store_digest);
176  debugs(71, 6, "storeDigestDel: checking entry, key: " << entry->getMD5Text());
177 
178  if (!EBIT_TEST(entry->flags, KEY_PRIVATE)) {
179  if (!store_digest->contains(static_cast<const cache_key *>(entry->key))) {
181  debugs(71, 6, "storeDigestDel: lost entry, key: " << entry->getMD5Text() << " url: " << entry->url() );
182  } else {
184  store_digest->remove(static_cast<const cache_key *>(entry->key));
185  debugs(71, 6, "storeDigestDel: deled entry, key: " << entry->getMD5Text());
186  }
187  }
188 #else
189  (void)entry;
190 #endif //USE_CACHE_DIGESTS
191 }
192 
193 void
195 {
196 #if USE_CACHE_DIGESTS
197 
199  return;
200  }
201 
202  if (store_digest) {
203  static const SBuf label("store");
204  cacheDigestReport(store_digest, label, e);
205  storeAppendPrintf(e, "\t added: %d rejected: %d ( %.2f %%) del-ed: %d\n",
210  storeAppendPrintf(e, "\t collisions: on add: %.2f %% on rej: %.2f %%\n",
213  } else {
214  storeAppendPrintf(e, "store digest: disabled.\n");
215  }
216 #else
217  (void)e;
218 #endif //USE_CACHE_DIGESTS
219 }
220 
221 /*
222  * LOCAL FUNCTIONS
223  */
224 
225 #if USE_CACHE_DIGESTS
226 
227 /* should we digest this entry? used by storeDigestAdd() */
228 static int
230 {
231  /* add some stats! XXX */
232 
233  debugs(71, 6, "storeDigestAddable: checking entry, key: " << e->getMD5Text());
234 
235  /* check various entry flags (mimics StoreEntry::checkCachable XXX) */
236 
237  if (EBIT_TEST(e->flags, KEY_PRIVATE)) {
238  debugs(71, 6, "storeDigestAddable: NO: private key");
239  return 0;
240  }
241 
242  if (EBIT_TEST(e->flags, ENTRY_NEGCACHED)) {
243  debugs(71, 6, "storeDigestAddable: NO: negative cached");
244  return 0;
245  }
246 
247  if (EBIT_TEST(e->flags, RELEASE_REQUEST)) {
248  debugs(71, 6, "storeDigestAddable: NO: release requested");
249  return 0;
250  }
251 
253  debugs(71, 6, "storeDigestAddable: NO: wrong content-length");
254  return 0;
255  }
256 
257  /* do not digest huge objects */
258  if (e->swap_file_sz > (uint64_t )Config.Store.maxObjectSize) {
259  debugs(71, 6, "storeDigestAddable: NO: too big");
260  return 0;
261  }
262 
263  /* still here? check staleness */
264  /* Note: We should use the time of the next rebuild, not (cur_time+period) */
266  debugs(71, 6, "storeDigestAdd: entry expires within " << Config.digest.rebuild_period << " secs, ignoring");
267  return 0;
268  }
269 
270  /*
271  * idea: how about also skipping very fresh (thus, potentially
272  * unstable) entries? Should be configurable through
273  * cd_refresh_pattern, of course.
274  */
275  /*
276  * idea: skip objects that are going to be purged before the next
277  * update.
278  */
279  return 1;
280 }
281 
282 static void
284 {
285  assert(entry && store_digest);
286 
287  if (storeDigestAddable(entry)) {
289 
290  if (store_digest->contains(static_cast<const cache_key *>(entry->key)))
292 
293  store_digest->add(static_cast<const cache_key *>(entry->key));
294 
295  debugs(71, 6, "storeDigestAdd: added entry, key: " << entry->getMD5Text());
296  } else {
298 
299  if (store_digest->contains(static_cast<const cache_key *>(entry->key)))
301  }
302 }
303 
304 /* rebuilds digest from scratch */
305 static void
307 {
309  /* prevent overlapping if rebuild schedule is too tight */
310 
311  if (sd_state.rebuild_lock) {
312  debugs(71, DBG_IMPORTANT, "storeDigestRebuildStart: overlap detected, consider increasing rebuild period");
313  return;
314  }
315 
317  debugs(71, 2, "storeDigestRebuildStart: rebuild #" << sd_state.rebuild_count + 1);
318 
319  if (sd_state.rewrite_lock) {
320  debugs(71, 2, "storeDigestRebuildStart: waiting for Rewrite to finish.");
321  return;
322  }
323 
325 }
326 
328 static bool
330 {
331  const uint64_t cap = storeDigestCalcCap();
333  uint64_t diff;
334  if (cap > store_digest->capacity)
335  diff = cap - store_digest->capacity;
336  else
337  diff = store_digest->capacity - cap;
338  debugs(71, 2, store_digest->capacity << " -> " << cap << "; change: " <<
339  diff << " (" << xpercentInt(diff, store_digest->capacity) << "%)" );
340  /* avoid minor adjustments */
341 
342  if (diff <= store_digest->capacity / 10) {
343  debugs(71, 2, "small change, will not resize.");
344  return false;
345  } else {
346  debugs(71, 2, "big change, resizing.");
348  }
349  return true;
350 }
351 
352 /* called be Rewrite to push Rebuild forward */
353 static void
355 {
359  /* resize or clear */
360 
361  if (!storeDigestResize())
362  store_digest->clear(); /* not clean()! */
363 
365 
366  eventAdd("storeDigestRebuildStep", storeDigestRebuildStep, NULL, 0.0, 1);
367 }
368 
369 /* finishes swap out sequence for the digest; schedules next rebuild */
370 static void
372 {
376  debugs(71, 2, "storeDigestRebuildFinish: done.");
377  eventAdd("storeDigestRebuildStart", storeDigestRebuildStart, NULL, (double)
379  /* resume pending Rewrite if any */
380 
383 }
384 
385 /* recalculate a few hash buckets per invocation; schedules next step */
386 static void
388 {
389  /* TODO: call Store::Root().size() to determine this.. */
390  int count = Config.Store.objectsPerBucket * (int) ceil((double) store_hash_buckets *
393 
394  debugs(71, 3, "storeDigestRebuildStep: buckets: " << store_hash_buckets << " entries to check: " << count);
395 
396  while (count-- && !sd_state.theSearch->isDone() && sd_state.theSearch->next())
398 
399  /* are we done ? */
400  if (sd_state.theSearch->isDone())
402  else
403  eventAdd("storeDigestRebuildStep", storeDigestRebuildStep, NULL, 0.0, 1);
404 }
405 
406 /* starts swap out sequence for the digest */
407 static void
409 {
411  /* prevent overlapping if rewrite schedule is too tight */
412 
413  if (sd_state.rewrite_lock) {
414  debugs(71, DBG_IMPORTANT, "storeDigestRewrite: overlap detected, consider increasing rewrite period");
415  return;
416  }
417 
418  debugs(71, 2, "storeDigestRewrite: start rewrite #" << sd_state.rewrite_count + 1);
419 
420  const char *url = internalLocalUri("/squid-internal-periodic/", SBuf(StoreDigestFileName));
422  auto req = HttpRequest::FromUrlXXX(url, mx);
423 
424  RequestFlags flags;
425  flags.cachable = true;
426 
427  StoreEntry *e = storeCreateEntry(url, url, flags, Http::METHOD_GET);
428  assert(e);
430  debugs(71, 3, "storeDigestRewrite: url: " << url << " key: " << e->getMD5Text());
431  e->mem_obj->request = req;
432 
433  /* wait for rebuild (if any) to finish */
434  if (sd_state.rebuild_lock) {
435  debugs(71, 2, "storeDigestRewriteStart: waiting for rebuild to finish.");
436  return;
437  }
438 
440 }
441 
442 static void
444 {
445  StoreEntry *e;
446 
452  /* setting public key will mark the old digest entry for removal once unlocked */
453  e->setPublicKey();
454  if (const auto oldEntry = sd_state.publicEntry) {
455  oldEntry->release(true);
456  sd_state.publicEntry = nullptr;
457  oldEntry->unlock("storeDigestRewriteResume");
458  }
459  assert(e->locked());
460  sd_state.publicEntry = e;
461  /* fake reply */
462  HttpReply *rep = new HttpReply;
463  rep->setHeaders(Http::scOkay, "Cache Digest OK",
464  "application/cache-digest", (store_digest->mask_size + sizeof(sd_state.cblock)),
466  debugs(71, 3, "storeDigestRewrite: entry expires on " << rep->expires <<
467  " (" << std::showpos << (int) (rep->expires - squid_curtime) << ")");
468  e->buffer();
469  e->replaceHttpReply(rep);
471  e->flush();
472  eventAdd("storeDigestSwapOutStep", storeDigestSwapOutStep, sd_state.rewrite_lock, 0.0, 1, false);
473 }
474 
475 /* finishes swap out sequence for the digest; schedules next rewrite */
476 static void
478 {
480  e->complete();
481  e->timestampsSet();
482  debugs(71, 2, "storeDigestRewriteFinish: digest expires at " << e->expires <<
483  " (" << std::showpos << (int) (e->expires - squid_curtime) << ")");
484  /* is this the write order? @?@ */
485  e->mem_obj->unlinkRequest();
488  eventAdd("storeDigestRewriteStart", storeDigestRewriteStart, NULL, (double)
490  /* resume pending Rebuild if any */
491 
494 }
495 
496 /* swaps out one digest "chunk" per invocation; schedules next swap out */
497 static void
499 {
500  StoreEntry *e = static_cast<StoreEntry *>(data);
501  int chunk_size = Config.digest.swapout_chunk_size;
503  assert(e);
504  /* _add_ check that nothing bad happened while we were waiting @?@ @?@ */
505 
506  if (static_cast<uint32_t>(sd_state.rewrite_offset + chunk_size) > store_digest->mask_size)
508 
509  e->append(store_digest->mask + sd_state.rewrite_offset, chunk_size);
510 
511  debugs(71, 3, "storeDigestSwapOutStep: size: " << store_digest->mask_size <<
512  " offset: " << sd_state.rewrite_offset << " chunk: " <<
513  chunk_size << " bytes");
514 
515  sd_state.rewrite_offset += chunk_size;
516 
517  /* are we done ? */
518  if (static_cast<uint32_t>(sd_state.rewrite_offset) >= store_digest->mask_size)
520  else
521  eventAdd("storeDigestSwapOutStep", storeDigestSwapOutStep, data, 0.0, 1, false);
522 }
523 
524 static void
526 {
527  memset(&sd_state.cblock, 0, sizeof(sd_state.cblock));
536  e->append((char *) &sd_state.cblock, sizeof(sd_state.cblock));
537 }
538 
539 #endif /* USE_CACHE_DIGESTS */
540 
unsigned char bits_per_entry
Definition: PeerDigest.h:34
struct SquidConfig::@119 digest
SQUIDCEXTERN double xpercent(double part, double whole)
Definition: util.c:54
virtual void buffer()
Definition: store.cc:1618
static void storeDigestRebuildStep(void *datanotused)
unsigned char cache_key
Store key.
Definition: forward.h:29
#define EBIT_SET(flag, bit)
Definition: defines.h:67
uint64_t capacity
Definition: CacheDigest.h:57
virtual void flush()
Definition: store.cc:1629
MemObject * mem_obj
Definition: Store.h:222
time_t rewrite_period
Definition: SquidConfig.h:496
static int storeDigestAddable(const StoreEntry *e)
void clear()
reset the digest mask and counters
Definition: CacheDigest.cc:79
const char * url() const
Definition: store.cc:1583
@ KEY_PRIVATE
Definition: enums.h:102
time_t rebuild_period
Definition: SquidConfig.h:495
StoreDigestCBlock cblock
Definition: store_digest.cc:47
void storeAppendPrintf(StoreEntry *e, const char *fmt,...)
Definition: store.cc:869
StoreSearchPointer theSearch
Definition: store_digest.cc:51
Definition: SBuf.h:87
StoreEntry * publicEntry
points to the previous store entry with the digest
Definition: store_digest.cc:50
int objectsPerBucket
Definition: SquidConfig.h:269
bool setPublicKey(const KeyScope keyScope=ksDefault)
Definition: store.cc:589
void updateCapacity(uint64_t newCapacity)
changes mask size to fit newCapacity, resets bits to 0
Definition: CacheDigest.cc:86
uint16_t flags
Definition: Store.h:233
static StoreDigestState sd_state
Definition: store_digest.cc:69
time_t expires
Definition: Store.h:227
void replaceHttpReply(const HttpReplyPointer &, const bool andStartWriting=true)
Definition: store.cc:1722
#define DBG_IMPORTANT
Definition: Debug.h:41
void remove(const cache_key *key)
Definition: CacheDigest.cc:140
@ ENTRY_BAD_LENGTH
Definition: enums.h:114
@ initCacheDigest
Cache Digest fetching code.
const char * StoreDigestFileName
time_t expires
Definition: HttpReply.h:44
#define NULL
Definition: types.h:166
void cacheDigestReport(CacheDigest *cd, const SBuf &label, StoreEntry *e)
Definition: CacheDigest.cc:245
static void storeDigestRewriteFinish(StoreEntry *e)
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
short int required
Definition: PeerDigest.h:21
int bits_per_entry
Definition: SquidConfig.h:494
void storeDigestDel(const StoreEntry *entry)
#define INT_MAX
Definition: types.h:76
#define EBIT_TEST(flag, bit)
Definition: defines.h:69
@ RELEASE_REQUEST
prohibits making the key public
Definition: enums.h:98
int unlock(const char *context)
Definition: store.cc:483
static void storeDigestRewriteResume(void)
static size_t inUseCount()
Definition: store.cc:197
store_status_t store_status
Definition: Store.h:245
#define assert(EX)
Definition: assert.h:19
void storeDigestNoteStoreReady(void)
virtual void next(void(callback)(void *cbdata), void *cbdata)=0
Version const CacheDigestVer
Definition: peer_digest.cc:56
void EVH void double
Definition: stub_event.cc:16
char * mask
Definition: CacheDigest.h:58
virtual bool isDone() const =0
virtual StoreEntry * currentItem()=0
uint64_t del_count
Definition: CacheDigest.h:56
@ ENTRY_SPECIAL
Definition: enums.h:84
StoreSearch * search()
Definition: Controller.cc:211
static EVH storeDigestSwapOutStep
Definition: store_digest.cc:80
@ STORE_OK
Definition: enums.h:50
time_t squid_curtime
Definition: stub_time.cc:17
StoreEntry * storeCreateEntry(const char *url, const char *logUrl, const RequestFlags &flags, const HttpRequestMethod &method)
Definition: store.cc:773
static bool storeDigestResize()
void storeDigestReport(StoreEntry *e)
void EVH(void *)
Definition: event.h:18
static void storeDigestRebuildResume(void)
virtual uint64_t currentSize() const override
current size
Definition: Controller.cc:173
int rebuild_lock
bucket number
Definition: store_digest.cc:48
virtual uint64_t maxSize() const override
Definition: Controller.cc:159
void complete()
Definition: store.cc:1045
void add(const cache_key *key)
Definition: CacheDigest.cc:107
short int current
Definition: PeerDigest.h:20
struct SquidConfig::@111 onoff
bool timestampsSet()
Definition: store.cc:1410
struct SquidConfig::@109 Store
static uint64_t storeDigestCalcCap()
calculates digest capacity
Definition: store_digest.cc:86
void unlinkRequest()
Definition: MemObject.h:55
static HttpRequest * FromUrlXXX(const char *url, const MasterXaction::Pointer &, const HttpRequestMethod &method=Http::METHOD_GET)
Definition: HttpRequest.cc:529
uint64_t swap_file_sz
Definition: Store.h:231
bool contains(const cache_key *key) const
Definition: CacheDigest.cc:93
int refreshCheckDigest(const StoreEntry *entry, time_t delta)
Definition: refresh.cc:621
CacheDigest * store_digest
int digest_generation
Definition: SquidConfig.h:316
int64_t maxObjectSize
Definition: SquidConfig.h:271
int64_t avgObjectSize
Definition: SquidConfig.h:270
unsigned char hash_func_count
Definition: PeerDigest.h:35
int rebuild_chunk_percentage
Definition: SquidConfig.h:498
int store_hash_buckets
virtual void append(char const *, int)
Appends a c-string to existing packed data.
Definition: store.cc:817
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition: HttpReply.cc:168
@ ENTRY_NEGCACHED
Definition: enums.h:112
static void storeDigestRebuildFinish(void)
int locked() const
Definition: Store.h:145
uint32_t mask_size
Definition: CacheDigest.h:59
@ scOkay
Definition: StatusCode.h:26
void RegisterAction(char const *action, char const *desc, OBJH *handler, int pw_req_flag, int atomic)
Definition: Registration.cc:16
@ METHOD_GET
Definition: MethodType.h:25
static void storeDigestCBlockSwapOut(StoreEntry *e)
uint64_t count
Definition: CacheDigest.h:55
char * internalLocalUri(const char *dir, const SBuf &name)
Definition: internal.cc:131
size_t swapout_chunk_size
Definition: SquidConfig.h:497
static StoreDigestStats sd_stats
Definition: store_digest.cc:70
SQUIDCEXTERN int xpercentInt(double part, double whole)
Definition: util.c:60
HttpRequestPointer request
Definition: MemObject.h:188
int CacheDigestHashFuncCount
const char * getMD5Text() const
Definition: store.cc:205
static void storeDigestRewriteStart(void *)
void eventAdd(const char *name, EVH *func, void *arg, double when, int weight, bool cbdata)
Definition: event.cc:108
void storeDigestInit(void)
class SquidConfig Config
Definition: SquidConfig.cc:12
int unsigned int
Definition: stub_fd.cc:19
static void storeDigestRebuildStart(void *datanotused)
static void storeDigestAdd(const StoreEntry *)
StoreEntry * rewrite_lock
points to store entry with the digest
Definition: store_digest.cc:49
Controller & Root()
safely access controller singleton
Definition: Controller.cc:934

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors