-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use File.list and File.walk within a try with resource #5718
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,8 @@ | |
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import org.eclipse.jetty.util.ClassLoadingObjectInputStream; | ||
import org.eclipse.jetty.util.MultiException; | ||
|
@@ -206,21 +208,24 @@ public void sweepDisk() | |
long now = System.currentTimeMillis(); | ||
if (LOG.isDebugEnabled()) | ||
LOG.debug("Sweeping {} for old session files", _storeDir); | ||
try | ||
try (Stream<Path> stream = Files.walk(_storeDir.toPath(), 1, FileVisitOption.FOLLOW_LINKS)) | ||
{ | ||
Files.walk(_storeDir.toPath(), 1, FileVisitOption.FOLLOW_LINKS) | ||
stream | ||
.filter(p -> !Files.isDirectory(p)).filter(p -> !isOurContextSessionFilename(p.getFileName().toString())) | ||
.filter(p -> isSessionFilename(p.getFileName().toString())) | ||
.collect(Collectors.toList()) // Don't delete whilst walking | ||
.forEach(p -> | ||
{ | ||
|
||
try | ||
{ | ||
sweepFile(now, p); | ||
if (!Files.deleteIfExists(p)) | ||
LOG.warn("Could not delete {}", p.getFileName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general, I'm not sure we need to do the delete outside of the stream: there are plenty of examples if you do a google search where the recommended way to do a recursive directory delete is to use Files.walk/list and delete each file. I can't find any counter examples that says that is a bad idea. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK I have switched back. |
||
if (LOG.isDebugEnabled()) | ||
LOG.debug("Sweep deleted {}", p.getFileName()); | ||
} | ||
catch (Exception e) | ||
catch (IOException e) | ||
{ | ||
LOG.warn(e); | ||
LOG.warn("Could not delete {}", p.getFileName(), e); | ||
} | ||
}); | ||
} | ||
|
@@ -230,6 +235,24 @@ public void sweepDisk() | |
} | ||
} | ||
|
||
public boolean isExpired(long now, Path p) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see the benefit of splitting sweepFile() into isExpired() and then deleteFile() - it's more efficient just to use sweepFile(). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah now that we no longer collect before delete, there is none.... |
||
{ | ||
if (p == null) | ||
return false; | ||
try | ||
{ | ||
long expiry = getExpiryFromFilename(p.getFileName().toString()); | ||
//files with 0 expiry never expire | ||
return expiry > 0 && ((now - expiry) >= (5 * TimeUnit.SECONDS.toMillis(_gracePeriodSec))); | ||
} | ||
catch (NumberFormatException e) | ||
{ | ||
LOG.warn("Not valid session filename {}", p.getFileName()); | ||
LOG.warn(e); | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Check to see if the expiry on the file is very old, and | ||
* delete the file if so. "Old" means that it expired at least | ||
|
@@ -242,24 +265,12 @@ public void sweepDisk() | |
public void sweepFile(long now, Path p) | ||
throws Exception | ||
{ | ||
if (p == null) | ||
return; | ||
|
||
try | ||
if (isExpired(now, p)) | ||
{ | ||
long expiry = getExpiryFromFilename(p.getFileName().toString()); | ||
//files with 0 expiry never expire | ||
if (expiry > 0 && ((now - expiry) >= (5 * TimeUnit.SECONDS.toMillis(_gracePeriodSec)))) | ||
{ | ||
Files.deleteIfExists(p); | ||
if (LOG.isDebugEnabled()) | ||
LOG.debug("Sweep deleted {}", p.getFileName()); | ||
} | ||
} | ||
catch (NumberFormatException e) | ||
{ | ||
LOG.warn("Not valid session filename {}", p.getFileName()); | ||
LOG.warn(e); | ||
if (!Files.deleteIfExists(p)) | ||
LOG.warn("Could not delete {}", p.getFileName()); | ||
if (LOG.isDebugEnabled()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the debug output looking like the delete happened will be done even if the file wasn't actually deleted. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. factored out to a better common deleteFile method |
||
LOG.debug("Sweep deleted {}", p.getFileName()); | ||
} | ||
} | ||
|
||
|
@@ -365,70 +376,70 @@ public void initializeStore() | |
MultiException me = new MultiException(); | ||
long now = System.currentTimeMillis(); | ||
|
||
Files.walk(_storeDir.toPath(), 1, FileVisitOption.FOLLOW_LINKS) | ||
.filter(p -> !Files.isDirectory(p)).filter(p -> isSessionFilename(p.getFileName().toString())) | ||
.forEach(p -> | ||
{ | ||
//first get rid of all ancient files, regardless of which | ||
//context they are for | ||
try | ||
{ | ||
sweepFile(now, p); | ||
} | ||
catch (Exception x) | ||
{ | ||
me.add(x); | ||
} | ||
|
||
String filename = p.getFileName().toString(); | ||
String context = getContextFromFilename(filename); | ||
//now process it if it wasn't deleted, and it is for our context | ||
if (Files.exists(p) && _contextString.equals(context)) | ||
// first get rid of all ancient files, regardless of which | ||
// context they are for. Don't do in the following sweep to avoid | ||
// deleting whilst walking. | ||
sweepDisk(); | ||
|
||
// Build session file map by walking directory | ||
try (Stream<Path> stream = Files.walk(_storeDir.toPath(), 1, FileVisitOption.FOLLOW_LINKS)) | ||
{ | ||
stream | ||
.filter(p -> !Files.isDirectory(p)) | ||
.filter(p -> isSessionFilename(p.getFileName().toString())) | ||
.forEach(p -> | ||
{ | ||
//the session is for our context, populate the map with it | ||
String sessionIdWithContext = getIdWithContextFromFilename(filename); | ||
if (sessionIdWithContext != null) | ||
String filename = p.getFileName().toString(); | ||
String context = getContextFromFilename(filename); | ||
//now process it if it wasn't deleted, and it is for our context | ||
if (Files.exists(p) && _contextString.equals(context)) | ||
{ | ||
//handle multiple session files existing for the same session: remove all | ||
//but the file with the most recent expiry time | ||
String existing = _sessionFileMap.putIfAbsent(sessionIdWithContext, filename); | ||
if (existing != null) | ||
//the session is for our context, populate the map with it | ||
String sessionIdWithContext = getIdWithContextFromFilename(filename); | ||
if (sessionIdWithContext != null) | ||
{ | ||
//if there was a prior filename, work out which has the most | ||
//recent modify time | ||
try | ||
//handle multiple session files existing for the same session: remove all | ||
//but the file with the most recent expiry time | ||
String existing = _sessionFileMap.putIfAbsent(sessionIdWithContext, filename); | ||
if (existing != null) | ||
{ | ||
long existingExpiry = getExpiryFromFilename(existing); | ||
long thisExpiry = getExpiryFromFilename(filename); | ||
|
||
if (thisExpiry > existingExpiry) | ||
//if there was a prior filename, work out which has the most | ||
//recent modify time | ||
try | ||
{ | ||
//replace with more recent file | ||
Path existingPath = _storeDir.toPath().resolve(existing); | ||
//update the file we're keeping | ||
_sessionFileMap.put(sessionIdWithContext, filename); | ||
//delete the old file | ||
Files.delete(existingPath); | ||
if (LOG.isDebugEnabled()) | ||
LOG.debug("Replaced {} with {}", existing, filename); | ||
long existingExpiry = getExpiryFromFilename(existing); | ||
long thisExpiry = getExpiryFromFilename(filename); | ||
|
||
if (thisExpiry > existingExpiry) | ||
{ | ||
//replace with more recent file | ||
Path existingPath = _storeDir.toPath().resolve(existing); | ||
//update the file we're keeping | ||
_sessionFileMap.put(sessionIdWithContext, filename); | ||
//delete the old file | ||
Files.delete(existingPath); | ||
janbartel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (LOG.isDebugEnabled()) | ||
LOG.debug("Replaced {} with {}", existing, filename); | ||
} | ||
else | ||
{ | ||
//we found an older file, delete it | ||
Files.delete(p); | ||
if (LOG.isDebugEnabled()) | ||
LOG.debug("Deleted expired session file {}", filename); | ||
} | ||
} | ||
else | ||
catch (IOException e) | ||
{ | ||
//we found an older file, delete it | ||
Files.delete(p); | ||
if (LOG.isDebugEnabled()) | ||
LOG.debug("Deleted expired session file {}", filename); | ||
me.add(e); | ||
} | ||
} | ||
catch (IOException e) | ||
{ | ||
me.add(e); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
me.ifExceptionThrow(); | ||
}); | ||
me.ifExceptionThrow(); | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bzzzt. Do not pass go, do not collect $100. YOu can only delete it if it has expired!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oooops fixed