5 * DEBUG: section 20 Store Rebuild Routines
6 * AUTHOR: Duane Wessels
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
40 #include "StoreSearch.h"
41 #include "SquidTime.h"
43 static struct _store_rebuild_data counts
;
45 static struct timeval rebuild_start
;
46 static void storeCleanup(void *);
49 /* total number of "swap.state" entries that will be read */
51 /* number of entries read so far */
53 } store_rebuild_progress
;
55 static store_rebuild_progress
*RebuildProgress
= NULL
;
58 storeCleanupDoubleCheck(StoreEntry
* e
)
60 SwapDir
*SD
= dynamic_cast<SwapDir
*>(INDEXSD(e
->swap_dirn
));
61 return (SD
->doubleCheck(*e
));
65 storeCleanup(void *datanotused
)
67 static int store_errors
= 0;
68 static StoreSearchPointer currentSearch
;
69 static int validated
= 0;
71 if (currentSearch
== NULL
|| currentSearch
->isDone())
72 currentSearch
= Store::Root().search(NULL
, NULL
);
74 size_t statCount
= 500;
76 while (statCount
-- && !currentSearch
->isDone() && currentSearch
->next()) {
79 e
= currentSearch
->currentItem();
81 if (EBIT_TEST(e
->flags
, ENTRY_VALIDATED
))
85 * Calling StoreEntry->release() has no effect because we're
86 * still in 'store_rebuilding' state
88 if (e
->swap_filen
< 0)
91 if (opt_store_doublecheck
)
92 if (storeCleanupDoubleCheck(e
))
95 EBIT_SET(e
->flags
, ENTRY_VALIDATED
);
98 * Only set the file bit if we know its a valid entry
99 * otherwise, set it in the validation procedure
102 if ((++validated
& 0x3FFFF) == 0)
103 /* TODO format the int with with a stream operator */
104 debugs(20, 1, " " << validated
<< " Entries Validated so far.");
107 if (currentSearch
->isDone()) {
108 debugs(20, 1, " Completed Validation Procedure");
109 debugs(20, 1, " Validated " << validated
<< " Entries");
110 debugs(20, 1, " store_swap_size = " << Store::Root().currentSize() / 1024.0 << " KB");
111 StoreController::store_dirs_rebuilding
--;
112 assert(0 == StoreController::store_dirs_rebuilding
);
114 if (opt_store_doublecheck
)
115 assert(store_errors
== 0);
118 storeDigestNoteStoreReady();
120 currentSearch
= NULL
;
122 eventAdd("storeCleanup", storeCleanup
, NULL
, 0.0, 1);
125 /* meta data recreated from disk image in swap directory */
128 storeRebuildComplete(struct _store_rebuild_data
*dc
)
131 counts
.objcount
+= dc
->objcount
;
132 counts
.expcount
+= dc
->expcount
;
133 counts
.scancount
+= dc
->scancount
;
134 counts
.clashcount
+= dc
->clashcount
;
135 counts
.dupcount
+= dc
->dupcount
;
136 counts
.cancelcount
+= dc
->cancelcount
;
137 counts
.invalid
+= dc
->invalid
;
138 counts
.badflags
+= dc
->badflags
;
139 counts
.bad_log_op
+= dc
->bad_log_op
;
140 counts
.zero_object_sz
+= dc
->zero_object_sz
;
142 * When store_dirs_rebuilding == 1, it means we are done reading
143 * or scanning all cache_dirs. Now report the stats and start
144 * the validation (storeCleanup()) thread.
147 if (StoreController::store_dirs_rebuilding
> 1)
150 dt
= tvSubDsec(rebuild_start
, current_time
);
152 debugs(20, 1, "Finished rebuilding storage from disk.");
153 debugs(20, 1, " " << std::setw(7) << counts
.scancount
<< " Entries scanned");
154 debugs(20, 1, " " << std::setw(7) << counts
.invalid
<< " Invalid entries.");
155 debugs(20, 1, " " << std::setw(7) << counts
.badflags
<< " With invalid flags.");
156 debugs(20, 1, " " << std::setw(7) << counts
.objcount
<< " Objects loaded.");
157 debugs(20, 1, " " << std::setw(7) << counts
.expcount
<< " Objects expired.");
158 debugs(20, 1, " " << std::setw(7) << counts
.cancelcount
<< " Objects cancelled.");
159 debugs(20, 1, " " << std::setw(7) << counts
.dupcount
<< " Duplicate URLs purged.");
160 debugs(20, 1, " " << std::setw(7) << counts
.clashcount
<< " Swapfile clashes avoided.");
161 debugs(20, 1, " Took "<< std::setw(3)<< std::setprecision(2) << dt
<< " seconds ("<< std::setw(6) <<
162 ((double) counts
.objcount
/ (dt
> 0.0 ? dt
: 1.0)) << " objects/sec).");
163 debugs(20, 1, "Beginning Validation Procedure");
165 eventAdd("storeCleanup", storeCleanup
, NULL
, 0.0, 1);
167 xfree(RebuildProgress
);
169 RebuildProgress
= NULL
;
173 * this is ugly. We don't actually start any rebuild threads here,
174 * but only initialize counters, etc. The rebuild threads are
175 * actually started by the filesystem "fooDirInit" function.
178 storeRebuildStart(void)
180 memset(&counts
, '\0', sizeof(counts
));
181 rebuild_start
= current_time
;
183 * Note: store_dirs_rebuilding is initialized to 1.
185 * When we parse the configuration and construct each swap dir,
186 * the construction of that raises the rebuild count.
188 * This prevents us from trying to write clean logs until we
189 * finished rebuilding - including after a reconfiguration that opens an
190 * existing swapdir. The corresponding decrement * occurs in
191 * storeCleanup(), when it is finished.
193 RebuildProgress
= (store_rebuild_progress
*)xcalloc(Config
.cacheSwap
.n_configured
,
194 sizeof(store_rebuild_progress
));
198 * A fs-specific rebuild procedure periodically reports its
202 storeRebuildProgress(int sd_index
, int total
, int sofar
)
204 static time_t last_report
= 0;
211 if (sd_index
>= Config
.cacheSwap
.n_configured
)
214 if (NULL
== RebuildProgress
)
217 RebuildProgress
[sd_index
].total
= total
;
219 RebuildProgress
[sd_index
].scanned
= sofar
;
221 if (squid_curtime
- last_report
< 15)
224 for (sd_index
= 0; sd_index
< Config
.cacheSwap
.n_configured
; sd_index
++) {
225 n
+= (double) RebuildProgress
[sd_index
].scanned
;
226 d
+= (double) RebuildProgress
[sd_index
].total
;
229 debugs(20, 1, "Store rebuilding is "<< std::setw(4)<< std::setprecision(2) << 100.0 * n
/ d
<< "% complete");
230 last_report
= squid_curtime
;
234 #include "StoreMetaUnpacker.h"
235 #include "StoreMeta.h"
238 struct InitStoreEntry
: public unary_function
<StoreMeta
, void> {
239 InitStoreEntry(StoreEntry
*anEntry
, cache_key
*aKey
):what(anEntry
),index(aKey
) {}
241 void operator()(StoreMeta
const &x
) {
242 switch (x
.getType()) {
245 assert(x
.length
== SQUID_MD5_DIGEST_LENGTH
);
246 memcpy(index
, x
.value
, SQUID_MD5_DIGEST_LENGTH
);
259 tmp
= (struct old_metahdr
*)x
.value
;
260 assert(x
.length
== STORE_HDR_METASIZE_OLD
);
261 what
->timestamp
= tmp
->timestamp
;
262 what
->lastref
= tmp
->lastref
;
263 what
->expires
= tmp
->expires
;
264 what
->lastmod
= tmp
->lastmod
;
265 what
->swap_file_sz
= tmp
->swap_file_sz
;
266 what
->refcount
= tmp
->refcount
;
267 what
->flags
= tmp
->flags
;
270 case STORE_META_STD_LFS
:
271 assert(x
.length
== STORE_HDR_METASIZE
);
272 memcpy(&what
->timestamp
, x
.value
, STORE_HDR_METASIZE
);
285 storeRebuildLoadEntry(int fd
, int diskIndex
, MemBuf
&buf
,
286 struct _store_rebuild_data
&counts
)
291 assert(buf
.hasSpace()); // caller must allocate
293 const int len
= FD_READ_METHOD(fd
, buf
.space(), buf
.spaceSize());
294 statCounter
.syscalls
.disk
.reads
++;
296 const int xerrno
= errno
;
297 debugs(47, 1, "cache_dir[" << diskIndex
<< "]: " <<
298 "failed to read swap entry meta data: " << xstrerr(xerrno
));
307 storeRebuildParseEntry(MemBuf
&buf
, StoreEntry
&tmpe
, cache_key
*key
,
308 struct _store_rebuild_data
&counts
,
309 uint64_t expectedSize
)
311 int swap_hdr_len
= 0;
312 StoreMetaUnpacker
aBuilder(buf
.content(), buf
.contentSize(), &swap_hdr_len
);
313 if (aBuilder
.isBufferZero()) {
314 debugs(47,5, HERE
<< "skipping empty record.");
318 if (!aBuilder
.isBufferSane()) {
319 debugs(47,1, "Warning: Ignoring malformed cache entry.");
323 StoreMeta
*tlv_list
= aBuilder
.createStoreMeta();
325 debugs(47, 1, HERE
<< "failed to get swap entry meta data list");
329 // TODO: consume parsed metadata?
331 debugs(47,7, HERE
<< "successful swap meta unpacking");
332 memset(key
, '\0', SQUID_MD5_DIGEST_LENGTH
);
334 InitStoreEntry
visitor(&tmpe
, key
);
335 for_each(*tlv_list
, visitor
);
336 storeSwapTLVFree(tlv_list
);
339 if (storeKeyNull(key
)) {
340 debugs(47,1, HERE
<< "NULL swap entry key");
347 if (expectedSize
> 0) {
348 if (tmpe
.swap_file_sz
== 0) {
349 tmpe
.swap_file_sz
= expectedSize
;
350 } else if (tmpe
.swap_file_sz
== (uint64_t)(expectedSize
- swap_hdr_len
)) {
351 tmpe
.swap_file_sz
= expectedSize
;
352 } else if (tmpe
.swap_file_sz
!= expectedSize
) {
353 debugs(47, 1, HERE
<< "swap entry SIZE MISMATCH " <<
354 tmpe
.swap_file_sz
<< "!=" << expectedSize
);
358 if (tmpe
.swap_file_sz
<= 0) {
359 debugs(47, 1, HERE
<< "missing swap entry size: " << tmpe
);
363 if (EBIT_TEST(tmpe
.flags
, KEY_PRIVATE
)) {
372 storeRebuildKeepEntry(const StoreEntry
&tmpe
, const cache_key
*key
,
373 struct _store_rebuild_data
&counts
)
375 /* this needs to become
377 * 2) make synthetic request with headers ?? or otherwise search
378 * for a matching object in the store
379 * TODO FIXME change to new async api
380 * TODO FIXME I think there is a race condition here with the
382 * store A reads in object foo, searchs for it, and finds nothing.
383 * store B reads in object foo, searchs for it, finds nothing.
384 * store A gets called back with nothing, so registers the object
385 * store B gets called back with nothing, so registers the object,
386 * which will conflict when the in core index gets around to scanning
389 * this suggests that rather than searching for duplicates, the
390 * index rebuild should just assume its the most recent accurate
391 * store entry and whoever indexes the stores handles duplicates.
393 if (StoreEntry
*e
= Store::Root().get(key
)) {
395 if (e
->lastref
>= tmpe
.lastref
) {
396 /* key already exists, old entry is newer */
397 /* keep old, ignore new */
400 // For some stores, get() creates/unpacks a store entry. Signal
401 // such stores that we will no longer use the get() result:
407 /* URL already exists, this swapfile not being used */
408 /* junk old, load new */
409 e
->release(); /* release old entry */