2 * Copyright (C) 1996-2015 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 20 Store Rebuild Routines */
15 #include "SquidConfig.h"
16 #include "SquidTime.h"
17 #include "StatCounters.h"
19 #include "store_digest.h"
20 #include "store_key_md5.h"
21 #include "store_rebuild.h"
22 #include "StoreSearch.h"
24 // for tvSubDsec() which should be in SquidTime.h
29 static StoreRebuildData counts
;
31 static struct timeval rebuild_start
;
32 static void storeCleanup(void *);
35 /* total number of "swap.state" entries that will be read */
37 /* number of entries read so far */
39 } store_rebuild_progress
;
41 static store_rebuild_progress
*RebuildProgress
= NULL
;
44 storeCleanupDoubleCheck(StoreEntry
* e
)
46 SwapDir
*SD
= dynamic_cast<SwapDir
*>(INDEXSD(e
->swap_dirn
));
47 return (SD
->doubleCheck(*e
));
53 static int store_errors
= 0;
54 static StoreSearchPointer currentSearch
;
55 static int validated
= 0;
58 if (currentSearch
== NULL
|| currentSearch
->isDone())
59 currentSearch
= Store::Root().search(NULL
, NULL
);
61 size_t statCount
= 500;
63 // TODO: Avoid the loop (and ENTRY_VALIDATED) unless opt_store_doublecheck.
64 while (statCount
-- && !currentSearch
->isDone() && currentSearch
->next()) {
67 e
= currentSearch
->currentItem();
71 if (EBIT_TEST(e
->flags
, ENTRY_VALIDATED
))
75 * Calling StoreEntry->release() has no effect because we're
76 * still in 'store_rebuilding' state
78 if (e
->swap_filen
< 0)
81 if (opt_store_doublecheck
)
82 if (storeCleanupDoubleCheck(e
))
85 EBIT_SET(e
->flags
, ENTRY_VALIDATED
);
88 * Only set the file bit if we know its a valid entry
89 * otherwise, set it in the validation procedure
92 if ((++validated
& 0x3FFFF) == 0)
93 /* TODO format the int with with a stream operator */
94 debugs(20, DBG_IMPORTANT
, " " << validated
<< " Entries Validated so far.");
97 if (currentSearch
->isDone()) {
98 debugs(20, 2, "Seen: " << seen
<< " entries");
99 debugs(20, DBG_IMPORTANT
, " Completed Validation Procedure");
100 debugs(20, DBG_IMPORTANT
, " Validated " << validated
<< " Entries");
101 debugs(20, DBG_IMPORTANT
, " store_swap_size = " << Store::Root().currentSize() / 1024.0 << " KB");
102 --StoreController::store_dirs_rebuilding
;
103 assert(0 == StoreController::store_dirs_rebuilding
);
105 if (opt_store_doublecheck
&& store_errors
) {
106 fatalf("Quitting after finding %d cache index inconsistencies. " \
107 "Removing cache index will force its slow rebuild. " \
108 "Removing -S will let Squid start with an inconsistent " \
109 "cache index (at your own risk).\n", store_errors
);
113 storeDigestNoteStoreReady();
115 currentSearch
= NULL
;
117 eventAdd("storeCleanup", storeCleanup
, NULL
, 0.0, 1);
120 /* meta data recreated from disk image in swap directory */
123 storeRebuildComplete(StoreRebuildData
*dc
)
126 counts
.objcount
+= dc
->objcount
;
127 counts
.expcount
+= dc
->expcount
;
128 counts
.scancount
+= dc
->scancount
;
129 counts
.clashcount
+= dc
->clashcount
;
130 counts
.dupcount
+= dc
->dupcount
;
131 counts
.cancelcount
+= dc
->cancelcount
;
132 counts
.invalid
+= dc
->invalid
;
133 counts
.badflags
+= dc
->badflags
;
134 counts
.bad_log_op
+= dc
->bad_log_op
;
135 counts
.zero_object_sz
+= dc
->zero_object_sz
;
137 * When store_dirs_rebuilding == 1, it means we are done reading
138 * or scanning all cache_dirs. Now report the stats and start
139 * the validation (storeCleanup()) thread.
142 if (StoreController::store_dirs_rebuilding
> 1)
145 dt
= tvSubDsec(rebuild_start
, current_time
);
147 debugs(20, DBG_IMPORTANT
, "Finished rebuilding storage from disk.");
148 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << counts
.scancount
<< " Entries scanned");
149 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << counts
.invalid
<< " Invalid entries.");
150 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << counts
.badflags
<< " With invalid flags.");
151 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << counts
.objcount
<< " Objects loaded.");
152 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << counts
.expcount
<< " Objects expired.");
153 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << counts
.cancelcount
<< " Objects cancelled.");
154 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << counts
.dupcount
<< " Duplicate URLs purged.");
155 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << counts
.clashcount
<< " Swapfile clashes avoided.");
156 debugs(20, DBG_IMPORTANT
, " Took "<< std::setw(3)<< std::setprecision(2) << dt
<< " seconds ("<< std::setw(6) <<
157 ((double) counts
.objcount
/ (dt
> 0.0 ? dt
: 1.0)) << " objects/sec).");
158 debugs(20, DBG_IMPORTANT
, "Beginning Validation Procedure");
160 eventAdd("storeCleanup", storeCleanup
, NULL
, 0.0, 1);
162 xfree(RebuildProgress
);
164 RebuildProgress
= NULL
;
168 * this is ugly. We don't actually start any rebuild threads here,
169 * but only initialize counters, etc. The rebuild threads are
170 * actually started by the filesystem "fooDirInit" function.
173 storeRebuildStart(void)
175 memset(&counts
, '\0', sizeof(counts
));
176 rebuild_start
= current_time
;
178 * Note: store_dirs_rebuilding is initialized to 1.
180 * When we parse the configuration and construct each swap dir,
181 * the construction of that raises the rebuild count.
183 * This prevents us from trying to write clean logs until we
184 * finished rebuilding - including after a reconfiguration that opens an
185 * existing swapdir. The corresponding decrement * occurs in
186 * storeCleanup(), when it is finished.
188 RebuildProgress
= (store_rebuild_progress
*)xcalloc(Config
.cacheSwap
.n_configured
,
189 sizeof(store_rebuild_progress
));
193 * A fs-specific rebuild procedure periodically reports its
197 storeRebuildProgress(int sd_index
, int total
, int sofar
)
199 static time_t last_report
= 0;
206 if (sd_index
>= Config
.cacheSwap
.n_configured
)
209 if (NULL
== RebuildProgress
)
212 RebuildProgress
[sd_index
].total
= total
;
214 RebuildProgress
[sd_index
].scanned
= sofar
;
216 if (squid_curtime
- last_report
< 15)
219 for (sd_index
= 0; sd_index
< Config
.cacheSwap
.n_configured
; ++sd_index
) {
220 n
+= (double) RebuildProgress
[sd_index
].scanned
;
221 d
+= (double) RebuildProgress
[sd_index
].total
;
224 debugs(20, DBG_IMPORTANT
, "Store rebuilding is "<< std::setw(4)<< std::setprecision(2) << 100.0 * n
/ d
<< "% complete");
225 last_report
= squid_curtime
;
230 #include "StoreMeta.h"
231 #include "StoreMetaUnpacker.h"
233 struct InitStoreEntry
: public unary_function
<StoreMeta
, void> {
234 InitStoreEntry(StoreEntry
*anEntry
, cache_key
*aKey
):what(anEntry
),index(aKey
) {}
236 void operator()(StoreMeta
const &x
) {
237 switch (x
.getType()) {
240 assert(x
.length
== SQUID_MD5_DIGEST_LENGTH
);
241 memcpy(index
, x
.value
, SQUID_MD5_DIGEST_LENGTH
);
254 tmp
= (struct old_metahdr
*)x
.value
;
255 assert(x
.length
== STORE_HDR_METASIZE_OLD
);
256 what
->timestamp
= tmp
->timestamp
;
257 what
->lastref
= tmp
->lastref
;
258 what
->expires
= tmp
->expires
;
259 what
->lastmod
= tmp
->lastmod
;
260 what
->swap_file_sz
= tmp
->swap_file_sz
;
261 what
->refcount
= tmp
->refcount
;
262 what
->flags
= tmp
->flags
;
265 case STORE_META_STD_LFS
:
266 assert(x
.length
== STORE_HDR_METASIZE
);
267 memcpy(&what
->timestamp
, x
.value
, STORE_HDR_METASIZE
);
280 storeRebuildLoadEntry(int fd
, int diskIndex
, MemBuf
&buf
, StoreRebuildData
&)
285 assert(buf
.hasSpace()); // caller must allocate
287 const int len
= FD_READ_METHOD(fd
, buf
.space(), buf
.spaceSize());
288 ++ statCounter
.syscalls
.disk
.reads
;
290 const int xerrno
= errno
;
291 debugs(47, DBG_IMPORTANT
, "WARNING: cache_dir[" << diskIndex
<< "]: " <<
292 "Ignoring cached entry after meta data read failure: " << xstrerr(xerrno
));
301 storeRebuildParseEntry(MemBuf
&buf
, StoreEntry
&tmpe
, cache_key
*key
,
302 StoreRebuildData
&stats
,
303 uint64_t expectedSize
)
305 int swap_hdr_len
= 0;
306 StoreMetaUnpacker
aBuilder(buf
.content(), buf
.contentSize(), &swap_hdr_len
);
307 if (aBuilder
.isBufferZero()) {
308 debugs(47,5, HERE
<< "skipping empty record.");
312 if (!aBuilder
.isBufferSane()) {
313 debugs(47, DBG_IMPORTANT
, "WARNING: Ignoring malformed cache entry.");
317 StoreMeta
*tlv_list
= aBuilder
.createStoreMeta();
319 debugs(47, DBG_IMPORTANT
, "WARNING: Ignoring cache entry with invalid " <<
324 // TODO: consume parsed metadata?
326 debugs(47,7, "successful swap meta unpacking; swap_file_sz=" << tmpe
.swap_file_sz
);
327 memset(key
, '\0', SQUID_MD5_DIGEST_LENGTH
);
329 InitStoreEntry
visitor(&tmpe
, key
);
330 for_each(*tlv_list
, visitor
);
331 storeSwapTLVFree(tlv_list
);
334 if (storeKeyNull(key
)) {
335 debugs(47, DBG_IMPORTANT
, "WARNING: Ignoring keyless cache entry");
342 if (expectedSize
> 0) {
343 if (tmpe
.swap_file_sz
== 0) {
344 tmpe
.swap_file_sz
= expectedSize
;
345 } else if (tmpe
.swap_file_sz
== (uint64_t)(expectedSize
- swap_hdr_len
)) {
346 tmpe
.swap_file_sz
= expectedSize
;
347 } else if (tmpe
.swap_file_sz
!= expectedSize
) {
348 debugs(47, DBG_IMPORTANT
, "WARNING: Ignoring cache entry due to a " <<
349 "SIZE MISMATCH " << tmpe
.swap_file_sz
<< "!=" << expectedSize
);
352 } else if (tmpe
.swap_file_sz
<= 0) {
353 // if caller cannot handle unknown sizes, it must check after the call.
354 debugs(47, 7, "unknown size: " << tmpe
);
357 if (EBIT_TEST(tmpe
.flags
, KEY_PRIVATE
)) {
366 storeRebuildKeepEntry(const StoreEntry
&tmpe
, const cache_key
*key
, StoreRebuildData
&stats
)
368 /* this needs to become
370 * 2) make synthetic request with headers ?? or otherwise search
371 * for a matching object in the store
372 * TODO FIXME change to new async api
373 * TODO FIXME I think there is a race condition here with the
375 * store A reads in object foo, searchs for it, and finds nothing.
376 * store B reads in object foo, searchs for it, finds nothing.
377 * store A gets called back with nothing, so registers the object
378 * store B gets called back with nothing, so registers the object,
379 * which will conflict when the in core index gets around to scanning
382 * this suggests that rather than searching for duplicates, the
383 * index rebuild should just assume its the most recent accurate
384 * store entry and whoever indexes the stores handles duplicates.
386 if (StoreEntry
*e
= Store::Root().get(key
)) {
388 if (e
->lastref
>= tmpe
.lastref
) {
389 /* key already exists, old entry is newer */
390 /* keep old, ignore new */
393 // For some stores, get() creates/unpacks a store entry. Signal
394 // such stores that we will no longer use the get() result:
395 e
->lock("storeRebuildKeepEntry");
396 e
->unlock("storeRebuildKeepEntry");
400 /* URL already exists, this swapfile not being used */
401 /* junk old, load new */
402 e
->release(); /* release old entry */