2 * DEBUG: section 47 Store Directory Routines
3 * AUTHOR: Robert Collins
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.
36 #include "RebuildState.h"
37 #include "SquidConfig.h"
38 #include "SquidTime.h"
39 #include "store_key_md5.h"
40 #include "store_rebuild.h"
41 #include "StoreSwapLogData.h"
43 #include "UFSSwapLogParser.h"
55 CBDATA_NAMESPACED_CLASS_INIT(Fs::Ufs
,RebuildState
);
57 Fs::Ufs::RebuildState::RebuildState(RefCount
<UFSSwapDir
> aSwapDir
) :
58 sd (aSwapDir
), LogParser(NULL
), e(NULL
), fromLog(true), _done (false)
61 * If the swap.state file exists in the cache_dir, then
62 * we'll use commonUfsDirRebuildFromSwapLog(), otherwise we'll
63 * use commonUfsDirRebuildFromDirectory() to open up each file
64 * and suck in the meta data.
67 int zeroLengthLog
= 0;
68 FILE *fp
= sd
->openTmpSwapLog(&clean
, &zeroLengthLog
);
70 if (fp
&& !zeroLengthLog
)
71 LogParser
= Fs::Ufs::UFSSwapLogParser::GetUFSSwapLogParser(fp
);
73 if (LogParser
== NULL
) {
81 flags
.clean
= (unsigned int) clean
;
85 flags
.need_to_validate
= 1;
87 debugs(47, DBG_IMPORTANT
, "Rebuilding storage in " << sd
->path
<< " (" <<
88 (clean
? "clean log" : (LogParser
? "dirty log" : "no log")) << ")");
91 Fs::Ufs::RebuildState::~RebuildState()
93 sd
->closeTmpSwapLog();
100 Fs::Ufs::RebuildState::RebuildStep(void *data
)
102 RebuildState
*rb
= (RebuildState
*)data
;
106 eventAdd("storeRebuild", RebuildStep
, rb
, 0.01, 1);
108 -- StoreController::store_dirs_rebuilding
;
109 storeRebuildComplete(&rb
->counts
);
114 /// load entries from swap.state or files until we run out of entries or time
116 Fs::Ufs::RebuildState::rebuildStep()
120 // Balance our desire to maximize the number of entries processed at once
121 // (and, hence, minimize overheads and total rebuild time) with a
122 // requirement to also process Coordinator events, disk I/Os, etc.
123 const int maxSpentMsec
= 50; // keep small: most RAM I/Os are under 1ms
124 const timeval loopStart
= current_time
;
126 const int totalEntries
= LogParser
? LogParser
->SwapLogEntries() : -1;
130 rebuildFromSwapLog();
132 rebuildFromDirectory();
134 // TODO: teach storeRebuildProgress to handle totalEntries <= 0
135 if (totalEntries
> 0 && (n_read
% 4000 == 0))
136 storeRebuildProgress(sd
->index
, totalEntries
, n_read
);
138 if (opt_foreground_rebuild
)
139 continue; // skip "few entries at a time" check below
142 const double elapsedMsec
= tvSubMsec(loopStart
, current_time
);
143 if (elapsedMsec
> maxSpentMsec
|| elapsedMsec
< 0) {
144 debugs(47, 5, HERE
<< "pausing after " << n_read
<< " entries in " <<
145 elapsedMsec
<< "ms; " << (elapsedMsec
/n_read
) << "ms per entry");
151 /// process one cache file
153 Fs::Ufs::RebuildState::rebuildFromDirectory()
155 cache_key key
[SQUID_MD5_DIGEST_LENGTH
];
159 assert(this != NULL
);
160 debugs(47, 3, HERE
<< "DIR #" << sd
->index
);
165 fd
= getNextFile(&filn
, &size
);
168 debugs(47, DBG_IMPORTANT
, "Done scanning " << sd
->path
<< " dir (" <<
169 n_read
<< " entries)");
177 /* lets get file stats here */
181 if (fstat(fd
, &sb
) < 0) {
182 debugs(47, DBG_IMPORTANT
, HERE
<< "fstat(FD " << fd
<< "): " << xstrerror());
184 --store_open_disk_fd
;
190 buf
.init(SM_PAGE_SIZE
, SM_PAGE_SIZE
);
191 if (!storeRebuildLoadEntry(fd
, sd
->index
, buf
, counts
))
195 const bool loaded
= storeRebuildParseEntry(buf
, tmpe
, key
, counts
,
196 (int64_t)sb
.st_size
);
199 --store_open_disk_fd
;
203 // XXX: shouldn't this be a call to commonUfsUnlink?
204 sd
->unlinkFile(filn
); // should we unlink in all failure cases?
208 if (!storeRebuildKeepEntry(tmpe
, key
, counts
))
213 currentEntry(sd
->addDiskRestore(key
,
220 tmpe
.refcount
, /* refcount */
221 tmpe
.flags
, /* flags */
223 storeDirSwapLog(currentEntry(), SWAP_LOG_ADD
);
227 Fs::Ufs::RebuildState::currentEntry() const
233 Fs::Ufs::RebuildState::currentEntry(StoreEntry
*newValue
)
238 /// process one swap log entry
240 Fs::Ufs::RebuildState::rebuildFromSwapLog()
242 StoreSwapLogData swapData
;
244 if (LogParser
->ReadRecord(swapData
) != 1) {
245 debugs(47, DBG_IMPORTANT
, "Done reading " << sd
->path
<< " swaplog (" << n_read
<< " entries)");
255 if (!swapData
.sane()) {
261 * BC: during 2.4 development, we changed the way swap file
262 * numbers are assigned and stored. The high 16 bits used
263 * to encode the SD index number. There used to be a call
264 * to storeDirProperFileno here that re-assigned the index
265 * bits. Now, for backwards compatibility, we just need
268 swapData
.swap_filen
&= 0x00FFFFFF;
270 debugs(47, 3, HERE
<< swap_log_op_str
[(int) swapData
.op
] << " " <<
271 storeKeyText(swapData
.key
) << " "<< std::setfill('0') <<
272 std::hex
<< std::uppercase
<< std::setw(8) <<
273 swapData
.swap_filen
);
275 if (swapData
.op
== SWAP_LOG_ADD
) {
277 } else if (swapData
.op
== SWAP_LOG_DEL
) {
278 /* Delete unless we already have a newer copy anywhere in any store */
279 /* this needs to become
281 * 2) make synthetic request with headers ?? or otherwise search
282 * for a matching object in the store
283 * TODO FIXME change to new async api
285 currentEntry (Store::Root().get(swapData
.key
));
287 if (currentEntry() != NULL
&& swapData
.lastref
>= e
->lastref
) {
290 ++counts
.cancelcount
;
295 x
= ::log(static_cast<double>(++counts
.bad_log_op
)) / ::log(10.0);
297 if (0.0 == x
- (double) (int) x
)
298 debugs(47, DBG_IMPORTANT
, "WARNING: " << counts
.bad_log_op
<< " invalid swap log entries found");
305 ++counts
.scancount
; // XXX: should not this be incremented earlier?
307 if (!sd
->validFileno(swapData
.swap_filen
, 0)) {
312 if (EBIT_TEST(swapData
.flags
, KEY_PRIVATE
)) {
317 /* this needs to become
319 * 2) make synthetic request with headers ?? or otherwise search
320 * for a matching object in the store
321 * TODO FIXME change to new async api
323 currentEntry (Store::Root().get(swapData
.key
));
325 int used
; /* is swapfile already in use? */
327 used
= sd
->mapBitTest(swapData
.swap_filen
);
329 /* If this URL already exists in the cache, does the swap log
330 * appear to have a newer entry? Compare 'lastref' from the
331 * swap log to e->lastref. */
332 /* is the log entry newer than current entry? */
333 int disk_entry_newer
= currentEntry() ? (swapData
.lastref
> currentEntry()->lastref
? 1 : 0) : 0;
335 if (used
&& !disk_entry_newer
) {
336 /* log entry is old, ignore it */
339 } else if (used
&& currentEntry() && currentEntry()->swap_filen
== swapData
.swap_filen
&& currentEntry()->swap_dirn
== sd
->index
) {
340 /* swapfile taken, same URL, newer, update meta */
342 if (currentEntry()->store_status
== STORE_OK
) {
343 currentEntry()->lastref
= swapData
.timestamp
;
344 currentEntry()->timestamp
= swapData
.timestamp
;
345 currentEntry()->expires
= swapData
.expires
;
346 currentEntry()->lastmod
= swapData
.lastmod
;
347 currentEntry()->flags
= swapData
.flags
;
348 currentEntry()->refcount
+= swapData
.refcount
;
349 sd
->dereference(*currentEntry());
351 debug_trap("commonUfsDirRebuildFromSwapLog: bad condition");
352 debugs(47, DBG_IMPORTANT
, HERE
<< "bad condition");
356 /* swapfile in use, not by this URL, log entry is newer */
357 /* This is sorta bad: the log entry should NOT be newer at this
358 * point. If the log is dirty, the filesize check should have
359 * caught this. If the log is clean, there should never be a
361 debugs(47, DBG_IMPORTANT
, "WARNING: newer swaplog entry for dirno " <<
362 sd
->index
<< ", fileno "<< std::setfill('0') << std::hex
<<
363 std::uppercase
<< std::setw(8) << swapData
.swap_filen
);
365 /* I'm tempted to remove the swapfile here just to be safe,
366 * but there is a bad race condition in the NOVM version if
367 * the swapfile has recently been opened for writing, but
368 * not yet opened for reading. Because we can't map
369 * swapfiles back to StoreEntrys, we don't know the state
370 * of the entry using that file. */
371 /* We'll assume the existing entry is valid, probably because
372 * were in a slow rebuild and the the swap file number got taken
373 * and the validation procedure hasn't run. */
374 assert(flags
.need_to_validate
);
377 } else if (currentEntry() && !disk_entry_newer
) {
378 /* key already exists, current entry is newer */
379 /* keep old, ignore new */
382 } else if (currentEntry()) {
383 /* key already exists, this swapfile not being used */
384 /* junk old, load new */
389 /* URL doesnt exist, swapfile not in use */
396 currentEntry(sd
->addDiskRestore(swapData
.key
,
398 swapData
.swap_file_sz
,
407 storeDirSwapLog(currentEntry(), SWAP_LOG_ADD
);
410 /// undo the effects of adding an entry in rebuildFromSwapLog()
412 Fs::Ufs::RebuildState::undoAdd()
414 StoreEntry
*added
= currentEntry();
418 // TODO: Why bother with these two if we are going to release?!
420 added
->releaseRequest();
422 if (added
->swap_filen
> -1) {
423 UFSSwapDir
*sde
= dynamic_cast<UFSSwapDir
*>(INDEXSD(added
->swap_dirn
));
425 sde
->undoAddDiskRestore(added
);
432 Fs::Ufs::RebuildState::getNextFile(sfileno
* filn_p
, int *size
)
436 debugs(47, 3, HERE
<< "flag=" << flags
.init
<< ", " <<
437 sd
->index
<< ": /"<< std::setfill('0') << std::hex
<<
438 std::uppercase
<< std::setw(2) << curlvl1
<< "/" << std::setw(2) <<
444 while (fd
< 0 && done
== 0) {
447 if (0 == flags
.init
) { /* initialize, open first file */
453 assert(Config
.cacheSwap
.n_configured
> 0);
456 if (0 == in_dir
) { /* we need to read in a new directory */
457 snprintf(fullpath
, MAXPATHLEN
, "%s/%02X/%02X",
464 td
= opendir(fullpath
);
469 debugs(47, DBG_IMPORTANT
, HERE
<< "error in opendir (" << fullpath
<< "): " << xstrerror());
471 entry
= readdir(td
); /* skip . and .. */
474 if (entry
== NULL
&& errno
== ENOENT
)
475 debugs(47, DBG_IMPORTANT
, HERE
<< "WARNING: directory does not exist!");
476 debugs(47, 3, HERE
<< "Directory " << fullpath
);
480 if (td
!= NULL
&& (entry
= readdir(td
)) != NULL
) {
483 if (sscanf(entry
->d_name
, "%x", &fn
) != 1) {
484 debugs(47, 3, HERE
<< "invalid entry " << entry
->d_name
);
488 if (!UFSSwapDir::FilenoBelongsHere(fn
, sd
->index
, curlvl1
, curlvl2
)) {
489 debugs(47, 3, HERE
<< std::setfill('0') <<
490 std::hex
<< std::uppercase
<< std::setw(8) << fn
<<
491 " does not belong in " << std::dec
<< sd
->index
<< "/" <<
492 curlvl1
<< "/" << curlvl2
);
497 if (sd
->mapBitTest(fn
)) {
498 debugs(47, 3, HERE
<< "Locked, continuing with next.");
502 snprintf(fullfilename
, MAXPATHLEN
, "%s/%s",
503 fullpath
, entry
->d_name
);
504 debugs(47, 3, HERE
<< "Opening " << fullfilename
);
505 fd
= file_open(fullfilename
, O_RDONLY
| O_BINARY
);
508 debugs(47, DBG_IMPORTANT
, HERE
<< "error opening " << fullfilename
<< ": " << xstrerror());
510 ++store_open_disk_fd
;
522 if (sd
->validL2(++curlvl2
))
527 if (sd
->validL1(++curlvl1
))
540 Fs::Ufs::RebuildState::error() const
546 Fs::Ufs::RebuildState::isDone() const
552 Fs::Ufs::RebuildState::currentItem()
554 return currentEntry();