2 * DEBUG: section 20 Store Rebuild Routines
3 * AUTHOR: Duane Wessels
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
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.
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.
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.
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.
37 #include "StatCounters.h"
39 #include "store_key_md5.h"
41 #include "store_digest.h"
42 #include "store_rebuild.h"
43 #include "StoreSearch.h"
44 #include "SquidConfig.h"
45 #include "SquidTime.h"
50 static StoreRebuildData counts
;
52 static struct timeval rebuild_start
;
53 static void storeCleanup(void *);
56 /* total number of "swap.state" entries that will be read */
58 /* number of entries read so far */
60 } store_rebuild_progress
;
62 static store_rebuild_progress
*RebuildProgress
= NULL
;
65 storeCleanupDoubleCheck(StoreEntry
* e
)
67 SwapDir
*SD
= dynamic_cast<SwapDir
*>(INDEXSD(e
->swap_dirn
));
68 return (SD
->doubleCheck(*e
));
72 storeCleanup(void *datanotused
)
74 static int store_errors
= 0;
75 static StoreSearchPointer currentSearch
;
76 static int validated
= 0;
78 if (currentSearch
== NULL
|| currentSearch
->isDone())
79 currentSearch
= Store::Root().search(NULL
, NULL
);
81 size_t statCount
= 500;
83 // TODO: Avoid the loop (and ENTRY_VALIDATED) unless opt_store_doublecheck.
84 while (statCount
-- && !currentSearch
->isDone() && currentSearch
->next()) {
87 e
= currentSearch
->currentItem();
89 if (EBIT_TEST(e
->flags
, ENTRY_VALIDATED
))
93 * Calling StoreEntry->release() has no effect because we're
94 * still in 'store_rebuilding' state
96 if (e
->swap_filen
< 0)
99 if (opt_store_doublecheck
)
100 if (storeCleanupDoubleCheck(e
))
103 EBIT_SET(e
->flags
, ENTRY_VALIDATED
);
106 * Only set the file bit if we know its a valid entry
107 * otherwise, set it in the validation procedure
110 if ((++validated
& 0x3FFFF) == 0)
111 /* TODO format the int with with a stream operator */
112 debugs(20, DBG_IMPORTANT
, " " << validated
<< " Entries Validated so far.");
115 if (currentSearch
->isDone()) {
116 debugs(20, DBG_IMPORTANT
, " Completed Validation Procedure");
117 debugs(20, DBG_IMPORTANT
, " Validated " << validated
<< " Entries");
118 debugs(20, DBG_IMPORTANT
, " store_swap_size = " << Store::Root().currentSize() / 1024.0 << " KB");
119 --StoreController::store_dirs_rebuilding
;
120 assert(0 == StoreController::store_dirs_rebuilding
);
122 if (opt_store_doublecheck
&& store_errors
) {
123 fatalf("Quitting after finding %d cache index inconsistencies. " \
124 "Removing cache index will force its slow rebuild. " \
125 "Removing -S will let Squid start with an inconsistent " \
126 "cache index (at your own risk).\n", store_errors
);
130 storeDigestNoteStoreReady();
132 currentSearch
= NULL
;
134 eventAdd("storeCleanup", storeCleanup
, NULL
, 0.0, 1);
137 /* meta data recreated from disk image in swap directory */
140 storeRebuildComplete(StoreRebuildData
*dc
)
143 counts
.objcount
+= dc
->objcount
;
144 counts
.expcount
+= dc
->expcount
;
145 counts
.scancount
+= dc
->scancount
;
146 counts
.clashcount
+= dc
->clashcount
;
147 counts
.dupcount
+= dc
->dupcount
;
148 counts
.cancelcount
+= dc
->cancelcount
;
149 counts
.invalid
+= dc
->invalid
;
150 counts
.badflags
+= dc
->badflags
;
151 counts
.bad_log_op
+= dc
->bad_log_op
;
152 counts
.zero_object_sz
+= dc
->zero_object_sz
;
154 * When store_dirs_rebuilding == 1, it means we are done reading
155 * or scanning all cache_dirs. Now report the stats and start
156 * the validation (storeCleanup()) thread.
159 if (StoreController::store_dirs_rebuilding
> 1)
162 dt
= tvSubDsec(rebuild_start
, current_time
);
164 debugs(20, DBG_IMPORTANT
, "Finished rebuilding storage from disk.");
165 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << counts
.scancount
<< " Entries scanned");
166 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << counts
.invalid
<< " Invalid entries.");
167 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << counts
.badflags
<< " With invalid flags.");
168 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << counts
.objcount
<< " Objects loaded.");
169 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << counts
.expcount
<< " Objects expired.");
170 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << counts
.cancelcount
<< " Objects cancelled.");
171 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << counts
.dupcount
<< " Duplicate URLs purged.");
172 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << counts
.clashcount
<< " Swapfile clashes avoided.");
173 debugs(20, DBG_IMPORTANT
, " Took "<< std::setw(3)<< std::setprecision(2) << dt
<< " seconds ("<< std::setw(6) <<
174 ((double) counts
.objcount
/ (dt
> 0.0 ? dt
: 1.0)) << " objects/sec).");
175 debugs(20, DBG_IMPORTANT
, "Beginning Validation Procedure");
177 eventAdd("storeCleanup", storeCleanup
, NULL
, 0.0, 1);
179 xfree(RebuildProgress
);
181 RebuildProgress
= NULL
;
185 * this is ugly. We don't actually start any rebuild threads here,
186 * but only initialize counters, etc. The rebuild threads are
187 * actually started by the filesystem "fooDirInit" function.
190 storeRebuildStart(void)
192 memset(&counts
, '\0', sizeof(counts
));
193 rebuild_start
= current_time
;
195 * Note: store_dirs_rebuilding is initialized to 1.
197 * When we parse the configuration and construct each swap dir,
198 * the construction of that raises the rebuild count.
200 * This prevents us from trying to write clean logs until we
201 * finished rebuilding - including after a reconfiguration that opens an
202 * existing swapdir. The corresponding decrement * occurs in
203 * storeCleanup(), when it is finished.
205 RebuildProgress
= (store_rebuild_progress
*)xcalloc(Config
.cacheSwap
.n_configured
,
206 sizeof(store_rebuild_progress
));
210 * A fs-specific rebuild procedure periodically reports its
214 storeRebuildProgress(int sd_index
, int total
, int sofar
)
216 static time_t last_report
= 0;
223 if (sd_index
>= Config
.cacheSwap
.n_configured
)
226 if (NULL
== RebuildProgress
)
229 RebuildProgress
[sd_index
].total
= total
;
231 RebuildProgress
[sd_index
].scanned
= sofar
;
233 if (squid_curtime
- last_report
< 15)
236 for (sd_index
= 0; sd_index
< Config
.cacheSwap
.n_configured
; ++sd_index
) {
237 n
+= (double) RebuildProgress
[sd_index
].scanned
;
238 d
+= (double) RebuildProgress
[sd_index
].total
;
241 debugs(20, DBG_IMPORTANT
, "Store rebuilding is "<< std::setw(4)<< std::setprecision(2) << 100.0 * n
/ d
<< "% complete");
242 last_report
= squid_curtime
;
246 #include "StoreMetaUnpacker.h"
247 #include "StoreMeta.h"
250 struct InitStoreEntry
: public unary_function
<StoreMeta
, void> {
251 InitStoreEntry(StoreEntry
*anEntry
, cache_key
*aKey
):what(anEntry
),index(aKey
) {}
253 void operator()(StoreMeta
const &x
) {
254 switch (x
.getType()) {
257 assert(x
.length
== SQUID_MD5_DIGEST_LENGTH
);
258 memcpy(index
, x
.value
, SQUID_MD5_DIGEST_LENGTH
);
271 tmp
= (struct old_metahdr
*)x
.value
;
272 assert(x
.length
== STORE_HDR_METASIZE_OLD
);
273 what
->timestamp
= tmp
->timestamp
;
274 what
->lastref
= tmp
->lastref
;
275 what
->expires
= tmp
->expires
;
276 what
->lastmod
= tmp
->lastmod
;
277 what
->swap_file_sz
= tmp
->swap_file_sz
;
278 what
->refcount
= tmp
->refcount
;
279 what
->flags
= tmp
->flags
;
282 case STORE_META_STD_LFS
:
283 assert(x
.length
== STORE_HDR_METASIZE
);
284 memcpy(&what
->timestamp
, x
.value
, STORE_HDR_METASIZE
);
297 storeRebuildLoadEntry(int fd
, int diskIndex
, MemBuf
&buf
,
298 StoreRebuildData
&counts
)
303 assert(buf
.hasSpace()); // caller must allocate
305 const int len
= FD_READ_METHOD(fd
, buf
.space(), buf
.spaceSize());
306 ++ statCounter
.syscalls
.disk
.reads
;
308 const int xerrno
= errno
;
309 debugs(47, DBG_IMPORTANT
, "WARNING: cache_dir[" << diskIndex
<< "]: " <<
310 "Ignoring cached entry after meta data read failure: " << xstrerr(xerrno
));
319 storeRebuildParseEntry(MemBuf
&buf
, StoreEntry
&tmpe
, cache_key
*key
,
320 StoreRebuildData
&counts
,
321 uint64_t expectedSize
)
323 int swap_hdr_len
= 0;
324 StoreMetaUnpacker
aBuilder(buf
.content(), buf
.contentSize(), &swap_hdr_len
);
325 if (aBuilder
.isBufferZero()) {
326 debugs(47,5, HERE
<< "skipping empty record.");
330 if (!aBuilder
.isBufferSane()) {
331 debugs(47, DBG_IMPORTANT
, "WARNING: Ignoring malformed cache entry.");
335 StoreMeta
*tlv_list
= aBuilder
.createStoreMeta();
337 debugs(47, DBG_IMPORTANT
, "WARNING: Ignoring cache entry with invalid " <<
342 // TODO: consume parsed metadata?
344 debugs(47,7, HERE
<< "successful swap meta unpacking");
345 memset(key
, '\0', SQUID_MD5_DIGEST_LENGTH
);
347 InitStoreEntry
visitor(&tmpe
, key
);
348 for_each(*tlv_list
, visitor
);
349 storeSwapTLVFree(tlv_list
);
352 if (storeKeyNull(key
)) {
353 debugs(47, DBG_IMPORTANT
, "WARNING: Ignoring keyless cache entry");
360 if (expectedSize
> 0) {
361 if (tmpe
.swap_file_sz
== 0) {
362 tmpe
.swap_file_sz
= expectedSize
;
363 } else if (tmpe
.swap_file_sz
== (uint64_t)(expectedSize
- swap_hdr_len
)) {
364 tmpe
.swap_file_sz
= expectedSize
;
365 } else if (tmpe
.swap_file_sz
!= expectedSize
) {
366 debugs(47, DBG_IMPORTANT
, "WARNING: Ignoring cache entry due to a " <<
367 "SIZE MISMATCH " << tmpe
.swap_file_sz
<< "!=" << expectedSize
);
370 } else if (tmpe
.swap_file_sz
<= 0) {
371 debugs(47, DBG_IMPORTANT
, "WARNING: Ignoring cache entry with " <<
372 "unknown size: " << tmpe
);
376 if (EBIT_TEST(tmpe
.flags
, KEY_PRIVATE
)) {
385 storeRebuildKeepEntry(const StoreEntry
&tmpe
, const cache_key
*key
,
386 StoreRebuildData
&counts
)
388 /* this needs to become
390 * 2) make synthetic request with headers ?? or otherwise search
391 * for a matching object in the store
392 * TODO FIXME change to new async api
393 * TODO FIXME I think there is a race condition here with the
395 * store A reads in object foo, searchs for it, and finds nothing.
396 * store B reads in object foo, searchs for it, finds nothing.
397 * store A gets called back with nothing, so registers the object
398 * store B gets called back with nothing, so registers the object,
399 * which will conflict when the in core index gets around to scanning
402 * this suggests that rather than searching for duplicates, the
403 * index rebuild should just assume its the most recent accurate
404 * store entry and whoever indexes the stores handles duplicates.
406 if (StoreEntry
*e
= Store::Root().get(key
)) {
408 if (e
->lastref
>= tmpe
.lastref
) {
409 /* key already exists, old entry is newer */
410 /* keep old, ignore new */
413 // For some stores, get() creates/unpacks a store entry. Signal
414 // such stores that we will no longer use the get() result:
420 /* URL already exists, this swapfile not being used */
421 /* junk old, load new */
422 e
->release(); /* release old entry */